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