PageRenderTime 39ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/phpmyfaq/admin/editor/plugins/ajaxfilemanager/inc/function.base.php

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