From 4e7d4fd2e0c55ed6f628d01dcbb52c54d0e0fe77 Mon Sep 17 00:00:00 2001 From: dana Date: Tue, 6 Jan 2015 12:54:14 +0000 Subject: [PATCH] Added comments to functions in lib.php git-svn-id: http://voip.null.ro/svn/ansql/trunk@133 dbfed7de-b0aa-0410-b6a1-c7e608b77fc9 --- lib.php | 1201 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 791 insertions(+), 410 deletions(-) diff --git a/lib.php b/lib.php index b4dce82..5d324d8 100644 --- a/lib.php +++ b/lib.php @@ -22,6 +22,10 @@ global $module, $method, $action, $vm_base, $limit, $db_true, $db_false, $limit, $page, $system_standard_timezone; +/** + * Include the classes for database objects. + * @param $path String. Path to the files to be included. + */ function include_classes($path='') { Debug::func_start(__FUNCTION__,func_get_args(),"ansql"); @@ -48,6 +52,11 @@ function include_classes($path='') } } } + +/** + * Implementation of stripos function if it does not exist. + * Find the position of the first occurrence of a case-insensitive substring in a string. + */ if (!function_exists("stripos")) { // PHP 4 does not define stripos function stripos($haystack,$needle,$offset=0) @@ -61,23 +70,36 @@ function stripos($haystack,$needle,$offset=0) if (!isset($system_standard_timezone)) $system_standard_timezone = "GMT".substr(date("O"),0,3); +/** + * Establish the name of the default function to be called using some predefind criteria. + * @param $module String. The Module defined for each project. + * @param $method String. The Method associated to the Module. + * @param $action String. The action associated with a method. + * @param $call String. The name of the default function to be called. + * @return string of type $call + */ function get_default_function() { Debug::func_start(__FUNCTION__,func_get_args(),"ansql"); global $module, $method, $action; - if(!$method) + if (!$method) $method = $module; - if(substr($method,0,4) == "add_") + if (substr($method,0,4) == "add_") $method = str_replace("add_","edit_",$method); - if($action) + if ($action) $call = $method.'_'.$action; else $call = $method; return $call; } +/** + * Test a given Path. + * @param $path String. + * @return 403 Forbidden page only if $path matches a regular expression + */ function testpath($path) { Debug::func_start(__FUNCTION__,func_get_args(),"ansql"); @@ -89,6 +111,11 @@ function testpath($path) } } +/** + * Send a raw HTTP header with 403 Forbidden. Clears the session data. + * Displayes a page with Forbidden message. + * Terminates the current script. + */ function forbidden() { Debug::func_start(__FUNCTION__,func_get_args(),"ansql"); @@ -99,106 +126,181 @@ function forbidden() exit(); } -function start_form($action = NULL, $method = "post", $allow_upload = false, $form_name = NULL, $class=null) +/** + * Builds the HTML
tag with all possible attributes: + * @param $action String. The action of the FORM + * @param $method String. Allowed values: post|get. Defaults to 'post'. + * @param $allow_upload Bool. If true allow the upload of files. Defaults to false. + * @param $form_name String. Fill the attribute name of the FORM. Defaults to 'current_form' + * @param $class String. Fill the attribute class. No default value set. + */ +function start_form($action = NULL, $method = "post", $allow_upload = false, $form_name = NULL, $class = NULL) { Debug::func_start(__FUNCTION__,func_get_args(),"ansql"); global $module; - if(!$method) + if (!$method) $method = "post"; $form = (!$module) ? "current_form" : $module; - if(!$form_name) + if (!$form_name) $form_name = $form; - if(!$action) { - if(isset($_SESSION["main"])) + if (!$action) { + if (isset($_SESSION["main"])) $action = $_SESSION["main"]; else $action = "index.php"; } - ?>> method="" >
'; } +/** + * Displayes an error note with a predefined css + */ function errornote($text) { Debug::func_start(__FUNCTION__,func_get_args(),"ansql"); print "
Error!! $text
"; } -function message($text, $path=NULL) +/** + * Displayes a given text. + * @param $text String The text to be displayed + * @param $path String The path to use in link + * @param $return_text the link to return to requested Path + */ +function message($text, $path=NULL, $return_text="Go back to application") { Debug::func_start(__FUNCTION__,func_get_args(),"ansql"); global $module,$method; print '
'."\n"; print "$text\n"; - if($path != 'no') - print 'Go back to application'; + + if ($path == 'no') { + print '
'; + return; + } + + link_to_main_page($path, $return_text); + print ''; } -function errormess($text, $path=NULL) +/** + * Displayes a given text with a specific css for errors + * @param $text String The text to be displayed + * @param $path String The path to use in link + * @param $return_text the link to return to requested Path + */ +function errormess($text, $path=NULL, $return_text="Go back to application") { Debug::func_start(__FUNCTION__,func_get_args(),"ansql"); global $module; - if(!$path) - $path = ''; print '
'."\n"; print " Error!!"."\n"; print "$text"."\n"; - if($path != 'no') - print 'Go back to application'."\n"; + + if ($path == 'no') { + print '
'; + return; + } + + link_to_main_page($path, $return_text); + print ''; } +/** + * Displayes a specific build link for application + */ +function link_to_main_page($path, $return_text) +{ + if (isset($_SESSION["main"])) + $link = $_SESSION["main"]; + else + $link = "main.php"; + $link .= "?module=".$module; + + if ($path) + $link .= "&method=".$path; + + print ''.$return_text.''; +} + +/** + * Prints a message as a notice or an error type message and calls a function + * @param $message String The message to be displayed. + * @param $next String Setting it to 'no' stops the callback function. + * @param $no_error Boolean If is true a message id displayed + * else an error type message is displayed. Defaults to true. + */ function notice($message, $next=NULL, $no_error = true) { Debug::func_start(__FUNCTION__,func_get_args(),"ansql"); global $module; - if(!$next) + if (!$next) $next = $module; - if($no_error) + if ($no_error) print '
'.$message.'
'; else print '
Error!! '.$message.'
'; - if($next != "no") + if ($next != "no") $next(); } +/** + * Displayes a text with a bold font style set. + */ function plainmessage($text) { Debug::func_start(__FUNCTION__,func_get_args(),"ansql"); print "
$text

"; } +/** + * Displayes a message or an error message depending on the data given in array + * @param $res Array Contains on key 0: true/false + * and on key 1: the message to be displayed + */ function notify($res) { Debug::func_start(__FUNCTION__,func_get_args(),"ansql"); global $path; - if($res[0]) + if ($res[0]) message($res[1],$path); else errormess($res[1],$path); } +/** + * Escape the HTTP Request variables + */ function escape_page_params() { Debug::func_start(__FUNCTION__,func_get_args(),"ansql"); @@ -210,18 +312,29 @@ function escape_page_params() $_REQUEST[$param] = escape_page_param($value); } +/** + * Convert all applicable characters to HTML entities for a value or an array of values + * @param $value String / Array + * @return the modified $value + */ function escape_page_param($value) { Debug::func_start(__FUNCTION__,func_get_args(),"paranoid"); if (!is_array($value)) return htmlentities($value); - else { + else { foreach ($value as $index=>$val) $value[$index] = htmlentities($val); return $value; } } +/** + * Return the $_GET OR $_POST value of a given parameter + * @param $param String + * @return the value of the $param set in $_GET or $_POST + * or NULL if is not set or if is a specific sql abreviation used in queries + */ function getparam($param,$escape = true) { Debug::func_start(__FUNCTION__,func_get_args(),"paranoid"); @@ -232,21 +345,21 @@ function getparam($param,$escape = true) $ret = $_GET[$param]; else return NULL; - if(is_array($ret)) { - foreach($ret as $index=>$value) { - if (substr($ret[$index],0,6) == "__sql_") - $ret[$index] = NULL; - if ($ret[$index] == "__empty") - $ret[$index] = NULL; - if ($ret[$index] == "__non_empty" || $ret[$index] == "__not_empty") - $ret[$index] = NULL; - if (substr($ret[$index],0,6) == "__LIKE") - $ret[$index] = NULL; - if (substr($ret[$index],0,10) == "__NOT LIKE") - $ret = NULL; - } + if (is_array($ret)) { + foreach($ret as $index => $value) + $ret[$index] = escape_sql_param($ret[$index]); return $ret; } + $ret = escape_sql_param($ret); + return $ret; +} + +/** + * Return NULL if the value of a given param is specific to SQL abreviations + * or the value of the param + */ +function escape_sql_param($ret) +{ if (substr($ret,0,6) == "__sql_") $ret = NULL; if ($ret == "__empty") @@ -260,44 +373,58 @@ function getparam($param,$escape = true) return $ret; } +/** + * Returns the new string with "_" where were spaces. + * @param $value String + */ function killspaces($value) { Debug::func_start(__FUNCTION__,func_get_args(),"ansql"); return str_replace(' ','_',$value); } +/** + * Verifies if a string is numeric. + * @param $num String the number to be checked + * @param $very_big Bool if true verifies if every digit is numeric + * @return NULL if given string is not numeric. + */ function Numerify($num, $very_big = false) { Debug::func_start(__FUNCTION__,func_get_args(),"ansql"); if ($num == '0') $num = '0'; - if($very_big) { + if ($very_big) { for($i=0; $i23)) - return false; - $hour=sprintf(" %02u:%02u:%02u",$hour,$end,$end); + return false; + $hour = sprintf(" %02u:%02u:%02u",$hour,$end,$end); return gmdate("Y-m-d") . $hour; } if (!($year && $month && $day)) return false; if ($hour == "") - $hour=$end ? 23 : 0; + $hour = $end ? 23 : 0; if (!(is_numeric($year) && is_numeric($month) && is_numeric($day) && is_numeric($hour))) return false; if (($year<2000) || ($month<1) || ($month>12) || ($day<1) || ($day>31) || ($hour<0) || ($hour>23)) @@ -305,6 +432,11 @@ function dateCheck($year,$month,$day,$hour,$end) return sprintf("%04u-%02u-%02u %02u:%02u:%02u",$year,$month,$day,$hour,$end,$end); } +/** + * Displays number on page that are links which will + * make a reload of page with a new limit request + * @param $nrs Array contains the number of items to be displayed + */ function items_on_page($nrs = array(20,50,100)) { Debug::func_start(__FUNCTION__,func_get_args(),"ansql"); @@ -312,11 +444,14 @@ function items_on_page($nrs = array(20,50,100)) $link = $_SESSION["main"] ? $_SESSION["main"] : "main.php"; $link .= "?"; - foreach($_REQUEST as $param=>$value) - { - if($param=="page" || $param=="PHPSESSID" || ($param=="action" && $action) || ($param=="method" && $method) || ($param=="module" && $module) || $param == "limit" || !strlen($value)) + foreach($_REQUEST as $param=>$value) { + if ($param == "page" || $param == "PHPSESSID" || + ($param == "action" && $action) || + ($param == "method" && $method) || + ($param == "module" && $module) || + $param == "limit" || !strlen($value)) continue; - if (substr($link,-1)!="?") + if (substr($link,-1) != "?") $link .= "&"; $link .= "$param=".urlencode($value); } @@ -344,10 +479,14 @@ function items_on_page($nrs = array(20,50,100)) print ""; } +/** + * Build the links for the number of pages for a total divided by the limit + */ function pages($total = NULL, $params = array()) { Debug::func_start(__FUNCTION__,func_get_args(),"ansql"); global $limit, $page, $module, $method, $action; + if(!$limit) $limit = 20; @@ -358,33 +497,34 @@ function pages($total = NULL, $params = array()) $page = 0; foreach($_REQUEST as $param=>$value) { - if(($param=="action" && $action) || ($param=="module" && $module) || ($param=="method" && $method) || $param=="PHPSESSID" || !strlen($value)) + if (($param == "action" && $action) || + ($param == "module" && $module) || + ($param == "method" && $method) || + $param == "PHPSESSID" || !strlen($value)) continue; - if($param=="page") - { + if ($param == "page") { $page = $value; continue; } - if($link != $slink) + if ($link != $slink) $link .= "&"; $link .= "$param=".urlencode($value); - if($param == "total") - { + if ($param == "total") { $total = $value; $found_total = true; } } - if(!$total) + if (!$total) $total = 0; - if($total < $limit) + if ($total < $limit) return; - if(!$found_total) + if (!$found_total) $link .= "&total=$total"; - if($module) + if ($module) $link .= "&module=$module"; - if($method) + if ($method) $link .= "&method=$method"; if ($action) { /* if action param is set, check that $method_$action function exists before adding it to link */ @@ -396,8 +536,7 @@ function pages($total = NULL, $params = array()) $pages = floor($total/$limit); print '
'; print '
'; - if($page != 0) - { + if ($page != 0) { /* jump to first page */ print '|<  '; @@ -412,14 +551,11 @@ function pages($total = NULL, $params = array()) $diff = floor(($total - ($page + $limit * 2))/$limit) * $limit; $sp = $page - $limit * 2; - if($diff < 0){ + if ($diff < 0) $sp = $sp - abs($diff); - } - while($sp<0) $sp += $limit; - while($sp<$page) - { + while($sp<$page) { $pg_nr = $sp/$limit + 1; print ''.$pg_nr.'  '; $sp += $limit; @@ -427,16 +563,14 @@ function pages($total = NULL, $params = array()) } $pg_nr = $page/$limit + 1; print ''.$pg_nr.'  '; - if(($page+$limit)<=$total) - { - if($pg_nr>=3) + if (($page+$limit) <= $total) { + if($pg_nr >= 3) $stop_at = $pg_nr + 2; else $stop_at = $pg_nr + 5 - (floor($page/$limit)+1); $next_page = $page + $limit; - while($next_page<$total && $pg_nr<$stop_at) - { + while($next_page < $total && $pg_nr < $stop_at) { $pg_nr++; print ''.$pg_nr.'  '; $next_page += $limit; @@ -451,7 +585,7 @@ function pages($total = NULL, $params = array()) $last_page = floor($total/$limit) * $limit; /* jump 5 pages */ - if ($next5<$last_page) + if ($next5 < $last_page) print '>>  '; /* jump to last page */ @@ -461,6 +595,9 @@ function pages($total = NULL, $params = array()) print '
'; } +/** + * Create links used for navigation between pages (previous / next) + */ function navbuttons($params=array(),$class = "llink") { Debug::func_start(__FUNCTION__,func_get_args(),"ansql"); @@ -468,8 +605,7 @@ function navbuttons($params=array(),$class = "llink") $step = ''; $link="main.php?module=$module&method=$method&"; - foreach($params as $key => $value) - { + foreach($params as $key => $value) { if ($key=="page" || $key=="tot") continue; $link="$link$key=$value&"; @@ -478,31 +614,33 @@ function navbuttons($params=array(),$class = "llink") } $total = $params["tot"]; - if(!$step || $step == '') + if (!$step || $step == '') $step = 10; ?>
@@ -512,16 +650,27 @@ function navbuttons($params=array(),$class = "llink") \n"; - if(count($additional)) + if (count($additional)) foreach($additional as $key=>$value) print ''; @@ -704,6 +853,10 @@ function editObject($object, $fields, $title, $submit="Submit", $compulsory_noti print '
'; } +/** + * Creates an input cancel button with build onclick link + * to return to the previous page + */ function cancel_button($css="", $name="Cancel") { Debug::func_start(__FUNCTION__,func_get_args(),"ansql"); @@ -717,6 +870,9 @@ function cancel_button($css="", $name="Cancel") return $res; } +/** + * Returns a string with the link build from previous page data session variable + */ function cancel_params() { Debug::func_start(__FUNCTION__,func_get_args(),"ansql"); @@ -726,21 +882,24 @@ function cancel_params() return $link; } +/** + * Builds the HTML data for FORM + */ function display_pair($field_name, $field_format, $object, $form_identifier, $css, $show_advanced, $td_width) { Debug::func_start(__FUNCTION__,func_get_args(),"ansql"); $q_mark = false; - if(isset($field_format["advanced"])) + if (isset($field_format["advanced"])) $have_advanced = true; - if(isset($field_format["triggered_by"])) + if (isset($field_format["triggered_by"])) $needs_trigger = true; - if($object) + if ($object) $value = (!is_array($field_name) && isset($object->{$field_name})) ? $object->{$field_name} : NULL; else $value = NULL; - if(isset($field_format["value"])) + if (isset($field_format["value"])) $value = $field_format["value"]; if (!strlen($value) && isset($field_format["callback_for_value"]) && is_callable($field_format["callback_for_value"]["name"])) { @@ -754,7 +913,7 @@ function display_pair($field_name, $field_format, $object, $form_identifier, $cs // if($needs_trigger == true) // print 'name="'.$form_identifier.$field_name.'triggered'.$field_format["triggered_by"].'"'; - if(isset($field_format["error"]) && $field_format["error"]===true) + if (isset($field_format["error"]) && $field_format["error"]===true) $css .= " error_field"; print ' class="'.$css.'"'; if(isset($field_format["advanced"])) @@ -769,10 +928,10 @@ function display_pair($field_name, $field_format, $object, $form_identifier, $cs print ' style="display:none;" trigger=\"true\" '; else print ' style="display:table-row;" trigger=\"true\" '; - }else + } else print ' style="display:table-row;"'; - }elseif(isset($field_format["triggered_by"])){ - if($needs_trigger) + } elseif (isset($field_format["triggered_by"])) { + if ($needs_trigger) print ' style="display:none;" trigger=\"true\" '; else print ' style="display:table-row;" trigger=\"true\" '; @@ -782,15 +941,15 @@ function display_pair($field_name, $field_format, $object, $form_identifier, $cs $var_name = (isset($field_format[0])) ? $field_format[0] : $field_name; $display = (isset($field_format["display"])) ? $field_format["display"] : "text"; - if($object) - { + if ($object) { $variable = (!is_array($var_name)) ? $object->variable($var_name) : NULL; - if($variable) - if($variable->_type == "bool" && $display!="text") + if ($variable) { + if ($variable->_type == "bool" && $display!="text") $display = "checkbox"; + } } - if($display == "message") { + if ($display == "message") { print ''; print $value; print ''; @@ -798,21 +957,21 @@ function display_pair($field_name, $field_format, $object, $form_identifier, $cs return; } - if($display != "hidden") { + if ($display != "hidden") { print ''; - if(!isset($field_format["column_name"])) + if (!isset($field_format["column_name"])) print ucfirst(str_replace("_"," ",$field_name)); else print ucfirst($field_format["column_name"]); - if(isset($field_format["required"])) + if (isset($field_format["required"])) $field_format["compulsory"] = $field_format["required"]; - if(isset($field_format["compulsory"])) + if (isset($field_format["compulsory"])) if($field_format["compulsory"] === true || $field_format["compulsory"] == "yes" || $field_format["compulsory"] == "t" || $field_format["compulsory"] == "true") print '*'; print ' '; @@ -820,12 +979,11 @@ function display_pair($field_name, $field_format, $object, $form_identifier, $cs if (isset($field_format["custom_css_right"])) print $field_format["custom_css_right"]; print '"'; - if(isset($td_width["right"])) + if (isset($td_width["right"])) print ' style="width:'.$td_width["right"].'"'; print '>'; } - switch($display) - { + switch($display) { case "textarea": print '