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