source-function-F_renameMediaFile

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
Overview

Classes

Interfaces

Exceptions

Functions

  1: <?php
  2: //============================================================+
  3: // File name   : tce_functions_filemanager.php
  4: // Begin       : 2010-09-20
  5: // Last Update : 2013-04-12
  6: //
  7: // Description : Functions for TCExam file manager.
  8: //
  9: // Author: Nicola Asuni
 10: //
 11: // (c) Copyright:
 12: //               Nicola Asuni
 13: //               Tecnick.com LTD
 14: //               www.tecnick.com
 15: //               info@tecnick.com
 16: //
 17: // License:
 18: //    Copyright (C) 2004-2013 Nicola Asuni - Tecnick.com LTD
 19: //    See LICENSE.TXT file for more information.
 20: //============================================================+
 21: 
 22: /**
 23:  * @file
 24:  * Functions for TCExam file manager.
 25:  * @package com.tecnick.tcexam.admin
 26:  * @author Nicola Asuni
 27:  * @since 2010-09-20
 28:  */
 29: 
 30: /**
 31:  * Delete the selected media file
 32:  * @author Nicola Asuni
 33:  * @param $filename (string) the file name
 34:  * @return true in case of success, false otherwise
 35:  */
 36: function F_deleteMediaFile($filename)
 37: {
 38:     require_once('../config/tce_config.php');
 39:     if ($_SESSION['session_user_level'] < K_AUTH_DELETE_MEDIAFILE) {
 40:         // insufficient user level
 41:         return false;
 42:     }
 43:     $allowed_extensions = unserialize(K_ALLOWED_UPLOAD_EXTENSIONS);
 44:     $path_parts = pathinfo($filename);
 45:     if ((strpos($path_parts['dirname'].'/', K_PATH_CACHE) !== false)
 46:         and in_array(strtolower($path_parts['extension']), $allowed_extensions)
 47:         and (strpos($filename.'/', K_PATH_LANG_CACHE) === false)
 48:         and (strpos($filename.'/', K_PATH_BACKUP) === false)) {
 49:         return unlink($filename);
 50:     }
 51:     return false;
 52: }
 53: 
 54: /**
 55:  * Rename the selected media file
 56:  * @author Nicola Asuni
 57:  * @param $filename (string) old file name
 58:  * @param $newname (string) new file name
 59:  * @return true in case of success, false otherwise
 60:  */
 61: function F_renameMediaFile($filename, $newname)
 62: {
 63:     require_once('../config/tce_config.php');
 64:     if ($_SESSION['session_user_level'] < K_AUTH_RENAME_MEDIAFILE) {
 65:         // insufficient user level
 66:         return false;
 67:     }
 68:     $allowed_extensions = unserialize(K_ALLOWED_UPLOAD_EXTENSIONS);
 69:     $path_parts = pathinfo($filename);
 70:     $path_parts_new = pathinfo($newname);
 71:     if ((strpos($path_parts['dirname'].'/', K_PATH_CACHE) !== false)
 72:         and in_array(strtolower($path_parts['extension']), $allowed_extensions)
 73:         and (strpos($filename.'/', K_PATH_LANG_CACHE) === false)
 74:         and (strpos($filename.'/', K_PATH_BACKUP) === false)
 75:         and (strpos($path_parts_new['dirname'].'/', K_PATH_CACHE) !== false)
 76:         and in_array(strtolower($path_parts_new['extension']), $allowed_extensions)
 77:         and (strpos($newname.'/', K_PATH_LANG_CACHE) === false)
 78:         and (strpos($newname.'/', K_PATH_BACKUP) === false)) {
 79:         return rename($filename, $newname);
 80:     }
 81:     return false;
 82: }
 83: 
 84: /**
 85:  * Create a new media directory inside the cache
 86:  * @author Nicola Asuni
 87:  * @param $dirname (string) the directory name
 88:  * @return true in case of success, false otherwise
 89:  */
 90: function F_createMediaDir($dirname)
 91: {
 92:     require_once('../config/tce_config.php');
 93:     if ($_SESSION['session_user_level'] < K_AUTH_ADMIN_DIRS) {
 94:         // insufficient user level
 95:         return false;
 96:     }
 97:     if (strpos($dirname.'/', K_PATH_CACHE) !== false) {
 98:         $oldumask = @umask(0);
 99:         $ret = @mkdir($dirname, 0744, false);
100:         @umask($oldumask);
101:         return $ret;
102:     }
103:     return false;
104: }
105: 
106: /**
107:  * Delete the specified media directory
108:  * @author Nicola Asuni
109:  * @param $dirname (string) the directory name
110:  * @return true in case of success, false otherwise
111:  */
112: function F_deleteMediaDir($dirname)
113: {
114:     require_once('../config/tce_config.php');
115:     if ($_SESSION['session_user_level'] < K_AUTH_ADMIN_DIRS) {
116:         // insufficient user level
117:         return false;
118:     }
119:     if ((strpos($dirname.'/', K_PATH_CACHE) !== false) and (count(scandir($dirname)) <= 2)) {
120:         return @rmdir($dirname);
121:     }
122:     return false;
123: }
124: 
125: /**
126:  * Get file information
127:  * @author Nicola Asuni
128:  * @param $file (string) the file name
129:  * @return associative array containing file info or false in case of error
130:  */
131: function F_getFileInfo($file)
132: {
133:     require_once('../config/tce_config.php');
134:     $info = pathinfo($file);
135:     $info['dir'] = is_dir($file);
136:     $info['lastmod'] = date("Y-m-d H:i:s", @filemtime($file));
137:     $info['owner'] = @fileowner($file);
138:     $info['perms'] = @fileperms($file);
139:     if ($info['dir']) {
140:         $info['aperms'] = 'd';
141:     } else {
142:         $info['aperms'] = '-';
143:     }
144:     $info['aperms'] .= ($info['perms'] & 00400) ? 'r' : '-';
145:     $info['aperms'] .= ($info['perms'] & 00200) ? 'w' : '-';
146:     $info['aperms'] .= ($info['perms'] & 00100) ? 'x' : '-';
147:     $info['aperms'] .= ($info['perms'] & 00040) ? 'r' : '-';
148:     $info['aperms'] .= ($info['perms'] & 00020) ? 'w' : '-';
149:     $info['aperms'] .= ($info['perms'] & 00010) ? 'x' : '-';
150:     $info['aperms'] .= ($info['perms'] & 00004) ? 'r' : '-';
151:     $info['aperms'] .= ($info['perms'] & 00002) ? 'w' : '-';
152:     $info['aperms'] .= ($info['perms'] & 00001) ? 'x' : '-';
153:     $info['size'] = @filesize($file);
154:     $info['link'] = is_link($file);
155:     if ($info['link']) {
156:         $info['linkto'] = readlink($file);
157:     }
158:     if (!isset($info['extension'])) {
159:         $info['extension'] = '';
160:     }
161:     $info['tcefile'] = substr($file, strlen(K_PATH_CACHE));
162:     $info['tcename'] = substr($info['tcefile'], 0, -(strlen($info['extension']) + 1));
163:     return $info;
164: }
165: 
166: /**
167:  * Return a formatted file size
168:  * @author Nicola Asuni
169:  * @param $size (int) size in bytes
170:  * @return string formatted size
171:  */
172: function F_formatFileSize($size)
173: {
174:     $out = ''; // string to be returned
175:     $mult = array('B ', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); // multipliers
176:     if ($size == 0) {
177:         $out = '0';
178:     } else {
179:         $i = floor(log($size, 1024));
180:         $out .= round(($size / pow(1024, $i)), $i > 1 ? 2 : 0);
181:         $out .= ' '.$mult[$i];
182:     }
183:     return $out;
184: }
185: 
186: /**
187:  * Get an html string containing active path of the specified directory with links to subdirectories.
188:  * @author Nicola Asuni
189:  * @param $dirpath (string) the directory path
190:  * @param $viewmode (boolean) true=table, false=visual
191:  * @return an html string
192:  */
193: function F_getMediaDirPathLink($dirpath, $viewmode = true)
194: {
195:     global $l, $db;
196:     require_once('../config/tce_config.php');
197:     $mode = intval($viewmode);
198:     $out = ''; //string to be returned
199:     // write root link
200:     $out .= '<a href="'.$_SERVER['SCRIPT_NAME'].'?d='.urlencode(K_PATH_CACHE).'&amp;v='.$mode.'" title="CACHE ROOT">[CACHE]</a> /';
201:     // conform windows-style directories
202:     $dirpath = str_replace("\\", '/', $dirpath); // Windows compatibility
203:     // remove cache root from path
204:     $dirpath = substr($dirpath, strlen(K_PATH_CACHE));
205:     if ($dirpath !== false) {
206:         // get subdirs
207:         $dirs = preg_split('/[\/]+/', $dirpath, -1, PREG_SPLIT_NO_EMPTY);
208:         $current_dir = K_PATH_CACHE;
209:         foreach ($dirs as $dir) {
210:             $current_dir .= $dir.'/';
211:             $out .= ' <a href="'.$_SERVER['SCRIPT_NAME'].'?d='.urlencode($current_dir).'&amp;v='.$mode.'" title="'.$l['w_change_dir'].'">'.$dir.'</a> /';
212:         }
213:     }
214:     return $out;
215: }
216: 
217: /**
218:  * Get an associative array of directories and folder inside the specified dir.
219:  * @author Nicola Asuni
220:  * @param $dir (string) the starting directory path
221:  * @param $rootdir (string) the user root dir.
222:  * @param $authdirs (string) regular expression containing the authorized dirs.
223:  * @return an associative array containing sorted 'dirs' and 'files'
224:  */
225: function F_getDirFiles($dir, $rootdir = K_PATH_CACHE, $authdirs = '')
226: {
227:     $data['dirs'] = array();
228:     $data['files'] = array();
229:     // open dir
230:     $dirhdl = @opendir($dir);
231:     if ($dirhdl === false) {
232:         return $data;
233:     }
234:     while ($file = readdir($dirhdl)) {
235:         if (($file != '.') and ($file != '..')) {
236:             $filename = $dir.$file;
237:             if (F_isAuthorizedDir($filename.'/', $rootdir, $authdirs)) {
238:                 if (is_dir($filename)) {
239:                     if ((strpos($filename.'/', K_PATH_LANG_CACHE) === false) and (strpos($filename.'/', K_PATH_BACKUP) === false)) {
240:                         $data['dirs'][] = $filename;
241:                     }
242:                 } else {
243:                     $data['files'][] = $filename;
244:                 }
245:             }
246:         }
247:     }
248:     // sort files alphabetically
249:     natcasesort($data['dirs']);
250:     natcasesort($data['files']);
251:     return $data;
252: }
253: 
254: /**
255:  * Return true if the file is used on question or answer descriptions
256:  * @author Nicola Asuni
257:  * @param $file (string) the file to search
258:  * @return true if the file is used, false otherwise
259:  */
260: function F_isUsedMediaFile($file)
261: {
262:     global $l, $db;
263:     require_once('../config/tce_config.php');
264:     // remove cache root from file path
265:     $file = substr($file, strlen(K_PATH_CACHE));
266:     // search on questions
267:     $sql = 'SELECT question_id FROM '.K_TABLE_QUESTIONS.' WHERE question_description LIKE \'%'.$file.'[/object%\' OR question_explanation LIKE \'%'.$file.'[/object%\' LIMIT 1';
268:     if ($r = F_db_query($sql, $db)) {
269:         if ($m = F_db_fetch_array($r)) {
270:             return true;
271:         }
272:     } else {
273:         F_display_db_error();
274:     }
275:     // search on answers
276:     $sql = 'SELECT answer_id FROM '.K_TABLE_ANSWERS.' WHERE answer_description LIKE \'%'.$file.'[/object%\' OR answer_explanation LIKE \'%'.$file.'[/object%\' LIMIT 1';
277:     if ($r = F_db_query($sql, $db)) {
278:         if ($m = F_db_fetch_array($r)) {
279:             return true;
280:         }
281:     } else {
282:         F_display_db_error();
283:     }
284:     return false;
285: }
286: 
287: /**
288:  * Get an html table containing files and subdirs
289:  * @author Nicola Asuni
290:  * @param $dir (string) the starting directory path
291:  * @param $selected (string) the selected file
292:  * @param $params (string) additional parameters to add on links
293:  * @param $rootdir (string) the user root dir.
294:  * @param $authdirs (string) regular expression containing the authorized dirs.
295:  * @return html table
296:  */
297: function F_getDirTable($dir, $selected = '', $params = '', $rootdir = K_PATH_CACHE, $authdirs = '')
298: {
299:     global $l;
300:     require_once('../config/tce_config.php');
301:     $allowed_extensions = unserialize(K_ALLOWED_UPLOAD_EXTENSIONS);
302:     $out = ''; // html string to be returned
303:     $out .= '<table class="filemanager">'.K_NEWLINE;
304:     // header
305:     $out .= '<tr>';
306:     $out .= '<th>'.$l['w_name'].'</th>';
307:     $out .= '<th>'.$l['w_size'].'</th>';
308:     $out .= '<th title="'.$l['w_datetime_format'].'">'.$l['w_date'].'</th>';
309:     $out .= '<th>'.$l['w_permissions'].'</th>';
310:     $out .= '</tr>'.K_NEWLINE;
311:     $data = F_getDirFiles($dir, $rootdir, $authdirs);
312:     $usrdir = $rootdir.$_SESSION['session_user_id'];
313:     // dirs
314:     foreach ($data['dirs'] as $file) {
315:         $info = F_getFileInfo($file);
316:         $current_dir = urlencode($dir.$info['basename'].'/');
317:         if ($file == $usrdir) {
318:             $out .= '<tr style="background-color:#ddffdd;font-family:monospace;color:#660000;">';
319:         } else {
320:             $out .= '<tr style="background-color:#dddddd;font-family:monospace;color:#660000;">';
321:         }
322:         $out .= '<td><strong><a href="'.$_SERVER['SCRIPT_NAME'].'?d='.$current_dir.'&amp;v=1'.$params.'" title="'.$l['w_change_dir'].'" style="text-decoration:underline;">'.$info['basename'].'</a></strong></td>';
323:         $out .= '<td style="text-align:right;">'.F_formatFileSize($info['size']).'</td>';
324:         $out .= '<td>'.$info['lastmod'].'</td>';
325:         $out .= '<td>'.$info['aperms'].'</td>';
326:         $out .= '</tr>'.K_NEWLINE;
327:     }
328:     // files
329:     $current_dir = urlencode($dir);
330:     foreach ($data['files'] as $file) {
331:         $info = F_getFileInfo($file);
332:         if (isset($info['extension']) and in_array(strtolower($info['extension']), $allowed_extensions) and (substr($info['basename'], 0, 6) != 'latex_')) {
333:             $current_file = urlencode($dir.$info['basename']);
334:             if ($info['basename'] == $selected) {
335:                 $out .= '<tr style="background-color:#ffffcc;font-family:monospace;">';
336:             } else {
337:                 $out .= '<tr style="font-family:monospace;">';
338:             }
339:             $out .= '<td><a href="'.$_SERVER['SCRIPT_NAME'].'?d='.$current_dir.'&amp;f='.urlencode($current_file).'&amp;v=1'.$params.'" title="'.$l['w_select'].'">'.$info['basename'].'</a></td>';
340:             $out .= '<td style="text-align:right;">'.F_formatFileSize($info['size']).'</td>';
341:             //$out .= '<td style="text-align:right;">'.$info['size'].'</td>';
342:             $out .= '<td>'.$info['lastmod'].'</td>';
343:             $out .= '<td>'.$info['aperms'].'</td>';
344:             $out .= '</tr>'.K_NEWLINE;
345:         }
346:     }
347:     $out .= '</table>'.K_NEWLINE;
348:     return $out;
349: }
350: 
351: /**
352:  * Get an html visual list of files and subdirs
353:  * @author Nicola Asuni
354:  * @param $dir (string) the starting directory path
355:  * @param $selected (string) the selected file
356:  * @param $params (string) additional parameters to add on links
357:  * @param $rootdir (string) the user root dir.
358:  * @param $authdirs (string) regular expression containing the authorized dirs.
359:  * @return html table
360:  */
361: function F_getDirVisualTable($dir, $selected = '', $params = '', $rootdir = K_PATH_CACHE, $authdirs = '')
362: {
363:     global $l;
364:     require_once('../config/tce_config.php');
365:     $imgformats = array('gif', 'jpg', 'jpeg', 'png', 'svg');
366:     $allowed_extensions = unserialize(K_ALLOWED_UPLOAD_EXTENSIONS);
367:     $out = ''; // html string to be returned
368:     $data = F_getDirFiles($dir, $rootdir, $authdirs);
369:     // dirs
370:     foreach ($data['dirs'] as $file) {
371:         $info = F_getFileInfo($file);
372:         $current_dir = urlencode($dir.$info['basename'].'/');
373:         $out .= '<table style="float:left;border:none;margin:1px;padding:0;width:158px;background-color:#007fff;">';
374:         $out .= '<tr style="height:16px;font-family:monospace;font-size:12px;font-weight:bold;color:white;"><th>';
375:         $filename = $info['basename'];
376:         if (strlen($filename) > 20) {
377:             $filename = substr($filename, 0, 20).'...';
378:         }
379:         $out .= $filename;
380:         $out .= '</th></tr>';
381:         $out .= '<tr style="height:160px;"><td style="text-align:center;vertical-align:middle;background-color:white;">';
382:         $out .= '<a href="'.$_SERVER['SCRIPT_NAME'].'?d='.$current_dir.'&amp;v=0'.$params.'" title="'.$l['w_change_dir'].' : '.$info['basename'].'" style="text-decoration:underline;"><img src="'.K_PATH_IMAGES.'dir.png" width="50" height="50" alt="'.$l['w_change_dir'].' : '.$info['basename'].'" style="border:none;" /></a>';
383:         $out .= '</td></tr>';
384:         $out .= '</table>';
385:     }
386:     // files
387:     $current_dir = urlencode($dir);
388:     foreach ($data['files'] as $file) {
389:         $info = F_getFileInfo($file);
390:         if (isset($info['extension']) and in_array(strtolower($info['extension']), $allowed_extensions) and (substr($info['basename'], 0, 6) != 'latex_')) {
391:             $current_file = urlencode($dir.$info['basename']);
392:             if ($info['basename'] == $selected) {
393:                 $bgcolor = '#009900';
394:             } else {
395:                 $bgcolor = '#333333';
396:             }
397:             if (in_array(strtolower($info['extension']), $imgformats)) {
398:                 $w = 150;
399:                 $h = 150;
400:                 $imgicon = F_objects_replacement($info['tcename'], $info['extension'], 0, 0, $l['w_preview'], $w, $h);
401:             } else {
402:                 $imgicon = '<img src="'.K_PATH_IMAGES.'file.png" width="39" height="50" alt="'.$l['w_select'].' : '.$info['basename'].' ('.F_formatFileSize($info['size']).')'.'" style="border:none;" />';
403:             }
404:             $out .= '<table style="float:left;border:none;margin:1px;padding:0;width:158px;background-color:'.$bgcolor.';">';
405:             $out .= '<tr style="height:16px;font-family:monospace;font-size:12px;color:white;"><th>';
406:             $filename = $info['basename'];
407:             if (strlen($filename) > 20) {
408:                 $filename = substr(substr($filename, 0, -(strlen($info['extension']) + 1)), 0, 15).'&rarr;.'.$info['extension'];
409:             }
410:             $out .= $filename;
411:             $out .= '</th></tr>';
412:             $out .= '<tr style="height:160px;"><td style="text-align:center;vertical-align:middle;background-color:white;">';
413:             $out .= '<a href="'.$_SERVER['SCRIPT_NAME'].'?d='.$current_dir.'&amp;f='.urlencode($current_file).'&amp;v=0'.$params.'" title="'.$l['w_select'].' : '.$info['basename'].' ('.F_formatFileSize($info['size']).')'.'">'.$imgicon.'</a>';
414:             $out .= '</td></tr>';
415:             $out .= '</table>';
416:         }
417:     }
418:     $out .= '<br style="clear:both;" />';
419:     return $out;
420: }
421: 
422: /**
423:  * Returns a regular expression to match authorised directories.
424:  * @return a regular expression to match authorised directories.
425:  */
426: function F_getAuthorizedDirs()
427: {
428:     require_once('../config/tce_config.php');
429:     require_once('../../shared/code/tce_functions_authorization.php');
430:     if ($_SESSION['session_user_level'] >= K_AUTH_ADMINISTRATOR) {
431:         return '[^/]*';
432:     }
433:     $reg = F_getAuthorizedUsers($_SESSION['session_user_id']);
434:     return str_replace(',', '|', $reg);
435: }
436: 
437: /**
438:  * Returns true if the user is authorized to use the specified directory, false otherwise.
439:  * @param $dir (string) the directory to check.
440:  * @param $rootdir (string) the user root dir.
441:  * @param $authdirs (string) regular expression containing the authorized dirs.
442:  * @return true if the user is authorized to use the specified directory, false otherwise.
443:  */
444: function F_isAuthorizedDir($dir, $rootdir, $authdirs = '')
445: {
446:     require_once('../config/tce_config.php');
447:     if ($_SESSION['session_user_level'] >= K_AUTH_ADMINISTRATOR) {
448:         return true;
449:     }
450:     if (empty($authdirs)) {
451:         $authdirs = F_getAuthorizedDirs();
452:     }
453:     if (preg_match('#^'.$rootdir.'('.$authdirs.')/#', $dir) > 0) {
454:         return true;
455:     }
456:     return false;
457: }
458: 
459: //============================================================+
460: // END OF FILE
461: //============================================================+
462: 
 

© 2004-2018 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy