Location: PHPKode > projects > PhpProjectMaster > www/inc/sitelib.php
<?php
/*
 General WebSite functions library for PHP
 (c) 2004-2007 by "Oleg Savchuk" <hide@address.com>

 part of phpProjectMaster project
 http://phpprojmaster.sourceforge.net

 The contents of this file are subject to the GNU GENERAL PUBLIC LICENSE
 http://www.gnu.org/copyleft/gpl.html

*/


require_once "config.php";
require_once "sitedb.php";
require_once "sitetpl.php";
require_once "sess.php" ;

//for debug
 if ($IS_DEBUG){
    ini_set('display_errors',1);
    error_reporting(E_ALL ^ E_NOTICE);
 }


//if site_configuration used 
//require_once "site_conf_utils.php";
//load_site_conf();

//!!! functions to do:
//set_lang
//die_c

//********************************************  strip quotes if they set
 if (get_magic_quotes_gpc()){
    $_POST = array_map('kill_magic_quotes', $_POST);
    $_GET = array_map('kill_magic_quotes', $_GET);
    $_COOKIE = array_map('kill_magic_quotes', $_COOKIE);
    $_REQUEST= array_map('kill_magic_quotes', $_REQUEST);

 }

function kill_magic_quotes($value){
 $value = is_array($value) ?
              array_map('kill_magic_quotes', $value) :
              stripslashes($value);

 return $value;
}

//********************************************

//****** initialization of common global values - call from begin of each script
function global_init(){
 if ( function_exists('mb_internal_encoding') ) {
    mb_internal_encoding('UTF-8');
 }
# ini_set('mbstring.func_overload', 2);

 if (!$_SESSION['u_id']){
    check_permanent_login();
 }

 $GLOBALS['green_msg']          = $_REQUEST['green_msg']; 
 $GLOBALS['err_msg']            = $_REQUEST['err_msg'];
 $GLOBALS['this_script']        = $_SERVER['PHP_SELF'];

 $GLOBALS['rowcolor']           = array('odd','even');
}

// *********** call user function according to presence of parameter passed in CGI request
//used as:
// $CGI_ACTIONS=array(
//  ''           => 'show_item_list',
//  'AddNew'     => 'show_add_item',
//  'Edit'       => 'show_one_item',
//  'SaveRec'    => 'save_one_item',
//  'DelRec'     => 'delete_item',
// );
// go_action();
function go_action($arr_action=0, $arr_form=0){
 if (!is_array($arr_action)){
    global $CGI_ACTIONS;
    $arr_action=&$CGI_ACTIONS;
 }
 if (!is_array($arr_form)){
    $arr_form=&$_REQUEST;
 }

 $done=0;
 while ( list ($k,$v) = each($arr_action) ){
    if ($arr_form[$k]){
       call_user_func($v);
//       logger("RUN $k");
       $done=1;
       break;
    }
 }

 if (!$done) {
    call_user_func($arr_action['']);
//    logger("RUN EMPTY");
 }

}


########### usually used before calling get_sqlupdate_set and get_sqlinsert_set
# exmple: $IFORM=form2dbhash($FORM, 'fname lname address');
# from form hash $FORM
# fields $names(string) ...
function form2dbhash($FORM, $names){
 $IFORM=array();
 $anames=preg_split("/\s+/", $names);

 #copy fields
 foreach ($anames as $name){
     if ($name) $IFORM[$name]=$FORM[$name];
 }

 return $IFORM;
}


//******************************************** 




########################## for debug
function logger($str){
 global $site_error_log;

 $arr=debug_backtrace();
 $arr=$arr[1];

 //remove unnecessary site_root_offline path
 $arr['file']=str_replace( strtolower($site_root_offline), "", strtolower(str_replace('\\', '/', $arr['file'])) );

 $str=strftime("%Y-%m-%d %H:%M:%S")." ".$arr['file']."::".$arr['function'].$arr['type']."(".$arr['line'].") ".$str;
 error_log("$str\n", 3, $site_error_log);
}
###########################
function rw($str){
 echo "$str<br>\n";
}
###########################
function rwe($str){
 echo "$str<br>\n";
 die;
}
###########################
function dumparr($arr){
 foreach ($arr as $key=>$value){
    rw("dump array: $key=>$value");
 }
}



########################## Email funcs
function send_email($ToEmail, $Subj, $Message, $isBCC="", $FromEmail=""){
 global $FROM_CONST;

 if (!$FromEmail) $FromEmail=$FROM_CONST;

 $more="Content-Type: text/plain; charset=\"utf-8\" ; format=\"flowed\"\n";
 if ($FromEmail) $more.="From: $FromEmail\n";
 if ($isBCC) $more.="Bcc: $isBCC\n";

 $Subj="=?utf-8?B?".base64_encode($Subj)."?=";
 mail($ToEmail, $Subj, $Message, $more);
}


