PageRenderTime 78ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/install/upgrade.php

https://github.com/Arantor/Elkarte
PHP | 4467 lines | 3461 code | 597 blank | 409 comment | 721 complexity | 3fe4172b1f5f9edd38337f4143b567e3 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /**
  3. * @name ElkArte Forum
  4. * @copyright ElkArte Forum contributors
  5. * @license BSD http://opensource.org/licenses/BSD-3-Clause
  6. *
  7. * This software is a derived product, based on:
  8. *
  9. * Simple Machines Forum (SMF)
  10. * copyright: 2011 Simple Machines (http://www.simplemachines.org)
  11. * license: BSD, See included LICENSE.TXT for terms and conditions.
  12. *
  13. * @version 1.0 Alpha
  14. *
  15. */
  16. // Version information...
  17. define('CURRENT_VERSION', '1.0 Alpha');
  18. define('CURRENT_LANG_VERSION', '1.0');
  19. $GLOBALS['required_php_version'] = '5.1.0';
  20. $GLOBALS['required_mysql_version'] = '4.0.18';
  21. $databases = array(
  22. 'mysql' => array(
  23. 'name' => 'MySQL',
  24. 'version' => '4.0.18',
  25. 'version_check' => 'return min(mysql_get_server_info(), mysql_get_client_info());',
  26. 'utf8_support' => true,
  27. 'utf8_version' => '4.1.0',
  28. 'utf8_version_check' => 'return mysql_get_server_info();',
  29. 'alter_support' => true,
  30. ),
  31. 'postgresql' => array(
  32. 'name' => 'PostgreSQL',
  33. 'version' => '8.0',
  34. 'version_check' => '$version = pg_version(); return $version[\'client\'];',
  35. 'always_has_db' => true,
  36. ),
  37. 'sqlite' => array(
  38. 'name' => 'SQLite',
  39. 'version' => '1',
  40. 'version_check' => 'return 1;',
  41. 'always_has_db' => true,
  42. ),
  43. );
  44. // General options for the script.
  45. $timeLimitThreshold = 3;
  46. $upgrade_path = dirname(__FILE__);
  47. $upgradeurl = $_SERVER['PHP_SELF'];
  48. // Where the images etc are kept.
  49. $oursite = 'http://www.elkarte.net';
  50. // Disable the need for admins to login?
  51. $disable_security = false;
  52. // How long, in seconds, must admin be inactive to allow someone else to run?
  53. $upcontext['inactive_timeout'] = 10;
  54. // All the steps in detail.
  55. // Number,Name,Function,Progress Weight.
  56. $upcontext['steps'] = array(
  57. 0 => array(1, 'Login', 'action_welcomeLogin', 2),
  58. 1 => array(2, 'Upgrade Options', 'action_upgradeOptions', 2),
  59. 2 => array(3, 'Backup', 'action_backupDatabase', 10),
  60. 3 => array(4, 'Database Changes', 'action_databaseChanges', 70),
  61. // This is removed as it doesn't really work right at the moment.
  62. //4 => array(5, 'Cleanup Mods', 'action_cleanupMods', 10),
  63. 4 => array(5, 'Delete Upgrade', 'action_deleteUpgrade', 1),
  64. );
  65. // Just to remember which one has files in it.
  66. $upcontext['database_step'] = 3;
  67. @set_time_limit(600);
  68. if (!ini_get('safe_mode'))
  69. {
  70. ini_set('mysql.connect_timeout', -1);
  71. ini_set('default_socket_timeout', 900);
  72. }
  73. // Clean the upgrade path if this is from the client.
  74. if (!empty($_SERVER['argv']) && php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR']))
  75. for ($i = 1; $i < $_SERVER['argc']; $i++)
  76. {
  77. if (preg_match('~^--path=(.+)$~', $_SERVER['argv'][$i], $match) != 0)
  78. $upgrade_path = substr($match[1], -1) == '/' ? substr($match[1], 0, -1) : $match[1];
  79. }
  80. // Are we from the client?
  81. if (php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR']))
  82. {
  83. $command_line = true;
  84. $disable_security = 1;
  85. }
  86. else
  87. $command_line = false;
  88. // Load this now just because we can.
  89. require_once($upgrade_path . '/Settings.php');
  90. // Are we logged in?
  91. if (isset($upgradeData))
  92. {
  93. $upcontext['user'] = unserialize(base64_decode($upgradeData));
  94. // Check for sensible values.
  95. if (empty($upcontext['user']['started']) || $upcontext['user']['started'] < time() - 86400)
  96. $upcontext['user']['started'] = time();
  97. if (empty($upcontext['user']['updated']) || $upcontext['user']['updated'] < time() - 86400)
  98. $upcontext['user']['updated'] = 0;
  99. $upcontext['started'] = $upcontext['user']['started'];
  100. $upcontext['updated'] = $upcontext['user']['updated'];
  101. }
  102. // Nothing sensible?
  103. if (empty($upcontext['updated']))
  104. {
  105. $upcontext['started'] = time();
  106. $upcontext['updated'] = 0;
  107. $upcontext['user'] = array(
  108. 'id' => 0,
  109. 'name' => 'Guest',
  110. 'pass' => 0,
  111. 'started' => $upcontext['started'],
  112. 'updated' => $upcontext['updated'],
  113. );
  114. }
  115. // Load up some essential data...
  116. loadEssentialData();
  117. // Are we going to be mimic'ing SSI at this point?
  118. if (isset($_GET['ssi']))
  119. {
  120. require_once(SOURCEDIR . '/Subs.php');
  121. require_once(SOURCEDIR . '/Errors.php');
  122. require_once(SOURCEDIR . '/Logging.php');
  123. require_once(SOURCEDIR . '/Load.php');
  124. require_once(SUBSDIR . '/Cache.subs.php');
  125. require_once(SOURCEDIR . '/Security.php');
  126. require_once(SUBSDIR . '/Package.subs.php');
  127. loadUserSettings();
  128. loadPermissions();
  129. }
  130. // All the non-SSI stuff.
  131. if (!function_exists('ip2range'))
  132. require_once(SOURCEDIR . '/Subs.php');
  133. if (!function_exists('un_htmlspecialchars'))
  134. {
  135. function un_htmlspecialchars($string)
  136. {
  137. return strtr($string, array_flip(get_html_translation_table(HTML_SPECIALCHARS, ENT_QUOTES)) + array('&#039;' => '\'', '&nbsp;' => ' '));
  138. }
  139. }
  140. if (!function_exists('text2words'))
  141. {
  142. function text2words($text)
  143. {
  144. global $smcFunc;
  145. // Step 1: Remove entities/things we don't consider words:
  146. $words = preg_replace('~(?:[\x0B\0\xA0\t\r\s\n(){}\\[\\]<>!@$%^*.,:+=`\~\?/\\\\]+|&(?:amp|lt|gt|quot);)+~', ' ', $text);
  147. // Step 2: Entities we left to letters, where applicable, lowercase.
  148. $words = preg_replace('~([^&\d]|^)[#;]~', '$1 ', un_htmlspecialchars(strtolower($words)));
  149. // Step 3: Ready to split apart and index!
  150. $words = explode(' ', $words);
  151. $returned_words = array();
  152. foreach ($words as $word)
  153. {
  154. $word = trim($word, '-_\'');
  155. if ($word != '')
  156. $returned_words[] = substr($word, 0, 20);
  157. }
  158. return array_unique($returned_words);
  159. }
  160. }
  161. if (!function_exists('clean_cache'))
  162. {
  163. // Empty out the cache folder.
  164. function clean_cache($type = '')
  165. {
  166. // No directory = no game.
  167. if (!is_dir(CACHEDIR))
  168. return;
  169. // Remove the files in our own disk cache, if any
  170. $dh = opendir(CACHEDIR);
  171. while ($file = readdir($dh))
  172. {
  173. if ($file != '.' && $file != '..' && $file != 'index.php' && $file != '.htaccess' && (!$type || substr($file, 0, strlen($type)) == $type))
  174. @unlink(CACHEDIR . '/' . $file);
  175. }
  176. closedir($dh);
  177. // Invalidate cache, to be sure!
  178. // ... as long as Load.php can be modified, anyway.
  179. @touch(SOURCEDIR . '/' . 'Load.php');
  180. clearstatcache();
  181. }
  182. }
  183. // MD5 Encryption.
  184. if (!function_exists('md5_hmac'))
  185. {
  186. function md5_hmac($data, $key)
  187. {
  188. if (strlen($key) > 64)
  189. $key = pack('H*', md5($key));
  190. $key = str_pad($key, 64, chr(0x00));
  191. $k_ipad = $key ^ str_repeat(chr(0x36), 64);
  192. $k_opad = $key ^ str_repeat(chr(0x5c), 64);
  193. return md5($k_opad . pack('H*', md5($k_ipad . $data)));
  194. }
  195. }
  196. // http://www.faqs.org/rfcs/rfc959.html
  197. if (!class_exists('Ftp_Connection'))
  198. {
  199. class Ftp_Connection
  200. {
  201. var $connection = 'no_connection', $error = false, $last_message, $pasv = array();
  202. // Create a new FTP connection...
  203. function ftp_connection($ftp_server, $ftp_port = 21, $ftp_user = 'anonymous', $ftp_pass = 'ftpclient@yourdomain.org')
  204. {
  205. if ($ftp_server !== null)
  206. $this->connect($ftp_server, $ftp_port, $ftp_user, $ftp_pass);
  207. }
  208. function connect($ftp_server, $ftp_port = 21, $ftp_user = 'anonymous', $ftp_pass = 'ftpclient@yourdomain.org')
  209. {
  210. if (substr($ftp_server, 0, 6) == 'ftp://')
  211. $ftp_server = substr($ftp_server, 6);
  212. elseif (substr($ftp_server, 0, 7) == 'ftps://')
  213. $ftp_server = 'ssl://' . substr($ftp_server, 7);
  214. if (substr($ftp_server, 0, 7) == 'http://')
  215. $ftp_server = substr($ftp_server, 7);
  216. $ftp_server = strtr($ftp_server, array('/' => '', ':' => '', '@' => ''));
  217. // Connect to the FTP server.
  218. $this->connection = @fsockopen($ftp_server, $ftp_port, $err, $err, 5);
  219. if (!$this->connection)
  220. {
  221. $this->error = 'bad_server';
  222. return;
  223. }
  224. // Get the welcome message...
  225. if (!$this->check_response(220))
  226. {
  227. $this->error = 'bad_response';
  228. return;
  229. }
  230. // Send the username, it should ask for a password.
  231. fwrite($this->connection, 'USER ' . $ftp_user . "\r\n");
  232. if (!$this->check_response(331))
  233. {
  234. $this->error = 'bad_username';
  235. return;
  236. }
  237. // Now send the password... and hope it goes okay.
  238. fwrite($this->connection, 'PASS ' . $ftp_pass . "\r\n");
  239. if (!$this->check_response(230))
  240. {
  241. $this->error = 'bad_password';
  242. return;
  243. }
  244. }
  245. function chdir($ftp_path)
  246. {
  247. if (!is_resource($this->connection))
  248. return false;
  249. // No slash on the end, please...
  250. if (substr($ftp_path, -1) == '/' && $ftp_path !== '/')
  251. $ftp_path = substr($ftp_path, 0, -1);
  252. fwrite($this->connection, 'CWD ' . $ftp_path . "\r\n");
  253. if (!$this->check_response(250))
  254. {
  255. $this->error = 'bad_path';
  256. return false;
  257. }
  258. return true;
  259. }
  260. function chmod($ftp_file, $chmod)
  261. {
  262. if (!is_resource($this->connection))
  263. return false;
  264. // Convert the chmod value from octal (0777) to text ("777").
  265. fwrite($this->connection, 'SITE CHMOD ' . decoct($chmod) . ' ' . $ftp_file . "\r\n");
  266. if (!$this->check_response(200))
  267. {
  268. $this->error = 'bad_file';
  269. return false;
  270. }
  271. return true;
  272. }
  273. function unlink($ftp_file)
  274. {
  275. // We are actually connected, right?
  276. if (!is_resource($this->connection))
  277. return false;
  278. // Delete file X.
  279. fwrite($this->connection, 'DELE ' . $ftp_file . "\r\n");
  280. if (!$this->check_response(250))
  281. {
  282. fwrite($this->connection, 'RMD ' . $ftp_file . "\r\n");
  283. // Still no love?
  284. if (!$this->check_response(250))
  285. {
  286. $this->error = 'bad_file';
  287. return false;
  288. }
  289. }
  290. return true;
  291. }
  292. function check_response($desired)
  293. {
  294. // Wait for a response that isn't continued with -, but don't wait too long.
  295. $time = time();
  296. do
  297. $this->last_message = fgets($this->connection, 1024);
  298. while (substr($this->last_message, 3, 1) != ' ' && time() - $time < 5);
  299. // Was the desired response returned?
  300. return is_array($desired) ? in_array(substr($this->last_message, 0, 3), $desired) : substr($this->last_message, 0, 3) == $desired;
  301. }
  302. function passive()
  303. {
  304. // We can't create a passive data connection without a primary one first being there.
  305. if (!is_resource($this->connection))
  306. return false;
  307. // Request a passive connection - this means, we'll talk to you, you don't talk to us.
  308. @fwrite($this->connection, 'PASV' . "\r\n");
  309. $time = time();
  310. do
  311. $response = fgets($this->connection, 1024);
  312. while (substr($response, 3, 1) != ' ' && time() - $time < 5);
  313. // If it's not 227, we weren't given an IP and port, which means it failed.
  314. if (substr($response, 0, 4) != '227 ')
  315. {
  316. $this->error = 'bad_response';
  317. return false;
  318. }
  319. // Snatch the IP and port information, or die horribly trying...
  320. if (preg_match('~\((\d+),\s*(\d+),\s*(\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+))\)~', $response, $match) == 0)
  321. {
  322. $this->error = 'bad_response';
  323. return false;
  324. }
  325. // This is pretty simple - store it for later use ;).
  326. $this->pasv = array('ip' => $match[1] . '.' . $match[2] . '.' . $match[3] . '.' . $match[4], 'port' => $match[5] * 256 + $match[6]);
  327. return true;
  328. }
  329. function create_file($ftp_file)
  330. {
  331. // First, we have to be connected... very important.
  332. if (!is_resource($this->connection))
  333. return false;
  334. // I'd like one passive mode, please!
  335. if (!$this->passive())
  336. return false;
  337. // Seems logical enough, so far...
  338. fwrite($this->connection, 'STOR ' . $ftp_file . "\r\n");
  339. // Okay, now we connect to the data port. If it doesn't work out, it's probably "file already exists", etc.
  340. $fp = @fsockopen($this->pasv['ip'], $this->pasv['port'], $err, $err, 5);
  341. if (!$fp || !$this->check_response(150))
  342. {
  343. $this->error = 'bad_file';
  344. @fclose($fp);
  345. return false;
  346. }
  347. // This may look strange, but we're just closing it to indicate a zero-byte upload.
  348. fclose($fp);
  349. if (!$this->check_response(226))
  350. {
  351. $this->error = 'bad_response';
  352. return false;
  353. }
  354. return true;
  355. }
  356. function list_dir($ftp_path = '', $search = false)
  357. {
  358. // Are we even connected...?
  359. if (!is_resource($this->connection))
  360. return false;
  361. // Passive... non-agressive...
  362. if (!$this->passive())
  363. return false;
  364. // Get the listing!
  365. fwrite($this->connection, 'LIST -1' . ($search ? 'R' : '') . ($ftp_path == '' ? '' : ' ' . $ftp_path) . "\r\n");
  366. // Connect, assuming we've got a connection.
  367. $fp = @fsockopen($this->pasv['ip'], $this->pasv['port'], $err, $err, 5);
  368. if (!$fp || !$this->check_response(array(150, 125)))
  369. {
  370. $this->error = 'bad_response';
  371. @fclose($fp);
  372. return false;
  373. }
  374. // Read in the file listing.
  375. $data = '';
  376. while (!feof($fp))
  377. $data .= fread($fp, 4096);
  378. fclose($fp);
  379. // Everything go okay?
  380. if (!$this->check_response(226))
  381. {
  382. $this->error = 'bad_response';
  383. return false;
  384. }
  385. return $data;
  386. }
  387. function locate($file, $listing = null)
  388. {
  389. if ($listing === null)
  390. $listing = $this->list_dir('', true);
  391. $listing = explode("\n", $listing);
  392. @fwrite($this->connection, 'PWD' . "\r\n");
  393. $time = time();
  394. do
  395. $response = fgets($this->connection, 1024);
  396. while (substr($response, 3, 1) != ' ' && time() - $time < 5);
  397. // Check for 257!
  398. if (preg_match('~^257 "(.+?)" ~', $response, $match) != 0)
  399. $current_dir = strtr($match[1], array('""' => '"'));
  400. else
  401. $current_dir = '';
  402. for ($i = 0, $n = count($listing); $i < $n; $i++)
  403. {
  404. if (trim($listing[$i]) == '' && isset($listing[$i + 1]))
  405. {
  406. $current_dir = substr(trim($listing[++$i]), 0, -1);
  407. $i++;
  408. }
  409. // Okay, this file's name is:
  410. $listing[$i] = $current_dir . '/' . trim(strlen($listing[$i]) > 30 ? strrchr($listing[$i], ' ') : $listing[$i]);
  411. if (substr($file, 0, 1) == '*' && substr($listing[$i], -(strlen($file) - 1)) == substr($file, 1))
  412. return $listing[$i];
  413. if (substr($file, -1) == '*' && substr($listing[$i], 0, strlen($file) - 1) == substr($file, 0, -1))
  414. return $listing[$i];
  415. if (basename($listing[$i]) == $file || $listing[$i] == $file)
  416. return $listing[$i];
  417. }
  418. return false;
  419. }
  420. function create_dir($ftp_dir)
  421. {
  422. // We must be connected to the server to do something.
  423. if (!is_resource($this->connection))
  424. return false;
  425. // Make this new beautiful directory!
  426. fwrite($this->connection, 'MKD ' . $ftp_dir . "\r\n");
  427. if (!$this->check_response(257))
  428. {
  429. $this->error = 'bad_file';
  430. return false;
  431. }
  432. return true;
  433. }
  434. function detect_path($filesystem_path, $lookup_file = null)
  435. {
  436. $username = '';
  437. if (isset($_SERVER['DOCUMENT_ROOT']))
  438. {
  439. if (preg_match('~^/home[2]?/([^/]+?)/public_html~', $_SERVER['DOCUMENT_ROOT'], $match))
  440. {
  441. $username = $match[1];
  442. $path = strtr($_SERVER['DOCUMENT_ROOT'], array('/home/' . $match[1] . '/' => '', '/home2/' . $match[1] . '/' => ''));
  443. if (substr($path, -1) == '/')
  444. $path = substr($path, 0, -1);
  445. if (strlen(dirname($_SERVER['PHP_SELF'])) > 1)
  446. $path .= dirname($_SERVER['PHP_SELF']);
  447. }
  448. elseif (substr($filesystem_path, 0, 9) == '/var/www/')
  449. $path = substr($filesystem_path, 8);
  450. else
  451. $path = strtr(strtr($filesystem_path, array('\\' => '/')), array($_SERVER['DOCUMENT_ROOT'] => ''));
  452. }
  453. else
  454. $path = '';
  455. if (is_resource($this->connection) && $this->list_dir($path) == '')
  456. {
  457. $data = $this->list_dir('', true);
  458. if ($lookup_file === null)
  459. $lookup_file = $_SERVER['PHP_SELF'];
  460. $found_path = dirname($this->locate('*' . basename(dirname($lookup_file)) . '/' . basename($lookup_file), $data));
  461. if ($found_path == false)
  462. $found_path = dirname($this->locate(basename($lookup_file)));
  463. if ($found_path != false)
  464. $path = $found_path;
  465. }
  466. elseif (is_resource($this->connection))
  467. $found_path = true;
  468. return array($username, $path, isset($found_path));
  469. }
  470. function close()
  471. {
  472. // Goodbye!
  473. fwrite($this->connection, 'QUIT' . "\r\n");
  474. fclose($this->connection);
  475. return true;
  476. }
  477. }
  478. }
  479. // Don't do security check if on Yabbse
  480. if (!isset($modSettings['elkVersion']))
  481. $disable_security = true;
  482. // Does this exist?
  483. if (isset($modSettings['elkVersion']))
  484. {
  485. $request = $smcFunc['db_query']('', '
  486. SELECT variable, value
  487. FROM {db_prefix}themes
  488. WHERE id_theme = {int:id_theme}
  489. AND variable IN ({string:theme_url}, {string:theme_dir}, {string:images_url})',
  490. array(
  491. 'id_theme' => 1,
  492. 'theme_url' => 'theme_url',
  493. 'theme_dir' => 'theme_dir',
  494. 'images_url' => 'images_url',
  495. 'db_error_skip' => true,
  496. )
  497. );
  498. while ($row = $smcFunc['db_fetch_assoc']($request))
  499. $modSettings[$row['variable']] = $row['value'];
  500. $smcFunc['db_free_result']($request);
  501. }
  502. if (!isset($modSettings['theme_url']))
  503. {
  504. $modSettings['theme_dir'] = BOARDDIR . '/themes/default';
  505. $modSettings['theme_url'] = 'themes/default';
  506. $modSettings['images_url'] = 'themes/default/images';
  507. }
  508. if (!isset($settings['default_theme_url']))
  509. $settings['default_theme_url'] = $modSettings['theme_url'];
  510. if (!isset($settings['default_theme_dir']))
  511. $settings['default_theme_dir'] = $modSettings['theme_dir'];
  512. $upcontext['is_large_forum'] = (empty($modSettings['smfVersion']) || $modSettings['smfVersion'] <= '1.1 RC1') && !empty($modSettings['totalMessages']) && $modSettings['totalMessages'] > 75000;
  513. // Default title...
  514. $upcontext['page_title'] = isset($modSettings['elkVersion']) ? 'Updating Your Elkarte Install!' : isset($modSettings['smfVersion']) ? 'Upgrading from SMF!' : 'Upgrading from YaBB SE!';
  515. $upcontext['right_to_left'] = isset($txt['lang_rtl']) ? $txt['lang_rtl'] : false;
  516. // Have we got log data - if so use it (It will be clean!)
  517. if (isset($_GET['data']))
  518. {
  519. $upcontext['upgrade_status'] = unserialize(base64_decode($_GET['data']));
  520. $upcontext['current_step'] = $upcontext['upgrade_status']['curstep'];
  521. $upcontext['language'] = $upcontext['upgrade_status']['lang'];
  522. $upcontext['rid'] = $upcontext['upgrade_status']['rid'];
  523. $is_debug = $upcontext['upgrade_status']['debug'];
  524. $support_js = $upcontext['upgrade_status']['js'];
  525. // Load the language.
  526. if (file_exists($modSettings['theme_dir'] . '/languages/Install.' . $upcontext['language'] . '.php'))
  527. require_once($modSettings['theme_dir'] . '/languages/Install.' . $upcontext['language'] . '.php');
  528. }
  529. // Set the defaults.
  530. else
  531. {
  532. $upcontext['current_step'] = 0;
  533. $upcontext['rid'] = mt_rand(0, 5000);
  534. $upcontext['upgrade_status'] = array(
  535. 'curstep' => 0,
  536. 'lang' => isset($_GET['lang']) ? $_GET['lang'] : basename($language, '.lng'),
  537. 'rid' => $upcontext['rid'],
  538. 'pass' => 0,
  539. 'debug' => 0,
  540. 'js' => 0,
  541. );
  542. $upcontext['language'] = $upcontext['upgrade_status']['lang'];
  543. }
  544. // If this isn't the first stage see whether they are logging in and resuming.
  545. if ($upcontext['current_step'] != 0 || !empty($upcontext['user']['step']))
  546. checkLogin();
  547. if ($command_line)
  548. cmdStep0();
  549. // Don't error if we're using xml.
  550. if (isset($_GET['xml']))
  551. $upcontext['return_error'] = true;
  552. // Loop through all the steps doing each one as required.
  553. $upcontext['overall_percent'] = 0;
  554. foreach ($upcontext['steps'] as $num => $step)
  555. {
  556. if ($num >= $upcontext['current_step'])
  557. {
  558. // The current weight of this step in terms of overall progress.
  559. $upcontext['step_weight'] = $step[3];
  560. // Make sure we reset the skip button.
  561. $upcontext['skip'] = false;
  562. // We cannot proceed if we're not logged in.
  563. if ($num != 0 && !$disable_security && $upcontext['user']['pass'] != $upcontext['upgrade_status']['pass'])
  564. {
  565. $upcontext['steps'][0][2]();
  566. break;
  567. }
  568. // Call the step and if it returns false that means pause!
  569. if (function_exists($step[2]) && $step[2]() === false)
  570. break;
  571. elseif (function_exists($step[2]))
  572. $upcontext['current_step']++;
  573. }
  574. $upcontext['overall_percent'] += $step[3];
  575. }
  576. upgradeExit();
  577. // Exit the upgrade script.
  578. function upgradeExit($fallThrough = false)
  579. {
  580. global $upcontext, $upgradeurl, $command_line;
  581. // Save where we are...
  582. if (!empty($upcontext['current_step']) && !empty($upcontext['user']['id']))
  583. {
  584. $upcontext['user']['step'] = $upcontext['current_step'];
  585. $upcontext['user']['substep'] = $_GET['substep'];
  586. $upcontext['user']['updated'] = time();
  587. $upgradeData = base64_encode(serialize($upcontext['user']));
  588. copy(BOARDDIR . '/Settings.php', BOARDDIR . '/Settings_bak.php');
  589. changeSettings(array('upgradeData' => '"' . $upgradeData . '"'));
  590. updateLastError();
  591. }
  592. // Handle the progress of the step, if any.
  593. if (!empty($upcontext['step_progress']) && isset($upcontext['steps'][$upcontext['current_step']]))
  594. {
  595. $upcontext['step_progress'] = round($upcontext['step_progress'], 1);
  596. $upcontext['overall_percent'] += $upcontext['step_progress'] * ($upcontext['steps'][$upcontext['current_step']][3] / 100);
  597. }
  598. $upcontext['overall_percent'] = (int) $upcontext['overall_percent'];
  599. // We usually dump our templates out.
  600. if (!$fallThrough)
  601. {
  602. // This should not happen my dear... HELP ME DEVELOPERS!!
  603. if (!empty($command_line))
  604. {
  605. if (function_exists('debug_print_backtrace'))
  606. debug_print_backtrace();
  607. echo "\n" . 'Error: Unexpected call to use the ' . (isset($upcontext['sub_template']) ? $upcontext['sub_template'] : '') . ' template. Please copy and paste all the text above and visit the Elkarte Community to tell the Developers that they\'ve made a doh!; they\'ll get you up and running again.';
  608. flush();
  609. die();
  610. }
  611. if (!isset($_GET['xml']))
  612. template_upgrade_above();
  613. else
  614. {
  615. header('Content-Type: text/xml; charset=ISO-8859-1');
  616. // Sadly we need to retain the $_GET data thanks to the old upgrade scripts.
  617. $upcontext['get_data'] = array();
  618. foreach ($_GET as $k => $v)
  619. {
  620. if (substr($k, 0, 3) != 'amp' && !in_array($k, array('xml', 'substep', 'lang', 'data', 'step', 'filecount')))
  621. {
  622. $upcontext['get_data'][$k] = $v;
  623. }
  624. }
  625. template_xml_above();
  626. }
  627. // Call the template.
  628. if (isset($upcontext['sub_template']))
  629. {
  630. $upcontext['upgrade_status']['curstep'] = $upcontext['current_step'];
  631. $upcontext['form_url'] = $upgradeurl . '?step=' . $upcontext['current_step'] . '&amp;substep=' . $_GET['substep'] . '&amp;data=' . base64_encode(serialize($upcontext['upgrade_status']));
  632. // Custom stuff to pass back?
  633. if (!empty($upcontext['query_string']))
  634. $upcontext['form_url'] .= $upcontext['query_string'];
  635. call_user_func('template_' . $upcontext['sub_template']);
  636. }
  637. // Was there an error?
  638. if (!empty($upcontext['forced_error_message']))
  639. echo $upcontext['forced_error_message'];
  640. // Show the footer.
  641. if (!isset($_GET['xml']))
  642. template_upgrade_below();
  643. else
  644. template_xml_below();
  645. }
  646. // Bang - gone!
  647. die();
  648. }
  649. // Used to direct the user to another location.
  650. function redirectLocation($location, $addForm = true)
  651. {
  652. global $upgradeurl, $upcontext, $command_line;
  653. // Command line users can't be redirected.
  654. if ($command_line)
  655. upgradeExit(true);
  656. // Are we providing the core info?
  657. if ($addForm)
  658. {
  659. $upcontext['upgrade_status']['curstep'] = $upcontext['current_step'];
  660. $location = $upgradeurl . '?step=' . $upcontext['current_step'] . '&substep=' . $_GET['substep'] . '&data=' . base64_encode(serialize($upcontext['upgrade_status'])) . $location;
  661. }
  662. while (@ob_end_clean());
  663. header('Location: ' . strtr($location, array('&amp;' => '&')));
  664. // Exit - saving status as we go.
  665. upgradeExit(true);
  666. }
  667. // Load all essential data and connect to the DB as this is pre SSI.php
  668. function loadEssentialData()
  669. {
  670. global $db_server, $db_user, $db_passwd, $db_name, $db_connection, $db_prefix, $db_character_set, $db_type;
  671. global $modSettings, $smcFunc, $upcontext;
  672. // Do the non-SSI stuff...
  673. @set_magic_quotes_runtime(0);
  674. error_reporting(E_ALL);
  675. define('ELKARTE', 1);
  676. // Start the session.
  677. if (@ini_get('session.save_handler') == 'user')
  678. @ini_set('session.save_handler', 'files');
  679. @session_start();
  680. if (empty($smcFunc))
  681. $smcFunc = array();
  682. // Initialize everything...
  683. initialize_inputs();
  684. // Get the database going!
  685. if (empty($db_type))
  686. $db_type = 'mysql';
  687. if (file_exists(SOURCEDIR . '/database/Db-' . $db_type . '.subs.php'))
  688. {
  689. require_once(SOURCEDIR . '/database/Db-' . $db_type . '.subs.php');
  690. // Make the connection...
  691. $db_connection = smf_db_initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, array('non_fatal' => true));
  692. // Oh dear god!!
  693. if ($db_connection === null)
  694. die('Unable to connect to database - please check username and password are correct in Settings.php');
  695. if ($db_type == 'mysql' && isset($db_character_set) && preg_match('~^\w+$~', $db_character_set) === 1)
  696. $smcFunc['db_query']('', '
  697. SET NAMES ' . $db_character_set,
  698. array(
  699. 'db_error_skip' => true,
  700. )
  701. );
  702. // Load the modSettings data...
  703. $request = $smcFunc['db_query']('', '
  704. SELECT variable, value
  705. FROM {db_prefix}settings',
  706. array(
  707. 'db_error_skip' => true,
  708. )
  709. );
  710. $modSettings = array();
  711. while ($row = $smcFunc['db_fetch_assoc']($request))
  712. $modSettings[$row['variable']] = $row['value'];
  713. $smcFunc['db_free_result']($request);
  714. }
  715. else
  716. {
  717. return throw_error('Cannot find ' . SOURCEDIR . '/database/Db-' . $db_type . '.subs.php' . '. Please check you have uploaded all source files and have the correct paths set.');
  718. }
  719. // If they don't have the file, they're going to get a warning anyway so we won't need to clean request vars.
  720. if (file_exists(SOURCEDIR . '/QueryString.php'))
  721. {
  722. require_once(SOURCEDIR . '/QueryString.php');
  723. cleanRequest();
  724. }
  725. if (!isset($_GET['substep']))
  726. $_GET['substep'] = 0;
  727. }
  728. function initialize_inputs()
  729. {
  730. global $start_time, $upcontext, $db_type;
  731. $start_time = time();
  732. umask(0);
  733. // Fun. Low PHP version...
  734. if (!isset($_GET))
  735. {
  736. $GLOBALS['_GET']['step'] = 0;
  737. return;
  738. }
  739. ob_start();
  740. // Better to upgrade cleanly and fall apart than to screw everything up if things take too long.
  741. ignore_user_abort(true);
  742. // This is really quite simple; if ?delete is on the URL, delete the upgrader...
  743. if (isset($_GET['delete']))
  744. {
  745. deleteUpgrader();
  746. }
  747. // Are we calling the backup css file?
  748. if (isset($_GET['infile_css']))
  749. {
  750. header('Content-Type: text/css');
  751. template_css();
  752. exit;
  753. }
  754. // Something is causing this to happen, and it's annoying. Stop it.
  755. $temp = 'upgrade_php?step';
  756. while (strlen($temp) > 4)
  757. {
  758. if (isset($_GET[$temp]))
  759. unset($_GET[$temp]);
  760. $temp = substr($temp, 1);
  761. }
  762. // Force a step, defaulting to 0.
  763. $_GET['step'] = (int) @$_GET['step'];
  764. $_GET['substep'] = (int) @$_GET['substep'];
  765. }
  766. // Step 0 - Let's welcome them in and ask them to login!
  767. function action_welcomeLogin()
  768. {
  769. global $db_prefix, $language, $modSettings, $upgradeurl, $upcontext, $disable_security;
  770. global $smcFunc, $db_type, $databases, $txt;
  771. $upcontext['sub_template'] = 'welcome_message';
  772. // Check for some key files - one template, one language, and a new and an old source file.
  773. $check = @file_exists($modSettings['theme_dir'] . '/index.template.php')
  774. && @file_exists(SOURCEDIR . '/QueryString.php')
  775. && @file_exists(SOURCEDIR . '/database/Db-' . $db_type . '.subs.php')
  776. && @file_exists(dirname(__FILE__) . '/upgrade_2-1_' . $db_type . '.sql');
  777. // Need legacy scripts?
  778. if (!isset($modSettings['elkVersion']) || $modSettings['elkVersion'] < 2.1)
  779. $check &= @file_exists(dirname(__FILE__) . '/upgrade_2-0_' . $db_type . '.sql');
  780. if (!isset($modSettings['elkVersion']) || $modSettings['elkVersion'] < 2.0)
  781. $check &= @file_exists(dirname(__FILE__) . '/upgrade_1-1.sql');
  782. if (!isset($modSettings['elkVersion']) || $modSettings['elkVersion'] < 1.1)
  783. $check &= @file_exists(dirname(__FILE__) . '/upgrade_1-0.sql');
  784. if (!$check)
  785. // Don't tell them what files exactly because it's a spot check - just like teachers don't tell which problems they are spot checking, that's dumb.
  786. return throw_error('The upgrader was unable to find some crucial files.<br /><br />Please make sure you uploaded all of the files included in the package, including the themes, sources, and other directories.');
  787. // Do they meet the install requirements?
  788. if (!php_version_check())
  789. return throw_error('Warning! You do not appear to have a version of PHP installed on your webserver that meets ELKARTE\'s minimum installations requirements.<br /><br />Please ask your host to upgrade.');
  790. if (!db_version_check())
  791. return throw_error('Your ' . $databases[$db_type]['name'] . ' version does not meet the minimum requirements of ELKARTE.<br /><br />Please ask your host to upgrade.');
  792. // Do they have ALTER privileges?
  793. if (!empty($databases[$db_type]['alter_support']) && $smcFunc['db_query']('alter_boards', 'ALTER TABLE {db_prefix}boards ORDER BY id_board', array()) === false)
  794. return throw_error('The ' . $databases[$db_type]['name'] . ' user you have set in Settings.php does not have proper privileges.<br /><br />Please ask your host to give this user the ALTER, CREATE, and DROP privileges.');
  795. // Do a quick version spot check.
  796. $temp = substr(@implode('', @file(BOARDDIR . '/index.php')), 0, 4096);
  797. preg_match('~\*\s@version\s+(.+)[\s]{2}~i', $temp, $match);
  798. if (empty($match[1]) || $match[1] != CURRENT_VERSION)
  799. return throw_error('The upgrader found some old or outdated files.<br /><br />Please make certain you uploaded the new versions of all the files included in the package.');
  800. // What absolutely needs to be writable?
  801. $writable_files = array(
  802. BOARDDIR . '/Settings.php',
  803. BOARDDIR . '/Settings_bak.php',
  804. );
  805. require_once(SOURCEDIR . '/Security.php');
  806. $upcontext += createToken('login');
  807. // Check the cache directory.
  808. $CACHEDIR_temp = !defined('CACHEDIR') ? BOARDDIR . '/cache' : CACHEDIR;
  809. if (!file_exists($CACHEDIR_temp))
  810. @mkdir($CACHEDIR_temp);
  811. if (!file_exists($CACHEDIR_temp))
  812. return throw_error('The cache directory could not be found.<br /><br />Please make sure you have a directory called &quot;cache&quot; in your forum directory before continuing.');
  813. if (!file_exists($modSettings['theme_dir'] . '/languages/index.' . $upcontext['language'] . '.php') && !isset($modSettings['elkVersion']) && !isset($_GET['lang']))
  814. return throw_error('The upgrader was unable to find language files for the language specified in Settings.php.<br />ELKARTE will not work without the primary language files installed.<br /><br />Please either install them, or <a href="' . $upgradeurl . '?step=0;lang=english">use english instead</a>.');
  815. elseif (!isset($_GET['skiplang']))
  816. {
  817. $temp = substr(@implode('', @file($modSettings['theme_dir'] . '/languages/index.' . $upcontext['language'] . '.php')), 0, 4096);
  818. preg_match('~(?://|/\*)\s*Version:\s+(.+?);\s*index(?:[\s]{2}|\*/)~i', $temp, $match);
  819. if (empty($match[1]) || $match[1] != CURRENT_LANG_VERSION)
  820. return throw_error('The upgrader found some old or outdated language files, for the forum default language, ' . $upcontext['language'] . '.<br /><br />Please make certain you uploaded the new versions of all the files included in the package, even the theme and language files for the default theme.<br />&nbsp;&nbsp;&nbsp;[<a href="' . $upgradeurl . '?skiplang">SKIP</a>] [<a href="' . $upgradeurl . '?lang=english">Try English</a>]');
  821. }
  822. // This needs to exist!
  823. if (!file_exists($modSettings['theme_dir'] . '/languages/Install.' . $upcontext['language'] . '.php'))
  824. return throw_error('The upgrader could not find the &quot;Install&quot; language file for the forum default language, ' . $upcontext['language'] . '.<br /><br />Please make certain you uploaded all the files included in the package, even the theme and language files for the default theme.<br />&nbsp;&nbsp;&nbsp;[<a href="' . $upgradeurl . '?lang=english">Try English</a>]');
  825. else
  826. require_once($modSettings['theme_dir'] . '/languages/Install.' . $upcontext['language'] . '.php');
  827. if (!makeFilesWritable($writable_files))
  828. return false;
  829. // Check agreement.txt. (it may not exist, in which case BOARDDIR must be writable.)
  830. if (isset($modSettings['agreement']) && (!is_writable(BOARDDIR) || file_exists(BOARDDIR . '/agreement.txt')) && !is_writable(BOARDDIR . '/agreement.txt'))
  831. return throw_error('The upgrader was unable to obtain write access to agreement.txt.<br /><br />If you are using a linux or unix based server, please ensure that the file is chmod\'d to 777, or if it does not exist that the directory this upgrader is in is 777.<br />If your server is running Windows, please ensure that the internet guest account has the proper permissions on it or its folder.');
  832. // Upgrade the agreement.
  833. elseif (isset($modSettings['agreement']))
  834. {
  835. $fp = fopen(BOARDDIR . '/agreement.txt', 'w');
  836. fwrite($fp, $modSettings['agreement']);
  837. fclose($fp);
  838. }
  839. // We're going to check that their board dir setting is right incase they've been moving stuff around.
  840. if (strtr(BOARDDIR, array('/' => '', '\\' => '')) != strtr(dirname(__FILE__), array('/' => '', '\\' => '')))
  841. $upcontext['warning'] = '
  842. It looks as if your board directory settings <em>might</em> be incorrect. Your board directory is currently set to &quot;' . BOARDDIR . '&quot; but should probably be &quot;' . dirname(__FILE__) . '&quot;. Settings.php currently lists your paths as:<br />
  843. <ul>
  844. <li>Board Directory: ' . BOARDDIR . '</li>
  845. <li>Source Directory: ' . BOARDDIR . '</li>
  846. <li>Cache Directory: ' . $CACHEDIR_temp . '</li>
  847. </ul>
  848. If these seem incorrect please open Settings.php in a text editor before proceeding with this upgrade. If they are incorrect due to you moving your forum to a new location please download and execute the <a href="https://github.com/emanuele45/tools/downloads">Repair Settings</a> tool from the Elkarte website before continuing.';
  849. // Either we're logged in or we're going to present the login.
  850. if (checkLogin())
  851. return true;
  852. return false;
  853. }
  854. // Step 0.5: Does the login work?
  855. function checkLogin()
  856. {
  857. global $db_prefix, $language, $modSettings, $upgradeurl, $upcontext, $disable_security;
  858. global $smcFunc, $db_type, $databases, $support_js, $txt;
  859. // Are we trying to login?
  860. if (isset($_POST['contbutt']) && (!empty($_POST['user']) || $disable_security))
  861. {
  862. // If we've disabled security pick a suitable name!
  863. if (empty($_POST['user']))
  864. $_POST['user'] = 'Administrator';
  865. // Before 2.0 these column names were different!
  866. $oldDB = false;
  867. if (empty($db_type) || $db_type == 'mysql')
  868. {
  869. $request = $smcFunc['db_query']('', '
  870. SHOW COLUMNS
  871. FROM {db_prefix}members
  872. LIKE {string:member_name}',
  873. array(
  874. 'member_name' => 'memberName',
  875. 'db_error_skip' => true,
  876. )
  877. );
  878. if ($smcFunc['db_num_rows']($request) != 0)
  879. $oldDB = true;
  880. $smcFunc['db_free_result']($request);
  881. }
  882. // Get what we believe to be their details.
  883. if (!$disable_security)
  884. {
  885. if ($oldDB)
  886. $request = $smcFunc['db_query']('', '
  887. SELECT id_member, memberName AS member_name, passwd, id_group,
  888. additionalGroups AS additional_groups, lngfile
  889. FROM {db_prefix}members
  890. WHERE memberName = {string:member_name}',
  891. array(
  892. 'member_name' => $_POST['user'],
  893. 'db_error_skip' => true,
  894. )
  895. );
  896. else
  897. $request = $smcFunc['db_query']('', '
  898. SELECT id_member, member_name, passwd, id_group, additional_groups, lngfile
  899. FROM {db_prefix}members
  900. WHERE member_name = {string:member_name}',
  901. array(
  902. 'member_name' => $_POST['user'],
  903. 'db_error_skip' => true,
  904. )
  905. );
  906. if ($smcFunc['db_num_rows']($request) != 0)
  907. {
  908. list ($id_member, $name, $password, $id_group, $addGroups, $user_language) = $smcFunc['db_fetch_row']($request);
  909. $groups = explode(',', $addGroups);
  910. $groups[] = $id_group;
  911. foreach ($groups as $k => $v)
  912. $groups[$k] = (int) $v;
  913. // Figure out the password using our encryption - if what they typed is right.
  914. if (isset($_REQUEST['hash_passwrd']) && strlen($_REQUEST['hash_passwrd']) == 40)
  915. {
  916. // Challenge passed.
  917. if ($_REQUEST['hash_passwrd'] == sha1($password . $upcontext['rid']))
  918. $sha_passwd = $password;
  919. }
  920. else
  921. $sha_passwd = sha1(strtolower($name) . un_htmlspecialchars($_REQUEST['passwrd']));
  922. }
  923. else
  924. $upcontext['username_incorrect'] = true;
  925. $smcFunc['db_free_result']($request);
  926. }
  927. $upcontext['username'] = $_POST['user'];
  928. // Track whether javascript works!
  929. if (!empty($_POST['js_works']))
  930. {
  931. $upcontext['upgrade_status']['js'] = 1;
  932. $support_js = 1;
  933. }
  934. else
  935. $support_js = 0;
  936. // Note down the version we are coming from.
  937. if (!empty($modSettings['elkVersion']) && empty($upcontext['user']['version']))
  938. $upcontext['user']['version'] = $modSettings['elkVersion'];
  939. // Didn't get anywhere?
  940. if ((empty($sha_passwd) || $password != $sha_passwd) && empty($upcontext['username_incorrect']) && !$disable_security)
  941. {
  942. // MD5?
  943. $md5pass = md5_hmac($_REQUEST['passwrd'], strtolower($_POST['user']));
  944. if ($md5pass != $password)
  945. {
  946. $upcontext['password_failed'] = true;
  947. // Disable the hashing this time.
  948. $upcontext['disable_login_hashing'] = true;
  949. }
  950. }
  951. if ((empty($upcontext['password_failed']) && !empty($name)) || $disable_security)
  952. {
  953. // Set the password.
  954. if (!$disable_security)
  955. {
  956. // Do we actually have permission?
  957. if (!in_array(1, $groups))
  958. {
  959. $request = $smcFunc['db_query']('', '
  960. SELECT permission
  961. FROM {db_prefix}permissions
  962. WHERE id_group IN ({array_int:groups})
  963. AND permission = {string:admin_forum}',
  964. array(
  965. 'groups' => $groups,
  966. 'admin_forum' => 'admin_forum',
  967. 'db_error_skip' => true,
  968. )
  969. );
  970. if ($smcFunc['db_num_rows']($request) == 0)
  971. return throw_error('You need to be an admin to perform an upgrade!');
  972. $smcFunc['db_free_result']($request);
  973. }
  974. $upcontext['user']['id'] = $id_member;
  975. $upcontext['user']['name'] = $name;
  976. }
  977. else
  978. {
  979. $upcontext['user']['id'] = 1;
  980. $upcontext['user']['name'] = 'Administrator';
  981. }
  982. $upcontext['user']['pass'] = mt_rand(0,60000);
  983. // This basically is used to match the GET variables to Settings.php.
  984. $upcontext['upgrade_status']['pass'] = $upcontext['user']['pass'];
  985. // Set the language to that of the user?
  986. if (isset($user_language) && $user_language != $upcontext['language'] && file_exists($modSettings['theme_dir'] . '/languages/index.' . basename($user_language, '.lng') . '.php'))
  987. {
  988. $user_language = basename($user_language, '.lng');
  989. $temp = substr(@implode('', @file($modSettings['theme_dir'] . '/languages/index.' . $user_language . '.php')), 0, 4096);
  990. preg_match('~(?://|/\*)\s*Version:\s+(.+?);\s*index(?:[\s]{2}|\*/)~i', $temp, $match);
  991. if (empty($match[1]) || $match[1] != CURRENT_LANG_VERSION)
  992. $upcontext['upgrade_options_warning'] = 'The language files for your selected language, ' . $user_language . ', have not been updated to the latest version. Upgrade will continue with the forum default, ' . $upcontext['language'] . '.';
  993. elseif (!file_exists($modSettings['theme_dir'] . '/languages/Install.' . basename($user_language, '.lng') . '.php'))
  994. $upcontext['upgrade_options_warning'] = 'The language files for your selected language, ' . $user_language . ', have not been uploaded/updated as the &quot;Install&quot; language file is missing. Upgrade will continue with the forum default, ' . $upcontext['language'] . '.';
  995. else
  996. {
  997. // Set this as the new language.
  998. $upcontext['language'] = $user_language;
  999. $upcontext['upgrade_status']['lang'] = $upcontext['language'];
  1000. // Include the file.
  1001. require_once($modSettings['theme_dir'] . '/languages/Install.' . $user_language . '.php');
  1002. }
  1003. }
  1004. // If we're resuming set the step and substep to be correct.
  1005. if (isset($_POST['cont']))
  1006. {
  1007. $upcontext['current_step'] = $upcontext['user']['step'];
  1008. $_GET['substep'] = $upcontext['user']['substep'];
  1009. }
  1010. return true;
  1011. }
  1012. }
  1013. return false;
  1014. }
  1015. // Step 1: Do the maintenance and backup.
  1016. function action_upgradeOptions()
  1017. {
  1018. global $db_prefix, $command_line, $modSettings, $is_debug, $smcFunc;
  1019. global $boardurl, $maintenance, $mmessage, $upcontext, $db_type;
  1020. $upcontext['sub_template'] = 'upgrade_options';
  1021. $upcontext['page_title'] = 'Upgrade Options';
  1022. // If we've not submitted then we're done.
  1023. if (empty($_POST['upcont']))
  1024. return false;
  1025. // No one opts in so why collect incomplete stats
  1026. $smcFunc['db_query']('', '
  1027. DELETE FROM {db_prefix}settings
  1028. WHERE variable = {string:allow_sm_stats}',
  1029. array(
  1030. 'allow_sm_stats' => false,
  1031. 'db_error_skip' => true,
  1032. )
  1033. );
  1034. // Emptying the error log?
  1035. if (!empty($_POST['empty_error']))
  1036. $smcFunc['db_query']('truncate_table', '
  1037. TRUNCATE {db_prefix}log_errors',
  1038. array(
  1039. )
  1040. );
  1041. $changes = array();
  1042. // If we're overriding the language follow it through.
  1043. if (isset($_GET['lang']) && file_exists($modSettings['theme_dir'] . '/languages/index.' . $_GET['lang'] . '.php'))
  1044. $changes['language'] = '\'' . $_GET['lang'] . '\'';
  1045. if (!empty($_POST['maint']))
  1046. {
  1047. $changes['maintenance'] = '2';
  1048. // Remember what it was...
  1049. $upcontext['user']['main'] = $maintenance;
  1050. if (!empty($_POST['maintitle']))
  1051. {
  1052. $changes['mtitle'] = '\'' . addslashes($_POST['maintitle']) . '\'';
  1053. $changes['mmessage'] = '\'' . addslashes($_POST['mainmessage']) . '\'';
  1054. }
  1055. else
  1056. {
  1057. $changes['mtitle'] = '\'Upgrading the forum...\'';
  1058. $changes['mmessage'] = '\'Don\\\'t worry, we will be back shortly with an updated forum. It will only be a minute ;).\'';
  1059. }
  1060. }
  1061. if ($command_line)
  1062. echo ' * Updating Settings.php...';
  1063. // Backup the current one first.
  1064. copy(BOARDDIR . '/Settings.php', BOARDDIR . '/Settings_bak.php');
  1065. // Fix some old paths.
  1066. if (substr(BOARDDIR, 0, 1) == '.')
  1067. $changes['boarddir'] = '\'' . fixRelativePath(BOARDDIR) . '\'';
  1068. if (substr(SOURCEDIR, 0, 1) == '.')
  1069. $changes['sourcedir'] = '\'' . fixRelativePath(SOURCEDIR) . '\'';
  1070. if (!defined('CACHEDIR') || substr(CACHEDIR, 0, 1) == '.')
  1071. $changes['cachedir'] = '\'' . fixRelativePath(BOARDDIR) . '/cache\'';
  1072. // Not had the database type added before?
  1073. if (empty($db_type))
  1074. $changes['db_type'] = 'mysql';
  1075. // @todo Maybe change the cookie name if going to 1.1, too?
  1076. // Update Settings.php with the new settings.
  1077. changeSettings($changes);
  1078. if ($command_line)
  1079. echo ' Successful.' . "\n";
  1080. // Are we doing debug?
  1081. if (isset($_POST['debug']))
  1082. {
  1083. $upcontext['upgrade_status']['debug'] = true;
  1084. $is_debug = true;
  1085. }
  1086. // If we're not backing up then jump one.
  1087. if (empty($_POST['backup']))
  1088. $upcontext['current_step']++;
  1089. // If we've got here then let's proceed to the next step!
  1090. return true;
  1091. }
  1092. // Backup the database - why not...
  1093. function action_backupDatabase()
  1094. {
  1095. global $upcontext, $db_prefix, $command_line, $is_debug, $support_js, $file_steps, $smcFunc;
  1096. $upcontext['sub_template'] = isset($_GET['xml']) ? 'backup_xml' : 'backup_database';
  1097. $upcontext['page_title'] = 'Backup Database';
  1098. // Done it already - js wise?
  1099. if (!empty($_POST['backup_done']))
  1100. return true;
  1101. // Some useful stuff here.
  1102. db_extend();
  1103. // Get all the table names.
  1104. $filter = str_replace('_', '\_', preg_match('~^`(.+?)`\.(.+?)$~', $db_prefix, $match) != 0 ? $match[2] : $db_prefix) . '%';
  1105. $db = preg_match('~^`(.+?)`\.(.+?)$~', $db_prefix, $match) != 0 ? strtr($match[1], array('`' => '')) : false;
  1106. $tables = $smcFunc['db_list_tables']($db, $filter);
  1107. $table_names = array();
  1108. foreach ($tables as $table)
  1109. if (substr($table, 0, 7) !== 'backup_')
  1110. $table_names[] = $table;
  1111. $upcontext['table_count'] = count($table_names);
  1112. $upcontext['cur_table_num'] = $_GET['substep'];
  1113. $upcontext['cur_table_name'] = str_replace($db_prefix, '', isset($table_names[$_GET['substep']]) ? $table_names[$_GET['substep']] : $table_names[0]);
  1114. $upcontext['step_progress'] = (int) (($upcontext['cur_table_num'] / $upcontext['table_count']) * 100);
  1115. // For non-java auto submit...
  1116. $file_steps = $upcontext['table_count'];
  1117. // What ones have we already done?
  1118. foreach ($table_names as $id => $table)
  1119. if ($id < $_GET['substep'])
  1120. $upcontext['previous_tables'][] = $table;
  1121. if ($command_line)
  1122. echo 'Backing Up Tables.';
  1123. // If we don't support javascript we backup here.
  1124. if (!$support_js || isset($_GET['xml']))
  1125. {
  1126. // Backup each table!
  1127. for ($substep = $_GET['substep'], $n = count($table_names); $substep < $n; $substep++)
  1128. {
  1129. $upcontext['cur_table_name'] = str_replace($db_prefix, '', (isset($table_names[$substep + 1]) ? $table_names[$substep + 1] : $table_names[$substep]));
  1130. $upcontext['cur_table_num'] = $substep + 1;
  1131. $upcontext['step_progress'] = (int) (($upcontext['cur_table_num'] / $upcontext['table_count']) * 100);
  1132. // Do we need to pause?
  1133. nextSubstep($substep);
  1134. backupTable($table_names[$substep]);
  1135. // If this is XML to keep it nice for the user do one table at a time anyway!
  1136. if (isset($_GET['xml']))
  1137. return upgradeExit();
  1138. }
  1139. if ($is_debug && $command_line)
  1140. {
  1141. echo "\n" . ' Successful.\'' . "\n";
  1142. flush();
  1143. }
  1144. $upcontext['step_progress'] = 100;
  1145. $_GET['substep'] = 0;
  1146. // Make sure we move on!
  1147. return true;
  1148. }
  1149. // Either way next place to post will be database changes!
  1150. $_GET['substep'] = 0;
  1151. return false;
  1152. }
  1153. // Backup one table...
  1154. function backupTable($table)
  1155. {
  1156. global $is_debug, $command_line, $db_prefix, $smcFunc;
  1157. if ($is_debug && $command_line)
  1158. {
  1159. echo "\n" . ' +++ Backing up \"' . str_replace($db_prefix, '', $table) . '"...';
  1160. flush();
  1161. }
  1162. $smcFunc['db_backup_table']($table, 'backup_' . $table);
  1163. if ($is_debug && $command_line)
  1164. echo ' done.';
  1165. }
  1166. // Step 2: Everything.
  1167. function action_databaseChanges()
  1168. {
  1169. global $db_prefix, $modSettings, $command_line, $smcFunc;
  1170. global $language, $boardurl, $upcontext, $support_js, $db_type;
  1171. // Have we just completed this?
  1172. if (!empty($_POST['database_done']))
  1173. return true;
  1174. $upcontext['sub_template'] = isset($_GET['xml']) ? 'database_xml' : 'database_changes';
  1175. $upcontext['page_title'] = 'Database Changes';
  1176. // All possible files.
  1177. // Name, version, insert_on_complete.
  1178. $files = array(
  1179. array('upgrade_1-0.sql', '1.1', '1.1 RC0'),
  1180. array('upgrade_1-1.sql', '2.0', '2.0 a'),
  1181. array('upgrade_2-0_' . $db_type . '.sql', '2.1', '2.1 dev0'),
  1182. // array('upgrade_2-1_' . $db_type . '.sql', '3.0', '3.0 dev0'),
  1183. array('upgrade_dia_1-0_' . $db_type . '.sql', '1.1', CURRENT_VERSION),
  1184. );
  1185. // How many files are there in total?
  1186. if (isset($_GET['filecount']))
  1187. $upcontext['file_count'] = (int) $_GET['filecount'];
  1188. else
  1189. {
  1190. $upcontext['file_count'] = 0;
  1191. foreach ($files as $file)
  1192. {
  1193. if (!isset($modSettings['elkVersion']) && isset($modSettings['smfVersion']) && strpos($file[0], '_dia_') === false && $modSettings['smfVersion'] < $file[1])
  1194. $upcontext['file_count']++;
  1195. elseif (!isset($modSettings['elkVersion']) || (strpos($file[0], '_dia_') !== false && $modSettings['elkVersion'] < $file[1]))
  1196. $upcontext['file_count']++;
  1197. }
  1198. }
  1199. // Do each file!
  1200. $did_not_do = count($files) - $upcontext['file_count'];
  1201. $upcontext['step_progress'] = 0;
  1202. $upcontext['cur_file_num'] = 0;
  1203. foreach ($files as $file)
  1204. {
  1205. if ($did_not_do)
  1206. $did_not_do--;
  1207. else
  1208. {
  1209. $upcontext['cur_file_num']++;
  1210. $upcontext['cur_file_name'] = $file[0];
  1211. // Do we actually need to do this still?
  1212. if (!isset($modSettings['elkVersion']) || $modSettings['elkVersion'] < $file[1])
  1213. {
  1214. $nextFile = parse_sql(dirname(__FILE__) . '/' . $file[0]);
  1215. if ($nextFile)
  1216. {
  1217. // Only update the version of this if complete.
  1218. $smcFunc['db_insert']('replace',
  1219. $db_prefix . 'settings',
  1220. array('variable' => 'string', 'value' => 'string'),
  1221. array('elkVersion', $file[2]),
  1222. array('variable')
  1223. );
  1224. $modSettings['elkVersion'] = $file[2];
  1225. }
  1226. // If this is XML we only do this stuff once.
  1227. if (isset($_GET['xml']))
  1228. {
  1229. // Flag to move on to the next.
  1230. $upcontext['completed_step'] = true;
  1231. // Did we complete the whole file?
  1232. if ($nextFile)
  1233. $upcontext['current_debug_item_num'] = -1;
  1234. return upgradeExit();
  1235. }
  1236. elseif ($support_js)
  1237. break;
  1238. }
  1239. // Set the progress bar to be right as if we had - even if we hadn't...
  1240. $upcontext['step_progress'] = ($upcontext['cur_file_num'] / $upcontext['file_count']) * 100;
  1241. }
  1242. }
  1243. $_GET['substep'] = 0;
  1244. // So the template knows we're done.
  1245. if (!$support_js)
  1246. {
  1247. $upcontext['changes_complete'] = true;
  1248. // If this is the command line we can't do any more.
  1249. if ($command_line)
  1250. return action_deleteUpgrade();
  1251. return true;
  1252. }
  1253. return false;
  1254. }
  1255. // Clean up any mods installed...
  1256. function action_cleanupMods()
  1257. {
  1258. global $db_prefix, $modSettings, $upcontext, $settings, $smcFunc, $command_line;
  1259. // Sorry. Not supported for command line users.
  1260. if ($command_line)
  1261. return true;
  1262. // Skipping first?
  1263. if (!empty($_POST['skip']))
  1264. {
  1265. unset($_POST['skip']);
  1266. return true;
  1267. }
  1268. // If we get here withOUT SSI we need to redirect to ensure we get it!
  1269. if (!isset($_GET['ssi']) || !function_exists('mktree'))
  1270. redirectLocation('&ssi=1');
  1271. $upcontext['sub_template'] = 'clean_mods';
  1272. $upcontext['page_title'] = 'Cleanup Modifications';
  1273. // This can be skipped.
  1274. $upcontext['skip'] = true;
  1275. // If we're on the second redirect continue...
  1276. if (isset($_POST['cleandone2']))
  1277. return true;
  1278. // Do we already know about some writable files?
  1279. if (isset($_POST['writable_files']))
  1280. {
  1281. $writable_files = unserialize(base64_decode($_POST['writable_files']));
  1282. if (!makeFilesWritable($writable_files))
  1283. {
  1284. // What have we left?
  1285. $upcontext['writable_files'] = $writable_files;
  1286. return false;
  1287. }
  1288. }
  1289. // Load all theme paths....
  1290. $request = $smcFunc['db_query']('', '
  1291. SELECT id_theme, variable, value
  1292. FROM {db_prefix}themes
  1293. WHERE id_member = {int:id_member}
  1294. AND variable IN ({string:theme_dir}, {string:images_url})',
  1295. array(
  1296. 'id_member' => 0,
  1297. 'theme_dir' => 'theme_dir',
  1298. 'images_url' => 'images_url',
  1299. 'db_error_skip' => true,
  1300. )
  1301. );
  1302. $theme_paths = array();
  1303. while ($row = $smcFunc['db_fetch_assoc']($request))
  1304. {
  1305. if ($row['id_theme'] == 1)
  1306. $settings['default_' . $row['variable']] = $row['value'];
  1307. elseif ($row['variable'] == 'theme_dir')
  1308. $theme_paths[$row['id_theme']][$row['variable']] = $row['value'];
  1309. }
  1310. $smcFunc['db_free_result']($request);
  1311. // Are there are mods installed that may need uninstalling?
  1312. $request = $smcFunc['db_query']('', '
  1313. SELECT id_install, filename, name, themes_installed, version
  1314. FROM {db_prefix}log_packages
  1315. WHERE install_state = {int:installed}
  1316. ORDER BY time_installed DESC',
  1317. array(
  1318. 'installed' => 1,
  1319. 'db_error_skip' => true,
  1320. )
  1321. );
  1322. $upcontext['packages'] = array();
  1323. while ($row = $smcFunc['db_fetch_assoc']($request))
  1324. {
  1325. // Work out the status.
  1326. if (!file_exists(BOARDDIR . '/packages/' . $row['filename']))
  1327. {
  1328. $status = 'Missing';
  1329. $status_color = 'red';
  1330. $result = 'Removed';
  1331. }
  1332. else
  1333. {
  1334. $status = 'Installed';
  1335. $status_color = 'green';
  1336. $result = 'No Action Needed';
  1337. }
  1338. $upcontext['packages'][$row['id_install']] = array(
  1339. 'id' => $row['id_install'],
  1340. 'themes' => explode(',', $row['themes_installed']),
  1341. 'name' => $row['name'],
  1342. 'filename' => $row['filename'],
  1343. 'missing_file' => file_exists(BOARDDIR . '/packages/' . $row['filename']) ? 0 : 1,
  1344. 'files' => array(),
  1345. 'file_count' => 0,
  1346. 'status' => $status,
  1347. 'result' => $result,
  1348. 'color' => $status_color,
  1349. 'version' => $row['version'],
  1350. 'needs_removing' => false,
  1351. );
  1352. }
  1353. $smcFunc['db_free_result']($request);
  1354. // Don't carry on if there are none.
  1355. if (empty($upcontext['packages']))
  1356. return true;
  1357. // Setup some basics.
  1358. if (!empty($upcontext['user']['version']))
  1359. $_SESSION['version_emulate'] = $upcontext['user']['version'];
  1360. // Before we get started, don't report notice errors.
  1361. $oldErrorReporting = error_reporting(E_ALL ^ E_NOTICE);
  1362. if (!mktree(BOARDDIR . '/packages/temp', 0755))
  1363. {
  1364. deltree(BOARDDIR . '/packages/temp', false);
  1365. if (!mktree(BOARDDIR . '/packages/temp', 0777))
  1366. {
  1367. deltree(BOARDDIR . '/packages/temp', false);
  1368. // @todo Error here - plus chmod!
  1369. }
  1370. }
  1371. // Anything which reinstalled should not have its entry removed.
  1372. $reinstall_worked = array();
  1373. // We're gonna be doing some removin'
  1374. $test = isset($_POST['cleandone']) ? false : true;
  1375. foreach ($upcontext['packages'] as $id => $package)
  1376. {
  1377. // Can't do anything about this....
  1378. if ($package['missing_file'])
  1379. continue;
  1380. // Not testing *and* this wasn't checked?
  1381. if (!$test && (!isset($_POST['remove']) || !isset($_POST['remove'][$id])))
  1382. continue;
  1383. // What are the themes this was installed into?
  1384. $cur_theme_paths = array();
  1385. foreach ($theme_paths as $tid => $data)
  1386. if ($tid != 1 && in_array($tid, $package['themes']))
  1387. $cur_theme_paths[$tid] = $data;
  1388. // Get the modifications data if applicable.
  1389. $filename = $package['filename'];
  1390. $packageInfo = getPackageInfo($filename);
  1391. if (!is_array($packageInfo))
  1392. continue;
  1393. $info = parsePackageInfo($packageInfo['xml'], $test, 'uninstall');
  1394. // Also get the reinstall details...
  1395. if (isset($_POST['remove']))
  1396. $infoInstall = parsePackageInfo($packageInfo['xml'], true);
  1397. if (is_file(BOARDDIR . '/packages/' . $filename))
  1398. read_tgz_file(BOARDDIR . '/packages/' . $filename, BOARDDIR . '/packages/temp');
  1399. else
  1400. copytree(BOARDDIR . '/packages/' . $filename, BOARDDIR . '/packages/temp');
  1401. // Work out how we uninstall...
  1402. $files = array();
  1403. foreach ($info as $change)
  1404. {
  1405. // Work out two things:
  1406. // 1) Whether it's installed at the moment - and if so whether its fully installed, and:
  1407. // 2) Whether it could be installed on the new version.
  1408. if ($change['type'] == 'modification')
  1409. {
  1410. $contents = @file_get_contents(BOARDDIR . '/packages/temp/' . $upcontext['base_path'] . $change['filename']);
  1411. if ($change['boardmod'])
  1412. $results = parseBoardMod($contents, $test, $change['reverse'], $cur_theme_paths);
  1413. else
  1414. $results = parseModification($contents, $test, $change['reverse'], $cur_theme_paths);
  1415. foreach ($results as $action)
  1416. {
  1417. // Something we can remove? Probably means it existed!
  1418. if (($action['type'] == 'replace' || $action['type'] == 'append' || (!empty($action['filename']) && $action['type'] == 'failure')) && !in_array($action['filename'], $files))
  1419. $files[] = $action['filename'];
  1420. if ($action['type'] == 'failure')
  1421. {
  1422. $upcontext['packages'][$id]['needs_removing'] = true;
  1423. $upcontext['packages'][$id]['status'] = 'Reinstall Required';
  1424. $upcontext['packages'][$id]['color'] = '#FD6435';
  1425. }
  1426. }
  1427. }
  1428. }
  1429. // Store this info for the template as appropriate.
  1430. $upcontext['packages'][$id]['files'] = $files;
  1431. $upcontext['packages'][$id]['file_count'] = count($files);
  1432. // If we've done something save the changes!
  1433. if (!$test)
  1434. package_flush_cache();
  1435. // Are we attempting to reinstall this thing?
  1436. if (isset($_POST['remove']) && !$test && isset($infoInstall))
  1437. {
  1438. // Need to extract again I'm afraid.
  1439. if (is_file(BOARDDIR . '/packages/' . $filename))
  1440. read_tgz_file(BOARDDIR . '/packages/' . $filename, BOARDDIR . '/packages/temp');
  1441. else
  1442. copytree(BOARDDIR . '/packages/' . $filename, BOARDDIR . '/packages/temp');
  1443. $errors = false;
  1444. $upcontext['packages'][$id]['result'] = 'Removed';
  1445. foreach ($infoInstall as $change)
  1446. {
  1447. if ($change['type'] == 'modification')
  1448. {
  1449. $contents = @file_get_contents(BOARDDIR . '/packages/temp/' . $upcontext['base_path'] . $change['filename']);
  1450. if ($change['boardmod'])
  1451. $results = parseBoardMod($contents, true, $change['reverse'], $cur_theme_paths);
  1452. else
  1453. $results = parseModification($contents, true, $change['reverse'], $cur_theme_paths);
  1454. // Are there any errors?
  1455. foreach ($results as $action)
  1456. if ($action['type'] == 'failure')
  1457. $errors = true;
  1458. }
  1459. }
  1460. if (!$errors)
  1461. {
  1462. $reinstall_worked[] = $id;
  1463. $upcontext['packages'][$id]['result'] = 'Reinstalled';
  1464. $upcontext['packages'][$id]['color'] = 'green';
  1465. foreach ($infoInstall as $change)
  1466. {
  1467. if ($change['type'] == 'modification')
  1468. {
  1469. $contents = @file_get_contents(BOARDDIR . '/packages/temp/' . $upcontext['base_path'] . $change['filename']);
  1470. if ($change['boardmod'])
  1471. $results = parseBoardMod($contents, false, $change['reverse'], $cur_theme_paths);
  1472. else
  1473. $results = parseModification($contents, false, $change['reverse'], $cur_theme_paths);
  1474. }
  1475. }
  1476. // Save the changes.
  1477. package_flush_cache();
  1478. }
  1479. }
  1480. }
  1481. // Put errors back on a sec.
  1482. error_reporting($oldErrorReporting);
  1483. // Check everything is writable.
  1484. if ($test && !empty($upcontext['packages']))
  1485. {
  1486. $writable_files = array();
  1487. foreach ($upcontext['packages'] as $package)
  1488. {
  1489. if (!empty($package['files']))
  1490. foreach ($package['files'] as $file)
  1491. $writable_files[] = $file;
  1492. }
  1493. if (!empty($writable_files))
  1494. {
  1495. $writable_files = array_unique($writable_files);
  1496. $upcontext['writable_files'] = $writable_files;
  1497. if (!makeFilesWritable($writable_files))
  1498. return false;
  1499. }
  1500. }
  1501. if (file_exists(BOARDDIR . '/packages/temp'))
  1502. deltree(BOARDDIR . '/packages/temp');
  1503. // Removing/Reinstalling any packages?
  1504. if (isset($_POST['remove']))
  1505. {
  1506. $deletes = array();
  1507. foreach ($_POST['remove'] as $id => $dummy)
  1508. {
  1509. if (!in_array((int) $id, $reinstall_worked))
  1510. $deletes[] = (int) $id;
  1511. }
  1512. if (!empty($deletes))
  1513. upgrade_query( '
  1514. UPDATE ' . $db_prefix . 'log_packages
  1515. SET install_state = 0
  1516. WHERE id_install IN (' . implode(',', $deletes) . ')');
  1517. // Ensure we don't lose our changes!
  1518. package_put_contents(BOARDDIR . '/packages/installed.list', time());
  1519. $upcontext['sub_template'] = 'cleanup_done';
  1520. return false;
  1521. }
  1522. else
  1523. {
  1524. $allgood = true;
  1525. // Is there actually anything that needs our attention?
  1526. foreach ($upcontext['packages'] as $package)
  1527. if ($package['color'] != 'green')
  1528. $allgood = false;
  1529. if ($allgood)
  1530. return true;
  1531. }
  1532. $_GET['substep'] = 0;
  1533. return isset($_POST['cleandone']) ? true : false;
  1534. }
  1535. // Delete the damn thing!
  1536. function action_deleteUpgrade()
  1537. {
  1538. global $command_line, $language, $upcontext, $forum_version, $user_info, $maintenance, $smcFunc, $db_type;
  1539. // Now it's nice to have some of the basic source files.
  1540. if (!isset($_GET['ssi']) && !$command_line)
  1541. redirectLocation('&ssi=1');
  1542. $upcontext['sub_template'] = 'upgrade_complete';
  1543. $upcontext['page_title'] = 'Upgrade Complete';
  1544. $endl = $command_line ? "\n" : '<br />' . "\n";
  1545. $changes = array(
  1546. 'language' => '\'' . (substr($language, -4) == '.lng' ? substr($language, 0, -4) : $language) . '\'',
  1547. 'db_error_send' => '1',
  1548. 'upgradeData' => '#remove#',
  1549. );
  1550. // Are we in maintenance mode?
  1551. if (isset($upcontext['user']['main']))
  1552. {
  1553. if ($command_line)
  1554. echo ' * ';
  1555. $upcontext['removed_maintenance'] = true;
  1556. $changes['maintenance'] = $upcontext['user']['main'];
  1557. }
  1558. // Otherwise if somehow we are in 2 let's go to 1.
  1559. elseif (!empty($maintenance) && $maintenance == 2)
  1560. $changes['maintenance'] = 1;
  1561. // Wipe this out...
  1562. $upcontext['user'] = array();
  1563. // Make a backup of Settings.php first as otherwise earlier changes are lost.
  1564. copy(BOARDDIR . '/Settings.php', BOARDDIR . '/Settings_bak.php');
  1565. changeSettings($changes);
  1566. // Clean any old cache files away.
  1567. clean_cache();
  1568. // Can we delete the file?
  1569. $upcontext['can_delete_script'] = is_writable(dirname(__FILE__)) || is_writable(__FILE__);
  1570. // Now is the perfect time to fetch the SM files.
  1571. if ($command_line)
  1572. cli_scheduled_fetchFiles();
  1573. else
  1574. {
  1575. require_once(SOURCEDIR . '/ScheduledTasks.php');
  1576. $forum_version = CURRENT_VERSION; // The variable is usually defined in index.php so lets just use the constant to do it for us.
  1577. scheduled_fetchFiles(); // Now go get those files!
  1578. }
  1579. // Log what we've done.
  1580. if (empty($user_info['id']))
  1581. $user_info['id'] = !empty($upcontext['user']['id']) ? $upcontext['user']['id'] : 0;
  1582. // Log the action manually, so CLI still works.
  1583. $smcFunc['db_insert']('',
  1584. '{db_prefix}log_actions',
  1585. array(
  1586. 'log_time' => 'int', 'id_log' => 'int', 'id_member' => 'int', 'ip' => 'string-16', 'action' => 'string',
  1587. 'id_board' => 'int', 'id_topic' => 'int', 'id_msg' => 'int', 'extra' => 'string-65534',
  1588. ),
  1589. array(
  1590. time(), 3, $user_info['id'], $command_line ? '127.0.0.1' : $user_info['ip'], 'upgrade',
  1591. 0, 0, 0, serialize(array('version' => $forum_version, 'member' => $user_info['id'])),
  1592. ),
  1593. array('id_action')
  1594. );
  1595. $user_info['id'] = 0;
  1596. // Save the current database version.
  1597. $server_version = $smcFunc['db_server_info']();
  1598. if ($db_type == 'mysql' && in_array(substr($server_version, 0, 6), array('5.0.50', '5.0.51')))
  1599. updateSettings(array('db_mysql_group_by_fix' => '1'));
  1600. if ($command_line)
  1601. {
  1602. echo $endl;
  1603. echo 'Upgrade Complete!', $endl;
  1604. echo 'Please delete this file as soon as possible for security reasons.', $endl;
  1605. exit;
  1606. }
  1607. // Make sure it says we're done.
  1608. $upcontext['overall_percent'] = 100;
  1609. if (isset($upcontext['step_progress']))
  1610. unset($upcontext['step_progress']);
  1611. $_GET['substep'] = 0;
  1612. return false;
  1613. }
  1614. // Just like the built in one, but setup for CLI to not use themes.
  1615. function cli_scheduled_fetchFiles()
  1616. {
  1617. global $txt, $language, $settings, $forum_version, $modSettings, $smcFunc;
  1618. if (empty($modSettings['time_format']))
  1619. $modSettings['time_format'] = '%B %d, %Y, %I:%M:%S %p';
  1620. // What files do we want to get
  1621. $request = $smcFunc['db_query']('', '
  1622. SELECT id_file, filename, path, parameters
  1623. FROM {db_prefix}admin_info_files',
  1624. array(
  1625. )
  1626. );
  1627. $js_files = array();
  1628. while ($row = $smcFunc['db_fetch_assoc']($request))
  1629. {
  1630. $js_files[$row['id_file']] = array(
  1631. 'filename' => $row['filename'],
  1632. 'path' => $row['path'],
  1633. 'parameters' => sprintf($row['parameters'], $language, urlencode($modSettings['time_format']), urlencode($forum_version)),
  1634. );
  1635. }
  1636. $smcFunc['db_free_result']($request);
  1637. // We're gonna need fetch_web_data() to pull this off.
  1638. require_once(SUBSDIR . '/Package.subs.php');
  1639. foreach ($js_files as $ID_FILE => $file)
  1640. {
  1641. // Create the url
  1642. $server = empty($file['path']) || substr($file['path'], 0, 7) != 'http://' ? 'http://www.elkarte.net' : '';
  1643. $url = $server . (!empty($file['path']) ? $file['path'] : $file['path']) . $file['filename'] . (!empty($file['parameters']) ? '?' . $file['parameters'] : '');
  1644. // Get the file
  1645. $file_data = fetch_web_data($url);
  1646. // If we got an error - give up - the site might be down.
  1647. if ($file_data === false)
  1648. return throw_error(sprintf('Could not retrieve the file %1$s.', $url));
  1649. // Save the file to the database.
  1650. $smcFunc['db_query']('substring', '
  1651. UPDATE {db_prefix}admin_info_files
  1652. SET data = SUBSTRING({string:file_data}, 1, 65534)
  1653. WHERE id_file = {int:id_file}',
  1654. array(
  1655. 'id_file' => $ID_FILE,
  1656. 'file_data' => $file_data,
  1657. )
  1658. );
  1659. }
  1660. return true;
  1661. }
  1662. function convertSettingsToTheme()
  1663. {
  1664. global $db_prefix, $modSettings, $smcFunc;
  1665. $values = array(
  1666. 'show_latest_member' => @$GLOBALS['showlatestmember'],
  1667. 'show_bbc' => isset($GLOBALS['showyabbcbutt']) ? $GLOBALS['showyabbcbutt'] : @$GLOBALS['showbbcbutt'],
  1668. 'show_modify' => @$GLOBALS['showmodify'],
  1669. 'show_user_images' => @$GLOBALS['showuserpic'],
  1670. 'show_blurb' => @$GLOBALS['showusertext'],
  1671. 'show_gender' => @$GLOBALS['showgenderimage'],
  1672. 'show_newsfader' => @$GLOBALS['shownewsfader'],
  1673. 'display_recent_bar' => @$GLOBALS['Show_RecentBar'],
  1674. 'show_member_bar' => @$GLOBALS['Show_MemberBar'],
  1675. 'linktree_link' => @$GLOBALS['curposlinks'],
  1676. 'show_profile_buttons' => @$GLOBALS['profilebutton'],
  1677. 'show_mark_read' => @$GLOBALS['showmarkread'],
  1678. 'show_board_desc' => @$GLOBALS['ShowBDescrip'],
  1679. 'newsfader_time' => @$GLOBALS['fadertime'],
  1680. 'use_image_buttons' => empty($GLOBALS['MenuType']) ? 1 : 0,
  1681. 'enable_news' => @$GLOBALS['enable_news'],
  1682. 'return_to_post' => @$modSettings['returnToPost'],
  1683. );
  1684. $themeData = array();
  1685. foreach ($values as $variable => $value)
  1686. {
  1687. if (!isset($value) || $value === null)
  1688. $value = 0;
  1689. $themeData[] = array(0, 1, $variable, $value);
  1690. }
  1691. if (!empty($themeData))
  1692. {
  1693. $smcFunc['db_insert']('ignore',
  1694. $db_prefix . 'themes',
  1695. array('id_member' => 'int', 'id_theme' => 'int', 'variable' => 'string', 'value' => 'string'),
  1696. $themeData,
  1697. array('id_member', 'id_theme', 'variable')
  1698. );
  1699. }
  1700. }
  1701. // This function only works with MySQL but that's fine as it is only used for v1.0.
  1702. function convertSettingstoOptions()
  1703. {
  1704. global $db_prefix, $modSettings, $smcFunc;
  1705. // Format: new_setting -> old_setting_name.
  1706. $values = array(
  1707. 'calendar_start_day' => 'cal_startmonday',
  1708. 'view_newest_first' => 'viewNewestFirst',
  1709. 'view_newest_pm_first' => 'viewNewestFirst',
  1710. );
  1711. foreach ($values as $variable => $value)
  1712. {
  1713. if (empty($modSettings[$value[0]]))
  1714. continue;
  1715. $smcFunc['db_query']('', '
  1716. INSERT IGNORE INTO {db_prefix}themes
  1717. (id_member, id_theme, variable, value)
  1718. SELECT id_member, 1, {string:variable}, {string:value}
  1719. FROM {db_prefix}members',
  1720. array(
  1721. 'variable' => $variable,
  1722. 'value' => $modSettings[$value[0]],
  1723. 'db_error_skip' => true,
  1724. )
  1725. );
  1726. $smcFunc['db_query']('', '
  1727. INSERT IGNORE INTO {db_prefix}themes
  1728. (id_member, id_theme, variable, value)
  1729. VALUES (-1, 1, {string:variable}, {string:value})',
  1730. array(
  1731. 'variable' => $variable,
  1732. 'value' => $modSettings[$value[0]],
  1733. 'db_error_skip' => true,
  1734. )
  1735. );
  1736. }
  1737. }
  1738. function changeSettings($config_vars)
  1739. {
  1740. $settingsArray = file(BOARDDIR . '/Settings_bak.php');
  1741. if (count($settingsArray) == 1)
  1742. $settingsArray = preg_split('~[\r\n]~', $settingsArray[0]);
  1743. for ($i = 0, $n = count($settingsArray); $i < $n; $i++)
  1744. {
  1745. // Don't trim or bother with it if it's not a variable.
  1746. if (substr($settingsArray[$i], 0, 1) == '$')
  1747. {
  1748. $settingsArray[$i] = trim($settingsArray[$i]) . "\n";
  1749. foreach ($config_vars as $var => $val)
  1750. {
  1751. if (isset($settingsArray[$i]) && strncasecmp($settingsArray[$i], '$' . $var, 1 + strlen($var)) == 0)
  1752. {
  1753. if ($val == '#remove#')
  1754. unset($settingsArray[$i]);
  1755. else
  1756. {
  1757. $comment = strstr(substr($settingsArray[$i], strpos($settingsArray[$i], ';')), '#');
  1758. $settingsArray[$i] = '$' . $var . ' = ' . $val . ';' . ($comment != '' ? "\t\t" . $comment : "\n");
  1759. }
  1760. unset($config_vars[$var]);
  1761. }
  1762. }
  1763. }
  1764. }
  1765. if (!empty($config_vars))
  1766. {
  1767. $settingsArray[$i++] = '';
  1768. foreach ($config_vars as $var => $val)
  1769. {
  1770. if ($val != '#remove#')
  1771. $settingsArray[$i++] = '$' . $var . ' = ' . $val . ';' . "\n";
  1772. }
  1773. }
  1774. // Blank out the file - done to fix a oddity with some servers.
  1775. $fp = fopen(BOARDDIR . '/Settings.php', 'w');
  1776. fclose($fp);
  1777. $fp = fopen(BOARDDIR . '/Settings.php', 'r+');
  1778. $lines = count($settingsArray);
  1779. for ($i = 0; $i < $lines; $i++)
  1780. {
  1781. if (isset($settingsArray[$i]))
  1782. fwrite($fp, strtr($settingsArray[$i], "\r", ''));
  1783. }
  1784. fclose($fp);
  1785. }
  1786. function updateLastError()
  1787. {
  1788. // clear out the db_last_error file
  1789. file_put_contents(dirname(__FILE__) . '/db_last_error.php', '<' . '?' . "php\n" . '$db_last_error = 0;');
  1790. }
  1791. function php_version_check()
  1792. {
  1793. $minver = explode('.', $GLOBALS['required_php_version']);
  1794. $curver = explode('.', PHP_VERSION);
  1795. return !(($curver[0] <= $minver[0]) && ($curver[1] <= $minver[1]) && ($curver[1] <= $minver[1]) && ($curver[2][0] < $minver[2][0]));
  1796. }
  1797. function db_version_check()
  1798. {
  1799. global $db_type, $databases;
  1800. $curver = eval($databases[$db_type]['version_check']);
  1801. $curver = preg_replace('~\-.+?$~', '', $curver);
  1802. return version_compare($databases[$db_type]['version'], $curver, '<=');
  1803. }
  1804. function getMemberGroups()
  1805. {
  1806. global $db_prefix, $smcFunc;
  1807. static $member_groups = array();
  1808. if (!empty($member_groups))
  1809. return $member_groups;
  1810. $request = $smcFunc['db_query']('', '
  1811. SELECT group_name, id_group
  1812. FROM {db_prefix}membergroups
  1813. WHERE id_group = {int:admin_group} OR id_group > {int:old_group}',
  1814. array(
  1815. 'admin_group' => 1,
  1816. 'old_group' => 7,
  1817. 'db_error_skip' => true,
  1818. )
  1819. );
  1820. if ($request === false)
  1821. {
  1822. $request = $smcFunc['db_query']('', '
  1823. SELECT membergroup, id_group
  1824. FROM {db_prefix}membergroups
  1825. WHERE id_group = {int:admin_group} OR id_group > {int:old_group}',
  1826. array(
  1827. 'admin_group' => 1,
  1828. 'old_group' => 7,
  1829. 'db_error_skip' => true,
  1830. )
  1831. );
  1832. }
  1833. while ($row = $smcFunc['db_fetch_row']($request))
  1834. $member_groups[trim($row[0])] = $row[1];
  1835. $smcFunc['db_free_result']($request);
  1836. return $member_groups;
  1837. }
  1838. function fixRelativePath($path)
  1839. {
  1840. global $install_path;
  1841. // Fix the . at the start, clear any duplicate slashes, and fix any trailing slash...
  1842. return addslashes(preg_replace(array('~^\.([/\\\]|$)~', '~[/]+~', '~[\\\]+~', '~[/\\\]$~'), array($install_path . '$1', '/', '\\', ''), $path));
  1843. }
  1844. function parse_sql($filename)
  1845. {
  1846. global $db_prefix, $db_collation, $boardurl, $command_line, $file_steps, $step_progress, $custom_warning;
  1847. global $upcontext, $support_js, $is_debug, $smcFunc, $db_connection, $databases, $db_type, $db_character_set;
  1848. /*
  1849. Failure allowed on:
  1850. - INSERT INTO but not INSERT IGNORE INTO.
  1851. - UPDATE IGNORE but not UPDATE.
  1852. - ALTER TABLE and ALTER IGNORE TABLE.
  1853. - DROP TABLE.
  1854. Yes, I realize that this is a bit confusing... maybe it should be done differently?
  1855. If a comment...
  1856. - begins with --- it is to be output, with a break only in debug mode. (and say successful\n\n if there was one before.)
  1857. - begins with ---# it is a debugging statement, no break - only shown at all in debug.
  1858. - is only ---#, it is "done." and then a break - only shown in debug.
  1859. - begins with ---{ it is a code block terminating at ---}.
  1860. Every block of between "--- ..."s is a step. Every "---#" section represents a substep.
  1861. Replaces the following variables:
  1862. - {BOARDDIR}
  1863. - {$boardurl}
  1864. - {$db_prefix}
  1865. - {$db_collation}
  1866. */
  1867. // May want to use extended functionality.
  1868. db_extend();
  1869. db_extend('packages');
  1870. // Our custom error handler - does nothing but does stop public errors from XML!
  1871. if (!function_exists('sql_error_handler'))
  1872. {
  1873. function sql_error_handler($errno, $errstr, $errfile, $errline)
  1874. {
  1875. global $support_js;
  1876. if ($support_js)
  1877. return true;
  1878. else
  1879. echo 'Error: ' . $errstr . ' File: ' . $errfile . ' Line: ' . $errline;
  1880. }
  1881. }
  1882. // Make our own error handler.
  1883. set_error_handler('sql_error_handler');
  1884. // If we're on MySQL supporting collations then let's find out what the members table uses and put it in a global var - to allow upgrade script to match collations!
  1885. if (!empty($databases[$db_type]['utf8_support']) && version_compare($databases[$db_type]['utf8_version'], eval($databases[$db_type]['utf8_version_check']), '>'))
  1886. {
  1887. $request = $smcFunc['db_query']('', '
  1888. SHOW TABLE STATUS
  1889. LIKE {string:table_name}',
  1890. array(
  1891. 'table_name' => "{$db_prefix}members",
  1892. 'db_error_skip' => true,
  1893. )
  1894. );
  1895. if ($smcFunc['db_num_rows']($request) === 0)
  1896. die('Unable to find members table!');
  1897. $table_status = $smcFunc['db_fetch_assoc']($request);
  1898. $smcFunc['db_free_result']($request);
  1899. if (!empty($table_status['Collation']))
  1900. {
  1901. $request = $smcFunc['db_query']('', '
  1902. SHOW COLLATION
  1903. LIKE {string:collation}',
  1904. array(
  1905. 'collation' => $table_status['Collation'],
  1906. 'db_error_skip' => true,
  1907. )
  1908. );
  1909. // Got something?
  1910. if ($smcFunc['db_num_rows']($request) !== 0)
  1911. $collation_info = $smcFunc['db_fetch_assoc']($request);
  1912. $smcFunc['db_free_result']($request);
  1913. // Excellent!
  1914. if (!empty($collation_info['Collation']) && !empty($collation_info['Charset']))
  1915. $db_collation = ' CHARACTER SET ' . $collation_info['Charset'] . ' COLLATE ' . $collation_info['Collation'];
  1916. }
  1917. }
  1918. if (empty($db_collation))
  1919. $db_collation = '';
  1920. $endl = $command_line ? "\n" : '<br />' . "\n";
  1921. $lines = file($filename);
  1922. $current_type = 'sql';
  1923. $current_data = '';
  1924. $substep = 0;
  1925. $last_step = '';
  1926. // Make sure all newly created tables will have the proper characters set.
  1927. if (isset($db_character_set) && $db_character_set === 'utf8')
  1928. $lines = str_replace(') ENGINE=MyISAM;', ') ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;', $lines);
  1929. // Count the total number of steps within this file - for progress.
  1930. $file_steps = substr_count(implode('', $lines), '---#');
  1931. $upcontext['total_items'] = substr_count(implode('', $lines), '--- ');
  1932. $upcontext['debug_items'] = $file_steps;
  1933. $upcontext['current_item_num'] = 0;
  1934. $upcontext['current_item_name'] = '';
  1935. $upcontext['current_debug_item_num'] = 0;
  1936. $upcontext['current_debug_item_name'] = '';
  1937. // This array keeps a record of what we've done in case java is dead...
  1938. $upcontext['actioned_items'] = array();
  1939. $done_something = false;
  1940. foreach ($lines as $line_number => $line)
  1941. {
  1942. $do_current = $substep >= $_GET['substep'];
  1943. // Get rid of any comments in the beginning of the line...
  1944. if (substr(trim($line), 0, 2) === '/*')
  1945. $line = preg_replace('~/\*.+?\*/~', '', $line);
  1946. // Always flush. Flush, flush, flush. Flush, flush, flush, flush! FLUSH!
  1947. if ($is_debug && !$support_js && $command_line)
  1948. flush();
  1949. if (trim($line) === '')
  1950. continue;
  1951. if (trim(substr($line, 0, 3)) === '---')
  1952. {
  1953. $type = substr($line, 3, 1);
  1954. // An error??
  1955. if (trim($current_data) != '' && $type !== '}')
  1956. {
  1957. $upcontext['error_message'] = 'Error in upgrade script - line ' . $line_number . '!' . $endl;
  1958. if ($command_line)
  1959. echo $upcontext['error_message'];
  1960. }
  1961. if ($type == ' ')
  1962. {
  1963. if (!$support_js && $do_current && $_GET['substep'] != 0 && $command_line)
  1964. {
  1965. echo ' Successful.', $endl;
  1966. flush();
  1967. }
  1968. $last_step = htmlspecialchars(rtrim(substr($line, 4)));
  1969. $upcontext['current_item_num']++;
  1970. $upcontext['current_item_name'] = $last_step;
  1971. if ($do_current)
  1972. {
  1973. $upcontext['actioned_items'][] = $last_step;
  1974. if ($command_line)
  1975. echo ' * ';
  1976. }
  1977. }
  1978. elseif ($type == '#')
  1979. {
  1980. $upcontext['step_progress'] += (100 / $upcontext['file_count']) / $file_steps;
  1981. $upcontext['current_debug_item_num']++;
  1982. if (trim($line) != '---#')
  1983. $upcontext['current_debug_item_name'] = htmlspecialchars(rtrim(substr($line, 4)));
  1984. // Have we already done something?
  1985. if (isset($_GET['xml']) && $done_something)
  1986. {
  1987. restore_error_handler();
  1988. return $upcontext['current_debug_item_num'] >= $upcontext['debug_items'] ? true : false;
  1989. }
  1990. if ($do_current)
  1991. {
  1992. if (trim($line) == '---#' && $command_line)
  1993. echo ' done.', $endl;
  1994. elseif ($command_line)
  1995. echo ' +++ ', rtrim(substr($line, 4));
  1996. elseif (trim($line) != '---#')
  1997. {
  1998. if ($is_debug)
  1999. $upcontext['actioned_items'][] = htmlspecialchars(rtrim(substr($line, 4)));
  2000. }
  2001. }
  2002. if ($substep < $_GET['substep'] && $substep + 1 >= $_GET['substep'])
  2003. {
  2004. if ($command_line)
  2005. echo ' * ';
  2006. else
  2007. $upcontext['actioned_items'][] = $last_step;
  2008. }
  2009. // Small step - only if we're actually doing stuff.
  2010. if ($do_current)
  2011. nextSubstep(++$substep);
  2012. else
  2013. $substep++;
  2014. }
  2015. elseif ($type == '{')
  2016. $current_type = 'code';
  2017. elseif ($type == '}')
  2018. {
  2019. $current_type = 'sql';
  2020. if (!$do_current)
  2021. {
  2022. $current_data = '';
  2023. continue;
  2024. }
  2025. if (eval('global $db_prefix, $modSettings, $smcFunc; ' . $current_data) === false)
  2026. {
  2027. $upcontext['error_message'] = 'Error in upgrade script ' . basename($filename) . ' on line ' . $line_number . '!' . $endl;
  2028. if ($command_line)
  2029. echo $upcontext['error_message'];
  2030. }
  2031. // Done with code!
  2032. $current_data = '';
  2033. $done_something = true;
  2034. }
  2035. continue;
  2036. }
  2037. $current_data .= $line;
  2038. if (substr(rtrim($current_data), -1) === ';' && $current_type === 'sql')
  2039. {
  2040. if ((!$support_js || isset($_GET['xml'])))
  2041. {
  2042. if (!$do_current)
  2043. {
  2044. $current_data = '';
  2045. continue;
  2046. }
  2047. $current_data = strtr(substr(rtrim($current_data), 0, -1), array('{$db_prefix}' => $db_prefix, '{BOARDDIR}' => BOARDDIR, '{$sboarddir}' => addslashes(BOARDDIR), '{$boardurl}' => $boardurl, '{$db_collation}' => $db_collation));
  2048. upgrade_query($current_data);
  2049. // @todo This will be how it kinda does it once mysql all stripped out - needed for postgre (etc).
  2050. /*
  2051. $result = $smcFunc['db_query']('', $current_data, false, false);
  2052. // Went wrong?
  2053. if (!$result)
  2054. {
  2055. // Bit of a bodge - do we want the error?
  2056. if (!empty($upcontext['return_error']))
  2057. {
  2058. $upcontext['error_message'] = $smcFunc['db_error']($db_connection);
  2059. return false;
  2060. }
  2061. }*/
  2062. $done_something = true;
  2063. }
  2064. $current_data = '';
  2065. }
  2066. // If this is xml based and we're just getting the item name then that's grand.
  2067. elseif ($support_js && !isset($_GET['xml']) && $upcontext['current_debug_item_name'] != '' && $do_current)
  2068. {
  2069. restore_error_handler();
  2070. return false;
  2071. }
  2072. // Clean up by cleaning any step info.
  2073. $step_progress = array();
  2074. $custom_warning = '';
  2075. }
  2076. // Put back the error handler.
  2077. restore_error_handler();
  2078. if ($command_line)
  2079. {
  2080. echo ' Successful.' . "\n";
  2081. flush();
  2082. }
  2083. $_GET['substep'] = 0;
  2084. return true;
  2085. }
  2086. function upgrade_query($string, $unbuffered = false)
  2087. {
  2088. global $db_connection, $db_server, $db_user, $db_passwd, $db_type, $command_line, $upcontext, $upgradeurl, $modSettings;
  2089. global $db_name, $db_unbuffered, $smcFunc;
  2090. // Get the query result - working around some specific security - just this once!
  2091. $modSettings['disableQueryCheck'] = true;
  2092. $db_unbuffered = $unbuffered;
  2093. $result = $smcFunc['db_query']('', $string, 'security_override');
  2094. $db_unbuffered = false;
  2095. // Failure?!
  2096. if ($result !== false)
  2097. return $result;
  2098. $db_error_message = $smcFunc['db_error']($db_connection);
  2099. // If MySQL we do something more clever.
  2100. if ($db_type == 'mysql')
  2101. {
  2102. $mysql_errno = mysql_errno($db_connection);
  2103. $error_query = in_array(substr(trim($string), 0, 11), array('INSERT INTO', 'UPDATE IGNO', 'ALTER TABLE', 'DROP TABLE ', 'ALTER IGNOR'));
  2104. // Error numbers:
  2105. // 1016: Can't open file '....MYI'
  2106. // 1050: Table already exists.
  2107. // 1054: Unknown column name.
  2108. // 1060: Duplicate column name.
  2109. // 1061: Duplicate key name.
  2110. // 1062: Duplicate entry for unique key.
  2111. // 1068: Multiple primary keys.
  2112. // 1072: Key column '%s' doesn't exist in table.
  2113. // 1091: Can't drop key, doesn't exist.
  2114. // 1146: Table doesn't exist.
  2115. // 2013: Lost connection to server during query.
  2116. if ($mysql_errno == 1016)
  2117. {
  2118. if (preg_match('~\'([^\.\']+)~', $db_error_message, $match) != 0 && !empty($match[1]))
  2119. mysql_query( '
  2120. REPAIR TABLE `' . $match[1] . '`');
  2121. $result = mysql_query($string);
  2122. if ($result !== false)
  2123. return $result;
  2124. }
  2125. elseif ($mysql_errno == 2013)
  2126. {
  2127. $db_connection = mysql_connect($db_server, $db_user, $db_passwd);
  2128. mysql_select_db($db_name, $db_connection);
  2129. if ($db_connection)
  2130. {
  2131. $result = mysql_query($string);
  2132. if ($result !== false)
  2133. return $result;
  2134. }
  2135. }
  2136. // Duplicate column name... should be okay ;).
  2137. elseif (in_array($mysql_errno, array(1060, 1061, 1068, 1091)))
  2138. return false;
  2139. // Duplicate insert... make sure it's the proper type of query ;).
  2140. elseif (in_array($mysql_errno, array(1054, 1062, 1146)) && $error_query)
  2141. return false;
  2142. // Creating an index on a non-existent column.
  2143. elseif ($mysql_errno == 1072)
  2144. return false;
  2145. elseif ($mysql_errno == 1050 && substr(trim($string), 0, 12) == 'RENAME TABLE')
  2146. return false;
  2147. }
  2148. // If a table already exists don't go potty.
  2149. else
  2150. {
  2151. if (in_array(substr(trim($string), 0, 8), array('CREATE T', 'CREATE S', 'DROP TABL', 'ALTER TA', 'CREATE I')))
  2152. {
  2153. if (strpos($db_error_message, 'exist') !== false)
  2154. return true;
  2155. // SQLite
  2156. if (strpos($db_error_message, 'missing') !== false)
  2157. return true;
  2158. }
  2159. elseif (strpos(trim($string), 'INSERT ') !== false)
  2160. {
  2161. if (strpos($db_error_message, 'duplicate') !== false)
  2162. return true;
  2163. }
  2164. }
  2165. // Get the query string so we pass everything.
  2166. $query_string = '';
  2167. foreach ($_GET as $k => $v)
  2168. $query_string .= ';' . $k . '=' . $v;
  2169. if (strlen($query_string) != 0)
  2170. $query_string = '?' . substr($query_string, 1);
  2171. if ($command_line)
  2172. {
  2173. echo 'Unsuccessful! Database error message:', "\n", $db_error_message, "\n";
  2174. die;
  2175. }
  2176. // Bit of a bodge - do we want the error?
  2177. if (!empty($upcontext['return_error']))
  2178. {
  2179. $upcontext['error_message'] = $db_error_message;
  2180. return false;
  2181. }
  2182. // Otherwise we have to display this somewhere appropriate if possible.
  2183. $upcontext['forced_error_message'] = '
  2184. <strong>Unsuccessful!</strong><br />
  2185. <div style="margin: 2ex;">
  2186. This query:
  2187. <blockquote><tt>' . nl2br(htmlspecialchars(trim($string))) . ';</tt></blockquote>
  2188. Caused the error:
  2189. <blockquote>' . nl2br(htmlspecialchars($db_error_message)) . '</blockquote>
  2190. </div>
  2191. <form action="' . $upgradeurl . $query_string . '" method="post">
  2192. <input type="submit" value="Try again" class="button_submit" />
  2193. </form>
  2194. </div>';
  2195. upgradeExit();
  2196. }
  2197. // This performs a table alter, but does it unbuffered so the script can time out professionally.
  2198. function protected_alter($change, $substep, $is_test = false)
  2199. {
  2200. global $db_prefix, $smcFunc;
  2201. db_extend('packages');
  2202. // Firstly, check whether the current index/column exists.
  2203. $found = false;
  2204. if ($change['type'] === 'column')
  2205. {
  2206. $columns = $smcFunc['db_list_columns']('{db_prefix}' . $change['table'], true);
  2207. foreach ($columns as $column)
  2208. {
  2209. // Found it?
  2210. if ($column['name'] === $change['name'])
  2211. {
  2212. $found |= 1;
  2213. // Do some checks on the data if we have it set.
  2214. if (isset($change['col_type']))
  2215. $found &= $change['col_type'] === $column['type'];
  2216. if (isset($change['null_allowed']))
  2217. $found &= $column['null'] == $change['null_allowed'];
  2218. if (isset($change['default']))
  2219. $found &= $change['default'] === $column['default'];
  2220. }
  2221. }
  2222. }
  2223. elseif ($change['type'] === 'index')
  2224. {
  2225. $request = upgrade_query( '
  2226. SHOW INDEX
  2227. FROM ' . $db_prefix . $change['table']);
  2228. if ($request !== false)
  2229. {
  2230. $cur_index = array();
  2231. while ($row = $smcFunc['db_fetch_assoc']($request))
  2232. if ($row['Key_name'] === $change['name'])
  2233. $cur_index[(int) $row['Seq_in_index']] = $row['Column_name'];
  2234. ksort($cur_index, SORT_NUMERIC);
  2235. $found = array_values($cur_index) === $change['target_columns'];
  2236. $smcFunc['db_free_result']($request);
  2237. }
  2238. }
  2239. // If we're trying to add and it's added, we're done.
  2240. if ($found && in_array($change['method'], array('add', 'change')))
  2241. return true;
  2242. // Otherwise if we're removing and it wasn't found we're also done.
  2243. elseif (!$found && in_array($change['method'], array('remove', 'change_remove')))
  2244. return true;
  2245. // Otherwise is it just a test?
  2246. elseif ($is_test)
  2247. return false;
  2248. // Not found it yet? Bummer! How about we see if we're currently doing it?
  2249. $running = false;
  2250. $found = false;
  2251. while (1 == 1)
  2252. {
  2253. $request = upgrade_query('
  2254. SHOW FULL PROCESSLIST');
  2255. while ($row = $smcFunc['db_fetch_assoc']($request))
  2256. {
  2257. if (strpos($row['Info'], 'ALTER TABLE ' . $db_prefix . $change['table']) !== false && strpos($row['Info'], $change['text']) !== false)
  2258. $found = true;
  2259. }
  2260. // Can't find it? Then we need to run it fools!
  2261. if (!$found && !$running)
  2262. {
  2263. $smcFunc['db_free_result']($request);
  2264. $success = upgrade_query('
  2265. ALTER TABLE ' . $db_prefix . $change['table'] . '
  2266. ' . $change['text'], true) !== false;
  2267. if (!$success)
  2268. return false;
  2269. // Return
  2270. $running = true;
  2271. }
  2272. // What if we've not found it, but we'd ran it already? Must of completed.
  2273. elseif (!$found)
  2274. {
  2275. $smcFunc['db_free_result']($request);
  2276. return true;
  2277. }
  2278. // Pause execution for a sec or three.
  2279. sleep(3);
  2280. // Can never be too well protected.
  2281. nextSubstep($substep);
  2282. }
  2283. // Protect it.
  2284. nextSubstep($substep);
  2285. }
  2286. // Alter a text column definition preserving its character set.
  2287. function textfield_alter($change, $substep)
  2288. {
  2289. global $db_prefix, $databases, $db_type, $smcFunc;
  2290. // Versions of MySQL < 4.1 wouldn't benefit from character set detection.
  2291. if (empty($databases[$db_type]['utf8_support']) || version_compare($databases[$db_type]['utf8_version'], eval($databases[$db_type]['utf8_version_check']), '>'))
  2292. {
  2293. $column_fix = true;
  2294. $null_fix = !$change['null_allowed'];
  2295. }
  2296. else
  2297. {
  2298. $request = $smcFunc['db_query']('', '
  2299. SHOW FULL COLUMNS
  2300. FROM {db_prefix}' . $change['table'] . '
  2301. LIKE {string:column}',
  2302. array(
  2303. 'column' => $change['column'],
  2304. 'db_error_skip' => true,
  2305. )
  2306. );
  2307. if ($smcFunc['db_num_rows']($request) === 0)
  2308. die('Unable to find column ' . $change['column'] . ' inside table ' . $db_prefix . $change['table']);
  2309. $table_row = $smcFunc['db_fetch_assoc']($request);
  2310. $smcFunc['db_free_result']($request);
  2311. // If something of the current column definition is different, fix it.
  2312. $column_fix = $table_row['Type'] !== $change['type'] || (strtolower($table_row['Null']) === 'yes') !== $change['null_allowed'] || ($table_row['Default'] === null) !== !isset($change['default']) || (isset($change['default']) && $change['default'] !== $table_row['Default']);
  2313. // Columns that previously allowed null, need to be converted first.
  2314. $null_fix = strtolower($table_row['Null']) === 'yes' && !$change['null_allowed'];
  2315. // Get the character set that goes with the collation of the column.
  2316. if ($column_fix && !empty($table_row['Collation']))
  2317. {
  2318. $request = $smcFunc['db_query']('', '
  2319. SHOW COLLATION
  2320. LIKE {string:collation}',
  2321. array(
  2322. 'collation' => $table_row['Collation'],
  2323. 'db_error_skip' => true,
  2324. )
  2325. );
  2326. // No results? Just forget it all together.
  2327. if ($smcFunc['db_num_rows']($request) === 0)
  2328. unset($table_row['Collation']);
  2329. else
  2330. $collation_info = $smcFunc['db_fetch_assoc']($request);
  2331. $smcFunc['db_free_result']($request);
  2332. }
  2333. }
  2334. if ($column_fix)
  2335. {
  2336. // Make sure there are no NULL's left.
  2337. if ($null_fix)
  2338. $smcFunc['db_query']('', '
  2339. UPDATE {db_prefix}' . $change['table'] . '
  2340. SET ' . $change['column'] . ' = {string:default}
  2341. WHERE ' . $change['column'] . ' IS NULL',
  2342. array(
  2343. 'default' => isset($change['default']) ? $change['default'] : '',
  2344. 'db_error_skip' => true,
  2345. )
  2346. );
  2347. // Do the actual alteration.
  2348. $smcFunc['db_query']('', '
  2349. ALTER TABLE {db_prefix}' . $change['table'] . '
  2350. CHANGE COLUMN ' . $change['column'] . ' ' . $change['column'] . ' ' . $change['type'] . (isset($collation_info['Charset']) ? ' CHARACTER SET ' . $collation_info['Charset'] . ' COLLATE ' . $collation_info['Collation'] : '') . ($change['null_allowed'] ? '' : ' NOT NULL') . (isset($change['default']) ? ' default {string:default}' : ''),
  2351. array(
  2352. 'default' => isset($change['default']) ? $change['default'] : '',
  2353. 'db_error_skip' => true,
  2354. )
  2355. );
  2356. }
  2357. nextSubstep($substep);
  2358. }
  2359. // Check if we need to alter this query.
  2360. function checkChange(&$change)
  2361. {
  2362. global $smcFunc, $db_type, $databases;
  2363. static $database_version, $where_field_support;
  2364. // Attempt to find a database_version.
  2365. if (empty($database_version))
  2366. {
  2367. $database_version = $databases[$db_type]['version_check'];
  2368. $where_field_support = $db_type == 'mysql' && version_compare('5.0', $database_version, '<=');
  2369. }
  2370. // Not a column we need to check on?
  2371. if (!in_array($change['name'], array('memberGroups', 'passwordSalt')))
  2372. return;
  2373. // Break it up you (six|seven).
  2374. $temp = explode(' ', str_replace('NOT NULL', 'NOT_NULL', $change['text']));
  2375. // Can we support a shortcut method?
  2376. if ($where_field_support)
  2377. {
  2378. // Get the details about this change.
  2379. $request = $smcFunc['db_query']('', '
  2380. SHOW FIELDS
  2381. FROM {db_prefix}{raw:table}
  2382. WHERE Field = {string:old_name} OR Field = {string:new_name}',
  2383. array(
  2384. 'table' => $change['table'],
  2385. 'old_name' => $temp[1],
  2386. 'new_name' => $temp[2],
  2387. ));
  2388. if ($smcFunc['db_num_rows'] != 1)
  2389. return;
  2390. list (, $current_type) = $smcFunc['db_fetch_assoc']($request);
  2391. $smcFunc['db_free_result']($request);
  2392. }
  2393. else
  2394. {
  2395. // Do this the old fashion, sure method way.
  2396. $request = $smcFunc['db_query']('', '
  2397. SHOW FIELDS
  2398. FROM {db_prefix}{raw:table}',
  2399. array(
  2400. 'table' => $change['table'],
  2401. ));
  2402. // Mayday!
  2403. if ($smcFunc['db_num_rows'] == 0)
  2404. return;
  2405. // Oh where, oh where has my little field gone. Oh where can it be...
  2406. while ($row = $smcFunc['db_query']($request))
  2407. if ($row['Field'] == $temp[1] || $row['Field'] == $temp[2])
  2408. {
  2409. $current_type = $row['Type'];
  2410. break;
  2411. }
  2412. }
  2413. // If this doesn't match, the column may of been altered for a reason.
  2414. if (trim($current_type) != trim($temp[3]))
  2415. $temp[3] = $current_type;
  2416. // Piece this back together.
  2417. $change['text'] = str_replace('NOT_NULL', 'NOT NULL', implode(' ', $temp));
  2418. }
  2419. // The next substep.
  2420. function nextSubstep($substep)
  2421. {
  2422. global $start_time, $timeLimitThreshold, $command_line, $file_steps, $modSettings, $custom_warning;
  2423. global $step_progress, $is_debug, $upcontext;
  2424. if ($_GET['substep'] < $substep)
  2425. $_GET['substep'] = $substep;
  2426. if ($command_line)
  2427. {
  2428. if (time() - $start_time > 1 && empty($is_debug))
  2429. {
  2430. echo '.';
  2431. $start_time = time();
  2432. }
  2433. return;
  2434. }
  2435. @set_time_limit(300);
  2436. if (function_exists('apache_reset_timeout'))
  2437. @apache_reset_timeout();
  2438. if (time() - $start_time <= $timeLimitThreshold)
  2439. return;
  2440. // Do we have some custom step progress stuff?
  2441. if (!empty($step_progress))
  2442. {
  2443. $upcontext['substep_progress'] = 0;
  2444. $upcontext['substep_progress_name'] = $step_progress['name'];
  2445. if ($step_progress['current'] > $step_progress['total'])
  2446. $upcontext['substep_progress'] = 99.9;
  2447. else
  2448. $upcontext['substep_progress'] = ($step_progress['current'] / $step_progress['total']) * 100;
  2449. // Make it nicely rounded.
  2450. $upcontext['substep_progress'] = round($upcontext['substep_progress'], 1);
  2451. }
  2452. // If this is XML we just exit right away!
  2453. if (isset($_GET['xml']))
  2454. return upgradeExit();
  2455. // We're going to pause after this!
  2456. $upcontext['pause'] = true;
  2457. $upcontext['query_string'] = '';
  2458. foreach ($_GET as $k => $v)
  2459. {
  2460. if ($k != 'data' && $k != 'substep' && $k != 'step')
  2461. $upcontext['query_string'] .= ';' . $k . '=' . $v;
  2462. }
  2463. // Custom warning?
  2464. if (!empty($custom_warning))
  2465. $upcontext['custom_warning'] = $custom_warning;
  2466. upgradeExit();
  2467. }
  2468. function cmdStep0()
  2469. {
  2470. global $db_prefix, $language, $modSettings, $start_time, $databases, $db_type, $smcFunc, $upcontext;
  2471. global $language, $is_debug, $txt;
  2472. $start_time = time();
  2473. ob_end_clean();
  2474. ob_implicit_flush(true);
  2475. @set_time_limit(600);
  2476. if (!isset($_SERVER['argv']))
  2477. $_SERVER['argv'] = array();
  2478. $_GET['maint'] = 1;
  2479. foreach ($_SERVER['argv'] as $i => $arg)
  2480. {
  2481. if (preg_match('~^--language=(.+)$~', $arg, $match) != 0)
  2482. $_GET['lang'] = $match[1];
  2483. elseif (preg_match('~^--path=(.+)$~', $arg) != 0)
  2484. continue;
  2485. elseif ($arg == '--no-maintenance')
  2486. $_GET['maint'] = 0;
  2487. elseif ($arg == '--debug')
  2488. $is_debug = true;
  2489. elseif ($arg == '--backup')
  2490. $_POST['backup'] = 1;
  2491. elseif ($arg == '--template' && (file_exists(BOARDDIR . '/template.php') || file_exists(BOARDDIR . '/template.html') && !file_exists($modSettings['theme_dir'] . '/converted')))
  2492. $_GET['conv'] = 1;
  2493. elseif ($i != 0)
  2494. {
  2495. echo 'ELKARTE Command-line Upgrader
  2496. Usage: /path/to/php -f ' . basename(__FILE__) . ' -- [OPTION]...
  2497. --language=LANG Reset the forum\'s language to LANG.
  2498. --no-maintenance Don\'t put the forum into maintenance mode.
  2499. --debug Output debugging information.
  2500. --backup Create backups of tables with "backup_" prefix.';
  2501. echo "\n";
  2502. exit;
  2503. }
  2504. }
  2505. if (!php_version_check())
  2506. print_error('Error: PHP ' . PHP_VERSION . ' does not match version requirements.', true);
  2507. if (!db_version_check())
  2508. print_error('Error: ' . $databases[$db_type]['name'] . ' ' . $databases[$db_type]['version'] . ' does not match minimum requirements.', true);
  2509. if (!empty($databases[$db_type]['alter_support']) && $smcFunc['db_query']('alter_boards', 'ALTER TABLE {db_prefix}boards ORDER BY id_board', array()) === false)
  2510. print_error('Error: The ' . $databases[$db_type]['name'] . ' account in Settings.php does not have sufficient privileges.', true);
  2511. $check = @file_exists($modSettings['theme_dir'] . '/index.template.php')
  2512. && @file_exists(SOURCEDIR . '/QueryString.php')
  2513. && @file_exists(SOURCEDIR . '/ManageBoards.php');
  2514. if (!$check && !isset($modSettings['elkVersion']))
  2515. print_error('Error: Some files are missing or out-of-date.', true);
  2516. // Do a quick version spot check.
  2517. $temp = substr(@implode('', @file(BOARDDIR . '/index.php')), 0, 4096);
  2518. preg_match('~\*\s@version\s+(.+)[\s]{2}~i', $temp, $match);
  2519. if (empty($match[1]) || $match[1] != CURRENT_VERSION)
  2520. print_error('Error: Some files have not yet been updated properly.');
  2521. // Make sure Settings.php is writable.
  2522. if (!is_writable(BOARDDIR . '/Settings.php'))
  2523. @chmod(BOARDDIR . '/Settings.php', 0777);
  2524. if (!is_writable(BOARDDIR . '/Settings.php'))
  2525. print_error('Error: Unable to obtain write access to "Settings.php".', true);
  2526. // Make sure Settings.php is writable.
  2527. if (!is_writable(BOARDDIR . '/Settings_bak.php'))
  2528. @chmod(BOARDDIR . '/Settings_bak.php', 0777);
  2529. if (!is_writable(BOARDDIR . '/Settings_bak.php'))
  2530. print_error('Error: Unable to obtain write access to "Settings_bak.php".');
  2531. if (isset($modSettings['agreement']) && (!is_writable(BOARDDIR) || file_exists(BOARDDIR . '/agreement.txt')) && !is_writable(BOARDDIR . '/agreement.txt'))
  2532. print_error('Error: Unable to obtain write access to "agreement.txt".');
  2533. elseif (isset($modSettings['agreement']))
  2534. {
  2535. $fp = fopen(BOARDDIR . '/agreement.txt', 'w');
  2536. fwrite($fp, $modSettings['agreement']);
  2537. fclose($fp);
  2538. }
  2539. // Make sure themes is writable.
  2540. if (!is_writable($modSettings['theme_dir']))
  2541. @chmod($modSettings['theme_dir'], 0777);
  2542. if (!is_writable($modSettings['theme_dir']) && !isset($modSettings['elkVersion']))
  2543. print_error('Error: Unable to obtain write access to "themes".');
  2544. // Make sure cache directory exists and is writable!
  2545. $CACHEDIR_temp = !defined('CACHEDIR') ? BOARDDIR . '/cache' : CACHEDIR;
  2546. if (!file_exists($CACHEDIR_temp))
  2547. @mkdir($CACHEDIR_temp);
  2548. if (!is_writable($CACHEDIR_temp))
  2549. @chmod($CACHEDIR_temp, 0777);
  2550. if (!is_writable($CACHEDIR_temp))
  2551. print_error('Error: Unable to obtain write access to "cache".', true);
  2552. if (!file_exists($modSettings['theme_dir'] . '/languages/index.' . $upcontext['language'] . '.php') && !isset($modSettings['elkVersion']) && !isset($_GET['lang']))
  2553. print_error('Error: Unable to find language files!', true);
  2554. else
  2555. {
  2556. $temp = substr(@implode('', @file($modSettings['theme_dir'] . '/languages/index.' . $upcontext['language'] . '.php')), 0, 4096);
  2557. preg_match('~(?://|/\*)\s*Version:\s+(.+?);\s*index(?:[\s]{2}|\*/)~i', $temp, $match);
  2558. if (empty($match[1]) || $match[1] != CURRENT_LANG_VERSION)
  2559. print_error('Error: Language files out of date.', true);
  2560. if (!file_exists($modSettings['theme_dir'] . 'languages/Install.' . $upcontext['language'] . '.php'))
  2561. print_error('Error: Install language is missing for selected language.', true);
  2562. // Otherwise include it!
  2563. require_once($modSettings['theme_dir'] . 'languages/Install.' . $upcontext['language'] . '.php');
  2564. }
  2565. // Make sure we skip the HTML for login.
  2566. $_POST['upcont'] = true;
  2567. $upcontext['current_step'] = 1;
  2568. }
  2569. function print_error($message, $fatal = false)
  2570. {
  2571. static $fp = null;
  2572. if ($fp === null)
  2573. $fp = fopen('php://stderr', 'wb');
  2574. fwrite($fp, $message . "\n");
  2575. if ($fatal)
  2576. exit;
  2577. }
  2578. function throw_error($message)
  2579. {
  2580. global $upcontext;
  2581. $upcontext['error_msg'] = $message;
  2582. $upcontext['sub_template'] = 'error_message';
  2583. return false;
  2584. }
  2585. // Check files are writable - make them writable if necessary...
  2586. function makeFilesWritable(&$files)
  2587. {
  2588. global $upcontext;
  2589. if (empty($files))
  2590. return true;
  2591. $failure = false;
  2592. // On linux, it's easy - just use is_writable!
  2593. if (substr(__FILE__, 1, 2) != ':\\')
  2594. {
  2595. foreach ($files as $k => $file)
  2596. {
  2597. if (!is_writable($file))
  2598. {
  2599. @chmod($file, 0755);
  2600. // Well, 755 hopefully worked... if not, try 777.
  2601. if (!is_writable($file) && !@chmod($file, 0777))
  2602. $failure = true;
  2603. // Otherwise remove it as it's good!
  2604. else
  2605. unset($files[$k]);
  2606. }
  2607. else
  2608. unset($files[$k]);
  2609. }
  2610. }
  2611. // Windows is trickier. Let's try opening for r+...
  2612. else
  2613. {
  2614. foreach ($files as $k => $file)
  2615. {
  2616. // Folders can't be opened for write... but the index.php in them can ;).
  2617. if (is_dir($file))
  2618. $file .= '/index.php';
  2619. // Funny enough, chmod actually does do something on windows - it removes the read only attribute.
  2620. @chmod($file, 0777);
  2621. $fp = @fopen($file, 'r+');
  2622. // Hmm, okay, try just for write in that case...
  2623. if (!$fp)
  2624. $fp = @fopen($file, 'w');
  2625. if (!$fp)
  2626. $failure = true;
  2627. else
  2628. unset($files[$k]);
  2629. @fclose($fp);
  2630. }
  2631. }
  2632. if (empty($files))
  2633. return true;
  2634. if (!isset($_SERVER))
  2635. return !$failure;
  2636. // What still needs to be done?
  2637. $upcontext['chmod']['files'] = $files;
  2638. // If it's windows it's a mess...
  2639. if ($failure && substr(__FILE__, 1, 2) == ':\\')
  2640. {
  2641. $upcontext['chmod']['ftp_error'] = 'total_mess';
  2642. return false;
  2643. }
  2644. // We're going to have to use... FTP!
  2645. elseif ($failure)
  2646. {
  2647. // Load any session data we might have...
  2648. if (!isset($_POST['ftp_username']) && isset($_SESSION['installer_temp_ftp']))
  2649. {
  2650. $upcontext['chmod']['server'] = $_SESSION['installer_temp_ftp']['server'];
  2651. $upcontext['chmod']['port'] = $_SESSION['installer_temp_ftp']['port'];
  2652. $upcontext['chmod']['username'] = $_SESSION['installer_temp_ftp']['username'];
  2653. $upcontext['chmod']['password'] = $_SESSION['installer_temp_ftp']['password'];
  2654. $upcontext['chmod']['path'] = $_SESSION['installer_temp_ftp']['path'];
  2655. }
  2656. // Or have we submitted?
  2657. elseif (isset($_POST['ftp_username']))
  2658. {
  2659. $upcontext['chmod']['server'] = $_POST['ftp_server'];
  2660. $upcontext['chmod']['port'] = $_POST['ftp_port'];
  2661. $upcontext['chmod']['username'] = $_POST['ftp_username'];
  2662. $upcontext['chmod']['password'] = $_POST['ftp_password'];
  2663. $upcontext['chmod']['path'] = $_POST['ftp_path'];
  2664. }
  2665. if (isset($upcontext['chmod']['username']))
  2666. {
  2667. $ftp = new Ftp_Connection($upcontext['chmod']['server'], $upcontext['chmod']['port'], $upcontext['chmod']['username'], $upcontext['chmod']['password']);
  2668. if ($ftp->error === false)
  2669. {
  2670. // Try it without /home/abc just in case they messed up.
  2671. if (!$ftp->chdir($upcontext['chmod']['path']))
  2672. {
  2673. $upcontext['chmod']['ftp_error'] = $ftp->last_message;
  2674. $ftp->chdir(preg_replace('~^/home[2]?/[^/]+?~', '', $upcontext['chmod']['path']));
  2675. }
  2676. }
  2677. }
  2678. if (!isset($ftp) || $ftp->error !== false)
  2679. {
  2680. if (!isset($ftp))
  2681. $ftp = new Ftp_Connection(null);
  2682. // Save the error so we can mess with listing...
  2683. elseif ($ftp->error !== false && !isset($upcontext['chmod']['ftp_error']))
  2684. $upcontext['chmod']['ftp_error'] = $ftp->last_message === null ? '' : $ftp->last_message;
  2685. list ($username, $detect_path, $found_path) = $ftp->detect_path(dirname(__FILE__));
  2686. if ($found_path || !isset($upcontext['chmod']['path']))
  2687. $upcontext['chmod']['path'] = $detect_path;
  2688. if (!isset($upcontext['chmod']['username']))
  2689. $upcontext['chmod']['username'] = $username;
  2690. return false;
  2691. }
  2692. else
  2693. {
  2694. // We want to do a relative path for FTP.
  2695. if (!in_array($upcontext['chmod']['path'], array('', '/')))
  2696. {
  2697. $ftp_root = strtr(BOARDDIR, array($upcontext['chmod']['path'] => ''));
  2698. if (substr($ftp_root, -1) == '/' && ($upcontext['chmod']['path'] == '' || $upcontext['chmod']['path'][0] === '/'))
  2699. $ftp_root = substr($ftp_root, 0, -1);
  2700. }
  2701. else
  2702. $ftp_root = BOARDDIR;
  2703. // Save the info for next time!
  2704. $_SESSION['installer_temp_ftp'] = array(
  2705. 'server' => $upcontext['chmod']['server'],
  2706. 'port' => $upcontext['chmod']['port'],
  2707. 'username' => $upcontext['chmod']['username'],
  2708. 'password' => $upcontext['chmod']['password'],
  2709. 'path' => $upcontext['chmod']['path'],
  2710. 'root' => $ftp_root,
  2711. );
  2712. foreach ($files as $k => $file)
  2713. {
  2714. if (!is_writable($file))
  2715. $ftp->chmod($file, 0755);
  2716. if (!is_writable($file))
  2717. $ftp->chmod($file, 0777);
  2718. // Assuming that didn't work calculate the path without the boarddir.
  2719. if (!is_writable($file))
  2720. {
  2721. if (strpos($file, BOARDDIR) === 0)
  2722. {
  2723. $ftp_file = strtr($file, array($_SESSION['installer_temp_ftp']['root'] => ''));
  2724. $ftp->chmod($ftp_file, 0755);
  2725. if (!is_writable($file))
  2726. $ftp->chmod($ftp_file, 0777);
  2727. // Sometimes an extra slash can help...
  2728. $ftp_file = '/' . $ftp_file;
  2729. if (!is_writable($file))
  2730. $ftp->chmod($ftp_file, 0755);
  2731. if (!is_writable($file))
  2732. $ftp->chmod($ftp_file, 0777);
  2733. }
  2734. }
  2735. if (is_writable($file))
  2736. unset($files[$k]);
  2737. }
  2738. $ftp->close();
  2739. }
  2740. }
  2741. // What remains?
  2742. $upcontext['chmod']['files'] = $files;
  2743. if (empty($files))
  2744. return true;
  2745. return false;
  2746. }
  2747. function deleteUpgrader()
  2748. {
  2749. @unlink(__FILE__);
  2750. // And the extra little files ;).
  2751. @unlink(dirname(__FILE__) . '/upgrade_1-0.sql');
  2752. @unlink(dirname(__FILE__) . '/upgrade_1-1.sql');
  2753. @unlink(dirname(__FILE__) . '/upgrade_2-0_' . $db_type . '.sql');
  2754. @unlink(dirname(__FILE__) . '/upgrade_2-1_' . $db_type . '.sql');
  2755. @unlink(dirname(__FILE__) . '/webinstall.php');
  2756. $dh = opendir(dirname(__FILE__));
  2757. while ($file = readdir($dh))
  2758. {
  2759. if (preg_match('~upgrade_\d-\d_([A-Za-z])+\.sql~i', $file, $matches) && isset($matches[1]))
  2760. @unlink(dirname(__FILE__) . '/' . $file);
  2761. }
  2762. closedir($dh);
  2763. header('Location: http://' . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT']) . dirname($_SERVER['PHP_SELF']) . '/themes/default/images/blank.png');
  2764. exit;
  2765. }
  2766. /******************************************************************************
  2767. ******************* Templates are below this point ****************************
  2768. ******************************************************************************/
  2769. // This is what is displayed if there's any chmod to be done. If not it returns nothing...
  2770. function template_chmod()
  2771. {
  2772. global $upcontext, $upgradeurl, $settings;
  2773. // Don't call me twice!
  2774. if (!empty($upcontext['chmod_called']))
  2775. return;
  2776. $upcontext['chmod_called'] = true;
  2777. // Nothing?
  2778. if (empty($upcontext['chmod']['files']) && empty($upcontext['chmod']['ftp_error']))
  2779. return;
  2780. // @todo Temporary!
  2781. $txt['error_ftp_no_connect'] = 'Unable to connect to FTP server with this combination of details.';
  2782. $txt['ftp_login'] = 'Your FTP connection information';
  2783. $txt['ftp_login_info'] = 'This web installer needs your FTP information in order to automate the installation for you. Please note that none of this information is saved in your installation, it is just used to setup ELKARTE.';
  2784. $txt['ftp_server'] = 'Server';
  2785. $txt['ftp_server_info'] = 'The address (often localhost) and port for your FTP server.';
  2786. $txt['ftp_port'] = 'Port';
  2787. $txt['ftp_username'] = 'Username';
  2788. $txt['ftp_username_info'] = 'The username to login with. <em>This will not be saved anywhere.</em>';
  2789. $txt['ftp_password'] = 'Password';
  2790. $txt['ftp_password_info'] = 'The password to login with. <em>This will not be saved anywhere.</em>';
  2791. $txt['ftp_path'] = 'Install Path';
  2792. $txt['ftp_path_info'] = 'This is the <em>relative</em> path you use in your FTP client <a href="' . $_SERVER['PHP_SELF'] . '?ftphelp" onclick="window.open(this.href, \'\', \'width=450,height=250\');return false;" target="_blank">(more help)</a>.';
  2793. $txt['ftp_path_found_info'] = 'The path in the box above was automatically detected.';
  2794. $txt['ftp_path_help'] = 'Your FTP path is the path you see when you log in to your FTP client. It commonly starts with &quot;<tt>www</tt>&quot;, &quot;<tt>public_html</tt>&quot;, or &quot;<tt>httpdocs</tt>&quot; - but it should include the directory ELKARTE is in too, such as &quot;/public_html/forum&quot;. It is different from your URL and full path.<br /><br />Files in this path may be overwritten, so make sure it\'s correct.';
  2795. $txt['ftp_path_help_close'] = 'Close';
  2796. $txt['ftp_connect'] = 'Connect';
  2797. // Was it a problem with Windows?
  2798. if (!empty($upcontext['chmod']['ftp_error']) && $upcontext['chmod']['ftp_error'] == 'total_mess')
  2799. {
  2800. echo '
  2801. <div class="error_message">
  2802. <div style="color: red;">The following files need to be writable to continue the upgrade. Please ensure the Windows permissions are correctly set to allow this:</div>
  2803. <ul style="margin: 2.5ex; font-family: monospace;">
  2804. <li>' . implode('</li>
  2805. <li>', $upcontext['chmod']['files']). '</li>
  2806. </ul>
  2807. </div>';
  2808. return false;
  2809. }
  2810. echo '
  2811. <div class="panel">
  2812. <h2>Your FTP connection information</h2>
  2813. <h3>The upgrader can fix any issues with file permissions to make upgrading as simple as possible. Simply enter your connection information below or alternatively click <a href="#" onclick="warning_popup();">here</a> for a list of files which need to be changed.</h3>
  2814. <script type="text/javascript"><!-- // --><![CDATA[
  2815. function warning_popup()
  2816. {
  2817. popup = window.open(\'\',\'popup\',\'height=150,width=400,scrollbars=yes\');
  2818. var content = popup.document;
  2819. content.write(\'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n\');
  2820. content.write(\'<html xmlns="http://www.w3.org/1999/xhtml"', $upcontext['right_to_left'] ? ' dir="rtl"' : '', '>\n\t<head>\n\t\t<meta name="robots" content="noindex" />\n\t\t\');
  2821. content.write(\'<title>Warning</title>\n\t\t<link rel="stylesheet" type="text/css" href="', $settings['default_theme_url'], '/css/index.css" />\n\t</head>\n\t<body id="popup">\n\t\t\');
  2822. content.write(\'<div class="windowbg description">\n\t\t\t<h4>The following files needs to be made writable to continue:</h4>\n\t\t\t\');
  2823. content.write(\'<p>', implode('<br />\n\t\t\t', $upcontext['chmod']['files']), '</p>\n\t\t\t\');
  2824. content.write(\'<a href="javascript:self.close();">close</a>\n\t\t</div>\n\t</body>\n</html>\');
  2825. content.close();
  2826. }
  2827. // ]]></script>';
  2828. if (!empty($upcontext['chmod']['ftp_error']))
  2829. echo '
  2830. <div class="error_message">
  2831. <div style="color: red;">
  2832. The following error was encountered when trying to connect:<br />
  2833. <br />
  2834. <code>', $upcontext['chmod']['ftp_error'], '</code>
  2835. </div>
  2836. </div>
  2837. <br />';
  2838. if (empty($upcontext['chmod_in_form']))
  2839. echo '
  2840. <form action="', $upcontext['form_url'], '" method="post">';
  2841. echo '
  2842. <table width="520" cellspacing="0" cellpadding="0" border="0" align="center" style="margin-bottom: 1ex;">
  2843. <tr>
  2844. <td width="26%" valign="top" class="textbox"><label for="ftp_server">', $txt['ftp_server'], ':</label></td>
  2845. <td>
  2846. <div style="float: right; margin-right: 1px;"><label for="ftp_port" class="textbox"><strong>', $txt['ftp_port'], ':&nbsp;</strong></label> <input type="text" size="3" name="ftp_port" id="ftp_port" value="', isset($upcontext['chmod']['port']) ? $upcontext['chmod']['port'] : '21', '" class="input_text" /></div>
  2847. <input type="text" size="30" name="ftp_server" id="ftp_server" value="', isset($upcontext['chmod']['server']) ? $upcontext['chmod']['server'] : 'localhost', '" style="width: 70%;" class="input_text" />
  2848. <div style="font-size: smaller; margin-bottom: 2ex;">', $txt['ftp_server_info'], '</div>
  2849. </td>
  2850. </tr><tr>
  2851. <td width="26%" valign="top" class="textbox"><label for="ftp_username">', $txt['ftp_username'], ':</label></td>
  2852. <td>
  2853. <input type="text" size="50" name="ftp_username" id="ftp_username" value="', isset($upcontext['chmod']['username']) ? $upcontext['chmod']['username'] : '', '" style="width: 99%;" class="input_text" />
  2854. <div style="font-size: smaller; margin-bottom: 2ex;">', $txt['ftp_username_info'], '</div>
  2855. </td>
  2856. </tr><tr>
  2857. <td width="26%" valign="top" class="textbox"><label for="ftp_password">', $txt['ftp_password'], ':</label></td>
  2858. <td>
  2859. <input type="password" size="50" name="ftp_password" id="ftp_password" style="width: 99%;" class="input_password" />
  2860. <div style="font-size: smaller; margin-bottom: 3ex;">', $txt['ftp_password_info'], '</div>
  2861. </td>
  2862. </tr><tr>
  2863. <td width="26%" valign="top" class="textbox"><label for="ftp_path">', $txt['ftp_path'], ':</label></td>
  2864. <td style="padding-bottom: 1ex;">
  2865. <input type="text" size="50" name="ftp_path" id="ftp_path" value="', isset($upcontext['chmod']['path']) ? $upcontext['chmod']['path'] : '', '" style="width: 99%;" class="input_text" />
  2866. <div style="font-size: smaller; margin-bottom: 2ex;">', !empty($upcontext['chmod']['path']) ? $txt['ftp_path_found_info'] : $txt['ftp_path_info'], '</div>
  2867. </td>
  2868. </tr>
  2869. </table>
  2870. <div class="righttext" style="margin: 1ex;"><input type="submit" value="', $txt['ftp_connect'], '" class="button_submit" /></div>
  2871. </div>';
  2872. if (empty($upcontext['chmod_in_form']))
  2873. echo '
  2874. </form>';
  2875. }
  2876. function template_upgrade_above()
  2877. {
  2878. global $modSettings, $txt, $oursite, $settings, $upcontext, $upgradeurl;
  2879. echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2880. <html xmlns="http://www.w3.org/1999/xhtml"', $upcontext['right_to_left'] ? ' dir="rtl"' : '', '>
  2881. <head>
  2882. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  2883. <meta name="robots" content="noindex" />
  2884. <title>', $txt['upgrade_upgrade_utility'], '</title>
  2885. <link rel="stylesheet" type="text/css" href="', $settings['default_theme_url'], '/css/index.css?alp21" />
  2886. <link rel="stylesheet" type="text/css" href="', $settings['default_theme_url'], '/css/install.css?alp21" />
  2887. <script type="text/javascript" src="', $settings['default_theme_url'], '/scripts/script.js"></script>
  2888. <script type="text/javascript"><!-- // --><![CDATA[
  2889. var smf_scripturl = \'', $upgradeurl, '\';
  2890. var smf_charset = \'UTF-8\';
  2891. var startPercent = ', $upcontext['overall_percent'], ';
  2892. // This function dynamically updates the step progress bar - and overall one as required.
  2893. function updateStepProgress(current, max, overall_weight)
  2894. {
  2895. // What out the actual percent.
  2896. var width = parseInt((current / max) * 100);
  2897. if (document.getElementById(\'step_progress\'))
  2898. {
  2899. document.getElementById(\'step_progress\').style.width = width + "%";
  2900. setInnerHTML(document.getElementById(\'step_text\'), width + "%");
  2901. }
  2902. if (overall_weight && document.getElementById(\'overall_progress\'))
  2903. {
  2904. overall_width = parseInt(startPercent + width * (overall_weight / 100));
  2905. document.getElementById(\'overall_progress\').style.width = overall_width + "%";
  2906. setInnerHTML(document.getElementById(\'overall_text\'), overall_width + "%");
  2907. }
  2908. }
  2909. // ]]></script>
  2910. </head>
  2911. <body>
  2912. <div id="header"><div class="frame">
  2913. <div id="top_section">
  2914. <h1 class="forumtitle">', $txt['upgrade_upgrade_utility'], '</h1>
  2915. <img id="logo" src="', $settings['default_theme_url'], '/images/logo.png" alt="Elkarte Community" title="Elkarte Community" />
  2916. </div>
  2917. <div id="upper_section" class="middletext flow_hidden">
  2918. <div class="user"></div>
  2919. <div class="news normaltext">
  2920. </div>
  2921. </div>
  2922. </div></div>
  2923. <div id="content_section"><div class="frame">
  2924. <div id="main_content_section">
  2925. <div id="main-steps">
  2926. <h2>', $txt['upgrade_progress'], '</h2>
  2927. <ul>';
  2928. foreach ($upcontext['steps'] as $num => $step)
  2929. echo '
  2930. <li class="', $num < $upcontext['current_step'] ? 'stepdone' : ($num == $upcontext['current_step'] ? 'stepcurrent' : 'stepwaiting'), '">', $txt['upgrade_step'], ' ', $step[0], ': ', $step[1], '</li>';
  2931. echo '
  2932. </ul>
  2933. </div>
  2934. <div style="float: left; width: 40%;">
  2935. <div style="font-size: 8pt; height: 12pt; border: 1px solid black; background-color: white; width: 50%; margin: auto;">
  2936. <div id="overall_text" style="color: #000; position: absolute; margin-left: -5em;">', $upcontext['overall_percent'], '%</div>
  2937. <div id="overall_progress" style="width: ', $upcontext['overall_percent'], '%; height: 12pt; z-index: 1; background-color: lime;">&nbsp;</div>
  2938. <div class="progress">', $txt['upgrade_overall_progress'], '</div>
  2939. </div>
  2940. ';
  2941. if (isset($upcontext['step_progress']))
  2942. echo '
  2943. <div style="font-size: 8pt; height: 12pt; border: 1px solid black; background-color: white; width: 50%; margin: 5px auto; ">
  2944. <div id="step_text" style="color: #000; position: absolute; margin-left: -5em;">', $upcontext['step_progress'], '%</div>
  2945. <div id="step_progress" style="width: ', $upcontext['step_progress'], '%; height: 12pt; z-index: 1; background-color: #ffd000;">&nbsp;</div>
  2946. <div class="progress">', $txt['upgrade_step_progress'], '</div>
  2947. </div>
  2948. ';
  2949. echo '
  2950. <div id="substep_bar_div" class="smalltext" style="display: ', isset($upcontext['substep_progress']) ? '' : 'none', ';">', isset($upcontext['substep_progress_name']) ? trim(strtr($upcontext['substep_progress_name'], array('.' => ''))) : '', ':</div>
  2951. <div id="substep_bar_div2" style="font-size: 8pt; height: 12pt; border: 1px solid black; background-color: white; width: 50%; margin: 5px auto; display: ', isset($upcontext['substep_progress']) ? '' : 'none', ';">
  2952. <div id="substep_text" style="color: #000; position: absolute; margin-left: -5em;">', isset($upcontext['substep_progress']) ? $upcontext['substep_progress'] : '', '%</div>
  2953. <div id="substep_progress" style="width: ', isset($upcontext['substep_progress']) ? $upcontext['substep_progress'] : 0, '%; height: 12pt; z-index: 1; background-color: #eebaf4;">&nbsp;</div>
  2954. </div>';
  2955. // How long have we been running this?
  2956. $elapsed = time() - $upcontext['started'];
  2957. $mins = (int) ($elapsed / 60);
  2958. $seconds = $elapsed - $mins * 60;
  2959. echo '
  2960. <div class="smalltext" style="padding: 5px; text-align: center;">', $txt['upgrade_time_elapsed'], ':
  2961. <span id="mins_elapsed">', $mins, '</span> ', $txt['upgrade_time_mins'], ', <span id="secs_elapsed">', $seconds, '</span> ', $txt['upgrade_time_secs'], '.
  2962. </div>';
  2963. echo '
  2964. </div>
  2965. <div id="main_screen" class="clear">
  2966. <h2>', $upcontext['page_title'], '</h2>
  2967. <div class="panel">
  2968. <div style="max-height: 360px; overflow: auto;">';
  2969. }
  2970. function template_upgrade_below()
  2971. {
  2972. global $upcontext, $txt;
  2973. if (!empty($upcontext['pause']))
  2974. echo '
  2975. <em>', $txt['upgrade_incomplete'], '.</em><br />
  2976. <h2 style="margin-top: 2ex;">', $txt['upgrade_not_quite_done'], '</h2>
  2977. <h3>
  2978. ', $txt['upgrade_paused_overload'], '
  2979. </h3>';
  2980. if (!empty($upcontext['custom_warning']))
  2981. echo '
  2982. <div style="margin: 2ex; padding: 2ex; border: 2px dashed #cc3344; color: black; background-color: #ffe4e9;">
  2983. <div style="float: left; width: 2ex; font-size: 2em; color: red;">!!</div>
  2984. <strong style="text-decoration: underline;">', $txt['upgrade_note'], '</strong><br />
  2985. <div style="padding-left: 6ex;">', $upcontext['custom_warning'], '</div>
  2986. </div>';
  2987. echo '
  2988. <div class="righttext" style="margin: 1ex;">';
  2989. if (!empty($upcontext['continue']))
  2990. echo '
  2991. <input type="submit" id="contbutt" name="contbutt" value="', $txt['upgrade_continue'], '"', $upcontext['continue'] == 2 ? ' disabled="disabled"' : '', ' class="button_submit" />';
  2992. if (!empty($upcontext['skip']))
  2993. echo '
  2994. <input type="submit" id="skip" name="skip" value="', $txt['upgrade_skip'], '" onclick="dontSubmit = true; document.getElementById(\'contbutt\').disabled = \'disabled\'; return true;" class="button_submit" />';
  2995. echo '
  2996. </div>
  2997. </form>
  2998. </div>
  2999. </div>
  3000. </div>
  3001. </div>
  3002. </div></div>
  3003. <div id="footer_section"><div class="frame" style="height: 40px;">
  3004. <div class="smalltext"><a href="http://www.elkarte.net/" title="Elkarte Community" target="_blank" class="new_win">ELKARTE &copy;2011, Elkarte</a></div>
  3005. </div></div>
  3006. </body>
  3007. </html>';
  3008. // Are we on a pause?
  3009. if (!empty($upcontext['pause']))
  3010. {
  3011. echo '
  3012. <script type="text/javascript"><!-- // --><![CDATA[
  3013. window.onload = doAutoSubmit;
  3014. var countdown = 3;
  3015. var dontSubmit = false;
  3016. function doAutoSubmit()
  3017. {
  3018. if (countdown == 0 && !dontSubmit)
  3019. document.upform.submit();
  3020. else if (countdown == -1)
  3021. return;
  3022. document.getElementById(\'contbutt\').value = "', $txt['upgrade_continue'], ' (" + countdown + ")";
  3023. countdown--;
  3024. setTimeout("doAutoSubmit();", 1000);
  3025. }
  3026. // ]]></script>';
  3027. }
  3028. }
  3029. function template_xml_above()
  3030. {
  3031. global $upcontext;
  3032. echo '<', '?xml version="1.0" encoding="ISO-8859-1"?', '>
  3033. <smf>';
  3034. if (!empty($upcontext['get_data']))
  3035. foreach ($upcontext['get_data'] as $k => $v)
  3036. echo '
  3037. <get key="', $k, '">', $v, '</get>';
  3038. }
  3039. function template_xml_below()
  3040. {
  3041. global $upcontext;
  3042. echo '
  3043. </smf>';
  3044. }
  3045. function template_error_message()
  3046. {
  3047. global $upcontext;
  3048. echo '
  3049. <div class="error_message">
  3050. <div style="color: red;">
  3051. ', $upcontext['error_msg'], '
  3052. </div>
  3053. <br />
  3054. <a href="', $_SERVER['PHP_SELF'], '">Click here to try again.</a>
  3055. </div>';
  3056. }
  3057. function template_welcome_message()
  3058. {
  3059. global $upcontext, $modSettings, $upgradeurl, $disable_security, $settings, $txt;
  3060. echo '
  3061. <script type="text/javascript" src="http://www.elkarte.net/current-version.js?version=' . CURRENT_VERSION . '"></script>
  3062. <script type="text/javascript" src="', $settings['default_theme_url'], '/scripts/sha1.js"></script>
  3063. <h3>', sprintf($txt['upgrade_ready_proceed'], CURRENT_VERSION), '</h3>
  3064. <form action="', $upcontext['form_url'], '" method="post" name="upform" id="upform" ', empty($upcontext['disable_login_hashing']) ? ' onsubmit="hashLoginPassword(this, \'' . $upcontext['rid'] . '\', \'' . (!empty($upcontext['login_token']) ? $upcontext['login_token'] : '') . '\');"' : '', '>
  3065. <input type="hidden" name="', $upcontext['login_token_var'], '" value="', $upcontext['login_token'], '" />
  3066. <div id="version_warning" style="margin: 2ex; padding: 2ex; border: 2px dashed #a92174; color: black; background-color: #fbbbe2; display: none;">
  3067. <div style="float: left; width: 2ex; font-size: 2em; color: red;">!!</div>
  3068. <strong style="text-decoration: underline;">', $txt['upgrade_warning'], '</strong><br />
  3069. <div style="padding-left: 6ex;">
  3070. ', sprintf($txt['upgrade_warning_out_of_date'], CURRENT_VERSION), '
  3071. </div>
  3072. </div>';
  3073. $upcontext['chmod_in_form'] = true;
  3074. template_chmod();
  3075. // For large, SMF pre-1.1 RC2 forums give them a warning about the possible impact of this upgrade!
  3076. if ($upcontext['is_large_forum'])
  3077. echo '
  3078. <div style="margin: 2ex; padding: 2ex; border: 2px dashed #cc3344; color: black; background-color: #ffe4e9;">
  3079. <div style="float: left; width: 2ex; font-size: 2em; color: red;">!!</div>
  3080. <strong style="text-decoration: underline;">', $txt['upgrade_warning'], '</strong><br />
  3081. <div style="padding-left: 6ex;">
  3082. ', $txt['upgrade_warning_lots_data'], '
  3083. </div>
  3084. </div>';
  3085. // A warning message?
  3086. if (!empty($upcontext['warning']))
  3087. echo '
  3088. <div style="margin: 2ex; padding: 2ex; border: 2px dashed #cc3344; color: black; background-color: #ffe4e9;">
  3089. <div style="float: left; width: 2ex; font-size: 2em; color: red;">!!</div>
  3090. <strong style="text-decoration: underline;">', $txt['upgrade_warning'], '</strong><br />
  3091. <div style="padding-left: 6ex;">
  3092. ', $upcontext['warning'], '
  3093. </div>
  3094. </div>';
  3095. // Paths are incorrect?
  3096. echo '
  3097. <div style="margin: 2ex; padding: 2ex; border: 2px dashed #804840; color: black; background-color: #fe5a44; ', (file_exists($settings['default_theme_dir'] . '/scripts/script.js') ? 'display: none;' : ''), '" id="js_script_missing_error">
  3098. <div style="float: left; width: 2ex; font-size: 2em; color: black;">!!</div>
  3099. <strong style="text-decoration: underline;">', $txt['upgrade_critical_error'], '</strong><br />
  3100. <div style="padding-left: 6ex;">
  3101. ', $txt['upgrade_error_script_js'], '
  3102. </div>
  3103. </div>';
  3104. // Is there someone already doing this?
  3105. if (!empty($upcontext['user']['id']) && (time() - $upcontext['started'] < 72600 || time() - $upcontext['updated'] < 3600))
  3106. {
  3107. $ago = time() - $upcontext['started'];
  3108. if ($ago < 60)
  3109. $ago = $ago . ' seconds';
  3110. elseif ($ago < 3600)
  3111. $ago = (int) ($ago / 60) . ' minutes';
  3112. else
  3113. $ago = (int) ($ago / 3600) . ' hours';
  3114. $active = time() - $upcontext['updated'];
  3115. if ($active < 60)
  3116. $updated = $active . ' seconds';
  3117. elseif ($active < 3600)
  3118. $updated = (int) ($active / 60) . ' minutes';
  3119. else
  3120. $updated = (int) ($active / 3600) . ' hours';
  3121. echo '
  3122. <div style="margin: 2ex; padding: 2ex; border: 2px dashed #cc3344; color: black; background-color: #ffe4e9;">
  3123. <div style="float: left; width: 2ex; font-size: 2em; color: red;">!!</div>
  3124. <strong style="text-decoration: underline;">', $txt['upgrade_warning'], '</strong><br />
  3125. <div style="padding-left: 6ex;">
  3126. &quot;', $upcontext['user']['name'], '&quot; has been running the upgrade script for the last ', $ago, ' - and was last active ', $updated, ' ago.';
  3127. if ($active < 600)
  3128. echo '
  3129. We recommend that you do not run this script unless you are sure that ', $upcontext['user']['name'], ' has completed their upgrade.';
  3130. if ($active > $upcontext['inactive_timeout'])
  3131. echo '
  3132. <br /><br />You can choose to either run the upgrade again from the beginning - or alternatively continue from the last step reached during the last upgrade.';
  3133. else
  3134. echo '
  3135. <br /><br />This upgrade script cannot be run until ', $upcontext['user']['name'], ' has been inactive for at least ', ($upcontext['inactive_timeout'] > 120 ? round($upcontext['inactive_timeout'] / 60, 1) . ' minutes!' : $upcontext['inactive_timeout'] . ' seconds!');
  3136. echo '
  3137. </div>
  3138. </div>';
  3139. }
  3140. echo '
  3141. <strong>Admin Login: ', $disable_security ? '(DISABLED)' : '', '</strong>
  3142. <h3>For security purposes please login with your admin account to proceed with the upgrade.</h3>
  3143. <table>
  3144. <tr valign="top">
  3145. <td><strong ', $disable_security ? 'style="color: gray;"' : '', '>Username:</strong></td>
  3146. <td>
  3147. <input type="text" name="user" value="', !empty($upcontext['username']) ? $upcontext['username'] : '', '" ', $disable_security ? 'disabled="disabled"' : '', ' class="input_text" />';
  3148. if (!empty($upcontext['username_incorrect']))
  3149. echo '
  3150. <div class="smalltext" style="color: red;">Username Incorrect</div>';
  3151. echo '
  3152. </td>
  3153. </tr>
  3154. <tr valign="top">
  3155. <td><strong ', $disable_security ? 'style="color: gray;"' : '', '>Password:</strong></td>
  3156. <td>
  3157. <input type="password" name="passwrd" value=""', $disable_security ? ' disabled="disabled"' : '', ' class="input_password" />
  3158. <input type="hidden" name="hash_passwrd" value="" />';
  3159. if (!empty($upcontext['password_failed']))
  3160. echo '
  3161. <div class="smalltext" style="color: red;">Password Incorrect</div>';
  3162. echo '
  3163. </td>
  3164. </tr>';
  3165. // Can they continue?
  3166. if (!empty($upcontext['user']['id']) && time() - $upcontext['user']['updated'] >= $upcontext['inactive_timeout'] && $upcontext['user']['step'] > 1)
  3167. {
  3168. echo '
  3169. <tr>
  3170. <td colspan="2">
  3171. <label for="cont"><input type="checkbox" id="cont" name="cont" checked="checked" class="input_check" />Continue from step reached during last execution of upgrade script.</label>
  3172. </td>
  3173. </tr>';
  3174. }
  3175. echo '
  3176. </table><br />
  3177. <span class="smalltext">
  3178. <strong>Note:</strong> If necessary the above security check can be bypassed for users who may administrate a server but not have admin rights on the forum. In order to bypass the above check simply open &quot;upgrade.php&quot; in a text editor and replace &quot;$disable_security = 0;&quot; with &quot;$disable_security = 1;&quot; and refresh this page.
  3179. </span>
  3180. <input type="hidden" name="login_attempt" id="login_attempt" value="1" />
  3181. <input type="hidden" name="js_works" id="js_works" value="0" />';
  3182. // Say we want the continue button!
  3183. $upcontext['continue'] = !empty($upcontext['user']['id']) && time() - $upcontext['user']['updated'] < $upcontext['inactive_timeout'] ? 2 : 1;
  3184. // This defines whether javascript is going to work elsewhere :D
  3185. echo '
  3186. <script type="text/javascript"><!-- // --><![CDATA[
  3187. if (\'XMLHttpRequest\' in window && document.getElementById(\'js_works\'))
  3188. document.getElementById(\'js_works\').value = 1;
  3189. // Latest version?
  3190. function ourCurrentVersion()
  3191. {
  3192. var ourVer, yourVer;
  3193. if (!(\'ourVersion\' in window))
  3194. return;
  3195. window.ourVersion = window.ourVersion.replace(/ELKARTE\s?/g, \'\');
  3196. ourVer = document.getElementById(\'ourVersion\');
  3197. yourVer = document.getElementById(\'yourVersion\');
  3198. setInnerHTML(ourVer, window.ourVersion);
  3199. var currentVersion = getInnerHTML(yourVer);
  3200. if (currentVersion < window.ourVersion)
  3201. document.getElementById(\'version_warning\').style.display = \'\';
  3202. }
  3203. addLoadEvent(ourCurrentVersion);
  3204. // This checks that the script file even exists!
  3205. if (typeof(ourSelectText) == \'undefined\')
  3206. document.getElementById(\'js_script_missing_error\').style.display = \'\';
  3207. // ]]></script>';
  3208. }
  3209. function template_upgrade_options()
  3210. {
  3211. global $upcontext, $modSettings, $upgradeurl, $disable_security, $settings, $db_prefix, $mmessage, $mtitle, $db_type;
  3212. echo '
  3213. <h3>Before the upgrade gets underway please review the options below - and hit continue when you\'re ready to begin.</h3>
  3214. <form action="', $upcontext['form_url'], '" method="post" name="upform" id="upform">';
  3215. // Warning message?
  3216. if (!empty($upcontext['upgrade_options_warning']))
  3217. echo '
  3218. <div style="margin: 1ex; padding: 1ex; border: 1px dashed #cc3344; color: black; background-color: #ffe4e9;">
  3219. <div style="float: left; width: 2ex; font-size: 2em; color: red;">!!</div>
  3220. <strong style="text-decoration: underline;">Warning!</strong><br />
  3221. <div style="padding-left: 4ex;">
  3222. ', $upcontext['upgrade_options_warning'], '
  3223. </div>
  3224. </div>';
  3225. echo '
  3226. <table cellpadding="1" cellspacing="0">
  3227. <tr valign="top">
  3228. <td width="2%">
  3229. <input type="checkbox" name="backup" id="backup" value="1"', $db_type != 'mysql' && $db_type != 'postgresql' ? ' disabled="disabled"' : '', ' class="input_check" />
  3230. </td>
  3231. <td width="100%">
  3232. <label for="backup">Backup tables in your database with the prefix &quot;backup_' . $db_prefix . '&quot;.</label>', isset($modSettings['elkVersion']) ? '' : ' (recommended!)', '
  3233. </td>
  3234. </tr>
  3235. <tr valign="top">
  3236. <td width="2%">
  3237. <input type="checkbox" name="maint" id="maint" value="1" checked="checked" class="input_check" />
  3238. </td>
  3239. <td width="100%">
  3240. <label for="maint">Put the forum into maintenance mode during upgrade.</label> <span class="smalltext">(<a href="#" onclick="document.getElementById(\'mainmess\').style.display = document.getElementById(\'mainmess\').style.display == \'\' ? \'none\' : \'\'">Customize</a>)</span>
  3241. <div id="mainmess" style="display: none;">
  3242. <strong class="smalltext">Maintenance Title: </strong><br />
  3243. <input type="text" name="maintitle" size="30" value="', htmlspecialchars($mtitle), '" class="input_text" /><br />
  3244. <strong class="smalltext">Maintenance Message: </strong><br />
  3245. <textarea name="mainmessage" rows="3" cols="50">', htmlspecialchars($mmessage), '</textarea>
  3246. </div>
  3247. </td>
  3248. </tr>
  3249. <tr valign="top">
  3250. <td width="2%">
  3251. <input type="checkbox" name="debug" id="debug" value="1" class="input_check" />
  3252. </td>
  3253. <td width="100%">
  3254. <label for="debug">Output extra debugging information</label>
  3255. </td>
  3256. </tr>
  3257. <tr valign="top">
  3258. <td width="2%">
  3259. <input type="checkbox" name="empty_error" id="empty_error" value="1" class="input_check" />
  3260. </td>
  3261. <td width="100%">
  3262. <label for="empty_error">Empty error log before upgrading</label>
  3263. </td>
  3264. </tr>
  3265. </table>
  3266. <input type="hidden" name="upcont" value="1" />';
  3267. // We need a normal continue button here!
  3268. $upcontext['continue'] = 1;
  3269. }
  3270. // Template for the database backup tool/
  3271. function template_backup_database()
  3272. {
  3273. global $upcontext, $modSettings, $upgradeurl, $disable_security, $settings, $support_js, $is_debug;
  3274. echo '
  3275. <h3>Please wait while a backup is created. For large forums this may take some time!</h3>';
  3276. echo '
  3277. <form action="', $upcontext['form_url'], '" name="upform" id="upform" method="post">
  3278. <input type="hidden" name="backup_done" id="backup_done" value="0" />
  3279. <strong>Completed <span id="tab_done">', $upcontext['cur_table_num'], '</span> out of ', $upcontext['table_count'], ' tables.</strong>
  3280. <span id="debuginfo"></span>';
  3281. // Dont any tables so far?
  3282. if (!empty($upcontext['previous_tables']))
  3283. foreach ($upcontext['previous_tables'] as $table)
  3284. echo '
  3285. <br />Completed Table: &quot;', $table, '&quot;.';
  3286. echo '
  3287. <h3 id="current_tab_div">Current Table: &quot;<span id="current_table">', $upcontext['cur_table_name'], '</span>&quot;</h3>
  3288. <br /><span id="commess" style="font-weight: bold; display: ', $upcontext['cur_table_num'] == $upcontext['table_count'] ? 'inline' : 'none', ';">Backup Complete! Click Continue to Proceed.</span>';
  3289. // Continue please!
  3290. $upcontext['continue'] = $support_js ? 2 : 1;
  3291. // If javascript allows we want to do this using XML.
  3292. if ($support_js)
  3293. {
  3294. echo '
  3295. <script type="text/javascript"><!-- // --><![CDATA[
  3296. var lastTable = ', $upcontext['cur_table_num'], ';
  3297. function getNextTables()
  3298. {
  3299. getXMLDocument(\'', $upcontext['form_url'], '&xml&substep=\' + lastTable, onBackupUpdate);
  3300. }
  3301. // Got an update!
  3302. function onBackupUpdate(oXMLDoc)
  3303. {
  3304. var sCurrentTableName = "";
  3305. var iTableNum = 0;
  3306. var sCompletedTableName = getInnerHTML(document.getElementById(\'current_table\'));
  3307. for (var i = 0; i < oXMLDoc.getElementsByTagName("table")[0].childNodes.length; i++)
  3308. sCurrentTableName += oXMLDoc.getElementsByTagName("table")[0].childNodes[i].nodeValue;
  3309. iTableNum = oXMLDoc.getElementsByTagName("table")[0].getAttribute("num");
  3310. // Update the page.
  3311. setInnerHTML(document.getElementById(\'tab_done\'), iTableNum);
  3312. setInnerHTML(document.getElementById(\'current_table\'), sCurrentTableName);
  3313. lastTable = iTableNum;
  3314. updateStepProgress(iTableNum, ', $upcontext['table_count'], ', ', $upcontext['step_weight'] * ((100 - $upcontext['step_progress']) / 100), ');';
  3315. // If debug flood the screen.
  3316. if ($is_debug)
  3317. echo '
  3318. setOuterHTML(document.getElementById(\'debuginfo\'), \'<br />Completed Table: &quot;\' + sCompletedTableName + \'&quot;.<span id="debuginfo"><\' + \'/span>\');';
  3319. echo '
  3320. // Get the next update...
  3321. if (iTableNum == ', $upcontext['table_count'], ')
  3322. {
  3323. document.getElementById(\'commess\').style.display = "";
  3324. document.getElementById(\'current_tab_div\').style.display = "none";
  3325. document.getElementById(\'contbutt\').disabled = 0;
  3326. document.getElementById(\'backup_done\').value = 1;
  3327. }
  3328. else
  3329. getNextTables();
  3330. }
  3331. getNextTables();
  3332. // ]]></script>';
  3333. }
  3334. }
  3335. function template_backup_xml()
  3336. {
  3337. global $upcontext, $settings, $options, $txt;
  3338. echo '
  3339. <table num="', $upcontext['cur_table_num'], '">', $upcontext['cur_table_name'], '</table>';
  3340. }
  3341. // Here is the actual "make the changes" template!
  3342. function template_database_changes()
  3343. {
  3344. global $upcontext, $modSettings, $upgradeurl, $disable_security, $settings, $support_js, $is_debug, $timeLimitThreshold;
  3345. echo '
  3346. <h3>Executing database changes</h3>
  3347. <h4 style="font-style: italic;">Please be patient - this may take some time on large forums. The time elapsed increments from the server to show progress is being made!</h4>';
  3348. echo '
  3349. <form action="', $upcontext['form_url'], '&amp;filecount=', $upcontext['file_count'], '" name="upform" id="upform" method="post">
  3350. <input type="hidden" name="database_done" id="database_done" value="0" />';
  3351. // No javascript looks rubbish!
  3352. if (!$support_js)
  3353. {
  3354. foreach ($upcontext['actioned_items'] as $num => $item)
  3355. {
  3356. if ($num != 0)
  3357. echo ' Successful!';
  3358. echo '<br />' . $item;
  3359. }
  3360. if (!empty($upcontext['changes_complete']))
  3361. echo ' Successful!<br /><br /><span id="commess" style="font-weight: bold;">Database Updates Complete! Click Continue to Proceed.</span><br />';
  3362. }
  3363. else
  3364. {
  3365. // Tell them how many files we have in total.
  3366. if ($upcontext['file_count'] > 1)
  3367. echo '
  3368. <strong id="info1">Executing upgrade script <span id="file_done">', $upcontext['cur_file_num'], '</span> of ', $upcontext['file_count'], '.</strong>';
  3369. echo '
  3370. <h3 id="info2"><strong>Executing:</strong> &quot;<span id="cur_item_name">', $upcontext['current_item_name'], '</span>&quot; (<span id="item_num">', $upcontext['current_item_num'], '</span> of <span id="total_items"><span id="item_count">', $upcontext['total_items'], '</span>', $upcontext['file_count'] > 1 ? ' - of this script' : '', ')</span></h3>
  3371. <br /><span id="commess" style="font-weight: bold; display: ', !empty($upcontext['changes_complete']) || $upcontext['current_debug_item_num'] == $upcontext['debug_items'] ? 'inline' : 'none', ';">Database Updates Complete! Click Continue to Proceed.</span>';
  3372. if ($is_debug)
  3373. {
  3374. echo '
  3375. <div id="debug_section" style="height: 200px; overflow: auto;">
  3376. <span id="debuginfo"></span>
  3377. </div>';
  3378. }
  3379. }
  3380. // Place for the XML error message.
  3381. echo '
  3382. <div id="error_block" style="margin: 2ex; padding: 2ex; border: 2px dashed #cc3344; color: black; background-color: #ffe4e9; display: ', empty($upcontext['error_message']) ? 'none' : '', ';">
  3383. <div style="float: left; width: 2ex; font-size: 2em; color: red;">!!</div>
  3384. <strong style="text-decoration: underline;">Error!</strong><br />
  3385. <div style="padding-left: 6ex;" id="error_message">', isset($upcontext['error_message']) ? $upcontext['error_message'] : 'Unknown Error!', '</div>
  3386. </div>';
  3387. // We want to continue at some point!
  3388. $upcontext['continue'] = $support_js ? 2 : 1;
  3389. // If javascript allows we want to do this using XML.
  3390. if ($support_js)
  3391. {
  3392. echo '
  3393. <script type="text/javascript"><!-- // --><![CDATA[
  3394. var lastItem = ', $upcontext['current_debug_item_num'], ';
  3395. var sLastString = "', strtr($upcontext['current_debug_item_name'], array('"' => '&quot;')), '";
  3396. var iLastSubStepProgress = -1;
  3397. var curFile = ', $upcontext['cur_file_num'], ';
  3398. var totalItems = 0;
  3399. var prevFile = 0;
  3400. var retryCount = 0;
  3401. var testvar = 0;
  3402. var timeOutID = 0;
  3403. var getData = "";
  3404. var debugItems = ', $upcontext['debug_items'], ';
  3405. function getNextItem()
  3406. {
  3407. // We want to track this...
  3408. if (timeOutID)
  3409. clearTimeout(timeOutID);
  3410. timeOutID = window.setTimeout("retTimeout()", ', (10 * $timeLimitThreshold), '000);
  3411. getXMLDocument(\'', $upcontext['form_url'], '&xml&filecount=', $upcontext['file_count'], '&substep=\' + lastItem + getData, onItemUpdate);
  3412. }
  3413. // Got an update!
  3414. function onItemUpdate(oXMLDoc)
  3415. {
  3416. var sItemName = "";
  3417. var sDebugName = "";
  3418. var iItemNum = 0;
  3419. var iSubStepProgress = -1;
  3420. var iDebugNum = 0;
  3421. var bIsComplete = 0;
  3422. getData = "";
  3423. // We\'ve got something - so reset the timeout!
  3424. if (timeOutID)
  3425. clearTimeout(timeOutID);
  3426. // Assume no error at this time...
  3427. document.getElementById("error_block").style.display = "none";
  3428. // Are we getting some duff info?
  3429. if (!oXMLDoc.getElementsByTagName("item")[0])
  3430. {
  3431. // Too many errors?
  3432. if (retryCount > 15)
  3433. {
  3434. document.getElementById("error_block").style.display = "";
  3435. setInnerHTML(document.getElementById("error_message"), "Error retrieving information on step: " + (sDebugName == "" ? sLastString : sDebugName));';
  3436. if ($is_debug)
  3437. echo '
  3438. setOuterHTML(document.getElementById(\'debuginfo\'), \'<span style="color: red;">failed<\' + \'/span><span id="debuginfo"><\' + \'/span>\');';
  3439. echo '
  3440. }
  3441. else
  3442. {
  3443. retryCount++;
  3444. getNextItem();
  3445. }
  3446. return false;
  3447. }
  3448. // Never allow loops.
  3449. if (curFile == prevFile)
  3450. {
  3451. retryCount++;
  3452. if (retryCount > 10)
  3453. {
  3454. document.getElementById("error_block").style.display = "";
  3455. setInnerHTML(document.getElementById("error_message"), "Upgrade script appears to be going into a loop - step: " + sDebugName);';
  3456. if ($is_debug)
  3457. echo '
  3458. setOuterHTML(document.getElementById(\'debuginfo\'), \'<span style="color: red;">failed<\' + \'/span><span id="debuginfo"><\' + \'/span>\');';
  3459. echo '
  3460. }
  3461. }
  3462. retryCount = 0;
  3463. for (var i = 0; i < oXMLDoc.getElementsByTagName("item")[0].childNodes.length; i++)
  3464. sItemName += oXMLDoc.getElementsByTagName("item")[0].childNodes[i].nodeValue;
  3465. for (var i = 0; i < oXMLDoc.getElementsByTagName("debug")[0].childNodes.length; i++)
  3466. sDebugName += oXMLDoc.getElementsByTagName("debug")[0].childNodes[i].nodeValue;
  3467. for (var i = 0; i < oXMLDoc.getElementsByTagName("get").length; i++)
  3468. {
  3469. getData += "&" + oXMLDoc.getElementsByTagName("get")[i].getAttribute("key") + "=";
  3470. for (var j = 0; j < oXMLDoc.getElementsByTagName("get")[i].childNodes.length; j++)
  3471. {
  3472. getData += oXMLDoc.getElementsByTagName("get")[i].childNodes[j].nodeValue;
  3473. }
  3474. }
  3475. iItemNum = oXMLDoc.getElementsByTagName("item")[0].getAttribute("num");
  3476. iDebugNum = parseInt(oXMLDoc.getElementsByTagName("debug")[0].getAttribute("num"));
  3477. bIsComplete = parseInt(oXMLDoc.getElementsByTagName("debug")[0].getAttribute("complete"));
  3478. iSubStepProgress = parseFloat(oXMLDoc.getElementsByTagName("debug")[0].getAttribute("percent"));
  3479. sLastString = sDebugName + " (Item: " + iDebugNum + ")";
  3480. curFile = parseInt(oXMLDoc.getElementsByTagName("file")[0].getAttribute("num"));
  3481. debugItems = parseInt(oXMLDoc.getElementsByTagName("file")[0].getAttribute("debug_items"));
  3482. totalItems = parseInt(oXMLDoc.getElementsByTagName("file")[0].getAttribute("items"));
  3483. // If we have an error we haven\'t completed!
  3484. if (oXMLDoc.getElementsByTagName("error")[0] && bIsComplete)
  3485. iDebugNum = lastItem;
  3486. // Do we have the additional progress bar?
  3487. if (iSubStepProgress != -1)
  3488. {
  3489. document.getElementById("substep_bar_div").style.display = "";
  3490. document.getElementById("substep_bar_div2").style.display = "";
  3491. document.getElementById("substep_progress").style.width = iSubStepProgress + "%";
  3492. setInnerHTML(document.getElementById("substep_text"), iSubStepProgress + "%");
  3493. setInnerHTML(document.getElementById("substep_bar_div"), sDebugName.replace(/\./g, "") + ":");
  3494. }
  3495. else
  3496. {
  3497. document.getElementById("substep_bar_div").style.display = "none";
  3498. document.getElementById("substep_bar_div2").style.display = "none";
  3499. }
  3500. // Move onto the next item?
  3501. if (bIsComplete)
  3502. lastItem = iDebugNum;
  3503. else
  3504. lastItem = iDebugNum - 1;
  3505. // Are we finished?
  3506. if (bIsComplete && iDebugNum == -1 && curFile >= ', $upcontext['file_count'], ')
  3507. {';
  3508. if ($is_debug)
  3509. echo '
  3510. document.getElementById(\'debug_section\').style.display = "none";';
  3511. echo '
  3512. document.getElementById(\'commess\').style.display = "";
  3513. document.getElementById(\'contbutt\').disabled = 0;
  3514. document.getElementById(\'database_done\').value = 1;';
  3515. if ($upcontext['file_count'] > 1)
  3516. echo '
  3517. document.getElementById(\'info1\').style.display = "none";';
  3518. echo '
  3519. document.getElementById(\'info2\').style.display = "none";
  3520. updateStepProgress(100, 100, ', $upcontext['step_weight'] * ((100 - $upcontext['step_progress']) / 100), ');
  3521. return true;
  3522. }
  3523. // Was it the last step in the file?
  3524. else if (bIsComplete && iDebugNum == -1)
  3525. {
  3526. lastItem = 0;
  3527. prevFile = curFile;';
  3528. if ($is_debug)
  3529. echo '
  3530. setOuterHTML(document.getElementById(\'debuginfo\'), \'Moving to next script file...done<br /><span id="debuginfo"><\' + \'/span>\');';
  3531. echo '
  3532. getNextItem();
  3533. return true;
  3534. }';
  3535. // If debug scroll the screen.
  3536. if ($is_debug)
  3537. echo '
  3538. if (iLastSubStepProgress == -1)
  3539. {
  3540. // Give it consistent dots.
  3541. dots = sDebugName.match(/\./g);
  3542. numDots = dots ? dots.length : 0;
  3543. for (var i = numDots; i < 3; i++)
  3544. sDebugName += ".";
  3545. setOuterHTML(document.getElementById(\'debuginfo\'), sDebugName + \'<span id="debuginfo"><\' + \'/span>\');
  3546. }
  3547. iLastSubStepProgress = iSubStepProgress;
  3548. if (bIsComplete)
  3549. setOuterHTML(document.getElementById(\'debuginfo\'), \'done<br /><span id="debuginfo"><\' + \'/span>\');
  3550. else
  3551. setOuterHTML(document.getElementById(\'debuginfo\'), \'...<span id="debuginfo"><\' + \'/span>\');
  3552. if (document.getElementById(\'debug_section\').scrollHeight)
  3553. document.getElementById(\'debug_section\').scrollTop = document.getElementById(\'debug_section\').scrollHeight';
  3554. echo '
  3555. // Update the page.
  3556. setInnerHTML(document.getElementById(\'item_num\'), iItemNum);
  3557. setInnerHTML(document.getElementById(\'cur_item_name\'), sItemName);';
  3558. if ($upcontext['file_count'] > 1)
  3559. {
  3560. echo '
  3561. setInnerHTML(document.getElementById(\'file_done\'), curFile);
  3562. setInnerHTML(document.getElementById(\'item_count\'), totalItems);';
  3563. }
  3564. echo '
  3565. // Is there an error?
  3566. if (oXMLDoc.getElementsByTagName("error")[0])
  3567. {
  3568. var sErrorMsg = "";
  3569. for (var i = 0; i < oXMLDoc.getElementsByTagName("error")[0].childNodes.length; i++)
  3570. sErrorMsg += oXMLDoc.getElementsByTagName("error")[0].childNodes[i].nodeValue;
  3571. document.getElementById("error_block").style.display = "";
  3572. setInnerHTML(document.getElementById("error_message"), sErrorMsg);
  3573. return false;
  3574. }
  3575. // Get the progress bar right.
  3576. barTotal = debugItems * ', $upcontext['file_count'], ';
  3577. barDone = (debugItems * (curFile - 1)) + lastItem;
  3578. updateStepProgress(barDone, barTotal, ', $upcontext['step_weight'] * ((100 - $upcontext['step_progress']) / 100), ');
  3579. // Finally - update the time here as it shows the server is responding!
  3580. curTime = new Date();
  3581. iElapsed = (curTime.getTime() / 1000 - ', $upcontext['started'], ');
  3582. mins = parseInt(iElapsed / 60);
  3583. secs = parseInt(iElapsed - mins * 60);
  3584. setInnerHTML(document.getElementById("mins_elapsed"), mins);
  3585. setInnerHTML(document.getElementById("secs_elapsed"), secs);
  3586. getNextItem();
  3587. return true;
  3588. }
  3589. // What if we timeout?!
  3590. function retTimeout(attemptAgain)
  3591. {
  3592. // Oh noes...
  3593. if (!attemptAgain)
  3594. {
  3595. document.getElementById("error_block").style.display = "";
  3596. setInnerHTML(document.getElementById("error_message"), "Server has not responded for ', ($timeLimitThreshold * 10), ' seconds. It may be worth waiting a little longer or otherwise please click <a href=\"#\" onclick=\"retTimeout(true); return false;\">here<" + "/a> to try this step again");
  3597. }
  3598. else
  3599. {
  3600. document.getElementById("error_block").style.display = "none";
  3601. getNextItem();
  3602. }
  3603. }';
  3604. // Start things off assuming we've not errored.
  3605. if (empty($upcontext['error_message']))
  3606. echo '
  3607. getNextItem();';
  3608. echo '
  3609. // ]]></script>';
  3610. }
  3611. return;
  3612. }
  3613. function template_database_xml()
  3614. {
  3615. global $upcontext, $settings, $options, $txt;
  3616. echo '
  3617. <file num="', $upcontext['cur_file_num'], '" items="', $upcontext['total_items'], '" debug_items="', $upcontext['debug_items'], '">', $upcontext['cur_file_name'], '</file>
  3618. <item num="', $upcontext['current_item_num'], '">', $upcontext['current_item_name'], '</item>
  3619. <debug num="', $upcontext['current_debug_item_num'], '" percent="', isset($upcontext['substep_progress']) ? $upcontext['substep_progress'] : '-1', '" complete="', empty($upcontext['completed_step']) ? 0 : 1, '">', $upcontext['current_debug_item_name'], '</debug>';
  3620. if (!empty($upcontext['error_message']))
  3621. echo '
  3622. <error>', $upcontext['error_message'], '</error>';
  3623. }
  3624. function template_clean_mods()
  3625. {
  3626. global $upcontext, $modSettings, $upgradeurl, $disable_security, $settings, $db_prefix, $boardurl;
  3627. $upcontext['chmod_in_form'] = true;
  3628. echo '
  3629. <h3>ELKARTE has detected some packages which were installed but not fully removed prior to upgrade. We recommend you remove the following mods and reinstall upon completion of the upgrade.</h3>
  3630. <form action="', $upcontext['form_url'], '&amp;ssi=1" name="upform" id="upform" method="post">';
  3631. // In case it's required.
  3632. template_chmod();
  3633. echo '
  3634. <table width="90%" align="center" cellspacing="1" cellpadding="2" style="background-color: black;">
  3635. <tr style="background-color: #eeeeee;">
  3636. <td width="40%"><strong>Modification Name</strong></td>
  3637. <td width="10%" align="center"><strong>Version</strong></td>
  3638. <td width="15%"><strong>Files Affected</strong></td>
  3639. <td width="20%"><strong>Status</strong></td>
  3640. <td width="5%" align="center"><strong>Fix?</strong></td>
  3641. </tr>';
  3642. foreach ($upcontext['packages'] as $package)
  3643. {
  3644. echo '
  3645. <tr style="background-color: #cccccc;">
  3646. <td width="40%">', $package['name'], '</td>
  3647. <td width="10%">', $package['version'], '</td>
  3648. <td width="15%">', $package['file_count'], ' <span class="smalltext">[<a href="#" onclick="alert(\'The following files are affected by this modification:\\n\\n', strtr(implode('<br />', $package['files']), array('\\' => '\\\\', '<br />' => '\\n')), '\'); return false;">details</a>]</td>
  3649. <td width="20%"><span style="font-weight: bold; color: ', $package['color'], '">', $package['status'], '</span></td>
  3650. <td width="5%" align="center">
  3651. <input type="hidden" name="remove[', $package['id'], ']" value="0" />
  3652. <input type="checkbox" name="remove[', $package['id'], ']"', $package['color'] == 'green' ? ' disabled="disabled"' : '', ' class="input_check" />
  3653. </td>
  3654. </tr>';
  3655. }
  3656. echo '
  3657. </table>
  3658. <input type="hidden" name="cleandone" value="1" />';
  3659. // Files to make writable?
  3660. if (!empty($upcontext['writable_files']))
  3661. echo '
  3662. <input type="hidden" name="writable_files" value="', base64_encode(serialize($upcontext['writable_files'])), '" />';
  3663. // We'll want a continue button...
  3664. if (empty($upcontext['chmod']['files']))
  3665. $upcontext['continue'] = 1;
  3666. }
  3667. // Finished with the mods - let them know what we've done.
  3668. function template_cleanup_done()
  3669. {
  3670. global $upcontext, $modSettings, $upgradeurl, $disable_security, $settings, $db_prefix, $boardurl;
  3671. echo '
  3672. <h3>ELKARTE has attempted to fix and reinstall mods as required. We recommend you visit the package manager upon completing upgrade to check the status of your modifications.</h3>
  3673. <form action="', $upcontext['form_url'], '&amp;ssi=1" name="upform" id="upform" method="post">
  3674. <table width="90%" align="center" cellspacing="1" cellpadding="2" style="background-color: black;">
  3675. <tr style="background-color: #eeeeee;">
  3676. <td width="100%"><strong>Actions Completed:</strong></td>
  3677. </tr>';
  3678. foreach ($upcontext['packages'] as $package)
  3679. {
  3680. echo '
  3681. <tr style="background-color: #cccccc;">
  3682. <td>', $package['name'], '... <span style="font-weight: bold; color: ', $package['color'], ';">', $package['result'], '</span></td>
  3683. </tr>';
  3684. }
  3685. echo '
  3686. </table>
  3687. <input type="hidden" name="cleandone2" value="1" />';
  3688. // We'll want a continue button...
  3689. $upcontext['continue'] = 1;
  3690. }
  3691. // Do they want to upgrade their templates?
  3692. function template_upgrade_templates()
  3693. {
  3694. global $upcontext, $modSettings, $upgradeurl, $disable_security, $settings, $db_prefix, $boardurl;
  3695. echo '
  3696. <h3>There have been numerous language and template changes since the previous version of ELKARTE. On this step the upgrader can attempt to automatically make these changes in your templates to save you from doing so manually.</h3>
  3697. <form action="', $upcontext['form_url'], '&amp;ssi=1', $upcontext['is_test'] ? '' : ';forreal=1', '" name="upform" id="upform" method="post">';
  3698. // Any files need to be writable?
  3699. $upcontext['chmod_in_form'] = true;
  3700. template_chmod();
  3701. // Language/Template files need an update?
  3702. if ($upcontext['temp_progress'] == 0 && !$upcontext['is_test'] && (!empty($upcontext['languages']) || !empty($upcontext['themes'])))
  3703. {
  3704. echo '
  3705. The following template files will be updated to ensure they are compatible with this version of ELKARTE. Note that this can only fix a limited number of compatibility issues and in general you should seek out the latest version of these themes/language files.
  3706. <table width="90%" align="center" cellspacing="1" cellpadding="2" style="background-color: black;">
  3707. <tr style="background-color: #eeeeee;">
  3708. <td width="80%"><strong>Area</strong></td>
  3709. <td width="20%" align="center"><strong>Changes Required</strong></td>
  3710. </tr>';
  3711. foreach ($upcontext['languages'] as $language)
  3712. {
  3713. echo '
  3714. <tr style="background-color: #cccccc;">
  3715. <td width="80%">
  3716. &quot;', $language['name'], '&quot; Language Pack
  3717. <div class="smalltext">(';
  3718. foreach ($language['files'] as $k => $file)
  3719. echo $file['name'], $k + 1 != count($language['files']) ? ', ' : ')';
  3720. echo '
  3721. </div>
  3722. </td>
  3723. <td width="20%" align="center">', $language['edit_count'] == 0 ? 1 : $language['edit_count'], '</td>
  3724. </tr>';
  3725. }
  3726. foreach ($upcontext['themes'] as $theme)
  3727. {
  3728. echo '
  3729. <tr style="background-color: #CCCCCC;">
  3730. <td width="80%">
  3731. &quot;', $theme['name'], '&quot; Theme
  3732. <div class="smalltext">(';
  3733. foreach ($theme['files'] as $k => $file)
  3734. echo $file['name'], $k + 1 != count($theme['files']) ? ', ' : ')';
  3735. echo '
  3736. </div>
  3737. </td>
  3738. <td width="20%" align="center">', $theme['edit_count'] == 0 ? 1 : $theme['edit_count'], '</td>
  3739. </tr>';
  3740. }
  3741. echo '
  3742. </table>';
  3743. }
  3744. else
  3745. {
  3746. $langFiles = 0;
  3747. $themeFiles = 0;
  3748. if (!empty($upcontext['languages']))
  3749. foreach ($upcontext['languages'] as $lang)
  3750. $langFiles += count($lang['files']);
  3751. if (!empty($upcontext['themes']))
  3752. foreach ($upcontext['themes'] as $theme)
  3753. $themeFiles += count($theme['files']);
  3754. echo sprintf('Found <strong>%d</strong> language files and <strong>%d</strong> templates requiring an update so far.', $langFiles, $themeFiles) . '<br />';
  3755. // What we're currently doing?
  3756. if (!empty($upcontext['current_message']))
  3757. echo '
  3758. ', $upcontext['current_message'];
  3759. }
  3760. echo '
  3761. <input type="hidden" name="uptempdone" value="1" />';
  3762. if (!empty($upcontext['languages']))
  3763. echo '
  3764. <input type="hidden" name="languages" value="', base64_encode(serialize($upcontext['languages'])), '" />';
  3765. if (!empty($upcontext['themes']))
  3766. echo '
  3767. <input type="hidden" name="themes" value="', base64_encode(serialize($upcontext['themes'])), '" />';
  3768. if (!empty($upcontext['writable_files']))
  3769. echo '
  3770. <input type="hidden" name="writable_files" value="', base64_encode(serialize($upcontext['writable_files'])), '" />';
  3771. // Offer them the option to upgrade from YaBB SE?
  3772. if (!empty($upcontext['can_upgrade_yabbse']))
  3773. echo '
  3774. <br /><label for="conv"><input type="checkbox" name="conv" id="conv" value="1" class="input_check" /> Convert the existing YaBB SE template and set it as default.</label><br />';
  3775. // We'll want a continue button... assuming chmod is OK (Otherwise let them use connect!)
  3776. if (empty($upcontext['chmod']['files']) || $upcontext['is_test'])
  3777. $upcontext['continue'] = 1;
  3778. }
  3779. function template_upgrade_complete()
  3780. {
  3781. global $upcontext, $modSettings, $upgradeurl, $disable_security, $settings, $db_prefix, $boardurl;
  3782. echo '
  3783. <h3>That wasn\'t so hard, was it? Now you are ready to use <a href="', $boardurl, '/index.php">your installation of ELKARTE</a>. Hope you like it!</h3>
  3784. <form action="', $boardurl, '/index.php">';
  3785. if (!empty($upcontext['can_delete_script']))
  3786. echo '
  3787. <label for="delete_self"><input type="checkbox" id="delete_self" onclick="doTheDelete(this);" class="input_check" /> Delete this upgrade.php and its data files now.</label> <em>(doesn\'t work on all servers.)</em>
  3788. <script type="text/javascript"><!-- // --><![CDATA[
  3789. function doTheDelete(theCheck)
  3790. {
  3791. var theImage = document.getElementById ? document.getElementById("delete_upgrader") : document.all.delete_upgrader;
  3792. theImage.src = "', $upgradeurl, '?delete=1&ts_" + (new Date().getTime());
  3793. theCheck.disabled = true;
  3794. }
  3795. // ]]></script>
  3796. <img src="', $settings['default_theme_url'], '/images/blank.png" alt="" id="delete_upgrader" /><br />';
  3797. echo '<br />
  3798. If you had any problems with this upgrade, or have any problems using Elkarte, please don\'t hesitate to <a href="http://www.elkarte.net/index.php">look to us for assistance</a>.<br />
  3799. <br />
  3800. Best of luck,<br />
  3801. Elkarte';
  3802. }