-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.php
285 lines (243 loc) · 8.41 KB
/
lib.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/***
* author @ [email protected]
***/
<?
if ( (isset($_COOKIE['debug']) && $_COOKIE['debug'] == 'on' && $_GET['debug'] != 'off') || $_GET['debug'] == 'on' ) {
define('DEBUG', true);
error_reporting(~E_ALL ^ ~E_NOTICE);
ini_set("display_errors", 1);
$debug_messages = Array();
} else {
define('DEBUG', false);
}
if ( (isset($_COOKIE['adv']) && $_COOKIE['adv'] == 'on' && $_GET['adv'] != 'off') || $_GET['adv'] == 'on' ) {
define('ADV_EDIT',true);
} else {
define('ADV_EDIT',false);
}
if ( !file_exists('../config.php') ) {
showHead();
showMessage(false, '<b>Error:</b> Config File Not Found! Reinstall Application To Recreate The Config File');
showTail();
Die();
}
include 'config.php';
include 'constants.php';
if ( DEBUG ) {
debugLog('$_REQUEST', $_REQUEST);
debugLog('$_GET', $_GET);
debugLog('$_POST', $_POST);
debugLog('$_SERVER', $_SERVER);
}
session_start();
//make sure user is logged in:
if ( !isset($_SESSION['login_session']) && CURRENT_PAGE != 'login' ) {
header("Location: " 'login.php');
Die();
}
function shortenUrl($str) {
$maxLen = 15;
$halfLen = (int)$maxLen - 4 / 2;
$str = str_ireplace('http://', '', $str);
$str = str_ireplace('https://', '', $str);
$str = str_ireplace('www.', '', $str);
if ( strlen($str) > $maxLen ) {
$str = substr($str, 0, $halfLen) . '....' . substr($str, strlen($str) - $halfLen, $halfLen);
}
return $str;
}
function showHead() {
header("X-Robots-Tag: noindex nofollow");
include_once('head.php');
printStatus();
}
function showTail($callback = null) {
include_once('tail.php');
}
function showMessage($status, $msg) {
global $statusMsgs;
$statusMsgs[] = Array('status' => $status, 'msg' => $msg);
}
function printStatus() {
global $statusMsgs;
if ( count($statusMsgs) > 0 ) {
foreach ( $statusMsgs as $msg ) {
$typeClass = $msg['status'] ? 'confirm' : 'error';
$icon = $msg['status'] ? ICON_OK : ICON_ERR;
?>
<div class="message <?= $typeClass ?>">
<img src="<?= $icon ?>" class="icon"><?= $msg['msg'] ?>
</div>
<?
}
}
}
function debugLog($msg, $var) {
global $debug_messages;
$debug_messages[] = Array(
'msg' => $msg,
'var' => $var
);
}
//Compare two sets of versions, where major/minor/etc. releases are separated by dots.
//Returns 0 if both are equal, 1 if A > B, and -1 if B < A.
function version_compare2($a, $b) {
$a = explode(".", rtrim($a, ".0")); //Split version into pieces and remove trailing .0
$b = explode(".", rtrim($b, ".0")); //Split version into pieces and remove trailing .0
foreach ( $a as $depth => $aVal )
{ //Iterate over each piece of A
if ( isset($b[$depth]) ) { //If B matches A to this depth, compare the values
if ( $aVal > $b[$depth] ) return 1; //Return A > B
else if ( $aVal < $b[$depth] ) return -1; //Return B > A
//An equal result is inconclusive at this point
}
else
{ //If B does not match A to this depth, then A comes after B in sort order
return 1; //so return A > B
}
}
//At this point, we know that to the depth that A and B extend to, they are equivalent.
//Either the loop ended because A is shorter than B, or both are equal.
return (count($a) < count($b)) ? -1 : 0;
}
function api_info() {
global $menu, $curl_config;
//GET INFO FROM WEBSERVICE
$fn = 'API URL HERE ?api_key=' . APIKEY . '&api_secret=' . APISECRET . '&format='json';
$ch = curl_init();
$curl_config[CURLOPT_URL] = $fn;
curl_setopt_array($ch, $curl_config);
$data = json_decode(curl_exec($ch), true);
$info = curl_getinfo($ch);
$curl_error = curl_errno($ch) === 0 ? 'No errors' : 'Curl Error Reading Stats from API: ' . curl_errno($ch) . ' > ' . curl_error($ch);
curl_close($ch);
if ( $curl_error !== 'No errors' || $data === false || empty($data) ) {
//curl returned error
debugLog('Json Failed. Response: ', array('response data' => $data, 'curl info' => $info));
writeLog('[ERROR] Failed To Access Webservice: ' . $curl_error);
showMessage(false, $curl_error);
return Array();
} else {
//ok data returned
debugLog('API stats.php Response', array('response data' => $data, 'curl info' => $info));
if ( $data['result'] === false || isset($data['error']) ) {
$msg = implode("\n", $data['error']);
showMessage(false, "Unable to access the noIPfraud webservice due to the following errors:\n$msg");
writeLog("[ERROR] Unable to access the noIPfraud webservice due to the following errors:\n$msg");
}
//check version
if ( version_compare($data['version'], CLIENT_VERSION) == 1 ) {
showMessage(false, 'You are running an outdated Client (version ' . CLIENT_VERSION . '). Please <a href="' . $menu['support']['url'] . '">download & install</a> the latest version (' . $data[version] . ').');
}
//account active
if ( isset($data['account']['active']) && $data['account']['active'] == 'No' ) {
showMessage(false, 'Your account has been disabled! <a href="' . $menu['support']['url'] . '">Please contact support</a> to reactivate your account.');
}
//sufficient click balance
if ( isset($data['account']['credits']) ) {
if ( $data['account']['credits'] <= 0 ) {
showMessage(false, 'You have run out of credits! <a href="' . $menu['support']['url'] . '">Please topup</a>.');
} elseif ( $data['account']['credits'] <= $data['account']['lastday'] * 5 ) {
$daysleft = round($data['account']['credits'] / $data['account']['lastday'], 1);
showMessage(false, 'You are running low on click balance! At your current volume you will run out within ' . $daysleft . ' days. <a href="' . $menu['support']['url'] . '">Please topup</a>.');
}
}
return $data;
}
}
function check_webservice() {
global $curl_config;
//GET INFO FROM WEBSERVICE
$fn = 'API URL HERE' . APIKEY . '&api_secret=' . APISECRET . '&a=check'. '&format='json';
$ch = curl_init();
$curl_config[CURLOPT_URL] = $fn;
curl_setopt_array($ch, $curl_config);
$data = json_decode(curl_exec($ch), true);
$info = curl_getinfo($ch);
$curl_error = curl_errno($ch) === 0 ? 'No errors' : 'Could not access webservice. Curl Error Checking Webserice Availability: ' . curl_errno($ch) . ' > ' . curl_error($ch);
curl_close($ch);
if ( $curl_error != 'No errors' || $data === false || empty($data) || $data['result'] === false ) {
//curl returned error
debugLog('Json Failed. Response: ', array('response data' => $data, 'curl info' => $info));
showMessage(false, 'Webservice Check Failed. Please check your logs for more info.');
writeLog('[ERROR] Webservice validation call:' . $fn);
return false;
} else {
debugLog('API Check Response: ', $data);
showMessage(true, "WebService Operational. Current Server Time: " . $data['timestamp']);
return true;
}
}
function writeLog($msg) {
$fn = LOC_LOG . date('Ymd') . '.log';
$fh = @fopen($fn, 'a');
if ( $fh ) {
fwrite($fh, date('Ymd H:i:s') . ' > ' . $msg . "\n");
fclose($fh);
}
}
function getIP() {
if ( isset($_SERVER['HTTP_CLIENT_IP']) ) {
//check ip from share internet
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif ( isset($_SERVER['HTTP_X_FORWARDED_FOR']) ) {
//to check ip is pass from proxy
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
$ip_array = explode(',', $ip);
return $ip_array[0];
}
function timedRedir($url, $sec) {
define('TIMEDREDIR_URL', $url);
define('TIMEDREDIR_S', $sec);
}
function randomAlphaNum($length) {
$rangeMin = pow(36, $length - 1); //smallest number to give length digits in base 36
$rangeMax = pow(36, $length) - 1; //largest number to give length digits in base 36
$base10Rand = mt_rand($rangeMin, $rangeMax); //get the random number
$newRand = base_convert($base10Rand, 10, 36); //convert it
return $newRand; //spit it out
}
function getCampaign($clid) {
$fn = getCmpName($clid);
if ( file_exists($fn) ) {
include $fn;
return $camp;
} else {
return false;
}
}
function getCmpName($clid) {
return LOC_CAMP . $clid . '.cmp.php';
}
function getVars($url, $parse = true) {
$u = parse_url($url);
$vars = array();
if ( isset($u['query']) ) {
$query = explode('&', $u['query']);
foreach ( $query as $q ) {
if ( preg_match('!\[{2}.*\]{2}!', $q) === 1 ) {
$data = explode('=', $q);
if ( $parse ) {
$data[1] = preg_replace('!\[|\]!', '', $data[1]);
$vars[$data[1]] = strtoupper($data[0]);
} else {
$vars[$data[0]] = $data[1];
}
}
}
}
if ( preg_match_all('!\[\[(.*?)\]\]!', $u['path'], $m) !== false ) {
foreach ( $m[1] as $data ) {
if ( $patch ) {
$vars[$data] = strtoupper($data);
} else {
$vars[$data] = "[[$data]]";
}
}
}
return $vars;
}
?>