Improve code to update the scripts directory.

This commit is contained in:
markjcrane 2015-07-20 15:32:52 -06:00
parent 6f9eb6ab29
commit b8f9b423c7
3 changed files with 147 additions and 145 deletions

View File

@ -17,138 +17,133 @@
The Initial Developer of the Original Code is The Initial Developer of the Original Code is
Mark J Crane <markjcrane@fusionpbx.com> Mark J Crane <markjcrane@fusionpbx.com>
Portions created by the Initial Developer are Copyright (C) 2008-2012 Portions created by the Initial Developer are Copyright (C) 2008-2015
the Initial Developer. All Rights Reserved. the Initial Developer. All Rights Reserved.
Contributor(s): Contributor(s):
Mark J Crane <markjcrane@fusionpbx.com> Mark J Crane <markjcrane@fusionpbx.com>
*/ */
if (strlen($_SESSION['switch']['scripts']['dir']) > 0) {
if ($domains_processed == 1) { if ($domains_processed == 1) {
//if the resource scripts resource directory does not exist then create it //process if the scripts directory exists
if (!is_dir($_SESSION['switch']['scripts']['dir']."/resources")) { if (strlen($_SESSION['switch']['scripts']['dir']) > 0) {
//if the resource scripts resource directory does not exist then create it
if (!is_dir($_SESSION['switch']['scripts']['dir']."/resources")) {
mkdir($_SESSION['switch']['scripts']['dir']."/resources",0755,true); mkdir($_SESSION['switch']['scripts']['dir']."/resources",0755,true);
}
//copy the files and directories from resources/install
$install = new install;
$install->domain_uuid = $domain_uuid;
$install->switch_scripts_dir = $_SESSION['switch']['scripts']['dir'];
$install->copy_scripts();
}
//update the software table
$sql = "select count(*) as num_rows from v_software ";
$prep_statement = $db->prepare($sql);
if ($prep_statement) {
$prep_statement->execute();
$row = $prep_statement->fetch(PDO::FETCH_ASSOC);
if ($row['num_rows'] == 0) {
$sql = "insert into v_software ";
$sql .= "(";
$sql .= "software_uuid, ";
$sql .= "software_name, ";
$sql .= "software_url, ";
$sql .= "software_version ";
$sql .= ")";
$sql .= "values ";
$sql .= "(";
$sql .= "'".uuid()."', ";
$sql .= "'FusionPBX', ";
$sql .= "'www.fusionpbx.com', ";
$sql .= "'".software_version()."' ";
$sql .= ")";
$db->exec(check_sql($sql));
unset($sql);
} }
else {
//copy the files and directories from resources/install $sql = "update v_software ";
//require_once "resources/classes/install.php"; $sql .= "set software_version = '".software_version()."' ";
$install = new install; $db->exec(check_sql($sql));
$install->domain_uuid = $domain_uuid; unset($sql);
$install->domain_name = $domain;
//$install->switch_conf_dir = $_SESSION['switch']['conf']['dir'];
$install->switch_scripts_dir = $_SESSION['switch']['scripts']['dir'];
//$install->switch_sounds_dir = $_SESSION['switch']['sounds']['dir'];
$install->copy_scripts();
//$install->copy_sounds();
//print_r($install->result);
//update the software table
$sql = "select count(*) as num_rows from v_software ";
$prep_statement = $db->prepare($sql);
if ($prep_statement) {
$prep_statement->execute();
$row = $prep_statement->fetch(PDO::FETCH_ASSOC);
if ($row['num_rows'] == 0) {
$sql = "insert into v_software ";
$sql .= "(";
$sql .= "software_uuid, ";
$sql .= "software_name, ";
$sql .= "software_url, ";
$sql .= "software_version ";
$sql .= ")";
$sql .= "values ";
$sql .= "(";
$sql .= "'".uuid()."', ";
$sql .= "'FusionPBX', ";
$sql .= "'www.fusionpbx.com', ";
$sql .= "'".software_version()."' ";
$sql .= ")";
$db->exec(check_sql($sql));
unset($sql);
}
else {
$sql = "update v_software ";
$sql .= "set software_version = '".software_version()."' ";
$db->exec(check_sql($sql));
unset($sql);
}
unset($prep_statement, $row);
} }
unset($prep_statement, $row);
}
//ensure the login message is set, if new message exists //ensure the login message is set, if new message exists
$sql = "select count(*) as num_rows from v_default_settings "; $sql = "select count(*) as num_rows from v_default_settings ";
$sql .= "where default_setting_category = 'login' "; $sql .= "where default_setting_category = 'login' ";
$sql .= "and default_setting_subcategory = 'message' "; $sql .= "and default_setting_subcategory = 'message' ";
$sql .= "and default_setting_name = 'text' "; $sql .= "and default_setting_name = 'text' ";
$prep_statement = $db->prepare($sql); $prep_statement = $db->prepare($sql);
if ($prep_statement) { if ($prep_statement) {
$prep_statement->execute(); $prep_statement->execute();
$row = $prep_statement->fetch(PDO::FETCH_ASSOC); $row = $prep_statement->fetch(PDO::FETCH_ASSOC);
unset($prep_statement); unset($prep_statement);
if ($row['num_rows'] == 0) { if ($row['num_rows'] == 0) {
// insert message // insert message
$sql = "insert into v_default_settings "; $sql = "insert into v_default_settings ";
$sql .= "("; $sql .= "(";
$sql .= "default_setting_uuid, "; $sql .= "default_setting_uuid, ";
$sql .= "default_setting_category, "; $sql .= "default_setting_category, ";
$sql .= "default_setting_subcategory, "; $sql .= "default_setting_subcategory, ";
$sql .= "default_setting_name, "; $sql .= "default_setting_name, ";
$sql .= "default_setting_value, "; $sql .= "default_setting_value, ";
$sql .= "default_setting_enabled, "; $sql .= "default_setting_enabled, ";
$sql .= "default_setting_description "; $sql .= "default_setting_description ";
$sql .= ")"; $sql .= ")";
$sql .= "values "; $sql .= "values ";
$sql .= "("; $sql .= "(";
$sql .= "'".uuid()."', "; $sql .= "'".uuid()."', ";
$sql .= "'login', "; $sql .= "'login', ";
$sql .= "'message', "; $sql .= "'message', ";
$sql .= "'text', "; $sql .= "'text', ";
$sql .= "'".$text['login-message_text']."', "; $sql .= "'".$text['login-message_text']."', ";
$sql .= "'true', "; $sql .= "'true', ";
$sql .= "'' "; $sql .= "'' ";
$sql .= ")"; $sql .= ")";
$db->exec(check_sql($sql)); $db->exec(check_sql($sql));
unset($sql); unset($sql);
} }
else { else {
// get current message value // get current message value
$sql = "select default_setting_uuid, default_setting_value "; $sql = "select default_setting_uuid, default_setting_value ";
$sql .= "from v_default_settings "; $sql .= "from v_default_settings ";
$sql .= "where default_setting_category = 'login' "; $sql .= "where default_setting_category = 'login' ";
$sql .= "and default_setting_subcategory = 'message' "; $sql .= "and default_setting_subcategory = 'message' ";
$sql .= "and default_setting_name = 'text' "; $sql .= "and default_setting_name = 'text' ";
$prep_statement = $db->prepare($sql); $prep_statement = $db->prepare($sql);
if ($prep_statement) { if ($prep_statement) {
$prep_statement->execute(); $prep_statement->execute();
$result = $prep_statement->fetchAll(PDO::FETCH_NAMED); $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
if (count($result) > 0) { if (count($result) > 0) {
foreach($result as $row) { foreach($result as $row) {
$current_default_setting_uuid = $row["default_setting_uuid"]; $current_default_setting_uuid = $row["default_setting_uuid"];
$current_default_setting_value = $row["default_setting_value"]; $current_default_setting_value = $row["default_setting_value"];
break; break;
} }
// compare to message in language file, update and enable if different // compare to message in language file, update and enable if different
$new_default_setting_value = str_replace("''", "'", $text['login-message_text']); $new_default_setting_value = str_replace("''", "'", $text['login-message_text']);
if ($current_default_setting_value != $new_default_setting_value) { if ($current_default_setting_value != $new_default_setting_value) {
$sql = "update v_default_settings set "; $sql = "update v_default_settings set ";
$sql .= "default_setting_value = '".$text['login-message_text']."', "; $sql .= "default_setting_value = '".$text['login-message_text']."', ";
$sql .= "default_setting_enabled = 'true' "; $sql .= "default_setting_enabled = 'true' ";
$sql .= "where default_setting_uuid = '".$current_default_setting_uuid."' "; $sql .= "where default_setting_uuid = '".$current_default_setting_uuid."' ";
$db->exec(check_sql($sql)); $db->exec(check_sql($sql));
unset($sql); unset($sql);
}
} }
unset($prep_statement, $result);
} }
unset($prep_statement, $result);
} }
} }
}
}
} }
?> ?>

