PageRenderTime 3ms CodeModel.GetById 201ms app.highlight 16ms RepoModel.GetById 145ms app.codeStats 1ms

/installer.php

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