install.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. <?php
  2. define ("ABSOLUTE_PATH", dirname (__FILE__) . "/");
  3. require_once (ABSOLUTE_PATH . "lib/webstart.php");
  4. require_once (ABSOLUTE_PATH . "lib/lib.php");
  5. ?>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  7. <html>
  8. <head>
  9. <title>Online-Bookmarks</title>
  10. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  11. </head>
  12. <body>
  13. <?php
  14. $mysql_hostname = set_post_string_var ('mysql_hostname', 'localhost');
  15. $mysql_db_name = set_post_string_var ('mysql_db_name', 'bookmarks');
  16. $mysql_db_username = set_post_string_var ('mysql_db_username', 'bookmarkmgr');
  17. $mysql_db_password = set_post_string_var ('mysql_db_password');
  18. $mysql_db_create = set_post_bool_var ('mysql_db_create', false);
  19. $mysql_db_su_username = set_post_string_var ('mysql_db_su_username', 'root');
  20. $mysql_db_su_password = set_post_string_var ('mysql_db_su_password');
  21. $cookie_name = set_post_string_var ('cookie_name', 'ob_cookie');
  22. $cookie_domain = set_post_string_var ('cookie_domain', '');
  23. $cookie_path = set_post_string_var ('cookie_path', '/');
  24. $cookie_seed = set_post_string_var ('cookie_seed', random_string ());
  25. $cookie_expire = set_post_string_var ('cookie_expire', '31536000');
  26. $submit = set_post_bool_var ('submit', false);
  27. $admin_message = '';
  28. if (intval(str_replace('.', '', phpversion())) < 430) {
  29. print_msg ('You are running PHP version '.PHP_VERSION.'. Online-Bookmarks requires at least PHP 4.3.0 to run properly. You must upgrade your PHP installation before you can continue.', "error");
  30. }
  31. ############## database control ##############
  32. function create_table_bookmark($link) {
  33. $query = "CREATE TABLE bookmark (
  34. user char(20) NOT NULL default '',
  35. title char(70) NOT NULL default '',
  36. url char(200) NOT NULL default '',
  37. description mediumtext default NULL,
  38. private enum('0','1') default NULL,
  39. date timestamp(6) NOT NULL,
  40. childof int(11) NOT NULL default '0',
  41. id int(11) NOT NULL auto_increment,
  42. deleted enum('0','1') NOT NULL default '0',
  43. favicon varchar(200),
  44. public enum('0','1') NOT NULL default '0',
  45. PRIMARY KEY (id),
  46. FULLTEXT KEY title (title,url,description)
  47. )";
  48. if (mysqli_query($link, $query)) {
  49. return true;
  50. }
  51. else {
  52. return false;
  53. }
  54. }
  55. function create_table_folder($link) {
  56. $query = "CREATE TABLE folder (
  57. id int(11) NOT NULL auto_increment,
  58. childof int(11) NOT NULL default '0',
  59. name char(70) NOT NULL default '',
  60. user char(20) NOT NULL default '',
  61. deleted enum('0','1') NOT NULL default '0',
  62. public enum('0','1') NOT NULL default '0',
  63. UNIQUE KEY id (id)
  64. )";
  65. if (mysqli_query($link, $query)) {
  66. return true;
  67. }
  68. else {
  69. return false;
  70. }
  71. }
  72. function create_table_user($link) {
  73. $query = "CREATE TABLE user (
  74. username char(50) NOT NULL default '',
  75. password char(50) NOT NULL default '',
  76. admin enum('0','1') NOT NULL default '0',
  77. language char(20) NOT NULL default '',
  78. root_folder_name char(50) NOT NULL default 'My Bookmarks',
  79. column_width_folder smallint(3) NOT NULL default '400',
  80. column_width_bookmark smallint(3) NOT NULL default '0',
  81. table_height smallint(3) NOT NULL default '400',
  82. confirm_delete enum('0','1') NOT NULL default '1',
  83. open_new_window enum('0','1') NOT NULL default '1',
  84. show_bookmark_description enum('0','1') NOT NULL default '1',
  85. show_bookmark_icon enum('0','1') NOT NULL default '1',
  86. show_column_date enum('0','1') NOT NULL default '1',
  87. date_format SMALLINT(6) NOT NULL DEFAULT '0',
  88. show_column_edit enum('0','1') NOT NULL default '1',
  89. show_column_move enum('0','1') NOT NULL default '1',
  90. show_column_delete enum('0','1') NOT NULL default '1',
  91. fast_folder_minus enum('0','1') NOT NULL default '1',
  92. fast_folder_plus enum('0','1') NOT NULL default '1',
  93. fast_symbol enum('0','1') NOT NULL default '1',
  94. simple_tree_mode enum('0','1') NOT NULL default '0',
  95. show_public enum('0','1') NOT NULL default '1',
  96. UNIQUE KEY id (username)
  97. )";
  98. if (mysqli_query($link, $query)) {
  99. return true;
  100. }
  101. else {
  102. return false;
  103. }
  104. }
  105. function create_admin_user($link) {
  106. $query = "INSERT INTO user (username, password, admin)
  107. VALUES ('admin', MD5('admin'), '1');";
  108. if (mysqli_query($link, $query)) {
  109. return true;
  110. }
  111. else {
  112. return false;
  113. }
  114. }
  115. function random_string ($max = 14){ # TODO: definitely replace this with some built in std function
  116. $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
  117. $string = '';
  118. for($i = 0; $i < $max; $i++){
  119. $rand_key = mt_rand (0, strlen($chars));
  120. $string .= substr ($chars, $rand_key, 1);
  121. }
  122. return str_shuffle ($string);
  123. }
  124. function print_msg ($message, $type = "") {
  125. if ($type == "success") {
  126. $color = "green";
  127. }
  128. else if ($type == "error") {
  129. $color = "red";
  130. }
  131. else if ($type == "notice") {
  132. $color = "orange";
  133. }
  134. else {
  135. $color = "black";
  136. }
  137. echo '<div style="font:bold 12pt Times; color: ' . $color . '">' . $message . '</div>' . "\n";
  138. }
  139. function check_table_version ($link, $table, $field) {
  140. $query = "DESC $table";
  141. $return = false;
  142. if ($result = mysqli_query ($link, $query)) {
  143. while ($row = mysqli_fetch_row ($result)) {
  144. if ($row[0] == $field) {
  145. $return = true;
  146. break;
  147. }
  148. }
  149. }
  150. return $return;
  151. }
  152. function upgrade_table ($link, $table, $field, $query) {
  153. if (check_table_version ($link, $table, $field)) {
  154. print_msg ("Table $table contains '$field' field, good.", "success");
  155. }
  156. else {
  157. print_msg ("Table $table does not contain $field field, attempting to upgrade", "notice");
  158. if (mysqli_query ($link, $query)) {
  159. print_msg ("Table $table altered, $field added.", "success");
  160. }
  161. else {
  162. print_msg ("Failure! Table $table not changed.", "error");
  163. }
  164. }
  165. }
  166. ############## html stuff ##############
  167. function html_db () {
  168. global $mysql_hostname,
  169. $mysql_db_name,
  170. $mysql_db_username,
  171. $mysql_db_su_username,
  172. $cookie_name,
  173. $cookie_domain,
  174. $cookie_path,
  175. $cookie_seed,
  176. $cookie_expire;
  177. ?>
  178. <h3>Database connection:</h3>
  179. <form method="POST" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
  180. <table>
  181. <tr>
  182. <td>Database hostname:</td>
  183. <td><input type="text" name="mysql_hostname" value="<?php echo $mysql_hostname; ?>"></td>
  184. <td></td>
  185. </tr>
  186. <tr>
  187. <td>Database name:</td>
  188. <td><input type="text" name="mysql_db_name" value="<?php echo $mysql_db_name; ?>"></td>
  189. <td></td>
  190. </tr>
  191. <tr>
  192. <td>Database username:</td>
  193. <td><input type="text" name="mysql_db_username" value="<?php echo $mysql_db_username; ?>"></td>
  194. <td></td>
  195. </tr>
  196. <tr>
  197. <td>Database password:</td>
  198. <td><input type="password" name="mysql_db_password" value=""></td>
  199. <td></td>
  200. </tr>
  201. <tr>
  202. <td>Create new database:</td>
  203. <td><input type="checkbox" name="mysql_db_create"></td>
  204. <td></td>
  205. </tr>
  206. <tr>
  207. <td>using Superuser account:</td>
  208. <td><input type="text" name="mysql_db_su_username" value="<?php echo $mysql_db_su_username; ?>"></td>
  209. <td></td>
  210. </tr>
  211. <tr>
  212. <td>Superuser password:</td>
  213. <td><input type="password" name="mysql_db_su_password" value=""></td>
  214. <td></td>
  215. </tr>
  216. <tr>
  217. <td><h3>Cookie settings:</h3></td>
  218. <td></td>
  219. <td></td>
  220. </tr>
  221. <tr>
  222. <td>Cookie name:</td>
  223. <td><input type="text" name="cookie_name" value="<?php echo $cookie_name; ?>"></td>
  224. <td></td>
  225. </tr>
  226. <tr>
  227. <td>Cookie domain:</td>
  228. <td><input type="text" name="cookie_domain" value="<?php echo $cookie_domain; ?>"></td>
  229. <td></td>
  230. </tr>
  231. <tr>
  232. <td>Cookie path:</td>
  233. <td><input type="text" name="cookie_path" value="<?php echo $cookie_path; ?>"></td>
  234. <td></td>
  235. </tr>
  236. <tr>
  237. <td>Cookie seed:</td>
  238. <td><input type="text" name="cookie_seed" value="<?php echo $cookie_seed; ?>"></td>
  239. <td>Just some random junk.</td>
  240. </tr>
  241. <tr>
  242. <td>Cookie expire:</td>
  243. <td><input type="text" name="cookie_expire" value="<?php echo $cookie_expire; ?>"></td>
  244. <td>Set an amount of seconds when the cookie will expire.</td>
  245. </tr>
  246. <tr>
  247. <td></td>
  248. <td><input type="submit" name="submit"></td>
  249. <td></td>
  250. </tr>
  251. </table>
  252. </form>
  253. <?php
  254. }
  255. if ($submit) {
  256. if ($mysql_db_create) {
  257. $link = mysqli_connect($mysql_hostname, $mysql_db_su_username, $mysql_db_su_password) or die("cannot connect to db");
  258. if (mysqli_query($link, "CREATE DATABASE IF NOT EXISTS $mysql_db_name")) {
  259. print_msg ("Database $mysql_db_name created", "success");
  260. }
  261. else {
  262. html_db ();
  263. print_msg (mysqli_error ($link), "error");
  264. require_once (ABSOLUTE_PATH . "footer.php");
  265. }
  266. if (mysqli_query($link, "GRANT ALL PRIVILEGES ON $mysql_db_name.* TO '$mysql_db_username'@'$mysql_hostname' IDENTIFIED BY '$mysql_db_password'")) {
  267. print_msg ("User $mysql_db_username created", "success");
  268. }
  269. else {
  270. html_db ();
  271. print_msg (mysqli_error ($link), "error");
  272. require_once (ABSOLUTE_PATH . "footer.php");
  273. }
  274. }
  275. mysqli_close($link);
  276. $dsn = array(
  277. 'db_username' => $mysql_db_username,
  278. 'db_password' => $mysql_db_password,
  279. 'db_hostname' => $mysql_hostname,
  280. 'db_name' => $mysql_db_name,
  281. );
  282. $link = mysqli_connect ($dsn['db_hostname'], $dsn['db_username'], $dsn['db_password']) or die("cannot connect to db");
  283. {
  284. {
  285. mysqli_select_db($link, $dsn['db_name']) or die("cannot select db");
  286. ############## DB support ##############
  287. print_msg ("DB connection succeeded", "success");
  288. $query = "SHOW TABLES";
  289. $tables = array ();
  290. $result = mysqli_query($link, $query);
  291. while ($row = mysqli_fetch_row($result)) {
  292. array_push ($tables, $row[0]);
  293. }
  294. # the bookmark table
  295. if (!in_array ("bookmark", $tables)) {
  296. if (create_table_bookmark ($link)) {
  297. print_msg ("Table bookmark created", "success");
  298. }
  299. else {
  300. print_msg (mysqli_error ($link), "error");
  301. }
  302. }
  303. else {
  304. print_msg ("Table bookmark exists, checking for version:", "notice");
  305. # check for favicon support
  306. upgrade_table ($link, "bookmark", "favicon", "ALTER TABLE bookmark ADD COLUMN favicon varchar(200)");
  307. # check for public field in table
  308. upgrade_table ($link, "bookmark", "public", "ALTER TABLE bookmark ADD COLUMN public ENUM('0','1') DEFAULT 0 NOT NULL");
  309. }
  310. # the folder table
  311. if (!in_array ("folder", $tables)) {
  312. if (create_table_folder ($link)) {
  313. print_msg ("Table folder created", "success");
  314. }
  315. else {
  316. print_msg (mysqli_error ($link), "error");
  317. }
  318. }
  319. else {
  320. print_msg ("Table folder exists, checking for version:", "notice");
  321. # check for public field in table
  322. upgrade_table ($link, "folder", "public", "ALTER TABLE folder ADD COLUMN public ENUM('0','1') DEFAULT 0 NOT NULL");
  323. }
  324. # the user table
  325. if (!in_array ("user", $tables)) {
  326. if (create_table_user ($link)) {
  327. print_msg ("Table user created", "success");
  328. if (create_admin_user ($link)) {
  329. print_msg ("Admin user created (see below)", "success");
  330. $admin_message = 'Initial user created. Login with username "admin" and password "admin"';
  331. }
  332. }
  333. else {
  334. print_msg (mysqli_error ($link), "error");
  335. }
  336. }
  337. else {
  338. print_msg ("Table user exists, checking for version:", "notice");
  339. # check for date_format field in table
  340. upgrade_table ($link, "user", "date_format", "ALTER TABLE user ADD COLUMN date_format SMALLINT(6) NOT NULL DEFAULT '0' AFTER show_column_date");
  341. # check for show_public field in table
  342. upgrade_table ($link, "user", "show_public", "ALTER TABLE user ADD COLUMN show_public ENUM('0','1') DEFAULT 1 NOT NULL");
  343. # check for admin field in table
  344. upgrade_table ($link, "user", "admin", "ALTER TABLE user ADD COLUMN admin ENUM('0','1') DEFAULT 0 NOT NULL AFTER password");
  345. }
  346. ############## favicon support ##############
  347. if ($convert = @exec ('which convert')) {
  348. $convert_favicons = "true";
  349. print_msg ("ImageMagick convert found: $convert", "success");
  350. }
  351. else {
  352. $convert = "";
  353. $convert_favicons = "false";
  354. print_msg ("ImageMagick convert not found. Make sure ImageMagick is installed and specify location of convert manually or set \$convert_favicons to false.", "error");
  355. }
  356. if ($identify = @exec ('which identify')) {
  357. $convert_favicons = "true";
  358. print_msg ("ImageMagick identify found: $identify", "success");
  359. }
  360. else {
  361. $identify = "";
  362. $convert_favicons = "false";
  363. print_msg ("ImageMagick identify not found. Make sure ImageMagick is installed and specify location of identify manually or set \$convert_favicons to false.", "error");
  364. }
  365. if (is_writable ("./favicons/")) {
  366. print_msg ("./favicons directory is writable by the webserver, good.", "success");
  367. }
  368. else {
  369. print_msg ("./favicons directory is not writable by the webserver. Adjust permissions manually.", "error");
  370. }
  371. $config = '
  372. &lt;?php
  373. if (basename ($_SERVER[\'SCRIPT_NAME\']) == basename (__FILE__)) {
  374. die ("no direct access allowed");
  375. }
  376. $dsn = array(
  377. \'username\' => \'' . $mysql_db_username . '\',
  378. \'password\' => \'' . $mysql_db_password . '\',
  379. \'hostspec\' => \'' . $mysql_hostname . '\',
  380. \'database\' => \'' . $mysql_db_name . '\',
  381. );
  382. $cookie = array (
  383. \'name\' => \'' . $cookie_name . '\',
  384. \'domain\' => \'' . $cookie_domain . '\',
  385. \'path\' => \'' . $cookie_path . '\',
  386. \'seed\' => \'' . $cookie_seed . '\',
  387. \'expire\' => time() + ' . $cookie_expire . ',
  388. );
  389. # Feel free to add values to this list as you like
  390. # according to the PHP documentation
  391. # http://www.php.net/manual/en/function.date.php
  392. $date_formats = array (
  393. \'d/m/Y\',
  394. \'Y-m-d\',
  395. \'m/d/Y\',
  396. \'d.m.Y\',
  397. \'F j, Y\',
  398. \'dS \o\f F Y\',
  399. \'dS F Y\',
  400. \'d F Y\',
  401. \'d. M Y\',
  402. \'Y F d\',
  403. \'F d, Y\',
  404. \'M. d, Y\',
  405. \'m/d/Y\',
  406. \'m-d-Y\',
  407. \'m.d.Y\',
  408. \'m.d.y\',
  409. );
  410. $convert_favicons = ' . $convert_favicons . ';
  411. $convert = \'' . $convert . '\';
  412. $identify = \'' . $identify . '\';
  413. $timeout = 5;
  414. $folder_closed = \'&lt;img src="./images/folder.gif" alt=""&gt;\';
  415. $folder_opened = \'&lt;img src="./images/folder_open.gif" alt=""&gt;\';
  416. $folder_closed_public = \'&lt;img src="./images/folder_red.gif" alt=""&gt;\';
  417. $folder_opened_public = \'&lt;img src="./images/folder_open_red.gif" alt=""&gt;\';
  418. $bookmark_image = \'&lt;img src="./images/bookmark_image.gif" alt=""&gt;\';
  419. $plus = \'&lt;img src="./images/plus.gif" alt=""&gt;&nbsp;\';
  420. $minus = \'&lt;img src="./images/minus.gif" alt=""&gt;&nbsp;\';
  421. $neutral = \'&lt;img src="./images/spacer.gif" width="13" height="1" alt=""&gt;&nbsp;\';
  422. $edit_image = \'&lt;img src="./images/edit.gif" title="%s" alt=""&gt;\';
  423. $move_image = \'&lt;img src="./images/move.gif" title="%s" alt=""&gt;\';
  424. $delete_image = \'&lt;img src="./images/delete.gif" title="%s" alt=""&gt;\';
  425. $delimiter = "/";
  426. ?&gt;';
  427. echo '<p>Paste the configuration shown below in the configuration file <span style="font-family:courier">./config/config.php</span></p>' . "\n";
  428. if ($admin_message != '') {
  429. echo $admin_message;
  430. }
  431. print_msg ("<p>IMPORTANT! Do not forget to remove this install.php script.</p>");
  432. echo '<pre style="background-color: #E0E0E0; border: 1px black solid; padding: 20px">';
  433. echo $config;
  434. echo "</pre>\n";
  435. echo '<a href="./index.php">Now go Bookmark...</a>';
  436. }
  437. }
  438. }
  439. else {
  440. html_db ();
  441. }
  442. require_once (ABSOLUTE_PATH . "footer.php");
  443. ?>