/src/install/action.php
PHP | 1733 lines | 1446 code | 238 blank | 49 comment | 138 complexity | 53abaa560d72181b0b2df3a99498892f MD5 | raw file
Possible License(s): MIT, CC0-1.0
Large files files are truncated, but you can click here to view the full file
- <?php
- /**
- * textMotion
- * ---
- * Written by Jose Carlos Nieto <xiam@menteslibres.org>
- * Copyright (c) 2007-2008, Jose Carlos Nieto <xiam@menteslibres.org>
- *
- * Licensed under The MIT License
- * Redistributions of files must retain the above copyright notice.
- *
- * @author Jose Carlos Nieto <xiam@menteslibres.org>
- * @package textMotion
- * @copyright Copyright (c) 2007-2008, J. Carlos Nieto <xiam@menteslibres.org>
- * @link http://www.textmotion.org
- * @version $Revision: 1507 $
- * @modifiedby $LastChangedBy: xiam.core $
- * @lastmodified $Date: 2008-11-07 12:05:50 +1100 (Fri, 07 Nov 2008) $
- * @license http://www.opensource.org/licenses/mit-license.php The MIT License
- *
- */
- class install_action extends tm_action {
- public function __construct($action = null) {
- extract(
- $this->using(
- 'session',
- 'env'
- )
- );
- $this->lock_file = TM_TEMP_DIR.'install.lock';
- if (preg_match('/^(test_ftp)$/', $action)) {
- // skip
- } else if (file_exists($this->lock_file)) {
- // skip only when the lock_key is set
- if (!$session->get('lock_key') || $session->get('lock_key') != $this->get_lock_key()) {
- // the key is incorrect
- $env->redirect('/');
- }
- }
- }
-
- private function get_lock_key() {
- $fh = fopen($this->lock_file, 'r');
- $key = trim(fread($fh, filesize($this->lock_file)));
- fclose($fh);
- return $key;
- }
- private function db_driver($driver) {
- $driver = preg_replace('/[^a-z0-9]/i', '', $driver);
- return TM_LIB_DIR.'databases/'.$driver.'.php';
- }
- private function clean_cache() {
- $archive =& $this->using('archive');
- $temp = $archive->scandir(TM_TEMP_DIR);
- foreach($temp as $file) {
- if ($file{0} != '.' && $file != 'debug.log') {
- $archive->clean(TM_TEMP_DIR.$file);
- }
- }
- }
- private function wp_format_fix($text) {
- $text = preg_replace_callback(
- '/(<pre[^>]*>.+?<\/pre>)|(\r?\n\r?\n)/is',
- create_function('$a', 'return ($a[0]{0} == "<") ? $a[0] : "<br /><br />\n";'),
- $text
- );
- return $text;
- }
- private function get_modules() {
- $archive =& $this->using('archive');
-
- $temp = $archive->scandir(TM_MODULES_DIR);
- $modules = array();
- foreach($temp as $test) {
- if (is_dir(TM_MODULES_DIR.$test) && $test{0} != '.') {
- $modules[] = $test;
- }
- }
-
- $main = array(
- 'conf',
- 'groups',
- 'users',
- 'menu',
- 'categories'
- );
- $modules = array_unique(array_merge($main, $modules));
- return $modules;
- }
- private function module_exists($module) {
- return (file_exists(TM_MODULES_DIR.$module) && is_dir(TM_MODULES_DIR.$module));
- }
- private function gekko_path_fix($str) {
- return str_replace('data/files/uploaded/', 'data/files/uploads/', $str);
- }
- private function install(&$conn = null) {
- extract(
- $this->using(
- 'session',
- 'param',
- 'db',
- 'xml',
- 'archive',
- 'env'
- )
- );
- if (is_object($conn)) {
-
- $db->db =& $conn->db;
- $db->config =& $conn->config;
- $db_conf = array();
- foreach ($db->config as $key => $val) {
- $db_conf[$key] = $val;
- }
- } else {
- $db_conf = $session->get('_db');
- require $this->db_driver($db_conf['driver']);
-
- $db_driver = $db_conf['driver'].'_driver';
- $db->db = new $db_driver;
-
- foreach ($db_conf as $key => $value) {
- $db->db->{$key} = $value;
- }
- $db->open(false);
- }
- $module_versions = array();
- $modules = $this->get_modules();
- foreach($modules as $module) {
- $module_versions[$module] = $this->install_module($db, $module);
- }
- $user = $session->get('_user');
- if ($user) {
- $db->update(
- 'users',
- array(
- 'username' => $user['username'],
- 'nickname' => $user['username'],
- 'email' => $user['email'],
- 'password' => md5($user['username'].$user['new_password']),
- array('date_created' => 'now()')
- ),
- 1
- );
- }
- $this->clean_cache();
- $this->using('conf');
- $conf =& new conf($t = 1);
-
- foreach($module_versions as $module => $version) {
- $conf->set($module.'/version', $version);
- }
- $conf->set('core/template', 'default', 's');
- $conf->set('core/template_style', 'default', 's');
- $conf->set('core/icon_theme', 'default', 's');
- $conf->set('core/emoticon_theme', 'default', 's');
- $conf->set('users/allow_register', false, 'b');
- $site = $session->get('_conf');
- if ($site) {
- foreach ($site as $class => $key) {
- foreach ($key as $name => $value) {
- $conf->set($class.'/'.$name, $value);
- }
- }
- }
- $this->save_dbconf($db);
- }
- private function save_dbconf(&$db) {
- $dbfile = TM_USER_DIR.'conf'.DS.'database.php';
- $conf =
- '<?php
- /**
- * Textmotion
- * ---
- * Written by Jose Carlos Nieto <xiam@menteslibres.org>
- * Copyright (c) 2007-2008, Jose Carlos Nieto <xiam@menteslibres.org>
- *
- * Licensed under The MIT License
- * Redistributions of files must retain the above copyright notice.
- *
- */
- class database {
- public $config = array(
- \'default\' => array (
- \'hostname\' => \''.str_replace("'", "\'", $db->db->hostname).'\',
- \'username\' => \''.str_replace("'", "\'", $db->db->username).'\',
- \'password\' => \''.str_replace("'", "\'", $db->db->password).'\',
- \'database\' => \''.str_replace("'", "\'", $db->db->database).'\',
- \'driver\' => \''.str_replace("'", "\'", $db->db->driver).'\',
- \'prefix\' => \''.str_replace("'", "\'", $db->db->prefix).'\'
- )
- );
- }
- ?>';
- $fh = fopen($dbfile, 'w');
- fwrite($fh, $conf);
- fclose($fh);
- @chmod($dbfile, 0755);
- }
- public function test_ftp() {
- extract(
- $this->using(
- 'param',
- 'session',
- 'json',
- 'net/ftp_client'
- )
- );
- $ftp = $param->get('ftp');
- $ftp_client = new ftp_client($ftp['host'], 21, $ftp['user'], $ftp['pass']);
- $ftp_client->open();
- if ($ftp_client->login()) {
- $this->session->set('next', $param->get('next'));
- $this->session->set('ftp', array(
- 'host' => $ftp['host'],
- 'user' => $ftp['user'],
- 'pass' => $ftp['pass']
- ));
- $json->response(
- array(
- 'successMessage' => 'Login successful.',
- 'execute' => 'startBrowser();'
- )
- );
- } else {
- $json->response(
- array(
- 'errorMessage' => 'Could not login into the FTP server.'
- )
- );
- }
- }
- private function test_db($data) {
- $this->using('db');
- $db = new db();
-
- $driver = $this->db_driver($data['driver']);
- if (file_exists($driver)) {
- require_once $driver;
- $copy = array(
- 'hostname',
- 'username',
- 'password',
- 'database',
- 'driver',
- 'prefix'
- );
- $class_name = $data['driver'].'_driver';
- $conf = array();
- $db->db = new $class_name;
- foreach($copy as $el) {
- $db->db->$el = $data[$el];
- $conf[$el] = $db->db->$el;
- }
- if ($db->db->open(false)) {
- return $db;
- } else {
- return $db->get_error();
- }
- }
- return false;
- }
- public function import() {
- extract(
- $this->using(
- 'db',
- 'param',
- 'session',
- 'json',
- 'env'
- )
- );
- $param->not_empty(
- 'catalog',
- 'db_from.driver',
- 'db_from.hostname',
- 'db_from.database',
- 'db_to.driver',
- 'db_to.hostname',
- 'db_to.database'
- );
- $db_from = $this->test_db($param->get('db_from'));
- $db_to = $this->test_db($param->get('db_to'));
- if (!is_object($db_from)) {
- $json->response(
- array(
- 'errorMessage' => __($db_from)
- )
- );
- }
- if (!is_object($db_to)) {
- $json->response(
- array(
- 'errorMessage' => __($db_to)
- )
- );
- }
- $arg = array();
- $arg['db_from'] =& $db_from;
- $arg['db_to'] =& $db_to;
- $this->install($db_to);
- switch($param->get('catalog')) {
- case 'gekko':
- $this->import_from_gekko($arg);
- break;
- case 'wordpress':
- $this->import_from_wordpress($arg);
- break;
- }
- $this->save_dbconf($db_to);
- $env->redirect('/action=install_success');
- }
- private function import_from_wordpress($vars) {
-
- extract(
- $this->using(
- 'db',
- 'param',
- 'json'
- )
- );
- extract($vars);
- $db_version = 0;
- $q = $db_from->find_one(
- array(
- 'option' => array(
- 'table' => 'options',
- 'fields' => array('option_value'),
- 'where' => $db_from->bind('option_name = ?', 'db_version')
- )
- )
- );
- if (!empty($q['option']['option_value'])) {
- $db_version = $q['option']['option_value'];
- }
- if ($db_version >= 3306) {
-
- $db_to->truncate('content_relations');
-
- // comments
- $src = $db_from->select('comments', '*', $db_from->bind('comment_approved = ?', '1'));
- $db_to->truncate('comments');
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'comments',
- array(
- 'id' => $row['comment_id'],
- 'parent_id' => $row['comment_parent'],
- 'content' => $this->wp_format_fix($row['comment_content']),
- 'author_id' => $row['user_id'],
- 'date_created' => $row['comment_date'],
- 'nickname' => $row['comment_author'],
- 'website' => $row['comment_author_url'],
- 'email' => $row['comment_author_email'],
- 'ip_address' => $row['comment_author_ip'],
- 'module' => 'blog',
- 'thread_id' => $row['comment_post_id']
- )
- );
- }
- // importing menu
- $src = $db_from->select('links', '*');
- $db_to->truncate('menu');
- $db_to->truncate('menu_links');
- while ($row = $db_from->fetch_array($src)) {
- if ($db_version < 3570) {
-
- } else if ($db_version < 6039) {
-
- $src2 = $db_from->select('link2cat', 'category_id', $db_from->bind('link_id = ?', $row['link_id']));
- $row2 = $db_from->fetch_array($src2);
- $src3 = $db_from->select('categories', 'cat_name', $db_from->bind('cat_id = ?', $row2['category_id']));
- $row3 = $db_from->fetch_array($src3);
-
- } else {
- $src3 = $db_from->select('terms', 'name', $db_from->bind('term_id = ?', $row['link_category']));
- $row3 = $db_from->fetch_array($src3);
- }
- if (isset($row3['name'])) {
- $src4 = $db_to->select('menu', 'id', $db_to->bind('title = ?', $row3['name']));
- $row4 = $db_to->fetch_array($src4);
- if (!$row4) {
- $db_to->insert(
- 'menu',
- array(
- 'title' => $row3['name']
- )
- );
- $menu_id = $db_to->last_insert_id();
- $db_to->insert(
- 'blocks',
- array(
- 'title' => $row3['name'],
- 'position' => 'left',
- 'embedded_script' => 'menu.display',
- 'variable' => serialize(array('menu_id' => $menu_id)),
- 'status' => 1,
- 'author_id' => 1
- )
- );
- $block_id = $db_to->last_insert_id();
- $db_to->insert(
- 'blocks_groups',
- array(
- 'block_id' => $block_id,
- 'group_id' => TM_GROUP_ALL
- )
- );
- $db_to->insert(
- 'blocks_modules',
- array(
- 'block_id' => $block_id,
- 'module_name' => 'all'
- )
- );
- } else {
- $menu_id = $row4['id'];
- }
-
- $db_to->insert(
- 'menu_links',
- array(
- 'menu_id' => $menu_id,
- 'url' => $row['link_url'],
- 'tooltip' => $row['link_description'],
- 'title' => $row['link_name'],
- 'order_id' => 0,
- 'level' => 0
- )
- );
- }
- }
- // importing users
- $src = $db_from->select('users', '*');
- $db_to->truncate('users');
- $db_to->truncate('groups_users');
- while ($row = $db_from->fetch_array($src)) {
- if ($db_version >= 7558) {
- $db_to->insert(
- 'users',
- array(
- 'id' => $row['id'],
- 'username' => $row['user_login'],
- 'password' => $row['user_pass'],
- 'date_created' => $row['user_registered'],
- 'realname' => $row['user_nicename'],
- 'email' => $row['user_email'],
- 'nickname' => $row['user_login'],
- 'status' => 1
- )
- );
- } else {
- $db_to->insert(
- 'users',
- array(
- 'id' => $row['id'],
- 'username' => $row['user_login'],
- 'password' => '$M1'.$row['user_pass'],
- 'date_created' => $row['user_registered'],
- 'realname' => $row['user_nicename'],
- 'email' => $row['user_email'],
- 'nickname' => $row['user_login'],
- 'status' => 1
- )
- );
- }
- }
- // importing groups
- $src = $db_from->select('usermeta', array('user_id', 'meta_value'), $db_from->bind('meta_key = ?', 'wp_capabilities'));
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'groups_users',
- array(
- 'user_id' => $row['user_id'],
- 'group_id' => TM_GROUP_USER
- )
- );
- $meta = @unserialize($row['meta_value']);
- if (!empty($meta['administrator'])) {
- $db_to->insert(
- 'groups_users',
- array(
- 'user_id' => $row['user_id'],
- 'group_id' => TM_GROUP_ADMIN
- )
- );
- }
- }
-
- if ($db_version < 6039) {
- // importing blog tags
- $src = $db_from->select('post2cat', '*');
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'content_relations',
- array(
- 'module' => 'blog',
- 'item_id' => $row['post_id'],
- 'relation_id' => $row['category_id'],
- 'relation_type' => 'category'
- )
- );
- }
- // importing categories
- $src = $db_from->select('categories', '*');
- $db_to->truncate('categories');
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'categories',
- array(
- 'id' => $row['cat_id'],
- 'title' => $row['cat_name'],
- 'text_id' => $param->textilize($row['cat_name']),
- 'author_id' => 1,
- array('date_created' => 'now()')
- )
- );
- }
-
- // importing blog
- $src = $db_from->select('posts', '*');
- $db_to->truncate('blog');
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'blog',
- array(
- 'id' => $row['id'],
- 'title' => $row['post_title'],
- 'content' => $this->wp_format_fix($row['post_content']),
- 'date_created' => $row['post_date'],
- 'date_publish' => $row['post_date'],
- 'date_modified' => $row['post_modified'],
- 'author_id' => $row['post_author'],
- 'modifier_id' => 0,
- 'allow_comments' => intval($row['comment_status'] == 'open'),
- 'status' => intval($row['post_status'] == 'publish')
- )
- );
- }
-
- // pages
- $src = $db_from->select('posts', '*', $db_from->bind('post_status = ?', 'static'));
- $db_to->truncate('pages');
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'pages',
- array(
- 'id' => $row['id'],
- 'text_id' => $param->textilize($row['post_title']),
- 'title' => $row['post_title'],
- 'parent_id' => $row['post_parent'],
- 'content' => $this->wp_format_fix($row['post_content']),
- 'date_created' => $row['post_date'],
- 'date_modified' => $row['post_modified'],
- 'author_id' => $row['post_author'],
- 'modifier_id' => 0,
- 'status' => intval($row['post_status'] == 'publish')
- )
- );
- }
-
- } else {
- // categories
- $src = $db_from->query('
- SELECT t.term_id, t.name FROM
- '.$db_from->table_name('terms').' t
- INNER JOIN
- '.$db_from->table_name('term_taxonomy').' tt
- ON tt.term_id = t.term_id AND tt.taxonomy = \'category\';
- ');
- $db_to->truncate('categories');
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'categories',
- array(
- 'id' => $row['term_id'],
- 'title' => $row['name'],
- 'text_id' => $param->textilize($row['name']),
- 'author_id' => 1,
- array('date_created' => 'now()')
- )
- );
- unset($row);
- }
-
- // tags
- $src = $db_from->query('
- SELECT t.term_id, t.name, tt.parent, tt.count FROM
- '.$db_from->table_name('terms').' t
- INNER JOIN
- '.$db_from->table_name('term_taxonomy').' tt
- ON tt.term_id = t.term_id AND tt.taxonomy = \'post_tag\';
- ');
- $db_to->truncate('tags');
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'tags',
- array(
- 'id' => $row['term_id'],
- 'title' => $row['name'],
- 'rank' => $row['count'],
- 'category_id' => $row['parent'],
- 'text_id' => $param->textilize($row['name'])
- )
- );
- unset($row);
- }
-
- // relations
- $src = $db_from->query('
- SELECT
- r.object_id AS item_id,
- t.term_id AS relation_id,
- t.taxonomy
- FROM
- '.$db_from->table_name('term_relationships').' r
- INNER JOIN
- '.$db_from->table_name('term_taxonomy').' t
- ON r.term_taxonomy_id = t.term_taxonomy_id
- ');
-
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'content_relations',
- array(
- 'module' => 'blog',
- 'item_id' => $row['item_id'],
- 'relation_id' => $row['relation_id'],
- 'relation_type' => ($row['taxonomy'] == 'post_tag') ? 'tag' : 'category'
- )
- );
- }
-
- // blog
- $src = $db_from->select('posts', '*', $db_from->bind('post_type = ?', 'post'));
- $db_to->truncate('blog');
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'blog',
- array(
- 'id' => $row['id'],
- 'title' => $row['post_title'],
- 'content' => $this->wp_format_fix($row['post_content']),
- 'date_created' => $row['post_date'],
- 'date_publish' => $row['post_date'],
- 'date_modified' => $row['post_modified'],
- 'author_id' => $row['post_author'],
- 'modifier_id' => 0,
- 'allow_comments' => intval($row['comment_status'] == 'open'),
- 'status' => intval($row['post_status'] == 'publish')
- )
- );
- }
-
- // pages
- $src = $db_from->select('posts', '*', $db_from->bind('post_type = ?', 'page'));
- $db_to->truncate('pages');
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'pages',
- array(
- 'id' => $row['id'],
- 'text_id' => $param->textilize($row['post_title']),
- 'title' => $row['post_title'],
- 'parent_id' => $row['post_parent'],
- 'content' => $this->wp_format_fix($row['post_content']),
- 'date_created' => $row['post_date'],
- 'date_modified' => $row['post_modified'],
- 'author_id' => $row['post_author'],
- 'modifier_id' => 0,
- 'status' => intval($row['post_status'] == 'publish')
- )
- );
- }
-
- }
- } else {
- $json->response(
- array(
- 'errorMessage' => 'Your WordPress version is very old. Please upgrade to a newer version and try again.'
- )
- );
- }
- }
- private function import_from_gekko($vars) {
- extract(
- $this->using(
- 'db',
- 'param'
- )
- );
- extract($vars);
-
- $db_to->truncate('content_relations');
- // importing statistics
- $src = $db_from->select('stats_day', '*');
- $db_to->truncate('statistics_daily');
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'statistics_daily',
- array(
- 'day' => $row['day'],
- 'h00' => $row['h00'],
- 'h01' => $row['h01'],
- 'h02' => $row['h02'],
- 'h03' => $row['h03'],
- 'h04' => $row['h04'],
- 'h05' => $row['h05'],
- 'h06' => $row['h06'],
- 'h07' => $row['h07'],
- 'h08' => $row['h08'],
- 'h09' => $row['h09'],
- 'h10' => $row['h10'],
- 'h11' => $row['h11'],
- 'h12' => $row['h12'],
- 'h13' => $row['h13'],
- 'h14' => $row['h14'],
- 'h15' => $row['h15'],
- 'h16' => $row['h16'],
- 'h17' => $row['h17'],
- 'h18' => $row['h18'],
- 'h19' => $row['h19'],
- 'h20' => $row['h20'],
- 'h21' => $row['h21'],
- 'h22' => $row['h22'],
- 'h23' => $row['h23'],
- 'type' => $row['variant'] ? 'total' : 'unique',
- 'total' => $row['total']
- )
- );
- }
-
- $src = $db_from->select('stats_search_keywords', '*');
- $db_to->truncate('statistics_search');
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'statistics_search',
- array(
- 'keywords' => $row['keywords'],
- 'origin' => $row['engine'],
- 'counter' => $row['counter'],
- 'url' => $row['url'],
- 'referer' => $row['referer']
- )
- );
- }
- $src = $db_from->select('stats_uri_logs', '*', $db_from->bind('type = ?', 'request'));
- $db_to->truncate('statistics_urls');
- while ($row = $db_from->fetch_array($src)) {
-
- $db_to->insert(
- 'statistics_urls',
- array(
- 'url' => $row['url'],
- 'counter' => $row['counter'],
- 'ip_address' => $row['last_ip'],
- 'author_id' => $row['last_user_id'],
- 'date_created' => $row['date_access']
- )
- );
- }
-
- $src = $db_from->select('stats_uri_logs', '*', $db_from->bind('type = ?', 'referer'));
- $db_to->truncate('statistics_referers');
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'statistics_referers',
- array(
- 'url' => $row['url'],
- 'counter' => $row['counter'],
- 'ip_address' => $row['last_ip'],
- 'author_id' => $row['last_user_id'],
- 'date_created' => $row['date_access']
- )
- );
- }
- $src = $db_from->select('stats_visits', '*');
- $db_to->truncate('statistics_global');
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'statistics_global',
- array(
- 'day' => $row['day'],
- 'unique_hits' => $row['uniquehits'],
- 'total' => $row['total']
- )
- );
- }
- // importing categories
- $src = $db_from->select('category', '*');
- $db_to->truncate('categories');
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'categories',
- array(
- 'id' => $row['id'],
- 'title' => $row['title'],
- 'text_id' => $param->textilize($row['title']),
- 'description' => $row['description']
- )
- );
- }
- // importing tags
- $src = $db_from->select('category_tag', '*');
- $db_to->truncate('tags');
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'tags',
- array(
- 'id' => $row['id'],
- 'title' => $row['tag_name'],
- 'text_id' => $param->textilize($row['tag_name']),
- 'category_id' => $row['category_id'],
- 'rank' => $row['tag_rank']
- )
- );
- }
- // importing blog tags
- $src = $db_from->select('category_tag_relation', '*', $db_from->bind('module_name = ?', 'blog'));
-
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'content_relations',
- array(
- 'module' => 'blog',
- 'relation_id' => $row['tag_id'],
- 'relation_type' => 'tag',
- 'item_id' => $row['module_item_id']
- )
- );
- }
- // importing blog
- $src = $db_from->select('blog', '*');
- $db_to->truncate('blog');
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'blog',
- array(
- 'id' => $row['id'],
- 'title' => $row['title'],
- 'content' => $this->gekko_path_fix($row['content']),
- 'date_created' => $row['date_created'],
- 'date_publish' => $row['date_created'],
- 'date_modified' => $row['date_modified'],
- 'author_id' => $row['author_id'],
- 'modifier_id' => 0,
- 'status' => $row['status'],
- 'footer' => $row['footer'],
- 'page_views' => $row['page_reads']
- )
- );
- $db_to->insert(
- 'content_relations',
- array(
- 'module' => 'blog',
- 'relation_id' => $row['category_id'],
- 'relation_type' => 'category',
- 'item_id' => $row['id']
- )
- );
- }
- // importing pages
- $src = $db_from->select('page', '*');
- $db_to->truncate('pages');
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'pages',
- array(
- 'id' => $row['id'],
- 'text_id' => $param->textilize($row['title']),
- 'title' => $row['title'],
- 'content' => $this->gekko_path_fix($row['content']),
- 'parent_id' => $row['parent_id'],
- 'author_id' => $row['author_id'],
- 'modifier_id' => $row['editor_id'],
- 'status' => $row['status'],
- 'date_created' => $row['date_created'],
- 'date_modified' => $row['date_modified']
- )
- );
- }
- // importing users
- $src = $db_from->select('user', '*');
- $db_to->truncate('users');
- $db_to->truncate('groups_users');
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'users',
- array(
- 'id' => $row['id'],
- 'username' => $row['username'],
- 'password' => $row['password'],
- 'last_login' => $row['date_login'],
- 'date_created' => $row['date_registered'],
- 'realname' => $row['realname'],
- 'email' => $row['email'],
- 'nickname' => $row['username'],
- 'status' => $row['status']
- )
- );
- $groups = explode(',', $row['groups']);
- $groups[] = TM_GROUP_USER;
- foreach($groups as $g) {
- $g = trim($g);
- // only administrator
- if ($g == 1) {
- $db_to->insert(
- 'groups_users',
- array(
- 'user_id' => $row['id'],
- 'group_id' => $g
- )
- );
- }
- }
- }
- // importing comments
- $src = $db_from->select('comment', '*');
- $db_to->truncate('comments');
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'comments',
- array(
- 'id' => $row['id'],
- 'parent_id' => $row['parent'],
- 'content' => $this->gekko_path_fix($row['content']),
- 'author_id' => $row['author_id'],
- 'date_created' => $row['date_created'],
- 'date_modified' => $row['date_modified'],
- 'nickname' => $row['nickname'],
- 'website' => $row['url'],
- 'email' => $row['email'],
- 'ip_address' => $row['ipaddr'],
- 'module' => $row['module'],
- 'thread_id' => $row['thread']
- )
- );
- }
- // OPTIONAL MODULES
- if ($this->module_exists('tagboard')) {
- // importing tagboard
- $src = $db_from->select('extension_tagboard', '*');
- $db_to->truncate('tagboard');
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'tagboard',
- array(
- 'author_id' => $row['author_id'],
- 'nickname' => $row['nickname'],
- 'url' => $row['link'],
- 'message' => $row['message'],
- 'date_created' => $row['date_created'],
- 'ip_address' => $row['author_ip']
- )
- );
- }
- }
- if ($this->module_exists('forum')) {
-
- $db_to->truncate('forum_categories');
- $db_to->truncate('forum_forums');
- $db_to->truncate('forum_messages');
- $db_to->truncate('forum_topics');
- $db_to->truncate('forum_topics_tags');
- $src = $db_from->select('forum_category');
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'forum_categories',
- array(
- 'id' => $row['id'],
- 'title' => $row['name'],
- 'text_id' => $param->textilize($row['name']),
- 'order_id' => $row['order_id']
- )
- );
- }
- $src = $db_from->select('forum_forum');
- while ($row = $db_from->fetch_array($src)) {
- $src2 = $db_from->query('
- SELECT
- t.id as last_topic_id,
- m.id as last_message_id,
- m.is_topic,
- m.date_created as last_message_date,
- m.author_id as last_author_id
- FROM
- '.$db_from->table_name('forum_forum').' f
- INNER JOIN '.$db_from->table_name('forum_thread').' t
- ON t.forum_id = f.id
- INNER JOIN '.$db_from->table_name('forum_message').' m
- ON m.topic_id = t.id
- WHERE f.id = \''.$row['id'].'\' ORDER BY m.date_created DESC LIMIT 1
- ');
- $row2 = $db_from->fetch_array($src2);
- if ($row2['is_topic']) {
- $row2['last_message_id'] = 0;
- }
- $db_to->insert(
- 'forum_forums',
- array(
- 'id' => $row['id'],
- 'parent_id' => $row['category_id'],
- 'title' => $row['name'],
- 'messages_count' => $row['messages_count'],
- 'text_id' => $param->textilize($row['name']),
- 'order_id' => $row['order_id'],
- 'description' => $row['description'],
- 'last_message_id' => pick($row2['last_message_id'], null),
- 'last_message_date' => pick($row2['last_message_date'], null),
- 'last_author_id' => pick($row2['last_author_id'], null),
- 'last_topic_id' => pick($row2['last_topic_id'], null)
- )
- );
- }
- $src = $db_from->select('forum_thread');
- while ($row = $db_from->fetch_array($src)) {
- $src2 = $db_from->select(
- 'forum_message',
- array(
- 'title',
- 'content',
- 'author_id',
- 'date_created',
- 'date_modified'
- ),
- $db_from->bind('is_topic = ? AND topic_id = ?', 1, $row['id']),
- 'ORDER BY date_created DESC LIMIT 1'
- );
- $row2 = $db_from->fetch_array($src2);
- $src3 = $db_from->select(
- 'forum_message',
- array(
- 'id',
- 'author_id',
- 'date_created'
- ),
- $db_from->bind('is_topic = ? AND topic_id = ?', 0, $row['id']),
- 'ORDER BY date_created DESC LIMIT 1'
- );
- $row3 = $db_from->fetch_array($src3);
- $messages = $db_from->count(
- 'forum_message',
- $db_from->bind('is_topic = ? AND topic_id = ?', 0, $row['id'])
- );
-
- if ($row2) {
- $db_to->insert(
- 'forum_topics',
- array(
- 'id' => $row['id'],
- 'parent_id' => $row['forum_id'],
- 'page_views' => $row['stat_reads'],
- 'title' => $row2['title'],
- 'text_id' => $param->textilize($row2['title']),
- 'message' => $row2['content'],
- 'author_id' => $row2['author_id'],
- 'date_created' => $row2['date_created'],
- 'date_modified' => $row2['date_modified'],
- 'status' => 1,
- 'messages_count' => $messages,
- 'importance' => 'active',
- 'last_message_id' => pick($row3['id'], ''),
- 'last_message_date' => pick($row3['date_created'], ''),
- 'last_author_id' => pick($row3['author_id'], ''),
- 'type' => 'normal'
- )
- );
- }
- }
- $src = $db_from->select('forum_message', '*', $db->bind('is_topic = ?', 0));
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'forum_messages',
- array(
- 'id' => $row['id'],
- 'parent_id' => $row['topic_id'],
- 'message' => $row['content'],
- 'author_id' => $row['author_id'],
- 'date_created' => $row['date_created'],
- 'date_modified' => $row['date_modified']
- )
- );
- }
- }
- if ($this->module_exists('gallery')) {
- // importing galleries
- $src = $db_from->select('gallery', '*');
- $db_to->truncate('galleries');
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'galleries',
- array(
- 'id' => $row['id'],
- 'parent_id' => $row['gallery_id'],
- 'title' => $row['title'],
- 'description' => $this->gekko_path_fix($row['description']),
- 'thumbnail' => $row['thumbnail_url'],
- 'author_id' => $row['author_id'],
- 'date_created' => $row['date_created']
- )
- );
- }
- $src = $db_from->select('gallery_item', '*');
- $db_to->truncate('gallery_items');
- while ($row = $db_from->fetch_array($src)) {
- $db_to->insert(
- 'gallery_items',
- array(
- 'id' => $row['id'],
- 'gallery_id' => $row['gallery_id'],
- 'media_file' => $row['media_url'],
- 'thumbnail' => $row['media_url'],
- 'title' => $row['title'],
- 'description' => $this->gekko_path_fix($row['description']),
- 'author_id' => $row['author_id'],
- 'date_created' => $row['date_created'],
- 'date_modified' => $row['date_modified']
- )
- );
- }
- }
- }
- public function db_setup() {
- extract(
- $this->using(
- 'db',
- 'param',
- 'session',
- 'json',
- 'env'
- )
- );
- $param->not_empty(
- 'db.driver',
- 'db.hostname',
- 'db.database'
- );
- $driver = $this->db_driver($param->get('db.driver'));
-
- $session->clear('db_ok');
- if (file_exists($driver)) {
- require $driver;
- $copy = array(
- 'hostname',
- 'username',
- 'password',
- 'database',
- 'driver',
- 'prefix'
- );
- $class_name = $param->get('db.driver').'_driver';
- $conf = array();
- $db->db = new $class_name;
- foreach($copy as $el) {
- $db->db->$el = $param->get('db.'.$el);
- $conf[$el] = $db->db->$el;
- }
- if ($db->db->open(false)) {
- $session->set('_db', $conf);
- $mode = $param->get('mode');
- $session->set('db_ok', true);
- $env->redirect(sprintf('/action=%s', $mode));
- }
- } else {
- $driver = false;
- }
- $json->response(
- array(
- 'errorMessage' => $driver ? $db->get_error() : 'Selected database driver does not exists.'
- )
- );
- }
- public function install_conf() {
- extract(
- $this->using(
- 'param',
- 'session',
- 'json',
- 'env'
- )
- );
- $param->not_empty(
- 'conf.core.site_name',
- 'conf.core.site_slogan',
- 'user.username',
- 'user.email',
- 'user.new_password',
- 'user.repeat_password'
- );
- $this->session->set('_user', $param->get('user'));
- $this->session->set('_conf', $param->get('conf'));
- if ($param->get('user.new_password') == $param->get('user.repeat_password')) {
- $env->redirect('/action=install_confirm');
- } else {
- $env->error('Password do not match.');
- }
- }
- public function upgrade() {
- extract(
- $this->using(
- 'db',
- 'json',
- 'param',
- 'session'
- )
- );
- $db_conf = $session->get('_db');
- require $this->db_driver($db_conf['driver']);
-
- $db_driver = $db_conf['driver'].'_driver';
- $db->db = new $db_driver;
-
- foreach ($db_conf as $key => $value) {
- $db->db->{$key} = $value;
- }
- $db->open(false);
-
- $this->using('conf');
-
- $conf =& new conf($t = 1);
-
- $modules = $this->get_modules();
- foreach ($modules as $module) {
- $version = $conf->get($module.'/version');
- if ($version && ($conf->check_version('1.1', TM_VERSION) <= 0 && file_exists(TM_MODULES_DIR.$module))) {
- $version = $this->upgrade_module($db, $module);
- } else {
- $version = $this->install_module($db, $module);
- }
- $conf->set($module.'/version', $version);
- }
-
- $conf->set('core/version', TM_VERSION);
- $this->save_dbconf($db);
-
- $this->clean_cache();
- $json->response(
- array(
- 'successMessage' => 'Textmotion has been successfully upgraded.',
- 'redirectTo' => $param->create('/action=upgrade_success')
- )
- );
- }
- private function install_module(&$db, $module) {
- $xml =& $this->using('xml');
- $version = 1;
- $table_fields = array();
- $data = array();
- $file = TM_MODULES_DIR.$module.'/package.xml';
- if (file_exists($file)) {
-
- $xml->load($file);
-
- $test_version = $xml->get_value('package/module/version');
- if ($test_version) {
- $version = $test_version;
- }
- // tables
- $nodes = $xml->get_nodes('package/module/database/table');
- foreach($nodes['_nodes'] as $i => $v) {
- $table_node =& $nodes['_nodes'][$i]['table'];
- $table_name = $table_node['_attr']['name'];
- $table_fields[$table_name] = array();
- if (isset($table_node)) {
- foreach($table_node['_nodes'] as $j => $w) {
- $field_node =& $table_node['_nodes'][$j]['field'];
- if (isset($field_node)) {
- $table_fields[$table_name][] = $field_node['_attr'];
- }
- }
- }
- }
- // default data
- $nodes = $xml->get_nodes('package/module/database/entries/table');
- foreach($nodes['_nodes'] as $i => $v) {
- $table_node =& $nodes['_nodes'][$i]['table'];
- if (isset($table_node)) {
- $data[$table_node['_attr']['name']] = array();
- foreach($table_node['_nodes'] as $j => $w) {
- $row_node =& $table_node['_nodes'][$j]['row'];
- $fields = array();
- if (isset($row_node)) {
- foreach ($row_node['_nodes'] as $k => $x) {
- $field_node =& $row_node['_nodes'][$k]['field'];
- if (isset($field_node)) {
- $fields[$field_node['_attr']['name']] = $field_node['_data'];
- }
- }
- $data[$table_node['_attr']['name']][] = $fields;
- }
- }
- }
- }
- foreach ($table_fields as $table => $fields) {
- $sql = array();
- foreach ($fields as $i => $v) {
- $v = array_merge(
- array(
- 'empty' => true,
- 'key' => false,
- 'extra' => null,
- 'default' => null
- ),
- $v
- );
- $v['empty'] = ($v['empty'] == 'true');
- switch($db->db->driver) {
- case 'mysql':
- if ($v['default'] && !preg_match('/^(CURRENT_TIMESTAMP)$/', $v['default'])) {
- $v['default'] = '\''.$db->escape($v['default']).'\'';
- }
- $fields[$i] = $db->escape($v['name']).' '.$v['type'].' '.($v['empty'] == false ? 'not null' : '').' '.($v['default'] ? 'default '.$v['default'] : '').' '.($v['extra']).' '.($v['key'] == 'PRI' ? ', PRIMARY KEY ('.$db->escape($v['name']).')' : '').' '.($v['key'] == 'MUL' ? ', KEY ('.$db->escape($v['name']).')' : '');
- break;
- }
- }
- switch($db->db->driver) {
- case 'mysql':
- $sql[] = 'DROP TABLE IF EXISTS '.$db->table_name($table);
- $sql[] = 'CREATE TABLE '.$db->table_name($table).' ('.implode(', ', $fields).')';
- break;
- }
- foreach($sql as $q) {
- $db->query($q);
- }
- }
- foreach ($data as $table => $inserts) {
- foreach ($inserts as $insert) {
- $db->insert($table, $insert);
- }
- }
- }
- return $version;
- }
- private function upgrade_module(&$db, $module) {
- extract(
- $this->using(
- 'conf',
- 'xml'
- )
- );
-
- $version = $conf->get($module.'/version');
- $file = TM_MODULES_DIR.$module.'/package.xml';
- if (file_exists($file)) {
-
- $xml->load($file);
-
- $package_version = $xml->get_value('package/module/version');
- $need_upgrade = $conf->check_version($package_version, $version);
- $alter = array();
- $update = array();
- if ($need_upgrade > 0) {
- $orig_version = $version;
- $version = $package_version;
-
- $nodes = $xml->get_nodes('package/module/database/upgrade/changeset');
- if ($nodes) {
-
- foreach($nodes['_nodes'] as $i => $v) {
- $changeset_node =& $nodes['_nodes'][$i]['changeset'];
- if (isset($changeset_node)) {
- debug('>> '.$changeset_node['_attr']['version']);
- debug('** '.$orig_version);
- debug('%% '.$conf->check_version($changeset_node['_attr']['version'], $orig_version));
-
- if ($conf->check_version($changeset_node['_attr']['version'], $orig_version) <= 0) {
- continue;
- }
- foreach($changeset_node['_nodes'] as $j => $w) {
- $table_node =& $changeset_node['_nodes'][$j]['table'];
- if (isset($table_node)) {
- $table_name = $table_node['_attr']['name'];
- foreach($table_node['_nodes'] as $k => $x) {
-
- $add_node =& $table_node['_nodes'][$k]['add'];
- $update_node =& $table_node['_nodes'][$k]['update'];
- $modify_node =& $table_node['_nodes'][$k]['modify'];
- $delete_node =& $table_node['_nodes'][$k]['delete'];
- $drop_node =& $table_node['_nodes'][$k]['drop'];
- $copy_node =& $table_node['_nodes'][$k]['copy'];
- $create_node =& $table_node['_nodes'][$k]['create'];
- $rename_node =& $table_node['_nodes'][$k]['rename'];
- if (isset($create_node)) {
- $from_table = $copy_node['_attr']['from'];
- $fields = array();
- $values = array();
- foreach($create_node['_nodes'] as $l => $y) {
- $field_node = $create_node['_nodes'][$l]['field'];
- if (isset($field_node)) {
- $attr = array_merge(
- array(
- 'empty' => true,
- 'key' => false,
- 'extra' => null,
- 'default' => null
- ),
- $field_node['_attr']
- );
- $fields[] = $db->escape($attr['name']).' '.$attr['type'].' '.($attr['empty'] ? '': 'NOT NULL').' '.($attr['default'] ? 'DEFAULT '.($attr['is_field'] ? $attr['default'] : $db->bind('?', $attr['default'])).'' : '').' '.$attr['extra'].' '.($attr['key'] ? ', PRIMARY KEY ('.$db->escape($attr['name']).')' : '');
- }
- }
- $db->query('
- CREATE TABLE '.$db->table_name($table_name).'
- ('.implode(', ', $fields).')
- ');
- }
- if (isset($copy_node)) {
- $from_table = $copy_node['_attr']['from'];
- $fields = array();
- $values = array();
- foreach($copy_node['_nodes'] as $l => $y) {
- $field_node = $copy_node['_nodes'][$l]['field'];
- if (isset($field_node)) {
- $fields[] = $field_node['_attr']['name'];
- if (isset($field_node['_attr']['copy'])) {
- $values[] = $field_node['_attr']['copy'];
- } else {
- $values[] = '\''.$db->escape($field_node['_data']).'\'';
- }
- }
- }
- $db->query('
- INSERT INTO '.$db->table_name($table_name).'
- (`'.implode('`, `', $fields).'`)
- SELECT
- '.implode(', ', $values).'
- FROM '.$db->table_name($from_table).'
- ');
- }
- if (isset($drop_node)) {
- $db->query('DROP TABLE '.$db->table_name($table_name).'');
- }
- if (isset($delete_node)) {
- foreach($delete_node['_nodes'] as $l => $y) {
- $field_node = $delete_node['_nodes'][$l]['field'];
- if (isset($field_node)) {
- $attr = $field_node['_attr'];
- $db->query('
- ALTER TABLE '.$db->table_name($table_name).'
- DROP COLUMN '.$attr['name'].'
- ');
- }
- }
- }
-
- if (isset($modify_node)) {
- foreach($modify_node['_nodes'] as $l => $y) {
- $field_node = $modify_node['_nodes'][$l]['field'];
- if (isset($field_node)) {
- $attr = array_merge(
- array(
- 'empty' => true,
- 'after' => null,
- 'default' => null
- ),
- $field_node['_attr']
- );
- $db->query('
- ALTER TABLE '.$db->table_name($table_name).'
- MODIFY '.$attr['name'].' '.$attr['type'].' '.($attr['empty'] ? '': 'NOT NULL').' '.($attr['default'] ? 'DEFAULT '.$db->bind('?', $attr['default'…
Large files files are truncated, but you can click here to view the full file