Location: PHPKode > projects > WSN Guest > wsn-guest/functions.php
<?php

function totalentries()
{
 global $db;
 $query = $db->select('id', 'linkstable', 'validated=1 AND hide=0', '', '');
 $num = $db->numrows($query);
 return $num;
}

function settingsreplacements($template)
{
 global $settings;
 $listofem = $settings->allnames();
 $listarray = explode(',', $listofem);
 $num = sizeof($listarray);
 for ($count=0; $count<$num; $count++)
 {
  $setvar = '{'. strtoupper($listarray[$count]) .'}';
  $template = str_replace($setvar, $settings->$listarray[$count], $template);
 }
 return $template;
}

function linkreplacements($template, $thelink)
{
 global $language, $isadmin, $templatesdir, $settings, $thismember, $noparsing, $admindir, $incomplete;

 if ($thelink->title == '') $template = str_replace('{LANG_VIEW_HOMEPAGE} <a href="{ENTRYURL}">{ENTRYTITLE}</a>', '', $template); 

 $template = str_replace('{TEMPLATESDIR}',$templatesdir, $template);
 $linkfields = explode(',', $settings->linkfields);
 $num = sizeof($linkfields);
 for ($count=0; $count<$num; $count++) 
 {
  $tempvar = '{ENTRY'. strtoupper($linkfields[$count]) .'}';
  if ($incomplete)
  {
   $replacewith = stripall(decodeit($thelink->$linkfields[$count]));
  }  
  else
  {
   $replacewith = decodeit($thelink->$linkfields[$count]);
   $replacewith = stripcode($replacewith);
   if (!$noparsing) $replacewith = parsemessage($replacewith);
  }
  if ($replacewith == '')  
  {
   $lang = strtoupper('{LANG_VIEW_'. $linkfields[$count] .'}');
   $toremove = $lang. ' '. $tempvar .' <br>';
   $template = str_replace($toremove, '', $template);
   $template = str_replace($templang, '', $template);
   $templang = '{LANG_VIEW_COUNTRY} {ENTRYCOUNTRY} <br>';
   $template = str_replace($templang, '', $template);
   $template = str_replace($tempvar, $replacewith, $template); 
  }
  else   $template = str_replace($tempvar, $replacewith, $template);
 }

 $template = str_replace('{ENTRYDATE}', $thelink->thedate(), $template);
 $template = str_replace('{ENTRYLASTEDITDATE}', $thelink->lasteditdate(), $template);
 $birthdate = getbirthdate($thelink->birthdate);
 if ($birthdate == '') $template = str_replace('{LANG_VIEW_BIRTHDATE} {ENTRYBIRTH} <br>', '', $template);
 else $template = str_replace('{ENTRYBIRTH}', getbirthdate($thelink->birthdate), $template);
 
 if ($thelink->aim != '') $template = str_replace('{ENTRYAIMIMG}', lesslinkreplacements($language->icon_aim, $thelink), $template);
 else $template = str_replace('{ENTRYAIMIMG}', '', $template);

 if ($thelink->icq != '') $template = str_replace('{ENTRYICQIMG}', lesslinkreplacements($language->icon_icq, $thelink), $template);
 else $template = str_replace('{ENTRYICQIMG}', '', $template); 

 if ($thelink->msn != '') $template = str_replace('{ENTRYMSNIMG}', lesslinkreplacements($language->icon_msn, $thelink), $template);
 else $template = str_replace('{ENTRYMSNIMG}', '', $template);

 if ($thelink->yahoo != '') $template = str_replace('{ENTRYYAHOOIMG}', lesslinkreplacements($language->icon_yahoo, $thelink), $template);
 else $template = str_replace('{ENTRYYAHOOIMG}', '', $template);

 if (strstr($template, '{ENTRYNEW}')) $new = marknew($thelink->time);
 $template = str_replace('{ENTRYNEW}', $new, $template);
 if (($thelink->id > 0) && ($thismember->canedit('link', $thelink->id)))
 {
    $path = 'edit.php?action=link&field=id&condition=equals&fieldvalue='. $thelink->id;
   $admin = str_replace('{PATH}', $path, $language->edit);
   $admin = str_replace('{TEMPLATESDIR}', $templatesdir, $admin);
 }
 $template = str_replace('{ENTRYADMIN}', $admin, $template);
 $template = str_replace('{DIRURL}', $settings->dirurl, $template);
 return $template;
}

