1465 lines
48 KiB
PHP
1465 lines
48 KiB
PHP
<?php
|
|
//--------------------------------------------------------------------//
|
|
// Filename : include/functions.php //
|
|
// Software : XOCP - X Open Community Portal //
|
|
// Version : 0.1 //
|
|
// Date : 2002-11-13 //
|
|
// Author : adiet //
|
|
// License : GPL //
|
|
//--------------------------------------------------------------------//
|
|
|
|
if ( !defined('XOCP_FUNCTIONS_DEFINED') ) {
|
|
define('XOCP_FUNCTIONS_DEFINED', TRUE);
|
|
|
|
// ################## Various functions from here ################
|
|
|
|
function resize_center_image($width_new, $height_new, $source_blob, $quality = 90){
|
|
$src_img = imagecreatefromstring($source_blob);
|
|
$width = imagesx($src_img);
|
|
$height = imagesy($src_img);
|
|
$mime = "image/jpeg";
|
|
$image = "imagejpeg";
|
|
$dst_img = imagecreatetruecolor($width_new, $height_new);
|
|
|
|
$offsetx = round(($width_new - $width) / 2);
|
|
$offsety = round(($height_new - $height) / 2);
|
|
|
|
imagecopyresampled($dst_img,$src_img,$offsetx,$offsety,0,0,$width,$height,$width,$height);
|
|
ob_start();
|
|
imagejpeg($dst_img,NULL,$quality);
|
|
$stringdata = ob_get_contents(); // read from buffer
|
|
ob_end_clean();
|
|
return $stringdata;
|
|
}
|
|
|
|
function get_mac_address() {
|
|
$ret = array();
|
|
exec("ipconfig /all", $out, $res);
|
|
foreach (preg_grep('/^\s*Physical Address[^:]*:\s*([0-9a-f-]+)/i', $out) as $line) {
|
|
$line = substr(strrchr($line, ' '), 1);
|
|
if($line!="00-00-00-00-00-00-00-E0") $ret[] = $line;
|
|
}
|
|
return $ret;
|
|
}
|
|
|
|
function _to_days($datetime) {
|
|
$tmp_bits = explode(" ",$datetime);
|
|
$bits = explode('-', $tmp_bits[0], 2);
|
|
$year = $bits[0];
|
|
if(_is_leap_year($year)) {
|
|
$bits[0] = '2000';
|
|
} else {
|
|
$bits[0] = '1999';
|
|
}
|
|
$date = implode('-',$bits);
|
|
$leaps = 0;
|
|
for($i = 0; $i < $year; $i++) {
|
|
if(_is_leap_year($i)) {
|
|
++$leaps;
|
|
}
|
|
}
|
|
$days = date('z', strtotime($date));
|
|
return $leaps + ($year * 365) + $days;
|
|
}
|
|
|
|
function _from_days($days) {
|
|
$date = strtotime("+".$days." days", strtotime("0000-01-01"));
|
|
return date("Y-m-d",$date);
|
|
}
|
|
|
|
function _date_add($datetime,$interval_days) {
|
|
$tmp_bits = explode(" ",$datetime);
|
|
return date("Y-m-d",strtotime("+".$interval_days." days", strtotime($tmp_bits[0]))) . ( isset($tmp_bits[1]) ? " $tmp_bits[1]":"");
|
|
}
|
|
|
|
function _is_leap_year($year) {
|
|
if($year % 100 == 0 && $year % 400 == 0) return true;
|
|
if($year % 100 == 0) return false;
|
|
if($year % 4 == 0) return true;
|
|
return false;
|
|
}
|
|
|
|
|
|
function age_group($age_year) {
|
|
if($age_year<=1) {
|
|
return 1;
|
|
} else if($age_year<=3) {
|
|
return 3;
|
|
} else if($age_year<=5) {
|
|
return 5;
|
|
} else if($age_year<=10) {
|
|
return 10;
|
|
} else if($age_year<=15) {
|
|
return 15;
|
|
} else if($age_year<=20) {
|
|
return 20;
|
|
} else if($age_year<=25) {
|
|
return 25;
|
|
} else if($age_year<=30) {
|
|
return 30;
|
|
} else if($age_year<=35) {
|
|
return 35;
|
|
} else if($age_year<=40) {
|
|
return 40;
|
|
} else if($age_year<=45) {
|
|
return 45;
|
|
} else if($age_year<=50) {
|
|
return 50;
|
|
} else if($age_year<=55) {
|
|
return 55;
|
|
} else if($age_year<=60) {
|
|
return 60;
|
|
} else if($age_year<=65) {
|
|
return 65;
|
|
} else if($age_year<=70) {
|
|
return 70;
|
|
} else if($age_year<=75) {
|
|
return 75;
|
|
} else if($age_year<=80) {
|
|
return 80;
|
|
} else if($age_year<=85) {
|
|
return 85;
|
|
} else if($age_year<=90) {
|
|
return 90;
|
|
} else if($age_year<=95) {
|
|
return 95;
|
|
} else {
|
|
return 100;
|
|
}
|
|
}
|
|
|
|
function recurse_delete($dir) {
|
|
if($dir=="/") return;
|
|
if (is_dir($dir)) {
|
|
$objects = scandir($dir);
|
|
foreach ($objects as $object) {
|
|
if ($object != "." && $object != "..") {
|
|
if (is_dir($dir."/".$object))
|
|
recurse_delete($dir."/".$object);
|
|
else
|
|
unlink($dir."/".$object);
|
|
}
|
|
}
|
|
rmdir($dir);
|
|
} elseif (is_file($dir)) {
|
|
unlink($dir);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Copy a file, or recursively copy a folder and its contents
|
|
* @author Aidan Lister <aidan@php.net>
|
|
* @version 1.0.1
|
|
* @link http://aidanlister.com/2004/04/recursively-copying-directories-in-php/
|
|
* @param string $source Source path
|
|
* @param string $dest Destination path
|
|
* @param int $permissions New folder creation permissions
|
|
* @return bool Returns true on success, false on failure
|
|
*/
|
|
function xcopy($source, $dest, $permissions = 0755) {
|
|
// Check for symlinks
|
|
if (is_link($source)) {
|
|
return symlink(readlink($source), $dest);
|
|
}
|
|
|
|
// Simple copy for a file
|
|
if (is_file($source)) {
|
|
return copy($source, $dest);
|
|
}
|
|
|
|
// Make destination directory
|
|
if (!is_dir($dest)) {
|
|
mkdir($dest, $permissions);
|
|
}
|
|
|
|
// Loop through the folder
|
|
$dir = dir($source);
|
|
while (false !== $entry = $dir->read()) {
|
|
// Skip pointers
|
|
if ($entry == '.' || $entry == '..') {
|
|
continue;
|
|
}
|
|
|
|
// Deep copy directories
|
|
xcopy("$source/$entry", "$dest/$entry", $permissions);
|
|
}
|
|
|
|
// Clean up
|
|
$dir->close();
|
|
return true;
|
|
}
|
|
|
|
function retrieve_message() {
|
|
$cmd = XOCP_DOC_ROOT."/modules/klaim/retrieve_message.php";
|
|
if (substr(php_uname(), 0, 7) == "Windows") {
|
|
$php = "c:/xampp/php/php.exe";
|
|
pclose(popen("start /B $php $cmd", "r"));
|
|
} else {
|
|
$php = "/usr/local/bin/php";
|
|
exec("$php $cmd > /dev/null &");
|
|
}
|
|
}
|
|
|
|
function install_patch() {
|
|
$cmd = XOCP_DOC_ROOT."/modules/klaim/run_patch.php";
|
|
if (substr(php_uname(), 0, 7) == "Windows") {
|
|
$php = "c:/xampp/php/php.exe";
|
|
pclose(popen("start /B $php $cmd", "r"));
|
|
} else {
|
|
$php = "/usr/local/bin/php";
|
|
exec("$php $cmd > /dev/null &");
|
|
}
|
|
}
|
|
|
|
function fetch_update() {
|
|
$cmd = XOCP_DOC_ROOT."/modules/klaim/fetch_update.php";
|
|
if (substr(php_uname(), 0, 7) == "Windows") {
|
|
$php = "c:/xampp/php/php.exe";
|
|
pclose(popen("start /B $php $cmd", "r"));
|
|
} else {
|
|
$php = "/usr/local/bin/php";
|
|
exec("$php $cmd > /dev/null &");
|
|
}
|
|
}
|
|
|
|
function unique_machine_id($salt = "") {
|
|
$db = new Database();
|
|
|
|
$mac = get_mac_address();
|
|
$sql = "SELECT mac_address,salt,machine_uniqid,TO_DAYS(now())-TO_DAYS(updated_dttm) FROM machine";
|
|
$result = $db->query($sql);
|
|
if($db->getRowsNum($result)==1) {
|
|
list($mac_address,$salt,$machine_uniqid,$age_days)=$db->fetchRow($result);
|
|
} else {
|
|
$sql = "TRUNCATE TABLE machine";
|
|
$db->query($sql);
|
|
$age_days = 999;
|
|
$salt = md5(uniqid());
|
|
}
|
|
|
|
if($age_days>7) {
|
|
$sql = "TRUNCATE TABLE machine";
|
|
$db->query($sql);
|
|
$mac_address = implode("#",$mac);
|
|
$machine_uniqid = md5($mac_address.$salt);
|
|
$sql = "INSERT INTO machine(mac_address,salt,machine_uniqid) VALUES ('$mac_address','$salt','$machine_uniqid')";
|
|
$db->query($sql);
|
|
$sql = "UPDATE kemenkes_setup SET machine_uniqid = '$machine_uniqid'";
|
|
$db->query($sql);
|
|
}
|
|
|
|
return $machine_uniqid;
|
|
}
|
|
|
|
function escape_js($str) {
|
|
// borrowed from smarty
|
|
return strtr($str, array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n','</'=>'<\/'));
|
|
}
|
|
|
|
function php_array_to_js($arr) {
|
|
$items = array();
|
|
|
|
foreach ($arr as $k => $v) {
|
|
if (is_array($v))
|
|
$items[] = php_array_to_js($v);
|
|
else if (is_int($v))
|
|
$items[] = $v;
|
|
else
|
|
$items[] = "'" . escape_js($v) . "'";
|
|
}
|
|
|
|
return '[' . join(',', $items) . ']';
|
|
}
|
|
|
|
|
|
function _get_key_a($kode_rs,$key) {
|
|
$key1 = hash_hmac("sha256", $kode_rs, $key);
|
|
return $key1;
|
|
}
|
|
|
|
function get_key_from_secret($kode_rs,$secret) {
|
|
$combined = $kode_rs."1110".$secret;
|
|
$signature = "";
|
|
for($i=0;$i<strlen($combined);$i++) {
|
|
$n = $combined[$i]*(10+$combined[$i]*3);
|
|
$signature .= $n;
|
|
}
|
|
$key = hash_hmac("sha256",$kode_rs.$signature,$secret);
|
|
return $key;
|
|
}
|
|
|
|
|
|
// Encrypt Function
|
|
function mc_encrypt($data, $key, $binary=FALSE, $chunk_split=TRUE) {
|
|
include_once(XOCP_DOC_ROOT."/include/random/random.php");
|
|
|
|
/// make binary representasion of $key
|
|
$key = hex2bin($key);
|
|
|
|
/// check key length, must be 256 bit or 32 bytes
|
|
if (mb_strlen($key, "8bit") !== 32) {
|
|
throw new Exception("Needs a 256-bit key!");
|
|
}
|
|
|
|
/// create initialization vector
|
|
$iv_size = openssl_cipher_iv_length("aes-256-cbc");
|
|
$iv = random_bytes($iv_size);
|
|
|
|
/// encrypt
|
|
$encrypted = openssl_encrypt($data, "aes-256-cbc", $key, OPENSSL_RAW_DATA, $iv );
|
|
|
|
/// create signature, against padding oracle attacks
|
|
$signature = mb_substr(hash_hmac("sha256", $encrypted, $key, true),0,10,"8bit");
|
|
|
|
/// combine all, encode, and format
|
|
if($binary) {
|
|
return $signature.$iv.$encrypted;
|
|
} else {
|
|
if($chunk_split) {
|
|
return chunk_split(base64_encode($signature.$iv.$encrypted));
|
|
} else {
|
|
return base64_encode($signature.$iv.$encrypted);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
// Decrypt Function
|
|
function mc_decrypt($str, $strkey, $binary=FALSE){
|
|
|
|
/// make binary representation of $key
|
|
$key = hex2bin($strkey);
|
|
|
|
/// check key length, must be 256 bit or 32 bytes
|
|
if (mb_strlen($key, "8bit") !== 32) {
|
|
throw new Exception("Needs a 256-bit key!");
|
|
}
|
|
|
|
/// calculate iv size
|
|
$iv_size = openssl_cipher_iv_length("aes-256-cbc");
|
|
|
|
/// breakdown parts
|
|
if($binary) {
|
|
$decoded = $str;
|
|
} else {
|
|
$decoded = base64_decode($str);
|
|
}
|
|
$signature = mb_substr($decoded,0,10,"8bit");
|
|
$iv = mb_substr($decoded,10,$iv_size,"8bit");
|
|
$encrypted = mb_substr($decoded,$iv_size+10,NULL,"8bit");
|
|
|
|
/// check signature, against padding oracle attack
|
|
$calc_signature = mb_substr(hash_hmac("sha256", $encrypted, $key, true),0,10,"8bit");
|
|
if(!mc_compare($signature,$calc_signature)) {
|
|
return "SIGNATURE_NOT_MATCH"; /// signature doesn't match
|
|
}
|
|
|
|
$decrypted = openssl_decrypt($encrypted, "aes-256-cbc", $key, OPENSSL_RAW_DATA, $iv);
|
|
|
|
return $decrypted;
|
|
}
|
|
|
|
/// Compare Function
|
|
function mc_compare($a, $b) {
|
|
/// compare individually to prevent timing attacks
|
|
|
|
/// compare length
|
|
if (strlen($a) !== strlen($b)) return false;
|
|
|
|
/// compare individual
|
|
$result = 0;
|
|
for($i = 0; $i < strlen($a); $i ++) {
|
|
$result |= ord($a[$i]) ^ ord($b[$i]);
|
|
}
|
|
|
|
return $result == 0;
|
|
}
|
|
|
|
|
|
function mc_get_key($kode_rs) {
|
|
$kode_rs = preg_replace("/[^A-Za-z0-9]/", '', $kode_rs);
|
|
$arrno = str_split($kode_rs,1);
|
|
$sum = 0;
|
|
foreach($arrno as $no) {
|
|
$sum += $no;
|
|
}
|
|
$pos = $sum % (strlen(ENCRYPTION_KEY)-strlen($kode_rs));
|
|
$key = substr(ENCRYPTION_KEY,0,$pos).$kode_rs.substr(ENCRYPTION_KEY,$pos+strlen($kode_rs));
|
|
$key = hash_hmac("sha256", $key, ENCRYPTION_KEY);
|
|
return $key;
|
|
}
|
|
|
|
function _cbg_sqldate($sqldate) {
|
|
ereg("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $sqldate, $regs);
|
|
return sprintf("%02d/%02d/%04d", $regs[3], $regs[2], $regs[1]);
|
|
}
|
|
|
|
function check_port($host,$port,$timeout=1) {
|
|
$conn = @fsockopen($host, $port, $errno, $errstr, $timeout);
|
|
if ($conn) {
|
|
fclose($conn);
|
|
return TRUE;
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
function array_get_path($data, $path, &$result){
|
|
$found = true;
|
|
$path = explode("/", $path);
|
|
for ($x=0; ($x < count($path) and $found); $x++){
|
|
$key = $path[$x];
|
|
if (isset($data[$key])) {
|
|
$data = $data[$key];
|
|
} else {
|
|
$found = false;
|
|
}
|
|
}
|
|
$result = $data;
|
|
return $found;
|
|
}
|
|
|
|
function rgb2hex2rgb($c) {
|
|
|
|
if (!$c) return false;
|
|
|
|
$c = strtolower(trim($c));
|
|
$out = false;
|
|
|
|
if (preg_match("/^[0-9ABCDEFabcdef\#]+$/i", $c)) {
|
|
|
|
$c = str_replace('#','', $c);
|
|
$l = strlen($c) == 3 ? 1 : (strlen($c) == 6 ? 2 : false);
|
|
|
|
if ($l) {
|
|
unset($out);
|
|
$out['red'] = (int) hexdec(substr($c, 0,1*$l));
|
|
$out['green'] = (int) hexdec(substr($c, 1*$l,1*$l));
|
|
$out['blue'] = (int) hexdec(substr($c, 2*$l,1*$l));
|
|
} else $out = false;
|
|
|
|
} elseif (preg_match("/^[0-9]+(,| |.)+[0-9]+(,| |.)+[0-9]+$/i", $c)) {
|
|
$spr = str_replace(array(',',' ','.'), ':', $c);
|
|
$e = explode(":", $spr);
|
|
if(count($e) != 3) return false;
|
|
$out = '#';
|
|
for ($i = 0; $i<3; $i++) $e[$i] = dechex(($e[$i] <= 0) ? 0 : (($e[$i] >= 255) ? 255 : $e[$i] ));
|
|
for ($i = 0; $i<3; $i++) $out .= ((strlen($e[$i]) < 2) ? '0' : '').$e[$i];
|
|
$out = strtolower($out);
|
|
|
|
} else $out = false;
|
|
|
|
return $out;
|
|
}
|
|
|
|
function URLopen($url) {
|
|
// Fake the browser type
|
|
ini_set('user_agent','MSIE 4\.0b2;');
|
|
$dh = fopen("$url",'r');
|
|
$result = fread($dh,8192);
|
|
return $result;
|
|
}
|
|
|
|
function _bool_role_access($user_id,$role_id) {
|
|
$db = new Database();
|
|
$sql = "SELECT * FROM ".XOCP_PREFIX."user_role WHERE user_id = '$user_id' AND status_cd = 'normal' AND role_id = '$role_id'";
|
|
$result = $db->query($sql);
|
|
if($db->getRowsNum($result)>0) {
|
|
return TRUE;
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
function _fromDays($days) {
|
|
$db = new Database();
|
|
$sql = "SELECT FROM_DAYS('$days')";
|
|
$result = $db->query($sql);
|
|
list($dttm)=$db->fetchRow($result);
|
|
return $dttm;
|
|
}
|
|
|
|
function convert_to_username($fullname) {
|
|
$xname = explode('A/L', $fullname);
|
|
$xname2 = explode('A/P', $xname[0]);
|
|
$xname3 = explode('BIN', $xname2[0]);
|
|
$xname4 = explode('@', $xname3[0]);
|
|
|
|
$uname = str_replace(' ', '', $xname4[0]);
|
|
$uname = str_replace(',', '', $uname);
|
|
$uname = str_replace('.', '', $uname);
|
|
$uname = str_replace('-', '', $uname);
|
|
$uname = str_replace('`', '', $uname);
|
|
$uname = str_replace('(', '', $uname);
|
|
$uname = str_replace(')', '', $uname);
|
|
$uname = strtolower(str_replace('\'', '', $uname));
|
|
$uname = strtolower(str_replace('/', '', $uname));
|
|
$uname = substr($uname,0,12);
|
|
|
|
$uname = strtolower($uname); //turn all lowercase
|
|
|
|
return $uname;
|
|
}
|
|
|
|
|
|
function dateDifference($date_1 , $date_2 , $differenceFormat = '%yY %mM %dD' ) {
|
|
$datetime1 = date_create($date_1);
|
|
$datetime2 = date_create($date_2);
|
|
$interval = date_diff($datetime1, $datetime2);
|
|
return $interval->format($differenceFormat);
|
|
}
|
|
|
|
function _calcAge($age_day) {
|
|
$year = floor($age_day/365.25);
|
|
$month = floor(12*(($age_day/365.25)-$year));
|
|
if($month>0) {
|
|
$age = "(<span class='age'>$year tahun, $month bulan</span>)";
|
|
} else {
|
|
$age = "(<span class='age'>$year tahun</span>)";
|
|
}
|
|
return $age;
|
|
}
|
|
|
|
function _generate_data_link($source_app,$source_id) { //// link between data
|
|
$db = new Database();
|
|
$sql = "INSERT INTO ".XOCP_PREFIX."data_link (source_app,source_id) VALUES ('".addslashes($source_app)."','".addslashes($source_id)."')";
|
|
$result = $db->query($sql);
|
|
$data_link_id = $db->getInsertId();
|
|
return $data_link_id;
|
|
}
|
|
|
|
function getUserID() {
|
|
if(is_object($_SESSION["xocp_user"])) {
|
|
return $_SESSION["xocp_user"]->getVar("user_id");
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
function getPgroupID() {
|
|
if(is_object($_SESSION["xocp_user"])) {
|
|
return $_SESSION["xocp_user"]->getVar("pgroup_id");
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
function getEmployeeID() {
|
|
if(is_object($_SESSION["xocp_user"])) {
|
|
$user_id = $_SESSION["xocp_user"]->getVar("user_id");
|
|
$db = new Database();
|
|
$sql = "SELECT b.employee_id FROM ".XOCP_PREFIX."users a LEFT JOIN ".XOCP_PREFIX."employee b USING(person_id) WHERE a.user_id = '$user_id'";
|
|
$result = $db->query($sql);
|
|
if($db->getRowsNum($result)>0) {
|
|
list($employee_id)=$db->fetchRow($result);
|
|
return $employee_id;
|
|
}
|
|
return 0;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
function getUserName() {
|
|
if(is_object($_SESSION["xocp_user"])) {
|
|
return $_SESSION["xocp_user"]->getVar("user_nm");
|
|
} else {
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
function getUserFullname($user_id=0) {
|
|
if($user_id>0) {
|
|
$db = new Database();
|
|
$sql = "SELECT b.person_nm FROM xocp_users a LEFT JOIN xocp_persons b USING(person_id)"
|
|
. " WHERE a.user_id = '$user_id'";
|
|
$result = $db->query($sql);
|
|
if($db->getRowsNum($result)>0) {
|
|
list($user_fullname)=$db->fetchRow($result);
|
|
$result->free();
|
|
return $user_fullname;
|
|
} else {
|
|
return "";
|
|
}
|
|
} else {
|
|
if(is_object($_SESSION["xocp_user"])) {
|
|
return trim($_SESSION["xocp_user"]->getVar("title_front")." ".$_SESSION["xocp_user"]->getVar("person_nm")." ".$_SESSION["xocp_user"]->getVar("title_back"));
|
|
} else {
|
|
return "";
|
|
}
|
|
}
|
|
}
|
|
|
|
function getfilesizetext($file_size=0) {
|
|
if($file_size>1048576) {
|
|
$file_size_txt = number_format($file_size/1048576,2,".",",")." Mb";
|
|
} else if($file_size>1024) {
|
|
$file_size_txt = number_format($file_size/1024,2,".",",")." kb";
|
|
} else {
|
|
$file_size_txt = number_format($file_size,2,".",",")." b";
|
|
}
|
|
return $file_size_txt;
|
|
}
|
|
|
|
function getpdfpagecount($file) {
|
|
//where $file is the full path to your PDF document.
|
|
if(file_exists($file)) {
|
|
|
|
//open the file for reading
|
|
if($handle = @fopen($file, "rb")) {
|
|
$count = 0;
|
|
$i=0;
|
|
while (!feof($handle)) {
|
|
if($i > 0) {
|
|
$contents .= fread($handle,8152);
|
|
} else {
|
|
$contents = fread($handle, 1000);
|
|
//In some pdf files, there is an N tag containing the number of
|
|
//of pages. This doesn't seem to be a result of the PDF version.
|
|
//Saves reading the whole file.
|
|
if(preg_match("/\/N\s+([0-9]+)/", $contents, $found)) {
|
|
return $found[1];
|
|
}
|
|
}
|
|
$i++;
|
|
}
|
|
|
|
fclose($handle);
|
|
|
|
//get all the trees with 'pages' and 'count'. the biggest number
|
|
//is the total number of pages, if we couldn't find the /N switch above.
|
|
|
|
if(preg_match_all("/\/Type\s*\/Pages\s*.*\s*\/Count\s+([0-9]+)/", $contents, $capture, PREG_SET_ORDER)) {
|
|
foreach($capture as $c) {
|
|
if($c[1] > $count) $count = $c[1];
|
|
}
|
|
return $count;
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
function saveLastPage($page_id) {
|
|
$db = new Database();
|
|
$user_id = getUserID();
|
|
$user_nm = getUserName();
|
|
if($user_nm==$GLOBALS["xocpConfig"]["guestuser"]) {
|
|
return;
|
|
}
|
|
if($page_id == "syschpgroup") {
|
|
return;
|
|
}
|
|
|
|
if(isset($_SESSION["menuid"])&&$_SESSION["menuid"]>0) {
|
|
$setmenu = ",last_menuitem_id = '".$_SESSION["menuid"]."'";
|
|
} else {
|
|
$setmenu = "";
|
|
}
|
|
|
|
$pgroup_id = $_SESSION["xocp_user"]->getVar("pgroup_id");
|
|
$sql = "UPDATE ".XOCP_PREFIX."user_pgroup SET last_page_id = '$page_id' $setmenu"
|
|
. " WHERE user_id = '$user_id' AND pgroup_id = '$pgroup_id'";
|
|
$db->query($sql);
|
|
}
|
|
|
|
function ss_timing_start ($name = 'default') {
|
|
global $ss_timing_start_times;
|
|
$ss_timing_start_times[$name] = explode(' ', microtime());
|
|
}
|
|
|
|
function ss_timing_stop ($name = 'default') {
|
|
global $ss_timing_stop_times;
|
|
$ss_timing_stop_times[$name] = explode(' ', microtime());
|
|
}
|
|
|
|
function ss_timing_current ($name = 'default') {
|
|
global $ss_timing_start_times, $ss_timing_stop_times;
|
|
if (!isset($ss_timing_start_times[$name])) {
|
|
return 0;
|
|
}
|
|
if (!isset($ss_timing_stop_times[$name])) {
|
|
$stop_time = explode(' ', microtime());
|
|
} else {
|
|
$stop_time = $ss_timing_stop_times[$name];
|
|
}
|
|
// do the big numbers first so the small ones aren't lost
|
|
$current = $stop_time[1] - $ss_timing_start_times[$name][1];
|
|
$current += $stop_time[0] - $ss_timing_start_times[$name][0];
|
|
return $current;
|
|
}
|
|
|
|
function ss_timing_result() {
|
|
//$ret = "<div style='font-size: smaller;'>";
|
|
ss_timing_stop();
|
|
$ret .= sprintf("Page took %s seconds to load.",ss_timing_current());
|
|
//$ret .= "</div>\n";
|
|
return $ret;
|
|
}
|
|
|
|
|
|
|
|
function toMoney($arg,$digit=2) {
|
|
if($GLOBALS["xocpConfig"]["language"]=="english") {
|
|
return number_format($arg,$digit,".",",");
|
|
} else {
|
|
return number_format($arg,$digit,",",".");
|
|
}
|
|
}
|
|
|
|
function toMoneyShort($arg) {
|
|
if($GLOBALS["xocpConfig"]["language"]=="english") {
|
|
return number_format($arg,0,".",",");
|
|
} else {
|
|
return number_format($arg,0,",",".");
|
|
}
|
|
}
|
|
|
|
function toMoneyShortID($arg) {
|
|
return number_format(floatval($arg),0,",",".");
|
|
}
|
|
|
|
function formatQueryString($qstr) {
|
|
$qstr = preg_replace("/[[:space:]+ ><()~*:&|.\"-]+/"," ",trim(strtolower($qstr)));
|
|
$qstr = preg_replace("/[[:space:]]+/"," ",trim(strtolower($qstr)));
|
|
$q_array = explode(" ",$qstr);
|
|
$qstr = "";
|
|
$positive = 0;
|
|
foreach($q_array as $str) {
|
|
if(strlen($str)<=1) continue;
|
|
$str = trim($str);
|
|
if($str[0]=="-") {
|
|
$str = "-(".substr($str,1).") ";
|
|
} else {
|
|
$positive++;
|
|
if($str[0]=="+") {
|
|
$str = substr($str,1);
|
|
}
|
|
$str = "+($str*) ";
|
|
}
|
|
$qstr .= $str;
|
|
}
|
|
$qstr = trim($qstr);
|
|
if(strlen($qstr)>0&&$positive>0) {
|
|
return $qstr;
|
|
} else {
|
|
return "+1n154n64tj4r4n6d1t3muk4n";
|
|
}
|
|
}
|
|
|
|
function parseForm($ret) { ///// parse form variables
|
|
$arr = explode("@@",urldecode($ret));
|
|
$vars=array();
|
|
if(is_array($arr)) {
|
|
foreach($arr as $i=>$m) {
|
|
list($k,$v)=explode("^^",$m);
|
|
if($k=="") continue;
|
|
if(substr($k,-2,2)=="[]") {
|
|
$a = substr($k,0,-2);
|
|
if(!is_array($vars[$a])) {
|
|
$vars[$a] = array();
|
|
}
|
|
array_push($vars[$a],$v);
|
|
} else {
|
|
$vars[$k]=$v;
|
|
}
|
|
}
|
|
}
|
|
return $vars;
|
|
}
|
|
|
|
function _parseForm($frm) {
|
|
$vars=array();
|
|
$arr = explode("@@",urldecode($frm));
|
|
// parse form variables
|
|
if(is_array($arr)) {
|
|
foreach($arr as $i=>$m) {
|
|
list($k,$v)=explode("^^",$m);
|
|
if($k=="") continue;
|
|
if(substr($k,-2,2)=="[]") {
|
|
$a = substr($k,0,-2);
|
|
if(!is_array($vars[$a])) {
|
|
$vars[$a] = array();
|
|
}
|
|
array_push($vars[$a],$v);
|
|
} else {
|
|
$vars[$k]=$v;
|
|
}
|
|
}
|
|
}
|
|
return $vars;
|
|
}
|
|
|
|
function _xocp_personNameFormated($person_nm,$title_front="",$title_back="") {
|
|
$title_front = trim($title_front);
|
|
$title_back = trim($title_back);
|
|
$person_nm = trim($person_nm);
|
|
$fullname = trim("$title_front $person_nm").($title_back!=""?", $title_back":"");
|
|
return $fullname;
|
|
}
|
|
|
|
// bc match ////////
|
|
bcscale(30);
|
|
function _bctrim($str) {
|
|
if(preg_match("/\./",$str)) {
|
|
$tmp_str = rtrim($str,"0");
|
|
$tmp_str = rtrim($tmp_str,".");
|
|
} else {
|
|
$tmp_str = $str;
|
|
}
|
|
return $tmp_str;
|
|
} //////////////////
|
|
|
|
function getSQLDate($sqldate=NULL,$type="datetime") {
|
|
$y=$m=$d=$hh=$mm=$ss="";
|
|
if($sqldate!="") {
|
|
list($dt,$tm)=explode(" ",$sqldate);
|
|
preg_match( "/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/", $dt, $regs1 );
|
|
preg_match( "/([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})/", $tm, $regs2 );
|
|
$y = sprintf("%04d",$regs1[1]);
|
|
$m = sprintf("%02d",$regs1[2]);
|
|
$d = sprintf("%02d",$regs1[3]);
|
|
$hh = sprintf("%02d",$regs2[1]);
|
|
$mm = sprintf("%02d",$regs2[2]);
|
|
$ss = sprintf("%02d",$regs2[3]);
|
|
} else {
|
|
$regs = getdate();
|
|
$y = sprintf("%04d",$regs["year"]);
|
|
$m = sprintf("%02d",$regs["mon"]);
|
|
$d = sprintf("%02d",$regs["mday"]);
|
|
$hh = sprintf("%02d",$regs["hours"]);
|
|
$mm = sprintf("%02d",$regs["minutes"]);
|
|
$ss = sprintf("%02d",$regs["seconds"]);
|
|
}
|
|
switch($type) {
|
|
case "date":
|
|
return "$y-$m-$d 00:00:00";
|
|
break;
|
|
default:
|
|
return "$y-$m-$d $hh:$mm:$ss";
|
|
break;
|
|
}
|
|
}
|
|
|
|
function fromsqldate($sqldate) {
|
|
preg_match( "/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/", $sqldate, $regs );
|
|
return "$regs[3]-$regs[2]-$regs[1]";
|
|
}
|
|
|
|
function sql2ind($sqldate,$type="datetime") {
|
|
global $xocp_vars;
|
|
$tgl_ind = "";
|
|
$bulan = $xocp_vars['month_year_short'];
|
|
list($dt,$tm)=explode(" ",$sqldate);
|
|
preg_match( "/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/", $dt, $regs1 );
|
|
list ($fulldt,$thn,$bln,$tgl) = $regs1;
|
|
$bln += 0;
|
|
$tgl += 0;
|
|
$thn += 0;
|
|
if($thn == 0 || $tgl == 0 || $bln == 0) return "????-??-??";
|
|
if($type == "datetimesec") {
|
|
preg_match( "/([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})/", $tm, $regs2 );
|
|
list($fulltm,$jam,$menit,$detik) = $regs2;
|
|
$tm_ind = sprintf(" %02d:%02d:%02d",$jam,$menit,$detik);
|
|
} else if($tm != "" && $type != "date") {
|
|
preg_match( "/([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})/", $tm, $regs2 );
|
|
list($fulltm,$jam,$menit,$detik) = $regs2;
|
|
$tm_ind = sprintf(" %02d:%02d",$jam,$menit);
|
|
} else {
|
|
$tm_ind = "";
|
|
}
|
|
if($type != "time") {
|
|
$tgl_ind = "$tgl $bulan[$bln] $thn";
|
|
}
|
|
return trim("$tgl_ind$tm_ind");
|
|
}
|
|
|
|
function sql2indshort($sqldate,$type="datetime") {
|
|
global $xocp_vars;
|
|
$tgl_ind = "";
|
|
$bulan = $xocp_vars['month_year_short'];
|
|
list($dt,$tm)=explode(" ",$sqldate);
|
|
preg_match( "/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/", $dt, $regs1 );
|
|
list ($fulldt,$thn,$bln,$tgl) = $regs1;
|
|
$bln += 0;
|
|
$tgl += 0;
|
|
$thn += 0;
|
|
if($thn == 0 || $tgl == 0 || $bln == 0) return "????-??-??";
|
|
if($type == "datetimesec") {
|
|
preg_match( "/([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})/", $tm, $regs2 );
|
|
list($fulltm,$jam,$menit,$detik) = $regs2;
|
|
$tm_ind = sprintf(" %02d:%02d:%02d",$jam,$menit,$detik);
|
|
} else if($tm != "" && $type != "date") {
|
|
preg_match( "/([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})/", $tm, $regs2 );
|
|
list($fulltm,$jam,$menit,$detik) = $regs2;
|
|
$tm_ind = sprintf(" %02d:%02d",$jam,$menit);
|
|
} else {
|
|
$tm_ind = "";
|
|
}
|
|
if($type != "time") {
|
|
$tgl_ind = "$tgl $bulan[$bln] $thn";
|
|
}
|
|
return trim("$tgl_ind$tm_ind");
|
|
}
|
|
|
|
function sql2indshortday($sqldate,$type="datetime") {
|
|
global $xocp_vars;
|
|
$tgl_ind = "";
|
|
$bulan = $xocp_vars['month_year_short'];
|
|
list($dt,$tm)=explode(" ",$sqldate);
|
|
preg_match( "/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/", $dt, $regs1 );
|
|
list ($fulldt,$thn,$bln,$tgl) = $regs1;
|
|
$bln += 0;
|
|
$tgl += 0;
|
|
$thn += 0;
|
|
if($thn == 0 || $tgl == 0 || $bln == 0) return "????-??-??";
|
|
if($type == "datetimesec") {
|
|
preg_match( "/([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})/", $tm, $regs2 );
|
|
list($fulltm,$jam,$menit,$detik) = $regs2;
|
|
$tm_ind = sprintf(" %02d:%02d:%02d",$jam,$menit,$detik);
|
|
} else if($tm != "" && $type != "date") {
|
|
preg_match( "/([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})/", $tm, $regs2 );
|
|
list($fulltm,$jam,$menit,$detik) = $regs2;
|
|
$tm_ind = sprintf(" %02d:%02d",$jam,$menit);
|
|
} else {
|
|
$tm_ind = "";
|
|
}
|
|
|
|
$tahuntmp = intval($thn);
|
|
$bulantmp = intval($bln);
|
|
$haritmp = intval($tgl);
|
|
$num_day = date("w", mktime (0,0,0,$bulantmp,$haritmp,$tahuntmp));
|
|
$day = $xocp_vars["dayofweek2short"][$num_day] . ",";
|
|
|
|
if($type != "time") {
|
|
$tgl_ind = "$tgl $bulan[$bln] $thn";
|
|
}
|
|
|
|
return trim(" $day $tgl $bulan[$bln] $thn");
|
|
}
|
|
|
|
function sql2ind2($sqldate) {
|
|
global $xocp_vars;
|
|
$bulan = $xocp_vars['month_year'];
|
|
preg_match( "/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/", $sqldate, $regs );
|
|
list ($full,$thn,$bln,$tgl) = $regs;
|
|
$bln += 0;
|
|
$bln = $bln == 0 ? "" : $bln;
|
|
$tgl += 0;
|
|
$tgl = $tgl == 0 ? "" : $tgl;
|
|
$thn += 0;
|
|
$thn = $thn == 0 ? "" : $thn;
|
|
return trim("$tgl $bulan[$bln] $thn");
|
|
}
|
|
|
|
|
|
//// old logging system /////////////////////////////////////////////////////////////////////
|
|
function debugit($cmd) {
|
|
error_log("$cmd\n",3,XOCP_DOC_ROOT."/tmp/phplog");
|
|
}
|
|
|
|
function auditlog($cmd) {
|
|
error_log(date("Y-m-d H:i:s ").$_SERVER["REMOTE_ADDR"]." ".$_SESSION["xocp_user"]->getVar("user_nm")." : $cmd\n",3,XOCP_DOC_ROOT."/tmp/auditlog");
|
|
}
|
|
|
|
function mylog_secure($cmd) {
|
|
error_log("$cmd\n",3,XOCP_DOC_ROOT."/tmp/securelog");
|
|
}
|
|
|
|
function ajaxlog($txt) {
|
|
error_log("$txt\n",3,XOCP_DOC_ROOT."/tmp/ajaxlog");
|
|
}
|
|
|
|
function dumpit($var) {
|
|
ob_start();
|
|
print_r($var);
|
|
debugit(ob_get_contents());
|
|
ob_end_clean();
|
|
}
|
|
|
|
//// new logging system /////////////////////////////////////////////////////////////////////
|
|
function _debuglog($cmd,$go=FALSE) {
|
|
$go = (_XOCP_DEBUG_LOG==1?TRUE:FALSE);
|
|
if($go) error_log("$cmd\n",3,XOCP_DOC_ROOT."/tmp/debuglog");
|
|
}
|
|
|
|
function _patklinlog($cmd) {
|
|
error_log("$cmd\n",3,XOCP_DOC_ROOT."/tmp/patklinlog");
|
|
}
|
|
|
|
function _breaklog($cmd) {
|
|
error_log("$cmd\n",3,XOCP_DOC_ROOT."/tmp/breaklog");
|
|
}
|
|
|
|
function _auditlog($cmd) {
|
|
error_log(date("YmdHis ").$_SERVER["REMOTE_ADDR"]." ".$_SESSION["xocp_user"]->getVar("user_nm")."/"
|
|
.$_SESSION["xocp_user"]->getVar("user_id")." : $cmd\n",3,XOCP_DOC_ROOT."/tmp/auditlog");
|
|
}
|
|
|
|
function _securelog($cmd) {
|
|
error_log("$cmd\n",3,XOCP_DOC_ROOT."/tmp/securelog");
|
|
}
|
|
|
|
function _ajaxlog($txt) {
|
|
error_log("$txt\n",3,XOCP_DOC_ROOT."/tmp/ajaxlog");
|
|
}
|
|
|
|
function _log_patient($src_app,$log_cd,$log_desc="",$read="read",$patient_id="") {
|
|
return;
|
|
$db = new Database();
|
|
$user_id = getUserID();
|
|
$ip_address=$_SERVER['REMOTE_ADDR'];
|
|
$mac_address = _getmacaddress($ip_address);
|
|
$sql = "INSERT INTO ".XOCP_PREFIX."log_patient (src_app,log_cd,log_type,log_desc,created_user_id,src_ip_address,src_mac_address,patient_id)"
|
|
. " VALUES ('".addslashes($src_app)."','".addslashes($log_cd)."','$read','".addslashes($log_desc)."','$user_id','$ip_address','$mac_address','$patient_id')";
|
|
$db->query($sql);
|
|
}
|
|
|
|
function _log_master($src_app,$log_cd,$log_desc="",$read="read",$obj_id="",$concept_id="") {
|
|
return;
|
|
$db = new Database();
|
|
$user_id = getUserID();
|
|
$ip_address=$_SERVER['REMOTE_ADDR'];
|
|
$mac_address = _getmacaddress($ip_address);
|
|
$sql = "INSERT INTO ".XOCP_PREFIX."log_master (src_app,log_cd,log_type,log_desc,created_user_id,src_ip_address,src_mac_address,obj_id,concept_id)"
|
|
. " VALUES ('".addslashes($src_app)."','".addslashes($log_cd)."','$read','".addslashes($log_desc)."','$user_id','$ip_address','$mac_address','$obj_id','$concept_id')";
|
|
//$db->query($sql);
|
|
}
|
|
|
|
function _dumpvar($var,$go=FALSE) {
|
|
ob_start();
|
|
print_r($var);
|
|
_debuglog(ob_get_contents(),$go);
|
|
ob_end_clean();
|
|
}
|
|
|
|
|
|
function _activitylog($src_app,$log_cd,$log_desc="",$read="read",$accessed_employee_id=0,$accessed_person_id=0) {
|
|
return;
|
|
$db = new Database();
|
|
$user_id = getUserID();
|
|
$ip_address=$_SERVER['REMOTE_ADDR'];
|
|
$mac_address = _getmacaddress($ip_address);
|
|
$accessed_employee_id += 0;
|
|
$accessed_person_id += 0;
|
|
$sql = "INSERT INTO ".XOCP_PREFIX."log_activity (src_app,log_cd,log_type,log_desc,created_user_id,src_ip_address,src_mac_address,accessed_employee_id,accessed_person_id)"
|
|
. " VALUES ('".addslashes($src_app)."','".addslashes($log_cd)."','$read','".addslashes($log_desc)."','$user_id','$ip_address','$mac_address','$accessed_employee_id','$accessed_person_id')";
|
|
//$db->query($sql);
|
|
}
|
|
|
|
function _getmacaddress($ip_address) {
|
|
$mac_addr="";
|
|
|
|
/// run the external command, break output into lines
|
|
|
|
return "";
|
|
|
|
//// for mac mini
|
|
$arp = trim(`which arp`); $arp_result=`$arp -n $ip_address`; $lines=explode("\n", $arp_result);
|
|
/// look for the output line describing our IP address
|
|
foreach($lines as $line) {
|
|
$cols=preg_split('/\s+/', trim($line));
|
|
if (isset($cols[1])&&$cols[1]=="($ip_address)") {
|
|
$mac_addr=$cols[3];
|
|
}
|
|
}
|
|
|
|
return $mac_addr;
|
|
}
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
function score_cmp($a,$b) {
|
|
$al = end($a);
|
|
$bl = end($b);
|
|
if($al == $bl) {
|
|
return 0;
|
|
}
|
|
return ($al > $bl ? -1:1);
|
|
}
|
|
|
|
|
|
function catchVar($module) {
|
|
foreach($_REQUEST as $key => $val) {
|
|
$v = explode("_",$key);
|
|
if($v[0] == "X") {
|
|
array_shift($v);
|
|
$m_nm = implode("_",$v);
|
|
//if($m_nm == $module) {
|
|
return $val;
|
|
//} else {
|
|
// return NULL;
|
|
//}
|
|
} elseif ($v[0] == "XP") {
|
|
array_shift($v);
|
|
_activitylog("FRAMEWORK","CHANGE_PAGE","Change page to: ".$v[0],"read_write");
|
|
array_shift($v);
|
|
$m_nm = implode("_",$v);
|
|
//if($m_nm == $module) {
|
|
return $val;
|
|
//} else {
|
|
// return NULL;
|
|
//}
|
|
}
|
|
}
|
|
}
|
|
|
|
function catchPage() {
|
|
foreach($_REQUEST as $key => $val) {
|
|
$v = explode("_",$key);
|
|
if ($v[0] == "XP") {
|
|
$page_id = $v[1];
|
|
|
|
if(!file_exists(XOCP_DOC_ROOT."/cache/pages/${page_id}.php")) {
|
|
die("Page not found.");
|
|
}
|
|
|
|
if(trim($page_id) == "") {
|
|
$page_id = $GLOBALS["xocpConfig"]["startpage"];
|
|
} else {
|
|
if(isset($_GET["menuid"])) {
|
|
$_SESSION["menuid"] = $_GET["menuid"];
|
|
}
|
|
if(isset($_GET["mpid"])) {
|
|
$_SESSION["mpid"] = $_GET["mpid"];
|
|
}
|
|
}
|
|
|
|
if(isset($_SESSION["xocp_user"]) && isset($_SESSION["xocp_user"]->allowed_pages[$page_id]) &&
|
|
$_SESSION["xocp_user"]->allowed_pages[$page_id] == 1) {
|
|
$_SESSION["xocp_page_id"] = $page_id;
|
|
$_SESSION["menuitem_id"] = $_SESSION["xocp_user"]->pages_menuitem_id[$page_id];
|
|
} else {
|
|
$_SESSION["xocp_page_id"] = $GLOBALS["xocpConfig"]["startpage"];
|
|
}
|
|
return array("XP",$page_id);
|
|
} elseif($v[0] == "XG") {
|
|
$_SESSION["xocp_page_id"] = $val;
|
|
$_SESSION["xocp_user"]->setVar("pgroup_id",$v[1]);
|
|
$_SESSION["xocp_user"]->storeGroup();
|
|
$_SESSION["xocp_user"]->reload_pages();
|
|
|
|
return array("XG",$val);
|
|
} elseif ($_SESSION["xocp_user"]->getVar("user_id")>0) {
|
|
if(isset($_SESSION["xocp_page_id"])&&$_SESSION["xocp_page_id"]!="") {
|
|
$pxx = $_SESSION["xocp_page_id"];
|
|
if(isset($_SESSION["xocp_user"]->allowed_pages[$pxx])) {
|
|
return array("",$_SESSION["xocp_page_id"]);
|
|
} else {
|
|
return array("",$_SESSION["xocp_user"]->getVar("startpage"));
|
|
}
|
|
} else {
|
|
$pgroup_id = $_SESSION["xocp_user"]->getVar("pgroup_id");
|
|
$user_id = getUserID();
|
|
|
|
if($_SESSION["xocp_user"]->getVar("startpage")=="guest") {
|
|
///// fall back routine
|
|
$db = new Database();
|
|
$sql = "SELECT startpage FROM ".XOCP_PREFIX."pgroups WHERE pgroup_id = '$pgroup_id'";
|
|
$result = $db->query($sql);
|
|
list($startpage)=$db->fetchRow($result);
|
|
$_SESSION["xocp_page_id"] = $startpage;
|
|
return array("",$startpage);
|
|
} else {
|
|
$_SESSION["xocp_page_id"] = $_SESSION["xocp_user"]->getVar("startpage");
|
|
return array("",$_SESSION["xocp_user"]->getVar("startpage"));
|
|
}
|
|
}
|
|
} else {
|
|
if(isset($_SESSION["xocp_page_id"])&&$_SESSION["xocp_page_id"]!="") {
|
|
return array("",$_SESSION["xocp_page_id"]);
|
|
} else {
|
|
return array("",$_SESSION["xocp_user"]->getVar("startpage"));
|
|
}
|
|
}
|
|
}
|
|
return array("",$_SESSION["xocp_user"]->getVar("startpage"));
|
|
}
|
|
|
|
function runCopy($tblname,$pkname,$origin,$copy) {
|
|
$db = new Database();
|
|
$sql = "SELECT * FROM ".XOCP_PREFIX."$tblname WHERE $pkname = '$origin'";
|
|
$result = $db->query($sql);
|
|
if($db->getRowsNum($result)>0) {
|
|
while($data = $db->fetchArray($result)) {
|
|
$sql = "REPLACE INTO ".XOCP_PREFIX."$tblname";
|
|
$attr = $val = "";
|
|
foreach($data as $k=>$v) {
|
|
if($k == $pkname) $v = $copy;
|
|
$attr .= "$k,";
|
|
$val .= "'$v',";
|
|
}
|
|
$attr = substr($attr,0,-1);
|
|
$val = substr($val,0,-1);
|
|
$sql .= "($attr) VALUES ($val)";
|
|
$db->query($sql);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
* Function to display formatted times in user timezone
|
|
*/
|
|
function formatTimestamp($time, $format="l", $timeoffset="") {
|
|
if ( $timeoffset == "" ) {
|
|
if ( $_SESSION["xocp_user"] ) {
|
|
$timeoffset = $_SESSION["xocp_user"]->getVar("timezone_offset");
|
|
} else {
|
|
$timeoffset = $GLOBALS["xocpConfig"]['default_TZ'];
|
|
}
|
|
}
|
|
$usertimestamp = $time + ($timeoffset - $GLOBALS["xocpConfig"]['server_TZ'])*3600;
|
|
if ( $format == "s" ) {
|
|
$datestring = _SHORTDATESTRING;
|
|
} elseif ( $format == "m" ) {
|
|
$datestring = _MEDIUMDATESTRING;
|
|
} elseif ( $format == "l" ) {
|
|
$datestring = _DATESTRING;
|
|
} elseif ( $format == "mysql" ) {
|
|
$datestring = "Y-m-d H:i:s";
|
|
} elseif ( $format != "" ) {
|
|
$datestring = $format;
|
|
} else {
|
|
$datestring = _DATESTRING;
|
|
}
|
|
$datetime = date($datestring, $usertimestamp);
|
|
$datetime = ucfirst($datetime);
|
|
return $datetime;
|
|
}
|
|
|
|
/*
|
|
* Function to calculate server timestamp from user entered time (timestamp)
|
|
*/
|
|
function userTimeToServerTime($timestamp, $userTZ=NULL){
|
|
if ( !isset($userTZ) ) {
|
|
$userTZ = $GLOBALS["xocpConfig"]['default_TZ'];
|
|
}
|
|
$offset = $userTZ - $GLOBALS["xocpConfig"]['server_TZ'];
|
|
$timestamp = $timestamp - ($offset * 3600);
|
|
return $timestamp;
|
|
}
|
|
|
|
|
|
function makePass() {
|
|
$makepass="";
|
|
$syllables = array("er","in","tia","wol","fe","pre","vet","jo","nes","al","len","son","cha","ir","ler","bo","ok","tio","nar","sim","ple","bla","ten","toe","cho","co","lat","spe","ak","er","po","co","lor","pen","cil","li","ght","wh","at","the","he","ck","is","mam","bo","no","fi","ve","any","way","pol","iti","cs","ra","dio","sou","rce","sea","rch","pa","per","com","bo","sp","eak","st","fi","rst","gr","oup","boy","ea","gle","tr","ail","bi","ble","brb","pri","dee","kay","en","be","se");
|
|
srand((double)microtime()*1000000);
|
|
for ($count=1;$count<=4;$count++) {
|
|
if (rand()%10 == 1) {
|
|
$makepass .= sprintf("%0.0f",(rand()%50)+1);
|
|
} else {
|
|
$makepass .= sprintf("%s",$syllables[rand()%62]);
|
|
}
|
|
}
|
|
return $makepass;
|
|
}
|
|
|
|
function checkIp($ip){
|
|
global $xocpBadIps;
|
|
if ( !empty($xocpBadIps) ) {
|
|
foreach ($xocpBadIps as $xbi) {
|
|
if ( !empty($xbi) && preg_match("/".$xbi."/", $ip)) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function checkEmail($email){
|
|
if (!$email || !eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+([\.][a-z0-9-]+)+$",$email)){
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
function formatURL($url){
|
|
if (($url != "") && (!(eregi('(^http[s]*:[/]+)(.*)', $url)))){
|
|
$url = "http://" . $url;
|
|
}
|
|
return $url;
|
|
}
|
|
|
|
/*
|
|
* Prints allowed html tags on this site
|
|
*/
|
|
function get_allowed_html(){
|
|
$allowed = str_replace(">","> ",$GLOBALS["xocpConfig"]['allowed_html']);
|
|
return htmlspecialchars($allowed);
|
|
}
|
|
|
|
|
|
/*
|
|
* Just a simple wrap to php mail() function
|
|
*/
|
|
function xocp_mail($to, $subject, $message, $headers=""){
|
|
if ( $headers == "" ) {
|
|
$headers = "From: ".$GLOBALS["xocpConfig"]['sitename']." <".$GLOBALS["xocpConfig"]['adminmail'].">\n";
|
|
$headers .= "X-Mailer: PHP/".phpversion()."\n";
|
|
}
|
|
mail($to, $subject, $message, $headers);
|
|
}
|
|
|
|
function tinycss($whatdir) {
|
|
global $HTTP_USER_AGENT;
|
|
if(ereg('MSIE',$HTTP_USER_AGENT) && !ereg('Opera',$HTTP_USER_AGENT)){
|
|
$str_css = "tiny_style.css";
|
|
}else{
|
|
$str_css = "tiny_styleNN.css";
|
|
}
|
|
$themedir = XOCP_DOC_ROOT."/themes";
|
|
$filepath = "$themedir/$whatdir/style/$str_css";
|
|
$default = "$themedir/$whatdir/style/tiny_style.css";
|
|
if ( file_exists($filepath) ) {
|
|
//need to change to absolute path for inclusion from modules
|
|
$whatcss = "themes/$whatdir/style/$str_css";
|
|
} elseif ( file_exists($default) ) {
|
|
$whatcss = "themes/$whatdir/style/tiny_style.css";
|
|
} else {
|
|
$whatcss = "";
|
|
}
|
|
return $whatcss."?serial=100000037";
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
* Function to get a user selected theme file
|
|
*/
|
|
function getTheme($theme=""){
|
|
global $xocp_db;
|
|
$themedir = XOCP_DOC_ROOT."/themes";
|
|
if ( !isset($theme) || trim($theme) == "" ) {
|
|
if ( isset($_SESSION) && is_object($_SESSION["xocp_user"]) && strtolower(get_class($_SESSION["xocp_user"])) == "xocpuser") {
|
|
$theme = $_SESSION["xocp_user"]->getVar("theme");
|
|
if ( isset($theme) && $theme != "" ) {
|
|
if ( file_exists($themedir."/".$theme."/theme.php") ) {
|
|
return $theme;
|
|
}
|
|
} else {
|
|
return $GLOBALS["xocpConfig"]['default_theme'];
|
|
}
|
|
} else {
|
|
return $GLOBALS["xocpConfig"]['default_theme'];
|
|
}
|
|
} else {
|
|
$theme = trim($theme);
|
|
if ( file_exists("${themedir}/${theme}/theme.php") ) {
|
|
return $theme;
|
|
}
|
|
}
|
|
return $GLOBALS["xocpConfig"]['default_theme'];
|
|
}
|
|
|
|
/*
|
|
* Function to get css file for a certain theme
|
|
*/
|
|
function getcss($whatdir) {
|
|
global $HTTP_USER_AGENT;
|
|
$str_css = "style.css.php";
|
|
$themedir = XOCP_DOC_ROOT."/themes";
|
|
$filepath = "$themedir/$whatdir/style/$str_css";
|
|
$default = "$themedir/$whatdir/style/style.css.php";
|
|
if ( file_exists($filepath) ) {
|
|
//need to change to absolute path for inclusion from modules
|
|
$whatcss = "themes/$whatdir/style/$str_css";
|
|
} elseif ( file_exists($default) ) {
|
|
$whatcss = "themes/$whatdir/style/style.css.php";
|
|
} else {
|
|
$whatcss = "";
|
|
}
|
|
return $whatcss."?serial=100000030";
|
|
}
|
|
|
|
/*
|
|
* Function to display a message encouraging users
|
|
* to use web standards browser
|
|
*/
|
|
function waspInfo() {
|
|
return "<p class='ahem'><small>This site will look MUCH better in a browser that supports <a title='The Web Standards Project's BROWSER UPGRADE initiative.' href='http://www.webstandards.org/upgrade/'>web standards</a>, but its content is accessible to any browser or Internet device.</small></p>";
|
|
}
|
|
|
|
function avatarExists($uid){
|
|
// recommend not to change this
|
|
$allowed_ext = array("gif", "jpeg", "jpg", "png");
|
|
foreach($allowed_ext as $ext){
|
|
if ( file_exists(XOCP_DOC_ROOT."/images/avatar/users/".$uid.".".$ext ) ) {
|
|
return "users/".$uid.".".$ext;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function sanitize_filename($filename, $forceextension="") {
|
|
/// from timdw in http://forums.codecharge.com/posts.php?post_id=75694
|
|
|
|
/*
|
|
1. Remove leading and trailing dots
|
|
2. Remove dodgy characters from filename, including spaces and dots except last.
|
|
3. Force extension if specified
|
|
*/
|
|
|
|
$defaultfilename = "none";
|
|
$dodgychars = "[^0-9a-zA-Z()_-]"; // allow only alphanumeric, underscore, parentheses and hyphen
|
|
|
|
$filename = preg_replace("/^[.]*/","",$filename); // lose any leading dots
|
|
$filename = preg_replace("/[.]*$/","",$filename); // lose any trailing dots
|
|
$filename = $filename?$filename:$defaultfilename; // if filename is blank, provide default
|
|
|
|
$lastdotpos=strrpos($filename, "."); // save last dot position
|
|
$filename = preg_replace("/$dodgychars/","_",$filename); // replace dodgy characters
|
|
$afterdot = "";
|
|
if ($lastdotpos !== false) { // Split into name and extension, if any.
|
|
$beforedot = substr($filename, 0, $lastdotpos);
|
|
if ($lastdotpos < (strlen($filename) - 1)) {
|
|
$afterdot = substr($filename, $lastdotpos + 1);
|
|
}
|
|
} else { // no extension
|
|
$beforedot = $filename;
|
|
}
|
|
|
|
if ($forceextension&&$forceextension!="") {
|
|
$filename = $beforedot . "." . $forceextension;
|
|
} elseif ($afterdot) {
|
|
$filename = $beforedot . "." . $afterdot;
|
|
} else {
|
|
$filename = $beforedot;
|
|
}
|
|
|
|
return $filename;
|
|
}
|
|
|
|
/**
|
|
* generates html select dropdown list with options
|
|
* if values is two dimensional then adds optgroup too
|
|
*
|
|
* @param string $name selectbox name and id
|
|
* @param array $values options
|
|
* @param mixed $selected selected option
|
|
* @param array $attributes additonal attributes
|
|
*
|
|
* @return string html source with selectbox
|
|
$opt_array[$parent_id][$id] = $label;
|
|
$labels[$id] = $label;
|
|
*/
|
|
function render_optgroup($parent_id,$opt_array=array(),$labels=array(),$parents=array()) {
|
|
$ret = "";
|
|
$rfunc = __FUNCTION__;
|
|
if(!is_array($opt_array)) return;
|
|
foreach($opt_array[$parent_id] as $k=>$v) {
|
|
if(is_array($opt_array[$k])) {
|
|
$ret .= "\n<optgroup label='".$labels[$k]."'>".$rfunc($k,$opt_array,$labels,$parents)."\n</optgroup>";
|
|
} else {
|
|
$ret .= "\n <option value='$k'>$v</option>";
|
|
}
|
|
}
|
|
return $ret;
|
|
}
|
|
|
|
|
|
|
|
} // XOCP_FUNCTIONS_DEFINED
|
|
|