Example #1
0
    protected function create_tables()
    {
        if (isset($this->argtoks['flags']['d'])) {
            ini_set('display_errors', 1);
            error_reporting(E_ALL);
        }
        if (!is_string($this->data['config']['db-pfx']) || '' === trim($this->data['config']['db-pfx'])) {
            $str = 'Please provide a table prefix!';
            $this->result->out = $str;
            $this->result->js .= 'alert("' . $str . '");';
            return;
        }
        $settings = array('driver' => $this->data['config']['db-driver'], 'host' => $this->data['config']['db-host'], 'dbname' => $this->data['config']['db-dbname'], 'user' => $this->data['config']['db-user'], 'password' => $this->data['config']['db-pwd'], 'pfx' => $this->data['config']['db-pfx']);
        $this->_db->Connect($settings, false);
        if (true !== $this->_db->connected) {
            $str = 'No database connection!';
            $this->result->out = $str;
            $this->result->js .= 'alert("' . $str . '");';
            return;
        }
        try {
            $S = new \frdl\_db();
            $cfile = $this->data['config']['FILES']['database-schema'];
            if (file_exists($cfile)) {
                $oldSchema = $S->load_schema(file_get_contents($cfile));
                //$oldSchema = $S->load_schema($this->read($cfile, 'rb',  null));
            } else {
                $oldSchema = $S->schema();
            }
            if (!is_object($oldSchema)) {
                $oldSchema = $S->schema();
            }
            $S->check($schema, $tables, null, true, true, true, $this->_db, $settings, $oldSchema);
            $this->result->js .= '
            	$(document).wdPostbox(\'deleteMessage\', \'system-error-database-missing-or-obsolete-tables-' . $oldSchema->version . '\',  \'update\', false);	 	 
            	$(document).wdPostbox(\'deleteMessage\', \'system-error-database-missing-or-obsolete-tables-' . $schema->version . '\',  \'update\', false);	 	   	
					 	   
          ';
        } catch (\Exception $e) {
            $tablesOK = false;
            $this->result->out = 'Error checking tables! ' . $e->getMessage();
            return $this->result;
        }
        $S->tables($tables, false);
        $report = $S->check_tables($schema, $tables, $schema);
        file_put_contents($cfile, $S->save_schema($schema, 128));
        $this->data['config']['db-schema-version'] = $schema->version;
        if (true !== $this->writeToConfigFile()) {
            \webdof\wResponse::status(409);
            $this->result->out = 'set config: ERROR writing config file';
        } else {
            $this->result->out = $report;
        }
        return $this->result;
    }
Example #2
0
 protected function _test_tables()
 {
     if (true !== $this->loadConfigFromFile(true)) {
         $this->result->out = 'config ERROR: cannot readf config file';
         return;
     }
     try {
         $_s = new \frdl\o();
         $tables = array();
         $S = new \frdl\_db();
         $db = new \frdl\DB(array('driver' => $this->data['config']['db-driver'], 'host' => $this->data['config']['db-host'], 'dbname' => $this->data['config']['db-dbname'], 'user' => $this->data['config']['db-user'], 'password' => $this->data['config']['db-pwd'], 'pfx' => $this->data['config']['db-pfx']), true);
         $S->check($_s, $tables, null, true, false, false, $db, array('driver' => $this->data['config']['db-driver'], 'host' => $this->data['config']['db-host'], 'dbname' => $this->data['config']['db-dbname'], 'user' => $this->data['config']['db-user'], 'password' => $this->data['config']['db-pwd'], 'pfx' => $this->data['config']['db-pfx']));
         $this->result->schema = $_s;
         $this->result->tables = $tables;
         $this->result->code = 200;
         $this->result->out = 'Ok';
     } catch (\Exception $e) {
         die($e->getMessage());
     }
     return $this->result;
 }
Example #3
0
File: html.php Project: frdl/webfan
 protected function item_db()
 {
     $tab = 'window_main_frdl-webfan-db';
     if (true !== $this->check($html, $tab, true, false)) {
         return $html;
     }
     $this->_check_db();
     $html = '';
     $html .= '<div id="' . $tab . '" class="wd-tab">';
     $html .= '<table style="width:100%;vertical-align:top;">';
     $html .= '<tr>';
     $html .= '<td style="width:50%;">';
     $html .= '<h2 class="webfan-blue"><span>Database</span> <span>Settings</span></h2>';
     $html .= '<div>';
     $html .= '<legend>Driver</legend>';
     $html .= '<input type="text" id="db-driver" value="' . (isset($this->data['config']['db-driver']) ? $this->data['config']['db-driver'] : '') . '" />';
     $html .= '<button onclick="$.WebfanDesktop.Registry.Programs[\'frdl-webfan\'].cnf(\'db-driver\', Dom.g(\'db-driver\').value,null,$.WebfanDesktop.Registry.Programs[\'frdl-webfan\'].formConfig, true);$.WebfanDesktop.Registry.Programs[\'frdl-webfan\'].html(\'db\');">change</button> (e.g. mysql)';
     $html .= '</div>';
     $html .= '<div>';
     $html .= '<legend>Host</legend>';
     $html .= '<input type="text" id="db-host" value="' . (isset($this->data['config']['db-host']) ? $this->data['config']['db-host'] : '') . '" />';
     $html .= '<button onclick="$.WebfanDesktop.Registry.Programs[\'frdl-webfan\'].cnf(\'db-host\', Dom.g(\'db-host\').value,null,$.WebfanDesktop.Registry.Programs[\'frdl-webfan\'].formConfig, true);$.WebfanDesktop.Registry.Programs[\'frdl-webfan\'].html(\'db\');">change</button> (e.g. localhost)';
     $html .= '</div>';
     $html .= '<div>';
     $html .= '<legend>User</legend>';
     $html .= '<input type="text" id="db-user" value="' . (isset($this->data['config']['db-user']) ? $this->data['config']['db-user'] : '') . '" />';
     $html .= '<button onclick="$.WebfanDesktop.Registry.Programs[\'frdl-webfan\'].cnf(\'db-user\', Dom.g(\'db-user\').value,null,$.WebfanDesktop.Registry.Programs[\'frdl-webfan\'].formConfig, true);$.WebfanDesktop.Registry.Programs[\'frdl-webfan\'].html(\'db\');">change</button>';
     $html .= '</div>';
     $html .= '<div>';
     $html .= '<legend>Database</legend>';
     $html .= '<input type="text" id="db-dbname" value="' . (isset($this->data['config']['db-dbname']) ? $this->data['config']['db-dbname'] : '') . '" />';
     $html .= '<button onclick="$.WebfanDesktop.Registry.Programs[\'frdl-webfan\'].cnf(\'db-dbname\', Dom.g(\'db-dbname\').value,null,$.WebfanDesktop.Registry.Programs[\'frdl-webfan\'].formConfig, true);$.WebfanDesktop.Registry.Programs[\'frdl-webfan\'].html(\'db\');">change</button>';
     $html .= '</div>';
     $html .= '<div>';
     $html .= '<legend>Password</legend>';
     $html .= '<input type="password" id="db-pwd" value="' . (isset($this->data['config']['db-pwd']) ? $this->data['config']['db-pwd'] : '') . '" />';
     $html .= '<button onclick="$.WebfanDesktop.Registry.Programs[\'frdl-webfan\'].cnf(\'db-pwd\', Dom.g(\'db-pwd\').value,null,$.WebfanDesktop.Registry.Programs[\'frdl-webfan\'].formConfig, true);$.WebfanDesktop.Registry.Programs[\'frdl-webfan\'].html(\'db\');">change</button>';
     $html .= '</div>';
     $html .= '<div>';
     $html .= '<legend>Table-Prefix</legend>';
     $html .= '<input type="text" id="db-pfx" value="' . (isset($this->data['config']['db-pfx']) ? $this->data['config']['db-pfx'] : '') . '" />';
     $html .= '<button onclick="$.WebfanDesktop.Registry.Programs[\'frdl-webfan\'].cnf(\'db-pfx\', Dom.g(\'db-pfx\').value,null,$.WebfanDesktop.Registry.Programs[\'frdl-webfan\'].formConfig, true);$.WebfanDesktop.Registry.Programs[\'frdl-webfan\'].html(\'db\');">change</button> (e.g. wd' . mt_rand(1000, 9999) . ')';
     $html .= '</div>';
     $html .= '<div>';
     $html .= '<legend>Test Connection</legend>';
     $html .= '<button onclick="$.WebfanDesktop.Registry.Programs[\'frdl-webfan\'].test(\'database\'); ">Test Connection</button>';
     $html .= '</div>';
     $html .= '<div style="margin:10px;padding:10px;text-align:center;font-style:italic;">';
     $html .= '<p><span class="webfan-red">Change these settings wisely</span></p>';
     $html .= '</div>';
     $html .= '</td>';
     $html .= '<td>';
     $html .= '<h2 class="webfan-blue"><span>Database</span> <span>Tables</span></h2>';
     $tables = array();
     if (true === $this->connected && isset($this->data['config']['db-driver']) && isset($this->data['config']['db-driver']) && isset($this->data['config']['db-host']) && isset($this->data['config']['db-driver']) && isset($this->data['config']['db-dbname']) && isset($this->data['config']['db-user']) && isset($this->data['config']['db-pwd']) && isset($this->data['config']['db-user']) && isset($this->data['config']['db-pfx'])) {
         try {
             $S = new \frdl\_db();
             $cfile = $this->data['config']['FILES']['database-schema'];
             $settings = array('driver' => $this->data['config']['db-driver'], 'host' => $this->data['config']['db-host'], 'dbname' => $this->data['config']['db-dbname'], 'user' => $this->data['config']['db-user'], 'password' => $this->data['config']['db-pwd'], 'pfx' => $this->data['config']['db-pfx']);
             if (file_exists($cfile)) {
                 $oldSchema = $S->load_schema(file_get_contents($cfile));
                 //$oldSchema = $S->load_schema($this->read($cfile, 'rb',  null));
             } else {
                 $oldSchema = $S->schema();
             }
             if (!is_object($oldSchema)) {
                 $oldSchema = $S->schema();
             }
             $S->check($schema, $tables, null, true, false, false, $this->db, $settings, $oldSchema);
             $S->tables($tables, false);
             $report = $S->check_tables($schema, $tables, $oldSchema);
             $html .= '<p>Version: ' . $oldSchema->version . '/' . $schema->version . '</p>';
             $newTables = false;
             $T = array();
             $_html = '';
             $_html .= '<div class="data-box" style="height:260px;overflow:auto;">';
             foreach ($schema->tables as $alias => $t) {
                 $_html .= '<div>';
                 $_html .= '<p><span style="color:' . ($S->isFresh($oldSchema->tables[$alias]['version'], $schema->tables[$alias]['version']) ? 'green' : 'red') . ';">' . $alias . ' (' . $oldSchema->tables[$alias]['version'] . '/' . $schema->tables[$alias]['version'] . ')</span></p>';
                 $_html .= '<p>' . $t['table'] . '</p>';
                 $_html .= '</div>';
                 if (!isset($tables[$t['table']]) || !$S->isFresh($oldSchema->tables[$alias]['version'], $schema->tables[$alias]['version'])) {
                     $newTables = true;
                 }
                 $T[$t['table']] =& $t;
             }
             $_html .= '</div>';
             if (true === $newTables || !$S->isFresh($oldSchema->version, $schema->version)) {
                 $this->wizard_error('<span>Missing database tables! Please run the database setup!</span>', E_USER_WARNING);
                 $html .= '<div style="text-align:center;">
   	 <button style="color:green;font-weight:bold;font-size:1.2em;" onclick="if(true!==confirm(\'Please backup your data first, possible data loss while converting!\\nBitte machen Sie zunaechst ein Datenbank Backup, Daten koennen durch der Konvertierung verloren gehen!\\n(@ToDo: Convert data from db versions)\'))return false;$.WebfanDesktop.Registry.Programs[\'frdl-webfan\'].cmd(\'frdl setup create-tables -bd\', function(){$.WebfanDesktop.Registry.Programs[\'frdl-webfan\'].html(\'db\');}); ">
   	 &rArr;&rArr;&rArr;<span>Create</span> <span>Tables</span>&lArr;&lArr;&lArr;
   	 </button>
   	 </div>
   	 ';
                 $this->result->js .= " \n\t\t\ttry{\n\t\t    \t\$.WebfanDesktop.Registry.Programs['nachrichtendienst'].post({\n\t\t    \t\ttext : '<span>Missing or obsolete database tables! Please run the database setup!</span>',\n\t\t    \t\ttype : 'error',\n\t\t    \t\tshow : true,\n\t\t    \t\tcallback : function(){\$('#window_frdl-webfan').show();\$.WebfanDesktop.Registry.Programs['frdl-webfan'].html('db');},\n\t\t    \t\ttime : new Date().getTime() / 1000,\n\t\t    \t\tnewnotif : true,\n\t\t    \t\tid : 'system-error-database-missing-or-obsolete-tables-" . (isset($schema->version) ? $schema->version : '') . "'\n\t\t    \t});\n\t\t\t}catch(err){\n\t\t\t\tconsole.error(err);\n\t\t\t}\t   \t \n          ";
             } else {
                 $this->result->js .= " \n\t   \t        \$(document).wdPostbox('deleteMessage', 'system-error-database-missing-or-obsolete-tables-" . (isset($oldSchema->version) ? $oldSchema->version : '') . "',  'update', false);\t \t   \t\n\t\t\t    \$(document).wdPostbox('deleteMessage', 'system-error-database-missing-or-obsolete-tables-" . (isset($schema->version) ? $schema->version : '') . "',  'update', false);\t \t   \t\n\t\t\t\n\t     \t ";
             }
             $html .= $_html;
             $html .= '<br />';
             $html .= '<p>Other tables in ' . $this->data['config']['db-dbname'] . ':</p>';
             $html .= '<div class="data-box" style="height:160px;overflow:auto;">';
             foreach ($tables as $name => $table) {
                 if (isset($T[$name])) {
                     continue;
                 }
                 $html .= '<p>' . $name . '</p>';
             }
             $html .= '</div>';
         } catch (\Exception $e) {
             $tablesOK = false;
             $html .= '<span class="webfan-red">Error checking tables!</span>';
             return $html;
         }
     } else {
         $html .= '<span class="webfan-red">Not connected.</span>';
     }
     $html .= '</td>';
     $html .= '</tr>';
     $html .= '</table>';
     $html .= '</div>';
     if (true !== $this->connected) {
         $this->result->js .= " \n\t\t\ttry{\n\t\t    \t\$.WebfanDesktop.Registry.Programs['nachrichtendienst'].post({\n\t\t    \t\ttext : '<span>No connection to database</span>. <span>Please goto the database settings!</span>',\n\t\t    \t\ttype : 'error',\n\t\t    \t\tshow : true,\n\t\t    \t\tcallback : function(){\$('#window_frdl-webfan').show();\$.WebfanDesktop.Registry.Programs['frdl-webfan'].html('db');},\n\t\t    \t\ttime : new Date().getTime() / 1000,\n\t\t    \t\tnewnotif : true,\n\t\t    \t\tid : 'system-error-no-database-connection-" . (isset($schema->version) ? $schema->version : '') . "'\n\t\t    \t});\n\t\t\t}catch(err){\n\t\t\t\tconsole.error(err);\n\t\t\t}\t   \t \n";
     } else {
         $this->result->js .= " \n\t   \t        \$('#window_main_postbox-ttt-all').wdPostbox('deleteMessage', 'system-error-no-database-connection-" . (isset($schema->version) ? $schema->version : '') . "',  'update', false);\t \t   \t\n\t\t\t\n\t     \t ";
     }
     return $html;
 }