Эх сурвалжийг харах

Functions: Enhance is_uuid(), is_mac() and format_device_address() functions.

fusionate 2 жил өмнө
parent
commit
a8ced1a441
1 өөрчлөгдсөн 18 нэмэгдсэн , 7 устгасан
  1. 18 7
      resources/functions.php

+ 18 - 7
resources/functions.php

@@ -150,12 +150,19 @@
 	}
 
 	if (!function_exists('is_uuid')) {
-		function is_uuid($uuid) {
-			if (gettype($uuid) == 'string') {
-				$regex = '/^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i';
-				return preg_match($regex, $uuid);
+		function is_uuid($str) {
+			$is_uuid = false;
+			if (gettype($str) == 'string') {
+				if (substr_count($str, '-') != 0 && strlen($str) == 36) {
+					$regex = '/^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i';
+					$is_uuid = preg_match($regex, $str);
+				}
+				else if (strlen(preg_replace("#[^a-fA-F0-9]#", '', $str)) == 32) {
+					$regex = '/^[0-9A-F]{32}$/i';
+					$is_uuid = preg_match($regex, $str);
+				}
 			}
-			return false;
+			return $is_uuid;
 		}
 	}
 
@@ -1485,7 +1492,7 @@ function number_pad($number,$n) {
 //mac detection
 	if (!function_exists('is_mac')) {
 		function is_mac($str) {
-			return (preg_match('/([a-fA-F0-9]{2}[:|\-]?){6}/', $str) == 1) ? true : false;
+			return preg_match('/([a-fA-F0-9]{2}[:|\-]?){6}/', $str) == 1 && strlen(preg_replace("#[^a-fA-F0-9]#", '', $str)) == 12 ? true : false;
 		}
 	}
 
@@ -1506,10 +1513,14 @@ function number_pad($number,$n) {
 	if (!function_exists('format_device_address')) {
 		function format_device_address($str, $delim = '-', $case = 'lower') {
 			if (empty($str)) { return false; }
+			$str = preg_replace("#[^a-fA-F0-9]#", '', $str); //remove formatting, if any
 			if (is_mac($str)) {
 				$str = join($delim, str_split($str, 2));
 			}
-			$str = ($case == 'upper') ? strtoupper($str) : strtolower($str);
+			else if (is_uuid($str)) {
+				$str = substr($str, 0, 8).'-'.substr($str, 8, 4).'-'.substr($str, 12, 4).'-'.substr($str, 16, 4).'-'.substr($str, 20, 12);
+			}
+			$str = $case == 'upper' ? strtoupper($str) : strtolower($str);
 			return $str;
 		}
 	}