376 lines
8.2 KiB
PHP
376 lines
8.2 KiB
PHP
<?php
|
||
|
||
namespace Core;
|
||
class Utils {
|
||
|
||
/*
|
||
@ Ìåòîä ðåäèðåêòà íà äðóãóþ ñòðàíèöó
|
||
*/
|
||
public function redirect( $url ) {
|
||
header( 'location: ' . $url );
|
||
}
|
||
|
||
/*
|
||
@ Ìåòîä ðàçäåëåíèÿ ïàðàìåòðîâ
|
||
@
|
||
*/
|
||
public static function lookStats($m) {
|
||
$ist = array();
|
||
$di = explode('|',$m);
|
||
$i = 0; $de = false;
|
||
while($i<count($di))
|
||
{
|
||
$de = explode('=',$di[$i]);
|
||
if(isset($de[0],$de[1]))
|
||
{
|
||
if(!isset($ist[$de[0]])) {
|
||
$ist[$de[0]] = 0;
|
||
}
|
||
$ist[$de[0]] = $de[1];
|
||
}
|
||
$i++;
|
||
}
|
||
return $ist;
|
||
}
|
||
|
||
/*
|
||
@ Ìåòîä âûâîäà öåëûõ ÷èñåë
|
||
@ TYPE: 0 - ëþáîå ÷èñëî
|
||
*/
|
||
public function num( $val, $type ) {
|
||
if( $type == 0 ) {
|
||
$val = floor((int)$val);
|
||
}
|
||
return $val;
|
||
}
|
||
|
||
/*
|
||
@ Ìåòîä îïðåäåëåíèÿ ïóñòîå çíà÷åíèå èëè íåò
|
||
@ TYPE: 0 - ÷èñëî, 1 - òåêñò
|
||
*/
|
||
public function emptyVal( $val, $type ) {
|
||
$r = true;
|
||
if( !isset($val) ) {
|
||
$r = false;
|
||
}elseif( $type == 0 ) {
|
||
if( $val == 0 ) {
|
||
$r = false;
|
||
}
|
||
}elseif( $type == 1 ) {
|
||
$val = str_replace( ' ', '', str_replace( ' ', '', $val ) );
|
||
if( $val == '' ) {
|
||
$r = false;
|
||
}
|
||
}
|
||
return $r;
|
||
}
|
||
|
||
/*
|
||
@ Ìåòîä ôèëüòðàöèè ñòðîêè (äëÿ POST èëè GET)
|
||
*/
|
||
public function fs( $val ) {
|
||
$val = htmlspecialchars( $val ) ;
|
||
return $val;
|
||
}
|
||
|
||
/*
|
||
@ Ìåòîä ñîçäàíèÿ êóêè è óäàëåíèÿ
|
||
*/
|
||
public function cookie( $name , $value = NULL , $time = NULL ) {
|
||
if( $value == NULL ) {
|
||
return $_COOKIE[$name];
|
||
}elseif( $value != false ) {
|
||
if( $time == NULL ) {
|
||
$time = 86400;
|
||
}
|
||
return setcookie( $name , $value , OK + $time , '/' );
|
||
}else{
|
||
return setcookie( $name , '' , OK - 86400 , '/' );
|
||
}
|
||
}
|
||
|
||
/*
|
||
@ Ìåòîä íà÷àëà ñåññèè
|
||
*/
|
||
public function ses_start() {
|
||
if ( session_id() ) return true;
|
||
else return session_start();
|
||
}
|
||
|
||
/*
|
||
@ Ìåòîäïðîâåðêè òåêñòà
|
||
*/
|
||
public function testVal( $val , $min , $max , $sym , $nosym , $nostart , $noend , $data ) {
|
||
$r = true;
|
||
if( mb_strlen($val,'UTF-8') < $min || mb_strlen($val,'UTF-8') > $max ) {
|
||
$r = false;
|
||
}else{
|
||
//Äîïóñòèìûå ñèìâîëû
|
||
if( $sym != false ) {
|
||
$i = 0;
|
||
$new_val = mb_strtolower($val,'UTF-8');
|
||
while( $i < mb_strlen($val,'UTF-8') ) {
|
||
$j = 0;
|
||
$k = 0;
|
||
$k2 = 0;
|
||
while( $j < mb_strlen($sym,'UTF-8') ) {
|
||
if( mb_strtolower($val[$i],'UTF-8') == mb_strtolower($sym[$j],'UTF-8') ) {
|
||
$k++;
|
||
}else{
|
||
if( isset($data['noXsym']) ) {
|
||
//Íåëüçÿ èñïîëüçîâàòü áîëåå X ñèìâîëîâ ïîäðÿä
|
||
$l = 0;
|
||
$notxt = '';
|
||
while( $l < $data['noXsym'] ) {
|
||
$notxt .= mb_strtolower($sym[$j],'UTF-8');
|
||
$l++;
|
||
}
|
||
if( mb_strpos($new_val,$notxt,NULL,'UTF-8') !== false ) {
|
||
$k2++;
|
||
}
|
||
}
|
||
}
|
||
$j++;
|
||
}
|
||
if( $k == 0 || $k2 > 0 ) {
|
||
$i = mb_strlen($val,'UTF-8');
|
||
$r = false;
|
||
}
|
||
$i++;
|
||
}
|
||
}
|
||
//Íå äîïóñòèìûå ñèìâîëû
|
||
if( $nosym != false ) {
|
||
$i = 0;
|
||
$new_val = '';
|
||
while( $i < count($nosym) ) {
|
||
if( mb_strpos(mb_strtolower($val,'UTF-8'),mb_strtolower($nosym[$i],'UTF-8'),NULL,'UTF-8') !== false ) {
|
||
$i = count($nosym);
|
||
$r = false;
|
||
}
|
||
$i++;
|
||
}
|
||
}
|
||
//íå äîïóñòèìîå íà÷àëî
|
||
if( $nostart != false ) {
|
||
$i = 0;
|
||
$new_val = '';
|
||
while( $i < count($nostart) ) {
|
||
if( mb_substr( $val, 0, mb_strlen($nostart[$i],'UTF-8'),'UTF-8') == $nostart[$i] ) {
|
||
$i = count($nostart);
|
||
$r = false;
|
||
}
|
||
$i++;
|
||
}
|
||
}
|
||
//íå äîïóñòèìûé êîíåö
|
||
if( $noend != false ) {
|
||
$i = 0;
|
||
$new_val = '';
|
||
while( $i < count($noend) ) {
|
||
if( mb_substr( $val, ( mb_strlen($val,'UTF-8') - mb_strlen($noend[$i],'UTF-8') ) , 0 , 'UTF-8') == $noend[$i] ) {
|
||
$i = count($noend);
|
||
$r = false;
|
||
}
|
||
$i++;
|
||
}
|
||
}
|
||
}
|
||
return $r;
|
||
}
|
||
|
||
/*
|
||
@ Ìåòîä "çàâåðøåíèÿ" ñåññèè
|
||
*/
|
||
public function ses_end() {
|
||
if ( session_id() ) {
|
||
// Åñëè åñòü àêòèâíàÿ ñåññèÿ, óäàëÿåì êóêè ñåññèè,
|
||
setcookie(session_name(), session_id(), time()-60*60*24);
|
||
// è óíè÷òîæàåì ñåññèþ
|
||
session_unset();
|
||
session_destroy();
|
||
}
|
||
}
|
||
|
||
/*
|
||
@ Ìåòîä îïðåäåëåíèÿ òèïà ñòðîêà, öèôðû, ýë.ïî÷òà
|
||
*/
|
||
public function takeType( $val ) {
|
||
|
||
if( preg_match( "|^[-0-9a-z_\.]+@[-0-9a-z_^\.]+\.[a-z]{2,6}$|i", $val ) ) {
|
||
//Ýë.ïî÷òà
|
||
return 2;
|
||
}else{
|
||
preg_match_all( '([0-9])', $val, $matches );
|
||
$res = implode( NULL, $matches[0] );
|
||
if( mb_strlen( $val, 'UTF-8' ) > mb_strlen( $res, 'UTF-8' ) ) {
|
||
//Ñòðîêà
|
||
return 1;
|
||
}else{
|
||
//×èñëî
|
||
return 3;
|
||
}
|
||
}
|
||
}
|
||
|
||
/*
|
||
@ Ìåòîä ïðîâåðêè ïðàâèëüíîñòè ïàðîëåé
|
||
*/
|
||
public function testPass( $psw1, $psw2 ) {
|
||
if( $psw1 == $psw2 ) {
|
||
return true;
|
||
}else{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/*
|
||
@ Ìåòîä ãåíèðàöèè êëþ÷à àâòîðèçàöèè
|
||
*/
|
||
public function createAuth( $par ) {
|
||
if( !isset( $par['rand'] ) ) {
|
||
$par['rand'] = rand(10000000,90000000);
|
||
}
|
||
$r = $par['rand'] . md5( $par['ip'] . '+' . $par['id'] . '+' . $par['pass'] . '+' . $par['rand'] );
|
||
return $r;
|
||
}
|
||
|
||
/*
|
||
@ Ìåòîä ïðîâåðêè êëþ÷à àâòîðèçàöèè
|
||
*/
|
||
public function testAuth( $auth, $par ) {
|
||
$par['rand'] = substr( $auth, 0, 8 );
|
||
if( $auth == self::createAuth( $par ) ) {
|
||
return true;
|
||
}else{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/*
|
||
@ Ìåòîä âûâîäà JSON äàííûõ
|
||
*/
|
||
public function JSON_Headers() {
|
||
header('Expires: Mon, 26 Jul 1970 05:00:00 GMT');
|
||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
||
header('Cache-Control: no-store, no-cache, must-revalidate');
|
||
header('Cache-Control: post-check=0, pre-check=0', false);
|
||
header('Pragma: no-cache');
|
||
header('Content-Type: application/json; charset=utf-8');
|
||
return true;
|
||
}
|
||
|
||
/*
|
||
@ Ìåòîä ïðèîáðàçîâàíèÿ îáüåêòà â JSON
|
||
*/
|
||
public function jsonencode( $val ) {
|
||
array_walk_recursive( $val, function( &$value, $key ) {
|
||
$value = iconv( "CP1251", "UTF-8", $value );
|
||
});
|
||
return json_encode( $val );
|
||
//return json_encode( $val );
|
||
//return self::json_fix_cyr( json_encode( $val ) );
|
||
}
|
||
|
||
/*
|
||
@ Ìåòîä ïðèîáðàçîâàíèÿ JSON â îáüåêò
|
||
*/
|
||
public function jsondecode( $val ) {
|
||
return json_decode( $val );
|
||
}
|
||
|
||
/*
|
||
@ Ìåòîä ôèêñà êèðèëëè÷åñêèõ ñèìâîëîâ
|
||
*/
|
||
public function json_fix_cyr($json_str) {
|
||
$cyr_chars = array (
|
||
'\u0430' => 'à', '\u0410' => 'À',
|
||
'\u0431' => 'á', '\u0411' => 'Á',
|
||
'\u0432' => 'â', '\u0412' => 'Â',
|
||
'\u0433' => 'ã', '\u0413' => 'Ã',
|
||
'\u0434' => 'ä', '\u0414' => 'Ä',
|
||
'\u0435' => 'å', '\u0415' => 'Å',
|
||
'\u0451' => '¸', '\u0401' => '¨',
|
||
'\u0436' => 'æ', '\u0416' => 'Æ',
|
||
'\u0437' => 'ç', '\u0417' => 'Ç',
|
||
'\u0438' => 'è', '\u0418' => 'È',
|
||
'\u0439' => 'é', '\u0419' => 'É',
|
||
'\u043a' => 'ê', '\u041a' => 'Ê',
|
||
'\u043b' => 'ë', '\u041b' => 'Ë',
|
||
'\u043c' => 'ì', '\u041c' => 'Ì',
|
||
'\u043d' => 'í', '\u041d' => 'Í',
|
||
'\u043e' => 'î', '\u041e' => 'Î',
|
||
'\u043f' => 'ï', '\u041f' => 'Ï',
|
||
'\u0440' => 'ð', '\u0420' => 'Ð',
|
||
'\u0441' => 'ñ', '\u0421' => 'Ñ',
|
||
'\u0442' => 'ò', '\u0422' => 'Ò',
|
||
'\u0443' => 'ó', '\u0423' => 'Ó',
|
||
'\u0444' => 'ô', '\u0424' => 'Ô',
|
||
'\u0445' => 'õ', '\u0425' => 'Õ',
|
||
'\u0446' => 'ö', '\u0426' => 'Ö',
|
||
'\u0447' => '÷', '\u0427' => '×',
|
||
'\u0448' => 'ø', '\u0428' => 'Ø',
|
||
'\u0449' => 'ù', '\u0429' => 'Ù',
|
||
'\u044a' => 'ú', '\u042a' => 'Ú',
|
||
'\u044b' => 'û', '\u042b' => 'Û',
|
||
'\u044c' => 'ü', '\u042c' => 'Ü',
|
||
'\u044d' => 'ý', '\u042d' => 'Ý',
|
||
'\u044e' => 'þ', '\u042e' => 'Þ',
|
||
'\u044f' => 'ÿ', '\u042f' => 'ß',
|
||
|
||
'\r' => '',
|
||
'\n' => '<br />',
|
||
'\t' => ''
|
||
);
|
||
foreach ($cyr_chars as $cyr_char_key => $cyr_char) {
|
||
$json_str = str_replace($cyr_char_key, $cyr_char, $json_str);
|
||
}
|
||
return $json_str;
|
||
}
|
||
|
||
public static function timeOut($ttm)
|
||
{
|
||
$out = '';
|
||
$time_still = $ttm;
|
||
$tmp = floor($time_still/2592000);
|
||
$id=0;
|
||
if ($tmp > 0)
|
||
{
|
||
$id++;
|
||
if ($id<3) {$out .= $tmp." ìåñ. ";}
|
||
$time_still = $time_still-$tmp*2592000;
|
||
}
|
||
$tmp = floor($time_still/86400);
|
||
if ($tmp > 0)
|
||
{
|
||
$id++;
|
||
if ($id<3) {$out .= $tmp." äí. ";}
|
||
$time_still = $time_still-$tmp*86400;
|
||
}
|
||
$tmp = floor($time_still/3600);
|
||
if ($tmp > 0)
|
||
{
|
||
$id++;
|
||
if ($id<3) {$out .= $tmp." ÷. ";}
|
||
$time_still = $time_still-$tmp*3600;
|
||
}
|
||
$tmp = floor($time_still/60);
|
||
if ($tmp > 0)
|
||
{
|
||
$id++;
|
||
if ($id<3) {$out .= $tmp." ìèí. ";}
|
||
}
|
||
if($out=='')
|
||
{
|
||
if($time_still<0)
|
||
{
|
||
$time_still = 0;
|
||
}
|
||
$out = $time_still.' ñåê.';
|
||
}
|
||
return $out;
|
||
}
|
||
|
||
}
|
||
|
||
?>
|