-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc_misc.php
448 lines (371 loc) · 14.7 KB
/
func_misc.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<?php
# @package hubzero-metrics
# @file func_misc.php
# @author Swaroop Samek <[email protected]>
# @copyright Copyright (c) 2011-2015 HUBzero Foundation, LLC.
# @license http://www.gnu.org/licenses/lgpl-3.0.html LGPLv3
#
# Copyright (c) 2011-2015 HUBzero Foundation, LLC.
#
# This file is part of: The HUBzero(R) Platform for Scientific Collaboration
#
# The HUBzero(R) Platform for Scientific Collaboration (HUBzero) is free
# software: you can redistribute it and/or modify it under the terms of
# the GNU Lesser General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# HUBzero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# HUBzero is a registered trademark of HUBzero Foundation, LLC.
#
function get_tool_versions_aliases(&$db_hub, $aliases_x) {
global $hub_db, $db_prefix;
if ($aliases_x) {
$aliases = $aliases_x.',';
$sql = 'SELECT DISTINCT instance FROM '.$hub_db.'.'.$db_prefix.'tool_version WHERE toolname IN ('.$aliases_x.') AND instance NOT LIKE "%\_dev"';
$result = mysql_query($sql, $db_hub);
if($result) {
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_row($result)) {
$aliases .= '"'.$row[0].'",';
}
}
} else {
$msg = mysql_error($db_hub).' while executing '.$sql.n;
clean_exit($msg);
}
$aliases_x = rtrim ($aliases,',');
}
/* not needed as HUBs don't have jos_tool_version_alias tables */
if ($aliases_x) {
$sql = 'SELECT DISTINCT tva.alias FROM '.$hub_db.'.'.$db_prefix.'tool_version_alias AS tva, '.$hub_db.'.'.$db_prefix.'tool_version AS tv WHERE tva.tool_version_id = tv.id AND tv.toolname IN ('.$aliases_x.') AND tva.alias NOT LIKE "%\_dev"';
$result = mysql_query($sql, $db_hub);
if($result) {
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_row($result)) {
$aliases .= '"'.$row[0].'",';
}
}
} else {
$msg = mysql_error($db_hub).' while executing '.$sql.n;
clean_exit($msg);
}
$aliases_x = rtrim ($aliases,',');
}
if ($aliases_x) {
$sql = 'SELECT DISTINCT instance FROM '.$hub_db.'.'.$db_prefix.'tool_version WHERE toolname IN ('.$aliases_x.') AND instance NOT LIKE "%\_dev"';
$result = mysql_query($sql, $db_hub);
if($result) {
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_row($result)) {
$aliases .= '"'.$row[0].'",';
}
}
} else {
$msg = mysql_error($db_hub).' while executing '.$sql.n;
clean_exit($msg);
}
$aliases_x = rtrim ($aliases,',');
}
return $aliases_x;
}
# --------------------------------------------------------------------------------------------
# Execute sql function used for SELECT statements with single returns (for $db_hub only)
function mysql_fetch(&$db_hub, $sql) {
global $debug;
$val = '';
if (!mysql_ping($db_hub))
$db_hub = db_connect('db_hub');
if ($debug)
print $sql.n;
$result = mysql_query($sql, $db_hub);
if($result) {
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_row($result)) {
$val = $row[0];
}
}
} else {
$msg = mysql_error($db_hub).' while executing '.$sql.n;
clean_exit($msg);
}
return $val;
}
# --------------------------------------------------------------------------------------------
# Execute sql function used for INSERTs, DELETEs and UPDATEs
function mysql_exec($db, $sql) {
$result = mysql_query($sql, $db);
if(!$result) {
$msg = mysql_error($db).' while executing '.$sql.n;
clean_exit($msg);
}
}
# --------------------------------------------------------------------------------------------
function get_dates($dthis_, $period) {
$dt_pattern_1 = '/^(\d{4})-(\d{2})-(\d{2})$/';
$dt_pattern_2 = '/^(\d{4})-(\d{2})$/';
if ( (preg_match($dt_pattern_1, $dthis_, $matches) <> 0) && ($matches[3] <> '00') ) {
$dthis = $matches[1].'-'.$matches[2];
$dates = get_dates_for_period($dthis, $period);
} else if (preg_match($dt_pattern_2, $dthis_, $matches) <> 0) {
$dates = get_dates_for_period($matches[0], $period);
} else {
$msg = 'Invalid Date'.n;
clean_exit($msg);
}
$dates['dthis'] = $matches[1]."-".$matches[2].'-00';
return $dates;
}
# --------------------------------------------------------------------------------------------
function dateformat_plot($seldate) {
$year = substr($seldate, 0, 4);
$month = substr($seldate, 5, 2);
$day = substr($seldate, 8, 2);
if($day > 0) {
return(sprintf("%02d/%02d/%04d", $month, $day, $year));
}
else {
return(sprintf("%02d/%04d", $month, $year));
}
}
// --------------------------------------------------------------
function get_dates_for_period($dthis, $period) {
$givendate = $dthis.'-01';
$cd = strtotime($givendate);
$d_month = date('m', $cd);
$d_year = date('Y', $cd);
switch ($period) {
// Calendar Year
case 0:
$date1 = date('Y', mktime(date('h',$cd),date('i',$cd), date('s',$cd), date('m',$cd), date('d',$cd), date('Y',$cd))).'-01-01';
$date2 = date('Y-m', mktime(date('h',$cd),date('i',$cd), date('s',$cd), date('m',$cd)+1, date('d',$cd), date('Y',$cd))).'-01';
$dates = array("start"=>$date1,"stop"=>$date2);
break;
// Month
case 1:
$date1 = date('Y-m', mktime(date('h',$cd),date('i',$cd), date('s',$cd), date('m',$cd), date('d',$cd), date('Y',$cd))).'-01';
$date2 = date('Y-m', mktime(date('h',$cd),date('i',$cd), date('s',$cd), date('m',$cd)+1, date('d',$cd), date('Y',$cd))).'-01';
$dates = array("start"=>$date1,"stop"=>$date2);
break;
// Quarter
case 3:
if ($d_month >= 1 && $d_month <=3) {
$date1 = $d_year."-01-01";
} else if ($d_month >= 4 && $d_month <=6) {
$date1 = $d_year."-04-01";
} else if ($d_month >= 7 && $d_month <=9) {
$date1 = $d_year."-07-01";
} else if ($d_month >= 10 && $d_month <=12) {
$date1 = $d_year."-10-01";
}
$date2 = date('Y-m', mktime(date('h',$cd),date('i',$cd), date('s',$cd), date('m',$cd)+1, date('d',$cd), date('Y',$cd))).'-01';
$dates = array("start"=>$date1,"stop"=>$date2);
break;
// 12 months
case 12:
$date1 = date('Y-m', mktime(date('h',$cd),date('i',$cd), date('s',$cd), date('m',$cd)-11, date('d',$cd), date('Y',$cd))).'-01';
$date2 = date('Y-m', mktime(date('h',$cd),date('i',$cd), date('s',$cd), date('m',$cd)+1, date('d',$cd), date('Y',$cd))).'-01';
$dates = array("start"=>$date1,"stop"=>$date2);
break;
// Fiscal Year (Oct - Sep)
case 13:
$date2 = date('Y-m', mktime(date('h',$cd),date('i',$cd), date('s',$cd), date('m',$cd)+1, date('d',$cd), date('Y',$cd))).'-01';
if ($d_month >= 10) {
$date1 = $d_year.'-10-01';
} else {
$date1 = ($d_year-1).'-10-01';
}
$dates = array("start"=>$date1,"stop"=>$date2);
break;
// Overall Time period
case 14:
$date1 = "1995-01-01";
$date2 = date('Y-m', mktime(date('h',$cd),date('i',$cd), date('s',$cd), date('m',$cd)+1, date('d',$cd), date('Y',$cd))).'-01';
$dates = array("start"=>$date1,"stop"=>$date2);
break;
default:
$msg = 'Invalid Period '.$period.n;
clean_exit($msg);
}
return $dates;
}
function xgethostbyaddr($ip, $timeout = 1)
{
$cmd = "/bin/bash -c \"/usr/bin/host -W $timeout $ip 2>/dev/null\"";
$output = shell_exec($cmd);
if (preg_match('/.*pointer ([A-Za-z0-9.-]+)\..*/',$output,$regs))
return $regs[1];
return $ip;
}
function dbquote($str) {
global $db_hub;
return '"' . mysql_real_escape_string($str, $db_hub) . '"';
}
function get_countries(&$db_hub, $sql) {
if (!mysql_ping($db_hub))
$db_hub = db_connect('db_hub');
$countries = "";
$result = mysql_query($sql, $db_hub);
if($result) {
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_row($result)) {
$countries .= '"'.$row[0].'",';
}
}
} else {
$msg = mysql_error($db_hub).' while executing '.$sql.n;
clean_exit($msg);
}
$countries = rtrim($countries,',');
return $countries;
}
function get_rappture_tools() {
$rappture_tools = '"workspace",';
$cmd = "/bin/bash -c \"find /apps -name 'tool.xml' 2>/dev/null\"";
$cmd_res = shell_exec($cmd);
if ($cmd_res) {
$applines = explode("\n", $cmd_res);
$applines = array_filter($applines);
$apps = array();
foreach ($applines as $appline) {
$tmp = explode("/",$appline);
if ($tmp[2])
array_push($apps, $tmp[2]);
}
$apps = array_unique($apps);
foreach ($apps as $app)
$rappture_tools .= '"'.$app.'",';
$rappture_tools = rtrim($rappture_tools, ',');
}
return $rappture_tools;
}
function get_ip_list(&$db_hub, $sql) {
$login_ips = '"127.0.0.1",';
$result = mysql_query($sql, $db_hub);
if($result) {
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_row($result)) {
$login_ips .= '"'.$row[0].'",';
}
}
} else {
$msg = mysql_error($db_hub).' while executing '.$sql.n;
clean_exit($msg);
}
$login_ips = rtrim($login_ips,',');
return $login_ips;
}
function search_array($needle, $haystack)
{
if (is_array($haystack)) {
foreach ($haystack as $hay) {
if (stripos($needle, $hay) !== false) {
return true;
}
}
}
return false;
}
function gen_exclude_list($type) {
global $metrics_db, $db_hub;
if ($type == "ip")
$arr = array("127.0.0.1");
if ($type == "url")
$arr = array("task=diskusage");
if ($type == "useragent")
$arr = array("gsa-purdue-crawler");
$arr = array();
$sql = 'SELECT filter FROM '.$metrics_db.'.exclude_list WHERE type = '.dbquote($type);
$result = mysql_query($sql, $db_hub);
if($result) {
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_row($result)) {
array_push($arr, $row[0]);
}
}
} else {
$msg = mysql_error($db_hub).' while executing '.$sql.n;
clean_exit($msg);
}
return $arr;
}
function checkbot (&$db_hub, $useragent) {
global $metrics_db;
$bot = 0;
$sql = 'SELECT COUNT(*) FROM '.$metrics_db.'.bot_useragents WHERE useragent = '.dbquote($useragent);
$bot = mysql_fetch($db_hub, $sql);
return $bot;
}
function get_ip_geodata($hubzero_ipgeo_url, $hub_key, $n_ip) {
global $hub_db, $db_hub, $db_prefix;
$geo_data = array();
$geo_data['n_ip'] = $n_ip;
$geo_data['countrySHORT'] = '-';
$geo_data['countryLONG'] = '-';
$geo_data['ipREGION'] = '-';
$geo_data['ipCITY'] = '-';
$geo_data['ipLATITUDE'] = '-';
$geo_data['ipLONGITUDE'] = '-';
$local_exists = 0;
if (!is_numeric($n_ip))
return $geo_data;
$sql = 'SELECT COUNT(*) FROM '.$hub_db.'.'.$db_prefix.'metrics_ipgeo_cache WHERE ip = '.dbquote($n_ip).' AND TO_DAYS(CURDATE())-TO_DAYS(lookup_datetime) <= 90';
$local_exists = mysql_fetch($db_hub, $sql);
if ($local_exists) {
$sql = 'SELECT * FROM '.$hub_db.'.'.$db_prefix.'metrics_ipgeo_cache WHERE ip = '.dbquote($n_ip).' AND TO_DAYS(CURDATE())-TO_DAYS(lookup_datetime) <= 90';
$result = mysql_query($sql, $db_hub);
if($result) {
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_assoc($result)) {
$geo_data['n_ip'] = $n_ip;
$geo_data['countrySHORT'] = $row[1];
$geo_data['countryLONG'] = $row[2];
$geo_data['ipREGION'] = $row[3];
$geo_data['ipCITY'] = $row[4];
$geo_data['ipLATITUDE'] = $row[5];
$geo_data['ipLONGITUDE'] = $row[6];
return $geo_data;
}
}
} else {
$msg = mysql_error($db_hub).' while executing '.$sql.n;
clean_exit($msg);
}
} else {
$url = $hubzero_ipgeo_url.'/?&hub_key='.$hub_key.'&n_ip='.$n_ip;
$xml = @simplexml_load_file($url);
if (!$xml)
print 'Warning: Could not connect to remote ip-location lookup webservice on '.$hubzero_ipgeo_url.n;
if ( ($xml->status == "_SUCCESS_") && ($n_ip == $xml->ipset->n_ip) ) {
$geo_data['n_ip'] = $xml->ipset->n_ip;
$geo_data['countrySHORT'] = $xml->ipset->countryCode;
$geo_data['countryLONG'] = $xml->ipset->countryName;
$geo_data['ipREGION'] = $xml->ipset->region;
$geo_data['ipCITY'] = $xml->ipset->city;
$geo_data['ipLATITUDE'] = $xml->ipset->lat;
$geo_data['ipLONGITUDE'] = $xml->ipset->long;
if ($geo_data['countrySHORT'] <> '-') {
$sql_ins = 'INSERT INTO '.$hub_db.'.'.$db_prefix.'metrics_ipgeo_cache (ip, countrySHORT, countryLONG, ipREGION, ipCITY, ipLATITUDE, ipLONGITUDE) VALUES ('.dbquote($geo_data['n_ip']).','.dbquote($geo_data['countrySHORT']).','.dbquote($geo_data['countryLONG']).','.dbquote($geo_data['ipREGION']).','.dbquote($geo_data['ipCITY']).','.dbquote($geo_data['ipLATITUDE']).','.dbquote($geo_data['ipLONGITUDE']).') ON DUPLICATE KEY UPDATE countrySHORT = '.dbquote($geo_data['countrySHORT']).', countryLONG = '.dbquote($geo_data['countryLONG']).', ipREGION = '.dbquote($geo_data['ipREGION']).', ipCITY = '.dbquote($geo_data['ipCITY']).', ipLATITUDE = '.dbquote($geo_data['ipLATITUDE']).', ipLONGITUDE = '.dbquote($geo_data['ipLONGITUDE']);
mysql_exec($db_hub, $sql_ins);
}
} else if ( $xml->status == "_INVALID_KEY_OR_KEY-HUB_HOSTNAME_MISMATCH_" ) {
print 'Warning: HUBzero.org IP-Geo location key is invalid. Please check hubconfiguration.php for "$hubzero_ipgeo_key". Please submit a support ticket on hubzero.org if the problem persists.'.n;
}
}
return $geo_data;
}
function clean_exit($msg="") {
global $db_hub;
print $msg;
if (isset($db_hub) && mysql_ping($db_hub))
db_close($db_hub);
die;
}
?>