PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/_install/installer.inc.php

http://compactcms.googlecode.com/
PHP | 469 lines | 339 code | 37 blank | 93 comment | 73 complexity | 143bc3eed64f1af0ad2d4b17904d58ef MD5 | raw file
Possible License(s): GPL-3.0, CC0-1.0
  1. <?php
  2. /* ************************************************************
  3. Copyright (C) 2008 - 2010 by Xander Groesbeek (CompactCMS.nl)
  4. Revision: CompactCMS - v 1.4.1
  5. This file is part of CompactCMS.
  6. CompactCMS is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. CompactCMS is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. A reference to the original author of CompactCMS and its copyright
  15. should be clearly visible AT ALL TIMES for the user of the back-
  16. end. You are NOT allowed to remove any references to the original
  17. author, communicating the product to be your own, without written
  18. permission of the original copyright owner.
  19. You should have received a copy of the GNU General Public License
  20. along with CompactCMS. If not, see <http://www.gnu.org/licenses/>.
  21. > Contact me for any inquiries.
  22. > E: Xander@CompactCMS.nl
  23. > W: http://community.CompactCMS.nl/forum
  24. ************************************************************ */
  25. // Start the current session
  26. session_start();
  27. // Set current && additional step
  28. $nextstep = (isset($_POST['do'])&&!empty($_POST['do'])?$_POST['do']:'ea2b2676c28c0db26d39331a336c6b92');
  29. $additional = (isset($_GET['do'])&&!empty($_GET['do'])?$_GET['do']:null);
  30. // Define default root folder
  31. @define('BASE_PATH',dirname(dirname(__FILE__)));
  32. /**
  33. *
  34. * Per step processing of input
  35. *
  36. **/
  37. // Step two
  38. if($nextstep == md5('2') && md5(session_id())==$_SESSION['id'] && md5($_SERVER['HTTP_HOST']) == $_SESSION['host']) {
  39. //
  40. // Installation actions
  41. // - Environmental variables
  42. //
  43. $rootdir = array("rootdir" => $_POST['rootdir']);
  44. $homepage = array("homepage" => $_POST['homepage']);
  45. $language = array("language" => $_POST['language']);
  46. // Add new data to variable session
  47. $_SESSION['variables'] = array_merge($rootdir,$homepage,$language);
  48. ?>
  49. <legend class="installMsg">Step 2 - Setting your preferences</legend>
  50. <label for="sitename"><span class="ss_sprite ss_pencil">Site name</span></label><input type="text" class="alt title" name="sitename" style="width:300px;" value="<?php echo (!isset($_SESSION['variables']['sitename'])?ucfirst(preg_replace("/^www\./", "", $_SERVER['HTTP_HOST'])):$_SESSION['variables']['sitename']);?>" id="sitename" />
  51. <br class="clear"/>
  52. <label for="version"><input type="checkbox" name="version" value="true" checked id="version" /> Show version information</label>
  53. <label for="iframe"><input type="checkbox" name="iframe" value="true" id="iframe" /> Support &amp; allow iframes</label>
  54. <label for="wysiwyg"><input type="checkbox" name="wysiwyg" value="true" checked id="wysiwyg" /> Enable the visual content editor</label>
  55. <label for="protect"><input type="checkbox" name="protect" value="true" checked id="protect" /> Password protect the administration</label>
  56. <br class="clear"/>
  57. <label for="authcode"><span class="ss_sprite ss_textfield_key">Authentication PIN</span></label>
  58. <input type="text" class="alt title" name="authcode" maxlenght="5" style="width:300px;" value="<?php echo rand('12345','98765');?>" id="authcode" />
  59. <p class="span-8 right">
  60. <button name="submit" type="submit"><span class="ss_sprite ss_lock_go">Proceed</span></button>
  61. <a href="index.php" title="Back to step first step">Cancel</a>
  62. <input type="hidden" name="do" value="<?php echo md5('3'); ?>" id="do" />
  63. </p>
  64. <?php
  65. } // Close step two
  66. // Step three
  67. if($nextstep == md5('3') && md5(session_id())==$_SESSION['id'] && md5($_SERVER['HTTP_HOST']) == $_SESSION['host']) {
  68. //
  69. // Installation actions
  70. // - Saving preferences
  71. //
  72. $sitename = array("sitename" => $_POST['sitename']);
  73. $version = array("version" => (isset($_POST['version'])&&$_POST['version']=='true'?'true':'false'));
  74. $iframe = array("iframe" => (isset($_POST['iframe'])&&$_POST['iframe']=='true'?'true':'false'));
  75. $wysiwyg = array("wysiwyg" => (isset($_POST['wysiwyg'])&&$_POST['wysiwyg']=='true'?'true':'false'));
  76. $protect = array("protect" => (isset($_POST['protect'])&&$_POST['protect']=='true'?'true':'false'));
  77. $authcode = array("authcode" => $_POST['authcode']);
  78. // Add new data to variable session
  79. $_SESSION['variables'] = array_merge($_SESSION['variables'],$sitename,$version,$iframe,$wysiwyg,$protect,$authcode);
  80. ?>
  81. <legend class="installMsg">Step 3 - Collecting your database details</legend>
  82. <label for="db_host"><span class="ss_sprite ss_server_database">Database host</span></label><input type="text" class="alt title" name="db_host" style="width:300px;" value="localhost" id="db_host" />
  83. <br class="clear"/>
  84. <label for="db_user"><span class="ss_sprite ss_drive_user">Database username</span></label><input type="text" class="alt title" name="db_user" style="width:300px;" value="" id="db_user" />
  85. <br class="clear"/>
  86. <label for="db_pass"><span class="ss_sprite ss_drive_key">Database password</span></label><input type="password" class="title" name="db_pass" style="width:300px;" value="" id="db_pass" />
  87. <br class="clear"/>
  88. <label for="db_name"><span class="ss_sprite ss_database">Database name</span></label><input type="text" class="alt title" name="db_name" style="width:300px;" value="compactcms" id="db_name" />
  89. <br class="clear"/>
  90. <label for="db_prefix"><span class="ss_sprite ss_database_table">Database table prefix</span></label><input type="text" class="alt title" name="db_prefix" style="width:300px;" value="ccms_" id="db_prefix" />
  91. <p class="span-8 right">
  92. <button name="submit" type="submit"><span class="ss_sprite ss_information">To confirmation</span></button>
  93. <a href="index.php" title="Back to step first step">Cancel</a>
  94. <input type="hidden" name="do" value="<?php echo md5('4'); ?>" id="do" />
  95. </p>
  96. <?php
  97. } // Close step three
  98. // Step four
  99. if($nextstep == md5('4') && md5(session_id())==$_SESSION['id'] && md5($_SERVER['HTTP_HOST']) == $_SESSION['host']) {
  100. //
  101. // Installation actions
  102. // - Process database
  103. //
  104. $db_host = array("db_host" => $_POST['db_host']);
  105. $db_user = array("db_user" => $_POST['db_user']);
  106. $db_pass = array("db_pass" => $_POST['db_pass']);
  107. $db_name = array("db_name" => $_POST['db_name']);
  108. $db_prefix = array("db_prefix" => $_POST['db_prefix']);
  109. // Add new data to variable session
  110. $_SESSION['variables'] = array_merge($_SESSION['variables'],$db_host,$db_user,$db_pass,$db_name,$db_prefix);
  111. // Define alternative table row color
  112. $alt_row = "#CDE6B3";
  113. //
  114. // Check for current chmod() if server != Windows
  115. //
  116. $chmod = 0;
  117. if(!strpos($_SERVER['SERVER_SOFTWARE'], "Win")) {
  118. (substr(decoct(fileperms('../.htaccess')),1)!='0666'?$chmod++:null);
  119. (substr(decoct(fileperms('../lib/config.inc.php')),1)!='0666'?$chmod++:null);
  120. (substr(decoct(fileperms('../content/')),1)!='0755'?$chmod++:null);
  121. (substr(decoct(fileperms('../lib/includes/cache/')),1)!='0777'?$chmod++:null);
  122. (substr(decoct(fileperms('../lib/modules/backup-restore/files/')),1)!='0777'?$chmod++:null);
  123. (substr(decoct(fileperms('../media/')),1)!='0777'?$chmod++:null);
  124. (substr(decoct(fileperms('../media/albums/')),1)!='0777'?$chmod++:null);
  125. }
  126. ?>
  127. <legend class="installMsg">Step 4 - Review your input</legend>
  128. <?php if(ini_get('safe_mode') || $chmod>0) {?>
  129. <h2>Warning</h2>
  130. <p>It appears that it <abbr title="Based on current chmod() rights and/or safe mode restrictions">might not be possible</abbr> for the installer to chmod() various files. Please consider doing so manually <em>or</em> by using the <a href="index.php?do=ff104b2dfab9fe8c0676587292a636d3">built-in FTP chmod function</a>.</p>
  131. <span>&rarr; <em>Files that require chmod():</em></span>
  132. <ul>
  133. <li>./.htaccess (0666)</li>
  134. <li>./lib/config.inc.php (0666)</li>
  135. <li>./content/ (0777) <a href="http://community.compactcms.nl/forum/" target="_blank"><span class="small quiet">more info</span></a></li>
  136. <li>./lib/includes/cache/ (0777)</li>
  137. <li>../lib/modules/backup-restore/files/ (0777)</li>
  138. <li>./media/ (0777)</li>
  139. <li>./media/albums/ (0777)</li>
  140. </ul>
  141. <?php } ?>
  142. <span class="ss_sprite ss_computer">&#160;</span><h2 style="display:inline;">Environment</h2>
  143. <table width="100%" border="0" cellspacing="0" cellpadding="0">
  144. <tr>
  145. <th width="45%" scope="row">Root directory</th>
  146. <td><?php echo $_SESSION['variables']['rootdir'];?></td>
  147. </tr>
  148. <tr>
  149. <th scope="row">Homepage</th>
  150. <td><?php echo $_SESSION['variables']['homepage'];?></td>
  151. </tr>
  152. <tr style="background-color: <?php echo $alt_row; ?>;">
  153. <th scope="row">Language</th>
  154. <td><?php echo $_SESSION['variables']['language'];?></td>
  155. </tr>
  156. </table>
  157. <br class="clear"/>
  158. <span class="ss_sprite ss_cog">&#160;</span><h2 style="display:inline;">Preferences</h2>
  159. <table width="100%" border="0" cellspacing="0" cellpadding="0">
  160. <tr>
  161. <th width="45%" scope="row">Sitename</th>
  162. <td><?php echo $_SESSION['variables']['sitename'];?></td>
  163. </tr>
  164. <tr style="background-color: <?php echo $alt_row; ?>;">
  165. <th scope="row">Version</th>
  166. <td><?php echo $_SESSION['variables']['version'];?></td>
  167. </tr>
  168. <tr>
  169. <th scope="row">Iframe</th>
  170. <td><?php echo $_SESSION['variables']['iframe'];?></td>
  171. </tr>
  172. <tr style="background-color: <?php echo $alt_row; ?>;">
  173. <th scope="row">Visual editor</th>
  174. <td><?php echo $_SESSION['variables']['wysiwyg'];?></td>
  175. </tr>
  176. <tr>
  177. <th scope="row">User authentication</th>
  178. <td><?php echo $_SESSION['variables']['protect'];?></td>
  179. </tr>
  180. <tr style="background-color: <?php echo $alt_row; ?>;">
  181. <th scope="row">Authentication PIN</th>
  182. <td><?php echo $_SESSION['variables']['authcode'];?></td>
  183. </tr>
  184. </table>
  185. <br class="clear"/>
  186. <span class="ss_sprite ss_database">&#160;</span><h2 style="display:inline;">Database details</h2>
  187. <table width="100%" border="0" cellspacing="0" cellpadding="0">
  188. <tr>
  189. <th width="45%" scope="row">Database host</th>
  190. <td><?php echo $_SESSION['variables']['db_host'];?></td>
  191. </tr>
  192. <tr style="background-color: <?php echo $alt_row; ?>;">
  193. <th scope="row">Database username</th>
  194. <td><?php echo $_SESSION['variables']['db_user'];?></td>
  195. </tr>
  196. <tr>
  197. <th scope="row">Database password</th>
  198. <td> *** </td>
  199. </tr>
  200. <tr style="background-color: <?php echo $alt_row; ?>;">
  201. <th scope="row">Database name</th>
  202. <td><?php echo $_SESSION['variables']['db_name'];?></td>
  203. </tr>
  204. <tr>
  205. <th scope="row">Database table prefix</th>
  206. <td><?php echo $_SESSION['variables']['db_prefix'];?></td>
  207. </tr>
  208. </table>
  209. <hr noshade="noshade" />
  210. <p class="quiet">
  211. <strong><span class="ss_sprite ss_exclamation">Please note</span></strong><br/>
  212. Any data that is currently in <strong><?php echo $_SESSION['variables']['db_prefix']; ?>pages</strong> and <strong><?php echo $_SESSION['variables']['db_prefix']; ?>users</strong> might be overwritten, depending your servers' configuration.
  213. </p>
  214. <p class="span-8 right">
  215. <button name="submit" id="installbtn" type="submit"><span class="ss_sprite ss_accept">Install <strong>CompactCMS</strong></span></button>
  216. <a href="index.php" title="Back to step first step">Cancel</a>
  217. <input type="hidden" name="do" value="<?php echo md5('final'); ?>" id="do" />
  218. </p>
  219. <?php
  220. } // Close step four
  221. /**
  222. *
  223. * Do the actual configuration
  224. *
  225. **/
  226. // Final step
  227. if($nextstep == md5('final') && md5(session_id())==$_SESSION['id'] && md5($_SERVER['HTTP_HOST']) == $_SESSION['host']) {
  228. //
  229. // Installation actions
  230. // - Set collected data
  231. //
  232. // Let's start with a clean sheet
  233. $err = 0;
  234. // Include MySQL class && initiate
  235. require_once(BASE_PATH.'/lib/class/mysql.class.php');
  236. $db = new MySQL();
  237. //
  238. // Try database connection
  239. //
  240. if (!$db->Open($_SESSION['variables']['db_name'], $_SESSION['variables']['db_host'], $_SESSION['variables']['db_user'], $_SESSION['variables']['db_pass'])) {
  241. $errors[] = 'Error: could not connect to the database';
  242. $errors[] = $db->Error();
  243. $err++;
  244. } else {
  245. $log[] = "Database connection successful";
  246. }
  247. //
  248. // Insert database structure and sample data
  249. //
  250. if($err==0) {
  251. $sql = file_get_contents(BASE_PATH.'/_docs/structure.sql');
  252. $sql = preg_replace('/ccms_/', $_SESSION['variables']['db_prefix'], $sql);
  253. // Execute per sql piece
  254. $tok = strtok($sql, ";");
  255. while ($tok !== false) {
  256. $results = $db->Query("$tok");
  257. $tok = strtok(";");
  258. } $log[] = "Database structure and data successfully imported";
  259. }
  260. //
  261. // Set chmod on config.inc.php, .htaccess, content, cache and albums
  262. //
  263. if($err==0 && !isset($_POST['ftp_host']) && empty($_POST['ftp_host'])) {
  264. // Set warning when safe mode is enabled
  265. if(ini_get('safe_mode')) {
  266. $errors[] = 'Warning: safe mode is enabled, skipping chmod()';
  267. }
  268. // Count chmod() successes
  269. $chmod = 0;
  270. // Do chmod() per necessary folder and set status
  271. if(@chmod(BASE_PATH."/.htaccess", 0666)) { $chmod++; }
  272. if(@chmod(BASE_PATH."/lib/config.inc.php", 0666)) { $chmod++; }
  273. if(@chmod(BASE_PATH."/content/", 0777)) { $chmod++; }
  274. if(@chmod(BASE_PATH."/content/home.php", 0666)) { $chmod++; }
  275. if(@chmod(BASE_PATH."/content/installation.php", 0666)) { $chmod++; }
  276. if(@chmod(BASE_PATH."/content/contact.php", 0666)) { $chmod++; }
  277. if(@chmod(BASE_PATH."/lib/includes/cache/", 0777)) { $chmod++; }
  278. if(@chmod(BASE_PATH."/lib/templates/ccms.tpl.html", 0666)) { $chmod++; }
  279. if(@chmod(BASE_PATH."/admin/includes/modules/backup-restore/files/", 0777)) { $chmod++; }
  280. if(@chmod(BASE_PATH."/media/", 0777)) { $chmod++; }
  281. if(@chmod(BASE_PATH."/media/albums/", 0777)) { $chmod++; }
  282. if($chmod>0) {
  283. $log[] = '<abbr title=".htaccess, config.inc.php, ./content/, ./lib/includes/cache/, back-up folder &amp; 2 media folders">Successful chmod() on '.$chmod.' files</abbr>';
  284. } elseif($chmod==0) {
  285. $errors[] = 'Warning: could not chmod() all files.';
  286. $errors[] = 'Either use the <a href="index.php?do=ff104b2dfab9fe8c0676587292a636d3">built-in FTP chmod function</a>, or manually perform chmod().';
  287. }
  288. }
  289. //
  290. // Perform optional FTP chmod command
  291. //
  292. if(isset($_POST['ftp_host']) && !empty($_POST['ftp_host']) && isset($_POST['ftp_user']) && !empty($_POST['ftp_user'])) {
  293. // Set up a connection or die
  294. $conn_id = ftp_connect($_POST['ftp_host']) or die("Couldn't connect to ".$_POST['ftp_host']);
  295. // Try to login using provided details
  296. if (@ftp_login($conn_id, $_POST['ftp_user'], $_POST['ftp_pass'])) {
  297. if (ftp_chdir($conn_id, $_POST['ftp_path'])) {
  298. $log[] = "Successfully connected to FTP server";
  299. }
  300. } else {
  301. $errors[] = "Fatal: couldn't connect to the FTP server. Perform chmod() manually.";
  302. $err++;
  303. }
  304. // Count the ftp_chmod() successes
  305. $ftp_chmod = 0;
  306. // Perform the ftp_chmod command
  307. if(@ftp_chmod($conn_id, 0666, "./.htaccess")) { $ftp_chmod++; }
  308. if(@ftp_chmod($conn_id, 0666, "./lib/config.inc.php")) { $ftp_chmod++; }
  309. if(@ftp_chmod($conn_id, 0777, "./content/")) { $ftp_chmod++; }
  310. if(@ftp_chmod($conn_id, 0666, "./content/home.php")) { $ftp_chmod++; }
  311. if(@ftp_chmod($conn_id, 0666, "./content/installation.php")) { $ftp_chmod++; }
  312. if(@ftp_chmod($conn_id, 0666, "./content/contact.php")) { $ftp_chmod++; }
  313. if(@ftp_chmod($conn_id, 0777, "./lib/includes/cache/")) { $ftp_chmod++; }
  314. if(@ftp_chmod($conn_id, 0666, "./lib/templates/ccms.tpl.html")) { $ftp_chmod++; }
  315. if(@ftp_chmod($conn_id, 0777, "./admin/includes/modules/backup-restore/files/")) { $ftp_chmod++; }
  316. if(@ftp_chmod($conn_id, 0777, "./media/")) { $ftp_chmod++; }
  317. if(@ftp_chmod($conn_id, 0777, "./media/albums")) { $ftp_chmod++; }
  318. if($ftp_chmod>0) {
  319. $log[] = '<abbr title=".htaccess, config.inc.php, ./content/, ./lib/includes/cache/, back-up folder &amp; 2 media folders">Successful chmod() on '.$chmod.' files using FTP.</abbr>';
  320. } elseif($ftp_chmod==0) {
  321. $errors[] = 'Fatal: could not FTP chmod() various files.';
  322. $err++;
  323. }
  324. // Close the connection
  325. ftp_close($conn_id);
  326. }
  327. //
  328. // Write config.inc.php file
  329. //
  330. if($err==0) {
  331. include(BASE_PATH.'/lib/config.inc.php');
  332. $config_str = "\$cfg = array();\r\n";
  333. $write_err = null;
  334. // Write new variables to configuration file
  335. if ($fp = @fopen(BASE_PATH.'/lib/config.inc.php', 'w')) {
  336. // Write start line
  337. fwrite($fp, "<?php\r\n// Copyright (C) 2008 - ".date('Y')." by Xander Groesbeek (CompactCMS.nl)\r\n// This file is part of CompactCMS\r\n// Please refer to license.txt for information on license conditions.\r\n");
  338. // Compare old and new variables
  339. foreach($cfg as $key=>$val) {
  340. if (isset($_SESSION['variables'][$key])) {
  341. $new_val = $_SESSION['variables'][$key];
  342. } else {
  343. $new_val = $cfg[$key];
  344. }
  345. // Rewrite the previous loaded string
  346. if($new_val=="true"||$new_val=="false") {
  347. $config_str = "\$cfg['{$key}'] = {$new_val}; \r\n";
  348. } else {
  349. $config_str = "\$cfg['{$key}'] = '{$new_val}'; \r\n";
  350. }
  351. // Write each new variable to the config file
  352. if(!fwrite($fp, $config_str, strlen($config_str))) {
  353. $write_err = "1";
  354. $errors[] = "Fatal: Problem saving new configuration values";
  355. $err++;
  356. }
  357. }
  358. // Write end lines
  359. fwrite($fp, "\$cfg['restrict'] = array();\r\n?>");
  360. // Check for errors
  361. if(empty($write_err)) {
  362. $log[] = "Configuration successfully saved to config.inc.php";
  363. }
  364. } else {
  365. $errors[] = 'Fatal: the configuration file is not writable.';
  366. $errors[] = 'Make sure the file is writable, or <a href="index.php?do=ff104b2dfab9fe8c0676587292a636d3">do so now</a>.';
  367. $err++;
  368. }
  369. }
  370. //
  371. // Modify .htaccess file
  372. //
  373. if($err==0 && $_SESSION['variables']['rootdir']!='/') {
  374. $htaccess = file_get_contents(BASE_PATH.'/.htaccess');
  375. $htaccess = preg_replace("/RewriteBase \//", "RewriteBase ".$_SESSION['variables']['rootdir'], $htaccess);
  376. if ($fp = fopen(BASE_PATH.'/.htaccess', 'w')) {
  377. if(fwrite($fp, $htaccess, strlen($htaccess))) {
  378. $log[] = "Successfully rewrote the .htaccess file";
  379. }
  380. } elseif($_SESSION['variables']['rootdir']=="/") {
  381. $errors[] = 'Warning: the .htaccess file is not writable.';
  382. } elseif($_SESSION['variables']['rootdir']!="/") {
  383. $errors[] = 'Fatal: the .htaccess file is not writable.';
  384. $errors[] = 'Make sure the file is writable, or <a href="index.php?do=ff104b2dfab9fe8c0676587292a636d3">do so now</a>.';
  385. $err++;
  386. }
  387. }
  388. ?>
  389. <legend class="installMsg">Final - Finishing the installation</legend>
  390. <?php if(isset($log)) { ?>
  391. <h2>Process results</h2>
  392. <p>
  393. <?php
  394. while (list($key,$value) = each($log)) {
  395. echo '<span class="ss_sprite ss_accept">'.$value.'</span><br />';
  396. } ?>
  397. </p>
  398. <?php } if(isset($errors)) { ?>
  399. <h2>Errors &amp; warnings</h2>
  400. <p>
  401. <?php
  402. while (list($key,$value) = each($errors)) {
  403. echo '<span class="ss_sprite ss_exclamation">'.$value.'</span><br />';
  404. } ?>
  405. </p>
  406. <?php } if($err==0) { ?>
  407. <h2>What's next?</h2>
  408. <p>The installation has been successful! You should now follow the steps below, to get you started.</p>
  409. <ol>
  410. <li>Delete the <em>./_install</em> directory</li>
  411. <li><a href="../admin/">Login</a> using details <strong>admin</strong> and <strong>pass</strong></li>
  412. <li>Change your password through the back-end</li>
  413. <li><a href="http://www.compactcms.nl/contact.html" target="_blank">Let me know</a> how you like CompactCMS!</li>
  414. </ol>
  415. <?php } else echo '<a href="index.php">Retry setting the necessary variables</a>'; ?>
  416. <?php
  417. } // Close final processing
  418. ?>