Dotclear

source: plugins/subscribeToComments/_public.php @ 2198

Revision 2198, 15.4 KB checked in by Moe, 14 years ago (diff)

Subscribe to comments 1.3.1 :

  • updated template file
  • force type of integers in SQL queries, I don't remember why
Line 
1<?php 
2# ***** BEGIN LICENSE BLOCK *****
3#
4# This file is part of Subscribe to comments.
5# Copyright 2008,2009 Moe (http://gniark.net/)
6#
7# Subscribe to comments 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 3 of the License, or
10# (at your option) any later version.
11#
12# Subscribe to comments 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 this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20# Icon (icon.png) is from Silk Icons : http://www.famfamfam.com/lab/icons/silk/
21#
22# Inspired by http://txfx.net/code/wordpress/subscribe-to-comments/
23#
24# ***** END LICENSE BLOCK *****
25
26if (!defined('DC_RC_PATH')) {return;}
27
28l10n::set(dirname(__FILE__).'/locales/'.$_lang.'/public');
29
30$core->tpl->addBlock('SubscribeToCommentsIsActive',
31     'subscribeToCommentsIsActive');
32
33function subscribeToCommentsIsActive($attr,$content)
34{
35     return '<?php if ($core->blog->settings->subscribetocomments_active) : ?>'.
36          $content.
37          '<?php endif; ?>';
38}
39
40/**
41@ingroup Subscribe to comments
42@brief Document
43*/
44class subscribeToCommentsDocument extends dcUrlHandlers
45{
46     /**
47     serve the document
48     */
49     public static function page($args)
50     {
51          global $core;
52
53          if (!$core->blog->settings->subscribetocomments_active)
54          {
55               self::p404();
56               return;
57          }
58         
59          $session_id = session_id();
60          if (empty($session_id)) {session_start();}
61         
62          $_ctx =& $GLOBALS['_ctx'];
63         
64          $_ctx->subscribeToComments = new ArrayObject();
65          $_ctx->subscribeToComments->email = '';
66          $_ctx->subscribeToComments->checkCookie = false;
67          $_ctx->subscribeToComments->blocked = false;
68         
69          try {
70               subscribeToComments::cleanKeys();
71
72               if (((isset($_GET['post_id']))) && (!is_numeric($_GET['post_id'])))
73               {
74                    throw new Exception(__('Invalid post ID.'));
75               }
76
77               if (isset($_POST['logout'])) {
78                    subscriber::checkNonce();
79                    subscriber::logout();
80                    subscribeToComments::redirect('loggedout');
81               }
82               # login with key
83               elseif ((isset($_GET['email'])) AND (isset($_GET['key'])))
84               {
85                    subscribeToComments::checkEmail($_GET['email']);
86                    subscribeToComments::checkKey($_GET['key']);
87                    subscriber::loginKey($_GET['email'],$_GET['key']);
88                    subscribeToComments::redirect('loggedin');
89               }
90               # subscribe
91               elseif ((isset($_POST['subscribe'])) AND (isset($_POST['post_id'])))
92               {
93                    subscriber::checkNonce();
94                    if (isset($_POST['email']))
95                    {
96                         subscribeToComments::checkEmail($_POST['email']);
97                         $email = $_POST['email'];
98                    }
99                    elseif (subscriber::checkCookie())
100                    {
101                         $email = subscriber::getCookie('email');
102                    }
103                    if (!empty($email))
104                    {
105                         $subscriber = new subscriber($email);
106                         $subscriber->subscribe($_POST['post_id']);
107                         subscribeToComments::redirect('subscribed');
108                    }
109               }
110               # request account informations
111               elseif ((isset($_POST['resend'])) AND (isset($_POST['email'])))
112               {
113                    subscriber::checkNonce();
114                    subscribeToComments::checkEmail($_POST['email']);
115                    subscriber::resendInformations($_POST['email']);
116                    subscribeToComments::redirect('informationsresent');
117               }
118               # update the email address
119               elseif ((isset($_GET['new_email'])) AND (isset($_GET['temp_key'])))
120               {
121                    subscribeToComments::checkEmail($_GET['new_email']);
122                    subscribeToComments::checkKey($_GET['temp_key']);
123                    subscriber::updateEmail($_GET['new_email'],$_GET['temp_key']);
124                    subscribeToComments::redirect('updatedemail');
125               }
126               
127               # email address
128               $_ctx->subscribeToComments->email = '';
129               if (isset($_COOKIE['comment_info']))
130               {
131                    $email = explode("\n",$_COOKIE['comment_info']);
132                    $_ctx->subscribeToComments->email = $email['1'];
133                    unset($email);
134               }
135
136               # subscriber is logged in
137               $_ctx->subscribeToComments->checkCookie = subscriber::checkCookie();
138               if ($_ctx->subscribeToComments->checkCookie)
139               {
140                    $subscriber = new subscriber(subscriber::getCookie('email'));
141                    $_ctx->subscribeToComments->email = $subscriber->email;
142     
143                    if ((isset($_POST['requestChangeEmail'])) AND (isset($_POST['new_email'])))
144                    {
145                         subscriber::checkNonce();
146                         subscribeToComments::checkEmail($_POST['new_email']);
147                         $subscriber->requestUpdateEmail($_POST['new_email']);
148                         subscribeToComments::redirect('requestsent');     
149                    }
150                    elseif ((isset($_POST['remove'])) AND (isset($_POST['entries'])))
151                    {
152                         subscriber::checkNonce();
153                         $subscriber->removeSubscription($_POST['entries']);
154                         subscribeToComments::redirect('removedsubscriptions');
155                    }
156                    elseif (isset($_POST['deleteAccount'])) {
157                         subscriber::checkNonce();
158                         $subscriber->deleteAccount();
159                         subscribeToComments::redirect('accountdeleted');
160                    }
161                    elseif (isset($_POST['blockEmails'])) {
162                         subscriber::checkNonce();
163                         $subscriber->blockEmails(true);
164                         subscribeToComments::redirect('emailsblocked');
165                    }
166                    elseif (isset($_POST['allowEmails'])) {
167                         subscriber::checkNonce();
168                         $subscriber->blockEmails(false);
169                         subscribeToComments::redirect('emailsallowed');
170                    }
171               }
172          }
173          catch (Exception $e)
174          {
175               $_ctx->form_error = $e->getMessage();
176          }
177         
178          $_ctx->subscribeToComments->blocked = subscriber::blocked();
179         
180          # message
181          # inspired by contactMe/_public.php
182          switch($args)
183          {
184               case 'informationsresent' :
185                    $msg = __('Account informations sent');
186                    break;
187               case 'removedsubscriptions' :
188                    $msg = __('Subscriptions removed');
189                    break;
190               case 'loggedout' :
191                    $msg = __('Logged out');
192                    break;
193               case 'loggedin' :
194                    $msg = __('Logged in');
195                    break;
196               case 'emailsblocked' :
197                    $msg = __('Emails blocked');
198                    break;
199               case 'emailsallowed' :
200                    $msg = __('Emails allowed');
201                    break;
202               case 'requestsent' :
203                    $msg = __('An email has been sent to the new email address');
204                    break;
205               case 'updatedemail' :
206                    $msg = __('Email address changed');
207                    break;
208               case 'accountdeleted' :
209                    $msg = __('Account deleted');
210                    break;
211               case 'subscribed' :
212                    $msg = __('Subscribed to the entry');
213                    break;
214                default :
215                    $msg = null;
216                    break;
217          }
218         
219          $_ctx->subscribeToComments->message = $msg;
220          # /message
221         
222          $core->tpl->setPath($core->tpl->getPath(),
223               dirname(__FILE__).'/default-templates/');
224         
225          self::serveDocument('subscribetocomments.html','text/html',false,false);
226     }
227}
228
229/**
230@ingroup Subscribe to comments
231@brief Template
232*/
233class subscribeToCommentsTpl
234{
235     /**
236     check the box on post.html if a cookie is present
237     @return   <b>string</b> PHP block
238     */
239     public static function formChecked()
240     {
241          return("<?php ".
242          "if (isset(\$_POST['subscribeToComments'])) {echo(' checked=\"checked\" ');}".
243          "elseif (isset(\$_COOKIE['subscribetocomments']))".
244          "{echo(' checked=\"checked\" ');}".
245          " ?>");
246     }
247
248     /**
249     get link from post.html to subscriptions page
250     @return   <b>string</b> text and PHP block
251     */
252     public static function formLink()
253     {
254          global $core;
255
256          if ($core->blog->settings->subscribetocomments_active)
257          {
258               return("<?php echo(subscribeToComments::url().".
259               "((\$core->blog->settings->url_scan == 'query_string') ? '&amp;' : '?').".
260               "'post_id='.\$_ctx->posts->post_id); ?>");
261          }
262     }
263         
264     /**
265     if there is a message
266     @param    attr <b>array</b>   Attribute
267     @param    content   <b>string</b>  Content
268     @return   <b>string</b> PHP block
269     */
270     public static function ifMessage($attr,$content)
271     {
272          return
273          "<?php if (\$_ctx->subscribeToComments->message !== null) : ?>"."\n".
274          $content.
275          "<?php endif; ?>";
276     }
277
278     /**
279     display a message
280     @return   <b>string</b> PHP block
281     */
282     public static function message()
283     {
284          return("<?php if (\$_ctx->subscribeToComments->message !== null) :"."\n".
285          "echo(\$_ctx->subscribeToComments->message);".
286          "endif; ?>");
287     }
288
289     /**
290     get nonce
291     @return   <b>string</b> Nonce
292     */
293     public static function getNonce()
294     {
295          return "<?php echo(crypt::hmac(DC_MASTER_KEY,session_id())); ?>";
296     }
297
298     /**
299     if it's a post
300     @param    attr <b>array</b>   Attribute
301     @param    content   <b>string</b>  Content
302     @return   <b>string</b> PHP block
303     */
304     public static function entryIf($attr,$content)
305     {
306          return
307          "<?php if ((isset(\$_GET['post_id'])) AND ".
308          "(is_numeric(\$_GET['post_id']))) : "."\n".
309          "\$_ctx->posts = \$core->blog->getPosts(".
310          "array('no_content' => true, 'post_id' => \$_GET['post_id'],".
311          "'post_open_comment' => 1,".
312          "'post_type' => subscribeToComments::getAllowedPostTypes())".
313          "); "."\n".
314          "if (!\$_ctx->posts->isEmpty()) : ?>"."\n".
315          $content.
316          "<?php unset(\$_ctx->posts); ".
317          "endif;"."\n".
318          "endif; ?>";
319     }
320
321     /**
322     if user is not logged in
323     @param    attr <b>array</b>   Attribute
324     @param    content   <b>string</b>  Content
325     @return   <b>string</b> PHP block
326     */
327     public static function loggedIfNot($attr,$content)
328     {
329          return('<?php if (!$_ctx->subscribeToComments->checkCookie) : ?>'."\n".
330          $content."\n".
331          "<?php endif; ?>");
332     }
333
334     /**
335     if user is logged in
336     @param    attr <b>array</b>   Attribute
337     @param    content   <b>string</b>  Content
338     @return   <b>string</b> PHP block
339     */
340     public static function loggedIf($attr,$content)
341     {
342          return('<?php if ($_ctx->subscribeToComments->checkCookie) : ?>'."\n".
343          $content."\n".
344          "<?php endif; ?>");
345     }
346
347     /**
348     if user is not blocked
349     @param    attr <b>array</b>   Attribute
350     @param    content   <b>string</b>  Content
351     @return   <b>string</b> PHP block
352     */
353     public static function blockedIfNot($attr,$content)
354     {
355          return('<?php if (!$_ctx->subscribeToComments->blocked) : ?>'."\n".
356          $content."\n".
357          "<?php endif; ?>");
358     }
359
360     /**
361     if user is blocked
362     @param    attr <b>array</b>   Attribute
363     @param    content   <b>string</b>  Content
364     @return   <b>string</b> PHP block
365     */
366     public static function blockedIf($attr,$content)
367     {
368          return('<?php if ($_ctx->subscribeToComments->blocked) : ?>'."\n".
369          $content."\n".
370          "<?php endif; ?>");
371     }
372
373     /**
374     loop on posts
375     @param    attr <b>array</b>   Attribute
376     @param    content   <b>string</b>  Content
377     @return   <b>string</b> PHP block
378     */
379     public static function entries($attr,$content)
380     {
381          return("<?php ".
382          '$_ctx->meta = new dcMeta($core);'.
383          "\$_ctx->posts = \$_ctx->meta->getPostsByMeta(array(".
384          "'meta_type' => 'subscriber','meta_id' => ".
385          "subscriber::getCookie('id'),".
386          "'no_content' => true,".
387          "'post_type' => subscribeToComments::getAllowedPostTypes()));".
388          "if (!\$_ctx->posts->isEmpty()) :"."\n".
389          "while (\$_ctx->posts->fetch()) : ?>"."\n".
390          $content.
391          "<?php endwhile; "."\n".
392          " endif;"."\n".
393          'unset($_ctx->meta);'.
394          "unset(\$_ctx->posts); ?>");
395     }
396
397     /**
398     get email address
399     @return   <b>string</b> PHP block
400     */
401     public static function email()
402     {
403          return('<?php echo($_ctx->subscribeToComments->email); ?>');     
404     }
405
406     /**
407     get the URL of the subscriptions page
408     @return   <b>string</b> URL
409     */
410     public static function url()
411     {
412          return("<?php echo(subscribeToComments::url()); ?>");
413     }
414
415     /**
416     display checkbox to subscribe to comments
417     */
418     public static function publicCommentFormAfterContent()
419     {
420          global $_ctx;
421
422          if (subscribeToComments::getPost($_ctx->posts->post_id) == false)
423          {return;}
424
425          $checked = null;
426
427          # if checkbox is unchecked, don't check it
428          if (isset($_POST['subscribeToComments'])) 
429               {$checked = true;}
430          elseif (isset($_COOKIE['subscribetocomments']))
431               {$checked = true;}
432          if ($checked) {$checked =  ' checked="checked" ';}
433
434          $logged = 
435          (subscriber::checkCookie())
436          ?
437               $logged = ' (<strong><a href="'.subscribeToComments::url().'">'.
438                    __('Logged in').'</a></strong>)'
439          : '';
440
441          echo '<p>'.
442          '<label><input type="checkbox" name="subscribeToComments" '.
443          'id="subscribeToComments"'.$checked.' /> '.
444          __('Receive following comments by email').'</label>'.
445          $logged.
446          '</p>';
447     }
448     
449     /**
450     display a CSS rule for default themes
451     */
452     public static function publicHeadContent()
453     {
454          echo '<style type="text/css" media="screen">'."\n".
455          '#comment-form #subscribeToComments '.
456          '{width:auto;border:0;margin:0 5px 0 140px;}'."\n".
457          '</style>';
458     }
459
460     /**
461     add tpl code after the <tpl:EntryIf comments_active="1">...</tpl:EntryIf> tag
462     @param    core <b>core</b>    Dotclear core
463     @param    b    <b>array</b>   tag
464     @param    attr <b>array</b>   attributes
465     */
466     public static function templateAfterBlock(&$core,$b,$attr)
467     {
468          global $_ctx;
469
470          if ($core->url->type == 'feed') {return;}
471
472          if ($b == 'EntryIf' && isset($attr['comments_active'])
473               && $attr['comments_active'] == 1 && !isset($attr['pings_active']))
474          {
475               if ((!is_numeric($_ctx->posts->post_id)) OR
476               (subscribeToComments::getPost($_ctx->posts->post_id) == false))
477               {
478                    return;
479               }
480               # else
481               return 
482               '<?php if (($core->blog->settings->subscribetocomments_active) &&
483                    $_ctx->posts->commentsActive()) : ?>
484                    <div id="subscribetocomments_block">
485                         <h3><?php echo __("Subscribe to comments"); ?></h3>
486                         <p>
487                              <a href="<?php echo(subscribeToComments::url().
488                              (($core->blog->settings->url_scan == "query_string") ? "&amp;" : "?").
489                              "post_id=".$_ctx->posts->post_id); ?>">
490                                   <!-- # If the subscriber is logged in -->
491                                   <?php if (subscriber::checkCookie()) : ?>
492                                        <?php echo __("Subscribe to receive following comments by email or manage subscriptions"); ?>
493                                   <?php endif; ?>
494                                   <!-- # If the subscriber is not logged in -->
495                                   <?php if (!subscriber::checkCookie()) : ?>
496                                        <?php echo __("Subscribe to receive following comments by email"); ?>
497                                   <?php endif; ?>
498                              </a>
499                         </p>
500                    </div>
501               <?php endif; ?>';
502               # strings
503               __("Subscribe to receive following comments by email or manage subscriptions");
504               __("Subscribe to receive following comments by email");
505          }
506     }
507}
508
509if ($core->blog->settings->subscribetocomments_active)
510{
511     # behaviors
512     $core->addBehavior('coreAfterCommentCreate',array('subscribeToComments',
513          'coreAfterCommentCreate'));
514
515     # post.html
516     $core->tpl->addValue('SubscribeToCommentsFormChecked',
517          array('subscribeToCommentsTpl','formChecked'));
518     $core->tpl->addValue('SubscribeToCommentsFormLink',
519          array('subscribeToCommentsTpl','formLink'));
520
521     # blocks
522     $core->tpl->addBlock('SubscribeToCommentsLoggedIf',
523          array('subscribeToCommentsTpl','loggedIf'));
524     $core->tpl->addBlock('SubscribeToCommentsLoggedIfNot',
525          array('subscribeToCommentsTpl','loggedIfNot'));
526     $core->tpl->addBlock('SubscribeToCommentsBlockedIf',
527          array('subscribeToCommentsTpl','blockedIf'));
528     $core->tpl->addBlock('SubscribeToCommentsBlockedIfNot',
529          array('subscribeToCommentsTpl','blockedIfNot'));
530
531     # nonce
532     $core->tpl->addValue('SubscribeToCommentsNonce',
533          array('subscribeToCommentsTpl','getNonce'));
534
535     # page
536     $core->tpl->addBlock('SubscribeToCommentsEntryIf',
537          array('subscribeToCommentsTpl','entryIf'));
538     
539     # message
540     $core->tpl->addBlock('SubscribeToCommentsIfMessage',
541          array('subscribeToCommentsTpl','ifMessage'));
542     $core->tpl->addValue('SubscribeToCommentsMessage',
543          array('subscribeToCommentsTpl','message'));
544
545     # form   
546     $core->tpl->addValue('SubscribeToCommentsURL',
547          array('subscribeToCommentsTpl','url'));
548     $core->tpl->addValue('SubscribeToCommentsEmail',
549          array('subscribeToCommentsTpl','email'));
550
551     # posts
552     $core->tpl->addBlock('SubscribeToCommentsEntries',
553          array('subscribeToCommentsTpl','entries'));
554
555     # add code to post.html
556     if ($core->blog->settings->subscribetocomments_tpl_checkbox === true)
557     {
558          $core->addBehavior('publicCommentFormAfterContent',
559               array('subscribeToCommentsTpl','publicCommentFormAfterContent'));
560     }
561     if ($core->blog->settings->subscribetocomments_tpl_css === true)
562     {
563          $core->addBehavior('publicHeadContent',
564               array('subscribeToCommentsTpl','publicHeadContent'));
565     }
566     if ($core->blog->settings->subscribetocomments_tpl_link === true)
567     {
568          $core->addBehavior('templateAfterBlock',
569               array('subscribeToCommentsTpl','templateAfterBlock'));
570     }
571}
572
573?>
Note: See TracBrowser for help on using the repository browser.

Sites map