Dotclear

source: plugins/subscribeToComments/inc/lib.subscribeToComments.php @ 2294

Revision 2294, 13.0 KB checked in by Moe, 14 years ago (diff)

Subscribe to comments 1.3.2 :

  • limited subscribers list to the current blog (closes #461)
  • moved _public.php classes in inc/
  • switched to GPL v2
  • PHP 5.3 compatibility ?
Line 
1<?php
2# ***** BEGIN LICENSE BLOCK *****
3#
4# This file is part of Subscribe to comments, a plugin for Dotclear 2
5# Copyright (C) 2008,2009,2010 Moe (http://gniark.net/)
6#
7# Subscribe to comments is free software; you can redistribute it and/or
8# modify it under the terms of the GNU General Public License v2.0
9# as published by the Free Software Foundation.
10#
11# Subscribe to comments is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public
17# License along with this program. If not, see
18# <http://www.gnu.org/licenses/>.
19#
20# Icon (icon.png) and images are from Silk Icons :
21# <http://www.famfamfam.com/lab/icons/silk/>
22#
23# Inspired by Subscribe to Comments for WordPress :
24# <http://txfx.net/code/wordpress/subscribe-to-comments/>
25#
26# ***** END LICENSE BLOCK *****
27
28if (!defined('DC_RC_PATH')) {return;}
29
30/**
31@ingroup Subscribe to comments
32@brief Generic functions
33*/
34class subscribeToComments
35{
36     /** check if the string is a valid email address
37     @param    email     <b>string</b>  Email address
38     @return   <b>boolean</b> Is an email address ?
39     */
40     public static function checkEmail(&$email)
41     {
42          $email = urldecode($email);
43          if (!text::isEmail($email))
44          {
45               throw new Exception(__('Invalid email address.'));
46          }   
47     }
48
49     /** check if the string is a valid key
50     @param    key  <b>string</b>  Key
51     @return   <b>boolean</b> Is a key ?
52     \see http://www.php.net/manual/fr/function.md5.php#40251
53     */
54     public static function checkKey($key)
55     {
56          if (!(preg_match('/^[a-f0-9]{40}$/',$key)))
57          {
58               throw new Exception(__('Invalid key.'));
59          }
60     }
61
62     /**
63     remove old temporary keys
64     */
65     public static function cleanKeys()
66     {
67          global $core;
68
69          if ($_SERVER['REQUEST_TIME'] <= 
70               $core->blog->settings->subscribetocomments_clean_keys) {return;}
71
72          $core->blog->settings->setNameSpace('subscribetocomments');
73          $core->blog->settings->put('subscribetocomments_clean_keys',strtotime('+1 hour'),
74          'integer','Clean temporary keys');
75
76          # delete old temporary keys
77          $cur = $core->con->openCursor($core->prefix.'comment_subscriber');
78          $cur->temp_key = null;
79          $cur->temp_expire = null;
80          $cur->update('WHERE ((temp_expire IS NOT NULL)'.
81               ' AND (temp_expire < \''.date('Y-m-d H:i:s').'\'))');
82     }
83
84     /**
85     get the URL of the subscriptions page
86     @return   <b>string</b> URL
87     */
88     public static function url()
89     {
90          global $core;
91         
92          return($core->blog->url.$core->url->getBase('subscribetocomments'));
93     }
94     
95     /**
96     get a plugin's setting
97     @param    setting   <b>string</b>  Setting
98     @return   <b>string</b> setting
99     */
100     public static function getSetting($setting)
101     {
102          global $core;
103         
104          $setting = $core->blog->settings->{'subscribetocomments_'.$setting};
105         
106          if (strlen($setting) == 0) {return '';}
107          # else         
108          return(base64_decode($setting));
109     }
110     
111     /**
112     format settings
113     @param    tags <b>array</b>   Tags array
114     @param    str  <b>string</b>  String
115     @param    flip <b>boolean</b> Flip the tags array
116     @param    encode    <b>boolean</b> Serialize and encode returned string
117     @return   <b>string</b> string
118     */
119     public static function format($tags,$str,$flip=false,$encode=false)
120     {
121          global $tags_global;
122          $array = array();
123          foreach ($tags_global as $k => $v)
124          {
125               $array[$k] = $v['tag'];
126          }
127          if (empty($tags)) {$tags = array();}
128          foreach ($tags as $k => $v)
129          {
130               $array[$k] = $v['tag'];
131          }
132          if ($flip) {
133               $array = array_flip($array);
134          }
135          $str = str_replace(array_keys($array),array_values($array),$str);
136         
137          if ($encode)
138          {
139               $str = base64_encode($str);
140          }
141         
142          return($str);
143     }
144     
145     public static function setDefaultSettings($replace=false,$lang='en')
146     {
147          global $core;
148         
149          global $tags_global, $tags_account, $tags_subscribe, $tags_comment,
150               $tags_email;
151         
152          $settings =& $core->blog->settings;
153         
154          # load locales for the blog language
155          l10n::set(dirname(__FILE__).'/../locales/'.$lang.'/admin');
156         
157          $settings->setNameSpace('subscribetocomments');
158         
159          # Change From: header of outbound emails
160          $settings->put('subscribetocomments_email_from',
161          'dotclear@'.$_SERVER['HTTP_HOST'],
162          'string','Change From: header of outbound emails',$replace);
163         
164          # Allowed post types
165          $settings->put('subscribetocomments_post_types',
166          serialize(subscribeToComments::getPostTypes()),
167          'string','Allowed post types',$replace);
168         
169          $nl = "\n";
170          $nls = $nl.$nl;
171          $separator = '----------';
172          $foot_separator = '--';
173          $hello = __('Hello [email],');
174          $account = 
175               __('To manage your subscriptions, change your email address or block emails, click here: [manageurl]');
176         
177          # Account subject
178          $settings->put('subscribetocomments_account_subject',
179               subscribeToComments::format($tags_account,__('Your account on [blogname]'),
180                    false,true),'text',
181               'Email subject',$replace);
182          # Account content
183          $settings->put('subscribetocomments_account_content',
184               subscribeToComments::format($tags_account,
185                    $hello.$nl.
186                    __('here are some informations about your account on [blogname]:').
187                    $nls.
188                    __('Email address: [email]').$nls.
189                    $account.$nls.
190                    $foot_separator.$nl.'[blogurl]',false,true)
191               ,'text','Email content',$replace);
192         
193          # Send an email for each subscription
194          $settings->put('subscribetocomments_subscribe_active',
195               false,'boolean','Send an email for each subscription');
196          # Subscription subject
197          $settings->put('subscribetocomments_subscribe_subject',
198               subscribeToComments::format($tags_subscribe,
199                    __('Subscribed to [posttitle] - [blogname]'),false,true),'text',
200                    'Subscription subject',$replace);
201          # Subscription content
202          $settings->put('subscribetocomments_subscribe_content',
203               subscribeToComments::format($tags_subscribe,
204                    $hello.$nl.
205                    __('you subscribed to [posttitle]: [posturl]').$nls.
206                    $separator.$nls.
207                    $account.$nls.
208                    $foot_separator.$nl.'[blogurl]',false,true)
209               ,'text','Subscription content',$replace);
210         
211          # Comment subject
212          $settings->put('subscribetocomments_comment_subject',
213               subscribeToComments::format($tags_comment,
214               __('New comment on [posttitle] - [blogname]'),false,true),'text',
215               'Comment subject',$replace);
216          # Comment content
217          $settings->put('subscribetocomments_comment_content',
218               subscribeToComments::format($tags_comment,
219                    $hello.$nl.
220                    __('a new comment has been posted by [commentauthor] on [posttitle]:').
221                    $nls. 
222                    $separator.$nls.
223                    '[commentcontent]'.$nls.
224                    $separator.$nls.
225                    __('View the comment: [commenturl]').$nls.
226                    __('View the post: [posturl]').$nls.
227                    $separator.$nls.
228                    $account.$nls.
229                    $foot_separator.$nl.'[blogurl]',false,true)
230               ,'text','Comment content',$replace);
231         
232          # Email subject
233          $settings->put('subscribetocomments_email_subject',
234               subscribeToComments::format($tags_email,
235                    __('Change email address on [blogname]'),false,true),'text','Email subject',
236                    $replace);
237          # Email content
238          $settings->put('subscribetocomments_email_content',
239               subscribeToComments::format($tags_email,
240                    $hello.$nl.
241                    __('you have requested to change the email address of your subscriptions to [newemail], click on this link: [emailurl]').
242                    $nls.
243                    __('This link is valid for 24 hours.').$nls.
244                    $separator.$nls.
245                    $account.$nls.
246                    $foot_separator.$nl.'[blogurl]',false,true)
247               ,'text','Email content',$replace);
248         
249          # display
250          $settings->put('subscribetocomments_tpl_checkbox',true,
251               'boolean','Checkbox in comment form',$replace);
252         
253          $subscribetocomments_tpl_css = false;
254          $theme = $settings->theme;
255          if (($theme == 'default') OR ($theme == 'blueSilence'))
256          {
257               $subscribetocomments_tpl_css = true;
258          }
259          $settings->put('subscribetocomments_tpl_css',
260               $subscribetocomments_tpl_css,'boolean','Add CSS rule',$replace);
261         
262          $settings->put('subscribetocomments_tpl_link',true,
263               'boolean','Link to Subscribe to comments page',$replace);
264     }
265     
266     /**
267     get informations about a post
268     @param    post_id <b>integer</b> Post ID
269     @return   <b>array</b>   Array with informations
270     */
271     public static function getPost($post_id)
272     {
273          global $core;
274
275          $rs = $core->blog->getPosts(array('no_content' => true,
276               'post_id' => $post_id, 'post_open_comment' => 1,
277               'post_type' => self::getAllowedPostTypes())
278          );
279
280          if ($rs->isEmpty()) {return(false);}
281
282          $array['title'] = $rs->post_title;
283          # from getURL()
284          $array['url'] = $rs->getURL();
285          # /from getURL()
286          return($array);
287     }
288
289     /**
290     get available post types
291     @return   <b>array</b>   Array with post types
292     */
293     public static function getPostTypes($blog=false)
294     {
295          global $core;
296
297          $rs = $core->con->select('SELECT post_type AS type '.
298          'FROM '.$core->prefix.'post '.
299          (($blog)
300               ?    'WHERE blog_id = \''.$core->con->escape($core->blog->id).'\' '
301               : '').
302          'GROUP BY type ORDER BY type ASC;');
303
304          if ($rs->isEmpty()) {return(array());}
305
306          $types = array();
307
308          while ($rs->fetch())
309          {
310               $types[] = $rs->type;
311          }
312
313          return($types);
314     }
315
316     /**
317     get allowed post types
318     @return   <b>array</b>   Array with post types
319     */
320     public static function getAllowedPostTypes()
321     {
322          global $core;
323
324          $post_types = @unserialize(
325               $core->blog->settings->subscribetocomments_post_types);
326
327          if (empty($post_types))
328          {
329               return(array());
330          }
331
332          return($post_types);
333     }
334
335     /**
336     behavior coreAfterCommentCreate
337     @param    core <b></b> dcCore object
338     @param    cur <b>cursor</b> Cursor
339     \see http://dev.dotclear.net/2.0/changeset/2181
340     */
341     public static function coreAfterCommentCreate($core,$cur)
342     {
343          if (isset($_POST['subscribeToComments']))
344          {
345               $subscriber = new subscriber($cur->comment_email);
346               $subscriber->subscribe($cur->post_id);
347          }
348          self::send($cur,$cur->comment_id);
349     }
350     
351     /**
352     behavior coreAfterCommentUpdate
353     @param    this_ <b></b> null
354     @param    cur <b>cursor</b> Cursor
355     @param    rs <b>recordset</b> Recordset
356     */
357     public static function coreAfterCommentUpdate($this_,$cur,$rs)
358     {
359          $cur->post_id = $rs->post_id;
360          $cur->comment_trackback = $rs->comment_trackback;
361          self::send($cur,$rs->comment_id);
362     }
363     
364     /**
365     send emails
366     @param    cur <b>cursor</b> Cursor
367     @param    comment_id <b>integer</b> Comment ID
368     */
369     public static function send($cur,$comment_id)
370     {
371          # We don't want notification for spam and trackbacks
372          # from emailNotification (modified)
373          if (($cur->comment_status != 1) OR ($cur->comment_trackback == 1))  {
374               return;
375          }
376          # /from emailNotification
377
378          global $core;
379
380          # we send only one mail to notify the subscribers
381          # won't send multiple emails when updating an email from the backend
382          $rs = $core->con->select(
383               'SELECT notification_sent FROM '.$core->prefix.'comment '.
384               'WHERE (comment_id = '.$core->con->escape($comment_id).') '.
385               'AND (notification_sent = 1);'
386          );
387
388          if ($rs->isEmpty())
389          {
390               # get the subscribers' email addresses
391               $rs = $core->con->select(
392                    'SELECT S.id, S.email, S.user_key FROM '.
393                    $core->prefix.'comment_subscriber S '.
394                    'INNER JOIN '.$core->prefix.'meta M ON '.
395                    (($core->con->driver() == 'pgsql') ?
396                    # CAST = PostgreSQL compatibility :
397                    # PGSQL need datas of the same type to compare
398                    '(S.id = CAST(M.meta_id AS integer))': 
399                    '(S.id = M.meta_id)').
400                    ' AND (M.meta_type = \'subscriber\') AND (M.post_id = '.$cur->post_id.')'.
401                    ' AND (S.email != \''.$cur->comment_email.'\')'.
402                    ' AND (S.status = \'1\');'
403               );
404
405               # remember that the comment's notification was sent
406               $cur_sent = $core->con->openCursor($core->prefix.'comment');
407               $cur_sent->notification_sent = 1;
408               $cur_sent->update('WHERE comment_id = '.
409                    $core->con->escape($comment_id).';');
410
411               if (!$rs->isEmpty())
412               {
413                    $post = self::getPost($cur->post_id);
414                    if ($post == false) {return;}
415               
416                    # from emailNotification/behaviors.php
417                    $comment = preg_replace('%</p>\s*<p>%msu',"\n\n",$cur->comment_content);
418                    $comment = str_replace('<br />',"\n",$comment);
419                    $comment = html::clean($comment);
420
421                    while ($rs->fetch())
422                    {
423                         # email
424                         $subject = sprintf(
425                              self::getSetting('comment_subject'),
426                              $core->blog->name,$core->blog->url,$rs->email,
427                              subscriber::pageLink($rs->email,$rs->user_key),
428                              $post['title'],$post['url'],$post['url'].'#c'.$comment_id,
429                              $cur->comment_author,$comment);
430                         $content = sprintf(
431                              self::getSetting('comment_content'),
432                              $core->blog->name,$core->blog->url,$rs->email,
433                              subscriber::pageLink($rs->email,$rs->user_key),
434                              $post['title'],$post['url'],$post['url'].'#c'.$comment_id,
435                              $cur->comment_author,$comment);
436                         self::mail($rs->email,$subject,$content);
437                    }
438               }
439          }
440     }
441
442     /**
443     send an email
444     @param    to <b>string</b> Email recipient
445     @param    subject <b>string</b> Email subject
446     @param    content <b>string</b> Email content
447     */
448     public static function mail($to,$subject,$content)
449     {
450          global $core;
451
452          $headers = array(
453               'From: '.$core->blog->settings->subscribetocomments_email_from,
454               'MIME-Version: 1.0',
455               'Content-Type: text/plain; charset=UTF-8;',
456               'X-Mailer: Dotclear'
457          );
458
459          # from /dotclear/admin/auth.php : mail::B64Header($subject)
460          mail::sendMail($to,mail::B64Header($subject),
461               wordwrap($content,70),$headers);
462     }
463
464     /**
465     redirect to an URL with a message and exit
466     @param    get <b>string</b> GET URL
467     */
468     public static function redirect($get='')
469     {
470          http::redirect(subscribeToComments::url().'/'.$get);
471          exit();
472     }
473}
474
475?>
Note: See TracBrowser for help on using the repository browser.

Sites map