OSDN Git Service

NP_DragAndDropUploader v1.1
[nucleus-jp/nucleus-plugins.git] / trunk / NP_DragAndDropUploader / sharedlibs / cles / Template.php
1 <?php\r
2 // vim: tabstop=2:shiftwidth=2\r
3 \r
4 /**\r
5   * Template.php ($Revision: 1.11 $)\r
6   * \r
7   * by hsur ( http://blog.cles.jp/np_cles )\r
8   * $Id: Template.php,v 1.11 2008/08/15 21:17:02 hsur Exp $\r
9 */\r
10 \r
11 /*\r
12   * Copyright (C) 2006-2008 CLES. All rights reserved.\r
13   *\r
14   * This program is free software; you can redistribute it and/or\r
15   * modify it under the terms of the GNU General Public License\r
16   * as published by the Free Software Foundation; either version 2\r
17   * of the License, or (at your option) any later version.\r
18   * \r
19   * This program is distributed in the hope that it will be useful,\r
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
22   * GNU General Public License for more details.\r
23   * \r
24   * You should have received a copy of the GNU General Public License\r
25   * along with this program; if not, write to the Free Software\r
26   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA\r
27   * \r
28   * In addition, as a special exception, cles( http://blog.cles.jp/np_cles ) gives\r
29   * permission to link the code of this program with those files in the PEAR\r
30   * library that are licensed under the PHP License (or with modified versions\r
31   * of those files that use the same license as those files), and distribute\r
32   * linked combinations including the two. You must obey the GNU General Public\r
33   * License in all respects for all of the code used other than those files in\r
34   * the PEAR library that are licensed under the PHP License. If you modify\r
35   * this file, you may extend this exception to your version of the file,\r
36   * but you are not obligated to do so. If you do not wish to do so, delete\r
37   * this exception statement from your version.\r
38 */\r
39 \r
40 if (!function_exists('getLanguageName')){\r
41         function getLanguageName(){\r
42                 return 'japanese-utf8';\r
43         }\r
44 }\r
45 \r
46 class cles_Template {\r
47         var $defaultLang = 'japanese-utf8';\r
48         var $defalutPattern = '#{{(.*?)(\|)?}}#ie';\r
49         var $lang;\r
50         var $templateDir;\r
51 \r
52         function cles_Template($templateDir) {\r
53                 global $CONF;\r
54                 $this->templateDir = $templateDir;\r
55                 $this->lang = ereg_replace( '[\\|/]', '', getLanguageName());\r
56         }\r
57 \r
58         function fetch($name, $dir = null, $suffix = 'html') {\r
59                 $path = $this->templateDir.'/'.( $dir ? strtolower($dir) . '/' : '' ).strtolower($name).'_'.$this->lang.( $suffix ? '.'.strtolower($suffix) : '' );\r
60                 if ( ! file_exists($path) ){\r
61                         $path = $this->templateDir.'/'.( $dir ? strtolower($dir) . '/' : '' ).strtolower($name).'_'.$this->defaultLang.( $suffix ? '.'.strtolower($suffix) : '' );\r
62                         if ( ! file_exists($path) )\r
63                                 return '';\r
64                 }\r
65                 \r
66                 $fsize = filesize($path);\r
67                 if ($fsize <= 0) return '';\r
68                 \r
69                 $fd = fopen($path, 'r');\r
70                 $contents = fread($fd, $fsize);\r
71                 fclose($fd);\r
72                 return $contents;\r
73         }\r
74         \r
75         function fill($template, $values, $default = null) {\r
76                 if( $default )\r
77                         return preg_replace($this->defalutPattern, 'isset($values[\'$1\']) ? (\'$2\' ? htmlspecialchars($values[\'$1\'], ENT_QUOTES) : $values[\'$1\']) : $default', $template);\r
78                 if( $default === null )\r
79                         return preg_replace($this->defalutPattern, '(\'$2\') ? htmlspecialchars($values[\'$1\'], ENT_QUOTES) : $values[\'$1\']', $template);\r
80                 return preg_replace($this->defalutPattern, 'isset($values[\'$1\']) ? (\'$2\' ? htmlspecialchars($values[\'$1\'], ENT_QUOTES) : $values[\'$1\']) : \'{{$1}}\' ', $template);\r
81         }\r
82         \r
83         function fetchAndFill($name, $values, $dir = null, $suffix = 'html', $default = null){\r
84                 $tpl = $this->fetch($name, $dir, $suffix);\r
85                 return $this->fill($tpl, $values, $default);\r
86         }\r
87 }\r