source: extensions/Subscribe_to_comments/include/subscribe_to_comments.inc.php @ 12620

Last change on this file since 12620 was 12620, checked in by mistic100, 12 years ago

fix key generation function when mcrypt is disabled, fix display on some themes

File size: 11.8 KB
Line 
1<?php
2if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
3
4/**
5 * detect 'subscriptions' section and load page
6 */
7function stc_detect_section() {
8  global $tokens, $page;
9 
10  if ($tokens[0] == 'subscriptions')
11  {
12    $page['section'] = 'subscriptions';
13  }
14}
15
16function stc_load_section() {
17  global $page;
18
19  if (isset($page['section']) and $page['section'] == 'subscriptions')
20  {
21    include(SUBSCRIBE_TO_PATH.'include/subscribtions_page.inc.php');
22  }
23}
24
25
26/**
27 * send notifications and/or add subscriber
28 */
29function stc_comment_insertion($comm)
30{
31  global $page, $template;
32 
33  $infos = $errors = array();
34 
35  if ($comm['action'] == 'validate')
36  {
37    send_comment_to_subscribers($comm);
38  }
39 
40  if ( !empty($_POST['stc_check']) and ( $comm['action'] == 'validate' or $comm['action'] == 'moderate' ) )
41  {
42    if (isset($comm['image_id']))
43    {
44      $return = subscribe_to_comments($comm['image_id'], @$_POST['stc_mail'], 'image');
45    }
46    else if (isset($comm['category_id']))
47    {
48      $return = subscribe_to_comments($comm['category_id'], @$_POST['stc_mail'], 'category');
49     
50    }
51   
52    if (isset($return))
53    {
54      if ($return === 'confirm_mail')
55      {
56        array_push($infos, l10n('Please check your email inbox to confirm your subscription.'));
57      }
58      else if ($return === true)
59      {
60        array_push($infos, l10n('You have been added to the list of subscribers for this '.(isset($comm['image_id'])?'picture':'album').'.'));
61      }
62      else
63      {
64        array_push($errors, l10n('Invalid email adress, your are not subscribed to comments.'));
65      }
66     
67       // messages management
68      if (!empty($errors))
69      {
70        $errors_bak = $template->get_template_vars('errors');
71        if (empty($errors_bak)) $errors_bak = array();
72        $template->assign('errors', array_merge($errors_bak, $errors));
73        $template->set_prefilter('index', 'coa_messages'); // here we use a prefilter existing in COA
74      }
75      if (!empty($infos))
76      {
77        $infos_bak = $template->get_template_vars('infos');
78        if (empty($infos_bak)) $infos_bak = array();
79        $template->assign('infos', array_merge($infos_bak, $infos));
80        $template->set_prefilter('index', 'coa_messages');
81      }
82    }
83  }
84}
85
86function stc_comment_validation($comm_ids, $type='image')
87{
88  if (!is_array($comm_ids)) $comm_ids = array($comm_ids);
89 
90  foreach($comm_ids as $comm_id)
91  {
92    if ($type == 'image')
93    {
94      $query = '
95SELECT
96    id,
97    image_id,
98    author,
99    content
100  FROM '.COMMENTS_TABLE.'
101  WHERE id = '.$comm_id.'
102;';
103    }
104    else if ($type == 'category')
105    {
106      $query = '
107SELECT
108    id,
109    category_id,
110    author,
111    content
112  FROM '.COA_TABLE.'
113  WHERE id = '.$comm_id.'
114;';
115    }
116   
117    $comm = pwg_db_fetch_assoc(pwg_query($query));
118    send_comment_to_subscribers($comm);
119  }
120}
121
122
123/**
124 * add field and link on picture page
125 */
126function stc_on_picture()
127{
128  global $template, $picture, $page;
129 
130  $infos = $array = array();
131 
132  if (isset($_POST['stc_submit']))
133  {
134    $return = subscribe_to_comments($picture['current']['id'], @$_POST['stc_mail_stdl'], 'image');
135    if ($return === 'confirm_mail')
136    {
137      array_push($infos, l10n('Please check your email inbox to confirm your subscription.'));
138    }
139    else if ($return === true)
140    {
141      array_push($infos, l10n('You have been added to the list of subscribers for this picture.'));
142    }
143    else
144    {
145      array_push($errors, l10n('Invalid email adress, your are not subscribed to comments.'));
146    }
147  }
148  else if (isset($_GET['stc_unsubscribe']))
149  {
150    if (un_subscribe_to_comments($picture['current']['id'], null, 'image'))
151    {
152      array_push($infos, l10n('Successfully unsubscribed your email address from receiving notifications.'));
153    }
154  }
155 
156  // messages management
157  if (!empty($errors))
158  {
159    $errors_bak = $template->get_template_vars('errors');
160    if (empty($errors_bak)) $errors_bak = array();
161    $template->assign('errors', array_merge($errors_bak, $errors));
162  }
163  if (!empty($infos))
164  {
165    $infos_bak = $template->get_template_vars('infos');
166    if (empty($infos_bak)) $infos_bak = array();
167    $template->assign('infos', array_merge($infos_bak, $infos));
168  }
169 
170  $template->set_prefilter('picture', 'stc_on_picture_prefilter');
171}
172
173function stc_on_picture_prefilter($content, &$smarty)
174{
175  global $user, $picture;
176 
177  // if registered user with mail we check if already subscribed
178  $subscribed = false;
179  if ( !is_a_guest() and !empty($user['email']) )
180  {
181    $query = '
182SELECT id
183  FROM '.SUBSCRIBE_TO_TABLE.'
184  WHERE
185    email = "'.$user['email'].'"
186    AND image_id = '.$picture['current']['id'].'
187    AND validated = "true"
188;';
189    if (pwg_db_num_rows(pwg_query($query)))
190    {
191      $subscribed = true;
192    }
193  }
194 
195  ## subscribe at any moment ##
196  $search = '#\<\/div\>(.{0,5})\{\/if\}(.{0,5})\{\*comments\*\}#is';
197 
198  $replace = '
199<form method="post" action="{$comment_add.F_ACTION}" class="filter" id="stc_standalone">
200  <fieldset>';
201 
202  if ($subscribed)
203  {
204    $replace.= '
205    {\'You are currently subscribed to comments of this picture.\'|@translate}
206    <a href="'.add_url_params($picture['current']['url'], array('stc_unsubscribe'=>'1')).'">{\'Unsubscribe\'|@translate}';
207  }
208  else
209  {
210    $replace.= '
211    <legend>{\'Subscribe without commenting\'|@translate}</legend>';
212    if ( is_a_guest() or empty($user['email']) ) // email input for guest or users without mail
213    {
214      $replace.= '
215      <label>{\'Email address\'|@translate} <input type="text" name="stc_mail_stdl"></label>
216      <label><input type="submit" name="stc_submit" value="{\'Submit\'|@translate}"></label>';
217    }
218    else
219    {
220      $replace.= '
221      <label><input type="submit" name="stc_submit" value="{\'Subscribe\'|@translate}"></label>';
222    }
223  }
224     
225  $replace.= '
226  </fieldset>
227</form>
228</div>
229{/if}{*comments*}';
230
231  $content = preg_replace($search, $replace, $content);
232
233
234  ## subscribe while add a comment ##
235  $search = '#<input type="hidden" name="key" value="{\$comment_add\.KEY}"([ /]*)>#';
236  $replace = '<input type="hidden" name="key" value="{$comment_add.KEY}"$1>';
237 
238  if (!$subscribed)
239  {
240    $replace.= '
241    <label>{\'Notify me of followup comments\'|@translate} <input type="checkbox" name="stc_check" value="1"></label>';
242  }
243  if ( is_a_guest() or empty($user['email']) )
244  {
245    $replace.= '
246    <label id="stc_mail" style="display:none;">{\'Email address\'|@translate} <input type="text" name="stc_mail"></label>
247    {footer_script require="jquery"}{literal}
248    jQuery(document).ready(function() {
249      $("input[name=stc_check]").change(function() {
250        if ($(this).is(":checked")) $("#stc_mail").css("display", "");
251        else $("#stc_mail").css("display", "none");
252      });
253    });
254    {/literal}{/footer_script}';
255  }
256 
257  $content = preg_replace($search, $replace, $content);
258 
259  return $content;
260}
261
262
263/**
264 * add field and on album page
265 */
266function stc_on_album()
267{
268  global $page, $template, $pwg_loaded_plugins;
269 
270  $infos = $errors = array();
271 
272  if (
273      script_basename() != 'index' or !isset($page['section']) or
274      !isset($pwg_loaded_plugins['Comments_on_Albums']) or 
275      $page['section'] != 'categories' or !isset($page['category'])
276    )
277  {
278    return;
279  }
280 
281  if (isset($_POST['stc_submit']))
282  {
283    $return = subscribe_to_comments($page['category']['id'], @$_POST['stc_mail_stdl'], 'category');
284    if ($return === 'confirm_mail')
285    {
286      array_push($infos, l10n('Please check your email inbox to confirm your subscription.'));
287    }
288    else if ($return === true)
289    {
290      array_push($infos, l10n('You have been added to the list of subscribers for this album.'));
291    }
292    else
293    {
294      array_push($errors, l10n('Invalid email adress, your are not subscribed to comments.'));
295    }
296  }
297  else if (isset($_GET['stc_unsubscribe']))
298  {
299    if (un_subscribe_to_comments($page['category']['id'], null, 'category'))
300    {
301      array_push($infos, l10n('Successfully unsubscribed your email address from receiving notifications.'));
302    }
303  }
304 
305  // messages management
306  if (!empty($errors))
307  {
308    $errors_bak = $template->get_template_vars('errors');
309    if (empty($errors_bak)) $errors_bak = array();
310    $template->assign('errors', array_merge($errors_bak, $errors));
311    $template->set_prefilter('index', 'coa_messages'); // here we use a prefilter existing in COA
312  }
313  if (!empty($infos))
314  {
315    $infos_bak = $template->get_template_vars('infos');
316    if (empty($infos_bak)) $infos_bak = array();
317    $template->assign('infos', array_merge($infos_bak, $infos));
318    $template->set_prefilter('index', 'coa_messages');
319  }
320 
321  $template->set_prefilter('comments_on_albums', 'stc_on_album_prefilter');
322}
323
324function stc_on_album_prefilter($content, &$smarty)
325{
326  global $user, $page;
327 
328  // if registered user we check if already subscribed
329  $subscribed = false;
330  if ( !is_a_guest() and !empty($user['email']) )
331  {
332    $query = '
333SELECT id
334  FROM '.SUBSCRIBE_TO_TABLE.'
335  WHERE
336    email = "'.$user['email'].'"
337    AND category_id = '.$page['category']['id'].'
338    AND validated = "true"
339;';
340    if (pwg_db_num_rows(pwg_query($query)))
341    {
342      $subscribed = true;
343    }
344  }
345 
346  ## subscribe at any moment ##
347  $search = '#\<\/div\>(.{0,5})\{\/if\}(.{0,5})\{\*comments\*\}#is';
348 
349  $replace = '
350<form method="post" action="{$comment_add.F_ACTION}" class="filter" id="stc_standalone">
351  <fieldset>';
352 
353  if ($subscribed)
354  {
355    $url_params['section'] = 'categories';
356    $url_params['category'] = $page['category'];
357   
358    $element_url = make_index_url($url_params);
359 
360    $replace.= '
361    {\'You are currently subscribed to comments of this album.\'|@translate}
362    <a href="'.add_url_params($element_url, array('stc_unsubscribe'=>'1')).'">{\'Unsubscribe\'|@translate}';
363  }
364  else
365  {
366    $replace.= '
367    <legend>{\'Subscribe without commenting\'|@translate}</legend>';
368    if ( is_a_guest() or empty($user['email']) ) // email input for guest or users without mail
369    {
370      $replace.= '
371      <label>{\'Email address\'|@translate} <input type="text" name="stc_mail_stdl"></label>
372      <label><input type="submit" name="stc_submit" value="{\'Submit\'|@translate}"></label>';
373    }
374    else
375    {
376      $replace.= '
377      <label><input type="submit" name="stc_submit" value="{\'Subscribe\'|@translate}"></label>';
378    }
379  }
380     
381  $replace.= '
382  </fieldset>
383</form>
384</div>
385{/if}{*comments*}';
386
387  $content = preg_replace($search, $replace, $content);
388
389
390  ## subscribe while add a comment ##
391  $search = '<input type="hidden" name="key" value="{$comment_add.KEY}">';
392  $replace = $search;
393 
394  if (!$subscribed)
395  {
396    $replace.= '
397    <label>{\'Notify me of followup comments\'|@translate} <input type="checkbox" name="stc_check" value="1"></label>';
398  }
399  if ( is_a_guest() or empty($user['email']) )
400  {
401    $replace.= '
402    <label id="stc_mail" style="display:none;">{\'Email address\'|@translate} <input type="text" name="stc_mail"></label>
403    {footer_script require="jquery"}{literal}
404    jQuery(document).ready(function() {
405      $("input[name=stc_check]").change(function() {
406        if ($(this).is(":checked")) $("#stc_mail").css("display", "");
407        else $("#stc_mail").css("display", "none");
408      });
409    });
410    {/literal}{/footer_script}';
411  }
412
413  $content = str_replace($search, $replace, $content);
414 
415  return $content;
416}
417
418
419/**
420 * add link to management page for registered users
421 */
422function stc_profile_link()
423{
424  global $template, $user;
425 
426  if (!empty($user['email']))
427  {
428    $template->set_prefilter('profile_content', 'stc_profile_link_prefilter');
429  }
430}
431
432function stc_profile_link_prefilter($content, &$smarty)
433{
434  global $user;
435 
436  $search = '<p class="bottomButtons">';
437  $replace = '<a href="'.make_stc_url('manage', $user['email']).'" title="{\'Manage my subscriptions to comments\'|@translate}" rel="nofollow">{\'Manage my subscriptions to comments\'|@translate}</a><br>';
438 
439  return str_replace($search, $search.$replace, $content);
440}
441?>
Note: See TracBrowser for help on using the repository browser.