[8771] | 1 | <?php |
---|
| 2 | global $user, $conf; |
---|
| 3 | |
---|
[19968] | 4 | class Piwecard { |
---|
[19956] | 5 | var $my_config; |
---|
[8771] | 6 | var $user_groups = array(); |
---|
| 7 | |
---|
| 8 | // Class constructor |
---|
[19956] | 9 | function __construct() { |
---|
[8771] | 10 | $this->load_config(); |
---|
| 11 | } |
---|
| 12 | |
---|
| 13 | // Load general configuration from config_database |
---|
[19732] | 14 | function load_config() { |
---|
[19956] | 15 | $query = 'SELECT value FROM '.CONFIG_TABLE.' WHERE param="piwecard";'; |
---|
[8771] | 16 | $result = pwg_query($query); |
---|
| 17 | |
---|
[19732] | 18 | if(isset($result)) { |
---|
[19968] | 19 | $row = pwg_db_fetch_row($result); |
---|
[19732] | 20 | if(is_string($row[0])) { |
---|
| 21 | $this->my_config = unserialize(($row[0])); |
---|
| 22 | } |
---|
[8771] | 23 | } |
---|
| 24 | $this->load_default_config(); |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | // Initialize default values of params |
---|
[19732] | 28 | private function load_default_config() { |
---|
[20010] | 29 | require(ECARD_INSTALL_PATH.'default_values.inc.php'); |
---|
[19732] | 30 | foreach ($ecard_default_values as $key => $value) { |
---|
| 31 | if (!isset($this->my_config[$key])) |
---|
| 32 | $this->my_config[$key] = $value; |
---|
[8771] | 33 | } |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | // Save general configuration to config_database |
---|
[19956] | 37 | function save_config() { |
---|
[20010] | 38 | conf_update_param('piwecard', pwg_db_real_escape_string(serialize($this->my_config))); |
---|
[8771] | 39 | } |
---|
| 40 | |
---|
[19732] | 41 | function section_init_ecard() { |
---|
[8771] | 42 | global $tokens, $page; |
---|
[19732] | 43 | |
---|
[8771] | 44 | if ($tokens[0] == 'ecard') |
---|
[19732] | 45 | $page['section'] = 'ecard'; |
---|
[8771] | 46 | } |
---|
| 47 | |
---|
[19732] | 48 | function index_ecard() { |
---|
[8771] | 49 | global $page; |
---|
| 50 | |
---|
[19732] | 51 | if (isset($page['section']) and $page['section'] == 'ecard') { |
---|
[8771] | 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) { |
---|
[19732] | 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; |
---|
[8771] | 65 | } |
---|
| 66 | |
---|
[19732] | 67 | function parse($data, $_POST = NULL, $image_element = NULL) { |
---|
[8771] | 68 | include (ECARD_PATH.'include/config_param.inc.php'); |
---|
| 69 | |
---|
| 70 | $patterns = array(); |
---|
| 71 | $replacements = array(); |
---|
[19956] | 72 | foreach ($ecard_parse as $key => $value) { |
---|
[8771] | 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 |
---|
[19732] | 81 | function get_nb_ecard() { |
---|
[19956] | 82 | $query = 'SELECT COUNT(DISTINCT id) AS nb FROM '.ECARD_TABLE.' ORDER BY date_creation;'; |
---|
[8771] | 83 | $result = pwg_query($query); |
---|
[19956] | 84 | |
---|
[19732] | 85 | if ($result) { |
---|
[19968] | 86 | $nb=pwg_db_fetch_assoc($result); |
---|
[8771] | 87 | return $nb['nb']; |
---|
[19956] | 88 | } else |
---|
[8771] | 89 | return 0; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | // Get the number of valid ecard in the database |
---|
[19732] | 93 | function get_nb_valid_ecard() { |
---|
[19956] | 94 | $query = 'SELECT COUNT(DISTINCT id) AS nb FROM '.ECARD_TABLE.' WHERE date_validity IS NULL OR date_validity > NOW();'; |
---|
[8771] | 95 | $result = pwg_query($query); |
---|
[19956] | 96 | |
---|
| 97 | if ($result) { |
---|
[19968] | 98 | $nb=pwg_db_fetch_assoc($result); |
---|
[19956] | 99 | return $nb['nb']; |
---|
| 100 | } else |
---|
| 101 | return 0; |
---|
[8771] | 102 | } |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | // Get ecard information into array |
---|
[19732] | 106 | function get_ecard($ecard_id = null) { |
---|
| 107 | if ($ecard_id!== null) { |
---|
[19956] | 108 | $query = 'SELECT * FROM ' . ECARD_TABLE .' WHERE id ="' . $ecard_id . '" LIMIT 1;'; |
---|
[8771] | 109 | |
---|
| 110 | $result = pwg_query($query); |
---|
| 111 | if ($result) |
---|
[19968] | 112 | return pwg_db_fetch_assoc($result); |
---|
[8771] | 113 | else |
---|
| 114 | return false; |
---|
| 115 | } |
---|
| 116 | } |
---|
| 117 | |
---|
[19956] | 118 | function is_valid($ecard_id = null) { |
---|
| 119 | if (isset($ecard_id)) { |
---|
[8771] | 120 | $ecard_info = $this->get_ecard($ecard_id); |
---|
[19956] | 121 | if (isset($ecard_info)) { |
---|
[8771] | 122 | // Valid duration for an ecard |
---|
[19956] | 123 | $date_validity = $ecard_info['date_validity']; |
---|
[8771] | 124 | |
---|
[19956] | 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; |
---|
[19732] | 132 | } else { |
---|
[8771] | 133 | return true; |
---|
| 134 | } |
---|
[19956] | 135 | } else |
---|
[8771] | 136 | return false; |
---|
[19732] | 137 | } else { |
---|
[19956] | 138 | return false; |
---|
[8771] | 139 | } |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | // delete one ecard |
---|
| 143 | // force to delete valid ecard |
---|
[19956] | 144 | function delete_ecard($ecard_id = null) { |
---|
| 145 | if (isset($ecard_id)) { |
---|
| 146 | $query = 'DELETE FROM ' . ECARD_TABLE .' WHERE id="' . $ecard_id . '";'; |
---|
[8771] | 147 | pwg_query($query); |
---|
[19956] | 148 | } else |
---|
[19732] | 149 | return false; |
---|
[8771] | 150 | } |
---|
| 151 | |
---|
| 152 | // Delete all invalid ecard |
---|
[19732] | 153 | function delete_allinvalid_ecard() { |
---|
[19956] | 154 | $query = 'DELETE FROM ' . ECARD_TABLE .' WHERE date_validity < NOW();'; |
---|
[8771] | 155 | pwg_query($query); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | // Add tpl to picture.php page to display ecard informations |
---|
[19732] | 159 | function display_ecard_to_picture() { |
---|
| 160 | global $page, $user, $template; |
---|
[8771] | 161 | |
---|
[19732] | 162 | // Only on category page! |
---|
| 163 | if (isset($page['section'])) { |
---|
[8771] | 164 | $upper_names = null; |
---|
[19732] | 165 | |
---|
| 166 | if (!empty($page['category'])) { |
---|
| 167 | // Gets all upper categories from the image category to test |
---|
| 168 | // - if the upper category is activated for this function |
---|
[20010] | 169 | $query = 'SELECT * FROM '.CATEGORIES_TABLE.' WHERE id = '.pwg_db_real_escape_string($page['category']['id']).';'; |
---|
[19968] | 170 | $cat = pwg_db_fetch_assoc(pwg_query($query)); |
---|
[19732] | 171 | |
---|
| 172 | if (empty($cat)) { |
---|
| 173 | $upper_ids = null; |
---|
| 174 | } else { |
---|
| 175 | foreach ($cat as $k => $v) { |
---|
| 176 | // If the field is true or false, the variable is transformed into a |
---|
| 177 | // boolean value. |
---|
| 178 | if ($cat[$k] == 'true' or $cat[$k] == 'false') { |
---|
| 179 | $cat[$k] = get_boolean($cat[$k]); |
---|
| 180 | } |
---|
| 181 | } |
---|
| 182 | $upper_ids = explode(',', $cat['uppercats']); |
---|
| 183 | } |
---|
[8771] | 184 | } |
---|
[19732] | 185 | |
---|
[20010] | 186 | if ($this->my_config['authorizations'] == 'user_cats') { // !Function only allowed on user image |
---|
[8771] | 187 | // Check the category name, user name et img author |
---|
| 188 | // Get all name for upper categories and current category |
---|
[19732] | 189 | if (isset($cat) and !empty($cat)) { |
---|
| 190 | $catname[0] = $cat['name']; |
---|
[20010] | 191 | if (isset($upper_ids)) { |
---|
[19732] | 192 | $nb=1; |
---|
| 193 | foreach ($upper_ids as $upper_cat) { |
---|
| 194 | // Get upper cat info and store the name |
---|
| 195 | $cat_info = get_cat_info($upper_cat); |
---|
| 196 | $catname[$nb++] = $cat_info['name']; |
---|
| 197 | } |
---|
| 198 | } |
---|
[8771] | 199 | } |
---|
| 200 | // Username or the current user |
---|
| 201 | $username = $user['username']; |
---|
| 202 | |
---|
| 203 | // author of the photo |
---|
[19956] | 204 | $query = 'SELECT author FROM '.IMAGES_TABLE.' WHERE id = '.$page['image_id'].' LIMIT 1;'; |
---|
[8771] | 205 | $result = pwg_query($query); |
---|
[19732] | 206 | if (isset($result)) { |
---|
[8771] | 207 | $img_infos = mysql_fetch_array($result); |
---|
| 208 | $authorname = $img_infos['author']; |
---|
| 209 | } |
---|
[20010] | 210 | } |
---|
| 211 | |
---|
[19732] | 212 | // Only on available cats |
---|
[20010] | 213 | if (($this->my_config['authorizations'] == 'all_cats') //Parameter : all_cats |
---|
[20013] | 214 | OR ($this->my_config['authorizations'] == 'selected_cats' AND isset($upper_ids) AND (array_intersect($upper_ids, $this->my_config['selected_cats']) != array())) //Parameter : selected_cats and recursive |
---|
| 215 | OR ($this->my_config['authorizations'] == 'user_cats' AND (in_array($username, $catname) OR $username == $authorname)) //Parameter : user_cats |
---|
[20010] | 216 | ) { |
---|
| 217 | // Check if user is guest. |
---|
| 218 | // In this case, force mail to default mail (in params) |
---|
| 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 | 'subject' => l10n('ecard_send_title'), |
---|
| 227 | 'message' => l10n('ecard_send_message'), |
---|
| 228 | 'sender_name' => $user['username'], |
---|
| 229 | 'sender_email' => $user['email'], |
---|
| 230 | 'recipient_name' => l10n('ecard_send_dest_name'), |
---|
| 231 | 'recipient_email' => l10n('ecard_send_dest_mail'), |
---|
| 232 | 'copy' => $this->my_config['sender_copy'] ? 'checked="checked"' : '', |
---|
[20013] | 233 | 'changemail' => (!isset($user['email']) OR $this->my_config['sender_email_change']) ? '' : 'disabled="disabled"' |
---|
| 234 | ) |
---|
| 235 | ); |
---|
[8771] | 236 | |
---|
[20010] | 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('ecard_nolimit') : $validity.' '.l10n('ecard_days'), |
---|
| 243 | 'selected' => ($this->my_config['validity_default'] == $validity ? 'checked' : '') |
---|
| 244 | ) |
---|
| 245 | ); |
---|
[19968] | 246 | } |
---|
[20010] | 247 | } |
---|
[19968] | 248 | |
---|
[20010] | 249 | foreach ($this->my_config['email_format'] as $email_format) { |
---|
| 250 | $template->append('ecard_send_method', array( |
---|
| 251 | 'id' => $email_format, |
---|
| 252 | 'name' => l10n('ecard_mail_format_'.$email_format), |
---|
[20013] | 253 | 'selected' => (($this->my_config['email_format_default'] == $email_format) ? '' : 'checked'), |
---|
[20010] | 254 | ) |
---|
| 255 | ); |
---|
| 256 | } |
---|
[8771] | 257 | |
---|
[20010] | 258 | // Send the card |
---|
| 259 | if (isset($_POST['ecard_submit'])) { |
---|
| 260 | $send_method = $_POST['ecard_send_method']; |
---|
| 261 | |
---|
| 262 | // If conf doesn't allow to modify the %votremail 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(); |
---|
[8771] | 268 | |
---|
[20010] | 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 | $image_element['next_element_id'] = $this->random(64); |
---|
[8771] | 277 | |
---|
[20010] | 278 | // Image infos |
---|
| 279 | if ($this->my_config['show_image_infos']) { |
---|
| 280 | if (isset($image_element['name'])) { |
---|
| 281 | $image_element['picture_infos'] = $image_element['name']; |
---|
| 282 | if (isset($image_element['author'])) |
---|
| 283 | $image_element['picture_infos'] .= ' ('.$image_element['author'].')'; |
---|
[8771] | 284 | } |
---|
[20010] | 285 | } |
---|
[8771] | 286 | |
---|
[20010] | 287 | $insert = array( |
---|
| 288 | 'id' => $image_element['next_element_id'], |
---|
| 289 | 'sender_name' => $_POST['ecard_sender_name'], |
---|
| 290 | 'recipient_name' => $_POST['ecard_recipient_name'], |
---|
| 291 | 'sender_email' => $_POST['ecard_sender_email'], |
---|
| 292 | 'recipient_email' => $_POST['ecard_recipient_email'], |
---|
| 293 | 'subject' => $_POST['ecard_subject'], |
---|
| 294 | 'message' => $_POST['ecard_message'], |
---|
| 295 | 'image' => $image_element['id'], |
---|
| 296 | 'date_creation' => date("Y-m-d H:i:s"), |
---|
| 297 | ); |
---|
| 298 | if ($_POST['ecard_validity'] != '0') { |
---|
| 299 | $date = new DateTime(); |
---|
| 300 | $date->modify("+".$_POST['ecard_validity']." day"); |
---|
| 301 | $insert['date_validity'] = $date->format('Y-m-d H:i:s'); |
---|
| 302 | } |
---|
| 303 | single_insert(ECARD_TABLE, $insert); |
---|
| 304 | |
---|
| 305 | // Complete the image_element array with Link for the ecard url to be added in the mail |
---|
| 306 | set_make_full_url(); |
---|
| 307 | $ecard_url = embellish_url(get_absolute_root_url() . './index.php?/ecard/'.$image_element['next_element_id']); |
---|
| 308 | $image_element['ecard_url'] = $ecard_url; |
---|
| 309 | unset_make_full_url(); |
---|
| 310 | |
---|
| 311 | // Complete the image_element with the url to point to the image url |
---|
| 312 | set_make_full_url(); |
---|
| 313 | $image_element['picture_url'] = duplicate_picture_url( |
---|
| 314 | array( |
---|
| 315 | 'image_id' => $image_element['id'], |
---|
| 316 | 'image_file' => $image_element['file'] |
---|
| 317 | ), |
---|
| 318 | array('start') |
---|
| 319 | ); |
---|
| 320 | unset_make_full_url(); |
---|
[8771] | 321 | |
---|
[20010] | 322 | $email_subject = htmlspecialchars_decode($this->parse( $this->my_config['email_subject'], $_POST)); |
---|
[20013] | 323 | $email_message = htmlspecialchars_decode($this->parse($this->my_config['email_message'][$send_method], $_POST, $image_element)); |
---|
[20010] | 324 | $email_arg=array( 'from' => $_POST['ecard_sender_email'], |
---|
| 325 | 'subject' => $email_subject, |
---|
| 326 | 'content' => $email_message, |
---|
| 327 | ); |
---|
| 328 | |
---|
| 329 | switch($send_method) { |
---|
| 330 | case 'text': // text |
---|
| 331 | $email_arg=array( 'content_format' => "text/plain", |
---|
| 332 | 'email_format' => "text/plain" |
---|
| 333 | ); |
---|
| 334 | break; |
---|
| 335 | case 'html': // html |
---|
| 336 | $email_arg=array( 'content_format' => "text/html", |
---|
[19968] | 337 | 'email_format' => "text/html" |
---|
[20010] | 338 | ); |
---|
| 339 | break; |
---|
| 340 | default: |
---|
| 341 | break; |
---|
[8771] | 342 | } |
---|
[19732] | 343 | |
---|
[20010] | 344 | // Add the copy to expe if param. |
---|
| 345 | if (isset($_POST['ecard_copy'])) // send copy to sender |
---|
| 346 | $email_arg['Bcc'] = array((isset($_POST['ecard_sender_email']) ? $_POST['ecard_sender_email'] : $user['email'])); |
---|
| 347 | |
---|
| 348 | // Send the mail |
---|
| 349 | pwg_mail($_POST['ecard_recipient_email'], $email_arg); |
---|
[8771] | 350 | } |
---|
[20010] | 351 | |
---|
| 352 | $template->set_filenames(array('ecard_template' => ECARD_ROOT.'/template/ecard.tpl')); |
---|
| 353 | $template->concat('COMMENT_IMG', $template->parse('ecard_template', true)); |
---|
[8771] | 354 | } |
---|
| 355 | } |
---|
| 356 | } |
---|
| 357 | } |
---|
[19923] | 358 | ?> |
---|