Dotclear

source: inc/public/lib.urlhandlers.php @ 2577:9a2b43a68e3d

Revision 2577:9a2b43a68e3d, 16.4 KB checked in by franck <carnet.franck.paul@…>, 12 years ago (diff)

Number of entries on first home page may be set (does not modify number of entries on other pages), fixes #1269

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
6# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
7# Licensed under the GPL version 2.0 license.
8# See LICENSE file or
9# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10#
11# -- END LICENSE BLOCK -----------------------------------------
12if (!defined('DC_RC_PATH')) { return; }
13
14class dcUrlHandlers extends urlHandler
15{
16     public $args;
17
18     public function getURLFor($type,$value='') {
19          $core =& $GLOBALS['core'];
20          $url = $core->callBehavior("publicGetURLFor",$type,$value);
21          if (!$url) {
22               $url = $this->getBase($type);
23               if ($value) {
24                    if ($url) {
25                         $url .= '/';
26                    }
27                    $url .= $value;
28               }
29          }
30          return $url;
31     }
32
33     public function register($type,$url,$representation,$handler)
34     {
35          $core =& $GLOBALS['core'];
36          $t = new ArrayObject(array($type,$url,$representation,$handler));
37          $core->callBehavior("publicRegisterURL",$t);
38          parent::register($t[0],$t[1],$t[2],$t[3]);
39     }
40
41     public static function p404()
42     {
43          throw new Exception ("Page not found",404);
44     }
45
46     public static function default404($args,$type,$e)
47     {
48          if ($e->getCode() != 404) {
49               throw $e;
50          }
51          $_ctx =& $GLOBALS['_ctx'];
52          $core = $GLOBALS['core'];
53
54          header('Content-Type: text/html; charset=UTF-8');
55          http::head(404,'Not Found');
56          $core->url->type = '404';
57          $_ctx->current_tpl = '404.html';
58          $_ctx->content_type = 'text/html';
59
60          echo $core->tpl->getData($_ctx->current_tpl);
61
62          # --BEHAVIOR-- publicAfterDocument
63          $core->callBehavior('publicAfterDocument',$core);
64          exit;
65     }
66
67     protected static function getPageNumber(&$args)
68     {
69          if (preg_match('#(^|/)page/([0-9]+)$#',$args,$m)) {
70               $n = (integer) $m[2];
71               if ($n > 0) {
72                    $args = preg_replace('#(^|/)page/([0-9]+)$#','',$args);
73                    return $n;
74               }
75          }
76
77          return false;
78     }
79
80     protected static function serveDocument($tpl,$content_type='text/html',$http_cache=true,$http_etag=true)
81     {
82          $_ctx =& $GLOBALS['_ctx'];
83          $core =& $GLOBALS['core'];
84
85          if ($_ctx->nb_entry_per_page === null) {
86               $_ctx->nb_entry_per_page = $core->blog->settings->system->nb_post_per_page;
87          }
88          if ($_ctx->nb_entry_first_page === null) {
89               $_ctx->nb_entry_first_page = $_ctx->nb_entry_per_page;
90          }
91
92          $tpl_file = $core->tpl->getFilePath($tpl);
93
94          if (!$tpl_file) {
95               throw new Exception('Unable to find template ');
96          }
97
98          $result = new ArrayObject;
99
100          $_ctx->current_tpl = $tpl;
101          $_ctx->content_type = $content_type;
102          $_ctx->http_cache = $http_cache;
103          $_ctx->http_etag = $http_etag;
104          $core->callBehavior('urlHandlerBeforeGetData',$_ctx);
105
106          if ($_ctx->http_cache) {
107               $GLOBALS['mod_files'][] = $tpl_file;
108               http::cache($GLOBALS['mod_files'],$GLOBALS['mod_ts']);
109          }
110
111          header('Content-Type: '.$_ctx->content_type.'; charset=UTF-8');
112          $result['content'] = $core->tpl->getData($_ctx->current_tpl);
113          $result['content_type'] = $_ctx->content_type;
114          $result['tpl'] = $_ctx->current_tpl;
115          $result['blogupddt'] = $core->blog->upddt;
116
117          # --BEHAVIOR-- urlHandlerServeDocument
118          $core->callBehavior('urlHandlerServeDocument',$result);
119
120          if ($_ctx->http_cache && $_ctx->http_etag) {
121               http::etag($result['content'],http::getSelfURI());
122          }
123          echo $result['content'];
124     }
125
126     public function getDocument()
127     {
128          $core =& $GLOBALS['core'];
129
130          $type = $args = '';
131
132          if ($this->mode == 'path_info')
133          {
134               $part = substr($_SERVER['PATH_INFO'],1);
135          }
136          else
137          {
138               $part = '';
139
140               $qs = $this->parseQueryString();
141
142               # Recreates some _GET and _REQUEST pairs
143               if (!empty($qs))
144               {
145                    foreach ($_GET as $k => $v) {
146                         if (isset($_REQUEST[$k])) {
147                              unset($_REQUEST[$k]);
148                         }
149                    }
150                    $_GET = $qs;
151                    $_REQUEST = array_merge($qs,$_REQUEST);
152
153                    list($k,$v) = each($qs);
154                    if ($v === null) {
155                         $part = $k;
156                         unset($_GET[$k]);
157                         unset($_REQUEST[$k]);
158                    }
159               }
160          }
161
162          $_SERVER['URL_REQUEST_PART'] = $part;
163
164          $this->getArgs($part,$type,$this->args);
165
166          # --BEHAVIOR-- urlHandlerGetArgsDocument
167          $core->callBehavior('urlHandlerGetArgsDocument',$this);
168
169          if (!$type)
170          {
171               $this->type = 'default';
172               $this->callDefaultHandler($this->args);
173          }
174          else
175          {
176               $this->type = $type;
177               $this->callHandler($type,$this->args);
178          }
179     }
180
181     public static function home($args)
182     {
183          $n = self::getPageNumber($args);
184
185          if ($args && !$n)
186          {
187               # "Then specified URL went unrecognized by all URL handlers and
188               # defaults to the home page, but is not a page number.
189               self::p404();
190          }
191          else
192          {
193               $_ctx =& $GLOBALS['_ctx'];
194               $core =& $GLOBALS['core'];
195
196               if ($n) {
197                    $GLOBALS['_page_number'] = $n;
198                    $core->url->type = $n > 1 ? 'default-page' : 'default';
199               }
200
201               if (empty($_GET['q'])) {
202                    if ($core->blog->settings->system->nb_post_for_home !== null) {
203                         $_ctx->nb_entry_first_page = $core->blog->settings->system->nb_post_for_home;
204                    }
205                    self::serveDocument('home.html');
206                    $core->blog->publishScheduledEntries();
207               } else {
208                    self::search();
209               }
210          }
211     }
212
213     public static function search()
214     {
215          $_ctx =& $GLOBALS['_ctx'];
216          $core =& $GLOBALS['core'];
217
218          $core->url->type='search';
219
220          $GLOBALS['_search'] = !empty($_GET['q']) ? rawurldecode($_GET['q']) : '';
221          if ($GLOBALS['_search']) {
222               $params = new ArrayObject(array('search' => $GLOBALS['_search']));
223               $core->callBehavior('publicBeforeSearchCount',$params);
224               $GLOBALS['_search_count'] = $core->blog->getPosts($params,true)->f(0);
225          }
226
227          self::serveDocument('search.html');
228     }
229
230     public static function lang($args)
231     {
232          $_ctx =& $GLOBALS['_ctx'];
233          $core =& $GLOBALS['core'];
234
235          $n = self::getPageNumber($args);
236          $params = new ArrayObject(array(
237               'lang' => $args));
238
239          $core->callBehavior('publicLangBeforeGetLangs',$params,$args);
240
241          $_ctx->langs = $core->blog->getLangs($params);
242
243          if ($_ctx->langs->isEmpty()) {
244               # The specified language does not exist.
245               self::p404();
246          }
247          else
248          {
249               if ($n) {
250                    $GLOBALS['_page_number'] = $n;
251               }
252               $_ctx->cur_lang = $args;
253               self::home(null);
254          }
255     }
256
257     public static function category($args)
258     {
259          $_ctx =& $GLOBALS['_ctx'];
260          $core =& $GLOBALS['core'];
261
262          $n = self::getPageNumber($args);
263
264          if ($args == '' && !$n) {
265               # No category was specified.
266               self::p404();
267          }
268          else
269          {
270               $params = new ArrayObject(array(
271                    'cat_url' => $args,
272                    'post_type' => 'post',
273                    'without_empty' => false));
274
275               $core->callBehavior('publicCategoryBeforeGetCategories',$params,$args);
276
277               $_ctx->categories = $core->blog->getCategories($params);
278
279               if ($_ctx->categories->isEmpty()) {
280                    # The specified category does no exist.
281                    self::p404();
282               }
283               else
284               {
285                    if ($n) {
286                         $GLOBALS['_page_number'] = $n;
287                    }
288                    self::serveDocument('category.html');
289               }
290          }
291     }
292
293     public static function archive($args)
294     {
295          $_ctx =& $GLOBALS['_ctx'];
296          $core =& $GLOBALS['core'];
297
298          $year = $month = $cat_url = null;
299          # Nothing or year and month
300          if ($args == '')
301          {
302               self::serveDocument('archive.html');
303          }
304          elseif (preg_match('|^/([0-9]{4})/([0-9]{2})$|',$args,$m))
305          {
306               $params = new ArrayObject(array(
307                    'year' => $m[1],
308                    'month' => $m[2],
309                    'type' => 'month'));
310
311               $core->callBehavior('publicArchiveBeforeGetDates',$params,$args);
312
313               $_ctx->archives = $core->blog->getDates($params);
314
315               if ($_ctx->archives->isEmpty()) {
316                    # There is no entries for the specified period.
317                    self::p404();
318               }
319               else
320               {
321                    self::serveDocument('archive_month.html');
322               }
323          }
324          else {
325               # The specified URL is not a date.
326               self::p404();
327          }
328     }
329
330     public static function post($args)
331     {
332          if ($args == '') {
333               # No entry was specified.
334               self::p404();
335          }
336          else
337          {
338               $_ctx =& $GLOBALS['_ctx'];
339               $core =& $GLOBALS['core'];
340
341               $core->blog->withoutPassword(false);
342
343               $params = new ArrayObject(array(
344                    'post_url' => $args));
345
346               $core->callBehavior('publicPostBeforeGetPosts',$params,$args);
347
348               $_ctx->posts = $core->blog->getPosts($params);
349
350               $_ctx->comment_preview = new ArrayObject();
351               $_ctx->comment_preview['content'] = '';
352               $_ctx->comment_preview['rawcontent'] = '';
353               $_ctx->comment_preview['name'] = '';
354               $_ctx->comment_preview['mail'] = '';
355               $_ctx->comment_preview['site'] = '';
356               $_ctx->comment_preview['preview'] = false;
357               $_ctx->comment_preview['remember'] = false;
358
359               $core->blog->withoutPassword(true);
360
361               if ($_ctx->posts->isEmpty())
362               {
363                    # The specified entry does not exist.
364                    self::p404();
365               }
366               else
367               {
368                    $post_id = $_ctx->posts->post_id;
369                    $post_password = $_ctx->posts->post_password;
370
371                    # Password protected entry
372                    if ($post_password != '' && !$_ctx->preview)
373                    {
374                         # Get passwords cookie
375                         if (isset($_COOKIE['dc_passwd'])) {
376                              $pwd_cookie = unserialize($_COOKIE['dc_passwd']);
377                         } else {
378                              $pwd_cookie = array();
379                         }
380
381                         # Check for match
382                         if ((!empty($_POST['password']) && $_POST['password'] == $post_password)
383                         || (isset($pwd_cookie[$post_id]) && $pwd_cookie[$post_id] == $post_password))
384                         {
385                              $pwd_cookie[$post_id] = $post_password;
386                              setcookie('dc_passwd',serialize($pwd_cookie),0,'/');
387                         }
388                         else
389                         {
390                              self::serveDocument('password-form.html','text/html',false);
391                              return;
392                         }
393                    }
394
395                    $post_comment =
396                         isset($_POST['c_name']) && isset($_POST['c_mail']) &&
397                         isset($_POST['c_site']) && isset($_POST['c_content']) &&
398                         $_ctx->posts->commentsActive();
399
400                    # Posting a comment
401                    if ($post_comment)
402                    {
403                         # Spam trap
404                         if (!empty($_POST['f_mail'])) {
405                              http::head(412,'Precondition Failed');
406                              header('Content-Type: text/plain');
407                              echo "So Long, and Thanks For All the Fish";
408                              # Exits immediately the application to preserve the server.
409                              exit;
410                         }
411
412                         $name = $_POST['c_name'];
413                         $mail = $_POST['c_mail'];
414                         $site = $_POST['c_site'];
415                         $content = $_POST['c_content'];
416                         $preview = !empty($_POST['preview']);
417
418                         if ($content != '')
419                         {
420                              if ($core->blog->settings->system->wiki_comments) {
421                                   $core->initWikiComment();
422                              } else {
423                                   $core->initWikiSimpleComment();
424                              }
425                              $content = $core->wikiTransform($content);
426                              $content = $core->HTMLfilter($content);
427                         }
428
429                         $_ctx->comment_preview['content'] = $content;
430                         $_ctx->comment_preview['rawcontent'] = $_POST['c_content'];
431                         $_ctx->comment_preview['name'] = $name;
432                         $_ctx->comment_preview['mail'] = $mail;
433                         $_ctx->comment_preview['site'] = $site;
434
435                         if ($preview)
436                         {
437                              # --BEHAVIOR-- publicBeforeCommentPreview
438                              $core->callBehavior('publicBeforeCommentPreview',$_ctx->comment_preview);
439
440                              $_ctx->comment_preview['preview'] = true;
441                         }
442                         else
443                         {
444                              # Post the comment
445                              $cur = $core->con->openCursor($core->prefix.'comment');
446                              $cur->comment_author = $name;
447                              $cur->comment_site = html::clean($site);
448                              $cur->comment_email = html::clean($mail);
449                              $cur->comment_content = $content;
450                              $cur->post_id = $_ctx->posts->post_id;
451                              $cur->comment_status = $core->blog->settings->system->comments_pub ? 1 : -1;
452                              $cur->comment_ip = http::realIP();
453
454                              $redir = $_ctx->posts->getURL();
455                              $redir .= $core->blog->settings->system->url_scan == 'query_string' ? '&' : '?';
456
457                              try
458                              {
459                                   if (!text::isEmail($cur->comment_email)) {
460                                        throw new Exception(__('You must provide a valid email address.'));
461                                   }
462
463                                   # --BEHAVIOR-- publicBeforeCommentCreate
464                                   $core->callBehavior('publicBeforeCommentCreate',$cur);
465                                   if ($cur->post_id) {
466                                        $comment_id = $core->blog->addComment($cur);
467
468                                        # --BEHAVIOR-- publicAfterCommentCreate
469                                        $core->callBehavior('publicAfterCommentCreate',$cur,$comment_id);
470                                   }
471
472                                   if ($cur->comment_status == 1) {
473                                        $redir_arg = 'pub=1';
474                                   } else {
475                                        $redir_arg = 'pub=0';
476                                   }
477
478                                   header('Location: '.$redir.$redir_arg);
479                              }
480                              catch (Exception $e)
481                              {
482                                   $_ctx->form_error = $e->getMessage();
483                                   $_ctx->form_error;
484                              }
485                         }
486                    }
487
488                    # The entry
489                    if ($_ctx->posts->trackbacksActive()) {
490                         header('X-Pingback: '.$core->blog->url.$core->url->getURLFor("xmlrpc",$core->blog->id));
491                    }
492                    self::serveDocument('post.html');
493               }
494          }
495     }
496
497     public static function preview($args)
498     {
499          $core = $GLOBALS['core'];
500          $_ctx = $GLOBALS['_ctx'];
501
502          if (!preg_match('#^(.+?)/([0-9a-z]{40})/(.+?)$#',$args,$m)) {
503               # The specified Preview URL is malformed.
504               self::p404();
505          }
506          else
507          {
508               $user_id = $m[1];
509               $user_key = $m[2];
510               $post_url = $m[3];
511               if (!$core->auth->checkUser($user_id,null,$user_key)) {
512                    # The user has no access to the entry.
513                    self::p404();
514               }
515               else
516               {
517                    $_ctx->preview = true;
518                    self::post($post_url);
519               }
520          }
521     }
522
523     public static function feed($args)
524     {
525          $type = null;
526          $comments = false;
527          $cat_url = false;
528          $post_id = null;
529          $subtitle = '';
530
531          $mime = 'application/xml';
532
533          $_ctx =& $GLOBALS['_ctx'];
534          $core =& $GLOBALS['core'];
535
536          if (preg_match('!^([a-z]{2}(-[a-z]{2})?)/(.*)$!',$args,$m)) {
537               $params = new ArrayObject(array('lang' => $m[1]));
538
539               $args = $m[3];
540
541               $core->callBehavior('publicFeedBeforeGetLangs',$params,$args);
542
543               $_ctx->langs = $core->blog->getLangs($params);
544
545               if ($_ctx->langs->isEmpty()) {
546                    # The specified language does not exist.
547                    self::p404();
548                    return;
549               } else {
550                    $_ctx->cur_lang = $m[1];
551               }
552          }
553
554          if (preg_match('#^rss2/xslt$#',$args,$m))
555          {
556               # RSS XSLT stylesheet
557               self::serveDocument('rss2.xsl','text/xml');
558               return;
559          }
560          elseif (preg_match('#^(atom|rss2)/comments/([0-9]+)$#',$args,$m))
561          {
562               # Post comments feed
563               $type = $m[1];
564               $comments = true;
565               $post_id = (integer) $m[2];
566          }
567          elseif (preg_match('#^(?:category/(.+)/)?(atom|rss2)(/comments)?$#',$args,$m))
568          {
569               # All posts or comments feed
570               $type = $m[2];
571               $comments = !empty($m[3]);
572               if (!empty($m[1])) {
573                    $cat_url = $m[1];
574               }
575          }
576          else
577          {
578               # The specified Feed URL is malformed.
579               self::p404();
580               return;
581          }
582
583          if ($cat_url)
584          {
585               $params = new ArrayObject(array(
586                    'cat_url' => $cat_url,
587                    'post_type' => 'post'));
588
589               $core->callBehavior('publicFeedBeforeGetCategories',$params,$args);
590
591               $_ctx->categories = $core->blog->getCategories($params);
592
593               if ($_ctx->categories->isEmpty()) {
594                    # The specified category does no exist.
595                    self::p404();
596                    return;
597               }
598
599               $subtitle = ' - '.$_ctx->categories->cat_title;
600          }
601          elseif ($post_id)
602          {
603               $params = new ArrayObject(array(
604                    'post_id' => $post_id,
605                    'post_type' => ''));
606
607               $core->callBehavior('publicFeedBeforeGetPosts',$params,$args);
608
609               $_ctx->posts = $core->blog->getPosts($params);
610
611               if ($_ctx->posts->isEmpty()) {
612                    # The specified post does not exist.
613                    self::p404();
614                    return;
615               }
616
617               $subtitle = ' - '.$_ctx->posts->post_title;
618          }
619
620          $tpl = $type;
621          if ($comments) {
622               $tpl .= '-comments';
623               $_ctx->nb_comment_per_page = $core->blog->settings->system->nb_comment_per_feed;
624          } else {
625               $_ctx->nb_entry_per_page = $core->blog->settings->system->nb_post_per_feed;
626               $_ctx->short_feed_items = $core->blog->settings->system->short_feed_items;
627          }
628          $tpl .= '.xml';
629
630          if ($type == 'atom') {
631               $mime = 'application/atom+xml';
632          }
633
634          $_ctx->feed_subtitle = $subtitle;
635
636          header('X-Robots-Tag: '.context::robotsPolicy($core->blog->settings->system->robots_policy,''));
637          self::serveDocument($tpl,$mime);
638          if (!$comments && !$cat_url) {
639               $core->blog->publishScheduledEntries();
640          }
641     }
642
643     public static function trackback($args)
644     {
645          if (!preg_match('/^[0-9]+$/',$args)) {
646               # The specified trackback URL is not an number
647               self::p404();
648          } else {
649               $tb = new dcTrackback($GLOBALS['core']);
650               $tb->receive($args);
651          }
652     }
653
654     public static function rsd($args)
655     {
656          $core =& $GLOBALS['core'];
657          http::cache($GLOBALS['mod_files'],$GLOBALS['mod_ts']);
658
659          header('Content-Type: text/xml; charset=UTF-8');
660          echo
661          '<?xml version="1.0" encoding="UTF-8"?>'."\n".
662          '<rsd version="1.0" xmlns="http://archipelago.phrasewise.com/rsd">'."\n".
663          "<service>\n".
664          "  <engineName>Dotclear</engineName>\n".
665          "  <engineLink>http://www.dotclear.org/</engineLink>\n".
666          '  <homePageLink>'.html::escapeHTML($core->blog->url)."</homePageLink>\n";
667
668          if ($core->blog->settings->system->enable_xmlrpc)
669          {
670               $u = sprintf(DC_XMLRPC_URL,$core->blog->url,$core->blog->id);
671
672               echo
673               "  <apis>\n".
674               '    <api name="WordPress" blogID="1" preferred="true" apiLink="'.$u.'"/>'."\n".
675               '    <api name="Movable Type" blogID="1" preferred="false" apiLink="'.$u.'"/>'."\n".
676               '    <api name="MetaWeblog" blogID="1" preferred="false" apiLink="'.$u.'"/>'."\n".
677               '    <api name="Blogger" blogID="1" preferred="false" apiLink="'.$u.'"/>'."\n".
678               "  </apis>\n";
679          }
680
681          echo
682          "</service>\n".
683          "</rsd>\n";
684     }
685
686     public static function xmlrpc($args)
687     {
688          $core =& $GLOBALS['core'];
689          $blog_id = preg_replace('#^([^/]*).*#','$1',$args);
690          $server = new dcXmlRpc($core,$blog_id);
691          $server->serve();
692     }
693}
Note: See TracBrowser for help on using the repository browser.

Sites map