1 | <?php |
---|
2 | global $user, $conf; |
---|
3 | |
---|
4 | class Piwecard { |
---|
5 | var $config; |
---|
6 | var $user_groups = array(); |
---|
7 | |
---|
8 | // Class constructor |
---|
9 | function __construct() { |
---|
10 | $this->get_config(); |
---|
11 | } |
---|
12 | |
---|
13 | // Load general configuration from config_database |
---|
14 | function get_config() { |
---|
15 | $query = 'SELECT value FROM '.CONFIG_TABLE.' WHERE param="piwecard";'; |
---|
16 | $result = pwg_query($query); |
---|
17 | |
---|
18 | if(isset($result)) { |
---|
19 | $row = pwg_db_fetch_row($result); |
---|
20 | if(is_string($row[0])) { |
---|
21 | $this->config = unserialize(($row[0])); |
---|
22 | } |
---|
23 | } |
---|
24 | $this->get_default_config(); |
---|
25 | } |
---|
26 | |
---|
27 | // Initialize default values of params |
---|
28 | private function get_default_config() { |
---|
29 | require(PIWECARD_INSTALL_PATH.'default_values.inc.php'); |
---|
30 | foreach ($ecard_default_values as $key => $value) { |
---|
31 | if (!isset($this->config[$key])) |
---|
32 | $this->config[$key] = $value; |
---|
33 | } |
---|
34 | } |
---|
35 | |
---|
36 | //Get the default value of a parameter |
---|
37 | function get_default_config_param($param) { |
---|
38 | require(PIWECARD_INSTALL_PATH.'default_values.inc.php'); |
---|
39 | return $ecard_default_values[$param]; |
---|
40 | } |
---|
41 | |
---|
42 | // Save general configuration to config_database |
---|
43 | function set_config() { |
---|
44 | conf_update_param('piwecard', pwg_db_real_escape_string(serialize($this->config))); |
---|
45 | } |
---|
46 | |
---|
47 | function section_init_ecard() { |
---|
48 | global $tokens, $page; |
---|
49 | |
---|
50 | if ($tokens[0] == 'ecard') |
---|
51 | $page['section'] = 'ecard'; |
---|
52 | } |
---|
53 | |
---|
54 | function index_ecard() { |
---|
55 | global $page; |
---|
56 | |
---|
57 | if (isset($page['section']) and $page['section'] == 'ecard') { |
---|
58 | include('publish.inc.php'); |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | //Générer une chaine de caractère unique et aléatoire |
---|
63 | private function random($car) { |
---|
64 | $string = ""; |
---|
65 | $chaine = "abcdefghijklmnpqrstuvwxy0123456789"; |
---|
66 | srand((double)microtime()*1000000); |
---|
67 | for($i=0; $i<$car; $i++) { |
---|
68 | $string .= $chaine[rand()%strlen($chaine)]; |
---|
69 | } |
---|
70 | return $string; |
---|
71 | } |
---|
72 | |
---|
73 | function parse($data, $values, $image_element) { |
---|
74 | include (PIWECARD_PATH.'include/config_param.inc.php'); |
---|
75 | |
---|
76 | $patterns = array(); |
---|
77 | $replacements = array(); |
---|
78 | foreach ($ecard_parse as $key => $value) { |
---|
79 | array_push($patterns, $key); |
---|
80 | array_push($replacements, $value); |
---|
81 | } |
---|
82 | |
---|
83 | return str_replace($patterns, $replacements, $data); |
---|
84 | } |
---|
85 | |
---|
86 | // Get the number of ecard in the database |
---|
87 | function get_nb_ecard() { |
---|
88 | $query = 'SELECT COUNT(DISTINCT ecard_id) AS nb FROM '.PIWECARD_TABLE.' ORDER BY date_creation;'; |
---|
89 | $result = pwg_query($query); |
---|
90 | |
---|
91 | if ($result) { |
---|
92 | $nb=pwg_db_fetch_assoc($result); |
---|
93 | return $nb['nb']; |
---|
94 | } else |
---|
95 | return 0; |
---|
96 | } |
---|
97 | |
---|
98 | // Get the number of valid ecard in the database |
---|
99 | function get_nb_valid_ecard() { |
---|
100 | $query = 'SELECT COUNT(DISTINCT ecard_id) AS nb FROM '.PIWECARD_TABLE.' WHERE date_validity IS NULL OR date_validity > NOW();'; |
---|
101 | $result = pwg_query($query); |
---|
102 | |
---|
103 | if ($result) { |
---|
104 | $nb=pwg_db_fetch_assoc($result); |
---|
105 | return $nb['nb']; |
---|
106 | } else |
---|
107 | return 0; |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | // Get ecard information into array |
---|
112 | function get_ecard($ecard_id = null) { |
---|
113 | if ($ecard_id!== null) { |
---|
114 | $query = 'SELECT * FROM ' . PIWECARD_TABLE .' WHERE ecard_id="' . $ecard_id . '" LIMIT 1;'; |
---|
115 | |
---|
116 | $result = pwg_query($query); |
---|
117 | if ($result) |
---|
118 | return pwg_db_fetch_assoc($result); |
---|
119 | else |
---|
120 | return false; |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | function is_valid($ecard_id = null) { |
---|
125 | if (isset($ecard_id)) { |
---|
126 | $ecard_info = $this->get_ecard($ecard_id); |
---|
127 | if (isset($ecard_info)) { |
---|
128 | // Valid duration for an ecard |
---|
129 | $date_validity = $ecard_info['date_validity']; |
---|
130 | |
---|
131 | if (isset($date_validity)) { |
---|
132 | $now = new DateTime(date("Y-m-d H:i:s")); |
---|
133 | $date_validity = new DateTime($date_validity); |
---|
134 | if ($date_validity > $now) |
---|
135 | return true; |
---|
136 | else |
---|
137 | return false; |
---|
138 | } else { |
---|
139 | return true; |
---|
140 | } |
---|
141 | } else |
---|
142 | return false; |
---|
143 | } else { |
---|
144 | return false; |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | // delete one ecard |
---|
149 | // force to delete valid ecard |
---|
150 | function delete_ecard($ecard_id = null) { |
---|
151 | if (isset($ecard_id)) { |
---|
152 | $query = 'DELETE FROM ' . PIWECARD_TABLE .' WHERE ecard_id="' . $ecard_id . '";'; |
---|
153 | pwg_query($query); |
---|
154 | } else |
---|
155 | return false; |
---|
156 | } |
---|
157 | |
---|
158 | // Delete all invalid ecard |
---|
159 | function delete_allinvalid_ecard() { |
---|
160 | $query = 'DELETE FROM ' . PIWECARD_TABLE .' WHERE date_validity < NOW();'; |
---|
161 | pwg_query($query); |
---|
162 | } |
---|
163 | |
---|
164 | function is_valid_email($email_address) { |
---|
165 | $syntax = '#^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,6}$#'; |
---|
166 | |
---|
167 | if(preg_match($syntax, $email_address)) |
---|
168 | return true; |
---|
169 | else |
---|
170 | return false; |
---|
171 | } |
---|
172 | |
---|
173 | // Add tpl to picture.php page to display ecard informations |
---|
174 | function display_ecard_to_picture() { |
---|
175 | global $page, $user, $template; |
---|
176 | |
---|
177 | // Only on category page! |
---|
178 | if (isset($page['section'])) { |
---|
179 | $upper_names = null; |
---|
180 | |
---|
181 | if (!empty($page['category'])) { |
---|
182 | // Gets all upper categories from the image category to test |
---|
183 | // - if the upper category is activated for this function |
---|
184 | $query = 'SELECT * FROM '.CATEGORIES_TABLE.' WHERE id = '.pwg_db_real_escape_string($page['category']['id']).';'; |
---|
185 | $cat = pwg_db_fetch_assoc(pwg_query($query)); |
---|
186 | |
---|
187 | if (empty($cat)) { |
---|
188 | $upper_ids = null; |
---|
189 | } else { |
---|
190 | $upper_ids = explode(',', $cat['uppercats']); |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | if ($this->config['authorized_cats'] == 'user') { // !Function only allowed on user image |
---|
195 | if (isset($cat) and !empty($cat)) { |
---|
196 | $catname[0] = $cat['name']; |
---|
197 | if (isset($upper_ids)) { |
---|
198 | $nb=1; |
---|
199 | foreach ($upper_ids as $upper_cat) { |
---|
200 | $cat_info = get_cat_info($upper_cat); |
---|
201 | $catname[$nb++] = $cat_info['name']; |
---|
202 | } |
---|
203 | } |
---|
204 | } |
---|
205 | // Username or the current user |
---|
206 | $username = $user['username']; |
---|
207 | if (!$this->config['user_cats_case_sensitive']) |
---|
208 | $catname = array_map('strtolower', $catname); |
---|
209 | |
---|
210 | // author of the photo |
---|
211 | $query = 'SELECT author FROM '.IMAGES_TABLE.' WHERE id = '.$page['image_id'].' LIMIT 1;'; |
---|
212 | $result = pwg_query($query); |
---|
213 | if (isset($result)) { |
---|
214 | $img_infos = mysql_fetch_array($result); |
---|
215 | $authorname = $img_infos['author']; |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | if ($this->config['authorized_groups_users'] == 'granted' OR $this->config['authorized_groups_users'] == 'denied') { |
---|
220 | $user_groups = array(); |
---|
221 | $query = 'SELECT group_id FROM '.USER_GROUP_TABLE.' WHERE user_id='.$user['id'].';'; |
---|
222 | $result = pwg_query($query); |
---|
223 | while ($row = pwg_db_fetch_assoc($result)) { |
---|
224 | array_push($user_groups, $row['group_id']); |
---|
225 | } |
---|
226 | } |
---|
227 | |
---|
228 | // Only on available cats |
---|
229 | if (($this->config['authorized_cats'] == 'all') //Parameter : all |
---|
230 | OR ($this->config['authorized_cats'] == 'selected' AND !empty($upper_ids) AND (array_intersect($upper_ids, $this->config['selected_cats']) != array())) //Parameter : selected |
---|
231 | OR ($this->config['authorized_cats'] == 'user' AND $this->config['user_cats_case_sensitive'] AND (in_array($username, $catname) OR $username == $authorname)) //Parameter : user AND case sensitive |
---|
232 | OR ($this->config['authorized_cats'] == 'user' AND !$this->config['user_cats_case_sensitive'] AND (in_array(strtolower($username), $catname) OR $username == $authorname)) //Parameter : user AND not case sensitive |
---|
233 | ) { |
---|
234 | if (($this->config['authorized_groups_users'] == 'all') //Parameter : all |
---|
235 | OR ($this->config['authorized_groups_users'] == 'granted' AND ((array_intersect($user_groups, $this->config['selected_groups']) != array()) OR in_array($user['id'], $this->config['selected_users']))) //Parameter : granted |
---|
236 | OR ($this->config['authorized_groups_users'] == 'denied' AND ((array_intersect($user_groups, $this->config['selected_groups']) == array()) AND !in_array($user['id'], $this->config['selected_users']))) //Parameter : denied |
---|
237 | ) { |
---|
238 | // Check if user is guest. |
---|
239 | // In this case, force mail to default mail (in configuration) |
---|
240 | if (is_a_guest()) { |
---|
241 | if (!empty($this->config['default_guest_email'])) |
---|
242 | $user['email'] = $this->config['default_guest_email']; |
---|
243 | } |
---|
244 | |
---|
245 | // Template informations |
---|
246 | $template->assign('ecard', array( |
---|
247 | 'title' => l10n('Title'), |
---|
248 | 'message' => l10n('piwecard_message'), |
---|
249 | 'sender_name' => $user['username'], |
---|
250 | 'sender_email' => $user['email'], |
---|
251 | 'recipient_name' => l10n('piwecard_recipient_name'), |
---|
252 | 'recipient_email' => l10n('piwecard_recipient_email'), |
---|
253 | 'copy' => $this->config['sender_copy'] ? 'checked="checked"' : '', |
---|
254 | 'changemail' => (!isset($user['email']) OR $this->config['sender_email_change']) ? '' : 'disabled="disabled"', |
---|
255 | 'nb_max_recipients' => $this->config['nb_max_recipients'], |
---|
256 | ) |
---|
257 | ); |
---|
258 | |
---|
259 | // Template add for the active parameter choice by the user |
---|
260 | if ($this->config['validity_choice']) { |
---|
261 | foreach($this->config['validity'] as $validity) { |
---|
262 | $template->append('ecard_validity', array( |
---|
263 | 'id' => $validity, |
---|
264 | 'name' => ($validity == 0) ? l10n('piwecard_nolimit') : $validity.' '.l10n('piwecard_days'), |
---|
265 | 'selected' => ($this->config['validity_default'] == $validity ? 'checked' : '') |
---|
266 | ) |
---|
267 | ); |
---|
268 | } |
---|
269 | } else { |
---|
270 | $template->assign('ecard_validity_hidden', $this->config['validity_default']); |
---|
271 | } |
---|
272 | |
---|
273 | foreach ($this->config['email_format_authorized'] as $email_format) { |
---|
274 | $template->append('ecard_email_format', array( |
---|
275 | 'id' => $email_format, |
---|
276 | 'name' => l10n('piwecard_email_format_'.$email_format), |
---|
277 | 'selected' => (($this->config['email_format_default'] == $email_format) ? 'checked' : ''), |
---|
278 | ) |
---|
279 | ); |
---|
280 | } |
---|
281 | |
---|
282 | $template->set_filenames(array('ecard_template' => PIWECARD_ROOT.'/template/ecard.tpl')); |
---|
283 | $template->concat('COMMENT_IMG', $template->parse('ecard_template', true)); |
---|
284 | |
---|
285 | // Send the card |
---|
286 | if (isset($_POST['ecard_submit'])) { |
---|
287 | // If conf doesn't allow to modify the %yourmail% param, force it to user mail |
---|
288 | if (!$this->config['sender_email_change']) |
---|
289 | $_POST['ecard_sender_email'] = $user['email']; |
---|
290 | |
---|
291 | $email_format = $_POST['ecard_email_format']; |
---|
292 | |
---|
293 | //Check fields |
---|
294 | if ($_POST['ecard_sender_name'] == '' |
---|
295 | OR $_POST['ecard_title'] == '' |
---|
296 | OR $_POST['ecard_message'] == '') { |
---|
297 | return; |
---|
298 | } |
---|
299 | |
---|
300 | if ($_POST['ecard_sender_email'] == '' OR !$this->is_valid_email($_POST['ecard_sender_email'])) |
---|
301 | return; |
---|
302 | |
---|
303 | // Initialize the array for image element |
---|
304 | $image_element = array(); |
---|
305 | |
---|
306 | // Get all image informations |
---|
307 | $query = 'SELECT * FROM '.IMAGES_TABLE.' WHERE id='.$page['image_id'].' LIMIT 1;'; |
---|
308 | $result = pwg_query($query); |
---|
309 | if (isset($result)) |
---|
310 | $image_element = mysql_fetch_array($result); |
---|
311 | |
---|
312 | // Generate random number |
---|
313 | $next_element_id_random = $this->random(64); |
---|
314 | while (pwg_db_num_rows(pwg_query('SELECT ecard_id FROM '.PIWECARD_TABLE.' WHERE ecard_id="'.$next_element_id_random.'";')) != 0) { |
---|
315 | $next_element_id_random = $this->random(64); |
---|
316 | } |
---|
317 | $image_element['next_element_id'] = $next_element_id_random; |
---|
318 | |
---|
319 | // Image infos |
---|
320 | if ($this->config['show_image_infos']) { |
---|
321 | if (isset($image_element['name'])) { |
---|
322 | $image_element['picture_infos'] = $image_element['name']; |
---|
323 | if (isset($image_element['author'])) |
---|
324 | $image_element['picture_infos'] .= ' ('.$image_element['author'].')'; |
---|
325 | } |
---|
326 | } |
---|
327 | |
---|
328 | // Complete the image_element array with Link for the ecard url to be added in the mail |
---|
329 | set_make_full_url(); |
---|
330 | $ecard_url = embellish_url(get_absolute_root_url() . './index.php?/ecard/'.$image_element['next_element_id']); |
---|
331 | $image_element['ecard_url'] = $ecard_url; |
---|
332 | unset_make_full_url(); |
---|
333 | |
---|
334 | // Complete the image_element with the url to point to the image url |
---|
335 | set_make_full_url(); |
---|
336 | $image_element['picture_url'] = duplicate_picture_url( |
---|
337 | array( |
---|
338 | 'image_id' => $image_element['id'], |
---|
339 | 'image_file' => $image_element['file'] |
---|
340 | ), |
---|
341 | array('start') |
---|
342 | ); |
---|
343 | unset_make_full_url(); |
---|
344 | |
---|
345 | // Send the mail |
---|
346 | $recipient_infos = array_combine($_POST['ecard_recipient_name'], $_POST['ecard_recipient_email']); |
---|
347 | foreach ($recipient_infos as $recipient_name => $recipient_email) { |
---|
348 | if ($recipient_name == '' OR $recipient_email == '' OR !$this->is_valid_email($recipient_email)) |
---|
349 | continue; |
---|
350 | |
---|
351 | $parse_list = array( |
---|
352 | 'ecard_sender_name' => $_POST['ecard_sender_name'], |
---|
353 | 'ecard_sender_email' => $_POST['ecard_sender_email'], |
---|
354 | 'ecard_recipient_name' => $recipient_name, |
---|
355 | 'ecard_recipient_email' => $recipient_email, |
---|
356 | 'ecard_title' => $_POST['ecard_title'], |
---|
357 | 'ecard_message' => $_POST['ecard_message'], |
---|
358 | ); |
---|
359 | |
---|
360 | $email_infos = array( |
---|
361 | 'from_name' => $_POST['ecard_sender_name'], |
---|
362 | 'from_email' => (isset($_POST['ecard_sender_email']) ? $_POST['ecard_sender_email'] : $user['email']), |
---|
363 | 'to' => $recipient_email, |
---|
364 | 'subject' => htmlspecialchars_decode($this->parse($this->config['email_subject'], $parse_list, $image_element)), |
---|
365 | ); |
---|
366 | |
---|
367 | $email_message_text = stripslashes(strip_tags($this->parse($this->config['email_message']['text'], $parse_list, $image_element))); |
---|
368 | $email_message_html = stripslashes($this->parse($this->config['email_message']['html'], $parse_list, $image_element)); |
---|
369 | switch($email_format) { |
---|
370 | case 'text': // text |
---|
371 | $email_infos['message'] = array( |
---|
372 | 'text' => $email_message_text |
---|
373 | ); |
---|
374 | break; |
---|
375 | case 'html': // html |
---|
376 | $email_infos['message'] = array( |
---|
377 | 'text' => $email_message_text, |
---|
378 | 'html' => $email_message_html, |
---|
379 | ); |
---|
380 | default: |
---|
381 | break; |
---|
382 | } |
---|
383 | |
---|
384 | $this->mail($email_infos); |
---|
385 | |
---|
386 | // Add the copy to expe if param. |
---|
387 | if (isset($_POST['ecard_copy'])) // send copy to sender |
---|
388 | $email_infos['bcc'] = $email_infos['from_email']; |
---|
389 | |
---|
390 | //Insert into database |
---|
391 | $insert = array( |
---|
392 | 'ecard_id' => $image_element['next_element_id'], |
---|
393 | 'sender_name' => $_POST['ecard_sender_name'], |
---|
394 | 'recipient_name' => $recipient_name, |
---|
395 | 'sender_email' => $_POST['ecard_sender_email'], |
---|
396 | 'recipient_email' => $recipient_email, |
---|
397 | 'title' => $_POST['ecard_title'], |
---|
398 | 'message' => $_POST['ecard_message'], |
---|
399 | 'image' => $image_element['id'], |
---|
400 | 'date_creation' => date("Y-m-d H:i:s"), |
---|
401 | ); |
---|
402 | if ($_POST['ecard_validity'] != '0') { |
---|
403 | $date = new DateTime(); |
---|
404 | $date->modify("+".$_POST['ecard_validity']." day"); |
---|
405 | $insert['date_validity'] = $date->format('Y-m-d H:i:s'); |
---|
406 | } |
---|
407 | single_insert(PIWECARD_TABLE, $insert); |
---|
408 | } |
---|
409 | } |
---|
410 | } |
---|
411 | } |
---|
412 | } |
---|
413 | } |
---|
414 | |
---|
415 | //Send an email |
---|
416 | function mail($email_infos) { |
---|
417 | global $conf, $page, $lang_info; |
---|
418 | $template_mail = new Template(PIWECARD_MAIL_PATH.'template'); |
---|
419 | $smarty = $template_mail->smarty; |
---|
420 | |
---|
421 | $from = $email_infos['from_name']." <".$email_infos['from_email'].">"; |
---|
422 | $subject = encode_mime_header(trim(preg_replace('#[\n\r]+#s', '', $email_infos['subject']))); |
---|
423 | $boundary = '_----------='.md5(uniqid(mt_rand())); |
---|
424 | |
---|
425 | $headers = 'From: '.$from."\n"; |
---|
426 | $headers .= 'Reply-To: '.$from."\n"; |
---|
427 | if (!empty($email_infos['bcc'])) |
---|
428 | $headers .= 'Bcc: '.$email_infos['bcc']."\n"; |
---|
429 | $headers .= 'X-Sender: <'.get_absolute_root_url().'>'."\n"; |
---|
430 | $headers .= 'X-Mailer: Piwigo Mailer'."\n"; |
---|
431 | $headers .= 'X-auth-smtp-user: '.$from."\n"; |
---|
432 | $headers .= 'X-abuse-contact: '.$from."\n"; |
---|
433 | $headers .= 'Date: '.date("D, j M Y G:i:s O")."\n"; |
---|
434 | |
---|
435 | $message = ''; |
---|
436 | |
---|
437 | if (empty($email_infos['message']['html'])) { //Text plain email |
---|
438 | $headers .= 'Content-Transfer-Encoding: 8bit'."\n"; |
---|
439 | $message .= $email_infos['message']['text']; |
---|
440 | $smarty->assign(array( |
---|
441 | 'GALLERY_TITLE' => isset($page['gallery_title']) ? $page['gallery_title'] : $conf['gallery_title'], |
---|
442 | 'GALLERY_URL' => get_absolute_root_url(), |
---|
443 | 'MAIL' => get_webmaster_mail_address(), |
---|
444 | ) |
---|
445 | ); |
---|
446 | $message .= $smarty->fetch('mail_text.tpl'); |
---|
447 | $message = utf8_decode($message); |
---|
448 | } else { |
---|
449 | $headers .= 'MIME-Version: 1.0'."\n"; |
---|
450 | $headers .= 'Content-Type: multipart/alternative; boundary="'.$boundary.'"'; |
---|
451 | $message .= 'This is a multi-part message in MIME format'."\n\n"; |
---|
452 | $message .= '--'.$boundary."\n"; |
---|
453 | $message .= 'Content-Type: text/plain; charset="'.get_pwg_charset().'"'."\n"; |
---|
454 | $message .= 'Content-Transfer-Encoding: binary'."\n\n"; |
---|
455 | $message .= $email_infos['message']['text']."\n\n"; |
---|
456 | $message .= '--'.$boundary."\n"; |
---|
457 | $message .= 'Content-Type: text/html; charset="'.get_pwg_charset().'"'."\n"; |
---|
458 | $message .= 'Content-Transfer-Encoding: binary;'."\n\n"; |
---|
459 | $smarty->assign(array( |
---|
460 | 'CONTENT_ENCODING' => get_pwg_charset(), |
---|
461 | 'GALLERY_URL' => get_absolute_root_url(), |
---|
462 | 'GALLERY_TITLE' => isset($page['gallery_title']) ? $page['gallery_title'] : $conf['gallery_title'], |
---|
463 | 'VERSION' => $conf['show_version'] ? PHPWG_VERSION : '', |
---|
464 | 'MAIL' => get_webmaster_mail_address(), |
---|
465 | 'MESSAGE_HTML' => $email_infos['message']['html'], |
---|
466 | ) |
---|
467 | ); |
---|
468 | $message .= $smarty->fetch('mail_html.tpl'); |
---|
469 | $message .= "\n\n"; |
---|
470 | $message .= '--'.$boundary."--\n"; |
---|
471 | } |
---|
472 | |
---|
473 | mail($email_infos['to'], $subject, $message, $headers); |
---|
474 | } |
---|
475 | } |
---|
476 | ?> |
---|