1<?php
2
3class MDB3Statement extends PDOStatement {
4	public $dbh;
5    protected function __construct($dbh) {
6        $this->dbh = $dbh;
7    }
8	function fetchRow() {
9		return $this->fetch();
10	}
11	function numRows() {
12		return $this->rowCount();
13	}
14}
15
16class MDB3 extends PDO {
17	function __construct($dsn, $username="", $password="", $driver_options=array()) {
18        parent::__construct($dsn,$username,$password, $driver_options);
19        $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('MDB3Statement', array($this)));
20        $this->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
21    }
22	function query($sql, $mode = null, ...$params) {
23		global $ALOCAL;
24
25		if ($ALOCAL['debug']) error_log($sql, 3, '/tmp/php.log');
26		$stmt = parent::prepare($sql);
27		$stmt->execute($params);
28		return $stmt;
29	}
30	function getAll($query) {
31		$stmt = $this->query($query);
32		return $stmt->fetchAll();
33	}
34	function setOption($option,$val) {
35        if ('persistent' === $option)
36			$this->setAttribute(PDO::ATTR_PERSISTENT, $val);
37	}
38	function autoCommit($autocommit) {
39		if (! $autocommit && ! $this->inTransaction()) $this->beginTransaction();
40	}
41	function slaveOK()
42	{
43		$resultat = $this->query('show slave status');
44		$etat = $resultat->fetch();
45		if ($etat && $etat['Slave_IO_Running'] == 'Yes' && $etat['Slave_SQL_Running'] == 'Yes') return TRUE;
46		return FALSE;
47	}
48}
49
50class PEAR
51{
52	static function isError($obj)
53	{
54		return FALSE;
55	}
56}
57
58class DB extends PEAR
59{
60    static function &connect($dsn, $options = array())
61    {
62		if (! is_array($dsn) && strpos($dsn,'//')) {
63			$parts = explode('/', $dsn);
64			list($dsna['phptype']) = explode(':', $parts[0]);
65			list($creds , $dsna['hostspec']) = explode('@', $parts[2]);
66			list($dsna['username'], $dsna['password']) = explode(':', $creds);
67			$dsna['database'] = $parts[3];
68			$dsn = $dsna;
69		}
70		if (is_array($dsn)) $obj = new MDB3($dsn['phptype'].':host='.$dsn['hostspec'].';dbname='.$dsn['database'], $dsn['username'], $dsn['password']);
71		else $obj = new MDB3($dsn);
72		$obj->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
73		$obj->dsn = $dsn;
74        return $obj;
75    }
76}
77
78if (! function_exists('mysql_connect')) {
79
80function mysql_connect($server, $login, $password) {
81	global $DSN, $DB;
82	if ($DB instanceof MDB3) return TRUE;
83
84	$DSN = array('phptype' => 'mysql',
85			'username' => $login,
86			'password' => $password,
87			'hostspec' => $server);
88	try {
89		$DB = new PDO($DSN['phptype'].':host='.$DSN['hostspec'], $DSN['username'], $DSN['password']);
90		$DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
91	} catch (Exception $e) {
92		return FALSE;
93	}
94	return true;
95}
96function mysql_select_db($db) {
97	global $DSN, $DB;
98	if ($DB instanceof MDB3) return TRUE;
99
100	$DSN['database'] = $db;
101	try {
102		$DB = new PDO($DSN['phptype'].':host='.$DSN['hostspec'].';dbname='.$DSN['database'], $DSN['username'], $DSN['password']);
103		$DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
104	} catch (Exception $e) {
105		return FALSE;
106	}
107	return true;
108}
109function mysql_query($query) {
110	global $DB;
111
112	return $DB->query($query);
113}
114function mysql_num_rows($statement) {
115	return $statement->rowCount();
116}
117function mysql_fetch_array($statement, $params = NULL) {
118	return $statement->fetch();
119}
120function mysql_fetch_assoc($statement, $params = NULL) {
121	return $statement->fetch();
122}
123function mysql_error($statement = NULL) {
124	return 'No Error Today';
125}
126
127}
128