PHP7 fix
[Pman.Core] / UpdateDatabase.php
index 4881108..d3ec063 100644 (file)
@@ -67,12 +67,24 @@ class Pman_Core_UpdateDatabase extends Pman
             'min' => 1,
             'max' => 1,
         ),
+        'skip-email-import' => array(
+            'desc' => 'Skip email import',
+            'default' => '',
+            'min' => 1,
+            'max' => 1,
+        ),
         'procedures-only' => array(
             'desc' => 'Only import procedures (not supported by most modules yet) - ignores sql directory',
             'default' => '',
             'min' => 1,
             'max' => 1,
         ),
+        'disable-create-triggers' => array(
+            'desc' => 'So not create the mysql triggers',
+            'default' => '',
+            'min' => 1,
+            'max' => 1,
+        ),
         
         'json-person' => array(
             'desc' => 'Person JSON file',
@@ -90,7 +102,7 @@ class Pman_Core_UpdateDatabase extends Pman
             
         ),
     );
-    
+  
     static function cli_opts()
     {
         
@@ -113,7 +125,6 @@ class Pman_Core_UpdateDatabase extends Pman
                   
                 $ret = array_merge($ret, $cls->getStaticPropertyValue('cli_opts'));
             }
-            
         }
         
         return $ret;
@@ -127,6 +138,32 @@ class Pman_Core_UpdateDatabase extends Pman
     
     var $local_base_url = false;
     
+    var $emailTemplates = array(
+        'EVENT_ERRORS_REPORT' => array(
+            'bcc_group' => 'Empty Group',
+            'test_class' => 'Pman/Admin/Report/SendEventErrors',
+            'to_group' => 'Administrators',
+            'active' => 1,
+            'description' => '9.2 System Error Messages',
+            'template_dir' => '/Pman/Admin/templates/mail/'
+        ),
+         'ADMIN_PASSWORD_RESET' => array(
+            'bcc_group' => 'Administrators',
+            'test_class' => 'Pman/Core/DataObjects/Core_person',
+            'to_group' => '',
+            'active' => 1,
+            'description' => '9.1 Management System Password Reset',
+            'template_dir' => '/Pman/Core/templates/mail/'
+
+            
+        )
+    );
+    
+    var $required_extensions = array(
+        'curl',
+        'gd'
+    );
+    
     function getAuth() {
         
         
@@ -147,11 +184,12 @@ class Pman_Core_UpdateDatabase extends Pman
     
     function get($args, $opts=array())
     {
-         
         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
    
         $this->checkSystem();
         
+        $this->verifyExtensions($this->required_extensions);
+        
         if (class_exists('PDO_DataObjects_Introspection')) {
             PDO_DataObject_Introspection::$cache = array();
         }
@@ -182,27 +220,49 @@ class Pman_Core_UpdateDatabase extends Pman
         }
         
         // ask all the modules to verify the opts
-        echo "Checi options\n";
+        echo "Checking options\n";
         $this->checkOpts($opts);
         
-          
+        $response = $this->curl("http://localhost{$this->local_base_url}/Core/UpdateDatabase/VerifyExtensions");
+        $json = json_decode($response, true);
+        
+        if(empty($json['data']) || $json['data'] != 'DONE'){
+            echo "\nError: Missing php extensions:\n";
+            print_r($response);
+            echo "Please install the above extensions and restart the apache.\n";
+            sleep(5);
+            
+            //exit;
+        }
+        
+        echo "Checking Setup Requirements\n";
+        require_once 'Pman/Core/UpdateDatabase/VerifyConfig.php';
+        $sq = new Pman_Core_UpdateDatabase_VerifyConfig();
+        $ret = $sq->get($args, $opts);
+        
+        if(!empty($ret)){
+            echo implode("\n", $ret) . "\n";
+            sleep(30);
+        }
         
         // do this first, so the innodb change + utf8 fixes column max sizes
         
         // this will trigger errors about freetext indexes - we will have to remove them manually.?
         // otherwise we need to do an sql query to find them, then remove them (not really worth it as it only affects really old code..)
-        
+        echo "Run extensions\n";
+
         $this->runExtensions(); 
 
         
         if (empty($opts['data-only'])) {
+            echo "Import SQL\n";
             $this->importSQL();
         }
         if (!empty($opts['only-module-sql'])) {
             return;
         }
         
-        
+        echo "run Update Modules Data\n";
 
         $this->runUpdateModulesData();
         
@@ -217,6 +277,12 @@ class Pman_Core_UpdateDatabase extends Pman
         $this->clearApacheDataobjectsCache();
         
         $this->clearApacheAssetCache();
+        
+        
+        
+        
+        
+        
     }
     
     function output() {
@@ -303,9 +369,10 @@ class Pman_Core_UpdateDatabase extends Pman
             // new -- sql directory..
             // new style will not support migrate ... they have to go into mysql-migrate.... directories..
             // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
+            $dbdir = $dbtype == 'mysqli' ? 'mysql' : $dbtype;
             
             $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/sql");
-            $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}");
+            $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbdir}");
             
            
             
