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