View File

@ -17,7 +17,7 @@
The Initial Developer of the Original Code is The Initial Developer of the Original Code is
Mark J Crane <markjcrane@fusionpbx.com> Mark J Crane <markjcrane@fusionpbx.com>
Portions created by the Initial Developer are Copyright (C) 2008-2012 Portions created by the Initial Developer are Copyright (C) 2008-2015
the Initial Developer. All Rights Reserved. the Initial Developer. All Rights Reserved.
Contributor(s): Contributor(s):
@ -70,28 +70,18 @@ if (sizeof($_POST) > 0) {
} }
else { else {
//update scripts folder, if allowed (default) //update scripts folder, if allowed (default)
if ($_SESSION['switch']['scripts_update']['boolean'] != 'false' && $_SESSION['switch']['scripts']['dir'] != '') { if ($_SESSION['switch']['scripts']['dir'] != '') {
$scripts_dir_target = $_SESSION['switch']['scripts']['dir']; //copy the files and directories from resources/install
$scripts_dir_source = realpath($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/resources/install/scripts'); $install = new install;
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($scripts_dir_source)) as $file_path_source) { $install->domain_uuid = $domain_uuid;
if ( $install->switch_scripts_dir = $_SESSION['switch']['scripts']['dir'];
substr_count($file_path_source, '/..') == 0 && $install->copy_scripts();
substr_count($file_path_source, '/.') == 0 && //set the message
substr_count($file_path_source, '/.svn') == 0 && $response_message = $text['message-upgrade_source_scripts'];
substr_count($file_path_source, '/.git') == 0
) {
$file_path_target = str_replace($scripts_dir_source, $scripts_dir_target, $file_path_source);
if ($file_path_target != $scripts_dir_target.'/resources/config.lua') {
//echo $file_path_source.' ---> '.$file_path_target.'<br>';
copy($file_path_source, $file_path_target);
chmod($file_path_target, 0755);
}
}
}
$response_message = $text['message-upgrade_source_scripts'];
} }
else { else {
$response_message = $text['message-upgrade_source']; //set the message
$response_message = $text['message-upgrade_source'];
} }
} }
} }

