PageRenderTime 62ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/upload/install/update/update.php

https://github.com/BlackCatDevelopment/BlackCatCMS
PHP | 344 lines | 236 code | 30 blank | 78 comment | 23 complexity | 38a10aa7aa92cec07dbb6635b0e4d0d4 MD5 | raw file
Possible License(s): GPL-3.0, Apache-2.0
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 3 of the License, or (at
  6. * your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * @author Black Cat Development
  17. * @copyright 2016, Black Cat Development
  18. * @link https://blackcat-cms.org
  19. * @license http://www.gnu.org/licenses/gpl.html
  20. * @category CAT_Core
  21. * @package CAT_Core
  22. *
  23. */
  24. define('CAT_INSTALL',true);
  25. #define('CAT_INSTALL_PROCESS',true);
  26. define('CAT_LOGFILE',dirname(__FILE__).'/../../temp/update.log');
  27. //**************************************************************************
  28. // add framework subdir to include path
  29. //**************************************************************************
  30. set_include_path(implode(PATH_SEPARATOR, array(
  31. realpath(dirname(__FILE__) . '/framework'),
  32. get_include_path()
  33. )));
  34. //**************************************************************************
  35. // register autoloader
  36. //**************************************************************************
  37. spl_autoload_register(function($class)
  38. {
  39. $file = str_replace('_', '/', $class);
  40. if (file_exists(dirname(__FILE__).'/../../framework/' . $file . '.php'))
  41. {
  42. @require dirname(__FILE__).'/../../framework/' . $file . '.php';
  43. }
  44. // next in stack
  45. });
  46. $lang = CAT_Helper_I18n::getInstance();
  47. $lang->addFile( $lang->getLang().'.php', dirname(__FILE__).'/../languages' );
  48. // allow upgrade vom v1.2, too
  49. if(!isset($_GET['do']) && CAT_Helper_Addons::versionCompare(CAT_VERSION,'1.2','<'))
  50. update11to12pre();
  51. // keep wb2compat.php happy
  52. foreach(array_values(array('DEFAULT_THEME','CATMAILER_DEFAULT_SENDERNAME','DEFAULT_TIMEZONE_STRING','SERVER_EMAIL')) as $const) {
  53. define($const,'');
  54. }
  55. define('LANGUAGE','EN');
  56. @require_once dirname(__FILE__).'/../../config.php';
  57. $result = $database->query(sprintf("SELECT `value` FROM `%ssettings` WHERE `name`='%s'",CAT_TABLE_PREFIX,'cat_version'));
  58. if($result->rowCount() > 0)
  59. {
  60. $row = $result->fetch();
  61. define('CAT_VERSION',$row['value']);
  62. }
  63. // Try to guess installer URL
  64. $installer_uri = (isset($_SERVER['HTTPS']) ? 'https://' : 'http://')
  65. . $_SERVER["SERVER_NAME"]
  66. . (($_SERVER['SERVER_PORT']!=80 && !isset($_SERVER['HTTPS']))
  67. ? ':'.$_SERVER['SERVER_PORT']
  68. : '' )
  69. . $_SERVER["SCRIPT_NAME"];
  70. $installer_uri = dirname( $installer_uri );
  71. $installer_uri = str_ireplace('update','',$installer_uri);
  72. if(!CAT_Helper_Addons::versionCompare( CAT_VERSION, '1.1' ))
  73. pre_update_error($lang->translate(
  74. 'You need to have <strong>BlackCat CMS v1.1</strong> installed to use the Update.<br />You have <strong>{{version}}</strong> installed.',
  75. array( 'version' => CAT_VERSION )
  76. ));
  77. // get new version from tag.txt
  78. if ( file_exists(dirname(__FILE__).'/../tag.txt') )
  79. {
  80. $tag = fopen( dirname(__FILE__).'/../tag.txt', 'r' );
  81. list ( $current_version, $current_build, $current_build ) = explode( '#', fgets($tag) );
  82. fclose($tag);
  83. }
  84. else
  85. {
  86. pre_update_error($lang->translate(
  87. 'The file <pre>tag.txt</pre> is missing! Unable to upgrade!'
  88. ));
  89. exit;
  90. }
  91. if(!CAT_Helper_Validate::getInstance()->sanitizeGet('do'))
  92. {
  93. update_wizard_header();
  94. echo '
  95. <h1>BlackCat CMS Update Wizard</h1>
  96. <h2>'.$lang->translate('Welcome!').'</h2>
  97. '.$lang->translate('This wizard will help you to upgrade your current BlackCat CMS Version').'<br />
  98. <span style="font-weight:bold;color:#f00;">'.CAT_VERSION.'</span><br />
  99. '.$lang->translate('to Version').'<br />
  100. <span style="font-weight:bold;color:#f00;">'.$current_version.' Build '.$current_build.'</span>
  101. <form method="get" action="'.$installer_uri.'/update/update.php">
  102. <input type="hidden" name="do" value="1" />
  103. <input type="submit" value="'.$lang->translate('To start the update, please click here').'" />
  104. </form>
  105. ';
  106. update_wizard_footer();
  107. }
  108. /*******************************************************************************
  109. * DO THE UPDATE
  110. ******************************************************************************/
  111. ob_start();
  112. /*******************************************************************************
  113. * 1.1 TO 1.2
  114. ******************************************************************************/
  115. $database->query(
  116. "CREATE TABLE IF NOT EXISTS `:prefix:dashboard` (
  117. `id` int(11) NOT NULL AUTO_INCREMENT,
  118. `user_id` int(11) NOT NULL DEFAULT '0',
  119. `module` varchar(50) DEFAULT '0',
  120. `layout` varchar(10) NOT NULL,
  121. `widgets` text NOT NULL,
  122. PRIMARY KEY (`id`),
  123. UNIQUE KEY `id_user_id_module` (`user_id`,`module`)
  124. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"
  125. );
  126. /*******************************************************************************
  127. 1.2 TO 1.2.1
  128. *******************************************************************************/
  129. $sql = "UPDATE `:prefix:system_permissions` SET `perm_bit`=:val WHERE `perm_name`=:perm";
  130. $database->query( $sql, array('val'=>1,'perm'=>'settings'));
  131. $database->query( $sql, array('val'=>2,'perm'=>'settings_basic'));
  132. $database->query( $sql, array('val'=>4,'perm'=>'settings_advanced'));
  133. // update module versions
  134. $sql = "UPDATE `:prefix:addons` SET `upgraded`=:time, `version`=:ver WHERE `directory`=:dir";
  135. foreach(array_values(array('lib_getid3','lib_wblib','wysiwyg')) as $module)
  136. {
  137. $addon_dir = CAT_PATH.'/modules/'.$module;
  138. $addon_info = CAT_Helper_Addons::checkInfo($addon_dir);
  139. $database->query( $sql, array('time' => time(), 'ver' => $addon_info['module_version'], 'dir' => $addon_info['module_directory'] ) );
  140. }
  141. /*******************************************************************************
  142. add missing database entries for addons catalog
  143. *******************************************************************************/
  144. $database->query("INSERT IGNORE INTO `:prefix:class_secure` (`module`, `filepath`) VALUES (0, '/backend/addons/ajax_get_template.php');");
  145. $database->query("INSERT IGNORE INTO `:prefix:class_secure` (`module`, `filepath`) VALUES (0, '/backend/addons/ajax_update_catalog.php');");
  146. /*******************************************************************************
  147. ALL VERSIONS
  148. *******************************************************************************/
  149. // delete templates cache (the folder will be re-created by the DwooDriver)
  150. $temp_path = CAT_Helper_Directory::sanitizePath(dirname(__FILE__).'/../../temp/');
  151. CAT_Helper_Directory::removeDirectory($temp_path.'/compiled');
  152. /*******************************************************************************
  153. ALL VERSIONS: update version info
  154. *******************************************************************************/
  155. $database->query(sprintf(
  156. 'UPDATE `%ssettings` SET `value`="%s" WHERE `name`="%s"',
  157. CAT_TABLE_PREFIX, $current_version, 'cat_version'
  158. ));
  159. $database->query(sprintf(
  160. 'UPDATE `%ssettings` SET `value`="%s" WHERE `name`="%s"',
  161. CAT_TABLE_PREFIX, $current_build, 'cat_build'
  162. ));
  163. ob_end_clean();
  164. /*******************************************************************************
  165. *******************************************************************************/
  166. $installer_uri = str_replace('/update','',$installer_uri);
  167. update_wizard_header();
  168. echo '
  169. <h2>'.$lang->translate('Update done').'</h2>
  170. <form method="get" action="'.CAT_ADMIN_URL.'">
  171. <input type="submit" value="'.$lang->translate('Click here to enter the backend').'" />
  172. </form>
  173. ';
  174. update_wizard_footer();
  175. exit;
  176. function pre_update_error( $msg ) {
  177. global $installer_uri, $lang;
  178. update_wizard_header(true);
  179. echo'
  180. <div style="float:left">
  181. <img src="templates/default/images/fail.png" alt="Fail" title="Fail" />
  182. </div>
  183. <h1>BlackCat CMS Update Prerequistes Error</h1>
  184. <h2>'.$lang->translate('Sorry, the BlackCat CMS Update prerequisites check failed.').'</h2>
  185. <span style="display:inline-block;background-color:#343434;color:#ff3030;font-size:1.5em;border:1px solid #ff3030;padding:15px;width:100%;margin:15px auto;-webkit-border-radius: 8px;-moz-border-radius: 8px;-khtml-border-radius: 8px;border-radius: 8px;">'.$msg.'</span><br /><br />
  186. <h2>'.$lang->translate('You will need to fix the errors quoted above to start the installation.').'</h2>';
  187. update_wizard_footer();
  188. } // end function pre_update_error()
  189. function update_wizard_header($is_error=false) {
  190. global $installer_uri, $lang;
  191. $header = $is_error
  192. ? 'BlackCat CMS Update Prerequistes Error'
  193. : 'BlackCat CMS Update Wizard'
  194. ;
  195. echo '<?xml version="1.0" encoding="UTF-8"?>
  196. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  197. "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  198. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
  199. <head>
  200. <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
  201. <title>'.$header.'</title>
  202. <link rel="stylesheet" href="'.$installer_uri.'templates/default/index.css" type="text/css" />
  203. </head>
  204. <body>
  205. <div style="width:800px;min-width:800px;margin:auto;margin-top:20%;text-align:center;color:#5AA2DA;">
  206. <div style="float:left;width:100%;">';
  207. }
  208. function update_wizard_footer() {
  209. echo '
  210. </div>
  211. </div>
  212. <div id="header">
  213. <div>Update Wizard</div>
  214. </div>
  215. <div id="footer">
  216. <div style="float:left;margin:0;padding:0;padding-left:50px;"><h3>enjoy the difference!</h3></div>
  217. <div>
  218. <!-- Please note: the below reference to the GNU GPL should not be removed, as it provides a link for users to read about warranty, etc. -->
  219. <a href="http://blackcat-cms.org" title="BlackCat CMS" target="_blank">BlackCat CMS Core</a> is released under the
  220. <a href="http://www.gnu.org/licenses/gpl.html" title="BlackCat CMS Core is GPL" target="_blank">GNU General Public License</a>.<br />
  221. <!-- Please note: the above reference to the GNU GPL should not be removed, as it provides a link for users to read about warranty, etc. -->
  222. <a href="http://blackcat-cms.org" title="BlackCat CMS Bundle" target="_blank">BlackCat CMS Bundle</a> is released under several different licenses.
  223. </div>
  224. </div>
  225. </body>
  226. </html>
  227. ';
  228. exit;
  229. }
  230. /*******************************************************************************
  231. 1.1 TO 1.2: We must create the new database settings file first!
  232. Note: We cannot include / require the original config.php as it will
  233. cause lots of errors
  234. *******************************************************************************/
  235. function update11to12pre()
  236. {
  237. $db_config_file_path = CAT_Helper_Directory::sanitizePath(dirname(__FILE__).'/../../framework/CAT/Helper/DB');
  238. if(is_dir($db_config_file_path)) {
  239. // find file
  240. // note: .bc.php as suffix filter does not work!
  241. $configfiles = CAT_Helper_Directory::scanDirectory(dirname(__FILE__).'/../../framework/CAT/Helper/DB',true,true,NULL,array('php'),NULL,array('index.php'));
  242. } else {
  243. mkdir($db_config_file_path,'0755');
  244. }
  245. if(!is_array($configfiles) || !count($configfiles))
  246. {
  247. include dirname(__FILE__).'/../admin_dummy.inc.php';
  248. $admin = new admin_dummy();
  249. // get the DB config from config.php
  250. $config = file_get_contents(dirname(__FILE__).'/../../config.php');
  251. preg_match_all("~define\(\'CAT_(DB_\w+)[^,].+?\'([^\'].+?)\'~i",$config,$m);
  252. if(is_array($m) && count($m)) {
  253. $db = array();
  254. for($i=0;$i<count($m[0]);$i++) {
  255. $db[$m[1][$i]] = $m[2][$i];
  256. }
  257. $db_config_content = "
  258. ;<?php
  259. ;die(); // For further security
  260. ;/*
  261. [CAT_DB]
  262. TYPE=mysql
  263. HOST=".$db['DB_HOST']."
  264. PORT=".$db['DB_PORT']."
  265. USERNAME=".$db['DB_USERNAME']."
  266. PASSWORD=\"".$db['DB_PASSWORD']."\"
  267. NAME=".$db['DB_NAME']."
  268. ;*/
  269. ;?>
  270. ";
  271. // save database settings; we generate a file name here
  272. $db_settings_file = $db_config_file_path.'/'.$admin->createGUID('').'.bc.php';
  273. write2log('trying to create '.$db_settings_file);
  274. if(($handle = @fopen($db_settings_file, 'w')) === false) {
  275. write2log('!!!ERROR!!! Cannot create database settings file ['.$db_settings_file.']');
  276. pre_update_error('!!!ERROR!!! Cannot create database settings file ['.$db_settings_file.']');
  277. exit;
  278. } else {
  279. if (fwrite($handle, $db_config_content, strlen($db_config_content) ) === FALSE) {
  280. write2log('!!!ERROR!!! Cannot write to database settings file ['.$db_settings_file.']');
  281. fclose($handle);
  282. pre_update_error('!!!ERROR!!! Cannot write to database settings file ['.$db_settings_file.']');
  283. exit;
  284. }
  285. }
  286. write2log('>>> ok');
  287. // remove DB config from config.php
  288. write2log('removing db settings from config.php');
  289. $config = preg_replace("~define\(\'CAT_(DB_\w+).*~i","",$config);
  290. $config = preg_replace("~\n\n+~","\n\n",$config);
  291. $fh = fopen(dirname(__FILE__).'/../../config.php','w');
  292. fwrite($fh,$config);
  293. ftruncate($fh,ftell($fh));
  294. fclose($fh);
  295. // remove index.php
  296. if(file_exists($db_config_file_path.'/index.php'))
  297. {
  298. unlink($db_config_file_path.'/index.php');
  299. }
  300. }
  301. }
  302. }
  303. function write2log($msg)
  304. {
  305. global $depth;
  306. if(substr($msg,0,1) == '<') $depth--;
  307. $logh = fopen(CAT_LOGFILE,'a');
  308. fwrite($logh,str_repeat(' ',$depth) . $msg."\n");
  309. fclose($logh);
  310. if(substr($msg,0,1) == '>') $depth++;
  311. }