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

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

[piwecard] version 2.4.a.b2

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