PageRenderTime 47ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/db_update.php

https://bitbucket.org/yoorick/fluxbb.pe
PHP | 2506 lines | 1703 code | 504 blank | 299 comment | 307 complexity | 77c17742443f40dac5b7fa190fab8fe1 MD5 | raw file
Possible License(s): GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2011-2012 Yoory Nagumanov
  4. * @copyright Copyright (C) 2008-2012 FluxBB
  5. * @copyright based on code by Rickard Andersson copyright (C) 2002-2008 PunBB
  6. * @license http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
  7. */
  8. // The FluxBB version this script updates to
  9. define('UPDATE_TO', '1.4.5');
  10. define('UPDATE_TO_PE', '0.1-alpha4');
  11. define('UPDATE_TO_DB_REVISION', 11);
  12. define('UPDATE_TO_PE_DB_REVISION', 18);
  13. define('UPDATE_TO_SI_REVISION', 2);
  14. define('UPDATE_TO_PARSER_REVISION', 2);
  15. define('MIN_PHP_VERSION', '5.3.0');
  16. define('MIN_MYSQL_VERSION', '4.1.2');
  17. define('MIN_PGSQL_VERSION', '7.0.0');
  18. define('PUN_SEARCH_MIN_WORD', 3);
  19. define('PUN_SEARCH_MAX_WORD', 20);
  20. // Enable DEBUG mode by removing // from the following line
  21. //define('PUN_DEBUG', 1);
  22. // Show the Exception stack trace on error page.
  23. // Works only in DEBUG mode (needs PUN_DEBUG to be defined too)
  24. // DO NOT enable this in a production environment! (Leakage of sensitive information may occur!)
  25. //define('SHOW_EXCEPTION_STACK_TRACE', 1);
  26. // This displays all executed queries in the page footer.
  27. // DO NOT enable this in a production environment! (Leakage of sensitive information may occur!)
  28. //define('PUN_SHOW_QUERIES', 1);
  29. // The MySQL connection character set that was used for FluxBB 1.2 - in 99% of cases this should be detected automatically,
  30. // but can be overridden using the below constant if required.
  31. //define('FORUM_DEFAULT_CHARSET', 'latin1');
  32. // The number of items to process per page view (lower this if the update script times out during UTF-8 conversion)
  33. define('PER_PAGE', 300);
  34. // Don't set to UTF-8 until after we've found out what the default character set is
  35. define('FORUM_NO_SET_NAMES', 1);
  36. // Make sure we are running at least MIN_PHP_VERSION
  37. if (!function_exists('version_compare') || version_compare(PHP_VERSION, MIN_PHP_VERSION, '<'))
  38. exit('You are running PHP version '.PHP_VERSION.'. FluxBB '.UPDATE_TO.' requires at least PHP '.MIN_PHP_VERSION.' to run properly. You must upgrade your PHP installation before you can continue.');
  39. define('PUN_ROOT', dirname(__FILE__).'/');
  40. require PUN_ROOT . 'protected/pebase.php';
  41. /* На данный момент, PUN_ROOT и Pe::get('root') содержат абсолютно идентичные значения. Заменять PUN_ROOT на Pe::get('root') в тексте скрипта нет смысла по соображениям эффективности. Аналогичная ситуация у FORUM_CACHE_DIR и Pe::get('forum_cache_dir'). Однако некоторые подмены вида Pe::set('var', $var) ниже в скрипте необходимы для правильной работы функций, находящихся в подключаемых скриптах. */
  42. if (file_exists(PUN_ROOT . 'protected/config/main.php'))
  43. Pe::config(include(PUN_ROOT . 'protected/config/main.php'));
  44. else
  45. throw new PeException('Application is not configured');
  46. /* TODO: попрежнему далеко от совершенства. Даже если база данных обновится корректно, нужно как-то сообщить пользователям, что еще нужно конфиг переместить и изменить его формат :(
  47. C PE на PE должно обновляться нормально. И с FluxBB на FluxBB.PE должно нормально обвновлять структуру базы данных (но не config.php!). Если, конечно, где-то не запрятан глюк.
  48. */
  49. // Attempt to load the configuration file config.php (FluxBB)
  50. if (file_exists(PUN_ROOT.'config.php'))
  51. {
  52. include PUN_ROOT.'config.php';
  53. // If we have the 1.3-legacy constant defined, define the proper 1.4 constant so we don't get an incorrect "need to install" message
  54. if (defined('FORUM'))
  55. define('PUN', FORUM);
  56. // If PUN isn't defined, config.php is missing or corrupt
  57. if (!defined('PUN'))
  58. {
  59. header('Location: install.php');
  60. exit;
  61. }
  62. Pe::config(array(
  63. 'db_type' => $db_type,
  64. 'db_host' => $db_host,
  65. 'db_name' => $db_name,
  66. 'db_username' => $db_username,
  67. 'db_password' => $db_password,
  68. 'db_prefix' => $db_prefix,
  69. 'p_connect' => $p_connect,
  70. 'cookie_name' => $cookie_name,
  71. 'cookie_domain' => $cookie_domain,
  72. 'cookie_path' => $cookie_path,
  73. 'cookie_secure' => $cookie_secure,
  74. 'cookie_seed' => $cookie_seed,
  75. ));
  76. }
  77. // Attempt to load the configuration file config.php (PunBB PE)
  78. else if (file_exists(PUN_ROOT.'include/config.php'))
  79. {
  80. include PUN_ROOT.'include/config.php';
  81. // If we have the 1.3-legacy constant defined, define the proper 1.4 constant so we don't get an incorrect "need to install" message
  82. if (defined('FORUM'))
  83. define('PUN', FORUM);
  84. // If PUN isn't defined, config.php is missing or corrupt
  85. if (!defined('PUN'))
  86. {
  87. header('Location: install.php');
  88. exit;
  89. }
  90. Pe::config(array(
  91. 'db_type' => $db_type,
  92. 'db_host' => $db_host,
  93. 'db_name' => $db_name,
  94. 'db_username' => $db_username,
  95. 'db_password' => $db_password,
  96. 'db_prefix' => $db_prefix,
  97. 'p_connect' => $p_connect,
  98. 'cookie_name' => $cookie_name,
  99. 'cookie_domain' => $cookie_domain,
  100. 'cookie_path' => $cookie_path,
  101. 'cookie_secure' => $cookie_secure,
  102. 'cookie_seed' => $cookie_seed,
  103. ));
  104. }
  105. // Check to see whether FluxBB PE is already installed
  106. else if (file_exists(PUN_ROOT.'protected/config/config.php'))
  107. {
  108. $cfg = require(PUN_ROOT.'protected/config/config.php');
  109. Pe::config($cfg);
  110. extract($cfg);
  111. unset($cfg);
  112. }
  113. else
  114. {
  115. header('Location: install.php');
  116. exit;
  117. }
  118. /* TODO: подумать, есть ли смысл завести отдельный конфиг для db_update.php (как для install.php)?
  119. * Сейчас склоняюсь, что нет, но тем не менее отдельный файл с некоторыми дефайнами отсюда можно сделать.
  120. *
  121. * С другой стороны хотелось бы исключить лишние зависимости, если они есть.
  122. * Насколько я вижу из кода, db_update.php старается как можно меньше использовать
  123. * возможности движка, которым нужна база данных.
  124. */
  125. // Enable debug mode
  126. if (!defined('PUN_DEBUG'))
  127. define('PUN_DEBUG', 1);
  128. // Load the functions script
  129. require PUN_ROOT.'include/functions.php';
  130. // Load UTF-8 functions
  131. require PUN_ROOT.'include/utf8/utf8.php';
  132. // Strip out "bad" UTF-8 characters
  133. forum_remove_bad_characters();
  134. // Reverse the effect of register_globals
  135. forum_unregister_globals();
  136. // Turn on full PHP error reporting
  137. error_reporting(E_ALL);
  138. // Force POSIX locale (to prevent functions such as strtolower() from messing up UTF-8 strings)
  139. setlocale(LC_CTYPE, 'C');
  140. // Turn off magic_quotes_runtime
  141. if (get_magic_quotes_runtime())
  142. set_magic_quotes_runtime(0);
  143. // Strip slashes from GET/POST/COOKIE (if magic_quotes_gpc is enabled)
  144. if (get_magic_quotes_gpc())
  145. {
  146. function stripslashes_array($array)
  147. {
  148. return is_array($array) ? array_map('stripslashes_array', $array) : stripslashes($array);
  149. }
  150. $_GET = stripslashes_array($_GET);
  151. $_POST = stripslashes_array($_POST);
  152. $_COOKIE = stripslashes_array($_COOKIE);
  153. $_REQUEST = stripslashes_array($_REQUEST);
  154. }
  155. // If a cookie name is not specified in config.php, we use the default (forum_cookie)
  156. if (empty($cookie_name))
  157. $cookie_name = 'pun_cookie';
  158. // If the cache directory is not specified, we use the default setting
  159. if (!defined('FORUM_CACHE_DIR'))
  160. define('FORUM_CACHE_DIR', PUN_ROOT.'cache/');
  161. // Turn off PHP time limit
  162. @set_time_limit(0);
  163. // Define a few commonly used constants
  164. define('PUN_UNVERIFIED', 0);
  165. define('PUN_ADMIN', 1);
  166. define('PUN_MOD', 2);
  167. define('PUN_GUEST', 3);
  168. define('PUN_MEMBER', 4);
  169. // Load DB abstraction layer and try to connect
  170. /* @var $db \pe\dblayer\DBLayer */
  171. $db = Pe::get('db');
  172. // Check what the default character set is - since 1.2 didn't specify any we will use whatever the default was (usually latin1)
  173. $old_connection_charset = defined('FORUM_DEFAULT_CHARSET') ? FORUM_DEFAULT_CHARSET : $db->get_names();
  174. // Set the connection to UTF-8 now
  175. $db->set_names('utf8');
  176. // Get the forum config
  177. $result = $db->query('SELECT * FROM '.$db->prefix.'config');
  178. while ($cur_config_item = $db->fetch_row($result))
  179. $pun_config[$cur_config_item[0]] = $cur_config_item[1];
  180. // Временное решение:
  181. Pe::set('pun_config', $pun_config); // Чтобы не обрабатывался вызов к CacheLoader (и может быть CacheGenerator)
  182. // Load language file
  183. $default_lang = $pun_config['o_default_lang'];
  184. if (!file_exists(PUN_ROOT.'lang/'.$default_lang.'/update.php'))
  185. $default_lang = 'English';
  186. $lang_update = require PUN_ROOT.'lang/'.$default_lang.'/update.php';
  187. // Set the $pun_user to satisfy the dependences
  188. Pe::set('pun_user', array('lang' => $default_lang));
  189. // Check current version
  190. $cur_version = $pun_config['o_cur_version'];
  191. if (version_compare($cur_version, '1.2', '<'))
  192. throw new PeException(sprintf($lang_update['Version mismatch error'], $db_name));
  193. // Do some DB type specific checks
  194. $mysql = false;
  195. switch ($db_type)
  196. {
  197. case 'mysql':
  198. case 'mysqli':
  199. case 'mysql_innodb':
  200. case 'mysqli_innodb':
  201. $mysql_info = $db->get_version();
  202. if (version_compare($mysql_info['version'], MIN_MYSQL_VERSION, '<'))
  203. throw new PeDbException(sprintf($lang_update['You are running error'], 'MySQL', $mysql_info['version'], UPDATE_TO, MIN_MYSQL_VERSION));
  204. $mysql = true;
  205. break;
  206. case 'pgsql':
  207. $pgsql_info = $db->get_version();
  208. if (version_compare($pgsql_info['version'], MIN_PGSQL_VERSION, '<'))
  209. throw new PeDbException(sprintf($lang_update['You are running error'], 'PostgreSQL', $pgsql_info['version'], UPDATE_TO, MIN_PGSQL_VERSION));
  210. break;
  211. }
  212. // Check the database, search index and parser revision and the current version
  213. if (isset($pun_config['o_database_revision']) && $pun_config['o_database_revision'] >= UPDATE_TO_DB_REVISION &&
  214. isset($pun_config['o_pe_database_revision']) && $pun_config['o_pe_database_revision'] >= UPDATE_TO_PE_DB_REVISION&&
  215. isset($pun_config['o_searchindex_revision']) && $pun_config['o_searchindex_revision'] >= UPDATE_TO_SI_REVISION &&
  216. isset($pun_config['o_parser_revision']) && $pun_config['o_parser_revision'] >= UPDATE_TO_PARSER_REVISION &&
  217. version_compare($pun_config['o_cur_version'], UPDATE_TO, '>=') &&
  218. version_compare($pun_config['o_cur_pe_version'], UPDATE_TO_PE, '>='))
  219. throw new PeException($lang_update['No update error']);
  220. $default_style = $pun_config['o_default_style'];
  221. if (!file_exists(PUN_ROOT.'style/'.$default_style.'.css'))
  222. $default_style = 'Air';
  223. // Start a session, used to queue up errors if duplicate users occur when converting from FluxBB v1.2.
  224. session_start();
  225. //
  226. // Determines whether $str is UTF-8 encoded or not
  227. //
  228. function seems_utf8($str)
  229. {
  230. $str_len = strlen($str);
  231. for ($i = 0; $i < $str_len; ++$i)
  232. {
  233. if (ord($str[$i]) < 0x80) continue; # 0bbbbbbb
  234. else if ((ord($str[$i]) & 0xE0) == 0xC0) $n=1; # 110bbbbb
  235. else if ((ord($str[$i]) & 0xF0) == 0xE0) $n=2; # 1110bbbb
  236. else if ((ord($str[$i]) & 0xF8) == 0xF0) $n=3; # 11110bbb
  237. else if ((ord($str[$i]) & 0xFC) == 0xF8) $n=4; # 111110bb
  238. else if ((ord($str[$i]) & 0xFE) == 0xFC) $n=5; # 1111110b
  239. else return false; # Does not match any model
  240. for ($j = 0; $j < $n; ++$j) # n bytes matching 10bbbbbb follow ?
  241. {
  242. if ((++$i == strlen($str)) || ((ord($str[$i]) & 0xC0) != 0x80))
  243. return false;
  244. }
  245. }
  246. return true;
  247. }
  248. //
  249. // Translates the number from a HTML numeric entity into an UTF-8 character
  250. //
  251. function dcr2utf8($src)
  252. {
  253. $dest = '';
  254. if ($src < 0)
  255. return false;
  256. else if ($src <= 0x007f)
  257. $dest .= chr($src);
  258. else if ($src <= 0x07ff)
  259. {
  260. $dest .= chr(0xc0 | ($src >> 6));
  261. $dest .= chr(0x80 | ($src & 0x003f));
  262. }
  263. else if ($src == 0xFEFF)
  264. {
  265. // nop -- zap the BOM
  266. }
  267. else if ($src >= 0xD800 && $src <= 0xDFFF)
  268. {
  269. // found a surrogate
  270. return false;
  271. }
  272. else if ($src <= 0xffff)
  273. {
  274. $dest .= chr(0xe0 | ($src >> 12));
  275. $dest .= chr(0x80 | (($src >> 6) & 0x003f));
  276. $dest .= chr(0x80 | ($src & 0x003f));
  277. }
  278. else if ($src <= 0x10ffff)
  279. {
  280. $dest .= chr(0xf0 | ($src >> 18));
  281. $dest .= chr(0x80 | (($src >> 12) & 0x3f));
  282. $dest .= chr(0x80 | (($src >> 6) & 0x3f));
  283. $dest .= chr(0x80 | ($src & 0x3f));
  284. }
  285. else
  286. {
  287. // out of range
  288. return false;
  289. }
  290. return $dest;
  291. }
  292. //
  293. // Attempts to convert $str from $old_charset to UTF-8. Also converts HTML entities (including numeric entities) to UTF-8 characters
  294. //
  295. function convert_to_utf8(&$str, $old_charset)
  296. {
  297. if ($str === null || $str == '')
  298. return false;
  299. $save = $str;
  300. // Replace literal entities (for non-UTF-8 compliant html_entity_encode)
  301. if (version_compare(PHP_VERSION, '5.0.0', '<') && $old_charset == 'ISO-8859-1' || $old_charset == 'ISO-8859-15')
  302. $str = html_entity_decode($str, ENT_QUOTES, $old_charset);
  303. if ($old_charset != 'UTF-8' && !seems_utf8($str))
  304. {
  305. if (function_exists('iconv'))
  306. $str = iconv($old_charset == 'ISO-8859-1' ? 'WINDOWS-1252' : 'ISO-8859-1', 'UTF-8', $str);
  307. else if (function_exists('mb_convert_encoding'))
  308. $str = mb_convert_encoding($str, 'UTF-8', $old_charset == 'ISO-8859-1' ? 'WINDOWS-1252' : 'ISO-8859-1');
  309. else if ($old_charset == 'ISO-8859-1')
  310. $str = utf8_encode($str);
  311. }
  312. // Replace literal entities (for UTF-8 compliant html_entity_encode)
  313. if (version_compare(PHP_VERSION, '5.0.0', '>='))
  314. $str = html_entity_decode($str, ENT_QUOTES, 'UTF-8');
  315. // Replace numeric entities
  316. $str = preg_replace_callback('/&#([0-9]+);/', 'utf8_callback_1', $str);
  317. $str = preg_replace_callback('/&#x([a-f0-9]+);/i', 'utf8_callback_2', $str);
  318. // Remove "bad" characters
  319. $str = remove_bad_characters($str);
  320. return ($save != $str);
  321. }
  322. function utf8_callback_1($matches)
  323. {
  324. return dcr2utf8($matches[1]);
  325. }
  326. function utf8_callback_2($matches)
  327. {
  328. return dcr2utf8(hexdec($matches[1]));
  329. }
  330. //
  331. // Alter a table to be utf8. MySQL only
  332. // Function based on update_convert_table_utf8() from the Drupal project (http://drupal.org/)
  333. //
  334. function alter_table_utf8($table)
  335. {
  336. global $mysql, $db;
  337. static $types;
  338. if (!$mysql)
  339. return;
  340. if (!isset($types))
  341. {
  342. $types = array(
  343. 'char' => 'binary',
  344. 'varchar' => 'varbinary',
  345. 'tinytext' => 'tinyblob',
  346. 'mediumtext' => 'mediumblob',
  347. 'text' => 'blob',
  348. 'longtext' => 'longblob'
  349. );
  350. }
  351. // Set table default charset to utf8
  352. $db->query('ALTER TABLE '.$table.' CHARACTER SET utf8');
  353. // Find out which columns need converting and build SQL statements
  354. $result = $db->query('SHOW FULL COLUMNS FROM '.$table);
  355. while ($cur_column = $db->fetch_assoc($result))
  356. {
  357. if ($cur_column['Collation'] === null)
  358. continue;
  359. list($type) = explode('(', $cur_column['Type']);
  360. if (isset($types[$type]) && strpos($cur_column['Collation'], 'utf8') === false)
  361. {
  362. $allow_null = ($cur_column['Null'] == 'YES');
  363. $collate = (substr($cur_column['Collation'], -3) == 'bin') ? 'utf8_bin' : 'utf8_general_ci';
  364. $db->alter_field($table, $cur_column['Field'], preg_replace('/'.$type.'/i', $types[$type], $cur_column['Type']), $allow_null, $cur_column['Default'], null, true);
  365. $db->alter_field($table, $cur_column['Field'], $cur_column['Type'].' CHARACTER SET utf8 COLLATE '.$collate, $allow_null, $cur_column['Default'], null, true);
  366. }
  367. }
  368. }
  369. //
  370. // Safely converts text type columns into utf8
  371. // If finished returns true, otherwise returns $end_at
  372. //
  373. function convert_table_utf8($table, $callback, $old_charset, $key = null, $start_at = null, $error_callback = null)
  374. {
  375. global $mysql, $db, $old_connection_charset;
  376. $finished = true;
  377. $end_at = 0;
  378. if ($mysql)
  379. {
  380. // Only set up the tables if we are doing this in 1 go, or its the first go
  381. if ($start_at === null || $start_at == 0)
  382. {
  383. // Drop any temp table that exists, in-case it's left over from a failed update
  384. $db->drop_table($table.'_utf8', true);
  385. // Copy the table
  386. $db->query('CREATE TABLE '.$table.'_utf8 LIKE '.$table);
  387. // Set table default charset to utf8
  388. alter_table_utf8($table.'_utf8');
  389. }
  390. // Change to the old character set so MySQL doesn't attempt to perform conversion on the data from the old table
  391. $db->set_names($old_connection_charset);
  392. // Move & Convert everything
  393. $result = $db->query('SELECT * FROM '.$table.($start_at === null ? '' : ' WHERE '.$key.'>'.$start_at).' ORDER BY '.$key.' ASC'.($start_at === null ? '' : ' LIMIT '.PER_PAGE), false);
  394. // Change back to utf8 mode so we can insert it into the new table
  395. $db->set_names('utf8');
  396. while ($cur_item = $db->fetch_assoc($result))
  397. {
  398. $cur_item = call_user_func($callback, $cur_item, $old_charset);
  399. $temp = array();
  400. foreach ($cur_item as $idx => $value)
  401. $temp[$idx] = $value === null ? 'NULL' : '\''.$db->escape($value).'\'';
  402. try
  403. {
  404. $db->query('INSERT INTO '.$table.'_utf8('.implode(',', array_keys($temp)).') VALUES ('.implode(',', array_values($temp)).')');
  405. }
  406. catch (PeDbException $ex)
  407. {
  408. if ($error_callback === null)
  409. throw $ex;
  410. else
  411. call_user_func($error_callback, $cur_item);
  412. }
  413. $end_at = $cur_item[$key];
  414. }
  415. // If we aren't doing this all in 1 go and $end_at has a value (i.e. we have processed at least 1 row), figure out if we have more to do or not
  416. if ($start_at !== null && $end_at > 0)
  417. {
  418. $result = $db->query('SELECT 1 FROM '.$table.' WHERE '.$key.'>'.$end_at.' ORDER BY '.$key.' ASC LIMIT 1');
  419. $finished = $db->num_rows($result) == 0;
  420. }
  421. // Only swap the tables if we are doing this in 1 go, or its the last go
  422. if ($finished)
  423. {
  424. // Delete old table
  425. $db->drop_table($table, true);
  426. // Rename table
  427. $db->query('ALTER TABLE '.$table.'_utf8 RENAME '.$table);
  428. return true;
  429. }
  430. return $end_at;
  431. }
  432. else
  433. {
  434. // Convert everything
  435. $result = $db->query('SELECT * FROM '.$table.($start_at === null ? '' : ' WHERE '.$key.'>'.$start_at).' ORDER BY '.$key.' ASC'.($start_at === null ? '' : ' LIMIT '.PER_PAGE));
  436. while ($cur_item = $db->fetch_assoc($result))
  437. {
  438. $cur_item = call_user_func($callback, $cur_item, $old_charset);
  439. $temp = array();
  440. foreach ($cur_item as $idx => $value)
  441. $temp[] = $idx.'='.($value === null ? 'NULL' : '\''.$db->escape($value).'\'');
  442. if (!empty($temp))
  443. $db->query('UPDATE '.$table.' SET '.implode(', ', $temp).' WHERE '.$key.'=\''.$db->escape($cur_item[$key]).'\'');
  444. $end_at = $cur_item[$key];
  445. }
  446. if ($start_at !== null && $end_at > 0)
  447. {
  448. $result = $db->query('SELECT 1 FROM '.$table.' WHERE '.$key.'>'.$end_at.' ORDER BY '.$key.' ASC LIMIT 1');
  449. if ($db->num_rows($result) == 0)
  450. return true;
  451. return $end_at;
  452. }
  453. return true;
  454. }
  455. }
  456. header('Content-type: text/html; charset=utf-8');
  457. // Empty all output buffers and stop buffering
  458. while (@ob_end_clean());
  459. $stage = isset($_REQUEST['stage']) ? $_REQUEST['stage'] : '';
  460. $old_charset = isset($_REQUEST['req_old_charset']) ? str_replace('ISO8859', 'ISO-8859', strtoupper($_REQUEST['req_old_charset'])) : 'ISO-8859-1';
  461. $start_at = isset($_REQUEST['start_at']) ? intval($_REQUEST['start_at']) : 0;
  462. $query_str = '';
  463. // Show form
  464. if (empty($stage))
  465. {
  466. ?>
  467. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  468. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
  469. <head>
  470. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  471. <title><?php echo $lang_update['Update'] ?></title>
  472. <link rel="stylesheet" type="text/css" href="style/<?php echo $default_style ?>.css" />
  473. </head>
  474. <body onload="document.getElementById('install').req_db_type.focus();document.getElementById('install').start.disabled=false;">
  475. <div id="pundb_update" class="pun">
  476. <div class="top-box"><div><!-- Top Corners --></div></div>
  477. <div class="punwrap">
  478. <div id="brdheader" class="block">
  479. <div class="box">
  480. <div id="brdtitle" class="inbox">
  481. <h1><span><?php echo $lang_update['Update'] ?></span></h1>
  482. <div id="brddesc"><p><?php echo $lang_update['Update message'] ?></p><p><strong><?php echo $lang_update['Note']; ?></strong> <?php echo $lang_update['Members message']; ?></p></div>
  483. </div>
  484. </div>
  485. </div>
  486. <div id="brdmain">
  487. <div class="blockform">
  488. <h2><span><?php echo $lang_update['Update'] ?></span></h2>
  489. <div class="box">
  490. <form method="post" action="db_update.php">
  491. <input type="hidden" name="stage" value="start" />
  492. <div class="inform">
  493. <fieldset>
  494. <legend><?php echo $lang_update['Administrator only'] ?></legend>
  495. <div class="infldset">
  496. <p><?php echo $lang_update['Database password info'] ?></p>
  497. <p><strong><?php echo $lang_update['Note']; ?></strong> <?php echo $lang_update['Database password note'] ?></p>
  498. <label class="required"><strong><?php echo $lang_update['Database password'] ?> <span><?php echo $lang_update['Required'] ?></span></strong><br /><input type="password" id="req_db_pass" name="req_db_pass" /><br /></label>
  499. </div>
  500. </fieldset>
  501. </div>
  502. <div class="inform">
  503. <div class="forminfo">
  504. <p><?php echo $lang_update['Intro 1'] ?></p>
  505. <p><?php echo $lang_update['Intro 2'] ?></p>
  506. <?php
  507. if (strpos($cur_version, '1.2') === 0)
  508. {
  509. if (!function_exists('iconv') && !function_exists('mb_convert_encoding'))
  510. {
  511. ?>
  512. <p><?php echo $lang_update['No charset conversion'] ?></p>
  513. <?php
  514. }
  515. ?>
  516. </div>
  517. </div>
  518. <div class="inform">
  519. <div class="forminfo">
  520. <p><?php echo $lang_update['Enable conversion'] ?></p>
  521. <p><?php echo $lang_update['Current character set'] ?></p>
  522. </div>
  523. <fieldset>
  524. <legend><?php echo $lang_update['Charset conversion'] ?></legend>
  525. <div class="infldset">
  526. <div class="rbox">
  527. <label><input type="checkbox" name="convert_charset" value="1" checked="checked" /><?php echo $lang_update['Enable conversion label'] ?><br /></label>
  528. </div>
  529. <label>
  530. <strong><?php echo $lang_update['Current character set label'] ?></strong><br /><?php echo $lang_update['Current character set info'] ?><br />
  531. <input type="text" name="req_old_charset" size="12" maxlength="20" value="<?php echo $old_charset ?>" /><br />
  532. </label>
  533. </div>
  534. </fieldset>
  535. <?php
  536. }
  537. else
  538. echo "\t\t\t\t".'</div>'."\n";
  539. ?>
  540. </div>
  541. <p class="buttons"><input type="submit" name="start" value="<?php echo $lang_update['Start update'] ?>" /></p>
  542. </form>
  543. </div>
  544. </div>
  545. </div>
  546. </div>
  547. <div class="end-box"><div><!-- Bottom Corners --></div></div>
  548. </div>
  549. </body>
  550. </html>
  551. <?php
  552. $db->end_transaction();
  553. $db->close();
  554. exit;
  555. }
  556. // Read the lock file
  557. $lock = file_exists(FORUM_CACHE_DIR.'db_update.lock') ? trim(file_get_contents(FORUM_CACHE_DIR.'db_update.lock')) : false;
  558. $lock_error = false;
  559. // Generate or fetch the UID - this confirms we have a valid admin
  560. if (isset($_POST['req_db_pass']))
  561. {
  562. $req_db_pass = strtolower(trim($_POST['req_db_pass']));
  563. switch ($db_type)
  564. {
  565. // For SQLite we compare against the database file name, since the password is left blank
  566. case 'sqlite':
  567. if ($req_db_pass != strtolower($db_name))
  568. throw new PeAccessException(sprintf($lang_update['Invalid file error'], 'config.php'));
  569. break;
  570. // For everything else, check the password matches
  571. default:
  572. if ($req_db_pass != strtolower($db_password))
  573. throw new PeAccessException(sprintf($lang_update['Invalid password error'], 'config.php'));
  574. break;
  575. }
  576. // Generate a unique id to identify this session, only if this is a valid session
  577. $uid = pun_hash($req_db_pass.'|'.uniqid(rand(), true));
  578. if ($lock) // We already have a lock file
  579. $lock_error = true;
  580. else // Create the lock file
  581. {
  582. $fh = @fopen(FORUM_CACHE_DIR.'db_update.lock', 'wb');
  583. if (!$fh)
  584. throw new PeAccessException(sprintf($lang_update['Unable to lock error'], 'cache'));
  585. fwrite($fh, $uid);
  586. fclose($fh);
  587. }
  588. }
  589. else if (isset($_GET['uid']))
  590. {
  591. $uid = trim($_GET['uid']);
  592. if (!$lock || $lock != $uid) // The lock doesn't exist or doesn't match the given UID
  593. $lock_error = true;
  594. }
  595. else
  596. throw new PeAccessException($lang_update['No password error']);
  597. // If there is an error with the lock file
  598. if ($lock_error)
  599. throw new PeAccessException(sprintf($lang_update['Script runs error'], FORUM_CACHE_DIR.'db_update.lock'));
  600. switch ($stage)
  601. {
  602. // Start by updating the database structure
  603. case 'start':
  604. $query_str = '?stage=preparse_posts';
  605. // If we don't need to update the database, skip this stage
  606. if (isset($pun_config['o_database_revision']) && $pun_config['o_database_revision'] >= UPDATE_TO_DB_REVISION && isset($pun_config['o_pe_database_revision']) && $pun_config['o_pe_database_revision'] >= UPDATE_TO_PE_DB_REVISION)
  607. break;
  608. // Make all email fields VARCHAR(80)
  609. $db->alter_field('bans', 'email', 'VARCHAR(80)', true);
  610. $db->alter_field('posts', 'poster_email', 'VARCHAR(80)', true);
  611. $db->alter_field('users', 'email', 'VARCHAR(80)', false, '');
  612. $db->alter_field('users', 'jabber', 'VARCHAR(80)', true);
  613. $db->alter_field('users', 'msn', 'VARCHAR(80)', true);
  614. $db->alter_field('users', 'activate_string', 'VARCHAR(80)', true);
  615. // Make all IP fields VARCHAR(39) to support IPv6
  616. $db->alter_field('posts', 'poster_ip', 'VARCHAR(39)', true);
  617. $db->alter_field('users', 'registration_ip', 'VARCHAR(39)', false, '0.0.0.0');
  618. // Make the message field MEDIUMTEXT to allow proper conversion of 65535 character posts to UTF-8
  619. $db->alter_field('posts', 'message', 'MEDIUMTEXT', true);
  620. // Add the kind field to the forums table
  621. $db->add_field('forums', 'kind', 'TINYINT(1)', false, Pe::get('pun_kind_forum'), 'cat_id');
  622. // Limited support for PunBB Power Edition
  623. if ($db->field_exists('categories', 'kind'))
  624. {
  625. $result = $db->query('SELECT id, kind FROM '.$db->prefix.'categories');
  626. while ($cur_cat = $db->fetch_assoc($result))
  627. $db->query('UPDATE '.$db->prefix.'forums SET kind='.$cur_cat['kind'].' WHERE cat_id='.$cur_cat['id']);
  628. $db->drop_field('categories', 'kind');
  629. }
  630. // Add the DST option to the users table
  631. $db->add_field('users', 'dst', 'TINYINT(1)', false, 0, 'timezone');
  632. // Add the last_post field to the online table
  633. $db->add_field('online', 'last_post', 'INT(10) UNSIGNED', true, null, null);
  634. // Add the last_search field to the online table
  635. $db->add_field('online', 'last_search', 'INT(10) UNSIGNED', true, null, null);
  636. // Add the last_search column to the users table
  637. $db->add_field('users', 'last_search', 'INT(10) UNSIGNED', true, null, 'last_post');
  638. // Add the file_bonus column to the users table
  639. $db->add_field('users', 'file_bonus', 'INT(10)', false, 0);
  640. // Add the num_files column to the users table
  641. $db->add_field('users', 'num_files', 'INT(10)', false, 0);
  642. // Add the total_files column to the users table
  643. $db->add_field('users', 'total_files', 'INT(10)', false, 0);
  644. // Add the last_mark_read column to the users table
  645. $db->add_field('users', 'last_mark_read', 'INT(10)', false, 0);
  646. // Add the g_file_download column to the groups table
  647. $db->add_field('groups', 'g_file_download', 'TINYINT(1)', false, 1);
  648. // Add the g_file_upload column to the groups table
  649. $db->add_field('groups', 'g_file_upload', 'TINYINT(1)', false, 0);
  650. // Add the g_file_private column to the groups table
  651. $db->add_field('groups', 'g_file_private', 'TINYINT(1)', false, 0);
  652. // Add the g_file_limit column to the groups table
  653. $db->add_field('groups', 'g_file_limit', 'INT(10)', false, 0);
  654. // Add the g_file_limit_mode column to the groups table
  655. $db->add_field('groups', 'g_file_limit_mode', 'TINYINT(1)', false, 1);
  656. // Add the g_file_bonus_mode column to the groups table
  657. $db->add_field('groups', 'g_file_bonus_mode', 'TINYINT(1)', false, 0);
  658. // Update unchangeable File Upload settings for Administrator group
  659. $db->query('UPDATE '.$db->prefix.'groups SET g_file_download=1, g_file_upload=1, g_file_private=1, g_file_limit=999, g_file_limit_mode=0 WHERE g_id=1');
  660. // Add the g_see_hidden_text column to the groups table
  661. $db->add_field('groups', 'g_see_hidden_text', 'TINYINT(1)', false, 0);
  662. // Make admin able to see hidden text
  663. $db->query('UPDATE '.$db->prefix.'groups SET g_see_hidden_text=1 WHERE g_id=1');
  664. // Add the cat_desc column to the categories table
  665. $db->add_field('categories', 'cat_desc', 'TEXT', false, '');
  666. // Add the cat_enabled column to the categories table
  667. $db->add_field('categories', 'cat_enabled', 'TINYINT(1)', false, 1);
  668. // Drop use_avatar column from users table
  669. $db->drop_field('users', 'use_avatar');
  670. // Drop save_pass column from users table
  671. $db->drop_field('users', 'save_pass');
  672. // Drop g_edit_subjects_interval column from groups table
  673. $db->drop_field('groups', 'g_edit_subjects_interval');
  674. // Add PE version
  675. if (!array_key_exists('o_cur_pe_version', $pun_config))
  676. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'o_cur_pe_version\', \'0\')');
  677. // Add database revision number
  678. if (!array_key_exists('o_database_revision', $pun_config))
  679. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'o_database_revision\', \'0\')');
  680. // Add PE database revision number
  681. if (!array_key_exists('o_pe_database_revision', $pun_config))
  682. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'o_pe_database_revision\', \'0\')');
  683. // Add search index revision number
  684. if (!array_key_exists('o_searchindex_revision', $pun_config))
  685. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'o_searchindex_revision\', \'0\')');
  686. // Add parser revision number
  687. if (!array_key_exists('o_parser_revision', $pun_config))
  688. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'o_parser_revision\', \'0\')');
  689. // Add default email setting option
  690. if (!array_key_exists('o_default_email_setting', $pun_config))
  691. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'o_default_email_setting\', \'1\')');
  692. // Make sure we have o_additional_navlinks (was added in 1.2.1)
  693. if (!array_key_exists('o_additional_navlinks', $pun_config))
  694. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'o_additional_navlinks\', \'\')');
  695. // Insert new config option o_topic_views
  696. if (!array_key_exists('o_topic_views', $pun_config))
  697. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'o_topic_views\', \'1\')');
  698. // Insert new config option o_signatures
  699. if (!array_key_exists('o_signatures', $pun_config))
  700. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'o_signatures\', \'1\')');
  701. // Insert new config option o_smtp_ssl
  702. if (!array_key_exists('o_smtp_ssl', $pun_config))
  703. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'o_smtp_ssl\', \'0\')');
  704. // Insert new config option o_default_dst
  705. if (!array_key_exists('o_default_dst', $pun_config))
  706. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'o_default_dst\', \'0\')');
  707. // Insert new config option o_quote_depth
  708. if (!array_key_exists('o_quote_depth', $pun_config))
  709. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'o_quote_depth\', \'3\')');
  710. // Insert new config option o_feed_type
  711. if (!array_key_exists('o_feed_type', $pun_config))
  712. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'o_feed_type\', \'2\')');
  713. // Insert new config option o_feed_ttl
  714. if (!array_key_exists('o_feed_ttl', $pun_config))
  715. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'o_feed_ttl\', \'0\')');
  716. // Insert new config option file_upload_path
  717. if (!array_key_exists('file_upload_path', $pun_config))
  718. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'file_upload_path\', \'upload/\')');
  719. // Insert new config option file_current_subpath
  720. if (!array_key_exists('file_current_subpath', $pun_config))
  721. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'file_current_subpath\', \'\')');
  722. // Insert new config option file_max_subpath_files
  723. if (!array_key_exists('file_max_subpath_files', $pun_config))
  724. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'file_max_subpath_files\', \'10000\')');
  725. // Insert new config option file_friendly_url
  726. if (!array_key_exists('file_friendly_url', $pun_config))
  727. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'file_friendly_url\', \'0\')');
  728. // Insert new config option file_thumb_path
  729. if (!array_key_exists('file_thumb_path', $pun_config))
  730. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'file_thumb_path\', \'img/thumb/\')');
  731. // Insert new config option file_preview_path
  732. if (!array_key_exists('file_preview_path', $pun_config))
  733. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'file_preview_path\', \'img/preview/\')');
  734. // Insert new config option file_allowed_ext
  735. if (!array_key_exists('file_allowed_ext', $pun_config))
  736. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'file_allowed_ext\', \'gif,png,jpg,jpeg,zip,rar\')');
  737. // Insert new config option file_image_ext
  738. if (!array_key_exists('file_image_ext', $pun_config))
  739. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'file_image_ext\', \'gif,png,jpg,jpeg\')');
  740. // Insert new config option file_max_size
  741. if (!array_key_exists('file_max_size', $pun_config))
  742. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'file_max_size\', \'2097152\')');
  743. // Insert new config option file_max_dimension
  744. if (!array_key_exists('file_max_dimension', $pun_config))
  745. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'file_max_dimension\', \'3000\')');
  746. // Insert new config option file_preview_dimension
  747. if (!array_key_exists('file_preview_dimension', $pun_config))
  748. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'file_preview_dimension\', \'500\')');
  749. // Insert new config option file_thumb_dimension
  750. if (!array_key_exists('file_thumb_dimension', $pun_config))
  751. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'file_thumb_dimension\', \'100\')');
  752. // Insert new config option pe_enabled_kinds
  753. if (!array_key_exists('pe_enabled_kinds', $pun_config))
  754. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'pe_enabled_kinds\', \''.$db->escape(serialize(array(Pe::get('pun_kind_forum')))).'\')');
  755. // Insert new config option pe_navigation_type
  756. if (!array_key_exists('pe_navigation_type', $pun_config))
  757. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'pe_navigation_type\', \'1\')');
  758. // Insert new config option pe_default_kind
  759. if (!array_key_exists('pe_default_kind', $pun_config))
  760. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'pe_default_kind\', \''.Pe::get('pun_kind_forum').'\')');
  761. // Insert new config option o_topics_track_timeout
  762. if (!array_key_exists('o_topics_track_timeout', $pun_config))
  763. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'o_topics_track_timeout\', \'15\')');
  764. // Insert new config option o_new_topics_timeout
  765. if (!array_key_exists('o_new_topics_timeout', $pun_config))
  766. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'o_new_topics_timeout\', \'5\')');
  767. // Insert new config option pe_default_category
  768. if (!array_key_exists('pe_default_category', $pun_config))
  769. {
  770. // Fetch the first real category ID form the DB or use 0 if none.
  771. $result = $db->query('SELECT id FROM '.$db->prefix.'categories ORDER BY id ASC LIMIT 1');
  772. $def_cat = $db->num_rows($result) ? $db->result($result) : 0;
  773. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'pe_default_category\', \''.$def_cat.'\')');
  774. }
  775. // Insert new config option pe_default_index
  776. if (!array_key_exists('pe_default_index', $pun_config))
  777. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'pe_default_index\', \'0\')');
  778. // Insert config option o_base_url which was removed in 1.3
  779. if (!array_key_exists('o_base_url', $pun_config))
  780. {
  781. // If it isn't in $pun_config['o_base_url'] it should be in $base_url, but just in-case it isn't we can make a guess at it
  782. if (!isset($base_url))
  783. {
  784. // Make an educated guess regarding base_url
  785. $base_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://'; // protocol
  786. $base_url .= preg_replace('/:(80|443)$/', '', $_SERVER['HTTP_HOST']); // host[:port]
  787. $base_url .= str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'])); // path
  788. }
  789. if (substr($base_url, -1) == '/')
  790. $base_url = substr($base_url, 0, -1);
  791. $db->query('INSERT INTO '.$db->prefix.'config (conf_name, conf_value) VALUES (\'o_base_url\', \''.$db->escape($base_url).'\')');
  792. }
  793. if (strpos($cur_version, '1.2') === 0)
  794. {
  795. // Groups are almost the same as 1.2:
  796. // unverified: 32000 -> 0
  797. $db->query('UPDATE '.$db->prefix.'users SET group_id = 0 WHERE group_id = 32000');
  798. }
  799. else if (strpos($cur_version, '1.3') === 0)
  800. {
  801. // Groups have changed quite a lot from 1.3:
  802. // unverified: 0 -> 0
  803. // admin: 1 -> 1
  804. // mod: ? -> 2
  805. // guest: 2 -> 3
  806. // member: ? -> 4
  807. $result = $db->query('SELECT MAX(g_id) + 1 FROM '.$db->prefix.'groups');
  808. $temp_id = $db->result($result);
  809. $result = $db->query('SELECT g_id FROM '.$db->prefix.'groups WHERE g_moderator = 1 AND g_id > 1 LIMIT 1');
  810. if ($db->num_rows($result))
  811. $mod_gid = $db->result($result);
  812. else
  813. {
  814. $db->query('INSERT INTO '.$db->prefix.'groups (g_title, g_user_title, g_moderator, g_mod_edit_users, g_mod_rename_users, g_mod_change_passwords, g_mod_ban_users, g_read_board, g_view_users, g_post_replies, g_post_topics, g_edit_posts, g_delete_posts, g_delete_topics, g_set_title, g_search, g_search_users, g_send_email, g_post_flood, g_search_flood, g_email_flood) VALUES('."'Moderators', 'Moderator', 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0)");
  815. $mod_gid = $db->insert_id();
  816. }
  817. $member_gid = $pun_config['o_default_user_group'];
  818. // move the mod group to a temp place
  819. $db->query('UPDATE '.$db->prefix.'groups SET g_id = '.$temp_id.' WHERE g_id = '.$mod_gid);
  820. $db->query('UPDATE '.$db->prefix.'users SET group_id = '.$temp_id.' WHERE group_id = '.$mod_gid);
  821. $db->query('UPDATE '.$db->prefix.'forum_perms SET group_id = '.$temp_id.' WHERE group_id = '.$mod_gid);
  822. if ($member_gid == $mod_gid) $member_gid = $temp_id;
  823. // move whoever is in 3 to a spare slot
  824. $db->query('UPDATE '.$db->prefix.'groups SET g_id = '.$mod_gid.' WHERE g_id = 3');
  825. $db->query('UPDATE '.$db->prefix.'users SET group_id = '.$mod_gid.' WHERE group_id = 3');
  826. $db->query('UPDATE '.$db->prefix.'forum_perms SET group_id = '.$mod_gid.' WHERE group_id = 3');
  827. if ($member_gid == 3) $member_gid = $mod_gid;
  828. // move guest to 3
  829. $db->query('UPDATE '.$db->prefix.'groups SET g_id = 3 WHERE g_id = 2');
  830. $db->query('UPDATE '.$db->prefix.'users SET group_id = 3 WHERE group_id = 2');
  831. $db->query('UPDATE '.$db->prefix.'forum_perms SET group_id = 3 WHERE group_id = 2');
  832. if ($member_gid == 2) $member_gid = 3;
  833. // move mod group in temp place to 2
  834. $db->query('UPDATE '.$db->prefix.'groups SET g_id = 2 WHERE g_id = '.$temp_id);
  835. $db->query('UPDATE '.$db->prefix.'users SET group_id = 2 WHERE group_id = '.$temp_id);
  836. $db->query('UPDATE '.$db->prefix.'forum_perms SET group_id = 2 WHERE group_id = '.$temp_id);
  837. if ($member_gid == $temp_id) $member_gid = 2;
  838. // Only move stuff around if it isn't already in the right place
  839. if ($member_gid != $mod_gid || $member_gid != 4)
  840. {
  841. // move members to temp place
  842. $db->query('UPDATE '.$db->prefix.'groups SET g_id = '.$temp_id.' WHERE g_id = '.$member_gid);
  843. $db->query('UPDATE '.$db->prefix.'users SET group_id = '.$temp_id.' WHERE group_id = '.$member_gid);
  844. $db->query('UPDATE '.$db->prefix.'forum_perms SET group_id = '.$temp_id.' WHERE group_id = '.$member_gid);
  845. // move whoever is in 4 to members place
  846. $db->query('UPDATE '.$db->prefix.'groups SET g_id = '.$member_gid.' WHERE g_id = 4');
  847. $db->query('UPDATE '.$db->prefix.'users SET group_id = '.$member_gid.' WHERE group_id = 4');
  848. $db->query('UPDATE '.$db->prefix.'forum_perms SET group_id = '.$member_gid.' WHERE group_id = 4');
  849. // move members in temp place to 4
  850. $db->query('UPDATE '.$db->prefix.'groups SET g_id = 4 WHERE g_id = '.$temp_id);
  851. $db->query('UPDATE '.$db->prefix.'users SET group_id = 4 WHERE group_id = '.$temp_id);
  852. $db->query('UPDATE '.$db->prefix.'forum_perms SET group_id = 4 WHERE group_id = '.$temp_id);
  853. }
  854. $db->query('UPDATE '.$db->prefix.'config SET conf_value=\''.$member_gid.'\' WHERE conf_name=\'o_default_user_group\'');
  855. }
  856. // Server time zone is now simply the default time zone
  857. if (!array_key_exists('o_default_timezone', $pun_config))
  858. $db->query('UPDATE '.$db->prefix.'config SET conf_name = \'o_default_timezone\' WHERE conf_name = \'o_server_timezone\'');
  859. // Increase visit timeout to 30 minutes (only if it hasn't been changed from the default)
  860. if (!array_key_exists('o_database_revision', $pun_config) && $pun_config['o_timeout_visit'] == '600')
  861. $db->query('UPDATE '.$db->prefix.'config SET conf_value = \'1800\' WHERE conf_name = \'o_timeout_visit\'');
  862. // Remove obsolete g_post_polls permission from groups table
  863. $db->drop_field('groups', 'g_post_polls');
  864. // Make room for multiple moderator groups
  865. if (!$db->field_exists('groups', 'g_moderator'))
  866. {
  867. // Add g_moderator column to groups table
  868. $db->add_field('groups', 'g_moderator', 'TINYINT(1)', false, 0, 'g_user_title');
  869. // Give the moderator group moderator privileges
  870. $db->query('UPDATE '.$db->prefix.'groups SET g_moderator = 1 WHERE g_id = 2');
  871. }
  872. // Replace obsolete p_mod_edit_users config setting with new per-group permission
  873. if (array_key_exists('p_mod_edit_users', $pun_config))
  874. {
  875. $db->query('DELETE FROM '.$db->prefix.'config WHERE conf_name = \'p_mod_edit_users\'');
  876. $db->add_field('groups', 'g_mod_edit_users', 'TINYINT(1)', false, 0, 'g_moderator');
  877. $db->query('UPDATE '.$db->prefix.'groups SET g_mod_edit_users = '.$pun_config['p_mod_edit_users'].' WHERE g_moderator = 1');
  878. }
  879. // Replace obsolete p_mod_rename_users config setting with new per-group permission
  880. if (array_key_exists('p_mod_rename_users', $pun_config))
  881. {
  882. $db->query('DELETE FROM '.$db->prefix.'config WHERE conf_name = \'p_mod_rename_users\'');
  883. $db->add_field('groups', 'g_mod_rename_users', 'TINYINT(1)', false, 0, 'g_mod_edit_users');
  884. $db->query('UPDATE '.$db->prefix.'groups SET g_mod_rename_users = '.$pun_config['p_mod_rename_users'].' WHERE g_moderator = 1');
  885. }
  886. // Replace obsolete p_mod_change_passwords config setting with new per-group permission
  887. if (array_key_exists('p_mod_change_passwords', $pun_config))
  888. {
  889. $db->query('DELETE FROM '.$db->prefix.'config WHERE conf_name = \'p_mod_change_passwords\'');
  890. $db->add_field('groups', 'g_mod_change_passwords', 'TINYINT(1)', false, 0, 'g_mod_rename_users');
  891. $db->query('UPDATE '.$db->prefix.'groups SET g_mod_change_passwords = '.$pun_config['p_mod_change_passwords'].' WHERE g_moderator = 1');
  892. }
  893. // Replace obsolete p_mod_ban_users config setting with new per-group permission
  894. if (array_key_exists('p_mod_ban_users', $pun_config))
  895. {
  896. $db->query('DELETE FROM '.$db->prefix.'config WHERE conf_name = \'p_mod_ban_users\'');
  897. $db->add_field('groups', 'g_mod_ban_users', 'TINYINT(1)', false, 0, 'g_mod_change_passwords');
  898. $db->query('UPDATE '.$db->prefix.'groups SET g_mod_ban_users = '.$pun_config['p_mod_ban_users'].' WHERE g_moderator = 1');
  899. }
  900. // We need to add a unique index to avoid users having multiple rows in the online table
  901. if (!$db->index_exists('online', 'user_id_ident_idx'))
  902. {
  903. $db->truncate_table('online');
  904. if ($mysql)
  905. $db->add_index('online', 'user_id_ident_idx', array('user_id', 'ident(25)'), true);
  906. else
  907. $db->add_index('online', 'user_id_ident_idx', array('user_id', 'ident'), true);
  908. }
  909. // Remove the redundant user_id_idx on the online table
  910. $db->drop_index('online', 'user_id_idx');
  911. // Add an index to ident on the online table
  912. if ($mysql)
  913. $db->add_index('online', 'ident_idx', array('ident(25)'));
  914. else
  915. $db->add_index('online', 'ident_idx', array('ident'));
  916. // Add an index to logged in the online table
  917. $db->add_index('online', 'logged_idx', array('logged'));
  918. // Add an index to last_post in the topics table
  919. $db->add_index('topics', 'last_post_idx', array('last_post'));
  920. // Add an index to username on the bans table
  921. if ($mysql)
  922. $db->add_index('bans', 'username_idx', array('username(25)'));
  923. else
  924. $db->add_index('bans', 'username_idx', array('username'));
  925. // Change the username_idx on users to a unique index of max size 25
  926. $db->drop_index('users', 'username_idx');
  927. $field = $mysql ? 'username(25)' : 'username';
  928. // Attempt to add a unique index. If the user doesn't use a transactional database this can fail due to multiple matching usernames in the
  929. // users table. This is bad, but just giving up if it happens is even worse! If it fails just add a regular non-unique index.
  930. if (!$db->add_index('users', 'username_idx', array($field), true))
  931. $db->add_index('users', 'username_idx', array($field));
  932. // Add g_view_users field to groups table
  933. $db->add_field('groups', 'g_view_users', 'TINYINT(1)', false, 1, 'g_read_board');
  934. // Add the last_email_sent column to the users table and the g_send_email and
  935. // g_email_flood columns to the groups table
  936. $db->add_field('users', 'last_email_sent', 'INT(10) UNSIGNED', true, null, 'last_search');
  937. $db->add_field('groups', 'g_send_email', 'TINYINT(1)', false, 1, 'g_search_users');
  938. $db->add_field('groups', 'g_email_flood', 'SMALLINT(6)', false, 60, 'g_search_flood');
  939. // Set non-default g_send_email and g_flood_email values properly
  940. $db->query('UPDATE '.$db->prefix.'groups SET g_send_email = 0 WHERE g_id = 3');
  941. $db->query('UPDATE '.$db->prefix.'groups SET g_email_flood = 0 WHERE g_id IN (1,2,3)');
  942. // Add the auto notify/subscription option to the users table
  943. $db->add_field('users', 'auto_notify', 'TINYINT(1)', false, 1, 'notify_with_post');
  944. // Change the auto_notify field default value to '1' (in FluxBB 1.4 it is '0' by default)
  945. $db->alter_field('users', 'auto_notify', 'TINYINT(1)', false, 1);
  946. // Add the first_post_id column to the topics table
  947. if (!$db->field_exists('topics', 'first_post_id'))
  948. {
  949. $db->add_field('topics', 'first_post_id', 'INT(10) UNSIGNED', false, 0, 'posted');
  950. $db->add_index('topics', 'first_post_id_idx', array('first_post_id'));
  951. // Now that we've added the column and indexed it, we need to give it correct data
  952. $result = $db->query('SELECT MIN(id) AS first_post, topic_id FROM '.$db->prefix.'posts GROUP BY topic_id');
  953. while ($cur_post = $db->fetch_assoc($result))
  954. $db->query('UPDATE '.$db->prefix.'topics SET first_post_id = '.$cur_post['first_post'].' WHERE id = '.$cur_post['topic_id']);
  955. }
  956. // Move any users with the old unverified status to their new group
  957. $db->query('UPDATE '.$db->prefix.'users SET group_id=0 WHERE group_id=32000');
  958. // Add the ban_creator column to the bans table
  959. $db->add_field('bans', 'ban_creator', 'INT(10) UNSIGNED', false, 0);
  960. // Add the time/date format settings to the user table
  961. $db->add_field('users', 'time_format', 'TINYINT(1)', false, 0, 'dst');
  962. $db->add_field('users', 'date_format', 'TINYINT(1)', false, 0, 'dst');
  963. // Change the search_data field to mediumtext
  964. $db->alter_field('search_cache', 'search_data', 'MEDIUMTEXT', true);
  965. // Incase we had the fulltext search extension installed (1.3-legacy), remove it
  966. $db->drop_index('topics', 'subject_idx');
  967. $db->drop_index('posts', 'message_idx');
  968. // Incase we had the fulltext search mod installed (1.2), remove it
  969. $db->drop_index('topics', 'subject_fulltext_search');
  970. $db->drop_index('posts', 'message_fulltext_search');
  971. // If the search_cache table has been dropped by the fulltext search extension, recreate it
  972. if (!$db->table_exists('search_cache'))
  973. {
  974. $schema = array(
  975. 'FIELDS' => array(
  976. 'id' => array(
  977. 'datatype' => 'INT(10) UNSIGNED',
  978. 'allow_null' => false,
  979. 'default' => '0'
  980. ),
  981. 'ident' => array(
  982. 'datatype' => 'VARCHAR(200)',
  983. 'allow_null' => false,
  984. 'default' => '\'\''
  985. ),
  986. 'search_data' => array(
  987. 'datatype' => 'MEDIUMTEXT',
  988. 'allow_null' => true
  989. )
  990. ),
  991. 'PRIMARY KEY' => array('id'),
  992. 'INDEXES' => array(
  993. 'ident_idx' => array('ident')
  994. )
  995. );
  996. if ($db_type == 'mysql' || $db_type == 'mysqli' || $db_type == 'mysql_innodb' || $db_type == 'mysqli_innodb')
  997. $schema['INDEXES']['ident_idx'] = array('ident(8)');
  998. $db->create_table('search_cache', $schema);
  999. }
  1000. // If the search_matches table has been dropped by the fulltext search extension, recreate it
  1001. if (!$db->table_exists('search_matches'))
  1002. {
  1003. $schema = array(
  1004. 'FIELDS' => array(
  1005. 'post_id' => array(
  1006. 'datatype' => 'INT(10) UNSIGNED',
  1007. 'allow_null' => false,
  1008. 'default' => '0'
  1009. ),
  1010. 'word_id' => array(
  1011. 'datatype' => 'INT(10) UNSIGNED',
  1012. 'allow_null' => false,
  1013. 'default' => '0'
  1014. ),
  1015. 'subject_match' => array(
  1016. 'datatype' => 'TINYINT(1)',
  1017. 'allow_null' => false,
  1018. 'default' => '0'
  1019. )
  1020. ),
  1021. 'INDEXES' => array(
  1022. 'word_id_idx' => array('word_id'),

Large files files are truncated, but you can click here to view the full file