Skip to content

Commit b0c7510

Browse files
authored
fix
1 parent d5a4720 commit b0c7510

File tree

3 files changed

+63
-51
lines changed

3 files changed

+63
-51
lines changed

App/Models/CORS.php

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,90 +4,102 @@
44

55
class CORS
66
{
7-
// Initialize private properties to store CORS settings
8-
private $allowedOrigins = array();
9-
private $allowedMethods = array();
10-
private $allowedHeaders = array();
11-
private $exposedHeaders = array();
12-
private $maxAge = 0;
13-
private $allowCredentials = false;
7+
// Define private static properties to store CORS settings
8+
static private $allowedOrigins = array();
9+
static private $allowedMethods = array();
10+
static private $allowedHeaders = array();
11+
static private $exposedHeaders = array();
12+
static private $maxAge = 0;
13+
static private $allowCredentials = false;
1414

1515
// Method to set allowed origins
16-
public function origin($origins)
16+
public static function origin($origins)
1717
{
18-
$this->allowedOrigins = $origins;
19-
return $this;
18+
// Assign passed origins to the static property
19+
self::$allowedOrigins = $origins;
20+
// Return new instance of the class for method chaining
21+
return new static;
2022
}
2123

2224
// Method to set allowed methods
23-
public function methods($methods)
25+
public static function methods($methods)
2426
{
25-
$this->allowedMethods = $methods;
26-
return $this;
27+
// Assign passed methods to the static property
28+
self::$allowedMethods = $methods;
29+
// Return new instance of the class for method chaining
30+
return new static;
2731
}
2832

2933
// Method to set allowed headers
30-
public function headers($headers)
34+
public static function headers($headers)
3135
{
32-
$this->allowedHeaders = $headers;
33-
return $this;
36+
// Assign passed headers to the static property
37+
self::$allowedHeaders = $headers;
38+
// Return new instance of the class for method chaining
39+
return new static;
3440
}
3541

3642
// Method to set exposed headers
37-
public function expose($headers)
43+
public static function expose($headers)
3844
{
39-
$this->exposedHeaders = $headers;
40-
return $this;
45+
// Assign passed headers to the static property
46+
self::$exposedHeaders = $headers;
47+
// Return new instance of the class for method chaining
48+
return new static;
4149
}
4250

4351
// Method to set max age
44-
public function maxAge($age)
52+
public static function maxAge($age)
4553
{
46-
$this->maxAge = $age;
47-
return $this;
54+
// Assign passed age to the static property
55+
self::$maxAge = $age;
56+
// Return new instance of the class for method chaining
57+
return new static;
4858
}
4959

5060
// Method to set allow credentials
51-
public function credentials($credentials)
61+
public static function credentials($credentials)
5262
{
53-
$this->allowCredentials = $credentials;
54-
return $this;
63+
// Assign passed credentials to the static property
64+
self::$allowCredentials = $credentials;
65+
// Return new instance of the class for method chaining
66+
return new static;
5567
}
5668

5769
// Method to set CORS headers based on the properties set in the constructor
58-
public function setHeaders()
70+
public static function setHeaders()
5971
{
6072
// Check if the allowed origins include all origins by checking if '*' is in the array
61-
if (in_array('*', $this->allowedOrigins)) {
73+
if (in_array('*', self::$allowedOrigins)) {
6274
// Allow all origins with a wildcard
6375
header('Access-Control-Allow-Origin: *');
6476
} else {
6577
// Check if the origin of the request is in the allowed origins array
6678
$origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
67-
if (in_array($origin, $this->allowedOrigins)) {
79+
if (in_array($origin, self::$allowedOrigins)) {
6880
// Set the allowed origin to the origin of the request
6981
header("Access-Control-Allow-Origin: $origin");
7082
}
7183
}
7284
// Check if credentials are allowed and set the allow credentials header if true
73-
if ($this->allowCredentials) {
85+
if (self::$allowCredentials) {
7486
header('Access-Control-Allow-Credentials: true');
7587
}
7688
// Set the exposed headers header if there are any exposed headers
77-
if (!empty($this->exposedHeaders)) {
78-
header('Access-Control-Expose-Headers: ' . implode(', ', $this->exposedHeaders));
89+
if (!empty(self::$exposedHeaders)) {
90+
header('Access-Control-Expose-Headers: ' . implode(', ', self::$exposedHeaders));
7991
}
8092
// Set the max age header if the max age is greater than 0
81-
if ($this->maxAge > 0) {
82-
header("Access-Control-Max-Age: $this->maxAge");
93+
if (self::$maxAge > 0) {
94+
header("Access-Control-Max-Age: " . self::$maxAge);
8395
}
8496
// Set the allowed methods header if there are any allowed methods
85-
if (!empty($this->allowedMethods)) {
86-
header('Access-Control-Allow-Methods: ' . implode(', ', $this->allowedMethods));
97+
if (!empty(self::$allowedMethods)) {
98+
header('Access-Control-Allow-Methods: ' . implode(', ', self::$allowedMethods));
8799
}
88100
// Set the allowed headers header if there are any allowed headers
89-
if (!empty($this->allowedHeaders)) {
90-
header('Access-Control-Allow-Headers: ' . implode(', ', $this->allowedHeaders));
101+
if (!empty(self::$allowedHeaders)) {
102+
header('Access-Control-Allow-Headers: ' . implode(', ', self::$allowedHeaders));
91103
}
92104
}
93105
}

App/Models/Info.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Info
1313
Retrieves the domain name of the server.
1414
@return string The domain name of the server.
1515
*/
16-
public function Domain()
16+
public static function Domain()
1717
{
1818
return $_SERVER['SERVER_NAME'];
1919
}
@@ -22,7 +22,7 @@ public function Domain()
2222
Retrieves the host name used in the HTTP request.
2323
@return string The host name used in the HTTP request.
2424
*/
25-
public function Host()
25+
public static function Host()
2626
{
2727
return $_SERVER['HTTP_HOST'];
2828
}
@@ -31,7 +31,7 @@ public function Host()
3131
Retrieves the IP address of the server's host.
3232
@return string The IP address of the server's host.
3333
*/
34-
public function HostIP()
34+
public static function HostIP()
3535
{
3636
return gethostbyname($_SERVER['SERVER_NAME']);
3737
}
@@ -40,7 +40,7 @@ public function HostIP()
4040
Retrieves the IP address of the user making the request.
4141
@return string The IP address of the user making the request.
4242
*/
43-
public function UserIP()
43+
public static function UserIP()
4444
{
4545
return $_SERVER['REMOTE_ADDR'];
4646
}
@@ -49,7 +49,7 @@ public function UserIP()
4949
Retrieves the cookies sent by the user.
5050
@return array An associative array of cookies sent by the user.
5151
*/
52-
public function Cookies()
52+
public static function Cookies()
5353
{
5454
return $_COOKIE;
5555
}
@@ -58,7 +58,7 @@ public function Cookies()
5858
Retrieves the full cookie string sent by the user.
5959
@return string The full cookie string sent by the user.
6060
*/
61-
public function FullCookie()
61+
public static function FullCookie()
6262
{
6363
return $_SERVER['HTTP_COOKIE'];
6464
}
@@ -67,7 +67,7 @@ public function FullCookie()
6767
Retrieves the path of the current request.
6868
@return string The path of the current request.
6969
*/
70-
public function Path()
70+
public static function Path()
7171
{
7272
return $_SERVER['REQUEST_URI'];
7373
}
@@ -76,7 +76,7 @@ public function Path()
7676
Retrieves the type of device used by the user.
7777
@return string The type of device used by the user (Mobile, Tablet, or Desktop).
7878
*/
79-
public function Device()
79+
public static function Device()
8080
{
8181
$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
8282
if (strpos($userAgent, 'mobile') !== false) {
@@ -92,7 +92,7 @@ public function Device()
9292
Retrieves the user agent string sent by the user.
9393
@return string The user agent string sent by the user.
9494
*/
95-
public function UserAgent()
95+
public static function UserAgent()
9696
{
9797
return $_SERVER['HTTP_USER_AGENT'];
9898
}
@@ -101,7 +101,7 @@ public function UserAgent()
101101
Retrieves the HTTP headers sent by the user.
102102
@return array An associative array of HTTP headers sent by the user.
103103
*/
104-
public function getHeaders()
104+
public static function getHeaders()
105105
{
106106
$headers = [];
107107
foreach ($_SERVER as $name => $value) {
@@ -115,7 +115,7 @@ public function getHeaders()
115115
/*
116116
Echoes the cookies sent by the user.
117117
*/
118-
public function echoCookies()
118+
public static function echoCookies()
119119
{
120120
$cookies = $_COOKIE;
121121
foreach ($cookies as $key => $value) {
@@ -129,7 +129,7 @@ public function echoCookies()
129129
130130
@return string The operating system of the user.
131131
*/
132-
public function getOS()
132+
public static function getOS()
133133
{
134134
$userAgent = $_SERVER['HTTP_USER_AGENT'];
135135
$devices = [

App/Models/Json.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
class Json
66
{
77
// Clean and output the given data as JSON with indentation and Unicode and slashes escaping
8-
public function clean(array $data, int $code = 200)
8+
public static function clean(array $data, int $code = 200)
99
{
1010
http_response_code($code); // HTTP Code
1111
header('Content-type: application/json; charset=utf-8'); // Set the response header to indicate JSON content type
1212
echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); // Encode the data as JSON with pretty print and Unicode and slashes escaping
1313
}
1414

1515
// Output the given data as JSON without any special formatting
16-
public function show(array $data, int $code = 200)
16+
public static function show(array $data, int $code = 200)
1717
{
1818
http_response_code($code); // HTTP Code
1919
header('Content-type: application/json; charset=utf-8'); // Set the response header to indicate JSON content type

0 commit comments

Comments
 (0)