1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # |
---|
4 | # This file is part of Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2011 Olivier Meunier & Association Dotclear |
---|
7 | # Licensed under the GPL version 2.0 license. |
---|
8 | # See LICENSE file or |
---|
9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
10 | # |
---|
11 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
12 | if (!defined('DC_RC_PATH')) { return; } |
---|
13 | |
---|
14 | class context |
---|
15 | { |
---|
16 | public $stack = array(); |
---|
17 | |
---|
18 | public function __set($name,$var) |
---|
19 | { |
---|
20 | if ($var === null) { |
---|
21 | $this->pop($name); |
---|
22 | } else { |
---|
23 | $this->stack[$name][] =& $var; |
---|
24 | if ($var instanceof record) { |
---|
25 | $this->stack['cur_loop'][] =& $var; |
---|
26 | } |
---|
27 | } |
---|
28 | } |
---|
29 | |
---|
30 | public function __get($name) |
---|
31 | { |
---|
32 | if (!isset($this->stack[$name])) { |
---|
33 | return null; |
---|
34 | } |
---|
35 | |
---|
36 | $n = count($this->stack[$name]); |
---|
37 | if ($n > 0) { |
---|
38 | return $this->stack[$name][($n-1)]; |
---|
39 | } |
---|
40 | |
---|
41 | return null; |
---|
42 | } |
---|
43 | |
---|
44 | public function exists($name) |
---|
45 | { |
---|
46 | return isset($this->stack[$name][0]); |
---|
47 | } |
---|
48 | |
---|
49 | public function pop($name) |
---|
50 | { |
---|
51 | if (isset($this->stack[$name])) { |
---|
52 | $v = array_pop($this->stack[$name]); |
---|
53 | if ($v instanceof record) { |
---|
54 | array_pop($this->stack['cur_loop']); |
---|
55 | } |
---|
56 | unset($v); |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | # Loop position tests |
---|
61 | public function loopPosition($start,$length=null,$even=null) |
---|
62 | { |
---|
63 | if (!$this->cur_loop) { |
---|
64 | return false; |
---|
65 | } |
---|
66 | |
---|
67 | $index = $this->cur_loop->index(); |
---|
68 | $size = $this->cur_loop->count(); |
---|
69 | |
---|
70 | $test = false; |
---|
71 | if ($start >= 0) |
---|
72 | { |
---|
73 | $test = $index >= $start; |
---|
74 | if ($length !== null) { |
---|
75 | if ($length >= 0) { |
---|
76 | $test = $test && $index < $start + $length; |
---|
77 | } else { |
---|
78 | $test = $test && $index < $size + $length; |
---|
79 | } |
---|
80 | } |
---|
81 | } |
---|
82 | else |
---|
83 | { |
---|
84 | $test = $index >= $size + $start; |
---|
85 | if ($length !== null) { |
---|
86 | if ($length >= 0) { |
---|
87 | $test = $test && $index < $size + $start + $length; |
---|
88 | } else { |
---|
89 | $test = $test && $index < $size + $length; |
---|
90 | } |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | if ($even !== null) { |
---|
95 | $test = $test && $index%2 == $even; |
---|
96 | } |
---|
97 | |
---|
98 | return $test; |
---|
99 | } |
---|
100 | |
---|
101 | |
---|
102 | # Static methods |
---|
103 | public static function global_filter($str, |
---|
104 | $encode_xml, $remove_html, $cut_string, $lower_case, $upper_case ,$tag='') |
---|
105 | { |
---|
106 | $args = func_get_args(); |
---|
107 | array_pop($args); |
---|
108 | $args[0] =& $str; |
---|
109 | |
---|
110 | # --BEHAVIOR-- publicBeforeContentFilter |
---|
111 | $res = $GLOBALS['core']->callBehavior('publicBeforeContentFilter',$GLOBALS['core'],$tag,$args); |
---|
112 | |
---|
113 | if ($remove_html) { |
---|
114 | $str = self::remove_html($str); |
---|
115 | $str = preg_replace('/\s+/',' ',$str); |
---|
116 | } |
---|
117 | |
---|
118 | if ($encode_xml) { |
---|
119 | $str = self::encode_xml($str); |
---|
120 | } |
---|
121 | |
---|
122 | if ($cut_string) { |
---|
123 | $str = self::cut_string($str,(integer) $cut_string); |
---|
124 | } |
---|
125 | |
---|
126 | if ($lower_case) { |
---|
127 | $str = self::lower_case($str); |
---|
128 | } elseif ($upper_case) { |
---|
129 | if ($upper_case == 2) { |
---|
130 | $str = self::capitalize($str); |
---|
131 | } else { |
---|
132 | $str = self::upper_case($str); |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | # --BEHAVIOR-- publicAfterContentFilter |
---|
137 | $res = $GLOBALS['core']->callBehavior('publicAfterContentFilter',$GLOBALS['core'],$tag,$args); |
---|
138 | |
---|
139 | return $str; |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | public static function cut_string($str,$l) |
---|
144 | { |
---|
145 | return text::cutString($str,$l); |
---|
146 | } |
---|
147 | |
---|
148 | public static function encode_xml($str) |
---|
149 | { |
---|
150 | return html::escapeHTML($str); |
---|
151 | } |
---|
152 | |
---|
153 | public static function remove_html($str) |
---|
154 | { |
---|
155 | return html::decodeEntities(html::clean($str)); |
---|
156 | } |
---|
157 | |
---|
158 | public static function lower_case($str) |
---|
159 | { |
---|
160 | return mb_strtolower($str); |
---|
161 | } |
---|
162 | |
---|
163 | public static function upper_case($str) |
---|
164 | { |
---|
165 | return mb_strtoupper($str); |
---|
166 | } |
---|
167 | |
---|
168 | public static function capitalize($str) |
---|
169 | { |
---|
170 | if ($str != '') { |
---|
171 | $str[0] = mb_strtoupper($str[0]); |
---|
172 | } |
---|
173 | return $str; |
---|
174 | } |
---|
175 | |
---|
176 | # Static methods for pagination |
---|
177 | public static function PaginationNbPages() |
---|
178 | { |
---|
179 | global $_ctx; |
---|
180 | |
---|
181 | if ($_ctx->pagination === null) { |
---|
182 | return false; |
---|
183 | } |
---|
184 | |
---|
185 | $nb_posts = $_ctx->pagination->f(0); |
---|
186 | $nb_per_page = $_ctx->post_params['limit'][1]; |
---|
187 | |
---|
188 | $nb_pages = ceil($nb_posts/$nb_per_page); |
---|
189 | |
---|
190 | return $nb_pages; |
---|
191 | } |
---|
192 | |
---|
193 | public static function PaginationPosition($offset=0) |
---|
194 | { |
---|
195 | if (isset($GLOBALS['_page_number'])) { |
---|
196 | $p = $GLOBALS['_page_number']; |
---|
197 | } else { |
---|
198 | $p = 1; |
---|
199 | } |
---|
200 | |
---|
201 | $p = $p+$offset; |
---|
202 | |
---|
203 | $n = self::PaginationNbPages(); |
---|
204 | if (!$n) { |
---|
205 | return $p; |
---|
206 | } |
---|
207 | |
---|
208 | if ($p > $n || $p <= 0) { |
---|
209 | return 1; |
---|
210 | } else { |
---|
211 | return $p; |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | public static function PaginationStart() |
---|
216 | { |
---|
217 | if (isset($GLOBALS['_page_number'])) { |
---|
218 | return self::PaginationPosition() == 1; |
---|
219 | } |
---|
220 | |
---|
221 | return true; |
---|
222 | } |
---|
223 | |
---|
224 | public static function PaginationEnd() |
---|
225 | { |
---|
226 | if (isset($GLOBALS['_page_number'])) { |
---|
227 | return self::PaginationPosition() == self::PaginationNbPages(); |
---|
228 | } |
---|
229 | |
---|
230 | return false; |
---|
231 | } |
---|
232 | |
---|
233 | public static function PaginationURL($offset=0) |
---|
234 | { |
---|
235 | $args = $_SERVER['URL_REQUEST_PART']; |
---|
236 | |
---|
237 | $n = self::PaginationPosition($offset); |
---|
238 | |
---|
239 | $args = preg_replace('#(^|/)page/([0-9]+)$#','',$args); |
---|
240 | |
---|
241 | $url = $GLOBALS['core']->blog->url.$args; |
---|
242 | |
---|
243 | if ($n > 1) { |
---|
244 | $url = preg_replace('#/$#','',$url); |
---|
245 | $url .= '/page/'.$n; |
---|
246 | } |
---|
247 | |
---|
248 | # If search param |
---|
249 | if (!empty($_GET['q'])) { |
---|
250 | $s = strpos($url,'?') !== false ? '&' : '?'; |
---|
251 | $url .= $s.'q='.rawurlencode($_GET['q']); |
---|
252 | } |
---|
253 | return $url; |
---|
254 | } |
---|
255 | |
---|
256 | # Robots policy |
---|
257 | public static function robotsPolicy($base,$over) |
---|
258 | { |
---|
259 | $pol = array('INDEX' => 'INDEX','FOLLOW' => 'FOLLOW', 'ARCHIVE' => 'ARCHIVE'); |
---|
260 | $base = array_flip(preg_split('/\s*,\s*/',$base)); |
---|
261 | $over = array_flip(preg_split('/\s*,\s*/',$over)); |
---|
262 | |
---|
263 | foreach ($pol as $k => &$v) |
---|
264 | { |
---|
265 | if (isset($base[$k]) || isset($base['NO'.$k])) { |
---|
266 | $v = isset($base['NO'.$k]) ? 'NO'.$k : $k; |
---|
267 | } |
---|
268 | if (isset($over[$k]) || isset($over['NO'.$k])) { |
---|
269 | $v = isset($over['NO'.$k]) ? 'NO'.$k : $k; |
---|
270 | } |
---|
271 | } |
---|
272 | |
---|
273 | if ($pol['ARCHIVE'] == 'ARCHIVE') { |
---|
274 | unset($pol['ARCHIVE']); |
---|
275 | } |
---|
276 | |
---|
277 | return implode(', ',$pol); |
---|
278 | } |
---|
279 | |
---|
280 | # Smilies static methods |
---|
281 | public static function getSmilies($blog) |
---|
282 | { |
---|
283 | $path = array(); |
---|
284 | if (isset($GLOBALS['__theme'])) { |
---|
285 | $path[] = $GLOBALS['__theme']; |
---|
286 | } |
---|
287 | $path[] = 'default'; |
---|
288 | $definition = $blog->themes_path.'/%s/smilies/smilies.txt'; |
---|
289 | $base_url = $blog->settings->system->themes_url.'/%s/smilies/'; |
---|
290 | |
---|
291 | $res = array(); |
---|
292 | |
---|
293 | foreach ($path as $t) |
---|
294 | { |
---|
295 | if (file_exists(sprintf($definition,$t))) { |
---|
296 | $base_url = sprintf($base_url,$t); |
---|
297 | return self::smiliesDefinition(sprintf($definition,$t),$base_url); |
---|
298 | } |
---|
299 | } |
---|
300 | return false; |
---|
301 | } |
---|
302 | |
---|
303 | public static function smiliesDefinition($f,$url) |
---|
304 | { |
---|
305 | $def = file($f); |
---|
306 | |
---|
307 | $res = array(); |
---|
308 | foreach($def as $v) |
---|
309 | { |
---|
310 | $v = trim($v); |
---|
311 | if (preg_match('|^([^\t]*)[\t]+(.*)$|',$v,$matches)) |
---|
312 | { |
---|
313 | $r = '/(\A|[\s]+|>)('.preg_quote($matches[1],'/').')([\s]+|[<]|\Z)/ms'; |
---|
314 | $s = '$1<img src="'.$url.$matches[2].'" '. |
---|
315 | 'alt="$2" class="smiley" />$3'; |
---|
316 | $res[$r] = $s; |
---|
317 | } |
---|
318 | } |
---|
319 | |
---|
320 | return $res; |
---|
321 | } |
---|
322 | |
---|
323 | public static function addSmilies($str) |
---|
324 | { |
---|
325 | if (!isset($GLOBALS['__smilies']) || !is_array($GLOBALS['__smilies'])) { |
---|
326 | return $str; |
---|
327 | } |
---|
328 | |
---|
329 | # Process part adapted from SmartyPants engine (J. Gruber et al.) : |
---|
330 | |
---|
331 | $tokens = self::tokenizeHTML($str); |
---|
332 | $result = ''; |
---|
333 | $in_pre = 0; # Keep track of when we're inside <pre> or <code> tags. |
---|
334 | |
---|
335 | foreach ($tokens as $cur_token) { |
---|
336 | if ($cur_token[0] == "tag") { |
---|
337 | # Don't mess with quotes inside tags. |
---|
338 | $result .= $cur_token[1]; |
---|
339 | if (preg_match('@<(/?)(?:pre|code|kbd|script|math)[\s>]@', $cur_token[1], $matches)) { |
---|
340 | $in_pre = isset($matches[1]) && $matches[1] == '/' ? 0 : 1; |
---|
341 | } |
---|
342 | } else { |
---|
343 | $t = $cur_token[1]; |
---|
344 | if (!$in_pre) { |
---|
345 | $t = preg_replace(array_keys($GLOBALS['__smilies']),array_values($GLOBALS['__smilies']),$t); |
---|
346 | } |
---|
347 | $result .= $t; |
---|
348 | } |
---|
349 | } |
---|
350 | |
---|
351 | return $result; |
---|
352 | } |
---|
353 | |
---|
354 | private static function tokenizeHTML($str) |
---|
355 | { |
---|
356 | # Function from SmartyPants engine (J. Gruber et al.) |
---|
357 | # |
---|
358 | # Parameter: String containing HTML markup. |
---|
359 | # Returns: An array of the tokens comprising the input |
---|
360 | # string. Each token is either a tag (possibly with nested, |
---|
361 | # tags contained therein, such as <a href="<MTFoo>">, or a |
---|
362 | # run of text between tags. Each element of the array is a |
---|
363 | # two-element array; the first is either 'tag' or 'text'; |
---|
364 | # the second is the actual value. |
---|
365 | # |
---|
366 | # |
---|
367 | # Regular expression derived from the _tokenize() subroutine in |
---|
368 | # Brad Choate's MTRegex plugin. |
---|
369 | # <http://www.bradchoate.com/past/mtregex.php> |
---|
370 | # |
---|
371 | $index = 0; |
---|
372 | $tokens = array(); |
---|
373 | |
---|
374 | $match = '(?s:<!(?:--.*?--\s*)+>)|'. # comment |
---|
375 | '(?s:<\?.*?\?>)|'. # processing instruction |
---|
376 | # regular tags |
---|
377 | '(?:<[/!$]?[-a-zA-Z0-9:]+\b(?>[^"\'>]+|"[^"]*"|\'[^\']*\')*>)'; |
---|
378 | |
---|
379 | $parts = preg_split("{($match)}", $str, -1, PREG_SPLIT_DELIM_CAPTURE); |
---|
380 | |
---|
381 | foreach ($parts as $part) { |
---|
382 | if (++$index % 2 && $part != '') |
---|
383 | $tokens[] = array('text', $part); |
---|
384 | else |
---|
385 | $tokens[] = array('tag', $part); |
---|
386 | } |
---|
387 | return $tokens; |
---|
388 | } |
---|
389 | |
---|
390 | } |
---|
391 | ?> |
---|