$fieldName should not be empty"); } /** * * if directory not exists - create it * @param $dir */ public static function checkCreateDir($dir){ if(!is_dir($dir)) mkdir($dir); if(!is_dir($dir)) self::throwError("Could not create directory: $dir"); } /** * * delete file, validate if deleted */ public static function checkDeleteFile($filepath){ if(file_exists($filepath) == false) return(false); $success = @unlink($filepath); if($success == false) self::throwError("Failed to delete the file: $filepath"); } //------------------------------------------------------------ //filter array, leaving only needed fields - also array public static function filterArrFields($arr,$fields){ $arrNew = array(); foreach($fields as $field){ if(isset($arr[$field])) $arrNew[$field] = $arr[$field]; } return($arrNew); } //------------------------------------------------------------ //get path info of certain path with all needed fields public static function getPathInfo($filepath){ $info = pathinfo($filepath); //fix the filename problem if(!isset($info["filename"])){ $filename = $info["basename"]; if(isset($info["extension"])) $filename = substr($info["basename"],0,(-strlen($info["extension"])-1)); $info["filename"] = $filename; } return($info); } /** * Convert std class to array, with all sons * @param unknown_type $arr */ public static function convertStdClassToArray($arr){ $arr = (array)$arr; $arrNew = array(); foreach($arr as $key=>$item){ $item = (array)$item; $arrNew[$key] = $item; } return($arrNew); } //------------------------------------------------------------ //save some file to the filesystem with some text public static function writeFile($str,$filepath){ $fp = fopen($filepath,"w+"); fwrite($fp,$str); fclose($fp); } //------------------------------------------------------------ //save some file to the filesystem with some text public static function writeDebug($str,$filepath="debug.txt",$showInputs = true){ $post = print_r($_POST,true); $server = print_r($_SERVER,true); if(getType($str) == "array") $str = print_r($str,true); if($showInputs == true){ $output = "--------------------"."\n"; $output .= $str."\n"; $output .= "Post: ".$post."\n"; }else{ $output = "---"."\n"; $output .= $str . "\n"; } if(!empty($_GET)){ $get = print_r($_GET,true); $output .= "Get: ".$get."\n"; } //$output .= "Server: ".$server."\n"; $fp = fopen($filepath,"a+"); fwrite($fp,$output); fclose($fp); } /** * * clear debug file */ public static function clearDebug($filepath = "debug.txt"){ if(file_exists($filepath)) unlink($filepath); } /** * * save to filesystem the error */ public static function writeDebugError(Exception $e,$filepath = "debug.txt"){ $message = $e->getMessage(); $trace = $e->getTraceAsString(); $output = $message."\n"; $output .= $trace."\n"; $fp = fopen($filepath,"a+"); fwrite($fp,$output); fclose($fp); } //------------------------------------------------------------ //save some file to the filesystem with some text public static function addToFile($str,$filepath){ $fp = fopen($filepath,"a+"); fwrite($fp,"---------------------\n"); fwrite($fp,$str."\n"); fclose($fp); } //-------------------------------------------------------------- //check the php version. throw exception if the version beneath 5 private static function checkPHPVersion(){ $strVersion = phpversion(); $version = (float)$strVersion; if($version < 5) throw new Exception("You must have php5 and higher in order to run the application. Your php version is: $version"); } //-------------------------------------------------------------- // valiadte if gd exists. if not - throw exception private static function validateGD(){ if(function_exists('gd_info') == false) throw new Exception("You need GD library to be available in order to run this application. Please turn it on in php.ini"); } //-------------------------------------------------------------- //return if the json library is activated public static function isJsonActivated(){ return(function_exists('json_encode')); } /** * * encode array into json for client side */ public static function jsonEncodeForClientSide($arr){ $json = ""; if(!empty($arr)){ $json = json_encode($arr); $json = addslashes($json); } $json = "'".$json."'"; return($json); } /** * * decode json from the client side */ public static function jsonDecodeFromClientSide($data){ $data = stripslashes($data); $data = str_replace('\"','\"',$data); $data = json_decode($data); $data = (array)$data; return($data); } //-------------------------------------------------------------- //validate if some directory is writable, if not - throw a exception private static function validateWritable($name,$path,$strList,$validateExists = true){ if($validateExists == true){ //if the file/directory doesn't exists - throw an error. if(file_exists($path) == false) throw new Exception("$name doesn't exists"); } else{ //if the file not exists - don't check. it will be created. if(file_exists($path) == false) return(false); } if(is_writable($path) == false){ chmod($path,0755); //try to change the permissions if(is_writable($path) == false){ $strType = "Folder"; if(is_file($path)) $strType = "File"; $message = "$strType $name is doesn't have a write permissions. Those folders/files must have a write permissions in order that this application will work properly: $strList"; throw new Exception($message); } } } //-------------------------------------------------------------- //validate presets for identical keys public static function validatePresets(){ global $g_presets; if(empty($g_presets)) return(false); //check for duplicates $assoc = array(); foreach($g_presets as $preset){ $id = $preset["id"]; if(isset($assoc[$id])) throw new Exception("Double preset ID detected: $id"); $assoc[$id] = true; } } //-------------------------------------------------------------- //Get url of image for output public static function getImageOutputUrl($filename,$width=0,$height=0,$exact=false){ //exact validation: if($exact == "true" && (empty($width) || empty($height) )) self::throwError("Exact must have both - width and height"); $url = CMGlobals::$URL_GALLERY."?img=".$filename; if(!empty($width)) $url .= "&w=".$width; if(!empty($height)) $url .= "&h=".$height; if($exact == true) $url .= "&t=exact"; return($url); } /** * * get list of all files in the directory * ext - filter by his extension only */ public static function getFileList($path,$ext=""){ $dir = scandir($path); $arrFiles = array(); foreach($dir as $file){ if($file == "." || $file == "..") continue; if(!empty($ext)){ $info = pathinfo($file); $extension = UniteFunctionsRev::getVal($info, "extension"); if($ext != strtolower($extension)) continue; } $filepath = $path . "/" . $file; if(is_file($filepath)) $arrFiles[] = $file; } return($arrFiles); } /** * * get list of all files in the directory */ public static function getFoldersList($path){ $dir = scandir($path); $arrFiles = array(); foreach($dir as $file){ if($file == "." || $file == "..") continue; $filepath = $path . "/" . $file; if(is_dir($filepath)) $arrFiles[] = $file; } return($arrFiles); } /** * * do "trim" operation on all array items. */ public static function trimArrayItems($arr){ if(gettype($arr) != "array") UniteFunctionsRev::throwError("trimArrayItems error: The type must be array"); foreach ($arr as $key=>$item) $arr[$key] = trim($item); return($arr); } /** * * get url contents */ public static function getUrlContents($url,$arrPost=array(),$method = "post",$debug=false){ $ch = curl_init(); $timeout = 0; $strPost = ''; foreach($arrPost as $key=>$value){ if(!empty($strPost)) $strPost .= "&"; $value = urlencode($value); $strPost .= "$key=$value"; } //set curl options if(strtolower($method) == "post"){ curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS,$strPost); } else //get $url .= "?".$strPost; //remove me //Functions::addToLogFile(SERVICE_LOG_SERVICE, "url", $url); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $headers = array(); $headers[] = "User-Agent:Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8"; $headers[] = "Accept-Charset:utf-8;q=0.7,*;q=0.7"; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($ch); if($debug == true){ dmp($url); dmp($response); exit(); } if($response == false) throw new Exception("getUrlContents Request failed"); curl_close($ch); return($response); } /** * * get link html */ public static function getHtmlLink($link,$text,$id="",$class=""){ if(!empty($class)) $class = " class='$class'"; if(!empty($id)) $id = " id='$id'"; $html = "$text"; return($html); } /** * * get select from array */ public static function getHTMLSelect($arr,$default="",$htmlParams="",$assoc = false){ $html = ""; return($html); } /** * * Convert array to assoc array by some field */ public static function arrayToAssoc($arr,$field=null){ $arrAssoc = array(); foreach($arr as $item){ if(empty($field)) $arrAssoc[$item] = $item; else $arrAssoc[$item[$field]] = $item; } return($arrAssoc); } /** * * convert assoc array to array */ public static function assocToArray($assoc){ $arr = array(); foreach($assoc as $item) $arr[] = $item; return($arr); } /** * * strip slashes from textarea content after ajax request to server */ public static function normalizeTextareaContent($content){ if(empty($content)) return($content); $content = stripslashes($content); $content = trim($content); return($content); } /** * * get random array item */ public static function getRandomArrayItem($arr){ $numItems = count($arr); $rand = rand(0, $numItems-1); $item = $arr[$rand]; return($item); } /** * * recursive delete directory or file */ public static function deleteDir($path,$deleteOriginal = true, $arrNotDeleted = array(),$originalPath = ""){ if(empty($originalPath)) $originalPath = $path; //in case of paths array if(getType($path) == "array"){ $arrPaths = $path; foreach($path as $singlePath) $arrNotDeleted = self::deleteDir($singlePath,$deleteOriginal,$arrNotDeleted,$originalPath); return($arrNotDeleted); } if(!file_exists($path)) return($arrNotDeleted); if(is_file($path)){ // delete file $deleted = unlink($path); if(!$deleted) $arrNotDeleted[] = $path; } else{ //delete directory $arrPaths = scandir($path); foreach($arrPaths as $file){ if($file == "." || $file == "..") continue; $filepath = realpath($path."/".$file); $arrNotDeleted = self::deleteDir($filepath,$deleteOriginal,$arrNotDeleted,$originalPath); } if($deleteOriginal == true || $originalPath != $path){ $deleted = @rmdir($path); if(!$deleted) $arrNotDeleted[] = $path; } } return($arrNotDeleted); } /** * copy folder to another location. * */ public static function copyDir($source,$dest,$rel_path = "",$blackList = null){ $full_source = $source; if(!empty($rel_path)) $full_source = $source."/".$rel_path; $full_dest = $dest; if(!empty($full_dest)) $full_dest = $dest."/".$rel_path; if(!is_dir($full_source)) self::throwError("The source directroy: '$full_source' not exists."); if(!is_dir($full_dest)) mkdir($full_dest); $files = scandir($full_source); foreach($files as $file){ if($file == "." || $file == "..") continue; $path_source = $full_source."/".$file; $path_dest = $full_dest."/".$file; //validate black list $rel_path_file = $file; if(!empty($rel_path)) $rel_path_file = $rel_path."/".$file; //if the file or folder is in black list - pass it if(array_search($rel_path_file, $blackList) !== false) continue; //if file - copy file if(is_file($path_source)){ copy($path_source,$path_dest); } else{ //if directory - recursive copy directory if(empty($rel_path)) $rel_path_new = $file; else $rel_path_new = $rel_path."/".$file; self::copyDir($source,$dest,$rel_path_new,$blackList); } } } } ?>