]> git.wh0rd.org Git - tt-rss.git/commitdiff
initial
authorAndrew Dolgov <fox@fakecake.org>
Wed, 17 Apr 2013 17:19:00 +0000 (21:19 +0400)
committerAndrew Dolgov <fox@fakecake.org>
Wed, 17 Apr 2013 17:19:00 +0000 (21:19 +0400)
classes/db.php
classes/db/pdo.php [new file with mode: 0644]
include/errorhandler.php
plugins/auth_internal/init.php

index c9d9ad5ea9326bded4afb2e3cda7e040ddc1ed57..86d2ab897d0afa608a5a7f945dfc32a0dfa5a75f 100644 (file)
@@ -5,22 +5,37 @@ class Db implements IDb {
        private $link;
 
        private function __construct() {
-               switch (DB_TYPE) {
-               case "mysql":
-                       if (function_exists("mysqli_connect")) {
-                               $this->adapter = new Db_Mysqli();
-                       } else {
-                               $this->adapter = new Db_Mysql();
+
+               $er = error_reporting(E_ALL);
+
+               if (class_exists("PDO")) {
+                       $this->adapter = new Db_PDO();
+               } else {
+                       switch (DB_TYPE) {
+                       case "mysql":
+                               if (function_exists("mysqli_connect")) {
+                                       $this->adapter = new Db_Mysqli();
+                               } else {
+                                       $this->adapter = new Db_Mysql();
+                               }
+                               break;
+                       case "pgsql":
+                               $this->adapter = new Db_Pgsql();
+                               break;
+                       default:
+                               die("Unknown DB_TYPE: " . DB_TYPE);
                        }
-                       break;
-               case "pgsql":
-                       $this->adapter = new Db_Pgsql();
-                       break;
-               default:
-                       die("Unknown DB_TYPE: " . DB_TYPE);
                }
 
+               if (!$this->adapter) die("Error initializing database adapter for " . DB_TYPE);
+
                $this->link = $this->adapter->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, defined('DB_PORT') ? DB_PORT : false);
+
+               if (!$this->link) {
+                       die("Error connecting through adapter: " . $this->adapter->last_error());
+               }
+
+               error_reporting($er);
        }
 
        private function __clone() {
diff --git a/classes/db/pdo.php b/classes/db/pdo.php
new file mode 100644 (file)
index 0000000..aaac892
--- /dev/null
@@ -0,0 +1,87 @@
+<?php
+class Db_PDO implements IDb {
+       private $pdo;
+
+       function connect($host, $user, $pass, $db, $port) {
+               $connstr = DB_TYPE . ":host=$host;dbname=$db;charset=utf8";
+
+               try {
+                       $this->pdo = new PDO($connstr, $user, $pass);
+               } catch (PDOException $e) {
+                       die($e->getMessage());
+               }
+
+               return $this->pdo;
+       }
+
+       function escape_string($s, $strip_tags = true) {
+               if ($strip_tags) $s = strip_tags($s);
+
+               $qs = $this->pdo->quote($s);
+
+               return mb_substr($qs, 1, mb_strlen($qs)-2);
+       }
+
+       function query($query, $die_on_error = true) {
+               try {
+                       return $this->pdo->query($query);
+               } catch (PDOException $e) {
+                       user_error($e->getMessage(), $die_on_error ? E_USER_ERROR : E_USER_WARNING);
+               }
+       }
+
+       function fetch_assoc($result) {
+               try {
+                       if ($result) {
+                               return $result->fetch();
+                       } else {
+                               return null;
+                       }
+               } catch (PDOException $e) {
+                       user_error($e->getMessage(), E_USER_WARNING);
+               }
+       }
+
+       function num_rows($result) {
+               try {
+                       if ($result) {
+                               return $result->rowCount();
+                       } else {
+                               return false;
+                       }
+               } catch (PDOException $e) {
+                       user_error($e->getMessage(), E_USER_WARNING);
+               }
+       }
+
+       function fetch_result($result, $row, $param) {
+               $line = $this->fetch_assoc($result);
+
+               if ($line)
+                       return $line[$param];
+               else
+                       return null;
+
+       }
+
+       function close() {
+               $this->pdo = null;
+       }
+
+       function affected_rows($result) {
+               try {
+                       if ($result) {
+                               return $result->rowCount();
+                       } else {
+                               return null;
+                       }
+               } catch (PDOException $e) {
+                       user_error($e->getMessage(), E_USER_WARNING);
+               }
+       }
+
+       function last_error() {
+               return join(" ", $pdo->errorInfo());
+       }
+}
+?>
index 2c8d35f8371c4a8b2759d014642245910b50ad97..6b64d5161bad2ced4252cc4ec7981a1c4e65b1f1 100644 (file)
@@ -49,6 +49,6 @@ function ttrss_fatal_handler() {
        return false;
 }
 
-register_shutdown_function('ttrss_fatal_handler');
-set_error_handler('ttrss_error_handler');
+//register_shutdown_function('ttrss_fatal_handler');
+//set_error_handler('ttrss_error_handler');
 ?>
index c6f075036f8a47935058e33707b6b20119edaa97..33f90d4b16906e9d36fa692717d0b76aa19f6d15 100644 (file)
@@ -24,10 +24,12 @@ class Auth_Internal extends Plugin implements IAuthModule {
 
                if (get_schema_version() > 96) {
                        if (!defined('AUTH_DISABLE_OTP') || !AUTH_DISABLE_OTP) {
+
                                $result = db_query("SELECT otp_enabled,salt FROM ttrss_users WHERE
                                        login = '$login'");
 
                                if (db_num_rows($result) > 0) {
+
                                        require_once "lib/otphp/vendor/base32.php";
                                        require_once "lib/otphp/lib/otp.php";
                                        require_once "lib/otphp/lib/totp.php";