tbrehm
2013-02-22 526b997c9891a796b152cdbab8e329b356b1f596
server/lib/classes/file.inc.php
@@ -33,14 +33,14 @@
    function rf($file){
      global $app;
      clearstatcache();
      if(!$fp = fopen ($file, "rb")){
        $app->log("WARNING: could not open file ".$file, 2);
      if(!$fp = fopen ($file, 'rb')){
        $app->log('WARNING: Could not open file '.$file, 2);
        return false;
      } else {
        if(filesize($file) > 0){
          $content = fread($fp, filesize($file));
        } else {
          $content = "";
          $content = '';
        }
        fclose($fp);
        return $content;
@@ -50,8 +50,8 @@
    function wf($file, $content){
      global $app;
      $this->mkdirs(dirname($file));
      if(!$fp = fopen ($file, "wb")){
        $app->log("WARNING: could not open file ".$file, 2);
      if(!$fp = fopen ($file, 'wb')){
        $app->log('WARNING: Could not open file '.$file, 2);
        return false;
      } else {
        fwrite($fp,$content);
@@ -63,8 +63,8 @@
    function af($file, $content){
      global $app;
      $this->mkdirs(dirname($file));
      if(!$fp = fopen ($file, "ab")){
        $app->log("WARNING: could not open file ".$file, 2);
      if(!$fp = fopen ($file, 'ab')){
        $app->log('WARNING: Could not open file '.$file, 2);
        return false;
      } else {
        fwrite($fp,$content);
@@ -83,7 +83,7 @@
            if($pos != 0){
              $new_lines[] = substr($line,0,$pos);
            } else {
              $new_lines[] = "";
              $new_lines[] = '';
            }
          } else {
            $new_lines[] = $line;
@@ -95,7 +95,7 @@
        $new_lines = NULL;
        return $content_without_comments;
      } else {
        return "";
        return '';
      }
    }
    
@@ -106,12 +106,12 @@
        $manual = "\n".trim($parts[1]);
        return $manual;
      } else {
        return "";
        return '';
      }
    }
    
    function remove_blank_lines($input, $file = 1){
      //Leerzeilen löschen
      //Leerzeilen löschen
      if($file){
        $content = $this->unix_nl($this->rf($input));
      } else {
@@ -120,13 +120,13 @@
      $lines = explode("\n", $content);
      if(!empty($lines)){
        foreach($lines as $line){
          if(trim($line) != "") $new_lines[] = $line;
          if(trim($line) != '') $new_lines[] = $line;
        }
      }
      if(is_array($new_lines)){
        $content = implode("\n", $new_lines);
      } else {
        $content = "";
        $content = '';
      }
      if($file){
        $this->wf($input, $content);
@@ -158,65 +158,19 @@
      return $ret_val;
    }
    
    function find_includes($file){
      ob_start();
      $httpd_root = system('httpd -V | awk -F"\"" \'$1==" -D HTTPD_ROOT="{print $2}\'');
      ob_end_clean();
      clearstatcache();
      if(is_file($file) && filesize($file) > 0){
        $includes[] = $file;
        $inhalt = $this->unix_nl($this->no_comments($file));
        $lines = explode("\n", $inhalt);
        if(!empty($lines)){
          foreach($lines as $line){
            if(stristr($line, "include ")){
              $include_file = str_replace("\n", "", trim(shell_exec("echo \"$line\" | awk '{print \$2}'")));
              if(substr($include_file,0,1) != "/"){
                $include_file = $httpd_root."/".$include_file;
              }
              if(is_file($include_file)){
                if($further_includes = $this->find_includes($include_file)){
                  $includes = array_merge($includes, $further_includes);
                }
              } else {
                if(strstr($include_file, "*")){
                  $more_files = explode("\n", shell_exec("ls -l $include_file | awk '{print \$9}'"));
                  if(!empty($more_files)){
                    foreach($more_files as $more_file){
                      if(is_file($more_file)){
                        if($further_includes = $this->find_includes($more_file)){
                          $includes = array_merge($includes, $further_includes);
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
      if(is_array($includes)){
        $includes = array_unique($includes);
        return $includes;
      } else {
        return false;
      }
    }
    function edit_dist($var, $val){
      global $$var;
      $files = array("/root/ispconfig/dist.inc.php");
      $files = array('/root/ispconfig/dist.inc.php');
      foreach($files as $file){
        if(is_file($file)){
          $file_content = $this->unix_nl($this->rf($file));
          $lines = explode("\n", $file_content);
          for($i=0;$i<sizeof($lines);$i++){
            $parts = explode("=", $lines[$i]);
            $parts = explode('=', $lines[$i]);
            if($parts[0] == $var || $parts[0] == '$'.$var.' '){
              $parts[1] = str_replace($$var, $val, $parts[1]);
            }
            $lines[$i] = implode("=", $parts);
            $lines[$i] = implode('=', $parts);
          }
          $file_content = implode("\n", $lines);
          $this->wf($file, $file_content);
@@ -224,7 +178,7 @@
      }
    }
    
    function getDirectoryListing($dirname, $sortorder = "a", $show_subdirs = 0, $show_subdirfiles = 0, $exts = "", $ext_save = 1){
    function getDirectoryListing($dirname, $sortorder = 'a', $show_subdirs = 0, $show_subdirfiles = 0, $exts = '', $ext_save = 1){
    // This function will return an array with filenames based on the criteria you can set in the variables
    // @sortorder : a for ascending (the standard) or d for descending (you can use the "r" for reverse as well, works the same)
    // @show_subdirs : 0 for NO, 1 for YES - meaning it will show the names of subdirectories if there are any
@@ -236,33 +190,33 @@
    // @ext_save : 1 for YES, 0 for NO - meaning it will filter out system files or not (such as .htaccess)
    
       $dirname = realpath($dirname);
       if (!$exts || empty($exts) || $exts == "") {
           $exts = array("jpg", "gif", "jpeg", "png");
       if (!$exts || empty($exts) || $exts == '') {
           $exts = array('jpg', 'gif', 'jpeg', 'png');
       }
       if ($handle = opendir($dirname)) {
           $filelist = array();
           while (false !== ($file = readdir($handle))) {
    
               // Filter out higher directory references
               if ($file != "." && $file != "..") {
               if ($file != '.' && $file != '..') {
                   // Only look at directories or files, filter out symbolic links
                   if ( filetype ($dirname."/".$file) != "link") {
                   if ( filetype ($dirname.'/'.$file) != 'link') {
                       // If it's a file, check against valid extentions and add to the list
                       if ( filetype ($dirname."/".$file) == "file" ) {
                       if ( filetype ($dirname.'/'.$file) == 'file' ) {
                           if ($this->checkFileExtension($file, $exts, $ext_save)) {
                                           $filelist[] = $file;
                           }
                       }
                       // If it's a directory and either subdirs should be listed or files from subdirs add relevant names to the list
                       else if ( filetype ($dirname."/".$file) == "dir" && ($show_subdirs == 1 || $show_subdirfiles == 1)) {
                       else if ( filetype ($dirname.'/'.$file) == 'dir' && ($show_subdirs == 1 || $show_subdirfiles == 1)) {
                           if ($show_subdirs == 1) {
                               $filelist[] = $file;
                           }
                           if ($show_subdirfiles == 1) {
                               $subdirname = $file;
                               $subdirfilelist = $this->getDirectoryListing($dirname."/".$subdirname."/", $sortorder, $show_subdirs, $show_subdirfiles, $exts, $ext_save);
                               $subdirfilelist = $this->getDirectoryListing($dirname.'/'.$subdirname.'/', $sortorder, $show_subdirs, $show_subdirfiles, $exts, $ext_save);
                               for ($i = 0 ; $i < count($subdirfilelist) ; $i++) {
                                   $subdirfilelist[$i] = $subdirname."/".$subdirfilelist[$i];
                                   $subdirfilelist[$i] = $subdirname.'/'.$subdirfilelist[$i];
                               }
                               $filelist = array_merge($filelist, $subdirfilelist);
                           }
@@ -277,7 +231,7 @@
           // Sort the results
           if (count($filelist) > 1) {
               natcasesort($filelist);
               if ($sortorder == "d" || $sortorder == "r" ) {
               if ($sortorder == 'd' || $sortorder == 'r' ) {
                   $filelist = array_reverse($filelist, TRUE);
               }
           }
@@ -295,7 +249,7 @@
               return $passed;
           }
       }
       if ($exts == "all") {
       if ($exts == 'all') {
                       $passed = TRUE;
           return $passed;
       }
@@ -314,6 +268,23 @@
       }
       return $passed;
    }
   function removeDirectory($dir){
      //TODO: implement something to delete files/directories recursively that are owned by a certain user or group
      if(is_dir($dir)){
         $files = array_diff(scandir($dir), array('.','..'));
         if(is_array($files) && !empty($files)){
            foreach($files as $file){
               if(is_dir($dir.'/'.$file)){
                  $this->removeDirectory($dir.'/'.$file);
               } else {
                  @unlink($dir.'/'.$file);
               }
            }
         }
         @rmdir($dir);
      }
   }
}
?>
?>