########################## Administrator message alerter
function admin_alert($ToEmail){
 global $ADMIN_EMAIL;
 send_email($ADMIN_EMAIL,'SITE SYSTEM ALERT!', $ToEmail);
}



###################
#### MISC UTILS ###
###################

//*********************
function escape_str($str){
 return urlencode($str);
}

##########################
function n2br($str, $is_compress=''){
 $res=preg_replace("/\r/","",$str);
 $regexp="/\n/";
 if ($is_compress) $regexp="/\n+/";
 return preg_replace($regexp,"<br>",$res);
}
function br2n($str){
 return preg_replace("/<br>/i","\n",$str);
}
function dehtml($str){
 $aaa=preg_replace("/<[^>]*>/","",$str);
 return preg_replace("/%%[^%]*%%/","",$aaa);  //remove special tags too
}










############################
# $cur_l_num=int($_[0]);     #current page
# $all_s_num=int($_[1]);     #total number of items
# $ITEMS_ON_PAGE=$_[2];
# $page_name_tpl=$_[3];      #page url /ukr/dollar/notes.cgi?page=    for notes.cgi
# $lang=$_[4];
# $show_all=$_[5];           #print Totals?
function make_List_Navigation($cur_l_num, $all_s_num, $ITEMS_ON_PAGE, $page_name_tpl, $lang='e', $show_all=''){
  if (!$ITEMS_ON_PAGE) $ITEMS_ON_PAGE=10;
  $cgi_mode=0;
  if (preg_match("/\.php\?/i",$page_name_tpl)) $cgi_mode=1;
  $all_l_num=floor($all_s_num/$ITEMS_ON_PAGE+0.999999);

  $page_name='';
  $result='';
  if ($lang=='r'){
     $list_w=array('Áëèæå','Äàëüøå','Íàçàä','Âïåðåä','Íà÷àëî','Âñåãî');
  }
  else{
     $list_w=array('Less','More','Back','Next','First','Total');
  }

  $start_l_num=$cur_l_num-2;
  if($start_l_num<0){
    $start_l_num=0;
  }
  if($all_l_num-$start_l_num<5 && $all_l_num>=5){
    $start_l_num=$all_l_num-5;
  }

  $result="";

  if($start_l_num>0){
    $page_name=page_enum_name($start_l_num-1,$page_name_tpl);
    $result.="<a href=\"$page_name\"><font class=\"nav0\">$list_w[0]</font></a>";       
    $result.=" &nbsp;|&nbsp;\n";
  }
  for($p_p=$start_l_num;$p_p<$all_l_num && $p_p<$start_l_num+5;$p_p++){
     $first_s=$p_p*$ITEMS_ON_PAGE+1;
     $last_s=($p_p+1)*$ITEMS_ON_PAGE;
     $page_name=page_enum_name($p_p,$page_name_tpl);
     if($last_s>$all_s_num){
       $last_s=$all_s_num;
     }      
     if($p_p==$cur_l_num){
        $result.="<b>${first_s}..${last_s}</b></font>";
     }else{
        $result.="<a href=\"$page_name\"><font class=\"nav0\">${first_s}..${last_s}</font></a>";       
     }       
     if($p_p+1<$all_l_num){
        $result.=" &nbsp;|&nbsp;\n";
     }
  }
  if($start_l_num+5<$all_l_num){
    $page_name=page_enum_name($start_l_num+5,$page_name_tpl);
    $result.="<a href=\"$page_name\"><font class=\"nav0\">$list_w[1]</font></a>";       
  }
  $result.=" <br>\n";

  if($cur_l_num>0){
    $page_name=page_enum_name($cur_l_num-1,$page_name_tpl);
    $result.="<a href=\"$page_name\"><font class=\"nav0\">$list_w[2]</font></a> &nbsp;|&nbsp; ";
    $page_name=page_enum_name(0,$page_name_tpl);
    $result.="<a href=\"$page_name\"><font class=\"nav0\">$list_w[4]</font></a>";   
  }
  if($cur_l_num>0 && $cur_l_num+1<$all_l_num){
    $result.=" &nbsp;|&nbsp; ";
  }
  if($cur_l_num+1<$all_l_num){
    $page_name=page_enum_name($cur_l_num+1,$page_name_tpl);
    $result.="<a href=\"$page_name\"><font class=\"nav0\">$list_w[3]</font></a>";    
  }    
  if ($show_all) $result.="  <b><font class=\"nav0\">($list_w[5] - $all_s_num)</font></b> ";

  # print $result."\n";
  return $result;
}


