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