Dotclear

source: inc/public/lib.urlhandlers.php @ 2635:ad168cf3085a

Revision 2635:ad168cf3085a, 16.8 KB checked in by Dsls, 12 years ago (diff)

Merge with 2.6

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 = json_decode($_COOKIE['dc_passwd']);
377                              if ($pwd_cookie === NULL) {
378                                   $pwd_cookie = array();
379                              } else {
380                                   $pwd_cookie = (array) $pwd_cookie;
381                              }
382                         } else {
383                              $pwd_cookie = array();
384                         }
385
386                         # Check for match
387                         if ((!empty($_POST['password']) && $_POST['password'] == $post_password)
388                         || (isset($pwd_cookie[$post_id]) && $pwd_cookie[$post_id] == $post_password))
389                         {
390                              $pwd_cookie[$post_id] = $post_password;
391                              setcookie('dc_passwd',json_encode($pwd_cookie),0,'/');
392                         }
393                         else
394                         {
395                              self::serveDocument('password-form.html','text/html',false);
396                              return;
397                         }
398                    }
399
400                    $post_comment =
401                         isset($_POST['c_name']) && isset($_POST['c_mail']) &&
402                         isset($_POST['c_site']) && isset($_POST['c_content']) &&
403                         $_ctx->posts->commentsActive();
404
405                    # Posting a comment
406                    if ($post_comment)
407                    {
408                         # Spam trap
409                         if (!empty($_POST['f_mail'])) {
410                              http::head(412,'Precondition Failed');
411                              header('Content-Type: text/plain');
412                              echo "So Long, and Thanks For All the Fish";
413                              # Exits immediately the application to preserve the server.
414                              exit;
415                         }
416
417                         $name = $_POST['c_name'];
418                         $mail = $_POST['c_mail'];
419                         $site = $_POST['c_site'];
420                         $content = $_POST['c_content'];
421                         $preview = !empty($_POST['preview']);
422
423                         if ($content != '')
424                         {
425                              # --BEHAVIOR-- publicBeforeCommentTransform
426                              $buffer = $core->callBehavior('publicBeforeCommentTransform',$content);
427                              if ($buffer != '') {
428                                   $content = $buffer;
429                              } else {
430                                   if ($core->blog->settings->system->wiki_comments) {
431                                        $core->initWikiComment();
432                                   } else {
433                                        $core->initWikiSimpleComment();
434                                   }
435                                   $content = $core->wikiTransform($content);
436                              }
437                              $content = $core->HTMLfilter($content);
438                         }
439
440                         $_ctx->comment_preview['content'] = $content;
441                         $_ctx->comment_preview['rawcontent'] = $_POST['c_content'];
442                         $_ctx->comment_preview['name'] = $name;
443                         $_ctx->comment_preview['mail'] = $mail;
444                         $_ctx->comment_preview['site'] = $site;
445
446                         if ($preview)
447                         {
448                              # --BEHAVIOR-- publicBeforeCommentPreview
449                              $core->callBehavior('publicBeforeCommentPreview',$_ctx->comment_preview);
450
451                              $_ctx->comment_preview['preview'] = true;
452                         }
453                         else
454                         {
455                              # Post the comment
456                              $cur = $core->con->openCursor($core->prefix.'comment');
457                              $cur->comment_author = $name;
458                              $cur->comment_site = html::clean($site);
459                              $cur->comment_email = html::clean($mail);
460                              $cur->comment_content = $content;
461                              $cur->post_id = $_ctx->posts->post_id;
462                              $cur->comment_status = $core->blog->settings->system->comments_pub ? 1 : -1;
463                              $cur->comment_ip = http::realIP();
464
465                              $redir = $_ctx->posts->getURL();
466                              $redir .= $core->blog->settings->system->url_scan == 'query_string' ? '&' : '?';
467
468                              try
469                              {
470                                   if (!text::isEmail($cur->comment_email)) {
471                                        throw new Exception(__('You must provide a valid email address.'));
472                                   }
473
474                                   # --BEHAVIOR-- publicBeforeCommentCreate
475                                   $core->callBehavior('publicBeforeCommentCreate',$cur);
476                                   if ($cur->post_id) {
477                                        $comment_id = $core->blog->addComment($cur);
478
479                                        # --BEHAVIOR-- publicAfterCommentCreate
480                                        $core->callBehavior('publicAfterCommentCreate',$cur,$comment_id);
481                                   }
482
483                                   if ($cur->comment_status == 1) {
484                                        $redir_arg = 'pub=1';
485                                   } else {
486                                        $redir_arg = 'pub=0';
487                                   }
488
489                                   header('Location: '.$redir.$redir_arg);
490                              }
491                              catch (Exception $e)
492                              {
493                                   $_ctx->form_error = $e->getMessage();
494                                   $_ctx->form_error;
495                              }
496                         }
497                    }
498
499                    # The entry
500                    if ($_ctx->posts->trackbacksActive()) {
501                         header('X-Pingback: '.$core->blog->url.$core->url->getURLFor("xmlrpc",$core->blog->id));
502                    }
503                    self::serveDocument('post.html');
504               }
505          }
506     }
507
508     public static function preview($args)
509     {
510          $core = $GLOBALS['core'];
511          $_ctx = $GLOBALS['_ctx'];
512
513          if (!preg_match('#^(.+?)/([0-9a-z]{40})/(.+?)$#',$args,$m)) {
514               # The specified Preview URL is malformed.
515               self::p404();
516          }
517          else
518          {
519               $user_id = $m[1];
520               $user_key = $m[2];
521               $post_url = $m[3];
522               if (!$core->auth->checkUser($user_id,null,$user_key)) {
523                    # The user has no access to the entry.
524                    self::p404();
525               }
526               else
527               {
528                    $_ctx->preview = true;
529                    self::post($post_url);
530               }
531          }
532     }
533
534     public static function feed($args)
535     {
536          $type = null;
537          $comments = false;
538          $cat_url = false;
539          $post_id = null;
540          $subtitle = '';
541
542          $mime = 'application/xml';
543
544          $_ctx =& $GLOBALS['_ctx'];
545          $core =& $GLOBALS['core'];
546
547          if (preg_match('!^([a-z]{2}(-[a-z]{2})?)/(.*)$!',$args,$m)) {
548               $params = new ArrayObject(array('lang' => $m[1]));
549
550               $args = $m[3];
551
552               $core->callBehavior('publicFeedBeforeGetLangs',$params,$args);
553
554               $_ctx->langs = $core->blog->getLangs($params);
555
556               if ($_ctx->langs->isEmpty()) {
557                    # The specified language does not exist.
558                    self::p404();
559                    return;
560               } else {
561                    $_ctx->cur_lang = $m[1];
562               }
563          }
564
565          if (preg_match('#^rss2/xslt$#',$args,$m))
566          {
567               # RSS XSLT stylesheet
568               self::serveDocument('rss2.xsl','text/xml');
569               return;
570          }
571          elseif (preg_match('#^(atom|rss2)/comments/([0-9]+)$#',$args,$m))
572          {
573               # Post comments feed
574               $type = $m[1];
575               $comments = true;
576               $post_id = (integer) $m[2];
577          }
578          elseif (preg_match('#^(?:category/(.+)/)?(atom|rss2)(/comments)?$#',$args,$m))
579          {
580               # All posts or comments feed
581               $type = $m[2];
582               $comments = !empty($m[3]);
583               if (!empty($m[1])) {
584                    $cat_url = $m[1];
585               }
586          }
587          else
588          {
589               # The specified Feed URL is malformed.
590               self::p404();
591               return;
592          }
593
594          if ($cat_url)
595          {
596               $params = new ArrayObject(array(
597                    'cat_url' => $cat_url,
598                    'post_type' => 'post'));
599
600               $core->callBehavior('publicFeedBeforeGetCategories',$params,$args);
601
602               $_ctx->categories = $core->blog->getCategories($params);
603
604               if ($_ctx->categories->isEmpty()) {
605                    # The specified category does no exist.
606                    self::p404();
607                    return;
608               }
609
610               $subtitle = ' - '.$_ctx->categories->cat_title;
611          }
612          elseif ($post_id)
613          {
614               $params = new ArrayObject(array(
615                    'post_id' => $post_id,
616                    'post_type' => ''));
617
618               $core->callBehavior('publicFeedBeforeGetPosts',$params,$args);
619
620               $_ctx->posts = $core->blog->getPosts($params);
621
622               if ($_ctx->posts->isEmpty()) {
623                    # The specified post does not exist.
624                    self::p404();
625                    return;
626               }
627
628               $subtitle = ' - '.$_ctx->posts->post_title;
629          }
630
631          $tpl = $type;
632          if ($comments) {
633               $tpl .= '-comments';
634               $_ctx->nb_comment_per_page = $core->blog->settings->system->nb_comment_per_feed;
635          } else {
636               $_ctx->nb_entry_per_page = $core->blog->settings->system->nb_post_per_feed;
637               $_ctx->short_feed_items = $core->blog->settings->system->short_feed_items;
638          }
639          $tpl .= '.xml';
640
641          if ($type == 'atom') {
642               $mime = 'application/atom+xml';
643          }
644
645          $_ctx->feed_subtitle = $subtitle;
646
647          header('X-Robots-Tag: '.context::robotsPolicy($core->blog->settings->system->robots_policy,''));
648          self::serveDocument($tpl,$mime);
649          if (!$comments && !$cat_url) {
650               $core->blog->publishScheduledEntries();
651          }
652     }
653
654     public static function trackback($args)
655     {
656          if (!preg_match('/^[0-9]+$/',$args)) {
657               # The specified trackback URL is not an number
658               self::p404();
659          } else {
660               $tb = new dcTrackback($GLOBALS['core']);
661               $tb->receive($args);
662          }
663     }
664
665     public static function rsd($args)
666     {
667          $core =& $GLOBALS['core'];
668          http::cache($GLOBALS['mod_files'],$GLOBALS['mod_ts']);
669
670          header('Content-Type: text/xml; charset=UTF-8');
671          echo
672          '<?xml version="1.0" encoding="UTF-8"?>'."\n".
673          '<rsd version="1.0" xmlns="http://archipelago.phrasewise.com/rsd">'."\n".
674          "<service>\n".
675          "  <engineName>Dotclear</engineName>\n".
676          "  <engineLink>http://www.dotclear.org/</engineLink>\n".
677          '  <homePageLink>'.html::escapeHTML($core->blog->url)."</homePageLink>\n";
678
679          if ($core->blog->settings->system->enable_xmlrpc)
680          {
681               $u = sprintf(DC_XMLRPC_URL,$core->blog->url,$core->blog->id);
682
683               echo
684               "  <apis>\n".
685               '    <api name="WordPress" blogID="1" preferred="true" apiLink="'.$u.'"/>'."\n".
686               '    <api name="Movable Type" blogID="1" preferred="false" apiLink="'.$u.'"/>'."\n".
687               '    <api name="MetaWeblog" blogID="1" preferred="false" apiLink="'.$u.'"/>'."\n".
688               '    <api name="Blogger" blogID="1" preferred="false" apiLink="'.$u.'"/>'."\n".
689               "  </apis>\n";
690          }
691
692          echo
693          "</service>\n".
694          "</rsd>\n";
695     }
696
697     public static function xmlrpc($args)
698     {
699          $core =& $GLOBALS['core'];
700          $blog_id = preg_replace('#^([^/]*).*#','$1',$args);
701          $server = new dcXmlRpc($core,$blog_id);
702          $server->serve();
703     }
704}
Note: See TracBrowser for help on using the repository browser.

Sites map