1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # This file is part of Dotclear 2. |
---|
4 | # |
---|
5 | # Copyright (c) 2003-2011 Olivier Meunier & Association Dotclear |
---|
6 | # Licensed under the GPL version 2.0 license. |
---|
7 | # See LICENSE file or |
---|
8 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
9 | # |
---|
10 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
11 | if (!defined('DC_RC_PATH')) { return; } |
---|
12 | |
---|
13 | /** |
---|
14 | @ingroup DC_CORE |
---|
15 | @brief Dotclear post record helpers. |
---|
16 | |
---|
17 | This class adds new methods to database post results. |
---|
18 | You can call them on every record comming from dcBlog::getPosts and similar |
---|
19 | methods. |
---|
20 | |
---|
21 | @warning You should not give the first argument (usualy $rs) of every described |
---|
22 | function. |
---|
23 | */ |
---|
24 | class rsExtPost |
---|
25 | { |
---|
26 | /** |
---|
27 | Returns whether post is editable. |
---|
28 | |
---|
29 | @param rs Invisible parameter |
---|
30 | @return <b>boolean</b> |
---|
31 | */ |
---|
32 | public static function isEditable($rs) |
---|
33 | { |
---|
34 | # If user is admin or contentadmin, true |
---|
35 | if ($rs->core->auth->check('contentadmin',$rs->core->blog->id)) { |
---|
36 | return true; |
---|
37 | } |
---|
38 | |
---|
39 | # No user id in result ? false |
---|
40 | if (!$rs->exists('user_id')) { |
---|
41 | return false; |
---|
42 | } |
---|
43 | |
---|
44 | # If user is usage and owner of the entrie |
---|
45 | if ($rs->core->auth->check('usage',$rs->core->blog->id) |
---|
46 | && $rs->user_id == $rs->core->auth->userID()) { |
---|
47 | return true; |
---|
48 | } |
---|
49 | |
---|
50 | return false; |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | Returns whether post is deletable |
---|
55 | |
---|
56 | @param rs Invisible parameter |
---|
57 | @return <b>boolean</b> |
---|
58 | */ |
---|
59 | public static function isDeletable($rs) |
---|
60 | { |
---|
61 | # If user is admin, or contentadmin, true |
---|
62 | if ($rs->core->auth->check('contentadmin',$rs->core->blog->id)) { |
---|
63 | return true; |
---|
64 | } |
---|
65 | |
---|
66 | # No user id in result ? false |
---|
67 | if (!$rs->exists('user_id')) { |
---|
68 | return false; |
---|
69 | } |
---|
70 | |
---|
71 | # If user has delete rights and is owner of the entrie |
---|
72 | if ($rs->core->auth->check('delete',$rs->core->blog->id) |
---|
73 | && $rs->user_id == $rs->core->auth->userID()) { |
---|
74 | return true; |
---|
75 | } |
---|
76 | |
---|
77 | return false; |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | Returns whether post is the first one of its day. |
---|
82 | |
---|
83 | @param rs Invisible parameter |
---|
84 | @return <b>boolean</b> |
---|
85 | */ |
---|
86 | public static function firstPostOfDay($rs) |
---|
87 | { |
---|
88 | if ($rs->isStart()) { |
---|
89 | return true; |
---|
90 | } |
---|
91 | |
---|
92 | $cdate = date('Ymd',strtotime($rs->post_dt)); |
---|
93 | $rs->movePrev(); |
---|
94 | $ndate = date('Ymd',strtotime($rs->post_dt)); |
---|
95 | $rs->moveNext(); |
---|
96 | return $ndate != $cdate; |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | Returns whether post is the last one of its day. |
---|
101 | |
---|
102 | @param rs Invisible parameter |
---|
103 | @return <b>boolean</b> |
---|
104 | */ |
---|
105 | public static function lastPostOfDay($rs) |
---|
106 | { |
---|
107 | if ($rs->isEnd()) { |
---|
108 | return true; |
---|
109 | } |
---|
110 | |
---|
111 | $cdate = date('Ymd',strtotime($rs->post_dt)); |
---|
112 | $rs->moveNext(); |
---|
113 | $ndate = date('Ymd',strtotime($rs->post_dt)); |
---|
114 | $rs->movePrev(); |
---|
115 | return $ndate != $cdate; |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | Returns full post URL. |
---|
120 | |
---|
121 | @param rs Invisible parameter |
---|
122 | @return <b>string</b> |
---|
123 | */ |
---|
124 | public static function getURL($rs) |
---|
125 | { |
---|
126 | return $rs->core->blog->url.$rs->core->getPostPublicURL( |
---|
127 | $rs->post_type,html::sanitizeURL($rs->post_url) |
---|
128 | ); |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | Returns whether post has an excerpt. |
---|
133 | |
---|
134 | @param rs Invisible parameter |
---|
135 | @return <b>boolean</b> |
---|
136 | */ |
---|
137 | public static function isExtended($rs) |
---|
138 | { |
---|
139 | return $rs->post_excerpt_xhtml != ''; |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | Returns post timestamp. |
---|
144 | |
---|
145 | @param rs Invisible parameter |
---|
146 | @param type <b>string</b> (dt|upddt|creadt) defaults to post_dt |
---|
147 | @return <b>integer</b> |
---|
148 | */ |
---|
149 | public static function getTS($rs,$type='') |
---|
150 | { |
---|
151 | if ($type == 'upddt') { |
---|
152 | return strtotime($rs->post_upddt); |
---|
153 | } elseif ($type == 'creadt') { |
---|
154 | return strtotime($rs->post_creadt); |
---|
155 | } else { |
---|
156 | return strtotime($rs->post_dt); |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | Returns post date formating according to the ISO 8601 standard. |
---|
162 | |
---|
163 | @param rs Invisible parameter |
---|
164 | @param type <b>string</b> (dt|upddt|creadt) defaults to post_dt |
---|
165 | @return <b>string</b> |
---|
166 | */ |
---|
167 | public static function getISO8601Date($rs,$type='') |
---|
168 | { |
---|
169 | if ($type == 'upddt' || $type == 'creadt') { |
---|
170 | return dt::iso8601($rs->getTS($type)+dt::getTimeOffset($rs->post_tz),$rs->post_tz); |
---|
171 | } else { |
---|
172 | return dt::iso8601($rs->getTS(),$rs->post_tz); |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | /** |
---|
177 | Returns post date formating according to RFC 822. |
---|
178 | |
---|
179 | @param rs Invisible parameter |
---|
180 | @param type <b>string</b> (dt|upddt|creadt) defaults to post_dt |
---|
181 | @return <b>string</b> |
---|
182 | */ |
---|
183 | public static function getRFC822Date($rs,$type='') |
---|
184 | { |
---|
185 | if ($type == 'upddt' || $type == 'creadt') { |
---|
186 | return dt::rfc822($rs->getTS($type)+dt::getTimeOffset($rs->post_tz),$rs->post_tz); |
---|
187 | } else { |
---|
188 | return dt::rfc822($rs->getTS($type),$rs->post_tz); |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | /** |
---|
193 | Returns post date with <var>$format</var> as formatting pattern. If format |
---|
194 | is empty, uses <var>date_format</var> blog setting. |
---|
195 | |
---|
196 | @param rs Invisible parameter |
---|
197 | @param format <b>string</b> Date format pattern |
---|
198 | @param type <b>string</b> (dt|upddt|creadt) defaults to post_dt |
---|
199 | @return <b>string</b> |
---|
200 | */ |
---|
201 | public static function getDate($rs,$format,$type='') |
---|
202 | { |
---|
203 | if (!$format) { |
---|
204 | $format = $rs->core->blog->settings->system->date_format; |
---|
205 | } |
---|
206 | |
---|
207 | if ($type == 'upddt') { |
---|
208 | return dt::dt2str($format,$rs->post_upddt,$rs->post_tz); |
---|
209 | } elseif ($type == 'creadt') { |
---|
210 | return dt::dt2str($format,$rs->post_creadt,$rs->post_tz); |
---|
211 | } else { |
---|
212 | return dt::dt2str($format,$rs->post_dt); |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | /** |
---|
217 | Returns post time with <var>$format</var> as formatting pattern. If format |
---|
218 | is empty, uses <var>time_format</var> blog setting. |
---|
219 | |
---|
220 | @param rs Invisible parameter |
---|
221 | @param format <b>string</b> Time format pattern |
---|
222 | @param type <b>string</b> (dt|upddt|creadt) defaults to post_dt |
---|
223 | @return <b>string</b> |
---|
224 | */ |
---|
225 | public static function getTime($rs,$format,$type='') |
---|
226 | { |
---|
227 | if (!$format) { |
---|
228 | $format = $rs->core->blog->settings->system->time_format; |
---|
229 | } |
---|
230 | |
---|
231 | if ($type == 'upddt') { |
---|
232 | return dt::dt2str($format,$rs->post_upddt,$rs->post_tz); |
---|
233 | } elseif ($type == 'creadt') { |
---|
234 | return dt::dt2str($format,$rs->post_creadt,$rs->post_tz); |
---|
235 | } else { |
---|
236 | return dt::dt2str($format,$rs->post_dt); |
---|
237 | } |
---|
238 | } |
---|
239 | |
---|
240 | /** |
---|
241 | Returns author common name using user_id, user_name, user_firstname and |
---|
242 | user_displayname fields. |
---|
243 | |
---|
244 | @param rs Invisible parameter |
---|
245 | @return <b>string</b> |
---|
246 | */ |
---|
247 | public static function getAuthorCN($rs) |
---|
248 | { |
---|
249 | return dcUtils::getUserCN($rs->user_id, $rs->user_name, |
---|
250 | $rs->user_firstname, $rs->user_displayname); |
---|
251 | } |
---|
252 | |
---|
253 | /** |
---|
254 | Returns author common name with a link if he specified one in its |
---|
255 | preferences. |
---|
256 | |
---|
257 | @param rs Invisible parameter |
---|
258 | @return <b>string</b> |
---|
259 | */ |
---|
260 | public static function getAuthorLink($rs) |
---|
261 | { |
---|
262 | $res = '%1$s'; |
---|
263 | $url = $rs->user_url; |
---|
264 | if ($url) { |
---|
265 | $res = '<a href="%2$s">%1$s</a>'; |
---|
266 | } |
---|
267 | |
---|
268 | return sprintf($res,html::escapeHTML($rs->getAuthorCN()),html::escapeHTML($url)); |
---|
269 | } |
---|
270 | |
---|
271 | /** |
---|
272 | Returns author e-mail address. If <var>$encoded</var> is true, "@" sign is |
---|
273 | replaced by "%40" and "." by "%2e". |
---|
274 | |
---|
275 | @param rs Invisible parameter |
---|
276 | @param encoded <b>boolean</b> Encode address. |
---|
277 | @return <b>string</b> |
---|
278 | */ |
---|
279 | public static function getAuthorEmail($rs,$encoded=true) |
---|
280 | { |
---|
281 | if ($encoded) { |
---|
282 | return strtr($rs->user_email,array('@'=>'%40','.'=>'%2e')); |
---|
283 | } |
---|
284 | return $rs->user_email; |
---|
285 | } |
---|
286 | |
---|
287 | /** |
---|
288 | Returns post feed unique ID. |
---|
289 | |
---|
290 | @param rs Invisible parameter |
---|
291 | @return <b>string</b> |
---|
292 | */ |
---|
293 | public static function getFeedID($rs) |
---|
294 | { |
---|
295 | return 'urn:md5:'.md5($rs->core->blog->uid.$rs->post_id); |
---|
296 | |
---|
297 | $url = parse_url($rs->core->blog->url); |
---|
298 | $date_part = date('Y-m-d',strtotime($rs->post_creadt)); |
---|
299 | |
---|
300 | return 'tag:'.$url['host'].','.$date_part.':'.$rs->post_id; |
---|
301 | } |
---|
302 | |
---|
303 | /** |
---|
304 | Returns post content. If <var>$absolute_urls</var> is true, appends full |
---|
305 | blog URL to each relative post URLs. |
---|
306 | |
---|
307 | @param rs Invisible parameter |
---|
308 | @param absolute_urls <b>boolean</b> With absolute URLs |
---|
309 | @return <b>string</b> |
---|
310 | */ |
---|
311 | public static function getContent($rs,$absolute_urls=false) |
---|
312 | { |
---|
313 | if ($absolute_urls) { |
---|
314 | return html::absoluteURLs($rs->post_content_xhtml,$rs->getURL()); |
---|
315 | } else { |
---|
316 | return $rs->post_content_xhtml; |
---|
317 | } |
---|
318 | } |
---|
319 | |
---|
320 | /** |
---|
321 | Returns post excerpt. If <var>$absolute_urls</var> is true, appends full |
---|
322 | blog URL to each relative post URLs. |
---|
323 | |
---|
324 | @param rs Invisible parameter |
---|
325 | @param absolute_urls <b>boolean</b> With absolute URLs |
---|
326 | @return <b>string</b> |
---|
327 | */ |
---|
328 | public static function getExcerpt($rs,$absolute_urls=false) |
---|
329 | { |
---|
330 | if ($absolute_urls) { |
---|
331 | return html::absoluteURLs($rs->post_excerpt_xhtml,$rs->getURL()); |
---|
332 | } else { |
---|
333 | return $rs->post_excerpt_xhtml; |
---|
334 | } |
---|
335 | } |
---|
336 | |
---|
337 | } |
---|
338 | |
---|
339 | /** |
---|
340 | @ingroup DC_CORE |
---|
341 | @brief Dotclear dates record helpers. |
---|
342 | |
---|
343 | This class adds new methods to database dates results. |
---|
344 | You can call them on every record comming from dcBlog::getDates. |
---|
345 | |
---|
346 | @warning You should not give the first argument (usualy $rs) of every described |
---|
347 | function. |
---|
348 | */ |
---|
349 | class rsExtDates |
---|
350 | { |
---|
351 | /** |
---|
352 | @param rs Invisible parameter |
---|
353 | @return <b>integer</b> Date timestamp |
---|
354 | */ |
---|
355 | public static function ts($rs) |
---|
356 | { |
---|
357 | return strtotime($rs->dt); |
---|
358 | } |
---|
359 | |
---|
360 | /** |
---|
361 | @param rs Invisible parameter |
---|
362 | @return <b>string</b> Date year |
---|
363 | */ |
---|
364 | public static function year($rs) |
---|
365 | { |
---|
366 | return date('Y',strtotime($rs->dt)); |
---|
367 | } |
---|
368 | |
---|
369 | /** |
---|
370 | @param rs Invisible parameter |
---|
371 | @return <b>string</b> Date month |
---|
372 | */ |
---|
373 | public static function month($rs) |
---|
374 | { |
---|
375 | return date('m',strtotime($rs->dt)); |
---|
376 | } |
---|
377 | |
---|
378 | /** |
---|
379 | @param rs Invisible parameter |
---|
380 | @return <b>integer</b> Date day |
---|
381 | */ |
---|
382 | public static function day($rs) |
---|
383 | { |
---|
384 | return date('d',strtotime($rs->dt)); |
---|
385 | } |
---|
386 | |
---|
387 | /** |
---|
388 | Returns date month archive full URL. |
---|
389 | |
---|
390 | @param rs Invisible parameter |
---|
391 | @param core <b>dcCore</b> dcCore instance |
---|
392 | @return <b>integer</b> |
---|
393 | */ |
---|
394 | public static function url($rs,$core) |
---|
395 | { |
---|
396 | $url = date('Y/m',strtotime($rs->dt)); |
---|
397 | |
---|
398 | return $core->blog->url.$core->url->getURLFor('archive',$url); |
---|
399 | } |
---|
400 | |
---|
401 | /** |
---|
402 | Returns whether date is the first of year. |
---|
403 | |
---|
404 | @param rs Invisible parameter |
---|
405 | @return <b>boolean</b> |
---|
406 | */ |
---|
407 | public static function yearHeader($rs) |
---|
408 | { |
---|
409 | if ($rs->isStart()) { |
---|
410 | return true; |
---|
411 | } |
---|
412 | |
---|
413 | $y = $rs->year(); |
---|
414 | $rs->movePrev(); |
---|
415 | $py = $rs->year(); |
---|
416 | $rs->moveNext(); |
---|
417 | |
---|
418 | return $y != $py; |
---|
419 | } |
---|
420 | |
---|
421 | /** |
---|
422 | Returns whether date is the last of year. |
---|
423 | |
---|
424 | @param rs Invisible parameter |
---|
425 | @return <b>boolean</b> |
---|
426 | */ |
---|
427 | public static function yearFooter($rs) |
---|
428 | { |
---|
429 | if ($rs->isEnd()) { |
---|
430 | return true; |
---|
431 | } |
---|
432 | |
---|
433 | $y = $rs->year(); |
---|
434 | if ($rs->moveNext()) { |
---|
435 | $ny = $rs->year(); |
---|
436 | $rs->movePrev(); |
---|
437 | return $y != $ny; |
---|
438 | } |
---|
439 | return false; |
---|
440 | |
---|
441 | } |
---|
442 | } |
---|
443 | |
---|
444 | /** |
---|
445 | @ingroup DC_CORE |
---|
446 | @brief Dotclear dates record helpers. |
---|
447 | |
---|
448 | This class adds new methods to database dates results. |
---|
449 | You can call them on every record comming from dcAuth::checkUser and |
---|
450 | dcCore::getUsers. |
---|
451 | |
---|
452 | @warning You should not give the first argument (usualy $rs) of every described |
---|
453 | function. |
---|
454 | */ |
---|
455 | class rsExtUser |
---|
456 | { |
---|
457 | /** |
---|
458 | Returns a user option. |
---|
459 | |
---|
460 | @param rs Invisible parameter |
---|
461 | @param name <b>string</b> Option name |
---|
462 | @return <b>string</b> |
---|
463 | */ |
---|
464 | public static function option($rs,$name) |
---|
465 | { |
---|
466 | $options = self::options($rs); |
---|
467 | |
---|
468 | if (isset($options[$name])) { |
---|
469 | return $options[$name]; |
---|
470 | } |
---|
471 | return null; |
---|
472 | } |
---|
473 | |
---|
474 | /** |
---|
475 | Returns all user options. |
---|
476 | |
---|
477 | @param rs Invisible parameter |
---|
478 | @return <b>array</b> |
---|
479 | */ |
---|
480 | public static function options($rs) |
---|
481 | { |
---|
482 | $options = @unserialize($rs->user_options); |
---|
483 | if (is_array($options)) { |
---|
484 | return $options; |
---|
485 | } |
---|
486 | return array(); |
---|
487 | } |
---|
488 | } |
---|
489 | ?> |
---|