forked from phpLicenseWatcher/phpLicenseWatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.php
More file actions
417 lines (304 loc) · 12 KB
/
tools.php
File metadata and controls
417 lines (304 loc) · 12 KB
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
<?php
################################################################################################
# This build
################################################################################################
function build_license_expiration_array($lmutil_loc, $server, &$expiration_array) {
$total_licenses = 0;
$file = popen($lmutil_loc . " lmcksum -c " . $server,"r");
$today = time();
# Let's read in the file line by line
while (!feof ($file)) {
$line = fgets ($file, 1024);
if ( preg_match("/INCREMENT .*/i", $line, $out ) || preg_match("/FEATURE .*/i", $line, $out ) ) {
$license = explode(" ", $out[0]);
if ( $license[4] ) {
# UNIX time stamps go only till year 2038 (or so) so convert
# any license dates 9999 or 0000 (which means infinity) to
# an acceptable year. 2036 seems like a good number
$license[4] = strtolower($license[4]);
$license[4] = str_replace("-9999", "-2036", $license[4]);
$license[4] = str_replace("-0000", "-2036", $license[4]);
$license[4] = str_replace("-2099", "-2036", $license[4]);
$license[4] = str_replace("-2242", "-2036", $license[4]);
$license[4] = str_replace("-jan-00", "-jan-2036", $license[4]);
$license[4] = str_replace("-jan-0", "-jan-2036", $license[4]);
$license[4] = str_replace("-0", "-2036", $license[4]);
$license[4] = str_replace("permanent", "05-jul-2036", $license[4]);
$unixdate2 = strtotime($license[4]);
# Convert the date you got into UNIX time
$unixdate2 = strtotime($license[4]);
}
$days_to_expiration = ceil ((1 + strtotime($license[4]) - $today) / 86400);
##############################################################################
# If there is more than 4000 days = 10 years+ until expiration I will
# consider the license to be permanent
##############################################################################
if ( $days_to_expiration > 4000 ) {
$license[4] = "permanent";
# $days_to_expiration = "permanent";
}
##############################################################################
# Add to the expiration array
$expiration_array[$license[1]][] = array(
"vendor_daemon" => $license[2],
"expiration_date" => $license[4],
"num_licenses" => $license[5],
"days_to_expiration" => (int) $days_to_expiration );
}
}
#echo("<pre>");
#print_r($expiration_array);
pclose($file);
return 1;
}
#################################################################
# Compare two dates
# $Date$
# Returns: 1 if date1 greater than date2
# 0 if date1 equal to date2
# -1 if date less than date2
#################################################################
function compare_dates ($date1, $date2) {
# Convert any "english" looking date into UNIX time stamp
$unixdate1 = strtotime($date1);
$unixdate2 = strtotime($date2);
if ($unixdate1 > $unixdate2) {
return 1;
}
elseif ($unixdate1 < $unxidate2) {
return -1;
}
elseif ($unixdate1 == $unxidate2) {
return 0;
}
}
#---------------------------------------------------------------------
#This function will convert a MySQL date ie. 2001-05-20 to the
#US date format ie. 05/20/2001
#---------------------------------------------------------------------
function convert_from_mysql_date($date) {
$stringArray = explode("-", $string);
$date = mktime(0,0,0,$stringArray[1],$stringArray[2],$stringArray[0]);
return date("m/d/Y", $date);
}
function convert_to_mysql_date($date) {
$stringArray = explode("/", $date);
$date = mktime(0,0,0,$stringArray[0],$stringArray[1],$stringArray[2]);
return date("Y-m-d", $date);
}
function build_select_box ($array, $name, $checked_val="xzxz") {
/*
Takes a result set, with the first column being the "id" or value
and the second column being the text you want displayed
The second parameter is the name you want assigned to this form element
The third parameter is optional. Pass the value of the item that should
be checked
*/
echo '<select name="'.$name.'">';
for ($i=0; $i<sizeof($array); $i++) {
echo '
<option value="'.$array[$i].'"';
if ($array[$i] == $checked_val) {
echo ' selected';
}
echo '>'.$array[$i].'</option>';
}
echo '
</select>';
}
##################################################################
# This function will get the number of available licenses for a particular feature
# Arguments: Feature as a String
##################################################################
function num_licenses_available($myfeature) {
global $servers, $lmutil_loc;
$LICENSE_FILE="";
# Build LM_LICENSE_FILE
for ( $i = 0 ; $i < sizeof($servers); $i++ )
$LICENSE_FILE .= $servers[$i] . ":";
$fp = popen($lmutil_loc . " lmstat -f " . $myfeature . " -c " . $LICENSE_FILE, "r");
while ( !feof ($fp) ) {
$line = fgets ($fp, 1024);
# Look for features in the output. You will see stuff like
# Users of Allegro_Viewer: (Total of 5 licenses available
if ( preg_match("/^Users of/i", $line ) ) {
$out = explode(" ", $line);
pclose($fp);
# Return the number
return $out[6];
}
}
}
##################################################################
# This function will get the number of used licenses for a particular feature
# Arguments: Feature as a String
##################################################################
function num_licenses_used($myfeature) {
global $servers, $lmstat_loc;
$LICENSE_FILE="";
# Build LM_LICENSE_FILE
for ( $i = 0 ; $i < sizeof($servers); $i++ )
$LICENSE_FILE .= $servers[$i] . ":";
$fp = popen($lmstat_loc . " -f " . $myfeature . " -c " . $LICENSE_FILE, "r");
$num_licenses = 0;
while ( !feof ($fp) ) {
$line = fgets ($fp, 1024);
# Look for features in the output. You will see stuff like
# Users of Allegro_Viewer: (Total of 5 licenses available
if ( preg_match("/, start/i", $line ) )
$num_licenses++;
}
pclose($fp);
return $num_licenses;
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# B00zy's timespan script v1.2 #
# #
# timespan -- get the exact time span between any two moments in time. #
# #
# Description: #
# #
# class timespan, function calc ( int timestamp1, int timestamp2) #
# #
# The purpose of this script is to be able to return the time span #
# between any two specific moments in time AFTER the Unix Epoch #
# (January 1 1970) in a human-readable format. You could, for example, #
# determine your age, how long you have been married, or the last time #
# you... you know. ;) #
# #
# The class, "timespan", will produce variables within the class #
# respectively titled years, months, weeks, days, hours, minutes, #
# seconds. #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# Example 1. B00zy's age. #
# #
# $t = new timespan( time(), mktime(0,13,0,8,28,1982)); #
# print "B00zy is $t->years years, $t->months months, ". #
# "$t->days days, $t->hours hours, $t->minutes minutes, ". #
# "and $t->seconds seconds old.\n"; #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
define('day', 60*60*24 );
define('hour', 60*60 );
define('minute', 60 );
class timespan
{
var $years;
var $months;
var $weeks;
var $days;
var $hours;
var $minutes;
var $seconds;
function leap($time)
{
if (date('L',$time) and (date('z',$time) > 58))
return (double)(60*60*24*366);
else
{
$de = getdate($time);
$mkt = mktime(0,0,0,$de['mon'],$de['mday'],($de['year'] - 1));
if ((date('z',$time) <= 58) and date('L',$mkt))
return (double)(60*60*24*366);
else
return (double)(60*60*24*365);
}
}
function readable()
{
$values = array('years','months','weeks','days','hours','minutes','seconds');
foreach ($values as $k => $v)
if ($this->{$v}) $fmt .= ( $fmt? ', ': '') . $this->{$v} . " $v";
return $fmt . ( $fmt? '.': '') ;
}
function timespan($after,$before)
{
# Set variables to zero, instead of null.
$this->years = 0;
$this->months = 0;
$this->weeks = 0;
$this->days = 0;
$this->hours = 0;
$this->minutes = 0;
$this->seconds = 0;
$duration = $after - $before;
# 1. Number of years
$dec = $after;
$year = $this->leap($dec);
while (floor($duration / $year) >= 1)
{
# We don't need this VV
#print date("F j, Y\n",$dec);
$this->years += 1;
$duration -= (int)$year;
$dec -= (int)$year;
$year = $this->leap($dec);
}
# 2. Number of months
$dec = $after;
$m = date('n',$after);
$d = date('j',$after);
while (($duration - day) >= 0)
{
$duration -= day;
$dec -= day;
$this->days += 1;
if ( (date('n',$dec) != $m) and (date('j',$dec) <= $d) )
{
$m = date('n',$dec);
$d = date('j',$dec);
$this->months += 1;
$this->days = 0;
}
}
# 3. Number of weeks.
$this->weeks = floor($this->days / 7);
$this->days %= 7;
# 4. Number of hours, minutes, and seconds.
$this->hours = floor($duration / (60*60));
$duration %= (60*60);
$this->minutes = floor($duration / 60);
$duration %= 60;
$this->seconds = $duration;
}
}
function run_command( $command ){
global $lmutil_loc;
$data = "";
if( !cache_check($command, $data) ){
$fp = popen( $command , "r");
$data = "";
while ( !feof ($fp) ) {
$data .= fgets ($fp, 1024);
}
pclose($fp);
cache_store( $command , $data );
}
return $data;
}
function cache_check( $command , &$data ){
$result = false;
$cache_dir = '/tmp/';
$hash = md5( $command );
$cacheFile = $cache_dir . $hash ;
if( file_exists( $cacheFile ) ){
if (time()-filemtime($cacheFile) > 2 * 3600) {
// file older than 2 hours
} else {
// file younger than 2 hours
$data = file_get_contents($cacheFile);
$result = true;
}
}
return $result;
}
function cache_store( $command , $data ){
$cache_dir = '/tmp/';
$hash = md5( $command );
$cacheFile = $cache_dir . $hash ;
file_put_contents($cacheFile, $data );
}
?>