Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
/**
3
 * Smarty write file plugin
4
 *
5
 * @package    Smarty
6
 * @subpackage PluginsInternal
7
 * @author     Monte Ohrt
8
 */
9
 
10
/**
11
 * Smarty Internal Write File Class
12
 *
13
 * @package    Smarty
14
 * @subpackage PluginsInternal
15
 */
16
class Smarty_Internal_Runtime_WriteFile
17
{
18
    /**
19
     * Writes file in a safe way to disk
20
     *
21
     * @param string $_filepath complete filepath
22
     * @param string $_contents file content
23
     * @param Smarty $smarty    smarty instance
24
     *
25
     * @throws SmartyException
26
     * @return boolean true
27
     */
28
    public function writeFile($_filepath, $_contents, Smarty $smarty)
29
    {
30
        $_error_reporting = error_reporting();
31
        error_reporting($_error_reporting & ~E_NOTICE & ~E_WARNING);
32
        $_dirpath = dirname($_filepath);
33
        // if subdirs, create dir structure
34
        if ($_dirpath !== '.') {
35
            $i = 0;
36
            // loop if concurrency problem occurs
37
            // see https://bugs.php.net/bug.php?id=35326
38
            while (!is_dir($_dirpath)) {
39
                if (@mkdir($_dirpath, 0777, true)) {
40
                    break;
41
                }
42
                clearstatcache();
43
                if (++$i === 3) {
44
                    error_reporting($_error_reporting);
45
                    throw new SmartyException("unable to create directory {$_dirpath}");
46
                }
47
                sleep(1);
48
            }
49
        }
50
        // write to tmp file, then move to overt file lock race condition
51
        $_tmp_file = $_dirpath . DIRECTORY_SEPARATOR . str_replace(array('.', ','), '_', uniqid('wrt', true));
52
        if (!file_put_contents($_tmp_file, $_contents)) {
53
            error_reporting($_error_reporting);
54
            throw new SmartyException("unable to write file {$_tmp_file}");
55
        }
56
        /*
57
         * Windows' rename() fails if the destination exists,
58
         * Linux' rename() properly handles the overwrite.
59
         * Simply unlink()ing a file might cause other processes
60
         * currently reading that file to fail, but linux' rename()
61
         * seems to be smart enough to handle that for us.
62
         */
63
        if (Smarty::$_IS_WINDOWS) {
64
            // remove original file
65
            if (is_file($_filepath)) {
66
                @unlink($_filepath);
67
            }
68
            // rename tmp file
69
            $success = @rename($_tmp_file, $_filepath);
70
        } else {
71
            // rename tmp file
72
            $success = @rename($_tmp_file, $_filepath);
73
            if (!$success) {
74
                // remove original file
75
                if (is_file($_filepath)) {
76
                    @unlink($_filepath);
77
                }
78
                // rename tmp file
79
                $success = @rename($_tmp_file, $_filepath);
80
            }
81
        }
82
        if (!$success) {
83
            error_reporting($_error_reporting);
84
            throw new SmartyException("unable to write file {$_filepath}");
85
        }
86
        // set file permissions
87
        @chmod($_filepath, 0666 & ~umask());
88
        error_reporting($_error_reporting);
89
        return true;
90
    }
91
}