c8402670936a116aefc0a17482fb77b2029eb690
[atutor.git] / mods / utf8conv / readDir.php
1 <?php
2 /* This is a php class to Read a directory recursively and executes defined events */
3 /* when each directory element is read                                             */
4
5 class readDir
6 {  
7         var $path; 
8         var $errtxt; 
9         var $errcount; 
10         var $recurse; 
11         var $events; 
12         var $handlers;  
13         
14         /*** Constructor (executed when we instatiate the class)*/ 
15         function readDir()
16         { 
17                 $this->recursive = false; 
18                 $this->errcount = 0; 
19                 $this->events = array('readDir_dir', 'readDir_file'); 
20                 $this->handlers = array(); 
21         }  
22         
23         /*** Set the directory to read* @param string full directory path*/ 
24         function setPath( $path ) 
25         { 
26                 if (!is_dir($path)) 
27                 { 
28                         $this->_error('The supplied argument, '.$path.', is not a valid directory path!'); 
29                         return false; 
30                 } 
31                 
32                 $this->path = $path; 
33                 return true; 
34         }  
35         
36         /*** Set and event handler* @param string event name* @param string event handler function name*/ 
37         function setEvent( $event, $handler ) 
38         { 
39                 if (in_array($event, $this->events) !== false) 
40                 { 
41                         $this->handlers[$event] = $handler; 
42                 } 
43                 else 
44                 { 
45                         $this->_error('Event Type specified does not exist.'); 
46                         return false; 
47                 } 
48                 return true; 
49         }  
50         
51         /*** Set if we want to read through sub folders recursively* @param bool TRUE or FALSE*/ 
52         function readRecursive( $bool = true ) 
53         { 
54                 $this->recurse = $bool; 
55         }  
56         
57         /*** Read the directory*/ 
58         function read() 
59         { 
60                 if ( !is_dir($this->path) ) 
61                 { 
62                         $this->_error('Directory to read from is invalid.'.'Please use setPath() to defind a valid directory.'); 
63                         return false; 
64                 }  
65                 
66                 // all set, start reading 
67                 return $this->_read($this->path); 
68         }  
69                 
70         function _read($dir) 
71         { 
72                 if ($dh = opendir($dir)) 
73                 { 
74                         $i = 0; 
75                         while ($el = readdir($dh)) 
76                         { $path = $dir.'/'.$el;  
77                                 
78                                 if (is_dir($path) && $el != '.' && $el != '..') 
79                                 { 
80                                         if ($this->_trigger('readDir_dir', $path, $el) == -1) 
81                                         { 
82                                                 closedir($dh); 
83                                                 return true; 
84                                         }  
85                                         
86                                         if ($this->recurse) 
87                                         { 
88                                                 // read sub directories recursively 
89                                                 $this->_read($path); 
90                                         } 
91                                 } 
92                                 elseif (is_file($path)) 
93                                 { 
94                                         if ($this->_trigger('readDir_file', $path, $el) == -1) 
95                                         { 
96                                                 closedir($dh); 
97                                                 return true; 
98                                         } 
99                                 } 
100                                 
101                                 $i++; 
102                         }  
103                         
104                         closedir($dh); 
105                         return true; 
106                 } 
107                 
108                 else 
109                 { 
110                         $this->_error('Could not open the directory, '.$path); 
111                 } 
112                 return false; 
113         }  
114         
115         function _trigger($event, $path, $el) 
116         { 
117                 if ($this->handlers[$event]) 
118                 { 
119                         if (!function_exists($this->handlers[$event])) 
120                         { 
121                                 $this->_error('User Function, '.$this->handlers[$event].', defined for the event, '.$event.', does not exist'); 
122                                 return false; 
123                         } 
124                         
125                         return call_user_func($this->handlers[$event], $path, $el); 
126                 } 
127         }  
128         
129         function _error($txt) 
130         { 
131                 $this->errcount++; 
132                 $this->errtxt = $txt; 
133         }  
134         
135         /*** View the last error logged*/ 
136         function error() 
137         { 
138                 return $this->errtxt; 
139         }  
140         
141         /*** View the last error number*/ 
142         function errorCount() 
143         { 
144                 return $this->errcount; 
145         } 
146
147                 
148 ?>