function lesslinkreplacements($template, $thelink)
{
 global $language, $isadmin, $templatesdir, $settings, $thismember, $noparsing, $admindir;
 $template = str_replace('{TEMPLATESDIR}',$templatesdir, $template);
 $linkfields = explode(',', $settings->linkfields);
 $num = sizeof($linkfields);
 for ($count=0; $count<$num; $count++) 
 {
  $tempvar = '{ENTRY'. strtoupper($linkfields[$count]) .'}';
  $replacewith = decodeit($thelink->$linkfields[$count]);
  $replacewith = stripcode($replacewith);
  if (!$noparsing) $replacewith = parsemessage($replacewith);
  $template = str_replace($tempvar, $replacewith, $template);
 }
 return $template;
}


function langreplacements($template)
{
 global $language;
 $listofem = $language->allnames();
 $listarray = explode(',', $listofem);
 $num = sizeof($listarray);
 for ($count=0; $count<$num; $count++)
 {
  $setvar = '{LANG_'. strtoupper($listarray[$count]) .'}';
  if ($listarray[$count]) $template = str_replace($setvar, $language->$listarray[$count], $template);
 }
 return $template;
}

function commentreplacements($template, $comment)
{ 
 global $language, $isadmin, $templatesdir, $settings, $thismember, $noparsing, $admindir;
 $commentfields = explode(',', $settings->commentfields);
 $num = sizeof($commentfields);
 for ($count=0; $count<$num; $count++) 
 {
  $tempvar = '{COMMENT'. strtoupper($commentfields[$count]) .'}';
  $replacewith = stripcode(decodeit($comment->$commentfields[$count]));
  if (!$noparsing) $replacewith = parsemessage($replacewith);
  $template = str_replace($tempvar, $replacewith, $template);
 }
 $template = str_replace('{COMMENTDATE}', $comment->thedate(), $template);
 $template = str_replace('{COMMENTLASTEDITDATE}', $comment->lasteditdate(), $template); 
 if (($comment->id > 0) && ($thismember->canedit('comment', $comment->id)))
 {
    $path = 'edit.php?action=comment&field=id&condition=equals&fieldvalue='. $comment->id;
   $admin = str_replace('{PATH}', $path, $language->edit);
   $admin = str_replace('{TEMPLATESDIR}', $templatesdir, $admin);
 }
  $template = str_replace('{COMMENTADMIN}', $admin, $template);
 return $template;
}  


function memberreplacements($template, $amember)
{
 global $language, $isadmin, $templatesdir, $settings, $thismember, $noparsing, $admindir, $original;
 if ($original != '') $memberfields = explode(',', $settings->memberfields .','. $original);
 else $memberfields = explode(',', $settings->memberfields);
 $num = sizeof($memberfields);
 for ($count=0; $count<$num; $count++) 
 {
  $tempvar = '{MEMBER'. strtoupper($memberfields[$count]) .'}';
  $replacewith = decodeit($amember->$memberfields[$count]);
  $replacewith = stripcode($replacewith);
  if (!$noparsing) $replacewith = parsemessage($replacewith);
  $template = str_replace($tempvar, $replacewith, $template);
 }
 if (($amember->id > 0) && ($thismember->canedit('member', $amember->id)))
 {
    $path = 'edit.php?action=member&field=id&condition=equals&fieldvalue='. $amember->id;
   $admin = str_replace('{PATH}', $path, $language->edit);
   $admin = str_replace('{TEMPLATESDIR}', $templatesdir, $admin);
 }

 $template = str_replace('{MEMBERADMIN}', $admin, $template);
 $template = str_replace('{MEMBERREGDATE}', $amember->regdate(), $template);

 $ugfields = explode(',', $settings->usergroupfields);
 $num = sizeof($ugfields);
 for ($count=0; $count<$num; $count++) 
 {
  $tempvar = '{MEMBERGROUP'. strtoupper($ugfields[$count]) .'}';
  $item = 'group' . $ugfields[$count];
  $template = str_replace($tempvar, $amember->$item, $template);
 }

 return $template;
}

function marknew($time)
{
 global $settings;
 $maxdaysago = $settings->marknew;
 $maxtimeago = $maxdaysago * 24 * 60 * 60;
 $compare = time() - $time;
 if (($compare < $maxtimeago) && ($time > 0))
   $new = '<img src='. $settings->templatesdir .'/images/new.gif>';
 else
   $new = '';
 return $new;
}

function countryselector($current)
{
 global $language, $settings;
 $countries = explode(', ', $language->countrylist);
 $num = sizeof($countries);
 for ($x=0; $x<$num; $x++)
 {
  if ($countries[$x] == $current) $theoptions .= "<option value='$countries[$x]' selected>$countries[$x]</option>";
  else $theoptions .= "<option value='$countries[$x]'>$countries[$x]</option>";
 }
 return $theoptions;
}