View File

@ -17,7 +17,7 @@
The Initial Developer of the Original Code is The Initial Developer of the Original Code is
Mark J Crane <markjcrane@fusionpbx.com> Mark J Crane <markjcrane@fusionpbx.com>
Copyright (C) 2010-2014 Copyright (C) 2010-2015
All Rights Reserved. All Rights Reserved.
Contributor(s): Contributor(s):
@ -35,16 +35,18 @@ include "root.php";
var $switch_scripts_dir; var $switch_scripts_dir;
var $switch_sounds_dir; var $switch_sounds_dir;
//$option '-n' --no-clobber //$options '-n' --no-clobber
public function recursive_copy($src, $dst, $option = '') { public function recursive_copy($src, $dst, $options = '') {
if (file_exists('/bin/cp')) { if (file_exists('/bin/cp')) {
if (strtoupper(substr(PHP_OS, 0, 3)) === 'SUN') { if (strtoupper(substr(PHP_OS, 0, 3)) === 'SUN') {
//copy -R recursive, preserve attributes for SUN //copy -R recursive, preserve attributes for SUN
exec ('cp -Rp '.$src.'/* '.$dst); $cmd = 'cp -Rp '.$src.'/* '.$dst;
} else { } else {
//copy -R recursive, -L follow symbolic links, -p preserve attributes for other Posix systemss //copy -R recursive, -L follow symbolic links, -p preserve attributes for other Posix systemss
exec ('cp -RLp '.$option.' '.$src.'/* '.$dst); $cmd = 'cp -RLp '.$options.' '.$src.'/* '.$dst;
} }
exec ($cmd);
//echo $cmd."\n";
} }
else { else {
$dir = opendir($src); $dir = opendir($src);
@ -58,6 +60,23 @@ include "root.php";
throw new Exception("recursive_copy() failed to create destination directory '".$dst."'"); throw new Exception("recursive_copy() failed to create destination directory '".$dst."'");
} }
} }
$scripts_dir_target = $_SESSION['switch']['scripts']['dir'];
$scripts_dir_source = realpath($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH.'/resources/install/scripts');
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($src)) as $file_path_source) {
if (
substr_count($file_path_source, '/..') == 0 &&
substr_count($file_path_source, '/.') == 0 &&
substr_count($file_path_source, '/.svn') == 0 &&
substr_count($file_path_source, '/.git') == 0
) {
if ($dst != $src.'/resources/config.lua') {
//echo $file_path_source.' ---> '.$dst.'<br>';
copy($file_path_source, $dst);
chmod($dst, 0755);
}
}
}
while(false !== ($file = readdir($dir))) { while(false !== ($file = readdir($dir))) {
if (($file != '.') && ($file != '..')) { if (($file != '.') && ($file != '..')) {
if (is_dir($src.'/'.$file)) { if (is_dir($src.'/'.$file)) {
@ -65,7 +84,7 @@ include "root.php";
} }
else { else {
//copy only missing files -n --no-clobber //copy only missing files -n --no-clobber
if ($option == '-n') { if (strpos($options,'-n') !== false) {
if (!file_exists($dst.'/'.$file)) { if (!file_exists($dst.'/'.$file)) {
copy($src.'/'.$file, $dst.'/'.$file); copy($src.'/'.$file, $dst.'/'.$file);
//echo "copy(".$src."/".$file.", ".$dst."/".$file.");<br />\n"; //echo "copy(".$src."/".$file.", ".$dst."/".$file.");<br />\n";
@ -126,12 +145,10 @@ include "root.php";
} }
} }
//copy resources/templates/conf to the freeswitch conf dir //copy resources/templates/conf to the freeswitch conf dir
// added /examples/ into the string
if (file_exists('/usr/share/examples/fusionpbx/resources/templates/conf')){ if (file_exists('/usr/share/examples/fusionpbx/resources/templates/conf')){
$src_dir = "/usr/share/examples/fusionpbx/resources/templates/conf"; $src_dir = "/usr/share/examples/fusionpbx/resources/templates/conf";
} }
else { else {
$src_dir = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/resources/templates/conf"; $src_dir = $_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/resources/templates/conf";
} }
$dst_dir = $this->switch_conf_dir; $dst_dir = $this->switch_conf_dir;
@ -152,7 +169,7 @@ include "root.php";
} }
$dst_dir = $this->switch_scripts_dir; $dst_dir = $this->switch_scripts_dir;
if (is_readable($this->switch_scripts_dir)) { if (is_readable($this->switch_scripts_dir)) {
$this->recursive_copy($src_dir, $dst_dir, "-n"); $this->recursive_copy($src_dir, $dst_dir, $_SESSION['scripts']['options']['text']);
unset($src_dir, $dst_dir); unset($src_dir, $dst_dir);
} }
chmod($dst_dir, 0774); chmod($dst_dir, 0774);