Merge pull request #1339 from mafoo/Enhance-resouce/functions.php

migrated install_switch's recursive_* routines
This commit is contained in:
FusionPBX 2015-12-21 04:25:29 -07:00
commit f156805c8c
2 changed files with 81 additions and 129 deletions

View File

@ -69,103 +69,6 @@ include "root.php";
} }
} }
//$options '-n' --no-clobber
protected function recursive_copy($src, $dst, $options = '') {
if (file_exists('/bin/cp')) {
if (strtoupper(substr(PHP_OS, 0, 3)) === 'SUN') {
//copy -R recursive, preserve attributes for SUN
$cmd = 'cp -Rp '.$src.'/* '.$dst;
} else {
//copy -R recursive, -L follow symbolic links, -p preserve attributes for other Posix systemss
$cmd = 'cp -RLp '.$options.' '.$src.'/* '.$dst;
}
$this->write_debug($cmd);
exec ($cmd);
}
elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'){
$src = normalize_path_to_os($src);
$dst = normalize_path_to_os($dst);
exec("xcopy /E /Y \"$src\" \"$dst\"");
}
else {
throw new Exception('Could not perform copy operation on this platform, implementation missing');
$dir = opendir($src);
if (!$dir) {
if (!mkdir($src, 0755, true)) {
throw new Exception("recursive_copy() source directory '".$src."' does not exist.");
}
}
if (!is_dir($dst)) {
if (!mkdir($dst, 0755, true)) {
throw new Exception("recursive_copy() failed to create destination directory '".$dst."'");
}
}
//This looks wrong, essentially if we can't use /bin/cp it manually fils dirs, not correct
$script_dir_target = $_SESSION['switch']['scripts']['dir'];
$script_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') {
$this->write_debug($file_path_source.' ---> '.$dst);
copy($file_path_source, $dst);
chmod($dst, 0755);
}
}
}
while(false !== ($file = readdir($dir))) {
if (($file != '.') && ($file != '..')) {
if (is_dir($src.'/'.$file)) {
$this->recursive_copy($src.'/'.$file, $dst.'/'.$file);
}
else {
//copy only missing files -n --no-clobber
if (strpos($options,'-n') !== false) {
if (!file_exists($dst.'/'.$file)) {
$this->write_debug("copy(".$src."/".$file.", ".$dst."/".$file.")");
copy($src.'/'.$file, $dst.'/'.$file);
}
}
else {
copy($src.'/'.$file, $dst.'/'.$file);
}
}
}
}
closedir($dir);
}
}
protected function recursive_delete($dir) {
if (file_exists('/bin/rm')) {
$this->write_debug('rm -Rf '.$dir.'/*');
exec ('rm -Rf '.$dir.'/*');
}
elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'){
$dst = normalize_path_to_os($dst);
$this->write_debug("del /S /F /Q \"$dir\"");
exec("del /S /F /Q \"$dir\"");
}
else {
foreach (glob($dir) as $file) {
if (is_dir($file)) {
$this->write_debug("rm dir: ".$file);
$this->recursive_delete("$file/*");
rmdir($file);
} else {
$this->write_debug("delete file: ".$file);
unlink($file);
}
}
}
clearstatcache();
}
protected function backup_dir($dir, $backup_name){ protected function backup_dir($dir, $backup_name){
if (!is_readable($dir)) { if (!is_readable($dir)) {
throw new Exception("backup_dir() source directory '".$dir."' does not exist."); throw new Exception("backup_dir() source directory '".$dir."' does not exist.");
@ -208,7 +111,7 @@ include "root.php";
//make a backup of the config //make a backup of the config
if (file_exists($this->global_settings->switch_conf_dir())) { if (file_exists($this->global_settings->switch_conf_dir())) {
$this->backup_dir($this->global_settings->switch_conf_dir(), 'fusionpbx_switch_config'); $this->backup_dir($this->global_settings->switch_conf_dir(), 'fusionpbx_switch_config');
$this->recursive_delete($this->global_settings->switch_conf_dir()); recursive_delete($this->global_settings->switch_conf_dir());
} }
//make sure the conf directory exists //make sure the conf directory exists
if (!is_dir($this->global_settings->switch_conf_dir())) { if (!is_dir($this->global_settings->switch_conf_dir())) {
@ -225,7 +128,7 @@ include "root.php";
} }
$dst_dir = $this->global_settings->switch_conf_dir(); $dst_dir = $this->global_settings->switch_conf_dir();
if (is_readable($dst_dir)) { if (is_readable($dst_dir)) {
$this->recursive_copy($src_dir, $dst_dir); recursive_copy($src_dir, $dst_dir);
unset($src_dir, $dst_dir); unset($src_dir, $dst_dir);
} }
$fax_dir = join( DIRECTORY_SEPARATOR, array($this->global_settings->switch_storage_dir(), 'fax')); $fax_dir = join( DIRECTORY_SEPARATOR, array($this->global_settings->switch_storage_dir(), 'fax'));
@ -273,7 +176,7 @@ include "root.php";
} }
$dst_dir = $script_dir; $dst_dir = $script_dir;
if (is_readable($script_dir)) { if (is_readable($script_dir)) {
$this->recursive_copy($src_dir, $dst_dir, $_SESSION['scripts']['options']['text']); recursive_copy($src_dir, $dst_dir, $_SESSION['scripts']['options']['text']);
unset($src_dir, $dst_dir); unset($src_dir, $dst_dir);
}else{ }else{
throw new Exception("Cannot read from '$src_dir' to get the scripts"); throw new Exception("Cannot read from '$src_dir' to get the scripts");

View File

@ -144,27 +144,78 @@
} }
if (!function_exists('recursive_copy')) { if (!function_exists('recursive_copy')) {
function recursive_copy($src,$dst) { if (file_exists('/bin/cp')) {
$dir = opendir($src); function recursive_copy($src, $dst, $options = '') {
if (!$dir) { if (strtoupper(substr(PHP_OS, 0, 3)) === 'SUN') {
throw new Exception("recursive_copy() source directory '".$src."' does not exist."); //copy -R recursive, preserve attributes for SUN
} $cmd = 'cp -Rp '.$src.'/* '.$dst;
if (!is_dir($dst)) { } else {
if (!mkdir($dst)) { //copy -R recursive, -L follow symbolic links, -p preserve attributes for other Posix systemss
throw new Exception("recursive_copy() failed to create destination directory '".$dst."'"); $cmd = 'cp -RLp '.$options.' '.$src.'/* '.$dst;
} }
$this->write_debug($cmd);
exec ($cmd);
} }
while(false !== ( $file = readdir($dir)) ) { }elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'){
if (( $file != '.' ) && ( $file != '..' )) { function recursive_copy($src, $dst, $options = '') {
if ( is_dir($src . '/' . $file) ) { $src = normalize_path_to_os($src);
recursive_copy($src . '/' . $file,$dst . '/' . $file); $dst = normalize_path_to_os($dst);
} exec("xcopy /E /Y \"$src\" \"$dst\"");
else { }
copy($src . '/' . $file,$dst . '/' . $file); }else{
function recursive_copy($src, $dst, $options = '') {
$dir = opendir($src);
if (!$dir) {
throw new Exception("recursive_copy() source directory '".$src."' does not exist.");
}
if (!is_dir($dst)) {
if (!mkdir($dst)) {
throw new Exception("recursive_copy() failed to create destination directory '".$dst."'");
} }
} }
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
recursive_copy($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
}
}
}
if (!function_exists('recursive_delete')) {
if (file_exists('/bin/rm')) {
protected function recursive_delete($dir) {
$this->write_debug('rm -Rf '.$dir.'/*');
exec ('rm -Rf '.$dir.'/*');
clearstatcache();
}
}elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'){
protected function recursive_delete($dir) {
$dst = normalize_path_to_os($dst);
$this->write_debug("del /S /F /Q \"$dir\"");
exec("del /S /F /Q \"$dir\"");
clearstatcache();
}
}else{
protected function recursive_delete($dir) {
foreach (glob($dir) as $file) {
if (is_dir($file)) {
$this->write_debug("rm dir: ".$file);
$this->recursive_delete("$file/*");
rmdir($file);
} else {
$this->write_debug("delete file: ".$file);
unlink($file);
}
}
clearstatcache();
} }
closedir($dir);
} }
} }
@ -1326,19 +1377,17 @@ function number_pad($number,$n) {
} }
//email validate //email validate
if (!function_exists('email_validate')) { function email_validate($strEmail){
function email_validate($strEmail){ $validRegExp = '/^[a-zA-Z0-9\._-]+@[a-zA-Z0-9\._-]+\.[a-zA-Z]{2,3}$/';
$validRegExp = '/^[a-zA-Z0-9\._-]+@[a-zA-Z0-9\._-]+\.[a-zA-Z]{2,3}$/'; // search email text for regular exp matches
// search email text for regular exp matches preg_match($validRegExp, $strEmail, $matches, PREG_OFFSET_CAPTURE);
preg_match($validRegExp, $strEmail, $matches, PREG_OFFSET_CAPTURE);
if (count($matches) == 0) { if (count($matches) == 0) {
return 0; return 0;
} }
else { else {
return 1; return 1;
} }
} }
}
?> ?>