1 |
joko |
1.1 |
<?php |
2 |
|
|
/** |
3 |
|
|
* Smarty plugin |
4 |
|
|
* @package Smarty |
5 |
|
|
* @subpackage plugins |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
/** |
9 |
|
|
* write out a file to disk |
10 |
|
|
* |
11 |
|
|
* @param string $filename |
12 |
|
|
* @param string $contents |
13 |
|
|
* @param boolean $create_dirs |
14 |
|
|
* @return boolean |
15 |
|
|
*/ |
16 |
|
|
function smarty_core_write_file($params, &$smarty) |
17 |
|
|
{ |
18 |
|
|
$_dirname = dirname($params['filename']); |
19 |
|
|
|
20 |
|
|
if ($params['create_dirs']) { |
21 |
|
|
$_params = array('dir' => $_dirname); |
22 |
|
|
require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.create_dir_structure.php'); |
23 |
|
|
smarty_core_create_dir_structure($_params, $smarty); |
24 |
|
|
} |
25 |
|
|
|
26 |
|
|
// write to tmp file, then rename it to avoid |
27 |
|
|
// file locking race condition |
28 |
|
|
$_tmp_file = tempnam($_dirname, 'write_'); |
29 |
|
|
|
30 |
|
|
if (!($fd = @fopen($_tmp_file, 'w'))) { |
31 |
|
|
$smarty->trigger_error("problem writing temporary file '$_tmp_file'"); |
32 |
|
|
return false; |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
fwrite($fd, $params['contents']); |
36 |
|
|
|
37 |
|
|
// Set the file's mtime |
38 |
|
|
if (isset($params['timestamp'])) { |
39 |
|
|
touch($_tmp_file, $params['timestamp']); |
40 |
|
|
} |
41 |
|
|
fclose($fd); |
42 |
|
|
|
43 |
|
|
// Delete the file if it allready exists (this is needed on Win, |
44 |
|
|
// because it cannot overwrite files with rename() |
45 |
|
|
if (file_exists($params['filename'])) { |
46 |
|
|
@unlink($params['filename']); |
47 |
|
|
} |
48 |
|
|
@rename($_tmp_file, $params['filename']); |
49 |
|
|
@chmod($params['filename'], $smarty->_file_perms); |
50 |
|
|
|
51 |
|
|
return true; |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
/* vim: set expandtab: */ |
55 |
|
|
|
56 |
|
|
?> |