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