· 6 years ago · Oct 07, 2019, 01:12 PM
1<?php
2/**
3 * Class for AUTH of user log in
4 */
5class AUTH
6{
7 //Check if the user is logged in
8 public function LoginAUTH($session, $redirect)
9 {
10 // If session variable is not set it will redirect to login page
11 if (!isset($_SESSION[$session]) || empty($_SESSION[$session])) {
12 header("location: $redirect");
13 exit;
14 }
15 }
16
17 //Log out the user
18 public function Logout()
19 {
20 // Unset all of the session variables
21 $_SESSION = array();
22 // Destroy the session.
23 session_destroy();
24 }
25
26 //Check if session expired
27 public function SessionExpired($time, $location)
28 {
29 //Checking if session ended
30 if ($time > $_SESSION['expire']) {
31 session_destroy();
32 header("location: $location");
33 }
34 }
35}
36/**
37 * Class for Getting Userip and other IP informations
38 */
39class IP
40{
41 //Get User ip
42 public function UserIp()
43 {
44 $client = @$_SERVER['HTTP_CLIENT_IP'];
45 $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
46 $remote = $_SERVER['REMOTE_ADDR'];
47
48 if (filter_var($client, FILTER_VALIDATE_IP)) {
49 $ip = $client;
50 } elseif (filter_var($forward, FILTER_VALIDATE_IP)) {
51 $ip = $forward;
52 } else {
53 $ip = $remote;
54 }
55
56 return $ip;
57 }
58 //Get User Ip Info
59 public function ipinfo($ip = null, $purpose = "location", $deep_detect = true)
60 {
61 $output = null;
62
63 $purpose = str_replace(array("name", "\n", "\t", " ", "-", "_"), null, strtolower(trim($purpose)));
64 $support = array("country", "countrycode", "state", "region", "city", "location", "address");
65 $continents = array(
66 "AF" => "Africa",
67 "AN" => "Antarctica",
68 "AS" => "Asia",
69 "EU" => "Europe",
70 "OC" => "Australia (Oceania)",
71 "NA" => "North America",
72 "SA" => "South America",
73 );
74 if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {
75 $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
76 if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
77 switch ($purpose) {
78 case "location":
79 $output = array(
80 "city" => @$ipdat->geoplugin_city,
81 "state" => @$ipdat->geoplugin_regionName,
82 "country" => @$ipdat->geoplugin_countryName,
83 "country_code" => @$ipdat->geoplugin_countryCode,
84 "continent" => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
85 "continent_code" => @$ipdat->geoplugin_continentCode,
86 );
87 break;
88 case "address":
89 $address = array($ipdat->geoplugin_countryName);
90 if (@strlen($ipdat->geoplugin_regionName) >= 1) {
91 $address[] = $ipdat->geoplugin_regionName;
92 }
93
94 if (@strlen($ipdat->geoplugin_city) >= 1) {
95 $address[] = $ipdat->geoplugin_city;
96 }
97
98 $output = implode(", ", array_reverse($address));
99 break;
100 case "city":
101 $output = @$ipdat->geoplugin_city;
102 break;
103 case "state":
104 $output = @$ipdat->geoplugin_regionName;
105 break;
106 case "region":
107 $output = @$ipdat->geoplugin_regionName;
108 break;
109 case "country":
110 $output = @$ipdat->geoplugin_countryName;
111 break;
112 case "countrycode":
113 $output = @$ipdat->geoplugin_countryCode;
114 break;
115 }
116 }
117 }
118 return $output;
119
120 }
121
122}
123
124/**
125 * Class for validating variables and encoding decoding variables
126 */
127class ValidateVariable
128{
129
130 public function CleanString($variable)
131 {
132 $string = htmlspecialchars(stripslashes($variable));
133 return $string;
134 }
135
136 public function encode($string,$key) {
137 $key = sha1($key);
138 $j = 0;
139 $hash = "";
140 $strLen = strlen($string);
141 $keyLen = strlen($key);
142 for ($i = 0; $i < $strLen; $i++) {
143 $ordStr = ord(substr($string,$i,1));
144 if ($j == $keyLen) { $j = 0; }
145 $ordKey = ord(substr($key,$j,1));
146 $j++;
147 $hash .= strrev(base_convert(dechex($ordStr + $ordKey),16,36));
148 }
149 return $hash;
150 }
151 public function decode($string,$key) {
152 $key = sha1($key);
153 $j = 0;
154 $hash = "";
155 $strLen = strlen($string);
156 $keyLen = strlen($key);
157 for ($i = 0; $i < $strLen; $i+=2) {
158 $ordStr = hexdec(base_convert(strrev(substr($string,$i,2)),36,16));
159 if ($j == $keyLen) { $j = 0; }
160 $ordKey = ord(substr($key,$j,1));
161 $j++;
162 $hash .= chr($ordStr - $ordKey);
163 }
164 return $hash;
165 }
166
167}
168/**
169 * Create a random Hash with give length
170 */
171class Hash
172{
173 public function Random($length = false)
174 {
175 if (empty($length)) {
176 $length = "6";
177 }
178 $str = "";
179 $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
180 $size = strlen($chars);
181 for ($i = 0; $i < $length; $i++) {
182 $str .= $chars[rand(0, $size - 1)];
183 }
184 return $str;
185
186 }
187}
188/**
189 * Check if email is valid and check if entered email is gmail or hotmail
190 */
191class Email
192{
193 //Check if entered string is an email
194 public function ValidateEmail($string)
195 {
196 if (filter_var($string, FILTER_VALIDATE_EMAIL)) {
197 return $string;
198 } else {
199 return "error";
200 }
201 }
202 //Check if entere email is using hosts gmail or hotmail
203 public function ValidateHost($email, $host)
204 {
205 switch ($host) {
206 case 'gmail':
207 $host = "gmail.com";
208 $length = "9";
209 break;
210 case 'hotmail':
211 $host = "hotmail.com";
212 $length = "11";
213 break;
214 default:
215 $host = "gmail.com";
216 $length = "10";
217 break;
218 }
219 if (mb_substr($email, -$length) == $host) {
220 return "ok";
221 } else {
222 return "error";
223 }
224 }
225
226}
227/**
228 * Check and create server request POST GET
229 */
230class Request
231{
232
233 public function Method($string)
234 {
235 $string = strtoupper($string);
236
237 switch ($string) {
238 case 'POST':
239 $string = "POST";
240 break;
241 case 'GET':
242 $string = "GET";
243 break;
244 default:
245 $string = "POST";
246 break;
247 }
248 if ($_SERVER['REQUEST_METHOD'] == $string) {
249 return "ok";
250 } else {
251 $response = json_encode(array(
252 'status' => '404',
253 'message' => 'INVALID REQUEST METHOD',
254 'message2' => '',
255 ), true);
256
257 return $response;
258 die();
259 }
260 }
261 //Get the current url
262 public function GetUrl()
263 {
264 $url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}";
265 return $url;
266 }
267 //Function to sort path of files
268 public function GetFilesPath($path)
269 {
270 ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . $path . DIRECTORY_SEPARATOR);
271 }
272 //Check if POST values from a form are empty
273 public function CheckEmptyValues($data)
274 {
275 $arr_msg = array();
276 //Check if any of the data is empty
277 foreach($data as $x => $x_value) {
278 if (empty($_POST[$x])) {
279 $arr_msg[] = $x_value;
280 }
281 }
282 return $arr_msg;
283 }
284}
285/**
286 * Get distance between 2 cities with longitude and latitude
287 */
288class Distance
289{
290
291 public function getDistance($latitude1, $longitude1, $latitude2, $longitude2)
292 {
293 $earth_radius = 6371;
294
295 $dLat = deg2rad($latitude2 - $latitude1);
296 $dLon = deg2rad($longitude2 - $longitude1);
297
298 $a = sin($dLat / 2) * sin($dLat / 2) + cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * sin($dLon / 2) * sin($dLon / 2);
299 $c = 2 * asin(sqrt($a));
300 $d = $earth_radius * $c;
301
302 return $d;
303 }
304}
305/**
306 * Some math functions
307 */
308class MATH
309{
310 public function PercentageNumber($percentage,$number)
311 {
312 $math_d = ($percentage / 100) * $number;
313 return $math_d;
314
315 }
316}