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

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

[piwecard] lots of improvements

  • Property svn:eol-style set to native
File size: 11.6 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->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 = pwg_db_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            require(ECARD_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 save_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(ECARD_PATH . 'publish.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 (ECARD_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 '.ECARD_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 '.ECARD_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 ' . ECARD_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 ' . ECARD_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 ' . ECARD_TABLE .' WHERE date_validity < NOW();';
155                pwg_query($query);
156        }
157       
158        // Add tpl to picture.php page to display ecard informations
159        function display_ecard_to_picture() {
160                global $page, $user, $template;
161
162                // Only on category page!
163                if (isset($page['section'])) {
164                        $upper_names = null;
165                       
166                        if (!empty($page['category'])) {
167                                // Gets all upper categories from the image category to test
168                                //      - if the parameter for "recursive" is OK
169                                //      - if the upper category is activated for this function
170                                $query = 'SELECT * FROM '.CATEGORIES_TABLE.' WHERE id = '.pwg_db_real_escape_string($page['category']['id']).';';
171                                $cat = pwg_db_fetch_assoc(pwg_query($query));
172                               
173                                if (empty($cat)) {
174                                        $upper_ids = null;
175                                } else {
176                                        foreach ($cat as $k => $v) {
177                                                // If the field is true or false, the variable is transformed into a
178                                                // boolean value.
179                                                if ($cat[$k] == 'true' or $cat[$k] == 'false') {
180                                                  $cat[$k] = get_boolean($cat[$k]);
181                                                }
182                                        }
183                                        $upper_ids = explode(',', $cat['uppercats']);
184                                }
185                        }
186                       
187                        if ($this->my_config['authorizations'] == 'user_cats') {        // !Function only allowed on user image
188                                // Check the category name, user name et img author
189                                // Get all name for upper categories and current category
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                                                        // Get upper cat info and store the name
196                                                        $cat_info = get_cat_info($upper_cat);
197                                                        $catname[$nb++] = $cat_info['name'];
198                                                }
199                                        }
200                                }
201                                // Username or the current user
202                                $username = $user['username'];
203                               
204                                // author of the photo
205                                $query = 'SELECT author FROM '.IMAGES_TABLE.' WHERE id = '.$page['image_id'].' LIMIT 1;';
206                                $result = pwg_query($query);
207                                if (isset($result)) {
208                                        $img_infos = mysql_fetch_array($result);
209                                        $authorname = $img_infos['author'];
210                                }
211                        }
212                       
213                        // Only on available cats
214                        if (($this->my_config['authorizations'] == 'all_cats')          //Parameter : all_cats
215                                OR ($this->my_config['authorizations'] == 'selected_cats' AND !empty($page['category']) AND in_array($page['category']['id'], $this->my_config['selected_cats']))       //Parameter : selected_cats but not recursive
216                                OR ($this->my_config['authorizations'] == 'selected_cats' AND $this->my_config['recursive'] AND isset($upper_ids) AND (array_intersect($upper_ids, $this->my_config['selected_cats']) != array())) //Parameter : selected_cats and recursive
217                                OR      ($this->my_config['authorizations'] == 'user_cats' AND (in_array($username, $catname) OR $username == $authorname)) //Parameter : selected_cats and recursive
218                        ) {
219                                // Check if user is guest.
220                                // In this case, force mail to default mail (in params)
221                                if (is_a_guest()) {
222                                        if (!empty($this->my_config['default_guest_email']))
223                                                $user['email'] = $this->my_config['default_guest_email'];
224                                }
225                       
226                                // Template informations
227                                $template->assign('ecard', array(
228                                                'subject'                       => l10n('ecard_send_title'),
229                                                'message'                       => l10n('ecard_send_message'),
230                                                'sender_name'           => $user['username'],
231                                                'sender_email'          => $user['email'],
232                                                'recipient_name'        => l10n('ecard_send_dest_name'),
233                                                'recipient_email'       => l10n('ecard_send_dest_mail'),
234                                                'copy'                          => $this->my_config['sender_copy'] ? 'checked="checked"' : '',
235                                                'changemail'            => (!isset($user['email']) OR $this->my_config['sender_email_change']) ? '' : 'disabled="disabled"')
236                                                ));
237
238                                // Template add for the active parameter choice by the user
239                                if ($this->my_config['validity_choice']) {
240                                        foreach($this->my_config['validity'] as $validity) {
241                                                $template->append('ecard_validity', array(
242                                                                                                                                        'id' => $validity,
243                                                                                                                                        'name' => ($validity == 0) ? l10n('ecard_nolimit') : $validity.' '.l10n('ecard_days'),
244                                                                                                                                        'selected' => ($this->my_config['validity_default'] == $validity ? 'checked' : '')
245                                                                                                                                )
246                                                );
247                                        }
248                                }
249                               
250                                foreach ($this->my_config['email_format'] as $email_format) {
251                                        $template->append('ecard_send_method', array(
252                                                                                                                                'id' => $email_format,
253                                                                                                                                'name' => l10n('ecard_mail_format_'.$email_format),
254                                                                                                                                'selected' => (($this->my_config['email_format_default'] == $email_format) ?  '' : 'checked')
255                                                                                                                        )
256                                        );
257                                }
258
259                                // Send the card
260                                if (isset($_POST['ecard_submit'])) {
261                                        $send_method = $_POST['ecard_send_method'];
262                                       
263                                        // If conf doesn't allow to modify the %votremail param, force it to user mail
264                                        if (!$this->my_config['sender_email_change'])
265                                                $_POST['ecard_sender_email'] = $user['email'];
266                                       
267                                        // Initialize the array for image element
268                                        $image_element = array();
269
270                                        // Get all image informations
271                                        $query = 'SELECT * FROM '.IMAGES_TABLE.' WHERE id='.$page['image_id'].' LIMIT 1;';
272                                        $result = pwg_query($query);
273                                        if (isset($result))
274                                                $image_element = mysql_fetch_array($result);
275                                       
276                                        // Generate random number
277                                        $image_element['next_element_id']  = $this->random(64);
278
279                                        // Image infos
280                                        if ($this->my_config['show_image_infos']) {
281                                                if (isset($image_element['name'])) {
282                                                        $image_element['picture_infos'] = $image_element['name'];
283                                                        if (isset($image_element['author']))
284                                                                $image_element['picture_infos'] .= ' ('.$image_element['author'].')';
285                                                }
286                                        }
287
288                                        $insert = array(
289                                                                'id'                            => $image_element['next_element_id'],
290                                                                'sender_name'           => $_POST['ecard_sender_name'],
291                                                                'recipient_name'        => $_POST['ecard_recipient_name'],
292                                                                'sender_email'          => $_POST['ecard_sender_email'],
293                                                                'recipient_email'       => $_POST['ecard_recipient_email'],
294                                                                'subject'                       => $_POST['ecard_subject'],
295                                                                'message'                       => $_POST['ecard_message'],
296                                                                'image'                         => $image_element['id'],
297                                                                'date_creation'         => date("Y-m-d H:i:s"),
298                                        );
299                                        if ($_POST['ecard_validity'] != '0') {
300                                                $date = new DateTime();
301                                                $date->modify("+".$_POST['ecard_validity']." day");
302                                                $insert['date_validity'] = $date->format('Y-m-d H:i:s');
303                                        }
304                                        single_insert(ECARD_TABLE, $insert);
305                                       
306                                        // Complete the image_element array with Link for the ecard url to be added in the mail
307                                        set_make_full_url();
308                                        $ecard_url = embellish_url(get_absolute_root_url() . './index.php?/ecard/'.$image_element['next_element_id']);
309                                        $image_element['ecard_url'] = $ecard_url;
310                                        unset_make_full_url();
311                                       
312                                        // Complete the image_element with the url to point to the image url
313                                        set_make_full_url();
314                                        $image_element['picture_url'] = duplicate_picture_url(
315                                                array(
316                                                        'image_id' => $image_element['id'],
317                                                        'image_file' => $image_element['file']
318                                                ),
319                                                array('start')
320                                        );
321                                        unset_make_full_url();
322
323                                        $email_subject = htmlspecialchars_decode($this->parse( $this->my_config['email_subject'], $_POST));
324                                        $email_message = htmlspecialchars_decode($this->parse($this->my_config['email_'.$this->my_config['email_format']], $_POST, $image_element));
325                                        $email_arg=array(       'from' => $_POST['ecard_sender_email'],
326                                                                                'subject'                       => $email_subject,
327                                                                                'content'                       => $email_message,
328                                                                        );
329                                                                       
330                                        switch($send_method) {
331                                                case 'text': // text
332                                                        $email_arg=array(       'content_format'        => "text/plain",
333                                                                                                'email_format'          => "text/plain"
334                                                                                        );
335                                                        break;
336                                                case 'html': // html
337                                                        $email_arg=array(       'content_format' => "text/html",
338                                                                                                'email_format' => "text/html"
339                                                                                        );
340                                                        break;
341                                                default:
342                                                        break;
343                                        }
344                                       
345                                        // Add the copy to expe if param.
346                                        if (isset($_POST['ecard_copy']))        // send copy to sender
347                                                $email_arg['Bcc'] = array((isset($_POST['ecard_sender_email']) ? $_POST['ecard_sender_email'] : $user['email']));
348                                       
349                                        // Send the mail
350                                        pwg_mail($_POST['ecard_recipient_email'], $email_arg);
351                                }
352                               
353                                $template->set_filenames(array('ecard_template' =>  ECARD_ROOT.'/template/ecard.tpl'));
354                                $template->concat('COMMENT_IMG', $template->parse('ecard_template', true));
355                        }
356                }
357        }
358}
359?>
Note: See TracBrowser for help on using the repository browser.