changed git call from https to git readonly
[atutor.git] / mods / phpdoc / PHPDoc / filehandler / PhpdocFileHandler.php
1 <?php
2 /**
3 * File handling functions in phpdoc. 
4 *
5 * @version      $Id $
6 * @author               Ulf Wendel <ulf@redsys.de>
7 */
8 class PhpdocFileHandler extends PhpdocObject {
9         
10         /**
11         * Filepath. The path is automatically added in front of all filenames
12         *
13         * @var          string  $path
14         * @see          setFilePath()
15         */
16         var $path = "";
17                 
18         /**
19         * Reads a list of files or one file.
20         *
21         * @param        mixed                           Filename or an array filenames, $k => $filename
22         * @throws PhpdocError
23         * @access       public
24         */              
25         function get($files) {
26                 if ("" == $files) {
27                         $this->err[] = new PhpdocError("No files specified.", __FILE__, __LINE__);
28                         return array("", "");
29                 }
30                 
31                 if (!is_array($files))
32                         $files = array($files);
33                 
34                 $contents = array();    
35                 $together = "";
36                 
37                 reset($files);
38                 while (list($k, $filename) = each($files)) 
39                         $contents[$filename] = $this->getFile($filename);
40                 
41                 return $contents;
42         } // end func get
43         
44         /**
45         * Sets the filepath. The path is automatically added in front of all filenames
46         *
47         * @param        string  $path
48         * @return       bool    $ok
49         * @access       public
50         */
51         function setFilePath($path) {
52                 $this->path = $path;
53         } // end func setFilePath
54         
55         /** 
56         * Reads a file. 
57         *
58         * @param        string  $filename
59         * @return       string  $content
60         * @throws       PhpdocError
61         */
62         function getFile($filename) {
63                 if ("" == $filename) {
64                         $this->err[] = new PhpdocError("getFile(), no filename specified.", __FILE__, __LINE__);                                
65                         return "";
66                 }
67                 if (!file_exists($filename)) {
68                         $this->err[] = new PhpdocError("getFile(), unknown file '$filename'.", __FILE__, __LINE__);
69                         return "";
70                 }
71                 if (!$fh = @fopen($filename, "r")) {
72                         $this->err[] = new PhpdocError("getFile(), can't open file '$filename' for reading.", __FILE__, __LINE__);
73                         return "";
74                 }
75
76                 $content = fread($fh, filesize($filename));
77                 fclose($fh);
78                 
79                 return $content;                        
80         } // end func getFile
81         
82         /**
83         * Appends a string to a file.
84         * 
85         * @param        string  Filename
86         * @param        string  Content to append
87         * @param        string  Directory prefix
88         * @throw        PHPDocError
89         * @return       boolean
90         * @todo         ... add a function boldy.
91         */
92         function appendToFile($filename, $content, $directory = "") {
93                 if ("" == $filename || "" == $content) {
94                         $this->err[] = new PhpdocError("No filename and/or no content given.", __FILE__, __LINE__);
95                         return false;
96                 }
97                 
98                 $fh = @fopen($filename, "a");
99                 if (!$fh) {
100                         print $filename;
101                         return false;
102                 }
103                 
104                 fwrite($fh, $content);
105                 fclose($fh);
106                 
107                 return true;
108         } // end func appendToFile
109         
110         /**
111         * Creates a new file.
112         * 
113         * Create or overrides a file in a specified directory. If the
114         * directory does not exists, it attempts to create it.
115         * 
116         * @param        string
117         * @param        string
118         * @param        string
119         * @throws       PHPDocError
120         * @return       boolean
121         */ 
122         function createFile($filename, $content, $directory = "") {
123                 if ("" == $filename || "" == $content) {
124                         $this->err[] = new PhpdocError("No filename or no content given.", __FILE__, __LINE__);
125                         return false;
126                 }
127                 
128                 $fh = @fopen($filename, "w");
129                 if (!$fh) {
130                         $this->err[] = new PhpdocError("Can't create file '$filename'.", __FILE__, __LINE__);
131                         return false;
132                 }
133                 
134                 fwrite($fh, $content);
135                 fclose($fh);
136                 
137                 return true;
138         } // end func createFile
139         
140         /**
141         * Returns a list of files in a specified directory
142         *
143         * @param        string  $directory
144         * @param        mixed           $suffix                         Suffix of the files returned 
145         * @param        boolean $flag_subdir    include subdirectories? 
146         * @param        array           $files                          New entries are added to this variable if provided.
147         *                                                                                                                               Used only for the subdir feature.
148         * @return       array           $files
149         * @throws       PhpdocError
150         */
151         function getFilesInDirectory($directory, $suffix = "", $flag_subdir = true, $files = "") {
152                 if ("" == $directory) {
153                         $this->err[] = new PhpdocError("No directory specified", __FILE__, __LINE__);
154                         return array();
155                 }
156                 
157                 if ("/" != substr($directory, -1))
158                         $directory .= "/";
159
160                 if ("" == $suffix) 
161                         $flag_all = true;
162                 else {
163                 
164                         $flag_all = false;
165                         $allowed        = array();
166                         
167                         if (!is_array($suffix))
168                                 $suffix = array($suffix);
169                         
170                         reset($suffix);
171                         while (list($k, $v) = each($suffix))
172                                 $allowed[".$v"] = true;
173                                 
174                 }
175
176                 if (!is_array($files)) 
177                         $files = array();
178                 
179                 $dh = @opendir($directory);
180                 if (!$dh) {
181                         $this->err[] = new PhpdocError("Can't open '$directory' for reading.", __FILE__, __LINE__);
182                         return array();
183                 }
184                 
185                 while ($file = readdir($dh)) {
186                         if ("." == $file || ".." == $file)
187                                 continue;
188                         
189                         if ($flag_subdir && is_dir($directory.$file))
190                                 $files = $this->getFilesInDirectory($directory.$file, $suffix, true, $files);
191                                 
192                         if (!is_file($directory.$file))
193                                 continue;
194                         
195                         if ($flag_all) {
196                                 $files[] = $file;
197                         } else {                        
198                                 if (isset($allowed[substr($file, strrpos($file, "."))]))
199                                         $files[] = $directory.$file;
200                         }
201                         
202                 }
203                 closedir($dh);
204                 
205                 return $files;
206         } // end fun getFilesInDirectory
207                 
208 } // end class PhpdocFileHandler
209 ?>