This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HelperClass.php
111 lines (102 loc) · 3.79 KB
/
HelperClass.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
error_reporting(1);
class HelperClass
{
function returnMySQLErrorArray($errorMsg, $errorCode)
{
$status = array();
$status[RESULT] = ERROR;
$status[MYSQL_ERROR] = $errorMsg;
$status[ERROR_NO] = $errorCode;
return $status;
}
function returnError($errorMessage)
{
$status = array();
$status[RESULT] = ERROR;
$status[MESSAGE] = $errorMessage;
return $status;
}
function returnSuccessArray($data)
{
$array = array();
$array[RESULT] = SUCCESS;
if ($data !== null)
{
$array[DATA] = $data;
}
return $array;
}
function returnTunnelError($errorMessage)
{
$array = array();
$array[RESULT] = ERROR;
$array[MESSAGE] = $errorMessage;
return $array;
}
/**
* Prints out the output to be picked up by Android in the correct format.
* If using version 1.2.0.0 or above then the output is encrypted before it is sent
* back, older versions (older versions don't post the version) revert back to
* the previous behaviour and the output is not encrypted
* @param type $postArray The array that was posted from android (includes the version)
* @param type $response The response that should be json_encoded and possibly encrypted depending on version
* @throws Exception
*/
public static function printResponseInCorrectEncodingAndCloseTunnelIfNeeded($postArray, $response)
{
require_once 'Encryption.php';
require_once 'TunnelManager.php';
$encryption = new Encryption();
//If the version is set and is version 1.2.0.0 or higher than
//print out an encrypted response
//Remove any tunnelling information if tunnel is not enabled
if ((isset($response->LocalTunnelPort) &&
empty($response->LocalTunnelPort) || $response->LocalTunnelPort === "-1") ||
$response->LocalTunnelPort === null)
{
unset($response->LocalTunnelPort);
}
if (isset($postArray["version"]) && $postArray["version"] >= "1.2.0.0")
{
print $encryption->encrypt(json_encode($response));
}
else
{
if (isset($response->data))
{
print json_encode($response->data);
}
else
{
print json_encode($response);
}
}
if (isset($response->LocalTunnelPort) && !empty($response->LocalTunnelPort) && $response->LocalTunnelPort !== "-1")
{
$tunnelManager = new TunnelManager();
$tunnelManager->closeTunnel($response->LocalTunnelPort);
}
/*else if (isset($response[TUNNEL_STATUS]) && $response[TUNNEL_STATUS][LOCAL_TUNNEL_PORT] && $response[TUNNEL_STATUS][LOCAL_TUNNEL_PORT] !== "-1")
{
$tunnelManager = new TunnelManager();
$tunnelManager->closeTunnel($response[TUNNEL_STATUS][LOCAL_TUNNEL_PORT]);
}*/
}
/**
* Return the clients IP address
* @return string
*/
public static function getIP()
{
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
}