PageRenderTime 72ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/other/upgrade.php

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