Subversion Repositories oidplus

Rev

Rev 117 | 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. if (!defined('IN_OIDPLUS')) die();
  21.  
  22. class OIDplusDataBaseMySQL implements OIDplusDataBase {
  23.         private $mysqli;
  24.         private $last_query; // for debugging
  25.  
  26.         public function query($sql) {
  27.                 $this->last_query = $sql;
  28.                 // $sql = str_replace('???_', OIDPLUS_TABLENAME_PREFIX, $sql);
  29.                 return $this->mysqli->query($sql, MYSQLI_STORE_RESULT);
  30.         }
  31.         public function num_rows($res) {
  32.                 if (!is_object($res)) {
  33.                         throw new Exception("num_rows called on non object. Last query: ".$this->last_query);
  34.                 } else {
  35.                         return $res->num_rows;
  36.                 }
  37.         }
  38.         public function fetch_array($res) {
  39.                 if (!is_object($res)) {
  40.                         throw new Exception("fetch_array called on non object. Last query: ".$this->last_query);
  41.                 } else {
  42.                         return $res->fetch_array(MYSQLI_BOTH);
  43.                 }
  44.         }
  45.         public function fetch_object($res) {
  46.                 if (!is_object($res)) {
  47.                         throw new Exception("fetch_object called on non object. Last query: ".$this->last_query);
  48.                 } else {
  49.                         return $res->fetch_object("stdClass");
  50.                 }
  51.         }
  52.         public function real_escape_string($str) {
  53.                 return $this->mysqli->real_escape_string($str);
  54.         }
  55.         public function insert_id() {
  56.                 return $this->mysqli->insert_id;
  57.         }
  58.         public function escape_bool($str) {
  59.                 return (($str == 'true') || ($str == '1') || ($str == 'On') || ($str == 'on')) ? '1' : '0';
  60.         }
  61.         public function set_charset($charset) {
  62.                 return $this->mysqli->set_charset($charset);
  63.         }
  64.         public function error() {
  65.                 return !empty($this->mysqli->connect_error) ? $this->mysqli->connect_error : $this->mysqli->error;
  66.         }
  67.         public function __construct() {
  68.                 $html = OIDPLUS_HTML_OUTPUT;
  69.  
  70.                 // Try connecting to the database
  71.                 $this->mysqli = @new mysqli(OIDPLUS_MYSQL_HOST, OIDPLUS_MYSQL_USERNAME, base64_decode(OIDPLUS_MYSQL_PASSWORD), OIDPLUS_MYSQL_DATABASE, ini_get("mysqli.default_port"), ini_get("mysqli.default_socket"));
  72.                 if (!empty($this->mysqli->connect_error) || ($this->mysqli->connect_errno != 0)) {
  73.                         if ($html) {
  74.                                 echo "<h1>Error</h1><p>Database connection failed!</p>";
  75.                                 if (is_dir(__DIR__.'/../../setup')) {
  76.                                         echo '<p>If you believe that the login credentials are wrong, please run <a href="setup/">setup</a> again.</p>';
  77.                                 }
  78.                         } else {
  79.                                 echo "Error: Database connection failed!";
  80.                                 if (is_dir(__DIR__.'/../../setup')) {
  81.                                         echo ' If you believe that the login credentials are wrong, please run setup again.';
  82.                                 }
  83.                         }
  84.                         die();
  85.                 }
  86.  
  87.                 // Check if database tables are existing
  88.                 $table_names = array('objects', 'asn1id', 'iri', 'ra', 'config');
  89.                 foreach ($table_names as $tablename) {
  90.                         if (!$this->query("DESCRIBE `".OIDPLUS_TABLENAME_PREFIX.$tablename."`")) {
  91.                                 if ($html) {
  92.                                         echo '<h1>Error</h1><p>Table <b>'.OIDPLUS_TABLENAME_PREFIX.$tablename.'</b> does not exist.</p><p>Please run <a href="setup/">setup</a> again.</p>';
  93.                                 } else {
  94.                                         echo 'Error: Table '.OIDPLUS_TABLENAME_PREFIX.$tablename.' does not exist. Please run setup again.';
  95.                                 }
  96.                                 die();
  97.                         }
  98.                 }
  99.  
  100.                 // Do the database table tables need an update?
  101.                 // Note: The config setting "database_version" is inserted in setup/sql/...sql, not in the OIDplus core init
  102.  
  103.                 /*
  104.                 $res = $this->query("SELECT value FROM `".OIDPLUS_TABLENAME_PREFIX."config` WHERE name = 'database_version'");
  105.                 $row = $this->fetch_array($res);
  106.                 $version = $row['value'];
  107.                 if ($version == 200) {
  108.                         // Do stuff to update 200 -> 201
  109.                         $version = 201;
  110.                         $this->query("UPDATE `".OIDPLUS_TABLENAME_PREFIX."config` SET value = '$version' WHERE name = 'database_version'");
  111.                 }
  112.                 if ($version == 201) {
  113.                         // Do stuff to update 201 -> 202
  114.                         $version = 202;
  115.                         $this->query("UPDATE `".OIDPLUS_TABLENAME_PREFIX."config` SET value = '$version' WHERE name = 'database_version'");
  116.                 }
  117.                 */
  118.         }
  119.  
  120.         // TODO: better create some kind of Object-Type-API that does the sorting. But this means, the sorting won't be done with SQL
  121.         public function natOrder($fieldname, $maxdepth=100) { // TODO: also "desc" and "asc" support?
  122.                 /*
  123.                    CREATE FUNCTION SPLIT_STRING(str VARCHAR(255), delim VARCHAR(12), pos INT)
  124.                    RETURNS VARCHAR(255)
  125.                    RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(str, delim, pos),
  126.                    LENGTH(SUBSTRING_INDEX(str, delim, pos-1)) + 1), delim, '');
  127.                  */
  128.                 $out = array();
  129.                 $out[] = "(REPLACE(SUBSTRING(SUBSTRING_INDEX($fieldname, ':', 1),LENGTH(SUBSTRING_INDEX($fieldname, ':', 0)) + 1),':', '')) asc"; // first sort by NS (namespace)
  130.                 for ($i=1; $i<=$maxdepth; $i++) {
  131.         //              $out[] = "LENGTH(SPLIT_STRING($fieldname, '.', $i) asc";
  132.         //              $out[] = "SPLIT_STRING($fieldname, '.', $i) asc";
  133.  
  134.                         $out[] = "LENGTH(REPLACE(SUBSTRING(SUBSTRING_INDEX($fieldname, '.', $i),LENGTH(SUBSTRING_INDEX($fieldname, '.', $i-1)) + 1),'.', '')) asc";
  135.                         $out[] = "(REPLACE(SUBSTRING(SUBSTRING_INDEX($fieldname, '.', $i),LENGTH(SUBSTRING_INDEX($fieldname, '.', $i-1)) + 1),'.', '')) asc";
  136.  
  137.                 }
  138.                 return implode(', ', $out);
  139.         }
  140.  
  141. }
  142.  
  143.