[0] | 1 | <?php |
---|
[3731] | 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 | |
---|
[3730] | 18 | if (!defined('DC_RC_PATH')) {return;} |
---|
[0] | 19 | |
---|
| 20 | class rsExtPost |
---|
| 21 | { |
---|
[3730] | 22 | /** |
---|
| 23 | Returns whether post is editable. |
---|
[2566] | 24 | |
---|
[3730] | 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 | } |
---|
[2566] | 34 | |
---|
[3730] | 35 | # No user id in result ? false |
---|
| 36 | if (!$rs->exists('user_id')) { |
---|
| 37 | return false; |
---|
| 38 | } |
---|
[2566] | 39 | |
---|
[3730] | 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 | } |
---|
[2566] | 45 | |
---|
[3730] | 46 | return false; |
---|
| 47 | } |
---|
[2566] | 48 | |
---|
[3730] | 49 | /** |
---|
| 50 | Returns whether post is deletable |
---|
[2566] | 51 | |
---|
[3730] | 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 | } |
---|
[2566] | 61 | |
---|
[3730] | 62 | # No user id in result ? false |
---|
| 63 | if (!$rs->exists('user_id')) { |
---|
| 64 | return false; |
---|
| 65 | } |
---|
[2566] | 66 | |
---|
[3730] | 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 | } |
---|
[2566] | 72 | |
---|
[3730] | 73 | return false; |
---|
| 74 | } |
---|
[2566] | 75 | |
---|
[3730] | 76 | /** |
---|
| 77 | Returns whether post is the first one of its day. |
---|
[2566] | 78 | |
---|
[3730] | 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 | } |
---|
[2566] | 87 | |
---|
[3730] | 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 | } |
---|
[2566] | 94 | |
---|
[3730] | 95 | /** |
---|
| 96 | Returns whether post is the last one of its day. |
---|
[2566] | 97 | |
---|
[3730] | 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 | } |
---|
[2566] | 106 | |
---|
[3730] | 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 | } |
---|
[2566] | 113 | |
---|
[3730] | 114 | /** |
---|
| 115 | Returns whether comments are enabled on post. |
---|
[2566] | 116 | |
---|
[3730] | 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 | } |
---|
[2566] | 128 | |
---|
[3730] | 129 | /** |
---|
| 130 | Returns whether trackbacks are enabled on post. |
---|
[2566] | 131 | |
---|
[3730] | 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 | } |
---|
[2566] | 143 | |
---|
[3730] | 144 | /** |
---|
| 145 | Returns whether post has at least one comment. |
---|
[2566] | 146 | |
---|
[3730] | 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 | } |
---|
[2566] | 154 | |
---|
[3730] | 155 | /** |
---|
| 156 | Returns whether post has at least one trackbacks. |
---|
[2566] | 157 | |
---|
[3730] | 158 | @return <b>boolean</b> |
---|
| 159 | */ |
---|
| 160 | public static function hasTrackbacks($rs) |
---|
| 161 | { |
---|
| 162 | return $rs->nb_trackback > 0; |
---|
| 163 | } |
---|
[912] | 164 | |
---|
[3730] | 165 | /** |
---|
| 166 | Returns whether post has been updated since publication. |
---|
[912] | 167 | |
---|
[3730] | 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 | } |
---|
[2566] | 174 | |
---|
[3730] | 175 | /** |
---|
| 176 | Returns full post URL. |
---|
[2566] | 177 | |
---|
[3730] | 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 | } |
---|
[2566] | 187 | |
---|
[3730] | 188 | /** |
---|
| 189 | Returns full post category URL. |
---|
[2566] | 190 | |
---|
[3730] | 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 | } |
---|
[2566] | 198 | |
---|
[3730] | 199 | /** |
---|
| 200 | Returns whether post has an excerpt. |
---|
[2566] | 201 | |
---|
[3730] | 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 | } |
---|
[2566] | 209 | |
---|
[3730] | 210 | /** |
---|
| 211 | Returns post timestamp. |
---|
[2566] | 212 | |
---|
[3730] | 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 | } |
---|
[2566] | 227 | |
---|
[3730] | 228 | /** |
---|
| 229 | Returns post date formating according to the ISO 8601 standard. |
---|
[2566] | 230 | |
---|
[3730] | 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 | } |
---|
[2566] | 243 | |
---|
[3730] | 244 | /** |
---|
| 245 | Returns post date formating according to RFC 822. |
---|
[2566] | 246 | |
---|
[3730] | 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 | } |
---|
[2566] | 259 | |
---|
[3730] | 260 | /** |
---|
| 261 | Returns post date with <var>$format</var> as formatting pattern. If format |
---|
| 262 | is empty, uses <var>date_format</var> blog setting. |
---|
[2566] | 263 | |
---|
[3730] | 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 | } |
---|
[2566] | 274 | |
---|
[3730] | 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 | } |
---|
[2566] | 283 | |
---|
[3730] | 284 | /** |
---|
| 285 | Returns post time with <var>$format</var> as formatting pattern. If format |
---|
| 286 | is empty, uses <var>time_format</var> blog setting. |
---|
[2566] | 287 | |
---|
[3730] | 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 | } |
---|
[2566] | 298 | |
---|
[3730] | 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 | } |
---|
[2566] | 307 | |
---|
[3730] | 308 | /** |
---|
| 309 | Returns author common name using user_id, user_name, user_firstname and |
---|
| 310 | user_displayname fields. |
---|
[2566] | 311 | |
---|
[3730] | 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 | } |
---|
[2566] | 320 | |
---|
[3730] | 321 | /** |
---|
| 322 | Returns author common name with a link if he specified one in its |
---|
| 323 | preferences. |
---|
[2566] | 324 | |
---|
[3730] | 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 | } |
---|
[2566] | 335 | |
---|
[3730] | 336 | return sprintf($res, html::escapeHTML($rs->getAuthorCN()), html::escapeHTML($url)); |
---|
| 337 | } |
---|
[2566] | 338 | |
---|
[3730] | 339 | /** |
---|
| 340 | Returns author e-mail address. If <var>$encoded</var> is true, "@" sign is |
---|
| 341 | replaced by "%40" and "." by "%2e". |
---|
[2566] | 342 | |
---|
[3730] | 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 | } |
---|
[2566] | 354 | |
---|
[3730] | 355 | /** |
---|
| 356 | Returns post feed unique ID. |
---|
[2566] | 357 | |
---|
[3730] | 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 | } |
---|
[2566] | 365 | |
---|
[3730] | 366 | /** |
---|
| 367 | Returns trackback RDF information block in HTML comment. |
---|
[2566] | 368 | |
---|
[3730] | 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 | } |
---|
[2566] | 389 | |
---|
[3730] | 390 | /** |
---|
| 391 | Returns post trackback full URL. |
---|
[2566] | 392 | |
---|
[3730] | 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 | } |
---|
[2566] | 400 | |
---|
[3730] | 401 | /** |
---|
| 402 | Returns post content. If <var>$absolute_urls</var> is true, appends full |
---|
| 403 | blog URL to each relative post URLs. |
---|
[2566] | 404 | |
---|
[3730] | 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 | } |
---|
[2566] | 417 | |
---|
[3730] | 418 | /** |
---|
| 419 | Returns post excerpt. If <var>$absolute_urls</var> is true, appends full |
---|
| 420 | blog URL to each relative post URLs. |
---|
[2566] | 421 | |
---|
[3730] | 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 | } |
---|
[2566] | 434 | |
---|
[3730] | 435 | /** |
---|
| 436 | Returns post media count using a subquery. |
---|
[2566] | 437 | |
---|
[3730] | 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 { |
---|
[3761] | 446 | $sql = new dcSelectStatement($rs->core, 'coreRsExtCountMedia'); |
---|
| 447 | $sql |
---|
| 448 | ->columns(array('count(media_id)')) |
---|
| 449 | ->from($rs->core->prefix . 'post_media') |
---|
| 450 | ->where('post_id = ' . $sql->quote((integer) $rs->post_id)); |
---|
[3730] | 451 | if ($link_type != null) { |
---|
[3761] | 452 | $sql->where('link_type = ' . $sql->quote($link_type)); |
---|
[3730] | 453 | } |
---|
[3761] | 454 | $res = (integer) $rs->core->con->select($sql->statement())->f(0); |
---|
[3730] | 455 | $rs->_nb_media[$rs->index()] = $res; |
---|
| 456 | return $res; |
---|
| 457 | } |
---|
| 458 | } |
---|
[0] | 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. |
---|
[3730] | 471 | */ |
---|
[0] | 472 | class rsExtComment |
---|
| 473 | { |
---|
[3730] | 474 | /** |
---|
| 475 | Returns comment date with <var>$format</var> as formatting pattern. If |
---|
| 476 | format is empty, uses <var>date_format</var> blog setting. |
---|
[2566] | 477 | |
---|
[3730] | 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 | } |
---|
[2566] | 488 | |
---|
[3730] | 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 | } |
---|
[2566] | 495 | |
---|
[3730] | 496 | /** |
---|
| 497 | Returns comment time with <var>$format</var> as formatting pattern. If |
---|
| 498 | format is empty, uses <var>time_format</var> blog setting. |
---|
[2566] | 499 | |
---|
[3730] | 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 | } |
---|
[2566] | 510 | |
---|
[3730] | 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 | } |
---|
[2566] | 517 | |
---|
[3730] | 518 | /** |
---|
| 519 | Returns comment timestamp. |
---|
[2566] | 520 | |
---|
[3730] | 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 | } |
---|
[2566] | 533 | |
---|
[3730] | 534 | /** |
---|
| 535 | Returns comment date formating according to the ISO 8601 standard. |
---|
[2566] | 536 | |
---|
[3730] | 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 | } |
---|
[2566] | 549 | |
---|
[3730] | 550 | /** |
---|
| 551 | Returns comment date formating according to RFC 822. |
---|
[2566] | 552 | |
---|
[3730] | 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 | } |
---|
[2566] | 565 | |
---|
[3730] | 566 | /** |
---|
| 567 | Returns comment content. If <var>$absolute_urls</var> is true, appends full |
---|
| 568 | blog URL to each relative post URLs. |
---|
[2566] | 569 | |
---|
[3730] | 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; |
---|
[2566] | 577 | |
---|
[3730] | 578 | if ($rs->core->blog->settings->system->comments_nofollow) { |
---|
| 579 | $res = preg_replace_callback('#<a(.*?href=".*?".*?)>#ms', array('self', 'noFollowURL'), $res); |
---|
| 580 | } |
---|
[2566] | 581 | |
---|
[3730] | 582 | if ($absolute_urls) { |
---|
| 583 | $res = html::absoluteURLs($res, $rs->getPostURL()); |
---|
| 584 | } |
---|
[2566] | 585 | |
---|
[3730] | 586 | return $res; |
---|
| 587 | } |
---|
[2566] | 588 | |
---|
[3730] | 589 | private static function noFollowURL($m) |
---|
| 590 | { |
---|
| 591 | if (preg_match('/rel="nofollow"/', $m[1])) { |
---|
| 592 | return $m[0]; |
---|
| 593 | } |
---|
[2566] | 594 | |
---|
[3730] | 595 | return '<a' . $m[1] . ' rel="nofollow">'; |
---|
| 596 | } |
---|
[2566] | 597 | |
---|
[3730] | 598 | /** |
---|
| 599 | Returns comment author link to his website if he specified one. |
---|
[2566] | 600 | |
---|
[3730] | 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 | } |
---|
[2566] | 610 | |
---|
[3730] | 611 | /** |
---|
| 612 | Returns comment post full URL. |
---|
[2566] | 613 | |
---|
[3730] | 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 | } |
---|
[2566] | 623 | |
---|
[3730] | 624 | /** |
---|
| 625 | Returns comment author name in a link to his website if he specified one. |
---|
[2566] | 626 | |
---|
[3730] | 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 | } |
---|
[2566] | 637 | |
---|
[3730] | 638 | $nofollow = ''; |
---|
| 639 | if ($rs->core->blog->settings->system->comments_nofollow) { |
---|
| 640 | $nofollow = ' rel="nofollow"'; |
---|
| 641 | } |
---|
[2566] | 642 | |
---|
[3730] | 643 | return sprintf($res, html::escapeHTML($rs->comment_author), html::escapeHTML($url), $nofollow); |
---|
| 644 | } |
---|
[2566] | 645 | |
---|
[3730] | 646 | /** |
---|
| 647 | Returns comment author e-mail address. If <var>$encoded</var> is true, |
---|
| 648 | "@" sign is replaced by "%40" and "." by "%2e". |
---|
[2566] | 649 | |
---|
[3730] | 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 | } |
---|
[2566] | 661 | |
---|
[3730] | 662 | /** |
---|
| 663 | Returns trackback site title if comment is a trackback. |
---|
[2566] | 664 | |
---|
[3730] | 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 | } |
---|
[2566] | 676 | |
---|
[3730] | 677 | /** |
---|
| 678 | Returns trackback content if comment is a trackback. |
---|
[2566] | 679 | |
---|
[3730] | 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 | } |
---|
[2566] | 690 | |
---|
[3730] | 691 | /** |
---|
| 692 | Returns comment feed unique ID. |
---|
[2566] | 693 | |
---|
[3730] | 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 | } |
---|
[2566] | 701 | |
---|
[3730] | 702 | /** |
---|
| 703 | Returns whether comment is from the post author. |
---|
[2566] | 704 | |
---|
[3730] | 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 | } |
---|
[0] | 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. |
---|
[3730] | 726 | */ |
---|
[0] | 727 | class rsExtDates |
---|
| 728 | { |
---|
[3730] | 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 | } |
---|
[2566] | 737 | |
---|
[3730] | 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 | } |
---|
[2566] | 746 | |
---|
[3730] | 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 | } |
---|
[2566] | 755 | |
---|
[3730] | 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 | } |
---|
[2566] | 764 | |
---|
[3730] | 765 | /** |
---|
| 766 | Returns date month archive full URL. |
---|
[2566] | 767 | |
---|
[3730] | 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)); |
---|
[2566] | 775 | |
---|
[3730] | 776 | return $core->blog->url . $core->url->getURLFor('archive', $url); |
---|
| 777 | } |
---|
[2566] | 778 | |
---|
[3730] | 779 | /** |
---|
| 780 | Returns whether date is the first of year. |
---|
[2566] | 781 | |
---|
[3730] | 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 | } |
---|
[2566] | 790 | |
---|
[3730] | 791 | $y = $rs->year(); |
---|
| 792 | $rs->movePrev(); |
---|
| 793 | $py = $rs->year(); |
---|
| 794 | $rs->moveNext(); |
---|
[2566] | 795 | |
---|
[3730] | 796 | return $y != $py; |
---|
| 797 | } |
---|
[2566] | 798 | |
---|
[3730] | 799 | /** |
---|
| 800 | Returns whether date is the last of year. |
---|
[2566] | 801 | |
---|
[3730] | 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 | } |
---|
[2566] | 810 | |
---|
[3730] | 811 | $y = $rs->year(); |
---|
| 812 | if ($rs->moveNext()) { |
---|
| 813 | $ny = $rs->year(); |
---|
| 814 | $rs->movePrev(); |
---|
| 815 | return $y != $ny; |
---|
| 816 | } |
---|
| 817 | return false; |
---|
[2566] | 818 | |
---|
[3730] | 819 | } |
---|
[0] | 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. |
---|
[3730] | 832 | */ |
---|
[0] | 833 | class rsExtUser |
---|
| 834 | { |
---|
[3730] | 835 | private static $sortfield; |
---|
| 836 | private static $sortsign; |
---|
| 837 | /** |
---|
| 838 | Returns a user option. |
---|
[2566] | 839 | |
---|
[3730] | 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); |
---|
[2566] | 847 | |
---|
[3730] | 848 | if (isset($options[$name])) { |
---|
| 849 | return $options[$name]; |
---|
| 850 | } |
---|
| 851 | return; |
---|
| 852 | } |
---|
[2566] | 853 | |
---|
[3730] | 854 | /** |
---|
| 855 | Returns all user options. |
---|
[2566] | 856 | |
---|
[3730] | 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 | } |
---|
[3105] | 868 | |
---|
[3730] | 869 | /** |
---|
| 870 | Converts this record to a {@link extStaticRecord} instance. |
---|
[3105] | 871 | |
---|
[3730] | 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 | } |
---|
[0] | 881 | } |
---|
[3105] | 882 | |
---|
[3106] | 883 | class rsExtBlog |
---|
| 884 | { |
---|
[3730] | 885 | private static $sortfield; |
---|
| 886 | private static $sortsign; |
---|
| 887 | /** |
---|
| 888 | Converts this record to a {@link extStaticRecord} instance. |
---|
[3106] | 889 | |
---|
[3730] | 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 | } |
---|
[3106] | 899 | } |
---|
| 900 | |
---|
[3105] | 901 | class extStaticRecord extends staticRecord |
---|
| 902 | { |
---|
[3730] | 903 | private $sortfield; |
---|
| 904 | private $sortsign; |
---|
[3105] | 905 | |
---|
[3730] | 906 | public function __construct($rs) |
---|
| 907 | { |
---|
| 908 | parent::__construct($rs->__data, $rs->__info); |
---|
| 909 | } |
---|
[3105] | 910 | |
---|
[3730] | 911 | /** |
---|
| 912 | Lexically sort. |
---|
[3105] | 913 | |
---|
[3730] | 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; |
---|
[3105] | 921 | |
---|
[3730] | 922 | usort($this->__data, array($this, 'lexicalSortCallback')); |
---|
[3105] | 923 | |
---|
[3730] | 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]; |
---|
[3105] | 931 | |
---|
[3730] | 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 | } |
---|
[3105] | 938 | |
---|
[3730] | 939 | return strcoll(strtolower(dcUtils::removeDiacritics($a)), strtolower(dcUtils::removeDiacritics($b))) * $this->sortsign; |
---|
| 940 | } |
---|
[3105] | 941 | } |
---|