function stateselector($current)
{
 global $language, $settings;
 $states = explode(', ', $language->statelist);
 $num = sizeof($states);
 for ($x=0; $x<$num; $x++)
 {
  if ($states[$x] == $current) $theoptions .= "<option value='$states[$x]' selected>$states[$x]</option>";
  else $theoptions .= "<option value='$states[$x]'>$states[$x]</option>";
 }
 return $theoptions;
}


function autovalidate($type, $usergroup)
{
 global $db;
 if ($type == 'link')
    $query = $db->select('validateentries', 'membergroupstable', "id=$usergroup", '', '');
 else if ($type == 'comments')
    $query = $db->select('validatecomments', 'membergroupstable', "id=$usergroup", '', '');
 
 $requirevalidate = $db->rowitem($query);
 if ($requirevalidate == 0) $validate = true; else $validate = false;
 return $validate;
}

function sendactivationcode($amember)
{
 global $language, $settings;
 $adminaddress = $settings->email;
 $submitter = $amember->email;
 $subject = memberreplacements($language->email_activationtitle, $amember);
 $message = memberreplacements($language->email_activationbody, $amember);   
 $message = str_replace('{DIRURL}', $settings->dirurl, $message);
 $message = str_replace('{ACTIVATIONSTRING}', $amember->password, $message); 
 $message = decodeit($message);
 sendemail("$submitter", "$subject", "$message", "From: $adminaddress");
 return true;
}


function entriesdata()
{ 
 global $db, $settings, $orderlinks, $begin, $toshow;
 if ($begin == '') $begin = 0;
 if ($toshow == '') $toshow = 1000;
 $condition = "hide=0 AND validated=1";
 if ($settings->condition != '') $condition .= ' AND '. $settings->condition;
 $entriesdata = $db->select($settings->linkfields, 'linkstable', $condition, $orderlinks, "LIMIT $begin,$toshow");
  return $entriesdata;
}

function getlinkinfo ($field, $fieldvalue, $condition)
{
 global $db, $settings;
 if ($field == 'catid')
 {
  $query = $db->select('id', 'categoriestable', "name='$fieldvalue'", '', '');
  $fieldvalue = $db->rowitem($query);
 }
 if ($condition == 'equals') $condition="$field='$fieldvalue'";
 if ($condition == 'like') $condition="$field LIKE '%$fieldvalue%'";
 if ($condition == 'greater') $condition="$field > '$fieldvalue'";
 if ($condition == 'less') $condition="$field < '$fieldvalue'";
 return $db->select($settings->linkfields, 'linkstable', $condition, '', '');
}

function getcommentinfo($field, $fieldvalue, $condition)
{
 global $db, $settings;
 if ($condition == 'equals') $condition="$field='$fieldvalue'";
 if ($condition == 'like') $condition="$field LIKE '%$fieldvalue%'";
 return $db->select($settings->commentfields, 'commentstable', $condition, '', '');
}

function getmemberinfo($field, $fieldvalue, $condition)
{
 global $db, $settings;
 if ($condition == 'equals') $condition="$field='$fieldvalue'";
 if ($condition == 'like') $condition="$field LIKE '%$fieldvalue%'";
 return $db->select($settings->memberfields, 'memberstable', $condition, '', '');
}

function getbirthdate($birthday)
{
 global $language;
 $birthday = explode('/', $birthday);
 $month = $birthday[0];
 $day = $birthday[1];
 $year = $birthday[2];
 
 if ($year == '0') $year = '';
 if ($day == '0') $day = '';
 $months = explode(', ', $language->dateformat_months);
 if ($month == '0') $themonth == '';
 else $themonth = $months[$month];
 $product = $language->dateformat_birthday;
 $product = str_replace('{DAY}', $day, $product);
 $product = str_replace('{YEAR}', $year, $product);
 $product = str_replace('{MONTH}', $themonth, $product);
 if (stristr($product, $day .', '. $year)) $product = str_replace(', '. $year, '', $product);
 if (!(($day > 0) || ($month > 0) || ($year > 0))) $product = '';
 return $product;
}


function dayoptions()
{
 $options = '<option value="0"></option>';
 $num = 32;
 for ($x=1; $x<$num; $x++)
 {
  $options .= '<option value="'. $x .'">'. $x .'</option>';
 }
 return $options;
}

function yearoptions()
{
 $options = '<option value="0"></option>';
 for ($x=2003; $x>1900; $x--)
 {
  $options .= '<option value="'. $x .'">'. $x .'</option>';
 }
 return $options;
}

