forked from phpLicenseWatcher/phpLicenseWatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
license_cache.php
53 lines (45 loc) · 1.46 KB
/
license_cache.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
<?php
require_once __DIR__ . "/common.php";
require_once __DIR__ . "/lmtools.php";
db_connect($db);
$sql = <<<SQL
INSERT IGNORE INTO `available` (`license_id`, `date`, `num_licenses`)
SELECT `licenses`.`id`, NOW(), ? FROM `licenses`
JOIN `servers` ON `licenses`.`server_id`=`servers`.`id`
JOIN `features` ON `licenses`.`feature_id`=`features`.`id`
WHERE `servers`.`name`=? AND `features`.`name`=?;
SQL;
$query = $db->prepare($sql);
$servers = db_get_servers($db, array('name', 'license_manager'));
$lmtools = new lmtools();
$db->begin_transaction(MYSQLI_TRANS_START_READ_WRITE);
foreach ($servers as $server) {
if ($lmtools->lm_open($server['license_manager'], 'license_cache', $server['name']) === false) {
$db->rollback();
fprintf(STDERR, "%s\n", $lmtools->err);
exit(1);
}
$lmdata = $lmtools->lm_nextline();
if ($lmdata === false) {
$db->rollback();
fprintf(STDERR, "%s\n", $lmtools->err);
exit(1);
}
while (!is_null($lmdata)) {
$query->bind_param("iss", $lmdata['num_licenses'], $server['name'], $lmdata['feature']);
if ($query->execute() === false) {
$db->rollback();
fprintf(STDERR, "MySQL: %s\n", $query->error);
exit(1);
}
$lmdata = $lmtools->lm_nextline();
if ($lmdata === false) {
$db->rollback();
fprintf(STDERR, "%s\n", $lmtools->err);
exit(1);
}
}
}
$db->commit();
$db->close();
?>