changed git call from https to git readonly
[atutor.git] / mods / phpdoc2 / PhpDocumentor / phpDocumentor / Io.inc
1 <?php\r
2 /**\r
3  * File and input handling routines\r
4  * \r
5  * This class parses command-line options, and works with files to\r
6  * generate lists of files to parse based on the ignore/include options\r
7  * \r
8  * phpDocumentor :: automatic documentation generator\r
9  * \r
10  * PHP versions 4 and 5\r
11  *\r
12  * Copyright (c) 2000-2006 Joshua Eichorn, Gregory Beaver\r
13  * \r
14  * LICENSE:\r
15  * \r
16  * This library is free software; you can redistribute it\r
17  * and/or modify it under the terms of the GNU Lesser General\r
18  * Public License as published by the Free Software Foundation;\r
19  * either version 2.1 of the License, or (at your option) any\r
20  * later version.\r
21  * \r
22  * This library is distributed in the hope that it will be useful,\r
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
25  * Lesser General Public License for more details.\r
26  * \r
27  * You should have received a copy of the GNU Lesser General Public\r
28  * License along with this library; if not, write to the Free Software\r
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
30  *\r
31  * @package    phpDocumentor\r
32  * @author     Joshua Eichorn <jeichorn@phpdoc.org>\r
33  * @author     Gregory Beaver <cellog@php.net>\r
34  * @copyright  2000-2006 Joshua Eichorn, Gregory Beaver\r
35  * @license    http://www.opensource.org/licenses/lgpl-license.php LGPL\r
36  * @version    CVS: $Id: Io.inc,v 1.18 2007/12/08 01:31:50 ashnazg Exp $\r
37  * @filesource\r
38  * @link       http://www.phpdoc.org\r
39  * @link       http://pear.php.net/PhpDocumentor\r
40  * @since      0.1\r
41  */\r
42 /**\r
43  * Class to handle file and user io opperations\r
44  *\r
45  * @author    Joshua Eichorn <jeichorn@phpdoc.org>\r
46  * @author     Gregory Beaver <cellog@php.net>\r
47  * @version    $Id: Io.inc,v 1.18 2007/12/08 01:31:50 ashnazg Exp $\r
48  * @package     phpDocumentor\r
49  */\r
50 class Io \r
51 {\r
52     \r
53     /**\r
54      * Holds all the options that are avaible to the cmd line interface\r
55      * and to the different web interfaces\r
56      */\r
57     var $phpDocOptions;\r
58     /**\r
59      * Format: array(array(regexp-ready string to search for whole path,\r
60      * regexp-ready string to search for basename of ignore strings),...)\r
61      * @var false|array\r
62      */\r
63     var $ignore;\r
64     /**\r
65      * A specific array of values that boolean-based arguments can understand,\r
66      * aided by the {@link decideOnOrOff()} helper method.\r
67      * \r
68      * Use lowercase letters always, to simplify string comparisons\r
69      * @var array \r
70      */\r
71     var $valid_booleans = array\r
72     (\r
73         '', ' ', 'on', 'y', 'yes', 'true', '1', \r
74         'off', 'n', 'no', 'false', '0'\r
75 \r
76     );\r
77     \r
78     /**\r
79      * creates an array $this->phpDocOptions and sets program options in it.\r
80      * Array is in the format of:\r
81      * <pre>\r
82      * [filename][tag][] = "f";\r
83      * [filename][tag][] = "-file";\r
84      * [filename][desc] "name of file to parse"\r
85      * </pre>\r
86      */\r
87     function Io()\r
88     {\r
89         $this->phpDocOptions['filename']['tag'] = array( "-f", "--filename");\r
90         $this->phpDocOptions['filename']['desc'] = "name of file(s) to parse ',' file1,file2.  Can contain complete path and * ? wildcards";\r
91         $this->phpDocOptions['filename']['type'] = "path";\r
92 \r
93         $this->phpDocOptions['directory']['tag'] = array( "-d", "--directory");\r
94         $this->phpDocOptions['directory']['desc'] = "name of a directory(s) to parse directory1,directory2";\r
95         $this->phpDocOptions['directory']['type'] = "path";\r
96 \r
97         $this->phpDocOptions['examplesdir']['tag'] = array( "-ed", "--examplesdir");\r
98         $this->phpDocOptions['examplesdir']['desc'] = "full path of the directory to look for example files from @example tags";\r
99         $this->phpDocOptions['examplesdir']['type'] = "path";\r
100 \r
101         $this->phpDocOptions['templatebase']['tag'] = array( "-tb", "--templatebase");\r
102         $this->phpDocOptions['templatebase']['desc'] = "base location of all templates for this parse.";\r
103         $this->phpDocOptions['templatebase']['type'] = "path";\r
104 \r
105         $this->phpDocOptions['target']['tag'] = array("-t", "--target");\r
106         $this->phpDocOptions['target']['desc'] = "path where to save the generated files";\r
107         $this->phpDocOptions['target']['type'] = "path";\r
108         \r
109         $this->phpDocOptions['ignore']['tag'] = array("-i", "--ignore");\r
110         $this->phpDocOptions['ignore']['desc'] = "file(s) that will be ignored, multiple separated by ','.  Wildcards * and ? are ok";\r
111         $this->phpDocOptions['ignore']['type'] = "path";\r
112 \r
113         $this->phpDocOptions['ignoresymlinks']['tag'] = array("-is", "--ignoresymlinks");\r
114         $this->phpDocOptions['ignoresymlinks']['desc'] = "ignore symlinks to other files or directories, default is off";\r
115         $this->phpDocOptions['ignoresymlinks']['type'] = "set";\r
116         $this->phpDocOptions['ignoresymlinks']['validvalues'] = $this->valid_booleans;\r
117 \r
118         $this->phpDocOptions['ignoretags']['tag'] = array("-it", "--ignore-tags");\r
119         $this->phpDocOptions['ignoretags']['desc'] = "tags to ignore for this parse.  @package, @subpackage, @access and @ignore may not be ignored.";\r
120         $this->phpDocOptions['ignoretags']['type'] = "value";\r
121 \r
122         $this->phpDocOptions['hidden']['tag'] = array("-dh", "--hidden");\r
123         $this->phpDocOptions['hidden']['desc'] = "set equal to on (-dh on) to descend into hidden directories (directories starting with '.'), default is off";\r
124         $this->phpDocOptions['hidden']['type'] = "set";\r
125         $this->phpDocOptions['hidden']['validvalues'] = $this->valid_booleans;\r
126 \r
127         $this->phpDocOptions['quiet']['tag'] = array("-q", "--quiet");\r
128         $this->phpDocOptions['quiet']['desc'] = "do not display parsing/conversion messages.  Useful for cron jobs on/off default off";\r
129         $this->phpDocOptions['quiet']['type'] = "set";\r
130         $this->phpDocOptions['quiet']['validvalues'] = $this->valid_booleans;\r
131 \r
132         $this->phpDocOptions['undocumentedelements']['tag'] = array("-ue", "--undocumentedelements");\r
133         $this->phpDocOptions['undocumentedelements']['desc'] = "Control whether or not warnings will be shown for undocumented elements. Useful for identifying classes and methods that haven't yet been documented on/off default off";\r
134         $this->phpDocOptions['undocumentedelements']['type'] = "set";\r
135         $this->phpDocOptions['undocumentedelements']['validvalues'] = $this->valid_booleans;\r
136 \r
137         $this->phpDocOptions['title']['tag'] = array("-ti","--title");\r
138         $this->phpDocOptions['title']['desc'] = "title of generated documentation, default is 'Generated Documentation'";\r
139         $this->phpDocOptions['title']['type'] = "value";\r
140 \r
141         $this->phpDocOptions['help']['tag'] = array("-h", "--help");\r
142         $this->phpDocOptions['help']['desc'] = "    show this help message";\r
143 \r
144         $this->phpDocOptions['useconfig']['tag'] = array("-c","--useconfig");\r
145         $this->phpDocOptions['useconfig']['desc'] = "Use a Config file in the users/ subdirectory for all command-line options";\r
146         $this->phpDocOptions['useconfig']['type'] = "value";\r
147 \r
148         $this->phpDocOptions['parseprivate']['tag'] = array("-pp","--parseprivate");\r
149         $this->phpDocOptions['parseprivate']['desc'] = "parse @internal and elements marked private with @access.  Use on/off, default off";\r
150         $this->phpDocOptions['parseprivate']['type'] = "set";\r
151         $this->phpDocOptions['parseprivate']['validvalues'] = array('on', 'off');\r
152 \r
153         $this->phpDocOptions['packageoutput']['tag'] = array("-po","--packageoutput");\r
154         $this->phpDocOptions['packageoutput']['desc'] = "output documentation only for selected packages.  Use a comma-delimited list";\r
155         $this->phpDocOptions['packageoutput']['type'] = "value";\r
156 \r
157         $this->phpDocOptions['defaultpackagename']['tag'] = array("-dn","--defaultpackagename");\r
158         $this->phpDocOptions['defaultpackagename']['desc'] = "name to use for the default package.  If not specified, uses 'default'";\r
159         $this->phpDocOptions['defaultpackagename']['type'] = "value";\r
160 \r
161         $this->phpDocOptions['defaultcategoryname']['tag'] = array("-dc","--defaultcategoryname");\r
162         $this->phpDocOptions['defaultcategoryname']['desc'] = "name to use for the default category.  If not specified, uses 'default'";\r
163         $this->phpDocOptions['defaultcategoryname']['type'] = "value";\r
164 \r
165         $this->phpDocOptions['output']['tag'] = array("-o","--output");\r
166         $this->phpDocOptions['output']['desc'] = "output information to use separated by ','.  Format: output:converter:templatedir like \"HTML:frames:phpedit\"";\r
167         $this->phpDocOptions['output']['type'] = "value";\r
168 \r
169         $this->phpDocOptions['converterparams']['tag'] = array("-cp","--converterparams");\r
170         $this->phpDocOptions['converterparams']['desc'] = "dynamic parameters for a converter, separate values with commas";\r
171         $this->phpDocOptions['converterparams']['type'] = "value";\r
172 \r
173         $this->phpDocOptions['customtags']['tag'] = array("-ct","--customtags");\r
174         $this->phpDocOptions['customtags']['desc'] = "custom tags, will be recognized and put in tags[] instead of unknowntags[]";\r
175         $this->phpDocOptions['customtags']['type'] = "value";\r
176 \r
177         $this->phpDocOptions['sourcecode']['tag'] = array("-s","--sourcecode");\r
178         $this->phpDocOptions['sourcecode']['desc'] = "generate highlighted sourcecode for every parsed file (PHP 4.3.0+ only) on/off default off";\r
179         $this->phpDocOptions['sourcecode']['type'] = "set";\r
180         $this->phpDocOptions['sourcecode']['validvalues'] = array('on', 'off');\r
181 \r
182         $this->phpDocOptions['javadocdesc']['tag'] = array("-j","--javadocdesc");\r
183         $this->phpDocOptions['javadocdesc']['desc'] = "JavaDoc-compliant description parsing.  Use on/off, default off (more flexibility)";\r
184         $this->phpDocOptions['javadocdesc']['type'] = "set";\r
185         $this->phpDocOptions['javadocdesc']['validvalues'] = array('on', 'off');\r
186 \r
187         $this->phpDocOptions['pear']['tag'] = array("-p","--pear");\r
188         $this->phpDocOptions['pear']['desc'] = "Parse a PEAR-style repository (package is directory, _members are @access private) on/off default off";\r
189         $this->phpDocOptions['pear']['type'] = "set";\r
190         $this->phpDocOptions['pear']['validvalues'] = array('on', 'off');\r
191 \r
192         $this->phpDocOptions['readmeinstallchangelog']['tag'] = array("-ric","--readmeinstallchangelog");\r
193         $this->phpDocOptions['readmeinstallchangelog']['desc'] = "Specify custom filenames to parse like README, INSTALL or CHANGELOG files";\r
194         $this->phpDocOptions['readmeinstallchangelog']['type'] = "value";\r
195 \r
196         $this->phpDocOptions['general']['message'] ="You can have multiple directories and multiple files, as well as a combination of both options";\r
197     }\r
198 \r
199     \r
200     /**\r
201      * create the help message for display on the command-line\r
202      * @return string a string containing a help message\r
203      */\r
204     function displayHelpMsg()\r
205     {\r
206         unset($ret);\r
207         $ret = "\n";\r
208         foreach($this->phpDocOptions as $data)\r
209         {\r
210             unset($tag);\r
211             $tag = "";\r
212             if (isset($data['tag']))\r
213             {\r
214                 if (is_array($data['tag'])) {\r
215                     foreach($data['tag'] as $param) {\r
216                         $tag .= "$param    ";\r
217                     }\r
218                 }\r
219                 $taglen = 34;\r
220                 $outputwidth = 79;\r
221                 $tagspace = str_repeat(" ",$taglen);\r
222                 $tmp = "  ".trim($tag).$tagspace;\r
223                 $tmp = substr($tmp,0,$taglen);\r
224                 $d = wordwrap(ltrim($data['desc']),($outputwidth-$taglen));\r
225                 $dt = explode("\n",$d);\r
226                 $dt[0] = $tmp .$dt[0];\r
227                 for($i=1;$i<count($dt);$i++)\r
228                 {\r
229                         $dt[$i] = $tagspace.$dt[$i];\r
230                 }\r
231                 $ret .= implode("\n",$dt)."\n\n";\r
232                 \r
233             }\r
234         }\r
235         $ret .= "\n".wordwrap($data['message'],$outputwidth)."\n";\r
236         return $ret; \r
237     }\r
238     \r
239     /**\r
240      * calls {@link file_exists()} for each value in include_path,\r
241      * then calls {@link is_readable()} when it finds the file\r
242      * @param string\r
243      * @return boolean\r
244      */\r
245     function isIncludeable($filename)\r
246     {\r
247         $test = realpath($filename);\r
248         if ($test && is_readable($test)) {\r
249             return true; // for absolute paths\r
250         }\r
251         $ip = get_include_path();\r
252         if (PHPDOCUMENTOR_WINDOWS)\r
253         {\r
254             $ip = explode(';', $ip);\r
255         } else {\r
256             $ip = explode(':', $ip);\r
257         }\r
258         foreach($ip as $path)\r
259         {\r
260             if ($a = realpath($path . DIRECTORY_SEPARATOR . $filename))\r
261             {\r
262                 if (is_readable($a))\r
263                 {\r
264                     return true;\r
265                 }\r
266             }\r
267         }\r
268         return false;\r
269     }\r
270 \r
271     /**\r
272      * Parses $_SERVER['argv'] and creates a setup array\r
273      * @return array a setup array\r
274      * @global array command-line arguments\r
275      * @todo replace with Console_* ?\r
276      */\r
277     function parseArgv()\r
278     {\r
279         global $argv;\r
280 \r
281         // defaults for setting\r
282         $setting['hidden'] = "off";\r
283         $setting['ignoresymlinks'] = 'off';\r
284         $setting['template'] = 'templates' . PATH_DELIMITER .'default' . PATH_DELIMITER;\r
285 \r
286         $valnext = "junk";\r
287         $data = array();\r
288         if(isset($argv) && is_array($argv))\r
289         {\r
290             foreach ($argv as $cmd)\r
291             {\r
292                 if ($cmd == '--') {\r
293                     continue;\r
294                 }\r
295                 if ($cmd == '-h' || $cmd == '--help')\r
296                 {\r
297                     echo $this->displayHelpMsg();\r
298                     die();\r
299                 }\r
300 \r
301                 // at first, set the arg value as if we\r
302                 // already know it's formatted normally, e.g.\r
303                 //    -q on\r
304                 $setting[$valnext] = $cmd;\r
305 \r
306                 if (isset($data['type']) && $data['type'] == 'set') {\r
307 \r
308                     if ($valnext !== 'junk' && strpos(trim($cmd),'-') === 0) {\r
309                         // if valnext isn't 'junk' (i.e it was an arg option) \r
310                         // then the first arg needs an implicit "" as its value, e.g.\r
311                         //     ... -q -pp ...  ===>  ... -q '' -pp ... \r
312                         $setting[$valnext] = '';\r
313 \r
314                     } else if (!in_array(strtolower($cmd), $data['validvalues'], true)) {\r
315                         // the arg value is not a valid value\r
316                         addErrorDie(PDERROR_INVALID_VALUES, $valnext, $cmd,\r
317                             '(' . implode(', ', $data['validvalues']) . ')');\r
318                     }\r
319                 }\r
320 \r
321                 foreach( $this->phpDocOptions as $name => $data )\r
322                 {\r
323                     if (!empty($data['tag']))\r
324                     {\r
325                         if (in_array($cmd,$data['tag']))\r
326                         {\r
327                             $valnext = $name;\r
328                             break;\r
329                         } \r
330                         else\r
331                         {\r
332                             $valnext = "junk";\r
333                         }\r
334                     }\r
335                 }\r
336 \r
337                 if ($valnext == 'junk' && (strpos(trim($cmd),'-') === 0)) {\r
338                     // this indicates the last arg of the command \r
339                     // is an arg option (-) that was preceded by unrecognized "junk"\r
340                     addErrorDie(PDERROR_UNKNOWN_COMMANDLINE,$cmd);\r
341 \r
342                 } else if ($valnext != 'junk' && (strpos(trim($cmd),'-') === 0)) {\r
343                     // this indicates the last arg of the command \r
344                     // is an arg option (-) without an arg value\r
345                     \r
346                     // add an empty arg "value" for this arg "option"\r
347                     $setting[$valnext] = '';\r
348                 }\r
349 \r
350 \r
351             }\r
352         } else\r
353         {\r
354             echo "Please use php-cli.exe in windows, or set register_argc_argv On";\r
355             die;\r
356         }\r
357         /* $setting will always have at least 3 elements\r
358         [hidden] => off\r
359         [ignoresymlinks] => 'off'\r
360         [template] => templates/default\r
361          */\r
362         if (count($setting) < 4) {\r
363             echo $this->displayhelpMsg();\r
364             die();\r
365         }\r
366 \r
367         return $setting;\r
368     }\r
369 \r
370 \r
371     /**\r
372      * @return array list of files in a directory\r
373      * @param string $directory full path to the directory you want the list of\r
374      * @param bool whether to list files that begin with . like .bash_history\r
375      * @param bool whether to ignore symlinks\r
376      */\r
377     function dirList($orig_directory, $hidden = false, $ignore_symlinks = false)\r
378     {\r
379         $directory = realpath($orig_directory);\r
380         $ret = false;\r
381         if (! @is_dir($directory))\r
382         {\r
383             die("directory: '$directory'  not found\n");\r
384         }\r
385         $ret = array();\r
386         $d = @dir($directory); // thanks to Jason E Sweat (jsweat@users.sourceforge.net) for fix\r
387         while($d && ($entry=$d->read()) !== false) {\r
388             if (strcmp($entry,".") == 0 || strcmp($entry,"..") == 0) {\r
389                 continue;\r
390             }\r
391 \r
392             // skip hidden files, if we're supposed to\r
393             if (!$hidden)\r
394             {\r
395                 if (substr($entry,0,1) == ".")\r
396                 {\r
397                     phpDocumentor_out("Hidden " . $directory . PATH_DELIMITER . $entry . " Ignored\n");\r
398                     continue;\r
399                 }\r
400             }\r
401 \r
402             // skip symlink files, if we're supposed to\r
403             if ($ignore_symlinks)\r
404             {\r
405                 if (is_link($directory . PATH_DELIMITER . $entry))\r
406                 {\r
407                     phpDocumentor_out("Symlink " . $directory . PATH_DELIMITER . $entry . " Ignored\n");\r
408                     continue;\r
409                 }\r
410             }\r
411             \r
412             if (is_file($directory . PATH_DELIMITER . $entry)) {\r
413                 $ret[] = $directory . PATH_DELIMITER . $entry;\r
414             }\r
415             if (is_dir($directory . PATH_DELIMITER . $entry)) {\r
416                 $tmp = $this->dirList($directory . PATH_DELIMITER . $entry, $hidden, $ignore_symlinks);\r
417                 if (is_array($tmp)) {\r
418                     foreach($tmp as $ent) {\r
419                         $ret[] = $ent;\r
420                     }\r
421                 }\r
422             }\r
423         }\r
424         if ($d) $d->close();\r
425         return $ret;\r
426     }\r
427 \r
428     /**\r
429      * Retrieve common directory (case-insensitive in windows)\r
430      *\r
431      * takes the list of files, and returns the subdirectory they share in common,\r
432      * so in this list:\r
433      *\r
434      * <code>\r
435      * array(\r
436      * "/dir1/dir2/subdir/dir3/filename.ext",\r
437      * "/dir1/dir2/subdir/dir4/filename.ext",\r
438      * "/dir1/dir2/mydir/dir5/filename.ext");\r
439      * </code>\r
440      *\r
441      * getBase will return "/dir1/dir2"\r
442      * @param array array of strings\r
443      */\r
444     function getBase($filelist)\r
445     {\r
446         $masterPath = false;\r
447         foreach($filelist as $path)\r
448         {\r
449             if (!$masterPath)\r
450             {\r
451                 $masterPath = str_replace('\\','/',dirname($path));\r
452             } else\r
453             {\r
454                 if (dirname($path) != $masterPath)\r
455                 {\r
456                     $mp = split(PATH_DELIMITER,$masterPath);\r
457                     $np = split(PATH_DELIMITER,str_replace('\\','/',dirname($path)));\r
458                     if (count($np) < count($mp))\r
459                     {\r
460                         $masterPath = join($np, PATH_DELIMITER);\r
461                     } else\r
462                     {\r
463                         $test = false;\r
464                         $found = false;\r
465                         for($i=0;$i < count($mp) && $i < count($np);$i++)\r
466                         {\r
467                             if (PHPDOCUMENTOR_WINDOWS)\r
468                             {\r
469                                 if (strtolower($mp[$i]) != strtolower($np[$i])) $found = $i;\r
470                             } else\r
471                             {\r
472                                 if ($mp[$i] != $np[$i]) $found = $i;\r
473                             }\r
474                         }\r
475                         if ($found !== false)\r
476                         {\r
477                             $mp = array_slice($mp,0,$found);\r
478                             $masterPath = join($mp,PATH_DELIMITER);\r
479                         }\r
480                     }\r
481                 }\r
482             }\r
483         }\r
484         return $masterPath;\r
485     }\r
486     \r
487     /**\r
488      * Retrieve tutorial subdirectories and their contents from the list of\r
489      * files to parse\r
490      * @param array array of paths (strings)\r
491      * @return array array(filelist - tutorials, tutorials)\r
492      */\r
493     function getTutorials($filelist)\r
494     {\r
495         $list = $tutorials = array();\r
496         foreach($filelist as $file)\r
497         {\r
498             if (strpos($file,'tutorials/') !== false)\r
499             {\r
500                 $tutedir = explode('/',substr($file,strpos($file,'tutorials/')));\r
501                 array_shift($tutedir);\r
502                 if (count($tutedir) <= 3)\r
503                 {\r
504                     $res = array();\r
505                     // kludge - will need to fix for 2.0\r
506                     $res['category'] = $GLOBALS['phpDocumentor_DefaultCategoryName'];\r
507                     $res['package'] = array_shift($tutedir);\r
508                     $res['subpackage'] = '';\r
509                     if (count($tutedir) > 1)\r
510                     $res['subpackage'] = array_shift($tutedir);\r
511                     $f = array_shift($tutedir);\r
512                     $res['tutename'] = $f;\r
513                     $f = explode('.',$f);\r
514                     $res['tutetype'] = array_pop($f);\r
515                     if ($res['tutetype'] == 'ini') continue;\r
516                     $res['path'] = $file;\r
517                     if (@file_exists($file . '.ini'))\r
518                     {\r
519                         $res['ini'] = phpDocumentor_parse_ini_file($file . '.ini', true);\r
520                     } else\r
521                     {\r
522                         $res['ini'] = false;\r
523                     }\r
524                     $tutorials[] = $res;\r
525                 }\r
526             } else $list[] = $file;\r
527         }\r
528         return array($list,$tutorials);\r
529     }\r
530     \r
531     /**\r
532      * @param string base directory from {@link getBase()}\r
533      * @param array file list from {@link dirList()}\r
534      * @return array array(filelist - README/INSTALL/CHANGELOG,\r
535      *                     README/INSTALL/CHANGELOG)\r
536      */\r
537     function getReadmeInstallChangelog($base,$filelist)\r
538     {\r
539         $list = $ric = array();\r
540         $names = $GLOBALS['_phpDocumentor_RIC_files'];\r
541         foreach($filelist as $file)\r
542         {\r
543             if ((dirname($file) == $base) && in_array(strtoupper(basename($file)), $names))\r
544             { // be sure to change $this->checkIgnore() if any other files are added here!!\r
545                 $ric[] = $file;\r
546             } else\r
547             {\r
548                 $list[] = $file;\r
549             }\r
550         }\r
551         return array($list,$ric);\r
552     }\r
553 \r
554     /**\r
555      * @param string directory\r
556      * @param string base directory\r
557      * @param array array of ignored items\r
558      * @param boolean the "hidden" flag\r
559      * @param boolean the "ignoresymlinks" flag\r
560      * @uses dirList\r
561      * @uses checkIgnore\r
562      * @uses setup_dirs\r
563      */    \r
564     function getDirTree($dir, $base_dir, $ignore = array(), $hidden = false, $ignoresymlinks = false)\r
565     {\r
566         $allfiles = $this->dirList($dir,$hidden,$ignoresymlinks);\r
567         $struc = array();\r
568         foreach($allfiles as $file)\r
569         {\r
570             if ($this->checkIgnore(basename($file),dirname(realpath($file)),$ignore,$ignoresymlinks)) continue;\r
571             if (PHPDOCUMENTOR_WINDOWS) {\r
572                 $path = substr(dirname($file),strlen(str_replace('\\','/',realpath($base_dir))));\r
573             } else {\r
574                 $path = substr(dirname($file),strlen(str_replace('\\','/',realpath($base_dir)))+1);\r
575             }\r
576             if (!$path) $path = '/';\r
577             $parts = pathinfo($file);\r
578             if (!isset($parts['extension']))\r
579             {\r
580                 $parts['extension'] = '';\r
581             }\r
582             $struc[$path][] = array(\r
583                 'file' => $parts['basename'],\r
584                 'ext' => $parts['extension'],\r
585                 'path' => $file);\r
586         }\r
587         uksort($struc,'strnatcasecmp');\r
588         foreach($struc as $key => $ind)\r
589         {\r
590             usort($ind,'Ioinc_sortfiles');\r
591             $struc[$key] = $ind;\r
592             $save = $key;\r
593             if ($key != '/')\r
594             {\r
595                 $key = explode('/',$key);\r
596                 while (count($key))\r
597                 {\r
598                     array_pop($key);\r
599                     if (isset($struc[join('/',$key)]))\r
600                     {\r
601                         $struc[join('/',$key)][substr($save,strlen(join('/',$key)) + 1)] = $ind;\r
602                         unset($struc[$save]);\r
603                     }\r
604                 }\r
605             }\r
606         }\r
607         foreach($struc as $key => $ind)\r
608         {\r
609             if ($key != '/')\r
610             {\r
611                 if (count(explode('/',$key)) == 1)\r
612                 {\r
613                     $struc['/'][$key] = $struc[$key];\r
614                     unset($struc[$key]);\r
615                 }\r
616             }\r
617         }\r
618         $tempstruc = $struc;\r
619         unset($tempstruc['/']);\r
620         $leftover_dirs = array_keys($tempstruc);\r
621         $splitdirs = array();\r
622         foreach($leftover_dirs as $dir)\r
623         {\r
624             $splitdirs[] = explode('/',$dir);\r
625         }\r
626         $leftover_dirs = array();\r
627 \r
628         foreach($splitdirs as $dir)\r
629         {\r
630             $save = join($dir,'/');\r
631             $struc['/'] = setup_dirs($struc['/'], $dir, $tempstruc[$save]);\r
632             unset($struc[$save]);\r
633         }\r
634         @uksort($struc['/'],'Ioinc_mystrucsort');\r
635         return $struc;\r
636     }\r
637     \r
638     /**\r
639      * Reads a file and returns it as a string\r
640      * Does basic error checking\r
641      *\r
642      * file extensions are set in {@link phpdoc.inc}\r
643      *\r
644      * @global array PHP File extensions, used to validate that $path is a PHP File\r
645      * @global array PHP File extensions in a CVS repository, used to validate that $path is a PHP File\r
646      * @param    string    $path\r
647      */\r
648     function readPhpFile($path, $quietMode = false)\r
649     {\r
650         global $_phpDocumentor_cvsphpfile_exts, $_phpDocumentor_phpfile_exts;\r
651         // tiberiusblue addition\r
652         $cvsExt = $_phpDocumentor_cvsphpfile_exts; \r
653         $ext = $_phpDocumentor_phpfile_exts;\r
654         if (file_exists($path))\r
655         {\r
656             if (is_file($path))\r
657             {\r
658                 // check extension\r
659                 $tmp = explode(".",$path);\r
660                 // tiberiusblue addition\r
661                 $tmp2 = $tmp;\r
662                 if (in_array(array_pop($tmp),$ext))\r
663                 {\r
664                     phpDocumentor_out(" -- Parsing file\n");\r
665                     flush();\r
666                     if (function_exists('file_get_contents')) {\r
667                         return file_get_contents($path);\r
668                     }\r
669                     $fp = fopen($path,"r");\r
670                     $ret = fread($fp,filesize($path));\r
671                     fclose($fp);\r
672                     return $ret;\r
673                 } elseif (in_array(array_pop($tmp2),$cvsExt)) \r
674                 { \r
675                     phpDocumentor_out(" CVS file [EXPERIMENTAL]\n"); \r
676                     flush();\r
677                     if (function_exists('file_get_contents')) {\r
678                         $ret = file_get_contents($path);\r
679                     } else {\r
680                         $fp = fopen($path,"r"); \r
681                         $ret = fread($fp,filesize($path)); \r
682                         fclose($fp); \r
683                     }\r
684                     $ret = strstr($ret,"<?");\r
685                     $ret = substr($ret,0,strpos($ret,"@\n"));\r
686                     $ret = str_replace("@@","@",$ret); \r
687                     return $ret; \r
688                 } else\r
689                 {\r
690                     phpDocumentor_out(" -- File not parsed, not a php file\n");\r
691                     flush();\r
692                 }\r
693             } else {\r
694                 phpDocumentor_out(" -- Unable to read file, not a file\n");\r
695                 flush();\r
696             }\r
697         } else {\r
698             phpDocumentor_out(" -- Unable to read file, file does not exist\n");\r
699             flush();\r
700            }\r
701     }\r
702 \r
703     /**\r
704      * Tell whether to ignore a file or a directory\r
705      * allows * and ? wildcards\r
706      *\r
707      * @author Greg Beaver <cellog@php.net>\r
708      * @param    string  $file    just the file name of the file or directory,\r
709      *                          in the case of directories this is the last dir\r
710      * @param    string  $path    the path to consider (should be checked by\r
711      *                            realpath() before, and may be relative)\r
712      * @param    array   $ignore\r
713      * @param    bool\r
714      * @param    bool    Ignore symlinks?\r
715      * @return   bool    true if $path should be ignored, false if it should not\r
716      */\r
717     function checkIgnore($file,$path,$ignore,$ignore_no_ext = true,$ignoresymlinks = false)\r
718     {\r
719         global $_phpDocumentor_RIC_files;\r
720 \r
721         if ($ignoresymlinks && is_link($path . PATH_DELIMITER . $file)) return false;\r
722         \r
723         if (!count($ignore)) return false;\r
724 \r
725         if (!isset($this->ignore))\r
726         {\r
727             $this->_setupIgnore($ignore);\r
728         }\r
729         if (!$this->ignore)\r
730         {\r
731             return false;\r
732         }\r
733 \r
734         if ($ignore_no_ext && \r
735             !in_array(strtoupper($file), $_phpDocumentor_RIC_files))\r
736         {\r
737             if (!is_numeric(strpos($file,'.'))) return true;\r
738         }\r
739         if (is_array($this->ignore))\r
740         {\r
741             foreach($this->ignore as $match)\r
742             {\r
743                 // match is an array if the ignore parameter was a /path/to/pattern\r
744                 if (is_array($match))\r
745                 {\r
746                     // check to see if the path matches with a path delimiter appended\r
747                     preg_match('/^' . strtoupper($match[0]).'$/', strtoupper($path) . PATH_DELIMITER,$find);\r
748                     if (!count($find))\r
749                     {\r
750                         // check to see if it matches without an appended path delimiter\r
751                         preg_match('/^' . strtoupper($match[0]).'$/', strtoupper($path), $find);\r
752                     }\r
753                     if (count($find))\r
754                     {\r
755                         // check to see if the file matches the file portion of the regex string\r
756                         preg_match('/^' . strtoupper($match[1]).'$/', strtoupper($file), $find);\r
757                         if (count($find))\r
758                         {\r
759                             return true;\r
760                         }\r
761                     }\r
762                     // check to see if the full path matches the regex\r
763                     preg_match('/^' . strtoupper($match[0]).'$/',\r
764                                strtoupper($path . DIRECTORY_SEPARATOR . $file), $find);\r
765                     if (count($find))\r
766                     {\r
767                         return true;\r
768                     }\r
769                 } else\r
770                 {\r
771                     // ignore parameter was just a pattern with no path delimiters\r
772                     // check it against the path\r
773                     preg_match('/^' . strtoupper($match).'$/', strtoupper($path), $find);\r
774                     if (count($find))\r
775                     {\r
776                         return true;\r
777                     }\r
778                     // check it against the file only\r
779                     preg_match('/^' . strtoupper($match).'$/', strtoupper($file), $find);\r
780                     if (count($find))\r
781                     {\r
782                         return true;\r
783                     }\r
784                 }\r
785             }\r
786         }\r
787         return false;\r
788     }\r
789     \r
790     /**\r
791      * Construct the {@link $ignore} array\r
792      * @author Greg Beaver <cellog@php.net>\r
793      * @param array strings of files/paths/wildcards to ignore\r
794      * @access protected\r
795      */\r
796     function _setupIgnore($ignore)\r
797     {\r
798         $ig = array();\r
799         if ( ! is_array($ignore))\r
800         {\r
801             $this->ignore = false;\r
802             return;\r
803         }\r
804         for($i=0; $i<count($ignore);$i++)\r
805         {\r
806             if (empty($ignore[$i]))\r
807                 continue;\r
808 \r
809             $ignore[$i] = strtr($ignore[$i], '\\', '/');\r
810             $ignore[$i] = str_replace('//','/',$ignore[$i]);\r
811 \r
812             if (!is_numeric(strpos($ignore[$i],PATH_DELIMITER)))\r
813             {\r
814                 $ig[] = $this->getRegExpableSearchString($ignore[$i]);\r
815             } else\r
816             {\r
817                 if (basename($ignore[$i]) . PATH_DELIMITER == $ignore[$i])\r
818                 $ig[] = $this->getRegExpableSearchString($ignore[$i]);\r
819                 else\r
820                 $ig[] = array($this->getRegExpableSearchString($ignore[$i]),$this->getRegExpableSearchString(basename($ignore[$i])));\r
821             }\r
822         }\r
823         if (count($ig)) $this->ignore = $ig;\r
824     }\r
825     \r
826     /**\r
827      * Converts $s into a string that can be used with preg_match\r
828      * @param string $s string with wildcards ? and *\r
829      * @author Greg Beaver <cellog@php.net>\r
830      * @return string converts * to .*, ? to ., etc.\r
831      */\r
832     function getRegExpableSearchString($s)\r
833     {\r
834         $y = '\/';\r
835         if (DIRECTORY_SEPARATOR == '\\')\r
836         {\r
837             $y = '\\\\';\r
838         }\r
839         $s = str_replace('/', DIRECTORY_SEPARATOR, $s);\r
840         $x = strtr($s, array('?' => '.','*' => '.*','.' => '\\.','\\' => '\\\\','/' => '\\/',\r
841                                 '[' => '\\[',']' => '\\]','-' => '\\-'));\r
842         if (strpos($s, DIRECTORY_SEPARATOR) !== false &&\r
843             strrpos($s, DIRECTORY_SEPARATOR) === strlen($s) - 1)\r
844         {\r
845             $x = "(?:.*$y$x?.*|$x.*)";\r
846         }\r
847         return $x;\r
848     }\r
849     \r
850     /**\r
851      * Removes files from the $dir array that do not match the search string in\r
852      * $match\r
853      * @param array $dir array of filenames (full path)\r
854      * @param string $match search string with wildcards\r
855      * @author Greg Beaver <cellog@php.net>\r
856      * @return string|array listing of every file in a directory that matches\r
857      *                      the search string\r
858      */\r
859     function removeNonMatches($dir, $match)\r
860     {\r
861         $match = $this->getRegExpableSearchString($match);\r
862         $nodir = false;\r
863         if (!is_array($dir))\r
864         {\r
865             $dir = array($dir);\r
866             $nodir = true;\r
867         }\r
868         foreach($dir as $i => $file)\r
869         {\r
870             preg_match('/^'.$match.'$/',basename($file),$find);\r
871             if (!count($find)) unset($dir[$i]);\r
872         }\r
873         if ($nodir) return $dir[0];\r
874         return $dir;\r
875     }\r
876     \r
877     /**\r
878      * Take a filename with wildcards and return all files that match the\r
879      * wildcards\r
880      * @param string $file a full path from the -f command-line parameter, with\r
881      * potential * and ? wildcards.\r
882      * @return mixed if $file contains wildcards, returns an array of matching\r
883      *               files, otherwise returns false\r
884      * @author Greg Beaver <cellog@php.net>\r
885      * @uses dirList\r
886      * @uses removeNonMatches\r
887      */\r
888     function getAllFiles($file)\r
889     {\r
890         $path = realpath(dirname($file));\r
891         $file = basename($file);\r
892         // any wildcards?\r
893         if (is_numeric(strpos($file,'?')) || is_numeric(strpos($file,'*')))\r
894         {\r
895             $files = $this->dirList($path);\r
896             $a = $this->removeNonMatches($files,$file);\r
897             return $a;\r
898         }\r
899         return false;\r
900     }\r
901 }\r
902 \r
903 /**#@+\r
904  * Sorting functions for the file list\r
905  * @param string\r
906  * @param string\r
907  */\r
908 function Ioinc_sortfiles($a, $b)\r
909 {\r
910     return strnatcasecmp($a['file'],$b['file']);\r
911 }\r
912 \r
913 function Ioinc_mystrucsort($a, $b)\r
914 {\r
915     if (is_numeric($a) && is_string($b)) return 1;\r
916     if (is_numeric($b) && is_string($a)) return -1;\r
917     if (is_numeric($a) && is_numeric($b))\r
918     {\r
919         if ($a > $b) return 1;\r
920         if ($a < $b) return -1;\r
921         if ($a == $b) return 0;\r
922     }\r
923     return strnatcasecmp($a,$b);\r
924 }\r
925 /**#@-*/\r
926 \r
927 /**\r
928  * Recursively add all the subdirectories of $contents to $dir without erasing anything in\r
929  * $dir\r
930  * @param array\r
931  * @param array\r
932  * @return array processed $dir\r
933  */\r
934 function set_dir($dir,$contents)\r
935 {\r
936     while(list($one,$two) = each($contents))\r
937     {\r
938         if (isset($dir[$one]))\r
939         {\r
940             $dir[$one] = set_dir($dir[$one],$contents[$one]);\r
941         } else $dir[$one] = $two;\r
942     }\r
943     return $dir;\r
944 }\r
945 \r
946 /**\r
947  * Recursively move contents of $struc into associative array\r
948  *\r
949  * The contents of $struc have many indexes like 'dir/subdir/subdir2'.\r
950  * This function converts them to\r
951  * array('dir' => array('subdir' => array('subdir2')))\r
952  * @param array struc is array('dir' => array of files in dir,'dir/subdir' => array of files in dir/subdir,...)\r
953  * @param array array form of 'dir/subdir/subdir2' array('dir','subdir','subdir2')\r
954  * @return array same as struc but with array('dir' => array(file1,file2,'subdir' => array(file1,...)))\r
955  */\r
956 function setup_dirs($struc,$dir,$contents)\r
957 {\r
958     if (!count($dir))\r
959     {\r
960         foreach($contents as $dir => $files)\r
961         {\r
962             if (is_string($dir))\r
963             {\r
964                 if (strpos($dir,'/'))\r
965                 {\r
966                     $test = true;\r
967                     $a = $contents[$dir];\r
968                     unset($contents[$dir]);\r
969                     $b = explode('/',$dir);\r
970                     $c = array_shift($b);\r
971                     if (isset($contents[$c]))\r
972                     {\r
973                         $contents[$c] = set_dir($contents[$c],setup_dirs(array(),$b,$a));\r
974                     } else $contents[$c] = setup_dirs(array(),$b,$a);\r
975                 }\r
976             }\r
977         }\r
978         return $contents;\r
979     }\r
980     $me = array_shift($dir);\r
981     if (!isset($struc[$me])) $struc[$me] = array();\r
982     $struc[$me] = setup_dirs($struc[$me],$dir,$contents);\r
983     return $struc;\r
984 }\r
985 \r
986 if (!function_exists('get_include_path')) {\r
987 function get_include_path()\r
988 {\r
989     return ini_get('include_path');\r
990 }\r
991 }\r
992 ?>\r