function monthoptions()
{
 global $language;
 $options = '<option value="0"></option>';
 $months = explode(', ', $language->dateformat_months);
 $num = sizeof($months);
 for ($x=1; $x<=$num; $x++)
 {
  $options .= '<option value="'. $x .'">'. $months[$x] .'</option>';
 }
 return $options;
}

function usergroupoptions($selected)
{
 global $db;
 $query = $db->select('id,title', 'membergroupstable', 'id>0', 'ORDER BY id ASC', '');
 $num = $db->numrows($query);
 for ($x=0; $x<$num; $x++)
 {
  $next = $db->row($query);
  if ($next[0] == $selected)
    $usergroupoptions .= '<option value="'. $next[0] .'" selected>'. $next[1] .'</option>';
  else
    $usergroupoptions .= '<option value="'. $next[0] .'">'. $next[1] .'</option>';
 }
 return $usergroupoptions;
}

function sendthanks($entry)
{
 global $language, $settings;
 if ($entry->email != '')
 {
  $subject = linkreplacements($language->email_sendthankssubject, $entry);
  $message = linkreplacements($language->email_sendthanksbody, $entry);
  sendemail($entry->email, $subject, $message, 'From: '. $settings->email); 
 }
 return true;
}


function uploadfile($varname, $todo)
{
 global $settings, $_FILES;
 // contains full path to uploaded file in temprary storage
 $upload_temp = $_FILES[$varname]['tmp_name'];
 // get file name portion of source file
 $upload_file = $_FILES[$varname]['name'];
 // build target filename
 $ext = extension($_FILES[$varname]['name']);
 clearstatcache();
 $filesize = filesize($_FILES[$varname]['tmp_name']);
 if ($todo == 'randomize') $upload_file = md5(microtime()) .'.'. $ext;
 $target_file = $settings->uploadpath . $upload_file;
 $uploaded = false;
 $allowed = false;
 $types = explode(',', $settings->filetypes);
 $num = sizeof($types);
 for ($x=0; $x<$num; $x++) if ($types[$x] == $ext) $allowed = true;
 if (($filesize < $settings->filesize) && ($allowed))
 { 
  // try to copy file to real upload directory
  if (copy($upload_temp, $target_file))
  {
   $uploaded = $upload_file;
  }
 }
 return $uploaded; // returns file name if it worked, or false if failed
}

function uploadavatar($varname, $todo = '')
{
 global $settings, $memberstable, $db;
 $uploadpath = $settings->uploadpath;
 $upload_temp = $_FILES[$varname]['tmp_name'];
 $upload_file = $_FILES[$varname]['name'];
 $ext = extension($_FILES[$varname]['name']);
 clearstatcache();
 $filesize = filesize($_FILES[$varname]['tmp_name']);
 $upload_file = md5(microtime()) .'.'. $ext;
 $target_file = $uploadpath . $upload_file;
 $uploaded = false;
 $allowed = false;
 $types = explode(',', strtolower($settings->avatartypes));
 $num = sizeof($types);
 for ($x=0; $x<$num; $x++) if (strtolower($types[$x]) == strtolower($ext)) $allowed = true;
 $imagearray = getimagesize($upload_temp);
 $xwidth = $imagearray[0]; $yheight = $imagearray[1]; 
 if ($allowed && ($xwidth > $settings->avatarwidth || $yheight > $settings->avatarheight)) 
 {
	require_once 'classes/gd.php'; 
	move_uploaded_file($upload_temp, $target_file);
	$scale = min($settings->avatarwidth/$xwidth, $settings->avatarheight/$yheight);
	$new_width = floor($scale*$xwidth);
   $new_height = floor($scale*$yheight);
   $wbh = $new_width .'x'. $new_height;
   resizeimage($target_file, $new_width, $new_height);	
   $imagearray = getimagesize($target_file);	
   $filesize = filesize($target_file);   
   $uploaded = $upload_file;   
 }
 else
 { 
  if ($imagearray[0] > $settings->avatarwidth) $allowed = false;
  if ($imagearray[1] > $settings->avatarheight) $allowed = false;  
  if (($filesize < $settings->avatarsize) && ($allowed))
  { 
   // try to copy file to real upload directory
   if (move_uploaded_file($upload_temp, $target_file))
   {
    $uploaded = $upload_file;
   }
  }
 }  
 @chmod($target_file, 0666);    
 return $uploaded; // returns file name if it worked, or false if failed
}

?>
Return current item: WSN Guest