Moodle-Flashcard /filesystemlib.php

Language PHP Lines 399
MD5 Hash 1bffd7e52262df511610b6203b360346
Repository https://github.com/ccle/Moodle-Flashcard.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?php

/**
 * File System Abstract Layer
 * Support library
 *
 * @author Valery Fremaux (France) (admin@ethnoinformatique.fr)
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package extralibs
 * @category third-party libs
 * @date 2007/11/04
 *
 */

//avoids reloading the lib when keeped in third party plugin
if (!function_exists('filesystem_create_dir')) {

define('FS_RECURSIVE', true);
define('FS_NON_RECURSIVE', false);

define('FS_SHOW_HIDDEN', true);
define('FS_IGNORE_HIDDEN', false);

define('FS_NO_DIRS', 2);
define('FS_ONLY_DIRS', 1);
define('FS_ALL_ENTRIES', 0);

define('FS_FULL_DELETE', true);
define('FS_CLEAR_CONTENT', false);


/**
* creates a dir in file system optionally creating all pathes on the way
* @param string $path the relative path from dataroot
* @param boolean $recursive if true, creates recursively all path elements
* @param string $pathbase the base path
*/
function filesystem_create_dir($path, $recursive = 0, $pathbase=null) {
    global $CFG;
   
    if (is_null($pathbase)){
        $pathbase = $CFG->dataroot . '/';
    } elseif ($pathbase === '') {
        $pathbase = '';
    } else {
        $pathbase = $pathbase . '/'; 
    }

    $result = true;
    if (!$recursive){
       if (@$CFG->filedebug) mtrace("creating dir <i>{$path}</i><br/>");
       $oldMask = umask(0);
       if(!filesystem_is_dir($path, $pathbase)) $result = @mkdir($pathbase . $path, 0777);
       umask($oldMask);
       return $result;
    }
    else {
       $parts = explode('/', $path);
       $pathTo = '';
       for($i = 0; $i < count($parts) && $result; $i++){
          $pathTo .= '/' . $parts[$i];
          $result = filesystem_create_dir($pathTo, 0, $pathbase);
       }
       return $result;
    }
}

/**
* tests if path is a dir. A simple wrapper to is_dir 
* @param string $relativepath the path from dataroot
* @param string $pathbase the base path
*/
function filesystem_is_dir($relativepath, $pathbase=null){
    global $CFG;

    if (is_null($pathbase)){
        $pathbase = $CFG->dataroot . '/';
    } elseif ($pathbase === '') {
        $pathbase = '';
    } else {
        $pathbase = $pathbase . '/'; 
    }

    if (@$CFG->filedebug) mtrace("is dir <i>$pathbase$relativepath</i><br/>");
    return is_dir($pathbase . $relativepath);
} 

/**
* checks if file (or dir) exists. A simple wrapper to file_exists
* @param string $relativepath the path from dataroot
* @param string $pathbase the base path
*/
function filesystem_file_exists($relativepath, $pathbase=null){
    global $CFG;

    if (is_null($pathbase)){
        $pathbase = $CFG->dataroot . '/';
    } elseif ($pathbase === '') {
        $pathbase = '';
    } else {
        $pathbase = $pathbase . '/'; 
    }

    if (@$CFG->filedebug) mtrace("file exists <i>$pathbase$relativepath</i><br/>");
    return file_exists($pathbase . $relativepath);
} 

/**
* scans for entries within a directory
* @param string $relativepath the path from dataroot
* @param boolean $hiddens shows or hide hidden files
* @param int $what selects only dirs, files or both
* @param string $pathbase the base path
* @return an array of entries wich are local names in path
*/
function filesystem_scan_dir($relativepath, $hiddens = 0, $what = 0, $pathbase=null){
    global $CFG;

    if (is_null($pathbase)){
        $pathbase = $CFG->dataroot . '/';
    } elseif ($pathbase === '') {
        $pathbase = '';
    } else {
        $pathbase = $pathbase . '/'; 
    }

    if (@$CFG->filedebug) mtrace("scanning <i>$pathbase$relativepath</i><br/>");
    $dir = opendir($pathbase . $relativepath);
    $entries = array();
    while ($anEntry = readdir($dir)){
        if ($what == FS_ONLY_DIRS){
            $subpath = $relativepath . '/' . $anEntry;
            $subpath = preg_replace("/^\//", "", $subpath);
            if (!filesystem_is_dir($subpath, $pathbase)) continue ;
        }
        if ($what == FS_NO_DIRS){
            $subpath = $relativepath . '/' . $anEntry;
            $subpath = preg_replace("/^\//", "", $subpath);
            if (filesystem_is_dir($subpath, $pathbase)) continue ;
        }
        if ($hiddens) {
            if (($anEntry != '.') && ($anEntry != '..')) $entries[] = $anEntry;
        } else {
            if (!preg_match("/^\./", $anEntry)) $entries[] = $anEntry;
        }
    }
    closedir($dir);
    return $entries;
} 

/**
* clears and removes an entire dir
* @param string $relativepath the path from dataroot
* @param boolean $fulldelete if true, removes the dir root either
* @param string $pathbase the base path
* @return an array of entries wich are local names in path
*/
function filesystem_clear_dir($relativepath, $fullDelete = false, $pathbase=null) {
    global $CFG;

    if (is_null($pathbase)){
        $pathbase = $CFG->dataroot . '/';
    } elseif ($pathbase === '') {
        $pathbase = '';
    } else {
        $pathbase = $pathbase . '/'; 
    }

    if (@$CFG->filedebug) mtrace("clearing dir <i>$pathbase$relativepath</i><br/>");
    $exists = filesystem_is_dir($relativepath, $pathbase);
    if (!$exists && !$fullDelete) {
        return filesystem_create_dir($relativepath, $pathbase);   
    }
    if (!$exists && $fullDelete) {
        return true;
    }
    $files = filesystem_scan_dir($relativepath, FS_SHOW_HIDDEN, FS_ALL_ENTRIES, $pathbase);
    foreach($files as $aFile) {
        if ($aFile == "." || $aFile == "..") continue ;
        if (filesystem_is_dir("{$relativepath}/{$aFile}", $pathbase)){
            filesystem_clear_dir("{$relativepath}/{$aFile}", FS_FULL_DELETE, $pathbase);
            // fs_removeDir("{$relativepath}/{$aFile}");
        } else {
            filesystem_delete_file("{$relativepath}/{$aFile}", $pathbase);
        }
    }
    if (file_exists($pathbase . $relativepath) && $fullDelete) return filesystem_remove_dir($relativepath, $pathbase);
    return false;
}

/**
* copies recursively a subtree from a location to another
* @param string $source the source path from dataroot
* @param string $dest the dest path from dataroot
* @param string $pathbase the base path
* @return void
*/
function filesystem_copy_tree($source, $dest, $pathbase=null) {
    global $CFG;
   
    if (is_null($pathbase)){
        $pathbase = $CFG->dataroot . '/';
    } elseif ($pathbase === '') {
        $pathbase = '';
    } else {
        $pathbase = $pathbase . '/'; 
    }

   if (@$CFG->filedebug) mtrace("copying tree <i>$pathbase$source</i> to <i>$pathbase$dest</i><br/>");
   if (file_exists($dest) && !filesystem_is_dir($dest, $pathbase)) {
      return;
   }
   if (!filesystem_is_dir($dest, $pathbase)) {
      filesystem_create_dir($dest, FS_RECURSIVE, $pathbase);
   }
   $files = array();
   $files = filesystem_scan_dir($source, FS_SHOW_HIDDEN, FS_ALL_ENTRIES, $pathbase);
   foreach($files as $aFile) {
      if ($aFile == '.' || $aFile == '..') next;
      if (filesystem_is_dir("{$source}/{$aFile}", $pathbase)) {
         filesystem_create_dir("{$dest}/{$aFile}", FS_NON_RECURSIVE, $pathbase);
         if (count(filesystem_is_dir("{$source}/{$aFile}", $pathbase)) != 0) {
             filesystem_copy_tree("{$source}/{$aFile}", "{$dest}/{$aFile}", $pathbase);
         }
      } else {
           filesystem_copy_file("{$source}/{$aFile}", "{$dest}/{$aFile}", $pathbase);
      }
   }
}

/**
* stores a file content in the file system, creating on the way directories if needed
* @param string $relativepath the path from dataroot
* @param string $data the data to store in
* @param string $pathbase the base path
*/
function filesystem_store_file($relativepath, $data, $pathbase=null) {
    global $CFG;

    if (is_null($pathbase)){
        $pathbase = $CFG->dataroot . '/';
    } elseif ($pathbase === '') {
        $pathbase = '';
    } else {
        $pathbase = $pathbase . '/'; 
    }

    if (@$CFG->filedebug) mtrace("storing <i>$pathbase$relativepath</i><br/>");
    $parts = pathinfo($relativepath);
    if (!filesystem_is_dir($parts['dirname'], $pathbase)) filesystem_create_dir($parts['dirname'], $pathbase);
    $FILE = fopen($pathbase . $relativepath, "w");
    if (!$FILE) return false;
    fwrite ($FILE, $data);
    fclose($FILE);
    return true;
}

/**
* reads a file content and returns scalar string
* @param string $relativepath the path from dataroot
* @param string $pathbase the base path
* @return the data as a string
*/
function filesystem_read_a_file($relativepath, $pathbase=null) {
    global $CFG;

    if (is_null($pathbase)){
        $pathbase = $CFG->dataroot . '/';
    } elseif ($pathbase === '') {
        $pathbase = '';
    } else {
        $pathbase = $pathbase . '/'; 
    }

    if (@$CFG->filedebug) mtrace("reading <i>$pathbase$relativepath</i><br/>");
    $fullPath = $pathbase . $relativepath;
    if (file_exists($fullPath)){
        $FILE = file($fullPath);
        return implode('', $FILE);
    }
    return false;
}

/**
* deletes a file. Simple wrapper to unlink
* @param string $relativepath the path from dataroot
* @param string $pathbase the base path
* @return the data as a string
*/
function filesystem_delete_file($relativepath, $pathbase=null){
    global $CFG;

    if (is_null($pathbase)){
        $pathbase = $CFG->dataroot . '/';
    } elseif ($pathbase === '') {
        $pathbase = '';
    } else {
        $pathbase = $pathbase . '/'; 
    }

    if (@$CFG->filedebug) mtrace("deleting file <i>$relativepath</i><br/>");
    if (filesystem_file_exists($relativepath, $pathbase) && !filesystem_is_dir($relativepath, $pathbase))
        return unlink($pathbase . $relativepath);
    return false;
}

/**
* removes an empty dir. Simple wrapper to rmdir
* @param string $relativepath the path from dataroot
* @param string $pathbase the base path
*/
function filesystem_remove_dir($relativepath, $pathbase=null){
    global $CFG;

    if (is_null($pathbase)){
        $pathbase = $CFG->dataroot . '/';
    } elseif ($pathbase === '') {
        $pathbase = '';
    } else {
        $pathbase = $pathbase . '/'; 
    }

    if (@$CFG->filedebug) mtrace("deleting dir <i>$relativepath</i><br/>");
    if (filesystem_file_exists($relativepath, $pathbase))
      return rmdir($pathbase . $relativepath);
}

/**
* renames a file. Simple wrapper to rename
* @param string $relativepath the path from dataroot
* @param string $pathbase the base path
*/
function filesystem_move_file($source, $dest, $pathbase=null){
    global $CFG;

    if (is_null($pathbase)){
        $pathbase = $CFG->dataroot . '/';
    } elseif ($pathbase === '') {
        $pathbase = '';
    } else {
        $pathbase = $pathbase . '/'; 
    }

    if (filesystem_file_exists($source, $pathbase)){
        if (@$CFG->filedebug) mtrace("moving file/dir <i>$source</i> to <i>$dest</i><br/>");
        return rename($pathbase . $source, $pathbase . '/' . $dest);
    }
    return false;
}

/**
* copy a file creating all path on the way if needed
* @param string $source the source path from dataroot
* @param string $dest the dest path from dataroot
* @param string $pathbase the base path
*/
function filesystem_copy_file($source, $dest, $pathbase=null) {
    global $CFG;

    if (is_null($pathbase)){
        $pathbase = $CFG->dataroot . '/';
    } elseif ($pathbase === '') {
        $pathbase = '';
    } else {
        $pathbase = $pathbase . '/'; 
    }

    if (@$CFG->filedebug) mtrace("copying file <i>$pathbase$source</i> to <i>$pathbase$dest</i><br/>");
    if (!filesystem_file_exists($source, $pathbase)) return -1;
    $parts = pathinfo($dest);
    if (!filesystem_is_dir($parts['dirname'], $pathbase)) filesystem_create_dir($parts['dirname'], $pathbase);
    return copy($pathbase . $source, $pathbase . $dest);
}

/**
* gets a filtered list of files
* @param string $path the path from dataroot
* @param string $filemask the filemask for filtering
* @param string $pathbase the base path
*/
function filesystem_get_file_list($path, $filemask = "*.*", $pathbase=null) {
    global $CFG;

    if (is_null($pathbase)){
        $pathbase = $CFG->dataroot . '/';
    } elseif ($pathbase === '') {
        $pathbase = '';
    } else {
        $pathbase = $pathbase . '/'; 
    }
   
    if (preg_match("/(.*)\/$/", $path, $matches)) $path = $matches[1];
    $files = glob($pathbase . "{$path}/{$filemask}");
    return $files;
}

//
}
?>
Back to Top