out of links
* get_page_name -> get the filename of the current page
* array_search_recursive -> search for a value/key combination in an array of arrays
* ByteStringFormat -> format bytes into KB, MB, GB, ...
* TimeStringFormat -> format a timestamp (seconds) into days, months, ... also with ms
* StringToTime -> reverste a TimeStringFormat to a timestamp
* GenAssocArray -> generactes a new associativ array from an existing array
* CheckDate -> checks if a date is valid
* CompareDate -> compares two dates. -1 if the first is smaller, 0 if they are equal, 1 if the first is bigger
* CompareDateTime -> compares two dates with time. -1 if the first is smaller, 0 if they are equal, 1 if the first is bigger
* _crc32b -> behaves like the hash("crc32b") in php < 5.2.8. this function will flip the hash like it was (wrong)
* before if a new php version is found
* crypt* -> encrypt and decrypt login string data, used by Login class
* setFormToken/validateFormToken -> form protection with token
*
* PRIVATE METHODS
* fdebug_fp -> opens and closes file, called from fdebug method
* write_error_msg -> writes error msg to file if requested
*
* HISTORY:
* 2010/12/24 (cs) add crypt classes with auto detect what crypt we can use, add php version check class
* 2008/08/07 (cs) fixed strange DEBUG_ALL on off behavour. data was written even thought DBEUG_ALL was off. now no debug logging is done at all if DEBUG_ALL is off
* 2007/11/13 (cs) add Comparedate function
* 2007/11/05 (cs) added GenAssocArray and CheckDate functions
* 2007/10/10 (cs) magic links function can use http:///path as a local prefix. blank target is removed & http:// also
* 2006/03/09 (cs) added Byte/TimeStringFormat functions
* 2006/02/21 (cs) fix various problems with the mime magic function: || not always working, fix prefix replacement, etc
* 2006/02/09 (cs) added _mb_mime_encode function, replacement for php internal one
* 2005/07/12 (cs) added some small stylesheet defs to debug output
* 2005/06/24 (cs) made the check selected/checked function way easier
* 2005/06/24 (cs) added a function to wrap around print_r for html formatted array print
* 2005/06/21 (cs) made the error_msg file writing immediatly after something is written with debug method
* 2005/06/20 (cs) added a quick to file write function, removed the mobile detect code
* 2005/06/20 (cs) test debug method, add surpress of
in debug output
* 2005/06/17 (cs) error_msg is an array, to put in various levels of error reporting
* 2005/04/06 (cs) added filename for error page when print to file
* 2005/05/31 (cs) added file printout of errors
* 2005/03/01 (cs) set a global regex for checking the email
* 2005/01/27 (cs) updated checked, haystack can be valur or array
* 2004/11/16 (cs) removed mobile detection here
* 2004/11/15 (cs) error_msg is no longer echoed, but returned
* 2004/11/15 (cs) added new functions: checked, magic_links, get_page_name
* 2004/08/06 (cs) bug with $_GLOBALS, should be $GLOBALS
* 2004/07/15 (cs) added print_error_msg method, updated to new schema
* 2003-06-09: added "detect_mobile" class for japanese mobile phone
* detection
* 2003-03-24: start of stub/basic class
*********************************************************************/
class basic
{
public $class_info; // class info var
public $page_name;
public $host_name;
public $host_port;
private $error_id; // error ID for errors in classes
private $error_string; // error strings in classes (for error_id)
private $error_msg = array (); // the "connection" to the outside errors
public $debug_output; // if this is true, show debug on desconstructor
public $debug_output_not;
public $debug_output_all;
public $echo_output; // errors: echo out, default is 1
public $echo_output_not;
public $echo_output_all;
public $print_output; // errors: print to file, default is 0
public $print_output_not;
public $print_output_all;
// log file name
private $log_file_name_ext = 'log'; // use this for date rotate
public $log_max_filesize = 0; // set in kilobytes
private $log_print_file = 'error_msg##LOGID####LEVEL####CLASS####PAGENAME####DATE##';
private $log_file_unique_id; // a unique ID set only once for call derived from this class
public $log_print_file_date = 1; // if set add Y-m-d and do automatic daily rotation
private $log_file_id = LOG_FILE_ID ? LOG_FILE_ID : ''; // a alphanumeric name that has to be set as global definition
public $log_per_level = 0; // set, it will split per level (first parameter in debug call)
public $log_per_class = 0; // set, will split log per class
public $log_per_page = 0; // set, will split log per called file
public $log_per_run = 0; // create a new log file per run (time stamp + unique ID)
public $starttime; // start time if time debug is used
public $endtime; // end time if time debug is used
public $email_regex; // regex var for email check
public $keitai_email_regex; // regex var for email check
public $error_msg_prefix = ''; // prefix to the error string (the class name)
public $debug_fp = ''; // filepointer for writing to file
public $debug_filename = 'debug_file.log'; // where to write output
public $hash_algo = 'crc32b'; // the hash algo used for the internal debug uid
public $running_uid = ''; // unique ID set on class init and used in logging as prefix
// data path for files
public $data_path = array ();
// error char for the char conver
public $mbErrorChar;
// crypt saslt prefix
public $cryptSaltPrefix = '';
public $cryptSaltSuffix = '';
public $cryptIterationCost = 7; // this is for staying backwards compatible with the old ones
public $cryptSaltSize = 22; // default 22 chars for blowfish, 2 for STD DES, 8 for MD5,
// session name
private $session_name = '';
private $session_id = '';
// form token (used for form validation)
private $form_token = '';
// METHOD __construct
// PARAMS debug_all (0)/1, echo_all (1)/0, print_all (0)/1
// RETURN none
// DESC constructor
public function __construct($debug_all = 0, $echo_all = 1, $print_all = 0)
{
// set per run UID for logging
$this->running_uid = hash($this->hash_algo, uniqid(rand(), true));
// internal info var
$this->class_info["basic"] = array (
"class_name" => "Basic",
"class_version" => "0.9.0",
"class_created" => "2003-03-24",
"class_author" => 'Clemens "Gullevek" Schwaighofer (.at)'
);
// set the page name
$this->page_name = $this->get_page_name();
$this->host_name = $this->get_host_name();
// set the paths matching to the valid file types
$this->data_path = array (
'P' => PICTURES,
'F' => FLASH,
'V' => VIDEOS,
'D' => DOCUMENTS,
'A' => PDFS,
'B' => BINARIES
);
// if given via parameters, only for all
$this->debug_output_all = $debug_all;
$this->echo_output_all = $echo_all;
$this->print_output_all = $print_all;
// globals overrule given settings, for one (array), eg $ECHO['db'] = 1;
if (isset($GLOBALS['DEBUG'])) {
$this->debug_output = $GLOBALS['DEBUG'];
}
if (isset($GLOBALS['ECHO'])) {
$this->echo_output = $GLOBALS['ECHO'];
}
if (isset($GLOBALS['PRINT'])) {
$this->print_output = $GLOBALS['PRINT'];
}
// exclude these ones from output
if (isset($GLOBALS['DEBUG_NOT'])) {
$this->debug_output_not = $GLOBALS['DEBUG_NOT'];
}
if (isset($GLOBALS['ECHO_NOT'])) {
$this->echo_output_not = $GLOBALS['ECHO_NOT'];
}
if (isset($GLOBALS['PRINT_NOT'])) {
$this->print_output_not = $GLOBALS['PRINT_NOT'];
}
// all overrule
if (isset($GLOBALS['DEBUG_ALL'])) {
$this->debug_output_all = $GLOBALS['DEBUG_ALL'];
}
if (isset($GLOBALS['ECHO_ALL'])) {
$this->echo_output_all = $GLOBALS['ECHO_ALL'];
}
if (isset($GLOBALS['PRINT_ALL'])) {
$this->print_output_all = $GLOBALS['PRINT_ALL'];
}
// GLOBAL rules for log writing
if (isset($GLOBALS['LOG_PRINT_FILE_DATE'])) {
$this->log_print_file_date = $GLOBALS['LOG_PRINT_FILE_DATE'];
}
if (isset($GLOBALS['LOG_PER_LEVEL'])) {
$this->log_per_level = $GLOBALS['LOG_PER_LEVEL'];
}
if (isset($GLOBALS['LOG_PER_CLASS'])) {
$this->log_per_class = $GLOBALS['LOG_PER_CLASS'];
}
if (isset($GLOBALS['LOG_PER_PAGE'])) {
$this->log_per_page = $GLOBALS['LOG_PER_PAGE'];
}
if (isset($GLOBALS['LOG_PER_RUN'])) {
$this->log_per_run = $GLOBALS['LOG_PER_RUN'];
}
// set the regex for checking emails
$this->email_regex = "^[A-Za-z0-9!#$%&'*+-\/=?^_`{|}~][A-Za-z0-9!#$%:\(\)&'*+-\/=?^_`{|}~\.]{0,63}@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]{1,})*\.([a-zA-Z]{2,}){1}$";
// this is for error check parts in where the email regex failed
$this->email_regex_check = array (
1 => "@(.*)@(.*)", // double @
2 => "^[A-Za-z0-9!#$%&'*+-\/=?^_`{|}~][A-Za-z0-9!#$%:\(\)&'*+-\/=?^_`{|}~\.]{0,63}@", // wrong part before @
3 => "@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]{1,})*\.([a-zA-Z]{2,}){1}$", // wrong part after @
4 => "@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]{1,})*\.", // wrong domain name part
5 => "\.([a-zA-Z]{2,6}){1}$", // wrong top level part
6 => "@(.*)\.{2,}", // double .. in domain name part
7 => "@.*\.$" // ends with a dot, top level, domain missing
);
// the array with the mobile types that are valid
$this->mobile_email_type = array (
'.*@docomo\.ne\.jp$' => 'keitai_docomo',
'.*@([a-z0-9]{2}\.)?ezweb\.ne\.jp$' => 'keitai_kddi_ezweb', # correct are a[2-4], b2, c[1-9], e[2-9], h[2-4], t[1-9]
'.*@(ez[a-j]{1}\.)?ido\.ne\.jp$' => 'keitai_kddi_ido', # ez[a-j] or nothing
'.*@([a-z]{2}\.)?sky\.tu-ka\.ne\.jp$' => 'keitai_kddi_tu-ka', # (sky group)
'.*@([a-z]{2}\.)?sky\.tk[kc]{1}\.ne\.jp$' => 'keitai_kddi_sky', # (sky group) [tkk,tkc only]
'.*@([a-z]{2}\.)?sky\.dtg\.ne\.jp$' => 'keitai_kddi_dtg', # dtg (sky group)
'.*@[tkdhcrnsq]{1}\.vodafone\.ne\.jp$' => 'keitai_softbank_vodafone', # old vodafone [t,k,d,h,c,r,n,s,q]
'.*@jp-[dhtkrsnqc]{1}\.ne\.jp$' => 'keitai_softbank_j-phone', # very old j-phone (pre vodafone) [d,h,t,k,r,s,n,q,c]
'.*@([dhtcrknsq]{1}\.)?softbank\.ne\.jp$' => 'keitai_softbank', # add i for iphone also as keitai, others similar to the vodafone group
'.*@i{1}\.softbank(\.ne)?\.jp$' => 'smartphone_softbank_iphone', # add iPhone also as keitai and not as pc
'.*@disney\.ne\.jp$' => 'keitai_softbank_disney', # (kids)
'.*@willcom\.ne\.jp$' => 'keitai_willcom',
'.*@willcom\.com$' => 'keitai_willcom', # new for pdx.ne.jp address
'.*@wcm\.ne\.jp$' => 'keitai_willcom', # old willcom wcm.ne.jp
'.*@pdx\.ne\.jp$' => 'keitai_willcom_pdx', # old pdx address for willcom
'.*@bandai\.jp$' => 'keitai_willcom_bandai', # willcom paipo! (kids)
'.*@pipopa\.ne\.jp$' => 'keitai_willcom_pipopa', # willcom paipo! (kids)
'.*@([a-z0-9]{2,4}\.)?pdx\.ne\.jp$' => 'keitai_willcom_pdx', # actually only di,dj,dk,wm -> all others are "wrong", but none also allowed?
'.*@ymobile([1]{1})?\.ne\.jp$' => 'keitai_willcom_ymobile', # ymobile, ymobile1 techincally not willcom, but I group them there
'.*@y-mobile\.ne\.jp$' => 'keitai_willcom_ymobile', # y-mobile techincally not willcom, but I group them there
'.*@emnet\.ne\.jp$' => 'keitai_willcom_emnet', # e-mobile, group will willcom
'.*@emobile\.ne\.jp$' => 'keitai_willcom_emnet', # e-mobile, group will willcom
'.*@emobile-s\.ne\.jp$' => 'keitai_willcom_emnet' # e-mobile, group will willcom
);
// short list for mobile email types
$this->mobile_email_type_short = array (
'keitai_docomo' => 'docomo',
'keitai_kddi_ezweb' => 'kddi',
'keitai_kddi' => 'kddi',
'keitai_kddi_tu-ka' => 'kddi',
'keitai_kddi_sky' => 'kddi',
'keitai_softbank' => 'softbank',
'smartphone_softbank_iphone' => 'iphone',
'keitai_softbank_disney' => 'softbank',
'keitai_softbank_vodafone' => 'softbank',
'keitai_softbank_j-phone' => 'softbank',
'keitai_willcom' => 'willcom',
'keitai_willcom_pdx' => 'willcom',
'keitai_willcom_bandai' => 'willcom',
'keitai_willcom_pipopa' => 'willcom',
'keitai_willcom_ymobile' => 'willcom',
'keitai_willcom_emnet' => 'willcom',
'pc_html' => 'pc',
// old sets -> to be removed later
'docomo' => 'docomo',
'kddi_ezweb' => 'kddi',
'kddi' => 'kddi',
'kddi_tu-ka' => 'kddi',
'kddi_sky' => 'kddi',
'softbank' => 'softbank',
'keitai_softbank_iphone' => 'iphone',
'softbank_iphone' => 'iphone',
'softbank_disney' => 'softbank',
'softbank_vodafone' => 'softbank',
'softbank_j-phone' => 'softbank',
'willcom' => 'willcom',
'willcom_pdx' => 'willcom',
'willcom_bandai' => 'willcom',
'willcom_pipopa' => 'willcom',
'willcom_ymobile' => 'willcom',
'willcom_emnet' => 'willcom',
'pc' => 'pc'
);
// initial the session if there is no session running already
if (!session_id()) {
// check if we have an external session name given, else skip this step
if (SET_SESSION_NAME) {
// set the session name for possible later check
$this->session_name = SET_SESSION_NAME;
session_name($this->session_name);
}
// start session
session_start();
// set internal session id, we can use that later for protection check
$this->session_id = session_id();
}
// init crypt settings
$this->cryptInit();
// start logging running time
$this->running_time();
}
// METHOD __destruct
// PARAMS none
// RETURN if debug is on, return error data
// DESC basic deconstructor (should be called from all deconstructors in higher classes)
// writes out $error_msg to global var
public function __destruct()
{
// this has to be changed, not returned here, this is the last class to close
// return $this->error_msg;
// close open file handles
// $this->fdebug_fp('c');
}
// *************************************************************
// GENERAL METHODS
// *************************************************************
// METHOD db_io_info
// PARAMS show, default 1, if set to 0 won't write to error_msg var
// RETURN string with info
// DESC default class info (prints out class info)
public function info($class_name = "basic", $stdio = 0)
{
unset($string);
list($major, $minor, $patchlvl) = explode(".", $this->class_info[$class_name]["class_version"]);
$string .= "-Class-info-[".$class_name."]-> Class Name: ".$this->class_info[$class_name]["class_name"]."
";
$string .= "-Class-info-[".$class_name."]-> Class Author: ".$this->class_info[$class_name]["class_author"]."
";
$string .= "-Class-info-[".$class_name."]-> Class Version: ".$this->class_info[$class_name]["class_version"]."
";
$string .= "-Class-info-[".$class_name."]-> Class Revision: ".$this->class_info[$class_name]["class_revision"]."
";
$string .= "-Class-info-[".$class_name."]-> Class Created: ".$this->class_info[$class_name]["class_created"]."
";
$string .= "-Class-info-[".$class_name."]-> Class Last Change: ".$this->class_info[$class_name]["class_last_changed"]."";
if ($stdio) {
echo $string.'
';
} else {
$this->debug('info', '
'.$string);
}
return $string;
}
public function running_time($simple = false)
{
list($micro, $timestamp) = explode(" ", microtime());
$string = '';
$running_time = '';
if (!$this->starttime) {
$this->starttime = ((float)$micro + (float)$timestamp);
$string .= $simple ? 'Start: ' : "Started at: ";
} else {
$this->endtime = ((float)$micro + (float)$timestamp);
$string .= $simple ? 'End: ' : "Stopped at: ";
}
$string .= date("Y-m-d H:i:s", $timestamp);
$string .= " ".$micro;
if ($this->starttime && $this->endtime) {
$running_time = $this->endtime - $this->starttime;
$string .= ($simple ? 'Run: ' : "
Script running time: ").$running_time." s";
}
// $this->debug('info', $string);
return $running_time;
}
// METHOD print_time
// PARAMS $set_microtime, 0 shows none, default (-1) shows all, positive number is for rounding
// RETURN formated datetime string
// DESCRIPTION
// wrapper around microtime function to print out y-m-d h:i:s.ms
public static function print_time($set_microtime = -1)
{
list($microtime, $timestamp) = explode(" ", microtime());
$string = date("Y-m-d H:i:s", $timestamp);
// if microtime flag is -1 no round, if 0, no microtime, if >= 1, round that size
if ($set_microtime == -1) {
$string .= substr($microtime, 1);
} elseif ($set_microtime >= 1) {
$string .= substr(round($microtime, $set_microtime), 1);
}
return $string;
}
// METHOD fdebug
// PARAMS $string: data to write to file
// $enter: default on true, if set to false, no linebreak (\n) will be put at the end
// RETURN none
// DESCRIPTION
// writes a string to a file immediatly, for fast debug output
public function fdebug($string, $enter = 1)
{
if ($this->debug_filename) {
$this->fdebug_fp();
if ($enter) {
$string .= "\n";
}
$string = "[".$this->print_time()."] [".$this->get_page_name(2)."] - ".$string;
fwrite($this->debug_fp, $string);
$this->fdebug_fp();
}
}
// METHOD fdebug_fp
// PARAMS $flag: default '', 'o' -> open, 'c' -> close
// RETURN none
// DESCRIPTION
// if no debug_fp found, opens a new one; if fp exists close it
private function fdebug_fp($flag = '')
{
if (!$this->debug_fp || $flag == 'o') {
$fn = ROOT.LOG.$this->debug_filename;
$this->debug_fp = @fopen($fn, 'a');
} elseif ($this->debug_fp || $flag == 'c') {
fclose($this->debug_fp);
}
}
// METHOD debug_for
// PARAMS $type: error, echo, print
// $flag: on/off
// $array of levels to turn on/off debug
// RETURN none
// DESCRIPTION
// passes list of level names, to turn on debug
// eg $foo->debug_for('print', 'on', array('LOG', 'DEBUG', 'INFO'));
public function debug_for($type, $flag)
{
$debug_on = func_get_args();
array_shift($debug_on); // kick out type
array_shift($debug_on); // kick out flag (on/off)
if (count($debug_on) >= 1) {
foreach ($debug_on as $level) {
$switch = $type."_output";
if ($flag == 'off') {
$switch .= '_not';
}
$this->{$switch}[$level] = 1;
}
}
}
// METHOD debug
// PARAMS $level: id for error message, groups messages together
// $string: the actual error message
// $strip: default on false, if set to true, all html tags will be stripped and
changed to \n
// this is only used for debug output
// RETURN none
// DESCRIPTION
// write debug data to error_msg array
public function debug($level, $string, $strip = false)
{
if (($this->debug_output[$level] || $this->debug_output_all) && !$this->debug_output_not[$level]) {
if (!isset($this->error_msg[$level])) {
$this->error_msg[$level] = '';
}
$error_string = '';
$error_string .= '['.$this->print_time().'] ';
$error_string .= '['.$level.'] ';
$error_string .= '['.$this->host_name.'] ';
$error_string .= '['.$this->page_name.'] ';
$error_string .= '['.$this->running_uid.'] ';
$error_string .= '{'.get_class($this).'} - '.$string;
$error_string .= "
";
if ($strip) {
// find any
and replace them with \n
$string = str_replace('
', "\n", $string);
// strip rest of html elements
$string = preg_replace("/(<\/?)(\w+)([^>]*>)/", "", $string);
}
// same string put for print (no html crap inside)
$error_string_print = '['.$this->print_time().'] ['.$this->host_name.'] ['.$this->get_page_name(2).'] ['.$this->running_uid.'] {'.get_class($this).'} <'.$level.'> - '.$string;
$error_string_print .= "\n";
// write to file if set
$this->write_error_msg($level, $error_string_print);
// write to error level
if (($this->echo_output[$level] || $this->echo_output_all) && !$this->echo_output_not[$level]) {
$this->error_msg[$level] .= $error_string;
}
}
}
// METHOD: get_caller_method
// PARAMS: none (optional later: which level to deb ug)
// RETURN: null or the function that called the function where this methid is called
// DESC : if there is a need to find out which parent method called a child method, eg for debugging, this function does this
// call this method in the child method and you get the parent function that called it
public function get_caller_method($level = 2)
{
$traces = debug_backtrace();
// extended info (later)
/*
* $file = $trace[$level]['file'];
* $line = $trace[$level]['line'];
* $object = $trace[$level]['object'];
* if (is_object($object)) { $object = get_class($object); }
*
* return "Where called: line $line of $object \n(in $file)";
*/
// sets the start point here, and in level two (the sub call) we find this
if (isset($traces[$level])) {
return $traces[$level]['function'];
}
return null;
}
// METHOD merge_errors
// PARAMS error array
// RETURN none
// DESC merges the given error array with the one from this class
// only merges visible ones
public function merge_errors($error_msg = array ())
{
if (!is_array($error_msg)) {
$error_msg = array ();
}
foreach ($error_msg as $level => $msg) {
$this->error_msg[$level] .= $msg;
}
}
// METHOD print_error_msg
// PARAMS $string: prefix string for header
// RETURN error msg for all levels
// DESCRIPTION
// prints out the error string
public function print_error_msg($string = '')
{
$string_output = '';
if ($this->debug_output_all) {
if ($this->error_msg_prefix) {
$string = $this->error_msg_prefix;
}
$script_end = $this->running_time();
while (list($level, $temp_debug_output) = each($this->error_msg)) {
if (($this->debug_output[$level] || $this->debug_output_all) && !$this->debug_output_not[$level]) {
if (($this->echo_output[$level] || $this->echo_output_all) && !$this->echo_output_not[$level]) {
$string_output .= '['.$level.'] '.(($string) ? "**** ".$this->htmlent($string)." ****\n" : "").'
';
$string_output .= $temp_debug_output;
} // echo it out
} // do printout
} // for each level
// create the output wrapper around, so we have a nice formated output per class
if ($string_output) {
$string_prefix = '';
$string_prefix .= '
{'.get_class($this).'}
';
$string_output = $string_prefix.$string_output;
$string_output .= '
Script Run Time: '.$script_end.'
';
$string_output .= '
';
}
}
return $string_output;
}
// METHOD write_error_msg
// PARAMS $level: the level to write
// RETURN none
// DESCRIPTION
// writes error msg data to file for current level
private function write_error_msg($level, $error_string)
{
if (($this->debug_output[$level] || $this->debug_output_all) && !$this->debug_output_not[$level]) {
// only write if write is requested
if (($this->print_output[$level] || $this->print_output_all) && !$this->print_output_not[$level]) {
// replace all html tags
// $error_string = preg_replace("/(<\/?)(\w+)([^>]*>)/", "##\\2##", $error_string);
// $error_string = preg_replace("/(<\/?)(\w+)([^>]*>)/", "", $error_string);
// replace special line break tag
// $error_string = str_replace('', "\n", $error_string);
// init output variable
$output = $error_string; // output formated error string to output file
// init base file path
$fn = ROOT.LOG.$this->log_print_file.'.'.$this->log_file_name_ext;
// log ID prefix settings, if not valid, replace with empty
if (preg_match("/^[A-Za-z0-9]+$/", $this->log_file_id)) {
$rpl_string = '_'.$this->log_file_id;
} else {
$rpl_string = '';
}
$fn = str_replace('##LOGID##', $rpl_string, $fn); // log id (like a log file prefix)
if ($this->log_per_run) {
if ($GLOBALS['LOG_FILE_UNIQUE_ID']) {
$this->log_file_unique_id = $GLOBALS['LOG_FILE_UNIQUE_ID'];
}
if (!$this->log_file_unique_id) {
$GLOBALS['LOG_FILE_UNIQUE_ID'] = $this->log_file_unique_id = date('Y-m-d_His').'_U_'.substr(hash('sha1', uniqid(mt_rand(), true)), 0, 8);
}
$rpl_string = '_'.$this->log_file_unique_id; // add 8 char unique string
} else {
$rpl_string = !$this->log_print_file_date ? '' : '_'.date('Y-m-d'); // add date to file
}
$fn = str_replace('##DATE##', $rpl_string, $fn); // create output filename
$rpl_string = !$this->log_per_level ? '' : '_'.$level; // if request to write to one file
$fn = str_replace('##LEVEL##', $rpl_string, $fn); // create output filename
$rpl_string = !$this->log_per_class ? '' : '_'.get_class($this); // set sub class settings
$fn = str_replace('##CLASS##', $rpl_string, $fn); // create output filename
$rpl_string = !$this->log_per_page ? '' : '_'.$this->get_page_name(1); // if request to write to one file
$fn = str_replace('##PAGENAME##', $rpl_string, $fn); // create output filename
// write to file
// first check if max file size is is set and file is bigger
if ($this->log_max_filesize > 0 && ((filesize($fn) / 1024) > $this->log_max_filesize)) {
// for easy purpose, rename file only to attach timestamp, nur sequence numbering
rename($fn, $fn.'.'.date("YmdHis"));
}
$fp = fopen($fn, 'a');
if ($fp !== false) {
fwrite($fp, $output);
fclose($fp);
} else {
echo "";
}
} // do write to file
}
}
// METHOD reset_error_msg
// PARAMS level -> optional level
// RETURN none
// DESC unsests the error message array
// can be used if writing is primary to file
// if no level given resets all
public function reset_error_msg($level = '')
{
if (!$level) {
unset($this->error_msg);
} else {
unset($this->error_msg[$level]);
}
}
// METHOD ErrorHandler
// PARAMS none
// RETURN none
// DESC catch function to handle all errors that are not handled by php itself
// eg all errors that would be surpressed are written to a log file if this function is enabled
// to use it call with set_error_handler(array("baisc", "ErrorHandler"));
// NOTE: this will only catch any additional erros created AFTER the set_error_hanlder was set, so mostly no strict/notices from the classes are visible
public static function ErrorHandler($type, $message, $file, $line, $context)
{
// error levels for PHP
// values based on 5.3
$error_level = array (
1 => 'E_ERROR',
2 => 'E_WARNING',
4 => 'E_PARSE',
8 => 'E_NOTICE',
16 => 'E_CORE_ERROR',
32 => 'E_CORE_WARNING',
64 => 'E_COMPILE_ERROR',
128 => 'E_COMPILE_WARNING',
256 => 'E_USER_ERROR',
512 => 'E_USER_WARNING',
1024 => 'E_USER_NOTICE',
2048 => 'E_STRICT',
4096 => 'E_RECOVERABLE_ERROR', // since 5.2
8192 => 'E_DEPRICATED', // since 5.3
16384 => 'E_USER_DEPRICATED', // since 5.3
30719 => 'E_ALL' // 6143 in 5.2, 2047 in previous versions
);
$fn = ROOT.LOG.'php_errors-'.date('Y-m-d').'.log';
$output = '['.basic::print_time().'] {'.basic::get_page_name().'} ['.$file.'] <'.$line.'> ['.$error_level[$type].'|'.$type.']: '.$message."\n";
$fp = fopen($fn, 'a');
fwrite($fp, $output);
fclose($fp);
return false;
}
// METHOD print_ar
// PARAMS $array
// RETURN string html formatted
// DESCRIPTION
// prints a html formatted (pre) array
public static function print_ar($array)
{
return "".print_r($array, 1)."
";
}
// METHOD checked
// PARAMS haystack (search in), needle (search for), type: 0: returns selected, 1, returns checked
// haystack can be an array or a string
// RETURN returns checked or selected, else returns nothing (empty return)
// DESCRIPTION
// returns 'checked' or 'selected' if okay
// $needle is a var, $haystack an array
// **** THE RETURN VALUE WILL CHANGE TO A DEFAULT "FALSE" ****
public static function checked($haystack, $needle, $type = 0)
{
if (is_array($haystack)) {
if (in_array((string)$needle, $haystack)) {
return (($type) ? "checked" : "selected");
}
} else {
if ($haystack == $needle) {
return (($type) ? "checked" : "selected");
}
}
}
// METHOD magic_links
// PARAMS string: data to transform to a valud HTML url, target: default _blank
// RETURN correctly formed html url link
// DESCRIPTION
// tries to find mailto:user@bubu.at and changes it into ->