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