PageRenderTime 29ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/public/responsive_filemanager/filemanager/include/utils.php

https://gitlab.com/devtoannh/cafe
PHP | 1039 lines | 879 code | 38 blank | 122 comment | 60 complexity | 5ee632276f3c122ff063d4a54f3c273e MD5 | raw file
  1. <?php
  2. if ($_SESSION['RF']["verify"] != "RESPONSIVEfilemanager")
  3. {
  4. die('forbiden');
  5. }
  6. require dirname(__FILE__) . '/Response.php';
  7. if ( ! function_exists('response'))
  8. {
  9. /**
  10. * Response construction helper
  11. *
  12. * @param string $content
  13. * @param int $statusCode
  14. * @param array $headers
  15. *
  16. * @return \Response|\Illuminate\Http\Response
  17. */
  18. function response($content = '', $statusCode = 200, $headers = array())
  19. {
  20. $responseClass = class_exists('Illuminate\Http\Response') ? '\Illuminate\Http\Response' : 'Response';
  21. return new $responseClass($content, $statusCode, $headers);
  22. }
  23. }
  24. if ( ! function_exists('trans'))
  25. {
  26. // language
  27. if ( ! isset($_SESSION['RF']['language'])
  28. || file_exists($_SESSION['RF']['language_file']) === false
  29. || ! is_readable($_SESSION['RF']['language_file'])
  30. )
  31. {
  32. $lang = $default_language;
  33. if (isset($_GET['lang']) && $_GET['lang'] != 'undefined' && $_GET['lang'] != '')
  34. {
  35. $lang = fix_get_params($_GET['lang']);
  36. $lang = trim($lang);
  37. }
  38. $language_file = dirname(dirname(__FILE__)) . '/lang/' . $default_language . '.php';
  39. if ($lang != $default_language)
  40. {
  41. $path_parts = pathinfo($lang);
  42. if (is_readable(dirname(dirname(__FILE__)) . '/lang/' . $path_parts['basename'] . '.php'))
  43. {
  44. $language_file = dirname(dirname(__FILE__)) . '/lang/' . $path_parts['basename'] . '.php';
  45. }
  46. else
  47. {
  48. echo "<script>console.log('The " . $lang . " language file is not readable! Falling back...');</script>";
  49. }
  50. }
  51. // add lang file to session for easy include
  52. $_SESSION['RF']['language'] = $lang;
  53. $_SESSION['RF']['language_file'] = $language_file;
  54. }
  55. else
  56. {
  57. $lang = $_SESSION['RF']['language'];
  58. $language_file = $_SESSION['RF']['language_file'];
  59. }
  60. $lang_vars = include $language_file;
  61. if ( ! is_array($lang_vars))
  62. {
  63. $lang_vars = array();
  64. }
  65. /**
  66. * Translate language variable
  67. *
  68. * @param $var string name
  69. *
  70. * @return string translated variable
  71. */
  72. function trans($var)
  73. {
  74. global $lang_vars;
  75. return (array_key_exists($var, $lang_vars)) ? $lang_vars[ $var ] : $var;
  76. }
  77. }
  78. /**
  79. * Delete directory
  80. *
  81. * @param string $dir
  82. *
  83. * @return bool
  84. */
  85. function deleteDir($dir)
  86. {
  87. if ( ! file_exists($dir))
  88. {
  89. return true;
  90. }
  91. if ( ! is_dir($dir))
  92. {
  93. return unlink($dir);
  94. }
  95. foreach (scandir($dir) as $item)
  96. {
  97. if ($item == '.' || $item == '..')
  98. {
  99. continue;
  100. }
  101. if ( ! deleteDir($dir . DIRECTORY_SEPARATOR . $item))
  102. {
  103. return false;
  104. }
  105. }
  106. return rmdir($dir);
  107. }
  108. /**
  109. * Make a file copy
  110. *
  111. * @param string $old_path
  112. * @param string $name New file name without extension
  113. *
  114. * @return bool
  115. */
  116. function duplicate_file($old_path, $name)
  117. {
  118. if (file_exists($old_path))
  119. {
  120. $info = pathinfo($old_path);
  121. $new_path = $info['dirname'] . "/" . $name . "." . $info['extension'];
  122. if (file_exists($new_path) && $old_path == $new_path)
  123. {
  124. return false;
  125. }
  126. return copy($old_path, $new_path);
  127. }
  128. }
  129. /**
  130. * Rename file
  131. *
  132. * @param string $old_path File to rename
  133. * @param string $name New file name without extension
  134. * @param bool $transliteration
  135. *
  136. * @return bool
  137. */
  138. function rename_file($old_path, $name, $transliteration)
  139. {
  140. $name = fix_filename($name, $transliteration);
  141. if (file_exists($old_path))
  142. {
  143. $info = pathinfo($old_path);
  144. $new_path = $info['dirname'] . "/" . $name . "." . $info['extension'];
  145. if (file_exists($new_path) && $old_path == $new_path)
  146. {
  147. return false;
  148. }
  149. return rename($old_path, $new_path);
  150. }
  151. }
  152. /**
  153. * Rename directory
  154. *
  155. * @param string $old_path Directory to rename
  156. * @param string $name New directory name
  157. * @param bool $transliteration
  158. *
  159. * @return bool
  160. */
  161. function rename_folder($old_path, $name, $transliteration)
  162. {
  163. $name = fix_filename($name, $transliteration, false, '_', true);
  164. if (file_exists($old_path))
  165. {
  166. $new_path = fix_dirname($old_path) . "/" . $name;
  167. if (file_exists($new_path) && $old_path == $new_path)
  168. {
  169. return false;
  170. }
  171. return rename($old_path, $new_path);
  172. }
  173. }
  174. /**
  175. * Create new image from existing file
  176. *
  177. * @param string $imgfile Source image file name
  178. * @param string $imgthumb Thumbnail file name
  179. * @param int $newwidth Thumbnail width
  180. * @param int $newheight Optional thumbnail height
  181. * @param string $option Type of resize
  182. *
  183. * @return bool
  184. * @throws \Exception
  185. */
  186. function create_img($imgfile, $imgthumb, $newwidth, $newheight = null, $option = "crop")
  187. {
  188. $timeLimit = ini_get('max_execution_time');
  189. set_time_limit(30);
  190. $result = false;
  191. if (image_check_memory_usage($imgfile, $newwidth, $newheight))
  192. {
  193. require_once('php_image_magician.php');
  194. $magicianObj = new imageLib($imgfile);
  195. $magicianObj->resizeImage($newwidth, $newheight, $option);
  196. $magicianObj->saveImage($imgthumb, 80);
  197. $result = true;
  198. }
  199. set_time_limit($timeLimit);
  200. return $result;
  201. }
  202. /**
  203. * Convert convert size in bytes to human readable
  204. *
  205. * @param int $size
  206. *
  207. * @return string
  208. */
  209. function makeSize($size)
  210. {
  211. $units = array( 'B', 'KB', 'MB', 'GB', 'TB' );
  212. $u = 0;
  213. while ((round($size / 1024) > 0) && ($u < 4))
  214. {
  215. $size = $size / 1024;
  216. $u++;
  217. }
  218. return (number_format($size, 0) . " " . $units[ $u ]);
  219. }
  220. /**
  221. * Determine directory size
  222. *
  223. * @param string $path
  224. *
  225. * @return int
  226. */
  227. function foldersize($path)
  228. {
  229. $total_size = 0;
  230. $files = scandir($path);
  231. $cleanPath = rtrim($path, '/') . '/';
  232. foreach ($files as $t)
  233. {
  234. if ($t != "." && $t != "..")
  235. {
  236. $currentFile = $cleanPath . $t;
  237. if (is_dir($currentFile))
  238. {
  239. $size = foldersize($currentFile);
  240. $total_size += $size;
  241. }
  242. else
  243. {
  244. $size = filesize($currentFile);
  245. $total_size += $size;
  246. }
  247. }
  248. }
  249. return $total_size;
  250. }
  251. /**
  252. * Get number of files in a directory
  253. *
  254. * @param string $path
  255. *
  256. * @return int
  257. */
  258. function filescount($path)
  259. {
  260. $total_count = 0;
  261. $files = scandir($path);
  262. $cleanPath = rtrim($path, '/') . '/';
  263. foreach ($files as $t)
  264. {
  265. if ($t != "." && $t != "..")
  266. {
  267. $currentFile = $cleanPath . $t;
  268. if (is_dir($currentFile))
  269. {
  270. $size = filescount($currentFile);
  271. $total_count += $size;
  272. }
  273. else
  274. {
  275. $total_count += 1;
  276. }
  277. }
  278. }
  279. return $total_count;
  280. }
  281. /**
  282. * Create directory for images and/or thumbnails
  283. *
  284. * @param string $path
  285. * @param string $path_thumbs
  286. */
  287. function create_folder($path = null, $path_thumbs = null)
  288. {
  289. $oldumask = umask(0);
  290. if ($path && ! file_exists($path))
  291. {
  292. mkdir($path, 0755, true);
  293. } // or even 01777 so you get the sticky bit set
  294. if ($path_thumbs && ! file_exists($path_thumbs))
  295. {
  296. mkdir($path_thumbs, 0755, true) or die("$path_thumbs cannot be found");
  297. } // or even 01777 so you get the sticky bit set
  298. umask($oldumask);
  299. }
  300. /**
  301. * Get file extension present in directory
  302. *
  303. * @param string $path
  304. * @param string $ext
  305. */
  306. function check_files_extensions_on_path($path, $ext)
  307. {
  308. if ( ! is_dir($path))
  309. {
  310. $fileinfo = pathinfo($path);
  311. if ( ! in_array(mb_strtolower($fileinfo['extension']), $ext))
  312. {
  313. unlink($path);
  314. }
  315. }
  316. else
  317. {
  318. $files = scandir($path);
  319. foreach ($files as $file)
  320. {
  321. check_files_extensions_on_path(trim($path, '/') . "/" . $file, $ext);
  322. }
  323. }
  324. }
  325. /**
  326. * Get file extension present in PHAR file
  327. *
  328. * @param string $phar
  329. * @param array $files
  330. * @param string $basepath
  331. * @param string $ext
  332. */
  333. function check_files_extensions_on_phar($phar, &$files, $basepath, $ext)
  334. {
  335. foreach ($phar as $file)
  336. {
  337. if ($file->isFile())
  338. {
  339. if (in_array(mb_strtolower($file->getExtension()), $ext))
  340. {
  341. $files[] = $basepath . $file->getFileName();
  342. }
  343. }
  344. else
  345. {
  346. if ($file->isDir())
  347. {
  348. $iterator = new DirectoryIterator($file);
  349. check_files_extensions_on_phar($iterator, $files, $basepath . $file->getFileName() . '/', $ext);
  350. }
  351. }
  352. }
  353. }
  354. /**
  355. * Cleanup input
  356. *
  357. * @param string $str
  358. *
  359. * @return string
  360. */
  361. function fix_get_params($str)
  362. {
  363. return strip_tags(preg_replace("/[^a-zA-Z0-9\.\[\]_| -]/", '', $str));
  364. }
  365. /**
  366. * Cleanup filename
  367. *
  368. * @param string $str
  369. * @param bool $transliteration
  370. * @param bool $convert_spaces
  371. * @param string $replace_with
  372. * @param bool $is_folder
  373. *
  374. * @return string
  375. */
  376. function fix_filename($str, $transliteration, $convert_spaces = false, $replace_with = "_", $is_folder = false)
  377. {
  378. if ($convert_spaces)
  379. {
  380. $str = str_replace(' ', $replace_with, $str);
  381. }
  382. if ($transliteration)
  383. {
  384. if (function_exists('transliterator_transliterate'))
  385. {
  386. $str = transliterator_transliterate('Accents-Any', utf8_encode($str));
  387. }
  388. else
  389. {
  390. $str = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $str);
  391. }
  392. $str = preg_replace("/[^a-zA-Z0-9\.\[\]_| -]/", '', $str);
  393. }
  394. $str = str_replace(array( '"', "'", "/", "\\" ), "", $str);
  395. $str = strip_tags($str);
  396. // Empty or incorrectly transliterated filename.
  397. // Here is a point: a good file UNKNOWN_LANGUAGE.jpg could become .jpg in previous code.
  398. // So we add that default 'file' name to fix that issue.
  399. if (strpos($str, '.') === 0 && $is_folder === false)
  400. {
  401. $str = 'file' . $str;
  402. }
  403. return trim($str);
  404. }
  405. /**
  406. * Cleanup directory name
  407. *
  408. * @param string $str
  409. *
  410. * @return string
  411. */
  412. function fix_dirname($str)
  413. {
  414. return str_replace('~', ' ', dirname(str_replace(' ', '~', $str)));
  415. }
  416. /**
  417. * Correct strtoupper handling
  418. *
  419. * @param string $str
  420. *
  421. * @return string
  422. */
  423. function fix_strtoupper($str)
  424. {
  425. if (function_exists('mb_strtoupper'))
  426. {
  427. return mb_strtoupper($str);
  428. }
  429. else
  430. {
  431. return strtoupper($str);
  432. }
  433. }
  434. /**
  435. * Correct strtolower handling
  436. *
  437. * @param string $str
  438. *
  439. * @return string
  440. */
  441. function fix_strtolower($str)
  442. {
  443. if (function_exists('mb_strtoupper'))
  444. {
  445. return mb_strtolower($str);
  446. }
  447. else
  448. {
  449. return strtolower($str);
  450. }
  451. }
  452. function fix_path($path, $transliteration, $convert_spaces = false, $replace_with = "_")
  453. {
  454. $info = pathinfo($path);
  455. $tmp_path = $info['dirname'];
  456. $str = fix_filename($info['filename'], $transliteration, $convert_spaces, $replace_with);
  457. if ($tmp_path != "")
  458. {
  459. return $tmp_path . DIRECTORY_SEPARATOR . $str;
  460. }
  461. else
  462. {
  463. return $str;
  464. }
  465. }
  466. /**
  467. * Get current base url
  468. *
  469. * @return string
  470. */
  471. function base_url()
  472. {
  473. return sprintf(
  474. "%s://%s",
  475. isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
  476. $_SERVER['HTTP_HOST']
  477. );
  478. }
  479. /**
  480. * @param $current_path
  481. * @param $fld
  482. *
  483. * @return bool
  484. */
  485. function config_loading($current_path, $fld)
  486. {
  487. if (file_exists($current_path . $fld . ".config"))
  488. {
  489. require_once($current_path . $fld . ".config");
  490. return true;
  491. }
  492. echo "!!!!" . $parent = fix_dirname($fld);
  493. if ($parent != "." && ! empty($parent))
  494. {
  495. config_loading($current_path, $parent);
  496. }
  497. return false;
  498. }
  499. /**
  500. * Check if memory is enough to process image
  501. *
  502. * @param string $img
  503. * @param int $max_breedte
  504. * @param int $max_hoogte
  505. *
  506. * @return bool
  507. */
  508. function image_check_memory_usage($img, $max_breedte, $max_hoogte)
  509. {
  510. if (file_exists($img))
  511. {
  512. $K64 = 65536; // number of bytes in 64K
  513. $memory_usage = memory_get_usage();
  514. $memory_limit = abs(intval(str_replace('M', '', ini_get('memory_limit')) * 1024 * 1024));
  515. $image_properties = getimagesize($img);
  516. $image_width = $image_properties[0];
  517. $image_height = $image_properties[1];
  518. $image_bits = $image_properties['bits'];
  519. $image_memory_usage = $K64 + ($image_width * $image_height * ($image_bits) * 2);
  520. $thumb_memory_usage = $K64 + ($max_breedte * $max_hoogte * ($image_bits) * 2);
  521. $memory_needed = intval($memory_usage + $image_memory_usage + $thumb_memory_usage);
  522. if ($memory_needed > $memory_limit)
  523. {
  524. ini_set('memory_limit', (intval($memory_needed / 1024 / 1024) + 5) . 'M');
  525. if (ini_get('memory_limit') == (intval($memory_needed / 1024 / 1024) + 5) . 'M')
  526. {
  527. return true;
  528. }
  529. else
  530. {
  531. return false;
  532. }
  533. }
  534. else
  535. {
  536. return true;
  537. }
  538. }
  539. else
  540. {
  541. return false;
  542. }
  543. }
  544. /**
  545. * Check is string is ended with needle
  546. *
  547. * @param string $haystack
  548. * @param string $needle
  549. *
  550. * @return bool
  551. */
  552. function endsWith($haystack, $needle)
  553. {
  554. return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
  555. }
  556. /**
  557. * TODO REFACTOR THIS!
  558. *
  559. * @param $targetPath
  560. * @param $targetFile
  561. * @param $name
  562. * @param $current_path
  563. * @param $relative_image_creation
  564. * @param $relative_path_from_current_pos
  565. * @param $relative_image_creation_name_to_prepend
  566. * @param $relative_image_creation_name_to_append
  567. * @param $relative_image_creation_width
  568. * @param $relative_image_creation_height
  569. * @param $relative_image_creation_option
  570. * @param $fixed_image_creation
  571. * @param $fixed_path_from_filemanager
  572. * @param $fixed_image_creation_name_to_prepend
  573. * @param $fixed_image_creation_to_append
  574. * @param $fixed_image_creation_width
  575. * @param $fixed_image_creation_height
  576. * @param $fixed_image_creation_option
  577. *
  578. * @return bool
  579. */
  580. function new_thumbnails_creation($targetPath, $targetFile, $name, $current_path, $relative_image_creation, $relative_path_from_current_pos, $relative_image_creation_name_to_prepend, $relative_image_creation_name_to_append, $relative_image_creation_width, $relative_image_creation_height, $relative_image_creation_option, $fixed_image_creation, $fixed_path_from_filemanager, $fixed_image_creation_name_to_prepend, $fixed_image_creation_to_append, $fixed_image_creation_width, $fixed_image_creation_height, $fixed_image_creation_option)
  581. {
  582. //create relative thumbs
  583. $all_ok = true;
  584. if ($relative_image_creation)
  585. {
  586. foreach ($relative_path_from_current_pos as $k => $path)
  587. {
  588. if ($path != "" && $path[ strlen($path) - 1 ] != "/")
  589. {
  590. $path .= "/";
  591. }
  592. if ( ! file_exists($targetPath . $path))
  593. {
  594. create_folder($targetPath . $path, false);
  595. }
  596. $info = pathinfo($name);
  597. if ( ! endsWith($targetPath, $path))
  598. {
  599. if ( ! create_img($targetFile, $targetPath . $path . $relative_image_creation_name_to_prepend[ $k ] . $info['filename'] . $relative_image_creation_name_to_append[ $k ] . "." . $info['extension'], $relative_image_creation_width[ $k ], $relative_image_creation_height[ $k ], $relative_image_creation_option[ $k ]))
  600. {
  601. $all_ok = false;
  602. }
  603. }
  604. }
  605. }
  606. //create fixed thumbs
  607. if ($fixed_image_creation)
  608. {
  609. foreach ($fixed_path_from_filemanager as $k => $path)
  610. {
  611. if ($path != "" && $path[ strlen($path) - 1 ] != "/")
  612. {
  613. $path .= "/";
  614. }
  615. $base_dir = $path . substr_replace($targetPath, '', 0, strlen($current_path));
  616. if ( ! file_exists($base_dir))
  617. {
  618. create_folder($base_dir, false);
  619. }
  620. $info = pathinfo($name);
  621. if ( ! create_img($targetFile, $base_dir . $fixed_image_creation_name_to_prepend[ $k ] . $info['filename'] . $fixed_image_creation_to_append[ $k ] . "." . $info['extension'], $fixed_image_creation_width[ $k ], $fixed_image_creation_height[ $k ], $fixed_image_creation_option[ $k ]))
  622. {
  623. $all_ok = false;
  624. }
  625. }
  626. }
  627. return $all_ok;
  628. }
  629. /**
  630. * Get a remote file, using whichever mechanism is enabled
  631. *
  632. * @param string $url
  633. *
  634. * @return bool|mixed|string
  635. */
  636. function get_file_by_url($url)
  637. {
  638. if (ini_get('allow_url_fopen'))
  639. {
  640. return file_get_contents($url);
  641. }
  642. if ( ! function_exists('curl_version'))
  643. {
  644. return false;
  645. }
  646. $ch = curl_init();
  647. curl_setopt($ch, CURLOPT_HEADER, 0);
  648. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  649. curl_setopt($ch, CURLOPT_URL, $url);
  650. $data = curl_exec($ch);
  651. curl_close($ch);
  652. return $data;
  653. }
  654. /**
  655. * test for dir/file writability properly
  656. *
  657. * @param string $dir
  658. *
  659. * @return bool
  660. */
  661. function is_really_writable($dir)
  662. {
  663. $dir = rtrim($dir, '/');
  664. // linux, safe off
  665. if (DIRECTORY_SEPARATOR == '/' && @ini_get("safe_mode") == false)
  666. {
  667. return is_writable($dir);
  668. }
  669. // Windows, safe ON. (have to write a file :S)
  670. if (is_dir($dir))
  671. {
  672. $dir = $dir . '/' . md5(mt_rand(1, 1000) . mt_rand(1, 1000));
  673. if (($fp = @fopen($dir, 'ab')) === false)
  674. {
  675. return false;
  676. }
  677. fclose($fp);
  678. @chmod($dir, 0755);
  679. @unlink($dir);
  680. return true;
  681. }
  682. elseif ( ! is_file($dir) || ($fp = @fopen($dir, 'ab')) === false)
  683. {
  684. return false;
  685. }
  686. fclose($fp);
  687. return true;
  688. }
  689. /**
  690. * Check if a function is callable.
  691. * Some servers disable copy,rename etc.
  692. *
  693. * @parm string $name
  694. *
  695. * @return bool
  696. */
  697. function is_function_callable($name)
  698. {
  699. if (function_exists($name) === false)
  700. {
  701. return false;
  702. }
  703. $disabled = explode(',', ini_get('disable_functions'));
  704. return ! in_array($name, $disabled);
  705. }
  706. /**
  707. * recursivly copies everything
  708. *
  709. * @param string $source
  710. * @param string $destination
  711. * @param bool $is_rec
  712. */
  713. function rcopy($source, $destination, $is_rec = false)
  714. {
  715. if (is_dir($source))
  716. {
  717. if ($is_rec === false)
  718. {
  719. $pinfo = pathinfo($source);
  720. $destination = rtrim($destination, '/') . DIRECTORY_SEPARATOR . $pinfo['basename'];
  721. }
  722. if (is_dir($destination) === false)
  723. {
  724. mkdir($destination, 0755, true);
  725. }
  726. $files = scandir($source);
  727. foreach ($files as $file)
  728. {
  729. if ($file != "." && $file != "..")
  730. {
  731. rcopy($source . DIRECTORY_SEPARATOR . $file, rtrim($destination, '/') . DIRECTORY_SEPARATOR . $file, true);
  732. }
  733. }
  734. }
  735. else
  736. {
  737. if (file_exists($source))
  738. {
  739. if (is_dir($destination) === true)
  740. {
  741. $pinfo = pathinfo($source);
  742. $dest2 = rtrim($destination, '/') . DIRECTORY_SEPARATOR . $pinfo['basename'];
  743. }
  744. else
  745. {
  746. $dest2 = $destination;
  747. }
  748. copy($source, $dest2);
  749. }
  750. }
  751. }
  752. /**
  753. * recursivly renames everything
  754. *
  755. * I know copy and rename could be done with just one function
  756. * but i split the 2 because sometimes rename fails on windows
  757. * Need more feedback from users and refactor if needed
  758. *
  759. * @param string $source
  760. * @param string $destination
  761. * @param bool $is_rec
  762. */
  763. function rrename($source, $destination, $is_rec = false)
  764. {
  765. if (is_dir($source))
  766. {
  767. if ($is_rec === false)
  768. {
  769. $pinfo = pathinfo($source);
  770. $destination = rtrim($destination, '/') . DIRECTORY_SEPARATOR . $pinfo['basename'];
  771. }
  772. if (is_dir($destination) === false)
  773. {
  774. mkdir($destination, 0755, true);
  775. }
  776. $files = scandir($source);
  777. foreach ($files as $file)
  778. {
  779. if ($file != "." && $file != "..")
  780. {
  781. rrename($source . DIRECTORY_SEPARATOR . $file, rtrim($destination, '/') . DIRECTORY_SEPARATOR . $file, true);
  782. }
  783. }
  784. }
  785. else
  786. {
  787. if (file_exists($source))
  788. {
  789. if (is_dir($destination) === true)
  790. {
  791. $pinfo = pathinfo($source);
  792. $dest2 = rtrim($destination, '/') . DIRECTORY_SEPARATOR . $pinfo['basename'];
  793. }
  794. else
  795. {
  796. $dest2 = $destination;
  797. }
  798. rename($source, $dest2);
  799. }
  800. }
  801. }
  802. // On windows rename leaves folders sometime
  803. // This will clear leftover folders
  804. // After more feedback will merge it with rrename
  805. function rrename_after_cleaner($source)
  806. {
  807. $files = scandir($source);
  808. foreach ($files as $file)
  809. {
  810. if ($file != "." && $file != "..")
  811. {
  812. if (is_dir($source . DIRECTORY_SEPARATOR . $file))
  813. {
  814. rrename_after_cleaner($source . DIRECTORY_SEPARATOR . $file);
  815. }
  816. else
  817. {
  818. unlink($source . DIRECTORY_SEPARATOR . $file);
  819. }
  820. }
  821. }
  822. return rmdir($source);
  823. }
  824. /**
  825. * Recursive chmod
  826. * @param string $source
  827. * @param int $mode
  828. * @param string $rec_option
  829. * @param bool $is_rec
  830. */
  831. function rchmod($source, $mode, $rec_option = "none", $is_rec = false)
  832. {
  833. if ($rec_option == "none")
  834. {
  835. chmod($source, $mode);
  836. }
  837. else
  838. {
  839. if ($is_rec === false)
  840. {
  841. chmod($source, $mode);
  842. }
  843. $files = scandir($source);
  844. foreach ($files as $file)
  845. {
  846. if ($file != "." && $file != "..")
  847. {
  848. if (is_dir($source . DIRECTORY_SEPARATOR . $file))
  849. {
  850. if ($rec_option == "folders" || $rec_option == "both")
  851. {
  852. chmod($source . DIRECTORY_SEPARATOR . $file, $mode);
  853. }
  854. rchmod($source . DIRECTORY_SEPARATOR . $file, $mode, $rec_option, true);
  855. }
  856. else
  857. {
  858. if ($rec_option == "files" || $rec_option == "both")
  859. {
  860. chmod($source . DIRECTORY_SEPARATOR . $file, $mode);
  861. }
  862. }
  863. }
  864. }
  865. }
  866. }
  867. /**
  868. * Check if chmod is valid
  869. *
  870. * @param $perm
  871. * @param $val
  872. *
  873. * @return bool
  874. */
  875. function chmod_logic_helper($perm, $val)
  876. {
  877. $valid = array(
  878. 1 => array( 1, 3, 5, 7 ),
  879. 2 => array( 2, 3, 6, 7 ),
  880. 4 => array( 4, 5, 6, 7 )
  881. );
  882. if (in_array($perm, $valid[ $val ]))
  883. {
  884. return true;
  885. }
  886. else
  887. {
  888. return false;
  889. }
  890. }
  891. /**
  892. * @param string $input
  893. * @param bool $trace
  894. * @param bool $halt
  895. */
  896. function debugger($input, $trace = false, $halt = false)
  897. {
  898. ob_start();
  899. echo "<br>----- DEBUG DUMP -----";
  900. echo "<pre>";
  901. var_dump($input);
  902. echo "</pre>";
  903. if ($trace)
  904. {
  905. if (is_php('5.3.6'))
  906. {
  907. $debug = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
  908. }
  909. else
  910. {
  911. $debug = debug_backtrace(false);
  912. }
  913. echo "<br>-----STACK TRACE-----";
  914. echo "<pre>";
  915. var_dump($debug);
  916. echo "</pre>";
  917. }
  918. echo "</pre>";
  919. echo "---------------------------<br>";
  920. $ret = ob_get_contents();
  921. ob_end_clean();
  922. echo $ret;
  923. if ($halt == true)
  924. {
  925. exit();
  926. }
  927. }
  928. /**
  929. * @param string $version
  930. *
  931. * @return bool
  932. */
  933. function is_php($version = '5.0.0')
  934. {
  935. static $phpVer;
  936. $version = (string) $version;
  937. if ( ! isset($phpVer[ $version ]))
  938. {
  939. $phpVer[ $version ] = (version_compare(PHP_VERSION, $version) < 0) ? false : true;
  940. }
  941. return $phpVer[ $version ];
  942. }