db.php 12 KB

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