| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * @brief Dotclear post record helpers |
|---|
| 4 | * |
|---|
| 5 | * This class adds new methods to database post results. |
|---|
| 6 | * You can call them on every record comming from dcBlog::getPosts and similar |
|---|
| 7 | * methods. |
|---|
| 8 | * |
|---|
| 9 | * @warning You should not give the first argument (usualy $rs) of every described function. |
|---|
| 10 | * |
|---|
| 11 | * @package Dotclear |
|---|
| 12 | * @subpackage Core |
|---|
| 13 | * |
|---|
| 14 | * @copyright Olivier Meunier & Association Dotclear |
|---|
| 15 | * @copyright GPL-2.0-only |
|---|
| 16 | */ |
|---|
| 17 | |
|---|
| 18 | if (!defined('DC_RC_PATH')) {return;} |
|---|
| 19 | |
|---|
| 20 | class rsExtPost |
|---|
| 21 | { |
|---|
| 22 | /** |
|---|
| 23 | Returns whether post is editable. |
|---|
| 24 | |
|---|
| 25 | @param rs Invisible parameter |
|---|
| 26 | @return <b>boolean</b> |
|---|
| 27 | */ |
|---|
| 28 | public static function isEditable($rs) |
|---|
| 29 | { |
|---|
| 30 | # If user is admin or contentadmin, true |
|---|
| 31 | if ($rs->core->auth->check('contentadmin', $rs->core->blog->id)) { |
|---|
| 32 | return true; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | # No user id in result ? false |
|---|
| 36 | if (!$rs->exists('user_id')) { |
|---|
| 37 | return false; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | # If user is usage and owner of the entrie |
|---|
| 41 | if ($rs->core->auth->check('usage', $rs->core->blog->id) |
|---|
| 42 | && $rs->user_id == $rs->core->auth->userID()) { |
|---|
| 43 | return true; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | return false; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | /** |
|---|
| 50 | Returns whether post is deletable |
|---|
| 51 | |
|---|
| 52 | @param rs Invisible parameter |
|---|
| 53 | @return <b>boolean</b> |
|---|
| 54 | */ |
|---|
| 55 | public static function isDeletable($rs) |
|---|
| 56 | { |
|---|
| 57 | # If user is admin, or contentadmin, true |
|---|
| 58 | if ($rs->core->auth->check('contentadmin', $rs->core->blog->id)) { |
|---|
| 59 | return true; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | # No user id in result ? false |
|---|
| 63 | if (!$rs->exists('user_id')) { |
|---|
| 64 | return false; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | # If user has delete rights and is owner of the entrie |
|---|
| 68 | if ($rs->core->auth->check('delete', $rs->core->blog->id) |
|---|
| 69 | && $rs->user_id == $rs->core->auth->userID()) { |
|---|
| 70 | return true; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | return false; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | /** |
|---|
| 77 | Returns whether post is the first one of its day. |
|---|
| 78 | |
|---|
| 79 | @param rs Invisible parameter |
|---|
| 80 | @return <b>boolean</b> |
|---|
| 81 | */ |
|---|
| 82 | public static function firstPostOfDay($rs) |
|---|
| 83 | { |
|---|
| 84 | if ($rs->isStart()) { |
|---|
| 85 | return true; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | $cdate = date('Ymd', strtotime($rs->post_dt)); |
|---|
| 89 | $rs->movePrev(); |
|---|
| 90 | $ndate = date('Ymd', strtotime($rs->post_dt)); |
|---|
| 91 | $rs->moveNext(); |
|---|
| 92 | return $ndate != $cdate; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | /** |
|---|
| 96 | Returns whether post is the last one of its day. |
|---|
| 97 | |
|---|
| 98 | @param rs Invisible parameter |
|---|
| 99 | @return <b>boolean</b> |
|---|
| 100 | */ |
|---|
| 101 | public static function lastPostOfDay($rs) |
|---|
| 102 | { |
|---|
| 103 | if ($rs->isEnd()) { |
|---|
| 104 | return true; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | $cdate = date('Ymd', strtotime($rs->post_dt)); |
|---|
| 108 | $rs->moveNext(); |
|---|
| 109 | $ndate = date('Ymd', strtotime($rs->post_dt)); |
|---|
| 110 | $rs->movePrev(); |
|---|
| 111 | return $ndate != $cdate; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | /** |
|---|
| 115 | Returns whether comments are enabled on post. |
|---|
| 116 | |
|---|
| 117 | @param rs Invisible parameter |
|---|
| 118 | @return <b>boolean</b> |
|---|
| 119 | */ |
|---|
| 120 | public static function commentsActive($rs) |
|---|
| 121 | { |
|---|
| 122 | return |
|---|
| 123 | $rs->core->blog->settings->system->allow_comments |
|---|
| 124 | && $rs->post_open_comment |
|---|
| 125 | && ($rs->core->blog->settings->system->comments_ttl == 0 || |
|---|
| 126 | time() - ($rs->core->blog->settings->system->comments_ttl * 86400) < $rs->getTS()); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | /** |
|---|
| 130 | Returns whether trackbacks are enabled on post. |
|---|
| 131 | |
|---|
| 132 | @param rs Invisible parameter |
|---|
| 133 | @return <b>boolean</b> |
|---|
| 134 | */ |
|---|
| 135 | public static function trackbacksActive($rs) |
|---|
| 136 | { |
|---|
| 137 | return |
|---|
| 138 | $rs->core->blog->settings->system->allow_trackbacks |
|---|
| 139 | && $rs->post_open_tb |
|---|
| 140 | && ($rs->core->blog->settings->system->trackbacks_ttl == 0 || |
|---|
| 141 | time() - ($rs->core->blog->settings->system->trackbacks_ttl * 86400) < $rs->getTS()); |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | /** |
|---|
| 145 | Returns whether post has at least one comment. |
|---|
| 146 | |
|---|
| 147 | @param rs Invisible parameter |
|---|
| 148 | @return <b>boolean</b> |
|---|
| 149 | */ |
|---|
| 150 | public static function hasComments($rs) |
|---|
| 151 | { |
|---|
| 152 | return $rs->nb_comment > 0; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | /** |
|---|
| 156 | Returns whether post has at least one trackbacks. |
|---|
| 157 | |
|---|
| 158 | @return <b>boolean</b> |
|---|
| 159 | */ |
|---|
| 160 | public static function hasTrackbacks($rs) |
|---|
| 161 | { |
|---|
| 162 | return $rs->nb_trackback > 0; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | /** |
|---|
| 166 | Returns whether post has been updated since publication. |
|---|
| 167 | |
|---|
| 168 | @return <b>boolean</b> |
|---|
| 169 | */ |
|---|
| 170 | public static function isRepublished($rs) |
|---|
| 171 | { |
|---|
| 172 | return ($rs->getTS('upddt') + dt::getTimeOffset($rs->post_tz)) > $rs->getTS(); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | /** |
|---|
| 176 | Returns full post URL. |
|---|
| 177 | |
|---|
| 178 | @param rs Invisible parameter |
|---|
| 179 | @return <b>string</b> |
|---|
| 180 | */ |
|---|
| 181 | public static function getURL($rs) |
|---|
| 182 | { |
|---|
| 183 | return $rs->core->blog->url . $rs->core->getPostPublicURL( |
|---|
| 184 | $rs->post_type, html::sanitizeURL($rs->post_url) |
|---|
| 185 | ); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | /** |
|---|
| 189 | Returns full post category URL. |
|---|
| 190 | |
|---|
| 191 | @param rs Invisible parameter |
|---|
| 192 | @return <b>string</b> |
|---|
| 193 | */ |
|---|
| 194 | public static function getCategoryURL($rs) |
|---|
| 195 | { |
|---|
| 196 | return $rs->core->blog->url . $rs->core->url->getURLFor('category', html::sanitizeURL($rs->cat_url)); |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | /** |
|---|
| 200 | Returns whether post has an excerpt. |
|---|
| 201 | |
|---|
| 202 | @param rs Invisible parameter |
|---|
| 203 | @return <b>boolean</b> |
|---|
| 204 | */ |
|---|
| 205 | public static function isExtended($rs) |
|---|
| 206 | { |
|---|
| 207 | return $rs->post_excerpt_xhtml != ''; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | /** |
|---|
| 211 | Returns post timestamp. |
|---|
| 212 | |
|---|
| 213 | @param rs Invisible parameter |
|---|
| 214 | @param type <b>string</b> (dt|upddt|creadt) defaults to post_dt |
|---|
| 215 | @return <b>integer</b> |
|---|
| 216 | */ |
|---|
| 217 | public static function getTS($rs, $type = '') |
|---|
| 218 | { |
|---|
| 219 | if ($type == 'upddt') { |
|---|
| 220 | return strtotime($rs->post_upddt); |
|---|
| 221 | } elseif ($type == 'creadt') { |
|---|
| 222 | return strtotime($rs->post_creadt); |
|---|
| 223 | } else { |
|---|
| 224 | return strtotime($rs->post_dt); |
|---|
| 225 | } |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | /** |
|---|
| 229 | Returns post date formating according to the ISO 8601 standard. |
|---|
| 230 | |
|---|
| 231 | @param rs Invisible parameter |
|---|
| 232 | @param type <b>string</b> (dt|upddt|creadt) defaults to post_dt |
|---|
| 233 | @return <b>string</b> |
|---|
| 234 | */ |
|---|
| 235 | public static function getISO8601Date($rs, $type = '') |
|---|
| 236 | { |
|---|
| 237 | if ($type == 'upddt' || $type == 'creadt') { |
|---|
| 238 | return dt::iso8601($rs->getTS($type) + dt::getTimeOffset($rs->post_tz), $rs->post_tz); |
|---|
| 239 | } else { |
|---|
| 240 | return dt::iso8601($rs->getTS(), $rs->post_tz); |
|---|
| 241 | } |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | /** |
|---|
| 245 | Returns post date formating according to RFC 822. |
|---|
| 246 | |
|---|
| 247 | @param rs Invisible parameter |
|---|
| 248 | @param type <b>string</b> (dt|upddt|creadt) defaults to post_dt |
|---|
| 249 | @return <b>string</b> |
|---|
| 250 | */ |
|---|
| 251 | public static function getRFC822Date($rs, $type = '') |
|---|
| 252 | { |
|---|
| 253 | if ($type == 'upddt' || $type == 'creadt') { |
|---|
| 254 | return dt::rfc822($rs->getTS($type) + dt::getTimeOffset($rs->post_tz), $rs->post_tz); |
|---|
| 255 | } else { |
|---|
| 256 | return dt::rfc822($rs->getTS($type), $rs->post_tz); |
|---|
| 257 | } |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | /** |
|---|
| 261 | Returns post date with <var>$format</var> as formatting pattern. If format |
|---|
| 262 | is empty, uses <var>date_format</var> blog setting. |
|---|
| 263 | |
|---|
| 264 | @param rs Invisible parameter |
|---|
| 265 | @param format <b>string</b> Date format pattern |
|---|
| 266 | @param type <b>string</b> (dt|upddt|creadt) defaults to post_dt |
|---|
| 267 | @return <b>string</b> |
|---|
| 268 | */ |
|---|
| 269 | public static function getDate($rs, $format, $type = '') |
|---|
| 270 | { |
|---|
| 271 | if (!$format) { |
|---|
| 272 | $format = $rs->core->blog->settings->system->date_format; |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | if ($type == 'upddt') { |
|---|
| 276 | return dt::dt2str($format, $rs->post_upddt, $rs->post_tz); |
|---|
| 277 | } elseif ($type == 'creadt') { |
|---|
| 278 | return dt::dt2str($format, $rs->post_creadt, $rs->post_tz); |
|---|
| 279 | } else { |
|---|
| 280 | return dt::dt2str($format, $rs->post_dt); |
|---|
| 281 | } |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | /** |
|---|
| 285 | Returns post time with <var>$format</var> as formatting pattern. If format |
|---|
| 286 | is empty, uses <var>time_format</var> blog setting. |
|---|
| 287 | |
|---|
| 288 | @param rs Invisible parameter |
|---|
| 289 | @param format <b>string</b> Time format pattern |
|---|
| 290 | @param type <b>string</b> (dt|upddt|creadt) defaults to post_dt |
|---|
| 291 | @return <b>string</b> |
|---|
| 292 | */ |
|---|
| 293 | public static function getTime($rs, $format, $type = '') |
|---|
| 294 | { |
|---|
| 295 | if (!$format) { |
|---|
| 296 | $format = $rs->core->blog->settings->system->time_format; |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | if ($type == 'upddt') { |
|---|
| 300 | return dt::dt2str($format, $rs->post_upddt, $rs->post_tz); |
|---|
| 301 | } elseif ($type == 'creadt') { |
|---|
| 302 | return dt::dt2str($format, $rs->post_creadt, $rs->post_tz); |
|---|
| 303 | } else { |
|---|
| 304 | return dt::dt2str($format, $rs->post_dt); |
|---|
| 305 | } |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | /** |
|---|
| 309 | Returns author common name using user_id, user_name, user_firstname and |
|---|
| 310 | user_displayname fields. |
|---|
| 311 | |
|---|
| 312 | @param rs Invisible parameter |
|---|
| 313 | @return <b>string</b> |
|---|
| 314 | */ |
|---|
| 315 | public static function getAuthorCN($rs) |
|---|
| 316 | { |
|---|
| 317 | return dcUtils::getUserCN($rs->user_id, $rs->user_name, |
|---|
| 318 | $rs->user_firstname, $rs->user_displayname); |
|---|
| 319 | } |
|---|
| 320 | |
|---|
| 321 | /** |
|---|
| 322 | Returns author common name with a link if he specified one in its |
|---|
| 323 | preferences. |
|---|
| 324 | |
|---|
| 325 | @param rs Invisible parameter |
|---|
| 326 | @return <b>string</b> |
|---|
| 327 | */ |
|---|
| 328 | public static function getAuthorLink($rs) |
|---|
| 329 | { |
|---|
| 330 | $res = '%1$s'; |
|---|
| 331 | $url = $rs->user_url; |
|---|
| 332 | if ($url) { |
|---|
| 333 | $res = '<a href="%2$s">%1$s</a>'; |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | return sprintf($res, html::escapeHTML($rs->getAuthorCN()), html::escapeHTML($url)); |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | /** |
|---|
| 340 | Returns author e-mail address. If <var>$encoded</var> is true, "@" sign is |
|---|
| 341 | replaced by "%40" and "." by "%2e". |
|---|
| 342 | |
|---|
| 343 | @param rs Invisible parameter |
|---|
| 344 | @param encoded <b>boolean</b> Encode address. |
|---|
| 345 | @return <b>string</b> |
|---|
| 346 | */ |
|---|
| 347 | public static function getAuthorEmail($rs, $encoded = true) |
|---|
| 348 | { |
|---|
| 349 | if ($encoded) { |
|---|
| 350 | return strtr($rs->user_email, array('@' => '%40', '.' => '%2e')); |
|---|
| 351 | } |
|---|
| 352 | return $rs->user_email; |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | /** |
|---|
| 356 | Returns post feed unique ID. |
|---|
| 357 | |
|---|
| 358 | @param rs Invisible parameter |
|---|
| 359 | @return <b>string</b> |
|---|
| 360 | */ |
|---|
| 361 | public static function getFeedID($rs) |
|---|
| 362 | { |
|---|
| 363 | return 'urn:md5:' . md5($rs->core->blog->uid . $rs->post_id); |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | /** |
|---|
| 367 | Returns trackback RDF information block in HTML comment. |
|---|
| 368 | |
|---|
| 369 | @param rs Invisible parameter |
|---|
| 370 | @return <b>string</b> |
|---|
| 371 | */ |
|---|
| 372 | public static function getTrackbackData($rs, $format = 'html') |
|---|
| 373 | { |
|---|
| 374 | return |
|---|
| 375 | ($format == 'xml' ? "<![CDATA[>\n" : '') . |
|---|
| 376 | "<!--\n" . |
|---|
| 377 | '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"' . "\n" . |
|---|
| 378 | ' xmlns:dc="http://purl.org/dc/elements/1.1/"' . "\n" . |
|---|
| 379 | ' xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">' . "\n" . |
|---|
| 380 | "<rdf:Description\n" . |
|---|
| 381 | ' rdf:about="' . $rs->getURL() . '"' . "\n" . |
|---|
| 382 | ' dc:identifier="' . $rs->getURL() . '"' . "\n" . |
|---|
| 383 | ' dc:title="' . htmlspecialchars($rs->post_title, ENT_COMPAT, 'UTF-8') . '"' . "\n" . |
|---|
| 384 | ' trackback:ping="' . $rs->getTrackbackLink() . '" />' . "\n" . |
|---|
| 385 | "</rdf:RDF>\n" . |
|---|
| 386 | ($format == 'xml' ? "<!]]><!--" : '') . |
|---|
| 387 | "-->\n"; |
|---|
| 388 | } |
|---|
| 389 | |
|---|
| 390 | /** |
|---|
| 391 | Returns post trackback full URL. |
|---|
| 392 | |
|---|
| 393 | @param rs Invisible parameter |
|---|
| 394 | @return <b>string</b> |
|---|
| 395 | */ |
|---|
| 396 | public static function getTrackbackLink($rs) |
|---|
| 397 | { |
|---|
| 398 | return $rs->core->blog->url . $rs->core->url->getURLFor('trackback', $rs->post_id); |
|---|
| 399 | } |
|---|
| 400 | |
|---|
| 401 | /** |
|---|
| 402 | Returns post content. If <var>$absolute_urls</var> is true, appends full |
|---|
| 403 | blog URL to each relative post URLs. |
|---|
| 404 | |
|---|
| 405 | @param rs Invisible parameter |
|---|
| 406 | @param absolute_urls <b>boolean</b> With absolute URLs |
|---|
| 407 | @return <b>string</b> |
|---|
| 408 | */ |
|---|
| 409 | public static function getContent($rs, $absolute_urls = false) |
|---|
| 410 | { |
|---|
| 411 | if ($absolute_urls) { |
|---|
| 412 | return html::absoluteURLs($rs->post_content_xhtml, $rs->getURL()); |
|---|
| 413 | } else { |
|---|
| 414 | return $rs->post_content_xhtml; |
|---|
| 415 | } |
|---|
| 416 | } |
|---|
| 417 | |
|---|
| 418 | /** |
|---|
| 419 | Returns post excerpt. If <var>$absolute_urls</var> is true, appends full |
|---|
| 420 | blog URL to each relative post URLs. |
|---|
| 421 | |
|---|
| 422 | @param rs Invisible parameter |
|---|
| 423 | @param absolute_urls <b>boolean</b> With absolute URLs |
|---|
| 424 | @return <b>string</b> |
|---|
| 425 | */ |
|---|
| 426 | public static function getExcerpt($rs, $absolute_urls = false) |
|---|
| 427 | { |
|---|
| 428 | if ($absolute_urls) { |
|---|
| 429 | return html::absoluteURLs($rs->post_excerpt_xhtml, $rs->getURL()); |
|---|
| 430 | } else { |
|---|
| 431 | return $rs->post_excerpt_xhtml; |
|---|
| 432 | } |
|---|
| 433 | } |
|---|
| 434 | |
|---|
| 435 | /** |
|---|
| 436 | Returns post media count using a subquery. |
|---|
| 437 | |
|---|
| 438 | @param rs Invisible parameter |
|---|
| 439 | @return <b>integer</b> |
|---|
| 440 | */ |
|---|
| 441 | public static function countMedia($rs, $link_type = null) |
|---|
| 442 | { |
|---|
| 443 | if (isset($rs->_nb_media[$rs->index()])) { |
|---|
| 444 | return $rs->_nb_media[$rs->index()]; |
|---|
| 445 | } else { |
|---|
| 446 | $strReq = |
|---|
| 447 | 'SELECT count(media_id) ' . |
|---|
| 448 | 'FROM ' . $rs->core->prefix . 'post_media ' . |
|---|
| 449 | 'WHERE post_id = ' . (integer) $rs->post_id . ' '; |
|---|
| 450 | if ($link_type != null) { |
|---|
| 451 | $strReq .= "AND link_type = '" . $rs->core->con->escape($link_type) . "'"; |
|---|
| 452 | } |
|---|
| 453 | |
|---|
| 454 | $res = (integer) $rs->core->con->select($strReq)->f(0); |
|---|
| 455 | $rs->_nb_media[$rs->index()] = $res; |
|---|
| 456 | return $res; |
|---|
| 457 | } |
|---|
| 458 | } |
|---|
| 459 | } |
|---|
| 460 | |
|---|
| 461 | /** |
|---|
| 462 | @ingroup DC_CORE |
|---|
| 463 | @brief Dotclear comment record helpers. |
|---|
| 464 | |
|---|
| 465 | This class adds new methods to database comment results. |
|---|
| 466 | You can call them on every record comming from dcBlog::getComments and similar |
|---|
| 467 | methods. |
|---|
| 468 | |
|---|
| 469 | @warning You should not give the first argument (usualy $rs) of every described |
|---|
| 470 | function. |
|---|
| 471 | */ |
|---|
| 472 | class rsExtComment |
|---|
| 473 | { |
|---|
| 474 | /** |
|---|
| 475 | Returns comment date with <var>$format</var> as formatting pattern. If |
|---|
| 476 | format is empty, uses <var>date_format</var> blog setting. |
|---|
| 477 | |
|---|
| 478 | @param rs Invisible parameter |
|---|
| 479 | @param format <b>string</b> Date format pattern |
|---|
| 480 | @param type <b>string</b> (dt|upddt) defaults to comment_dt |
|---|
| 481 | @return <b>string</b> |
|---|
| 482 | */ |
|---|
| 483 | public static function getDate($rs, $format, $type = '') |
|---|
| 484 | { |
|---|
| 485 | if (!$format) { |
|---|
| 486 | $format = $rs->core->blog->settings->system->date_format; |
|---|
| 487 | } |
|---|
| 488 | |
|---|
| 489 | if ($type == 'upddt') { |
|---|
| 490 | return dt::dt2str($format, $rs->comment_upddt, $rs->comment_tz); |
|---|
| 491 | } else { |
|---|
| 492 | return dt::dt2str($format, $rs->comment_dt); |
|---|
| 493 | } |
|---|
| 494 | } |
|---|
| 495 | |
|---|
| 496 | /** |
|---|
| 497 | Returns comment time with <var>$format</var> as formatting pattern. If |
|---|
| 498 | format is empty, uses <var>time_format</var> blog setting. |
|---|
| 499 | |
|---|
| 500 | @param rs Invisible parameter |
|---|
| 501 | @param format <b>string</b> Date format pattern |
|---|
| 502 | @param type <b>string</b> (dt|upddt) defaults to comment_dt |
|---|
| 503 | @return <b>string</b> |
|---|
| 504 | */ |
|---|
| 505 | public static function getTime($rs, $format, $type = '') |
|---|
| 506 | { |
|---|
| 507 | if (!$format) { |
|---|
| 508 | $format = $rs->core->blog->settings->system->time_format; |
|---|
| 509 | } |
|---|
| 510 | |
|---|
| 511 | if ($type == 'upddt') { |
|---|
| 512 | return dt::dt2str($format, $rs->comment_updt, $rs->comment_tz); |
|---|
| 513 | } else { |
|---|
| 514 | return dt::dt2str($format, $rs->comment_dt); |
|---|
| 515 | } |
|---|
| 516 | } |
|---|
| 517 | |
|---|
| 518 | /** |
|---|
| 519 | Returns comment timestamp. |
|---|
| 520 | |
|---|
| 521 | @param rs Invisible parameter |
|---|
| 522 | @param type <b>string</b> (dt|upddt) defaults to comment_dt |
|---|
| 523 | @return <b>integer</b> |
|---|
| 524 | */ |
|---|
| 525 | public static function getTS($rs, $type = '') |
|---|
| 526 | { |
|---|
| 527 | if ($type == 'upddt') { |
|---|
| 528 | return strtotime($rs->comment_upddt); |
|---|
| 529 | } else { |
|---|
| 530 | return strtotime($rs->comment_dt); |
|---|
| 531 | } |
|---|
| 532 | } |
|---|
| 533 | |
|---|
| 534 | /** |
|---|
| 535 | Returns comment date formating according to the ISO 8601 standard. |
|---|
| 536 | |
|---|
| 537 | @param rs Invisible parameter |
|---|
| 538 | @param type <b>string</b> (dt|upddt) defaults to comment_dt |
|---|
| 539 | @return <b>string</b> |
|---|
| 540 | */ |
|---|
| 541 | public static function getISO8601Date($rs, $type = '') |
|---|
| 542 | { |
|---|
| 543 | if ($type == 'upddt') { |
|---|
| 544 | return dt::iso8601($rs->getTS($type) + dt::getTimeOffset($rs->comment_tz), $rs->comment_tz); |
|---|
| 545 | } else { |
|---|
| 546 | return dt::iso8601($rs->getTS(), $rs->comment_tz); |
|---|
| 547 | } |
|---|
| 548 | } |
|---|
| 549 | |
|---|
| 550 | /** |
|---|
| 551 | Returns comment date formating according to RFC 822. |
|---|
| 552 | |
|---|
| 553 | @param rs Invisible parameter |
|---|
| 554 | @param type <b>string</b> (dt|upddt) defaults to comment_dt |
|---|
| 555 | @return <b>string</b> |
|---|
| 556 | */ |
|---|
| 557 | public static function getRFC822Date($rs, $type = '') |
|---|
| 558 | { |
|---|
| 559 | if ($type == 'upddt') { |
|---|
| 560 | return dt::rfc822($rs->getTS($type) + dt::getTimeOffset($rs->comment_tz), $rs->comment_tz); |
|---|
| 561 | } else { |
|---|
| 562 | return dt::rfc822($rs->getTS(), $rs->comment_tz); |
|---|
| 563 | } |
|---|
| 564 | } |
|---|
| 565 | |
|---|
| 566 | /** |
|---|
| 567 | Returns comment content. If <var>$absolute_urls</var> is true, appends full |
|---|
| 568 | blog URL to each relative post URLs. |
|---|
| 569 | |
|---|
| 570 | @param rs Invisible parameter |
|---|
| 571 | @param absolute_urls <b>boolean</b> With absolute URLs |
|---|
| 572 | @return <b>string</b> |
|---|
| 573 | */ |
|---|
| 574 | public static function getContent($rs, $absolute_urls = false) |
|---|
| 575 | { |
|---|
| 576 | $res = $rs->comment_content; |
|---|
| 577 | |
|---|
| 578 | if ($rs->core->blog->settings->system->comments_nofollow) { |
|---|
| 579 | $res = preg_replace_callback('#<a(.*?href=".*?".*?)>#ms', array('self', 'noFollowURL'), $res); |
|---|
| 580 | } |
|---|
| 581 | |
|---|
| 582 | if ($absolute_urls) { |
|---|
| 583 | $res = html::absoluteURLs($res, $rs->getPostURL()); |
|---|
| 584 | } |
|---|
| 585 | |
|---|
| 586 | return $res; |
|---|
| 587 | } |
|---|
| 588 | |
|---|
| 589 | private static function noFollowURL($m) |
|---|
| 590 | { |
|---|
| 591 | if (preg_match('/rel="nofollow"/', $m[1])) { |
|---|
| 592 | return $m[0]; |
|---|
| 593 | } |
|---|
| 594 | |
|---|
| 595 | return '<a' . $m[1] . ' rel="nofollow">'; |
|---|
| 596 | } |
|---|
| 597 | |
|---|
| 598 | /** |
|---|
| 599 | Returns comment author link to his website if he specified one. |
|---|
| 600 | |
|---|
| 601 | @param rs Invisible parameter |
|---|
| 602 | @return <b>string</b> |
|---|
| 603 | */ |
|---|
| 604 | public static function getAuthorURL($rs) |
|---|
| 605 | { |
|---|
| 606 | if (trim($rs->comment_site)) { |
|---|
| 607 | return trim($rs->comment_site); |
|---|
| 608 | } |
|---|
| 609 | } |
|---|
| 610 | |
|---|
| 611 | /** |
|---|
| 612 | Returns comment post full URL. |
|---|
| 613 | |
|---|
| 614 | @param rs Invisible parameter |
|---|
| 615 | @return <b>string</b> |
|---|
| 616 | */ |
|---|
| 617 | public static function getPostURL($rs) |
|---|
| 618 | { |
|---|
| 619 | return $rs->core->blog->url . $rs->core->getPostPublicURL( |
|---|
| 620 | $rs->post_type, html::sanitizeURL($rs->post_url) |
|---|
| 621 | ); |
|---|
| 622 | } |
|---|
| 623 | |
|---|
| 624 | /** |
|---|
| 625 | Returns comment author name in a link to his website if he specified one. |
|---|
| 626 | |
|---|
| 627 | @param rs Invisible parameter |
|---|
| 628 | @return <b>string</b> |
|---|
| 629 | */ |
|---|
| 630 | public static function getAuthorLink($rs) |
|---|
| 631 | { |
|---|
| 632 | $res = '%1$s'; |
|---|
| 633 | $url = $rs->getAuthorURL(); |
|---|
| 634 | if ($url) { |
|---|
| 635 | $res = '<a href="%2$s"%3$s>%1$s</a>'; |
|---|
| 636 | } |
|---|
| 637 | |
|---|
| 638 | $nofollow = ''; |
|---|
| 639 | if ($rs->core->blog->settings->system->comments_nofollow) { |
|---|
| 640 | $nofollow = ' rel="nofollow"'; |
|---|
| 641 | } |
|---|
| 642 | |
|---|
| 643 | return sprintf($res, html::escapeHTML($rs->comment_author), html::escapeHTML($url), $nofollow); |
|---|
| 644 | } |
|---|
| 645 | |
|---|
| 646 | /** |
|---|
| 647 | Returns comment author e-mail address. If <var>$encoded</var> is true, |
|---|
| 648 | "@" sign is replaced by "%40" and "." by "%2e". |
|---|
| 649 | |
|---|
| 650 | @param rs Invisible parameter |
|---|
| 651 | @param encoded <b>boolean</b> Encode address. |
|---|
| 652 | @return <b>string</b> |
|---|
| 653 | */ |
|---|
| 654 | public static function getEmail($rs, $encoded = true) |
|---|
| 655 | { |
|---|
| 656 | if ($encoded) { |
|---|
| 657 | return strtr($rs->comment_email, array('@' => '%40', '.' => '%2e')); |
|---|
| 658 | } |
|---|
| 659 | return $rs->comment_email; |
|---|
| 660 | } |
|---|
| 661 | |
|---|
| 662 | /** |
|---|
| 663 | Returns trackback site title if comment is a trackback. |
|---|
| 664 | |
|---|
| 665 | @param rs Invisible parameter |
|---|
| 666 | @return <b>string</b> |
|---|
| 667 | */ |
|---|
| 668 | public static function getTrackbackTitle($rs) |
|---|
| 669 | { |
|---|
| 670 | if ($rs->comment_trackback == 1 && |
|---|
| 671 | preg_match('|<p><strong>(.*?)</strong></p>|msU', $rs->comment_content, |
|---|
| 672 | $match)) { |
|---|
| 673 | return html::decodeEntities($match[1]); |
|---|
| 674 | } |
|---|
| 675 | } |
|---|
| 676 | |
|---|
| 677 | /** |
|---|
| 678 | Returns trackback content if comment is a trackback. |
|---|
| 679 | |
|---|
| 680 | @param rs Invisible parameter |
|---|
| 681 | @return <b>string</b> |
|---|
| 682 | */ |
|---|
| 683 | public static function getTrackbackContent($rs) |
|---|
| 684 | { |
|---|
| 685 | if ($rs->comment_trackback == 1) { |
|---|
| 686 | return preg_replace('|<p><strong>.*?</strong></p>|msU', '', |
|---|
| 687 | $rs->comment_content); |
|---|
| 688 | } |
|---|
| 689 | } |
|---|
| 690 | |
|---|
| 691 | /** |
|---|
| 692 | Returns comment feed unique ID. |
|---|
| 693 | |
|---|
| 694 | @param rs Invisible parameter |
|---|
| 695 | @return <b>string</b> |
|---|
| 696 | */ |
|---|
| 697 | public static function getFeedID($rs) |
|---|
| 698 | { |
|---|
| 699 | return 'urn:md5:' . md5($rs->core->blog->uid . $rs->comment_id); |
|---|
| 700 | } |
|---|
| 701 | |
|---|
| 702 | /** |
|---|
| 703 | Returns whether comment is from the post author. |
|---|
| 704 | |
|---|
| 705 | @param rs Invisible parameter |
|---|
| 706 | @return <b>boolean</b> |
|---|
| 707 | */ |
|---|
| 708 | public static function isMe($rs) |
|---|
| 709 | { |
|---|
| 710 | return |
|---|
| 711 | $rs->comment_email && $rs->comment_site && |
|---|
| 712 | $rs->comment_email == $rs->user_email && |
|---|
| 713 | $rs->comment_site == $rs->user_url; |
|---|
| 714 | } |
|---|
| 715 | } |
|---|
| 716 | |
|---|
| 717 | /** |
|---|
| 718 | @ingroup DC_CORE |
|---|
| 719 | @brief Dotclear dates record helpers. |
|---|
| 720 | |
|---|
| 721 | This class adds new methods to database dates results. |
|---|
| 722 | You can call them on every record comming from dcBlog::getDates. |
|---|
| 723 | |
|---|
| 724 | @warning You should not give the first argument (usualy $rs) of every described |
|---|
| 725 | function. |
|---|
| 726 | */ |
|---|
| 727 | class rsExtDates |
|---|
| 728 | { |
|---|
| 729 | /** |
|---|
| 730 | @param rs Invisible parameter |
|---|
| 731 | @return <b>integer</b> Date timestamp |
|---|
| 732 | */ |
|---|
| 733 | public static function ts($rs) |
|---|
| 734 | { |
|---|
| 735 | return strtotime($rs->dt); |
|---|
| 736 | } |
|---|
| 737 | |
|---|
| 738 | /** |
|---|
| 739 | @param rs Invisible parameter |
|---|
| 740 | @return <b>string</b> Date year |
|---|
| 741 | */ |
|---|
| 742 | public static function year($rs) |
|---|
| 743 | { |
|---|
| 744 | return date('Y', strtotime($rs->dt)); |
|---|
| 745 | } |
|---|
| 746 | |
|---|
| 747 | /** |
|---|
| 748 | @param rs Invisible parameter |
|---|
| 749 | @return <b>string</b> Date month |
|---|
| 750 | */ |
|---|
| 751 | public static function month($rs) |
|---|
| 752 | { |
|---|
| 753 | return date('m', strtotime($rs->dt)); |
|---|
| 754 | } |
|---|
| 755 | |
|---|
| 756 | /** |
|---|
| 757 | @param rs Invisible parameter |
|---|
| 758 | @return <b>integer</b> Date day |
|---|
| 759 | */ |
|---|
| 760 | public static function day($rs) |
|---|
| 761 | { |
|---|
| 762 | return date('d', strtotime($rs->dt)); |
|---|
| 763 | } |
|---|
| 764 | |
|---|
| 765 | /** |
|---|
| 766 | Returns date month archive full URL. |
|---|
| 767 | |
|---|
| 768 | @param rs Invisible parameter |
|---|
| 769 | @param core <b>dcCore</b> dcCore instance |
|---|
| 770 | @return <b>integer</b> |
|---|
| 771 | */ |
|---|
| 772 | public static function url($rs, $core) |
|---|
| 773 | { |
|---|
| 774 | $url = date('Y/m', strtotime($rs->dt)); |
|---|
| 775 | |
|---|
| 776 | return $core->blog->url . $core->url->getURLFor('archive', $url); |
|---|
| 777 | } |
|---|
| 778 | |
|---|
| 779 | /** |
|---|
| 780 | Returns whether date is the first of year. |
|---|
| 781 | |
|---|
| 782 | @param rs Invisible parameter |
|---|
| 783 | @return <b>boolean</b> |
|---|
| 784 | */ |
|---|
| 785 | public static function yearHeader($rs) |
|---|
| 786 | { |
|---|
| 787 | if ($rs->isStart()) { |
|---|
| 788 | return true; |
|---|
| 789 | } |
|---|
| 790 | |
|---|
| 791 | $y = $rs->year(); |
|---|
| 792 | $rs->movePrev(); |
|---|
| 793 | $py = $rs->year(); |
|---|
| 794 | $rs->moveNext(); |
|---|
| 795 | |
|---|
| 796 | return $y != $py; |
|---|
| 797 | } |
|---|
| 798 | |
|---|
| 799 | /** |
|---|
| 800 | Returns whether date is the last of year. |
|---|
| 801 | |
|---|
| 802 | @param rs Invisible parameter |
|---|
| 803 | @return <b>boolean</b> |
|---|
| 804 | */ |
|---|
| 805 | public static function yearFooter($rs) |
|---|
| 806 | { |
|---|
| 807 | if ($rs->isEnd()) { |
|---|
| 808 | return true; |
|---|
| 809 | } |
|---|
| 810 | |
|---|
| 811 | $y = $rs->year(); |
|---|
| 812 | if ($rs->moveNext()) { |
|---|
| 813 | $ny = $rs->year(); |
|---|
| 814 | $rs->movePrev(); |
|---|
| 815 | return $y != $ny; |
|---|
| 816 | } |
|---|
| 817 | return false; |
|---|
| 818 | |
|---|
| 819 | } |
|---|
| 820 | } |
|---|
| 821 | |
|---|
| 822 | /** |
|---|
| 823 | @ingroup DC_CORE |
|---|
| 824 | @brief Dotclear dates record helpers. |
|---|
| 825 | |
|---|
| 826 | This class adds new methods to database dates results. |
|---|
| 827 | You can call them on every record comming from dcAuth::checkUser and |
|---|
| 828 | dcCore::getUsers. |
|---|
| 829 | |
|---|
| 830 | @warning You should not give the first argument (usualy $rs) of every described |
|---|
| 831 | function. |
|---|
| 832 | */ |
|---|
| 833 | class rsExtUser |
|---|
| 834 | { |
|---|
| 835 | private static $sortfield; |
|---|
| 836 | private static $sortsign; |
|---|
| 837 | /** |
|---|
| 838 | Returns a user option. |
|---|
| 839 | |
|---|
| 840 | @param rs Invisible parameter |
|---|
| 841 | @param name <b>string</b> Option name |
|---|
| 842 | @return <b>string</b> |
|---|
| 843 | */ |
|---|
| 844 | public static function option($rs, $name) |
|---|
| 845 | { |
|---|
| 846 | $options = self::options($rs); |
|---|
| 847 | |
|---|
| 848 | if (isset($options[$name])) { |
|---|
| 849 | return $options[$name]; |
|---|
| 850 | } |
|---|
| 851 | return; |
|---|
| 852 | } |
|---|
| 853 | |
|---|
| 854 | /** |
|---|
| 855 | Returns all user options. |
|---|
| 856 | |
|---|
| 857 | @param rs Invisible parameter |
|---|
| 858 | @return <b>array</b> |
|---|
| 859 | */ |
|---|
| 860 | public static function options($rs) |
|---|
| 861 | { |
|---|
| 862 | $options = @unserialize($rs->user_options); |
|---|
| 863 | if (is_array($options)) { |
|---|
| 864 | return $options; |
|---|
| 865 | } |
|---|
| 866 | return array(); |
|---|
| 867 | } |
|---|
| 868 | |
|---|
| 869 | /** |
|---|
| 870 | Converts this record to a {@link extStaticRecord} instance. |
|---|
| 871 | |
|---|
| 872 | @param rs Invisible parameter |
|---|
| 873 | */ |
|---|
| 874 | public static function toExtStatic($rs) |
|---|
| 875 | { |
|---|
| 876 | if ($rs instanceof extStaticRecord) { |
|---|
| 877 | return $rs; |
|---|
| 878 | } |
|---|
| 879 | return new extStaticRecord($rs); |
|---|
| 880 | } |
|---|
| 881 | } |
|---|
| 882 | |
|---|
| 883 | class rsExtBlog |
|---|
| 884 | { |
|---|
| 885 | private static $sortfield; |
|---|
| 886 | private static $sortsign; |
|---|
| 887 | /** |
|---|
| 888 | Converts this record to a {@link extStaticRecord} instance. |
|---|
| 889 | |
|---|
| 890 | @param rs Invisible parameter |
|---|
| 891 | */ |
|---|
| 892 | public static function toExtStatic($rs) |
|---|
| 893 | { |
|---|
| 894 | if ($rs instanceof extStaticRecord) { |
|---|
| 895 | return $rs; |
|---|
| 896 | } |
|---|
| 897 | return new extStaticRecord($rs); |
|---|
| 898 | } |
|---|
| 899 | } |
|---|
| 900 | |
|---|
| 901 | class extStaticRecord extends staticRecord |
|---|
| 902 | { |
|---|
| 903 | private $sortfield; |
|---|
| 904 | private $sortsign; |
|---|
| 905 | |
|---|
| 906 | public function __construct($rs) |
|---|
| 907 | { |
|---|
| 908 | parent::__construct($rs->__data, $rs->__info); |
|---|
| 909 | } |
|---|
| 910 | |
|---|
| 911 | /** |
|---|
| 912 | Lexically sort. |
|---|
| 913 | |
|---|
| 914 | @param field <b>string<b> sort field |
|---|
| 915 | @param order <b>string<b> sort order |
|---|
| 916 | */ |
|---|
| 917 | public function lexicalSort($field, $order = 'asc') |
|---|
| 918 | { |
|---|
| 919 | $this->sortfield = $field; |
|---|
| 920 | $this->sortsign = strtolower($order) == 'asc' ? 1 : -1; |
|---|
| 921 | |
|---|
| 922 | usort($this->__data, array($this, 'lexicalSortCallback')); |
|---|
| 923 | |
|---|
| 924 | $this->sortfield = null; |
|---|
| 925 | $this->sortsign = null; |
|---|
| 926 | } |
|---|
| 927 | private function lexicalSortCallback($a, $b) |
|---|
| 928 | { |
|---|
| 929 | $a = $a[$this->sortfield]; |
|---|
| 930 | $b = $b[$this->sortfield]; |
|---|
| 931 | |
|---|
| 932 | # Integer values |
|---|
| 933 | if ($a == (string) (integer) $a && $b == (string) (integer) $b) { |
|---|
| 934 | $a = (integer) $a; |
|---|
| 935 | $b = (integer) $b; |
|---|
| 936 | return ($a - $b) * $this->sortsign; |
|---|
| 937 | } |
|---|
| 938 | |
|---|
| 939 | return strcoll(strtolower(dcUtils::removeDiacritics($a)), strtolower(dcUtils::removeDiacritics($b))) * $this->sortsign; |
|---|
| 940 | } |
|---|
| 941 | } |
|---|