delete 0.4.2 directory
@@ -1,121 +0,0 @@ | ||
1 | -/* | |
2 | - +----------------------------------------------------------------------+ | |
3 | - | PHP Version 5 | | |
4 | - +----------------------------------------------------------------------+ | |
5 | - | Copyright (c) 1997-2004 The PHP Group | | |
6 | - +----------------------------------------------------------------------+ | |
7 | - | This source file is subject to version 3.0 of the PHP license, | | |
8 | - | that is bundled with this package in the file LICENSE, and is | | |
9 | - | available through the world-wide-web at the following url: | | |
10 | - | http://www.php.net/license/3_0.txt. | | |
11 | - | If you did not receive a copy of the PHP license and are unable to | | |
12 | - | obtain it through the world-wide-web, please send a note to | | |
13 | - | license@php.net so we can mail you a copy immediately. | | |
14 | - +----------------------------------------------------------------------+ | |
15 | - | Author: | | |
16 | - +----------------------------------------------------------------------+ | |
17 | -*/ | |
18 | - | |
19 | -/* $Id$ */ | |
20 | - | |
21 | -#ifndef PHP_SIMPLATE_H | |
22 | -#define PHP_SIMPLATE_H | |
23 | - | |
24 | -extern zend_module_entry simplate_module_entry; | |
25 | -#define phpext_simplate_ptr &simplate_module_entry | |
26 | - | |
27 | -#ifdef PHP_WIN32 | |
28 | -#define PHP_SIMPLATE_API __declspec(dllexport) | |
29 | -#ifndef S_ISDIR | |
30 | -#define S_ISDIR(m) (((m) & _S_IFDIR) == _S_IFDIR) | |
31 | -#endif | |
32 | - | |
33 | -#if _MSC_VER >= 1400 | |
34 | -#ifndef strdup | |
35 | -#define strdup _strdup | |
36 | -#endif /* strdup */ | |
37 | -#ifndef unlink | |
38 | -#define unlink _unlink | |
39 | -#endif | |
40 | -#endif /* _MSC_VER */ | |
41 | - | |
42 | -#else /* PHP_WIN32 */ | |
43 | -#define PHP_SIMPLATE_API | |
44 | -#endif | |
45 | - | |
46 | -#ifdef ZTS | |
47 | -#include "TSRM.h" | |
48 | -#endif | |
49 | -//#include <string> | |
50 | - | |
51 | -#include <sstream> // stringstream | |
52 | -using std::stringstream; | |
53 | - | |
54 | -PHP_MINIT_FUNCTION(simplate); | |
55 | -PHP_MSHUTDOWN_FUNCTION(simplate); | |
56 | -PHP_RINIT_FUNCTION(simplate); | |
57 | -PHP_RSHUTDOWN_FUNCTION(simplate); | |
58 | -PHP_MINFO_FUNCTION(simplate); | |
59 | - | |
60 | -#ifdef ZEND_ENGINE_2 | |
61 | -ZEND_METHOD(simplate,__construct); | |
62 | -ZEND_METHOD(simplate,assign); | |
63 | -ZEND_METHOD(simplate,fetch); | |
64 | -ZEND_METHOD(simplate,display); | |
65 | -ZEND_METHOD(simplate,clear_cache); | |
66 | -ZEND_METHOD(simplate,register_prefilter); | |
67 | -ZEND_METHOD(simplate,register_postfilter); | |
68 | -#else | |
69 | -PHP_FUNCTION(simplate_init); | |
70 | -PHP_FUNCTION(simplate_assign); | |
71 | -PHP_FUNCTION(simplate_fetch); | |
72 | -PHP_FUNCTION(simplate_display); | |
73 | -PHP_FUNCTION(simplate_clear_cache); | |
74 | -PHP_FUNCTION(simplate_register_prefilter); | |
75 | -PHP_FUNCTION(simplate_register_postfilter); | |
76 | -#endif // ZEND_ENGINE_2 | |
77 | - | |
78 | -/* | |
79 | - Declare any global variables you may need between the BEGIN | |
80 | - and END macros here: | |
81 | -*/ | |
82 | -ZEND_BEGIN_MODULE_GLOBALS(simplate) | |
83 | -// string global_string; | |
84 | - stringstream global_string; // The C++ string cause segmentation fault in fetch method. | |
85 | -ZEND_END_MODULE_GLOBALS(simplate) | |
86 | - | |
87 | -#ifdef _GLOBAL_ | |
88 | -ZEND_BEGIN_MODULE_GLOBALS(simplate) | |
89 | - long global_value; | |
90 | - char *global_string; | |
91 | -ZEND_END_MODULE_GLOBALS(simplate) | |
92 | -#endif // _GLOBAL_ | |
93 | - | |
94 | -/* In every utility function you add that needs to use variables | |
95 | - in php_simplate_globals, call TSRMLS_FETCH(); after declaring other | |
96 | - variables used by that function, or better yet, pass in TSRMLS_CC | |
97 | - after the last function argument and declare your utility function | |
98 | - with TSRMLS_DC after the last declared argument. Always refer to | |
99 | - the globals in your function as SIMPLATE_G(variable). You are | |
100 | - encouraged to rename these macros something shorter, see | |
101 | - examples in any other php module directory. | |
102 | -*/ | |
103 | -static int php_my_output_func(const char *str,uint str_len TSRMLS_DC); | |
104 | - | |
105 | -#ifdef ZTS | |
106 | -#define SIMPLATE_G(v) TSRMG(simplate_globals_id, zend_simplate_globals *, v) | |
107 | -#else | |
108 | -#define SIMPLATE_G(v) (simplate_globals.v) | |
109 | -#endif | |
110 | - | |
111 | -#endif /* PHP_SIMPLATE_H */ | |
112 | - | |
113 | - | |
114 | -/* | |
115 | - * Local variables: | |
116 | - * tab-width: 4 | |
117 | - * c-basic-offset: 4 | |
118 | - * End: | |
119 | - * vim600: noet sw=4 ts=4 fdm=marker | |
120 | - * vim<600: noet sw=4 ts=4 | |
121 | - */ |
@@ -1 +0,0 @@ | ||
1 | -simplate |
@@ -1,2294 +0,0 @@ | ||
1 | -/* | |
2 | - +----------------------------------------------------------------------+ | |
3 | - | PHP Version 4/5 | | |
4 | - +----------------------------------------------------------------------+ | |
5 | - | Copyright (c) Kazuhiro IIzuka All rights reserved. | | |
6 | - +----------------------------------------------------------------------+ | |
7 | - | This source file is the BSD License, | | |
8 | - | that is bundled with this package in the file LICENSE, and is | | |
9 | - | available through the world-wide-web at the following url: | | |
10 | - | http://static.aimy.jp/license.txt | | |
11 | - +----------------------------------------------------------------------+ | |
12 | - | Author: Kazuhiro IIzuka | | |
13 | - +----------------------------------------------------------------------+ | |
14 | -*/ | |
15 | - | |
16 | -/* $Id$ */ | |
17 | - | |
18 | -/** | |
19 | - * @file simplate.cpp | |
20 | - * @brief Simplate class implementation | |
21 | - * @author Kazuhiro IIzuka | |
22 | - * @author Shimizu | |
23 | - * @version $Id$ | |
24 | - * Copyright (C) Kazuhiro IIzuka | |
25 | - */ | |
26 | - | |
27 | -#ifdef HAVE_CONFIG_H | |
28 | -#include "config.h" | |
29 | -#endif | |
30 | -#ifdef PHP_WIN32 | |
31 | -#include "config.w32.h" | |
32 | -#endif // PHP_WIN32 | |
33 | - | |
34 | -// C++ header... | |
35 | -#include <iostream> | |
36 | -#include <ctime> | |
37 | -#include <string> | |
38 | -#include <vector> | |
39 | -#include <set> | |
40 | -using std::cout; | |
41 | -using std::cerr; | |
42 | -using std::endl; | |
43 | -using std::string; | |
44 | -using std::vector; | |
45 | -using std::set; | |
46 | - | |
47 | -extern "C" { | |
48 | -#include "php.h" | |
49 | -#include "php_ini.h" | |
50 | -#include "ext/standard/info.h" | |
51 | -#include "ext/standard/php_var.h" // var_dump | |
52 | -#ifdef PHP_WIN32 | |
53 | -#include "win32/flock.h" | |
54 | -#else | |
55 | -#include "ext/standard/flock_compat.h" | |
56 | -#endif // PHP_WIN32 | |
57 | -} | |
58 | -#include "php_simplate.h" | |
59 | - | |
60 | -//Debug | |
61 | -#ifdef SIMPLATE_DEBUG | |
62 | -FILE *fp; | |
63 | -char msg[1024]; | |
64 | -#ifdef PHP_WIN32 | |
65 | -#define DEBUG_PRINTF(format, ...) \ | |
66 | - sprintf(msg, format, __VA_ARGS__); \ | |
67 | - fp = fopen("debug.log", "a"); \ | |
68 | - if (NULL != fp) fprintf(fp, "%s(%d): %s\n", __FUNCTION__, __LINE__, msg); \ | |
69 | - fclose(fp); | |
70 | -#else | |
71 | -#define DEBUG_PRINTF(format, ...) \ | |
72 | - sprintf(msg, format, __VA_ARGS__); \ | |
73 | - fp = fopen("debug.log", "a"); \ | |
74 | - if (NULL != fp) fprintf(fp, "%s(%d): %s\n", __func__, __LINE__, msg); \ | |
75 | - fclose(fp); | |
76 | -#endif // PHP_WIN32 | |
77 | -#else | |
78 | -#define DEBUG_PRINTF(format, ...) | |
79 | -#endif // SIMPLATE_DEBUG | |
80 | - | |
81 | -//#define PHP5.3 or more | |
82 | -#ifndef Z_REFCOUNT_P | |
83 | - | |
84 | -#define Z_REFCOUNT_PP(ppz) ((*(ppz))->refcount) | |
85 | -#define Z_SET_REFCOUNT_PP(ppz, rc) ((*(ppz))->refcount = rc) | |
86 | -#define Z_ADDREF_PP(ppz) (++(*(ppz))->refcount) | |
87 | -#define Z_DELREF_PP(ppz) (--(*(ppz))->refcount) | |
88 | -#define Z_ISREF_PP(ppz) ((*(ppz))->is_ref) | |
89 | -#define Z_SET_ISREF_PP(ppz) ((*(ppz))->is_ref = 1) | |
90 | -#define Z_UNSET_ISREF_PP(ppz) ((*(ppz))->is_ref = 0) | |
91 | -#define Z_SET_ISREF_TO_PP(ppz, isref) ((*(ppz))->is_ref = isref) | |
92 | - | |
93 | -#define Z_REFCOUNT_P(pz) ((pz)->refcount) | |
94 | -#define Z_SET_REFCOUNT_P(z, rc) ((pz)->refcount = rc) | |
95 | -#define Z_ADDREF_P(pz) (++(pz)->refcount) | |
96 | -#define Z_DELREF_P(pz) (--(pz)->refcount) | |
97 | -#define Z_ISREF_P(pz) ((pz)->is_ref) | |
98 | -#define Z_SET_ISREF_P(pz) ((pz)->is_ref = 1) | |
99 | -#define Z_UNSET_ISREF_P(pz) ((pz)->is_ref = 0) | |
100 | -#define Z_SET_ISREF_TO_P(z, isref) ((pz)->is_ref = isref) | |
101 | - | |
102 | -#define Z_REFCOUNT(z) ((z).refcount) | |
103 | -#define Z_SET_REFCOUNT(z, rc) ((z).refcount = rc) | |
104 | -#define Z_ADDREF(z) (++(z).refcount) | |
105 | -#define Z_DELREF(z) (--(z).refcount) | |
106 | -#define Z_ISREF(z) ((z).is_ref) | |
107 | -#define Z_SET_ISREF(z) ((z).is_ref = 1) | |
108 | -#define Z_UNSET_ISREF(z) ((z).is_ref = 0) | |
109 | -#define Z_SET_ISREF_TO(z, isref) ((z).is_ref = isref) | |
110 | - | |
111 | -#endif | |
112 | - | |
113 | -//#define USE_ZEND_EXECUTE | |
114 | -#define SET_ZVAL_STRING(z, s) \ | |
115 | - INIT_PZVAL(&z); \ | |
116 | - Z_STRVAL(z) = estrndup(s, strlen(s)); \ | |
117 | - Z_STRLEN(z) = strlen(s); \ | |
118 | - Z_TYPE(z) = IS_STRING; | |
119 | - | |
120 | -/* If you declare any globals in php_simplate.h uncomment this: */ | |
121 | -ZEND_DECLARE_MODULE_GLOBALS(simplate) | |
122 | - | |
123 | -/* True global resources - no need for thread safety here */ | |
124 | -static int le_simplate; | |
125 | - | |
126 | -// default parameters | |
127 | -#define DEFAULT_TEMPLATE_DIR "template" | |
128 | -#define DEFAULT_COMPILE_DIR "template_c" | |
129 | -#define DEFAULT_CACHE_DIR "cache" | |
130 | -#define DEFAULT_LEFT_DELIMITER "<{" | |
131 | -#define DEFAULT_RIGHT_DELIMITER "}>" | |
132 | -#define DEFAULT_COMPILE_CHECK true | |
133 | -#define DEFAULT_FORCE_COMPILE false | |
134 | -#define DEFAULT_LAZY_CHECK false | |
135 | -#define DEFAULT_CACHE_LIFETIME 3600 | |
136 | -#define DEFAULT_CACHING 0 // 0: no caching | |
137 | - // 1: use class cache_lifetime value | |
138 | - // 2: use cache_lifetime in cache file | |
139 | - | |
140 | -// fetch mode | |
141 | -#define SIMPLATE_FETCH 0 | |
142 | -#define SIMPLATE_DISPLAY 1 | |
143 | - | |
144 | -// parameter names | |
145 | -#define TEMPLATE_DIR "template_dir" | |
146 | -#define COMPILE_DIR "compile_dir" | |
147 | -#define CACHE_DIR "cache_dir" | |
148 | -#define LEFT_DELIMITER "left_delimiter" | |
149 | -#define RIGHT_DELIMITER "right_delimiter" | |
150 | -#define COMPILE_CHECK "compile_check" | |
151 | -#define FORCE_COMPILE "force_compile" | |
152 | -#define LAZY_CHECK "lazy_check" | |
153 | -#define VERSION "0.4.2" | |
154 | -#define CACHE_LIFETIME "cache_lifetime" | |
155 | -#define CACHING "caching" | |
156 | - | |
157 | -// class entry pointer | |
158 | -static zend_class_entry *simplate_entry_ptr; | |
159 | -static zend_function_entry php_simplate_functions[] = { | |
160 | -#ifdef ZEND_ENGINE_2 | |
161 | - ZEND_ME(simplate, __construct, NULL, ZEND_ACC_PUBLIC) | |
162 | - ZEND_ME(simplate, assign, NULL, ZEND_ACC_PUBLIC) | |
163 | - ZEND_ME(simplate, fetch, NULL, ZEND_ACC_PUBLIC) | |
164 | - ZEND_ME(simplate, display, NULL, ZEND_ACC_PUBLIC) | |
165 | - ZEND_ME(simplate, clear_cache, NULL, ZEND_ACC_PUBLIC) | |
166 | - ZEND_ME(simplate, register_prefilter, NULL, ZEND_ACC_PUBLIC) | |
167 | - ZEND_ME(simplate, register_postfilter, NULL, ZEND_ACC_PUBLIC) | |
168 | -#else | |
169 | - PHP_FALIAS(simplate, simplate_init, NULL) | |
170 | - PHP_FALIAS(assign, simplate_assign, NULL) | |
171 | - PHP_FALIAS(fetch, simplate_fetch, NULL) | |
172 | - PHP_FALIAS(display, simplate_display, NULL) | |
173 | - PHP_FALIAS(clear_cache, simplate_clear_cache, NULL) | |
174 | - PHP_FALIAS(register_prefilter, simplate_register_prefilter, NULL) | |
175 | - PHP_FALIAS(register_postfilter, simplate_register_postfilter, NULL) | |
176 | -#endif // ZEND_ENGINE_2 | |
177 | - {NULL, NULL, NULL} | |
178 | -}; | |
179 | - | |
180 | -/* {{{ simplate_functions[] | |
181 | - * | |
182 | - * Every user visible function must have an entry in simplate_functions[]. | |
183 | - */ | |
184 | -function_entry simplate_functions[] = { | |
185 | - {NULL, NULL, NULL} /* Must be the last line in simplate_functions[] */ | |
186 | -}; | |
187 | -/* }}} */ | |
188 | - | |
189 | -/* {{{ simplate_module_entry | |
190 | - */ | |
191 | -zend_module_entry simplate_module_entry = { | |
192 | -#if ZEND_MODULE_API_NO >= 20010901 | |
193 | - STANDARD_MODULE_HEADER, | |
194 | -#endif | |
195 | - "simplate", | |
196 | - simplate_functions, | |
197 | - PHP_MINIT(simplate), | |
198 | - PHP_MSHUTDOWN(simplate), | |
199 | - PHP_RINIT(simplate), /* Replace with NULL if there's nothing to do at request start */ | |
200 | - PHP_RSHUTDOWN(simplate), /* Replace with NULL if there's nothing to do at request end */ | |
201 | - PHP_MINFO(simplate), | |
202 | -#if ZEND_MODULE_API_NO >= 20010901 | |
203 | - VERSION, | |
204 | -#endif | |
205 | - STANDARD_MODULE_PROPERTIES | |
206 | -}; | |
207 | -/* }}} */ | |
208 | - | |
209 | -#ifdef COMPILE_DL_SIMPLATE | |
210 | -BEGIN_EXTERN_C() | |
211 | -ZEND_GET_MODULE(simplate) | |
212 | -END_EXTERN_C() | |
213 | -#endif | |
214 | - | |
215 | -/* {{{ PHP_INI | |
216 | - */ | |
217 | -/* Remove comments and fill if you need to have entries in php.ini | |
218 | -PHP_INI_BEGIN() | |
219 | - STD_PHP_INI_ENTRY("simplate.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_simplate_globals, simplate_globals) | |
220 | - STD_PHP_INI_ENTRY("simplate.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_simplate_globals, simplate_globals) | |
221 | -PHP_INI_END() | |
222 | -*/ | |
223 | -/* }}} */ | |
224 | - | |
225 | -/* {{{ php_simplate_init_globals | |
226 | - */ | |
227 | -/* Uncomment this function if you have INI entries */ | |
228 | -static void php_simplate_init_globals( | |
229 | - zend_simplate_globals *simplate_globals TSRMLS_DC | |
230 | -) | |
231 | -{ | |
232 | - simplate_globals->global_string; | |
233 | -} | |
234 | -/* }}} */ | |
235 | - | |
236 | -#if 0 | |
237 | -// create custom objects | |
238 | -typedef struct{ | |
239 | - string fetch_buffer; | |
240 | - zend_object zo; | |
241 | -}simplate_object; | |
242 | - | |
243 | -static void simplate_object_dtor(void *object, zend_object_handle handle TSRMLS_DC) | |
244 | -{ | |
245 | - zend_objects_destroy_object(object, handle TSRMLS_CC); | |
246 | -} | |
247 | -static void simplate_objects_clone(void *object, void **object_clone TSRMLS_DC) | |
248 | -{ | |
249 | - simplate_object *intern = (simplate_object*)object; | |
250 | - simplate_object **intern_clone = (simplate_object**)object_clone; | |
251 | - | |
252 | - *intern_clone = emalloc(sizeof(simplate_object)); | |
253 | - (*intern_object)->zo.ce = intern->zo.ce; | |
254 | - (*intern_object)->zo.in_get = 0; | |
255 | - (*intern_object)->zo.in_set = 0; | |
256 | - ALLOC_HASHTABLE((*intern_clone)->zo.properties); | |
257 | - (*intern_clone)->fetch_buffer= | |
258 | - | |
259 | -} | |
260 | -#endif // 0 | |
261 | - | |
262 | -/* {{{ Returns full path of the specified filename. | |
263 | - * | |
264 | - * ex) | |
265 | - * filename = "foo.tpl" | |
266 | - * => template_dir + DEFAULT_SLASH + "foo.tpl" | |
267 | - * filename = "$variable" | |
268 | - * => template_dir + DEFAULT_SLASH + $variable | |
269 | - * filename = "$variable.tpl" | |
270 | - * => template_dir + DEFAULT_SLASH + $variable ".tpl" | |
271 | - * filename = "/path/to/foo.tpl" | |
272 | - * => "/path/to/foo.tpl" | |
273 | - * | |
274 | - * @param zval *obj instance of this class | |
275 | - * @param string template_dir template directory | |
276 | - * @param string filename filename | |
277 | - * | |
278 | - * @return string full path of the specified filename | |
279 | - */ | |
280 | -static string get_include_filename( | |
281 | - zval *obj, | |
282 | - string template_dir, | |
283 | - string filename TSRMLS_DC | |
284 | -) | |
285 | -{ | |
286 | - string included_filename; // return value | |
287 | - | |
288 | - // search "$" (use variable or don't use variable) | |
289 | - size_t pos = filename.find("$"); | |
290 | - if (pos != string::npos) { | |
291 | - // use variable | |
292 | - string include_variable = ""; | |
293 | - | |
294 | - // From "$" to "." string is cut out. | |
295 | - // $variable => $variable | |
296 | - // $variable.tpl => $variable | |
297 | - include_variable = filename.substr(pos); | |
298 | - size_t dot_pos = include_variable.find("."); | |
299 | - if (dot_pos != string::npos) { | |
300 | - include_variable = include_variable.substr(0, dot_pos); | |
301 | - } | |
302 | - | |
303 | -DEBUG_PRINTF("[B2]include_variable = (%s)", const_cast<char*>(include_variable.c_str())); | |
304 | - | |
305 | - // delete "$" | |
306 | - // $variable => variable | |
307 | - include_variable.erase(0, 1); | |
308 | - | |
309 | -#ifdef ZEND_ENGINE_2 | |
310 | - zval *_tpl_vars = zend_read_property(Z_OBJCE_P(obj), obj, const_cast<char*>("_tpl_vars"), strlen("_tpl_vars"), 1 TSRMLS_CC); | |
311 | -#else | |
312 | - zval *_tpl_vars; | |
313 | - if (zend_hash_find(Z_OBJPROP_P(obj), const_cast<char*>("_tpl_vars"), sizeof("_tpl_vars"), (void**)&_tpl_vars) != SUCCESS) { | |
314 | - zend_error(E_ERROR, "_tpl_vars not found from zend_hash."); | |
315 | - } | |
316 | -#endif // ZEND_ENGINE_2 | |
317 | - zval **zinclude_file; | |
318 | - string new_filename; | |
319 | - if (zend_hash_find(Z_ARRVAL_P(_tpl_vars), const_cast<char*>(include_variable.c_str()), include_variable.length() + 1, (void**)&zinclude_file) == SUCCESS | |
320 | - && Z_TYPE_PP(zinclude_file) == IS_STRING | |
321 | - ) { | |
322 | -DEBUG_PRINTF("(%s) -> (%s)", const_cast<char*>(include_variable.c_str()), Z_STRVAL_PP(zinclude_file)); | |
323 | - new_filename = filename.replace(filename.find("$"), include_variable.length() + 1, Z_STRVAL_PP(zinclude_file)); | |
324 | - } else { | |
325 | - zend_error(E_ERROR, "include file variable = ($%s) is not assigned.", include_variable.c_str()); | |
326 | - } | |
327 | - if (new_filename[0] == DEFAULT_SLASH) { | |
328 | - // use variable | |
329 | - // $variable = "/path/to/file.tpl" | |
330 | - included_filename = new_filename; | |
331 | - } else { | |
332 | - included_filename = template_dir + DEFAULT_SLASH + new_filename; | |
333 | - } | |
334 | - } else if (filename[0] == DEFAULT_SLASH) { | |
335 | - // don't use variable | |
336 | - // <{include file="/path/to/file.tpl"}> | |
337 | - included_filename = filename; | |
338 | - } else { | |
339 | - // don't use variable | |
340 | - // <{include file="file.tpl"}> | |
341 | - included_filename = template_dir + DEFAULT_SLASH + filename; | |
342 | - } | |
343 | - return included_filename; | |
344 | -} | |
345 | -/* }}} */ | |
346 | - | |
347 | -/* {{{ Read contents from the specified filename, and replaces xml start-end tags. | |
348 | - * | |
349 | - * <?xml | |
350 | - * => <?php echo'<?xml'; ?> | |
351 | - * ?> | |
352 | - * => <?php echo'?>'; ?> | |
353 | - * | |
354 | - * @param const char *filename filename | |
355 | - * | |
356 | - * @return string contents | |
357 | - */ | |
358 | -static string _readfile( | |
359 | - const char *filename TSRMLS_DC | |
360 | -) | |
361 | -{ | |
362 | - string line; | |
363 | - | |
364 | - // check open_basedir restriction in php.ini | |
365 | - if (php_check_open_basedir(filename TSRMLS_CC)) { | |
366 | - return ""; | |
367 | - } | |
368 | - | |
369 | -#ifdef PHP_WIN32 | |
370 | - php_stream *strm; | |
371 | - char *buffer = NULL; | |
372 | - size_t buffer_length = 0; | |
373 | - | |
374 | - strm = php_stream_open_wrapper(const_cast<char*>(filename), "rb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL); | |
375 | - if (!strm) { | |
376 | - zend_error(E_ERROR, "Cannot read such file or directory:%s", const_cast<char*>(filename)); | |
377 | - return ""; | |
378 | - } | |
379 | - | |
380 | - buffer_length = php_stream_copy_to_mem(strm, &buffer, PHP_STREAM_COPY_ALL, 0); | |
381 | - php_stream_close(strm); | |
382 | - if (buffer_length < 0) { | |
383 | - zend_error(E_ERROR, "Cannot read such file or directory:%s", const_cast<char*>(filename)); | |
384 | - efree(buffer); | |
385 | - return ""; | |
386 | - } | |
387 | - line = buffer; | |
388 | - efree(buffer); | |
389 | -#else // PHP_WIN32 | |
390 | - FILE *fp; | |
391 | - char buf[8192]; | |
392 | - fp = VCWD_FOPEN(filename, "rb"); | |
393 | - if (fp == NULL) { | |
394 | - zend_error(E_ERROR, "Cannot read such file or directory:%s", const_cast<char*>(filename)); | |
395 | - return ""; | |
396 | - } | |
397 | - | |
398 | - while (fgets(buf, sizeof(buf), fp) != 0) { | |
399 | - line += buf; | |
400 | - } | |
401 | - fclose(fp); | |
402 | -#endif // PHP_WIN32 | |
403 | - | |
404 | - // replace xml tag | |
405 | - // <?xml => <?php echo'<?xml'; ?> | |
406 | - // ?> => <?php echo'?>'; ?> | |
407 | - const char *xml_tag = "<?xml"; | |
408 | - size_t pos = 0; | |
409 | - string new_tag; | |
410 | - while ((pos = line.find(xml_tag, pos)) != string::npos) { | |
411 | - new_tag = "<?php echo \'"; | |
412 | - new_tag += xml_tag; | |
413 | - new_tag += "\'; ?>"; | |
414 | - line.replace(pos, strlen(xml_tag), new_tag); | |
415 | - pos += new_tag.length(); | |
416 | - | |
417 | - new_tag = "<?php echo \'?>\'; ?>"; | |
418 | - pos = line.find("?>", pos); | |
419 | - line.replace(pos, 2, new_tag); | |
420 | - pos += new_tag.length(); | |
421 | - } | |
422 | - | |
423 | - return line; | |
424 | -} | |
425 | -/* }}} */ | |
426 | - | |
427 | -/* {{{ Trims the specified string. | |
428 | - * | |
429 | - * @param const char *s string | |
430 | - * | |
431 | - * @return string trim string | |
432 | - */ | |
433 | -static string trim( | |
434 | - const char *s | |
435 | -) | |
436 | -{ | |
437 | - string new_str(s); | |
438 | - // trim from left-side | |
439 | - while (new_str[0] == ' ') { | |
440 | - new_str.erase(0, 1); | |
441 | - } | |
442 | - // trim from right-side | |
443 | - while (new_str[new_str.length() - 1] == ' ') { | |
444 | - new_str.erase(new_str.length() - 1, 1); | |
445 | - } | |
446 | - return new_str; | |
447 | -} | |
448 | -/* }}} */ | |
449 | - | |
450 | -/* {{{ Splits the specified string with the separator. The results is stored in the specified vector. | |
451 | - * | |
452 | - * @param const char *s string | |
453 | - * @param vector<string> &e vector | |
454 | - * @param char separator separator (= ".") | |
455 | - * | |
456 | - * @return void | |
457 | - */ | |
458 | -static void split_element( | |
459 | - const char *s, | |
460 | - vector<string> &e, | |
461 | - char separator = '.' | |
462 | -) | |
463 | -{ | |
464 | - string str(s); | |
465 | - size_t spos = 0, epos = 0; | |
466 | - e.push_back(str.substr(spos, str.find(separator, spos))); | |
467 | - while ((epos = str.find(separator, epos)) != string::npos) { | |
468 | - e.push_back(str.substr(epos + 1, str.find(separator, epos + 1) - epos - 1)); | |
469 | - epos++; | |
470 | - } | |
471 | -} | |
472 | -/* }}} */ | |
473 | - | |
474 | -/* {{{ Looks for [a-z0-9_] consecutive string from i position of the specified variable. The result is stored in the specified new_variable. | |
475 | - * | |
476 | - * @param const char *variable | |
477 | - * @param int i | |
478 | - * @param string &new_variable | |
479 | - * | |
480 | - * @return int length of [a-z0-9_] consecutive string | |
481 | - */ | |
482 | -static int get_identifier( | |
483 | - const char *variable, | |
484 | - int i, | |
485 | - string &new_variable | |
486 | -) | |
487 | -{ | |
488 | - int j = 0; | |
489 | - while (isalnum(variable[i]) || variable[i] == '_') { | |
490 | - new_variable += variable[i]; | |
491 | - i++; | |
492 | - j++; | |
493 | - } | |
494 | - return j; | |
495 | -} | |
496 | -/* }}} */ | |
497 | - | |
498 | -/* {{{ Parses the specified variable. | |
499 | - * | |
500 | - * array pattern | |
501 | - * $hoge[i] => _tpl_vars['hoge'][$i] | |
502 | - * $hoge[i].foo.bar => _tpl_vars['hoge'][$i]['foo']['bar'] | |
503 | - * $hoge[i].huga[j].foo => _tpl_vars['hoge'][$i]['huga'][$j]['foo'] | |
504 | - * $hoge[i]->huga[j]->foo => _tpl_vars['hoge'][$i]->huga[$j]->foo | |
505 | - * | |
506 | - * section pattern | |
507 | - * $simplate.section.i.index => $i | |
508 | - * $simplate.section.ary.count => count($this->_tpl_vars['ary']) | |
509 | - * $simplate.section.i.first => $this->_section['i']['first'] | |
510 | - * $simplate.section.i.last => $this->_section['i']['last'] | |
511 | - * | |
512 | - * arrow pattern | |
513 | - * $ary10.foo->bar => $this->_tpl_vars['ary10']['foo']->bar | |
514 | - * $foo->bar => $this->_tpl_vars['foo']->bar | |
515 | - * | |
516 | - * normal pattern | |
517 | - * $key => $this->_tpl_vars['key'] | |
518 | - * | |
519 | - * @param const char *variable variable string | |
520 | - * | |
521 | - * @return string parsed variable string | |
522 | - */ | |
523 | -static string parse_variable( | |
524 | - const char *variable | |
525 | -) | |
526 | -{ | |
527 | - string new_variable = ""; | |
528 | - | |
529 | -DEBUG_PRINTF("variable = (%s)", variable); | |
530 | - | |
531 | - if (variable[0] != '$' || strncmp(variable, "$this", 5) == 0) { | |
532 | - new_variable = variable; | |
533 | - | |
534 | -DEBUG_PRINTF("new_variable = (%s)", const_cast<char*>(new_variable.c_str())); | |
535 | - | |
536 | - return new_variable; | |
537 | - } | |
538 | - | |
539 | - if (strstr(variable, "[")) { | |
540 | - | |
541 | -DEBUG_PRINTF("%s", "array pattern"); | |
542 | - | |
543 | - // $hoge[i] => _tpl_vars['hoge'][$i] | |
544 | - // $hoge[i].foo.bar => _tpl_vars['hoge'][$i]['foo']['bar'] | |
545 | - // $hoge[i].huga[j].foo => _tpl_vars['hoge'][$i]['huga'][$j]['foo'] | |
546 | - // $hoge[i]->huga[j]->foo => _tpl_vars['hoge'][$i]->huga[$j]->foo | |
547 | - | |
548 | - int i = 0; | |
549 | - while (variable[i]) { | |
550 | - if (variable[i] == '$') { | |
551 | - new_variable += "$this->_tpl_vars['"; | |
552 | - i++; | |
553 | - i += get_identifier(variable, i, new_variable); | |
554 | - new_variable += "']"; | |
555 | - } else if (variable[i] == '.') { | |
556 | - new_variable += "['"; | |
557 | - i++; | |
558 | - i += get_identifier(variable, i, new_variable); | |
559 | - new_variable += "']"; | |
560 | - } else if (variable[i] == '[') { | |
561 | - new_variable += "[$"; | |
562 | - i++; | |
563 | - i += get_identifier(variable, i, new_variable); | |
564 | - new_variable += "]"; | |
565 | - i++; | |
566 | - } else if (i > 0 && variable[i] == '>' && variable[i - 1] == '-') { | |
567 | - new_variable += "->"; | |
568 | - i++; | |
569 | - i += get_identifier(variable, i, new_variable); | |
570 | - } else { | |
571 | - i++; | |
572 | - } | |
573 | - } | |
574 | - } else if (strstr(variable, ".")) { | |
575 | - | |
576 | -DEBUG_PRINTF("%s", "section pattern"); | |
577 | - | |
578 | - vector<string> elements; | |
579 | - split_element(variable + 1, elements); | |
580 | - | |
581 | -#ifdef SIMPLATE_DEBUG | |
582 | -for (vector<string>::iterator i = elements.begin(); i != elements.end(); ++i) { | |
583 | -DEBUG_PRINTF("vector(i) = (%s)", const_cast<char*>((*i).c_str())); | |
584 | -} | |
585 | -#endif // SIMPLATE_DEBUG | |
586 | - | |
587 | - // $simplate.section.i.index => $i | |
588 | - // $simplate.section.ary.count => count($this->_tpl_vars['ary']) | |
589 | - // $simplate.section.i.first => $this->_section['i']['first'] | |
590 | - // $simplate.section.i.last => $this->_section['i']['last'] | |
591 | - // $ary10.foo->bar => $this->_tpl_vars["ary10"]["foo"]->bar | |
592 | - if (elements.size() == 4 && elements[1] == "section" && elements[3] == "index") { | |
593 | - new_variable += "$"; | |
594 | - new_variable += elements[2]; | |
595 | - } else if (elements.size() == 4 && elements[1] == "section" && elements[3] == "count") { | |
596 | - new_variable += "count($this->_tpl_vars['"; | |
597 | - new_variable += elements[2]; | |
598 | - new_variable += "'])"; | |
599 | - } else if (elements.size() == 4 && elements[1] == "section" && (elements[3] == "first"|| elements[3] == "last")) { | |
600 | - new_variable += "$this->_section['"; | |
601 | - new_variable += elements[2]; | |
602 | - new_variable += "']['"; | |
603 | - new_variable += elements[3]; | |
604 | - new_variable += "']"; | |
605 | - } else { | |
606 | - new_variable += "$this->_tpl_vars"; | |
607 | - for (vector<string>::iterator i = elements.begin(); i != elements.end(); ++i) { | |
608 | - new_variable += "['"; | |
609 | - | |
610 | - size_t pos = 0; | |
611 | - if ((pos = (*i).find("->")) != string::npos) { | |
612 | - new_variable += (*i).substr(0, pos); | |
613 | - new_variable += "']"; | |
614 | - new_variable += (*i).substr(pos); | |
615 | - } else { | |
616 | - new_variable += *i; | |
617 | - new_variable += "']"; | |
618 | - } | |
619 | - } | |
620 | - } | |
621 | - } else if (strstr(variable, "->")) { | |
622 | - | |
623 | -DEBUG_PRINTF("%s", "arrow pattern"); | |
624 | - | |
625 | - // $foo->bar => $this->_tpl_vars['foo']->bar | |
626 | - string temp = variable; | |
627 | - new_variable += "$this->_tpl_vars['"; | |
628 | - new_variable += temp.substr(1, temp.find("->") - 1); | |
629 | - new_variable += "']"; | |
630 | - new_variable += temp.substr(temp.find("->")); | |
631 | - } else { | |
632 | - | |
633 | -DEBUG_PRINTF("%s", "normal pattern"); | |
634 | - | |
635 | - // $key => $this->_tpl_vars['key'] | |
636 | - new_variable += "$this->_tpl_vars['"; | |
637 | - new_variable += (variable + 1); | |
638 | - new_variable += "']"; | |
639 | - } | |
640 | - | |
641 | -DEBUG_PRINTF("new_variable = (%s)", const_cast<char*>(new_variable.c_str())); | |
642 | - | |
643 | - return new_variable; | |
644 | -} | |
645 | -/* }}} */ | |
646 | - | |
647 | -/* {{{ Returns the value of the simplate tag's attribute to which specified key is coinciding | |
648 | - * | |
649 | - * @param const char *s simplate tag | |
650 | - * @param const char *key attribute name | |
651 | - * | |
652 | - * @return string the value of simplate tag's attribute | |
653 | - */ | |
654 | -static string get_element( | |
655 | - const char *s, | |
656 | - const char *key | |
657 | -) | |
658 | -{ | |
659 | - int i; | |
660 | - string value = ""; | |
661 | - string key_name; | |
662 | - | |
663 | -DEBUG_PRINTF("s = (%s), key = (%s)", s, key); | |
664 | - | |
665 | - while ((s = strstr(s, key)) != NULL) { | |
666 | - i = 0; | |
667 | - key_name = ""; | |
668 | - while (isalnum(*(s + i))) { | |
669 | - key_name += *(s + i); | |
670 | - i++; | |
671 | - } | |
672 | - if (strcmp(key_name.c_str(), key) == 0) { | |
673 | - while (*(s + i) != '=') { | |
674 | - i++; | |
675 | - } | |
676 | - i++; | |
677 | - while (*(s + i) == '\'' || *(s + i) == '"' || *(s + i) == ' ') { | |
678 | - i++; | |
679 | - } | |
680 | - while (*(s + i) != '\'' && *(s + i) != '"' && *(s + i) != ' ' && *(s + i)) { | |
681 | - value += *(s + i); | |
682 | - i++; | |
683 | - } | |
684 | - break; | |
685 | - } | |
686 | - s++; | |
687 | - } | |
688 | - | |
689 | -DEBUG_PRINTF("value = (%s)", const_cast<char*>(value.c_str())); | |
690 | - | |
691 | - return value; | |
692 | -} | |
693 | -/* }}} */ | |
694 | - | |
695 | -/* {{{ Returns true if the specified file has compiled. | |
696 | - * | |
697 | - * @param zval *obj instance of this | |
698 | - * @param const char *template_dir template directory | |
699 | - * @param const char *compile_dir compile direcotry | |
700 | - * @param const char *filename target file | |
701 | - * @param const char *left_delimiter left-delimiter | |
702 | - * @param const char *right_delimiter right-delimiter | |
703 | - * | |
704 | - * @return boolean returns true if the specified file has compiled | |
705 | - */ | |
706 | -static inline bool _is_compiled( | |
707 | - zval *obj, | |
708 | - const char *template_dir, | |
709 | - const char *compile_dir, | |
710 | - const char *filename, | |
711 | - const char *left_delimiter, | |
712 | - const char *right_delimiter TSRMLS_DC | |
713 | -) | |
714 | -{ | |
715 | - string error; | |
716 | - unsigned char compile_check = DEFAULT_COMPILE_CHECK; | |
717 | - unsigned char force_compile = 0; | |
718 | - unsigned char lazy_check = 0; | |
719 | - | |
720 | -#ifdef ZEND_ENGINE_2 | |
721 | - zval *temp_zval; | |
722 | - | |
723 | - // Reads property COMPILE_CHECK | |
724 | - temp_zval = zend_read_property(Z_OBJCE_P(obj), obj, const_cast<char*>(COMPILE_CHECK), strlen(COMPILE_CHECK), 1 TSRMLS_CC); | |
725 | - if (Z_TYPE_P(temp_zval) == IS_BOOL) { | |
726 | - compile_check = Z_BVAL_P(temp_zval); | |
727 | - } | |
728 | - | |
729 | - // Reads property FORCE_COMPILE | |
730 | - temp_zval = zend_read_property(Z_OBJCE_P(obj), obj, const_cast<char*>(FORCE_COMPILE), strlen(FORCE_COMPILE), 1 TSRMLS_CC); | |
731 | - if (Z_TYPE_P(temp_zval) == IS_BOOL) { | |
732 | - force_compile = Z_BVAL_P(temp_zval); | |
733 | - } | |
734 | - | |
735 | - // Reads property LAZY_CHECK | |
736 | - temp_zval = zend_read_property(Z_OBJCE_P(obj), obj, const_cast<char*>(LAZY_CHECK), strlen(LAZY_CHECK), 1 TSRMLS_CC); | |
737 | - if (Z_TYPE_P(temp_zval) == IS_BOOL) { | |
738 | - lazy_check = Z_BVAL_P(temp_zval); | |
739 | - } | |
740 | -#else | |
741 | - zval **temp; | |
742 | - | |
743 | - // Reads property COMPILE_CHECK | |
744 | - if (zend_hash_find(Z_OBJPROP_P(obj), const_cast<char*>(COMPILE_CHECK), sizeof(COMPILE_CHECK), (void**)&temp) == SUCCESS) { | |
745 | - if (Z_TYPE_PP(temp) == IS_BOOL) { | |
746 | - compile_check = Z_BVAL_PP(temp); | |
747 | - } | |
748 | - } | |
749 | - | |
750 | - // Reads property FORCE_COMPILE | |
751 | - if (zend_hash_find(Z_OBJPROP_P(obj), const_cast<char*>(FORCE_COMPILE), sizeof(FORCE_COMPILE), (void**)&temp) == SUCCESS) { | |
752 | - if (Z_TYPE_PP(temp) == IS_BOOL) { | |
753 | - force_compile = Z_BVAL_PP(temp); | |
754 | - } | |
755 | - } | |
756 | - | |
757 | - // Reads property LAZY_CHECK | |
758 | - if (zend_hash_find(Z_OBJPROP_P(obj), const_cast<char*>(LAZY_CHECK), sizeof(LAZY_CHECK), (void**)&temp) == SUCCESS) { | |
759 | - if (Z_TYPE_PP(temp) == IS_BOOL) { | |
760 | - lazy_check = Z_BVAL_PP(temp); | |
761 | - } | |
762 | - } | |
763 | -#endif // ZEND_ENGINE_2 | |
764 | - | |
765 | -DEBUG_PRINTF("compile_check = (%d)", compile_check); | |
766 | -DEBUG_PRINTF("force_compile = (%d)", force_compile); | |
767 | -DEBUG_PRINTF("lazy_check = (%d)", lazy_check); | |
768 | - | |
769 | - // Returns false if force_compile is true. | |
770 | - if (force_compile) { | |
771 | - return false; | |
772 | - } | |
773 | - | |
774 | - // Has the compiled file already exists? | |
775 | - struct stat cs; | |
776 | - string full_compile_filename(compile_dir); | |
777 | - full_compile_filename += DEFAULT_SLASH; | |
778 | - full_compile_filename += filename; | |
779 | - if (VCWD_STAT(full_compile_filename.c_str(), &cs) == -1) { | |
780 | - // the compiled file doesn't exists. | |
781 | - return false; | |
782 | - } | |
783 | - | |
784 | - // Returns true if compile_check is false. | |
785 | - if (!compile_check) { | |
786 | - return true; | |
787 | - } | |
788 | - | |
789 | - // compare the file and compiled file. | |
790 | - struct stat ts; | |
791 | - string full_template_filename(template_dir); | |
792 | - full_template_filename += DEFAULT_SLASH; | |
793 | - full_template_filename += filename; | |
794 | - | |
795 | - if (VCWD_STAT(full_template_filename.c_str(), &ts) == -1) { | |
796 | - zend_error(E_ERROR, "cannot stat:%s", full_template_filename.c_str()); | |
797 | - return false; | |
798 | - } | |
799 | - | |
800 | -DEBUG_PRINTF("template_filename = (%s)", full_template_filename.c_str()); | |
801 | -DEBUG_PRINTF("compiled_filename = (%s)", full_compile_filename.c_str()); | |
802 | -DEBUG_PRINTF("cs.st_mtime = (%ld), ts.st_mtime = (%ld)", cs.st_mtime, ts.st_mtime); | |
803 | - | |
804 | - // Returns false if template file is newer than compiled file. | |
805 | - if (cs.st_mtime < ts.st_mtime) { | |
806 | - return false; | |
807 | - } else { | |
808 | - // Not checks the included file if lazy check is true. | |
809 | - if (!lazy_check) { | |
810 | - | |
811 | - size_t pos = 0; | |
812 | - size_t tag_start_pos, tag_end_pos; | |
813 | - size_t element_start_pos, element_end_pos; | |
814 | - string item; | |
815 | - string file_content = _readfile(full_template_filename.c_str() TSRMLS_CC); | |
816 | - | |
817 | - // Reads include tags. | |
818 | - while ((pos = file_content.find(left_delimiter, pos)) != string::npos) { | |
819 | - tag_start_pos = pos; | |
820 | - element_start_pos = tag_start_pos + strlen(left_delimiter); | |
821 | - element_end_pos = file_content.find(right_delimiter, tag_start_pos); | |
822 | - if (element_end_pos == string::npos) { | |
823 | - zend_error(E_ERROR, "No closed delimiter:`%s' in %s", right_delimiter, full_template_filename.c_str()); | |
824 | - return false; | |
825 | - } | |
826 | - tag_end_pos = element_end_pos + strlen(right_delimiter); | |
827 | - | |
828 | - // Reads between left_delimiter and right_delimiter. | |
829 | - item.assign(file_content, element_start_pos, element_end_pos - element_start_pos); | |
830 | - item = trim(item.c_str()); | |
831 | - | |
832 | - // Finds include tag. | |
833 | - if (item.substr(0, 7) == "include") { | |
834 | - | |
835 | -DEBUG_PRINTF("item = (%s)", item.c_str()); | |
836 | - | |
837 | - string filename = get_element(item.c_str(), "file"); | |
838 | - string included_filename = get_include_filename(obj, template_dir, filename TSRMLS_CC); | |
839 | - | |
840 | - // Check included template file timestamp | |
841 | - struct stat is; | |
842 | - if (VCWD_STAT(included_filename.c_str(), &is) == -1) { | |
843 | - zend_error(E_ERROR, "cannot stat:%s", included_filename.c_str()); | |
844 | - return false; | |
845 | - } | |
846 | - | |
847 | -DEBUG_PRINTF("included_filename = (%s)", included_filename.c_str()); | |
848 | -DEBUG_PRINTF("cs.st_mtime = (%ld), is.st_mtime = (%ld)", cs.st_mtime, is.st_mtime); | |
849 | - // Returns false if included file is newer than compiled file. | |
850 | - if (cs.st_mtime < is.st_mtime) { | |
851 | - return false; | |
852 | - } | |
853 | - | |
854 | - // Reads included file. | |
855 | - string new_condition = _readfile(included_filename.c_str() TSRMLS_CC); | |
856 | - | |
857 | - // Needs this check ? | |
858 | - if (new_condition.find(filename) != string::npos) { | |
859 | - zend_error(E_ERROR, "Found recursive include:%s", filename.c_str()); | |
860 | - return ""; | |
861 | - } | |
862 | - file_content.replace(tag_start_pos, tag_end_pos - tag_start_pos, new_condition); | |
863 | - continue; | |
864 | - } | |
865 | - pos++; | |
866 | - } | |
867 | - } // lazy check | |
868 | - } | |
869 | - | |
870 | - return true; | |
871 | -} | |
872 | -/* }}} */ | |
873 | - | |
874 | -/* {{{ Parses and compiles the specified temaplte file. | |
875 | - * | |
876 | - * @param INTERNAL_FUNCTION_PARAMETERS see zend.h | |
877 | - * @param char **fullfile_name returns full path of template file | |
878 | - * @param int mode SIMPLATE_FETCH or SIMPLATE_DISPLAY | |
879 | - * @param char **cache_content returns results (use fetch mode) | |
880 | - * | |
881 | - * @return void | |
882 | - */ | |
883 | -void read_parse_template( | |
884 | - INTERNAL_FUNCTION_PARAMETERS, | |
885 | - char **fullfile_name, | |
886 | - int mode, | |
887 | - char **cache_content = NULL | |
888 | -) | |
889 | -{ | |
890 | - zval *obj; | |
891 | - char *resource_name = NULL; | |
892 | - int resource_name_len = 0; | |
893 | - string template_dir; | |
894 | - char *compile_dir = NULL; | |
895 | - char *left_delimiter = NULL; | |
896 | - char *right_delimiter = NULL; | |
897 | - long caching = 0; | |
898 | - long cache_lifetime = 0; | |
899 | - size_t i = 0; | |
900 | - string error; | |
901 | - | |
902 | - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, const_cast<char*>("s"), &resource_name, &resource_name_len) == FAILURE) { | |
903 | - return; | |
904 | - } | |
905 | - | |
906 | -DEBUG_PRINTF("resource_name = (%s)", resource_name); | |
907 | - | |
908 | - obj = getThis(); | |
909 | -#ifdef ZEND_ENGINE_2 | |
910 | - zval *temp_zval; | |
911 | - temp_zval = zend_read_property(Z_OBJCE_P(obj), obj, const_cast<char*>(TEMPLATE_DIR), strlen(TEMPLATE_DIR), 1 TSRMLS_CC); | |
912 | - if (Z_TYPE_P(temp_zval) == IS_STRING) { | |
913 | - template_dir = Z_STRVAL_P(temp_zval); | |
914 | - } | |
915 | - temp_zval = zend_read_property(Z_OBJCE_P(obj), obj, const_cast<char*>(COMPILE_DIR), strlen(COMPILE_DIR), 1 TSRMLS_CC); | |
916 | - if (Z_TYPE_P(temp_zval) == IS_STRING) { | |
917 | - compile_dir = Z_STRVAL_P(temp_zval); | |
918 | - } | |
919 | - temp_zval = zend_read_property(Z_OBJCE_P(obj), obj, const_cast<char*>(LEFT_DELIMITER), strlen(LEFT_DELIMITER), 1 TSRMLS_CC); | |
920 | - if (Z_TYPE_P(temp_zval) == IS_STRING) { | |
921 | - left_delimiter = Z_STRVAL_P(temp_zval); | |
922 | - } | |
923 | - temp_zval = zend_read_property(Z_OBJCE_P(obj), obj, const_cast<char*>(RIGHT_DELIMITER), strlen(RIGHT_DELIMITER), 1 TSRMLS_CC); | |
924 | - if (Z_TYPE_P(temp_zval) == IS_STRING) { | |
925 | - right_delimiter = Z_STRVAL_P(temp_zval); | |
926 | - } | |
927 | - temp_zval = zend_read_property(Z_OBJCE_P(obj), obj, const_cast<char*>(CACHING), strlen(CACHING), 1 TSRMLS_CC); | |
928 | - if (Z_TYPE_P(temp_zval) == IS_LONG) { | |
929 | - caching = Z_LVAL_P(temp_zval); | |
930 | - } | |
931 | - temp_zval = zend_read_property(Z_OBJCE_P(obj), obj, const_cast<char*>(CACHE_LIFETIME), strlen(CACHE_LIFETIME), 1 TSRMLS_CC); | |
932 | - if (Z_TYPE_P(temp_zval) == IS_LONG) { | |
933 | - cache_lifetime = Z_LVAL_P(temp_zval); | |
934 | - } | |
935 | -#else | |
936 | - zval **temp_zval; | |
937 | - | |
938 | - // Logic to implement var_dump($this) for PHP4 | |
939 | - // $this = &$smarty; | |
940 | - // zend_hash_update(&EG(symbol_table), "this", strlen("this") + 1, &obj, sizeof(zval*), NULL); | |
941 | - | |
942 | - if (zend_hash_find(Z_OBJPROP_P(obj), TEMPLATE_DIR, sizeof(TEMPLATE_DIR), (void**)&temp_zval) == SUCCESS) { | |
943 | - if ((*temp_zval)->type == IS_STRING) { | |
944 | - template_dir = Z_STRVAL_PP(temp_zval); | |
945 | - } | |
946 | - } | |
947 | - if (zend_hash_find(Z_OBJPROP_P(obj), COMPILE_DIR, sizeof(COMPILE_DIR), (void**)&temp_zval) == SUCCESS) { | |
948 | - if ((*temp_zval)->type == IS_STRING) { | |
949 | - compile_dir = Z_STRVAL_PP(temp_zval); | |
950 | - } | |
951 | - } | |
952 | - if (zend_hash_find(Z_OBJPROP_P(obj), LEFT_DELIMITER, sizeof(LEFT_DELIMITER), (void**)&temp_zval) == SUCCESS) { | |
953 | - if ((*temp_zval)->type == IS_STRING) { | |
954 | - left_delimiter = Z_STRVAL_PP(temp_zval); | |
955 | - } | |
956 | - } | |
957 | - if (zend_hash_find(Z_OBJPROP_P(obj), RIGHT_DELIMITER, sizeof(RIGHT_DELIMITER), (void**)&temp_zval) == SUCCESS) { | |
958 | - if ((*temp_zval)->type == IS_STRING) { | |
959 | - right_delimiter = Z_STRVAL_PP(temp_zval); | |
960 | - } | |
961 | - } | |
962 | - if (zend_hash_find(Z_OBJPROP_P(obj), CACHING, sizeof(CACHING), (void**)&temp_zval) == SUCCESS) { | |
963 | - if ((*temp_zval)->type == IS_LONG) { | |
964 | - caching = Z_LVAL_PP(temp_zval); | |
965 | - } | |
966 | - } | |
967 | - if (zend_hash_find(Z_OBJPROP_P(obj), CACHE_LIFETIME, sizeof(CACHE_LIFETIME), (void**)&temp_zval) == SUCCESS) { | |
968 | - if ((*temp_zval)->type == IS_LONG) { | |
969 | - cache_lifetime = Z_LVAL_PP(temp_zval); | |
970 | - } | |
971 | - } | |
972 | -#endif // ZEND_ENGINE_2 | |
973 | - | |
974 | -DEBUG_PRINTF("template_dir = (%s)", template_dir.c_str()); | |
975 | -DEBUG_PRINTF("compile_dir = (%s)", compile_dir); | |
976 | -DEBUG_PRINTF("left_delimiter = (%s)", left_delimiter); | |
977 | -DEBUG_PRINTF("right_delimiter = (%s)", right_delimiter); | |
978 | -DEBUG_PRINTF("caching = (%ld)", caching); | |
979 | -DEBUG_PRINTF("cache_lifetime = (%ld)", cache_lifetime); | |
980 | - | |
981 | - if (template_dir[template_dir.length() - 1] == DEFAULT_SLASH) { | |
982 | - template_dir.erase(template_dir.length() - 1, 1); | |
983 | - } | |
984 | - string full_template_filename(template_dir); | |
985 | - full_template_filename += DEFAULT_SLASH; | |
986 | - full_template_filename += resource_name; | |
987 | - | |
988 | -DEBUG_PRINTF("full_template_filename = (%s)", full_template_filename.c_str()); | |
989 | - | |
990 | - string full_compile_filename(compile_dir); | |
991 | - if (full_compile_filename[full_compile_filename.length() - 1] != DEFAULT_SLASH) { | |
992 | - full_compile_filename += DEFAULT_SLASH; | |
993 | - } | |
994 | - full_compile_filename += resource_name; | |
995 | - | |
996 | -DEBUG_PRINTF("full_compile_filename = (%s)", full_compile_filename.c_str()); | |
997 | - | |
998 | - if (caching) { | |
999 | - struct stat cache_stat; | |
1000 | - char *cache_dir; | |
1001 | - unsigned char compile_check; | |
1002 | - unsigned char force_compile; | |
1003 | -#ifdef ZEND_ENGINE_2 | |
1004 | - cache_dir = Z_STRVAL_P(zend_read_property(Z_OBJCE_P(obj), obj, const_cast<char*>(CACHE_DIR), strlen(CACHE_DIR), 1 TSRMLS_CC)); | |
1005 | - compile_check = Z_BVAL_P(zend_read_property(Z_OBJCE_P(obj), obj, const_cast<char*>(COMPILE_CHECK), strlen(COMPILE_CHECK), 1 TSRMLS_CC)); | |
1006 | - force_compile = Z_BVAL_P(zend_read_property(Z_OBJCE_P(obj), obj, const_cast<char*>(FORCE_COMPILE), strlen(FORCE_COMPILE), 1 TSRMLS_CC)); | |
1007 | -#else | |
1008 | -#endif // ZEND_ENGINE_2 | |
1009 | - if (cache_dir[strlen(cache_dir) - 1] == DEFAULT_SLASH) { | |
1010 | - cache_dir[strlen(cache_dir) - 1] = '\0'; | |
1011 | - } | |
1012 | - // cache directory exists? | |
1013 | - if (VCWD_STAT(cache_dir, &cache_stat) != -1) { | |
1014 | - if (!S_ISDIR(cache_stat.st_mode)) { | |
1015 | - zend_error(E_ERROR, "does not exist cache directory: %s", cache_dir); | |
1016 | - return; | |
1017 | - } | |
1018 | - } | |
1019 | - string full_cache_filename(cache_dir); | |
1020 | - full_cache_filename += DEFAULT_SLASH; | |
1021 | - full_cache_filename += resource_name; | |
1022 | - | |
1023 | -DEBUG_PRINTF("full_cache_filename = (%s)", full_cache_filename.c_str()); | |
1024 | - | |
1025 | - // there are no cache file. | |
1026 | - if (VCWD_STAT(full_cache_filename.c_str(), &cache_stat) == -1 || force_compile) { | |
1027 | - | |
1028 | -create_cache: | |
1029 | - if (VCWD_STAT(full_compile_filename.c_str(), &cache_stat) == -1) { | |
1030 | - goto compile; | |
1031 | - } | |
1032 | - | |
1033 | - zend_file_handle file_handle; | |
1034 | - zend_op_array *op_array; | |
1035 | - file_handle.filename = const_cast<char*>(full_compile_filename.c_str()); | |
1036 | - file_handle.free_filename = 0; | |
1037 | - file_handle.type = ZEND_HANDLE_FILENAME; | |
1038 | - file_handle.opened_path = NULL; | |
1039 | - op_array = zend_compile_file(&file_handle, ZEND_INCLUDE TSRMLS_CC); | |
1040 | - if (!op_array) { | |
1041 | - zend_error(E_ERROR, "Error parsing script:%s", full_compile_filename.c_str()); | |
1042 | - return; | |
1043 | - } | |
1044 | - zend_destroy_file_handle(&file_handle TSRMLS_CC); | |
1045 | - | |
1046 | - SIMPLATE_G(global_string.str(std::string())); // let global_string empty. | |
1047 | - int (*old_output_func)(const char*, unsigned int TSRMLS_DC); | |
1048 | - old_output_func = OG(php_body_write); | |
1049 | - OG(php_body_write) = php_my_output_func; | |
1050 | - zend_execute(op_array TSRMLS_CC); | |
1051 | - OG(php_body_write) = old_output_func; | |
1052 | - | |
1053 | -#ifdef ZEND_ENGINE_2 | |
1054 | - destroy_op_array(op_array TSRMLS_CC); | |
1055 | -#else | |
1056 | - destroy_op_array(op_array); | |
1057 | -#endif // ZEND_ENGINE_2 | |
1058 | - efree(op_array); | |
1059 | - | |
1060 | -DEBUG_PRINTF("SIMPLATE_G(global_string) = (%ld)", SIMPLATE_G(global_string).str().length()); | |
1061 | - | |
1062 | - if (mode == SIMPLATE_FETCH) { | |
1063 | - *cache_content = estrndup(SIMPLATE_G(global_string).str().c_str(), SIMPLATE_G(global_string).str().length()); | |
1064 | - } else { | |
1065 | - if (SIMPLATE_G(global_string).str().length() > 0) { | |
1066 | - zend_printf("%s", SIMPLATE_G(global_string).str().c_str()); | |
1067 | - } | |
1068 | - } | |
1069 | - | |
1070 | -#ifdef PHP_WIN32 | |
1071 | - php_stream *strm = php_stream_open_wrapper(const_cast<char*>(full_cache_filename.c_str()), "wb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL); | |
1072 | - if (strm) { | |
1073 | - if (php_stream_supports_lock(strm)) { | |
1074 | - php_stream_lock(strm, LOCK_EX); | |
1075 | - } | |
1076 | - if (SIMPLATE_G(global_string).str().length() > 0) { | |
1077 | - php_stream_write_string(strm, const_cast<char*>(SIMPLATE_G(global_string).str().c_str())); | |
1078 | - } | |
1079 | - if (php_stream_supports_lock(strm)) { | |
1080 | - php_stream_lock(strm, LOCK_UN); | |
1081 | - } | |
1082 | - php_stream_close(strm); | |
1083 | - } | |
1084 | -#else | |
1085 | - FILE *fp = VCWD_FOPEN(full_cache_filename.c_str(), "wb"); | |
1086 | - if (fp == NULL) { | |
1087 | - // fail to create cache | |
1088 | - zend_error(E_ERROR, "fail to create cache:%s", full_cache_filename.c_str()); | |
1089 | - return; | |
1090 | - } | |
1091 | - if (fwrite(SIMPLATE_G(global_string).str().c_str(), 1, SIMPLATE_G(global_string).str().length(), fp) != SIMPLATE_G(global_string).str().length()) { | |
1092 | - // fail to write cache | |
1093 | - zend_error(E_ERROR, "fail to write cache:%s", full_cache_filename.c_str()); | |
1094 | - // Notice: don't retun, yet. | |
1095 | - } | |
1096 | - fclose(fp); | |
1097 | -#endif // PHP_WIN32 | |
1098 | - | |
1099 | - } else { | |
1100 | - | |
1101 | -DEBUG_PRINTF("%s", "cache file exists."); | |
1102 | - | |
1103 | - if (compile_check) { | |
1104 | - | |
1105 | -DEBUG_PRINTF("%s", "check cache file is compiled."); | |
1106 | - | |
1107 | - // check cache filestamp | |
1108 | - time_t now; | |
1109 | - time(&now); | |
1110 | - if (now - cache_stat.st_mtime > cache_lifetime) { | |
1111 | -DEBUG_PRINTF("%s", "cache file is not compiled."); | |
1112 | - goto create_cache; | |
1113 | - } | |
1114 | - } | |
1115 | - | |
1116 | - string file_contents = _readfile(full_cache_filename.c_str() TSRMLS_CC); | |
1117 | - if (mode == SIMPLATE_FETCH) { | |
1118 | - *cache_content = estrndup(file_contents.c_str(), file_contents.length()); | |
1119 | - } else { | |
1120 | - zend_printf("%s", file_contents.c_str()); | |
1121 | - } | |
1122 | - } | |
1123 | -DEBUG_PRINTF("%s", "end function"); | |
1124 | - | |
1125 | - return; | |
1126 | - } | |
1127 | - | |
1128 | - // the directory for compiled templates exists? | |
1129 | - struct stat compile_dir_stat; | |
1130 | - if (VCWD_STAT(compile_dir, &compile_dir_stat) != -1) { | |
1131 | - if (!S_ISDIR(compile_dir_stat.st_mode)) { | |
1132 | - zend_error(E_ERROR, "%s is not directory", compile_dir); | |
1133 | - return; | |
1134 | - } | |
1135 | - } else { | |
1136 | - // mkdir recursively | |
1137 | - php_stream_context *context = NULL; | |
1138 | - if (!php_stream_mkdir(compile_dir, 0777, (PHP_STREAM_MKDIR_RECURSIVE | REPORT_ERRORS), context)) { | |
1139 | - zend_error(E_ERROR, "fail to mkdir (%s)", compile_dir); | |
1140 | - return; | |
1141 | - } | |
1142 | - } | |
1143 | - | |
1144 | - // already compiled | |
1145 | - if (_is_compiled(obj, template_dir.c_str(), compile_dir, resource_name, left_delimiter, right_delimiter TSRMLS_CC)) { | |
1146 | - | |
1147 | - } else { | |
1148 | -compile: | |
1149 | - // compile now | |
1150 | - string compiled_file_content = _readfile(full_template_filename.c_str() TSRMLS_CC); | |
1151 | - | |
1152 | - // prefilter | |
1153 | - zval *plugins = zend_read_property(Z_OBJCE_P(obj), obj, const_cast<char*>("_plugins"), strlen("_plugins"), 1 TSRMLS_CC); | |
1154 | - if (plugins && Z_TYPE_P(plugins) == IS_ARRAY) { | |
1155 | - zval **prefilter; | |
1156 | - if (zend_hash_find(Z_ARRVAL_P(plugins), "prefilter", sizeof("prefilter"), (void**)&prefilter) == SUCCESS | |
1157 | - && Z_TYPE_PP(prefilter) == IS_ARRAY | |
1158 | - ) { | |
1159 | - zval **elem; | |
1160 | - zend_hash_internal_pointer_reset(Z_ARRVAL_PP(prefilter)); | |
1161 | - while (zend_hash_get_current_data(Z_ARRVAL_PP(prefilter), (void**)&elem) == SUCCESS) { | |
1162 | - | |
1163 | -DEBUG_PRINTF("prefilter function = (%s)", Z_STRVAL_PP(elem)); | |
1164 | - | |
1165 | - zval prefilter_function, prefilter_ret; | |
1166 | - zval zcontent; | |
1167 | - zval *prefilter_argv[1]; | |
1168 | - prefilter_argv[0] = &zcontent; | |
1169 | - SET_ZVAL_STRING(zcontent, compiled_file_content.c_str()); | |
1170 | - | |
1171 | - INIT_ZVAL(prefilter_function); | |
1172 | - ZVAL_STRING(&prefilter_function, Z_STRVAL_PP(elem), 1); | |
1173 | - | |
1174 | - if (call_user_function(EG(function_table), NULL, &prefilter_function, &prefilter_ret, 1, prefilter_argv TSRMLS_CC) == FAILURE) { | |
1175 | - zval_dtor(&zcontent); | |
1176 | - zend_error(E_ERROR, "fail to %s", Z_STRVAL_PP(elem)); | |
1177 | - return; | |
1178 | - } | |
1179 | - zval_dtor(&prefilter_function); | |
1180 | - zval_dtor(&zcontent); | |
1181 | - compiled_file_content = Z_STRVAL(prefilter_ret); | |
1182 | - | |
1183 | -DEBUG_PRINTF("compiled_file_content = (%s)", compiled_file_content.c_str()); | |
1184 | - | |
1185 | - zval_dtor(&prefilter_ret); | |
1186 | - zend_hash_move_forward(Z_ARRVAL_PP(prefilter)); | |
1187 | - } | |
1188 | - } | |
1189 | - } | |
1190 | - | |
1191 | - size_t pos = 0; | |
1192 | - size_t tag_start_pos, tag_end_pos; | |
1193 | - size_t element_start_pos, element_end_pos; | |
1194 | - string item; | |
1195 | - char end_comment_tag[12]; | |
1196 | - string new_condition; | |
1197 | - sprintf(end_comment_tag, "*%s", right_delimiter); | |
1198 | - while ((pos = compiled_file_content.find(left_delimiter, pos)) != string::npos) { | |
1199 | - | |
1200 | - // <{$hoge}> | |
1201 | - // ^ :tag_start_pos | |
1202 | - // ^ :element_start_pos | |
1203 | - // ^ :element_end_pos | |
1204 | - // ^:tag_end_pos | |
1205 | - // ^---^ :item | |
1206 | - | |
1207 | - tag_start_pos = pos; | |
1208 | - element_start_pos = tag_start_pos + strlen(left_delimiter); | |
1209 | - element_end_pos = compiled_file_content.find(right_delimiter, tag_start_pos); | |
1210 | - if (element_end_pos == string::npos) { | |
1211 | - zend_error(E_ERROR, "No closed delimiter:`%s' in %s", right_delimiter, full_template_filename.c_str()); | |
1212 | - return; | |
1213 | - } | |
1214 | - tag_end_pos = element_end_pos + strlen(right_delimiter); | |
1215 | - | |
1216 | - item.assign(compiled_file_content, element_start_pos, element_end_pos - element_start_pos); | |
1217 | - item = trim(item.c_str()); | |
1218 | - string variable; | |
1219 | - string new_variable; | |
1220 | - | |
1221 | -DEBUG_PRINTF("item = (%s)", item.c_str()); | |
1222 | - | |
1223 | - if (item[0] == '$') { | |
1224 | - | |
1225 | -DEBUG_PRINTF("variable item = (%s)", item.c_str()); | |
1226 | - | |
1227 | -cond_var: | |
1228 | - new_condition = "<?php echo "; | |
1229 | - i = 0; | |
1230 | - | |
1231 | - while (element_start_pos + i < element_end_pos) { | |
1232 | - if (compiled_file_content[element_start_pos + i] == '+' | |
1233 | - || (compiled_file_content[element_start_pos + i] == '-' | |
1234 | - && compiled_file_content.at(element_start_pos + i + 1) != '>') | |
1235 | - || compiled_file_content[element_start_pos + i] == '*' | |
1236 | - || compiled_file_content[element_start_pos + i] == '/' | |
1237 | - || compiled_file_content[element_start_pos + i] == '%' | |
1238 | - || compiled_file_content[element_start_pos + i] == '(' | |
1239 | - || compiled_file_content[element_start_pos + i] == ')' | |
1240 | - || compiled_file_content[element_start_pos + i] == ',' | |
1241 | - || compiled_file_content[element_start_pos + i] == '\'' | |
1242 | - || compiled_file_content[element_start_pos + i] == '"' | |
1243 | - || compiled_file_content[element_start_pos + i] == '?' | |
1244 | - || compiled_file_content[element_start_pos + i] == ':' | |
1245 | - || compiled_file_content[element_start_pos + i] == ';' | |
1246 | - || compiled_file_content[element_start_pos + i] == ',' | |
1247 | - || compiled_file_content[element_start_pos + i] == ' ' | |
1248 | - ) { | |
1249 | - | |
1250 | - if (variable != "$") { | |
1251 | - new_variable = parse_variable(variable.c_str()); | |
1252 | - | |
1253 | -DEBUG_PRINTF("(%s) = (%s)", variable.c_str(), new_variable.c_str()); | |
1254 | - | |
1255 | - } else { | |
1256 | - new_variable = variable; | |
1257 | - } | |
1258 | - new_condition += new_variable; | |
1259 | - new_condition += compiled_file_content[element_start_pos + i]; | |
1260 | - variable = ""; | |
1261 | - } else { | |
1262 | - variable += compiled_file_content[element_start_pos + i]; | |
1263 | -DEBUG_PRINTF("variable = (%s)", variable.c_str()); | |
1264 | - } | |
1265 | - i++; | |
1266 | - } | |
1267 | - new_variable = parse_variable(variable.c_str()); | |
1268 | - | |
1269 | -DEBUG_PRINTF("(%s) = (%s)", variable.c_str(), new_variable.c_str()); | |
1270 | - | |
1271 | - new_condition += new_variable; | |
1272 | - new_condition += "; ?>"; | |
1273 | - | |
1274 | -DEBUG_PRINTF("new_condition = (%s)", new_condition.c_str()); | |
1275 | - | |
1276 | - compiled_file_content.replace(tag_start_pos, tag_end_pos - tag_start_pos, new_condition); | |
1277 | - pos += new_condition.length() - 1; // 20060527 | |
1278 | - } else if (item[0] == '*') { // Comment | |
1279 | - if (item[item.length() - 1] != '*') { // support multi-line comment | |
1280 | - tag_end_pos = compiled_file_content.find(end_comment_tag, tag_end_pos) + strlen(end_comment_tag); | |
1281 | - } | |
1282 | - | |
1283 | -DEBUG_PRINTF("COMMENT = (%s)", compiled_file_content.substr(tag_start_pos, tag_end_pos - tag_start_pos).c_str()); | |
1284 | - | |
1285 | - compiled_file_content.erase(tag_start_pos, tag_end_pos - tag_start_pos); | |
1286 | - pos = tag_start_pos - 1; | |
1287 | - } else { | |
1288 | - | |
1289 | -DEBUG_PRINTF("CONDITION = (%s)", item.c_str()); | |
1290 | - | |
1291 | - string command_tag; | |
1292 | - string condition; | |
1293 | - if (item.find(" ") != string::npos) { | |
1294 | - command_tag = item.substr(0, item.find(" ")); | |
1295 | - condition = item.substr(item.find(" ") + 1); | |
1296 | - } else { | |
1297 | - command_tag = item; | |
1298 | - } | |
1299 | - | |
1300 | -DEBUG_PRINTF("COMMAND = (%s)", command_tag.c_str()); | |
1301 | -DEBUG_PRINTF("CONDITION = (%s)", condition.c_str()); | |
1302 | - | |
1303 | - if (command_tag == "if") { | |
1304 | - new_condition = "<?php if ("; | |
1305 | -cond_if: | |
1306 | - i = 0; | |
1307 | - while (i < condition.length()) { | |
1308 | - if (condition[i] == '+' | |
1309 | - || (condition[i] == '-' && condition.at(i + 1) != '>') | |
1310 | - || condition[i] == '*' | |
1311 | - || condition[i] == '/' | |
1312 | - || condition[i] == '%' | |
1313 | - || condition[i] == '<' | |
1314 | - || (condition[i] == '>' && condition.at(i - 1) != '-') | |
1315 | - || condition[i] == '=' | |
1316 | - || condition[i] == '!' | |
1317 | - || condition[i] == '|' | |
1318 | - || condition[i] == '&' | |
1319 | - || condition[i] == '(' | |
1320 | - || condition[i] == ')' | |
1321 | - || condition[i] == ',' | |
1322 | - || condition[i] == ' ' | |
1323 | - ) { | |
1324 | - new_variable = parse_variable(variable.c_str()); | |
1325 | - | |
1326 | -DEBUG_PRINTF("(%s) = (%s)", variable.c_str(), new_variable.c_str()); | |
1327 | - | |
1328 | - new_condition += new_variable; | |
1329 | - new_condition += condition[i]; | |
1330 | - variable = ""; | |
1331 | - } else { | |
1332 | - variable += condition[i]; | |
1333 | - } | |
1334 | - i++; | |
1335 | - } | |
1336 | - new_variable = parse_variable(variable.c_str()); | |
1337 | - new_condition += new_variable; | |
1338 | - new_condition += ") { ?>"; | |
1339 | - compiled_file_content.replace(tag_start_pos, tag_end_pos - tag_start_pos, new_condition); | |
1340 | - pos += new_condition.length() - 1; // 20060527 | |
1341 | - } else if (command_tag == "/if") { | |
1342 | - compiled_file_content.replace(tag_start_pos, tag_end_pos - tag_start_pos, "<?php } ?>"); | |
1343 | - pos += strlen("<?php } ?>") - 1; // 20060527 | |
1344 | - } else if (command_tag == "else") { | |
1345 | - if (item.find("else if") != string::npos) { | |
1346 | - new_condition = "<?php } else if ("; | |
1347 | - condition = item.substr(item.find("else if") + strlen("else if")); | |
1348 | - condition = trim(condition.c_str()); | |
1349 | - goto cond_if; | |
1350 | - } | |
1351 | - compiled_file_content.replace(tag_start_pos, tag_end_pos - tag_start_pos, "<?php } else { ?>"); | |
1352 | - pos += strlen("<?php } else { ?>") - 1; // 20060527 | |
1353 | - } else if (command_tag == "elseif") { | |
1354 | - new_condition = "<?php } else if ("; | |
1355 | - goto cond_if; | |
1356 | - } else if (command_tag == "section") { | |
1357 | - string section_name = get_element(item.c_str(), "name"); | |
1358 | - string section_loop = get_element(item.c_str(), "loop"); | |
1359 | - string section_start = get_element(item.c_str(), "start"); | |
1360 | - string section_step = get_element(item.c_str(), "step"); | |
1361 | - string new_section_loop2 = ""; | |
1362 | - condition = section_loop; | |
1363 | - | |
1364 | - i = 0; | |
1365 | - while (i < condition.length()) { | |
1366 | - if (condition[i] == '+' | |
1367 | - || condition[i] == '-' | |
1368 | - || condition[i] == '*' | |
1369 | - || condition[i] == '/' | |
1370 | - || condition[i] == '%' | |
1371 | - || condition[i] == '<' | |
1372 | - || condition[i] == '>' | |
1373 | - || condition[i] == '=' | |
1374 | - || condition[i] == '!' | |
1375 | - || condition[i] == '|' | |
1376 | - || condition[i] == '&' | |
1377 | - || condition[i] == '(' | |
1378 | - || condition[i] == ')' | |
1379 | - ) { | |
1380 | - new_variable = parse_variable(variable.c_str()); | |
1381 | - new_section_loop2 += new_variable; | |
1382 | - new_section_loop2 += condition[i]; | |
1383 | -DEBUG_PRINTF("variable = (%s), new_section_loop2 = (%s)", variable.c_str(), new_section_loop2.c_str()); | |
1384 | - variable = ""; | |
1385 | - } else { | |
1386 | - if (condition[i] != ' ') { | |
1387 | - variable += condition[i]; | |
1388 | - } | |
1389 | -DEBUG_PRINTF("variable = (%s)", variable.c_str()); | |
1390 | - } | |
1391 | - i++; | |
1392 | - } | |
1393 | - new_section_loop2 += parse_variable(variable.c_str()); | |
1394 | -DEBUG_PRINTF("new_section_loop2 = (%s)", new_section_loop2.c_str()); | |
1395 | - | |
1396 | - // "<?php $this->_section['%s']['total'] = count(%s);\n" | |
1397 | - new_condition = "<?php "; | |
1398 | - new_condition += "$this->_section['"; | |
1399 | - new_condition += section_name; | |
1400 | - new_condition += "']['total'] = "; | |
1401 | - new_condition += "count("; | |
1402 | - new_condition += new_section_loop2; | |
1403 | - new_condition += ");\n"; | |
1404 | - | |
1405 | - // "for ($%s = 0, $this->_section['%s']['iteration'] = 1; $%s<$this->_section['%s']['total']; $%s++, $this->_section['%s']['iteration']++) {\n" | |
1406 | - new_condition += "for ($"; | |
1407 | - new_condition += section_name; | |
1408 | - new_condition += " = "; | |
1409 | - if (section_start.length() > 0) { | |
1410 | - new_condition += section_start; | |
1411 | - } else { | |
1412 | - new_condition += "0"; | |
1413 | - } | |
1414 | - new_condition += ", $this->_section['"; | |
1415 | - new_condition += section_name; | |
1416 | - new_condition += "']['iteration'] = 1; $"; | |
1417 | - new_condition += section_name; | |
1418 | - new_condition += " < $this->_section['"; | |
1419 | - new_condition += section_name; | |
1420 | - new_condition += "']['total']; $"; | |
1421 | - new_condition += section_name; | |
1422 | - if (section_step.length() > 0) { | |
1423 | - new_condition += " += "; | |
1424 | - new_condition += section_step; | |
1425 | - } else { | |
1426 | - new_condition += "++"; | |
1427 | - } | |
1428 | - new_condition += ", $this->_section['"; | |
1429 | - new_condition += section_name; | |
1430 | - new_condition += "']['iteration']++"; | |
1431 | - new_condition += ") {\n"; | |
1432 | - | |
1433 | - // "$this->_section['%s']['first'] = ($% == 0);\n" | |
1434 | - new_condition += "$this->_section['"; | |
1435 | - new_condition += section_name; | |
1436 | - new_condition += "']['first'] = ($"; | |
1437 | - new_condition += section_name; | |
1438 | - new_condition += " == "; | |
1439 | - if (section_start.length() > 0) { | |
1440 | - new_condition += section_start; | |
1441 | - } else { | |
1442 | - new_condition += "0"; | |
1443 | - } | |
1444 | - new_condition += ");\n"; | |
1445 | - | |
1446 | - // "$this->_section['%s']['last'] == ($this->_section['%s']['iteration'] == $this->_section['%s']['total']);\n" | |
1447 | - new_condition += "$this->_section['"; | |
1448 | - new_condition += section_name; | |
1449 | - new_condition += "']['last'] = ($this->_section['"; | |
1450 | - new_condition += section_name; | |
1451 | - new_condition += "']['iteration']"; | |
1452 | - new_condition += " == $this->_section['"; | |
1453 | - new_condition += section_name; | |
1454 | - new_condition += "']['total']);\n"; | |
1455 | - new_condition += "?>"; | |
1456 | - | |
1457 | - compiled_file_content.replace(tag_start_pos, tag_end_pos - tag_start_pos, new_condition); | |
1458 | - pos += new_condition.length() - 1; // 20060527 | |
1459 | - } else if (command_tag == "/section") { | |
1460 | - compiled_file_content.replace(tag_start_pos, tag_end_pos - tag_start_pos, "<?php } ?>"); | |
1461 | - pos += strlen("<?php } ?>") - 1; // 20060527 | |
1462 | - } else if (command_tag == "foreach") { | |
1463 | - string foreach_key = get_element(item.c_str(), "key"); | |
1464 | - string foreach_item = get_element(item.c_str(), "item"); | |
1465 | - string foreach_from = get_element(item.c_str(), "from"); | |
1466 | - | |
1467 | - string new_foreach_from2 = ""; | |
1468 | - condition = foreach_from; | |
1469 | - i = 0; | |
1470 | - while (i < condition.length()) { | |
1471 | - if (condition[i] == '+' | |
1472 | - || condition[i] == '-' | |
1473 | - || condition[i] == '*' | |
1474 | - || condition[i] == '/' | |
1475 | - || condition[i] == '%' | |
1476 | - || condition[i] == '<' | |
1477 | - || condition[i] == '>' | |
1478 | - || condition[i] == '=' | |
1479 | - || condition[i] == '!' | |
1480 | - || condition[i] == '|' | |
1481 | - || condition[i] == '&' | |
1482 | - || condition[i] == '(' | |
1483 | - || condition[i] == ')' | |
1484 | - ) { | |
1485 | - new_variable = parse_variable(variable.c_str()); | |
1486 | - new_foreach_from2 += new_variable; | |
1487 | - new_foreach_from2 += condition[i]; | |
1488 | - variable = ""; | |
1489 | - } else { | |
1490 | - if (condition[i] != ' ') { | |
1491 | - variable += condition[i]; | |
1492 | - } | |
1493 | - } | |
1494 | - i++; | |
1495 | - } | |
1496 | - new_foreach_from2 += parse_variable(variable.c_str()); | |
1497 | - | |
1498 | - // "<?php if (!is_array(%s) && !is_object(%s)) { settype("%s", 'array') };\n" | |
1499 | - new_condition = "<?php "; | |
1500 | - new_condition += "if (!is_array("; | |
1501 | - new_condition += new_foreach_from2; | |
1502 | - new_condition += ") && !is_object("; | |
1503 | - new_condition += new_foreach_from2; | |
1504 | - new_condition += ")) { settype("; | |
1505 | - new_condition += new_foreach_from2; | |
1506 | - new_condition += ", 'array'); }\n"; | |
1507 | - | |
1508 | - // "if (count(%s)) {\n" | |
1509 | - new_condition += "if (count("; | |
1510 | - new_condition += new_foreach_from2; | |
1511 | - new_condition += ")) {\n"; | |
1512 | - | |
1513 | - // "foreach (%s as %s) { ?>" | |
1514 | - new_condition += "foreach ("; | |
1515 | - new_condition += new_foreach_from2; | |
1516 | - new_condition += " as "; | |
1517 | - if (foreach_key.length() > 0) { | |
1518 | - new_condition += "$this->_tpl_vars[\""; | |
1519 | - new_condition += foreach_key; | |
1520 | - new_condition += "\"] => "; | |
1521 | - } | |
1522 | - new_condition += "$this->_tpl_vars[\""; | |
1523 | - new_condition += foreach_item; | |
1524 | - new_condition += "\"]) { ?>"; | |
1525 | - | |
1526 | - compiled_file_content.replace(tag_start_pos, tag_end_pos - tag_start_pos, new_condition); | |
1527 | - pos += new_condition.length() - 1; // 20060527 | |
1528 | - | |
1529 | - } else if (command_tag == "/foreach") { | |
1530 | - compiled_file_content.replace(tag_start_pos, tag_end_pos - tag_start_pos, "<?php }} ?>"); | |
1531 | - pos += strlen("<?php }} ?>") - 1; // 20060527 | |
1532 | - } else if (command_tag == "include") { | |
1533 | - string filename = get_element(item.c_str(), "file"); | |
1534 | - string included_filename = get_include_filename(obj, template_dir, filename TSRMLS_CC); | |
1535 | - new_condition = _readfile(included_filename.c_str() TSRMLS_CC); | |
1536 | - if (new_condition.find(filename) != string::npos) { | |
1537 | - zend_error(E_ERROR, "Found recursive include:%s", filename.c_str()); | |
1538 | - return; | |
1539 | - } | |
1540 | - compiled_file_content.replace(tag_start_pos, tag_end_pos - tag_start_pos, new_condition); | |
1541 | - continue; // don't skip with pos | |
1542 | - } else if (command_tag == "php") { | |
1543 | - string end_php_tag; | |
1544 | - size_t php_tag_end_pos; | |
1545 | - end_php_tag = left_delimiter; | |
1546 | - end_php_tag += "/php"; | |
1547 | - end_php_tag += right_delimiter; | |
1548 | - php_tag_end_pos = compiled_file_content.find(end_php_tag, tag_end_pos); | |
1549 | - new_condition = "<?php "; | |
1550 | - new_condition += compiled_file_content.substr(tag_end_pos, php_tag_end_pos - tag_end_pos); | |
1551 | - new_condition += " ?>"; | |
1552 | - compiled_file_content.replace(tag_start_pos, php_tag_end_pos+end_php_tag.length() - tag_start_pos, new_condition); | |
1553 | - pos += new_condition.length() - 1; | |
1554 | - } else if (command_tag == "literal") { | |
1555 | - string end_literal = left_delimiter; | |
1556 | - end_literal += "/literal"; | |
1557 | - end_literal += right_delimiter; | |
1558 | - new_condition = compiled_file_content.substr(tag_end_pos, compiled_file_content.find(end_literal, element_end_pos) - tag_end_pos); | |
1559 | - compiled_file_content.replace(tag_start_pos, compiled_file_content.find(end_literal, element_end_pos) + end_literal.length() - tag_start_pos, new_condition); | |
1560 | - pos += new_condition.length() - 1; | |
1561 | - } else { | |
1562 | - goto cond_var; | |
1563 | - } | |
1564 | - } | |
1565 | - pos++; | |
1566 | - } | |
1567 | - | |
1568 | - // postfilter | |
1569 | - if (plugins && Z_TYPE_P(plugins) == IS_ARRAY) { | |
1570 | - zval **postfilter; | |
1571 | - if (zend_hash_find(Z_ARRVAL_P(plugins), "postfilter", sizeof("postfilter"), (void**)&postfilter) == SUCCESS && Z_TYPE_PP(postfilter) == IS_ARRAY) { | |
1572 | - zval **elem; | |
1573 | - zend_hash_internal_pointer_reset(Z_ARRVAL_PP(postfilter)); | |
1574 | - while (zend_hash_get_current_data(Z_ARRVAL_PP(postfilter), (void**)&elem) == SUCCESS) { | |
1575 | - | |
1576 | - zval postfilter_function, postfilter_ret; | |
1577 | - zval zcontent; | |
1578 | - zval *postfilter_argv[1]; | |
1579 | - postfilter_argv[0] = &zcontent; | |
1580 | - SET_ZVAL_STRING(zcontent, compiled_file_content.c_str()); | |
1581 | - | |
1582 | - INIT_ZVAL(postfilter_function); | |
1583 | - ZVAL_STRING(&postfilter_function, Z_STRVAL_PP(elem), 1); | |
1584 | - | |
1585 | - if (call_user_function(EG(function_table), NULL, &postfilter_function, &postfilter_ret, 1, postfilter_argv TSRMLS_CC) == FAILURE) { | |
1586 | - zval_dtor(&zcontent); | |
1587 | - zend_error(E_ERROR, "fail to %s", Z_STRVAL_PP(elem)); | |
1588 | - return; | |
1589 | - } | |
1590 | - zval_dtor(&postfilter_function); | |
1591 | - zval_dtor(&zcontent); | |
1592 | - | |
1593 | - compiled_file_content = Z_STRVAL(postfilter_ret); | |
1594 | - zval_dtor(&postfilter_ret); | |
1595 | - | |
1596 | - zend_hash_move_forward(Z_ARRVAL_PP(postfilter)); | |
1597 | - } | |
1598 | - } | |
1599 | - } | |
1600 | - | |
1601 | -DEBUG_PRINTF("write compiled file path = (%s)", const_cast<char*>(full_compile_filename.c_str())); | |
1602 | - | |
1603 | -#ifdef PHP_WIN32 | |
1604 | - php_stream *strm = php_stream_open_wrapper(const_cast<char*>(full_compile_filename.c_str()), "wb", ENFORCE_SAFE_MODE|REPORT_ERRORS, NULL); | |
1605 | - if (!strm) { | |
1606 | - | |
1607 | - // create sub directory path | |
1608 | - if (full_compile_filename.find(DEFAULT_SLASH) != string::npos) { | |
1609 | - size_t slash_rpos = full_compile_filename.rfind("/"); | |
1610 | - size_t default_slash_rpos = full_compile_filename.rfind(DEFAULT_SLASH); | |
1611 | - | |
1612 | -DEBUG_PRINTF("slash_rpos=(%d), default_slash_rpos=(%d)", slash_rpos, default_slash_rpos); | |
1613 | - | |
1614 | - if (slash_rpos == string::npos || slash_rpos < default_slash_rpos) { | |
1615 | - slash_rpos = default_slash_rpos; | |
1616 | - } | |
1617 | - string directory_path = full_compile_filename.substr(0, slash_rpos); | |
1618 | - | |
1619 | -DEBUG_PRINTF("slash_rpos=(%d)", slash_rpos); | |
1620 | -DEBUG_PRINTF("directory_path=(%s)", const_cast<char*>(directory_path.c_str())); | |
1621 | - | |
1622 | - // mkdir recursively | |
1623 | - php_stream_context *context = NULL; | |
1624 | - if (!php_stream_mkdir(const_cast<char*>(directory_path.c_str()), 0755, (PHP_STREAM_MKDIR_RECURSIVE | REPORT_ERRORS), context)) { | |
1625 | - zend_error(E_ERROR, "fail to php_stream_mkdir(win32) (%s)", const_cast<char*>(directory_path.c_str())); | |
1626 | - return; | |
1627 | - } | |
1628 | - strm = php_stream_open_wrapper(const_cast<char*>(full_compile_filename.c_str()), "wb", ENFORCE_SAFE_MODE|REPORT_ERRORS, NULL); | |
1629 | - } | |
1630 | - } | |
1631 | - if (strm) { | |
1632 | - if (php_stream_supports_lock(strm)) { | |
1633 | - php_stream_lock(strm, LOCK_EX); | |
1634 | - } | |
1635 | - if (compiled_file_content.length() > 0) { | |
1636 | - php_stream_write_string(strm, const_cast<char*>(compiled_file_content.c_str())); | |
1637 | - } | |
1638 | - if (php_stream_supports_lock(strm)) { | |
1639 | - php_stream_lock(strm, LOCK_UN); | |
1640 | - } | |
1641 | - php_stream_close(strm); | |
1642 | - } | |
1643 | -#else | |
1644 | - // write compiled file | |
1645 | - FILE *fp = VCWD_FOPEN(full_compile_filename.c_str(), "wb"); | |
1646 | - if (fp == NULL) { | |
1647 | - // create sub directory path | |
1648 | - if (full_compile_filename.find(DEFAULT_SLASH) != string::npos) { | |
1649 | - size_t slash_rpos = full_compile_filename.rfind("\\"); | |
1650 | - size_t default_slash_rpos = full_compile_filename.rfind(DEFAULT_SLASH); | |
1651 | - | |
1652 | -DEBUG_PRINTF("slash_rpos=(%d), default_slash_rpos=(%d)", slash_rpos, default_slash_rpos); | |
1653 | - | |
1654 | - if (slash_rpos == string::npos || slash_rpos < default_slash_rpos) { | |
1655 | - slash_rpos = default_slash_rpos; | |
1656 | - } | |
1657 | - string directory_path = full_compile_filename.substr(0, slash_rpos); | |
1658 | - | |
1659 | -DEBUG_PRINTF("slash_rpos=(%d)", slash_rpos); | |
1660 | -DEBUG_PRINTF("directory_path=(%s)", const_cast<char*>(directory_path.c_str())); | |
1661 | - | |
1662 | - // mkdir recursively | |
1663 | - php_stream_context *context = NULL; | |
1664 | - if (!php_stream_mkdir(const_cast<char*>(directory_path.c_str()), 0755, (PHP_STREAM_MKDIR_RECURSIVE | REPORT_ERRORS), context)) { | |
1665 | - zend_error(E_ERROR, "fail to php_stream_mkdir (%s)", const_cast<char*>(directory_path.c_str())); | |
1666 | - return; | |
1667 | - } | |
1668 | - } | |
1669 | - | |
1670 | - fp = VCWD_FOPEN(full_compile_filename.c_str(), "wb"); | |
1671 | - if (fp == NULL) { | |
1672 | - // error handling | |
1673 | - zend_error(E_ERROR, "fail to write : %s", full_compile_filename.c_str()); | |
1674 | - return; | |
1675 | - } | |
1676 | - } | |
1677 | - if (fwrite(compiled_file_content.c_str(), 1, compiled_file_content.length(), fp) != compiled_file_content.length()) { | |
1678 | - zend_error(E_WARNING, "fail to write:%s", full_compile_filename.c_str()); | |
1679 | - return; | |
1680 | - } | |
1681 | - fclose(fp); | |
1682 | -#endif // PHP_WIN32 | |
1683 | - } | |
1684 | - | |
1685 | - *fullfile_name = estrndup(full_compile_filename.c_str(), full_compile_filename.length()); | |
1686 | - | |
1687 | -DEBUG_PRINTF("%s", "end function"); | |
1688 | - | |
1689 | - return; | |
1690 | -} | |
1691 | - | |
1692 | -/* {{{ Registers simplate properties. | |
1693 | - * | |
1694 | - * @return void | |
1695 | - */ | |
1696 | -static void register_simplate_properties(TSRMLS_D) | |
1697 | -{ | |
1698 | - zend_class_entry simplate_ce; | |
1699 | - | |
1700 | - // register class entry | |
1701 | - INIT_CLASS_ENTRY(simplate_ce, "Simplate", php_simplate_functions); | |
1702 | - simplate_entry_ptr = zend_register_internal_class(&simplate_ce TSRMLS_CC); | |
1703 | - | |
1704 | -#ifdef ZEND_ENGINE_2 | |
1705 | - /** | |
1706 | - * Class property | |
1707 | - */ | |
1708 | - // directory parameter seting | |
1709 | - zend_declare_property_string(simplate_entry_ptr, const_cast<char*>(TEMPLATE_DIR), strlen(TEMPLATE_DIR), const_cast<char*>(DEFAULT_TEMPLATE_DIR), ZEND_ACC_PUBLIC TSRMLS_CC); | |
1710 | - zend_declare_property_string(simplate_entry_ptr, const_cast<char*>(COMPILE_DIR), strlen(COMPILE_DIR), const_cast<char*>(DEFAULT_COMPILE_DIR), ZEND_ACC_PUBLIC TSRMLS_CC); | |
1711 | - zend_declare_property_string(simplate_entry_ptr, const_cast<char*>(CACHE_DIR), strlen(CACHE_DIR), const_cast<char*>(DEFAULT_CACHE_DIR), ZEND_ACC_PUBLIC TSRMLS_CC); | |
1712 | - zend_declare_property_long(simplate_entry_ptr, const_cast<char*>(CACHING), strlen(CACHING), DEFAULT_CACHING, ZEND_ACC_PUBLIC TSRMLS_CC); | |
1713 | - zend_declare_property_long(simplate_entry_ptr, const_cast<char*>(CACHE_LIFETIME), strlen(CACHE_LIFETIME), DEFAULT_CACHE_LIFETIME, ZEND_ACC_PUBLIC TSRMLS_CC); | |
1714 | - | |
1715 | - // delimitter | |
1716 | - zend_declare_property_string(simplate_entry_ptr, const_cast<char*>(LEFT_DELIMITER), strlen(LEFT_DELIMITER), const_cast<char*>(DEFAULT_LEFT_DELIMITER), ZEND_ACC_PUBLIC TSRMLS_CC); | |
1717 | - zend_declare_property_string(simplate_entry_ptr, const_cast<char*>(RIGHT_DELIMITER), strlen(RIGHT_DELIMITER), const_cast<char*>(DEFAULT_RIGHT_DELIMITER), ZEND_ACC_PUBLIC TSRMLS_CC); | |
1718 | - zend_declare_property_bool(simplate_entry_ptr, const_cast<char*>(COMPILE_CHECK), strlen(COMPILE_CHECK), DEFAULT_COMPILE_CHECK, ZEND_ACC_PUBLIC TSRMLS_CC); | |
1719 | - | |
1720 | - // force_compile, lazy_check | |
1721 | - zend_declare_property_bool(simplate_entry_ptr, const_cast<char*>(FORCE_COMPILE), strlen(FORCE_COMPILE), DEFAULT_FORCE_COMPILE, ZEND_ACC_PUBLIC TSRMLS_CC); | |
1722 | - zend_declare_property_bool(simplate_entry_ptr, const_cast<char*>(LAZY_CHECK), strlen(LAZY_CHECK), DEFAULT_LAZY_CHECK, ZEND_ACC_PUBLIC TSRMLS_CC); | |
1723 | - | |
1724 | - // version | |
1725 | - zend_declare_property_string(simplate_entry_ptr, const_cast<char*>("version"), strlen("version"), const_cast<char*>(VERSION), ZEND_ACC_PUBLIC TSRMLS_CC); | |
1726 | - | |
1727 | - // temporary | |
1728 | - zend_declare_property_null(simplate_entry_ptr, const_cast<char*>("_tpl_vars"), strlen("_tpl_vars"), ZEND_ACC_PUBLIC TSRMLS_CC); | |
1729 | - // plugins | |
1730 | - zend_declare_property_null(simplate_entry_ptr, const_cast<char*>("_plugins"), strlen("_plugins"), ZEND_ACC_PUBLIC TSRMLS_CC); | |
1731 | -#endif // ZEND_ENGINE_2 | |
1732 | -} | |
1733 | -/* }}} */ | |
1734 | - | |
1735 | -/* {{{ PHP_MINIT_FUNCTION | |
1736 | - * | |
1737 | - * @return int returns SUCCESS if function succeeded | |
1738 | - */ | |
1739 | -PHP_MINIT_FUNCTION(simplate) | |
1740 | -{ | |
1741 | - /* If you have INI entries, uncomment these lines | |
1742 | - ZEND_INIT_MODULE_GLOBALS(simplate, php_simplate_init_globals, NULL); | |
1743 | - REGISTER_INI_ENTRIES(); | |
1744 | - */ | |
1745 | - ZEND_INIT_MODULE_GLOBALS(simplate, php_simplate_init_globals, NULL); | |
1746 | - register_simplate_properties(TSRMLS_C); | |
1747 | - | |
1748 | - return SUCCESS; | |
1749 | -} | |
1750 | -/* }}} */ | |
1751 | - | |
1752 | -/* {{{ PHP_MSHUTDOWN_FUNCTION | |
1753 | - * | |
1754 | - * @return int returns SUCCESS if function succeeded | |
1755 | - */ | |
1756 | -PHP_MSHUTDOWN_FUNCTION(simplate) | |
1757 | -{ | |
1758 | - /* uncomment this line if you have INI entries | |
1759 | - UNREGISTER_INI_ENTRIES(); | |
1760 | - */ | |
1761 | - return SUCCESS; | |
1762 | -} | |
1763 | -/* }}} */ | |
1764 | - | |
1765 | -/* Remove if there's nothing to do at request start */ | |
1766 | -/* {{{ PHP_RINIT_FUNCTION | |
1767 | - * | |
1768 | - * @return int returns SUCCESS if function succeeded | |
1769 | - */ | |
1770 | -PHP_RINIT_FUNCTION(simplate) | |
1771 | -{ | |
1772 | - return SUCCESS; | |
1773 | -} | |
1774 | -/* }}} */ | |
1775 | - | |
1776 | -/* Remove if there's nothing to do at request end */ | |
1777 | -/* {{{ PHP_RSHUTDOWN_FUNCTION | |
1778 | - * | |
1779 | - * @return int returns SUCCESS if function succeeded | |
1780 | - */ | |
1781 | -PHP_RSHUTDOWN_FUNCTION(simplate) | |
1782 | -{ | |
1783 | - return SUCCESS; | |
1784 | -} | |
1785 | -/* }}} */ | |
1786 | - | |
1787 | -/* {{{ PHP_MINFO_FUNCTION | |
1788 | - * | |
1789 | - * @return void | |
1790 | - */ | |
1791 | -PHP_MINFO_FUNCTION(simplate) | |
1792 | -{ | |
1793 | - php_info_print_table_start(); | |
1794 | - php_info_print_table_header(2, "simplate support", "enabled"); | |
1795 | - php_info_print_table_row(2, "Version", VERSION); | |
1796 | - php_info_print_table_end(); | |
1797 | - | |
1798 | - /* Remove comments if you have entries in php.ini | |
1799 | - DISPLAY_INI_ENTRIES(); | |
1800 | - */ | |
1801 | -} | |
1802 | -/* }}} */ | |
1803 | - | |
1804 | -#ifdef ZEND_ENGINE_2 | |
1805 | -/* {{{ ZEND_METHOD __construct | |
1806 | - * | |
1807 | - * @return void | |
1808 | - */ | |
1809 | -ZEND_METHOD(simplate, __construct) | |
1810 | -#else | |
1811 | -/* {{{ PHP_FUNCTION simplate_init | |
1812 | - * | |
1813 | - * @return void | |
1814 | - */ | |
1815 | -PHP_FUNCTION(simplate_init) | |
1816 | -#endif // ZEND_ENGINE_2 | |
1817 | -{ | |
1818 | -#ifdef ZEND_ENGINE_2 | |
1819 | -#else | |
1820 | - zval *obj = getThis(); | |
1821 | - object_init_ex(obj, simplate_entry_ptr); | |
1822 | - add_property_string(obj, TEMPLATE_DIR, DEFAULT_TEMPLATE_DIR, 1); | |
1823 | - add_property_string(obj, COMPILE_DIR, DEFAULT_COMPILE_DIR, 1); | |
1824 | - add_property_string(obj, CACHE_DIR, DEFAULT_CACHE_DIR, 1); | |
1825 | - add_property_long(obj, CACHING, DEFAULT_CACHING); | |
1826 | - add_property_long(obj, CACHE_LIFETIME, DEFAULT_CACHE_LIFETIME); | |
1827 | - | |
1828 | - add_property_string(obj, LEFT_DELIMITER, DEFAULT_LEFT_DELIMITER, 1); | |
1829 | - add_property_string(obj, RIGHT_DELIMITER, DEFAULT_RIGHT_DELIMITER, 1); | |
1830 | - add_property_bool(obj, COMPILE_CHECK, DEFAULT_COMPILE_CHECK); | |
1831 | - add_property_bool(obj, FORCE_COMPILE, DEFAULT_FORCE_COMPILE); | |
1832 | - add_property_bool(obj, LAZY_CHECK, DEFAULT_LAZY_CHECK); | |
1833 | - add_property_string(obj, "version", VERSION, 1); | |
1834 | -#endif // ZEND_ENGINE_2 | |
1835 | -} | |
1836 | -/* }}} */ | |
1837 | - | |
1838 | -#ifdef ZEND_ENGINE_2 | |
1839 | -#else | |
1840 | -/* {{{ Set already exist array on new array recursively(PHP4). | |
1841 | - * | |
1842 | - * @param zval **struc old array | |
1843 | - * @param zval **new_array new array to be set | |
1844 | - * | |
1845 | - * @return void | |
1846 | - */ | |
1847 | -static void php_set_r( | |
1848 | - zval **struc, | |
1849 | - zval **new_array | |
1850 | -) | |
1851 | -{ | |
1852 | - zval **elem; | |
1853 | - char *k = 0; | |
1854 | - ulong i = 0; | |
1855 | - char buf[128]; | |
1856 | - | |
1857 | - zend_hash_internal_pointer_reset(Z_ARRVAL_PP(struc)); | |
1858 | - while (zend_hash_get_current_data(Z_ARRVAL_PP(struc), (void **)&elem) == SUCCESS) { | |
1859 | - zend_hash_get_current_key(Z_ARRVAL_PP(struc), &k, &i, 0); | |
1860 | - sprintf(buf, "%d", i); | |
1861 | - switch (Z_TYPE_PP(elem)) { | |
1862 | - case IS_NULL: | |
1863 | - add_assoc_unset(*new_array, k); | |
1864 | - break; | |
1865 | - case IS_BOOL: | |
1866 | - add_assoc_bool(*new_array, k, Z_BVAL_PP(elem)); | |
1867 | - break; | |
1868 | - case IS_LONG: | |
1869 | - add_assoc_long(*new_array, k, Z_LVAL_PP(elem)); | |
1870 | - break; | |
1871 | - case IS_DOUBLE: | |
1872 | - add_assoc_double(*new_array, k, Z_DVAL_PP(elem)); | |
1873 | - break; | |
1874 | - case IS_STRING: | |
1875 | - add_assoc_stringl(*new_array, k, Z_STRVAL_PP(elem), Z_STRLEN_P(elem)/* (*elem)->value.str.val */, 1); | |
1876 | - break; | |
1877 | - case IS_ARRAY: | |
1878 | - zval_add_ref(elem); | |
1879 | - add_assoc_zval(*new_array, k, *elem); | |
1880 | - } | |
1881 | - zend_hash_move_forward(Z_ARRVAL_PP(struc)); | |
1882 | - } | |
1883 | -} | |
1884 | -#endif // ZEND_ENGINE_2 | |
1885 | -/* }}} */ | |
1886 | - | |
1887 | -#ifdef ZEND_ENGINE_2 | |
1888 | -/* {{{ ZEND_METHOD assign | |
1889 | - * | |
1890 | - * @return void | |
1891 | - */ | |
1892 | -ZEND_METHOD(simplate, assign) | |
1893 | -#else | |
1894 | -/* {{{ PHP_FUNCTION simplate_assign | |
1895 | - * | |
1896 | - * @return void | |
1897 | - */ | |
1898 | -PHP_FUNCTION(simplate_assign) | |
1899 | -#endif // ZEND_ENGINE_2 | |
1900 | -{ | |
1901 | - char *key = NULL; | |
1902 | - zval *zvalue; | |
1903 | - int key_len; | |
1904 | - | |
1905 | -DEBUG_PRINTF("%s", "start function"); | |
1906 | - | |
1907 | - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, const_cast<char*>("sz"), &key, &key_len, &zvalue) == FAILURE) { | |
1908 | - return; | |
1909 | - } | |
1910 | - zval *obj = getThis(); | |
1911 | - zval *new_array; | |
1912 | - | |
1913 | -#ifdef ZEND_ENGINE_2 | |
1914 | - new_array = zend_read_property(Z_OBJCE_P(obj), obj, const_cast<char*>("_tpl_vars"), strlen("_tpl_vars"), 1 TSRMLS_CC); | |
1915 | - // initialize.use new_array on the first time. | |
1916 | - if (new_array == EG(uninitialized_zval_ptr) || Z_TYPE_P(new_array) == IS_NULL) { | |
1917 | - MAKE_STD_ZVAL(new_array); | |
1918 | - array_init(new_array); | |
1919 | - Z_DELREF_P(new_array); | |
1920 | - } | |
1921 | -#else | |
1922 | - zval **tmp; | |
1923 | - MAKE_STD_ZVAL(new_array); | |
1924 | - array_init(new_array); | |
1925 | - // the ``_tpl_vars'' exists already. | |
1926 | - if (zend_hash_find(Z_OBJPROP_P(obj), "_tpl_vars", sizeof("_tpl_vars"), (void**)&tmp) == SUCCESS) { | |
1927 | - // set old array to new array recursively. | |
1928 | - php_set_r(tmp, &new_array); | |
1929 | - } | |
1930 | -#endif // ZEND_ENGINE_2 | |
1931 | - | |
1932 | - switch (Z_TYPE_P(zvalue)) { | |
1933 | - case IS_NULL: | |
1934 | - add_assoc_unset(new_array, key); | |
1935 | - break; | |
1936 | - case IS_BOOL: | |
1937 | - add_assoc_bool(new_array, key, Z_BVAL_P(zvalue)); | |
1938 | - break; | |
1939 | - case IS_DOUBLE: | |
1940 | - add_assoc_double(new_array, key, Z_DVAL_P(zvalue)); | |
1941 | - break; | |
1942 | - case IS_STRING: | |
1943 | - add_assoc_stringl(new_array, key, Z_STRVAL_P(zvalue), Z_STRLEN_P(zvalue), 1); | |
1944 | - break; | |
1945 | - case IS_LONG: | |
1946 | - add_assoc_long(new_array, key, Z_LVAL_P(zvalue)); | |
1947 | - break; | |
1948 | - case IS_ARRAY: | |
1949 | - zval_add_ref(&zvalue); // don't forget this! | |
1950 | - add_assoc_zval(new_array, key, zvalue); | |
1951 | - break; | |
1952 | - case IS_OBJECT: | |
1953 | - zval_add_ref(&zvalue); | |
1954 | - add_assoc_zval(new_array, key, zvalue); | |
1955 | - } | |
1956 | - | |
1957 | -#ifdef ZEND_ENGINE_2 | |
1958 | - zend_update_property(Z_OBJCE_P(obj), obj, const_cast<char*>("_tpl_vars"), strlen("_tpl_vars"), new_array TSRMLS_CC); | |
1959 | -#else | |
1960 | - zend_hash_update(Z_OBJPROP_P(obj), "_tpl_vars", strlen("_tpl_vars") + 1, &new_array, sizeof(zval*), NULL); | |
1961 | -#endif // ZEND_ENGINE_2 | |
1962 | - | |
1963 | -DEBUG_PRINTF("%s", "end function"); | |
1964 | - | |
1965 | - return; | |
1966 | -} | |
1967 | -/* }}} */ | |
1968 | - | |
1969 | -/* {{{ Outputs to global_string. | |
1970 | - * | |
1971 | - * @param const char *str | |
1972 | - * @param uint strlen | |
1973 | - * | |
1974 | - * @return int returns strlen | |
1975 | - */ | |
1976 | -static int php_my_output_func( | |
1977 | - const char *str, | |
1978 | - uint str_len TSRMLS_DC | |
1979 | -) | |
1980 | -{ | |
1981 | - SIMPLATE_G(global_string) << str; | |
1982 | - return str_len; | |
1983 | -} | |
1984 | -/* }}} */ | |
1985 | - | |
1986 | -#ifdef ZEND_ENGINE_2 | |
1987 | -/* {{{ ZEND_METHOD fetch | |
1988 | - * | |
1989 | - * @return void | |
1990 | - */ | |
1991 | -ZEND_METHOD(simplate, fetch) | |
1992 | -#else | |
1993 | -/* {{{ PHP_FUNCTION simplate_fetch | |
1994 | - * | |
1995 | - * @return void | |
1996 | - */ | |
1997 | -PHP_FUNCTION(simplate_fetch) | |
1998 | -#endif // ZEND_ENGINE_2 | |
1999 | -{ | |
2000 | - string error; | |
2001 | - char *fullfile_name = NULL; | |
2002 | - char *fetch_content = NULL; | |
2003 | - | |
2004 | -DEBUG_PRINTF("%s", "start function"); | |
2005 | - | |
2006 | - read_parse_template(INTERNAL_FUNCTION_PARAM_PASSTHRU, &fullfile_name, SIMPLATE_FETCH, &fetch_content); | |
2007 | - if (fetch_content) { | |
2008 | - RETURN_STRING(fetch_content, 1); | |
2009 | - } | |
2010 | - | |
2011 | - if (!fullfile_name || strlen(fullfile_name) <= 0) { | |
2012 | - return; | |
2013 | - } | |
2014 | - | |
2015 | - // | |
2016 | - // Execute compiled file | |
2017 | - // | |
2018 | - zend_file_handle file_handle; | |
2019 | - zend_op_array *op_array; | |
2020 | - file_handle.filename = fullfile_name; | |
2021 | - file_handle.free_filename = 0; | |
2022 | - file_handle.type = ZEND_HANDLE_FILENAME; | |
2023 | - file_handle.opened_path = NULL; | |
2024 | - op_array = zend_compile_file(&file_handle, ZEND_INCLUDE TSRMLS_CC); | |
2025 | - if (!op_array) { | |
2026 | - zend_error(E_ERROR, "Error parsing script:%s", fullfile_name); | |
2027 | - return; | |
2028 | - } | |
2029 | - zend_destroy_file_handle(&file_handle TSRMLS_CC); | |
2030 | - | |
2031 | -#ifdef USE_ZEND_EXECUTE | |
2032 | - SIMPLATE_G(global_string.str(std::string())); | |
2033 | - int (*old_output_func)(const char*, unsigned int TSRMLS_DC); | |
2034 | - old_output_func = OG(php_body_write); | |
2035 | - OG(php_body_write) = php_my_output_func; | |
2036 | - zend_execute(op_array TSRMLS_CC); | |
2037 | - OG(php_body_write) = old_output_func; | |
2038 | - RETURN_STRING(const_cast<char*>(SIMPLATE_G(global_string).str().c_str()), 1); | |
2039 | -#else | |
2040 | - // ob_start(); | |
2041 | - zval *output_handler = NULL; | |
2042 | - zend_bool erase = 1; | |
2043 | - long chunk_size = 0; | |
2044 | - if (php_start_ob_buffer(output_handler, chunk_size, erase TSRMLS_CC) == FAILURE) { | |
2045 | - zend_error(E_ERROR, "Error: fail to ob_start"); | |
2046 | - RETURN_FALSE; | |
2047 | - } | |
2048 | - // include | |
2049 | - string include_execute = "include '"; | |
2050 | - include_execute += fullfile_name; | |
2051 | - include_execute += "';"; | |
2052 | - zend_eval_string(const_cast<char*>(include_execute.c_str()), NULL, const_cast<char*>("simplate") TSRMLS_CC); | |
2053 | - | |
2054 | -#ifdef ZEND_ENGINE_2 | |
2055 | - destroy_op_array(op_array TSRMLS_CC); | |
2056 | -#else | |
2057 | - destroy_op_array(op_array); | |
2058 | -#endif // ZEND_ENGINE_2 | |
2059 | - efree(op_array); | |
2060 | - efree(fullfile_name); | |
2061 | - if (php_ob_get_buffer(return_value TSRMLS_CC) == FAILURE) { | |
2062 | - RETURN_FALSE; | |
2063 | - } | |
2064 | - php_end_ob_buffer(0, 0 TSRMLS_CC); | |
2065 | -#endif // USE_ZEND_EXECUTE | |
2066 | - | |
2067 | -DEBUG_PRINTF("%s", "end function"); | |
2068 | -} | |
2069 | -/* }}} */ | |
2070 | - | |
2071 | -#ifdef ZEND_ENGINE_2 | |
2072 | -/* {{{ ZEND_METHOD display | |
2073 | - * | |
2074 | - * @return void | |
2075 | - */ | |
2076 | -ZEND_METHOD(simplate, display) | |
2077 | -#else | |
2078 | -/* {{{ PHP_FUNCTION simplate_display | |
2079 | - * | |
2080 | - * @return void | |
2081 | - */ | |
2082 | -PHP_FUNCTION(simplate_display) | |
2083 | -#endif // ZEND_ENGINE_2 | |
2084 | -{ | |
2085 | -DEBUG_PRINTF("%s", "start function"); | |
2086 | - | |
2087 | - char *fullfile_name = NULL; | |
2088 | - read_parse_template(INTERNAL_FUNCTION_PARAM_PASSTHRU, &fullfile_name, SIMPLATE_DISPLAY); | |
2089 | - if (!fullfile_name || strlen(fullfile_name) <= 0) { | |
2090 | -DEBUG_PRINTF("%s", "end function"); | |
2091 | - return; | |
2092 | - } | |
2093 | - | |
2094 | -DEBUG_PRINTF("full file name = (%s)", fullfile_name); | |
2095 | - | |
2096 | - // | |
2097 | - // Execute compiled file | |
2098 | - // | |
2099 | -#ifdef USE_ZEND_EXECUTE | |
2100 | - zend_file_handle file_handle; | |
2101 | - zend_op_array *op_array; | |
2102 | - file_handle.filename = full_compile_filename; | |
2103 | - file_handle.free_filename = 0; | |
2104 | - file_handle.type = ZEND_HANDLE_FILENAME; | |
2105 | - file_handle.opened_path = NULL; | |
2106 | - op_array = zend_compile_file(&file_handle, ZEND_INCLUDE TSRMLS_CC); | |
2107 | - if (!op_array) { | |
2108 | - zend_error(E_ERROR, "Error parsing script:%s", full_compile_filename.c_str()); | |
2109 | - efree(fullfile_name); | |
2110 | - return; | |
2111 | - } | |
2112 | - zend_destroy_file_handle(&file_handle TSRMLS_CC); | |
2113 | - zend_execute(op_array TSRMLS_CC); | |
2114 | -#else | |
2115 | - // This way is safer | |
2116 | - string include_execute = "include '"; | |
2117 | - include_execute += fullfile_name; | |
2118 | - include_execute += "';"; | |
2119 | - | |
2120 | -DEBUG_PRINTF("include_execute = (%s)", include_execute.c_str()); | |
2121 | - | |
2122 | - zend_eval_string(const_cast<char*>(include_execute.c_str()), NULL, const_cast<char*>("simplate") TSRMLS_CC); | |
2123 | -#endif // USE_ZEND_EXECUTE | |
2124 | - efree(fullfile_name); | |
2125 | - | |
2126 | -DEBUG_PRINTF("%s", "end function"); | |
2127 | - | |
2128 | - return; | |
2129 | -} | |
2130 | -/* }}} */ | |
2131 | - | |
2132 | -#ifdef ZEND_ENGINE_2 | |
2133 | -/* {{{ ZEND_METHOD clear_cache | |
2134 | - * | |
2135 | - * @return void | |
2136 | - */ | |
2137 | -ZEND_METHOD(simplate, clear_cache) | |
2138 | -#else | |
2139 | -/* {{{ PHP_FUNCTION simplate_clear_cache | |
2140 | - * | |
2141 | - * @return void | |
2142 | - */ | |
2143 | -PHP_FUNCTION(simplate_clear_cache) | |
2144 | -#endif // ZEND_ENGINE_2 | |
2145 | -{ | |
2146 | - zval *obj; | |
2147 | - char *resource_name = NULL; | |
2148 | - int resource_name_len = 0; | |
2149 | - long caching = 0; | |
2150 | - char *cache_dir; | |
2151 | - string error; | |
2152 | - | |
2153 | - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, const_cast<char*>("s"), &resource_name, &resource_name_len) == FAILURE) { | |
2154 | - RETURN_FALSE; | |
2155 | - } | |
2156 | - | |
2157 | - obj = getThis(); | |
2158 | -#ifdef ZEND_ENGINE_2 | |
2159 | - caching = Z_LVAL_P(zend_read_property(Z_OBJCE_P(obj), obj, const_cast<char*>(CACHING), strlen(CACHING), 1 TSRMLS_CC)); | |
2160 | -#else | |
2161 | -#endif // ZEND_ENGINE_2 | |
2162 | - if (caching) { | |
2163 | -#ifdef ZEND_ENGINE_2 | |
2164 | - cache_dir = Z_STRVAL_P(zend_read_property(Z_OBJCE_P(obj), obj, const_cast<char*>(CACHE_DIR), strlen(CACHE_DIR), 1 TSRMLS_CC)); | |
2165 | -#else | |
2166 | -#endif // ZEND_ENGINE_2 | |
2167 | - | |
2168 | - if (cache_dir[strlen(cache_dir) - 1] == DEFAULT_SLASH) { | |
2169 | - cache_dir[strlen(cache_dir) - 1] = '\0'; | |
2170 | - } | |
2171 | - | |
2172 | - struct stat cache_stat; | |
2173 | - // cache directory exists? | |
2174 | - if (VCWD_STAT(cache_dir, &cache_stat) != -1) { | |
2175 | - if (!S_ISDIR(cache_stat.st_mode)) { | |
2176 | - zend_error(E_ERROR, "does not exist cache directory:%s", cache_dir); | |
2177 | - } | |
2178 | - } | |
2179 | - string full_cache_filename(cache_dir); | |
2180 | - full_cache_filename += DEFAULT_SLASH; | |
2181 | - full_cache_filename += resource_name; | |
2182 | - if (VCWD_STAT(full_cache_filename.c_str(), &cache_stat) != -1) { | |
2183 | - unlink(full_cache_filename.c_str()); | |
2184 | - } | |
2185 | - } | |
2186 | -} | |
2187 | -/* }}} */ | |
2188 | - | |
2189 | -/* {{{ Registers filter into plugins. | |
2190 | - * | |
2191 | - * @param INTERNAL_FUNCTION_PARAMETERS see zend.h | |
2192 | - * @param char *filter_name filter's name | |
2193 | - * | |
2194 | - * @return void | |
2195 | - */ | |
2196 | -void register_plugins(INTERNAL_FUNCTION_PARAMETERS, char *filter_name) | |
2197 | -{ | |
2198 | - char *function_name = NULL; | |
2199 | - int function_name_len = 0; | |
2200 | - | |
2201 | -DEBUG_PRINTF("%s", "start function"); | |
2202 | - | |
2203 | - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, const_cast<char*>("s"), &function_name, &function_name_len) == FAILURE) { | |
2204 | - return; | |
2205 | - } | |
2206 | - | |
2207 | -DEBUG_PRINTF("filter_name = (%s)", filter_name); | |
2208 | -DEBUG_PRINTF("function_name = (%s)", function_name); | |
2209 | - | |
2210 | - zval *obj = getThis(); | |
2211 | - zval *_plugins; | |
2212 | - | |
2213 | -#ifdef ZEND_ENGINE_2 | |
2214 | - _plugins = zend_read_property(Z_OBJCE_P(obj), obj, const_cast<char*>("_plugins"), strlen("_plugins"), 1 TSRMLS_CC); | |
2215 | - // initialize.use _plugins on the first time. | |
2216 | - if (_plugins == EG(uninitialized_zval_ptr) || Z_TYPE_P(_plugins) == IS_NULL) { | |
2217 | - MAKE_STD_ZVAL(_plugins); | |
2218 | - array_init(_plugins); | |
2219 | - Z_DELREF_P(_plugins); | |
2220 | - } | |
2221 | -#else | |
2222 | -#endif // ZEND_ENGINE_2 | |
2223 | -//php_var_dump(&_plugins, 1 TSRMLS_CC); | |
2224 | - | |
2225 | - zval **zfilter; | |
2226 | - // filter | |
2227 | - if (zend_hash_find(Z_ARRVAL_P(_plugins), filter_name, strlen(filter_name) + 1, (void**)&zfilter) == SUCCESS) { | |
2228 | - | |
2229 | -//php_var_dump(zfilter, 1 TSRMLS_CC); | |
2230 | - add_assoc_string(*zfilter, function_name, function_name, 1); | |
2231 | - | |
2232 | - } else { | |
2233 | - zval *data; | |
2234 | - MAKE_STD_ZVAL(data); | |
2235 | - array_init(data); | |
2236 | - | |
2237 | - add_assoc_string(data, function_name, function_name, 1); | |
2238 | - add_assoc_zval(_plugins, filter_name, data); | |
2239 | - } | |
2240 | - | |
2241 | -#ifdef ZEND_ENGINE_2 | |
2242 | - zend_update_property(Z_OBJCE_P(obj), obj, const_cast<char*>("_plugins"), strlen("_plugins"), _plugins TSRMLS_CC); | |
2243 | -#else | |
2244 | -#endif // ZEND_ENGINE_2 | |
2245 | - | |
2246 | -DEBUG_PRINTF("%s", "end function"); | |
2247 | -} | |
2248 | -/* }}} */ | |
2249 | - | |
2250 | -#ifdef ZEND_ENGINE_2 | |
2251 | -/* {{{ ZEND_METHOD register_prefilter | |
2252 | - * | |
2253 | - * @return void | |
2254 | - */ | |
2255 | -ZEND_METHOD(simplate, register_prefilter) | |
2256 | -#else | |
2257 | -/* {{{ PHP_FUNCTION simplate_register_prefilter | |
2258 | - * | |
2259 | - * @return void | |
2260 | - */ | |
2261 | -PHP_FUNCTION(simplate_register_prefilter) | |
2262 | -#endif // ZEND_ENGINE_2 | |
2263 | -{ | |
2264 | - register_plugins(INTERNAL_FUNCTION_PARAM_PASSTHRU, const_cast<char*>("prefilter")); | |
2265 | -} | |
2266 | -/* }}} */ | |
2267 | - | |
2268 | -#ifdef ZEND_ENGINE_2 | |
2269 | -/* {{{ ZEND_METHOD register_postfilter | |
2270 | - * | |
2271 | - * @return void | |
2272 | - */ | |
2273 | -ZEND_METHOD(simplate, register_postfilter) | |
2274 | -#else | |
2275 | -/* {{{ PHP_FUNCTION simplate_register_postfilter | |
2276 | - * | |
2277 | - * @return void | |
2278 | - */ | |
2279 | -PHP_FUNCTION(simplate_register_postfilter) | |
2280 | -#endif // ZEND_ENGINE_2 | |
2281 | -{ | |
2282 | - register_plugins(INTERNAL_FUNCTION_PARAM_PASSTHRU, const_cast<char*>("postfilter")); | |
2283 | -} | |
2284 | -/* }}} */ | |
2285 | - | |
2286 | -/* | |
2287 | - * Local variables: | |
2288 | - * tab-width: 4 | |
2289 | - * c-basic-offset: 4 | |
2290 | - * indent-tabs-mode: nil | |
2291 | - * End: | |
2292 | - * vim600: noet sw=4 ts=4 sts=4 fdm=marker | |
2293 | - * vim<600: noet sw=4 ts=4 sts=4 | |
2294 | - */ |
@@ -1,31 +0,0 @@ | ||
1 | -Software License Agreement (BSD License) | |
2 | - | |
3 | -Copyright (c) 2009, Kazuhiro Iizuka , The simplate Project Team | |
4 | -All rights reserved. | |
5 | -(see http://sourceforge.jp/projects/simplate/wiki/FrontPage about The simplate Project Team) | |
6 | - | |
7 | -Redistribution and use of this software in source and binary forms, with or without modification, are | |
8 | -permitted provided that the following conditions are met: | |
9 | - | |
10 | -* Redistributions of source code must retain the above | |
11 | - copyright notice, this list of conditions and the | |
12 | - following disclaimer. | |
13 | - | |
14 | -* Redistributions in binary form must reproduce the above | |
15 | - copyright notice, this list of conditions and the | |
16 | - following disclaimer in the documentation and/or other | |
17 | - materials provided with the distribution. | |
18 | - | |
19 | -* Neither the name of Kazuhiro Iizuka nor the names of its | |
20 | - contributors may be used to endorse or promote products | |
21 | - derived from this software without specific prior | |
22 | - written permission of Kazuhiro Iizuka. | |
23 | - | |
24 | -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED | |
25 | -WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | |
26 | -PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |
27 | -ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
28 | -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
29 | -INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | |
30 | -TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
31 | -ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
@@ -1,82 +0,0 @@ | ||
1 | -<?php | |
2 | - | |
3 | -if (!extension_loaded('simplate')) { dl('simplate.so'); } | |
4 | - | |
5 | -define('START_TIME', microtime(true)); | |
6 | -$smarty =& new simplate(); | |
7 | -switch (@$_REQUEST['f']) { | |
8 | -case 2: | |
9 | - $smarty->cache_lifetime = 600; | |
10 | - $smarty->caching = true; | |
11 | - break; | |
12 | -case 1: | |
13 | - $smarty->force_compile = true; | |
14 | - break; | |
15 | -default: | |
16 | - $smarty->force_compile = false; | |
17 | -} | |
18 | -//$smarty->force_compile=true; | |
19 | - | |
20 | -$hoge="aaaa"; | |
21 | -$smarty->assign("hoge1", $hoge); // Seg fault?? | |
22 | -$smarty->assign("myname", "Simplate"); // set string | |
23 | -$smarty->assign("greeting", "hello"); // set string | |
24 | -$smarty->assign("date", date("Y-m-d H:i:s")); // set string | |
25 | -$smarty->assign("ary", array("hoge1", "hoge2", "hoge3")); | |
26 | -$smarty->assign("ary2", array("foo1" => "bar1", "foo2" => "bar2", "foo3" => "bar3")); | |
27 | -$smarty->assign("custid", array("001-0001", "002-0003", "010-9999")); | |
28 | -$smarty->assign('contacts', | |
29 | - array( | |
30 | - array( | |
31 | - 'phone' => '1', | |
32 | - 'fax' => '2', | |
33 | - 'cell' => '3'), | |
34 | - array( | |
35 | - 'phone' => '555-4444', | |
36 | - 'fax' => '555-3333', | |
37 | - 'cell' => '760-1234') | |
38 | - )); | |
39 | -//$fetch=$smarty->fetch('fetch.tpl'); | |
40 | -$smarty->assign("fetched_content", $smarty->fetch('fetch.tpl')); | |
41 | -//echo "fetch=$fetch"; | |
42 | - | |
43 | -$entry = array( | |
44 | - array( | |
45 | - "entry_id" => "hoge", | |
46 | - "comments" => array( | |
47 | - array("comment_id" => "comment1"), | |
48 | - array("comment_id" => "comment2") | |
49 | - ) | |
50 | - ) | |
51 | -); | |
52 | -$smarty->assign("entry", $entry); | |
53 | -$smarty->register_prefilter('remove_comment'); | |
54 | -$smarty->register_postfilter('convert_ad'); | |
55 | - | |
56 | -//$smarty->assign("fetched_content", $fetch); | |
57 | -//$smarty->assign("hoge2", $hoge); // Seg fault?? | |
58 | - | |
59 | -//$smarty->clear_assign("date"); // assign with null | |
60 | -//$smarty->assign("date", null); | |
61 | -//$smarty->clear_assign(array("date", "greeting")); | |
62 | -//var_dump($smarty); | |
63 | -//$smarty->fetch("sample.tpl"); | |
64 | -$smarty->display("simplate.tpl"); | |
65 | -//$smarty->clear_cache("simplate.tpl"); | |
66 | - | |
67 | -$end_time = sprintf("%01.04f", microtime(true) - START_TIME); | |
68 | -echo "<font color=red>$end_time</font>\n"; | |
69 | - | |
70 | -function remove_comment($source) | |
71 | -{ | |
72 | - return preg_replace("/<!--.*-->/U", "", $source); | |
73 | -} | |
74 | - | |
75 | -function convert_ad($source) | |
76 | -{ | |
77 | - $before = array('/<\/body>/'); | |
78 | - $after = array('<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script></body>'); | |
79 | - | |
80 | - return preg_replace($before, $after, $source); | |
81 | -} | |
82 | -?> |
@@ -1,64 +0,0 @@ | ||
1 | -dnl $Id$ | |
2 | -dnl config.m4 for extension simplate | |
3 | - | |
4 | -dnl Comments in this file start with the string 'dnl'. | |
5 | -dnl Remove where necessary. This file will not work | |
6 | -dnl without editing. | |
7 | - | |
8 | -dnl If your extension references something external, use with: | |
9 | - | |
10 | -dnl PHP_ARG_WITH(simplate, for simplate support, | |
11 | -dnl Make sure that the comment is aligned: | |
12 | -dnl [ --with-simplate Include simplate support]) | |
13 | - | |
14 | -dnl Otherwise use enable: | |
15 | - | |
16 | -PHP_ARG_ENABLE(simplate, whether to enable simplate support, | |
17 | -dnl Make sure that the comment is aligned: | |
18 | -[ --enable-simplate Enable simplate support]) | |
19 | - | |
20 | -if test "$PHP_SIMPLATE" != "no"; then | |
21 | - dnl Write more examples of tests here... | |
22 | - | |
23 | - dnl # --with-simplate -> check with-path | |
24 | - dnl SEARCH_PATH="/usr/local /usr" # you might want to change this | |
25 | - dnl SEARCH_FOR="/include/simplate.h" # you most likely want to change this | |
26 | - dnl if test -r $PHP_SIMPLATE/$SEARCH_FOR; then # path given as parameter | |
27 | - dnl SIMPLATE_DIR=$PHP_SIMPLATE | |
28 | - dnl else # search default path list | |
29 | - dnl AC_MSG_CHECKING([for simplate files in default path]) | |
30 | - dnl for i in $SEARCH_PATH ; do | |
31 | - dnl if test -r $i/$SEARCH_FOR; then | |
32 | - dnl SIMPLATE_DIR=$i | |
33 | - dnl AC_MSG_RESULT(found in $i) | |
34 | - dnl fi | |
35 | - dnl done | |
36 | - dnl fi | |
37 | - dnl | |
38 | - dnl if test -z "$SIMPLATE_DIR"; then | |
39 | - dnl AC_MSG_RESULT([not found]) | |
40 | - dnl AC_MSG_ERROR([Please reinstall the simplate distribution]) | |
41 | - dnl fi | |
42 | - | |
43 | - dnl # --with-simplate -> add include path | |
44 | - dnl PHP_ADD_INCLUDE($SIMPLATE_DIR/include) | |
45 | - | |
46 | - dnl # --with-simplate -> check for lib and symbol presence | |
47 | - dnl LIBNAME=simplate # you may want to change this | |
48 | - dnl LIBSYMBOL=simplate # you most likely want to change this | |
49 | - | |
50 | - dnl PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL, | |
51 | - dnl [ | |
52 | - dnl PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $SIMPLATE_DIR/lib, SIMPLATE_SHARED_LIBADD) | |
53 | - dnl AC_DEFINE(HAVE_SIMPLATELIB,1,[ ]) | |
54 | - dnl ],[ | |
55 | - dnl AC_MSG_ERROR([wrong simplate lib version or lib not found]) | |
56 | - dnl ],[ | |
57 | - dnl -L$SIMPLATE_DIR/lib -lm -ldl | |
58 | - dnl ]) | |
59 | - dnl | |
60 | - PHP_SUBST(SIMPLATE_SHARED_LIBADD) | |
61 | - PHP_ADD_LIBRARY(stdc++,1,SIMPLATE_SHARED_LIBADD) | |
62 | - PHP_REQUIRE_CXX() | |
63 | - PHP_NEW_EXTENSION(simplate, simplate.cpp, $ext_shared) | |
64 | -fi |
@@ -1,123 +0,0 @@ | ||
1 | -2009-10-17 FUJIOKA Satoshi <simpalte @ gmail.com> Version:0.4.2 | |
2 | - * 保守性向上のためコメント記載 | |
3 | - * php5.3 以上に対応 ZVAL_xxx を Z_xxx に変更し互換を保つためにマクロ定義 | |
4 | - * fix #15205 コメント直後の if 文が正しくパースされないバグを修正 | |
5 | - * fix #19139 Windows 版でも compile_dir の作成を再帰的に作成するようにした。 | |
6 | - | |
7 | -2008-04-07 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.4.0 | |
8 | - * if文中のparseに関数を使えるように、parserの書き換え。 | |
9 | - | |
10 | -2007-10-03 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.3.8 | |
11 | - * force_compileがfalseでinclude file="$var"の処理が漏れていたため、対応した。 | |
12 | - * 今回からtestsを有効利用するようにしたが、Makefileでextension_dirを指定してphpを実行するため、他のextensionを利用していると、失敗してしまう。 | |
13 | - - test:のところに-Uの前辺りに、-nを追加して、make testすれば、通常通りにテストできる。 | |
14 | - | |
15 | -2007-09-21 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.3.7 | |
16 | - * compile_dirの作成を再帰的に作成するようにした。 | |
17 | - | |
18 | -2007-06-19 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.3.6 | |
19 | - * sectionのfirst,lastのサポート | |
20 | - | |
21 | -2007-06-18 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.3.5 | |
22 | - * revuloさんによるパッチの追加 | |
23 | - - compile_checkの実装を追加。 | |
24 | - - デリミタを{}にしたときの{php}タグの挙動の不具合修正。 | |
25 | - * \0を含んだ文字をassignしたときの不具合修正。 | |
26 | - | |
27 | -2007-05-14 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.3.4 | |
28 | - * simplateオブジェクトを継承したメソッドをテンプレート内で使えるように、$thisをテンプレート内で記述できるようにした。 | |
29 | - | |
30 | -2007-02-05 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.3.3 | |
31 | - * include file="$var.tpl"の形式もサポート | |
32 | - * force compileの不具合修正 | |
33 | - | |
34 | -2007-01-19 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.3.2 | |
35 | - * 64ビット環境でも動くように修正。 | |
36 | - | |
37 | -2006-10-30 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.3.1 | |
38 | - * register_{pre,post}filterの実装。とりあえず、unregister_{pre,post}filterは、実装しない。 | |
39 | - | |
40 | -2006-10-26 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.2.9 | |
41 | - * xmlタグを複数行(XSLTなど)書けるようにした。 | |
42 | - | |
43 | -2006-07-08 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.2.8 | |
44 | - * <?xml encoding="<{$encoding}>" ?>のような書き方ができないバグの修正。 | |
45 | - * foreach文をサポート | |
46 | - * デリミタを{,}と設定できないバグ(どこかのバージョンでデグレードしていたようです。) | |
47 | - | |
48 | -2006-03-09 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.2.7 | |
49 | - * Windows版でも使えるように修正(静的キャッシュ機能は使えない) | |
50 | - * 変数内で関数も使えるように修正。<{htmlspecialchars($var)}>や<{user_function($var)}>のような自作関数も使えます。 | |
51 | - * 静的キャッシュ機能でfetchしたときの不具合一部修正。 | |
52 | - | |
53 | -2006-02-05 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.2.6 | |
54 | - * 清水さんのpatchにより、幾つかの修正。 | |
55 | - - windows版でもコンパイルできるようにTSRMLSの修正。 | |
56 | - - 幾つかののメモリリークの修正。 | |
57 | - | |
58 | -2006-02-04 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.2.5 | |
59 | - * デグレードして、PHP4でコンパイルができなくなっていたので、その修正を行う。 | |
60 | - * zend_read_property後、型のチェックをするようにした。 | |
61 | - | |
62 | -2006-01-22 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.2.4 | |
63 | - * オブジェクト変数が使えるようにparse部分修正 | |
64 | - | |
65 | -2005-12-29 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.2.3 | |
66 | - * PHP-5.1.1におけるzend extension(Xdebug)との相性問題解決 | |
67 | - | |
68 | -2005-12-27 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.2.2 | |
69 | - * キャッシュの実装 | |
70 | - | |
71 | -2005-12-17 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.2.1 | |
72 | - * php.ini内で設定したopen_basedirの設定をチェックするように修正。(/etc/passwdのようなincludeをさせないため) | |
73 | - * コンパイル済みのチェック時にincludeしたファイルを全てチェックしているが、それを行うと、fstatの部分が遅いようなので、includeのファイル数が多いと、force_compileしている方が高速になってしまうため、表示するファイルのみのタイムスタンプをチェックする方式も利用できるようにlazy_check変数を導入する。 | |
74 | - | |
75 | -2005-12-01 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.2.0 | |
76 | - * 0から書き直してみて、ロジックの整理。 | |
77 | - * CLIでの使用時にでていたSegmentation faultの修正。 | |
78 | - * <{$tag_name+1}>のような形式が表示できなかったバグ修正。 | |
79 | - | |
80 | -2005-11-02 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.1.3 | |
81 | - * section以外でも連想配列形式($people.phone.mobile)の書式をサポート | |
82 | - | |
83 | -2005-10-05 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.1.2 | |
84 | - * simplateクラスにバージョン情報も与えた。さらに、グローバルのオブジェクトを安全に使えるようにマクロを使ってアクセスするようにした。 | |
85 | - | |
86 | -2005-09-14 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.1.1 | |
87 | - * xmlテンプレートを使えるようにした。 | |
88 | - | |
89 | -2005-08-31 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.1.0 | |
90 | - * util.ccのget_element部分の書き換え。今までは、key="value"しかサポートしていなかった。key = "value"とかも取れるようにした。 | |
91 | - | |
92 | -2005-08-27 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.0.10 | |
93 | - * エラーのクリアし忘れているバグの修正 | |
94 | - * section内で使える新しい記法の追加($entry[index].key) | |
95 | - | |
96 | -2005-08-15 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.0.9 | |
97 | - * 多次元配列をassignできるようにした(php4) | |
98 | - * assignメソッドのコードを整理。 | |
99 | - * 初期化関数の整理。 | |
100 | - * fetchのバッファリングをphp4/5で共通化 | |
101 | - | |
102 | -2005-08-13 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.0.8 | |
103 | - * DOUBLE,LONG型の変数がassignできなくなるバグが入り込んでしまったため、その修正。(php5のみ) | |
104 | - | |
105 | -2005-08-12 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.0.6,0.0.7 | |
106 | - * literal,elseifのサポート、delimiterが変更できないバグの修正 | |
107 | - * 多次元配列をassignできるようにした(php5のみ) | |
108 | - | |
109 | -2005-08-09 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.0.5 | |
110 | - * simplate内部変数を使えるようにした | |
111 | - | |
112 | -2005-08-07 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.0.4 | |
113 | - * fetchを追加 | |
114 | - | |
115 | -2005-08-04 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.0.3 | |
116 | - * lib_smartyでコンパイルエラーが発生しているようなので、util.{cc,h}でusing std::string;するように修正 | |
117 | - | |
118 | -2005-08-03 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.0.2 | |
119 | - * php4でも使えるように更新。 | |
120 | - | |
121 | -2005-08-01 Kazuhiro IIzuka <simplate @ gmail.com> Version:0.0.1 | |
122 | - * Simplate公開 | |
123 | - |