/** * @author Ignacio Vazquez - elpepe.uy@gmail.com * @param array of string $pluginNames * TODO avoid using mysql functions - (copied from installer) */ static function executeInstaller($name) { $table_prefix = TABLE_PREFIX; tpl_assign('table_prefix', $table_prefix); $default_charset = 'DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'; tpl_assign('default_charset', $default_charset); $default_collation = 'collate utf8_unicode_ci'; tpl_assign('default_collation', $default_collation); $engine = DB_ENGINE; tpl_assign('engine', $engine); $path = ROOT . "/plugins/{$name}/info.php"; if (file_exists($path)) { DB::beginWork(); $pluginInfo = (include_once $path); //0. Check if exists in plg table $sql = "SELECT id FROM " . TABLE_PREFIX . "plugins WHERE name = '{$name}' "; $res = @mysql_query($sql); if (!$res) { DB::rollback(); return false; } $plg_obj = mysql_fetch_object($res); if (!$plg_obj) { //1. Insert into PLUGIN TABLE $cols = "name, is_installed, is_activated, version"; $values = "'{$name}', 1, 1 ,'" . array_var($pluginInfo, 'version') . "'"; if (is_numeric(array_var($pluginInfo, 'id'))) { $cols = "id, " . $cols; $values = array_var($pluginInfo, 'id') . ", " . $values; } $sql = "INSERT INTO " . TABLE_PREFIX . "plugins ({$cols}) VALUES ({$values}) "; if (@mysql_query($sql)) { $id = @mysql_insert_id(); $pluginInfo['id'] = $id; } else { echo "ERROR: " . mysql_error(); @mysql_query('ROLLBACK'); return false; } } else { $id = $plg_obj->id; $pluginInfo['id'] = $id; } //2. IF Plugin defines types, INSERT INTO ITS TABLE if (count(array_var($pluginInfo, 'types'))) { foreach ($pluginInfo['types'] as $k => $type) { if (isset($type['name'])) { $sql = "\n\t\t\t\t\t\t\tINSERT INTO " . TABLE_PREFIX . "object_types (name, handler_class, table_name, type, icon, plugin_id)\n\t\t\t\t\t\t\t \tVALUES (\n\t\t\t\t\t\t\t \t'" . array_var($type, "name") . "', \n\t\t\t\t\t\t\t \t'" . array_var($type, "handler_class") . "', \n\t\t\t\t\t\t\t \t'" . array_var($type, "table_name") . "', \n\t\t\t\t\t\t\t \t'" . array_var($type, "type") . "', \n\t\t\t\t\t\t\t \t'" . array_var($type, "icon") . "', \n\t\t\t\t\t\t\t\t{$id}\n\t\t\t\t\t\t\t)"; if (@mysql_query($sql)) { $pluginInfo['types'][$k]['id'] = @mysql_insert_id(); $type['id'] = @mysql_insert_id(); } else { echo $sql . "<br/>"; echo mysql_error() . "<br/>"; DB::rollback(); return false; } } } } //2. IF Plugin defines tabs, INSERT INTO ITS TABLE if (count(array_var($pluginInfo, 'tabs'))) { foreach ($pluginInfo['tabs'] as $k => $tab) { if (isset($tab['title'])) { $type_id = array_var($type, "id"); $sql = "\n\t\t\t\t\t\t\tINSERT INTO " . TABLE_PREFIX . "tab_panels (\n\t\t\t\t\t\t\t\tid,\n\t\t\t\t\t\t\t\ttitle, \n\t\t\t\t\t\t\t\ticon_cls, \n\t\t\t\t\t\t\t\trefresh_on_context_change, \n\t\t\t\t\t\t\t\tdefault_controller, \n\t\t\t\t\t\t\t\tdefault_action, \n\t\t\t\t\t\t\t\tinitial_controller, \n\t\t\t\t\t\t\t\tinitial_action, \n\t\t\t\t\t\t\t\tenabled, \n\t\t\t\t\t\t\t\ttype, \n\t\t\t\t\t\t\t\tplugin_id, \n\t\t\t\t\t\t\t\tobject_type_id )\n\t\t\t\t\t\t \tVALUES (\n\t\t\t\t\t\t \t\t'" . array_var($tab, 'id') . "', \n\t\t\t\t\t\t \t\t'" . array_var($tab, 'title') . "', \n\t\t\t\t\t\t \t\t'" . array_var($tab, 'icon_cls') . "',\n\t\t\t\t\t\t \t\t'" . array_var($tab, 'refresh_on_context_change') . "',\n\t\t\t\t\t\t \t\t'" . array_var($tab, 'default_controller') . "',\n\t\t\t\t\t\t \t\t'" . array_var($tab, 'default_action') . "',\n\t\t\t\t\t\t\t\t'" . array_var($tab, 'initial_controller') . "',\n\t\t\t\t\t\t\t\t'" . array_var($tab, 'initial_action') . "',\n\t\t\t\t\t\t\t\t'" . array_var($tab, 'enabled', 1) . "',\n\t\t\t\t\t\t\t\t'" . array_var($tab, 'type') . "',\n\t\t\t\t\t\t\t\t{$id},\n\t\t\t\t\t\t\t\t" . array_var($tab, 'object_type_id') . "\n\t\t\t\t\t\t\t)"; if (!@mysql_query($sql)) { echo $sql; echo mysql_error(); DB::rollback(); return false; } // INSERT INTO TAB PANEL PERMISSSION $sql = "\n\t\t\t\t\t\t\tINSERT INTO " . TABLE_PREFIX . "tab_panel_permissions (\n\t\t\t\t\t\t\t\tpermission_group_id,\n\t\t\t\t\t\t\t\ttab_panel_id \n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t \tVALUES ( 1,'" . array_var($tab, 'id') . "' ), ( 2,'" . array_var($tab, 'id') . "' ) ON DUPLICATE KEY UPDATE permission_group_id = permission_group_id "; if (!@mysql_query($sql)) { echo $sql; echo mysql_error(); @mysql_query('ROLLBACK'); DB::rollback(); return false; } } } } // Create schema sql query $schema_creation = ROOT . "/plugins/{$name}/install/sql/mysql_schema.php"; if (file_exists($schema_creation)) { $total_queries = 0; $executed_queries = 0; if (executeMultipleQueries(tpl_fetch($schema_creation), $total_queries, $executed_queries)) { logger::log("Schema created for plugin {$name} "); } else { //echo tpl_fetch ( $schema_creation ); echo mysql_error(); echo "llega <br>"; DB::rollback(); return false; } } // Create schema sql query $schema_query = ROOT . "/plugins/{$name}/install/sql/mysql_initial_data.php"; if (file_exists($schema_query)) { $total_queries = 0; $executed_queries = 0; if (executeMultipleQueries(tpl_fetch($schema_query), $total_queries, $executed_queries)) { logger::log("Initial data loaded for plugin '{$name}'." . mysql_error()); } else { echo mysql_error(); DB::rollback(); return false; } } $install_script = ROOT . "/plugins/{$name}/install/install.php"; if (file_exists($install_script)) { include_once $install_script; } DB::commit(); return true; } return false; }
/** * @param array of string $pluginNames */ static function executeInstaller($name) { $table_prefix = TABLE_PREFIX; tpl_assign('table_prefix', $table_prefix); $default_charset = 'DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'; tpl_assign('default_charset', $default_charset); $default_collation = 'collate utf8_unicode_ci'; tpl_assign('default_collation', $default_collation); $engine = DB_ENGINE; tpl_assign('engine', $engine); try { $path = ROOT . "/plugins/$name/info.php"; if (file_exists ( $path )) { DB::beginWork (); $pluginInfo = include_once $path; //0. Check if exists in plg table $sql = "SELECT id FROM " . TABLE_PREFIX . "plugins WHERE name = '$name' "; $plg_obj = DB::executeOne($sql); if (! $plg_obj) { //1. Insert into PLUGIN TABLE $cols = "name, is_installed, is_activated, version"; $values = "'$name', 1, 1 ,'".array_var ( $pluginInfo, 'version' )."'"; if (is_numeric ( array_var ( $pluginInfo, 'id' ) )) { $cols = "id, " . $cols; $values = array_var ( $pluginInfo, 'id' ) . ", " . $values; } $sql = "INSERT INTO " . TABLE_PREFIX . "plugins ($cols) VALUES ($values) ON DUPLICATE KEY UPDATE name=name"; DB::executeOne($sql); $id = DB::lastInsertId(); $pluginInfo ['id'] = $id; } else { $id = $plg_obj['id']; $pluginInfo ['id'] = $id; } //2. IF Plugin defines types, INSERT INTO ITS TABLE if (count ( array_var ( $pluginInfo, 'types' ) )) { foreach ( $pluginInfo ['types'] as $k => $type ) { if (isset ( $type ['name'] )) { $sql = " INSERT INTO " . TABLE_PREFIX . "object_types (name, handler_class, table_name, type, icon, plugin_id) VALUES ( '" . array_var ( $type, "name" ) . "', '" . array_var ( $type, "handler_class" ) . "', '" . array_var ( $type, "table_name" ) . "', '" . array_var ( $type, "type" ) . "', '" . array_var ( $type, "icon" ) . "', $id ) ON DUPLICATE KEY UPDATE name=name"; DB::executeOne($sql); $last_id = DB::lastInsertId(); $pluginInfo['types'][$k]['id'] = $last_id; $type['id'] = $last_id; } } } //2. IF Plugin defines tabs, INSERT INTO ITS TABLE if (count ( array_var ( $pluginInfo, 'tabs' ) )) { foreach ( $pluginInfo ['tabs'] as $k => $tab ) { if (isset ( $tab ['title'] )) { $type_id = array_var ( $type, "id" ); $sql = " INSERT INTO " . TABLE_PREFIX . "tab_panels ( id, title, icon_cls, refresh_on_context_change, default_controller, default_action, initial_controller, initial_action, enabled, type, plugin_id, object_type_id ) VALUES ( '" . array_var ( $tab, 'id' ) . "', '" . array_var ( $tab, 'title' ) . "', '" . array_var ( $tab, 'icon_cls' ) . "', '" . array_var ( $tab, 'refresh_on_context_change' ) . "', '" . array_var ( $tab, 'default_controller' ) . "', '" . array_var ( $tab, 'default_action' ) . "', '" . array_var ( $tab, 'initial_controller' ) . "', '" . array_var ( $tab, 'initial_action' ) . "', '" . array_var ( $tab, 'enabled', 1 ) . "', '" . array_var ( $tab, 'type' ) . "', $id, " . array_var ( $tab, 'object_type_id' ) . " ) ON DUPLICATE KEY UPDATE name=name"; DB::executeOne($sql); // INSERT INTO TAB PANEL PERMISSSION $sql = " INSERT INTO " . TABLE_PREFIX . "tab_panel_permissions ( permission_group_id, tab_panel_id ) VALUES ( 1,'" . array_var ( $tab, 'id' ) . "' ), ( 2,'" . array_var ( $tab, 'id' ) . "' ) ON DUPLICATE KEY UPDATE permission_group_id = permission_group_id "; DB::executeOne($sql); } } } // Create schema sql query $schema_creation = ROOT . "/plugins/$name/install/sql/mysql_schema.php"; if (file_exists ( $schema_creation )) { $total_queries = 0; $executed_queries = 0; executeMultipleQueries ( tpl_fetch ( $schema_creation ), $total_queries, $executed_queries ); Logger::log("Schema created for plugin $name"); } // Create schema sql query $schema_query = ROOT . "/plugins/$name/install/sql/mysql_initial_data.php"; if (file_exists ( $schema_query )) { $total_queries = 0; $executed_queries = 0; executeMultipleQueries ( tpl_fetch ( $schema_query ), $total_queries, $executed_queries ); Logger::log ( "Initial data loaded for plugin '$name'." . mysql_error () ); } $install_script = ROOT . "/plugins/$name/install/install.php"; if (file_exists ( $install_script )) { include_once $install_script; } DB::commit (); return true; } } catch (Exception $e) { //echo $e->getMessage(); DB::rollback(); throw $e; } return false; }
error_reporting(E_ALL); require '../../config/config.php'; $link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die('Could not connect to database server ' . DB_HOST); mysql_select_db(DB_NAME, $link) or die('Could not select database ' . DB_NAME); $id = $_GET['id']; //$cs = 'character set '.config_option('character_set', 'utf8'); //$co = 'collate '.config_option('collation', 'utf8_unicode_ci'); $cs = 'character set utf8'; $co = 'collate utf8_unicode_ci'; $sql = file_get_contents("{$id}.sql"); $sql = str_replace('{$tp}', DB_PREFIX, $sql); $sql = str_replace('<?php echo $table_prefix ?>', DB_PREFIX, $sql); $sql = str_replace('<?php echo $default_collation ?>', $co, $sql); $sql = str_replace('<?php echo $default_charset ?>', $cs, $sql); executeMultipleQueries($sql, &$total_queries, &$executed_queries, $link); echo 'Summary' . "<br>\n"; echo 'Total queries in SQL: ' . $total_queries . " <br>\n"; echo 'Total executed queries: ' . $executed_queries . " <br>\n"; mysql_close($link); function executeMultipleQueries($sql, &$total_queries, &$executed_queries, $link) { if (!trim($sql)) { $total_queries = 0; $executed_queries = 0; return true; } // if // Make it work on PHP 5.0.4 $sql = str_replace(array("\r\n", "\r"), array("\n", "\n"), $sql); $queries = explode(";\n", $sql);
/** * @param array of string $pluginNames */ static function executeInstaller($name) { $table_prefix = TABLE_PREFIX; tpl_assign('table_prefix', $table_prefix); $default_charset = 'DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'; tpl_assign('default_charset', $default_charset); $default_collation = 'collate utf8_unicode_ci'; tpl_assign('default_collation', $default_collation); $engine = DB_ENGINE; tpl_assign('engine', $engine); try { $path = ROOT . "/plugins/{$name}/info.php"; if (file_exists($path)) { DB::beginWork(); $pluginInfo = (include_once $path); //0. Check if exists in plg table $sql = "SELECT id FROM " . TABLE_PREFIX . "plugins WHERE name = '{$name}' "; $plg_obj = DB::executeOne($sql); if (!$plg_obj) { //1. Insert into PLUGIN TABLE $cols = "name, is_installed, is_activated, version"; $values = "'{$name}', 1, 1 ,'" . array_var($pluginInfo, 'version') . "'"; if (is_numeric(array_var($pluginInfo, 'id'))) { $cols = "id, " . $cols; $values = array_var($pluginInfo, 'id') . ", " . $values; } $sql = "INSERT INTO " . TABLE_PREFIX . "plugins ({$cols}) VALUES ({$values}) ON DUPLICATE KEY UPDATE version='" . array_var($pluginInfo, 'version') . "'"; DB::executeOne($sql); $id = DB::lastInsertId(); $pluginInfo['id'] = $id; } else { $id = $plg_obj['id']; $pluginInfo['id'] = $id; } if (isset($pluginInfo['dependences']) && is_array($pluginInfo['dependences'])) { foreach ($pluginInfo['dependences'] as $dep_plugin_name) { if (!Plugins::instance()->isActivePlugin($dep_plugin_name)) { throw new Exception("To install this plugin you need to install '{$dep_plugin_name}' first."); } } } //2. IF Plugin defines types, INSERT INTO ITS TABLE if (count(array_var($pluginInfo, 'types'))) { foreach ($pluginInfo['types'] as $k => $type) { if (isset($type['name'])) { $sql = "\r\n\t\t\t\t\t\t\tINSERT INTO " . TABLE_PREFIX . "object_types (name, handler_class, table_name, type, icon, plugin_id)\r\n\t\t\t\t\t\t\t \tVALUES (\r\n\t\t\t\t\t\t\t \t'" . array_var($type, "name") . "', \r\n\t\t\t\t\t\t\t \t'" . array_var($type, "handler_class") . "', \r\n\t\t\t\t\t\t\t \t'" . array_var($type, "table_name") . "', \r\n\t\t\t\t\t\t\t \t'" . array_var($type, "type") . "', \r\n\t\t\t\t\t\t\t \t'" . array_var($type, "icon") . "', \r\n\t\t\t\t\t\t\t\t{$id}\r\n\t\t\t\t\t\t\t) ON DUPLICATE KEY UPDATE name=name"; DB::executeOne($sql); $last_id = DB::lastInsertId(); $pluginInfo['types'][$k]['id'] = $last_id; $type['id'] = $last_id; } } } //2. IF Plugin defines tabs, INSERT INTO ITS TABLE if (count(array_var($pluginInfo, 'tabs'))) { foreach ($pluginInfo['tabs'] as $k => $tab) { if (isset($tab['title'])) { $type_id = array_var($type, "id"); $sql = "\r\n\t\t\t\t\t\t\tINSERT INTO " . TABLE_PREFIX . "tab_panels (\r\n\t\t\t\t\t\t\t\tid,\r\n\t\t\t\t\t\t\t\ttitle, \r\n\t\t\t\t\t\t\t\ticon_cls, \r\n\t\t\t\t\t\t\t\trefresh_on_context_change, \r\n\t\t\t\t\t\t\t\tdefault_controller, \r\n\t\t\t\t\t\t\t\tdefault_action, \r\n\t\t\t\t\t\t\t\tinitial_controller, \r\n\t\t\t\t\t\t\t\tinitial_action, \r\n\t\t\t\t\t\t\t\tenabled, \r\n\t\t\t\t\t\t\t\ttype, \r\n\t\t\t\t\t\t\t\tplugin_id, \r\n\t\t\t\t\t\t\t\tobject_type_id )\r\n\t\t\t\t\t\t \tVALUES (\r\n\t\t\t\t\t\t \t\t'" . array_var($tab, 'id') . "', \r\n\t\t\t\t\t\t \t\t'" . array_var($tab, 'title') . "', \r\n\t\t\t\t\t\t \t\t'" . array_var($tab, 'icon_cls') . "',\r\n\t\t\t\t\t\t \t\t'" . array_var($tab, 'refresh_on_context_change') . "',\r\n\t\t\t\t\t\t \t\t'" . array_var($tab, 'default_controller') . "',\r\n\t\t\t\t\t\t \t\t'" . array_var($tab, 'default_action') . "',\r\n\t\t\t\t\t\t\t\t'" . array_var($tab, 'initial_controller') . "',\r\n\t\t\t\t\t\t\t\t'" . array_var($tab, 'initial_action') . "',\r\n\t\t\t\t\t\t\t\t'" . array_var($tab, 'enabled', 1) . "',\r\n\t\t\t\t\t\t\t\t'" . array_var($tab, 'type') . "',\r\n\t\t\t\t\t\t\t\t{$id},\r\n\t\t\t\t\t\t\t\t" . array_var($tab, 'object_type_id') . "\r\n\t\t\t\t\t\t\t) ON DUPLICATE KEY UPDATE \r\n\t\t\t\t\t\t\t\tid = VALUES(`id`), \r\n\t\t\t\t\t\t\t\ttitle = VALUES(`title`), \r\n\t\t\t\t\t\t\t\ticon_cls = VALUES(`icon_cls`), \r\n\t\t\t\t\t\t\t\trefresh_on_context_change = VALUES(`refresh_on_context_change`), \r\n\t\t\t\t\t\t\t\tdefault_controller = VALUES(`default_controller`), \r\n\t\t\t\t\t\t\t\tdefault_action = VALUES(`default_action`), \r\n\t\t\t\t\t\t\t\tinitial_controller = VALUES(`initial_controller`), \r\n\t\t\t\t\t\t\t\tinitial_action = VALUES(`initial_action`), \r\n\t\t\t\t\t\t\t\tenabled = VALUES(`enabled`), \r\n\t\t\t\t\t\t\t\ttype = VALUES(`type`), \r\n\t\t\t\t\t\t\t\tplugin_id = VALUES(`plugin_id`),\r\n\t\t\t\t\t\t\t\tobject_type_id = VALUES(`object_type_id`);\r\n\t\t\t\t\t\t\t"; DB::executeOne($sql); // INSERT INTO TAB PANEL PERMISSSION $sql = "\r\n\t\t\t\t\t\t\tINSERT INTO " . TABLE_PREFIX . "tab_panel_permissions (\r\n\t\t\t\t\t\t\t\tpermission_group_id,\r\n\t\t\t\t\t\t\t\ttab_panel_id \r\n\t\t\t\t\t\t\t)\r\n\t\t\t\t\t\t \tVALUES ( 1,'" . array_var($tab, 'id') . "' ), ( 2,'" . array_var($tab, 'id') . "' ) ON DUPLICATE KEY UPDATE permission_group_id = permission_group_id "; DB::executeOne($sql); } } } // Create schema sql query $schema_creation = ROOT . "/plugins/{$name}/install/sql/mysql_schema.php"; if (file_exists($schema_creation)) { $total_queries = 0; $executed_queries = 0; executeMultipleQueries(tpl_fetch($schema_creation), $total_queries, $executed_queries); Logger::log("Schema created for plugin {$name}"); } // Create schema sql query $schema_query = ROOT . "/plugins/{$name}/install/sql/mysql_initial_data.php"; if (file_exists($schema_query)) { $total_queries = 0; $executed_queries = 0; executeMultipleQueries(tpl_fetch($schema_query), $total_queries, $executed_queries); Logger::log("Initial data loaded for plugin '{$name}'." . mysql_error()); } $install_script = ROOT . "/plugins/{$name}/install/install.php"; if (file_exists($install_script)) { include_once $install_script; } DB::commit(); return true; } } catch (Exception $e) { //echo $e->getMessage(); DB::rollback(); throw $e; } return false; }