ctucx.git: tinyDAV

[php] Cal-/ CardDAV server with a simple web-GUI based on SabreDAV

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 
101 
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 <?php

class Database {
	private $db;
	public $lastQuery;

	public $handleError = false;

	public function __construct(array $options) {
		try {
			if ($options['type'] !== 'mysql') {
				$this->db = new PDO('sqlite:'.$options['file']);
			} else {
				$this->db = new PDO('mysql:host='.$options['host'].';dbname='.$options['name'], $options['user'], $options['password']);
			}
		} catch (PDOException $e) {
			die('Connection to database faild: '.$e->getMessage());
		}

		$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		$this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
	}

	public function query($sql, array $args = null) {
		try {
			if (!is_null($args)) {
				$this->lastQuery = $this->db->prepare($sql);
				$this->lastQuery->execute($args);
			} else {
				$this->lastQuery = $this->db->query($sql);
			}

			return $this->lastQuery;
		} catch (PDOException $e) {
			if ($this->handleError) {
				$this->error($e->getMessage(), $sql, $e->getCode());
			} else {
				throw $e;
			}
		}
	}

	public function prepare($sql) {
		try {
			return $this->db->prepare($sql);
		} catch (PDOException $e) {
			if ($this->handleError) {
				$this->error($e->getMessage(), $sql, $e->getCode());
			} else {
				throw $e;
			}
		}
	}

	public function fetchObject(PDOStatement $stmt = null) {
		if (!is_null($stmt)) {
			return $stmt->fetch(PDO::FETCH_OBJ);
		} else {
			return $this->lastQuery->fetch(PDO::FETCH_OBJ);
		}
	}

	public function fetch(PDOStatement $stmt = null, $fetch_style = FETCH_ASSOC) {
		if (!is_null($stmt)) {
			return $stmt->fetch(PDO::$fetch_style);
		} else {
			return $this->lastQuery->fetch(PDO::$fetch_style);
		}
	}

	public function numRows(PDOStatement $stmt = null) {
		if (!is_null($stmt)) {
			return $stmt->rowCount();
		} else {
			return $this->lastQuery->rowCount();
		}
	}

	function tableExists($table) {
		try {
			$result = $this->query('SHOW TABLES LIKE ?', [$table])->numRows();
		} catch (Exception $e) {
			return $e->getMessage();
		}

	    return $result > 0 ? true : false;
	}

	public function insertID() {
		return $this->db->lastInsertId();
	}

	public function beginTransaction() {
		return $this->db->beginTransaction();
	}

	public function commit() {
		return $this->db->commit();
	}

	public function rollback() {
		return $this->db->rollBack();
	}

	public function errorInfo() {
		return $this->db->errorinfo();
	}
		
	protected function error($msg, $sql, $code) {
		Helpers::log(Helpers::LOG_ERROR, $msg . '<br><br><code>' . $sql . '</code>');
	}
}