1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # |
---|
4 | # This file is part of Dotclear 2. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2013 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,$modulo=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 | if ($modulo !== null) { |
---|
99 | $test = $test && ($index % $modulo == 0); |
---|
100 | } |
---|
101 | |
---|
102 | return $test; |
---|
103 | } |
---|
104 | |
---|
105 | # Static methods |
---|
106 | public static function global_filter($str, |
---|
107 | $encode_xml, $remove_html, $cut_string, $lower_case, $upper_case ,$encode_url ,$tag='') |
---|
108 | { |
---|
109 | $args = func_get_args(); |
---|
110 | array_pop($args); |
---|
111 | $args[0] =& $str; |
---|
112 | |
---|
113 | # --BEHAVIOR-- publicBeforeContentFilter |
---|
114 | $res = $GLOBALS['core']->callBehavior('publicBeforeContentFilter',$GLOBALS['core'],$tag,$args); |
---|
115 | |
---|
116 | if ($remove_html) { |
---|
117 | $str = self::remove_html($str); |
---|
118 | $str = preg_replace('/\s+/',' ',$str); |
---|
119 | } |
---|
120 | |
---|
121 | if ($encode_xml) { |
---|
122 | $str = self::encode_xml($str); |
---|
123 | } |
---|
124 | |
---|
125 | if ($cut_string) { |
---|
126 | $str = self::cut_string($str,(integer) $cut_string); |
---|
127 | } |
---|
128 | |
---|
129 | if ($lower_case) { |
---|
130 | $str = self::lower_case($str); |
---|
131 | } elseif ($upper_case) { |
---|
132 | if ($upper_case == 2) { |
---|
133 | $str = self::capitalize($str); |
---|
134 | } else { |
---|
135 | $str = self::upper_case($str); |
---|
136 | } |
---|
137 | } |
---|
138 | if ($encode_url) { |
---|
139 | $str = self::encode_url($str); |
---|
140 | } |
---|
141 | |
---|
142 | # --BEHAVIOR-- publicAfterContentFilter |
---|
143 | $res = $GLOBALS['core']->callBehavior('publicAfterContentFilter',$GLOBALS['core'],$tag,$args); |
---|
144 | |
---|
145 | return $str; |
---|
146 | } |
---|
147 | |
---|
148 | public static function encode_url($str) |
---|
149 | { |
---|
150 | return urlencode($str); |
---|
151 | } |
---|
152 | |
---|
153 | public static function cut_string($str,$l) |
---|
154 | { |
---|
155 | return text::cutString($str,$l); |
---|
156 | } |
---|
157 | |
---|
158 | public static function encode_xml($str) |
---|
159 | { |
---|
160 | return html::escapeHTML($str); |
---|
161 | } |
---|
162 | |
---|
163 | public static function remove_html($str) |
---|
164 | { |
---|
165 | return html::decodeEntities(html::clean($str)); |
---|
166 | } |
---|
167 | |
---|
168 | public static function lower_case($str) |
---|
169 | { |
---|
170 | return mb_strtolower($str); |
---|
171 | } |
---|
172 | |
---|
173 | public static function upper_case($str) |
---|
174 | { |
---|
175 | return mb_strtoupper($str); |
---|
176 | } |
---|
177 | |
---|
178 | public static function capitalize($str) |
---|
179 | { |
---|
180 | if ($str != '') { |
---|
181 | $str[0] = mb_strtoupper($str[0]); |
---|
182 | } |
---|
183 | return $str; |
---|
184 | } |
---|
185 | |
---|
186 | public static function categoryPostParam(&$p) |
---|
187 | { |
---|
188 | $not = substr($p['cat_url'],0,1) == '!'; |
---|
189 | if ($not) { |
---|
190 | $p['cat_url'] = substr($p['cat_url'],1); |
---|
191 | } |
---|
192 | |
---|
193 | $p['cat_url'] = preg_split('/\s*,\s*/',$p['cat_url'],-1,PREG_SPLIT_NO_EMPTY); |
---|
194 | |
---|
195 | foreach ($p['cat_url'] as &$v) |
---|
196 | { |
---|
197 | if ($not) { |
---|
198 | $v .= ' ?not'; |
---|
199 | } |
---|
200 | if ($GLOBALS['_ctx']->exists('categories') && preg_match('/#self/',$v)) { |
---|
201 | $v = preg_replace('/#self/',$GLOBALS['_ctx']->categories->cat_url,$v); |
---|
202 | } elseif ($GLOBALS['_ctx']->exists('posts') && preg_match('/#self/',$v)) { |
---|
203 | $v = preg_replace('/#self/',$GLOBALS['_ctx']->posts->cat_url,$v); |
---|
204 | } |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | # Static methods for pagination |
---|
209 | public static function PaginationNbPages() |
---|
210 | { |
---|
211 | global $_ctx; |
---|
212 | |
---|
213 | if ($_ctx->pagination === null) { |
---|
214 | return false; |
---|
215 | } |
---|
216 | |
---|
217 | $nb_posts = $_ctx->pagination->f(0); |
---|
218 | if (($GLOBALS['core']->url->type == 'default') || ($GLOBALS['core']->url->type == 'default-page')) { |
---|
219 | $nb_pages = ceil(($nb_posts - $_ctx->nb_entry_first_page) / $_ctx->nb_entry_per_page + 1); |
---|
220 | } else { |
---|
221 | $nb_pages = ceil($nb_posts / $_ctx->nb_entry_per_page); |
---|
222 | } |
---|
223 | |
---|
224 | return $nb_pages; |
---|
225 | } |
---|
226 | |
---|
227 | public static function PaginationPosition($offset=0) |
---|
228 | { |
---|
229 | if (isset($GLOBALS['_page_number'])) { |
---|
230 | $p = $GLOBALS['_page_number']; |
---|
231 | } else { |
---|
232 | $p = 1; |
---|
233 | } |
---|
234 | |
---|
235 | $p = $p+$offset; |
---|
236 | |
---|
237 | $n = self::PaginationNbPages(); |
---|
238 | if (!$n) { |
---|
239 | return $p; |
---|
240 | } |
---|
241 | |
---|
242 | if ($p > $n || $p <= 0) { |
---|
243 | return 1; |
---|
244 | } else { |
---|
245 | return $p; |
---|
246 | } |
---|
247 | } |
---|
248 | |
---|
249 | public static function PaginationStart() |
---|
250 | { |
---|
251 | if (isset($GLOBALS['_page_number'])) { |
---|
252 | return self::PaginationPosition() == 1; |
---|
253 | } |
---|
254 | |
---|
255 | return true; |
---|
256 | } |
---|
257 | |
---|
258 | public static function PaginationEnd() |
---|
259 | { |
---|
260 | if (isset($GLOBALS['_page_number'])) { |
---|
261 | return self::PaginationPosition() == self::PaginationNbPages(); |
---|
262 | } |
---|
263 | |
---|
264 | return false; |
---|
265 | } |
---|
266 | |
---|
267 | public static function PaginationURL($offset=0) |
---|
268 | { |
---|
269 | $args = $_SERVER['URL_REQUEST_PART']; |
---|
270 | |
---|
271 | $n = self::PaginationPosition($offset); |
---|
272 | |
---|
273 | $args = preg_replace('#(^|/)page/([0-9]+)$#','',$args); |
---|
274 | |
---|
275 | $url = $GLOBALS['core']->blog->url.$args; |
---|
276 | |
---|
277 | if ($n > 1) { |
---|
278 | $url = preg_replace('#/$#','',$url); |
---|
279 | $url .= '/page/'.$n; |
---|
280 | } |
---|
281 | |
---|
282 | # If search param |
---|
283 | if (!empty($_GET['q'])) { |
---|
284 | $s = strpos($url,'?') !== false ? '&' : '?'; |
---|
285 | $url .= $s.'q='.rawurlencode($_GET['q']); |
---|
286 | } |
---|
287 | return $url; |
---|
288 | } |
---|
289 | |
---|
290 | # Robots policy |
---|
291 | public static function robotsPolicy($base,$over) |
---|
292 | { |
---|
293 | $pol = array('INDEX' => 'INDEX','FOLLOW' => 'FOLLOW', 'ARCHIVE' => 'ARCHIVE'); |
---|
294 | $base = array_flip(preg_split('/\s*,\s*/',$base)); |
---|
295 | $over = array_flip(preg_split('/\s*,\s*/',$over)); |
---|
296 | |
---|
297 | foreach ($pol as $k => &$v) |
---|
298 | { |
---|
299 | if (isset($base[$k]) || isset($base['NO'.$k])) { |
---|
300 | $v = isset($base['NO'.$k]) ? 'NO'.$k : $k; |
---|
301 | } |
---|
302 | if (isset($over[$k]) || isset($over['NO'.$k])) { |
---|
303 | $v = isset($over['NO'.$k]) ? 'NO'.$k : $k; |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | if ($pol['ARCHIVE'] == 'ARCHIVE') { |
---|
308 | unset($pol['ARCHIVE']); |
---|
309 | } |
---|
310 | |
---|
311 | return implode(', ',$pol); |
---|
312 | } |
---|
313 | |
---|
314 | # Smilies static methods |
---|
315 | public static function getSmilies($blog) |
---|
316 | { |
---|
317 | $path = array(); |
---|
318 | if (isset($GLOBALS['__theme'])) { |
---|
319 | $path[] = $GLOBALS['__theme']; |
---|
320 | if (isset($GLOBALS['__parent_theme'])) { |
---|
321 | $path[] = $GLOBALS['__parent_theme']; |
---|
322 | } |
---|
323 | } |
---|
324 | $path[] = 'default'; |
---|
325 | $definition = $blog->themes_path.'/%s/smilies/smilies.txt'; |
---|
326 | $base_url = $blog->settings->system->themes_url.'/%s/smilies/'; |
---|
327 | |
---|
328 | $res = array(); |
---|
329 | |
---|
330 | foreach ($path as $t) |
---|
331 | { |
---|
332 | if (file_exists(sprintf($definition,$t))) { |
---|
333 | $base_url = sprintf($base_url,$t); |
---|
334 | return self::smiliesDefinition(sprintf($definition,$t),$base_url); |
---|
335 | } |
---|
336 | } |
---|
337 | return false; |
---|
338 | } |
---|
339 | |
---|
340 | public static function smiliesDefinition($f,$url) |
---|
341 | { |
---|
342 | $def = file($f); |
---|
343 | |
---|
344 | $res = array(); |
---|
345 | foreach($def as $v) |
---|
346 | { |
---|
347 | $v = trim($v); |
---|
348 | if (preg_match('|^([^\t]*)[\t]+(.*)$|',$v,$matches)) |
---|
349 | { |
---|
350 | $r = '/(\A|[\s]+|>)('.preg_quote($matches[1],'/').')([\s]+|[<]|\Z)/ms'; |
---|
351 | $s = '$1<img src="'.$url.$matches[2].'" '. |
---|
352 | 'alt="$2" class="smiley" />$3'; |
---|
353 | $res[$r] = $s; |
---|
354 | } |
---|
355 | } |
---|
356 | |
---|
357 | return $res; |
---|
358 | } |
---|
359 | |
---|
360 | public static function addSmilies($str) |
---|
361 | { |
---|
362 | if (!isset($GLOBALS['__smilies']) || !is_array($GLOBALS['__smilies'])) { |
---|
363 | return $str; |
---|
364 | } |
---|
365 | |
---|
366 | # Process part adapted from SmartyPants engine (J. Gruber et al.) : |
---|
367 | |
---|
368 | $tokens = self::tokenizeHTML($str); |
---|
369 | $result = ''; |
---|
370 | $in_pre = 0; # Keep track of when we're inside <pre> or <code> tags. |
---|
371 | |
---|
372 | foreach ($tokens as $cur_token) { |
---|
373 | if ($cur_token[0] == "tag") { |
---|
374 | # Don't mess with quotes inside tags. |
---|
375 | $result .= $cur_token[1]; |
---|
376 | if (preg_match('@<(/?)(?:pre|code|kbd|script|math)[\s>]@', $cur_token[1], $matches)) { |
---|
377 | $in_pre = isset($matches[1]) && $matches[1] == '/' ? 0 : 1; |
---|
378 | } |
---|
379 | } else { |
---|
380 | $t = $cur_token[1]; |
---|
381 | if (!$in_pre) { |
---|
382 | $t = preg_replace(array_keys($GLOBALS['__smilies']),array_values($GLOBALS['__smilies']),$t); |
---|
383 | } |
---|
384 | $result .= $t; |
---|
385 | } |
---|
386 | } |
---|
387 | |
---|
388 | return $result; |
---|
389 | } |
---|
390 | |
---|
391 | private static function tokenizeHTML($str) |
---|
392 | { |
---|
393 | # Function from SmartyPants engine (J. Gruber et al.) |
---|
394 | # |
---|
395 | # Parameter: String containing HTML markup. |
---|
396 | # Returns: An array of the tokens comprising the input |
---|
397 | # string. Each token is either a tag (possibly with nested, |
---|
398 | # tags contained therein, such as <a href="<MTFoo>">, or a |
---|
399 | # run of text between tags. Each element of the array is a |
---|
400 | # two-element array; the first is either 'tag' or 'text'; |
---|
401 | # the second is the actual value. |
---|
402 | # |
---|
403 | # |
---|
404 | # Regular expression derived from the _tokenize() subroutine in |
---|
405 | # Brad Choate's MTRegex plugin. |
---|
406 | # <http://www.bradchoate.com/past/mtregex.php> |
---|
407 | # |
---|
408 | $index = 0; |
---|
409 | $tokens = array(); |
---|
410 | |
---|
411 | $match = '(?s:<!(?:--.*?--\s*)+>)|'. # comment |
---|
412 | '(?s:<\?.*?\?>)|'. # processing instruction |
---|
413 | # regular tags |
---|
414 | '(?:<[/!$]?[-a-zA-Z0-9:]+\b(?>[^"\'>]+|"[^"]*"|\'[^\']*\')*>)'; |
---|
415 | |
---|
416 | $parts = preg_split("{($match)}", $str, -1, PREG_SPLIT_DELIM_CAPTURE); |
---|
417 | |
---|
418 | foreach ($parts as $part) { |
---|
419 | if (++$index % 2 && $part != '') |
---|
420 | $tokens[] = array('text', $part); |
---|
421 | else |
---|
422 | $tokens[] = array('tag', $part); |
---|
423 | } |
---|
424 | return $tokens; |
---|
425 | } |
---|
426 | |
---|
427 | |
---|
428 | # First post image helpers |
---|
429 | public static function EntryFirstImageHelper($size,$with_category,$class="",$no_tag=false,$content_only=false,$cat_only=false) |
---|
430 | { |
---|
431 | global $core, $_ctx; |
---|
432 | |
---|
433 | try { |
---|
434 | $media = new dcMedia($core); |
---|
435 | $sizes = implode('|',array_keys($media->thumb_sizes)).'|o'; |
---|
436 | if (!preg_match('/^'.$sizes.'$/',$size)) { |
---|
437 | $size = 's'; |
---|
438 | } |
---|
439 | $p_url = $core->blog->settings->system->public_url; |
---|
440 | $p_site = preg_replace('#^(.+?//.+?)/(.*)$#','$1',$core->blog->url); |
---|
441 | $p_root = $core->blog->public_path; |
---|
442 | |
---|
443 | $pattern = '(?:'.preg_quote($p_site,'/').')?'.preg_quote($p_url,'/'); |
---|
444 | $pattern = sprintf('/<img.+?src="%s(.*?\.(?:jpg|jpeg|gif|png))"[^>]+/msui',$pattern); |
---|
445 | |
---|
446 | $src = ''; |
---|
447 | $alt = ''; |
---|
448 | |
---|
449 | # We first look in post content |
---|
450 | if (!$cat_only && $_ctx->posts) |
---|
451 | { |
---|
452 | $subject = ($content_only ? '' : $_ctx->posts->post_excerpt_xhtml).$_ctx->posts->post_content_xhtml; |
---|
453 | if (preg_match_all($pattern,$subject,$m) > 0) |
---|
454 | { |
---|
455 | foreach ($m[1] as $i => $img) { |
---|
456 | if (($src = self::ContentFirstImageLookup($p_root,$img,$size)) !== false) { |
---|
457 | $dirname = str_replace('\\', '/', dirname($img)); |
---|
458 | $src = $p_url.($dirname != '/' ? $dirname : '').'/'.$src; |
---|
459 | if (preg_match('/alt="([^"]+)"/',$m[0][$i],$malt)) { |
---|
460 | $alt = $malt[1]; |
---|
461 | } |
---|
462 | break; |
---|
463 | } |
---|
464 | } |
---|
465 | } |
---|
466 | } |
---|
467 | |
---|
468 | # No src, look in category description if available |
---|
469 | if (!$src && $with_category && $_ctx->posts->cat_desc) |
---|
470 | { |
---|
471 | if (preg_match_all($pattern,$_ctx->posts->cat_desc,$m) > 0) |
---|
472 | { |
---|
473 | foreach ($m[1] as $i => $img) { |
---|
474 | if (($src = self::ContentFirstImageLookup($p_root,$img,$size)) !== false) { |
---|
475 | $dirname = str_replace('\\', '/', dirname($img)); |
---|
476 | $src = $p_url.($dirname != '/' ? $dirname : '').'/'.$src; |
---|
477 | if (preg_match('/alt="([^"]+)"/',$m[0][$i],$malt)) { |
---|
478 | $alt = $malt[1]; |
---|
479 | } |
---|
480 | break; |
---|
481 | } |
---|
482 | } |
---|
483 | }; |
---|
484 | } |
---|
485 | |
---|
486 | if ($src) { |
---|
487 | if ($no_tag) { |
---|
488 | return $src; |
---|
489 | } else { |
---|
490 | return '<img alt="'.$alt.'" src="'.$src.'" class="'.$class.'" />'; |
---|
491 | } |
---|
492 | } |
---|
493 | |
---|
494 | } catch (Exception $e) { |
---|
495 | $core->error->add($e->getMessage()); |
---|
496 | } |
---|
497 | } |
---|
498 | |
---|
499 | private static function ContentFirstImageLookup($root,$img,$size) |
---|
500 | { |
---|
501 | global $core; |
---|
502 | |
---|
503 | # Get base name and extension |
---|
504 | $info = path::info($img); |
---|
505 | $base = $info['base']; |
---|
506 | |
---|
507 | try { |
---|
508 | $media = new dcMedia($core); |
---|
509 | $sizes = implode('|',array_keys($media->thumb_sizes)); |
---|
510 | if (preg_match('/^\.(.+)_('.$sizes.')$/',$base,$m)) { |
---|
511 | $base = $m[1]; |
---|
512 | } |
---|
513 | |
---|
514 | $res = false; |
---|
515 | if ($size != 'o' && file_exists($root.'/'.$info['dirname'].'/.'.$base.'_'.$size.'.jpg')) |
---|
516 | { |
---|
517 | $res = '.'.$base.'_'.$size.'.jpg'; |
---|
518 | } |
---|
519 | elseif ($size != 'o' && file_exists($root.'/'.$info['dirname'].'/.'.$base.'_'.$size.'.png')) |
---|
520 | { |
---|
521 | $res = '.'.$base.'_'.$size.'.png'; |
---|
522 | } |
---|
523 | else |
---|
524 | { |
---|
525 | $f = $root.'/'.$info['dirname'].'/'.$base; |
---|
526 | if (file_exists($f.'.'.$info['extension'])) { |
---|
527 | $res = $base.'.'.$info['extension']; |
---|
528 | } elseif (file_exists($f.'.jpg')) { |
---|
529 | $res = $base.'.jpg'; |
---|
530 | } elseif (file_exists($f.'.jpeg')) { |
---|
531 | $res = $base.'.jpeg'; |
---|
532 | } elseif (file_exists($f.'.png')) { |
---|
533 | $res = $base.'.png'; |
---|
534 | } elseif (file_exists($f.'.gif')) { |
---|
535 | $res = $base.'.gif'; |
---|
536 | } elseif (file_exists($f.'.JPG')) { |
---|
537 | $res = $base.'.JPG'; |
---|
538 | } elseif (file_exists($f.'.JPEG')) { |
---|
539 | $res = $base.'.JPEG'; |
---|
540 | } elseif (file_exists($f.'.PNG')) { |
---|
541 | $res = $base.'.PNG'; |
---|
542 | } elseif (file_exists($f.'.GIF')) { |
---|
543 | $res = $base.'.GIF'; |
---|
544 | } |
---|
545 | } |
---|
546 | } catch (Exception $e) { |
---|
547 | $core->error->add($e->getMessage()); |
---|
548 | } |
---|
549 | |
---|
550 | if ($res) { |
---|
551 | return $res; |
---|
552 | } |
---|
553 | return false; |
---|
554 | } |
---|
555 | } |
---|