annotate Smarty/internals/core.write_file.php @ 1:f5a9f0eb4858

deleted: LICENSE.ja
author Sushi-k <epgrec@park.mda.or.jp>
date Wed, 08 Jul 2009 11:44:50 +0900
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
1 <?php
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
2 /**
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
3 * Smarty plugin
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
4 * @package Smarty
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
5 * @subpackage plugins
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
6 */
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
7
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
8 /**
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
9 * write out a file to disk
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
10 *
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
11 * @param string $filename
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
12 * @param string $contents
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
13 * @param boolean $create_dirs
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
14 * @return boolean
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
15 */
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
16 function smarty_core_write_file($params, &$smarty)
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
17 {
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
18 $_dirname = dirname($params['filename']);
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
19
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
20 if ($params['create_dirs']) {
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
21 $_params = array('dir' => $_dirname);
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
22 require_once(SMARTY_CORE_DIR . 'core.create_dir_structure.php');
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
23 smarty_core_create_dir_structure($_params, $smarty);
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
24 }
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
25
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
26 // write to tmp file, then rename it to avoid file locking race condition
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
27 $_tmp_file = tempnam($_dirname, 'wrt');
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
28
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
29 if (!($fd = @fopen($_tmp_file, 'wb'))) {
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
30 $_tmp_file = $_dirname . DIRECTORY_SEPARATOR . uniqid('wrt');
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
31 if (!($fd = @fopen($_tmp_file, 'wb'))) {
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
32 $smarty->trigger_error("problem writing temporary file '$_tmp_file'");
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
33 return false;
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
34 }
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
35 }
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
36
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
37 fwrite($fd, $params['contents']);
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
38 fclose($fd);
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
39
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
40 if (DIRECTORY_SEPARATOR == '\\' || !@rename($_tmp_file, $params['filename'])) {
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
41 // On platforms and filesystems that cannot overwrite with rename()
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
42 // delete the file before renaming it -- because windows always suffers
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
43 // this, it is short-circuited to avoid the initial rename() attempt
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
44 @unlink($params['filename']);
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
45 @rename($_tmp_file, $params['filename']);
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
46 }
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
47 @chmod($params['filename'], $smarty->_file_perms);
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
48
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
49 return true;
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
50 }
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
51
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
52 /* vim: set expandtab: */
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
53
f5a9f0eb4858 deleted: LICENSE.ja
Sushi-k <epgrec@park.mda.or.jp>
parents:
diff changeset
54 ?>