PageRenderTime 41ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/install.php

https://gitlab.com/Poorchop/Jirafeau
PHP | 370 lines | 348 code | 2 blank | 20 comment | 5 complexity | 093258dfca7bf5b54583184b6dde2347 MD5 | raw file
  1. <?php
  2. /*
  3. * Jirafeau, your web file repository
  4. * Copyright (C) 2008 Julien "axolotl" BERNARD <axolotl@magieeternelle.org>
  5. * Copyright (C) 2015 Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. define ('JIRAFEAU_ROOT', dirname (__FILE__) . '/');
  21. define ('NL', "\n");
  22. define ('QUOTE', "'");
  23. define ('JIRAFEAU_CFG', JIRAFEAU_ROOT.'lib/config.local.php');
  24. define ('JIRAFEAU_VAR_RAND_LENGTH', 15);
  25. require (JIRAFEAU_ROOT . 'lib/functions.php');
  26. require (JIRAFEAU_ROOT . 'lib/lang.php');
  27. require (JIRAFEAU_ROOT . 'lib/config.original.php');
  28. function
  29. jirafeau_quoted ($str)
  30. {
  31. return QUOTE . str_replace (QUOTE, "\'", $str) . QUOTE;
  32. }
  33. function
  34. jirafeau_export_cfg ($cfg)
  35. {
  36. $handle = fopen (JIRAFEAU_CFG, 'w');
  37. fwrite ($handle, '<?php' . NL);
  38. fwrite ($handle,
  39. '/* ' .
  40. t ('This file was generated by the install process. ' .
  41. 'You can edit it. Please see config.original.php to understand the ' .
  42. 'configuration items.') . ' */' . NL);
  43. foreach ($cfg as $key => $item)
  44. {
  45. fwrite ($handle, '$cfg[' . jirafeau_quoted ($key) . '] = ');
  46. if (is_bool ($item))
  47. fwrite ($handle, ($item ? 'true' : 'false'));
  48. else if (is_string ($item))
  49. fwrite ($handle, jirafeau_quoted ($item));
  50. else if (is_int ($item))
  51. fwrite ($handle, $item);
  52. else if (is_array ($item))
  53. fwrite ($handle, str_replace(array("\n", "\r"), "",
  54. var_export ($item, true)));
  55. else
  56. fwrite ($handle, 'null');
  57. fwrite ($handle, ';'.NL);
  58. }
  59. /* No newline at the end of the file to be able to send headers. */
  60. fwrite ($handle, '?>');
  61. fclose ($handle);
  62. }
  63. function
  64. jirafeau_mkdir ($path)
  65. {
  66. return !(!file_exists ($path) && !@mkdir ($path, 0755));
  67. }
  68. /**
  69. * Returns true whether the path is writable or we manage to make it
  70. * so, which essentially is the same thing.
  71. * @param $path is the file or directory to be tested.
  72. * @return true if $path is writable.
  73. */
  74. function
  75. jirafeau_is_writable ($path)
  76. {
  77. /* "@" gets rid of error messages. */
  78. return is_writable ($path) || @chmod ($path, 0777);
  79. }
  80. function
  81. jirafeau_check_var_dir ($path)
  82. {
  83. $mkdir_str1 = t('The following directory could not be created') . ':';
  84. $mkdir_str2 = t('You should create this directory manually.');
  85. $write_str1 = t('The following directory is not writable') . ':';
  86. $write_str2 = t('You should give the write permission to the web server on ' .
  87. 'this directory.');
  88. $solution_str = t('Here is a solution') . ':';
  89. if (!jirafeau_mkdir ($path) || !jirafeau_is_writable ($path))
  90. return array ('has_error' => true,
  91. 'why' => $mkdir_str1 . '<br /><code>' .
  92. $path . '</code><br />' . $solution_str .
  93. '<br />' . $mkdir_str2);
  94. foreach (array ('files', 'links', 'async', 'alias') as $subdir)
  95. {
  96. $subpath = $path.$subdir;
  97. if (!jirafeau_mkdir ($subpath) || !jirafeau_is_writable ($subpath))
  98. return array ('has_error' => true,
  99. 'why' => $mkdir_str1 . '<br /><code>' .
  100. $subpath . '</code><br />' . $solution_str .
  101. '<br />' . $mkdir_str2);
  102. }
  103. return array ('has_error' => false, 'why' => '');
  104. }
  105. function
  106. jirafeau_add_ending_slash ($path)
  107. {
  108. return $path . ((substr ($path, -1) == '/') ? '' : '/');
  109. }
  110. if ($cfg['installation_done'] === true)
  111. {
  112. header('Location: index.php');
  113. exit;
  114. }
  115. if (!file_exists (JIRAFEAU_CFG))
  116. {
  117. /* We try to create an empty one. */
  118. if (!@touch (JIRAFEAU_CFG))
  119. {
  120. require (JIRAFEAU_ROOT . 'lib/template/header.php');
  121. echo '<div class="error"><p>' .
  122. t('The local configuration file could not be created. Create a ' .
  123. '<code>lib/config.local.php</code> file and give the write ' .
  124. 'permission to the web server (preferred solution), or give the ' .
  125. 'write permission to the web server on the <code>lib</code> ' .
  126. 'directory.') .
  127. '</p></div>';
  128. require (JIRAFEAU_ROOT . 'lib/template/footer.php');
  129. exit;
  130. }
  131. }
  132. if (!is_writable (JIRAFEAU_CFG) && !@chmod (JIRAFEAU_CFG, '0666'))
  133. {
  134. require (JIRAFEAU_ROOT . 'lib/template/header.php');
  135. echo '<div class="error"><p>' .
  136. t('The local configuration is not writable by the web server. ' .
  137. 'Give the write permission to the web server on the ' .
  138. '<code>lib/config.local.php</code> file.') .
  139. '</p></div>';
  140. require (JIRAFEAU_ROOT . 'lib/template/footer.php');
  141. exit;
  142. }
  143. if (isset ($_POST['step']) && isset ($_POST['next']))
  144. {
  145. switch ($_POST['step'])
  146. {
  147. case 1:
  148. $cfg['lang'] = $_POST['lang'];
  149. jirafeau_export_cfg ($cfg);
  150. break;
  151. case 2:
  152. $cfg['admin_password'] = $_POST['admin_password'];
  153. jirafeau_export_cfg ($cfg);
  154. break;
  155. case 3:
  156. $cfg['web_root'] = jirafeau_add_ending_slash ($_POST['web_root']);
  157. $cfg['var_root'] = jirafeau_add_ending_slash ($_POST['var_root']);
  158. jirafeau_export_cfg ($cfg);
  159. break;
  160. case 4:
  161. $cfg['web_root'] = jirafeau_add_ending_slash ($_POST['web_root']);
  162. $cfg['var_root'] = jirafeau_add_ending_slash ($_POST['var_root']);
  163. jirafeau_export_cfg ($cfg);
  164. break;
  165. }
  166. }
  167. require (JIRAFEAU_ROOT . 'lib/settings.php');
  168. require (JIRAFEAU_ROOT . 'lib/template/header.php');
  169. $current = 1;
  170. if (isset ($_POST['next']))
  171. $current = $_POST['step'] + 1;
  172. else if (isset ($_POST['previous']))
  173. $current = $_POST['step'] - 1;
  174. else if (isset ($_POST['retry']))
  175. $current = $_POST['step'];
  176. switch ($current)
  177. {
  178. case 1:
  179. default:
  180. ?><h2><?php printf (t('Installation of Jirafeau') . ' - ' . t('step') .
  181. ' %d ' . t('out of') . ' %d', 1, 4);
  182. ?></h2> <div id = "install"> <form action =
  183. "<?php echo basename(__FILE__); ?>" method = "post"> <input type =
  184. "hidden" name = "jirafeau" value =
  185. "<?php echo JIRAFEAU_VERSION; ?>" /><input type = "hidden" name =
  186. "step" value = "1" /><fieldset> <legend><?php echo t('Language');
  187. ?></legend> <table> <tr> <td class = "info" colspan =
  188. "2"><?php echo
  189. t
  190. ('Jirafeau is internationalised. Choose a specific langage or ' .
  191. 'choose Automatic (langage is provided by user\'s browser).');
  192. ?></td> </tr> <tr> <td class = "label"><label for = "select_lang"
  193. ><?php echo t('Choose the default language') . ':';
  194. ?></label></td>
  195. <td class = "field">
  196. <select name = "lang" id = "select_lang">
  197. <?php foreach ($languages_list as $key => $item)
  198. {
  199. echo '<option value="'.$key.'"'.($key ==
  200. $cfg['lang'] ? ' selected="selected"'
  201. : '').'>'.$item.'</option>'.NL;
  202. }
  203. ?></select>
  204. </td>
  205. </tr>
  206. <tr class = "nav">
  207. <td></td>
  208. <td class = "nav next"><input type = "submit" name = "next" value =
  209. "<?php echo t('Next step'); ?>" /></td> </tr> </table>
  210. </fieldset> </form> </div> <?php
  211. break;
  212. case 2:
  213. ?><h2><?php printf (t('Installation of Jirafeau') . ' - ' . t('step') .
  214. ' %d ' . t('out of') . ' %d', 2, 4);
  215. ?></h2> <div id = "install"> <form action =
  216. "<?php echo basename(__FILE__); ?>" method = "post"> <input type =
  217. "hidden" name = "jirafeau" value =
  218. "<?php echo JIRAFEAU_VERSION; ?>" /><input type = "hidden" name =
  219. "step" value = "2" /><fieldset> <legend><?php
  220. echo t('Administration password');
  221. ?></legend> <table> <tr> <td class = "info" colspan =
  222. "2"><?php echo
  223. t
  224. ('Jirafeau has an administration interface (through admin.php). ' .
  225. 'You can set a password to access the interface or leave it empty ' .
  226. 'to disable the interface.');
  227. ?></td> </tr> <tr> <td class = "label"><label for = "select_password"
  228. ><?php echo t('Administration password') . ':';
  229. ?></label></td>
  230. <td class = "field"><input type = "password" name = "admin_password"
  231. id = "admin_password" size = "40" /></td>
  232. </tr>
  233. <tr class = "nav">
  234. <td></td>
  235. <td class = "nav next">
  236. <input type = "submit"
  237. class = "navleft" name = "previous" value = "<?php
  238. echo t('Previous step'); ?>" />
  239. <input type = "submit" name = "next" value =
  240. "<?php echo t('Next step'); ?>" /></td> </tr> </table>
  241. </fieldset> </form> </div> <?php
  242. break;
  243. case 3:
  244. ?><h2><?php printf (t('Installation of Jirafeau') . ' - ' . t('step') .
  245. ' %d ' . t('out of') . ' %d', 3, 4);
  246. ?></h2> <div id = "install"> <form action =
  247. "<?php echo basename(__FILE__); ?>" method = "post"> <input type =
  248. "hidden" name = "jirafeau" value =
  249. "<?php echo JIRAFEAU_VERSION; ?>" /><input type = "hidden" name =
  250. "step" value =
  251. "3" /><fieldset> <legend><?php echo t('Information');
  252. ?></legend> <table> <tr> <td class = "info" colspan =
  253. "2"><?php echo
  254. t
  255. ('The base address of Jirafeau is the first part of the URL, until ' .
  256. '(and including) the last slash. For example: ' .
  257. '"http://www.example.com/". Do not forget the trailing slash!');
  258. ?></td> </tr> <tr> <td class = "label"><label for = "input_web_root"
  259. ><?php echo t('Base address') . ':';
  260. ?></label></td>
  261. <td class = "field"><input type = "text" name = "web_root"
  262. id = "input_web_root" value = "<?php
  263. echo (empty($cfg['web_root']) ?
  264. 'http://' . $_SERVER['HTTP_HOST'] . str_replace(basename(__FILE__),
  265. '', $_SERVER['REQUEST_URI']) : $cfg['web_root']);
  266. ?>" size = "40" /></td>
  267. </tr> <tr> <td class = "info" colspan = "2"><?php
  268. echo t('The data directory is where your files and information about' .
  269. ' your files will be stored. You should put it outside your web ' .
  270. 'site, or at least restrict the access to this directory. Do not ' .
  271. 'forget the trailing slash!');
  272. ?></td> </tr> <tr> <td class = "label"><label for = "input_var_root"
  273. ><?php echo t('Data directory') . ':';
  274. ?></label></td>
  275. <td class = "field"><input type = "text" name = "var_root"
  276. id = "input_var_root" value = "<?php
  277. if(empty($cfg['var_root'])) {
  278. $alphanum = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' .
  279. 'abcdefghijklmnopqrstuvwxyz' . '0123456789';
  280. $len_alphanum = strlen($alphanum);
  281. $var = 'var-';
  282. for($i = 0; $i <JIRAFEAU_VAR_RAND_LENGTH; $i++) {
  283. $var .= substr($alphanum, mt_rand(0, $len_alphanum - 1), 1);
  284. }
  285. echo JIRAFEAU_ROOT . $var . '/';
  286. }
  287. else
  288. echo $cfg['var_root'];
  289. ?>" size = "40" /></td>
  290. </tr> <tr> <td colspan = "2"><input type = "submit"
  291. class = "navleft" name = "previous" value = "<?php
  292. echo t('Previous step'); ?>" />
  293. <input type = "submit" class = "navright" name = "next" value = "
  294. <?php echo t('Next step'); ?>" />
  295. </td> </tr> </table> </fieldset>
  296. </form> </div> <?php
  297. break;
  298. case 4:
  299. ?><h2><?php printf (t('Installation of Jirafeau') . ' - ' . t('step') .
  300. ' %d ' . t('out of') . ' %d', 4, 4);
  301. ?></h2> <div id = "install"> <form action =
  302. "<?php echo basename(__FILE__); ?>" method = "post"> <input type =
  303. "hidden" name = "jirafeau" value =
  304. "<?php echo JIRAFEAU_VERSION; ?>" /><input type = "hidden" name =
  305. "step" value =
  306. "4" /><fieldset> <legend><?php echo t('Finalisation');
  307. ?></legend> <table> <tr> <td class = "info" colspan =
  308. "2"><?php echo
  309. t ('Jirafeau is setting the website according to the configuration ' .
  310. 'you provided.');
  311. ?></td> </tr> <tr> <td class = "nav previous"><input type =
  312. "submit" name = "previous" value =
  313. "
  314. <?php
  315. echo t('Previous step');
  316. ?>" /></td> <td></td> </tr>
  317. </table> </fieldset> </form> </div>
  318. <?php
  319. $err = jirafeau_check_var_dir ($cfg['var_root']);
  320. if ($err['has_error'])
  321. {
  322. echo '<div class="error"><p>'.$err['why'].'<br />'.NL;
  323. ?><form action = "<?php echo basename(__FILE__); ?>" method =
  324. "post"> <input type = "hidden" name = "jirafeau" value =
  325. "<?php echo JIRAFEAU_VERSION; ?>" /><input type = "hidden" name =
  326. "step" value = "4" /><input type = "submit" name =
  327. "retry" value =
  328. "<?php echo t('Retry this step'); ?>" /></form>
  329. <?php echo '</p></div>';
  330. }
  331. else
  332. {
  333. $cfg['installation_done'] = true;
  334. jirafeau_export_cfg ($cfg);
  335. echo '<div class="message"><p>' .
  336. t('Jirafeau is now fully operational') . ':' .
  337. '<br /><a href="' . $cfg['web_root'] . '">' .
  338. $cfg['web_root'].'</a></p></div>';
  339. }
  340. break;
  341. }
  342. require (JIRAFEAU_ROOT . 'lib/template/footer.php');
  343. ?>