Dotclear

source: inc/public/lib.urlhandlers.php @ 3415:5cdbb2593595

Revision 3415:5cdbb2593595, 18.2 KB checked in by Jean-Christian Denis, 9 years ago (diff)

Open trackbacks with behaviors and add basic Webmention support, closes #1917

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          if ($core->blog->settings->system->prevents_clickjacking) {
113               if ($_ctx->exists('xframeoption')) {
114                    $url = parse_url($_ctx->xframeoption);
115                    header(sprintf('X-Frame-Options: %s', is_array($url)?("ALLOW-FROM ".$url['scheme'].'://'.$url['host']):'SAMEORIGIN'));
116               } else {
117                    // Prevents Clickjacking as far as possible
118                    header('X-Frame-Options: SAMEORIGIN'); // FF 3.6.9+ Chrome 4.1+ IE 8+ Safari 4+ Opera 10.5+
119               }
120          }
121
122          $result['content'] = $core->tpl->getData($_ctx->current_tpl);
123          $result['content_type'] = $_ctx->content_type;
124          $result['tpl'] = $_ctx->current_tpl;
125          $result['blogupddt'] = $core->blog->upddt;
126
127          # --BEHAVIOR-- urlHandlerServeDocument
128          $core->callBehavior('urlHandlerServeDocument',$result);
129
130          if ($_ctx->http_cache && $_ctx->http_etag) {
131               http::etag($result['content'],http::getSelfURI());
132          }
133          echo $result['content'];
134     }
135
136     public function getDocument()
137     {
138          $core =& $GLOBALS['core'];
139
140          $type = $args = '';
141
142          if ($this->mode == 'path_info')
143          {
144               $part = substr($_SERVER['PATH_INFO'],1);
145          }
146          else
147          {
148               $part = '';
149
150               $qs = $this->parseQueryString();
151
152               # Recreates some _GET and _REQUEST pairs
153               if (!empty($qs))
154               {
155                    foreach ($_GET as $k => $v) {
156                         if (isset($_REQUEST[$k])) {
157                              unset($_REQUEST[$k]);
158                         }
159                    }
160                    $_GET = $qs;
161                    $_REQUEST = array_merge($qs,$_REQUEST);
162
163                    list($k,$v) = each($qs);
164                    if ($v === null) {
165                         $part = $k;
166                         unset($_GET[$k]);
167                         unset($_REQUEST[$k]);
168                    }
169               }
170          }
171
172          $_SERVER['URL_REQUEST_PART'] = $part;
173
174          $this->getArgs($part,$type,$this->args);
175
176          # --BEHAVIOR-- urlHandlerGetArgsDocument
177          $core->callBehavior('urlHandlerGetArgsDocument',$this);
178
179          if (!$type)
180          {
181               $this->type = 'default';
182               $this->callDefaultHandler($this->args);
183          }
184          else
185          {
186               $this->type = $type;
187               $this->callHandler($type,$this->args);
188          }
189     }
190
191     public static function home($args)
192     {
193          $n = self::getPageNumber($args);
194
195          if ($args && !$n)
196          {
197               # "Then specified URL went unrecognized by all URL handlers and
198               # defaults to the home page, but is not a page number.
199               self::p404();
200          }
201          else
202          {
203               $_ctx =& $GLOBALS['_ctx'];
204               $core =& $GLOBALS['core'];
205
206               if ($n) {
207                    $GLOBALS['_page_number'] = $n;
208                    $core->url->type = $n > 1 ? 'default-page' : 'default';
209               }
210
211               if (empty($_GET['q'])) {
212                    if ($core->blog->settings->system->nb_post_for_home !== null) {
213                         $_ctx->nb_entry_first_page = $core->blog->settings->system->nb_post_for_home;
214                    }
215                    self::serveDocument('home.html');
216                    $core->blog->publishScheduledEntries();
217               } else {
218                    self::search();
219               }
220          }
221     }
222
223     public static function search()
224     {
225          $_ctx =& $GLOBALS['_ctx'];
226          $core =& $GLOBALS['core'];
227
228          if ($core->blog->settings->system->no_search) {
229
230               # Search is disabled for this blog.
231               self::p404();
232
233          } else {
234
235               $core->url->type='search';
236
237               $GLOBALS['_search'] = !empty($_GET['q']) ? rawurldecode($_GET['q']) : '';
238               if ($GLOBALS['_search']) {
239                    $params = new ArrayObject(array('search' => $GLOBALS['_search']));
240                    $core->callBehavior('publicBeforeSearchCount',$params);
241                    $GLOBALS['_search_count'] = $core->blog->getPosts($params,true)->f(0);
242               }
243
244               self::serveDocument('search.html');
245          }
246     }
247
248     public static function lang($args)
249     {
250          $_ctx =& $GLOBALS['_ctx'];
251          $core =& $GLOBALS['core'];
252
253          $n = self::getPageNumber($args);
254          $params = new ArrayObject(array(
255               'lang' => $args));
256
257          $core->callBehavior('publicLangBeforeGetLangs',$params,$args);
258
259          $_ctx->langs = $core->blog->getLangs($params);
260
261          if ($_ctx->langs->isEmpty()) {
262               # The specified language does not exist.
263               self::p404();
264          }
265          else
266          {
267               if ($n) {
268                    $GLOBALS['_page_number'] = $n;
269               }
270               $_ctx->cur_lang = $args;
271               self::home(null);
272          }
273     }
274
275     public static function category($args)
276     {
277          $_ctx =& $GLOBALS['_ctx'];
278          $core =& $GLOBALS['core'];
279
280          $n = self::getPageNumber($args);
281
282          if ($args == '' && !$n) {
283               # No category was specified.
284               self::p404();
285          }
286          else
287          {
288               $params = new ArrayObject(array(
289                    'cat_url' => $args,
290                    'post_type' => 'post',
291                    'without_empty' => false));
292
293               $core->callBehavior('publicCategoryBeforeGetCategories',$params,$args);
294
295               $_ctx->categories = $core->blog->getCategories($params);
296
297               if ($_ctx->categories->isEmpty()) {
298                    # The specified category does no exist.
299                    self::p404();
300               }
301               else
302               {
303                    if ($n) {
304                         $GLOBALS['_page_number'] = $n;
305                    }
306                    self::serveDocument('category.html');
307               }
308          }
309     }
310
311     public static function archive($args)
312     {
313          $_ctx =& $GLOBALS['_ctx'];
314          $core =& $GLOBALS['core'];
315
316          $year = $month = $cat_url = null;
317          # Nothing or year and month
318          if ($args == '')
319          {
320               self::serveDocument('archive.html');
321          }
322          elseif (preg_match('|^/([0-9]{4})/([0-9]{2})$|',$args,$m))
323          {
324               $params = new ArrayObject(array(
325                    'year' => $m[1],
326                    'month' => $m[2],
327                    'type' => 'month'));
328
329               $core->callBehavior('publicArchiveBeforeGetDates',$params,$args);
330
331               $_ctx->archives = $core->blog->getDates($params);
332
333               if ($_ctx->archives->isEmpty()) {
334                    # There is no entries for the specified period.
335                    self::p404();
336               }
337               else
338               {
339                    self::serveDocument('archive_month.html');
340               }
341          }
342          else {
343               # The specified URL is not a date.
344               self::p404();
345          }
346     }
347
348     public static function post($args)
349     {
350          if ($args == '') {
351               # No entry was specified.
352               self::p404();
353          }
354          else
355          {
356               $_ctx =& $GLOBALS['_ctx'];
357               $core =& $GLOBALS['core'];
358
359               $core->blog->withoutPassword(false);
360
361               $params = new ArrayObject(array(
362                    'post_url' => $args));
363
364               $core->callBehavior('publicPostBeforeGetPosts',$params,$args);
365
366               $_ctx->posts = $core->blog->getPosts($params);
367
368               $_ctx->comment_preview = new ArrayObject();
369               $_ctx->comment_preview['content'] = '';
370               $_ctx->comment_preview['rawcontent'] = '';
371               $_ctx->comment_preview['name'] = '';
372               $_ctx->comment_preview['mail'] = '';
373               $_ctx->comment_preview['site'] = '';
374               $_ctx->comment_preview['preview'] = false;
375               $_ctx->comment_preview['remember'] = false;
376
377               $core->blog->withoutPassword(true);
378
379               if ($_ctx->posts->isEmpty())
380               {
381                    # The specified entry does not exist.
382                    self::p404();
383               }
384               else
385               {
386                    $post_id = $_ctx->posts->post_id;
387                    $post_password = $_ctx->posts->post_password;
388
389                    # Password protected entry
390                    if ($post_password != '' && !$_ctx->preview)
391                    {
392                         # Get passwords cookie
393                         if (isset($_COOKIE['dc_passwd'])) {
394                              $pwd_cookie = json_decode($_COOKIE['dc_passwd']);
395                              if ($pwd_cookie === NULL) {
396                                   $pwd_cookie = array();
397                              } else {
398                                   $pwd_cookie = (array) $pwd_cookie;
399                              }
400                         } else {
401                              $pwd_cookie = array();
402                         }
403
404                         # Check for match
405                         # Note: We must prefix post_id key with '#'' in pwd_cookie array in order to avoid integer conversion
406                         # because MyArray["12345"] is treated as MyArray[12345]
407                         if ((!empty($_POST['password']) && $_POST['password'] == $post_password)
408                         || (isset($pwd_cookie['#'.$post_id]) && $pwd_cookie['#'.$post_id] == $post_password))
409                         {
410                              $pwd_cookie['#'.$post_id] = $post_password;
411                              setcookie('dc_passwd',json_encode($pwd_cookie),0,'/');
412                         }
413                         else
414                         {
415                              self::serveDocument('password-form.html','text/html',false);
416                              return;
417                         }
418                    }
419
420                    $post_comment =
421                         isset($_POST['c_name']) && isset($_POST['c_mail']) &&
422                         isset($_POST['c_site']) && isset($_POST['c_content']) &&
423                         $_ctx->posts->commentsActive();
424
425                    # Posting a comment
426                    if ($post_comment)
427                    {
428                         # Spam trap
429                         if (!empty($_POST['f_mail'])) {
430                              http::head(412,'Precondition Failed');
431                              header('Content-Type: text/plain');
432                              echo "So Long, and Thanks For All the Fish";
433                              # Exits immediately the application to preserve the server.
434                              exit;
435                         }
436
437                         $name = $_POST['c_name'];
438                         $mail = $_POST['c_mail'];
439                         $site = $_POST['c_site'];
440                         $content = $_POST['c_content'];
441                         $preview = !empty($_POST['preview']);
442
443                         if ($content != '')
444                         {
445                              # --BEHAVIOR-- publicBeforeCommentTransform
446                              $buffer = $core->callBehavior('publicBeforeCommentTransform',$content);
447                              if ($buffer != '') {
448                                   $content = $buffer;
449                              } else {
450                                   if ($core->blog->settings->system->wiki_comments) {
451                                        $core->initWikiComment();
452                                   } else {
453                                        $core->initWikiSimpleComment();
454                                   }
455                                   $content = $core->wikiTransform($content);
456                              }
457                              $content = $core->HTMLfilter($content);
458                         }
459
460                         $_ctx->comment_preview['content'] = $content;
461                         $_ctx->comment_preview['rawcontent'] = $_POST['c_content'];
462                         $_ctx->comment_preview['name'] = $name;
463                         $_ctx->comment_preview['mail'] = $mail;
464                         $_ctx->comment_preview['site'] = $site;
465
466                         if ($preview)
467                         {
468                              # --BEHAVIOR-- publicBeforeCommentPreview
469                              $core->callBehavior('publicBeforeCommentPreview',$_ctx->comment_preview);
470
471                              $_ctx->comment_preview['preview'] = true;
472                         }
473                         else
474                         {
475                              # Post the comment
476                              $cur = $core->con->openCursor($core->prefix.'comment');
477                              $cur->comment_author = $name;
478                              $cur->comment_site = html::clean($site);
479                              $cur->comment_email = html::clean($mail);
480                              $cur->comment_content = $content;
481                              $cur->post_id = $_ctx->posts->post_id;
482                              $cur->comment_status = $core->blog->settings->system->comments_pub ? 1 : -1;
483                              $cur->comment_ip = http::realIP();
484
485                              $redir = $_ctx->posts->getURL();
486                              $redir .= $core->blog->settings->system->url_scan == 'query_string' ? '&' : '?';
487
488                              try
489                              {
490                                   if (!text::isEmail($cur->comment_email)) {
491                                        throw new Exception(__('You must provide a valid email address.'));
492                                   }
493
494                                   # --BEHAVIOR-- publicBeforeCommentCreate
495                                   $core->callBehavior('publicBeforeCommentCreate',$cur);
496                                   if ($cur->post_id) {
497                                        $comment_id = $core->blog->addComment($cur);
498
499                                        # --BEHAVIOR-- publicAfterCommentCreate
500                                        $core->callBehavior('publicAfterCommentCreate',$cur,$comment_id);
501                                   }
502
503                                   if ($cur->comment_status == 1) {
504                                        $redir_arg = 'pub=1';
505                                   } else {
506                                        $redir_arg = 'pub=0';
507                                   }
508
509                                   header('Location: '.$redir.$redir_arg);
510                              }
511                              catch (Exception $e)
512                              {
513                                   $_ctx->form_error = $e->getMessage();
514                                   $_ctx->form_error;
515                              }
516                         }
517                    }
518
519                    # The entry
520                    if ($_ctx->posts->trackbacksActive()) {
521                         header('X-Pingback: '.$core->blog->url.$core->url->getURLFor("xmlrpc",$core->blog->id));
522                         header('Link: <'.$core->blog->url.$core->url->getURLFor('webmention').'>; rel="webmention"');
523                    }
524                    self::serveDocument('post.html');
525               }
526          }
527     }
528
529     public static function preview($args)
530     {
531          $core = $GLOBALS['core'];
532          $_ctx = $GLOBALS['_ctx'];
533
534          if (!preg_match('#^(.+?)/([0-9a-z]{40})/(.+?)$#',$args,$m)) {
535               # The specified Preview URL is malformed.
536               self::p404();
537          }
538          else
539          {
540               $user_id = $m[1];
541               $user_key = $m[2];
542               $post_url = $m[3];
543               if (!$core->auth->checkUser($user_id,null,$user_key)) {
544                    # The user has no access to the entry.
545                    self::p404();
546               }
547               else
548               {
549                    $_ctx->preview = true;
550                    if (defined ("DC_ADMIN_URL")) {
551                         $_ctx->xframeoption=DC_ADMIN_URL;
552                    }
553                    self::post($post_url);
554               }
555          }
556     }
557
558     public static function feed($args)
559     {
560          $type = null;
561          $comments = false;
562          $cat_url = false;
563          $post_id = null;
564          $subtitle = '';
565
566          $mime = 'application/xml';
567
568          $_ctx =& $GLOBALS['_ctx'];
569          $core =& $GLOBALS['core'];
570
571          if (preg_match('!^([a-z]{2}(-[a-z]{2})?)/(.*)$!',$args,$m)) {
572               $params = new ArrayObject(array('lang' => $m[1]));
573
574               $args = $m[3];
575
576               $core->callBehavior('publicFeedBeforeGetLangs',$params,$args);
577
578               $_ctx->langs = $core->blog->getLangs($params);
579
580               if ($_ctx->langs->isEmpty()) {
581                    # The specified language does not exist.
582                    self::p404();
583                    return;
584               } else {
585                    $_ctx->cur_lang = $m[1];
586               }
587          }
588
589          if (preg_match('#^rss2/xslt$#',$args,$m))
590          {
591               # RSS XSLT stylesheet
592               self::serveDocument('rss2.xsl','text/xml');
593               return;
594          }
595          elseif (preg_match('#^(atom|rss2)/comments/([0-9]+)$#',$args,$m))
596          {
597               # Post comments feed
598               $type = $m[1];
599               $comments = true;
600               $post_id = (integer) $m[2];
601          }
602          elseif (preg_match('#^(?:category/(.+)/)?(atom|rss2)(/comments)?$#',$args,$m))
603          {
604               # All posts or comments feed
605               $type = $m[2];
606               $comments = !empty($m[3]);
607               if (!empty($m[1])) {
608                    $cat_url = $m[1];
609               }
610          }
611          else
612          {
613               # The specified Feed URL is malformed.
614               self::p404();
615               return;
616          }
617
618          if ($cat_url)
619          {
620               $params = new ArrayObject(array(
621                    'cat_url' => $cat_url,
622                    'post_type' => 'post'));
623
624               $core->callBehavior('publicFeedBeforeGetCategories',$params,$args);
625
626               $_ctx->categories = $core->blog->getCategories($params);
627
628               if ($_ctx->categories->isEmpty()) {
629                    # The specified category does no exist.
630                    self::p404();
631                    return;
632               }
633
634               $subtitle = ' - '.$_ctx->categories->cat_title;
635          }
636          elseif ($post_id)
637          {
638               $params = new ArrayObject(array(
639                    'post_id' => $post_id,
640                    'post_type' => ''));
641
642               $core->callBehavior('publicFeedBeforeGetPosts',$params,$args);
643
644               $_ctx->posts = $core->blog->getPosts($params);
645
646               if ($_ctx->posts->isEmpty()) {
647                    # The specified post does not exist.
648                    self::p404();
649                    return;
650               }
651
652               $subtitle = ' - '.$_ctx->posts->post_title;
653          }
654
655          $tpl = $type;
656          if ($comments) {
657               $tpl .= '-comments';
658               $_ctx->nb_comment_per_page = $core->blog->settings->system->nb_comment_per_feed;
659          } else {
660               $_ctx->nb_entry_per_page = $core->blog->settings->system->nb_post_per_feed;
661               $_ctx->short_feed_items = $core->blog->settings->system->short_feed_items;
662          }
663          $tpl .= '.xml';
664
665          if ($type == 'atom') {
666               $mime = 'application/atom+xml';
667          }
668
669          $_ctx->feed_subtitle = $subtitle;
670
671          header('X-Robots-Tag: '.context::robotsPolicy($core->blog->settings->system->robots_policy,''));
672          self::serveDocument($tpl,$mime);
673          if (!$comments && !$cat_url) {
674               $core->blog->publishScheduledEntries();
675          }
676     }
677
678     public static function trackback($args)
679     {
680          if (!preg_match('/^[0-9]+$/',$args)) {
681               # The specified trackback URL is not an number
682               self::p404();
683          } else {
684               $core =& $GLOBALS['core'];
685               if (!is_array($args)) $args = array();
686               $args['type'] = 'trackback';
687
688               # --BEHAVIOR-- publicBeforeReceiveTrackback
689               $core->callBehavior('publicBeforeReceiveTrackback',$core,$args);
690
691               $tb = new dcTrackback($GLOBALS['core']);
692               $tb->receiveTrackback($args);
693          }
694     }
695
696     public static function webmention($args)
697     {
698          $core =& $GLOBALS['core'];
699          if (!is_array($args)) $args = array();
700          $args['type'] = 'webmention';
701
702          # --BEHAVIOR-- publicBeforeReceiveTrackback
703          $core->callBehavior('publicBeforeReceiveTrackback',$core,$args);
704
705          $tb = new dcTrackback($core);
706          $tb->receiveWebmention();
707     }
708
709     public static function rsd($args)
710     {
711          $core =& $GLOBALS['core'];
712          http::cache($GLOBALS['mod_files'],$GLOBALS['mod_ts']);
713
714          header('Content-Type: text/xml; charset=UTF-8');
715          echo
716          '<?xml version="1.0" encoding="UTF-8"?>'."\n".
717          '<rsd version="1.0" xmlns="http://archipelago.phrasewise.com/rsd">'."\n".
718          "<service>\n".
719          "  <engineName>Dotclear</engineName>\n".
720          "  <engineLink>http://www.dotclear.org/</engineLink>\n".
721          '  <homePageLink>'.html::escapeHTML($core->blog->url)."</homePageLink>\n";
722
723          if ($core->blog->settings->system->enable_xmlrpc)
724          {
725               $u = sprintf(DC_XMLRPC_URL,$core->blog->url,$core->blog->id);
726
727               echo
728               "  <apis>\n".
729               '    <api name="WordPress" blogID="1" preferred="true" apiLink="'.$u.'"/>'."\n".
730               '    <api name="Movable Type" blogID="1" preferred="false" apiLink="'.$u.'"/>'."\n".
731               '    <api name="MetaWeblog" blogID="1" preferred="false" apiLink="'.$u.'"/>'."\n".
732               '    <api name="Blogger" blogID="1" preferred="false" apiLink="'.$u.'"/>'."\n".
733               "  </apis>\n";
734          }
735
736          echo
737          "</service>\n".
738          "</rsd>\n";
739     }
740
741     public static function xmlrpc($args)
742     {
743          $core =& $GLOBALS['core'];
744          $blog_id = preg_replace('#^([^/]*).*#','$1',$args);
745          $server = new dcXmlRpc($core,$blog_id);
746          $server->serve();
747     }
748}
Note: See TracBrowser for help on using the repository browser.

Sites map