/lib.php
PHP | 575 lines | 348 code | 41 blank | 186 comment | 52 complexity | 9404a590af4d134b8f7edd68f634cec9 MD5 | raw file
- <?php
- function b_getmicrotime() {
- list($usec, $sec) = explode(' ', microtime());
- return ((float)$usec + (float)$sec);
- }
- /**
- * Digunakan untuk melakukan validasi terhadap format sebuah email
- *
- * @param string $cemail
- * @return TRUE/FALSE
- */
- function b_emailcheck($cemail) {
- return (eregi('^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,6}$',$cemail));
- }
- /**
- * Digunakan untuk melakukan validasi atas hak seorang user untuk menggunakan sebuah modul
- *
- * @param integer $nid
- * @param string $modulsname
- * @return TRUE/FALSE
- */
- function b_modulgranted($nid_user, $modul) {
- include('./../connect.php');
- $breturn=true;
- if($nid_user!=1) {
- $csql='select a.nid
- from '.PREFIX.'granted as a
- inner join '.PREFIX.'moduls as b on a.nid_moduls=b.nid
- where a.nid_users=\''.$nid_user.'\' and b.nid=\''.$modul.'\'';
- $nresult=mysql_query($csql);
- if(!mysql_num_rows($nresult)==1) $breturn=false;
- }
- return $breturn;
- }
- /**
- * Fungsi penerjemah
- *
- * @param string $string
- */
- function b_echo($string) {
- include('translate.php');
- if(array_key_exists($string, $atranslate)) echo $atranslate[$string];
- else echo $string;
- }
- /**
- * Mendapatkan list file dari suatu direktori
- * default: ekstension
- *
- * @param string $cdir
- * @return Array
- */
- function b_getlistfile($cdir='./extensions') {
- if($dirhandle=opendir($cdir)) {
- while ($cfile = readdir($dirhandle)) {
- if ($cfile != '.' && $cfile != '..') {
- if(is_dir($cdir.'/'.$cfile)) {
- $alistfiletemp=b_getlistfile($cdir.'/'.$cfile);
- if(count($alistfiletemp)>0)
- foreach($alistfiletemp as $cfiletemp) $alistfile[]=$cfiletemp;
- } else {
- $alistfile[]=$cdir.'/'.$cfile;
- }
- }
- }
- }
- return $alistfile;
- }
- /**
- * Mendapatkan extension file
- *
- * @param string $cfilename
- * @return string
- */
- function b_getextension($cfilename) {
- return substr(strrchr($cfilename, '.'), 1);
- }
- /**
- * Mendapatkan nilai dari querry
- *
- * @param string $csql
- * @return string
- */
- function b_fetch($csql,$becho=false) {
- include('connect.php');
- $csql=str_replace('from',' as xxxxxxxxxx from ',$csql);
- if($becho) echo $csql;
- $nresult=mysql_query($csql);
- $oreturn=mysql_fetch_object($nresult);
- return $oreturn->xxxxxxxxxx;
- }
- /*
- * Cek apakah sebuah table ada dalam database
- *
- * @ param char $ctable
- * @return boolean
- */
- function b_checktableexist($ctable) {
- include('connect.php');
- $breturn=false;
- $csql='show tables from '.DATABASE.' where tables_in_'.DATABASE.'="'.$ctable.'"';
- $nresult=mysql_query($csql);
- if(mysql_num_rows($nresult)>0) {
- $breturn=true;
- }
- return $breturn;
- }
- /**
- * Cek apakah user memiliki akses GOD level
- *
- * @param integer $nid
- * @return boolean
- */
- function b_god($nid=1) {
- include('connect.php');
- $csql='select nstatus from '.PREFIX.'users where nid=\''.$nid.'\'';
- (b_fetch($csql)==4)?$breturn=true:$breturn=false;
- return $breturn;
- }
- /**
- * Mendapatkan info user
- *
- * @return aray (name,user,email)
- */
- function b_userinfo() {
- if(b_logged()) {
- $csql='select cname, cuser, cemail
- from '.PREFIX.'users where nid=\''.$_SESSION['nid_login'].'\'';
- $nresult=mysql_query($csql);
- $ouser=mysql_fetch_object($nresult);
- $auser['name']=$ouser->cname;
- $auser['user']=$ouser->cuser;
- $auser['email']=$ouser->cemail;
- $csql='select max(nid) from '.PREFIX.'history where nid_users=\''.$_SESSION['nid_login'].'\'';
- $nid_histtory=b_fetch($csql);
- $csql='select date_format(dlogin,\'%W, %M %D, %Y %T\') as dlogin_now
- from '.PREFIX.'history where nid=\''.$nid_histtory.'\'';
- $nresult=mysql_query($csql);
- $ouser=mysql_fetch_object($nresult);
- $auser['logged']=$ouser->dlogin_now;
- $csql='select max(nid) from '.PREFIX.'history where nid_users=\''.$_SESSION['nid_login'].'\' and nid<>\''.$nid_histtory.'\'';
- $nid_histtory=b_fetch($csql);
- $csql='select date_format(dlogin,\'%W, %M %D, %Y %T\') as dlogin_last,
- date_format(dlogout,\'%W, %M %D, %Y %T\') as dlogout_last, cip
- from '.PREFIX.'history where nid=\''.$nid_histtory.'\'';
- $nresult=mysql_query($csql);
- $ouser=mysql_fetch_object($nresult);
- $auser['lastlogin']=$ouser->dlogin_last;
- $auser['lastlogout']=$ouser->dlogout_last;
- $auser['ip']=$ouser->cip;
- return $auser;
- }
- }
- /**
- * Baca keterangan dari file init
- *
- * @param string $cfile
- */
- function b_readinit($cfile) {
- if(file_exists($cfile)){
- $handle=fopen($cfile,'r');
- $cread = fread($handle, filesize($cfile));
- fclose($handle);
- $aline=explode("\n",$cread);
- foreach($aline as $craw) {
- $araw=explode('::',$craw);
- if(trim($araw[0])=='*/') break;
- else if(trim($araw[0])<>'/*')
- $ainfo[strtolower(trim($araw[0]))]=trim($araw[1]);
- }
- return $ainfo;
- }
- }
- /**
- * Mengembalikan parameter index.php (admin)
- *
- * @return string
- */
- function b_urlact() {
- /*
- if(INDEXMODEL=='A')
- $creturn=$_SERVER['PHP_SELF'].'?cact='.$_GET['cact'].'&csub='.$_GET['csub'];
- else if(INDEXMODEL=='F') {
- $creturn=$_SERVER['PHP_SELF'].'?page='.$_GET['page'];
- }
- */
- $cprotocol=strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https')=== FALSE ?'http':'https';
- $chost=$_SERVER['HTTP_HOST'];
- $cscript=$_SERVER['SCRIPT_NAME'];
- $cparams=$_SERVER['QUERY_STRING'];
-
- return $cprotocol.'://'.$chost.$cscript.'?'.$cparams;
- }
- /**
- * Menghancurkan cookies
- *
- */
- function b_cookies_destroy() {
- setcookie('a','',time()-3600);
- setcookie('b','',time()-3600);
- setcookie('c','',time()-3600);
- setcookie('d','',time()-3600);
- }
- /**
- * Membuat cookies
- *
- */
- function b_cookies_create() {
- b_include('config.php');
- if(REMEMBER) {
- setcookie('a',base64_encode($_POST['cpass']),time()+60*60*24*30);
- setcookie('b',base64_encode($_POST['cuser']),time()+60*60*24*30);
- setcookie('c',md5($_POST['cpass']),time()+60*60*24*30);
- setcookie('d',md5($_POST['cuser']),time()+60*60*24*30);
- }
- }
- /**
- * Cek keberadaan cookies
- *
- * @return boolean
- *
- */
- function b_cookies_check() {
- b_include('config.php');
- if(REMEMBER) {
- if(isset($_COOKIE['a']) and isset($_COOKIE['b']) and isset($_COOKIE['c']) and isset($_COOKIE['d'])) {
- if(md5(base64_decode($_COOKIE['a']))==$_COOKIE['d'] and md5(base64_decode($_COOKIE['b']))==$_COOKIE['d'])
- $bcookies=true;
- else {
- b_cookies_destroy();
- $bcookies=false;
- }
- }
- }
- return $bcookies;
- }
- /**
- * Login
- *
- * @param string $cuser
- * @param string $cuser
- * @param boolean $bcookies
- *
- * @return boolean
- *
- */
- function b_login($cuser, $cpass) {
- $breturn=false;
- if(b_antisqlinjection($cuser) and b_antisqlinjection($cpass)) {
- if(b_cookies_check())
- $csql='select nid from '.PREFIX .'users where cuser=\''.base64_decode($_COOKIE['b']).'\' and cpass=password(\''.base64_decode($_COOKIE['a']).'\')';
- else
- $csql='select nid from '.PREFIX .'users where cuser=\''.$cuser.'\' and cpass=\''.md5($cpass).'\'';
- $nid=b_fetch($csql);
- if(trim($nid)<>'') {
- if(isset($_SESSION['nid_login'])) unset($_SESSION['nid_login']);
- $_SESSION['nid_login']=$nid;
- $csql='insert into '.PREFIX.'history (nid_users, dlogin, cip)
- values
- (\''.$nid.'\', now(), \''.$_SERVER['REMOTE_ADDR'].'\')';
- mysql_query($csql);
- $breturn=true;
- }
- if(REMEMBER) b_cookies_create();
- }
- return $breturn;
- }
- /**
- * logout
- *
- */
- function b_logout() {
- b_include('config.php');
- $csql='select max(nid) from '.PREFIX.'history where nid_users=\''.$_SESSION['nid_login'].'\'';
- $nid_histtory=b_fetch($csql);
- $csql='update '.PREFIX.'history set dlogout=now() where nid=\''.$nid_histtory.'\'';
- mysql_query($csql);
- unset($_SESSION['nid_login']);
- unset($_SESSION['ctoken']);
- if(REMEMBER) b_cookies_destroy();
- }
- /**
- * Cek apakah sudah login atau belum
- *
- * @return boolean
- *
- */
- function b_logged() {
- return isset($_SESSION['nid_login']);
- }
- /**
- * Sama dengan fungsi Include, tetapi melakukan pengecekan file exist terlebih dahulu
- *
- * @param string $cfile
- */
- function b_include($cfile) {
- if(file_exists($cfile))
- include($cfile);
- }
- /**
- * Mendapatkan ID user yang sedang login
- *
- * @return number ID
- *
- */
- function b_getuserlogin() {
- if(b_logged())
- $nreturn=$_SESSION['nid_login'];
- else
- $nreturn=0;
- return $nreturn;
- }
- /**
- * Anti SQL Injection
- *
- * @param string $csql
- * @return boolean
- */
- function b_antisqlinjection($input) {
- // daftarkan perintah-perintah SQL yang tidak boleh ada
- // dalam query dimana SQL Injection mungkin dilakukan
- $aforbidden = array (
- 'insert', 'select', 'update', 'delete', 'truncate',
- 'replace', 'drop', ' or ', ';', '#', '--', '=' );
- // lakukan cek, input tidak mengandung perintah yang tidak boleh
- $breturn=true;
- foreach($aforbidden as $cforbidden) {
- if(strrpos($input, strtolower($cforbidden))) {
- $breturn=false;
- break;
- }
- }
- return $breturn;
- }
- /**
- * Mengambil path directory tempat sebuah extension aktif berada
- *
- * @param string $cfileinclude
- */
- function b_pathinclude($cfileinclude){
- $csql='select cpath from '.PREFIX.'moduls where nid=\''.$_GET['csub'].'\'';
- include(dirname(b_fetch($csql)).'/'.$cfileinclude);
- }
- /**
- * Mengambil konfigurasi
- *
- * @return unknown
- * @param variant $config
- */
- function b_getconfig($config) {
- $csql='select * from '.PREFIX.'config where cname=\''.$config.'\'';
- $nreturn=mysql_query($csql);
- $oconfig=mysql_fetch_object($nreturn);
- if(strtolower(substr($config,0,1))=='c') $return=$oconfig->cconfig;
- if(strtolower(substr($config,0,1))=='n') $return=$oconfig->nconfig;
- if(strtolower(substr($config,0,1))=='d') $return=$oconfig->dconfig;
- return $return;
- }
- /**
- * Menyimpan config
- *
- * @param string $config
- * @param variant $value
- */
- function b_writeconfig($config, $value) {
- $csql='select * from '.PREFIX.'config where cname=\''.$config.'\'';
- $nresult=mysql_query($csql);
- if(mysql_num_rows($nresult)==0) {
- switch(strtolower(substr($config,0,1))) {
- case 'c' : $csql='insert into '.PREFIX.'config (cname, cconfig) values (\''.$config.'\',\''.$value.'\')';
- break;
- case 'n' : $csql='insert into '.PREFIX.'config (cname, nconfig) values (\''.$config.'\',\''.$value.'\')';
- break;
- case 'd' : $csql='insert into '.PREFIX.'config (cname, dconfig) values (\''.$config.'\',\''.$value.'\')';
- break;
- }
- } else {
- switch(strtolower(substr($config,0,1))) {
- case 'c' : $csql='update '.PREFIX.'config set cconfig=\''.$value.'\' where cname=\''.$config.'\'';
- break;
- case 'n' : $csql='update '.PREFIX.'config set nconfig=\''.$value.'\' where cname=\''.$config.'\'';
- break;
- case 'd' : $csql='update '.PREFIX.'config set dconfig=\''.$value.'\' where cname=\''.$config.'\'';
- break;
- }
- }
- mysql_query($csql);
- }
- /**
- * hapus config
- *
- * @param string $config
- */
- function b_deleteconfig($config) {
- $csql='delete from '.PREFIX.'config where cname=\''.$config.'\'';
- mysql_query($csql);
- }
- /**
- * Check apakah sebuah extension bisa di aktifkan dilihat dari dependency-nya
- *
- * @param unknown_type $cpath
- */
- function b_checkenabledependency($cpath) {
- $breturn=true;
- $ainfo=b_readinit(str_replace('.php','.init.php',strtolower($cpath)));
- if(trim($ainfo['dependency'])<>'') {
- $adependency=explode(',',$ainfo['dependency']);
- $csql='select cpath from '.PREFIX.'moduls';
- $nresult=mysql_query($csql);
- while($opath=mysql_fetch_object($nresult)) {
- $ainfo=b_readinit(str_replace('.php','.init.php',strtolower($opath->cpath)));
- $amoduls[]=trim(strtolower($ainfo['name']));
- }
- foreach($adependency as $cmodul)
- if(in_array(trim(strtolower($cmodul)), $amoduls)) $nexist++;
- if($nexist<>count($adependency)) $breturn=false;
- }
- return $breturn;
- }
- /**
- * Check apakah sebuah extension bisa di nonaktifkan dilihat dari dependency-nya
- *
- * @param unknown_type $cpath
- */
- function b_checkdisabledependency($cpath) {
- $breturn=true;
- $ainfo=b_readinit(str_replace('.php','.init.php',strtolower($cpath)));
- $cmodul=trim(strtolower($ainfo['name']));
- $csql='select cpath from '.PREFIX.'moduls where cpath<>\''.$cpath.'\'';
- $nresult=mysql_query($csql);
- while($opath=mysql_fetch_object($nresult)) {
- $ainfo=b_readinit(str_replace('.php','.init.php',strtolower($opath->cpath)));
- if(trim($ainfo['dependency'])<>'')
- $adependecy[]=trim(strtolower($ainfo['dependency']));
- }
- if(count($adependecy)>0) {
- foreach($adependecy as $cdependecy) {
- $cmoduls.=b_removeallspaces($cdependecy).',';
- }
- $amoduls=explode(',',$cmoduls);
- if(in_array(b_removeallspaces($cmodul),$amoduls)) $breturn=false;
- }
- return $breturn;
- }
- /**
- * Fungsi untuk memanggil fungsi-fungsi start-up
- *
- */
- function b_startup() {
- $afunction=get_defined_functions();
- foreach($afunction['user'] as $cfunction) {
- if(strtolower(substr($cfunction,0,2))=='s_') {
- if(function_exists($cfunction))
- $ctempath=PATHEXTENSION;
- $ctemp=substr($cfunction,2,strlen($cfunction)-2);
- $csql='select cpath from '.PREFIX.'moduls where cpath like \'%'.substr($ctemp,0,strpos($ctemp,'_')).'.php\'';
- $initfile=str_replace('./','./admin/',str_replace('.php','.init.php',strtolower(b_fetch($csql))));
- $GLOBALS['bv_pathextension']=dirname($initfile).'/';
- define('PATHEXTENSION_STARTUP',dirname($initfile).'/');
- call_user_func($cfunction, '');
- }
- }
- }
- function b_size2byte($nsize) {
- if ($nsize >= '1099511627776'){
- $cfinalsize = sprintf ('%01.7f',$nsize/'1099511627776') . ' TB ';
- } elseif ($nsize >= '1073741824') {
- $cfinalsize = sprintf ('%01.5f',$nsize/'1073741824') . ' GB ';
- } elseif ($nsize >= '1048576') {
- $cfinalsize = sprintf ('%01.3f',$nsize/'1048576') . ' MB ';
- } elseif ($nsize >= '1024') {
- $cfinalsize = sprintf ('%01.1f',$nsize/'1024') . ' KB ';
- } else{
- $cfinalsize = $nsize . ' B ';
- }
- return $cfinalsize;
- }
- /**
- * Fungsi untuk mengaktifkan tombol sesuai level user
- *
- * @param unknown_type $nminlevel_action
- * @param unknown_type $creturn
- */
- function b_levelaction($nminlevel_action,$creturn='disabled') {
- return (USERLEVEL>=$nminlevel_action)?'':'$creturn';
- }
- /**
- * Fungsi untuk menghilangkan seluruh space yang ada dalam sebuah string
- *
- * @param string $cstring
- * @param boolean $btoonlyonespace
- * @return string
- */
- function b_removeallspaces($cstring, $btoonlyonespace=false) {
- return preg_replace('/\s*/m',($btoonlyonespace)?' ':'',$cstring);
- }
- function b_foradmin($bother=true) {
- if(USERLEVEL<BC_ADMIN and !b_god(b_getuserlogin()) and $bother) echo 'disabled';
- }
- function b_forsuperuser($bother=true) {
- if(USERLEVEL<BC_SUPERUSER and !b_god(b_getuserlogin()) and $bother) echo 'disabled';
- }
- function b_foruser($bother=true) {
- if(USERLEVEL<BC_USER and !b_god(b_getuserlogin()) and $bother) echo 'disabled';
- }
- function b_ufirsteachwords($ctext) {
- $amustupper=array('pt', 'cv');
-
- $ctemp=strtolower($ctext);
- $ctemp=ucwords($ctemp);
- foreach($amustupper as $searchreplace) {
- $ctemp=str_replace(ucwords($searchreplace), strtoupper($searchreplace), $ctemp);
- }
- return $ctemp;
- }
- // Baca Library tambahan
- include('connect.php');
- $csql='select * from '.PREFIX.'moduls';
- $nresult=mysql_query($csql);
- while($omoduls=mysql_fetch_object($nresult)) {
- $cfile=str_replace('.php','.lib.php',strtolower($omoduls->cpath));
- /*
- if(file_exists($cfile)) {
- include($cfile);
- } else {
- $cfile2=str_replace("./","./admin/",$cfile);
- if(file_exists($cfile2))
- include($cfile2);
- else {
- */
- $cfile3=str_replace('./',ADMIN_LOCATION,$cfile);
- if(file_exists($cfile3))
- include($cfile3);
- // }
- //}
- }
- ?>