[ Index ]
 

Code source de Dotclear 2.0-beta6

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/inc/public/ -> lib.urlhandlers.php (source)

   1  <?php
   2  # ***** BEGIN LICENSE BLOCK *****
   3  # This file is part of DotClear.
   4  # Copyright (c) 2005 Olivier Meunier. All rights
   5  # reserved.
   6  #
   7  # DotClear is free software; you can redistribute it and/or modify
   8  # it under the terms of the GNU General Public License as published by
   9  # the Free Software Foundation; either version 2 of the License, or
  10  # (at your option) any later version.
  11  # 
  12  # DotClear is distributed in the hope that it will be useful,
  13  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15  # GNU General Public License for more details.
  16  # 
  17  # You should have received a copy of the GNU General Public License
  18  # along with DotClear; if not, write to the Free Software
  19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20  #
  21  # ***** END LICENSE BLOCK *****
  22  
  23  class dcUrlHandlers
  24  {
  25  	protected static function p404()
  26      {
  27          header('Content-Type: text/html; charset=UTF-8');
  28          http::head(404,'Not Found');
  29          $GLOBALS['core']->url->type = '404';
  30          echo $GLOBALS['core']->tpl->getData('404.html');
  31          exit;
  32      }
  33      
  34  	protected static function getPageNumber(&$args)
  35      {
  36          if (preg_match('#(^|/)page/([0-9]+)$#',$args,$m)) {
  37              $n = (integer) $m[2];
  38              if ($n > 0) {
  39                  $args = preg_replace('#(^|/)page/([0-9]+)$#','',$args);
  40                  return $n;
  41              }
  42          }
  43          
  44          return false;
  45      }
  46      
  47  	protected static function serveDocument($tpl,$content_type='text/html',$http_cache=true,$http_etag=true)
  48      {
  49          $tpl_file = $GLOBALS['core']->tpl->getFilePath($tpl);
  50          
  51          if (!$tpl_file) {
  52              header('Content-Type: text/plain; charset=UTF-8');
  53              echo 'Unable to find template';
  54              exit;
  55          }
  56          
  57          if ($http_cache) {
  58              $GLOBALS['mod_files'][] = $tpl_file;
  59              http::cache($GLOBALS['mod_files'],$GLOBALS['mod_ts']);
  60          }
  61          
  62          header('Content-Type: '.$content_type.'; charset=UTF-8');
  63          $out = $GLOBALS['core']->tpl->getData($tpl);
  64          
  65          if ($http_cache && $http_etag) {
  66              http::etag($out,http::getSelfURI());
  67          }
  68          echo $out;
  69      }
  70      
  71  	public static function home($args)
  72      {
  73          $n = self::getPageNumber($args);
  74          
  75          if ($args && !$n)
  76          {
  77              self::p404();
  78          }
  79          else
  80          {
  81              if ($n) {
  82                  $GLOBALS['_page_number'] = $n;
  83                  $GLOBALS['core']->url->type = $n > 1 ? 'defaut-page' : 'default';
  84              }
  85              
  86              if (empty($_GET['q'])) {
  87                  self::serveDocument('home.html');
  88                  $GLOBALS['core']->blog->publishScheduledEntries();
  89                  exit;
  90              } else {
  91                  self::search();
  92              }
  93          }
  94      }
  95      
  96  	public static function search()
  97      {
  98          $GLOBALS['_search'] = !empty($_GET['q']) ? rawurldecode($_GET['q']) : '';
  99          self::serveDocument('search.html');
 100      }
 101      
 102  	public static function lang($args)
 103      {
 104          $n = self::getPageNumber($args);
 105          
 106          $params['lang'] = $args;
 107          $GLOBALS['_ctx']->langs = $GLOBALS['core']->blog->getLangs($params);
 108          
 109          if ($GLOBALS['_ctx']->langs->isEmpty()) {
 110              self::p404();
 111          } else {
 112              if ($n) {
 113                  $GLOBALS['_page_number'] = $n;
 114              }
 115              $GLOBALS['_ctx']->cur_lang = $args;
 116              self::home(null);
 117          }
 118      }
 119      
 120  	public static function category($args)
 121      {
 122          $n = self::getPageNumber($args);
 123          
 124          if ($args == '' && !$n) {
 125              self::p404();
 126          }
 127          
 128          $params['cat_url'] = $args;
 129          
 130          $GLOBALS['_ctx']->categories = $GLOBALS['core']->blog->getCategories($params);
 131          
 132          if ($GLOBALS['_ctx']->categories->isEmpty()) {
 133              self::p404();
 134          } else {
 135              if ($n) {
 136                  $GLOBALS['_page_number'] = $n;
 137              }
 138              self::serveDocument('category.html');
 139              exit;
 140          }
 141      }
 142      
 143  	public static function archive($args)
 144      {
 145          $year = $month = $cat_url = null;
 146          # Nothing or year and month
 147          if ($args == '')
 148          {
 149              self::serveDocument('archive.html');
 150              exit;
 151          }
 152          elseif (preg_match('|^/([0-9]{4})/([0-9]{2})$|',$args,$m))
 153          {
 154              $params['year'] = $m[1];
 155              $params['month'] = $m[2];
 156              $params['type'] = 'month';
 157              $GLOBALS['_ctx']->archives = $GLOBALS['core']->blog->getDates($params);
 158              
 159              if ($GLOBALS['_ctx']->archives->isEmpty()) {
 160                  self::p404();
 161              }
 162              
 163              self::serveDocument('archive_month.html');
 164              exit;
 165          }
 166          
 167          self::p404();
 168      }
 169      
 170  	public static function post($args)
 171      {
 172          if ($args == '') {
 173              self::p404();
 174          }
 175          
 176          $GLOBALS['core']->blog->withoutPassword(false);
 177          
 178          $params = new ArrayObject();
 179          $params['post_url'] = $args;
 180          
 181          $GLOBALS['_ctx']->posts = $GLOBALS['core']->blog->getPosts($params);
 182          
 183          $GLOBALS['_ctx']->comment_preview = new ArrayObject();
 184          $GLOBALS['_ctx']->comment_preview['content'] = '';
 185          $GLOBALS['_ctx']->comment_preview['rawcontent'] = '';
 186          $GLOBALS['_ctx']->comment_preview['name'] = '';
 187          $GLOBALS['_ctx']->comment_preview['mail'] = '';
 188          $GLOBALS['_ctx']->comment_preview['site'] = '';
 189          $GLOBALS['_ctx']->comment_preview['preview'] = false;
 190          $GLOBALS['_ctx']->comment_preview['remember'] = false;
 191          
 192          $GLOBALS['core']->blog->withoutPassword(true);
 193          
 194          
 195          if ($GLOBALS['_ctx']->posts->isEmpty())
 196          {
 197              # No entry
 198              self::p404();
 199          }
 200          
 201          $post_id = $GLOBALS['_ctx']->posts->post_id;
 202          $post_password = $GLOBALS['_ctx']->posts->post_password;
 203          
 204          # Getting commenter informations from cookie
 205          if (!empty($_COOKIE['comment_info'])) {
 206              $c_cookie = unserialize($_COOKIE['comment_info']);
 207              foreach ($c_cookie as $k => $v) {
 208                  $GLOBALS['_ctx']->comment_preview[$k] = $v;
 209              }
 210              $GLOBALS['_ctx']->comment_preview['remember'] = true;
 211          }
 212          
 213          # Password protected entry
 214          if ($post_password != '')
 215          {
 216              # Get passwords cookie
 217              if (isset($_COOKIE['dc_passwd'])) {
 218                  $pwd_cookie = unserialize($_COOKIE['dc_passwd']);
 219              } else {
 220                  $pwd_cookie = array();
 221              }
 222              
 223              # Check for match
 224              if ((!empty($_POST['password']) && $_POST['password'] == $post_password)
 225              || (isset($pwd_cookie[$post_id]) && $pwd_cookie[$post_id] == $post_password))
 226              {
 227                  $pwd_cookie[$post_id] = $post_password;
 228                  setcookie('dc_passwd',serialize($pwd_cookie),0,'/');
 229              }
 230              else
 231              {
 232                  self::serveDocument('password-form.html','text/html',false);
 233                  exit;
 234              }
 235          }
 236          
 237          $post_comment =
 238              isset($_POST['c_name']) && isset($_POST['c_mail']) &&
 239              isset($_POST['c_site']) && isset($_POST['c_content']) &&
 240              $GLOBALS['_ctx']->posts->commentsActive();
 241          
 242          # Posting a comment
 243          if ($post_comment)
 244          {
 245              # Spam trap
 246              if (!empty($_POST['f_mail'])) {
 247                  http::head(412,'Precondition Failed');
 248                  header('Content-Type: text/plain');
 249                  echo "So Long, and Thanks For All the Fish";
 250                  exit;
 251              }
 252              
 253              $name = $_POST['c_name'];
 254              $mail = $_POST['c_mail'];
 255              $site = $_POST['c_site'];
 256              $content = $_POST['c_content'];
 257              $preview = !empty($_POST['preview']);
 258              
 259              # Storing commenter informations in cookie
 260              if (!empty($_POST['c_remember'])) {
 261                  $c_cookie = array('name' => $name,'mail' => $mail,
 262                  'site' => $site);
 263                  
 264                  $c_cookie = serialize($c_cookie);
 265                  setcookie('comment_info',$c_cookie,strtotime('+3 month'),'/');
 266              }
 267              
 268              if ($content != '')
 269              {
 270                  if ($GLOBALS['core']->blog->settings->wiki_comments) {
 271                      $GLOBALS['core']->initWikiComment();
 272                  } else {
 273                      $GLOBALS['core']->initWikiSimpleComment();
 274                  }
 275                  $content = $GLOBALS['core']->wikiTransform($content);
 276                  $content = $GLOBALS['core']->HTMLfilter($content);
 277              }
 278              
 279              $GLOBALS['_ctx']->comment_preview['content'] = $content;
 280              $GLOBALS['_ctx']->comment_preview['rawcontent'] = $_POST['c_content'];
 281              $GLOBALS['_ctx']->comment_preview['name'] = $name;
 282              $GLOBALS['_ctx']->comment_preview['mail'] = $mail;
 283              $GLOBALS['_ctx']->comment_preview['site'] = $site;
 284              
 285              if ($preview)
 286              {
 287                  $GLOBALS['_ctx']->comment_preview['preview'] = true;
 288              }
 289              else
 290              {
 291                  # Post the comment
 292                  $cur = $GLOBALS['core']->con->openCursor($GLOBALS['core']->prefix.'comment');
 293                  $cur->comment_author = $name;
 294                  $cur->comment_site = html::clean($site);
 295                  $cur->comment_email = html::clean($mail);
 296                  $cur->comment_content = $content;
 297                  $cur->post_id = $GLOBALS['_ctx']->posts->post_id;
 298                  $cur->comment_status = $GLOBALS['core']->blog->settings->comments_pub ? 1 : -1;
 299                  $cur->comment_ip = http::realIP();
 300                  
 301                  $redir = $GLOBALS['_ctx']->posts->getURL();
 302                  $redir .= strpos($redir,'?') !== false ? '&' : '?';
 303                  
 304                  try
 305                  {
 306                      if (!text::isEmail($cur->comment_email)) {
 307                          throw new Exception(__('You must provide a valid email adress.'));
 308                      }
 309                      
 310                      # --BEHAVIOR-- publicBeforeCommentCreate
 311                      $GLOBALS['core']->callBehavior('publicBeforeCommentCreate',$cur);
 312                      
 313                      $comment_id = $GLOBALS['core']->blog->addComment($cur);
 314                      
 315                      # --BEHAVIOR-- publicAfterCommentCreate
 316                      $GLOBALS['core']->callBehavior('publicAfterCommentCreate',$cur,$comment_id);
 317                      
 318                      if ($cur->comment_status == 1) {
 319                          $redir_arg = 'pub=1';
 320                      } else {
 321                          $redir_arg = 'pub=0';
 322                      }
 323                      
 324                      header('Location: '.$redir.$redir_arg);
 325                      exit;
 326                  }
 327                  catch (Exception $e)
 328                  {
 329                      $GLOBALS['_ctx']->form_error = $e->getMessage();
 330                      $GLOBALS['_ctx']->form_error;
 331                  }
 332              }
 333          }
 334          
 335          # The entry
 336          self::serveDocument('post.html');
 337          exit;
 338      }
 339      
 340  	public static function feed($args)
 341      {
 342          $type = null;
 343          $comments = false;
 344          $cat_url = false;
 345          $post_id = null;
 346          $params = array();
 347          $subtitle = '';
 348          
 349          $mime = 'application/xml';
 350          
 351          if (preg_match('!^([a-z]{2}(-[a-z]{2})?)/(.*)$!',$args,$m)) {
 352              $params['lang'] = $m[1];
 353              $args = $m[3];
 354  
 355              $GLOBALS['_ctx']->langs = $GLOBALS['core']->blog->getLangs($params);
 356          
 357              if ($GLOBALS['_ctx']->langs->isEmpty()) {
 358                  self::p404();
 359              } else {
 360                  $GLOBALS['_ctx']->cur_lang = $m[1];
 361              }
 362          }
 363  
 364          if (preg_match('#^rss2/xslt$#',$args,$m))
 365          {
 366              # RSS XSLT stylesheet
 367              self::serveDocument('rss2.xsl','text/xml');
 368              exit;
 369          }
 370          elseif (preg_match('#^(atom|rss2)/comments/([0-9]+)$#',$args,$m))
 371          {
 372              # Post comments feed
 373              $type = $m[1];
 374              $comments = true;
 375              $post_id = (integer) $m[2];
 376          }
 377          elseif (preg_match('#^(?:category/(.+)/)?(atom|rss2)(/comments)?$#',$args,$m))
 378          {
 379              # All posts or comments feed
 380              $type = $m[2];
 381              $comments = !empty($m[3]);
 382              if (!empty($m[1])) {
 383                  $cat_url = $m[1];
 384              }
 385          }
 386          else
 387          {
 388              self::p404();
 389          }
 390          
 391          if ($cat_url)
 392          {
 393              $params['cat_url'] = $cat_url;
 394              $GLOBALS['_ctx']->categories = $GLOBALS['core']->blog->getCategories($params);
 395              
 396              if ($GLOBALS['_ctx']->categories->isEmpty()) {
 397                  self::p404();
 398              }
 399              
 400              $subtitle = ' - '.$GLOBALS['_ctx']->categories->cat_title;
 401          }
 402          elseif ($post_id)
 403          {
 404              $params['post_id'] = $post_id;
 405              $GLOBALS['_ctx']->posts = $GLOBALS['core']->blog->getPosts($params);
 406              
 407              if ($GLOBALS['_ctx']->posts->isEmpty()) {
 408                  self::p404();
 409              }
 410              
 411              $subtitle = ' - '.$GLOBALS['_ctx']->posts->post_title;
 412          }
 413          
 414          $tpl = $type;
 415          if ($comments) {
 416              $tpl .= '-comments';
 417          }
 418          $tpl .= '.xml';
 419          
 420          if ($type == 'atom') {
 421              $mime = 'application/atom+xml';
 422          }
 423          
 424          $GLOBALS['_ctx']->feed_subtitle = $subtitle;
 425          
 426          self::serveDocument($tpl,$mime);
 427          if (!$comments && !$cat_url) {
 428              $GLOBALS['core']->blog->publishScheduledEntries();
 429          }
 430          exit;
 431      }
 432      
 433  	public static function trackback($args)
 434      {
 435          if (!preg_match('/^[0-9]+$/',$args)) {
 436              self::p404();
 437          } else {
 438              $tb = new dcTrackback($GLOBALS['core']);
 439              $tb->receive($args);
 440              exit;
 441          }
 442      }
 443      
 444  	public static function rsd($args)
 445      {
 446          global $core;
 447          http::cache($GLOBALS['mod_files'],$GLOBALS['mod_ts']);
 448          
 449          header('Content-Type: text/xml; charset=UTF-8');
 450          echo
 451          '<?xml version="1.0" encoding="UTF-8"?>'."\n".
 452          '<rsd version="1.0" xmlns="http://archipelago.phrasewise.com/rsd">'."\n".
 453          "<service>\n".
 454          "  <engineName>DotClear</engineName>\n".
 455          "  <engineLink>http://www.dotclear.net</engineLink>\n".
 456          '  <homePageLink>'.html::escapeHTML($core->blog->url)."</homePageLink>\n";
 457          
 458          if (DC_ADMIN_URL && $core->blog->settings->enable_xmlrpc)
 459          {
 460              $u = DC_ADMIN_URL.'/xmlrpc.php?b='.rawurlencode($core->blog->id);
 461              
 462              echo
 463              "  <apis>\n".
 464              '    <api name="Movable Type" blogID="1" preferred="true" apiLink="'.$u.'"/>'."\n".
 465              '    <api name="MetaWeblog" blogID="1" preferred="false" apiLink="'.$u.'"/>'."\n".
 466              '    <api name="Blogger" blogID="1" preferred="false" apiLink="'.$u.'"/>'."\n".
 467              "  </apis>\n";
 468          }
 469          
 470          echo
 471          "</service>\n".
 472          "</rsd>\n";
 473          exit;
 474      }
 475  }
 476  ?>


Généré le : Fri Feb 23 22:16:06 2007 par Balluche grâce à PHPXref 0.7