7 public static function IsIPv4($ip) {
8 return !empty($ip) && (FALSE !== filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4));
12 return !empty($ip) && (FALSE !== filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6));
19 if (function_exists(
"curl_init")) {
21 CURLOPT_TIMEOUT => 28800,
23 CURLOPT_RETURNTRANSFER => 1
26 curl_setopt_array($ch, $options);
27 $result = curl_exec($ch);
29 }
else if (ini_get(
'allow_url_fopen')) {
30 $file = fopen($url,
'rb');
32 while (!feof($file)) {
33 $result .= fread($file, 8192);
38 $result = file_get_contents($url);
40 }
catch (Exception $ex) {
43 if ($result && $saveTo && is_writable(dirname($saveTo))) {
45 file_put_contents($saveTo, $result);
46 }
catch (Exception $ex) {
52 public static function GetAllFiles($root, $trimRoot = FALSE, $level = 0) {
54 if (file_exists($root) && is_readable($root)) {
55 $handle = opendir($root);
59 while ($handle &&
false !== ($entry = readdir($handle))) {
60 if ($entry ==
"." || $entry ==
".." || strcasecmp($entry,
".DS_Store") == 0 || strcasecmp($entry,
"thumbs.db") == 0) {
63 $entryPath = self::CombinePath($root, $entry);
64 if (is_dir($entryPath)) {
67 $files[] = $entryPath;
71 foreach ($dirs as $dir) {
72 $files = array_merge(self::GetAllFiles($dir, FALSE, $level), $files);
74 if ($level == 0 && $trimRoot) {
75 for ($i = 0; $i < count($files); ++$i) {
76 $files[$i] = str_replace($root,
"", $files[$i]);
77 if (Utils::StartsWith($files[$i],
"/")) {
78 $files[$i] = substr($files[$i], 1);
85 public static function GetAllFolders($root, $recursive = FALSE, $trimRoot = FALSE, $level = 0) {
87 if (file_exists($root) && is_dir($root) && is_readable($root)) {
88 $handle = opendir($root);
92 while (
false !== ($entry = readdir($handle))) {
93 if (self::StartsWith($entry,
".") || is_file($entry)) {
96 $dir = self::CombinePath($root, $entry);
99 $dirs = array_merge($dirs, self::GetAllFolders($dir, TRUE, $trimRoot, $level + 1));
102 if ($level == 0 && $trimRoot) {
103 for ($i = 0; $i < count($dirs); ++$i) {
104 $dirs[$i] = str_replace($root,
"", $dirs[$i]);
113 http_response_code($code);
114 if (!empty($message)) {
121 $serverName = filter_input(INPUT_SERVER,
"SERVER_NAME");
122 if (empty($serverName) && isset($_SERVER[
"SERVER_NAME"])) {
123 $serverName = $_SERVER[
"SERVER_NAME"];
130 if (!empty(filter_input(INPUT_SERVER,
"HTTPS")) || (isset($_SERVER[
"HTTPS"]) && !empty($_SERVER[
"HTTPS"]))) {
133 $serverPort = filter_input(INPUT_SERVER,
"SERVER_PORT");
134 if (empty($serverPort) && isset($_SERVER[
"SERVER_PORT"])) {
135 $serverPort = $_SERVER[
"SERVER_PORT"];
137 if (!empty($serverPort) && $serverPort != 80) {
138 $serverPort =
":" . $serverPort;
142 return self::CombineUrl($protocol .
"://" . self::GetServerName() . $serverPort, self::CombineUrl(URL_ROOT, $url));
146 return self::GetServerUrl(self::CombineUrl(URL_UPLOAD, $url));
150 return self::CombinePath($path1, $path2,
"/");
153 public static function CombinePath($path1, $path2, $separator = DIRECTORY_SEPARATOR) {
155 if (!empty($path2)) {
156 if ($separator && self::EndsWith($path, $separator)) {
157 $path = substr($path, 0, strlen($path) - 1);
159 $path .= ($separator && !self::StartsWith($path2, $separator) ? $separator :
"") . $path2;
165 return new \DateTimeZone(
"Etc/UTC");
170 $timezone = self::GetDateTimeZoneUTC();
172 return new \DateTime(
"now", $timezone);
176 $now = self::GetCurrentDateTime($timezone);
177 return $now->format($format);
181 $ip = filter_input(INPUT_SERVER,
"REMOTE_ADDR");
182 if (empty($ip) && isset($_SERVER[
"REMOTE_ADDR"])) {
183 $ip = $_SERVER[
"REMOTE_ADDR"];
195 $mktime = (($ticks - 621355968000000000) / 10000000);
206 return number_format(($time * 10000000) + 621355968000000000 , 0,
'.',
'');
216 return time_to_ticks(strtotime($str));
220 return sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
222 mt_rand(0, 0xffff), mt_rand(0, 0xffff),
227 mt_rand(0, 0x0fff) | 0x4000,
231 mt_rand(0, 0x3fff) | 0x8000,
233 mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
239 $Database->CloseConnection();
241 $url = $_SERVER[
"PHP_SELF"];
242 header(
"Location: $url");
247 $Database->CloseConnection();
248 $url = $_SERVER[
"PHP_SELF"];
250 foreach ($_GET as $key => $value) {
251 $queryString .= ($queryString ?
"&" :
"") .
"$key=" . urlencode($value);
253 $url .=
"?" . $queryString;
254 header(
"Location: $url");
259 return (defined(
"RESPONSE_ENCRYPTED") && RESPONSE_ENCRYPTED === TRUE);
262 public static function EchoJson($text, $encodeJson = FALSE, $encryptResponse = FALSE) {
264 $Database->CloseConnection();
265 if ($encodeJson === TRUE) {
266 $text = json_encode($text);
269 header(
"Access-Control-Allow-Credentials:true");
270 header(
"Access-Control-Allow-Headers:Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time");
271 header(
"Access-Control-Allow-Methods:GET, POST, OPTIONS");
272 header(
"Access-Control-Allow-Origin:*");
273 header(
"Content-Type:text/json");
274 if ($encryptResponse && self::ServerResponseRequired()) {
275 echo json_encode(array(
"t" => self::GetCurrentDateTimeFormat(),
"d" => clientEncrypt($text) ));
282 public static function EchoXml($xml, $encryptResponse = FALSE) {
284 $Database->CloseConnection();
286 header(
"Access-Control-Allow-Credentials:true");
287 header(
"Access-Control-Allow-Headers:Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time");
288 header(
"Access-Control-Allow-Methods:GET, POST, OPTIONS");
289 header(
"Access-Control-Allow-Origin:*");
290 header(
"Content-Type:text/xml");
291 if ($encryptResponse && self::ServerResponseRequired()) {
292 echo sprintf(
"<response><t>%s</t><d>%s</d></response>", htmlentities(self::GetCurrentDateTimeFormat()), htmlentities(clientEncrypt($xml)));
301 $text = ErrorMessage::Get(ERROR_UNAUTHORIZED_REQUEST);
304 http_response_code(401);
309 public static function StartsWith($text, $search, $ignoreCase =
false) {
310 $s = substr($text, 0, strlen($search));
311 if ($ignoreCase ===
true) {
312 return (strtolower($s) == strtolower($search));
314 return ($s == $search);
317 public static function EndsWith($text, $search, $ignoreCase =
false) {
318 $s = substr($text, -strlen($search));
319 if ($ignoreCase ===
true) {
320 return (strtolower($s) == strtolower($search));
322 return ($s == $search);
334 if (strpos($data,
" ") ===
false) {
338 $a_data = substr($data, 0, strpos($data,
" "));
339 $a_time = substr($data, strpos($data,
" ") + 1);
341 $a_data = explode(
"/", str_replace(
"-",
"/", $a_data));
342 $a_time = explode(
":", str_replace(
".",
":", $a_time));
343 if (strlen($a_data[0]) == 4) {
345 $timestamp = mktime($a_time[0], $a_time[1], count($a_time) == 3 ? $a_time[2] : 0, $a_data[1], $add_days + $a_data[2], $a_data[0]);
348 $timestamp = mktime($a_time[0], $a_time[1], count($a_time) == 3 ? $a_time[2] : 0, $a_data[1], $add_days + $a_data[0], $a_data[2]);
355 if (preg_match(
'/^(\d{4})-(\d{2})-(\d{2})$/', $text, $datebit)) {
356 return checkdate($datebit[2], $datebit[3], $datebit[1]);
363 if (preg_match(
'/^(\d{2}):(\d{2})$/', $text, $datebit)) {
364 return ($datebit[1] >= 0 && $datebit[1] <= 59) && ($datebit[2] >= 0 && $datebit[2] <= 59);
365 }
else if (preg_match(
'/^(\d{2}):(\d{2}):(\d{2})$/', $text, $datebit)) {
366 return ($datebit[1] >= 0 && $datebit[1] <= 59) && ($datebit[2] >= 0 && $datebit[2] <= 59) && ($datebit[3] >= 0 && $datebit[3] <= 59);
372 $float = floatval($bytes);
375 $float = ( $float * 8 ) / 1024;
389 return round($float, 1);
394 $text = preg_replace(
"/\s{2,}/",
" ", $searchText);
396 preg_match_all(
"|\"[^\"]+\"|", $text, $multiple);
397 foreach ($multiple[0] as $key) {
398 $text = str_replace($key,
"", $text);
399 $keys[] = str_replace(
'"',
'', $key);
401 $arr_text = explode(
" ", $text);
402 foreach ($arr_text as $s) {
403 if (!empty($s) && !in_array($s, $keys)) {
411 if ($total == 0 || $count == 0) {
414 $perc = number_format((floatval($count) / floatval($total)) * 100, 2);
415 if (substr($perc, -2) ==
"00") {
416 return substr($perc, 0, strlen($perc) - 3);
418 if (substr($perc, -1) ==
"0") {
419 return substr($perc, 0, strlen($perc) - 1);
426 return $limit * ($page - 1);
433 return ceil($count / $limit);
448 $ext = Utils::GetFilenameExtension($name);
450 $name = substr($name, 0, strlen($name) - strlen($ext));
452 for ($i = 0; $i < strlen($name); $i++) {
453 $char = substr($name, $i, 1);
454 if (!preg_match(
"/^[a-zA-Z0-9._-]$/", $char)) {
459 $newName .= ($newExt !=
"" ? $newExt : $ext);
470 $i = strrpos($filename,
".");
474 return substr($filename, $i);
483 $tmp_name = md5(microtime()) . $fileExt;
495 if (defined(
"RANDOM_CODE_LENGTH") && RANDOM_CODE_LENGTH > 0) {
496 $length = intval(RANDOM_CODE_LENGTH);
502 if (defined(
"RANDOM_CODE_CHARS") && RANDOM_CODE_CHARS) {
503 $chars = RANDOM_CODE_CHARS;
505 $chars =
"bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ0123456789";
507 while (strlen($code) < $length) {
508 $code .= substr($chars, rand(0, strlen($chars) - 1), 1);
519 $regex =
'/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i';
520 return preg_match($regex, $email);
530 if (count($rows) > 0 && $rows[0] !== NULL && is_subclass_of($rows[0],
"DataClass")) {
532 foreach ($rows as $r) {
533 $temp[] = $r->ToArray();
537 for ($i = 0; $i < count($rows); $i++) {
538 foreach ($rows[$i] as $k => $v) {
539 if (is_array($v) || is_object($v)) {
542 if (is_numeric($v) || is_bool($v)) {
544 } elseif ($v == null || $v ==
"NULL") {
546 } elseif (substr($v, 0, 10) ==
"00/00/0000" || substr($v, 0, 10) ==
"0000-00-00") {
551 $result = array(
"total" => $count,
"results" => $rows);
552 if ($pagesCount !== NULL) {
553 $result[
"pages"] = $pagesCount;
555 if ($extraParams !== NULL && is_array($extraParams)) {
556 $result = array_merge($result, $extraParams);
558 return json_encode($result);
570 return json_encode(array(
"success" => $success,
"message" => $message,
"errors" => $errors));
579 $props = array_keys(get_class_vars(get_class($obj)));
580 foreach ($props as $prop) {
581 $requestValue = filter_input(INPUT_POST, $prop);
582 if (is_null($requestValue)) {
583 $requestValue = filter_input(INPUT_GET, $prop);
585 if (!is_null($requestValue)) {
586 $obj->$prop = $requestValue;
599 public static function FillObjectFromRow(&$obj, $row, $stripSlashes =
false, $callbackOnExists =
false) {
600 $props = get_class_vars(get_class($obj));
601 foreach ($props as $prop => $value) {
602 if (array_key_exists($prop, $row)) {
603 if (!$callbackOnExists) {
604 $value = ($stripSlashes ? trim(stripslashes($row[$prop])) : $row[$prop]);
608 $obj->$prop = $value;
610 $obj->$callbackOnExists($prop, $row[$prop]);
625 if (is_a($obj,
"stdClass")) {
626 $variables = get_object_vars($obj);
627 $keys = array_keys($variables);
628 foreach ($keys as $k) {
629 $array[$k] = $obj->$k;
632 $props = get_class_vars(get_class($obj));
633 foreach ($props as $prop => $value) {
634 $array[$prop] = $obj->$prop;
static ConvertSizeFromBytes($bytes, $to=NULL)
static EchoUnauthorized($text="")
static JsonEncodeSuccessMessage($success=true, $message="", $errors=array())
static GenerateRandomCode($length=0)
static CalcPercentage($total, $count)
static TimeToTicks($time)
static GetCurrentDateTimeFormat($format="Y-m-d H:i:s", $timezone=NULL)
static TicksToTime($ticks)
static EndsWith($text, $search, $ignoreCase=false)
static GetUploadUrl($url="")
static ObjectToArray($obj)
static JsonEncodeRowsMessage($rows, $count, $pagesCount=NULL, $extraParams=NULL)
static IsValidEmail($email)
static FillObjectFromRow(&$obj, $row, $stripSlashes=false, $callbackOnExists=false)
static GetFilenameExtension($filename)
static FillObjectFromRequest(&$obj)
static GetPagesCount($count, $limit)
static GetCurrentDateTime($timezone=NULL)
static ExtractSearchKeywords($searchText)
static HttpErrorCode($code=401, $message=NULL)
static ServerResponseRequired()
static EchoJson($text, $encodeJson=FALSE, $encryptResponse=FALSE)
static StringToTicks($str)
static EchoXml($xml, $encryptResponse=FALSE)
static CombineUrl($path1, $path2)
static GetPageOffset($page, $limit)
static GetAllFolders($root, $recursive=FALSE, $trimRoot=FALSE, $level=0)
static GetTempname($fileExt=".tmp")
static IsValidTime($text)
static StartsWith($text, $search, $ignoreCase=false)
static RedirectTo($url="")
static GetServerUrl($url="")
static GetAllFiles($root, $trimRoot=FALSE, $level=0)
static IsValidDate($text)
static GetDateTimeZoneUTC()
static GetValidFilename($oldName, $newExt="")
static GetTimestamp($data, $add_days=0)
static DownloadUrl($url, $saveTo=NULL)
static CombinePath($path1, $path2, $separator=DIRECTORY_SEPARATOR)