PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/install.php

https://gitlab.com/dali99/shimmie2-Material-Theme
PHP | 471 lines | 427 code | 11 blank | 33 comment | 16 complexity | 3084dc2dca589d3853c8aa316db95790 MD5 | raw file
  1. <?php
  2. /**
  3. * Shimmie Installer
  4. *
  5. * @package Shimmie
  6. * @copyright Copyright (c) 2007-2015, Shish et al.
  7. * @author Shish <webmaster at shishnet.org>, jgen <jeffgenovy at gmail.com>
  8. * @link http://code.shishnet.org/shimmie2/
  9. * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
  10. *
  11. * Initialise the database, check that folder
  12. * permissions are set properly.
  13. *
  14. * This file should be independant of the database
  15. * and other such things that aren't ready yet
  16. */
  17. // TODO: Rewrite the entire installer and make it more readable.
  18. ob_start();
  19. date_default_timezone_set('UTC');
  20. ?>
  21. <!DOCTYPE html>
  22. <html>
  23. <head>
  24. <title>Shimmie Installation</title>
  25. <link rel="shortcut icon" href="favicon.ico" />
  26. <link rel='stylesheet' href='lib/shimmie.css' type='text/css'>
  27. <script type="text/javascript" src="lib/jquery-1.11.0.min.js"></script>
  28. </head>
  29. <body>
  30. <?php if(false) { ?>
  31. <div id="installer">
  32. <h1>Install Error</h1>
  33. <p>Shimmie needs to be run via a web server with PHP support -- you
  34. appear to be either opening the file from your hard disk, or your
  35. web server is mis-configured and doesn't know how to handle PHP files.</p>
  36. <p>If you've installed a web server on your desktop PC, you probably
  37. want to visit <a href="http://localhost/">the local web server</a>.<br/><br/>
  38. </p>
  39. </div>
  40. <div style="display: none;">
  41. <PLAINTEXT>
  42. <?php }
  43. assert_options(ASSERT_ACTIVE, 1);
  44. assert_options(ASSERT_BAIL, 1);
  45. /*
  46. * Compute the path to the folder containing "install.php" and
  47. * store it as the 'Shimmie Root' folder for later on.
  48. *
  49. * Example:
  50. * __SHIMMIE_ROOT__ = '/var/www/shimmie2/'
  51. *
  52. */
  53. define('__SHIMMIE_ROOT__', trim(remove_trailing_slash(dirname(__FILE__))) . '/');
  54. // Pull in necessary files
  55. require_once __SHIMMIE_ROOT__."core/util.inc.php";
  56. require_once __SHIMMIE_ROOT__."core/exceptions.class.php";
  57. require_once __SHIMMIE_ROOT__."core/database.class.php";
  58. if(is_readable("data/config/shimmie.conf.php")) die("Shimmie is already installed.");
  59. do_install();
  60. // utilities {{{
  61. // TODO: Can some of these be pushed into "core/util.inc.php" ?
  62. /**
  63. * Strips off any kind of slash at the end so as to normalise the path.
  64. * @param string $path Path to normalise.
  65. * @return string Path without trailing slash.
  66. */
  67. function remove_trailing_slash($path) {
  68. if ((substr($path, -1) === '/') || (substr($path, -1) === '\\')) {
  69. return substr($path, 0, -1);
  70. } else {
  71. return $path;
  72. }
  73. }
  74. function check_gd_version() {
  75. $gdversion = 0;
  76. if (function_exists('gd_info')){
  77. $gd_info = gd_info();
  78. if (substr_count($gd_info['GD Version'], '2.')) {
  79. $gdversion = 2;
  80. } else if (substr_count($gd_info['GD Version'], '1.')) {
  81. $gdversion = 1;
  82. }
  83. }
  84. return $gdversion;
  85. }
  86. function check_im_version() {
  87. if(!ini_get('safe_mode')) {
  88. $convert_check = exec("convert");
  89. }
  90. return (empty($convert_check) ? 0 : 1);
  91. }
  92. function eok($name, $value) {
  93. echo "<br>$name ... ";
  94. if($value) {
  95. echo "<font color='green'>ok</font>\n";
  96. }
  97. else {
  98. echo "<font color='red'>failed</font>\n";
  99. }
  100. }
  101. // }}}
  102. function do_install() { // {{{
  103. if(file_exists("data/config/auto_install.conf.php")) {
  104. require_once "data/config/auto_install.conf.php";
  105. }
  106. else if(@$_POST["database_type"] == "sqlite" && isset($_POST["database_name"])) {
  107. define('DATABASE_DSN', "sqlite:{$_POST["database_name"]}");
  108. }
  109. else if(isset($_POST['database_type']) && isset($_POST['database_host']) && isset($_POST['database_user']) && isset($_POST['database_name'])) {
  110. define('DATABASE_DSN', "{$_POST['database_type']}:user={$_POST['database_user']};password={$_POST['database_password']};host={$_POST['database_host']};dbname={$_POST['database_name']}");
  111. }
  112. else {
  113. ask_questions();
  114. return;
  115. }
  116. define("DATABASE_KA", true);
  117. install_process();
  118. } // }}}
  119. function ask_questions() { // {{{
  120. $warnings = array();
  121. $errors = array();
  122. if(check_gd_version() == 0 && check_im_version() == 0) {
  123. $errors[] = "
  124. No thumbnailers cound be found - install the imagemagick
  125. tools (or the PHP-GD library, of imagemagick is unavailable).
  126. ";
  127. }
  128. else if(check_im_version() == 0) {
  129. $warnings[] = "
  130. The 'convert' command (from the imagemagick package)
  131. could not be found - PHP-GD can be used instead, but
  132. the size of thumbnails will be limited.
  133. ";
  134. }
  135. $drivers = PDO::getAvailableDrivers();
  136. if(
  137. !in_array("mysql", $drivers) &&
  138. !in_array("pgsql", $drivers) &&
  139. !in_array("sqlite", $drivers)
  140. ) {
  141. $errors[] = "
  142. No database connection library could be found; shimmie needs
  143. PDO with either Postgres, MySQL, or SQLite drivers
  144. ";
  145. }
  146. $db_m = in_array("mysql", $drivers) ? '<option value="mysql">MySQL</option>' : "";
  147. $db_p = in_array("pgsql", $drivers) ? '<option value="pgsql">PostgreSQL</option>' : "";
  148. $db_s = in_array("sqlite", $drivers) ? '<option value="sqlite">SQLite</option>' : "";
  149. $warn_msg = $warnings ? "<h3>Warnings</h3>".implode("\n<br>", $warnings) : "";
  150. $err_msg = $errors ? "<h3>Errors</h3>".implode("\n<br>", $errors) : "";
  151. print <<<EOD
  152. <div id="installer">
  153. <h1>Shimmie Installer</h1>
  154. $warn_msg
  155. $err_msg
  156. <h3>Database Install</h3>
  157. <form action="install.php" method="POST">
  158. <center>
  159. <table class='form'>
  160. <tr>
  161. <th>Type:</th>
  162. <td><select name="database_type" id="database_type" onchange="update_qs();">
  163. $db_m
  164. $db_p
  165. $db_s
  166. </select></td>
  167. </tr>
  168. <tr class="dbconf mysql pgsql">
  169. <th>Host:</th>
  170. <td><input type="text" name="database_host" size="40" value="localhost"></td>
  171. </tr>
  172. <tr class="dbconf mysql pgsql">
  173. <th>Username:</th>
  174. <td><input type="text" name="database_user" size="40"></td>
  175. </tr>
  176. <tr class="dbconf mysql pgsql">
  177. <th>Password:</th>
  178. <td><input type="password" name="database_password" size="40"></td>
  179. </tr>
  180. <tr class="dbconf mysql pgsql sqlite">
  181. <th>DB&nbsp;Name:</th>
  182. <td><input type="text" name="database_name" size="40" value="shimmie"></td>
  183. </tr>
  184. <tr><td colspan="2"><input type="submit" value="Go!"></td></tr>
  185. </table>
  186. </center>
  187. <script>
  188. $(function() {
  189. update_qs();
  190. });
  191. function update_qs() {
  192. $(".dbconf").hide();
  193. var seldb = $("#database_type").val() || "none";
  194. $("."+seldb).show();
  195. }
  196. </script>
  197. </form>
  198. <h3>Help</h3>
  199. <p class="dbconf mysql pgsql">
  200. Please make sure the database you have chosen exists and is empty.<br>
  201. The username provided must have access to create tables within the database.
  202. </p>
  203. <p class="dbconf sqlite">
  204. For SQLite the database name will be a filename on disk, relative to
  205. where shimmie was installed.
  206. </p>
  207. <p class="dbconf none">
  208. Drivers can generally be downloaded with your OS package manager;
  209. for Debian / Ubuntu you want php5-pgsql, php5-mysql, or php5-sqlite.
  210. </p>
  211. </div>
  212. EOD;
  213. } // }}}
  214. /**
  215. * This is where the install really takes place.
  216. */
  217. function install_process() { // {{{
  218. build_dirs();
  219. create_tables();
  220. insert_defaults();
  221. write_config();
  222. } // }}}
  223. function create_tables() { // {{{
  224. try {
  225. $db = new Database();
  226. if ( $db->count_tables() > 0 ) {
  227. print <<<EOD
  228. <div id="installer">
  229. <h1>Shimmie Installer</h1>
  230. <h3>Warning: The Database schema is not empty!</h3>
  231. <p>Please ensure that the database you are installing Shimmie with is empty before continuing.</p>
  232. <p>Once you have emptied the database of any tables, please hit 'refresh' to continue.</p>
  233. <br/><br/>
  234. </div>
  235. EOD;
  236. exit(2);
  237. }
  238. $db->create_table("aliases", "
  239. oldtag VARCHAR(128) NOT NULL,
  240. newtag VARCHAR(128) NOT NULL,
  241. PRIMARY KEY (oldtag)
  242. ");
  243. $db->execute("CREATE INDEX aliases_newtag_idx ON aliases(newtag)", array());
  244. $db->create_table("config", "
  245. name VARCHAR(128) NOT NULL,
  246. value TEXT,
  247. PRIMARY KEY (name)
  248. ");
  249. $db->create_table("users", "
  250. id SCORE_AIPK,
  251. name VARCHAR(32) UNIQUE NOT NULL,
  252. pass VARCHAR(250),
  253. joindate SCORE_DATETIME NOT NULL DEFAULT SCORE_NOW,
  254. class VARCHAR(32) NOT NULL DEFAULT 'user',
  255. email VARCHAR(128)
  256. ");
  257. $db->execute("CREATE INDEX users_name_idx ON users(name)", array());
  258. $db->create_table("images", "
  259. id SCORE_AIPK,
  260. owner_id INTEGER NOT NULL,
  261. owner_ip SCORE_INET NOT NULL,
  262. filename VARCHAR(64) NOT NULL,
  263. filesize INTEGER NOT NULL,
  264. hash CHAR(32) UNIQUE NOT NULL,
  265. ext CHAR(4) NOT NULL,
  266. source VARCHAR(255),
  267. width INTEGER NOT NULL,
  268. height INTEGER NOT NULL,
  269. posted SCORE_DATETIME NOT NULL DEFAULT SCORE_NOW,
  270. locked SCORE_BOOL NOT NULL DEFAULT SCORE_BOOL_N,
  271. FOREIGN KEY (owner_id) REFERENCES users(id) ON DELETE RESTRICT
  272. ");
  273. $db->execute("CREATE INDEX images_owner_id_idx ON images(owner_id)", array());
  274. $db->execute("CREATE INDEX images_width_idx ON images(width)", array());
  275. $db->execute("CREATE INDEX images_height_idx ON images(height)", array());
  276. $db->execute("CREATE INDEX images_hash_idx ON images(hash)", array());
  277. $db->create_table("tags", "
  278. id SCORE_AIPK,
  279. tag VARCHAR(64) UNIQUE NOT NULL,
  280. count INTEGER NOT NULL DEFAULT 0
  281. ");
  282. $db->execute("CREATE INDEX tags_tag_idx ON tags(tag)", array());
  283. $db->create_table("image_tags", "
  284. image_id INTEGER NOT NULL,
  285. tag_id INTEGER NOT NULL,
  286. UNIQUE(image_id, tag_id),
  287. FOREIGN KEY (image_id) REFERENCES images(id) ON DELETE CASCADE,
  288. FOREIGN KEY (tag_id) REFERENCES tags(id) ON DELETE CASCADE
  289. ");
  290. $db->execute("CREATE INDEX images_tags_image_id_idx ON image_tags(image_id)", array());
  291. $db->execute("CREATE INDEX images_tags_tag_id_idx ON image_tags(tag_id)", array());
  292. $db->execute("INSERT INTO config(name, value) VALUES('db_version', 11)");
  293. $db->commit();
  294. }
  295. catch(PDOException $e) {
  296. print <<<EOD
  297. <div id="installer">
  298. <h1>Shimmie Installer</h1>
  299. <h3>Database Error:</h3>
  300. <p>An error occured while trying to create the database tables necessary for Shimmie.</p>
  301. <p>Please check and ensure that the database configuration options are all correct.</p>
  302. <p>{$e->getMessage()}</p>
  303. </div>
  304. EOD;
  305. exit(3);
  306. }
  307. catch (Exception $e) {
  308. print <<<EOD
  309. <div id="installer">
  310. <h1>Shimmie Installer</h1>
  311. <h3>Unknown Error:</h3>
  312. <p>An unknown error occured while trying to create the database tables necessary for Shimmie.</p>
  313. <p>Please check the server log files for more information.</p>
  314. <p>{$e->getMessage()}</p>
  315. </div>
  316. EOD;
  317. exit(4);
  318. }
  319. } // }}}
  320. function insert_defaults() { // {{{
  321. try {
  322. $db = new Database();
  323. $db->execute("INSERT INTO users(name, pass, joindate, class) VALUES(:name, :pass, now(), :class)", Array("name" => 'Anonymous', "pass" => null, "class" => 'anonymous'));
  324. $db->execute("INSERT INTO config(name, value) VALUES(:name, :value)", Array("name" => 'anon_id', "value" => $db->get_last_insert_id('users_id_seq')));
  325. if(check_im_version() > 0) {
  326. $db->execute("INSERT INTO config(name, value) VALUES(:name, :value)", Array("name" => 'thumb_engine', "value" => 'convert'));
  327. }
  328. $db->commit();
  329. }
  330. catch(PDOException $e)
  331. {
  332. print <<<EOD
  333. <div id="installer">
  334. <h1>Shimmie Installer</h1>
  335. <h3>Database Error:</h3>
  336. <p>An error occured while trying to insert data into the database.</p>
  337. <p>Please check and ensure that the database configuration options are all correct.</p>
  338. <p>{$e->getMessage()}</p>
  339. </div>
  340. EOD;
  341. exit(5);
  342. }
  343. catch (Exception $e)
  344. {
  345. print <<<EOD
  346. <div id="installer">
  347. <h1>Shimmie Installer</h1>
  348. <h3>Unknown Error:</h3>
  349. <p>An unknown error occured while trying to insert data into the database.</p>
  350. <p>Please check the server log files for more information.</p>
  351. <p>{$e->getMessage()}</p>
  352. </div>
  353. EOD;
  354. exit(6);
  355. }
  356. } // }}}
  357. function build_dirs() { // {{{
  358. // *try* and make default dirs. Ignore any errors --
  359. // if something is amiss, we'll tell the user later
  360. if(!file_exists("images")) @mkdir("images");
  361. if(!file_exists("thumbs")) @mkdir("thumbs");
  362. if(!file_exists("data") ) @mkdir("data");
  363. if(!is_writable("images")) @chmod("images", 0755);
  364. if(!is_writable("thumbs")) @chmod("thumbs", 0755);
  365. if(!is_writable("data") ) @chmod("data", 0755);
  366. // Clear file status cache before checking again.
  367. clearstatcache();
  368. if(
  369. !file_exists("images") || !file_exists("thumbs") || !file_exists("data") ||
  370. !is_writable("images") || !is_writable("thumbs") || !is_writable("data")
  371. ) {
  372. print "
  373. <div id='installer'>
  374. <h1>Shimmie Installer</h1>
  375. <h3>Directory Permissions Error:</h3>
  376. <p>Shimmie needs to make three folders in it's directory, '<i>images</i>', '<i>thumbs</i>', and '<i>data</i>', and they need to be writable by the PHP user.</p>
  377. <p>If you see this error, if probably means the folders are owned by you, and they need to be writable by the web server.</p>
  378. <p>PHP reports that it is currently running as user: ".$_ENV["USER"]." (". $_SERVER["USER"] .")</p>
  379. <p>Once you have created these folders and / or changed the ownership of the shimmie folder, hit 'refresh' to continue.</p>
  380. <br/><br/>
  381. </div>
  382. ";
  383. exit(7);
  384. }
  385. } // }}}
  386. function write_config() { // {{{
  387. $file_content = '<' . '?php' . "\n" .
  388. "define('DATABASE_DSN', '".DATABASE_DSN."');\n" .
  389. '?' . '>';
  390. if(!file_exists("data/config")) {
  391. mkdir("data/config", 0755, true);
  392. }
  393. if(file_put_contents("data/config/shimmie.conf.php", $file_content, LOCK_EX)) {
  394. header("Location: index.php");
  395. print <<<EOD
  396. <div id="installer">
  397. <h1>Shimmie Installer</h1>
  398. <h3>Things are OK \o/</h3>
  399. <p>If you aren't redirected, <a href="index.php">click here to Continue</a>.
  400. </div>
  401. EOD;
  402. }
  403. else {
  404. $h_file_content = htmlentities($file_content);
  405. print <<<EOD
  406. <div id="installer">
  407. <h1>Shimmie Installer</h1>
  408. <h3>File Permissions Error:</h3>
  409. The web server isn't allowed to write to the config file; please copy
  410. the text below, save it as 'data/config/shimmie.conf.php', and upload it into the shimmie
  411. folder manually. Make sure that when you save it, there is no whitespace
  412. before the "&lt;?php" or after the "?&gt;"
  413. <p><textarea cols="80" rows="2">$h_file_content</textarea>
  414. <p>Once done, <a href="index.php">click here to Continue</a>.
  415. <br/><br/>
  416. </div>
  417. EOD;
  418. }
  419. echo "\n";
  420. } // }}}
  421. ?>
  422. </body>
  423. </html>