Last active
July 4, 2022 07:27
-
-
Save 24Ryou/a6dd75d575d973b32d5d94635a1a7de7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* -------------------------------- functions ------------------------------- */ | |
// get backup of tabels in database PDO | |
function backup_tables($DBH, $tables) { | |
$DBH->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_NATURAL); | |
//Script Variables | |
$compression = false; | |
$BACKUP_PATH = "../backup/"; | |
$nowtimename = time(); | |
//create/open files | |
if ($compression) { | |
$zp = gzopen($BACKUP_PATH . $nowtimename . '.sql.gz', "a9"); | |
} else { | |
$handle = fopen($BACKUP_PATH . $nowtimename . '.sql', 'a+'); | |
} | |
//array of all database field types which just take numbers | |
$numtypes = array('tinyint', 'smallint', 'mediumint', 'int', 'bigint', 'float', 'double', 'decimal', 'real'); | |
//get all of the tables | |
if (empty($tables)) { | |
$pstm1 = $DBH->query('SHOW TABLES'); | |
while ($row = $pstm1->fetch(PDO::FETCH_NUM)) { | |
$tables[] = $row[0]; | |
} | |
} else { | |
$tables = is_array($tables) ? $tables : explode(',', $tables); | |
} | |
//cycle through the table(s) | |
foreach ($tables as $table) { | |
$result = $DBH->query("SELECT * FROM $table"); | |
$num_fields = $result->columnCount(); | |
$num_rows = $result->rowCount(); | |
$return = ""; | |
//uncomment below if you want 'DROP TABLE IF EXISTS' displayed | |
//$return.= 'DROP TABLE IF EXISTS `'.$table.'`;'; | |
//table structure | |
$pstm2 = $DBH->query("SHOW CREATE TABLE $table"); | |
$row2 = $pstm2->fetch(PDO::FETCH_NUM); | |
$ifnotexists = str_replace('CREATE TABLE', 'CREATE TABLE IF NOT EXISTS', $row2[1]); | |
$return .= "\n\n" . $ifnotexists . ";\n\n"; | |
if ($compression) { | |
gzwrite($zp, $return); | |
} else { | |
fwrite($handle, $return); | |
} | |
$return = ""; | |
//insert values | |
if ($num_rows) { | |
$return = 'INSERT INTO `' . $table . '` ('; | |
$pstm3 = $DBH->query("SHOW COLUMNS FROM $table"); | |
$count = 0; | |
$type = array(); | |
while ($rows = $pstm3->fetch(PDO::FETCH_NUM)) { | |
if (stripos($rows[1], '(')) { | |
$type[$table][] = stristr($rows[1], '(', true); | |
} else { | |
$type[$table][] = $rows[1]; | |
} | |
$return .= "`" . $rows[0] . "`"; | |
$count++; | |
if ($count < ($pstm3->rowCount())) { | |
$return .= ", "; | |
} | |
} | |
$return .= ")" . ' VALUES'; | |
if ($compression) { | |
gzwrite($zp, $return); | |
} else { | |
fwrite($handle, $return); | |
} | |
$return = ""; | |
} | |
$count = 0; | |
while ($row = $result->fetch(PDO::FETCH_NUM)) { | |
$return = "\n\t("; | |
for ($j = 0; $j < $num_fields; $j++) { | |
//$row[$j] = preg_replace("\n","\\n",$row[$j]); | |
if (isset($row[$j])) { | |
//if number, take away "". else leave as string | |
if ((in_array($type[$table][$j], $numtypes)) && (!empty($row[$j]))) { | |
$return .= $row[$j]; | |
} else { | |
$return .= $DBH->quote($row[$j]); | |
} | |
} else { | |
$return .= 'NULL'; | |
} | |
if ($j < ($num_fields - 1)) { | |
$return .= ','; | |
} | |
} | |
$count++; | |
if ($count < ($result->rowCount())) { | |
$return .= "),"; | |
} else { | |
$return .= ");"; | |
} | |
if ($compression) { | |
gzwrite($zp, $return); | |
} else { | |
fwrite($handle, $return); | |
} | |
$return = ""; | |
} | |
$return = "\n\n-- ------------------------------------------------ \n\n"; | |
if ($compression) { | |
gzwrite($zp, $return); | |
} else { | |
fwrite($handle, $return); | |
} | |
$return = ""; | |
} | |
$error1 = $pstm2->errorInfo(); | |
$error2 = $pstm3->errorInfo(); | |
$error3 = $result->errorInfo(); | |
echo $error1[2]; | |
echo $error2[2]; | |
echo $error3[2]; | |
if ($compression) { | |
gzclose($zp); | |
} else { | |
fclose($handle); | |
} | |
} | |
function get_string_between($string, $start, $end) | |
{ | |
$string = ' ' . $string; | |
$ini = strpos($string, $start); | |
if ($ini == 0) return ''; | |
$ini += strlen($start); | |
$len = strpos($string, $end, $ini) - $ini; | |
return substr($string, $ini, $len); | |
} | |
function createFolder($path) | |
{ | |
if(!is_dir($path)) | |
{ | |
mkdir($path, 0777); | |
} | |
} | |
function uploadfile($filearray,$targetPath,$filename = "") | |
{ | |
$result = array('error'=>false,'name'=>'','extension'=>'','path'=>''); | |
// get details of the uploaded file | |
$fileTmpPath = $filearray['tmp_name']; | |
$fileName = $filearray['name']; | |
// $fileSize = $filearray['size']; | |
// $fileType = $filearray['type']; | |
$fileNameCmps = explode(".", $fileName); | |
$fileExtension = strtolower(end($fileNameCmps)); | |
$result['extension'] = $fileExtension; | |
// sanitize file-name | |
if ($filename === "") { | |
$result['name'] = $fileName; | |
$newFileName = md5(time() . $fileName) . '.' . $fileExtension; | |
} | |
else { | |
$result['name'] = $filename; | |
$newFileName = $filename.'.'.$fileExtension; | |
} | |
// check if file has one of the following extensions | |
$allowedfileExtensions = array('jpg', 'gif', 'png', 'zip', 'txt', 'xls', 'doc', 'pdf', 'mkv', 'mp4', 'mp3', 'rar'); | |
if (in_array($fileExtension, $allowedfileExtensions)) { | |
// directory in which the uploaded file will be moved | |
$uploadFileDir = $targetPath; | |
$dest_path = $uploadFileDir . $newFileName; | |
$result['path'] = $dest_path; | |
if (move_uploaded_file($fileTmpPath, $dest_path)) { | |
$result['error'] = true; | |
} | |
} | |
return $result; | |
} | |
/** | |
* get hour minute sec of a datetime format | |
*/ | |
function getHMS($date) | |
{ | |
return date("H:i:s", strtotime($date)); | |
} | |
/** | |
* get year month day of a datetime/date format | |
*/ | |
function getYMD($date) | |
{ | |
$jy = date("Y", strtotime($date)); | |
$jm = date("m", strtotime($date)); | |
$jd = date("d", strtotime($date)); | |
$datetime = jalali_to_gregorian($jy, $jm, $jd, "-"); | |
return date("Y-m-d", strtotime($datetime)); | |
} | |
/** | |
* calculate time elaps between $datetime(given-time) and $now(current-time) | |
*/ | |
function time_elapsed_string($datetime, $full = false) | |
{ | |
$now = new DateTime("now", new DateTimeZone("Asia/Tehran")); | |
$ago = new DateTime($datetime, new DateTimeZone("Asia/Tehran")); | |
$diff = $now->diff($ago); | |
$diff->w = floor($diff->d / 7); | |
$diff->d -= $diff->w * 7; | |
$string = array( | |
// 'y' => 'year', | |
// 'm' => 'month', | |
// 'w' => 'week', | |
// 'd' => 'day', | |
// 'h' => 'hour', | |
// 'i' => 'minute', | |
// 's' => 'second', | |
'y' => 'سال', | |
'm' => 'ماه', | |
'w' => 'هفته', | |
'd' => 'روز', | |
'h' => 'ساعت', | |
'i' => 'دقیقه', | |
's' => 'ثانیه', | |
); | |
foreach ($string as $k => &$v) { | |
if ($diff->$k) { | |
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? '' : ''); | |
} else { | |
unset($string[$k]); | |
} | |
} | |
if (!$full) $string = array_slice($string, 0, 1); | |
return $string ? implode(' و ', $string) . ' قبل' : 'همین حالا'; | |
} | |
/* ---------------------------- built-in methods ---------------------------- */ | |
$breakLine = '<br>'; | |
echo base64_decode('YWVzLTI1Ni1nY206ZUBmci1zcy').$breakLine; | |
echo base64_encode('hello_world!!!:this-is-test-12345#'); | |
// check one line if statement 'active' when if is true and 'disable' for false | |
$result = ($data->status == 1) ? 'active' : 'disable'; | |
// for loop | |
$fruits = ["apple", "banana", "orange"]; | |
for($i=0;$i<count($fruits);$i++){ | |
echo "Index of ".$i."= ".$fruits[$i]."<br>"; | |
} | |
// switch case | |
$age=20; | |
switch ($age) { | |
case 19: | |
echo "Age is 18"; | |
break; | |
case 20: | |
echo "Age is 20"; | |
break; | |
case 21: | |
echo "Age is 21"; | |
break; | |
} | |
/* -------------------------------- text-area ------------------------------- */ | |
// echo encode_string | base64 --decode | |
$my_url = 'ss://YWVzLTI1Ni1nY206ZjYxZXc5OEFkZUBmci1zcy5yYWNldnBuLmNvbToyNDQz#racevpn_frss'; | |
$parsed = get_string_between($my_url,'//','#'); | |
echo $parsed.'<br>'; | |
echo base64_decode($parsed).'<br>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment