Dotclear

source: plugins/iphoneview/_public.php @ 1102

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

fix iphone them url

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          $pathes = $core->tpl->getPath();
141          foreach ($pathes as $k => $p)
142          {
143               $pathes[$k] = $p . '/iphone';
144          }
145          return $pathes;
146     }
147     
148     /**
149      * Set iPhone cookie on
150      *
151      * @return void
152      * @author Hadrien Lanneau (contact at hadrien dot eu)
153      **/
154     public static function setiPhoneCookieON()
155     {
156          setCookie(
157               'iphoneview', null
158          );
159     }
160     
161     //--------------------------------------------------------------------------
162     // Pages
163     //--------------------------------------------------------------------------
164     
165     /**
166      * iPhone Home page
167      */
168     public static function home($args)
169     {
170          self::setiPhoneCookieON();
171         
172          $n = $args;
173         
174          if ($args && !$n)
175          {
176               self::p404();
177          }
178          else
179          {
180               $core =& $GLOBALS['core'];
181               
182               $core->tpl->setPath(
183                    self::getTplPath(),
184                    dirname(__FILE__) . '/default-templates'
185               );
186               
187               if ($n)
188               {
189                    $GLOBALS['_page_number'] = $n;
190                    $core->url->type = $n > 1 ? 'default-page' : 'default';
191                   
192                    self::serveDocument('_entries.html');
193                    $core->blog->publishScheduledEntries();
194                    exit;
195               }
196               
197               if (empty($_GET['q']))
198               {
199                    self::serveDocument('home.html');
200                    $core->blog->publishScheduledEntries();
201                    exit;
202               }
203               else
204               {
205                    self::search();
206               }
207          }
208     }
209     
210     /**
211      * iPhone Post page
212      */
213     public static function post($args)
214     {
215          self::setiPhoneCookieON();
216         
217          if ($args == '') {
218               self::p404();
219          }
220         
221          $_ctx =& $GLOBALS['_ctx'];
222          $core =& $GLOBALS['core'];
223         
224          $core->blog->withoutPassword(false);
225         
226          $params = new ArrayObject();
227          $params['post_url'] = $args;
228         
229          $_ctx->posts = $core->blog->getPosts($params);
230         
231          $_ctx->comment_preview = new ArrayObject();
232          $_ctx->comment_preview['content'] = '';
233          $_ctx->comment_preview['rawcontent'] = '';
234          $_ctx->comment_preview['name'] = '';
235          $_ctx->comment_preview['mail'] = '';
236          $_ctx->comment_preview['site'] = '';
237          $_ctx->comment_preview['preview'] = false;
238          $_ctx->comment_preview['remember'] = false;
239         
240          $core->blog->withoutPassword(true);
241         
242         
243          if ($_ctx->posts->isEmpty())
244          {
245               # No entry
246               self::p404();
247          }
248         
249          $post_id = $_ctx->posts->post_id;
250          $post_password = $_ctx->posts->post_password;
251         
252          # Password protected entry
253          if ($post_password != '')
254          {
255               http::redirect(
256                    self::convertUrlToStandard()
257               );
258               exit();
259          }
260         
261          $post_comment =
262               isset($_POST['c_name']) && isset($_POST['c_mail']) &&
263               isset($_POST['c_site']) && isset($_POST['c_content']) &&
264               $_ctx->posts->commentsActive();
265         
266          # Posting a comment
267          if ($post_comment)
268          {
269               # Spam trap
270               if (!empty($_POST['f_mail'])) {
271                    http::head(412,'Precondition Failed');
272                    header('Content-Type: text/plain');
273                    echo "So Long, and Thanks For All the Fish";
274                    exit;
275               }
276               
277               $name = $_POST['c_name'];
278               $mail = $_POST['c_mail'];
279               $site = $_POST['c_site'];
280               $content = $_POST['c_content'];
281               $preview = !empty($_POST['preview']);
282               
283               if ($content != '')
284               {
285                    if ($core->blog->settings->wiki_comments) {
286                         $core->initWikiComment();
287                    } else {
288                         $core->initWikiSimpleComment();
289                    }
290                    $content = $core->wikiTransform($content);
291                    $content = $core->HTMLfilter($content);
292               }
293               
294               $_ctx->comment_preview['content'] = $content;
295               $_ctx->comment_preview['rawcontent'] = $_POST['c_content'];
296               $_ctx->comment_preview['name'] = $name;
297               $_ctx->comment_preview['mail'] = $mail;
298               $_ctx->comment_preview['site'] = $site;
299               
300               if ($preview)
301               {
302                    # --BEHAVIOR-- publicBeforeCommentPreview
303                    $core->callBehavior('publicBeforeCommentPreview',$_ctx->comment_preview);
304                   
305                    $_ctx->comment_preview['preview'] = true;
306               }
307               else
308               {
309                    # Post the comment
310                    $cur = $core->con->openCursor($core->prefix.'comment');
311                    $cur->comment_author = $name;
312                    $cur->comment_site = html::clean($site);
313                    $cur->comment_email = html::clean($mail);
314                    $cur->comment_content = $content;
315                    $cur->post_id = $_ctx->posts->post_id;
316                    $cur->comment_status = $core->blog->settings->comments_pub ? 1 : -1;
317                    $cur->comment_ip = http::realIP();
318                   
319                    $redir = $_ctx->posts->getURL();
320                    $redir .= strpos($redir,'?') !== false ? '&' : '?';
321                   
322                    try
323                    {
324                         if (!text::isEmail($cur->comment_email)) {
325                              throw new Exception(__('You must provide a valid email address.'));
326                         }
327                         
328                         # --BEHAVIOR-- publicBeforeCommentCreate
329                         $core->callBehavior('publicBeforeCommentCreate',$cur);
330                         if ($cur->post_id) {                         
331                              $comment_id = $core->blog->addComment($cur);
332                         
333                              # --BEHAVIOR-- publicAfterCommentCreate
334                              $core->callBehavior('publicAfterCommentCreate',$cur,$comment_id);
335                         }
336                         
337                         if ($cur->comment_status == 1) {
338                              $redir_arg = 'pub=1';
339                         } else {
340                              $redir_arg = 'pub=0';
341                         }
342                         
343                         header('Location: '.$redir.$redir_arg);
344                         exit;
345                    }
346                    catch (Exception $e)
347                    {
348                         $_ctx->form_error = $e->getMessage();
349                         $_ctx->form_error;
350                    }
351               }
352          }
353         
354          $core->tpl->setPath(
355               self::getTplPath(),
356               dirname(__FILE__) . '/default-templates'
357          );
358         
359          # The entry
360          self::serveDocument('post.html');
361          exit;
362     }
363     
364     public static function category($args)
365     {
366          self::setiPhoneCookieON();
367         
368          if (preg_match(
369                    '/(\w*)\/page\/(.*?)$/',
370                    $args,
371                    $m
372               ))
373          {
374               $cat = $m[1];
375               $n = intval($m[2]);
376          }
377          else
378          {
379               $cat = $args;
380               $n = null;
381          }
382         
383          $_ctx =& $GLOBALS['_ctx'];
384          $core =& $GLOBALS['core'];
385         
386          $core->tpl->setPath(
387               self::getTplPath(),
388               dirname(__FILE__) . '/default-templates'
389          );
390         
391          if ($args == '' && !$n) {
392               self::p404();
393          }
394         
395          $params['cat_url'] = $cat;
396          $params['post_type'] = 'post';
397         
398          $_ctx->categories = $core->blog->getCategories($params);
399         
400          if ($_ctx->categories->isEmpty()) {
401               self::p404();
402          }
403          elseif ($n)
404          {
405               $GLOBALS['_page_number'] = $n;
406               $core->url->type = $n > 1 ? 'default-page' : 'default';
407               
408               self::serveDocument('_entries.html');
409               $core->blog->publishScheduledEntries();
410               exit;
411          }
412          else
413          {
414               if ($n) {
415                    $GLOBALS['_page_number'] = $n;
416               }
417               
418               $core->tpl->setPath(
419                    self::getTplPath(),
420                    dirname(__FILE__) . '/default-templates'
421               );
422               
423               self::serveDocument('category.html');
424               exit;
425          }
426     }
427     
428     public static function search()
429     {
430          self::setiPhoneCookieON();
431         
432          $_ctx =& $GLOBALS['_ctx'];
433          $core =& $GLOBALS['core'];
434         
435          $GLOBALS['_search'] = !empty($_GET['q']) ? rawurldecode($_GET['q']) : '';
436          if ($GLOBALS['_search']) {
437               $GLOBALS['_search_count'] = $core->blog->getPosts(array('search' => $GLOBALS['_search']),true)->f(0);
438          }
439         
440          $core->tpl->setPath(
441               self::getTplPath(),
442               dirname(__FILE__) . '/default-templates'
443          );
444         
445          self::serveDocument('search.html');
446     }
447     
448     public static function tag($args)
449     {
450          self::setiPhoneCookieON();
451         
452          if (preg_match(
453                    '/(\w*)\/page\/(.*?)$/',
454                    $args,
455                    $m
456               ))
457          {
458               $tag = $m[1];
459               $n = intval($m[2]);
460          }
461          else
462          {
463               $tag = $args;
464               $n = null;
465          }
466         
467          global $core;
468          $core->tpl->setPath(
469               self::getTplPath(),
470               dirname(__FILE__) . '/default-templates'
471          );
472         
473          if ($tag == '' && !$n)
474          {
475               self::p404();
476          }
477          else
478          {
479               $objMeta = new dcMeta($GLOBALS['core']);
480               
481               $GLOBALS['_ctx']->meta = $objMeta->getMeta('tag',null,$tag);
482               
483               if ($GLOBALS['_ctx']->meta->isEmpty()) {
484                    self::p404();
485               }
486               elseif ($n)
487               {
488                    $GLOBALS['_page_number'] = $n;
489                    $core->url->type = $n > 1 ? 'default-page' : 'default';
490                   
491                    self::serveDocument('_entries.html');
492                    $core->blog->publishScheduledEntries();
493                    exit;
494               }
495               else
496               {
497                    self::serveDocument('tag.html');
498               }
499          }
500          exit;
501     }
502     
503     //--------------------------------------------------------------------------
504     // Templates vars
505     //--------------------------------------------------------------------------
506     
507     public function iPhoneThemeURL()
508     {
509          return '<?php
510          if (file_exists(
511                    $core->blog->themes_path . "/" .
512                    $core->blog->settings->theme . "/iphone/"
513               ))
514          {
515               echo $core->blog->settings->themes_url."/".$core->blog->settings->theme . "/iphone/";
516          }
517          else
518          {
519               echo "/plugins/iphoneview/default-templates/";
520          }
521          ?>';
522     }
523     
524     /**
525      * Get current URL
526      *
527      * @return string
528      * @author Hadrien Lanneau (contact at hadrien dot eu)
529      **/
530     public static function CurrentURL($attr)
531     {
532          global $core;
533         
534          return str_replace(
535               $core->blog->url,
536               $core->blog->normalurl,
537               http::getSelfURI()
538          );
539     }
540     
541     /**
542      *
543      */
544     public function BlogiPhoneURL($attr)
545     {
546          global $core;
547         
548          $f = $core->tpl->getFilters($attr);
549          return '<?php
550          echo $core->blog->url . "iphone/";
551          ?>';
552     }
553     
554     /**
555      *
556      */
557     public function EntryiPhoneURL($attr)
558     {
559          global $core;
560         
561          $f = $core->tpl->getFilters($attr);
562          return '<?php
563          echo str_replace(
564               "/post/",
565               "/iphone/post/",
566               '.sprintf($f,'$_ctx->posts->getURL()').'
567          );
568          ?>';
569     }
570     
571     /**
572      *
573      */
574     public function EntryCategoryiPhoneURL($attr)
575     {
576          global $core;
577         
578          $f = $core->tpl->getFilters($attr);
579          return '<?php
580          echo str_replace(
581               "/category/",
582               "/iphone/category/",
583               '.sprintf($f,'$_ctx->posts->getCategoryURL()').'
584          );
585          ?>';
586     }
587     
588     public function CategoryiPhoneURL($attr)
589     {
590          global $core;
591         
592          $f = $core->tpl->getFilters($attr);
593          return '<?php
594          echo str_replace(
595               "/category/",
596               "/iphone/category/",
597               '.sprintf($f,'$core->blog->url.$core->url->getBase("category")."/".$_ctx->categories->cat_url').'
598          );
599          ?>';
600     }
601     
602     
603     /**
604      *
605      */
606     public function MetaiPhoneURL($attr)
607     {
608          global $core;
609         
610          $f = $core->tpl->getFilters($attr);
611          return '<?php
612          echo str_replace(
613               "/tag/",
614               "/iphone/tag/",
615               '.sprintf($f,'$core->blog->url.$core->url->getBase("tag").'.
616               '"/".rawurlencode($_ctx->meta->meta_id)').'
617          );
618          ?>';
619     }
620}
Note: See TracBrowser for help on using the repository browser.

Sites map