PageRenderTime 73ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/phpmyfaq/install/update.php

https://github.com/cyrke/phpMyFAQ
PHP | 866 lines | 792 code | 37 blank | 37 comment | 31 complexity | 4941ce3f6edd4b6a8e8e565818ff2238 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * Main update script
  4. *
  5. * PHP Version 5.3
  6. *
  7. * This Source Code Form is subject to the terms of the Mozilla Public License,
  8. * v. 2.0. If a copy of the MPL was not distributed with this file, You can
  9. * obtain one at http://mozilla.org/MPL/2.0/.
  10. *
  11. * @category phpMyFAQ
  12. * @package Setup
  13. * @author Thorsten Rinne <thorsten@phpmyfaq.de>
  14. * @author Thomas Melchinger <t.melchinger@uni.de>
  15. * @author Matteo Scaramuccia <matteo@phpmyfaq.de>
  16. * @copyright 2002-2012 phpMyFAQ Team
  17. * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
  18. * @link http://www.phpmyfaq.de
  19. * @since 2002-01-10
  20. */
  21. define('COPYRIGHT', '&copy; 2001-2012 <a href="http://www.phpmyfaq.de/">phpMyFAQ Team</a> | Follow us on <a href="http://twitter.com/phpMyFAQ">Twitter</a> | All rights reserved.');
  22. define('PMF_ROOT_DIR', dirname(dirname(__FILE__)));
  23. define('IS_VALID_PHPMYFAQ', null);
  24. if ((@ini_get('safe_mode') != 'On' || @ini_get('safe_mode') !== 1)) {
  25. set_time_limit(0);
  26. }
  27. require PMF_ROOT_DIR . '/inc/Bootstrap.php';
  28. $step = PMF_Filter::filterInput(INPUT_GET, 'step', FILTER_VALIDATE_INT, 1);
  29. $version = PMF_Filter::filterInput(INPUT_POST, 'version', FILTER_SANITIZE_STRING);
  30. $query = array();
  31. if (file_exists(PMF_ROOT_DIR.'/inc/data.php')) {
  32. require PMF_ROOT_DIR . '/inc/data.php'; // before 2.6.0-alpha
  33. } else {
  34. if (!file_exists(PMF_ROOT_DIR . '/config/database.php')) {
  35. header("Location: setup.php");
  36. exit();
  37. }
  38. require PMF_ROOT_DIR . '/config/database.php'; // after 2.6.0-alpha
  39. }
  40. ?>
  41. <!doctype html>
  42. <!--[if lt IE 7 ]> <html lang="en" class="no-js ie6"> <![endif]-->
  43. <!--[if IE 7 ]> <html lang="en" class="no-js ie7"> <![endif]-->
  44. <!--[if IE 8 ]> <html lang="en" class="no-js ie8"> <![endif]-->
  45. <!--[if IE 9 ]> <html lang="en" class="no-js ie9"> <![endif]-->
  46. <!--[if (gt IE 9)|!(IE)]><!--> <html lang="en" class="no-js"> <!--<![endif]-->
  47. <head>
  48. <meta charset="utf-8">
  49. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  50. <title>phpMyFAQ <?php echo PMF_System::getVersion(); ?> Update</title>
  51. <meta name="viewport" content="width=device-width;">
  52. <meta name="application-name" content="phpMyFAQ <?php echo PMF_System::getVersion(); ?>">
  53. <meta name="copyright" content="(c) 2001-<?php echo date('Y'); ?> phpMyFAQ Team">
  54. <link rel="stylesheet" href="../assets/template/default/css/style.css?v=1">
  55. <script src="../assets/js/libs/modernizr.min.js"></script>
  56. <script src="../assets/js/libs/jquery.min.js"></script>
  57. <link rel="shortcut icon" href="../assets/template/default/favicon.ico">
  58. <link rel="apple-touch-icon" href="../assets/template/default/apple-touch-icon.png">
  59. </head>
  60. <body>
  61. <!--[if lt IE 8 ]>
  62. <div class="internet-explorer-error">
  63. Do you know that your Internet Explorer is out of date?<br/>
  64. Please use Internet Explorer 8+, Mozilla Firefox 4+, Google Chrome, Apple Safari 5+ or Opera 11+
  65. </div>
  66. <![endif]-->
  67. <div class="navbar navbar-fixed-top">
  68. <div class="navbar-inner">
  69. <div class="container">
  70. <nav class="nav-collapse">
  71. <ul class="nav">
  72. <li><a target="_blank" href="http://www.phpmyfaq.de/documentation.php">Documentation</a></li>
  73. <li><a target="_blank" href="http://www.phpmyfaq.de/support.php">Support</a></li>
  74. <li><a target="_blank" href="http://forum.phpmyfaq.de/">Forums</a></li>
  75. <li><a target="_blank" href="http://faq.phpmyfaq.de/">FAQ</a></li>
  76. <li class="divider-vertical"></li>
  77. <li><a href="../">Back to your FAQ</a></li>
  78. </ul>
  79. </nav>
  80. </div>
  81. </div>
  82. </div>
  83. <section id="main">
  84. <div class="container">
  85. <div class="row" style="padding-left: 20px;">
  86. <div class="hero-unit hello-phpmyfaq" style="text-align: center; height: 55px;">
  87. <h1>phpMyFAQ <?php echo PMF_System::getVersion(); ?> Update</h1>
  88. </div>
  89. </div>
  90. <div class="row" style="padding-left: 20px;">
  91. <div class="span1">&nbsp;</div>
  92. <div class="span10">
  93. <?php
  94. $installer = new PMF_Installer();
  95. $installer->checkPreUpgrade();
  96. /**************************** STEP 1 OF 3 ***************************/
  97. if ($step === 1) {
  98. $version = $faqConfig->get('main.currentVersion');
  99. ?>
  100. <form action="update.php?step=2" method="post">
  101. <input name="version" type="hidden" value="<?php echo $version; ?>"/>
  102. <fieldset>
  103. <legend>
  104. <strong>
  105. phpMyFAQ <?php echo PMF_System::getVersion(); ?> Update (Step 1 of 3)
  106. </strong>
  107. </legend>
  108. <p class="alert alert-info">
  109. <strong>
  110. Please create a full backup of your database, your templates, attachments and uploaded images
  111. before running this update.
  112. </strong>
  113. </p>
  114. <?php
  115. if (version_compare($version, '2.8.0-alpha2', '>=')) {
  116. if (! $faqConfig->get('main.maintenanceMode')) {
  117. echo '<p class="alert alert-warning"><strong>Warning!</strong> Your phpMyFAQ installation is not in ' .
  118. 'maintenance mode, you should enable the maintenance mode in your administration backend.</p>';
  119. }
  120. }
  121. ?>
  122. <p>This update script will work <strong>only</strong> for the following versions:</p>
  123. <ul>
  124. <li>phpMyFAQ 2.5.x (out of support since mid of 2010)</li>
  125. <li>phpMyFAQ 2.6.x (out of support since end of 2011)</li>
  126. <li>phpMyFAQ 2.7.x</li>
  127. <li>phpMyFAQ 2.8.x</li>
  128. </ul>
  129. <p>This update script <strong>will not</strong> work for the following versions:</p>
  130. <ul>
  131. <li>phpMyFAQ 0.x</li>
  132. <li>phpMyFAQ 1.x</li>
  133. <li>phpMyFAQ 2.0.x</li>
  134. </ul>
  135. <?php
  136. // 2.5 versions only
  137. if (version_compare($version, '2.6.0-alpha', '<') && !is_writeable('../template')) {
  138. echo '<p class="alert alert-error"><strong>Please change the directory ../template and its ' .
  139. 'contents writable (777 on Linux/UNIX).</strong></p>';
  140. }
  141. // We only support updates from 2.5+
  142. if (version_compare($version, '2.5.0', '>')) {
  143. printf(
  144. '<p class="alert alert-success">Your current phpMyFAQ version: %s</p>',
  145. $version
  146. );
  147. } else {
  148. printf(
  149. '<p class="alert alert-error">Your current phpMyFAQ version: %s</p>',
  150. $version
  151. );
  152. echo '<p>Please update to the latest phpMyFAQ 2.7 version first.</p>';
  153. }
  154. if ('hash' !== PMF_ENCRYPTION_TYPE) {
  155. printf(
  156. '<p class="alert alert-info">Your passwords are currently encoded with a %s() method.</p>',
  157. PMF_ENCRYPTION_TYPE
  158. );
  159. }
  160. ?>
  161. <p class="alert alert-danger">
  162. Dude, this is an early alpha version. Please don't update your production version!
  163. </p>
  164. <p style="text-align: center">
  165. <button class="btn btn-primary btn-large" type="submit">
  166. Go to step 2 of 3
  167. </button>
  168. </p>
  169. </fieldset>
  170. </form>
  171. <?php
  172. PMF_System::renderFooter();
  173. }
  174. /**************************** STEP 2 OF 3 ***************************/
  175. if ($step == 2) {
  176. $checkDatabaseSetupFile = $checkLdapSetupFile = $checkTemplateDirectory = false;
  177. // First backup old inc/data.php, then backup new config/bak.database.php and copy inc/data.php
  178. // to config/database.php
  179. // This is needed for 2.5 updates only
  180. if (file_exists(PMF_ROOT_DIR . '/inc/data.php')) {
  181. if (!copy(PMF_ROOT_DIR . '/inc/data.php', PMF_ROOT_DIR . '/config/database.bak.php') ||
  182. !copy(PMF_ROOT_DIR . '/inc/data.php', PMF_ROOT_DIR . '/config/database.php')) {
  183. echo "<p class=\"alert alert-error\"><strong>Error:</strong> The backup file ../config/database.bak.php " .
  184. "could not be written. Please correct this!</p>";
  185. } else {
  186. $checkDatabaseSetupFile = true;
  187. }
  188. }
  189. // The backup an existing config/database.php
  190. // 2.6+ updates
  191. if (file_exists(PMF_ROOT_DIR . '/config/database.php')) {
  192. if (!copy(PMF_ROOT_DIR . '/config/database.php', PMF_ROOT_DIR . '/config/database.bak.php')) {
  193. echo "<p class=\"alert alert-error\"><strong>Error:</strong> The backup file ../config/database.bak.php " .
  194. "could not be written. Please correct this!</p>";
  195. } else {
  196. $checkDatabaseSetupFile = true;
  197. }
  198. }
  199. // Now backup and move LDAP setup if available
  200. // This is needed for 2.5+ updates with a LDAP configuration file
  201. if (file_exists(PMF_ROOT_DIR . '/inc/dataldap.php')) {
  202. if (!copy(PMF_ROOT_DIR . '/inc/dataldap.php', PMF_ROOT_DIR . '/config/ldap.bak.php') ||
  203. !copy(PMF_ROOT_DIR . '/inc/dataldap.php', PMF_ROOT_DIR . '/config/ldap.php')) {
  204. echo "<p class=\"alert alert-error\"><strong>Error:</strong> The backup file ../config/ldap.bak.php " .
  205. "could not be written. Please correct this!</p>";
  206. } else {
  207. $checkLdapSetupFile = true;
  208. }
  209. }
  210. $oldTemplateDir = '../template'; // 2.5 -> 2.8.0-alpha
  211. $newTemplateDir = '../assets/template'; // 2.8.0-alpha2 and later
  212. $notWritableFiles = array();
  213. if (version_compare($version, '2.6.0-alpha', '<')) {
  214. foreach (new DirectoryIterator($oldTemplateDir) as $item) {
  215. if ($item->isFile() && !$item->isWritable()) {
  216. $notWritableFiles[] = "$oldTemplateDir/{$item->getFilename()}";
  217. }
  218. }
  219. }
  220. if (version_compare($version, '2.6.0-alpha', '<') && (!is_writeable($oldTemplateDir) || !empty($notWritableFiles))) {
  221. if (!is_writeable($oldTemplateDir)) {
  222. printf(
  223. "<p class=\"alert alert-error\"><strong>The directory %s isn't writable.</strong></p>\n",
  224. $oldTemplateDir
  225. );
  226. }
  227. if (!empty($notWritableFiles)) {
  228. foreach ($notWritableFiles as $item) {
  229. printf("<p class=\"alert alert-error\"><strong>The file %s isn't writable.</strong></p>\n", $item);
  230. }
  231. }
  232. } else {
  233. $checkTemplateDirectory = true;
  234. }
  235. // is everything is okay?
  236. if ($checkDatabaseSetupFile && $checkTemplateDirectory) {
  237. ?>
  238. <form action="update.php?step=3" method="post">
  239. <input type="hidden" name="version" value="<?php echo $version; ?>" />
  240. <fieldset>
  241. <legend><strong>phpMyFAQ <?php echo PMF_System::getVersion(); ?> Update (Step 2 of 3)</strong></legend>
  242. <p>A backup of your database configuration file has been made.</p>
  243. <p>The configuration will be updated after the next step.</p>
  244. <p style="text-align: center;">
  245. <button class="btn btn-primary btn-large" type="submit">
  246. Go to step 3 of 3
  247. </button>
  248. </p>
  249. </fieldset>
  250. </form>
  251. <?php
  252. PMF_System::renderFooter();
  253. } else {
  254. echo '<p class="alert alert-error"><strong>Error:</strong> Your version of phpMyFAQ could not updated.</p>';
  255. PMF_System::renderFooter();
  256. }
  257. }
  258. /**************************** STEP 3 OF 3 ***************************/
  259. if ($step == 3) {
  260. require_once PMF_ROOT_DIR . '/inc/Configuration.php';
  261. require_once PMF_ROOT_DIR . '/inc/Db.php';
  262. require_once PMF_ROOT_DIR . '/inc/DB/Driver.php';
  263. require_once PMF_ROOT_DIR . '/inc/Link.php';
  264. $images = array();
  265. //
  266. // UPDATES FROM 2.5.1
  267. //
  268. if (version_compare($version, '2.5.1', '<')) {
  269. // Truncate table and re-import all stopwords with the new Lithuanian ones
  270. $query[] = "DELETE FROM ". PMF_Db::getTablePrefix() . "faqstopwords";
  271. require 'stopwords.sql.php';
  272. }
  273. //
  274. // UPDATES FROM 2.5.2
  275. if (version_compare($version, '2.5.3', '<')) {
  276. $query[] = "UPDATE ". PMF_Db::getTablePrefix() . "faqconfig SET config_name = 'spam.enableCaptchaCode'
  277. WHERE config_name = 'spam.enableCatpchaCode'";
  278. }
  279. //
  280. // UPDATES FROM 2.6.0-alpha
  281. //
  282. if (version_compare($version, '2.6.0-alpha', '<')) {
  283. require '../lang/' . $faqConfig->get('main.language');
  284. if (isset($PMF_LANG['metaCharset']) && strtolower($PMF_LANG['metaCharset']) != 'utf-8') {
  285. // UTF-8 Migration
  286. switch($DB['type']) {
  287. case 'mysql':
  288. include 'mysql.utf8migration.php';
  289. break;
  290. case 'mysqli':
  291. include 'mysqli.utf8migration.php';
  292. break;
  293. default:
  294. echo '<p class="hint">Please read <a target="_blank" href="../docs/documentation.en.html">' .
  295. 'documenation</a> about migration to UTF-8.</p>';
  296. break;
  297. }
  298. }
  299. $faqConfig->add('main.enableUpdate', 'false');
  300. $faqConfig->add('security.useSslForLogins', 'false');
  301. $faqConfig->add('main.currentApiVersion', PMF_System::getApiVersion());
  302. $faqConfig->add('main.templateSet', 'default');
  303. $faqConfig->add('main.numberSearchTerms', '10');
  304. $faqConfig->add('records.orderingPopularFaqs', 'visits');
  305. // We did check in the first and second steps,
  306. // if the $templateDir and its contents are writable,
  307. // so now lets just backup existing templates
  308. $templateBackupDir = "$templateDir/backup";
  309. while (file_exists($templateBackupDir)) {
  310. $templateBackupDir = $templateBackupDir . mt_rand();
  311. }
  312. if (! mkdir($templateBackupDir, 0777)) {
  313. echo '<p class="alert alert-error">Couldn\'t create the templates backup directory.</p>';
  314. PMF_System::renderFooter();
  315. }
  316. foreach (new DirectoryIterator($templateDir) as $item) {
  317. if ($item->isFile() && $item->isWritable()) {
  318. rename("$templateDir/{$item->getFilename()}", "$templateBackupDir/{$item->getFilename()}");
  319. }
  320. }
  321. // Attachments stuff
  322. $faqConfig->add('records.attachmentsStorageType', '0');
  323. $faqConfig->add('records.enableAttachmentEncryption', 'false');
  324. $faqConfig->add('records.defaultAttachmentEncKey', '');
  325. switch($DB['type']) {
  326. case 'pgsql':
  327. $query[] = "CREATE TABLE " . PMF_Db::getTablePrefix() . "faqattachment (
  328. id SERIAL NOT NULL,
  329. record_id int4 NOT NULL,
  330. record_lang varchar(5) NOT NULL,
  331. real_hash char(32) NOT NULL,
  332. virtual_hash char(32) NOT NULL,
  333. password_hash char(40) NULL,
  334. filename varchar(255) NOT NULL,
  335. filesize int NOT NULL,
  336. encrypted int NOT NULL DEFAULT FALSE,
  337. mime_type varchar(255) NULL,
  338. PRIMARY KEY (id))";
  339. $query[] = "CREATE TABLE " . PMF_Db::getTablePrefix() . "faqattachment_file (
  340. virtual_hash char(32) NOT NULL,
  341. contents bytea,
  342. PRIMARY KEY (virtual_hash))";
  343. break;
  344. case 'mysqli':
  345. case 'mysql':
  346. $query[] = "CREATE TABLE " . PMF_Db::getTablePrefix() . "faqattachment (
  347. id int(11) NOT NULL,
  348. record_id int(11) NOT NULL,
  349. record_lang varchar(5) NOT NULL,
  350. real_hash char(32) NOT NULL,
  351. virtual_hash char(32) NOT NULL,
  352. password_hash char(40) NULL,
  353. filename varchar(255) NOT NULL,
  354. filesize int NOT NULL,
  355. encrypted tinyint NOT NULL DEFAULT 0,
  356. mime_type varchar(255) NULL,
  357. PRIMARY KEY (id))";
  358. $query[] = "CREATE TABLE " . PMF_Db::getTablePrefix() . "faqattachment_file (
  359. virtual_hash char(32) NOT NULL,
  360. contents blob NOT NULL,
  361. PRIMARY KEY (virtual_hash))";
  362. break;
  363. default:
  364. /**
  365. * Just try standard SQL and hope for the best
  366. */
  367. $query[] = "CREATE TABLE " . PMF_Db::getTablePrefix() . "faqattachment (
  368. id int NOT NULL,
  369. record_id int NOT NULL,
  370. record_lang varchar(5) NOT NULL,
  371. hash char(33) NOT NULL,
  372. filename varchar(255) NOT NULL,
  373. file_contents blob,
  374. encrypted int,
  375. PRIMARY KEY (id))";
  376. break;
  377. }
  378. }
  379. //
  380. // UPDATES FROM 2.6.0-RC
  381. //
  382. if (version_compare($version, '2.6.0-RC', '<')) {
  383. $faqConfig->add('main.optionalMailAddress', 'false');
  384. $faqConfig->add('main.useAjaxSearchOnStartpage', 'false');
  385. }
  386. //
  387. // UPDATES FROM 2.6.99
  388. //
  389. if (version_compare($version, '2.6.99', '<')) {
  390. $faqConfig->add('search.relevance', 'thema,content,keywords');
  391. $faqConfig->add('search.enableRelevance', 'false');
  392. $faqConfig->add('main.enableGoogleTranslation', 'false');
  393. $faqConfig->add('main.googleTranslationKey', 'INSERT-YOUR-KEY');
  394. }
  395. //
  396. // UPDATES FROM 2.7.0-alpha
  397. //
  398. if (version_compare($version, '2.7.0-alpha', '<')) {
  399. // Add new config values
  400. $faqConfig->add('socialnetworks.enableTwitterSupport', 'false');
  401. $faqConfig->add('socialnetworks.twitterConsumerKey', '');
  402. $faqConfig->add('socialnetworks.twitterConsumerSecret', '');
  403. $faqConfig->add('socialnetworks.twitterAccessTokenKey', '');
  404. $faqConfig->add('socialnetworks.twitterAccessTokenSecret', '');
  405. $faqConfig->add('socialnetworks.enableFacebookSupport', 'false');
  406. // Migrate faqquestion table to new structure
  407. switch($DB['type']) {
  408. case 'pgsql':
  409. $query[] = "ALTER TABLE " . PMF_Db::getTablePrefix() . "faqquestions RENAME COLUMN ask_username TO username";
  410. $query[] = "ALTER TABLE " . PMF_Db::getTablePrefix() . "faqquestions RENAME COLUMN ask_usermail TO email";
  411. $query[] = "ALTER TABLE " . PMF_Db::getTablePrefix() . "faqquestions RENAME COLUMN ask_rubrik TO category_id";
  412. $query[] = "ALTER TABLE " . PMF_Db::getTablePrefix() . "faqquestions RENAME COLUMN ask_content TO question";
  413. $query[] = "ALTER TABLE " . PMF_Db::getTablePrefix() . "faqquestions RENAME COLUMN ask_date TO created";
  414. break;
  415. case 'mssql':
  416. case 'sqlsrv':
  417. $query[] = "EXEC sp_RENAME '" . PMF_Db::getTablePrefix() . "faqquestions.ask_username', 'username', 'COLUMN'";
  418. $query[] = "EXEC sp_RENAME '" . PMF_Db::getTablePrefix() . "faqquestions.ask_usermail', 'email', 'COLUMN'";
  419. $query[] = "EXEC sp_RENAME '" . PMF_Db::getTablePrefix() . "faqquestions.ask_rubrik', 'category_id', 'COLUMN'";
  420. $query[] = "EXEC sp_RENAME '" . PMF_Db::getTablePrefix() . "faqquestions.ask_content', 'question', 'COLUMN'";
  421. $query[] = "EXEC sp_RENAME '" . PMF_Db::getTablePrefix() . "faqquestions.ask_date', 'created', 'COLUMN'";
  422. break;
  423. case 'mysql':
  424. case 'mysqli':
  425. $query[] = "ALTER TABLE " . PMF_Db::getTablePrefix() . "faqquestions CHANGE ask_username username VARCHAR(100) NOT NULL";
  426. $query[] = "ALTER TABLE " . PMF_Db::getTablePrefix() . "faqquestions CHANGE ask_usermail email VARCHAR(100) NOT NULL";
  427. $query[] = "ALTER TABLE " . PMF_Db::getTablePrefix() . "faqquestions CHANGE ask_rubrik category_id INT(11) NOT NULL";
  428. $query[] = "ALTER TABLE " . PMF_Db::getTablePrefix() . "faqquestions CHANGE ask_content question TEXT NOT NULL";
  429. $query[] = "ALTER TABLE " . PMF_Db::getTablePrefix() . "faqquestions CHANGE ask_date created VARCHAR(20) NOT NULL";
  430. break;
  431. case 'sqlite':
  432. $query[] = "BEGIN TRANSACTION";
  433. $query[] = "CREATE TEMPORARY TABLE " . PMF_Db::getTablePrefix() . "faqquestions_temp (
  434. id int(11) NOT NULL,
  435. username varchar(100) NOT NULL,
  436. email varchar(100) NOT NULL,
  437. category_id int(11) NOT NULL,
  438. question text NOT NULL,
  439. created varchar(20) NOT NULL,
  440. is_visible char(1) default 'Y',
  441. PRIMARY KEY (id))";
  442. $query[] = "INSERT INTO " . PMF_Db::getTablePrefix() . "faqquestions_temp SELECT * FROM " . PMF_Db::getTablePrefix() . "faqquestions";
  443. $query[] = "DROP TABLE " . PMF_Db::getTablePrefix() . "faqquestions";
  444. $query[] = "CREATE TABLE " . PMF_Db::getTablePrefix() . "faqquestions (
  445. id int(11) NOT NULL,
  446. username varchar(100) NOT NULL,
  447. email varchar(100) NOT NULL,
  448. category_id int(11) NOT NULL,
  449. question text NOT NULL,
  450. created varchar(20) NOT NULL,
  451. is_visible char(1) default 'Y',
  452. PRIMARY KEY (id))";
  453. $query[] = "INSERT INTO " . PMF_Db::getTablePrefix() . "faqquestions SELECT * FROM " . PMF_Db::getTablePrefix() . "faqquestions_temp";
  454. $query[] = "DROP TABLE " . PMF_Db::getTablePrefix() . "faqquestions_temp";
  455. $query[] = "COMMIT";
  456. break;
  457. }
  458. $query[] = "INSERT INTO ". PMF_Db::getTablePrefix() . "faqright (right_id, name, description, for_users, for_groups) VALUES
  459. (34, 'addattachment', 'Right to add attachments', 1, 1)";
  460. $query[] = "INSERT INTO ". PMF_Db::getTablePrefix() . "faqright (right_id, name, description, for_users, for_groups) VALUES
  461. (35, 'editattachment', 'Right to edit attachments', 1, 1)";
  462. $query[] = "INSERT INTO ". PMF_Db::getTablePrefix() . "faqright (right_id, name, description, for_users, for_groups) VALUES
  463. (36, 'delattachment', 'Right to delete attachments', 1, 1)";
  464. $query[] = "INSERT INTO ". PMF_Db::getTablePrefix() . "faqright (right_id, name, description, for_users, for_groups) VALUES
  465. (37, 'dlattachment', 'Right to download attachments', 1, 1)";
  466. $query[] = "INSERT INTO ". PMF_Db::getTablePrefix() . "faquser_right (user_id, right_id) VALUES (1, 34)";
  467. $query[] = "INSERT INTO ". PMF_Db::getTablePrefix() . "faquser_right (user_id, right_id) VALUES (1, 35)";
  468. $query[] = "INSERT INTO ". PMF_Db::getTablePrefix() . "faquser_right (user_id, right_id) VALUES (1, 36)";
  469. $query[] = "INSERT INTO ". PMF_Db::getTablePrefix() . "faquser_right (user_id, right_id) VALUES (1, 37)";
  470. }
  471. //
  472. // UPDATES FROM 2.7.0-alpha2
  473. //
  474. if (version_compare($version, '2.7.0-alpha2', '<')) {
  475. $query[] = "INSERT INTO ". PMF_Db::getTablePrefix() . "faqright (right_id, name, description, for_users, for_groups) VALUES
  476. (38, 'reports', 'Right to generate reports', 1, 1)";
  477. $query[] = "INSERT INTO ". PMF_Db::getTablePrefix() . "faquser_right (user_id, right_id) VALUES (1, 38)";
  478. }
  479. //
  480. // UPDATES FROM 2.7.0-beta
  481. //
  482. if (version_compare($version, '2.7.0-beta', '<')) {
  483. $query[] = "UPDATE ". PMF_Db::getTablePrefix() . "faqconfig SET config_name = 'search.numberSearchTerms'
  484. WHERE config_name = 'main.numberSearchTerms'";
  485. $query[] = "UPDATE ". PMF_Db::getTablePrefix() . "faqconfig SET config_name = 'search.useAjaxSearchOnStartpage'
  486. WHERE config_name = 'main.useAjaxSearchOnStartpage'";
  487. }
  488. //
  489. // UPDATES FROM 2.7.0-beta2
  490. //
  491. if (version_compare($version, '2.7.0-beta2', '<')) {
  492. $faqConfig->add('security.ssoSupport', 'false');
  493. $faqConfig->add('security.ssoLogoutRedirect', '');
  494. $faqConfig->add('main.dateFormat', 'Y-m-d H:i');
  495. $faqConfig->add('security.enableLoginOnly', 'false');
  496. }
  497. //
  498. // UPDATES FROM 2.7.0-RC
  499. //
  500. if (version_compare($version, '2.7.0-RC', '<')) {
  501. $query[] = "UPDATE ". PMF_Db::getTablePrefix() . "faqconfig SET config_name = 'records.numberOfRecordsPerPage'
  502. WHERE config_name = 'main.numberOfRecordsPerPage'";
  503. $query[] = "UPDATE ". PMF_Db::getTablePrefix() . "faqconfig SET config_name = 'records.numberOfShownNewsEntries'
  504. WHERE config_name = 'main.numberOfShownNewsEntries'";
  505. $query[] = "UPDATE ". PMF_Db::getTablePrefix() . "faqconfig SET config_name = 'records.orderingPopularFaqs'
  506. WHERE config_name = 'main.orderingPopularFaqs'";
  507. $query[] = "UPDATE ". PMF_Db::getTablePrefix() . "faqconfig SET config_name = 'records.disableAttachments'
  508. WHERE config_name = 'main.disableAttachments'";
  509. $query[] = "UPDATE ". PMF_Db::getTablePrefix() . "faqconfig SET config_name = 'records.maxAttachmentSize'
  510. WHERE config_name = 'main.maxAttachmentSize'";
  511. $query[] = "UPDATE ". PMF_Db::getTablePrefix() . "faqconfig SET config_name = 'records.attachmentsPath'
  512. WHERE config_name = 'main.attachmentsPath'";
  513. $query[] = "UPDATE ". PMF_Db::getTablePrefix() . "faqconfig SET config_name = 'records.attachmentsStorageType'
  514. WHERE config_name = 'main.attachmentsStorageType'";
  515. $query[] = "UPDATE ". PMF_Db::getTablePrefix() . "faqconfig SET config_name = 'records.enableAttachmentEncryption'
  516. WHERE config_name = 'main.enableAttachmentEncryption'";
  517. $query[] = "UPDATE ". PMF_Db::getTablePrefix() . "faqconfig SET config_name = 'records.defaultAttachmentEncKey'
  518. WHERE config_name = 'main.defaultAttachmentEncKey'";
  519. $query[] = "UPDATE ". PMF_Db::getTablePrefix() . "faqconfig SET config_name = 'security.permLevel'
  520. WHERE config_name = 'main.permLevel'";
  521. $query[] = "UPDATE ". PMF_Db::getTablePrefix() . "faqconfig SET config_name = 'security.ipCheck'
  522. WHERE config_name = 'main.ipCheck'";
  523. $query[] = "UPDATE ". PMF_Db::getTablePrefix() . "faqconfig SET config_name = 'security.enableLoginOnly'
  524. WHERE config_name = 'main.enableLoginOnly'";
  525. $query[] = "UPDATE ". PMF_Db::getTablePrefix() . "faqconfig SET config_name = 'security.ldapSupport'
  526. WHERE config_name = 'main.ldapSupport'";
  527. $query[] = "UPDATE ". PMF_Db::getTablePrefix() . "faqconfig SET config_name = 'security.bannedIPs'
  528. WHERE config_name = 'main.bannedIPs'";
  529. $query[] = "UPDATE ". PMF_Db::getTablePrefix() . "faqconfig SET config_name = 'security.ssoSupport'
  530. WHERE config_name = 'main.ssoSupport'";
  531. $query[] = "UPDATE ". PMF_Db::getTablePrefix() . "faqconfig SET config_name = 'security.ssoLogoutRedirect'
  532. WHERE config_name = 'main.ssoLogoutRedirect'";
  533. $query[] = "UPDATE ". PMF_Db::getTablePrefix() . "faqconfig SET config_name = 'security.useSslForLogins'
  534. WHERE config_name = 'main.useSslForLogins'";
  535. }
  536. //
  537. // UPDATES FROM 2.7.1
  538. //
  539. if (version_compare($version, '2.7.1', '<')) {
  540. $faqConfig->add('security.useSslOnly', 'false');
  541. }
  542. //
  543. // UPDATES FROM 2.7.3
  544. //
  545. if (version_compare($version, '2.7.3', '<')) {
  546. $query[] = "DELETE FROM ".PMF_Db::getTablePrefix()."faqright WHERE right_id = 18 AND right_id = 19";
  547. $query[] = "DELETE FROM ".PMF_Db::getTablePrefix()."faquser_right WHERE right_id = 18 AND right_id = 19";
  548. $query[] = "DELETE FROM ".PMF_Db::getTablePrefix()."faqgroup_right WHERE right_id = 18 AND right_id = 19";
  549. }
  550. //
  551. // UPDATES FROM 2.8.0-alpha
  552. //
  553. if (version_compare($version, '2.8.0-alpha', '<')) {
  554. $query[] = "INSERT INTO " . PMF_Db::getTablePrefix() . "faqright (right_id, name, description, for_users, for_groups) VALUES
  555. (39, 'addfaq', 'Right to add FAQs in frontend', 1, 1)";
  556. $query[] = "INSERT INTO " . PMF_Db::getTablePrefix() . "faquser_right (user_id, right_id) VALUES (1, 39)";
  557. $query[] = "INSERT INTO " . PMF_Db::getTablePrefix() . "faqright (right_id, name, description, for_users, for_groups) VALUES
  558. (40, 'addquestion', 'Right to add questions in frontend', 1, 1)";
  559. $query[] = "INSERT INTO " . PMF_Db::getTablePrefix() . "faquser_right (user_id, right_id) VALUES (1, 40)";
  560. $query[] = "INSERT INTO " . PMF_Db::getTablePrefix() . "faqright (right_id, name, description, for_users, for_groups) VALUES
  561. (41, 'addcomment', 'Right to add comments in frontend', 1, 1)";
  562. $query[] = "INSERT INTO " . PMF_Db::getTablePrefix() . "faquser_right (user_id, right_id) VALUES (1, 41)";
  563. $faqConfig->add('cache.varnishEnable', 'false');
  564. $faqConfig->add('cache.varnishHost', '127.0.0.1');
  565. $faqConfig->add('cache.varnishPort', '2000');
  566. $faqConfig->add('cache.varnishSecret', '');
  567. $faqConfig->add('cache.varnishTimeout', '500');
  568. $faqConfig->add('security.forcePasswordUpdate', 'true');
  569. $query[] = "ALTER TABLE " . PMF_Db::getTablePrefix() . "faqquestions ADD answer_id INT NOT NULL DEFAULT 0";
  570. $faqConfig->add('records.enableCloseQuestion', 'false');
  571. $faqConfig->add('records.enableDeleteQuestion', 'false');
  572. $query[] = "CREATE TEMPORARY TABLE " . PMF_Db::getTablePrefix() . "faquserlogin_temp (
  573. login varchar(128) NOT NULL,
  574. pass varchar(80) NOT NULL,
  575. PRIMARY KEY (login))";
  576. $query[] = "INSERT INTO " . PMF_Db::getTablePrefix() . "faquserlogin_temp SELECT * FROM " . PMF_Db::getTablePrefix() . "faquserlogin";
  577. $query[] = "DROP TABLE " . PMF_Db::getTablePrefix() . "faquserlogin";
  578. $query[] = "CREATE TABLE " . PMF_Db::getTablePrefix() . "faquserlogin (
  579. login varchar(128) NOT NULL,
  580. pass varchar(80) NOT NULL,
  581. PRIMARY KEY (login))";
  582. $query[] = "INSERT INTO " . PMF_Db::getTablePrefix() . "faquserlogin SELECT * FROM " . PMF_Db::getTablePrefix() . "faquserlogin_temp";
  583. $query[] = "DROP TABLE " . PMF_Db::getTablePrefix() . "faquserlogin_temp";
  584. }
  585. //
  586. // UPDATES FROM 2.8.0-alpha2
  587. //
  588. if (version_compare($version, '2.8.0-alpha2', '<')) {
  589. switch($DB['type']) {
  590. case 'pgsql':
  591. $query[] = "CREATE TABLE " . PMF_Db::getTablePrefix() . "faqinstances (
  592. id int4 NOT NULL,
  593. url VARCHAR(255) NOT NULL,
  594. instance VARCHAR(255) NOT NULL,
  595. comment TEXT NULL,
  596. created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  597. modified TIMESTAMP NOT NULL,
  598. PRIMARY KEY (id))";
  599. $query[] = "CREATE TABLE " . PMF_Db::getTablePrefix() . "faqinstances_config (
  600. instance_id int4 NOT NULL,
  601. config_name VARCHAR(255) NOT NULL default '',
  602. config_value VARCHAR(255) DEFAULT NULL,
  603. PRIMARY KEY (instance_id, config_name))";
  604. break;
  605. case 'mssql':
  606. case 'sqlsrv':
  607. case 'sqlite':
  608. case 'sqlite3':
  609. $query[] = "CREATE TABLE " . PMF_Db::getTablePrefix() . "faqinstances (
  610. id INT(11) NOT NULL,
  611. url VARCHAR(255) NOT NULL,
  612. instance VARCHAR(255) NOT NULL,
  613. comment TEXT NULL,
  614. created DATETIME NOT NULL,
  615. modified DATETIME NOT NULL,
  616. PRIMARY KEY (id))";
  617. $query[] = "CREATE TABLE " . PMF_Db::getTablePrefix() . "faqinstances_config (
  618. instance_id INT(11) NOT NULL,
  619. config_name VARCHAR(255) NOT NULL default '',
  620. config_value VARCHAR(255) DEFAULT NULL,
  621. PRIMARY KEY (instance_id, config_name))";
  622. break;
  623. case 'mysql':
  624. case 'mysqli':
  625. $query[] = "CREATE TABLE " . PMF_Db::getTablePrefix() . "faqinstances (
  626. id INT(11) NOT NULL,
  627. url VARCHAR(255) NOT NULL,
  628. instance VARCHAR(255) NOT NULL,
  629. comment TEXT NULL,
  630. created TIMESTAMP DEFAULT 0,
  631. modified TIMESTAMP ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  632. PRIMARY KEY (id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci";
  633. $query[] = "CREATE TABLE " . PMF_Db::getTablePrefix() . "faqinstances_config (
  634. instance_id INT(11) NOT NULL,
  635. config_name VARCHAR(255) NOT NULL default '',
  636. config_value VARCHAR(255) DEFAULT NULL,
  637. PRIMARY KEY (instance_id, config_name)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci";
  638. break;
  639. }
  640. $query[] = "INSERT INTO " . PMF_Db::getTablePrefix() . "faqright (right_id, name, description) VALUES
  641. (42, 'editinstances', 'Right to edit multi-site instances')";
  642. $query[] = "INSERT INTO " . PMF_Db::getTablePrefix() . "faquser_right (user_id, right_id) VALUES (1, 42)";
  643. $query[] = "INSERT INTO " . PMF_Db::getTablePrefix() . "faqright (right_id, name, description) VALUES
  644. (43, 'addinstances', 'Right to add multi-site instances')";
  645. $query[] = "INSERT INTO " . PMF_Db::getTablePrefix() . "faquser_right (user_id, right_id) VALUES (1, 43)";
  646. $query[] = "INSERT INTO " . PMF_Db::getTablePrefix() . "faqright (right_id, name, description) VALUES
  647. (44, 'delinstances', 'Right to delete multi-site instances')";
  648. $query[] = "INSERT INTO " . PMF_Db::getTablePrefix() . "faquser_right (user_id, right_id) VALUES (1, 44)";
  649. $query[] = "ALTER TABLE " . PMF_Db::getTablePrefix() . "faquser ADD remember_me VARCHAR(150) NULL";
  650. }
  651. // Perform the queries for updating/migrating the database
  652. if (isset($query)) {
  653. echo '<div class="center">';
  654. $count = 0;
  655. foreach ($query as $key => $executeQuery) {
  656. $result = $faqConfig->getDb()->query($executeQuery);
  657. echo '.';
  658. if (!($key % 100)) {
  659. echo '<br />';
  660. }
  661. if (!$result) {
  662. echo "</div>";
  663. echo '<p class="alert alert-error"><strong>Error:</strong> Please update your version of phpMyFAQ ' .
  664. 'once again or send us a <a href="http://bugs.phpmyfaq.de" target="_blank">bug report</a>.' .
  665. '</p>';
  666. printf(
  667. '<p class="alert alert-error"><strong>DB error:</strong> %s</p>',
  668. $faqConfig->getDb()->error()
  669. );
  670. printf(
  671. '<code>%s</code>',
  672. htmlentities($executeQuery)
  673. );
  674. PMF_System::renderFooter();
  675. }
  676. usleep(10000);
  677. $count++;
  678. if (!($count % 10)) {
  679. ob_flush();
  680. }
  681. }
  682. echo "</div>";
  683. }
  684. // Clear the array with the queries
  685. unset($query);
  686. $query = array();
  687. //
  688. // 2nd UPDATES FROM 2.8.0-alpha2
  689. //
  690. if (version_compare($version, '2.8.0-alpha2', '<')) {
  691. $link = new PMF_Link(null, $faqConfig);
  692. $instanceData = array(
  693. 'url' => $link->getSystemUri($_SERVER['SCRIPT_NAME']),
  694. 'instance' => $link->getSystemRelativeUri('install/update.php'),
  695. 'comment' => $faqConfig->get('main.titleFAQ')
  696. );
  697. $faqInstance = new PMF_Instance($faqConfig);
  698. $faqInstance->addInstance($instanceData);
  699. $faqInstanceMaster = new PMF_Instance_Master($faqConfig);
  700. $faqInstanceMaster->createMaster($faqInstance);
  701. $faqConfig->add('records.autosaveActive', 'false');
  702. $faqConfig->add('records.autosaveSecs', '180');
  703. $faqConfig->add('main.maintenanceMode', 'false');
  704. $faqConfig->add('security.salt', md5($faqConfig->get('main.referenceURL')));
  705. }
  706. //
  707. // UPDATES FROM 2.8.0-alpha3
  708. //
  709. if (version_compare($version, '2.8.0-alpha3', '<')) {
  710. $query[] = "DROP TABLE " . PMF_Db::getTablePrefix() . "faqlinkverifyrules";
  711. $query[] = "INSERT INTO " . PMF_Db::getTablePrefix() . "faqright (right_id, name, description) VALUES
  712. (45, 'export', 'Right to export the complete FAQ')";
  713. $query[] = "INSERT INTO " . PMF_Db::getTablePrefix() . "faquser_right (user_id, right_id) VALUES (1, 45)";
  714. }
  715. // Always the last step: Update version number
  716. if (version_compare($version, PMF_System::getVersion(), '<')) {
  717. $faqConfig->update(array('main.currentVersion' => PMF_System::getVersion()));
  718. }
  719. // optimize tables
  720. switch ($DB["type"]) {
  721. case 'mssql':
  722. case 'sqlsrv':
  723. // Get all table names
  724. $faqConfig->getDb()->getTableNames(PMF_Db::getTablePrefix());
  725. foreach ($faqConfig->getDb()->tableNames as $tableName) {
  726. $query[] = 'DBCC DBREINDEX ('.$tableName.')';
  727. }
  728. break;
  729. case 'mysql':
  730. case 'mysqli':
  731. // Get all table names
  732. $faqConfig->getDb()->getTableNames(PMF_Db::getTablePrefix());
  733. foreach ($faqConfig->getDb()->tableNames as $tableName) {
  734. $query[] = 'OPTIMIZE TABLE '.$tableName;
  735. }
  736. break;
  737. case 'pgsql':
  738. $query[] = "VACUUM ANALYZE;";
  739. break;
  740. }
  741. // Perform the queries for optimizing the database
  742. if (isset($query)) {
  743. echo '<div class="center">';
  744. foreach ($query as $executeQuery) {
  745. $result = $faqConfig->getDb()->query($executeQuery);
  746. printf('<span title="%s">.</span>', $executeQuery);
  747. if (!$result) {
  748. echo '<p class="alert alert-error"><strong>Error:</strong> Please install your version of phpMyFAQ once again ' .
  749. 'or send us a <a href="http://bugs.phpmyfaq.de" target="_blank">bug report</a>.</p>';
  750. printf('<p class="error"><strong>DB error:</strong> %s</p>', $faqConfig->getDb()->error());
  751. printf('<code>%s</code>', htmlentities($executeQuery));
  752. PMF_System::renderFooter();
  753. }
  754. usleep(10000);
  755. }
  756. echo "</div>";
  757. }
  758. echo "</p>\n";
  759. echo '<p class="alert alert-success">The database was updated successfully. Thank you very much for updating.</p>';
  760. echo '<h3>Back to your updated <a href="../index.php">phpMyFAQ installation</a> and have fun! :-)</h3>';
  761. // Remove backup files
  762. foreach (glob(PMF_ROOT_DIR.'/config/*.bak.php') as $filename) {
  763. if (!@unlink($filename)) {
  764. printf("<p class=\"alert alert-info\">Please remove the backup file %s manually.</p>\n", $filename);
  765. }
  766. }
  767. // Remove 'setup.php' file
  768. if (is_writeable(__DIR__ . '/setup.php') && @unlink(__DIR__ . '/setup.php')) {
  769. echo "<p class=\"alert alert-success\">The file <em>./install/setup.php</em> was deleted automatically.</p>\n";
  770. } else {
  771. echo "<p class=\"alert alert-error\">Please delete the file <em>./install/setup.php</em> manually.</p>\n";
  772. }
  773. // Remove 'update.php' file
  774. if (is_writeable(__DIR__ . '/update.php') && @unlink(__DIR__ . '/update.php')) {
  775. echo "<p class=\"alert alert-success\">The file <em>./install/update.php</em> was deleted automatically.</p>\n";
  776. } else {
  777. echo "<p class=\"alert alert-error\">Please delete the file <em>./install/update.php</em> manually.</p>\n";
  778. }
  779. PMF_System::renderFooter();
  780. }