Subversion Repositories oidplus

Rev

Rev 1116 | Rev 1162 | Go to most recent revision | 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
 
97 daniel-mar 21
require_once __DIR__ . '/../includes/oidplus.inc.php';
61 daniel-mar 22
 
23
OIDplus::init(false);
261 daniel-mar 24
if (!OIDplus::baseConfig()->exists('OIDINFO_API_URL')) {
61 daniel-mar 25
	die("OIDINFO_API_URL not available (API is currently not public)\n");
26
}
27
 
1116 daniel-mar 28
const VERBOSE = false;
29
const DEFAULT_EMAIL = 'oidra@viathinksoft.de';
30
const ALL_OID_LIST = '/home/oidplus/all_oids.txt'; // contains all OIDs (non-public file)
31
const DESIRED_ROOT = '1.3.6.1.4.1.37476';
61 daniel-mar 32
 
33
# ---
34
 
35
exec("cat ".escapeshellarg(ALL_OID_LIST)." | sort | grep '^".DESIRED_ROOT."'", $out, $ec);
36
 
37
foreach ($out as $oid) {
38
	$oid = trim($oid);
39
	if ($oid == '') continue;
40
 
41
	check_oid($oid);
42
}
43
 
1130 daniel-mar 44
/**
45
 * @param string $oid
46
 * @return void
47
 */
48
function check_oid(string $oid) {
61 daniel-mar 49
 
261 daniel-mar 50
	$res = OIDplus::db()->query("select * from ###objects where id = ?", array("oid:$oid"));
236 daniel-mar 51
	$ok = $res->num_rows() > 0;
61 daniel-mar 52
 
53
	if (!$ok) {
54
		echo "Not existing: $oid : Adding!\n";
55
		add_oid($oid);
56
	}
57
}
58
 
59
echo "OK\n";
60
 
61
# ---
62
 
1130 daniel-mar 63
/**
64
 * @param string $oid
65
 * @param string $root
66
 * @return void
67
 */
68
function add_oid(string $oid, string $root=DESIRED_ROOT) {
61 daniel-mar 69
	if (VERBOSE) echo "Adding $oid ...\n";
70
	$data = ft_get_oid_data($oid);
71
 
72
	if (!isset($data['oid'])) {
73
		echo "Warning: Cannot gain information about OID $oid\n";
74
 
75
		$parent = ($oid == $root) ? '' : oid_up($oid);
76
		$title = '(tbd)';
77
		$description = '(tbd)';
78
		$ra_email = '';
79
		if ((DEFAULT_EMAIL != '') && ($ra_email == '')) $ra_email = DEFAULT_EMAIL;
80
 
261 daniel-mar 81
		sql_execute("insert into ###objects (id, parent, title, description, ra_email) values (?, ?, ?, ?, ?)", array("oid:$oid", "oid:$parent", $title, $description, $ra_email));
61 daniel-mar 82
 
83
		return;
84
	}
85
 
86
	if (!isset($data['oid']['identifier'])) $data['oid']['identifier'] = array();
87
	foreach ($data['oid']['identifier'] as $identifier) {
261 daniel-mar 88
		sql_execute("insert into ###asn1id (oid, name) values (?, ?)", array("oid:$oid", $identifier));
61 daniel-mar 89
	}
90
 
91
	if (!isset($data['oid']['unicode-label'])) $data['oid']['unicode-label'] = array();
92
	foreach ($data['oid']['unicode-label'] as $identifier) {
261 daniel-mar 93
		sql_execute("insert into ###iri (oid, name) values (?, ?)", array("oid:$oid", $identifier));
61 daniel-mar 94
	}
95
 
96
	$parent = ($oid == $root) ? '' : oid_up($oid);
97
 
98
	$title = strip_tags(@$data['oid']['description']);
99
	$title = html_entity_decode($title, ENT_COMPAT, 'UTF-8');
100
 
101
	$description = '<strong>' . trim(@$data['oid']['description']) . '</strong><br><br>' . nl2br(trim(@$data['oid']['information']));
102
	$description = str_replace(array("\r", "\n"), '', $description);
103
	$description = str_ireplace('<br />', '<br>', $description);
104
	$description = str_ireplace('<br/>', '<br>', $description);
105
	$description = '<p>' . str_ireplace('<br><br>', '</p><p>', $description) . '</p>';
106
 
107
	$ra_email = @$data['oid']['current_registrant']['email'];
108
	if ($ra_email == '') $ra_email = @$data['oid']['first_registrant']['email'];
109
 
261 daniel-mar 110
	$resx = OIDplus::db()->query("select ra_email from ###objects where id = ?", array("oid:$parent"));
236 daniel-mar 111
	if ($rowx = $resx->fetch_array()) $ra_email = $rowx['ra_email'];
61 daniel-mar 112
 
113
	$created = @$data['oid']['first_registrant']['creation-date'];
114
	$updated = @$data['oid']['last-modification'];
115
 
261 daniel-mar 116
	sql_execute("insert into ###objects (id, parent, title, description, ra_email, created, updated) values (?, ?, ?, ?, ?, ?, ?)",
150 daniel-mar 117
		array("oid:$oid", "oid:$parent", $title, $description, $ra_email, $created, $updated));
61 daniel-mar 118
}
119
 
1130 daniel-mar 120
/**
121
 * @param string $oid
122
 * @return false|array
123
 */
124
function ft_get_oid_data(string $oid) {
261 daniel-mar 125
	$url = OIDplus::baseConfig()->getValue('OIDINFO_API_URL') . '&oid='.urlencode($oid);
61 daniel-mar 126
	$cont_json = @file_get_contents($url);
127
	if (!$cont_json) {
128
		sleep(5);
129
                $cont_json = @file_get_contents($url);
130
                if (!$cont_json) return false;
131
	}
1130 daniel-mar 132
	return json_decode($cont_json,true);
61 daniel-mar 133
}
134
 
1130 daniel-mar 135
/**
136
 * @param string $sql
137
 * @param array|null $prep
138
 * @return void
139
 */
140
function sql_execute(string $sql, array $prep=null) {
61 daniel-mar 141
#	echo "$sql\n";
237 daniel-mar 142
	try {
143
		OIDplus::db()->query($sql, $prep);
144
	} catch (Exception $e) {
145
		echo "Warning: " . $e->getMessage() . "\n";
146
	}
61 daniel-mar 147
}