function page_enum_name($nnnp,$nnnpage=0,$firstindex=0){
  if (preg_match("/\.php\?/i", $nnnpage)) return $nnnpage.$nnnp; //if CGI mode

  if($nnnp==0){
    if ($firstindex){
       return "index.html";
    }
    else{
       return "$nnnpage.html";
    }
  }else{
    return "$nnnpage$nnnp.html";
  }  
}

############################ Another version of List_Navigation
# $cur_l_num=int($_[0]);     #current page
# $all_s_num=int($_[1]);     #total number of items
# $ITEMS_ON_PAGE=$_[2];
# $page_name_tpl=$_[3];      #page url /ukr/dollar/notes.cgi?page=    for notes.cgi
# $lang=$_[4];
# $show_all=$_[5];           #print Totals?
function make_List_Navigation2($page, $total_items, $ITEMS_ON_PAGE, $page_name_tpl){
  $PAGES_DISPLAY=6;

  if (!$ITEMS_ON_PAGE) $ITEMS_ON_PAGE=10;
  $total_pages=floor($total_items/$ITEMS_ON_PAGE+0.999999);

//  $page=1; //debug
//  rw("$page, $total_items, $total_pages, $ITEMS_ON_PAGE, $page_name_tpl");

  $cdr=array();
  $start_page=$page-floor($PAGES_DISPLAY/2);
  if($start_page<0) $start_page=0;
  if($total_pages-$start_page<$PAGES_DISPLAY && $total_pages>=$PAGES_DISPLAY) $start_page=$total_pages-$PAGES_DISPLAY;

// rw("start_page=$start_page");

  $last_page=0;
  for($i=$start_page;$i<$total_pages && $i<$start_page+$PAGES_DISPLAY;$i++){
     $last_page=$i;
     $cdr[]=array(
         'page_sel' => ($i==$page)?1:0,
         'page_num' => $i+1,
         'page_url' => page_enum_name($i,$page_name_tpl),
     );
  }

  $ps=array(
//    'prev_link_url' => ($start_page>0)?page_enum_name($start_page-1,$page_name_tpl):'',
//    'next_link_url' => ($last_page<$total_pages-1)?page_enum_name($last_page+1,$page_name_tpl):'',
    'prev_link_url' => ($page>0)?page_enum_name($page-1,$page_name_tpl):'',
    'next_link_url' => ($page<$total_pages-1)?page_enum_name($page+1,$page_name_tpl):'',
    './page_dr' => $cdr,
  );
  return parse_page("/common/pagenav", "pagenav.html", $ps, 'v');
//debug  parse_page("/common/pagenav", "pagenav.html", $ps);
}

###########################
function do_redirect($url, $noexit=""){
 global $root_domain, $root_url;

 if (substr($url,0,1)=='/') $url="$root_domain$root_url$url";

 $ps=array(
   'url'=>$url
 );
 parse_page("", "redirect_js.html", $ps);
 if (!$noexit) exit();
}
//function redirect($location){
//header('location: http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']).'/'.$location);
//}

##########################################################
# Build Page Navigator
# input : /about/sample.htm and array of names (About,Sample)
# output: Home -> About -> Sample
#         /       /about/   /about/sample.htm (no link)
function get_page_navigator($page_url, $urlnames){
 global $root_url;
 $arr=split("/",$page_url);

// $navarr="&nbsp;<img src='$root_url/img/navarr.gif' width=10 height=10 border=0>&nbsp;";
 $navarr="&nbsp;&gt;&nbsp;";

 $navline="";
 $titleline="";
 $full_url="";
 for($i=0;$i<count($arr);$i++){
    if($i==0){
      $secname="Main Page";
    }
    else{
      $secname=$urlnames[$i-1];
    }
    if($navline) $navline.=$navarr;
    if ((count($arr)-$i-1)>0){
        $full_url.="$arr[$i]/";
        $navline.="<a href='$root_url$full_url'><font class=navf>$secname</font></a>";
    }
    else{
        $full_url.="$arr[$i]";
        $navline.="<strong><font class=navif>$secname</font></strong>";
    }

    $titleline="$secname - ".$titleline;
 }

 return array($navline, $titleline);
}

//
function get_rand_str($len){
 $result='';
 $chars=array("A","B","C","D","E","F","a","b","c","d","e","f",0,1,2,3,4,5,6,7,8,9);
 for($i=0;$i<$len;$i++) $result.=$chars[rand(0,count($chars))];
 return $result;
}

?>
Return current item: PhpProjectMaster