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

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

[piwecard] some updates (almost 2.4.a.b2)

  • Property svn:eol-style set to native
File size: 11.2 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                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->my_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                               
202                                // author of the photo
203                                $query = 'SELECT author FROM '.IMAGES_TABLE.' WHERE id = '.$page['image_id'].' LIMIT 1;';
204                                $result = pwg_query($query);
205                                if (isset($result)) {
206                                        $img_infos = mysql_fetch_array($result);
207                                        $authorname = $img_infos['author'];
208                                }
209                        }
210                       
211                        // Only on available cats
212                        if (($this->my_config['authorized_cats'] == 'all')              //Parameter : all
213                                OR ($this->my_config['authorized_cats'] == 'selected' AND !empty($upper_ids) AND (array_intersect($upper_ids, $this->my_config['selected_cats']) != array())) //Parameter : selected
214                                OR      ($this->my_config['authorized_cats'] == 'user' AND (in_array($username, $catname) OR $username == $authorname)) //Parameter : user
215                        ) {
216                                // Check if user is guest.
217                                // In this case, force mail to default mail (in configuration)
218                                if (is_a_guest()) {
219                                        if (!empty($this->my_config['default_guest_email']))
220                                                $user['email'] = $this->my_config['default_guest_email'];
221                                }
222                       
223                                // Template informations
224                                $template->assign('ecard', array(
225                                                'title'                         => l10n('Title'),
226                                                'message'                       => l10n('piwecard_message'),
227                                                'sender_name'           => $user['username'],
228                                                'sender_email'          => $user['email'],
229                                                'recipient_name'        => l10n('piwecard_recipient_name'),
230                                                'recipient_email'       => l10n('piwecard_recipient_email'),
231                                                'copy'                          => $this->my_config['sender_copy'] ? 'checked="checked"' : '',
232                                                'changemail'            => (!isset($user['email']) OR $this->my_config['sender_email_change']) ? '' : 'disabled="disabled"'
233                                                )
234                                );
235
236                                // Template add for the active parameter choice by the user
237                                if ($this->my_config['validity_choice']) {
238                                        foreach($this->my_config['validity'] as $validity) {
239                                                $template->append('ecard_validity', array(
240                                                                                                                                        'id' => $validity,
241                                                                                                                                        'name' => ($validity == 0) ? l10n('piwecard_nolimit') : $validity.' '.l10n('piwecard_days'),
242                                                                                                                                        'selected' => ($this->my_config['validity_default'] == $validity ? 'checked' : '')
243                                                                                                                                )
244                                                );
245                                        }
246                                }
247                               
248                                foreach ($this->my_config['email_format_authorized'] as $email_format) {
249                                        $template->append('ecard_email_format', array(
250                                                                                                                                'id' => $email_format,
251                                                                                                                                'name' => l10n('piwecard_email_format_'.$email_format),
252                                                                                                                                'selected' => (($this->my_config['email_format_default'] == $email_format) ?  '' : 'checked'),
253                                                                                                                        )
254                                        );
255                                }
256
257                                // Send the card
258                                if (isset($_POST['ecard_submit'])) {
259                                        $email_format = $_POST['ecard_email_format'];
260                                       
261                                        // If conf doesn't allow to modify the %yourmail% param, force it to user mail
262                                        if (!$this->my_config['sender_email_change'])
263                                                $_POST['ecard_sender_email'] = $user['email'];
264                                       
265                                        // Initialize the array for image element
266                                        $image_element = array();
267
268                                        // Get all image informations
269                                        $query = 'SELECT * FROM '.IMAGES_TABLE.' WHERE id='.$page['image_id'].' LIMIT 1;';
270                                        $result = pwg_query($query);
271                                        if (isset($result))
272                                                $image_element = mysql_fetch_array($result);
273                                       
274                                        // Generate random number
275                                        $image_element['next_element_id']  = $this->random(64);
276
277                                        // Image infos
278                                        if ($this->my_config['show_image_infos']) {
279                                                if (isset($image_element['name'])) {
280                                                        $image_element['picture_infos'] = $image_element['name'];
281                                                        if (isset($image_element['author']))
282                                                                $image_element['picture_infos'] .= ' ('.$image_element['author'].')';
283                                                }
284                                        }
285
286                                        $insert = array(
287                                                                'id'                            => $image_element['next_element_id'],
288                                                                'sender_name'           => $_POST['ecard_sender_name'],
289                                                                'recipient_name'        => $_POST['ecard_recipient_name'],
290                                                                'sender_email'          => $_POST['ecard_sender_email'],
291                                                                'recipient_email'       => $_POST['ecard_recipient_email'],
292                                                                'title'                         => $_POST['ecard_title'],
293                                                                'message'                       => $_POST['ecard_message'],
294                                                                'image'                         => $image_element['id'],
295                                                                'date_creation'         => date("Y-m-d H:i:s"),
296                                        );
297                                        if ($_POST['ecard_validity'] != '0') {
298                                                $date = new DateTime();
299                                                $date->modify("+".$_POST['ecard_validity']." day");
300                                                $insert['date_validity'] = $date->format('Y-m-d H:i:s');
301                                        }
302                                        single_insert(PIWECARD_TABLE, $insert);
303                                       
304                                        // Complete the image_element array with Link for the ecard url to be added in the mail
305                                        set_make_full_url();
306                                        $ecard_url = embellish_url(get_absolute_root_url() . './index.php?/ecard/'.$image_element['next_element_id']);
307                                        $image_element['ecard_url'] = $ecard_url;
308                                        unset_make_full_url();
309                                       
310                                        // Complete the image_element with the url to point to the image url
311                                        set_make_full_url();
312                                        $image_element['picture_url'] = duplicate_picture_url(
313                                                array(
314                                                        'image_id' => $image_element['id'],
315                                                        'image_file' => $image_element['file']
316                                                ),
317                                                array('start')
318                                        );
319                                        unset_make_full_url();
320
321                                        $email_subject = htmlspecialchars_decode($this->parse( $this->my_config['email_subject'], $_POST));
322                                        $email_message = htmlspecialchars_decode($this->parse($this->my_config['email_message'][$email_format], $_POST, $image_element));
323                                        $email_arg=array(       'from' => $_POST['ecard_sender_email'],
324                                                                                'subject'                       => $email_subject,
325                                                                                'content'                       => $email_message,
326                                                                        );
327                                                                       
328                                        switch($email_format) {
329                                                case 'text': // text
330                                                        $email_arg = array_merge($email_arg, array(
331                                                                                                                                                'content_format'        => "text/plain",
332                                                                                                                                                'email_format'          => "text/plain"
333                                                                                                                                        )
334                                                        );
335                                                        break;
336                                                case 'html': // html
337                                                        $email_arg = array_merge($email_arg, array(
338                                                                                                                                                'content_format'        => "text/html",
339                                                                                                                                                'email_format'          => "text/html"
340                                                                                                                                        )
341                                                        );
342                                                default:
343                                                        break;
344                                        }
345                                       
346                                        // Add the copy to expe if param.
347                                        if (isset($_POST['ecard_copy']))        // send copy to sender
348                                                $email_arg['Bcc'] = array((isset($_POST['ecard_sender_email']) ? $_POST['ecard_sender_email'] : $user['email']));
349                                       
350                                        // Send the mail
351                                        pwg_mail($_POST['ecard_recipient_email'], $email_arg);
352                                }
353                               
354                                $template->set_filenames(array('ecard_template' =>  PIWECARD_ROOT.'/template/ecard.tpl'));
355                                $template->concat('COMMENT_IMG', $template->parse('ecard_template', true));
356                        }
357                }
358        }
359}
360?>
Note: See TracBrowser for help on using the repository browser.