Subversion Repositories oidplus

Rev

Rev 1162 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
557 daniel-mar 1
#!/usr/bin/env php
61 daniel-mar 2
<?php
3
 
511 daniel-mar 4
/*
5
 * OIDplus 2.0
6
 * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
 
1162 daniel-mar 21
use ViaThinkSoft\OIDplus\OIDplus;
22
 
97 daniel-mar 23
require_once __DIR__ . '/../includes/oidplus.inc.php';
1416 daniel-mar 24
require_once __DIR__ . '/ft_get_oid_data.inc.php';
61 daniel-mar 25
 
26
OIDplus::init(false);
261 daniel-mar 27
if (!OIDplus::baseConfig()->exists('OIDINFO_API_URL')) {
61 daniel-mar 28
	die("OIDINFO_API_URL not available (API is currently not public)\n");
29
}
30
 
1116 daniel-mar 31
const VERBOSE = false;
32
const DEFAULT_EMAIL = 'oidra@viathinksoft.de';
33
const ALL_OID_LIST = '/home/oidplus/all_oids.txt'; // contains all OIDs (non-public file)
34
const DESIRED_ROOT = '1.3.6.1.4.1.37476';
61 daniel-mar 35
 
36
# ---
37
 
38
exec("cat ".escapeshellarg(ALL_OID_LIST)." | sort | grep '^".DESIRED_ROOT."'", $out, $ec);
39
 
40
foreach ($out as $oid) {
41
	$oid = trim($oid);
42
	if ($oid == '') continue;
43
 
44
	check_oid($oid);
45
}
46
 
1130 daniel-mar 47
/**
48
 * @param string $oid
49
 * @return void
50
 */
51
function check_oid(string $oid) {
61 daniel-mar 52
 
261 daniel-mar 53
	$res = OIDplus::db()->query("select * from ###objects where id = ?", array("oid:$oid"));
236 daniel-mar 54
	$ok = $res->num_rows() > 0;
61 daniel-mar 55
 
56
	if (!$ok) {
57
		echo "Not existing: $oid : Adding!\n";
58
		add_oid($oid);
59
	}
60
}
61
 
62
echo "OK\n";
63
 
64
# ---
65
 
1130 daniel-mar 66
/**
67
 * @param string $oid
68
 * @param string $root
69
 * @return void
70
 */
71
function add_oid(string $oid, string $root=DESIRED_ROOT) {
61 daniel-mar 72
	if (VERBOSE) echo "Adding $oid ...\n";
73
	$data = ft_get_oid_data($oid);
74
 
75
	if (!isset($data['oid'])) {
76
		echo "Warning: Cannot gain information about OID $oid\n";
77
 
78
		$parent = ($oid == $root) ? '' : oid_up($oid);
79
		$title = '(tbd)';
80
		$description = '(tbd)';
81
		$ra_email = '';
82
		if ((DEFAULT_EMAIL != '') && ($ra_email == '')) $ra_email = DEFAULT_EMAIL;
83
 
261 daniel-mar 84
		sql_execute("insert into ###objects (id, parent, title, description, ra_email) values (?, ?, ?, ?, ?)", array("oid:$oid", "oid:$parent", $title, $description, $ra_email));
61 daniel-mar 85
 
86
		return;
87
	}
88
 
89
	if (!isset($data['oid']['identifier'])) $data['oid']['identifier'] = array();
90
	foreach ($data['oid']['identifier'] as $identifier) {
261 daniel-mar 91
		sql_execute("insert into ###asn1id (oid, name) values (?, ?)", array("oid:$oid", $identifier));
61 daniel-mar 92
	}
93
 
94
	if (!isset($data['oid']['unicode-label'])) $data['oid']['unicode-label'] = array();
95
	foreach ($data['oid']['unicode-label'] as $identifier) {
261 daniel-mar 96
		sql_execute("insert into ###iri (oid, name) values (?, ?)", array("oid:$oid", $identifier));
61 daniel-mar 97
	}
98
 
99
	$parent = ($oid == $root) ? '' : oid_up($oid);
100
 
101
	$title = strip_tags(@$data['oid']['description']);
102
	$title = html_entity_decode($title, ENT_COMPAT, 'UTF-8');
103
 
104
	$description = '<strong>' . trim(@$data['oid']['description']) . '</strong><br><br>' . nl2br(trim(@$data['oid']['information']));
105
	$description = str_replace(array("\r", "\n"), '', $description);
106
	$description = str_ireplace('<br />', '<br>', $description);
107
	$description = str_ireplace('<br/>', '<br>', $description);
108
	$description = '<p>' . str_ireplace('<br><br>', '</p><p>', $description) . '</p>';
109
 
110
	$ra_email = @$data['oid']['current_registrant']['email'];
111
	if ($ra_email == '') $ra_email = @$data['oid']['first_registrant']['email'];
112
 
261 daniel-mar 113
	$resx = OIDplus::db()->query("select ra_email from ###objects where id = ?", array("oid:$parent"));
236 daniel-mar 114
	if ($rowx = $resx->fetch_array()) $ra_email = $rowx['ra_email'];
61 daniel-mar 115
 
116
	$created = @$data['oid']['first_registrant']['creation-date'];
117
	$updated = @$data['oid']['last-modification'];
118
 
261 daniel-mar 119
	sql_execute("insert into ###objects (id, parent, title, description, ra_email, created, updated) values (?, ?, ?, ?, ?, ?, ?)",
150 daniel-mar 120
		array("oid:$oid", "oid:$parent", $title, $description, $ra_email, $created, $updated));
61 daniel-mar 121
}
122
 
1130 daniel-mar 123
/**
124
 * @param string $sql
125
 * @param array|null $prep
126
 * @return void
127
 */
128
function sql_execute(string $sql, array $prep=null) {
61 daniel-mar 129
#	echo "$sql\n";
237 daniel-mar 130
	try {
131
		OIDplus::db()->query($sql, $prep);
132
	} catch (Exception $e) {
133
		echo "Warning: " . $e->getMessage() . "\n";
134
	}
61 daniel-mar 135
}