Sindbad~EG File Manager
<?php
class Helper
{
//http://www.ericdiviney.com/blog/how-to-register-helper-classes-in-laravel-4
//http://stackoverflow.com/questions/14275154/convert-gmt-time-to-ist
static function toIndianDate($string) // truncates the string
{
if($string != null)
{
$datetime = new DateTime($string);
$datetime->setTimezone(new DateTimeZone('Asia/Calcutta'));
return $datetime->format('Y-m-d');
}
else
return $string;
}
static function toIndianDateTime($string) // truncates the string
{
if($string != null)
{
$datetime = new DateTime($string);
$datetime->setTimezone(new DateTimeZone('Asia/Calcutta'));
return $datetime->format('Y-m-d H:i:s');
}
else
return $string;
}
static function toLocalDate($string,$timezone) // truncates the string
{
if($string != null)
{
$datetime = new DateTime($string);
$datetime->setTimezone(new DateTimeZone($timezone));
return $datetime->format('Y-m-d');
}
else
return $string;
}
static function toLocalDateTime($string,$timezone) // truncates the string
{
if($string != null)
{
$datetime = new DateTime($string);
$datetime->setTimezone(new DateTimeZone( trim($timezone) ));
return $datetime->format('Y-m-d H:i:s');
}
else
return $string;
}
static function currentIndianDate()
{
$datetime = new DateTime();
$datetime->setTimezone(new DateTimeZone('Asia/Calcutta'));
return $datetime->format('Y-m-d');
}
static function currentIndianTime()
{
$datetime = new DateTime();
$datetime->setTimezone(new DateTimeZone('Asia/Calcutta'));
return $datetime->format('Y-m-d H:i:s');
}
static function currentServerDateTime()
{
return date('Y-m-d H:i:s');
}
//Author : karthik k
//Date : 2014-05-23
//Description : To convert searilized datetime object to PHP Datetime object
//http://stackoverflow.com/questions/16749778/php-date-format-date1365004652303-0500
static function SearilizedDateToPHPDate($date)
{
try
{
preg_match('/[0-9]+/', $date, $matches);
return date('Y-m-d H:i:s', $matches[0]/1000);
}
catch (Exception $e)
{
return null;
}
}
//Author : karthik k
//Date : 2014-05-28
//Description : To check given string is not null and also not empty
//http://stackoverflow.com/questions/381265/better-way-to-check-variable-for-null-or-empty-string
static function isNullOrEmpty($str)
{
return (!isset($str) || trim($str)==='');
}
//Author : karthik
//To format DB Datetime
static function fromDBToDDMMYYYY($string)
{
if($string != null && trim($string)!='')
{
$datetime = new DateTime($string);
//return $datetime->format('j-F-Y');
return $datetime->format('d/m/Y');
}
else
{
return $string;
}
}
//Author : karthik
//To format DB Datetime
static function fromDBToDDMMYYYYHMS($string)
{
if($string != null && trim($string)!='')
{
$datetime = new DateTime($string);
//return $datetime->format('j-F-Y');
//return $datetime->format('d/m/Y H:i:s');
return $datetime->format('d/m/Y h:i:s a');
}
else
{
return $string;
}
}
//Author : karthik
//Example : 20-may-2015
static function fromDBToddMMyy($string)
{
if($string != null && trim($string)!='')
{
$datetime = new DateTime($string);
return $datetime->format('d-M-y');
}
else
{
return $string;
}
}
//Author : revathi
//Example : may-20-2015
static function fromDBToMMddyy($string)
{
if($string != null && trim($string)!='')
{
$datetime = new DateTime($string);
return $datetime->format('M-d-Y');
}
else
{
return $string;
}
}
//Author : karthik
//To convert datetime object to datetime string format
static function fromDBDate($string)
{
$result = null;
if($string != null && trim($string)!='')
{
$datetime = new DateTime($string);
$result = $datetime->format('d/m/Y');
}
return $result;
}
//Author : karthik
//To convert datetime string to datetime object
//http://www.paulund.co.uk/datetime-php
static function toDBDate($string)
{
$result = null;
try
{
if($string != null && trim($string)!='')
{
$array = explode('/',$string);
if(is_array($array) && count($array)>0)
{
$dt = new DateTime($array[2].'-'.$array[1].'-'.$array[0]);
$result = $dt->format('Y-m-d H:i:s');
}
}
}catch (Exception $e)
{
$result = null;
}
return $result;
}
//Author : karthik
//Date : 2014-06-16
//To check the given string is valid datetime
//http://stackoverflow.com/questions/5536029/php-datetime-accepting-invalid-date
static function isValidDate($date, $format = 'd/m/Y')
{
if($date != null && trim($date)!='')
{
$dt = DateTime::createFromFormat($format, $date);
return $dt && $dt->format($format) == $date;
}
else
{
return false;
}
}
//Author : karthik k
//Date : 2014 - 07 - 01
//To truncate given string
static function trimAndTruncate($text, $length = null)
{
$text = trim($text);
$text = $length > 0 ? substr($text, 0, $length) : $text ;
return strlen($text) > 0 ? $text : null ;
}
//cryptography
//Encryption
// to append string with trailing characters as for PKCS7 padding scheme
//http://stackoverflow.com/questions/4329260/cross-platform-php-to-c-sharp-
static function encrypt($text)
{
/*$key = Config::get('app.crypt_key');
$iv = Config::get('app.crypt_iv');
$block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
$padding = $block - (strlen($text) % $block);
$text .= str_repeat(chr($padding), $padding);
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $iv);
return base64_encode($crypttext);*/
return Crypt::encrypt($text);
}
//Decryption
//http://stackoverflow.com/questions/3431950/rijndael-256-encrypt-decrypt-between-c-sharp-and-php
//http://stackoverflow.com/questions/18908613/mcrypt-and-base64-with-php-and-c-sharp
//http://stackoverflow.com/questions/21414479/converting-laravels-aes-256-encryptor-to-c-sharp
static function decrypt($text)
{
return Crypt::decrypt($text);
/* $key = Config::get('app.crypt_key');
$iv = Config::get('app.crypt_iv');
$iv_utf = mb_convert_encoding($iv, 'UTF-8');
return mcrypt_decrypt("rijndael-256", $key, base64_decode($text), "cbc", $iv_utf);*/
}
//Hashing
//http://stackoverflow.com/questions/12804231/c-sharp-equivalent-to-hash-hmac-in-php
static function hash($text, $key)
{
return strtoupper(hash_hmac("sha256", $text, $key));
}
//Author : Sridhar
static function toDate($string)
{
if($string != null)
{
$datetime = new DateTime($string);
return $datetime->format('Y-m-d');
}
else
return $string;
}
//To convert dd/mm/yyyy hh:mm:ss format to yyyy-mm-dd hh:mm:ss
//input : dd/mm/yyyy hh:mm:ss 26/09/2014 12:50:50
//output : yyyy-mm-dd hh:mm:ss 2014-09-26 12:50:50
static function DMYTtoYMDT($string)
{
if($string != null)
{
return date_format(date_create_from_format("d/m/Y H:i:s", $string),"Y-m-d H:i:s");
}
else
return $string;
}
//To convert mm/dd/yyyy hh:mm:ss format to yyyy-mm-dd hh:mm:ss
//input : mm/dd/yyyy hh:mm:ss 09/26/2014 12:50:50
//output : yyyy-mm-dd hh:mm:ss 2014-09-26 12:50:50
static function MDYTtoYMDT($string)
{
if($string != null)
{
return date_format(date_create_from_format("m/d/Y H:i:s", $string),"Y-m-d H:i:s");
}
else
return $string;
}
// To calculate age by comparing given time with system current datetime
//input : dd/mm/yyyy 22/09/1989
//output : 25
static function calcutateAge($dob)
{
$dob = date("Y-m-d",strtotime($dob));
$dobObject = new DateTime($dob);
$nowObject = new DateTime();
$diff = $dobObject->diff($nowObject);
return $diff->y;
}
//To change db connection for user
static function ChangeDB($userId)
{
$user = Cache::get('user_' . $userId);
if($user != null)
{
Config::set('database.connections.mysql_user.host',$user->pt_db_hostname);
Config::set('database.connections.mysql_user.username',$user->pt_db_username);
Config::set('database.connections.mysql_user.password',$user->pt_db_password);
Config::set('database.connections.mysql_user.database',$user->pt_db_name);
DB::setDefaultConnection('mysql_user');
}
}
// search array with key value
//http://stackoverflow.com/questions/1019076/how-to-search-by-key-value-in-a-multidimensional-array-in-php
//To find out particular item(array) inside array by passing key and its value
static function search($array, $key, $value)
{
$results = array();
if (is_array($array)) {
if (isset($array[$key]) && $array[$key] == $value) {
$results[] = $array;
}
foreach ($array as $subarray) {
$results = array_merge($results, Helper::search($subarray, $key, $value));
}
}
return $results;
}
static function currentIndianDateDMY()
{
$datetime = new DateTime();
$datetime->setTimezone(new DateTimeZone('Asia/Calcutta'));
return $datetime->format('d/m/Y');
}
//http://stackoverflow.com/questions/6808013/get-the-youtube-video-thumbnail-from-youtube-video-url-using-php
static function getYoutubeThumbnail($e)
{
//GET THE URL
$url = $e;
$queryString = parse_url($url, PHP_URL_QUERY);
parse_str($queryString, $params);
$v ="";
if(isset($params['v']))
$v = $params['v'];
//DISPLAY THE IMAGE
if(strlen($v)>0)
{
return "http://img.youtube.com/vi/$v/default.jpg";
}
return "";
}
static function getTimeZone()
{
$timezones = DateTimeZone::listAbbreviations();
$cities = array();
foreach( $timezones as $key => $zones )
{
foreach( $zones as $id => $zone )
{
/**
* Only get timezones explicitely not part of "Others".
* @see http://www.php.net/manual/en/timezones.others.php
*/
if ( preg_match( '/^(America|Antartica|Arctic|Asia|Atlantic|Europe|Indian|Pacific)\//', $zone['timezone_id'] )
&& $zone['timezone_id']) {
$cities[$zone['timezone_id']][] = $key;
}
}
}
// For each city, have a comma separated list of all possible timezones for that city.
foreach( $cities as $key => $value )
$cities[$key] = join( ', ', $value);
// Only keep one city (the first and also most important) for each set of possibilities.
$cities = array_unique( $cities );
// Sort by area/city name.
$result = [];
foreach (array_keys($cities) as $key => $value) {
$result[$value] = $value;
}
return $result;
}
static function convert_number_to_words($no)
{
//creating array of word for each digit
$words = array('0'=> 'Zero' ,'1'=> 'one' ,'2'=> 'two' ,'3' => 'three','4' => 'four','5' => 'five','6' => 'six','7' => 'seven','8' => 'eight','9' => 'nine','10' => 'ten','11' => 'eleven','12' => 'twelve','13' => 'thirteen','14' => 'fourteen','15' => 'fifteen','16' => 'sixteen','17' => 'seventeen','18' => 'eighteen','19' => 'nineteen','20' => 'twenty','30' => 'thirty','40' => 'forty','50' => 'fifty','60' => 'sixty','70' => 'seventy','80' => 'eighty','90' => 'ninety','100' => 'hundred','1000' => 'thousand','100000' => 'lakh','10000000' => 'crore');
//$words = array('0'=> '0' ,'1'=> '1' ,'2'=> '2' ,'3' => '3','4' => '4','5' => '5','6' => '6','7' => '7','8' => '8','9' => '9','10' => '10','11' => '11','12' => '12','13' => '13','14' => '14','15' => '15','16' => '16','17' => '17','18' => '18','19' => '19','20' => '20','30' => '30','40' => '40','50' => '50','60' => '60','70' => '70','80' => '80','90' => '90','100' => '100','1000' => '1000','100000' => '100000','10000000' => '10000000');
//for decimal number taking decimal part
$cash=(int)$no; //take number wihout decimal
$decpart = $no - $cash; //get decimal part of number
$decpart=sprintf("%01.2f",$decpart); //take only two digit after decimal
$decpart1=substr($decpart,2,1); //take first digit after decimal
$decpart2=substr($decpart,3,1); //take second digit after decimal
$decimalstr='';
//if given no. is decimal than preparing string for decimal digit's word
if($decpart>0)
{
$decimalstr.="point ".$words[$decpart1]." ".$words[$decpart2];
}
if($no == 0)
return ' ';
else {
$novalue='';
$highno=$no;
$remainno=0;
$value=100;
$value1=1000;
while($no>=100) {
if(($value <= $no) &&($no < $value1)) {
$novalue=$words["$value"];
$highno = (int)($no/$value);
$remainno = $no % $value;
break;
}
$value= $value1;
$value1 = $value * 100;
}
if(array_key_exists("$highno",$words)) //check if $high value is in $words array
return $words["$highno"]." ".$novalue." ".Helper::convert_number_to_words($remainno).$decimalstr; //recursion
else {
$unit=$highno%10;
$ten =(int)($highno/10)*10;
return $words["$ten"]." ".$words["$unit"]." ".$novalue." ".Helper::convert_number_to_words($remainno
).$decimalstr; //recursion
}
}
}
//Author : Karthik Karuna
//Purpose : To generate random password contains 4 characters
//http://stackoverflow.com/questions/6101956/generating-a-random-password-in-php
static function randomPassword()
{
$alphabet = '1234567890';
$pass = array(); //remember to declare $pass as an array
$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
for ($i = 0; $i < 4; $i++) {
$n = rand(0, $alphaLength);
$pass[] = $alphabet[$n];
}
return implode($pass); //turn the array into a string
}
static function sms($mobiles, $msg)
{
if(isset($mobiles) && count($mobiles) > 0)
{
$sender = 'FBSAIO';
$Authkey = '93919A5FWuR1MX9n560b8975';
$mobiles = array_map(function($mnumber){ return "91".$mnumber; },$mobiles);
$route = 4;
$country = 0;
$msg = urlencode($msg);
$postData = array(
'authkey' => $Authkey,
'mobiles' => implode(',',$mobiles),
'message' => $msg,
'sender' => $sender,
'route' => $route
);
$url="http://api.msg91.com/api/sendhttp.php";
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData
//,CURLOPT_FOLLOWLOCATION => true
));
//Ignore SSL certificate verification
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//get response
$output = curl_exec($ch);
curl_close($ch);
}
}
//http://www.karlrixon.co.uk/writing/convert-numbers-to-words-with-php/
static function tmpconvert_number_to_words($number)
{
$hyphen = '-';
$conjunction = ' and ';
$separator = ', ';
$negative = 'negative ';
$decimal = ' point ';
$dictionary = array(
0 => 'zero',
1 => 'one',
2 => 'two',
3 => 'three',
4 => 'four',
5 => 'five',
6 => 'six',
7 => 'seven',
8 => 'eight',
9 => 'nine',
10 => 'ten',
11 => 'eleven',
12 => 'twelve',
13 => 'thirteen',
14 => 'fourteen',
15 => 'fifteen',
16 => 'sixteen',
17 => 'seventeen',
18 => 'eighteen',
19 => 'nineteen',
20 => 'twenty',
30 => 'thirty',
40 => 'fourty',
50 => 'fifty',
60 => 'sixty',
70 => 'seventy',
80 => 'eighty',
90 => 'ninety',
100 => 'hundred',
1000 => 'thousand',
1000000 => 'lakh(s)',
10000000 => 'crore',
1000000000000 => 'trillion',
1000000000000000 => 'quadrillion',
1000000000000000000 => 'quintillion'
);
if (!is_numeric($number)) {
return false;
}
if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
// overflow
trigger_error(
'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,
E_USER_WARNING
);
return false;
}
if ($number < 0) {
return $negative . Helper::convert_number_to_words(abs($number));
}
$string = $fraction = null;
if (strpos($number, '.') !== false) {
list($number, $fraction) = explode('.', $number);
}
switch (true) {
case $number < 21:
$string = $dictionary[$number];
break;
case $number < 100:
$tens = ((int) ($number / 10)) * 10;
$units = $number % 10;
$string = $dictionary[$tens];
if ($units) {
$string .= $hyphen . $dictionary[$units];
}
break;
case $number < 1000:
$hundreds = $number / 100;
$remainder = $number % 100;
$string = $dictionary[$hundreds] . ' ' . $dictionary[100];
if ($remainder) {
$string .= $conjunction . Helper::convert_number_to_words($remainder);
}
break;
default:
$baseUnit = pow(1000, floor(log($number, 1000)));
$numBaseUnits = (int) ($number / $baseUnit);
$remainder = $number % $baseUnit;
$string = Helper::convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
if ($remainder) {
$string .= $remainder < 100 ? $conjunction : $separator;
$string .= Helper::convert_number_to_words($remainder);
}
break;
}
if (null !== $fraction && is_numeric($fraction)) {
$string .= $decimal;
$words = array();
foreach (str_split((string) $fraction) as $number) {
$words[] = $dictionary[$number];
}
$string .= implode(' ', $words);
}
return $string;
}
//Function to check given mobile number is valid 10 digit mobile number
//Author : karthik
//Date : 2015-11-03
static function isValidMobileNo($mobileNo)
{
$status = false;
$inputs = array(
'mobile' => $mobileNo,
'mobile_no' => $mobileNo
);
$rules = array(
'mobile' => 'required|min:10|max:10',
'mobile_no' => 'required|numeric',
);
$messages = array();
$validator = Validator::make($inputs,$rules,$messages);
if(isset($validator) && !$validator->fails())
$status = true ;
return $status ;
}
//Function to send bulk sms
//Author : karthik
//Date : 2015-11-03
//param -
//$arrObjects
// - mobileno
// - msg
//
//$campaign - just to make group. For what purpose sms can go
static function smsBulk($arrObjects, $campaign = null)
{
$operationStatus = false;
$sender = 'FBSAIO';
$Authkey = '93919A5FWuR1MX9n560b8975';
$url = 'http://api.msg91.com/api/postsms.php';
$country = '91' ;
if(is_null($campaign))
$campaign = 'BULK SMS';
//Formatting input to post to msg91 site
$dataPrefix = '';
$dataSuffix = '';
$xml = [] ;
if(isset($arrObjects) && count($arrObjects) > 0)
{
$dataPrefix = '<MESSAGE>
<AUTHKEY>'.$Authkey.'</AUTHKEY>
<ROUTE>template</ROUTE>
<CAMPAIGN>'.$campaign.'</CAMPAIGN>
<COUNTRY>'.$country.'</COUNTRY>
<SENDER>'.$sender.'</SENDER>';
array_push($xml, $dataPrefix);
foreach ($arrObjects as $key => $obj)
{
array_push($xml,'<SMS TEXT="'.$obj->msg.'">
<ADDRESS TO="'.$obj->mobileno.'"></ADDRESS>
</SMS>'
);
}
$dataSuffix = '</MESSAGE>';
array_push($xml, $dataSuffix);
}
$data = implode('', $xml);
try
{
if(isset($data) && strlen($data) > 0)
{
$param = 'data=' .urlencode($data);
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $param);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
Log::info($response);
curl_close($ch);
if(strlen($response) == 24)// return 24 characters of alphanumeric string in case of success return
$operationStatus = true ;
}
}
catch (Exception $e)
{
Log::error("Helper=>smsBulk".$e->getMessage());
$operationStatus = false;
}
return $operationStatus;
}
//Function to check given input is valid email or not
//Author : karthik
//Date : 2015-11-03
static function isValidEmail($emailString)
{
$status = false;
$emailString = trim($emailString);
$inputs = array('email' => $emailString);
$rules = array('email' => 'required|email');
$messages = array();
$validator = Validator::make($inputs,$rules,$messages);
if(isset($validator) && !$validator->fails())
$status = true ;
return $status ;
}
static function currentDateDMYHMS()
{
return (new DateTime())->format('d/m/Y H:i:s');
}
/*
sridhar on 10 Feb 2018 2:31 PM
https://stackoverflow.com/questions/44078090/how-do-i-create-merge-tags-in-a-php-mysql-app
*/
static function merge_tag($str_val,$tag_values)
{
$search = array();
$replace = array();
foreach ($tag_values AS $index => $value)
{
$search[] = "*|" . $index . "|*"; // Wrapping the text in "*|...|*}"
$replace[] = $value;
}
// Do the replacement
return str_replace($search, $replace, $str_val);
}
}
?>
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists