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

/installer.php

http://prails.googlecode.com/
PHP | 242 lines | 217 code | 14 blank | 11 comment | 53 complexity | f99a93e7160eb5286985ac3b2a59cb10 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. // this installation file should be automatically placed into a temporary directory
  3. // in order to let it unpack the package contents and stuff
  4. $dir = dirname(__FILE__);
  5. $file = "prails.tar.bz2";
  6. function recurse_copy($src, $dst) {
  7. $dir = opendir($src);
  8. $arr_rollback = Array();
  9. $rollback = false;
  10. if (!is_dir($dst)) {
  11. array_push($arr_rollback, $dst);
  12. if (!@mkdir($dst)) {
  13. echo "Error creating directory ".$dst;
  14. $rollback = true;
  15. }
  16. }
  17. while (!$rollback && false !== ( $file = readdir($dir)) ) {
  18. if (( $file != '.' ) && ( $file != '..' )) {
  19. if ( is_dir($src . '/' . $file) ) {
  20. if (!recurse_copy($src . '/' . $file, $dst . '/' . $file)) {
  21. $rollback = true;
  22. }
  23. } else {
  24. array_push($arr_rollback, Array($src."/".$file, $dst."/".$file));
  25. // first create a backup of the original file, if existent
  26. if (file_exists($dst."/".$file)) {
  27. if (!copy($dst."/".$file, "backup.".md5($dst."/".$file))) {
  28. echo "Error creating backup for file ".$dst."/".$file;
  29. $rollback = true;
  30. break;
  31. }
  32. }
  33. if (!@copy($src . '/' . $file, $dst . '/' . $file)) {
  34. echo "Error copying file ".$src."/".$file." to ".$dst."/".$file."";
  35. $rollback = true;
  36. }
  37. }
  38. }
  39. }
  40. if ($rollback) {
  41. $arr_rollback = array_reverse($arr_rollback);
  42. foreach ($arr_rollback as $entry) {
  43. if (is_array($entry) && file_exists("backup.".md5($entry[1]))) {
  44. // if we have a backup file, restore it
  45. if (@copy("backup.".md5($entry[1]), $entry[1])) {
  46. @unlink("backup.".md5($entry[1]));
  47. }
  48. } else if (is_dir($entry)) {
  49. // if we created a directory during copy, remove it
  50. @unlink($entry);
  51. }
  52. }
  53. }
  54. closedir($dir);
  55. return !$rollback;
  56. }
  57. if ($_GET["version"]) {
  58. $version = $_GET["version"];
  59. $url = "http://prails.googlecode.com/files/prails-".$version.".tar.bz2";
  60. if (!($fileContent=file_get_contents($url))) {
  61. die("Error while downloading the Prails update.");
  62. }
  63. if (!file_put_contents($file, $fileContent)) {
  64. die("Error while downloading the Prails update. Please check permissions in ".$dir." .");
  65. }
  66. die("success\ncache/installer.php\nInstalling new version...");
  67. } else {
  68. $warnings = "";
  69. // unpack everything
  70. $disabled = explode(', ', ini_get('disable_functions'));
  71. if (in_array('exec', $disabled)) {
  72. die("Error while unpacking Prails update: cannot be unpacked due to disabled 'exec' function. Please check server configuration.");
  73. }
  74. if (!file_exists($file)) {
  75. die("Error while unpacking Prails update: package not found.");
  76. }
  77. exec("tar xvjf ".$file."");
  78. if (!file_exists("prails")) {
  79. die("Error while unpacking Prails update: unpacking failed.");
  80. }
  81. // run the actual installation
  82. // configuration file needs to be merged, so create a backup...
  83. $oldConf = file_get_contents("../conf/configuration.php");
  84. if (!copy("../conf/configuration.php", "backup.configuration.php") || !file_exists("backup.configuration.php")) {
  85. die("Error creating backup for configuration.");
  86. }
  87. // backup .htaccess
  88. if (!copy("../.htaccess", "backup.htaccess") || !file_exists("backup.htaccess")) {
  89. die("Error creating backup for .htaccess .");
  90. }
  91. copy("../.groups", "backup.groups");
  92. copy("../.users", "backup.users");
  93. copy("../favicon.ico", "backup.favicon.ico");
  94. // this should copy all files to the current installation directory
  95. if (!recurse_copy("prails", "..")) {
  96. die("Error installing new prails version.");
  97. }
  98. @unlink($file);
  99. exec("rm -rf prails");
  100. // copy back the .groups and .users file
  101. if (copy("backup.groups", "../.groups")) unlink("backup.groups"); else $warnings .= "Unable to restore groups. Backup stored in ".$dir."/backup.groups .<br/>";
  102. if (copy("backup.users", "../.users")) unlink("backup.users"); else $warnings .= "Unable to restore users. Backup stored in ".$dir."/backups.users .<br/>";
  103. copy("backup.favicon.ico", "../favicon.ico");
  104. $users = file("../.users");
  105. $adminFound = false;
  106. foreach ($users as &$user) {
  107. list($name, $pwd) = explode(":", $user);
  108. if ($name == "admin") {
  109. $adminFound = true;
  110. }
  111. if (strlen(trim($pwd)) != 32 || !preg_match('/^[a-f0-9]+$/mi', trim($pwd))) {
  112. $pwd = md5(trim($pwd));
  113. $user = $name.":".$pwd;
  114. }
  115. $user = trim($user);
  116. }
  117. if (!$adminFound) {
  118. $pwd = substr(md5(time()), 0, 6);
  119. $users[] = "admin:".md5($pwd);
  120. $groups = file("../.groups");
  121. $adminFound = false;
  122. foreach ($groups as &$group) {
  123. list($name, $users) = explode("=", $group);
  124. if ($name == "admin") {
  125. $users .= ",admin";
  126. $adminFound = true;
  127. $group = $name."=".$users;
  128. }
  129. $group = trim($group);
  130. }
  131. if (!$adminFound) $groups[] = "admin=admin";
  132. @file_put_contents("../.groups", implode("\n", $groups)) or $warnings .= "Unable to add new admin user to the admin group.<br/>";
  133. $warnings .= "An admin account has been added to your users to let you manage them. It's password is: ".$pwd."<br/>";
  134. }
  135. @file_put_contents("../.users", implode("\n", $users)) or $warnings .= "Unable to add new admin to list of users.<br/>";
  136. // merge .htaccess
  137. $oldHt = file_get_contents("backup.htaccess");
  138. $newHt = file_get_contents("../.htaccess");
  139. $startMarker = "#--START_CUSTOM--#";
  140. $endMarker = "#--END_CUSTOM--#";
  141. $start = strpos($oldHt, $startMarker) + strlen($startMarker);
  142. $len = (strpos($oldHt, $endMarker, $start) - 1) - $start;
  143. $area = substr($oldHt, $start, $len);
  144. $start = strpos($newHt, $startMarker) + strlen($startMarker);
  145. $len = (strpos($newHt, $endMarker, $start) - 1) - $start;
  146. $file = substr($newHt, 0, $start)."\n".$area."\n".substr($newHt, $start+$len);
  147. if (!@file_put_contents("../.htaccess", $file)) {
  148. $warnings .= "Unable to re-integrate custom .htaccess rules into the newer file. Backup stored in ".$dir."/backup.htaccess .<br/>";
  149. } else {
  150. unlink("backup.htaccess");
  151. }
  152. // merge configuration
  153. $newConf = $newBackupConf = file_get_contents("../conf/configuration.php");
  154. preg_match_all('@/\*<KEEP-([0-9]+)>\*/(([^/]|/[^*]|/\*[^<]|/\*<[^/]|/\*</[^K]|/\*</K[^E]|/\*</KE[^E]|/\*</KEE[^P])*)/\*</KEEP-\1>\*/@', $oldConf, $matches);
  155. $conf = Array();
  156. foreach ($matches[0] as $key=>$match) {
  157. $conf[$matches[1][$key]] = $matches[2][$key];
  158. }
  159. preg_match_all('@/\*<KEEP-([0-9]+)>\*/(([^/]|/[^*]|/\*[^<]|/\*<[^/]|/\*</[^K]|/\*</K[^E]|/\*</KE[^E]|/\*</KEE[^P])*)/\*</KEEP-\1>\*/@', $newConf, $matches);
  160. foreach ($matches[0] as $key => $match) {
  161. $value = $conf[$matches[1][$key]];
  162. // make sure that the DB is re-deployed the first time after upgrade
  163. if (strpos($value, "FIRST_RUN") !== false) {
  164. $lines = explode("\n", $value);
  165. foreach ($lines as &$line) {
  166. if (preg_match('/\s*"FIRST_RUN"\s*=>\s*[a-zA-Z]+\s*,/', $line)) {
  167. $line = '"FIRST_RUN" => true,';
  168. }
  169. }
  170. $value = implode("\n", $lines);
  171. }
  172. $newConf = str_replace($matches[2][$key], $value, $newConf);
  173. }
  174. file_put_contents("../conf/configuration.php", $newConf);
  175. exec("php -l ../conf/configuration.php", $error, $code);
  176. if ($code != 0) {
  177. // we have a syntax error in the code
  178. file_put_contents("../conf/configuration.php", $newBackupConf);
  179. $warnings .= "Error while restoring configuration options. ";
  180. $warnings .= "Configuration has been reset to installation default. Original configuration has been saved in ".$dir."/backup.configuration.php .<br/>";
  181. } else {
  182. if (!@unlink("backup.configuration.php")) {
  183. $warnings .= "Unable to remove backup configuration file.<br/>";
  184. }
  185. }
  186. $oldHandler = file_get_contents("backup.".md5("../modules/main/main_handler.php"));
  187. if (strlen($oldHandler) < 0) {
  188. $warnings .= "Unable to find global handler backup.";
  189. } else {
  190. $newHandler = file_get_contents("../modules/main/main_handler.php");
  191. $startPos = strpos($newHandler, "/** BEGIN_CODE **/") + strlen("/** BEGIN_CODE **/");
  192. if ($startPos - strlen("/** BEGIN_CODE **/") <= 0) {
  193. die("Error finding global home handler start!");
  194. }
  195. $endPos = strpos($newHandler, "/** END_CODE **/");
  196. if ($endPos === false) {
  197. die("Error finding global home handler end!");
  198. }
  199. $newPre = substr($newHandler, 0, $startPos);
  200. $newPost = substr($newHandler, $endPos);
  201. $oStartPos = strpos($oldHandler, "/** BEGIN_CODE **/") + strlen("/** BEGIN_CODE **/");
  202. $oEndPos = strpos($oldHandler, "/** END_CODE **/");
  203. $oldContent = substr($oldHandler, $oStartPos, $oEndPos - $oStartPos);
  204. $mergedHandler = $newPre . $oldContent . $newPost;
  205. if (!@file_put_contents("../modules/main/main_handler.php", $mergedHandler)) {
  206. $warnings .= "Unable to update global handler code<br/>";
  207. } else {
  208. exec("php -l ../modules/main/main_handler.php", $error, $code);
  209. if ($code != 0) {
  210. @file_put_contents("../modules/main/main_handler.php", $newHandler);
  211. $warnings .= "Error while merging global home handler. ";
  212. $warnings .= "It has been reset to installation default. Original handler code is saved in ".$dir."/backup.".md5("../modules/main/main_handler.php")."<br/>";
  213. }
  214. }
  215. }
  216. die("success\n?event=builder:updateSystem&empty=cache&warnings=".urlencode($warnings)."\nRe-deploying database and cleaning caches...");
  217. }
  218. ?>