Dotclear

source: plugins/iphoneview/_public.php @ 1125

Revision 1125, 12.0 KB checked in by hadrien, 14 years ago (diff)

fix current url variable

Line 
1<?php
2# ***** BEGIN LICENSE BLOCK *****
3# This file is part of iPhoneView.
4# Copyright (c) 2009 Hadrien Lanneau.
5# All rights reserved.
6#
7# Pixearch 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# iPhoneView 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 iPhoneView; if not, write to the Free Software
19# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20#
21# http://www.alti.info/pages/iPhoneView
22#
23# ***** END LICENSE BLOCK *****
24
25$core->addBehavior(
26     'publicPrepend', //publicPrepend
27     array(
28          'iPhoneViewUrls',
29          'detect'
30     )
31);
32
33
34$core->tpl->addValue(
35     'iPhoneThemeURL',
36     array(
37          'iPhoneViewUrls',
38          'iPhoneThemeURL'
39     )
40);
41$core->tpl->addValue(
42     'CurrentURL',
43     array(
44          'iPhoneViewUrls',
45          'CurrentURL'
46     )
47);
48
49$core->tpl->addValue(
50     'BlogiPhoneURL',
51     array(
52          'iPhoneViewUrls',
53          'BlogiPhoneURL'
54     )
55);
56$core->tpl->addValue(
57     'EntryiPhoneURL',
58     array(
59          'iPhoneViewUrls',
60          'EntryiPhoneURL'
61     )
62);
63$core->tpl->addValue(
64     'EntryCategoryiPhoneURL',
65     array(
66          'iPhoneViewUrls',
67          'EntryCategoryiPhoneURL'
68     )
69);
70$core->tpl->addValue(
71     'MetaiPhoneURL',
72     array(
73          'iPhoneViewUrls',
74          'MetaiPhoneURL'
75     )
76);
77$core->tpl->addValue(
78     'CategoryiPhoneURL',
79     array(
80          'iPhoneViewUrls',
81          'CategoryiPhoneURL'
82     )
83);
84
85/**
86* iPhoneView
87*/
88class iPhoneViewUrls extends dcUrlHandlers
89{
90     /**
91      * Detect by user agent or cookie if we have to display normal or iphone view
92      *
93      * @return redirect
94      * @author Hadrien Lanneau (contact at hadrien dot eu)
95      **/
96     public static function detect()
97     {
98          global $core;
99         
100          $iphoneUrl = $core->blog->url . 'iphone';
101         
102          if (preg_match(
103                    '/iPhone|iPod/',
104                    $_SERVER['HTTP_USER_AGENT']
105               ) and
106               $_COOKIE['iphoneview'] != 'no')
107          {
108               if (stripos(
109                         http::getSelfURI(),
110                         $iphoneUrl
111                    ) === false)
112               {
113                    http::redirect(
114                         $iphoneUrl . $_SERVER['REQUEST_URI']
115                    );
116               }
117          }
118     }
119
120     /**
121      * Convert iphone url to standard url
122      *
123      * @return string
124      * @author Hadrien Lanneau (contact at hadrien dot eu)
125      **/
126     public static function convertUrlToStandard()
127     {
128          return $core->blog->normalurl . $_SERVER['REQUEST_URI'];
129     }
130     
131     /**
132      * Get iPhone tpl path
133      *
134      * @return array
135      * @author Hadrien Lanneau (contact at hadrien dot eu)
136      **/
137     public static function getTplPath()
138     {
139          global $core;
140         
141          $pathes = array(
142               dirname(__FILE__) . '/../..' .
143               $core->blog->settings->themes_url . '/' .
144               $core->blog->settings->theme . '/iphone/tpl/'
145          );
146         
147          return $pathes;
148     }
149     
150     /**
151      * Set iPhone cookie on
152      *
153      * @return void
154      * @author Hadrien Lanneau (contact at hadrien dot eu)
155      **/
156     public static function setiPhoneCookieON()
157     {
158          setCookie(
159               'iphoneview', null
160          );
161     }
162     
163     //--------------------------------------------------------------------------
164     // Pages
165     //--------------------------------------------------------------------------
166     
167     /**
168      * iPhone Home page
169      */
170     public static function home($args)
171     {
172          self::setiPhoneCookieON();
173         
174          $n = $args;
175         
176          if ($args && !$n)
177          {
178               self::p404();
179          }
180          else
181          {
182               $core =& $GLOBALS['core'];
183               
184               $core->tpl->setPath(
185                    self::getTplPath(),
186                    dirname(__FILE__) . '/default-templates'
187               );
188               
189               if ($n)
190               {
191                    $GLOBALS['_page_number'] = $n;
192                    $core->url->type = $n > 1 ? 'default-page' : 'default';
193                   
194                    self::serveDocument('_entries.html');
195                    $core->blog->publishScheduledEntries();
196                    exit;
197               }
198               
199               if (empty($_GET['q']))
200               {
201                    self::serveDocument('home.html');
202                    $core->blog->publishScheduledEntries();
203                    exit;
204               }
205               else
206               {
207                    self::search();
208               }
209          }
210     }
211     
212     /**
213      * iPhone Post page
214      */
215     public static function post($args)
216     {
217          self::setiPhoneCookieON();
218         
219          if ($args == '') {
220               self::p404();
221          }
222         
223          $_ctx =& $GLOBALS['_ctx'];
224          $core =& $GLOBALS['core'];
225         
226          $core->blog->withoutPassword(false);
227         
228          $params = new ArrayObject();
229          $params['post_url'] = $args;
230         
231          $_ctx->posts = $core->blog->getPosts($params);
232         
233          $_ctx->comment_preview = new ArrayObject();
234          $_ctx->comment_preview['content'] = '';
235          $_ctx->comment_preview['rawcontent'] = '';
236          $_ctx->comment_preview['name'] = '';
237          $_ctx->comment_preview['mail'] = '';
238          $_ctx->comment_preview['site'] = '';
239          $_ctx->comment_preview['preview'] = false;
240          $_ctx->comment_preview['remember'] = false;
241         
242          $core->blog->withoutPassword(true);
243         
244         
245          if ($_ctx->posts->isEmpty())
246          {
247               # No entry
248               self::p404();
249          }
250         
251          $post_id = $_ctx->posts->post_id;
252          $post_password = $_ctx->posts->post_password;
253         
254          # Password protected entry
255          if ($post_password != '')
256          {
257               http::redirect(
258                    self::convertUrlToStandard()
259               );
260               exit();
261          }
262         
263          $post_comment =
264               isset($_POST['c_name']) && isset($_POST['c_mail']) &&
265               isset($_POST['c_site']) && isset($_POST['c_content']) &&
266               $_ctx->posts->commentsActive();
267         
268          # Posting a comment
269          if ($post_comment)
270          {
271               # Spam trap
272               if (!empty($_POST['f_mail'])) {
273                    http::head(412,'Precondition Failed');
274                    header('Content-Type: text/plain');
275                    echo "So Long, and Thanks For All the Fish";
276                    exit;
277               }
278               
279               $name = $_POST['c_name'];
280               $mail = $_POST['c_mail'];
281               $site = $_POST['c_site'];
282               $content = $_POST['c_content'];
283               $preview = !empty($_POST['preview']);
284               
285               if ($content != '')
286               {
287                    if ($core->blog->settings->wiki_comments) {
288                         $core->initWikiComment();
289                    } else {
290                         $core->initWikiSimpleComment();
291                    }
292                    $content = $core->wikiTransform($content);
293                    $content = $core->HTMLfilter($content);
294               }
295               
296               $_ctx->comment_preview['content'] = $content;
297               $_ctx->comment_preview['rawcontent'] = $_POST['c_content'];
298               $_ctx->comment_preview['name'] = $name;
299               $_ctx->comment_preview['mail'] = $mail;
300               $_ctx->comment_preview['site'] = $site;
301               
302               if ($preview)
303               {
304                    # --BEHAVIOR-- publicBeforeCommentPreview
305                    $core->callBehavior('publicBeforeCommentPreview',$_ctx->comment_preview);
306                   
307                    $_ctx->comment_preview['preview'] = true;
308               }
309               else
310               {
311                    # Post the comment
312                    $cur = $core->con->openCursor($core->prefix.'comment');
313                    $cur->comment_author = $name;
314                    $cur->comment_site = html::clean($site);
315                    $cur->comment_email = html::clean($mail);
316                    $cur->comment_content = $content;
317                    $cur->post_id = $_ctx->posts->post_id;
318                    $cur->comment_status = $core->blog->settings->comments_pub ? 1 : -1;
319                    $cur->comment_ip = http::realIP();
320                   
321                    $redir = $_ctx->posts->getURL();
322                    $redir .= strpos($redir,'?') !== false ? '&' : '?';
323                   
324                    try
325                    {
326                         if (!text::isEmail($cur->comment_email)) {
327                              throw new Exception(__('You must provide a valid email address.'));
328                         }
329                         
330                         # --BEHAVIOR-- publicBeforeCommentCreate
331                         $core->callBehavior('publicBeforeCommentCreate',$cur);
332                         if ($cur->post_id) {                         
333                              $comment_id = $core->blog->addComment($cur);
334                         
335                              # --BEHAVIOR-- publicAfterCommentCreate
336                              $core->callBehavior('publicAfterCommentCreate',$cur,$comment_id);
337                         }
338                         
339                         if ($cur->comment_status == 1) {
340                              $redir_arg = 'pub=1';
341                         } else {
342                              $redir_arg = 'pub=0';
343                         }
344                         
345                         header('Location: '.$redir.$redir_arg);
346                         exit;
347                    }
348                    catch (Exception $e)
349                    {
350                         $_ctx->form_error = $e->getMessage();
351                         $_ctx->form_error;
352                    }
353               }
354          }
355         
356          $core->tpl->setPath(
357               self::getTplPath(),
358               dirname(__FILE__) . '/default-templates'
359          );
360         
361          # The entry
362          self::serveDocument('post.html');
363          exit;
364     }
365     
366     public static function category($args)
367     {
368          self::setiPhoneCookieON();
369         
370          if (preg_match(
371                    '/(\w*)\/page\/(.*?)$/',
372                    $args,
373                    $m
374               ))
375          {
376               $cat = $m[1];
377               $n = intval($m[2]);
378          }
379          else
380          {
381               $cat = $args;
382               $n = null;
383          }
384         
385          $_ctx =& $GLOBALS['_ctx'];
386          $core =& $GLOBALS['core'];
387         
388          $core->tpl->setPath(
389               self::getTplPath(),
390               dirname(__FILE__) . '/default-templates'
391          );
392         
393          if ($args == '' && !$n) {
394               self::p404();
395          }
396         
397          $params['cat_url'] = $cat;
398          $params['post_type'] = 'post';
399         
400          $_ctx->categories = $core->blog->getCategories($params);
401         
402          if ($_ctx->categories->isEmpty()) {
403               self::p404();
404          }
405          elseif ($n)
406          {
407               $GLOBALS['_page_number'] = $n;
408               $core->url->type = $n > 1 ? 'default-page' : 'default';
409               
410               self::serveDocument('_entries.html');
411               $core->blog->publishScheduledEntries();
412               exit;
413          }
414          else
415          {
416               if ($n) {
417                    $GLOBALS['_page_number'] = $n;
418               }
419               
420               $core->tpl->setPath(
421                    self::getTplPath(),
422                    dirname(__FILE__) . '/default-templates'
423               );
424               
425               self::serveDocument('category.html');
426               exit;
427          }
428     }
429     
430     public static function search()
431     {
432          self::setiPhoneCookieON();
433         
434          $_ctx =& $GLOBALS['_ctx'];
435          $core =& $GLOBALS['core'];
436         
437          $GLOBALS['_search'] = !empty($_GET['q']) ? rawurldecode($_GET['q']) : '';
438          if ($GLOBALS['_search']) {
439               $GLOBALS['_search_count'] = $core->blog->getPosts(array('search' => $GLOBALS['_search']),true)->f(0);
440          }
441         
442          $core->tpl->setPath(
443               self::getTplPath(),
444               dirname(__FILE__) . '/default-templates'
445          );
446         
447          self::serveDocument('search.html');
448     }
449     
450     public static function tag($args)
451     {
452          self::setiPhoneCookieON();
453         
454          if (preg_match(
455                    '/(\w*)\/page\/(.*?)$/',
456                    $args,
457                    $m
458               ))
459          {
460               $tag = $m[1];
461               $n = intval($m[2]);
462          }
463          else
464          {
465               $tag = $args;
466               $n = null;
467          }
468         
469          global $core;
470          $core->tpl->setPath(
471               self::getTplPath(),
472               dirname(__FILE__) . '/default-templates'
473          );
474         
475          if ($tag == '' && !$n)
476          {
477               self::p404();
478          }
479          else
480          {
481               $objMeta = new dcMeta($GLOBALS['core']);
482               
483               $GLOBALS['_ctx']->meta = $objMeta->getMeta('tag',null,$tag);
484               
485               if ($GLOBALS['_ctx']->meta->isEmpty()) {
486                    self::p404();
487               }
488               elseif ($n)
489               {
490                    $GLOBALS['_page_number'] = $n;
491                    $core->url->type = $n > 1 ? 'default-page' : 'default';
492                   
493                    self::serveDocument('_entries.html');
494                    $core->blog->publishScheduledEntries();
495                    exit;
496               }
497               else
498               {
499                    self::serveDocument('tag.html');
500               }
501          }
502          exit;
503     }
504     
505     //--------------------------------------------------------------------------
506     // Templates vars
507     //--------------------------------------------------------------------------
508     
509     public function iPhoneThemeURL()
510     {
511          return '<?php
512          if (file_exists(
513                    $core->blog->themes_path . "/" .
514                    $core->blog->settings->theme . "/iphone/"
515               ))
516          {
517               echo $core->blog->settings->themes_url."/".$core->blog->settings->theme . "/iphone/";
518          }
519          else
520          {
521               echo "/plugins/iphoneview/default-templates/";
522          }
523          ?>';
524     }
525     
526     /**
527      * Get current URL
528      *
529      * @return string
530      * @author Hadrien Lanneau (contact at hadrien dot eu)
531      **/
532     public static function CurrentURL($attr)
533     {
534          global $core;
535         
536          return str_replace(
537               $core->blog->url . 'iphone/',
538               $core->blog->url,
539               http::getSelfURI()
540          );
541     }
542     
543     /**
544      *
545      */
546     public function BlogiPhoneURL($attr)
547     {
548          global $core;
549         
550          $f = $core->tpl->getFilters($attr);
551          return '<?php
552          echo $core->blog->url . "iphone/";
553          ?>';
554     }
555     
556     /**
557      *
558      */
559     public function EntryiPhoneURL($attr)
560     {
561          global $core;
562         
563          $f = $core->tpl->getFilters($attr);
564          return '<?php
565          echo str_replace(
566               "/post/",
567               "/iphone/post/",
568               '.sprintf($f,'$_ctx->posts->getURL()').'
569          );
570          ?>';
571     }
572     
573     /**
574      *
575      */
576     public function EntryCategoryiPhoneURL($attr)
577     {
578          global $core;
579         
580          $f = $core->tpl->getFilters($attr);
581          return '<?php
582          echo str_replace(
583               "/category/",
584               "/iphone/category/",
585               '.sprintf($f,'$_ctx->posts->getCategoryURL()').'
586          );
587          ?>';
588     }
589     
590     public function CategoryiPhoneURL($attr)
591     {
592          global $core;
593         
594          $f = $core->tpl->getFilters($attr);
595          return '<?php
596          echo str_replace(
597               "/category/",
598               "/iphone/category/",
599               '.sprintf($f,'$core->blog->url.$core->url->getBase("category")."/".$_ctx->categories->cat_url').'
600          );
601          ?>';
602     }
603     
604     
605     /**
606      *
607      */
608     public function MetaiPhoneURL($attr)
609     {
610          global $core;
611         
612          $f = $core->tpl->getFilters($attr);
613          return '<?php
614          echo str_replace(
615               "/tag/",
616               "/iphone/tag/",
617               '.sprintf($f,'$core->blog->url.$core->url->getBase("tag").'.
618               '"/".rawurlencode($_ctx->meta->meta_id)').'
619          );
620          ?>';
621     }
622}
Note: See TracBrowser for help on using the repository browser.

Sites map