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

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

[piwecard] Use of colorbox (better integration with theme). Add of some JS testing. Replace table by div/span

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