load_config(); } // Load general configuration from config_database function load_config() { $query = ' SELECT value FROM '.CONFIG_TABLE.' WHERE param = \'ecard\' ;'; $result = pwg_query($query); if(isset($result)) { $row = mysql_fetch_row($result); if(is_string($row[0])) { $this->my_config = unserialize(($row[0])); } } $this->load_default_config(); } // Initialize default values of params private function load_default_config() { include ECARD_INC_PATH.'default_values.inc.php'; foreach ($ecard_default_values as $key => $value) { if (!isset($this->my_config[$key])) $this->my_config[$key] = $value; } } // Save general configuration to config_database function save_config() { $query = ' REPLACE INTO '.CONFIG_TABLE.' VALUES( \'ecard\', \''.serialize($this->my_config).'\', \'Configuration ecard\') ;'; $result = pwg_query($query); if($result) return true; else return false; } // Retrieve user groups function get_user_groups() { global $user; $query = 'SELECT group_id FROM ' . USER_GROUP_TABLE . ' WHERE user_id = ' . $user['id'] . ';'; $result = pwg_query($query); while ($row = mysql_fetch_assoc($result)) { array_push($this->user_groups, $row['group_id']); } } function section_init_ecard() { global $tokens, $page; if ($tokens[0] == 'ecard') $page['section'] = 'ecard'; } function index_ecard() { global $page; if (isset($page['section']) and $page['section'] == 'ecard') { include(ECARD_PATH . 'publish.php'); } } //Générer une chaine de caractère unique et aléatoire private function random($car) { $string = ""; $chaine = "abcdefghijklmnpqrstuvwxy0123456789"; srand((double)microtime()*1000000); for($i=0; $i<$car; $i++) { $string .= $chaine[rand()%strlen($chaine)]; } return $string; } // NB of days between 2 dates "AAAA-MM-JJ HH:hh:ss" function NbJours($debut, $fin) { $tDeb = explode("-", substr($debut,0,strpos($debut, ' '))); $tFin = explode("-", substr($fin,0,strpos($fin, ' '))); $diff = mktime(0, 0, 0, $tFin[1], $tFin[2], $tFin[0]) - mktime(0, 0, 0, $tDeb[1], $tDeb[2], $tDeb[0]); return(($diff / 86400)); } function AjoutJours($debut, $jours, $soustrait = false) { $tDeb = explode("-", substr($debut,0,strpos($debut, ' '))); $tDebH = explode(":", substr($debut,strpos($debut, ' ')+1)); $tFin = ""; $nb_ans = (int)(($jours)/365); $nb_mois = (int)(( ($jours)%365) / 31); $nb_jours = (int)(( ($jours)%365) % 31); if ($soustrait) $tFin = date("Y-m-d H:m:s", mktime($tDebH[0], $tDebH[1], $tDebH[2], $tDeb[1] - $nb_mois, $tDeb[2] - $nb_jours, $tDeb[0] - $nb_ans)); else $tFin = date("Y-m-d H:m:s", mktime($tDebH[0], $tDebH[1], $tDebH[2], $tDeb[1] + $nb_mois, $tDeb[2] + $nb_jours, $tDeb[0] + $nb_ans)); return($tFin); } function parse($data, $_POST = NULL, $image_element = NULL) { include (ECARD_PATH.'include/config_param.inc.php'); $patterns = array(); $replacements = array(); foreach ($ecard_parse as $key => $value) { array_push($patterns, $key); array_push($replacements, $value); } return str_replace($patterns, $replacements, $data); } // Get the number of ecard in the database function get_nb_ecard() { $query = 'SELECT COUNT(DISTINCT numero) as nb FROM '.ECARD_TABLE . ' ORDER BY date' .';'; $result = pwg_query($query); if ($result) { $nb=mysql_fetch_assoc($result); return $nb['nb']; } else return 0; } // Get the number of valid ecard in the database function get_nb_valid_ecard() { $query = 'SELECT numero,date,duration FROM '.ECARD_TABLE .';'; $result = pwg_query($query); $count = 0; while($ecard_info = mysql_fetch_assoc($result)) { if ($ecard_info['duration'] == 0 OR $this->NbJours($ecard_info['date'], date("Y-m-d H:m:s")) <= $ecard_info['duration']) // activ ecard $count++; } return $count; } // Get ecard information into array function get_ecard($ecard_id = null) { if ($ecard_id!== null) { $query = ' SELECT * FROM ' . ECARD_TABLE .' WHERE numero ="' . $ecard_id . '" LIMIT 1 '; $result = pwg_query($query); if ($result) return mysql_fetch_assoc($result); else return false; } } // Get ecard information into array function is_valid($ecard_id = null, $param_date=false) { if ($ecard_id!== null) { $ecard_info = $this->get_ecard($ecard_id); if ($ecard_info != false) { // Valid duration for an ecard $duration = ($param_date ? $this->my_config['activ'] : $ecard_info['duration']); if ($this->debug) { foreach ($ecard_info as $i=>$v) echo "ecard[".$i."]=".$v." \ "; echo "NBjours = ".$this->NbJours($ecard_info['date'], date("Y-m-d H:m:s"))." -"; echo "Activenb=".$this->my_config['activ']." -"; } if (isset ($ecard_info) AND $duration != 0 // 0 means always activ AND ($this->NbJours($ecard_info['date'], date("Y-m-d H:m:s")) > $duration) // Inactiv ecard ) { return false; } else { return true; } } else return false; } else { return true; } } // delete one ecard // force to delete valid ecard function delete_ecard($ecard_id = null, $force = false) { if ($ecard_id!== null) { $ecard_info = $this->get_ecard($ecard_id); if ($this->debug) { foreach ($ecard_info as $i=>$v) echo "ecard[".$i."]=".$v." \ "; } if (isset ($ecard_info) and ( ($this->NbJours($ecard_info['date'], date("Y-m-d H:m:s")) > $this->my_config['activ']) // Inactiv ecard OR $force // Or force to delete even if activ ) ) $query = ' DELETE FROM ' . ECARD_TABLE .' WHERE numero ="' . $ecard_id . '" '; pwg_query($query); } else return false; } // Delete all invalid ecard function delete_allinvalid_ecard() { $date = $this->AjoutJours(date("Y-m-d H:m:s"), $this->my_config['activ'], true); $query = ' DELETE FROM ' . ECARD_TABLE .' WHERE date < "' . $date . '" '; pwg_query($query); } // Add tpl to picture.php page to display ecard informations function display_ecard_to_picture() { global $page, $user, $template; // Init user groups $this->get_user_groups(); // Only on category page! if (isset($page['section'])) { $upper_names = null; if (!empty($page['category'])) { // Gets all upper categories from the image category to test // - if the parameter for "recursive" is OK // - if the upper category is activated for this function $query = 'SELECT * FROM '.CATEGORIES_TABLE.' WHERE id = '.pwg_db_real_escape_string($page['category']['id']).' ;'; $cat = mysql_fetch_assoc(pwg_query($query)); if (empty($cat)) { $upper_ids = null; } else { foreach ($cat as $k => $v) { // If the field is true or false, the variable is transformed into a // boolean value. if ($cat[$k] == 'true' or $cat[$k] == 'false') { $cat[$k] = get_boolean($cat[$k]); } } $upper_ids = explode(',', $cat['uppercats']); } } if ($this->my_config['user_cat']) { // !Function only allowed on user image // Check the category name, user name et img author // Get all name for upper categories and current category if (isset($cat) and !empty($cat)) { $catname[0] = $cat['name']; if (isset($upper_ids) and $upper_ids != null) { $nb=1; foreach ($upper_ids as $upper_cat) { // Get upper cat info and store the name $cat_info = get_cat_info($upper_cat); $catname[$nb++] = $cat_info['name']; } } } // Username or the current user $username = $user['username']; // author of the photo $authorname = ""; $query = ' SELECT author FROM '.IMAGES_TABLE.' WHERE id = '.$page['image_id'] .' LIMIT 1' .';'; $result = pwg_query($query); if (isset($result)) { $img_infos = mysql_fetch_array($result); $authorname = $img_infos['author']; } } // Only on available cats if ($this->my_config['allcats'] // Available on all cats OR (!empty($page['category']) AND in_array($page['category']['id'], $this->my_config['cats'])) // Available on specific cats OR ($this->my_config['recursive'] AND isset($upper_ids) AND // Available on upper cats this recursiv mode (array_intersect($upper_ids, $this->my_config['cats']) != array())) OR // Available based on usename ($this->my_config['user_cat'] AND (in_array($username, $catname) // Available user categories OR $username == $authorname) ) ) { // And only available groups if (empty($this->my_config['groups']) OR (!empty($this->my_config['groups']) AND (array_intersect($this->user_groups, $this->my_config['groups']) != array())) ) { // Check if user is guest. // In this case, force mail to default mail (in params) if (is_a_guest()) { if (!empty($this->my_config['defaultmail'])) $user['email'] = $this->my_config['defaultmail']; } // Template informations $template->assign('ecard', array( 'subject' => l10n('ecard_send_title'), 'message' => l10n('ecard_send_message'), 'sender_name' => $user['username'], 'sender_email' => $user['email'], 'recipient_name' => l10n('ecard_send_dest_name'), 'recipient_email' => l10n('ecard_send_dest_mail'), 'copy' => $this->my_config['send_copy'] ? 'checked="checked"' : '', 'changemail' => ($this->my_config['expmail_change'] ? '' : 'disabled="disabled"') )); // Template add for the active parameter choice by the user if ($this->my_config['active_parameter']) { // Allowed for the user $template->append('ecard_validity',array('id' => 0, 'name' => l10n('ecard_nolimit'), 'selected' => ($this->my_config['activ'] == 0 ? 'checked' : ''))); for($jj=5; $jj < 30; $jj+=5) $template->append('ecard_validity',array('id' => $jj, 'name' => $jj , 'selected' => ($this->my_config['activ'] == $jj ? 'checked' : ''))); } // Template add for the send method to be chose, by the user // default : text $template->append('ecard_send_method',array('id' => 0, 'name' => l10n('ecard_maillink'), 'selected' => ($this->my_config['send_HTML'] ? '' : 'checked'))); if ($this->my_config['send_HTML']) { // Allowed for the user $template->append('ecard_send_method',array('id' => 1, 'name' => l10n('ecard_mailhtml'), 'selected' => ($this->my_config['send_HTML'] ? 'checked' : ''))); } // Send the card if (isset($_POST['ecard_submit'])) { $send_method = $_POST['ecard_send_method']; // If conf doesn't allow to modify the %votremail param, force it to user mail if (!isset($_POST['ecard_sender_email'])) $_POST['ecard_sender_email'] = $user['email']; // Initialize the array for image element $image_element = array(); // Get all image informations $query = 'SELECT * FROM '.IMAGES_TABLE.' WHERE id='.$page['image_id'].' LIMIT 1;'; $result = pwg_query($query); if (isset($result)) $image_element = mysql_fetch_array($result); // Generate random number $image_element['next_element_id'] = $this->random(64); // Image infos if ($this->my_config['ecard_showinfos']) { if (isset($image_element['name'])) { $image_element['imginfos'] = $image_element['name']; if (isset($image_element['author'])) $image_element['imginfos'] .= ' ('.$image_element['author'].')'; } } $insert = array( 'numero' => $image_element['next_element_id'], 'nomexp' => $_POST['ecard_sender_name'], 'nomdest' => $_POST['ecard_recipient_name'], 'adrexp' => $_POST['ecard_sender_email'], 'adrdest' => $_POST['ecard_recipient_email'], 'sujet' => $_POST['ecard_subject'], 'message' => $_POST['ecard_message'], 'image' => $image_element['id'], 'date' => date("Y-m-d H:i:s"), 'duration' => (isset($_POST['ecard_validity']) ? $_POST['ecard_validity'] : $this->my_config['activ']), ); // TO DO : add valid date (end date or duration) / add number (increment number) / single_insert(ECARD_TABLE, $insert); // Complete the image_element array with Link for the ecard url to be added in the mail set_make_full_url(); $mail_url = embellish_url(get_absolute_root_url() . './index.php?/ecard/'.$image_element['next_element_id']); $image_element['mail_url'] = $mail_url; unset_make_full_url(); // Complete the image_element with the url to point to the image url set_make_full_url(); $image_element['picture_url'] = duplicate_picture_url( array( 'image_id' => $image_element['id'], 'image_file' => $image_element['file'] ), array('start') ); unset_make_full_url(); $mail_subject = htmlspecialchars_decode($this->parse( $this->my_config['subject_link'], $_POST)); switch($send_method) { case 0 : // text // Get the standard message (in admin param) and parse it with the informations $mail_message = stripslashes(htmlspecialchars_decode($this->parse($this->my_config['message_link'], $_POST, $image_element))); $mail_arg=array('from' => $_POST['ecard_sender_email'], 'subject' => $mail_subject, 'content' => $mail_message, 'content_format' => "text/plain", 'email_format' => "text/html" ); break; case 1 : // html $mail_message_HTML = stripslashes(htmlspecialchars_decode($this->parse($this->my_config['message_HTML'], $_POST, $image_element))); $mail_arg=array('from' => $_POST['ecard_sender_email'], 'subject' => $mail_subject, 'content' => $mail_message_HTML, 'content_format' => "text/html", 'email_format' => "text/html" ); break; } // Add the copy to expe if param. if (isset($_POST['ecard_copy'])) // send copy to sender $mail_arg['Bcc'] = array((isset($_POST['ecard_sender_email']) ? $_POST['ecard_sender_email'] : $user['email'])); // Send the mail pwg_mail($_POST['ecard_recipient_email'], $mail_arg); } $template->set_filenames(array('ecard_template' => ECARD_ROOT.'/template/ecard.tpl')); $template->concat('COMMENT_IMG', $template->parse('ecard_template', true)); } } } } } ?>