source: extensions/Subscribe_to_comments/include/functions.inc.php @ 12561

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

and standalone links, fully compatible with Comments on Albums

File size: 7.6 KB
Line 
1<?php
2if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
3
4/**
5 * Send comment to subscribers
6 * @param array comm
7 */
8function send_comment_to_subscribers($comm)
9{
10  global $conf, $page, $user;
11 
12  $type= isset($comm['category_id']) ? 'category' : 'image';
13 
14  // exclude current user
15  $exclude = null;
16  if (!empty($_POST['stc_mail'])) $exclude = pwg_db_real_escape_string($_POST['stc_mail']);
17  else if (!is_a_guest()) $exclude = $user['email'];
18 
19  // get subscribers emails
20  $query = '
21SELECT
22    email
23  FROM '.SUBSCRIBE_TO_TABLE.'
24  WHERE
25    '.$type.'_id = '.$comm[$type.'_id'].'
26    AND validated = true
27    AND email != "'.$exclude.'"
28';
29  $emails = array_from_query($query, 'email');
30 
31  set_make_full_url();
32  if ($type == 'image')
33  {
34    // get image infos
35    $query = '
36SELECT
37    id,
38    name,
39    file
40  FROM '.IMAGES_TABLE.'
41  WHERE id = '.$comm['image_id'].'
42;';
43    $element = pwg_db_fetch_assoc(pwg_query($query));
44     
45    if (empty($element['name']))
46    {
47      $element['name'] = get_name_from_file($element['file']);
48    }
49   
50    $url_params = array('image_id' => $element['id']);
51    if (!empty($page['category']))
52    {
53      $url_params['section'] = 'categories';
54      $url_params['category'] = $page['category'];
55    }
56
57    $element['url'] = make_picture_url($url_params);
58  }
59  else if ($type == 'category')
60  {
61    // get category infos
62    $query = '
63SELECT
64    id,
65    name,
66    permalink
67  FROM '.CATEGORIES_TABLE.'
68  WHERE id = '.$comm['category_id'].'
69;';
70    $element = pwg_db_fetch_assoc(pwg_query($query));
71   
72    $url_params['section'] = 'categories';
73    $url_params['category'] = $element;
74   
75    $element['url'] = make_index_url($url_params);
76  }
77 
78  // get author name
79  if ($comm['author'] == 'guest')
80  {
81    $comm['author'] = l10n('guest');
82  }
83 
84  // mail content
85  $mail_args = array(
86    'subject' => '['.strip_tags($conf['gallery_title']).'] Re:'.$element['name'],
87    'content_format' => 'text/html',
88    );
89   
90  $generic_content = '
91<b>.'.trigger_event('render_comment_author', $comm['author']).'</b> wrote :
92
93<blockquote>'.trigger_event('render_comment_content', $comm['content']).'</blockquote>
94
95<a href="'.$element['url'].'#comment-'.$comm['id'].'">Link to comment</a>
96<br><br>
97================================
98<br><br>';
99
100  foreach ($emails as $email)
101  {
102    $mail_args['content'] = $generic_content.'
103<a href="'.make_stc_url('unsubscribe-'.$type, $email, $element['id']).'">Stop receiving notifications for this picture</a><br>
104<a href="'.make_stc_url('unsubscribe-all', $email).'">Stop receiving all notifications</a><br>
105';
106//<a href="'.make_stc_url('manage', $email).'">Manage my subscribtions</a>
107    pwg_mail($email, $mail_args);
108  }
109 
110  unset_make_full_url();
111}
112
113
114/*
115 * add an email to subscribers list
116 * @param int (image|category)_id
117 * @param string email
118 * @param string type (image|category)
119 */
120function subscribe_to_comments($element_id, $email, $type='image')
121{
122  global $page, $user, $conf, $template, $picture;
123 
124  $infos = $errors = array();
125  if ( is_a_guest() and empty($email) )
126  {
127    array_push($errors, l10n('Invalid email adress, your are not subscribed to comments.'));
128   
129    $orig = $template->get_template_vars('errors');
130    if (empty($orig)) $orig = array();
131    $template->assign('errors', array_merge($orig, $errors));
132   
133    if ($type == 'category') $template->set_prefilter('index', 'coa_messages'); // here we use a prefilter existing in COA
134   
135    return;
136  }
137  else if (!is_a_guest())
138  {
139    $email = $user['email'];
140  }
141 
142  // don't care if already registered
143  $query = '
144INSERT IGNORE INTO '.SUBSCRIBE_TO_TABLE.'(
145    email,
146    '.$type.'_id,
147    registration_date,
148    validated
149  )
150  VALUES(
151    "'.pwg_db_real_escape_string($email).'",
152    '.$element_id.',
153    NOW(),
154    "'.(is_a_guest() ? "false" : "true").'"
155  )
156;';
157  pwg_query($query);
158 
159  // send validation mail
160  if (is_a_guest() and pwg_db_insert_id() != 0)
161  {
162    $element_name = ($type == 'image') ? $picture['current']['name'] : $page['category']['name'];
163   
164    $mail_args = array(
165      'subject' => '['.strip_tags($conf['gallery_title']).'] Please confirm your subscribtion to comments',
166      'content_format' => 'text/html',
167      );
168     
169    $mail_args['content'] = '
170You requested to subscribe by email to comments on <b>'.$element_name.'</b>.<br>
171<br>
172We care about your inbox, so we want to confirm this request. Please click the confirm link to activate the subscription.<br>
173<br>
174<a href="'.make_stc_url('validate-'.$type, $email, $element_id).'">Confirm subscription</a><br>
175<br>
176If you did not request this action please disregard this message.
177';
178
179    pwg_mail($email, $mail_args);
180    array_push($infos, l10n('Please check your email inbox to confirm your subscription.'));
181  }
182  // just displat confirmation message
183  else if (pwg_db_insert_id() != 0)
184  {
185    array_push($infos, l10n('You have been added to the list of subscribers for this '.($type=='image' ? 'picture' : 'album').'.'));
186  }
187 
188 
189  if (!empty($infos))
190  {
191    $orig = $template->get_template_vars('infos');
192    if (empty($orig)) $orig = array();
193    $template->assign('infos', array_merge($orig, $infos));
194   
195    if ($type == 'category') $template->set_prefilter('index', 'coa_messages');
196  }
197}
198
199
200/**
201 * create absolute url to subscriptions section
202 * @param string action
203 * @param string email
204 * @return string
205 */
206function make_stc_url($action, $email)
207{
208  if ( empty($action) or empty($email) )
209  {
210    trigger_error('make_stc_url missing action and/or mail', E_USER_WARNING);
211    return null;
212  }
213 
214  global $conf;
215  set_make_full_url();
216 
217  $url_params = array(
218    'action' => $action,
219    'email' => $email,
220    'key' => crypt_value($action.$email, $conf['secret_key']),
221    );
222 
223  if (func_num_args() > 2)
224  {
225    $url_params['param'] = func_get_arg(2);
226  }
227 
228  $url = add_url_params(
229    make_index_url( array('section' => 'subscriptions') ),
230    $url_params
231    );
232   
233  unset_make_full_url();
234  return $url;
235}
236
237
238/**
239 * crypt a string using mcrypt extension or a binary method
240 * @param string value to crypt
241 * @param string key
242 * @return string
243 */
244function crypt_value($value, $key)
245{ 
246  if (extension_loaded('mcrypt'))
247  {
248    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
249    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
250    $value = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $value, MCRYPT_MODE_ECB, $iv);
251  }
252  else
253  {
254    $value = $value ^ $key; // binary XOR operation
255  }
256 
257  $value = base64url_encode($value);
258  return trim($value); 
259}
260
261/**
262 * decrypt a string crypted with previous function
263 * @param string value to decrypt
264 * @param string key
265 * @return string
266 */
267function decrypt_value($value, $key)
268{
269  $value = base64url_decode($value); 
270 
271  if (extension_loaded('mcrypt'))
272  {
273    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
274    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
275    $value = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $value, MCRYPT_MODE_ECB, $iv);
276  }
277  else
278  {
279    $value = $value ^ $key; // binary XOR operation
280  }
281 
282  return trim($value);
283}
284
285
286/**
287 * variant of base64 functions usable into url
288 * http://fr.php.net/manual/fr/function.base64-encode.php#103849
289 */
290function base64url_encode($data)
291{
292  return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
293}
294function base64url_decode($data)
295{
296  return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
297} 
298
299?>
Note: See TracBrowser for help on using the repository browser.