Guest User

Untitled

a guest
May 9th, 2012
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.79 KB | None | 0 0
  1. <?php
  2. // Installation/upgrade file
  3. define('VERSION', 'v0.9.3');
  4.  
  5. require 'inc/functions.php';
  6. require 'inc/display.php';
  7. require 'inc/template.php';
  8. require 'inc/database.php';
  9. require 'inc/user.php';
  10. $step = isset($_GET['step']) ? round($_GET['step']) : 0;
  11. $page = Array(
  12. 'config' => $config,
  13. 'title' => 'Install',
  14. 'body' => ''
  15. );
  16.  
  17. if(file_exists($config['has_installed'])) {
  18.  
  19. // Check the version number
  20. $version = trim(file_get_contents($config['has_installed']));
  21. if(empty($version))
  22. $version = 'v0.9.1';
  23.  
  24. switch($version) {
  25. case 'v0.9':
  26. case 'v0.9.1':
  27. // Upgrade to v0.9.2-dev
  28.  
  29. $boards = listBoards();
  30. foreach($boards as &$_board) {
  31. // Add `capcode` field after `trip`
  32. query(sprintf("ALTER TABLE `posts_%s` ADD `capcode` VARCHAR( 50 ) NULL AFTER `trip`", $_board['uri'])) or error(db_error());
  33.  
  34. // Resize `trip` to 15 characters
  35. query(sprintf("ALTER TABLE `posts_%s` CHANGE `trip` `trip` VARCHAR( 15 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL", $_board['uri'])) or error(db_error());
  36. }
  37. case 'v0.9.2-dev':
  38. // Upgrade to v0.9.2-dev-1
  39.  
  40. // New table: `theme_settings`
  41. query("CREATE TABLE IF NOT EXISTS `theme_settings` ( `name` varchar(40) NOT NULL, `value` text, UNIQUE KEY `name` (`name`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;") or error(db_error());
  42.  
  43. // New table: `news`
  44. query("CREATE TABLE IF NOT EXISTS `news` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` text NOT NULL, `time` int(11) NOT NULL, `subject` text NOT NULL, `body` text NOT NULL, UNIQUE KEY `id` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;") or error(db_error());
  45. case 'v0.9.2.1-dev':
  46. case 'v0.9.2-dev-1':
  47. // Fix broken version number/mistake
  48. $version = 'v0.9.2-dev-1';
  49. // Upgrade to v0.9.2-dev-2
  50.  
  51. $boards = listBoards();
  52. foreach($boards as &$_board) {
  53. // Increase field sizes
  54. query(sprintf("ALTER TABLE `posts_%s` CHANGE `subject` `subject` VARCHAR( 50 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL", $_board['uri'])) or error(db_error());
  55. query(sprintf("ALTER TABLE `posts_%s` CHANGE `name` `name` VARCHAR( 35 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL", $_board['uri'])) or error(db_error());
  56. }
  57. case 'v0.9.2-dev-2':
  58. // Upgrade to v0.9.2-dev-3 (v0.9.2)
  59.  
  60. $boards = listBoards();
  61. foreach($boards as &$_board) {
  62. // Add `custom_fields` field
  63. query(sprintf("ALTER TABLE `posts_%s` ADD `embed` TEXT NULL", $_board['uri'])) or error(db_error());
  64. }
  65. case 'v0.9.2-dev-3': // v0.9.2-dev-3 == v0.9.2
  66. case 'v0.9.2':
  67. // Upgrade to v0.9.3-dev-1
  68.  
  69. // Upgrade `theme_settings` table
  70. query("TRUNCATE TABLE `theme_settings`") or error(db_error());
  71. query("ALTER TABLE `theme_settings` ADD `theme` VARCHAR( 40 ) NOT NULL FIRST") or error(db_error());
  72. query("ALTER TABLE `theme_settings` CHANGE `name` `name` VARCHAR( 40 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL") or error(db_error());
  73. query("ALTER TABLE `theme_settings` DROP INDEX `name`") or error(db_error());
  74. case 'v0.9.3-dev-1':
  75. query("ALTER TABLE `mods` ADD `boards` TEXT NOT NULL") or error(db_error());
  76. query("UPDATE `mods` SET `boards` = '*'") or error(db_error());
  77. case 'v0.9.3-dev-2':
  78. $boards = listBoards();
  79. foreach($boards as &$_board) {
  80. query(sprintf("ALTER TABLE `posts_%s` CHANGE `filehash` `filehash` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL", $_board['uri'])) or error(db_error());
  81. }
  82. case 'v0.9.3-dev-3':
  83. // Board-specifc bans
  84. query("ALTER TABLE `bans` ADD `board` SMALLINT NULL AFTER `reason`") or error(db_error());
  85. case 'v0.9.3-dev-4':
  86. // add ban ID
  87. query("ALTER TABLE `bans` ADD `id` INT NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY ( `id` ), ADD UNIQUE (`id`)");
  88. case 'v0.9.3-dev-5':
  89. $boards = listBoards();
  90. foreach($boards as &$_board) {
  91. // Increase subject field size
  92. query(sprintf("ALTER TABLE `posts_%s` CHANGE `subject` `subject` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL", $_board['uri'])) or error(db_error());
  93. }
  94. case 'v0.9.3-dev-6':
  95. // change to MyISAM
  96. $tables = Array(
  97. 'bans', 'boards', 'ip_notes', 'modlogs', 'mods', 'mutes', 'noticeboard', 'pms', 'reports', 'robot', 'theme_settings', 'news'
  98. );
  99. $boards = listBoards();
  100. foreach($boards as &$board) {
  101. $tables[] = "posts_{$board['uri']}";
  102. }
  103.  
  104. foreach($tables as &$table) {
  105. query("ALTER TABLE `{$table}` ENGINE = MYISAM DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci") or error(db_error());
  106. }
  107. case 'v0.9.3-dev-7':
  108. $boards = listBoards();
  109. foreach($boards as &$board) {
  110. query(sprintf("ALTER TABLE `posts_%s` CHANGE `filename` `filename` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL", $board['uri'])) or error(db_error());
  111. }
  112. case 'v0.9.3-dev-8':
  113. $boards = listBoards();
  114. foreach($boards as &$board) {
  115. query(sprintf("ALTER TABLE `posts_%s` ADD INDEX ( `thread` )", $board['uri'])) or error(db_error());
  116. }
  117. case 'v0.9.3-dev-9':
  118. $boards = listBoards();
  119. foreach($boards as &$board) {
  120. query(sprintf("ALTER TABLE `posts_%s`ADD INDEX ( `time` )", $board['uri'])) or error(db_error());
  121. query(sprintf("ALTER TABLE `posts_%s`ADD FULLTEXT (`body`)", $board['uri'])) or error(db_error());
  122. }
  123. case false:
  124. // Update version number
  125. file_write($config['has_installed'], VERSION);
  126.  
  127. $page['title'] = 'Upgraded';
  128. $page['body'] = '<p style="text-align:center">Successfully upgraded from ' . $version . ' to <strong>' . VERSION . '</strong>.</p>';
  129. break;
  130. default:
  131. $page['title'] = 'Unknown version';
  132. $page['body'] = '<p style="text-align:center">Tinyboard was unable to determine what version is currently installed.</p>';
  133. break;
  134. case VERSION:
  135. $page['title'] = 'Already installed';
  136. $page['body'] = '<p style="text-align:center">It appears that Tinyboard is already installed (' . $version . ') and there is nothing to upgrade! Delete <strong>' . $config['has_installed'] . '</strong> to reinstall.</p>';
  137. break;
  138. }
  139.  
  140. die(Element('page.html', $page));
  141. }
  142.  
  143. if($step == 0) {
  144. // Agreeement
  145. $page['body'] = '
  146. <textarea style="width:700px;height:370px;margin:auto;display:block;background:white;color:black" disabled>' . htmlentities(file_get_contents('LICENSE.md')) . '</textarea>
  147. <p style="text-align:center">
  148. <a href="?step=1">I have read and understood the agreement. Proceed to installation.</a>
  149. </p>';
  150.  
  151. echo Element('page.html', $page);
  152. } elseif($step == 1) {
  153. $page['title'] = 'Pre-installation test';
  154.  
  155. $page['body'] = '<table class="test">';
  156.  
  157. function rheader($item) {
  158. global $page, $config;
  159.  
  160. $page['body'] .= '<tr class="h"><th colspan="2">' . $item . '</th></tr>';
  161. }
  162.  
  163. function row($item, $result) {
  164. global $page, $config, $__is_error;
  165. if(!$result)
  166. $__is_error = true;
  167. $page['body'] .= '<tr><th>' . $item . '</th><td><img style="width:16px;height:16px" src="' . $config['dir']['static'] . ($result ? 'ok.png' : 'error.png') . '" /></td></tr>';
  168. }
  169.  
  170.  
  171. // Required extensions
  172. rheader('PHP extensions');
  173. row('PDO', extension_loaded('pdo'));
  174. row('GD', extension_loaded('gd'));
  175.  
  176. // GD tests
  177. rheader('GD tests');
  178. row('JPEG', function_exists('imagecreatefromjpeg'));
  179. row('PNG', function_exists('imagecreatefrompng'));
  180. row('GIF', function_exists('imagecreatefromgif'));
  181.  
  182. // Database drivers
  183. $drivers = PDO::getAvailableDrivers();
  184.  
  185. rheader('PDO drivers <em>(currently installed drivers)</em>');
  186. foreach($drivers as &$driver) {
  187. row($driver, true);
  188. }
  189.  
  190. // Permissions
  191. rheader('File permissions');
  192. row('<em>root directory</em> (' . getcwd() . ')', is_writable('.'));
  193.  
  194. $page['body'] .= '</table>
  195. <p style="text-align:center">
  196. <a href="?step=2"' .
  197. (isset($__is_error) ? ' onclick="return confirm(\'Are you sure you want to continue when errors exist?\')"' : '') .
  198. '>Continue' . (isset($__is_error) ? ' anyway' : '') . '.</a>
  199. </p>';
  200.  
  201. echo Element('page.html', $page);
  202. } elseif($step == 2) {
  203. // Basic config
  204. $page['title'] = 'Configuration';
  205.  
  206. function create_salt() {
  207. return substr(base64_encode(sha1(rand())), 0, rand(25, 31));
  208. }
  209.  
  210. $page['body'] = '
  211. <form action="?step=3" method="post">
  212. <fieldset>
  213. <legend>Database</legend>
  214. <label for="db_type">Type:</label>
  215. <select id="db_type" name="db[type]">';
  216.  
  217. $drivers = PDO::getAvailableDrivers();
  218.  
  219. foreach($drivers as &$driver) {
  220. $driver_txt = $driver;
  221. switch($driver) {
  222. case 'cubrid':
  223. $driver_txt = 'Cubrid';
  224. break;
  225. case 'dblib':
  226. $driver_txt = 'FreeTDS / Microsoft SQL Server / Sybase';
  227. break;
  228. case 'firebird':
  229. $driver_txt = 'Firebird/Interbase 6';
  230. break;
  231. case 'ibm':
  232. $driver_txt = 'IBM DB2';
  233. break;
  234. case 'informix':
  235. $driver_txt = 'IBM Informix Dynamic Server';
  236. break;
  237. case 'mysql':
  238. $driver_txt = 'MySQL';
  239. break;
  240. case 'oci':
  241. $driver_txt = 'OCI';
  242. break;
  243. case 'odbc':
  244. $driver_txt = 'ODBC v3 (IBM DB2, unixODBC)';
  245. break;
  246. case 'pgsql':
  247. $driver_txt = 'PostgreSQL';
  248. break;
  249. case 'sqlite':
  250. $driver_txt = 'SQLite 3';
  251. break;
  252. case 'sqlite2':
  253. $driver_txt = 'SQLite 2';
  254. break;
  255. }
  256. $page['body'] .= '<option value="' . $driver . '">' . $driver_txt . '</option>';
  257. }
  258.  
  259. $page['body'] .= '
  260. </select>
  261.  
  262. <label for="db_server">Server:</label>
  263. <input type="text" id="db_server" name="db[server]" value="localhost" />
  264.  
  265. <label for="db_db">Database:</label>
  266. <input type="text" id="db_db" name="db[database]" value="" />
  267.  
  268. <label for="db_user">Username:</label>
  269. <input type="text" id="db_user" name="db[user]" value="" />
  270.  
  271. <label for="db_pass">Password:</label>
  272. <input type="password" id="db_pass" name="db[password]" value="" />
  273. </fieldset>
  274. <p style="text-align:center" class="unimportant">The following is all later configurable. For more options, <a href="http://tinyboard.org/wiki/index.php?title=Config">edit your configuration files</a> after installing.</p>
  275. <fieldset>
  276. <legend>Cookies</legend>
  277. <label for="cookies_session">Name of session cookie:</label>
  278. <input type="text" id="cookies_session" name="cookies[session]" value="' . session_name() . '" />
  279.  
  280. <label for="cookies_time">Cookie containing a timestamp of first arrival:</label>
  281. <input type="text" id="cookies_time" name="cookies[time]" value="' . $config['cookies']['time'] . '" />
  282.  
  283. <label for="cookies_hash">Cookie containing a hash for verification purposes:</label>
  284. <input type="text" id="cookies_hash" name="cookies[hash]" value="' . $config['cookies']['hash'] . '" />
  285.  
  286. <label for="cookies_mod">Moderator cookie:</label>
  287. <input type="text" id="cookies_mod" name="cookies[mod]" value="' . $config['cookies']['mod'] . '" />
  288.  
  289. <label for="cookies_salt">Secure salt:</label>
  290. <input type="text" id="cookies_salt" name="cookies[salt]" value="' . create_salt() . '" size="40" />
  291. </fieldset>
  292.  
  293. <fieldset>
  294. <legend>Flood control</legend>
  295. <label for="flood_time">Seconds before each post:</label>
  296. <input type="text" id="flood_time" name="flood_time" value="' . $config['flood_time'] . '" />
  297.  
  298. <label for="flood_time_ip">Seconds before you can repost something (post the exact same text):</label>
  299. <input type="text" id="flood_time_ip" name="flood_time_ip" value="' . $config['flood_time_ip'] . '" />
  300.  
  301. <label for="flood_time_same">Same as above, but with a different IP address:</label>
  302. <input type="text" id="flood_time_same" name="flood_time_same" value="' . $config['flood_time_same'] . '" />
  303.  
  304. <label for="max_body">Maximum post body length:</label>
  305. <input type="text" id="max_body" name="max_body" value="' . $config['max_body'] . '" />
  306.  
  307. <label for="reply_limit">Replies in a thread before it can no longer be bumped:</label>
  308. <input type="text" id="reply_limit" name="reply_limit" value="' . $config['reply_limit'] . '" />
  309.  
  310. <label for="max_links">Maximum number of links in a single post:</label>
  311. <input type="text" id="max_links" name="max_links" value="' . $config['max_links'] . '" />
  312. </fieldset>
  313.  
  314. <fieldset>
  315. <legend>Images</legend>
  316. <label for="max_filesize">Maximum image filesize:</label>
  317. <input type="text" id="max_filesize" name="max_filesize" value="' . $config['max_filesize'] . '" />
  318.  
  319. <label for="thumb_width">Thumbnail width:</label>
  320. <input type="text" id="thumb_width" name="thumb_width" value="' . $config['thumb_width'] . '" />
  321.  
  322. <label for="thumb_height">Thumbnail height:</label>
  323. <input type="text" id="thumb_height" name="thumb_height" value="' . $config['thumb_height'] . '" />
  324.  
  325. <label for="max_width">Maximum image width:</label>
  326. <input type="text" id="max_width" name="max_width" value="' . $config['max_width'] . '" />
  327.  
  328. <label for="max_height">Maximum image height:</label>
  329. <input type="text" id="max_height" name="max_height" value="' . $config['max_height'] . '" />
  330. </fieldset>
  331.  
  332. <fieldset>
  333. <legend>Display</legend>
  334. <label for="threads_per_page">Threads per page:</label>
  335. <input type="text" id="threads_per_page" name="threads_per_page" value="' . $config['threads_per_page'] . '" />
  336.  
  337. <label for="max_pages">Page limit:</label>
  338. <input type="text" id="max_pages" name="max_pages" value="' . $config['max_pages'] . '" />
  339.  
  340. <label for="threads_preview">Number of replies to show per thread on the index page:</label>
  341. <input type="text" id="threads_preview" name="threads_preview" value="' . $config['threads_preview'] . '" />
  342. </fieldset>
  343.  
  344. <fieldset>
  345. <legend>Directories</legend>
  346. <label for="root">Root URI (include trailing slash):</label>
  347. <input type="text" id="root" name="root" value="' . $config['root'] . '" />
  348.  
  349. <label for="dir_img">Image directory:</label>
  350. <input type="text" id="dir_img" name="dir[img]" value="' . $config['dir']['img'] . '" />
  351.  
  352. <label for="dir_thumb">Thumbnail directory:</label>
  353. <input type="text" id="dir_thumb" name="dir[thumb]" value="' . $config['dir']['thumb'] . '" />
  354.  
  355. <label for="dir_res">Thread directory:</label>
  356. <input type="text" id="dir_res" name="dir[res]" value="' . $config['dir']['res'] . '" />
  357. </fieldset>
  358.  
  359. <fieldset>
  360. <legend>Miscellaneous</legend>
  361. <label for="secure_trip_salt">Secure trip (##) salt:</label>
  362. <input type="text" id="secure_trip_salt" name="secure_trip_salt" value="' . create_salt() . '" size="40" />
  363. </fieldset>
  364.  
  365. <p style="text-align:center">
  366. <input type="submit" value="Complete installation" />
  367. </p>
  368. </form>
  369. ';
  370.  
  371.  
  372. echo Element('page.html', $page);
  373. } elseif($step == 3) {
  374. $instance_config =
  375. '<?php
  376.  
  377. /*
  378. * Instance Configuration
  379. * ----------------------
  380. * Edit this file and not config.php for imageboard configuration.
  381. *
  382. * You can copy values from config.php (defaults) and paste them here.
  383. */
  384.  
  385. ';
  386.  
  387. function create_config_from_array(&$instance_config, &$array, $prefix = '') {
  388. foreach($array as $name => $value) {
  389. if(is_array($value)) {
  390. $instance_config .= "\n";
  391. create_config_from_array($instance_config, $value, $prefix . '[\'' . addslashes($name) . '\']');
  392. $instance_config .= "\n";
  393. } else {
  394. $instance_config .= ' $config' . $prefix . '[\'' . addslashes($name) . '\'] = ';
  395.  
  396. if(is_numeric($value))
  397. $instance_config .= $value;
  398. else
  399. $instance_config .= "'" . addslashes($value) . "'";
  400.  
  401. $instance_config .= ";\n";
  402. }
  403. }
  404. }
  405.  
  406. create_config_from_array($instance_config, $_POST);
  407.  
  408. $instance_config .= '?>';
  409.  
  410. if(@file_put_contents('inc/instance-config.php', $instance_config)) {
  411. header('Location: ?step=4', true, $config['redirect_http']);
  412. } else {
  413. $page['title'] = 'Manual installation required';
  414. $page['body'] = '
  415. <p>I couldn\'t write to <strong>inc/instance-config.php</strong> with the new configuration, probably due to a permissions error.</p>
  416. <p>Please complete the installation manually by copying and pasting the following code into the contents of <strong>inc/instance-config.php</strong>:</p>
  417. <textarea style="width:700px;height:370px;margin:auto;display:block;background:white;color:black">' . htmlentities($instance_config) . '</textarea>
  418. <p style="text-align:center">
  419. <a href="?step=4">Once complete, click here to complete installation.</a>
  420. </p>
  421. ';
  422. echo Element('page.html', $page);
  423. }
  424. } elseif($step == 4) {
  425. // SQL installation
  426.  
  427. buildJavascript();
  428.  
  429. $sql = @file_get_contents('install.sql') or error("Couldn't load install.sql.");
  430.  
  431. // This code is probably horrible, but what I'm trying
  432. // to do is find all of the SQL queires and put them
  433. // in an array.
  434. preg_match_all("/(^|\n)((SET|CREATE|INSERT).+)\n\n/msU", $sql, $queries);
  435. $queries = $queries[2];
  436.  
  437. $sql_errors = '';
  438. foreach($queries as &$query) {
  439. if(!query($query))
  440. $sql_errors .= '<li>' . db_error() . '</li>';
  441. }
  442.  
  443. $boards = listBoards();
  444. foreach($boards as &$_board) {
  445. setupBoard($_board);
  446. buildIndex();
  447. }
  448.  
  449. $page['title'] = 'Installation complete';
  450. $page['body'] = '<p style="text-align:center">Thank you for using Tinyboard. Please remember to report any bugs you discover.</p>';
  451.  
  452. if(!empty($sql_errors)) {
  453. $page['body'] .= '<div class="ban"><h2>SQL errors</h2><p>SQL errors were encountered when trying to install the database. This may be the result of using a database which is already occupied with a Tinyboard installation; if so, you can probably ignore this.</p><p>The errors encountered were:</p><ul>' . $sql_errors . '</ul><p><a href="?step=5">Ignore errors and complete installation.</a></p></div>';
  454. } else {
  455. file_write($config['has_installed'], VERSION);
  456. if(!file_unlink(__FILE__)) {
  457. $page['body'] .= '<div class="ban"><h2>Delete install.php!</h2><p>I couldn\'t remove <strong>install.php</strong>. You will have to remove it manually.</p></div>';
  458. }
  459. }
  460.  
  461. echo Element('page.html', $page);
  462. } elseif($step == 5) {
  463. $page['title'] = 'Installation complete';
  464. $page['body'] = '<p style="text-align:center">Thank you for using Tinyboard. Please remember to report any bugs you discover.</p>';
  465.  
  466. file_write($config['has_installed'], VERSION);
  467. if(!file_unlink(__FILE__)) {
  468. $page['body'] .= '<div class="ban"><h2>Delete install.php!</h2><p>I couldn\'t remove <strong>install.php</strong>. You will have to remove it manually.</p></div>';
  469. }
  470.  
  471. echo Element('page.html', $page);
  472. }
  473. ?>
Add Comment
Please, Sign In to add comment