$backupfpath"; exec($backupcmd,$o,$bcstatus); if ($bcstatus) { error_condition("could not backup database to $backupfpath"); } if($verbose) { print (!empty($tableName))? "table $tableName " : "database "; print "backed up to $backupfpath\n"; } return $backupfpath; } // function error condition: // if a function called "rollback" is defined in the upgrade script // it calls it (note that rollback doesn't take any input args). // if rollback is not defined, it outputs whatever error message was passed // to it and dies function error_condition($message = "") { if(function_exists('rollback')) { print "\nERROR : " . $message ."\n"; rollback(); } else { die('ERROR: ' . $message); } } function fieldInTable($fieldname,$tablename) { $sql_check_field = sprintf("describe %s %s",$tablename,$fieldname); $check_field = mysql_query($sql_check_field) or die(mysql_error()); if(mysql_num_rows($check_field) > 0) return TRUE; else return FALSE; } //returns true if the variable is being assigned in the config file //otherwise returns false. //The variable should be specified without the $ symbol function variableInFile($varname,$vcpath) { //assign the contents of the file to an array of strings (one string for each line) $contentArray = file($vcpath); //the regular expression pattern to check if the variable already exists $pattern = sprintf("|[[:space:]]*\\$%s[[:space:]]*=.*|",$varname); $nLines = count($contentArray); for($iLine = 0;$iLine < $nLines; $iLine++) { $lineContent = $contentArray[$iLine]; if(preg_match($pattern,$lineContent)) { return TRUE; } } return FALSE; } // This function is useful for editing the variables_config.php file // or $variables.php. // Basically, it sees if the variable is being set in the config file. // If it is, it replaces the line by setting the variable to a new value. // Optionally, it can prompt the user whether or not to perform the replacement. // If the variable does not exist, it appends the variable to the end of the config file. //$vcpath = path to variables_config.php //$varname = the variable name to change/add //$value = the value to set $varname to //$warnBeforeReplace = whether or not to warn before replacing a value (default = true) //NEW FEATURE (11/16/09 S.T.): //Addition of "code" datattype //Code datatype allows the insertion of new code to config files e.g. include('filepath') //Pass the code to $varname, pass blank string to $value, and set $dataType="code" //to use this feature //NEW FEATURE (11/16/09 S.T.): //In order to remove a variable, use "" as the $value function changeConfigFile($vcpath,$varname,$value,$warnBeforeReplace = true,$dataType = "string") { print "\n\nEDITING $vcpath:\n\n"; //assign the contents of the file to an array of strings (one string for each line) $contentArray = file($vcpath); if(strcmp($dataType,"code") != 0) { //the regular expression pattern to check if the variable already exists $pattern = sprintf("|[[:space:]]*\\$%s[[:space:]]*=.*|",$varname); } else { $pattern = "|[[:space:]]*".preg_quote($varname)."[[:space:]]*|"; } if(!(strcmp($value,"") == 0)) { //the content to add or replace in the file switch($dataType) { case "string": $newContent = sprintf("$%s='%s';",$varname,$value); break; case "integer": case "double": $newContent = sprintf("$%s=%d;",$varname,$value); break; case "code": $newContent = $varname; break; default: error_condition(sprintf("function addVarToConfig, data type %s not recognized",$dataType)); } //$addContent flags whether or not to add content to the end of the file $addContent = true; } else { $addContent = false; $newContent = ""; } $nLines = count($contentArray); //$replaceContent flags whether or not the content was replaced $replaceContent = false; //loop over every line to try and find a match for the pattern for($iLine = 0;$iLine < $nLines; $iLine++) { $lineContent = $contentArray[$iLine]; if(preg_match($pattern,$lineContent)) { //if a match was found, do not add anything to end of file $addContent = false; //prompt first to see if user wants to replace the content if($warnBeforeReplace) { print "Found Line: $lineContent"; print "Replace with: $newContent\n"; $replaceConfirm = userConfirm(); } else $replaceConfirm = true; if($replaceConfirm) { $replaceContent = true; $contentArray[$iLine] = $newContent."\n\n"; break; } } //if preg_match } //for //if a match was found, $addContent should be false if($addContent) { print "Adding $newContent to the end of the file\n"; //pop from the end of contentArray until we find php end tag $popContent = ""; while(!preg_match("|.*\?\>|",$popContent)) $popContent = array_pop($contentArray); array_push($contentArray,$newContent."\n\n"); array_push($contentArray,"?>"); } //if we are either adding content, or replacing content //then rewrite the file. Otherwise, leave it alone if($addContent || $replaceContent) { $contentString = implode("",$contentArray); if (!file_put_contents($vcpath,$contentString)) { error_condition("ERROR writing to $vcpath\n\n"); } print "Done editing $vcpath\n"; } else { print "Leaving file $vcpath alone.\nMake sure the variables in this file are correct.\n\n"; } } ?>