db.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. /**
  3. * ZeroBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link http://sebsauvage.net/wiki/doku.php?id=php:zerobin
  8. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  9. * @license http://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  10. * @version 0.19
  11. */
  12. /**
  13. * zerobin_db
  14. *
  15. * Model for DB access, implemented as a singleton.
  16. */
  17. class zerobin_db extends zerobin_abstract
  18. {
  19. /**
  20. * cache for select queries
  21. *
  22. * @var array
  23. */
  24. private static $_cache = array();
  25. /**
  26. * instance of database connection
  27. *
  28. * @access private
  29. * @static
  30. * @var PDO
  31. */
  32. private static $_db;
  33. /**
  34. * table prefix
  35. *
  36. * @access private
  37. * @static
  38. * @var string
  39. */
  40. private static $_prefix = '';
  41. /**
  42. * database type
  43. *
  44. * @access private
  45. * @static
  46. * @var string
  47. */
  48. private static $_type = '';
  49. /**
  50. * get instance of singleton
  51. *
  52. * @access public
  53. * @static
  54. * @param array $options
  55. * @throws Exception
  56. * @return zerobin_db
  57. */
  58. public static function getInstance($options = null)
  59. {
  60. // if needed initialize the singleton
  61. if(!(self::$_instance instanceof zerobin_db)) {
  62. self::$_instance = new self;
  63. }
  64. if (is_array($options))
  65. {
  66. // set table prefix if given
  67. if (array_key_exists('tbl', $options)) self::$_prefix = $options['tbl'];
  68. // initialize the db connection with new options
  69. if (
  70. array_key_exists('dsn', $options) &&
  71. array_key_exists('usr', $options) &&
  72. array_key_exists('pwd', $options) &&
  73. array_key_exists('opt', $options)
  74. )
  75. {
  76. self::$_db = new PDO(
  77. $options['dsn'],
  78. $options['usr'],
  79. $options['pwd'],
  80. $options['opt']
  81. );
  82. // check if the database contains the required tables
  83. self::$_type = strtolower(
  84. substr($options['dsn'], 0, strpos($options['dsn'], ':'))
  85. );
  86. switch(self::$_type)
  87. {
  88. case 'ibm':
  89. $sql = 'SELECT tabname FROM SYSCAT.TABLES ';
  90. break;
  91. case 'informix':
  92. $sql = 'SELECT tabname FROM systables ';
  93. break;
  94. case 'mssql':
  95. $sql = "SELECT name FROM sysobjects "
  96. . "WHERE type = 'U' ORDER BY name";
  97. break;
  98. case 'mysql':
  99. $sql = 'SHOW TABLES';
  100. break;
  101. case 'oci':
  102. $sql = 'SELECT table_name FROM all_tables';
  103. break;
  104. case 'pgsql':
  105. $sql = "SELECT c.relname AS table_name "
  106. . "FROM pg_class c, pg_user u "
  107. . "WHERE c.relowner = u.usesysid AND c.relkind = 'r' "
  108. . "AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) "
  109. . "AND c.relname !~ '^(pg_|sql_)' "
  110. . "UNION "
  111. . "SELECT c.relname AS table_name "
  112. . "FROM pg_class c "
  113. . "WHERE c.relkind = 'r' "
  114. . "AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) "
  115. . "AND NOT EXISTS (SELECT 1 FROM pg_user WHERE usesysid = c.relowner) "
  116. . "AND c.relname !~ '^pg_'";
  117. break;
  118. case 'sqlite':
  119. $sql = "SELECT name FROM sqlite_master WHERE type='table' "
  120. . "UNION ALL SELECT name FROM sqlite_temp_master "
  121. . "WHERE type='table' ORDER BY name";
  122. break;
  123. default:
  124. throw new Exception(
  125. 'PDO type ' .
  126. self::$_type .
  127. ' is currently not supported.'
  128. );
  129. }
  130. $statement = self::$_db->query($sql);
  131. $tables = $statement->fetchAll(PDO::FETCH_COLUMN, 0);
  132. // create paste table if needed
  133. if (!array_key_exists(self::$_prefix . 'paste', $tables))
  134. {
  135. self::$_db->exec(
  136. 'CREATE TABLE ' . self::$_prefix . 'paste ( ' .
  137. 'dataid CHAR(16), ' .
  138. 'data TEXT, ' .
  139. 'postdate INT, ' .
  140. 'expiredate INT, ' .
  141. 'opendiscussion INT, ' .
  142. 'burnafterreading INT );'
  143. );
  144. }
  145. // create comment table if needed
  146. if (!array_key_exists(self::$_prefix . 'comment', $tables))
  147. {
  148. self::$_db->exec(
  149. 'CREATE TABLE ' . self::$_prefix . 'comment ( ' .
  150. 'dataid CHAR(16), ' .
  151. 'pasteid CHAR(16), ' .
  152. 'parentid CHAR(16), ' .
  153. 'data TEXT, ' .
  154. 'nickname VARCHAR(255), ' .
  155. 'vizhash TEXT, ' .
  156. 'postdate INT );'
  157. );
  158. }
  159. }
  160. }
  161. return parent::$_instance;
  162. }
  163. /**
  164. * Create a paste.
  165. *
  166. * @access public
  167. * @param string $pasteid
  168. * @param array $paste
  169. * @return bool
  170. */
  171. public function create($pasteid, $paste)
  172. {
  173. if (
  174. array_key_exists($pasteid, self::$_cache)
  175. ) {
  176. if(false !== self::$_cache[$pasteid]) {
  177. return false;
  178. } else {
  179. unset(self::$_cache[$pasteid]);
  180. }
  181. }
  182. if (
  183. !array_key_exists('opendiscussion', $paste['meta'])
  184. ) $paste['meta']['opendiscussion'] = false;
  185. if (
  186. !array_key_exists('burnafterreading', $paste['meta'])
  187. ) $paste['meta']['burnafterreading'] = false;
  188. return self::_exec(
  189. 'INSERT INTO ' . self::$_prefix . 'paste VALUES(?,?,?,?,?,?)',
  190. array(
  191. $pasteid,
  192. $paste['data'],
  193. $paste['meta']['postdate'],
  194. $paste['meta']['expire_date'],
  195. (int) $paste['meta']['opendiscussion'],
  196. (int) $paste['meta']['burnafterreading'],
  197. )
  198. );
  199. }
  200. /**
  201. * Read a paste.
  202. *
  203. * @access public
  204. * @param string $pasteid
  205. * @return stdClass|false
  206. */
  207. public function read($pasteid)
  208. {
  209. if (
  210. !array_key_exists($pasteid, self::$_cache)
  211. ) {
  212. self::$_cache[$pasteid] = false;
  213. $paste = self::_select(
  214. 'SELECT * FROM ' . self::$_prefix . 'paste WHERE dataid = ?',
  215. array($pasteid), true
  216. );
  217. if(false !== $paste) {
  218. // create object
  219. self::$_cache[$pasteid] = new stdClass;
  220. self::$_cache[$pasteid]->data = $paste['data'];
  221. self::$_cache[$pasteid]->meta = new stdClass;
  222. self::$_cache[$pasteid]->meta->postdate = (int) $paste['postdate'];
  223. self::$_cache[$pasteid]->meta->expire_date = (int) $paste['expiredate'];
  224. if (
  225. $paste['opendiscussion']
  226. ) self::$_cache[$pasteid]->meta->opendiscussion = true;
  227. if (
  228. $paste['burnafterreading']
  229. ) self::$_cache[$pasteid]->meta->burnafterreading = true;
  230. }
  231. }
  232. return self::$_cache[$pasteid];
  233. }
  234. /**
  235. * Delete a paste and its discussion.
  236. *
  237. * @access public
  238. * @param string $pasteid
  239. * @return void
  240. */
  241. public function delete($pasteid)
  242. {
  243. self::_exec(
  244. 'DELETE FROM ' . self::$_prefix . 'paste WHERE dataid = ?',
  245. array($pasteid)
  246. );
  247. self::_exec(
  248. 'DELETE FROM ' . self::$_prefix . 'comment WHERE pasteid = ?',
  249. array($pasteid)
  250. );
  251. if (
  252. array_key_exists($pasteid, self::$_cache)
  253. ) unset(self::$_cache[$pasteid]);
  254. }
  255. /**
  256. * Test if a paste exists.
  257. *
  258. * @access public
  259. * @param string $dataid
  260. * @return void
  261. */
  262. public function exists($pasteid)
  263. {
  264. if (
  265. !array_key_exists($pasteid, self::$_cache)
  266. ) self::$_cache[$pasteid] = $this->read($pasteid);
  267. return (bool) self::$_cache[$pasteid];
  268. }
  269. /**
  270. * Create a comment in a paste.
  271. *
  272. * @access public
  273. * @param string $pasteid
  274. * @param string $parentid
  275. * @param string $commentid
  276. * @param array $comment
  277. * @return int|false
  278. */
  279. public function createComment($pasteid, $parentid, $commentid, $comment)
  280. {
  281. return self::_exec(
  282. 'INSERT INTO ' . self::$_prefix . 'comment VALUES(?,?,?,?,?,?,?)',
  283. array(
  284. $commentid,
  285. $pasteid,
  286. $parentid,
  287. $comment['data'],
  288. $comment['meta']['nickname'],
  289. $comment['meta']['vizhash'],
  290. $comment['meta']['postdate'],
  291. )
  292. );
  293. }
  294. /**
  295. * Read all comments of paste.
  296. *
  297. * @access public
  298. * @param string $pasteid
  299. * @return array
  300. */
  301. public function readComments($pasteid)
  302. {
  303. $rows = self::_select(
  304. 'SELECT * FROM ' . self::$_prefix . 'comment WHERE pasteid = ?',
  305. array($pasteid)
  306. );
  307. // create object
  308. $commentTemplate = new stdClass;
  309. $commentTemplate->meta = new stdClass;
  310. // create comment list
  311. $comments = array();
  312. if (count($rows))
  313. {
  314. foreach ($rows as $row)
  315. {
  316. $i = (int) $row['postdate'];
  317. $comments[$i] = clone $commentTemplate;
  318. $comments[$i]->data = $row['data'];
  319. $comments[$i]->meta->nickname = $row['nickname'];
  320. $comments[$i]->meta->vizhash = $row['vizhash'];
  321. $comments[$i]->meta->postdate = $i;
  322. $comments[$i]->meta->commentid = $row['dataid'];
  323. $comments[$i]->meta->parentid = $row['parentid'];
  324. }
  325. ksort($comments);
  326. }
  327. return $comments;
  328. }
  329. /**
  330. * Test if a comment exists.
  331. *
  332. * @access public
  333. * @param string $dataid
  334. * @param string $parentid
  335. * @param string $commentid
  336. * @return void
  337. */
  338. public function existsComment($pasteid, $parentid, $commentid)
  339. {
  340. return (bool) self::_select(
  341. 'SELECT dataid FROM ' . self::$_prefix . 'comment ' .
  342. 'WHERE pasteid = ? AND parentid = ? AND dataid = ?',
  343. array($pasteid, $parentid, $commentid), true
  344. );
  345. }
  346. /**
  347. * execute a statement
  348. *
  349. * @access private
  350. * @static
  351. * @param string $sql
  352. * @param array $params
  353. * @throws PDOException
  354. * @return array
  355. */
  356. private static function _exec($sql, array $params)
  357. {
  358. $statement = self::$_db->prepare($sql);
  359. $result = $statement->execute($params);
  360. $statement->closeCursor();
  361. return $result;
  362. }
  363. /**
  364. * run a select statement
  365. *
  366. * @access private
  367. * @static
  368. * @param string $sql
  369. * @param array $params
  370. * @param bool $firstOnly if only the first row should be returned
  371. * @throws PDOException
  372. * @return array
  373. */
  374. private static function _select($sql, array $params, $firstOnly = false)
  375. {
  376. $statement = self::$_db->prepare($sql);
  377. $statement->execute($params);
  378. $result = $firstOnly ?
  379. $statement->fetch(PDO::FETCH_ASSOC) :
  380. $statement->fetchAll(PDO::FETCH_ASSOC);
  381. $statement->closeCursor();
  382. return $result;
  383. }
  384. }