@@ -781,16 +848,23 @@ class Pman_Core_UpdateDatabase extends Pman
             array(
                 'name' => 'bcc-email', // group who are bcc'ed on all requests.
                 'type' => 0, // system
+                'display_name' => 'Standard BCC Group'
             ),
             array(
                 'name' => 'system-email-from',
                 'type' => 0, // system
+                'display_name' => 'Standard System Email From Group'
             ),
             array(
                 'name' => 'core-person-signup-bcc',
                 'type' => 0, // system
+                'display_name' => 'Standard Person Signup BCC Group'
             ),
-        
+            array(
+                'name' => 'Empty Group', // use for no bcc emails.
+                'type' => 0,
+                'display_name' => 'Standard Empty Group'
+            )
 
         ));
         
@@ -803,17 +877,21 @@ class Pman_Core_UpdateDatabase extends Pman
         $c = DB_DataObject::Factory('core_company');
         $c->selectAdd();
         $c->selectAdd('distinct(comptype) as comptype');
-        $c->whereAdd("comptype != ''");
+        $c->whereAdd("
+                comptype != '' 
+            AND 
+                comptype != 'undefined' 
+            AND 
+                comptype != 'undefine'
+        ");
         
         $ctb = array();
         foreach($c->fetchAll('comptype') as $cts) {
             
-            
-            
-           $ctb[]= array( 'etype'=>'COMPTYPE', 'name' => $cts, 'display_name' => ucfirst(strtolower($cts)));
+            $ctb[]= array( 'etype'=>'COMPTYPE', 'name' => $cts, 'display_name' => ucfirst(strtolower($cts)));
         
         }
-         $c = DB_DataObject::Factory('core_enum');
+        $c = DB_DataObject::Factory('core_enum');
          
         $c->initEnums($ctb);
         //DB_DataObject::debugLevel(1);
@@ -835,6 +913,22 @@ class Pman_Core_UpdateDatabase extends Pman
         
     }
     
+    function updateDataEmails()
+    {
+        if (!empty($this->opts['skip-email-import'])) {
+            return;
+        }
+        foreach ($this->emailTemplates as $k => $mail) {
+            
+            $mail_dir = "{$this->rootDir}{$mail['template_dir']}";
+
+            $this->initEmails(
+                $mail_dir,
+                array($k => $mail),
+                false
+            );
+        }
+    }
     
     function initEmails($templateDir, $emails, $mapping = false)
     {
@@ -850,13 +944,14 @@ class Pman_Core_UpdateDatabase extends Pman
                 if (empty($data['bcc_group'])) {
                     $this->jerr("missing bcc_group for template $name");
                 }
+                
                 $g = DB_DataObject::Factory('core_group')->lookup('name',$data['bcc_group']);
                 
                 if (empty($g->id)) {
                     $this->jerr("bcc_group {$data['bcc_group']} does not exist when importing template $name");
                 }
                 
-                if (!$g->members('email')) {
+                if (!$g->members('email') && $g->name != 'Empty Group') {
                     $this->jerr("bcc_group {$data['bcc_group']} does not have any members");
                 }
                 
@@ -864,10 +959,11 @@ class Pman_Core_UpdateDatabase extends Pman
             }
             // initEmails will always have the latest location of the test class - in theory the user should not be changign the value of this...
             //if (empty($cm->test_class)) {
-                if (empty($data['test_class'])) {
-                    $this->jerr("missing test_class for template $name");
-                }
-                $cm->test_class = $data['test_class'];
+            if (empty($data['test_class'])) {
+                $this->jerr("missing test_class for template $name");
+            }
+            
+            $cm->test_class = $data['test_class'];
             //}
             if(isset($cm->to_group_id)) {
                 print_r('isset');
@@ -892,6 +988,14 @@ class Pman_Core_UpdateDatabase extends Pman
                 $cm->active = $data['active'];
             }
             
+            /*
+             * Set description to email.
+             * However we do not update if it is been set.
+             */
+            if(empty($cm->description) && !empty($data['description'])){
+                $cm->description = $cm->escape($data['description']);
+            }
+            
             require_once $cm->test_class . '.php';
             
             $clsname = str_replace('/','_', $cm->test_class);
@@ -943,17 +1047,14 @@ class Pman_Core_UpdateDatabase extends Pman
         }
         HTML_FlexyFramework::get()->generateDataobjectsCache(true);
         
