PageRenderTime 51ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 1ms

/admin800/ajaxfilemanager/inc/function.base.php

http://marocmall.googlecode.com/
PHP | 1225 lines | 797 code | 108 blank | 320 comment | 162 complexity | 50f1fcc560f803de80bfd67697ebfba1 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * function avaialble to the file manager
  4. * @author Logan Cai (cailongqun [at] yahoo [dot] com [dot] cn)
  5. * @link www.phpletter.com
  6. * @since 22/April/2007
  7. *
  8. */
  9. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . "config.php");
  10. /**
  11. * force to ensure existence of stripos
  12. */
  13. if (!function_exists("stripos"))
  14. {
  15. function stripos($str,$needle,$offset=0)
  16. {
  17. return @strpos(strtolower($str),strtolower($needle),$offset);
  18. }
  19. }
  20. /**
  21. * get the current Url but not the query string specified in $excls
  22. *
  23. * @param array $excls specify those unwanted query string
  24. * @return string
  25. */
  26. function getCurrentUrl($excls=array())
  27. {
  28. $output = $_SERVER['PHP_SELF'];
  29. $count = 1;
  30. foreach($_GET as $k=>$v)
  31. {
  32. if(array_search($k, $excls) ===false)
  33. {
  34. $strAppend = "&";
  35. if($count == 1)
  36. {
  37. $strAppend = "?";
  38. $count++;
  39. }
  40. $output .= $strAppend . $k . "=" . $v;
  41. }
  42. }
  43. return htmlspecialchars($output);
  44. }
  45. /**
  46. * print out an array
  47. *
  48. * @param array $array
  49. */
  50. function displayArray($array, $comments="")
  51. {
  52. echo "<pre>";
  53. echo $comments;
  54. print_r($array);
  55. echo $comments;
  56. echo "</pre>";
  57. }
  58. /**
  59. * check if a file extension is permitted
  60. *
  61. * @param string $filePath
  62. * @param array $validExts
  63. * @param array $invalidExts
  64. * @return boolean
  65. */
  66. function isValidExt($filePath, $validExts, $invalidExts=array())
  67. {
  68. $tem = array();
  69. if(sizeof($validExts))
  70. {
  71. foreach($validExts as $k=>$v)
  72. {
  73. $tem[$k] = strtolower(trim($v));
  74. }
  75. }
  76. $validExts = $tem;
  77. $tem = array();
  78. if(sizeof($invalidExts))
  79. {
  80. foreach($invalidExts as $k=>$v)
  81. {
  82. $tem[$k] = strtolower(trim($v));
  83. }
  84. }
  85. $invalidExts = $tem;
  86. if(sizeof($validExts) && sizeof($invalidExts))
  87. {
  88. foreach($validExts as $k=>$ext)
  89. {
  90. if(array_search($ext, $invalidExts) !== false)
  91. {
  92. unset($validExts[$k]);
  93. }
  94. }
  95. }
  96. if(sizeof($validExts))
  97. {
  98. if(array_search(strtolower(getFileExt($filePath)), $validExts) !== false)
  99. {
  100. return true;
  101. }else
  102. {
  103. return false;
  104. }
  105. }elseif(array_search(strtolower(getFileExt($filePath)), $invalidExts) === false)
  106. {
  107. return true;
  108. }else
  109. {
  110. return false;
  111. }
  112. }
  113. /**
  114. * transform file relative path to absolute path
  115. * @param string $value the path to the file
  116. * @return string
  117. */
  118. function relToAbs($value)
  119. {
  120. return backslashToSlash(preg_replace("/(\\\\)/","\\", getRealPath($value)));
  121. }
  122. function getRelativeFileUrl($value, $relativeTo)
  123. {
  124. $output = '';
  125. $wwwroot = removeTrailingSlash(backslashToSlash(getRootPath()));
  126. $urlprefix = "";
  127. $urlsuffix = "";
  128. $value = backslashToSlash(getRealPath($value));
  129. $pos = strpos($value, $wwwroot);
  130. if ($pos !== false && $pos == 0)
  131. {
  132. $output = $urlprefix . substr($value, strlen($wwwroot)) . $urlsuffix;
  133. }
  134. }
  135. /**
  136. * replace slash with backslash
  137. *
  138. * @param string $value the path to the file
  139. * @return string
  140. */
  141. function slashToBackslash($value) {
  142. return str_replace("/", DIRECTORY_SEPARATOR, $value);
  143. }
  144. /**
  145. * replace backslash with slash
  146. *
  147. * @param string $value the path to the file
  148. * @return string
  149. */
  150. function backslashToSlash($value) {
  151. return str_replace(DIRECTORY_SEPARATOR, "/", $value);
  152. }
  153. /**
  154. * removes the trailing slash
  155. *
  156. * @param string $value
  157. * @return string
  158. */
  159. function removeTrailingSlash($value) {
  160. if(preg_match('@^.+/$@i', $value))
  161. {
  162. $value = substr($value, 0, strlen($value)-1);
  163. }
  164. return $value;
  165. }
  166. /**
  167. * append a trailing slash
  168. *
  169. * @param string $value
  170. * @return string
  171. */
  172. function addTrailingSlash($value)
  173. {
  174. if(preg_match('@^.*[^/]{1}$@i', $value))
  175. {
  176. $value .= '/';
  177. }
  178. return $value;
  179. }
  180. /**
  181. * transform a file path to user friendly
  182. *
  183. * @param string $value
  184. * @return string
  185. */
  186. function transformFilePath($value) {
  187. $rootPath = addTrailingSlash(backslashToSlash(getRealPath(CONFIG_SYS_ROOT_PATH)));
  188. $value = addTrailingSlash(backslashToSlash(getRealPath($value)));
  189. if(!empty($rootPath) && ($i = strpos($value, $rootPath)) !== false)
  190. {
  191. $value = ($i == 0?substr($value, strlen($rootPath)):"/");
  192. }
  193. $value = prependSlash($value);
  194. return $value;
  195. }
  196. /**
  197. * prepend slash
  198. *
  199. * @param string $value
  200. * @return string
  201. */
  202. function prependSlash($value)
  203. {
  204. if (($value && $value[0] != '/') || !$value )
  205. {
  206. $value = "/" . $value;
  207. }
  208. return $value;
  209. }
  210. function writeInfo($data, $die = false)
  211. {
  212. $fp = @fopen(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'data.php', 'w+');
  213. @fwrite($fp, $data);
  214. @fwrite($fp, "\n\n" . date('d/M/Y H:i:s') );
  215. @fclose($fp);
  216. if($die)
  217. {
  218. die();
  219. }
  220. }
  221. /**
  222. * no cachable header
  223. */
  224. function addNoCacheHeaders() {
  225. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  226. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  227. header("Cache-Control: no-store, no-cache, must-revalidate");
  228. header("Cache-Control: post-check=0, pre-check=0", false);
  229. header("Pragma: no-cache");
  230. }
  231. /**
  232. * add extra query stiring to a url
  233. * @param string $baseUrl
  234. * @param string $extra the query string added to the base url
  235. */
  236. function appendQueryString($baseUrl, $extra)
  237. {
  238. $output = $baseUrl;
  239. if(!empty($extra))
  240. {
  241. if(strpos($baseUrl, "?") !== false)
  242. {
  243. $output .= "&" . $extra;
  244. }else
  245. {
  246. $output .= "?" . $extra;
  247. }
  248. }
  249. return $output;
  250. }
  251. /**
  252. * make the query strin from $_GET, but excluding those specified by $excluded
  253. *
  254. * @param array $excluded
  255. * @return string
  256. */
  257. function makeQueryString($excluded=array())
  258. {
  259. $output = '';
  260. $count = 1;
  261. foreach($_GET as $k=>$v)
  262. {
  263. if(array_search($k, $excluded) === false)
  264. {
  265. $output .= ($count>1?'&':'') . ($k . "=" . $v);
  266. $count++;
  267. }
  268. }
  269. return $output;
  270. }
  271. /**
  272. * get parent path from specific path
  273. *
  274. * @param string $value
  275. * @return string
  276. */
  277. function getParentPath($value)
  278. {
  279. $value = removeTrailingSlash(backslashToSlash($value));
  280. if(false !== ($index = strrpos($value, "/")) )
  281. {
  282. return substr($value, 0, $index);
  283. }
  284. }
  285. /**
  286. * check if the file/folder is sit under the root
  287. *
  288. * @param string $value
  289. * @return boolean
  290. */
  291. function isUnderRoot($value)
  292. {
  293. $roorPath = strtolower(addTrailingSlash(backslashToSlash(getRealPath(CONFIG_SYS_ROOT_PATH))));
  294. if(file_exists($value) && @strpos(strtolower(addTrailingSlash(backslashToSlash(getRealPath($value)))), $roorPath) === 0 )
  295. {
  296. return true;
  297. }
  298. return false;
  299. }
  300. /**
  301. * check if a file under the session folder
  302. *
  303. * @param string $value
  304. * @return boolean
  305. */
  306. function isUnderSession($value)
  307. {
  308. global $session;
  309. $sessionPath = strtolower(addTrailingSlash(backslashToSlash(getRealPath($session->getSessionDir()))));
  310. if(file_exists($value) && @strpos(strtolower(addTrailingSlash(backslashToSlash(getRealPath($value)))), $sessionPath) === 0 )
  311. {
  312. return true;
  313. }
  314. return false;
  315. }
  316. /**
  317. * get thumbnail width and height
  318. *
  319. * @param integer $originaleImageWidth
  320. * @param integer $originalImageHeight
  321. * @param integer $thumbnailWidth
  322. * @param integer $thumbnailHeight
  323. * @return array()
  324. */
  325. function getThumbWidthHeight( $originaleImageWidth, $originalImageHeight, $thumbnailWidth, $thumbnailHeight)
  326. {
  327. $outputs = array( "width"=>0, "height"=>0);
  328. $thumbnailWidth = (int)($thumbnailWidth);
  329. $thumbnailHeight = (int)($thumbnailHeight);
  330. if(!empty($originaleImageWidth) && !empty($originalImageHeight))
  331. {
  332. //start to get the thumbnail width & height
  333. if(($thumbnailWidth < 1 && $thumbnailHeight < 1) || ($thumbnailWidth > $originaleImageWidth && $thumbnailHeight > $originalImageHeight ))
  334. {
  335. $thumbnailWidth =$originaleImageWidth;
  336. $thumbnailHeight = $originalImageHeight;
  337. }elseif($thumbnailWidth < 1)
  338. {
  339. $thumbnailWidth = floor($thumbnailHeight / $originalImageHeight * $originaleImageWidth);
  340. }elseif($thumbnailHeight < 1)
  341. {
  342. $thumbnailHeight = floor($thumbnailWidth / $originaleImageWidth * $originalImageHeight);
  343. }else
  344. {
  345. $scale = min($thumbnailWidth/$originaleImageWidth, $thumbnailHeight/$originalImageHeight);
  346. $thumbnailWidth = floor($scale*$originaleImageWidth);
  347. $thumbnailHeight = floor($scale*$originalImageHeight);
  348. }
  349. $outputs['width'] = $thumbnailWidth;
  350. $outputs['height'] = $thumbnailHeight;
  351. }
  352. return $outputs;
  353. }
  354. /**
  355. * turn to absolute path from relative path
  356. *
  357. * @param string $value
  358. * @return string
  359. */
  360. function getAbsPath($value) {
  361. if (substr($value, 0, 1) == "/")
  362. return slashToBackslash(DIR_AJAX_ROOT . $value);
  363. return slashToBackslash(dirname(__FILE__) . "/" . $value);
  364. }
  365. /**
  366. * get file/folder base name
  367. *
  368. * @param string $value
  369. * @return string
  370. */
  371. function getBaseName($value)
  372. {
  373. $value = removeTrailingSlash(backslashToSlash($value));
  374. if(false !== ($index = strrpos($value, "/")) )
  375. {
  376. return substr($value, $index + 1);
  377. }else
  378. {
  379. return $value;
  380. }
  381. }
  382. function myRealPath($path) {
  383. if(strpos($path, ':/') !== false)
  384. {
  385. return $path;
  386. }
  387. // check if path begins with "/" ie. is absolute
  388. // if it isnt concat with script path
  389. if (strpos($path,"/") !== 0 ) {
  390. $base=dirname($_SERVER['SCRIPT_FILENAME']);
  391. $path=$base."/".$path;
  392. }
  393. // canonicalize
  394. $path=explode('/', $path);
  395. $newpath=array();
  396. for ($i=0; $i<sizeof($path); $i++) {
  397. if ($path[$i]==='' || $path[$i]==='.') continue;
  398. if ($path[$i]==='..') {
  399. array_pop($newpath);
  400. continue;
  401. }
  402. array_push($newpath, $path[$i]);
  403. }
  404. $finalpath="/".implode('/', $newpath);
  405. clearstatcache();
  406. // check then return valid path or filename
  407. if (file_exists($finalpath)) {
  408. return ($finalpath);
  409. }
  410. else return FALSE;
  411. }
  412. /**
  413. * calcuate realpath for a relative path
  414. *
  415. * @param string $value a relative path
  416. * @return string absolute path of the input
  417. */
  418. function getRealPath($value)
  419. {
  420. $output = '';
  421. if(($path = realpath($value)) && $path != $value)
  422. {
  423. $output = $path;
  424. }else
  425. {
  426. $output = myRealPath($value);
  427. }
  428. return $output;
  429. }
  430. /**
  431. * get file url
  432. *
  433. * @param string $value
  434. * @return string
  435. */
  436. function getFileUrl($value)
  437. {
  438. $output = '';
  439. $wwwroot = removeTrailingSlash(backslashToSlash(getRootPath()));
  440. $urlprefix = "";
  441. $urlsuffix = "";
  442. $value = backslashToSlash(getRealPath($value));
  443. $pos = stripos($value, $wwwroot);
  444. if ($pos !== false && $pos == 0)
  445. {
  446. $output = $urlprefix . substr($value, strlen($wwwroot)) . $urlsuffix;
  447. }else
  448. {
  449. $output = $value;
  450. }
  451. return "http://" . addTrailingSlash(backslashToSlash($_SERVER['HTTP_HOST'])) . removeBeginingSlash(backslashToSlash($output));
  452. }
  453. /**
  454. *
  455. * transfer file size number to human friendly string
  456. * @param integer $size.
  457. * @return String
  458. */
  459. function transformFileSize($size) {
  460. if ($size > 1048576)
  461. {
  462. return round($size / 1048576, 1) . " MB";
  463. }elseif ($size > 1024)
  464. {
  465. return round($size / 1024, 1) . " KB";
  466. }elseif($size == '')
  467. {
  468. return $size;
  469. }else
  470. {
  471. return $size . " b";
  472. }
  473. }
  474. /**
  475. * remove beginging slash
  476. *
  477. * @param string $value
  478. * @return string
  479. */
  480. function removeBeginingSlash($value)
  481. {
  482. $value = backslashToSlash($value);
  483. if(strpos($value, "/") === 0)
  484. {
  485. $value = substr($value, 1);
  486. }
  487. return $value;
  488. }
  489. /**
  490. * get site root path
  491. *
  492. * @return String.
  493. */
  494. function getRootPath() {
  495. $output = "";
  496. if (defined('CONFIG_WEBSITE_DOCUMENT_ROOT') && CONFIG_WEBSITE_DOCUMENT_ROOT)
  497. {
  498. return slashToBackslash(CONFIG_WEBSITE_DOCUMENT_ROOT);
  499. }
  500. if(isset($_SERVER['DOCUMENT_ROOT']) && ($output = relToAbs($_SERVER['DOCUMENT_ROOT'])) != '' )
  501. {
  502. return $output;
  503. }elseif(isset($_SERVER["SCRIPT_NAME"]) && isset($_SERVER["SCRIPT_FILENAME"]) && ($output = str_replace(backslashToSlash($_SERVER["SCRIPT_NAME"]), "", backslashToSlash($_SERVER["SCRIPT_FILENAME"]))) && is_dir($output))
  504. {
  505. return slashToBackslash($output);
  506. }elseif(isset($_SERVER["SCRIPT_NAME"]) && isset($_SERVER["PATH_TRANSLATED"]) && ($output = str_replace(backslashToSlash($_SERVER["SCRIPT_NAME"]), "", str_replace("//", "/", backslashToSlash($_SERVER["PATH_TRANSLATED"])))) && is_dir($output))
  507. {
  508. return $output;
  509. }else
  510. {
  511. return '';
  512. }
  513. return null;
  514. }
  515. /**
  516. * add beginging slash
  517. *
  518. * @param string $value
  519. * @return string
  520. */
  521. function addBeginingSlash($value)
  522. {
  523. if(strpos($value, "/") !== 0 && !empty($value))
  524. {
  525. $value .= "/" . $value;
  526. }
  527. return $value;
  528. }
  529. /**
  530. * get a file extension
  531. *
  532. * @param string $fileName the path to a file or just the file name
  533. */
  534. function getFileExt($filePath)
  535. {
  536. return @substr(@strrchr($filePath, "."), 1);
  537. }
  538. /**
  539. * reuturn the relative path between two url
  540. *
  541. * @param string $start_dir
  542. * @param string $final_dir
  543. * @return string
  544. */
  545. function getRelativePath($start_dir, $final_dir){
  546. //
  547. $firstPathParts = explode(DIRECTORY_SEPARATOR, $start_dir);
  548. $secondPathParts = explode(DIRECTORY_SEPARATOR, $final_dir);
  549. //
  550. $sameCounter = 0;
  551. for($i = 0; $i < min( count($firstPathParts), count($secondPathParts) ); $i++) {
  552. if( strtolower($firstPathParts[$i]) !== strtolower($secondPathParts[$i]) ) {
  553. break;
  554. }
  555. $sameCounter++;
  556. }
  557. if( $sameCounter == 0 ) {
  558. return $final_dir;
  559. }
  560. //
  561. $newPath = '';
  562. for($i = $sameCounter; $i < count($firstPathParts); $i++) {
  563. if( $i > $sameCounter ) {
  564. $newPath .= DIRECTORY_SEPARATOR;
  565. }
  566. $newPath .= "..";
  567. }
  568. if( count($newPath) == 0 ) {
  569. $newPath = ".";
  570. }
  571. for($i = $sameCounter; $i < count($secondPathParts); $i++) {
  572. $newPath .= DIRECTORY_SEPARATOR;
  573. $newPath .= $secondPathParts[$i];
  574. }
  575. //
  576. return $newPath;
  577. }
  578. /**
  579. * get the php server memory limit
  580. * @return integer
  581. *
  582. */
  583. function getMemoryLimit()
  584. {
  585. $output = @ini_get('memory_limit') or $output = -1 ;
  586. if((int)($output) < 0)
  587. {//unlimited
  588. $output = 999999999999999999;
  589. }
  590. elseif(strpos('g', strtolower($output)) !== false)
  591. {
  592. $output = (int)($output) * 1024 * 1024 * 1024;
  593. }elseif(strpos('k', strtolower($output)) !== false)
  594. {
  595. $output = (int)($output) * 1024 ;
  596. }else
  597. {
  598. $output = (int)($output) * 1024 * 1024;
  599. }
  600. return $output;
  601. }
  602. /**
  603. * get file content
  604. *
  605. * @param string $path
  606. */
  607. function getFileContent($path)
  608. {
  609. return @file_get_contents($path);
  610. //return str_replace(array("\r", "\n", '"', "\t"), array("", "\\n", '\"', "\\t"), @file_get_contents($path));
  611. }
  612. /**
  613. * get the list of folder under a specified folder
  614. * which will be used for drop-down menu
  615. * @param string $path the path of the specified folder
  616. * @param array $outputs
  617. * @param string $indexNumber
  618. * @param string $prefixNumber the prefix before the index number
  619. * @param string $prefixName the prefix before the folder name
  620. * @return array
  621. */
  622. function getFolderListing($path,$indexNumber=null, $prefixNumber =' ', $prefixName =' - ', $outputs=array())
  623. {
  624. $path = removeTrailingSlash(backslashToSlash($path));
  625. if(is_null($indexNumber))
  626. {
  627. $outputs[IMG_LBL_ROOT_FOLDER] = removeTrailingSlash(backslashToSlash($path));
  628. }
  629. $fh = @opendir($path);
  630. if($fh)
  631. {
  632. $count = 1;
  633. while($file = @readdir($fh))
  634. {
  635. $newPath = removeTrailingSlash(backslashToSlash($path . "/" . $file));
  636. if(isListingDocument($newPath) && $file != '.' && $file != '..' && is_dir($newPath))
  637. {
  638. if(!empty($indexNumber))
  639. {//this is not root folder
  640. $outputs[$prefixNumber . $indexNumber . "." . $count . $prefixName . $file] = $newPath;
  641. getFolderListing($newPath, $prefixNumber . $indexNumber . "." . $count , $prefixNumber, $prefixName, $outputs);
  642. }else
  643. {//this is root folder
  644. $outputs[$count . $prefixName . $file] = $newPath;
  645. getFolderListing($newPath, $count, $prefixNumber, $prefixName, $outputs);
  646. }
  647. $count++;
  648. }
  649. }
  650. @closedir($fh);
  651. }
  652. return $outputs;
  653. }
  654. /**
  655. * get the valid text editor extension
  656. * which is calcualte from the CONFIG_EDITABALE_VALID_EXTS
  657. * exclude those specified in CONFIG_UPLOAD_INVALID_EXTS
  658. * and those are not specified in CONFIG_UPLOAD_VALID_EXTS
  659. *
  660. * @return array
  661. */
  662. function getValidTextEditorExts()
  663. {
  664. $validEditorExts = explode(',', CONFIG_EDITABLE_VALID_EXTS);
  665. if(CONFIG_UPLOAD_VALID_EXTS)
  666. {//exclude those exts not shown on CONFIG_UPLOAD_VALID_EXTS
  667. $validUploadExts = explode(',', CONFIG_UPLOAD_VALID_EXTS);
  668. foreach($validEditorExts as $k=>$v)
  669. {
  670. if(array_search($v, $validUploadExts) === false)
  671. {
  672. unset($validEditorExts[$k]);
  673. }
  674. }
  675. }
  676. if(CONFIG_UPLOAD_INVALID_EXTS)
  677. {//exlcude those exists in CONFIG_UPLOAD_INVALID_EXTS
  678. $invalidUploadExts = explode(',', CONFIG_UPLOAD_INVALID_EXTS);
  679. foreach($validEditorExts as $k=>$v)
  680. {
  681. if(array_search($v, $invalidUploadExts) !== false)
  682. {
  683. unset($validEditorExts[$k]);
  684. }
  685. }
  686. }
  687. return $validEditorExts;
  688. }
  689. /**
  690. * check if file name or folder name is valid against a regular expression
  691. *
  692. * @param string $pattern regular expression, separated by , if multiple
  693. * @param string $string
  694. * @return booolean
  695. */
  696. function isValidPattern( $pattern, $string)
  697. {
  698. if(($pattern)=== '')
  699. {
  700. return true;
  701. }
  702. else if (strpos($pattern,",")!==false)
  703. {
  704. $regExps = explode(',', $pattern);
  705. foreach ($regExps as $regExp => $value)
  706. {
  707. if(eregi($value, $string))
  708. {
  709. return true;
  710. }
  711. }
  712. }
  713. else if(eregi($pattern, $string))
  714. {
  715. return true;
  716. }
  717. return false;
  718. }
  719. /**
  720. * check if file name or folder name is invalid against a regular expression
  721. *
  722. * @param string $pattern regular expression, separated by , if multiple
  723. * @param string $string
  724. * @return booolean
  725. */
  726. function isInvalidPattern( $pattern, $string)
  727. {
  728. if(($pattern)=== '')
  729. {
  730. return false;
  731. }
  732. else if (strpos($pattern,",")!==false)
  733. {
  734. $regExps = explode(',', $pattern);
  735. foreach ($regExps as $regExp => $value)
  736. {
  737. if(eregi($value, $string))
  738. {
  739. return true;
  740. }
  741. }
  742. }
  743. else if(eregi($pattern, $string))
  744. {
  745. return true;
  746. }
  747. return false;
  748. }
  749. /**
  750. * cut the file down to fit the list page
  751. *
  752. * @param string $fileName
  753. */
  754. function shortenFileName($fileName, $maxLeng=17, $indicate = '...')
  755. {
  756. if(strlen($fileName) > $maxLeng)
  757. {
  758. $fileName = substr($fileName, 0, $maxLeng - strlen($indicate)) . $indicate;
  759. }
  760. return $fileName;
  761. }
  762. if (!function_exists('mime_content_type'))
  763. {
  764. function mime_content_type ( $f )
  765. {
  766. return trim ( @exec ('file -bi ' . escapeshellarg ( $f ) ) ) ;
  767. }
  768. }
  769. /**
  770. * check if such document is allowed to shown on the list
  771. *
  772. * @param string $path the path to the document
  773. * @return boolean
  774. */
  775. function isListingDocument($path)
  776. {
  777. $file = basename($path);
  778. if(CONFIG_SYS_PATTERN_FORMAT == 'list')
  779. {// comma delimited vague file/folder name
  780. if(is_dir($path))
  781. {
  782. $includeDir = trimlrm(CONFIG_SYS_INC_DIR_PATTERN);
  783. $excludeDir = trimlrm(CONFIG_SYS_EXC_DIR_PATTERN);
  784. $found_includeDir = strpos($includeDir, $file);
  785. $found_excludeDir = strpos($excludeDir, $file);
  786. if((!CONFIG_SYS_INC_DIR_PATTERN || (!($found_includeDir === FALSE))) && (!CONFIG_SYS_EXC_DIR_PATTERN || (($found_excludeDir === FALSE))))
  787. {
  788. return true;
  789. }else
  790. {
  791. return false;
  792. }
  793. }elseif(is_file($path))
  794. {
  795. $includeFile = trimlrm(CONFIG_SYS_INC_FILE_PATTERN);
  796. $excludeFile = trimlrm(CONFIG_SYS_EXC_FILE_PATTERN);
  797. $found_includeFile = strpos($includeFile, $file);
  798. $found_excludeFile = strpos($excludeFile, $file);
  799. if((!CONFIG_SYS_INC_FILE_PATTERN || (!($found_includeFile === FALSE))) && (!CONFIG_SYS_EXC_FILE_PATTERN || (($found_excludeFile === FALSE))))
  800. {
  801. return true;
  802. }else
  803. {
  804. return false;
  805. }
  806. }
  807. }elseif(CONFIG_SYS_PATTERN_FORMAT == 'csv')
  808. {//comma delimited file/folder name
  809. if(is_dir($path))
  810. {
  811. $includeDir = trimlrm(CONFIG_SYS_INC_DIR_PATTERN);
  812. $excludeDir = trimlrm(CONFIG_SYS_EXC_DIR_PATTERN);
  813. if(!empty($includeDir) && !empty($excludeDir))
  814. {
  815. $validDir = explode(',', $includeDir);
  816. $invalidDir = explode(",", $excludeDir);
  817. if(array_search(basename($path), $validDir) !== false && array_search(basename($path), $invalidDir) === false)
  818. {
  819. return true;
  820. }else
  821. {
  822. return false;
  823. }
  824. }elseif(!empty($includeDir))
  825. {
  826. $validDir = explode(',', $includeDir);
  827. if(array_search(basename($path), $validDir) !== false)
  828. {
  829. return true;
  830. }else
  831. {
  832. return false;
  833. }
  834. }elseif(!empty($excludeFile))
  835. {
  836. $invalidDir = explode(",", $excludeDir);
  837. if(array_search(basename($path), $invalidDir) === false)
  838. {
  839. return true;
  840. }else
  841. {
  842. return false;
  843. }
  844. }
  845. return true;
  846. }elseif(is_file($path))
  847. {
  848. $includeFile = trimlrm(CONFIG_SYS_INC_FILE_PATTERN);
  849. $excludeFile = trimlrm(CONFIG_SYS_EXC_FILE_PATTERN);
  850. if(!empty($includeFile) && !empty($excludeFile))
  851. {
  852. $validFile = explode(',', $includeFile);
  853. $invalidFile = explode(',', $excludeFile);
  854. if(array_search(basename($path), $validFile) !== false && array_search(basename($path), $invalidFile) === false)
  855. {
  856. return true;
  857. }else
  858. {
  859. return false;
  860. }
  861. }elseif(!empty($includeFile))
  862. {
  863. $validFile = explode(',', $includeFile);
  864. if(array_search(basename($path), $validFile) !== false)
  865. {
  866. return true;
  867. }else
  868. {
  869. return false;
  870. }
  871. }elseif(!empty($excludeFile))
  872. {
  873. $invalidFile = explode(',', $excludeFile);
  874. if(array_search(basename($path), $invalidFile) === false)
  875. {
  876. return true;
  877. }else
  878. {
  879. return false;
  880. }
  881. }
  882. return true;
  883. }
  884. }
  885. else
  886. {//regular expression
  887. if(is_dir($path) )
  888. {
  889. if(isValidPattern(CONFIG_SYS_INC_DIR_PATTERN, $path) && !isInvalidPattern(CONFIG_SYS_EXC_DIR_PATTERN, $path))
  890. {
  891. return true;
  892. }else
  893. {
  894. return false;
  895. }
  896. }elseif(is_file($path))
  897. {
  898. if(isValidPattern(CONFIG_SYS_INC_FILE_PATTERN, $path) && !isInvalidPattern(CONFIG_SYS_EXC_FILE_PATTERN, $path) )
  899. {
  900. return true;
  901. }else
  902. {
  903. return false;
  904. }
  905. }
  906. }
  907. return false;
  908. }
  909. /**
  910. * force to down the specified file
  911. *
  912. * @param string $path
  913. *
  914. */
  915. function downloadFile($path, $newFileName=null)
  916. {
  917. if(file_exists($path) && is_file($path))
  918. {
  919. $mimeContentType = 'application/octet-stream';
  920. if(function_exists('finfo_open'))
  921. {
  922. if(($fp = @finfo_open($path)))
  923. {
  924. $mimeContentType = @finfo_file($fp, basename($path));
  925. @finfo_close($fp);
  926. }
  927. }elseif(($temMimeContentType = @mime_content_type($path)) && !empty($temMimeContentType))
  928. {
  929. $mimeContentType = $temMimeContentType;
  930. }
  931. // START ANDR??? SILVA DOWNLOAD CODE
  932. // required for IE, otherwise Content-disposition is ignored
  933. if(ini_get('zlib.output_compression'))
  934. ini_set('zlib.output_compression', 'Off');
  935. header("Pragma: public"); // required
  936. header("Expires: 0");
  937. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  938. header("Cache-Control: private",false); // required for certain browsers
  939. header("Content-Type: " . $mimeContentType );
  940. // change, added quotes to allow spaces in filenames, by Rajkumar Singh
  941. header("Content-Disposition: attachment; filename=\"".(is_null($newFileName)?basename($path):$newFileName)."\";" );
  942. header("Content-Transfer-Encoding: binary");
  943. header("Content-Length: ".filesize($path));
  944. readfile($path);
  945. exit();
  946. // END ANDR??? SILVA DOWNLOAD CODE
  947. }
  948. }
  949. /**
  950. * remove all white spaces
  951. *
  952. * @param string $hayStack
  953. * @param string $whiteSpaceChars
  954. * @return string
  955. */
  956. function trimlrm ($hayStack, $whiteSpaceChars="\t\n\r\0\x0B")
  957. {
  958. return str_replace($whiteSpaceChars, '', trim($hayStack));
  959. }
  960. /**
  961. * get the parent path of the specified path
  962. *
  963. * @param string $path
  964. * @return string
  965. */
  966. function getParentFolderPath($path)
  967. {
  968. $realPath = addTrailingSlash(backslashToSlash(getRealPath($path)));
  969. $parentRealPath = addTrailingSlash(backslashToSlash(dirname($realPath)));
  970. $differentPath = addTrailingSlash(substr($realPath, strlen($parentRealPath)));
  971. $parentPath = substr($path, 0, strlen(addTrailingSlash(backslashToSlash($path))) - strlen($differentPath));
  972. if(isUnderRoot($parentPath))
  973. {
  974. return $parentPath;
  975. }else
  976. {
  977. return CONFIG_SYS_DEFAULT_PATH;
  978. }
  979. }
  980. function getCurrentFolderPath()
  981. {
  982. $folderPathIndex = 'path';
  983. $lastVisitedFolderPathIndex = 'ajax_last_visited_folder';
  984. if(isset($_GET[$folderPathIndex]) && file_exists($_GET[$folderPathIndex]) && !is_file($_GET[$folderPathIndex]) )
  985. {
  986. $currentFolderPath = $_GET[$folderPathIndex];
  987. }
  988. elseif(isset($_SESSION[$lastVisitedFolderPathIndex]) && file_exists($_SESSION[$lastVisitedFolderPathIndex]) && !is_file($_SESSION[$lastVisitedFolderPathIndex]))
  989. {
  990. $currentFolderPath = $_SESSION[$lastVisitedFolderPathIndex];
  991. }else
  992. {
  993. $currentFolderPath = CONFIG_SYS_DEFAULT_PATH;
  994. }
  995. $currentFolderPath = (isUnderRoot($currentFolderPath)?backslashToSlash((addTrailingSlash($currentFolderPath))):CONFIG_SYS_DEFAULT_PATH);
  996. //keep track of this folder path in session
  997. $_SESSION[$lastVisitedFolderPathIndex] = $currentFolderPath;
  998. if(!file_exists($currentFolderPath))
  999. {
  1000. die(ERR_FOLDER_NOT_FOUND . $currentFolderPath);
  1001. }
  1002. }
  1003. if(!function_exists("imagerotate"))
  1004. {
  1005. function imagerotate($src_img, $angle, $bicubic=false)
  1006. {
  1007. // convert degrees to radians
  1008. $angle = (360 - $angle) + 180;
  1009. $angle = deg2rad($angle);
  1010. $src_x = imagesx($src_img);
  1011. $src_y = imagesy($src_img);
  1012. $center_x = floor($src_x/2);
  1013. $center_y = floor($src_y/2);
  1014. $rotate = imagecreatetruecolor($src_x, $src_y);
  1015. imagealphablending($rotate, false);
  1016. imagesavealpha($rotate, true);
  1017. $cosangle = cos($angle);
  1018. $sinangle = sin($angle);
  1019. for ($y = 0; $y < $src_y; $y++) {
  1020. for ($x = 0; $x < $src_x; $x++) {
  1021. // rotate...
  1022. $old_x = (($center_x-$x) * $cosangle + ($center_y-$y) * $sinangle)
  1023. + $center_x;
  1024. $old_y = (($center_y-$y) * $cosangle - ($center_x-$x) * $sinangle)
  1025. + $center_y;
  1026. if ( $old_x >= 0 && $old_x < $src_x
  1027. && $old_y >= 0 && $old_y < $src_y ) {
  1028. if ($bicubic == true) {
  1029. $sY = $old_y + 1;
  1030. $siY = $old_y;
  1031. $siY2 = $old_y - 1;
  1032. $sX = $old_x + 1;
  1033. $siX = $old_x;
  1034. $siX2 = $old_x - 1;
  1035. $c1 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX, $siY2));
  1036. $c2 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX, $siY));
  1037. $c3 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX2, $siY2));
  1038. $c4 = imagecolorsforindex($src_img, imagecolorat($src_img, $siX2, $siY));
  1039. $r = ($c1['red'] + $c2['red'] + $c3['red'] + $c4['red'] ) << 14;
  1040. $g = ($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) << 6;
  1041. $b = ($c1['blue'] + $c2['blue'] + $c3['blue'] + $c4['blue'] ) >> 2;
  1042. $a = ($c1['alpha'] + $c2['alpha'] + $c3['alpha'] + $c4['alpha'] ) >> 2;
  1043. $color = imagecolorallocatealpha($src_img, $r,$g,$b,$a);
  1044. } else {
  1045. $color = imagecolorat($src_img, $old_x, $old_y);
  1046. }
  1047. } else {
  1048. // this line sets the background colour
  1049. $color = imagecolorallocatealpha($src_img, 255, 255, 255, 127);
  1050. }
  1051. imagesetpixel($rotate, $x, $y, $color);
  1052. }
  1053. }
  1054. return $rotate;
  1055. /* $src_x = @imagesx($src_img);
  1056. $src_y = @imagesy($src_img);
  1057. if ($angle == 180)
  1058. {
  1059. $dest_x = $src_x;
  1060. $dest_y = $src_y;
  1061. }
  1062. elseif ($src_x <= $src_y)
  1063. {
  1064. $dest_x = $src_y;
  1065. $dest_y = $src_x;
  1066. }
  1067. elseif ($src_x >= $src_y)
  1068. {
  1069. $dest_x = $src_y;
  1070. $dest_y = $src_x;
  1071. }
  1072. if(function_exists('ImageCreateTrueColor'))
  1073. {
  1074. $rotate = @ImageCreateTrueColor($dst_w,$dst_h);
  1075. } else {
  1076. $rotate = @ImageCreate($dst_w,$dst_h);
  1077. }
  1078. @imagealphablending($rotate, false);
  1079. switch ($angle)
  1080. {
  1081. case 270:
  1082. for ($y = 0; $y < ($src_y); $y++)
  1083. {
  1084. for ($x = 0; $x < ($src_x); $x++)
  1085. {
  1086. $color = imagecolorat($src_img, $x, $y);
  1087. imagesetpixel($rotate, $dest_x - $y - 1, $x, $color);
  1088. }
  1089. }
  1090. break;
  1091. case 90:
  1092. for ($y = 0; $y < ($src_y); $y++)
  1093. {
  1094. for ($x = 0; $x < ($src_x); $x++)
  1095. {
  1096. $color = imagecolorat($src_img, $x, $y);
  1097. imagesetpixel($rotate, $y, $dest_y - $x - 1, $color);
  1098. }
  1099. }
  1100. break;
  1101. case 180:
  1102. for ($y = 0; $y < ($src_y); $y++)
  1103. {
  1104. for ($x = 0; $x < ($src_x); $x++)
  1105. {
  1106. $color = imagecolorat($src_img, $x, $y);
  1107. imagesetpixel($rotate, $dest_x - $x - 1, $dest_y - $y - 1, $color);
  1108. }
  1109. }
  1110. break;
  1111. default: $rotate = $src_img;
  1112. };
  1113. return $rotate;*/
  1114. }
  1115. }
  1116. ?>