source: extensions/Piwecard/include/ecard.class.php @ 19956

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

[piwecard] various improvements

  • Property svn:eol-style set to native
File size: 13.4 KB
Line 
1<?php
2global $user, $conf;
3
4class ecard {
5        var $my_config;
6        var $user_groups = array();
7       
8        // Class constructor
9        function __construct() {
10                $this->load_config();
11        }
12       
13        // Load general configuration from config_database
14        function load_config() {
15                $query = 'SELECT value FROM '.CONFIG_TABLE.' WHERE param="piwecard";';
16                $result = pwg_query($query);
17
18            if(isset($result)) {
19                        $row = mysql_fetch_row($result);
20                        if(is_string($row[0])) {
21                                $this->my_config = unserialize(($row[0]));
22                        }
23            }
24                $this->load_default_config();
25        }
26       
27        // Initialize default values of params
28        private function load_default_config() {
29            include ECARD_INC_PATH.'default_values.inc.php';
30            foreach ($ecard_default_values as $key => $value) {
31                    if (!isset($this->my_config[$key]))
32                                $this->my_config[$key] = $value;
33                }
34        }
35
36        // Save  general configuration to config_database
37        function save_config() {
38                $query = 'UPDATE '.CONFIG_TABLE.' SET value="'.serialize($this->my_config).'" WHERE param="piwecard";';
39                $result = pwg_query($query);
40
41                if($result) 
42                  return true;
43                else
44                  return false;
45        }
46       
47
48        // Retrieve user groups
49        function get_user_groups() {
50                global $user;
51               
52                $query = 'SELECT group_id FROM ' . USER_GROUP_TABLE . ' WHERE user_id=' . $user['id'] . ';';
53
54                $result = pwg_query($query);
55                while ($row = mysql_fetch_assoc($result)) {
56                        array_push($this->user_groups, $row['group_id']);
57                }
58        }
59
60
61        function section_init_ecard() {
62                global $tokens, $page;
63               
64                if ($tokens[0] == 'ecard')
65                        $page['section'] = 'ecard';
66        }
67       
68        function index_ecard() {
69                global $page;
70               
71                if (isset($page['section']) and $page['section'] == 'ecard') {
72                        include(ECARD_PATH . 'publish.php');
73                }
74        }
75
76        //Générer une chaine de caractère unique et aléatoire
77        private function random($car) {
78                $string = "";
79                $chaine = "abcdefghijklmnpqrstuvwxy0123456789";
80                srand((double)microtime()*1000000);
81                for($i=0; $i<$car; $i++) {
82                        $string .= $chaine[rand()%strlen($chaine)];
83                }
84                return $string;
85        }
86
87        // NB of days between 2 dates "AAAA-MM-JJ HH:hh:ss"
88        /* function NbJours($debut, $fin) {
89                $tDeb = explode("-", substr($debut,0,strpos($debut, ' ')));
90                $tFin = explode("-", substr($fin,0,strpos($fin, ' ')));
91
92                $diff = mktime(0, 0, 0, $tFin[1], $tFin[2], $tFin[0]) - mktime(0, 0, 0, $tDeb[1], $tDeb[2], $tDeb[0]);
93
94                return(($diff / 86400));
95        }
96
97        function AjoutJours($debut, $jours, $soustrait = false) {
98                $tDeb = explode("-", substr($debut,0,strpos($debut, ' ')));
99                $tDebH = explode(":", substr($debut,strpos($debut, ' ')+1));
100                $tFin = "";
101
102                $nb_ans = (int)(($jours)/365);
103                $nb_mois = (int)(( ($jours)%365) / 31);
104                $nb_jours = (int)(( ($jours)%365) % 31);               
105
106                if ($soustrait)
107                        $tFin = date("Y-m-d H:m:s", mktime($tDebH[0], $tDebH[1], $tDebH[2], $tDeb[1] - $nb_mois, $tDeb[2] - $nb_jours, $tDeb[0] - $nb_ans));
108                else
109                        $tFin = date("Y-m-d H:m:s", mktime($tDebH[0], $tDebH[1], $tDebH[2], $tDeb[1] + $nb_mois, $tDeb[2] + $nb_jours, $tDeb[0] + $nb_ans));
110
111                return($tFin);
112        } */
113       
114        function parse($data, $_POST = NULL, $image_element = NULL) {
115                include (ECARD_PATH.'include/config_param.inc.php');
116
117                $patterns = array();
118                $replacements = array();
119                foreach ($ecard_parse as $key => $value) {
120                        array_push($patterns, $key); 
121                        array_push($replacements, $value);
122                }
123
124                return str_replace($patterns, $replacements, $data);
125        }
126       
127        // Get the number of ecard in the database
128        function get_nb_ecard() {
129                $query = 'SELECT COUNT(DISTINCT id) AS nb FROM '.ECARD_TABLE.' ORDER BY date_creation;';
130                $result = pwg_query($query);
131               
132                if ($result) {
133                        $nb=mysql_fetch_assoc($result);
134                        return $nb['nb'];
135                } else 
136                        return 0;
137        }
138
139        // Get the number of valid ecard in the database
140        function get_nb_valid_ecard() {
141                $query = 'SELECT COUNT(DISTINCT id) AS nb FROM '.ECARD_TABLE.' WHERE date_validity IS NULL OR date_validity > NOW();';
142                $result = pwg_query($query);
143               
144                if ($result) {
145                        $nb=mysql_fetch_assoc($result);
146                        return $nb['nb'];
147                } else 
148                        return 0;
149        }
150       
151       
152        // Get ecard information into array
153        function get_ecard($ecard_id = null) {
154                if ($ecard_id!== null) {
155                        $query = 'SELECT * FROM ' . ECARD_TABLE .' WHERE id ="' . $ecard_id . '" LIMIT 1;';
156
157                        $result = pwg_query($query);
158                        if ($result)
159                                return  mysql_fetch_assoc($result);
160                        else 
161                                return false;
162                }
163        }
164
165        function is_valid($ecard_id = null) {
166                if (isset($ecard_id)) {
167                        $ecard_info = $this->get_ecard($ecard_id);
168                        if (isset($ecard_info)) {
169                                // Valid duration for an ecard
170                                $date_validity = $ecard_info['date_validity'];
171                               
172                                if (isset($date_validity)) {
173                                        $now = new DateTime(date("Y-m-d H:i:s")); 
174                                        $date_validity = new DateTime($date_validity); 
175                                        if ($date_validity > $now)
176                                                return true;
177                                        else
178                                                return false;
179                                } else {
180                                        return true;
181                                }
182                        } else
183                                return false;
184                } else {
185                        return false;
186                }
187        }
188       
189        // delete one ecard
190        // force to delete valid ecard
191        function delete_ecard($ecard_id = null) {
192                if (isset($ecard_id)) {
193                        $query = 'DELETE FROM ' . ECARD_TABLE .' WHERE id="' . $ecard_id  . '";';
194                        pwg_query($query);
195                } else
196                        return false;
197        }
198       
199        // Delete all invalid ecard
200        function delete_allinvalid_ecard() {
201                $query = 'DELETE FROM ' . ECARD_TABLE .' WHERE date_validity < NOW();';
202                pwg_query($query);
203        }
204       
205        // Add tpl to picture.php page to display ecard informations
206        function display_ecard_to_picture() {
207                global $page, $user, $template;
208               
209                // Init user groups
210                $this->get_user_groups();
211
212                // Only on category page!
213                if (isset($page['section'])) {
214                        $upper_names = null;
215                       
216                        if (!empty($page['category'])) {
217                                // Gets all upper categories from the image category to test
218                                //      - if the parameter for "recursive" is OK
219                                //      - if the upper category is activated for this function
220                                $query = 'SELECT *
221                                                        FROM '.CATEGORIES_TABLE.'
222                                                        WHERE id = '.pwg_db_real_escape_string($page['category']['id']).'
223                                                        ;';
224                                $cat = mysql_fetch_assoc(pwg_query($query));
225                               
226                                if (empty($cat)) {
227                                        $upper_ids = null;
228                                } else {
229                                        foreach ($cat as $k => $v) {
230                                                // If the field is true or false, the variable is transformed into a
231                                                // boolean value.
232                                                if ($cat[$k] == 'true' or $cat[$k] == 'false') {
233                                                  $cat[$k] = get_boolean($cat[$k]);
234                                                }
235                                        }
236                                        $upper_ids = explode(',', $cat['uppercats']);
237                                }
238                        }
239                       
240                        if ($this->my_config['user_cat']) {     // !Function only allowed on user image
241                                // Check the category name, user name et img author
242                                // Get all name for upper categories and current category
243                                if (isset($cat) and !empty($cat)) {
244                                        $catname[0] = $cat['name'];
245                                        if (isset($upper_ids) and $upper_ids != null) {
246                                                $nb=1;
247                                                foreach ($upper_ids as $upper_cat) {
248                                                        // Get upper cat info and store the name
249                                                        $cat_info = get_cat_info($upper_cat);
250                                                        $catname[$nb++] = $cat_info['name'];
251                                                }
252                                        }
253                                }
254                                // Username or the current user
255                                $username = $user['username'];
256                               
257                                // author of the photo
258                                $query = 'SELECT author FROM '.IMAGES_TABLE.' WHERE id = '.$page['image_id'].' LIMIT 1;';
259                                $result = pwg_query($query);
260                                if (isset($result)) {
261                                        $img_infos = mysql_fetch_array($result);
262                                        $authorname = $img_infos['author'];
263                                }
264                        }               
265               
266                        // Only on available cats
267                        if ($this->my_config['allcats']         // Available on all cats
268                                OR (!empty($page['category']) AND in_array($page['category']['id'], $this->my_config['cats']))  // Available on specific cats
269                                OR ($this->my_config['recursive'] AND isset($upper_ids) AND                             // Available on upper cats this recursiv mode
270                                                (array_intersect($upper_ids, $this->my_config['cats']) != array()))
271                                OR      // Available based on usename
272                                        ($this->my_config['user_cat']
273                                                AND (in_array($username, $catname)      // Available user categories
274                                                                OR $username == $authorname)
275
276                                        ) 
277                                )
278                        {
279                        // And only available groups
280                                if (empty($this->my_config['groups']) 
281                                        OR (!empty($this->my_config['groups'])
282                                                AND (array_intersect($this->user_groups, $this->my_config['groups']) != array())) 
283                                        )
284                                {
285                               
286                                // Check if user is guest.
287                                // In this case, force mail to default mail (in params)
288                                if (is_a_guest()) {
289                                        if (!empty($this->my_config['defaultmail']))
290                                                $user['email'] = $this->my_config['defaultmail'];
291                                }
292                       
293                                // Template informations
294                                $template->assign('ecard', array(
295                                                'subject'                       => l10n('ecard_send_title'),
296                                                'message'                       => l10n('ecard_send_message'),
297                                                'sender_name'           => $user['username'],
298                                                'sender_email'          => $user['email'],
299                                                'recipient_name'        => l10n('ecard_send_dest_name'),
300                                                'recipient_email'       => l10n('ecard_send_dest_mail'),
301                                                'copy'                          => $this->my_config['send_copy'] ? 'checked="checked"' : '',
302                                                'changemail'            => ($this->my_config['expmail_change'] ? '' : 'disabled="disabled"')
303                                                ));
304
305                                // Template add for the active parameter choice by the user
306                                if ($this->my_config['active_parameter']) {     // Allowed for the user
307                                        $template->append('ecard_validity',array('id' => 0, 'name' => l10n('ecard_nolimit'), 'selected' => ($this->my_config['activ'] == 0 ? 'checked' : '')));
308                                        for($jj=5; $jj < 30; $jj+=5)
309                                                $template->append('ecard_validity',array('id' => $jj, 'name' => $jj , 'selected' => ($this->my_config['activ'] == $jj ? 'checked' : '')));
310                                }
311                                                       
312                                // Template add for the send method to be chose, by the user
313                                // default : text
314                                $template->append('ecard_send_method',array('id' => 0, 'name' => l10n('ecard_maillink'), 'selected' => ($this->my_config['send_HTML'] ?  '' : 'checked')));
315                                if ($this->my_config['send_HTML']) {    // Allowed for the user
316                                        $template->append('ecard_send_method',array('id' => 1, 'name' => l10n('ecard_mailhtml'), 'selected' => ($this->my_config['send_HTML'] ? 'checked' : '')));
317                                }
318
319                                // Send the card
320                                if (isset($_POST['ecard_submit'])) {
321                                        $send_method = $_POST['ecard_send_method'];
322                                       
323                                        // If conf doesn't allow to modify the %votremail param, force it to user mail
324                                        if (!isset($_POST['ecard_sender_email']))
325                                                $_POST['ecard_sender_email'] = $user['email'];
326                                       
327                                        // Initialize the array for image element
328                                        $image_element = array();
329
330                                        // Get all image informations
331                                        $query = 'SELECT * FROM '.IMAGES_TABLE.' WHERE id='.$page['image_id'].' LIMIT 1;';
332                                        $result = pwg_query($query);
333                                        if (isset($result))
334                                                $image_element = mysql_fetch_array($result);
335                                       
336                                        // Generate random number
337                                        $image_element['next_element_id']  = $this->random(64);
338
339                                        // Image infos
340                                        if ($this->my_config['ecard_showinfos']) {
341                                                if (isset($image_element['name'])) {
342                                                        $image_element['picture_infos'] = $image_element['name'];
343                                                        if (isset($image_element['author']))
344                                                                $image_element['picture_infos'] .= ' ('.$image_element['author'].')';
345                                                }
346                                        }
347
348                                        $insert = array(
349                                                                'id'                            => $image_element['next_element_id'],
350                                                                'sender_name'           => $_POST['ecard_sender_name'],
351                                                                'recipient_name'        => $_POST['ecard_recipient_name'],
352                                                                'sender_email'          => $_POST['ecard_sender_email'],
353                                                                'recipient_email'       => $_POST['ecard_recipient_email'],
354                                                                'subject'                       => $_POST['ecard_subject'],
355                                                                'message'                       => $_POST['ecard_message'],
356                                                                'image'                         => $image_element['id'],
357                                                                'date_creation'         => date("Y-m-d H:i:s"),
358                                        );
359                                        if ($_POST['ecard_validity'] != '0') {
360                                                $date = new DateTime();
361                                                $date->modify("+".$_POST['ecard_validity']." day");
362                                                $insert['date_validity'] = $date->format('Y-m-d H:i:s');
363                                        }
364                                        single_insert(ECARD_TABLE, $insert);
365
366                                        // Complete the image_element array with Link for the ecard url to be added in the mail
367                                        set_make_full_url();
368                                        $ecard_url = embellish_url(get_absolute_root_url() . './index.php?/ecard/'.$image_element['next_element_id']);
369                                        $image_element['ecard_url'] = $ecard_url;
370                                        unset_make_full_url();
371
372                                        // Complete the image_element with the url to point to the image url
373                                        set_make_full_url();
374                                        $image_element['picture_url'] = duplicate_picture_url(
375                                                array(
376                                                        'image_id' => $image_element['id'],
377                                                        'image_file' => $image_element['file']
378                                                ),
379                                                array('start')
380                                        );
381                                        unset_make_full_url();
382
383                                        $mail_subject = htmlspecialchars_decode($this->parse( $this->my_config['subject_link'], $_POST));
384                                       
385                                        switch($send_method) {
386                                        case 0 : // text
387                                                // Get the standard message (in admin param) and parse it with the informations
388                                                $mail_message =  stripslashes(htmlspecialchars_decode($this->parse($this->my_config['message_link'], $_POST, $image_element)));
389                                                $mail_arg=array('from' => $_POST['ecard_sender_email'],
390                                                                                'subject' => $mail_subject,
391                                                                                'content' => $mail_message,
392                                                                                'content_format' => "text/plain",
393                                                                                'email_format' => "text/html"
394                                                                                );
395                                       
396                                                break;
397                                        case 1 : // html
398                                                $mail_message_HTML =  stripslashes(htmlspecialchars_decode($this->parse($this->my_config['message_HTML'], $_POST, $image_element)));
399                                                $mail_arg=array('from' => $_POST['ecard_sender_email'],
400                                                                                'subject' => $mail_subject,
401                                                                                'content' => $mail_message_HTML,
402                                                                                'content_format' => "text/html",
403                                                                                'email_format' => "text/html"
404                                                                                );
405                                                break;
406                                        }
407                                       
408                                        // Add the copy to expe if param.
409                                        if (isset($_POST['ecard_copy']))        // send copy to sender
410                                                $mail_arg['Bcc'] = array((isset($_POST['ecard_sender_email']) ? $_POST['ecard_sender_email'] : $user['email']));
411                                       
412                                        // Send the mail
413                                        pwg_mail($_POST['ecard_recipient_email'], $mail_arg);
414                                }
415                               
416                                $template->set_filenames(array('ecard_template' =>  ECARD_ROOT.'/template/ecard.tpl'));
417                                $template->concat('COMMENT_IMG', $template->parse('ecard_template', true));
418                                }
419                        }
420                }
421        }
422}
423?>
Note: See TracBrowser for help on using the repository browser.