Subversion Repositories oidplus

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 Daniel Marschall, ViaThinkSoft
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *     http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  */
  19.  
  20. define('OUTPUT_FORMAT_SPACER', 2);
  21. define('OUTPUT_FORMAT_MAX_LINE_LENGTH', 80);
  22.  
  23. # ---
  24.  
  25. require_once __DIR__ . '/../includes/oidplus.inc.php';
  26.  
  27. OIDplus::init(true);
  28.  
  29. OIDplus::db()->set_charset("UTF8");
  30. OIDplus::db()->query("SET NAMES 'utf8'");
  31.  
  32. header('Content-Type:text/plain; charset=UTF-8');
  33.  
  34. // Step 0: Get request parameter
  35.  
  36. if (php_sapi_name() == 'cli') {
  37.         if ($argc != 2) {
  38.                 echo "Syntax: $argv[0] <query>\n";
  39.                 exit(2);
  40.         }
  41.         $query = $argv[1];
  42. } else {
  43.         if (!isset($_REQUEST['query'])) {
  44.                 http_response_code(400);
  45.                 echo "Argument 'query' is missing\n";
  46.                 die();
  47.         }
  48.         $query = $_REQUEST['query'];
  49. }
  50.  
  51. $query = str_replace('oid:.', 'oid:', $query); // backwards compatibility: allow leading dot
  52.  
  53. // Step 1: Collect data
  54.  
  55. $out = array();
  56.  
  57. $out[] = "object: $query";
  58.  
  59. $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where id = '".OIDplus::db()->real_escape_string($query)."'");
  60. if ($row = OIDplus::db()->fetch_object($res)) {
  61.         $obj = OIDplusObject::parse($row->id);
  62.         if ($obj->isConfidential()) {
  63.                 $out[] = 'status: Found, confidential';
  64.         } else {
  65.                 $out[] = "status: Found";
  66.                 if (!empty($row->parent) && (!is_root($row->parent))) {
  67.                         $out[] = 'parent: ' . $row->parent . show_asn1_appendix($row->parent);
  68.                 }
  69.                 $out[] = 'title: ' . $row->title;
  70.                 $out[] = 'description: ' . trim(html_entity_decode(strip_tags($row->description)));
  71.  
  72.                 if (substr($query,0,4) === 'oid:') {
  73.                         $res2 = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."asn1id where oid = '".OIDplus::db()->real_escape_string($row->id)."'");
  74.                         while ($row2 = OIDplus::db()->fetch_object($res2)) {
  75.                                 $out[] = 'identifier: ' . $row2->name;
  76.                         }
  77.  
  78.                         $res2 = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."iri where oid = '".OIDplus::db()->real_escape_string($row->id)."'");
  79.                         while ($row2 = OIDplus::db()->fetch_object($res2)) {
  80.                                 $out[] = 'unicode-label: ' . $row2->name;
  81.                         }
  82.                 }
  83.  
  84.                 $out[] = 'created: ' . $row->created;
  85.                 $out[] = 'updated: ' . $row->updated;
  86.  
  87.                 $out[] = '';
  88.  
  89.                 $res2 = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."objects where parent = '".OIDplus::db()->real_escape_string($row->id)."'");
  90.                 if (OIDplus::db()->num_rows($res2) == 0) {
  91.                         $out[] = 'subordinate: (none)';
  92.                 }
  93.                 while ($row2 = OIDplus::db()->fetch_object($res2)) {
  94.                         $out[] = 'subordinate: ' . $row2->id . show_asn1_appendix($row2->id);
  95.                 }
  96.  
  97.                 $out[] = '';
  98.                 $out[] = 'ra-email: ' . $row->ra_email;
  99.                 $res2 = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."ra where email = '".OIDplus::db()->real_escape_string($row->ra_email)."'");
  100.                 if ($row2 = OIDplus::db()->fetch_object($res2)) {
  101.                         $out[] = 'ra-status: Registered';
  102.                         $out[] = 'ra-name: ' . $row2->ra_name;
  103.                         $out[] = 'ra-personal_name: ' . $row2->personal_name;
  104.                         $out[] = 'ra-organization: ' . $row2->organization;
  105.                         $out[] = 'ra-office: ' . $row2->office;
  106.                         if ($row2->privacy) {
  107.                                 $out[] = 'ra-street: ' . (!empty($row2->street) ? '(redacted)' : '');
  108.                                 $out[] = 'ra-zip_town: ' . (!empty($row2->zip_town) ? '(redacted)' : '');
  109.                                 $out[] = 'ra-country: ' . (!empty($row2->country) ? '(redacted)' : '');
  110.                                 $out[] = 'ra-phone: ' . (!empty($row2->phone) ? '(redacted)' : '');
  111.                                 $out[] = 'ra-mobile: ' . (!empty($row2->mobile) ? '(redacted)' : '');
  112.                                 $out[] = 'ra-fax: ' . (!empty($row2->fax) ? '(redacted)' : '');
  113.                         } else {
  114.                                 $out[] = 'ra-street: ' . $row2->street;
  115.                                 $out[] = 'ra-zip_town: ' . $row2->zip_town;
  116.                                 $out[] = 'ra-country: ' . $row2->country;
  117.                                 $out[] = 'ra-phone: ' . $row2->phone;
  118.                                 $out[] = 'ra-mobile: ' . $row2->mobile;
  119.                                 $out[] = 'ra-fax: ' . $row2->fax;
  120.                         }
  121.                         $out[] = 'ra-registered: ' . $row2->registered;
  122.                         $out[] = 'ra-updated: ' . $row2->updated;
  123.                 } else {
  124.                         $out[] = "ra-status: Not registered";
  125.                 }
  126.         }
  127. } else {
  128.         $out[] = "status: Not found";
  129. }
  130.  
  131. // Step 2: Format output
  132.  
  133. $longest_key = 0;
  134. foreach ($out as $line) {
  135.         $longest_key = max($longest_key, strlen(trim(explode(':',$line,2)[0])));
  136. }
  137.  
  138. echo '% ' . str_repeat('*', OUTPUT_FORMAT_MAX_LINE_LENGTH-2)."\n";
  139.  
  140. foreach ($out as $line) {
  141.         if (trim($line) == '') {
  142.                 echo "\n";
  143.                 continue;
  144.         }
  145.  
  146.         $ary = explode(':', $line, 2);
  147.  
  148.         $key = trim($ary[0]);
  149.  
  150.         $value = trim($ary[1]);
  151.         $value = wordwrap($value, OUTPUT_FORMAT_MAX_LINE_LENGTH - (OUTPUT_FORMAT_SPACER+1) - $longest_key);
  152.         $value = str_replace("\n", "\n".str_repeat(' ', $longest_key+OUTPUT_FORMAT_SPACER+1), $value);
  153.  
  154.         echo $key.':' . str_repeat(' ', $longest_key-strlen($key)) . str_repeat(' ', OUTPUT_FORMAT_SPACER) . (!empty($value) ? $value : '.') . "\n";
  155. }
  156.  
  157. echo '% ' . str_repeat('*', OUTPUT_FORMAT_MAX_LINE_LENGTH-2)."\n";
  158.  
  159. # ---
  160.  
  161. function show_asn1_appendix($id) {
  162.         if (substr($id,0,4) === 'oid:') {
  163.                 $appendix_asn1ids = array();
  164.                 $res3 = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."asn1id where oid = '".OIDplus::db()->real_escape_string($id)."'");
  165.                 while ($row3 = OIDplus::db()->fetch_object($res3)) {
  166.                         $appendix_asn1ids[] = $row3->name;
  167.                 }
  168.  
  169.                 $appendix = implode(', ', $appendix_asn1ids);
  170.                 if (!empty($appendix)) $appendix = " ($appendix)";
  171.         } else {
  172.                 $appendix = '';
  173.         }
  174.         return $appendix;
  175. }
  176.  
  177. function is_root($id) {
  178.         return empty(explode(':',$id,2)[1]);
  179. }
  180.