-         
         $this->updateDataEnums();
         $this->updateDataGroups();
         $this->updateDataCompanies();
         
+        $this->updateDataEmails();
+        
         $c = DB_DataObject::Factory('I18n');
         $c->buildDB();
-         
-       
-        
-        
     }
     
     function fixMysqlInnodb()
@@ -996,7 +1097,9 @@ class Pman_Core_UpdateDatabase extends Pman
                     in_array('STRICT_TRANS_TABLES', $modes) || 
                     !in_array('ALLOW_INVALID_DATES', $modes)
             ){
-                die("Error: set sql_mode include 'ALLOW_INVALID_DATES', remove 'NO_ZERO_IN_DATE' AND 'STRICT_TRANS_TABLES' AND 'NO_ZERO_DATE' in my.cnf\n\n");
+                die("Error: set sql_mode include 'ALLOW_INVALID_DATES', remove 'NO_ZERO_IN_DATE' AND 'STRICT_TRANS_TABLES' AND 'NO_ZERO_DATE' in my.cnf\n\n".
+                    "Recommended line: \n\nsql_mode = ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,ALLOW_INVALID_DATES\n\n"
+                );
             }
         }
         
@@ -1114,15 +1217,18 @@ class Pman_Core_UpdateDatabase extends Pman
         $dburl = parse_url($ff->database);
         
         $dbtype = $dburl['scheme'];
-       
+        $dbtype  = ($dbtype == 'mysqli') ? 'mysql' : $dbtype;
+        
         foreach($this->extensions as $ext) {
        
             $scls = ucfirst($dbtype). $ext;
             $cls = __CLASS__ . '_'. $scls;
             $fn = implode('/',explode('_', $cls)).'.php';
+            
             if (!file_exists(__DIR__.'/UpdateDatabase/'. $scls .'.php')) {
                 return;
             }
+            echo "Running : {$fn}\n";
             require_once $fn;
             $c = new $cls();
             
@@ -1144,7 +1250,8 @@ class Pman_Core_UpdateDatabase extends Pman
             'pdftoppm',
             'rsvg-convert',  //librsvg2-bin
             'strings',
-            'oathtool'
+            'oathtool',
+            'gifsicle', // used for gif conversions
         );
          
          
@@ -1190,16 +1297,20 @@ class Pman_Core_UpdateDatabase extends Pman
     
     function clearApacheDataobjectsCache()
     {
-        echo "Clearing Database Cache\n";
+        
         // this needs to clear it's own cache along with remote one..
   
+        $url = "http://localhost{$this->local_base_url}/Core/RefreshDatabaseCache";
+        
+        echo "Clearing Database Cache : http://localhost{$this->local_base_url}/Core/RefreshDatabaseCache\n";
         
-        $response = $this->curl("http://localhost{$this->local_base_url}/Core/RefreshDatabaseCache");
+        $response = $this->curl($url);
         
         $json = json_decode($response, true);
         
         if(empty($json['data']) || $json['data'] != 'DONE'){
-            echo $response. "\n";
+            echo "fetching $url\n";
+            echo "GOT:" . $response. "\n";
             echo "Clear DataObjects Cache failed\n";
             exit;
         }
@@ -1209,7 +1320,7 @@ class Pman_Core_UpdateDatabase extends Pman
     
     function clearApacheAssetCache()
     {
-        echo "Clearing Asset Cache\n";
+        echo "Clearing Asset Cache : http://localhost{$this->local_base_url}/Core/Asset\n";
         $response = $this->curl(
             "http://localhost{$this->local_base_url}/Core/Asset",
             array( '_clear_cache' => 1 ,'returnHTML' => 'NO' ),
@@ -1260,9 +1371,25 @@ class Pman_Core_UpdateDatabase extends Pman
         return $response;
     }
     
-    function verifyExtensions($extensions)
+    static function verifyExtensions($extensions)
     {
+        $error = array();
+        
+        foreach ($extensions as $e){
+            
+            if(empty($e) || extension_loaded($e)) {
+                continue;
+            }
+            
+            $error[] = "Error: Please install php extension: {$e}";
+        }
+        
+        if(empty($error)){
+           return true; 
+        }
+        $ff = HTML_FLexyFramework::get();
         
+        $ff->page->jerr(implode('\n', $error));
     }
     
 }