core.create_dir_structure.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * Smarty plugin
  4. * @package Smarty
  5. * @subpackage plugins
  6. */
  7. /**
  8. * create full directory structure
  9. *
  10. * @param string $dir
  11. */
  12. // $dir
  13. function smarty_core_create_dir_structure($params, &$smarty)
  14. {
  15. if (!file_exists($params['dir'])) {
  16. $_open_basedir_ini = ini_get('open_basedir');
  17. if (DIRECTORY_SEPARATOR=='/') {
  18. /* unix-style paths */
  19. $_dir = $params['dir'];
  20. $_dir_parts = preg_split('!/+!', $_dir, -1, PREG_SPLIT_NO_EMPTY);
  21. $_new_dir = (substr($_dir, 0, 1)=='/') ? '/' : getcwd().'/';
  22. if($_use_open_basedir = !empty($_open_basedir_ini)) {
  23. $_open_basedirs = explode(':', $_open_basedir_ini);
  24. }
  25. } else {
  26. /* other-style paths */
  27. $_dir = str_replace('\\','/', $params['dir']);
  28. $_dir_parts = preg_split('!/+!', $_dir, -1, PREG_SPLIT_NO_EMPTY);
  29. if (preg_match('!^((//)|([a-zA-Z]:/))!', $_dir, $_root_dir)) {
  30. /* leading "//" for network volume, or "[letter]:/" for full path */
  31. $_new_dir = $_root_dir[1];
  32. /* remove drive-letter from _dir_parts */
  33. if (isset($_root_dir[3])) array_shift($_dir_parts);
  34. } else {
  35. $_new_dir = str_replace('\\', '/', getcwd()).'/';
  36. }
  37. if($_use_open_basedir = !empty($_open_basedir_ini)) {
  38. $_open_basedirs = explode(';', str_replace('\\', '/', $_open_basedir_ini));
  39. }
  40. }
  41. /* all paths use "/" only from here */
  42. foreach ($_dir_parts as $_dir_part) {
  43. $_new_dir .= $_dir_part;
  44. if ($_use_open_basedir) {
  45. // do not attempt to test or make directories outside of open_basedir
  46. $_make_new_dir = false;
  47. foreach ($_open_basedirs as $_open_basedir) {
  48. if (substr($_new_dir, 0, strlen($_open_basedir)) == $_open_basedir) {
  49. $_make_new_dir = true;
  50. break;
  51. }
  52. }
  53. } else {
  54. $_make_new_dir = true;
  55. }
  56. if ($_make_new_dir && !file_exists($_new_dir) && !@mkdir($_new_dir, $smarty->_dir_perms) && !is_dir($_new_dir)) {
  57. $smarty->trigger_error("problem creating directory '" . $_new_dir . "'");
  58. return false;
  59. }
  60. $_new_dir .= '/';
  61. }
  62. }
  63. }
  64. /* vim: set expandtab: */
  65. ?>