Rev 1130 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/usr/bin/env php
<?php
/*
* OIDplus 2.0
* Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use ViaThinkSoft\OIDplus\OIDplus;
require_once __DIR__ . '/../includes/oidplus.inc.php';
OIDplus::init(false);
if (!OIDplus::baseConfig()->exists('OIDINFO_API_URL')) {
die("OIDINFO_API_URL not available (API is currently not public)\n");
}
const VERBOSE = false;
const DEFAULT_EMAIL = 'oidra@viathinksoft.de';
const ALL_OID_LIST = '/home/oidplus/all_oids.txt'; // contains all OIDs (non-public file)
const DESIRED_ROOT = '1.3.6.1.4.1.37476';
# ---
exec("cat ".escapeshellarg(ALL_OID_LIST)." | sort | grep '^".DESIRED_ROOT."'", $out, $ec);
foreach ($out as $oid) {
$oid = trim($oid);
if ($oid == '') continue;
check_oid($oid);
}
/**
* @param string $oid
* @return void
*/
function check_oid(string $oid) {
$res = OIDplus::db()->query("select * from ###objects where id = ?", array("oid:$oid"));
$ok = $res->num_rows() > 0;
if (!$ok) {
echo "Not existing: $oid : Adding!\n";
add_oid($oid);
}
}
echo "OK\n";
# ---
/**
* @param string $oid
* @param string $root
* @return void
*/
function add_oid(string $oid, string $root=DESIRED_ROOT) {
if (VERBOSE) echo "Adding $oid ...\n";
$data = ft_get_oid_data($oid);
if (!isset($data['oid'])) {
echo "Warning: Cannot gain information about OID $oid\n";
$parent = ($oid == $root) ? '' : oid_up($oid);
$title = '(tbd)';
$description = '(tbd)';
$ra_email = '';
if ((DEFAULT_EMAIL != '') && ($ra_email == '')) $ra_email = DEFAULT_EMAIL;
sql_execute("insert into ###objects (id, parent, title, description, ra_email) values (?, ?, ?, ?, ?)", array("oid:$oid", "oid:$parent", $title, $description, $ra_email));
return;
}
if (!isset($data['oid']['identifier'])) $data['oid']['identifier'] = array();
foreach ($data['oid']['identifier'] as $identifier) {
sql_execute("insert into ###asn1id (oid, name) values (?, ?)", array("oid:$oid", $identifier));
}
if (!isset($data['oid']['unicode-label'])) $data['oid']['unicode-label'] = array();
foreach ($data['oid']['unicode-label'] as $identifier) {
sql_execute("insert into ###iri (oid, name) values (?, ?)", array("oid:$oid", $identifier));
}
$parent = ($oid == $root) ? '' : oid_up($oid);
$title = strip_tags(@$data['oid']['description']);
$title = html_entity_decode($title, ENT_COMPAT, 'UTF-8');
$description = '<strong>' . trim(@$data['oid']['description']) . '</strong><br><br>' . nl2br(trim(@$data['oid']['information']));
$description = str_replace(array("\r", "\n"), '', $description);
$description = str_ireplace('<br />', '<br>', $description);
$description = str_ireplace('<br/>', '<br>', $description);
$description = '<p>' . str_ireplace('<br><br>', '</p><p>', $description) . '</p>';
$ra_email = @$data['oid']['current_registrant']['email'];
if ($ra_email == '') $ra_email = @$data['oid']['first_registrant']['email'];
$resx = OIDplus::db()->query("select ra_email from ###objects where id = ?", array("oid:$parent"));
if ($rowx = $resx->fetch_array()) $ra_email = $rowx['ra_email'];
$created = @$data['oid']['first_registrant']['creation-date'];
$updated = @$data['oid']['last-modification'];
sql_execute("insert into ###objects (id, parent, title, description, ra_email, created, updated) values (?, ?, ?, ?, ?, ?, ?)",
array("oid:$oid", "oid:$parent", $title, $description, $ra_email, $created, $updated));
}
/**
* @param string $oid
* @return false|array
*/
function ft_get_oid_data(string $oid) {
$url = OIDplus::baseConfig()->getValue('OIDINFO_API_URL') . '&oid='.urlencode($oid);
$cont_json = @file_get_contents($url);
if (!$cont_json) {
sleep(5);
$cont_json = @file_get_contents($url);
if (!$cont_json) return false;
}
return json_decode($cont_json,true);
}
/**
* @param string $sql
* @param array|null $prep
* @return void
*/
function sql_execute(string $sql, array $prep=null) {
# echo "$sql\n";
try {
OIDplus::db()->query($sql, $prep);
} catch (Exception $e) {
echo "Warning: " . $e->getMessage() . "\n";
}
}