1<?php
2
3//this script may only be included - so its better to die if called directly.
4if (strpos($_SERVER["SCRIPT_NAME"],basename(__FILE__)) !== false) {
5  header("location: index.php");
6  exit;
7}
8
9//
10// $Archive: /iPage/V1.1/include/dir.php $
11// $Date: 2005-05-18 11:01:38 $
12// $Revision: 1.3 $
13//
14// $History: dir.php $
15//
16//
17// *****************  Version 5  *****************
18// User: @PICNet      Date: 18.03.04   Time: 14:12
19// User: Hannesd      Date: 28.11.00   Time: 14:12
20// Updated in $/iPage/V1.1/include
21//
22
23if ( !defined( "INCLUCDED_DIR" ) ) {
24	define( "INCLUCDED_DIR", TRUE  );
25
26/**
27 * CDir
28* Class for reading a directory structure.
29* aFiles contains multiple aFile entries
30* aFile:   Path        => relative path eg. ../xx/yy/
31*          File        => filename eg. filename (without extension)
32*          Extension   => ext
33*          IsDirectory => true/false
34*          FullName    => Path . File . "." . Extension
35*          FileName    => File . "." . Extension
36
37* Notes
38* Filenames with multiple Extensions: only the last extensions is saved as extensions
39* eg: aaa.bbb.ccc results in File=aaa.bbb and Extension=ccc
40* Filenames are stored in the same case as the are stored in the filesystem
41* sFilter is only applied to files.
42 * @package or.apicnet.io
43 * @version $Id: cdir.php,v 1.3 2005-05-18 11:01:38 mose Exp $
44 * @access public
45 **/
46Class CDir extends ErrorManager {
47    var $aFiles;
48
49    Function __construct(){
50        $this->Init();
51		parent::__construct();
52    }
53
54    Function Init(){
55        unset( $this->aFiles );
56        $this->aFiles = array();
57    }
58
59    /**
60     * CDir::Read()
61     *
62     * @param $sPath path eg. "../xx/yy/" (note the last "/")
63     * @param string $sInclude regular expression for filtering path- and filenames
64     * @param boolean $fRecursive true/false: go down the whole structure
65     * @param integer $levelRecursive number of whole structure down
66     * @param boolean $fFiles result set will contain entries which are files
67     * @param boolean $fDirectories result set will contain entries which are directories
68     * @param string $sRoot Root-Path. Will be appended to the entries.
69     * @param string $sExclude  regular expression for filtering path- and filenames
70     * @return
71     **/
72    Function Read( $sPath, $sInclude = "", $fRecursive = false, $levelRecursive=2, $fFiles = true, $fDirectories = true, $sRoot = "", $sExclude = "" ){
73        $oHandle = opendir( $sPath );
74        while ( $sFilename = readdir( $oHandle ) ){
75            $fInsert = true;
76
77            if ( $sFilename == "." || $sFilename == ".." )  continue;
78
79            $fIsDirectory = is_dir( $sPath . $sFilename );
80
81            if ( !$fFiles && !$fIsDirectory )      $fInsert = false;
82            if ( !$fDirectories && $fIsDirectory ) $fInsert = false;
83
84            if ( $fInsert && !$fIsDirectory && ( !empty( $sInclude ) || !empty( $sExclude ) ) ) {
85                $sFullname = $sRoot;
86                $sFullname .= $sFilename;
87
88                if ( !empty( $sInclude ) )
89                    if ( !preg_match( '/' . $sInclude . '/', $sFullname ) )
90                        $fInsert = false;
91
92                if ( !empty( $sExclude ) )
93                    if ( preg_match( '/' . $sExclude . '/', $sFullname ) )
94                        $fInsert = false;
95            }
96
97            if ( $fInsert ){
98                $i = strrpos( $sFilename, "." ) + 1;
99                if ( substr( $sFilename, $i - 1, 1 ) == "." ) {
100                    $sFile = substr( $sFilename, 0, $i - 1 );
101                    $sExtension = substr( $sFilename, $i );
102                } else {
103                    $sFile = $sFilename;
104                    $sExtension = "";
105                }
106
107                $aFile = array(
108                        "Path" => $sRoot,
109                        "File" => $sFile,
110                        "Extension" => $sExtension,
111                        "IsDirectory" => $fIsDirectory
112                    );
113
114                //Insert current file into aFiles array
115                $this->aFiles[] = $aFile;
116            }
117
118            //Recursion?
119            if ( $fRecursive && $fIsDirectory && ($levelRecursive > 0 || $levelRecursive = -1))
120                $this->Read( $sPath . $sFilename . "/", $sInclude, $fRecursive, $levelRecursive - 1, $fFiles, $fDirectories, $sRoot . $sFilename . "/", $sExclude );
121        }
122
123        closedir( $oHandle );
124    }
125
126    /**
127     * CDir::Output()
128     *
129     * @return
130     **/
131    Function Output(){
132        reset( $this->aFiles );
133		foreach ($this->aFiles as $aFile) {
134			$this->OutputFile($aFile);
135		}
136	}
137
138    /**
139     * CDir::OutputFile()
140     *
141     * @param $aFile
142     * @return
143     **/
144    Function OutputFile( $aFile ){
145        printf( "Path: %s<br>\n", $this->GetPath( $aFile ) );
146        printf( "File: %s<br>\n", $this->GetFile( $aFile ) );
147        printf( "Extension: %s<br>\n", $this->GetExtension( $aFile ) );
148        printf( "IsDirectory: %s<br>\n", $this->GetIsDirectory( $aFile ) ? "true" : "false" );
149        printf( "IsFile: %s<br>\n", $this->GetIsFile( $aFile ) ? "true" : "false" );
150        printf( "FullName: %s<br>\n", $this->FullName( $aFile ) );
151        printf( "FileName: %s<br>\n", $this->FileName( $aFile ) );
152        printf( "DirectoryName: %s<br>\n", $this->DirectoryName( $aFile ) );
153        echo "<hr>\n";
154    }
155
156    /**
157     * CDir::GetPath()
158     *
159     * @param $aFile
160     * @return
161     **/
162    Function GetPath( $aFile ){
163        return( $aFile[ "Path" ] );
164    }
165
166    /**
167     * CDir::GetFile()
168     *
169     * @param $aFile
170     * @return
171     **/
172    Function GetFile( $aFile ){
173        return( $aFile[ "File" ] );
174    }
175
176    /**
177     * CDir::GetExtension()
178     *
179     * @param $aFile
180     * @return
181     **/
182    Function GetExtension( $aFile ){
183        return( $aFile[ "Extension" ] );
184    }
185
186    /**
187     * CDir::GetIsDirectory()
188     *
189     * @param $aFile
190     * @return
191     **/
192    Function GetIsDirectory( $aFile ){
193        return( $aFile[ "IsDirectory" ] );
194    }
195
196    /**
197     * CDir::GetIsFile()
198     *
199     * @param $aFile
200     * @return
201     **/
202    Function GetIsFile( $aFile ){
203        return( !$this->GetIsDirectory( $aFile ) );
204    }
205
206    /**
207     * CDir::FullName()
208     *
209     * @param $aFile
210     * @return
211     **/
212    Function FullName( $aFile ){
213        return( $this->GetPath( $aFile ) . $this->FileName( $aFile ) );
214    }
215
216    /**
217     * CDir::FileName()
218     *
219     * @param $aFile
220     * @return
221     **/
222    Function FileName( $aFile ){
223        $sBuffer = $this->DirectoryName( $aFile );
224        if ( $this->GetIsDirectory( $aFile ) )
225            $sBuffer .= "/";
226
227        return( $sBuffer );
228    }
229
230    /**
231     * CDir::DirectoryName() DirectoryName returns the same as FileName, but without a ending "/" for Directories.
232     *
233     * @param $aFile
234     * @return
235     **/
236    Function DirectoryName( $aFile ){
237        $sBuffer = $this->GetExtension( $aFile );
238        if ( !empty( $sBuffer ) )
239            $sBuffer = "." . $sBuffer;
240        $sBuffer = $this->GetFile( $aFile ) . $sBuffer;
241
242        return( $sBuffer );
243    }
244
245	// Based on the other notes given before.
246	// Sorts an array (you know the kind) by key
247	// and by the comparison operator you prefer.
248
249	// Note that instead of most important criteron first, it's
250	// least important criterion first.
251
252	// The default sort order is ascending, and the default sort
253	// type is strnatcmp.
254
255	// function multisort($array[, $key, $order, $type]...)
256	function multisort($array){
257	   for($i = 1; $i < func_num_args(); $i += 3){
258	       $key = func_get_arg($i);
259
260	       $order = true;
261	       if($i + 1 < func_num_args())
262	           $order = func_get_arg($i + 1);
263
264	       $type = 0;
265	       if($i + 2 < func_num_args())
266	           $type = func_get_arg($i + 2);
267
268	       switch($type){
269	           case 1: // Case insensitive natural.
270	               $t = 'strcasenatcmp($a[' . $key . '], $b[' . $key . '])';
271	               break;
272	           case 2: // Numeric.
273	               $t = '$a[' . $key . '] - $b[' . $key . ']';
274	               break;
275	           case 3: // Case sensitive string.
276	               $t = 'strcmp($a[' . $key . '], $b[' . $key . '])';
277	               break;
278	           case 4: // Case insensitive string.
279	               $t = 'strcasecmp($a[' . $key . '], $b[' . $key . '])';
280	               break;
281	           default: // Case sensitive natural.
282	               $t = 'strnatcmp($a[' . $key . '], $b[' . $key . '])';
283	               break;
284	       }
285
286	       uasort($array, create_function('$a, $b', 'return ' . ($order ? '' : '-') . '(' . $t . ');'));
287	   }
288
289	   return $array;
290	}
291
292	//function multisort([$key, $order, $type]...)
293	function sort(){
294		$error  = FALSE;
295		$params = "";
296		$plen   = func_num_args();
297		$result = NULL;
298
299		if (func_num_args() > 2) {
300
301			for($i = 0; $i < func_num_args(); $i += 3){
302				$Key = func_get_arg($i);
303				$order = func_get_arg($i + 1);
304				if ($order) $order = 1;
305				else $order = 0;
306				$type = func_get_arg($i + 2);
307				$params .= $Key.", ".$order.", ".$type;
308				if ($i + 3 < func_num_args()) $params .= ', ';
309			}
310		} else {
311			$error = TRUE;
312		}
313
314		if (!$error) {
315			//echo("params : multisort(\$this->aFiles, ".$params.");<br>");
316			eval("\$result = \$this->multisort(\$this->aFiles, ".$params.");");
317		} else {
318			$this -> ErrorTracker(4, "Error dans le trie du tableau", 'sort', __FILE__, __LINE__);
319		}
320
321		return $result;
322	}
323}
324
325}
326// if ( !INCLUCDED_DIR )
327