setup.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. # ***** BEGIN LICENSE BLOCK *****
  3. # Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. #
  5. # The contents of this file are subject to the Mozilla Public License Version
  6. # 1.1 (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. # http://www.mozilla.org/MPL/
  9. #
  10. # Software distributed under the License is distributed on an "AS IS" basis,
  11. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. # for the specific language governing rights and limitations under the
  13. # License.
  14. #
  15. # The Original Code is Weave Minimal Server
  16. #
  17. # The Initial Developer of the Original Code is
  18. # Stefan Fischer
  19. # Portions created by the Initial Developer are Copyright (C) 2012
  20. # the Initial Developer. All Rights Reserved.
  21. #
  22. # Contributor(s):
  23. # Daniel Triendl <daniel@pew.cc>
  24. # balu
  25. # Christian Wittmer <chris@computersalat.de>
  26. #
  27. # Alternatively, the contents of this file may be used under the terms of
  28. # either the GNU General Public License Version 2 or later (the "GPL"), or
  29. # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30. # in which case the provisions of the GPL or the LGPL are applicable instead
  31. # of those above. If you wish to allow use of your version of this file only
  32. # under the terms of either the GPL or the LGPL, and not to allow others to
  33. # use your version of this file under the terms of the MPL, indicate your
  34. # decision by deleting the provisions above and replace them with the notice
  35. # and other provisions required by the GPL or the LGPL. If you do not delete
  36. # the provisions above, a recipient may use your version of this file under
  37. # the terms of any one of the MPL, the GPL or the LGPL.
  38. #
  39. # ***** END LICENSE BLOCK *****
  40. // --------------------------------------------
  41. // variables start
  42. // --------------------------------------------
  43. $action = null;
  44. $dbType = null;
  45. $dbUser = null;
  46. $dbName = null;
  47. $dbPass = null;
  48. $dbHost = null;
  49. // --------------------------------------------
  50. // variables end
  51. // --------------------------------------------
  52. // --------------------------------------------
  53. // post handling start
  54. // --------------------------------------------
  55. if ( isset( $_POST['action'] ) ) {
  56. $action = check_input($_POST['action']);
  57. }
  58. if ( isset( $_POST['dbType'] ) ) {
  59. $dbType = check_input($_POST['dbType']);
  60. }
  61. if ( isset( $_POST['dbhost'] ) ) {
  62. $dbHost = check_input($_POST['dbhost']);
  63. }
  64. if ( isset( $_POST['dbname'] ) ) {
  65. $dbName = check_input($_POST['dbname']);
  66. }
  67. if ( isset( $_POST['dbuser'] ) ) {
  68. $dbUser = check_input($_POST['dbuser']);
  69. }
  70. if ( isset( $_POST['dbpass'] ) ) {
  71. $dbPass = check_input($_POST['dbpass']);
  72. }
  73. // --------------------------------------------
  74. // post handling end
  75. // --------------------------------------------
  76. // --------------------------------------------
  77. // functions start
  78. // --------------------------------------------
  79. /*
  80. ensure that the input is not total waste
  81. */
  82. function check_input( $data ) {
  83. $data = trim($data);
  84. $data = stripslashes($data);
  85. $data = htmlspecialchars($data);
  86. return $data;
  87. }
  88. /*
  89. create the config file with the database type
  90. and the given connection credentials
  91. */
  92. function write_config_file($dbt, $dbh, $dbn, $dbu, $dbp, $fsRoot) {
  93. // construct the name of config file
  94. //
  95. $path = explode('/', $_SERVER['SCRIPT_FILENAME']);
  96. array_pop($path);
  97. array_push($path, 'settings.php');
  98. $cfg_file_name = implode('/', $path);
  99. if ( file_exists($cfg_file_name) && filesize( $cfg_file_name ) > 0 ) {
  100. echo "<hr>The config file $cfg_file_name is already present</hr>";
  101. return;
  102. }
  103. echo "Creating cfg file: " . $cfg_file_name;
  104. // now build the content of the config file
  105. //
  106. $cfg_content = "<?php\n\n";
  107. $cfg_content .= " // you can disable registration to the firefox sync server here,\n";
  108. $cfg_content .= " // by setting ENABLE_REGISTER to false\n";
  109. $cfg_content .= " // \n";
  110. $cfg_content .= " define(\"ENABLE_REGISTER\", true);\n\n";
  111. $cfg_content .= " // firefox sync server url, this should end with a /\n";
  112. $cfg_content .= " // e.g. https://YourDomain.de/Folder_und_ggf_/index.php/\n";
  113. $cfg_content .= " // \n";
  114. $cfg_content .= " define(\"FSYNCMS_ROOT\", \"$fsRoot\");\n\n";
  115. $cfg_content .= " // Database connection credentials\n";
  116. $cfg_content .= " // \n";
  117. $cfg_content .= " define(\"SQLITE_FILE\", \"weave_db\");\n";
  118. if ( $dbt != "mysql" ) {
  119. $cfg_content .= " define(\"MYSQL_ENABLE\", false);\n";
  120. $cfg_content .= " define(\"MYSQL_HOST\", \"localhost\");\n";
  121. $cfg_content .= " define(\"MYSQL_DB\", \"fsync\");\n";
  122. $cfg_content .= " define(\"MYSQL_USER\", \"fsyncUserName\");\n";
  123. $cfg_content .= " define(\"MYSQL_PASSWORD\", \"fsyncUserPassword\");\n";
  124. } else {
  125. $cfg_content .= " define(\"MYSQL_ENABLE\", true);\n";
  126. $cfg_content .= " define(\"MYSQL_HOST\", \"$dbh\");\n";
  127. $cfg_content .= " define(\"MYSQL_DB\", \"$dbn\");\n";
  128. $cfg_content .= " define(\"MYSQL_USER\", \"$dbu\");\n";
  129. $cfg_content .= " define(\"MYSQL_PASSWORD\", \"$dbp\");\n";
  130. }
  131. $cfg_content .= "\n";
  132. $cfg_content .= " // Use bcrypt instead of MD5 for password hashing\n";
  133. $cfg_content .= " define(\"BCRYPT\", true);\n";
  134. $cfg_content .= " define(\"BCRYPT_ROUNDS\", 12);\n";
  135. $cfg_content .= "\n";
  136. $cfg_content .= " // you can enable logging to syslog for MINQUOTA_ERROR_OVER_QUOTA\n";
  137. $cfg_content .= " // if (quota_used > MINQUOTA && quota_used < MAXQUOTA)\n";
  138. $cfg_content .= " define(\"MINQUOTA_LOG_ERROR_OVER_QUOTA_ENABLE\", false);\n";
  139. $cfg_content .= "\n";
  140. $cfg_content .= " // set MinQuota and MaxQuota\n";
  141. $cfg_content .= " define(\"MINQUOTA\", 30000);\n";
  142. $cfg_content .= " define(\"MAXQUOTA\", 35000);\n";
  143. $cfg_content .= " // The setting below determines the time to live for quota totals\n";
  144. $cfg_content .= " // before recalculating how much database space has been used.\n";
  145. $cfg_content .= " define(\"QUOTA_TTL\", 3600);\n";
  146. $cfg_content .= "\n?>\n";
  147. // now write everything
  148. //
  149. $cfg_file = fopen($cfg_file_name, "a");
  150. fputs($cfg_file, "$cfg_content");
  151. fclose($cfg_file);
  152. }
  153. /*
  154. print the html header for the form
  155. */
  156. function print_header( $title ) {
  157. if ( ! isset( $title ) ) {
  158. $title = "";
  159. }
  160. print '<html><header><title>' . $title . '</title><body>
  161. <h1>Setup FSyncMS</h1>
  162. <form action="setup.php" method="post">';
  163. }
  164. /*
  165. print the html footer
  166. */
  167. function print_footer() {
  168. print '</form></body></html>';
  169. }
  170. /*
  171. print the html for for the mysql connection credentials
  172. */
  173. function print_mysql_connection_form() {
  174. print_header("MySQL database connection setup");
  175. print 'MySQL database connection setup
  176. <table>
  177. <tr>
  178. <td>Host</td>
  179. <td><input type="text" name="dbhost" /></td>
  180. </tr>
  181. <tr>
  182. <td>Instance name</td>
  183. <td><input type="text" name="dbname" /></td>
  184. </tr>
  185. <tr>
  186. <td>Username</td>
  187. <td><input type="text" name="dbuser" /></td>
  188. </tr>
  189. <tr>
  190. <td>Password</td>
  191. <td><input type="password" name="dbpass" /></td>
  192. </tr>
  193. </table>
  194. <input type="hidden" name="action" value="step2">
  195. <input type="hidden" name="dbType" value="mysql">
  196. <p><input type="submit" value="OK"></p>';
  197. print_footer();
  198. }
  199. // --------------------------------------------
  200. // functions end
  201. // --------------------------------------------
  202. // check if we have no configuration at the moment
  203. //
  204. if ( file_exists("settings.php") && filesize( "settings.php" ) > 0 ) {
  205. echo "<hr><h2>The setup looks like it's completed, please delete settings.php</h2><hr>";
  206. exit;
  207. }
  208. // inital page - select the database type
  209. //
  210. if ( ! $action ) {
  211. // first check if we have pdo installed (untested)
  212. //
  213. if ( ! extension_loaded('PDO') ) {
  214. print "ERROR - PDO is missing in the php installation!";
  215. exit();
  216. }
  217. $validPdoDriver = 0;
  218. print_header("Setup FSyncMS - DB Selection");
  219. print 'Which database type should be used?<br>';
  220. if ( extension_loaded('pdo_mysql') ) {
  221. print '<input type="radio" name="dbType" value="mysql" /> MySQL <br>';
  222. $validPdoDriver++;
  223. } else {
  224. print 'MySQL not possible (Driver missing) <br>';
  225. }
  226. if ( extension_loaded('pdo_sqlite') ) {
  227. print '<input type="radio" name="dbType" value="sqlite" checked="checked" /> SQLite ';
  228. $validPdoDriver++;
  229. } else {
  230. print 'SQLite not possible (Driver missing) <br>';
  231. }
  232. if ( $validPdoDriver < 1 ) {
  233. print '<hr> No valid pdo driver found! Please install a valid pdo driver first <hr>';
  234. } else {
  235. print '<input type="hidden" name="action" value="step1">
  236. <p><input type="submit" value="OK" /></p>';
  237. }
  238. // ensure we bail out at this point ;)
  239. exit();
  240. };
  241. // step 2 (connection data) below
  242. //
  243. if ( $action == "step1" ) {
  244. // now check if the database is in place
  245. //
  246. print_header("Setup FSyncMS - DB Setup: $dbType!");
  247. switch ( $dbType ) {
  248. case "sqlite":
  249. $action = "step2";
  250. break;
  251. case "mysql":
  252. print_mysql_connection_form();
  253. break;
  254. default:
  255. print "ERROR - This type of database ($dbType) is not valid at the moment!";
  256. exit();
  257. break;
  258. }
  259. }
  260. // now generate the database
  261. //
  262. if ( $action == "step2" ) {
  263. $dbInstalled = false;
  264. $dbHandle = null;
  265. try {
  266. if ( $dbType == "sqlite" ) {
  267. $path = explode('/', $_SERVER['SCRIPT_FILENAME']);
  268. $db_name = 'weave_db';
  269. array_pop($path);
  270. array_push($path, $db_name);
  271. $db_name = implode('/', $path);
  272. if ( file_exists($db_name) && filesize( $db_name ) > 0 ) {
  273. $dbInstalled = true;
  274. } else {
  275. echo("Creating sqlite weave storage: ". $db_name ."<br>");
  276. $dbHandle = new PDO('sqlite:' . $db_name);
  277. $dbHandle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  278. }
  279. } else if ( $dbType == "mysql" ) {
  280. $dbHandle = new PDO("mysql:host=". $dbHost .";dbname=". $dbName, $dbUser, $dbPass);
  281. $select_stmt = "show tables like 'wbo'";
  282. $sth = $dbHandle->prepare($select_stmt);
  283. $sth->execute();
  284. $count = $sth->rowCount();
  285. if ( $count > 0 ) {
  286. $dbInstalled = true;
  287. }
  288. };
  289. } catch ( PDOException $exception ) {
  290. echo("database unavailable " . $exception->getMessage());
  291. throw new Exception("Database unavailable " . $exception->getMessage() , 503);
  292. }
  293. if ( $dbInstalled ) {
  294. echo "DB is already installed!<br>";
  295. } else {
  296. echo "Now going to install the new database! Type is: $dbType<br>";
  297. try {
  298. $create_statement = " create table wbo ( username varchar(100), id varchar(65), collection varchar(100),
  299. parentid varchar(65), predecessorid int, modified real, sortindex int,
  300. payload text, payload_size int, ttl int, primary key (username,collection,id))";
  301. $create_statement2 = " create table users ( username varchar(255), md5 varchar(124), login int,
  302. quota_usage int, usage_time int,
  303. primary key (username)) ";
  304. $index1 = 'create index parentindex on wbo (username, parentid)';
  305. $index2 = 'create index predecessorindex on wbo (username, predecessorid)';
  306. $index3 = 'create index modifiedindex on wbo (username, collection, modified)';
  307. $sth = $dbHandle->prepare($create_statement);
  308. $sth->execute();
  309. $sth = $dbHandle->prepare($create_statement2);
  310. $sth->execute();
  311. $sth = $dbHandle->prepare($index1);
  312. $sth->execute();
  313. $sth = $dbHandle->prepare($index2);
  314. $sth->execute();
  315. $sth = $dbHandle->prepare($index3);
  316. $sth->execute();
  317. echo "Database created <br>";
  318. } catch( PDOException $exception ) {
  319. throw new Exception("Database unavailable", 503);
  320. }
  321. }
  322. //guessing fsroot
  323. // get the FSYNC_ROOT url
  324. //
  325. $fsRoot ="https://";
  326. if ( ! isset($_SERVER['HTTPS']) ) {
  327. $fsRoot = "http://";
  328. }
  329. $fsRoot .= $_SERVER['SERVER_NAME'] . dirname($_SERVER['SCRIPT_NAME']) . "/";
  330. if( strpos( $_SERVER['REQUEST_URI'], 'index.php') !== 0 ) {
  331. $fsRoot .= "index.php/";
  332. }
  333. // write settings.php, if not possible, display the needed contant
  334. //
  335. write_config_file($dbType, $dbHost, $dbName, $dbUser, $dbPass, $fsRoot);
  336. echo "<hr><hr> Finished the setup, please delete setup.php and go on with the FFSync<hr><hr>";
  337. echo <<<EOT
  338. <hr><hr>
  339. <h4>This script has guessed the Address of your installation, this might not be accurate,<br/>
  340. Please check if this script can be reached by <a href="$fsRoot">$fsRoot</a> .<br/>
  341. If thats not the case you have to ajust the settings.php<br />
  342. </h4>
  343. EOT;
  344. }
  345. ?>