Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * Smarty plugin
Chris PeBenito 696b41
 * @package Smarty
Chris PeBenito 696b41
 * @subpackage plugins
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
Chris PeBenito 696b41
/**
Chris PeBenito 696b41
 * create full directory structure
Chris PeBenito 696b41
 *
Chris PeBenito 696b41
 * @param string $dir
Chris PeBenito 696b41
 */
Chris PeBenito 696b41
Chris PeBenito 696b41
// $dir
Chris PeBenito 696b41
Chris PeBenito 696b41
function smarty_core_create_dir_structure($params, &$smarty)
Chris PeBenito 696b41
{
Chris PeBenito 696b41
    if (!file_exists($params['dir'])) {
Chris PeBenito 696b41
        $_open_basedir_ini = ini_get('open_basedir');
Chris PeBenito 696b41
Chris PeBenito 696b41
        if (DIRECTORY_SEPARATOR=='/') {
Chris PeBenito 696b41
            /* unix-style paths */
Chris PeBenito 696b41
            $_dir = $params['dir'];
Chris PeBenito 696b41
            $_dir_parts = preg_split('!/+!', $_dir, -1, PREG_SPLIT_NO_EMPTY);
Chris PeBenito 696b41
            $_new_dir = ($_dir{0}=='/') ? '/' : getcwd().'/';
Chris PeBenito 696b41
            if($_use_open_basedir = !empty($_open_basedir_ini)) {
Chris PeBenito 696b41
                $_open_basedirs = explode(':', $_open_basedir_ini);
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
Chris PeBenito 696b41
        } else {
Chris PeBenito 696b41
            /* other-style paths */
Chris PeBenito 696b41
            $_dir = str_replace('\\','/', $params['dir']);
Chris PeBenito 696b41
            $_dir_parts = preg_split('!/+!', $_dir, -1, PREG_SPLIT_NO_EMPTY);
Chris PeBenito 696b41
            if (preg_match('!^((//)|([a-zA-Z]:/))!', $_dir, $_root_dir)) {
Chris PeBenito 696b41
                /* leading "//" for network volume, or "[letter]:/" for full path */
Chris PeBenito 696b41
                $_new_dir = $_root_dir[1];
Chris PeBenito 696b41
                /* remove drive-letter from _dir_parts */
Chris PeBenito 696b41
                if (isset($_root_dir[3])) array_shift($_dir_parts);
Chris PeBenito 696b41
Chris PeBenito 696b41
            } else {
Chris PeBenito 696b41
                $_new_dir = str_replace('\\', '/', getcwd()).'/';
Chris PeBenito 696b41
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
Chris PeBenito 696b41
            if($_use_open_basedir = !empty($_open_basedir_ini)) {
Chris PeBenito 696b41
                $_open_basedirs = explode(';', str_replace('\\', '/', $_open_basedir_ini));
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
Chris PeBenito 696b41
        /* all paths use "/" only from here */
Chris PeBenito 696b41
        foreach ($_dir_parts as $_dir_part) {
Chris PeBenito 696b41
            $_new_dir .= $_dir_part;
Chris PeBenito 696b41
Chris PeBenito 696b41
            if ($_use_open_basedir) {
Chris PeBenito 696b41
                // do not attempt to test or make directories outside of open_basedir
Chris PeBenito 696b41
                $_make_new_dir = false;
Chris PeBenito 696b41
                foreach ($_open_basedirs as $_open_basedir) {
Chris PeBenito 696b41
                    if (substr($_new_dir, 0, strlen($_open_basedir)) == $_open_basedir) {
Chris PeBenito 696b41
                        $_make_new_dir = true;
Chris PeBenito 696b41
                        break;
Chris PeBenito 696b41
                    }
Chris PeBenito 696b41
                }
Chris PeBenito 696b41
            } else {
Chris PeBenito 696b41
                $_make_new_dir = true;
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
Chris PeBenito 696b41
            if ($_make_new_dir && !file_exists($_new_dir) && !@mkdir($_new_dir, $smarty->_dir_perms) && !is_dir($_new_dir)) {
Chris PeBenito 696b41
                $smarty->trigger_error("problem creating directory '" . $_new_dir . "'");
Chris PeBenito 696b41
                return false;
Chris PeBenito 696b41
            }
Chris PeBenito 696b41
            $_new_dir .= '/';
Chris PeBenito 696b41
        }
Chris PeBenito 696b41
    }
Chris PeBenito 696b41
}
Chris PeBenito 696b41
Chris PeBenito 696b41
/* vim: set expandtab: */
Chris PeBenito 696b41
Chris PeBenito 696b41
?>