PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/storecommander/ead6f6fce09/SC/lib/js/ajaxfilemanager/inc/function.base.php

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