00001 <?php
00029 class zFAccess extends zCore{
00041 function read($fileName, $projectRelative=true){
00042 if($projectRelative){
00043 $fileName = realpath(Z_PROJECT_ROOT.DRS.ltrim($fileName, '/'));
00044 }
00045 if(!file_exists($fileName)){
00046 perror("<code>zigmoyd.FAccess.read</code><br />The File $fileName does not exists");
00047 return false;
00048 }
00049 if(!is_readable($fileName)){
00050 perror("<code>zigmoyd.FAccess.read</code><br />The File $fileName is not readable");
00051 return false;
00052 }
00053 $fRes = fopen($fileName, 'rb');
00054 if($fRes){
00055 if(!$contents = fread($fRes, filesize($fileName))){
00056 perror("<code>zigmoyd.FAccess.read</code><br />Failed to Read $fileName");
00057 return false;
00058 }
00059 fclose($fRes);
00060 }else{
00061 perror("<code>zigmoyd.FAccess.read</code><br />Failed to Open $fileName for reading");
00062 return false;
00063 }
00064 return $contents;
00065 }
00078 function write($fileName, $data, $projectRelative=true){
00079 if($projectRelative){
00080 $fileName = (Z_PROJECT_ROOT.DRS.ltrim($fileName, '/'));
00081 }
00082 if($file = fopen($fileName, 'w')){
00083 $bytes = fwrite($file, is_array($data) ? implode('', $data) : $data);
00084 fclose($file); return $bytes;
00085 }else{
00086 perror("<code>zigmoyd.FAccess.write</code><br />Failed to Open $fileName for writing");
00087 return false;
00088 }
00089 }
00104 function parseINI($fileName, $processSections=false, $projectRelative=true){
00105 if($projectRelative){
00106 $fileName = realpath(Z_PROJECT_ROOT.DRS.'etc'.DRS.Z_DIR_ETC_CONF.DRS.ltrim($fileName, '/'));
00107 }
00108 if(!file_exists($fileName)){
00109 perror("<code>zigmoyd.FAccess.parseINI</code><br />The File $fileName does not exists");
00110 return false;
00111 }
00112 if(!is_readable($fileName)){
00113 perror("<code>zigmoyd.FAccess.parseINI</code><br />The File $fileName is not readable");
00114 return false;
00115 }
00116 return parse_ini_file($fileName, $processSections);
00117 }
00128 function listSiblings($baseDir, $pattern=null){
00129 if(!is_dir($baseDir)){
00130 perror("<code>zigmoyd.faccess.listSibling</code><br />The Directory $baseDir doesn't exist");
00131 return false;
00132 }
00133 if(!$dh = opendir($baseDir)){
00134 perror("<code>zigmoyd.faccess.listSibling</code><br />Failed to open the Directory $baseDir for reading");
00135 return false;
00136 }
00137 $files = array();
00138 while(false !== ($entry = readdir($dh))){
00139 if(!is_null($pattern)){
00140 $regx = preg_match($pattern, $entry);
00141 if(is_bool($regx) && !$regx){
00142 perror("<code>zigmoyd.zFAccess.listSiblings</code><br />Failed to match the Regular expression Provided. Invalid regular expression $pattern Supplied");
00143 return false;
00144 }
00145 }
00146 if(is_null($pattern) || (!is_null($pattern) && $regx > 0))
00147 $files[] = $entry;
00148 }
00149
00150 return $files;
00151 }
00162 function listSiblingFiles($baseDir, $pattern=null){
00163 if(!is_dir($baseDir)){
00164 perror("<code>zigmoyd.faccess.listSiblingFiles</code><br />The Directory $baseDir doesn't exist");
00165 return false;
00166 }
00167 if(!$dh = opendir($baseDir)){
00168 perror("<code>zigmoyd.faccess.listSiblingFiles</code><br />Failed to open the Directory $baseDir for reading");
00169 return false;
00170 }
00171 $files = array();
00172 while(false !== ($entry = readdir($dh))){
00173 if(is_null($pattern) || (!is_null($pattern) && preg_match($pattern, $entry) > 0))
00174 if(is_file($entry))
00175 $files[] = $entry;
00176 }
00177 sort($files);
00178 return $files;
00179 }
00190 function listSiblingDirs($baseDir, $pattern=null){
00191 if(!is_dir($baseDir)){
00192 perror("<code>zigmoyd.faccess.listSiblingDirs</code><br />The Directory $baseDir doesn't exist");
00193 return false;
00194 }
00195 if(!$dh = opendir($baseDir)){
00196 perror("<code>zigmoyd.faccess.listSiblingDirs</code><br />Failed to open the Directory $baseDir for reading");
00197 return false;
00198 }
00199 $files = array();
00200 while(false !== ($entry = readdir($dh))){
00201 if(is_null($pattern) || (!is_null($pattern) && preg_match($pattern, $entry) > 0))
00202 if(is_dir($entry))
00203 $files[] = $entry;
00204 }
00205 sort($files);
00206 return $files;
00207 }
00219 function tree($baseDir, $pattern=null){
00220 static $tree = array();
00221 foreach(zFAccess::listSiblings($baseDir, $pattern) as $entry){
00222 if(!($entry == '.' || $entry == '..')){
00223 array_push($tree, $baseDir.DRS.$entry);
00224 if(is_dir($baseDir.DRS.$entry)){
00225 zFAccess::tree($baseDir.DRS.$entry, $pattern);
00226 }
00227 }
00228 }
00229 return $tree;
00230 }
00242 function treeDirs($baseDir, $pattern=null){
00243 static $tree = array();
00244 foreach(zFAccess::listSiblings($baseDir, $pattern) as $entry){
00245 if((!($entry == '.' || $entry == '..')) && is_dir($baseDir.DRS.$entry)){
00246 if(!is_null($pattern)){
00247 $regx = preg_match($pattern, $entry);
00248 if(is_bool($regx) && !$regx){
00249 perror("<code>zigmoyd.zFAccess.listSiblings</code><br />Failed to match the Regular expression Provided. Invalid regular expression $pattern Supplied");
00250 return false;
00251 }
00252 }
00253 if(is_null($pattern) || (!is_null($pattern) && $regx > 0)){
00254 array_push($tree, $baseDir.DRS.$entry);
00255 }
00256 if(is_dir($baseDir.DRS.$entry)){
00257 zFAccess::treeDirs($baseDir.DRS.$entry, $pattern);
00258 }
00259 }
00260 }
00261 return $tree;
00262 }
00274 function treeFiles($baseDir, $pattern=null){
00275 static $tree = array();
00276 foreach(zFAccess::listSiblings($baseDir) as $entry){
00277 if(!($entry == '.' || $entry == '..')){
00278 if(file_exists($baseDir.DRS.$entry) && !is_dir($baseDir.DRS.$entry)){
00279 if(!is_null($pattern)){
00280 $regx = preg_match($pattern, $entry);
00281 if(is_bool($regx) && !$regx){
00282 perror("<code>zigmoyd.zFAccess.listSiblings</code><br />Failed to match the Regular expression Provided. Invalid regular expression $pattern Supplied");
00283 return false;
00284 }
00285 }
00286 if(is_null($pattern) || (!is_null($pattern) && $regx > 0)){
00287 array_push($tree, $baseDir.DRS.$entry);
00288 }
00289 }
00290 if(is_dir($baseDir.DRS.$entry)){
00291 zFAccess::treeFiles($baseDir.DRS.$entry, $pattern);
00292 }
00293 }
00294 }
00295 return $tree;
00296 }
00305 function getPerm($fileName, $fullPerm=false){
00306 if(!file_exists($fileName)){
00307 perror("<code>zigmoyd.FAccess.getPerm</code><br />The File $fileName does not exists");
00308 return false;
00309 }
00310 if(!$fullPerm){
00311 return substr(sprintf('%o', fileperms($fileName)), -4);
00312 }else{
00313 $perms = fileperms($fileName);
00314 if(($perms & 0xC000) == 0xC000){
00315 $info = 's';
00316 }elseif(($perms & 0xA000) == 0xA000){
00317 $info = 'l';
00318 }elseif(($perms & 0x8000) == 0x8000){
00319 $info = '-';
00320 }elseif(($perms & 0x6000) == 0x6000){
00321 $info = 'b';
00322 }elseif(($perms & 0x4000) == 0x4000){
00323 $info = 'd';
00324 }elseif(($perms & 0x2000) == 0x2000){
00325 $info = 'c';
00326 }elseif(($perms & 0x1000) == 0x1000){
00327 $info = 'p';
00328 }else{
00329 $info = 'u';
00330 }
00331 $info .= (($perms & 0x0100) ? 'r' : '-');
00332 $info .= (($perms & 0x0080) ? 'w' : '-');
00333 $info .= (($perms & 0x0040) ?
00334 (($perms & 0x0800) ? 's' : 'x' ) :
00335 (($perms & 0x0800) ? 'S' : '-'));
00336 $info .= (($perms & 0x0020) ? 'r' : '-');
00337 $info .= (($perms & 0x0010) ? 'w' : '-');
00338 $info .= (($perms & 0x0008) ?
00339 (($perms & 0x0400) ? 's' : 'x' ) :
00340 (($perms & 0x0400) ? 'S' : '-'));
00341 $info .= (($perms & 0x0004) ? 'r' : '-');
00342 $info .= (($perms & 0x0002) ? 'w' : '-');
00343 $info .= (($perms & 0x0001) ?
00344 (($perms & 0x0200) ? 't' : 'x' ) :
00345 (($perms & 0x0200) ? 'T' : '-'));
00346 return $info;
00347 }
00348 }
00358 function setPerm($fileName, $perm){
00359 if(!file_exists($fileName)){
00360 perror("<code>zigmoyd.FAccess.getPerm</code><br />The File $fileName does not exists");
00361 return false;
00362 }
00363 return chmod($fileName, $perm);
00364 }
00375 function copy($src, $dest, $recursive=true){
00376 if(!zFAccess::exists($src)){
00377 perror("<code>zigmoyd.zFAccess.copy</code><br />Source File/Directory $src doesnt exists");
00378 return false;
00379 }
00380 if(!$recursive){
00381 return copy($src, $dest);
00382 }else{
00383 if(is_dir($dest))zFAccess::remove($dest);
00384 if(!mkdir($dest)){
00385 perror("cannot make Directory `$dest`");
00386 }
00387 $d = dir($src);
00388 while(FALSE !==($entry = $d->read())){
00389 if($entry == '.' || $entry == '..'){continue;}
00390 $entry = $src . '/' . $entry;
00391 if(is_dir($entry)){
00392 if(!zFAccess::copy($entry, $dest . '/' . pathinfo($entry, PATHINFO_BASENAME))){
00393 return false;
00394 }
00395 continue;
00396 }
00397 if(!copy($entry, $dest . '/' . pathinfo($entry, PATHINFO_BASENAME))){
00398 perror("Couldn't Copy `$entry` to `$dest`");
00399 return false;
00400 }
00401 }
00402 $d->close();
00403 return true;
00404 }
00405 }
00416 function move($src, $dest, $recursive=true){
00417 if(!zFAccess::copy($src, $dest, $recursive)){
00418 perror("<code>zigmoyd.FAccess.move</code><br />Failed to Copy $src To $dest");
00419 return false;
00420 }
00421 if(!zFAccess::remove($src, $recursive)){
00422 perror("<code>zigmoyd.FAccess.move</code><br />Failed to remove Source Directory $src after Copying the Files");
00423 return false;
00424 }
00425 return true;
00426 }
00435 function rename($oldName, $newName){
00436 if(!zFAccess::exists($oldName)){
00437 perror("<code>zigmoyd.FAccess.remove</code><br />source File/Directory $src doesn't exists so removing failed");
00438 return false;
00439 }
00440 return rename($oldName, $newName);
00441 }
00450 function remove($src){
00451 if(!zFAccess::exists($src)){
00452 perror("<code>zigmoyd.FAccess.remove</code><br />File/Directory $src doesn't exists");
00453 return false;
00454 }
00455 if(is_file($src)){
00456 return unlink($src);
00457 }else{
00458 foreach(glob($src.'/*') as $sf){
00459 if(is_dir($sf) && !is_link($sf)){
00460 zFAccess::remove($sf);
00461 }else{
00462 unlink($sf);
00463 }
00464 }
00465 return rmdir($src);
00466 }
00467 }
00474 function exists($src){
00475 return (is_dir($src) || file_exists($src));
00476 }
00483 function fileExists($src){
00484 return file_exists($src);
00485 }
00492 function dirExists($src){
00493 return is_dir($src);
00494 }
00503 function mkDir($dirName, $perm=0777, $recursive=false){
00504 if(!$recursive){
00505 if(!is_dir(dirname($dirName))){
00506 perror("<code>zigmoyd.FAccess.mkdir</code><br />Parent Directory of $dirName doesnt Exists plese use recursive Option");
00507 return false;
00508 }
00509 return mkdir($dirName, $perm);
00510 }else{
00511 is_dir(dirname($dirName)) || zFAccess::mkDir(dirname($dirName), $perm, $recursive);
00512 return is_dir($dirName) || mkdir($dirName, $perm);
00513 }
00514 }
00522 function getExtension($fileName){
00523 $tmp = pathinfo($fileName);
00524 return @$tmp['extension'];
00525 }
00534 function createBlankFile($fileName, $projectRelative=true){
00535 return zFAccess::write($fileName, '', $projectRelative);
00536 }
00545 function getFileSize($fileName, $projectRelative=true){
00546 if($projectRelative){
00547 $fileName = realpath(Z_PROJECT_ROOT.DRS.ltrim($fileName, '/'));
00548 }
00549 if(!file_exists($fileName)){
00550 perror("<code>zigmoyd.FAccess.getFileSize</code><br />File $fileName doesn't exist");
00551 return false;
00552 }
00553 return filesize($fileName);
00554 }
00555 }
00557 ?>