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

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

[piwecard] various improvements

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