1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/freshrss_ynh.git synced 2024-09-03 18:36:33 +02:00
freshrss_ynh/sources/lib/Minz/ModelPdo.php

138 lines
3.4 KiB
PHP
Raw Normal View History

2014-07-23 15:52:50 +02:00
<?php
/**
* MINZ - Copyright 2011 Marien Fressinaud
* Sous licence AGPL3 <http://www.gnu.org/licenses/>
*/
/**
* La classe Model_sql représente le modèle interragissant avec les bases de données
* Seul la connexion MySQL est prise en charge pour le moment
*/
class Minz_ModelPdo {
/**
* Partage la connexion à la base de données entre toutes les instances.
*/
public static $useSharedBd = true;
private static $sharedBd = null;
private static $sharedPrefix;
2015-02-08 18:55:48 +01:00
private static $has_transaction = false;
private static $sharedCurrentUser;
2014-07-23 15:52:50 +02:00
protected static $sharedDbType;
/**
* $bd variable représentant la base de données
*/
protected $bd;
2015-02-08 18:55:48 +01:00
protected $current_user;
2014-07-23 15:52:50 +02:00
protected $prefix;
public function dbType() {
return self::$sharedDbType;
}
/**
* Créé la connexion à la base de données à l'aide des variables
* HOST, BASE, USER et PASS définies dans le fichier de configuration
*/
2014-09-21 12:12:35 +02:00
public function __construct($currentUser = null) {
if (self::$useSharedBd && self::$sharedBd != null && $currentUser === null) {
2014-07-23 15:52:50 +02:00
$this->bd = self::$sharedBd;
$this->prefix = self::$sharedPrefix;
2015-02-08 18:55:48 +01:00
$this->current_user = self::$sharedCurrentUser;
2014-07-23 15:52:50 +02:00
return;
}
2015-02-08 18:55:48 +01:00
$conf = Minz_Configuration::get('system');
$db = $conf->db;
2014-07-23 15:52:50 +02:00
2014-09-21 12:12:35 +02:00
if ($currentUser === null) {
$currentUser = Minz_Session::param('currentUser', '_');
}
2015-02-08 18:55:48 +01:00
$this->current_user = $currentUser;
self::$sharedCurrentUser = $currentUser;
2014-09-21 12:12:35 +02:00
2014-07-23 15:52:50 +02:00
try {
$type = $db['type'];
if ($type === 'mysql') {
$string = 'mysql:host=' . $db['host']
. ';dbname=' . $db['base']
. ';charset=utf8';
$driver_options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
2014-09-21 12:12:35 +02:00
$this->prefix = $db['prefix'] . $currentUser . '_';
2014-07-23 15:52:50 +02:00
} elseif ($type === 'sqlite') {
2015-02-08 18:55:48 +01:00
$string = 'sqlite:' . join_path(DATA_PATH, 'users', $currentUser, 'db.sqlite');
2014-07-23 15:52:50 +02:00
$driver_options = array(
//PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
);
$this->prefix = '';
} else {
throw new Minz_PDOConnectionException(
'Invalid database type!',
$db['user'], Minz_Exception::ERROR
);
}
self::$sharedDbType = $type;
self::$sharedPrefix = $this->prefix;
2014-09-21 12:12:35 +02:00
$this->bd = new MinzPDO(
2014-07-23 15:52:50 +02:00
$string,
$db['user'],
$db['password'],
$driver_options
);
2014-09-27 10:10:43 +02:00
if ($type === 'sqlite') {
$this->bd->exec('PRAGMA foreign_keys = ON;');
}
2014-07-23 15:52:50 +02:00
self::$sharedBd = $this->bd;
} catch (Exception $e) {
throw new Minz_PDOConnectionException(
$string,
$db['user'], Minz_Exception::ERROR
);
}
}
public function beginTransaction() {
$this->bd->beginTransaction();
2015-02-08 18:55:48 +01:00
self::$has_transaction = true;
}
public function hasTransaction() {
return self::$has_transaction;
2014-07-23 15:52:50 +02:00
}
public function commit() {
$this->bd->commit();
2015-02-08 18:55:48 +01:00
self::$has_transaction = false;
2014-07-23 15:52:50 +02:00
}
public function rollBack() {
$this->bd->rollBack();
2015-02-08 18:55:48 +01:00
self::$has_transaction = false;
2014-07-23 15:52:50 +02:00
}
public static function clean() {
self::$sharedBd = null;
self::$sharedPrefix = '';
}
}
2014-09-21 12:12:35 +02:00
class MinzPDO extends PDO {
2014-07-23 15:52:50 +02:00
private static function check($statement) {
if (preg_match('/^(?:UPDATE|INSERT|DELETE)/i', $statement)) {
invalidateHttpCache();
}
}
public function prepare($statement, $driver_options = array()) {
2014-09-21 12:12:35 +02:00
MinzPDO::check($statement);
2014-07-23 15:52:50 +02:00
return parent::prepare($statement, $driver_options);
}
public function exec($statement) {
2014-09-21 12:12:35 +02:00
MinzPDO::check($statement);
2014-07-23 15:52:50 +02:00
return parent::exec($statement);
}
}