PageRenderTime 61ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/backend/web/filemanager/include/utils.php

https://gitlab.com/angrydevops/workerpro
PHP | 1063 lines | 902 code | 39 blank | 122 comment | 68 complexity | 4f9b9e7efa22db1e985d8e5f2d0a171c 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. $result = false;
  189. if(file_exists($imgfile) || strpos($imgfile,'http')===0){
  190. $timeLimit = ini_get('max_execution_time');
  191. set_time_limit(30);
  192. if (strpos($imgfile,'http')===0 || image_check_memory_usage($imgfile, $newwidth, $newheight))
  193. {
  194. require_once('php_image_magician.php');
  195. $magicianObj = new imageLib($imgfile);
  196. $magicianObj->resizeImage($newwidth, $newheight, $option);
  197. $magicianObj->saveImage($imgthumb, 80);
  198. $result = true;
  199. }
  200. set_time_limit($timeLimit);
  201. }
  202. return $result;
  203. }
  204. /**
  205. * Convert convert size in bytes to human readable
  206. *
  207. * @param int $size
  208. *
  209. * @return string
  210. */
  211. function makeSize($size)
  212. {
  213. $units = array( 'B', 'KB', 'MB', 'GB', 'TB' );
  214. $u = 0;
  215. while ((round($size / 1024) > 0) && ($u < 4))
  216. {
  217. $size = $size / 1024;
  218. $u++;
  219. }
  220. return (number_format($size, 0) . " " . $units[ $u ]);
  221. }
  222. /**
  223. * Determine directory size
  224. *
  225. * @param string $path
  226. *
  227. * @return int
  228. */
  229. function folder_info($path)
  230. {
  231. $total_size = 0;
  232. $files = scandir($path);
  233. $cleanPath = rtrim($path, '/') . '/';
  234. $files_count = 0;
  235. $folders_count = 0;
  236. foreach ($files as $t)
  237. {
  238. if ($t != "." && $t != "..")
  239. {
  240. $currentFile = $cleanPath . $t;
  241. if (is_dir($currentFile))
  242. {
  243. list($size,$tmp,$tmp1) = folder_info($currentFile);
  244. $total_size += $size;
  245. $folders_count ++;
  246. }
  247. else
  248. {
  249. $size = filesize($currentFile);
  250. $total_size += $size;
  251. $files_count++;
  252. }
  253. }
  254. }
  255. return array($total_size,$files_count,$folders_count);
  256. }
  257. /**
  258. * Get number of files in a directory
  259. *
  260. * @param string $path
  261. *
  262. * @return int
  263. */
  264. function filescount($path)
  265. {
  266. $total_count = 0;
  267. $files = scandir($path);
  268. $cleanPath = rtrim($path, '/') . '/';
  269. foreach ($files as $t)
  270. {
  271. if ($t != "." && $t != "..")
  272. {
  273. $currentFile = $cleanPath . $t;
  274. if (is_dir($currentFile))
  275. {
  276. $size = filescount($currentFile);
  277. $total_count += $size;
  278. }
  279. else
  280. {
  281. $total_count += 1;
  282. }
  283. }
  284. }
  285. return $total_count;
  286. }
  287. /**
  288. * Create directory for images and/or thumbnails
  289. *
  290. * @param string $path
  291. * @param string $path_thumbs
  292. */
  293. function create_folder($path = null, $path_thumbs = null)
  294. {
  295. $oldumask = umask(0);
  296. if (function_exists('chgrp') && function_exists('chown')) {
  297. $params = include '../../../../common/config/params.php';
  298. }
  299. if ($path && !file_exists($path)) {
  300. mkdir($path, 0777, true);
  301. if (isset($params)) {
  302. @chgrp($path, $params['ownerGroup']);
  303. @chown($path, $params['ownerUser']);
  304. }
  305. }
  306. if($path_thumbs && !file_exists($path_thumbs)) {
  307. mkdir($path_thumbs, 0777, true) or die("$path_thumbs cannot be found");
  308. if (isset($params)) {
  309. @chgrp($path_thumbs, $params['ownerGroup']);
  310. @chown($path_thumbs, $params['ownerUser']);
  311. }
  312. }
  313. umask($oldumask);
  314. }
  315. /**
  316. * Get file extension present in directory
  317. *
  318. * @param string $path
  319. * @param string $ext
  320. */
  321. function check_files_extensions_on_path($path, $ext)
  322. {
  323. if ( ! is_dir($path))
  324. {
  325. $fileinfo = pathinfo($path);
  326. if ( ! in_array(mb_strtolower($fileinfo['extension']), $ext))
  327. {
  328. unlink($path);
  329. }
  330. }
  331. else
  332. {
  333. $files = scandir($path);
  334. foreach ($files as $file)
  335. {
  336. check_files_extensions_on_path(trim($path, '/') . "/" . $file, $ext);
  337. }
  338. }
  339. }
  340. /**
  341. * Get file extension present in PHAR file
  342. *
  343. * @param string $phar
  344. * @param array $files
  345. * @param string $basepath
  346. * @param string $ext
  347. */
  348. function check_files_extensions_on_phar($phar, &$files, $basepath, $ext)
  349. {
  350. foreach ($phar as $file)
  351. {
  352. if ($file->isFile())
  353. {
  354. if (in_array(mb_strtolower($file->getExtension()), $ext))
  355. {
  356. $files[] = $basepath . $file->getFileName();
  357. }
  358. }
  359. else
  360. {
  361. if ($file->isDir())
  362. {
  363. $iterator = new DirectoryIterator($file);
  364. check_files_extensions_on_phar($iterator, $files, $basepath . $file->getFileName() . '/', $ext);
  365. }
  366. }
  367. }
  368. }
  369. /**
  370. * Cleanup input
  371. *
  372. * @param string $str
  373. *
  374. * @return string
  375. */
  376. function fix_get_params($str)
  377. {
  378. return strip_tags(preg_replace("/[^a-zA-Z0-9\.\[\]_| -]/", '', $str));
  379. }
  380. /**
  381. * Cleanup filename
  382. *
  383. * @param string $str
  384. * @param bool $transliteration
  385. * @param bool $convert_spaces
  386. * @param string $replace_with
  387. * @param bool $is_folder
  388. *
  389. * @return string
  390. */
  391. function fix_filename($str, $transliteration, $convert_spaces = false, $replace_with = "_", $is_folder = false)
  392. {
  393. if ($convert_spaces)
  394. {
  395. $str = str_replace(' ', $replace_with, $str);
  396. }
  397. if ($transliteration)
  398. {
  399. $transliteration = dirname(dirname(dirname(dirname(dirname(__FILE__)))));
  400. require_once($transliteration . '/vendor/yiisoft/yii2/helpers/BaseInflector.php');
  401. require_once($transliteration . '/common/helpers/Inflector.php');
  402. $transliteration = mb_strrpos($str, '.');
  403. if ($transliteration === false) {
  404. $ext = '.jpg';
  405. } else {
  406. $ext = mb_strtolower(mb_substr($str, $transliteration));
  407. $str = mb_substr($str, 0, $transliteration);
  408. }
  409. $str = \common\helpers\Inflector::slug($str);
  410. $str = $str === '' ? 'xz' . $ext : $str . $ext;
  411. $str = preg_replace('/[^a-z0-9\.-]/', '-', $str);
  412. $str = preg_replace('/-{2,}/', '-', trim($str, '--'));
  413. }
  414. $str = str_replace(array( '"', "'", "/", "\\" ), "", $str);
  415. $str = strip_tags($str);
  416. // Empty or incorrectly transliterated filename.
  417. // Here is a point: a good file UNKNOWN_LANGUAGE.jpg could become .jpg in previous code.
  418. // So we add that default 'file' name to fix that issue.
  419. if (strpos($str, '.') === 0 && $is_folder === false)
  420. {
  421. $str = 'file' . $str;
  422. }
  423. return trim($str);
  424. }
  425. /**
  426. * Cleanup directory name
  427. *
  428. * @param string $str
  429. *
  430. * @return string
  431. */
  432. function fix_dirname($str)
  433. {
  434. return str_replace('~', ' ', dirname(str_replace(' ', '~', $str)));
  435. }
  436. /**
  437. * Correct strtoupper handling
  438. *
  439. * @param string $str
  440. *
  441. * @return string
  442. */
  443. function fix_strtoupper($str)
  444. {
  445. if (function_exists('mb_strtoupper'))
  446. {
  447. return mb_strtoupper($str, 'UTF-8');
  448. }
  449. else
  450. {
  451. return strtoupper($str);
  452. }
  453. }
  454. /**
  455. * Correct strtolower handling
  456. *
  457. * @param string $str
  458. *
  459. * @return string
  460. */
  461. function fix_strtolower($str)
  462. {
  463. if (function_exists('mb_strtoupper'))
  464. {
  465. return mb_strtolower($str, 'UTF-8');
  466. }
  467. else
  468. {
  469. return strtolower($str);
  470. }
  471. }
  472. function fix_path($path, $transliteration, $convert_spaces = false, $replace_with = "_")
  473. {
  474. $info = pathinfo($path);
  475. $tmp_path = $info['dirname'];
  476. $str = fix_filename($info['filename'], $transliteration, $convert_spaces, $replace_with);
  477. if ($tmp_path != "")
  478. {
  479. return $tmp_path . DIRECTORY_SEPARATOR . $str;
  480. }
  481. else
  482. {
  483. return $str;
  484. }
  485. }
  486. /**
  487. * Get current base url
  488. *
  489. * @return string
  490. */
  491. function base_url()
  492. {
  493. return sprintf(
  494. "%s://%s",
  495. isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http',
  496. $_SERVER['HTTP_HOST']
  497. );
  498. }
  499. /**
  500. * @param $current_path
  501. * @param $fld
  502. *
  503. * @return bool
  504. */
  505. function config_loading($current_path, $fld)
  506. {
  507. if (file_exists($current_path . $fld . ".config"))
  508. {
  509. require_once($current_path . $fld . ".config");
  510. return true;
  511. }
  512. echo "!!!!" . $parent = fix_dirname($fld);
  513. if ($parent != "." && ! empty($parent))
  514. {
  515. config_loading($current_path, $parent);
  516. }
  517. return false;
  518. }
  519. /**
  520. * Check if memory is enough to process image
  521. *
  522. * @param string $img
  523. * @param int $max_breedte
  524. * @param int $max_hoogte
  525. *
  526. * @return bool
  527. */
  528. function image_check_memory_usage($img, $max_breedte, $max_hoogte)
  529. {
  530. if (file_exists($img))
  531. {
  532. $K64 = 65536; // number of bytes in 64K
  533. $memory_usage = memory_get_usage();
  534. $memory_limit = abs(intval(str_replace('M', '', ini_get('memory_limit')) * 1024 * 1024));
  535. $image_properties = getimagesize($img);
  536. $image_width = $image_properties[0];
  537. $image_height = $image_properties[1];
  538. if (isset($image_properties['bits']))
  539. $image_bits = $image_properties['bits'];
  540. else
  541. $image_bits = 0;
  542. $image_memory_usage = $K64 + ($image_width * $image_height * ($image_bits) * 2);
  543. $thumb_memory_usage = $K64 + ($max_breedte * $max_hoogte * ($image_bits) * 2);
  544. $memory_needed = intval($memory_usage + $image_memory_usage + $thumb_memory_usage);
  545. if ($memory_needed > $memory_limit)
  546. {
  547. ini_set('memory_limit', (intval($memory_needed / 1024 / 1024) + 5) . 'M');
  548. if (ini_get('memory_limit') == (intval($memory_needed / 1024 / 1024) + 5) . 'M')
  549. {
  550. return true;
  551. }
  552. else
  553. {
  554. return false;
  555. }
  556. }
  557. else
  558. {
  559. return true;
  560. }
  561. }
  562. else
  563. {
  564. return false;
  565. }
  566. }
  567. /**
  568. * Check is string is ended with needle
  569. *
  570. * @param string $haystack
  571. * @param string $needle
  572. *
  573. * @return bool
  574. */
  575. function endsWith($haystack, $needle)
  576. {
  577. return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
  578. }
  579. /**
  580. * TODO REFACTOR THIS!
  581. *
  582. * @param $targetPath
  583. * @param $targetFile
  584. * @param $name
  585. * @param $current_path
  586. * @param $relative_image_creation
  587. * @param $relative_path_from_current_pos
  588. * @param $relative_image_creation_name_to_prepend
  589. * @param $relative_image_creation_name_to_append
  590. * @param $relative_image_creation_width
  591. * @param $relative_image_creation_height
  592. * @param $relative_image_creation_option
  593. * @param $fixed_image_creation
  594. * @param $fixed_path_from_filemanager
  595. * @param $fixed_image_creation_name_to_prepend
  596. * @param $fixed_image_creation_to_append
  597. * @param $fixed_image_creation_width
  598. * @param $fixed_image_creation_height
  599. * @param $fixed_image_creation_option
  600. *
  601. * @return bool
  602. */
  603. 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)
  604. {
  605. //create relative thumbs
  606. $all_ok = true;
  607. if ($relative_image_creation)
  608. {
  609. foreach ($relative_path_from_current_pos as $k => $path)
  610. {
  611. if ($path != "" && $path[ strlen($path) - 1 ] != "/")
  612. {
  613. $path .= "/";
  614. }
  615. if ( ! file_exists($targetPath . $path))
  616. {
  617. create_folder($targetPath . $path, false);
  618. }
  619. $info = pathinfo($name);
  620. if ( ! endsWith($targetPath, $path))
  621. {
  622. 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 ]))
  623. {
  624. $all_ok = false;
  625. }
  626. }
  627. }
  628. }
  629. //create fixed thumbs
  630. if ($fixed_image_creation)
  631. {
  632. foreach ($fixed_path_from_filemanager as $k => $path)
  633. {
  634. if ($path != "" && $path[ strlen($path) - 1 ] != "/")
  635. {
  636. $path .= "/";
  637. }
  638. $base_dir = $path . substr_replace($targetPath, '', 0, strlen($current_path));
  639. if ( ! file_exists($base_dir))
  640. {
  641. create_folder($base_dir, false);
  642. }
  643. $info = pathinfo($name);
  644. 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 ]))
  645. {
  646. $all_ok = false;
  647. }
  648. }
  649. }
  650. return $all_ok;
  651. }
  652. /**
  653. * Get a remote file, using whichever mechanism is enabled
  654. *
  655. * @param string $url
  656. *
  657. * @return bool|mixed|string
  658. */
  659. function get_file_by_url($url)
  660. {
  661. if (ini_get('allow_url_fopen'))
  662. {
  663. return file_get_contents($url);
  664. }
  665. if ( ! function_exists('curl_version'))
  666. {
  667. return false;
  668. }
  669. $ch = curl_init();
  670. curl_setopt($ch, CURLOPT_HEADER, 0);
  671. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  672. curl_setopt($ch, CURLOPT_URL, $url);
  673. $data = curl_exec($ch);
  674. curl_close($ch);
  675. return $data;
  676. }
  677. /**
  678. * test for dir/file writability properly
  679. *
  680. * @param string $dir
  681. *
  682. * @return bool
  683. */
  684. function is_really_writable($dir)
  685. {
  686. $dir = rtrim($dir, '/');
  687. // linux, safe off
  688. if (DIRECTORY_SEPARATOR == '/' && @ini_get("safe_mode") == false)
  689. {
  690. return is_writable($dir);
  691. }
  692. // Windows, safe ON. (have to write a file :S)
  693. if (is_dir($dir))
  694. {
  695. $dir = $dir . '/' . md5(mt_rand(1, 1000) . mt_rand(1, 1000));
  696. if (($fp = @fopen($dir, 'ab')) === false)
  697. {
  698. return false;
  699. }
  700. fclose($fp);
  701. @chmod($dir, 0755);
  702. @unlink($dir);
  703. return true;
  704. }
  705. elseif ( ! is_file($dir) || ($fp = @fopen($dir, 'ab')) === false)
  706. {
  707. return false;
  708. }
  709. fclose($fp);
  710. return true;
  711. }
  712. /**
  713. * Check if a function is callable.
  714. * Some servers disable copy,rename etc.
  715. *
  716. * @parm string $name
  717. *
  718. * @return bool
  719. */
  720. function is_function_callable($name)
  721. {
  722. if (function_exists($name) === false)
  723. {
  724. return false;
  725. }
  726. $disabled = explode(',', ini_get('disable_functions'));
  727. return ! in_array($name, $disabled);
  728. }
  729. /**
  730. * recursivly copies everything
  731. *
  732. * @param string $source
  733. * @param string $destination
  734. * @param bool $is_rec
  735. */
  736. function rcopy($source, $destination, $is_rec = false)
  737. {
  738. if (is_dir($source))
  739. {
  740. if ($is_rec === false)
  741. {
  742. $pinfo = pathinfo($source);
  743. $destination = rtrim($destination, '/') . DIRECTORY_SEPARATOR . $pinfo['basename'];
  744. }
  745. if (is_dir($destination) === false)
  746. {
  747. mkdir($destination, 0755, true);
  748. }
  749. $files = scandir($source);
  750. foreach ($files as $file)
  751. {
  752. if ($file != "." && $file != "..")
  753. {
  754. rcopy($source . DIRECTORY_SEPARATOR . $file, rtrim($destination, '/') . DIRECTORY_SEPARATOR . $file, true);
  755. }
  756. }
  757. }
  758. else
  759. {
  760. if (file_exists($source))
  761. {
  762. if (is_dir($destination) === true)
  763. {
  764. $pinfo = pathinfo($source);
  765. $dest2 = rtrim($destination, '/') . DIRECTORY_SEPARATOR . $pinfo['basename'];
  766. }
  767. else
  768. {
  769. $dest2 = $destination;
  770. }
  771. copy($source, $dest2);
  772. }
  773. }
  774. }
  775. /**
  776. * recursivly renames everything
  777. *
  778. * I know copy and rename could be done with just one function
  779. * but i split the 2 because sometimes rename fails on windows
  780. * Need more feedback from users and refactor if needed
  781. *
  782. * @param string $source
  783. * @param string $destination
  784. * @param bool $is_rec
  785. */
  786. function rrename($source, $destination, $is_rec = false)
  787. {
  788. if (is_dir($source))
  789. {
  790. if ($is_rec === false)
  791. {
  792. $pinfo = pathinfo($source);
  793. $destination = rtrim($destination, '/') . DIRECTORY_SEPARATOR . $pinfo['basename'];
  794. }
  795. if (is_dir($destination) === false)
  796. {
  797. mkdir($destination, 0755, true);
  798. }
  799. $files = scandir($source);
  800. foreach ($files as $file)
  801. {
  802. if ($file != "." && $file != "..")
  803. {
  804. rrename($source . DIRECTORY_SEPARATOR . $file, rtrim($destination, '/') . DIRECTORY_SEPARATOR . $file, true);
  805. }
  806. }
  807. }
  808. else
  809. {
  810. if (file_exists($source))
  811. {
  812. if (is_dir($destination) === true)
  813. {
  814. $pinfo = pathinfo($source);
  815. $dest2 = rtrim($destination, '/') . DIRECTORY_SEPARATOR . $pinfo['basename'];
  816. }
  817. else
  818. {
  819. $dest2 = $destination;
  820. }
  821. rename($source, $dest2);
  822. }
  823. }
  824. }
  825. // On windows rename leaves folders sometime
  826. // This will clear leftover folders
  827. // After more feedback will merge it with rrename
  828. function rrename_after_cleaner($source)
  829. {
  830. $files = scandir($source);
  831. foreach ($files as $file)
  832. {
  833. if ($file != "." && $file != "..")
  834. {
  835. if (is_dir($source . DIRECTORY_SEPARATOR . $file))
  836. {
  837. rrename_after_cleaner($source . DIRECTORY_SEPARATOR . $file);
  838. }
  839. else
  840. {
  841. unlink($source . DIRECTORY_SEPARATOR . $file);
  842. }
  843. }
  844. }
  845. return rmdir($source);
  846. }
  847. /**
  848. * Recursive chmod
  849. * @param string $source
  850. * @param int $mode
  851. * @param string $rec_option
  852. * @param bool $is_rec
  853. */
  854. function rchmod($source, $mode, $rec_option = "none", $is_rec = false)
  855. {
  856. if ($rec_option == "none")
  857. {
  858. chmod($source, $mode);
  859. }
  860. else
  861. {
  862. if ($is_rec === false)
  863. {
  864. chmod($source, $mode);
  865. }
  866. $files = scandir($source);
  867. foreach ($files as $file)
  868. {
  869. if ($file != "." && $file != "..")
  870. {
  871. if (is_dir($source . DIRECTORY_SEPARATOR . $file))
  872. {
  873. if ($rec_option == "folders" || $rec_option == "both")
  874. {
  875. chmod($source . DIRECTORY_SEPARATOR . $file, $mode);
  876. }
  877. rchmod($source . DIRECTORY_SEPARATOR . $file, $mode, $rec_option, true);
  878. }
  879. else
  880. {
  881. if ($rec_option == "files" || $rec_option == "both")
  882. {
  883. chmod($source . DIRECTORY_SEPARATOR . $file, $mode);
  884. }
  885. }
  886. }
  887. }
  888. }
  889. }
  890. /**
  891. * Check if chmod is valid
  892. *
  893. * @param $perm
  894. * @param $val
  895. *
  896. * @return bool
  897. */
  898. function chmod_logic_helper($perm, $val)
  899. {
  900. $valid = array(
  901. 1 => array( 1, 3, 5, 7 ),
  902. 2 => array( 2, 3, 6, 7 ),
  903. 4 => array( 4, 5, 6, 7 )
  904. );
  905. if (in_array($perm, $valid[ $val ]))
  906. {
  907. return true;
  908. }
  909. else
  910. {
  911. return false;
  912. }
  913. }
  914. /**
  915. * @param string $input
  916. * @param bool $trace
  917. * @param bool $halt
  918. */
  919. function debugger($input, $trace = false, $halt = false)
  920. {
  921. ob_start();
  922. echo "<br>----- DEBUG DUMP -----";
  923. echo "<pre>";
  924. var_dump($input);
  925. echo "</pre>";
  926. if ($trace)
  927. {
  928. if (is_php('5.3.6'))
  929. {
  930. $debug = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
  931. }
  932. else
  933. {
  934. $debug = debug_backtrace(false);
  935. }
  936. echo "<br>-----STACK TRACE-----";
  937. echo "<pre>";
  938. var_dump($debug);
  939. echo "</pre>";
  940. }
  941. echo "</pre>";
  942. echo "---------------------------<br>";
  943. $ret = ob_get_contents();
  944. ob_end_clean();
  945. echo $ret;
  946. if ($halt == true)
  947. {
  948. exit();
  949. }
  950. }
  951. /**
  952. * @param string $version
  953. *
  954. * @return bool
  955. */
  956. function is_php($version = '5.0.0')
  957. {
  958. static $phpVer;
  959. $version = (string) $version;
  960. if ( ! isset($phpVer[ $version ]))
  961. {
  962. $phpVer[ $version ] = (version_compare(PHP_VERSION, $version) < 0) ? false : true;
  963. }
  964. return $phpVer[ $version ];
  965. }