std/common.inc.php

00001 <?php if(!function_exists('startedIndexPhp')) { header("location:../index.php"); exit();}
00002 
00026 global $g_request_vars;
00027 $g_request_vars=array();
00028 
00029 global $g_tags_removed;
00030 $g_tags_removed= 0;
00031 
00032 
00036 function clearRequestVars() {
00037     global $g_request_vars;
00038     $g_request_vars=array();
00039 }
00040 
00041 
00042 
00048 function addRequestVars(&$referred_vars)
00049 {
00050     global $g_request_vars;
00051     global $g_tags_removed;
00052 
00053     if(!isset($g_request_vars)) {
00054         $g_request_vars= array();
00055     }
00056 
00057     if(!isset($referred_vars) ) {
00058         trigger_error('filter_vars() called without proper parameters', E_USER_NOTICE);
00059         return;
00060     }
00061 
00062     foreach(array_keys($referred_vars) as $key) {
00063 
00064         ### skip too long variable key (probably an hacking-attempt) ###
00065         if(strlen($key) > 256) {
00066             trigger_error('Skipping too long key: "'.$key.'"', E_USER_NOTICE);
00067             continue;
00068         }
00069 
00070         ### skip variables with invalid name ###
00071         if(preg_match("/[\\'<>\/\"]/",$key)) {
00072             trigger_error('Skipping maleformed key: "'.$key.'"', E_USER_NOTICE);
00073             continue;
00074         }
00075 
00076         $value= $referred_vars[$key];
00077 
00078         if(is_string($value)) {
00079 
00080             switch(confGet('CLEAN_REFERRED_VARS')) {
00081 
00082                 case 'STRIP_TAGS':
00083                     while ($value != strip_tags($value)) {
00084                        $g_tags_removed++;
00085                        $value = strip_tags($value);
00086                     }
00087 
00088                 case 'HTML_ENTITIES':
00089                     break;
00090 
00091                 default:
00092                     trigger_error("unknown setting for CLEAN_REFERRED_VARS: '".confGet('CLEAN_REFERRED_VARS')."'",E_USER_WARNING);
00093             }
00094 
00095             ### add slashes ###
00096             #$value= addslashes($value);             #@@@ this collides with real_escape_string
00097 
00098 
00099             ### strip length ###
00100             $value= substr( $value,0,confGet('STRING_SIZE_MAX'));
00101         }
00102         else if(! is_numeric($value) ) {
00103             trigger_error("Referred value for '$key' is of unknown type: '". gettype($value)."' ", E_USER_NOTICE);
00104         }
00105         $g_request_vars[$key] = $value;
00106     }
00107 }
00108 
00109 
00110 
00114 function get($key) {
00115     global $g_request_vars;
00116 
00117     if(isset($g_request_vars[$key])) {
00118         $value=$g_request_vars[$key];
00122         if(gettype($value) == 'boolean') {
00123 
00124             $value="";
00125         }
00126         return $value;
00127     }
00128 
00129     ### use wildcards ###
00130     else if(isset($g_request_vars) && ereg("\*",$key)) {
00131         $key= str_replace("*",".*",$key);
00132 
00133 
00134         $hash= array();
00135         foreach($g_request_vars as $ikey=>$ivalue) {
00136             if(ereg($key,$ikey)) {
00137                 $hash[$ikey]=$ivalue;
00138             }
00139         }
00140         return($hash);
00141     }
00142     return NULL;
00143 }
00144 
00145 
00152 function printFormVars()
00153 {
00154     global $g_request_vars;
00155     echo "%" . "%<pre>";
00156     print_r($g_request_vars);
00157     echo "</pre>";
00158 }
00159 
00160 
00171 function validateFormCrc()
00172 {
00173     if(!$handle= get('hidden_crc')) {
00174         return NULL;
00175     }
00176     global $PH;
00177     $params= $PH->getFromParams($handle);
00178     if(!$params) {
00179         log_message("Validing crc for hidden form value failed (from handle missing)", LOG_MESSAGE_HACKING_ALERT);
00180         return NULL;
00181     }
00182 
00183     $log_message='';
00184     $flag_failure= false;
00185     foreach($params as $key => $value) {
00186         if($key == 'go') {
00187             continue;
00188         }
00189         if(is_null(get($key)) || get($key) != $value) {
00190             $log_message.="'$key': '$value' -> '".get($key)."'  ";
00191             $flag_failure = true;
00192         }
00193     }
00194     if($flag_failure) {
00195         global $auth;
00196         log_message("HACK?? Failed hidden form CRC ($log_message) by ". $auth->cur_user->name, LOG_MESSAGE_HACKING_ALERT);
00197         return NULL;
00198 
00199     }
00200     return true;
00201 }
00202 
00203 
00221 function validateFormCaptcha($abort_on_failure = false)
00222 {
00223     global $auth;
00224     if($key= get('captcha_key')) {
00225         $captcha_input= get('captcha_input');
00226 
00227         $should_be= substr(md5( $key . $auth->cur_user->identifier ), 0, 5);
00228 
00229 
00230         if($captcha_input == $should_be) {
00231             return true;
00232         }
00233         else {
00234             if($abort_on_failure) {
00235                 global $PH;
00236                 $PH->abortWarning(__("Sorry, but the entered number did not match"));
00237             }
00238             return false;
00239         }
00240     }
00241     return true;
00242 }
00243 
00244 
00250 class BaseObject
00251 {
00252 
00253     public function __construct($args=NULL)
00254     {
00255         if($args) {
00256             foreach($args as $key=>$value) {
00257                 is_null($this->$key);   # cause E_NOTICE if member not defined
00258                 $this->$key=$value;
00259             }
00260         }
00261     }
00262 
00263 
00264     public function __set($name,$value)
00265     {
00266         if($this->$name) {
00267             $this->$name= $value;
00268         }
00269         else {
00270             trigger_error("setting undefined member '$name'  to '$value'  in Class '" .@get_class($this). "' ",E_USER_WARNING);
00271             $this->$name= $value;
00272         }
00273     }
00274 
00275 
00276     #--- get --------------------------------------
00277     public function __get($nm)
00278     {
00279         if (isset($this->$nm)) {
00280            return $r;
00281         }
00282         else {
00283             trigger_error("reading undefined member '$nm'  in '" .@get_class($this). "' ", E_USER_WARNING);
00284         }
00285     }
00286 }
00287 
00288 
00289 
00290 
00291 
00301 function fillMissingValues(&$list, $settings)
00302 {
00303     foreach($settings as $key => $value){
00304         if(!array_key_exists($key, $list)) {
00305             $list[$key]= $value;
00306         }
00307     }
00308 }
00309 
00310 
00311 
00316 function string2month(&$string) {
00317     $mon=1;
00318     foreach(array('Jan','Feb','Ma?.r','Apr','Ma','Jun','Jul','Aug','Sep','O','Nov','Dec') as $m) {
00319         if(preg_match("/^$m/i",$string,$matches)) {
00320             return "$mon";      # TODO-printf-formated layout for 2 digits
00321         }
00322         ++$mon;
00323     }
00324     return false;
00325 }
00326 
00327 
00328 function mysqlDatetime2utc($datetime) {
00329     $out=array();
00330     if(preg_match("/\b(\d\d\d\d)[^\d](\d?\d)[^\d](\d?\d)\s+(\d\d)[^\d](\d\d)[^\d](\d\d)\b/",$datetime,$matches)) {
00331         if(count($matches)==7) {
00332             $out['year']=$matches[1];
00333             $out['mon']=$matches[2];
00334             $out['day']=$matches[3];
00335             $out['hour']=$matches[4];
00336             $out['min']=$matches[5];
00337             $out['sec']=$matches[6];
00338             return $out;
00339         }
00340     }
00341      return false;
00342 }
00343 
00351 function getPassedIds($name=false,$wild=false)
00352 {
00353 
00354     $ids=NULL;
00355     #--- first check use wildcards --
00356     if(!$wild) {
00357         $wild= strtolower($name)."s_*";     # eg: 'objectS_*'
00358     }
00359     $selected_items= get($wild);
00360 
00361     if($selected_items) {
00362         $keys= array_keys($selected_items);
00363         foreach($keys as $key) {
00364             if(preg_match("/_(\d+)_chk/",$key,$matches)) {
00365                 $ids[]=$matches[1];
00366             }
00367         }
00368     }
00369     if(!$ids) {
00370         #--- try original id ---
00371         if($name) {
00372             $id=get($name);
00373             $ids=array();
00374             if(isset($id)) {
00375                 $ids[]=$id;
00376             }
00377         }
00378     }
00379     return $ids;
00380 }
00381 
00386 function getOnePassedId($name=false,$wild=false, $abort_on_failure=true,$message=NULL)
00387 {
00388     global $PH;
00389 
00390     if(!$message) {
00391         $message=__("No element selected? (could not find id)","Message if a function started without items selected");
00392     }
00393     $ids= getPassedIds($name,$wild);
00394     if(!$ids) {
00395         if($abort_on_failure) {
00396             $PH->abortWarning($message,ERROR_NOTE);
00397             exit("aborting");
00398         }
00399         return;
00400     }
00401     else if(count($ids)>1) {
00402         $message= __('only one item expected.');
00403         if($abort_on_failure) {
00404             $PH->abortWarning($message,ERROR_NOTE);
00405         }
00406         else {
00407             $PH->messages[]= $message;
00408             return;
00409         }
00410     }
00411     return $ids[0];
00412 }
00413 
00414 
00415 
00416 
00427 global $g_lang;
00428 $g_lang="en";
00429 function __ ( $str, $context=NULL ) {
00430     global $g_lang;
00431 
00432     if (!isset($g_lang) or $g_lang == "en") {
00433         return $str;
00434     }
00435 
00436     global $g_lang_table;
00437 
00438     ### first try clarified phrase ###
00439     if($context && isset($g_lang_table[$str."|".$context]) && $g_lang_table[$str."|".$context]!="" ) {
00440         return preg_replace('/\|.*/','',$g_lang_table[$str."|".$context]);
00441     }
00442 
00443     ### then try general phrase ###
00444     if(isset($g_lang_table[$str]) && $g_lang_table[$str] != "") {
00445         return preg_replace('/\|.*/','',$g_lang_table[$str]);
00446     }
00447 
00448     ### not found -> keep in not-found-list for later output ###
00449     global $g_lang_new;
00450     if(!isset($g_lang_new)) {
00451         $g_lang_new=array();
00452     }
00453     $g_lang_new[$str."|".$context]="?";
00454 
00455     return $str;
00456 }
00457 
00458 
00467 function setLang($lang) {
00468     global $g_lang;
00469     if($lang == $g_lang) {
00470         return;
00471     }
00472     if($lang == 'en') {
00473         $g_lang= 'en';
00474     }
00475     else {
00476         $filepath= "lang/{$lang}.inc.php";
00477         if(file_exists($filepath)) {
00478             require($filepath);
00479             $g_lang= $lang;
00480         }
00481         else {
00482             trigger_error("Undefined language '$lang'", E_USER_NOTICE);
00483             return;
00484         }
00485     }
00486 
00487     $locale = confGet('FORCE_LOCALE');
00488 
00489     if($locale != 'C') {
00490         // setlocale() is used to set the proper locale for date formatting
00491         // As locale identifiers are platform dependent, PHP allows to specify more than one,
00492         // they are tried in order until a supported one is found. Most *nix-based platforms
00493         // use "xx_XX.encoding", while Windows platforms use three letter forms. The more
00494         // locales are listed, the more compatible the code will be.
00495         // Please refer to documentation of function setlocale() for details.
00496         // TODO: should we set the locale also for LC_CTYPE and/or LC_COLLATE?
00497 
00498         if($locale == '')
00499             $locale = __('en_US.utf8,en_US,enu', 'list of locales');
00500 
00501         $res = setlocale(LC_TIME, explode(',', $locale));
00502 
00503         // this warning might be annoying, but we need a way to detect that setlocale failed
00504         // eventually the list of locales will be long enough to include all supported platforms
00505         if($res === FALSE)
00506             trigger_error("Could not set locale to '$locale'", E_USER_WARNING);
00507     }
00508 }
00509 
00510 
00511 
00512 
00513 
00518 function readfile_chunked($filename, $retbytes=true) {
00519     $chunksize = 1*(1024*1024); // how many bytes per chunk
00520     $buffer = '';
00521     $cnt =0;
00522     // $handle = fopen($filename, 'rb');
00523     $handle = fopen($filename, 'rb');
00524     if ($handle === false) {
00525         return false;
00526     }
00527     while (!feof($handle)) {
00528         $buffer = fread($handle, $chunksize);
00529         echo $buffer;
00530         if(ob_get_length()) {
00531             ob_flush();
00532         }
00533         flush();
00534         if ($retbytes) {
00535            $cnt += strlen($buffer);
00536         }
00537     }
00538     $status = fclose($handle);
00539 
00540     if ($retbytes && $status) {
00541        return $cnt; // return num. bytes delivered like readfile() does.
00542     }
00543     return $status;
00544 
00545 }
00546 
00547 
00554 function asAlphaNumeric($str) {
00555     return preg_replace("/[^0-9A-Z_]/i",'',$str);
00556 }
00557 
00558 function asMatchString($str) {
00559     return preg_replace("/[^0-9a-z_\* ]/i",'',strtolower($str));
00560 }
00561 
00562 function asCleanString($str)
00563 {
00564     return preg_replace("/[\\<>\`\´\"]/",'',$str);
00565 
00566 
00567 }
00568 
00569 
00570 function asSecureString($str)
00571 {
00572     global $sql_obj;
00573     if(!is_object($sql_obj)) {
00574         trigger_error("sql_obj not defined", E_USER_ERROR);
00575     }
00576     return $sql_obj->secure($str);
00577 
00578 }
00579 
00580 
00581 function getOrderByString($f_order_str=NULL, $default='')
00582 {
00583     if($tmp= asCleanString($f_order_str)) {
00584         return 'ORDER BY '. $tmp;
00585     }
00586     else if($tmp= asCleanString($default)) {
00587         return 'ORDER BY '. $tmp;
00588     }
00589     return '';
00590 }
00591 
00592 
00596 function asHtml(&$str) {
00597 
00598     #$str= str_replace("\\\"", '"',$str);
00599 
00600     return htmlSpecialChars($str, ENT_QUOTES,'UTF-8' );
00601 }
00602 
00606 function asKey($str) {
00607     return preg_replace("/[^0-9a-z_]/",'',strtolower($str));
00608 }
00609 
00610 
00611 
00612 
00613 
00614 
00615 
00621 function strToGMTime($str)
00622 {
00623     return (strToTime($str. " GMT") );
00624 }
00625 
00626 
00630 function clientTimeStrToGMTString($str)
00631 {
00632     global $auth;
00633     $time_offset= 0;
00634     if(isset($auth->cur_user)) {
00635         $time_offset= $auth->cur_user->time_offset;
00636     }
00637     return getGMTString( strToGMTime($str) - $time_offset  -  confGet('SERVER_TIME_OFFSET'));
00638 }
00639 
00640 
00644 function clientTimeToGMTString($time)
00645 {
00646     global $auth;
00647     $time_offset= 0;
00648     if(isset($auth->cur_user)) {
00649         $time_offset= $auth->cur_user->time_offset;
00650     }
00651     return getGMTString( $time - $time_offset -  confGet('SERVER_TIME_OFFSET'));
00652 }
00653 
00654 
00655 
00660 function getGMTString($time=NULL)
00661 {
00662     if(is_null($time)) {
00663         $time = time();
00664     }
00665     return gmdate("Y-m-d H:i:s", $time);
00666 }
00667 
00668 
00672 function strToClientTime($str)
00673 {
00674     if($str == '0000-00-00 00:00:00' || $str == '0000-00-00') {
00675         return 0;
00676     }
00677     global $auth;
00678     $time_offset= 0;
00679     if(isset($auth->cur_user)) {
00680         $time_offset= $auth->cur_user->time_offset;
00681     }
00682     return strToTime($str . " GMT")  + $time_offset +  confGet('SERVER_TIME_OFFSET');
00683 }
00684 
00685 
00689 function GMTToClientTime($time)
00690 {
00691     global $auth;
00692     $time_offset= 0;
00693     if(isset($auth->cur_user)) {
00694         $time_offset= $auth->cur_user->time_offset;
00695     }
00696     return $time + $time_offset + confGet('SERVER_TIME_OFFSET');
00697 }
00698 
00699 
00700 
00701 
00702 
00703 
00715 function &sortObjectsRecursively(&$obj_with_children, &$list, $level=0)
00716 {
00717 
00718     $obj_with_children->level= $level;
00719     $list[]= $obj_with_children;
00720 
00721     foreach($obj_with_children->children as $id => $child) {
00722         if($child->id) {
00723             sortObjectsRecursively(&$child, &$list, $level+1);
00724         }
00725     }
00726     return $list;
00727 }
00728 
00735 function isSpam($str) {
00736     $cleaned= preg_replace("/[^a-z]/",'', strtolower($str));
00737     $count= 0;
00738     $count_matched_words=0;
00739     foreach(confGet('SPAM_WORDS') as $word => $value) {
00740 
00741         if($tmp= substr_count($cleaned, $word)) {
00742             $count_matched_words += $value;
00743             $count+= $tmp * $value;
00744         }
00745     }
00746     if(str_word_count($str)) {
00747         $rate= $count * $count_matched_words/ str_word_count($str) / count(confGet('SPAM_WORDS'));
00748     }
00749     else {
00750         $rate= 0;
00751     }
00752     return $rate;
00753 }
00754 
00755 
00756 ?>

Generated on Sun Mar 4 17:19:32 2007 for streber by  doxygen 1.5.1-p1