소스 검색

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

migrated install_switch's recursive_* routines
FusionPBX 9 년 전
부모
커밋
437fc52f3b
2개의 변경된 파일82개의 추가작업 그리고 130개의 파일을 삭제
  1. 3 100
      core/install/resources/classes/install_switch.php
  2. 79 30
      resources/functions.php

+ 3 - 100
core/install/resources/classes/install_switch.php

@@ -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){
 			if (!is_readable($dir)) {
 				throw new Exception("backup_dir() source directory '".$dir."' does not exist.");
@@ -208,7 +111,7 @@ include "root.php";
 			//make a backup of the config
 				if (file_exists($this->global_settings->switch_conf_dir())) {
 					$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
 				if (!is_dir($this->global_settings->switch_conf_dir())) {
@@ -225,7 +128,7 @@ include "root.php";
 				}
 				$dst_dir = $this->global_settings->switch_conf_dir();
 				if (is_readable($dst_dir)) {
-					$this->recursive_copy($src_dir, $dst_dir);
+					recursive_copy($src_dir, $dst_dir);
 					unset($src_dir, $dst_dir);
 				}
 				$fax_dir = join( DIRECTORY_SEPARATOR, array($this->global_settings->switch_storage_dir(), 'fax'));
@@ -273,7 +176,7 @@ include "root.php";
 				}
 				$dst_dir = $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);
 				}else{
 					throw new Exception("Cannot read from '$src_dir' to get the scripts");

+ 79 - 30
resources/functions.php

@@ -144,27 +144,78 @@
 	}
 
 	if (!function_exists('recursive_copy')) {
-		function recursive_copy($src,$dst) {
-			$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."'");
+		if (file_exists('/bin/cp')) {
+			function recursive_copy($src, $dst, $options = '') {
+				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;
 				}
-			}
-			while(false !== ( $file = readdir($dir)) ) {
-				if (( $file != '.' ) && ( $file != '..' )) {
-					if ( is_dir($src . '/' . $file) ) {
-						recursive_copy($src . '/' . $file,$dst . '/' . $file);
+				$this->write_debug($cmd);
+				exec ($cmd);
+			}
+		}elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'){
+			function recursive_copy($src, $dst, $options = '') {
+				$src = normalize_path_to_os($src);
+				$dst = normalize_path_to_os($dst);
+				exec("xcopy /E /Y \"$src\" \"$dst\"");
+			}
+		}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."'");
 					}
-					else {
-						copy($src . '/' . $file,$dst . '/' . $file);
+				}
+				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
-	if (!function_exists('email_validate')) {
-		function email_validate($strEmail){
-			$validRegExp =  '/^[a-zA-Z0-9\._-]+@[a-zA-Z0-9\._-]+\.[a-zA-Z]{2,3}$/';
-			// search email text for regular exp matches
-			preg_match($validRegExp, $strEmail, $matches, PREG_OFFSET_CAPTURE);
-
-			if (count($matches) == 0) {
-				return 0;
-			}
-			else {
-				return 1;
-			}
-		}
-	}
+	function email_validate($strEmail){
+	   $validRegExp =  '/^[a-zA-Z0-9\._-]+@[a-zA-Z0-9\._-]+\.[a-zA-Z]{2,3}$/';
+	   // search email text for regular exp matches
+	   preg_match($validRegExp, $strEmail, $matches, PREG_OFFSET_CAPTURE);
+
+	   if (count($matches) == 0) {
+		return 0;
+	   }
+	   else {
+		return 1;
+	   }
+}
 
 ?>