source: extensions/Piwecard/include/piwecard.class.php @ 20180

Last change on this file since 20180 was 20180, checked in by julien1311, 11 years ago

[piwecard] several improvements

  • Property svn:eol-style set to native
File size: 14.2 KB
Line 
1<?php
2global $user, $conf;
3
4class 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        // Save  general configuration to config_database
37        function set_config() {
38                conf_update_param('piwecard', pwg_db_real_escape_string(serialize($this->config)));
39        }
40       
41        function section_init_ecard() {
42                global $tokens, $page;
43               
44                if ($tokens[0] == 'ecard')
45                        $page['section'] = 'ecard';
46        }
47       
48        function index_ecard() {
49                global $page;
50               
51                if (isset($page['section']) and $page['section'] == 'ecard') {
52                        include('publish.inc.php');
53                }
54        }
55
56        //Générer une chaine de caractère unique et aléatoire
57        private function random($car) {
58                $string = "";
59                $chaine = "abcdefghijklmnpqrstuvwxy0123456789";
60                srand((double)microtime()*1000000);
61                for($i=0; $i<$car; $i++) {
62                        $string .= $chaine[rand()%strlen($chaine)];
63                }
64                return $string;
65        }
66       
67        function parse($data, $values, $image_element) {
68                include (PIWECARD_PATH.'include/config_param.inc.php');
69
70                $patterns = array();
71                $replacements = array();
72                foreach ($ecard_parse as $key => $value) {
73                        array_push($patterns, $key); 
74                        array_push($replacements, $value);
75                }
76
77                return str_replace($patterns, $replacements, $data);
78        }
79       
80        // Get the number of ecard in the database
81        function get_nb_ecard() {
82                $query = 'SELECT COUNT(DISTINCT id) AS nb FROM '.PIWECARD_TABLE.' ORDER BY date_creation;';
83                $result = pwg_query($query);
84               
85                if ($result) {
86                        $nb=pwg_db_fetch_assoc($result);
87                        return $nb['nb'];
88                } else 
89                        return 0;
90        }
91
92        // Get the number of valid ecard in the database
93        function get_nb_valid_ecard() {
94                $query = 'SELECT COUNT(DISTINCT id) AS nb FROM '.PIWECARD_TABLE.' WHERE date_validity IS NULL OR date_validity > NOW();';
95                $result = pwg_query($query);
96               
97                if ($result) {
98                        $nb=pwg_db_fetch_assoc($result);
99                        return $nb['nb'];
100                } else 
101                        return 0;
102        }
103       
104       
105        // Get ecard information into array
106        function get_ecard($ecard_id = null) {
107                if ($ecard_id!== null) {
108                        $query = 'SELECT * FROM ' . PIWECARD_TABLE .' WHERE id ="' . $ecard_id . '" LIMIT 1;';
109
110                        $result = pwg_query($query);
111                        if ($result)
112                                return  pwg_db_fetch_assoc($result);
113                        else 
114                                return false;
115                }
116        }
117
118        function is_valid($ecard_id = null) {
119                if (isset($ecard_id)) {
120                        $ecard_info = $this->get_ecard($ecard_id);
121                        if (isset($ecard_info)) {
122                                // Valid duration for an ecard
123                                $date_validity = $ecard_info['date_validity'];
124                               
125                                if (isset($date_validity)) {
126                                        $now = new DateTime(date("Y-m-d H:i:s")); 
127                                        $date_validity = new DateTime($date_validity); 
128                                        if ($date_validity > $now)
129                                                return true;
130                                        else
131                                                return false;
132                                } else {
133                                        return true;
134                                }
135                        } else
136                                return false;
137                } else {
138                        return false;
139                }
140        }
141       
142        // delete one ecard
143        // force to delete valid ecard
144        function delete_ecard($ecard_id = null) {
145                if (isset($ecard_id)) {
146                        $query = 'DELETE FROM ' . PIWECARD_TABLE .' WHERE id="' . $ecard_id  . '";';
147                        pwg_query($query);
148                } else
149                        return false;
150        }
151       
152        // Delete all invalid ecard
153        function delete_allinvalid_ecard() {
154                $query = 'DELETE FROM ' . PIWECARD_TABLE .' WHERE date_validity < NOW();';
155                pwg_query($query);
156        }
157       
158        function is_valid_email($email_address) {
159                $syntax = '#^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,6}$#';
160               
161                if(preg_match($syntax, $email_address))
162                        return true;
163                else 
164                        return false;
165        }
166       
167        // Add tpl to picture.php page to display ecard informations
168        function display_ecard_to_picture() {
169                global $page, $user, $template;
170
171                // Only on category page!
172                if (isset($page['section'])) {
173                        $upper_names = null;
174                       
175                        if (!empty($page['category'])) {
176                                // Gets all upper categories from the image category to test
177                                //      - if the upper category is activated for this function
178                                $query = 'SELECT * FROM '.CATEGORIES_TABLE.' WHERE id = '.pwg_db_real_escape_string($page['category']['id']).';';
179                                $cat = pwg_db_fetch_assoc(pwg_query($query));
180                               
181                                if (empty($cat)) {
182                                        $upper_ids = null;
183                                } else {
184                                        $upper_ids = explode(',', $cat['uppercats']);
185                                }
186                        }
187                       
188                        if ($this->config['authorized_cats'] == 'user') {       // !Function only allowed on user image
189                                if (isset($cat) and !empty($cat)) {
190                                        $catname[0] = $cat['name'];
191                                        if (isset($upper_ids)) {
192                                                $nb=1;
193                                                foreach ($upper_ids as $upper_cat) {
194                                                        $cat_info = get_cat_info($upper_cat);
195                                                        $catname[$nb++] = $cat_info['name'];
196                                                }
197                                        }
198                                }
199                                // Username or the current user
200                                $username = $user['username'];
201                                if (!$this->config['user_cats_case_sensitive'])
202                                        $catname = array_map('strtolower', $catname);
203                               
204                                // author of the photo
205                                $query = 'SELECT author FROM '.IMAGES_TABLE.' WHERE id = '.$page['image_id'].' LIMIT 1;';
206                                $result = pwg_query($query);
207                                if (isset($result)) {
208                                        $img_infos = mysql_fetch_array($result);
209                                        $authorname = $img_infos['author'];
210                                }
211                        }
212                       
213                        if ($this->config['authorized_groups_users'] == 'granted' OR $this->config['authorized_groups_users'] == 'denied') {
214                                $user_groups = array();
215                                $query = 'SELECT group_id FROM '.USER_GROUP_TABLE.' WHERE user_id='.$user['id'].';';
216                                $result = pwg_query($query);
217                                while ($row = pwg_db_fetch_assoc($result)) {
218                                        array_push($user_groups, $row['group_id']);
219                                }
220                        }
221                       
222                        // Only on available cats
223                        if (($this->config['authorized_cats'] == 'all')         //Parameter : all
224                                OR ($this->config['authorized_cats'] == 'selected' AND !empty($upper_ids) AND (array_intersect($upper_ids, $this->config['selected_cats']) != array())) //Parameter : selected
225                                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
226                                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
227                        ) {
228                                if (($this->config['authorized_groups_users'] == 'all')         //Parameter : all
229                                        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
230                                        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
231                                ) {
232                                        // Check if user is guest.
233                                        // In this case, force mail to default mail (in configuration)
234                                        if (is_a_guest()) {
235                                                if (!empty($this->config['default_guest_email']))
236                                                        $user['email'] = $this->config['default_guest_email'];
237                                        }
238                               
239                                        // Template informations
240                                        $template->assign('ecard', array(
241                                                        'title'                         => l10n('Title'),
242                                                        'message'                       => l10n('piwecard_message'),
243                                                        'sender_name'           => $user['username'],
244                                                        'sender_email'          => $user['email'],
245                                                        'recipient_name'        => l10n('piwecard_recipient_name'),
246                                                        'recipient_email'       => l10n('piwecard_recipient_email'),
247                                                        'copy'                          => $this->config['sender_copy'] ? 'checked="checked"' : '',
248                                                        'changemail'            => (!isset($user['email']) OR $this->config['sender_email_change']) ? '' : 'disabled="disabled"'
249                                                        )
250                                        );
251
252                                        // Template add for the active parameter choice by the user
253                                        if ($this->config['validity_choice']) {
254                                                foreach($this->config['validity'] as $validity) {
255                                                        $template->append('ecard_validity', array(
256                                                                                                                                                'id' => $validity,
257                                                                                                                                                'name' => ($validity == 0) ? l10n('piwecard_nolimit') : $validity.' '.l10n('piwecard_days'),
258                                                                                                                                                'selected' => ($this->config['validity_default'] == $validity ? 'checked' : '')
259                                                                                                                                        )
260                                                        );
261                                                }
262                                        } else {
263                                                $template->assign('ecard_validity_hidden', $this->config['validity_default']);
264                                        }
265
266                                        foreach ($this->config['email_format_authorized'] as $email_format) {
267                                                $template->append('ecard_email_format', array(
268                                                                                                                                        'id' => $email_format,
269                                                                                                                                        'name' => l10n('piwecard_email_format_'.$email_format),
270                                                                                                                                        'selected' => (($this->config['email_format_default'] == $email_format) ?  'checked' : ''),
271                                                                                                                                )
272                                                );
273                                        }
274                                       
275                                        $template->set_filenames(array('ecard_template' =>  PIWECARD_ROOT.'/template/ecard.tpl'));
276                                        $template->concat('COMMENT_IMG', $template->parse('ecard_template', true));
277
278                                        // Send the card
279                                        if (isset($_POST['ecard_submit'])) {
280                                                // If conf doesn't allow to modify the %yourmail% param, force it to user mail
281                                                if (!$this->config['sender_email_change'])
282                                                        $_POST['ecard_sender_email'] = $user['email'];
283                                               
284                                                $email_format = $_POST['ecard_email_format'];
285                                               
286                                                //Check fields
287                                                if ($_POST['ecard_sender_name'] == ''
288                                                        OR $_POST['ecard_title'] == ''
289                                                        OR $_POST['ecard_message'] == '') {
290                                                        return;
291                                                }
292                                               
293                                                if ($_POST['ecard_sender_email'] == '' OR !$this->is_valid_email($_POST['ecard_sender_email']))
294                                                        return;
295                                               
296                                                foreach ($_POST['ecard_recipient_name'] as $recipient)
297                                                        if ($recipient == '')
298                                                                return;
299                                               
300                                                foreach ($_POST['ecard_recipient_email'] as $recipient)
301                                                if ($recipient == '' OR !$this->is_valid_email($recipient)) {
302                                                        return;
303                                                }
304                                               
305                                                // Initialize the array for image element
306                                                $image_element = array();
307
308                                                // Get all image informations
309                                                $query = 'SELECT * FROM '.IMAGES_TABLE.' WHERE id='.$page['image_id'].' LIMIT 1;';
310                                                $result = pwg_query($query);
311                                                if (isset($result))
312                                                        $image_element = mysql_fetch_array($result);
313                                               
314                                                // Generate random number
315                                                $next_element_id_random  = $this->random(64);
316                                                while (pwg_db_num_rows(pwg_query('SELECT id FROM '.PIWECARD_TABLE.' WHERE id="'.$next_element_id_random.'";')) != 0) {
317                                                        $next_element_id_random  = $this->random(64);
318                                                }
319                                                $image_element['next_element_id']  = $next_element_id_random;
320
321                                                // Image infos
322                                                if ($this->config['show_image_infos']) {
323                                                        if (isset($image_element['name'])) {
324                                                                $image_element['picture_infos'] = $image_element['name'];
325                                                                if (isset($image_element['author']))
326                                                                        $image_element['picture_infos'] .= ' ('.$image_element['author'].')';
327                                                        }
328                                                }
329                                               
330                                                // Complete the image_element array with Link for the ecard url to be added in the mail
331                                                set_make_full_url();
332                                                $ecard_url = embellish_url(get_absolute_root_url() . './index.php?/ecard/'.$image_element['next_element_id']);
333                                                $image_element['ecard_url'] = $ecard_url;
334                                                unset_make_full_url();
335                                               
336                                                // Complete the image_element with the url to point to the image url
337                                                set_make_full_url();
338                                                $image_element['picture_url'] = duplicate_picture_url(
339                                                        array(
340                                                                'image_id' => $image_element['id'],
341                                                                'image_file' => $image_element['file']
342                                                        ),
343                                                        array('start')
344                                                );
345                                                unset_make_full_url();
346                                               
347                                                // Send the mail
348                                                $recipient_infos = array_combine($_POST['ecard_recipient_name'], $_POST['ecard_recipient_email']);
349                                                foreach ($recipient_infos as $recipient_name => $recipient_email) {
350                                                        $parse_list = array(
351                                                                                                'ecard_sender_name' => $_POST['ecard_sender_name'],
352                                                                                                'ecard_sender_email' => $_POST['ecard_sender_email'],
353                                                                                                'ecard_recipient_name' => $recipient_name,
354                                                                                                'ecard_recipient_email' => $recipient_email,
355                                                                                                'ecard_title' => $_POST['ecard_title'],
356                                                                                                'ecard_message' => $_POST['ecard_message'],
357                                                        );
358                                               
359                                                        $email_subject = htmlspecialchars_decode($this->parse($this->config['email_subject'], $parse_list, $image_element));
360                                                        $email_message = htmlspecialchars_decode($this->parse($this->config['email_message'][$email_format], $parse_list, $image_element));
361                                                        $email_arg=array(       'from'                          => $_POST['ecard_sender_email'],
362                                                                                                'subject'                       => $email_subject,
363                                                                                        );
364                                                       
365                                                        switch($email_format) {
366                                                                case 'text': // text
367                                                                        $email_arg = array_merge($email_arg, array(
368                                                                                                                                                                'content'                       => $email_message,
369                                                                                                                                                                'content_format'        => "text/html",
370                                                                                                                                                                'email_format'          => "text/plain"
371                                                                                                                                                        )
372                                                                        );
373                                                                        break;
374                                                                case 'html': // html
375                                                                        $email_arg = array_merge($email_arg, array(
376                                                                                                                                                                'content'                       => '<html><head><title>'.$email_subject.'</title></head><body>'.$email_message.'</body></html>',
377                                                                                                                                                                'content_format'        => "text/html",
378                                                                                                                                                                'email_format'          => array("text/plain", "text/html"),
379                                                                                                                                                        )
380                                                                        );
381                                                                default:
382                                                                        break;
383                                                        }
384                                                       
385                                                        // Add the copy to expe if param.
386                                                        if (isset($_POST['ecard_copy']))        // send copy to sender
387                                                                $email_arg['Bcc'] = array((isset($_POST['ecard_sender_email']) ? $_POST['ecard_sender_email'] : $user['email']));
388                                                       
389                                                        pwg_mail($recipient_email, $email_arg);
390                                                       
391                                                        $insert = array(
392                                                                                '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?>
Note: See TracBrowser for help on using the repository browser.