0byt3m1n1
Path:
/
home
/
k74ns2xygros
/
www
/
clientms
/
client
/
includes
/
[
Home
]
File: function.php
<?php function getDB() { $dbhost = DB_HOST; $dbuser = DB_USER; $dbpass = DB_PASS; $dbname = DB_NAME; try { $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname;", $dbuser, $dbpass); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $dbh->query('SET NAMES utf8'); return $dbh; } catch (PDOException $e) { echo 'Connection Failed'; } } function getOS() { $user_agent = $_SERVER['HTTP_USER_AGENT']; $os_platform = "Unknown"; $os_array = array( '/windows nt 10/i' => 'Windows 10', '/windows nt 6.3/i' => 'Windows 8.1', '/windows nt 6.2/i' => 'Windows 8', '/windows nt 6.1/i' => 'Windows 7', '/windows nt 6.0/i' => 'Windows Vista', '/windows nt 5.2/i' => 'Windows Server 2003/XP x64', '/windows nt 5.1/i' => 'Windows XP', '/windows xp/i' => 'Windows XP', '/windows nt 5.0/i' => 'Windows 2000', '/windows me/i' => 'Windows ME', '/win98/i' => 'Windows 98', '/win95/i' => 'Windows 95', '/win16/i' => 'Windows 3.11', '/macintosh|mac os x/i' => 'Mac OS X', '/mac_powerpc/i' => 'Mac OS 9', '/linux/i' => 'Linux', '/ubuntu/i' => 'Ubuntu', '/iphone/i' => 'iPhone', '/ipod/i' => 'iPod', '/ipad/i' => 'iPad', '/android/i' => 'Android', '/blackberry/i' => 'BlackBerry', '/webos/i' => 'Mobile' ); foreach ($os_array as $regex => $value) if (preg_match($regex, $user_agent)) $os_platform = $value; return $os_platform; } function consolidate_spaces($string) { $result = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $string); return $result; } function fromExcelToLinux($excel_time) { $linux_time = ($excel_time - 25569) * 86400; $getDat = date("d-M-y", $linux_time); return $getDat; } function getBrowser() { $u_agent = $_SERVER['HTTP_USER_AGENT']; $bname = 'Unknown'; $platform = 'Unknown'; $version = ""; // Next get the name of the useragent yes seperately and for good reason if (preg_match('/MSIE/i', $u_agent) && !preg_match('/Opera/i', $u_agent)) { $bname = 'Internet Explorer'; $ub = "MSIE"; } elseif (preg_match('/Firefox/i', $u_agent)) { $bname = 'Mozilla Firefox'; $ub = "Firefox"; } elseif (preg_match('/Chrome/i', $u_agent)) { $bname = 'Google Chrome'; $ub = "Chrome"; } elseif (preg_match('/Safari/i', $u_agent)) { $bname = 'Apple Safari'; $ub = "Safari"; } elseif (preg_match('/Opera/i', $u_agent)) { $bname = 'Opera'; $ub = "Opera"; } elseif (preg_match('/Netscape/i', $u_agent)) { $bname = 'Netscape'; $ub = "Netscape"; } // finally get the correct version number $known = array('Version', $ub, 'other'); $pattern = '#(?<browser>' . join('|', $known) . ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#'; if (!preg_match_all($pattern, $u_agent, $matches)) { // we have no matching number just continue } // see how many we have $i = count($matches['browser']); if ($i != 1) { //we will have two since we are not using 'other' argument yet //see if version is before or after the name if (strripos($u_agent, "Version") < strripos($u_agent, $ub)) { $version = $matches['version'][0]; } else { $version = $matches['version'][1]; } } else { $version = $matches['version'][0]; } // check if we have a number if ($version == null || $version == "") { $version = "?"; } $browserDet = $ub . " " . $version; return $browserDet; } function getCountry($ip_address) { //$geopluginURL='http://www.geoplugin.net/php.gp?ip='.$ip_address; //$addrDetailsArr = unserialize(file_get_contents($geopluginURL)); /* Get City name by return array */ //$city = $addrDetailsArr['geoplugin_city']; /* Get Country name by return array */ //$country = $addrDetailsArr['geoplugin_countryName']; /* Comment out these line to see all the posible details */ /* echo '<pre>'; print_r($addrDetailsArr); die(); */ if (!$city) { $city = 'None'; } if (!$country) { $country = 'None'; } $res['city'] = $city; $res['country'] = $country; return $res; } //Get Only date in DateTime Formate function ConvertDate($date) { $middle = strtotime($date); $new_date = date('d-m-Y', $middle); return $new_date; } //Change DateTime Formate function convert_datetime($date) { if($date){ $middle = strtotime($date); $new_date = date('d-m-Y H:i:s', $middle); } else { $new_date=''; } return $new_date; } //Get Only date in DateTime Formate function OnlyDate($date) { $middle = strtotime($date); $new_date = date('Y-m-d', $middle); return $new_date; } function dec_enc($action, $string) { $output = false; $encrypt_method = "AES-256-CBC"; $secret_key = '3sc3RLrpd17'; $secret_iv = chr(0x0); // hash $key = hash('sha256', $secret_key); // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning $iv = substr(hash('sha256', $secret_iv), 0, 16); if ($action == 'encrypt') { $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv); $output = base64_encode($output); } else if ($action == 'decrypt') { $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv); } return $output; } //save user log function saveUserLog($user, $role, $ipaddress, $user_agent, $url, $city, $country, $remark) { $db = getDB(); $stmt = $db->prepare("INSERT INTO userlog(user,role,ipaddress,user_agent,login_datetime,url,city,country,remark)VALUES(:user,:role,:ipaddress,:user_agent,now(),:url,:city,:country,:remark)"); $stmt->bindParam(":user", $user, PDO::PARAM_STR); $stmt->bindParam(":role", $role, PDO::PARAM_STR); $stmt->bindParam(":ipaddress", $ipaddress, PDO::PARAM_STR); $stmt->bindParam(":user_agent", $user_agent, PDO::PARAM_STR); $stmt->bindValue(':url', !empty($url) ? $url : NULL, PDO::PARAM_STR); $stmt->bindValue(':city', !empty($city) ? $city : NULL, PDO::PARAM_STR); $stmt->bindValue(':country', !empty($country) ? $country : NULL, PDO::PARAM_STR); $stmt->bindParam(":remark", $remark, PDO::PARAM_STR); $stmt->execute(); $count = $stmt->rowCount(); if ($count > 0) { $res = '1'; } return $res; } function check_supadminlogin() { $db = getDB(); if ($_SESSION['user_id'] <= '0' || $_SESSION['user_id'] == NULL) { //insert record for logout history $remark = "Logout"; $user = $_SESSION['usermob']; $role = "Unknown"; $BrowserName = getBrowser(); $OSName = getOS(); $user_agent = $BrowserName . ", " . $OSName; $ipaddress = $_SERVER['REMOTE_ADDR']; $url = curPageName(); $res = getCountry($ipaddress); $city = $res['city']; $country = $res['country']; saveUserLog($user, $role, $ipaddress, $user_agent, $url, $city, $country, $remark); session_unset(); session_destroy(); msg_redirect("" . $_SESSION['action'] . "", "login.php", "0"); exit(); } elseif (intval($_SESSION['user_id']) > 0) { $stmt = $db->prepare("SELECT user_session_id FROM user_login WHERE user_id='" . $_SESSION['user_id'] . "'"); $stmt->execute(); $row = $stmt->fetch(); if ($_SESSION['user_session_id'] != $row['user_session_id']) { session_unset(); session_destroy(); msg_redirect("" . $_SESSION['action'] . "", "login.php", "0"); exit(); } } } function validUser($mytoken, $ipaddress, $user_agent, $domain) { if (hash_equals($mytoken, $_SESSION['mytoken']) === false) { $res = '1'; } else if ($ipaddress != $_SERVER['REMOTE_ADDR']) { $res = '1'; } else if ($user_agent != $_SERVER['HTTP_USER_AGENT']) { $res = '1'; } else if ($domain != $_SERVER['SERVER_NAME']) { $res = '1'; } else { $res = '0'; } return $res; } function curPageName() { return substr($_SERVER["SCRIPT_NAME"], strrpos($_SERVER["SCRIPT_NAME"], "/") + 1); } function msg_redirect($msg, $url, $seconds) { echo "<meta http-equiv=\"Refresh\" content=\"$seconds; URL=$url\">\n"; } //Fetch Record For ORGName function ORGName() { $db = getDB(); $stmt = $db->prepare("SELECT org_name FROM organization"); $stmt->execute(); $row = $stmt->fetch(); return strip_tags($row['org_name']); } //Fetch Record For ORGAlias function ORGAlias() { $db = getDB(); $stmt = $db->prepare("SELECT org_alias FROM organization"); $stmt->execute(); $row = $stmt->fetch(); return strip_tags($row['org_alias']); } //Fetch Record For ORGLogo function ORGLogo() { $db = getDB(); $stmt = $db->prepare("SELECT org_logo FROM organization"); $stmt->execute(); $row = $stmt->fetch(); return strip_tags($row['org_logo']); } //Fetch Record For ORGAddress function ORGAddress() { $db = getDB(); $stmt = $db->prepare("SELECT org_address FROM organization"); $stmt->execute(); $row = $stmt->fetch(); return strip_tags($row['org_address']); } //Fetch Record For Company Email function ORGEmail() { $db = getDB(); $stmt = $db->prepare("SELECT org_email FROM organization"); $stmt->execute(); $row = $stmt->fetch(); return strip_tags($row['org_email']); } //Fetch Record For login_authentication function LoginAuthentication() { $db = getDB(); $stmt = $db->prepare("SELECT login_authentication FROM organization"); $stmt->execute(); $row = $stmt->fetch(); return strip_tags($row['login_authentication']); } //Fetch Record For ORGWebsite function ORGFax($id) { $db = getDB(); $stmt = $db->prepare("SELECT org_fax FROM organization"); $stmt->execute(); $row = $stmt->fetch(); return strip_tags($row['org_fax']); } //Fetch Record For ORGContact function ORGContact() { $db = getDB(); $stmt = $db->prepare("SELECT org_phone FROM organization"); $stmt->execute(); $row = $stmt->fetch(); return strip_tags($row['org_phone']); } //Fetch Record For SaProfilePic function SaProfilePic($id) { $db = getDB(); $stmt = $db->prepare("SELECT profile_pic FROM user_login WHERE user_id='" . $id . "'"); $stmt->execute(); $row = $stmt->fetch(); return strip_tags($row['profile_pic']); } //creat password function createRandomPassword() { $chars = "abcdefghijkmnopqrstuvwxyz023456789"; srand((double) microtime() * 1000000); $i = 0; $pass = ''; while ($i <= 4) { $num = rand() % 33; $tmp = substr($chars, $num, 1); $pass = $pass . $tmp; $i++; } return $pass; } function getSalt($length = 50) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $charactersLength = strlen($characters); $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, $charactersLength - 1)]; } return $randomString; } function getSanitizeString($orig_string) { $new_string = preg_replace('~[\\\\/:*?"<>|+-]~', '', $orig_string); return $new_string; } //Fetch Record For UserName function UserName($id) { $db = getDB(); $stmt = $db->prepare("SELECT AdminName FROM tbladmin WHERE ID='" . $id . "'"); $stmt->execute(); $row = $stmt->fetch(); return strip_tags($row['AdminName']); } function ageCalculator($dob) { if (!empty($dob)) { $birthdate = new DateTime($dob); $today = new DateTime('today'); $age = $birthdate->diff($today)->y; return $age; } else { return 0; } } ?>