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($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)
{
// echo "value=".$key." \ ";
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;
if ($this->debug)
foreach ($user as $id=>$val)
{
if (is_array($val))
foreach ($val as $id2=>$val2)
echo "user[".$id."][".$id2."]=".$val2."\n";
else
echo "user[".$id."]=".$val."\n";
}
$query = 'SELECT group_id FROM ' . USER_GROUP_TABLE . ' WHERE user_id = ' . $user['id'] . ';';
if ($this->debug) echo $query."\n";
$result = pwg_query($query);
while ($row = mysql_fetch_assoc($result))
{
array_push($this->user_groups, $row['group_id']);
}
if ($this->debug)
foreach ($this->user_groups as $gr)
echo "Group=".$gr."\n";
}
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')
{
/*
foreach ($page as $i=>$p)
echo "page[".$i."]=".$p." ;";
*/
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
)
{
if ($this->debug) echo "Invalid";
return false;
}
else
{
if ($this->debug) echo "Valid";
return true;
}
}
else
return false;
}
else
{
if ($this->debug) echo "Invalid";
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 . '"
';
// echo "DELETE";
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 . '"
';
// echo "QUERY=".$query;
// echo "DELETE";
pwg_query($query);
}
// Add tpl to picture.php page to display ecard informations
function display_ecard_to_picture()
{
global $page, $user, $template, $conf;
// init user groups
$this->get_user_groups();
// Load language files
load_language('plugin.lang', ECARD_PATH);
// CSS and JS file for the template
$css_file = "lightbox-form.css";
$template->block_html_head('', '', $smarty, $repeat);
$template->block_html_head('', '', $smarty, $repeat);
// if (($page['section']) == 'categories' AND !empty($page['category'])) // only on category page!
if (isset($page['section']))
{
$upper_names = null;
if ( !empty($page['category']))
{
// get all upper categories from the image category to test
// - if the parameter for "recursiv" is OK
// - if the upper category is activated for this function
$query = 'SELECT *
FROM '.CATEGORIES_TABLE.'
WHERE id = '.$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 anly 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'];
if ($this->debug)
echo "Catname[".($nb-1)."]=".$catname[$nb-1]." ";
}
}
}
// 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'];
}
}
/*
foreach ($page as $k => $v)
{
if (is_array($v))
foreach ($v as $kk => $vv)
echo "page[".$k."][".$kk."]=".$vv." ";
else
echo "page[".$k."]=".$v." ";
}
*/
// 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())
{
// $this->my_config['expmail_change'] = false; // Disable exp mail change
if (!empty($this->my_config['defaultmail']))
$user['email'] = $this->my_config['defaultmail'];
}
// Template informations
$template->assign( 'ecard', array(
'sujet' => l10n('ecard_send_title'),
'message' => l10n('ecard_send_message'),
'votrenom' => $user['username'], //"votre nom",
'votremail' => $user['email'], //"votre mail (test@test.com)",
'sonnom' => l10n('ecard_send_dest_name'),
'sonmail' => 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_ACTIV',array('ID' => 0,'NAME' => l10n('ecard_nolimit'),'SELECTED' => ($this->my_config['activ'] == 0 ? 'checked' : '')));
for($jj=5; $jj < 30; $jj+=5)
$template->append('ECARD_ACTIV',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',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',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'];
// If conf dont allow to modify the %votremail param, force it to user mail
if (!isset($_POST['votremail']))
$_POST['votremail'] = $user['email'];
// Initialize the array for image element
$image_element = array();
// Get all image informations - to be replaced by a Core PWG function
$query = '
SELECT *
FROM '.IMAGES_TABLE.'
WHERE id = '.$page['image_id']
.' LIMIT 1'
.';';
// echo $query;
$result = pwg_query($query);
if (isset($result))
$image_element = mysql_fetch_array($result);
// Generate random number
$image_element['next_element_id'] = $this->random(15);
// 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'].')';
}
}
if ($this->debug) echo "Next_element=".$image_element['next_element_id']."\n";
$query = '
INSERT INTO ' . ECARD_TABLE .'
VALUES ( "'.$image_element['next_element_id'].'","'
.$_POST['votrenom'].'","'
.$_POST['sonnom'].'","'
.$_POST['votremail'].'","'
.$_POST['sonmail'].'","'
.$_POST['sujet'].'","'
.$_POST['message'].'",'
.$image_element['id'].',
now(),'
.(isset($_POST['ecard_activ']) ? $_POST['ecard_activ'] : $this->my_config['activ'])
.');';
// TO DO : add valid date (end date or duration) / add number (increment number) /
if ($this->debug) echo $query."\n";
pwg_query($query);
// Complete the image_element array with Link for the ecard url to be added in the mail
set_make_full_url();
$chemin = embellish_url(get_absolute_root_url() . './index.php?/ecard/'.$image_element['next_element_id']);
$image_element['chemin'] = $chemin;
unset_make_full_url();
// echo "chemin = ".$chemin."\n" ;
// Complete the image_element with the url to point to the image url
set_make_full_url();
$image_element['url_picture'] = duplicate_picture_url(
array(
'image_id' => $image_element['id'],
'image_file' => $image_element['file']
),
array('start')
);
unset_make_full_url();
$sujetdumail = 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
$messagemail = stripslashes(htmlspecialchars_decode($this->parse($this->my_config['message_link'], $_POST, $image_element)));
$mail_arg=array('from' => $_POST['votremail'],
'subject' => $sujetdumail,
'content' => $messagemail,
'content_format' => "text/plain",
'email_format' => "text/html"
);
break;
case 1 : // html
$messagemailHTML = stripslashes(htmlspecialchars_decode($this->parse($this->my_config['message_HTML'], $_POST, $image_element)));
$mail_arg=array('from' => $_POST['votremail'],
'subject' => $sujetdumail,
'content' => $messagemailHTML,
'content_format' => "text/html",
'email_format' => "text/html"
);
if ($this->debug) echo "messagemailHTML=".$messagemailHTML."\n";
break;
}
// Add the copy to expe if param.
if (isset($_POST['copy'])) // send copy to sender
$mail_arg['Bcc'] = array((isset($_POST['votremail']) ? $_POST['votremail'] : $user['email']));
// SEnd the mail
pwg_mail($_POST['sonmail'], $mail_arg);
}
$template->set_filenames(array('ecard' => ECARD_ROOT.'/template/ecard.tpl'));
$template->concat('COMMENT_IMG', $template->parse('ecard', true));
}
}
}
}
// Activation page administration
function ecard_admin_menu($menu)
{
array_push($menu,
array(
'NAME' => ECARD_NAME,
'URL' => get_admin_plugin_menu_link(ECARD_ADMIN_PATH.'admin.php')
)
);
return $menu;
}
}
?>