/****************************************************************************
** FUNCTION : Contains general functions
** DESCR. : kgui_datumFormat("DD-MM-JJJJ HH:MM") - maakt van een datum een leesbaardere versie
** kgui_processUploadedFile() - Upload a file (document or image). Image will be thumbnailed.
** kgui_fieldFormat() - Formats a value in to a string to be imported into MySQL command
** kgui_emailToWebmaster() - Sends an e-mail to the webmaster
** kgui_markeerZoekwoord()
** kgui_fileSize()
** check_fields()
** arraytostring()
** param_encode()
** param_decode()
** array_stripslashes()
** form_output()
** REVISION : 14-03-2009 - v1.0
**
** @package include/funcs.inc.php
*****************************************************************************/
include_once("/var/www/vhosts/kampergui.nl/public_html/include/resize.inc.php"); // Verkleinen foto
include_once("class.phpmailer.php"); // Send e-mail
/****************************************************************************
** FUNCTION : sendWebmasterEmail ( $logtext )
** DESCR. :
** PARAM. :
** REVISION : 19-01-2010 - v1.0
*****************************************************************************/
function sendWebmasterEmail($subject,$msg) {
$mail = new PHPmailer();
$mail->CharSet = "utf-8";
$mail->From = "beheer@kamperkogge.nl";
$mail->FromName = "[SYS] Kamper Kogge";
$mail->WordWrap = 80;
$mail->Body = $msg;
$mail->AltBody = $msg;
$mail->Subject = $subject;
$mail->IsHTML(false);
$mail->AddAddress ("pieter@kampergui.nl","Webmaster");
$mail->Send();
} // sendWebmasterEmail()
/****************************************************************************
** FUNCTION : kgui_log ( $logtext )
** DESCR. : Logs text $logtext into a system log file.
** PARAM. : $logtext = the text to be logged.
** REVISION : 02-05-09 - v1.0
*****************************************************************************/
function kgui_log ($logtext) { // V1.0
// if ( empty(SESSION::get('S_lid_id') ) )
file_put_contents ( CONST_SYS_UNIXpath.'/admin/log.txt', date('r').' | '.$_SERVER['REMOTE_ADDR'].' | '.substr(strrchr($_SERVER['SCRIPT_FILENAME'], "/"), 1).' | '.$logtext."\n", FILE_APPEND);
// else
// file_put_contents ( CONST_SYS_UNIXpath.'/log.txt', date('r').' | '.$_SERVER['REMOTE_ADDR'].' | '.SESSION::get('S_alias').' ('.SESSION::get('S_lid_id').') | '.$logtext."\n", FILE_APPEND);
} // kgui_log()
/****************************************************************************
** FUNCTION : kgui_abort ( $logtext )
** DESCR. : Logs text $logtext into a system log file AND EXITS system DUE UNRECOVERABLE ERROR
** PARAM. : $logtext = the text to be logged.
** REVISION : v1.0 22 aug 2009
** v1.1 30 aug 2010, Error numbers added
*****************************************************************************/
function kgui_abort ($error, $text1="", $text2="" ) { // V1.2
$msgNL = array();
$msgNL["MISSINGPAR"] = "Parameter %text1% ontbreekt";
$msgNL["UNKOWNACTION"] = "Onbekende actie %text1%";
$msgNL["NOACCOUNT"] = "Geen account ID beschikbaar (is leeg)";
$msgNL["DOUBBLEIDS"] = "Dubbele ID's (namen) voor %text1%";
$msgNL["TOOMAYTIMERS"] = "Te veel openstaande TIMER.TIMER's, aantal %text1%";
$msgNL["RECORDEXPECTED"] = "Record verwacht maar niet gevonden, %text1%. Found:%text2%";
$msgNL["DBERROR"] = "SQL-fout; '%text1%'\nQUERY: %text2%";
$msgNL["KEYEXIST"] = "Record index (key) bestaat reeds, %text1%";
$msgNL["COLOMNMISSING"] = "Tabelkolom niet gevonden: %text1%";
$msgNL["CONFIGMISSING"] = "Configuratie bestand niet gevonden: %text1%";
$msgText = $msgNL[$error];
if ( $msgText=="" ) $msgText = "Error code '".$error."' onbekend, aanvullende informatie: ".$text1." ".$text2 ;
else {
$msgText = str_replace("%text1%", $text1, $msgText);
$msgText = str_replace("%text2%", $text2, $msgText);
} // EndIf
//echo $msgText."
";
kgui_log("[".$error."] - ".$msgText);
kgui_log(" --- URI: ".$_SERVER['REQUEST_URI']);
// kgui_log(" --- PHP: ".$_SERVER['SCRIPT_FILENAME']);
// if ($_SERVER['QUERY_STRING']<>'') kgui_log(" --- QRY: ".$_SERVER['QUERY_STRING']);
echo "Er is een serieuze fout ontstaan. Waarschuw de webmaster (pieter@kampergui.nl).
" ;
echo "PHP: ".$_SERVER['SCRIPT_FILENAME']."
" ;
exit("[".$error."] - ".$msgText);
} // kgui_abort()
/****************************************************************************
** FUNCTION :
** DESCR. :
** PARAM. :
** REVISION :
*****************************************************************************/
function kgui_datumDateTimeToSQL ($datum) { // V1.0
if ( strlen($datum)<>19 ) return "0000-00-00 00:00:00";
else return substr($datum, 6,4)."-".substr($datum, 3,2)."-".substr($datum, 0,2).substr($datum, 10 );
} // kgui_datumDateTimeToSQL()
/****************************************************************************
** FUNCTION : kgui_datumFormat ( $datum )
** DESCR. : Converts the date with format DD-MM-JJJJ into a more readable (human)
** format. Using the names of the month, dates like; yesterday, today, tomorrow
** example 'Monday 12 april 2009'.
** PARAM. : $datum = the date to be formatted: INPUT FORMAT DD_MM_JJJJ HH:MM
** HH:MM are optional.
** REVISION : 15-03-09 - v1.1 - $Datum heeft de verplichte formaat DD-MM-JJJJ HH:MM
** USAGE : See ../Config/systemConfig-kw.php for definition of names Month / Days.
*****************************************************************************/
function kgui_datumFormat ($datum) { // V1.1
// $Datum heeft de verplichte formaat DD-MM-JJJJ HH:MM
if ($datum!='') {
$dag = (int)substr($datum, 0,2) ;
$maand = (int)substr($datum, 3,2) ;
$jaar = (int)substr($datum, 6,4) ;
$uur = substr($datum,11,2) ;
$minuut = substr($datum,14,2) ;
global $GLOBAL_maanden_kort ; // See ../Config/systemConfig.php
$gisteren = mktime(0, 0, 0, date("m"), date("d")-1, date("Y"));
$vandaag = mktime(0, 0, 0, date("m"), date("d") , date("Y"));
$morgen = mktime(0, 0, 0, date("m"), date("d")+1, date("Y"));
$datum = mktime(0, 0, 0, $maand, $dag, $jaar);
if ($datum==$vandaag) {
if ($uur=='') return 'vandaag';
else return 'vandaag om '.$uur.':'.$minuut;
} else if ($datum==$morgen) return 'morgen';
else if ($datum==$gisteren) {
if ($uur=='') return 'gisteren';
else return 'gisteren om '.$uur.':'.$minuut;
} else { // alle andere data
$datum = $dag.' '.$GLOBAL_maanden_kort[($maand-1)].' '.$jaar;
if ($uur!='') $datum = $datum.' '.$uur.':'.$minuut;
return $datum;
}
} else return ' ' ;
} // kgui_datumFormat()
/****************************************************************************
** FUNCTION : kgui_cutOffText ( $text, $maxsize, $linktext, $link )
** DESCR. : This function CUTS OFF the last part of a text $text,
** beginning at location $maxsize. The çuttoff'takes place at
** the last available SPACE. Then a '...' code is added to the
** end of the text. Then followed with a weblink $link,
** identified with de text $linktext.
** If textsize $text is smallen then $maxsize, no link info is displayed
** If $linktext is EMPTY, no link is given.
** PARAM. : $text = text to be displayed with a maximum op $maxsize characters.
** $maxsize = the maximum of chars to be display. From this location the last SPACE is used as a cutoff point.
** $linktext = text of the loink, as 'read more'.
** $link = URI to be linked to.
** REVISION : 15-03-09 - v1.0
*****************************************************************************/
function kgui_cutOffText ( $text, $maxsize=0, $linktext="", $link="" ) {
if ($maxsize>0) {
if (strlen($text)>$maxsize) {
$text = substr($text,0,$maxsize); // Afkappen op de lengte
// Zoeken naar een geschikt plek (SPATIE)
$lastSpace = strrpos($text,' ');
if ($lastSpace>0) $text = substr($text,0,$lastSpace).' …';
// Add - if requested- a 'read more' link
if ($linktext!='') {
$text = $text.'
'.$linktext.' »';
} // EndIf read more
} // EndIf Tekst was te lang dus afkappen
} // EndIf afkappen tekst
return $text;
} // kgui_cutOffText()
/****************************************************************************
** FUNCTION : kgui_processUploadedFile ( UploadFieldname )
** DESCR. : Checks and handles the UPLOADED file
** PARAM. : UploadFieldname is ARRAY $_FILES['name']
** REVISION : 11mrt2009 - v1.2
** : 1apr2009 - v1.3 Thumb photos in /thumb folder, to implement lightbox image viewer.
** 'foto_naam' and 'thumb_fotonaam', doesn't contain extension
** 5mei2009 - v1.4 New field 'naam_orgineel' added, contains original filename
** Reale filenames will be encrypted with MD5, to minimize double filenames
*****************************************************************************/
function kgui_processUploadedFile ( $UploadFieldname ) {
$resultArray['msg'] = ""; // systeem bericht (Error) voor de gebruiker
$resultArray['layout'] = ""; // 'landscape'|'stand-up', WIDTH|HEIGHT
$resultArray['error'] = 0; // No error
$resultArray['filesize'] = 0; // Bytes
$resultArray['naam_orgineel'] = basename( $UploadFieldname['name'] );
$resultArray['foto_naam'] = "";
$resultArray['foto_xsize'] = 0;
$resultArray['foto_ysize'] = 0;
$resultArray['thumb_foto_naam'] = "";
$resultArray['thumb_foto_xsize'] = 0;
$resultArray['thumb_foto_ysize'] = 0;
$extensionStart = strrpos($resultArray['naam_orgineel'],".");
$resultArray['extensie'] = strtolower(substr($resultArray['naam_orgineel'],($extensionStart+1)));
$newName = substr($resultArray['naam_orgineel'],0,$extensionStart).date('dmyHs');
$resultArray['foto_naam'] = md5 ( $newName, false );
$resultArray['thumb_foto_naam'] = $resultArray['foto_naam'] ;
if ( $extensionStart <=0 || !($resultArray['extensie']=='jpg' || $resultArray['extensie']=='png' || $resultArray['extensie']=='gif')) {
$resultArray['msg'] = "Bestand '".$resultArray['naam_orgineel']."' is geen juiste bestandformaat (.JPG, .PNG, .GIF).";
$resultArray['error'] = 1;
kgui_log($resultArray['msg']);
} else {
$vTargetPath = urldecode ( CONST_SYS_UNIXpath.'/images/fotos/'.$resultArray['foto_naam'].'.'.$resultArray['extensie'] ) ; // Make path of the file from the UNIX root system
$vTargetThumbPath = urldecode ( CONST_SYS_UNIXpath.'/images/fotos/thumb/'.$resultArray['thumb_foto_naam'].'.'.$resultArray['extensie'] ) ;
if ( $UploadFieldname['error'] > 0 ) { // File UPLOADED, with ERRORS
$resultArray['msg'] = "Foto bestand '".$resultArray['foto_naam'].".".$resultArray['extensie']."' is verstuurd met een error (".$UploadFieldname['error'].")";
$resultArray['error'] = 1;
kgui_log($resultArray['msg']);
} else { // Bestand is tijdelijk opgeslagen, CONTROLEER deze voordat deze definitief wordt
if ( file_exists($vTargetPath) ) { // Bestand bestaat reeds en wordt overschreven
$resultArray['msg'] = "Bestand '".$resultArray['foto_naam'].".".$resultArray['extensie']."' bestond reeds en is overschreven met de nieuwe versie (".round($UploadFieldname['size'] / 1024, 1)." KB)
";
kgui_log($resultArray['msg']);
unlink($vTargetPath); // Verwijder oude bestand
unlink($vTargetThumbPath); // Verwijder oude bestand
$resultArray['filesize'] = $UploadFieldname['size'];
$resultArray['error'] = 0;
} // EndIf BESTAND BESTOND REEDS
if ( ! move_uploaded_file($UploadFieldname['tmp_name'],$vTargetPath) ) {
$resultArray['msg'] = "Bestand '".$resultArray['foto_naam'].".".$resultArray['extensie']."' is niet verwerkt i.v.m. een error (". $UploadFieldname['error'].")";
$resultArray['error'] = 1;
kgui_log($resultArray['msg']);
} else {
// File stored on server
if ( $resultArray['extensie']=='jpg' || $resultArray['extensie']=='png' || $resultArray['extensie']=='gif' ) { // Is uploaded file an IMAGE ?
// Check PHOTO dimensions
// Photo lay-out ; LANDSCAPE or STAND-UP
$thumb=new thumbnail($vTargetPath); // prepare to generate "photo.jpg .png .gif"
$shrinkMaster = '';
if ($thumb->imageAttribute('xFoto')>$thumb->imageAttribute('yFoto')) { // LANDSCAPE PHOTO (wide)
$resultArray['layout'] = "landscape";
if ($thumb->imageAttribute('xFoto')>800) $shrinkMaster = '800'; // Max. breedte voor landscape
} else { // STAND-UP PHOTO (height)
$resultArray['layout'] = "stand-up";
if ($thumb->imageAttribute('yFoto')>560) $shrinkMaster = '560'; // Max. hoogte voor pasfoto formaat
} // EndIf
// Shrink IMAGE master is to big
// landscape => max. 800px width
// pasfoto => 560px height
if ($shrinkMaster<>'') {
$vTargetPathShrink = urldecode ( CONST_SYS_UNIXpath.'/images/fotos/'.$resultArray['foto_naam'].'_Shrink.'.$resultArray['extensie'] ) ;
if ($resultArray['layout'] == "landscape") $thumb->size_width((int)$shrinkMaster);
else $thumb->size_height((int)$shrinkMaster); // set width for thumbnail with 125 pixels
$thumb->save($vTargetPathShrink); // save my shrinked image to file "xxx_Shrink.jpg"
unlink ($vTargetPath); // Delete original file
rename ($vTargetPathShrink,$vTargetPath); // Rename SHRUNKEN file to original file
} // EndIf Shrink Master
// Make THUMBNAIL
// see online : http://kentung.f2o.org/scripts/thumbnail/sample.php
// Code changed 'resize.inc.php' THUMBNAIL pasfoto always in landscape format
$thumb=new thumbnail($vTargetPath); // prepare to generate "photo.jpg .png .gif" in directory "/www"
$thumb->size_width(125); // set width for thumbnail with 125 pixels
$thumb->saveAsLandscape($vTargetThumbPath); // save my thumbnail to file "huhu.jpg" in directory "/www/thumb
$resultArray['foto_xsize'] = $thumb->imageAttribute('xFoto');
$resultArray['foto_ysize'] = $thumb->imageAttribute('yFoto');
$resultArray['thumb_foto_xsize'] = $thumb->imageAttribute('xThumb');
$resultArray['thumb_foto_ysize'] = $thumb->imageAttribute('yThumb');
} // EndIf processing IMAGE
//chmod($vTargetPath, 644);
} // File uploaded and processed
} //EndIf File UPLOADED, no ERRORS
} // FOTO filenaam is OK
return $resultArray;
} // kgui_processUploadedFile()
/****************************************************************************
** Function : kgui_fieldFormat($vValue, $vType)
** DESCR. : Converts INPUT $vValue into the correct MySQL format. Including MAGIC QUOTES and SLASHES.
** PARAM. : $vValue = The value to be converted
** $vType = Conversion type ['txt','str','bool','int','float','?dates?']
** REVISION : 26-03-2009 - v1.0
** 10 jan 2010 V1.1 - _unformatted added
** 12 jan 2010 V1.2 - Format "DD-MM-JJJJ HH:MM:SS" added
*****************************************************************************/
function kgui_fieldFormat ($vValue, $vType) {
global $DB_connectie; // Database connection LINK, see Config/systemConnection-kw.php
// kgui_log("vType:".$vType.", vValue:".$vValue);
switch ($vType) {
case 'txt' :
if(get_magic_quotes_gpc()) $str=stripslashes($vValue);
else $str=$vValue;
$order = array("\r\n", "\n", "\r"); // Processes \r\n's first so they aren't converted twice.
$str = str_replace($order, '
', $str);
return '\''.mysql_real_escape_string($str, $DB_connectie).'\'';
break;
case 'str' :
if(get_magic_quotes_gpc()) $str=stripslashes($vValue);
else $str=$vValue;
return '\''.mysql_real_escape_string($str, $DB_connectie).'\'';
break;
case 'likestr' :
if(get_magic_quotes_gpc()) $str=stripslashes($vValue);
else $str=$vValue;
return '\'%'.mysql_real_escape_string($str, $DB_connectie).'%\'';
break;
case 'int' :
if ($vValue==""||$vValue==NULL) return "NULL";
else return (int)$vValue;
break;
case 'float' :
if ($vValue==""||$vValue==NULL) return "NULL";
else return (float)$vValue;
break;
case 'bool' :
if ( $vValue=="" ) $vValue="0";
else if ($vValue=="0" || $vValue==0) $vValue="0";
else $vValue="1";
return "'".$vValue."'";
break;
case 'DD-MM-JJJJ' :
if ($vValue=="") return "NULL";
return "STR_TO_DATE('".$vValue."','%d-%m-%Y')";
break;
case 'DDMMJJJJ' :
if ($vValue=="") return "NULL";
return "STR_TO_DATE('".$vValue."','%d%m%Y')";
break;
case 'DD-MM-JJJJ HH:MM:SS' :
if ($vValue=="") return "NULL";
return "STR_TO_DATE('".$vValue."','%d-%m-%Y %H:%i:%s')";
break;
case '_unformatted' :
return $vValue;
break;
} // EndSwitch
} // kgui_fieldFormat()
/****************************************************************************
** Function : kgui_fileSize($fileName)
*****************************************************************************/
if(!function_exists('str_ireplace')){
function str_ireplace($search,$replace,$subject){
$token = chr(1);
$haystack = strtolower($subject);
$needle = strtolower($search);
while (($pos=strpos($haystack,$needle))!==FALSE){
$subject = substr_replace($subject,$token,$pos,strlen($search));
$haystack = substr_replace($haystack,$token,$pos,strlen($search));
}
$subject = str_replace($token,$replace,$subject);
return $subject;
}
} // EndIF PHP < 5.0 str_ireplace() complement
function kgui_markeerZoekwoord ( $vTekst, $vZoek, $vClass ) {
if ( $vZoek!="" ) {
return str_ireplace ($vZoek, "".$vZoek."", $vTekst);
} else return $vTekst ;
} // kgui_markeerZoekwoord()
/****************************************************************************
** Function : kgui_fileSize($fileName)
*****************************************************************************/
function kgui_fileSize ($fileName) {
$milj = (1024*1024);
if ( file_exists ( urldecode ( CONST_SYS_UNIXpath.$fileName ))) {
$fileSize = filesize ( urldecode ( CONST_SYS_UNIXpath.$fileName ) );
if ( $fileSize < 1024 ) return $fileSize." B";
else { if ( $fileSize < $milj ) return round($fileSize/1024,1)." KB";
else return round($fileSize/$milj,1)." MB"; }
} else return "-";
} // kgui_fileSize ()
/****************************************************************************
** Function checks input variable by fixed criteries
**
** @function check_fields
** @package include/funcs.inc.php
** @param string val Variable to be checked
** @param string type Type of variable. s=string, d=date, i=integer, f=float
** @param integer min Minimal value of variable (for strings minimal length of variable)
** @param integer max Maximal value of variable (for strings maxiaml length of variable)
** @param array list List of possible values of input variable
** @return boolean true/false
**
*****************************************************************************/
function check_fields($val,$type='s',$min=false,$max=false,$list=false) {
switch ($type) {
case 's':
if (($min!==false) and (strlen($val)<$min)) return 0;
if (($max!==false) and (strlen($val)>$max)) return 0;
if ((is_array($list)) and (!in_array($val,$list))) return 0;
return 1;
break;
case 'i':
if (!is_numeric($val)) return 0;
if (($min!==false) and ($val<$min)) return 0;
if (($max!==false) and ($val>$max)) return 0;
if ((is_array($list)) and (!in_array(''.$val,$list))) return 0;
return 1;
break;
case 'd':
if(ereg("([0-9]{2})([0-9]{2})([0-9]{4})",$val,$re)) {
if (checkdate($re[2],$re[1],$re[3])) {
$tmpdate = mktime(0,0,0,$re[1],$re[2],$re[3]);
if (preg_match("/^([0-9]{2})([0-9]{2})([0-9]{4})$/",$min,$datemin)) {
if ($tmpdatemktime(0,0,0,$datemin[1],$datemin[2],$datemin[3])) return 0;
} else if ($max!==false) {
if ($tmpdate>$max) return 0;
}
if (is_array($list)) {
$res = 0;
while (list(,$dd) = each($list)) {
if (preg_match("/^([0-9]{2})([0-9]{2})([0-9]{4})$/",$dd,$datemin)) {
if ($tmpdate==mktime(0,0,0,$datemin[1],$datemin[2],$datemin[3])) $res = 1;
}else if ($dd) {
if ($tmpdate==$dd) $res = 1;
}
}
return $res;
}
return 1;
}
}
return 0;
break;
}
} //
/****************************************************************************
** Function transforms 2 dimension array into string
**
** @function arraytostring
** @package include/funcs.inc.php
** @param string arr Array
** @return string Transformed array
**
*****************************************************************************/
function arraytostring($arr) {
$str = '';
$rec_delimeter_begin="[";
$rec_delimeter_end="]";
$field_delimeter=",";
if (is_array($arr)) {
reset($arr);
$i=0;
while (list(,$val)=each($arr)) {
$str .= $rec_delimeter_begin.$val.$rec_delimeter_end;
if ($i++<(count($arr)-1)) $str .= $field_delimeter;
}
} else $str = $arr;
return $str;
}
/**
* Function clears all necessary session variables after patient submission
*
* @function clear_session_vars
* @package include/funcs.inc.php
*
**/
function clear_session_vars(){
reset($_SESSION);
while (list($key,$val)=each($_SESSION)) {
if (preg_match("/^((fld)|(ier))_[a-zA-Z_]+$/",$key) or preg_match("/^rd_scr\d{2}$/",$key)) {
if (is_array($_SESSION[$key])) unset($_SESSION[$key]);
else unset($_SESSION[$key]);
}
}
}
/**
* Shuffles data with random numbers and encodes with MIME base64
*
* @function param_encode
* @package include/forms.inc.php
* @param string data
* @return string Encoded data
*
**/
function param_encode($param) {
$param .= "{".session_id()."}";
for($i=0;$idata
*
**/
function param_decode($param) {
$mresult = preg_replace("/-/","",$param);
$mresult = substr($mresult,1,strlen($mresult));
if(($mresult % 4)!=0) $mresult .= "=";
$mresult = base64_decode($mresult);
for($i=0;$i'
* @param string input extra text which is added before main text, by default is empty string. This text is added if input text length greater than 0. This might be html for example '
'
* @param boolean input indicates if htmlspecialchars is needed
* @return string formatted string
*
**/
function form_output ( $instr, $alternative_txt='', $extra_text_after='', $extra_text_before='', $special_chars=true, $maxLength=0 ) { // v 1.1
if (!$instr) if ($maxLength!=0 && strlen($alternative_txt)>$maxLength) return substr($alternative_txt,0,$maxLength)."…";
else return $alternative_txt;
if ($special_chars) $outstr = htmlspecialchars($instr);
else $outstr = $instr;
if (trim($instr)) if ($maxLength!=0 && strlen($extra_text_before.$outstr.$extra_text_after)>$maxLength) return substr($extra_text_before.$outstr.$extra_text_after,0,$maxLength)."…";
else return $extra_text_before.$outstr.$extra_text_after;
else if ($maxLength!=0 && strlen($outstr)>$maxLength) return substr($outstr,0,$maxLength)."…";
else return $outstr ;
} // form_output()
?>