Database.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. <?php
  2. /**
  3. * PrivateBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link https://github.com/PrivateBin/PrivateBin
  8. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  9. * @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  10. * @version 1.2.1
  11. */
  12. namespace PrivateBin\Data;
  13. use Exception;
  14. use PDO;
  15. use PDOException;
  16. use PrivateBin\Controller;
  17. /**
  18. * Database
  19. *
  20. * Model for database access, implemented as a singleton.
  21. */
  22. class Database extends AbstractData
  23. {
  24. /**
  25. * cache for select queries
  26. *
  27. * @var array
  28. */
  29. private static $_cache = array();
  30. /**
  31. * instance of database connection
  32. *
  33. * @access private
  34. * @static
  35. * @var PDO
  36. */
  37. private static $_db;
  38. /**
  39. * table prefix
  40. *
  41. * @access private
  42. * @static
  43. * @var string
  44. */
  45. private static $_prefix = '';
  46. /**
  47. * database type
  48. *
  49. * @access private
  50. * @static
  51. * @var string
  52. */
  53. private static $_type = '';
  54. /**
  55. * get instance of singleton
  56. *
  57. * @access public
  58. * @static
  59. * @param array $options
  60. * @throws Exception
  61. * @return Database
  62. */
  63. public static function getInstance($options = null)
  64. {
  65. // if needed initialize the singleton
  66. if (!(self::$_instance instanceof self)) {
  67. self::$_instance = new self;
  68. }
  69. if (is_array($options)) {
  70. // set table prefix if given
  71. if (array_key_exists('tbl', $options)) {
  72. self::$_prefix = $options['tbl'];
  73. }
  74. // initialize the db connection with new options
  75. if (
  76. array_key_exists('dsn', $options) &&
  77. array_key_exists('usr', $options) &&
  78. array_key_exists('pwd', $options) &&
  79. array_key_exists('opt', $options)
  80. ) {
  81. // set default options
  82. $options['opt'][PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
  83. $options['opt'][PDO::ATTR_EMULATE_PREPARES] = false;
  84. $options['opt'][PDO::ATTR_PERSISTENT] = true;
  85. $db_tables_exist = true;
  86. // setup type and dabase connection
  87. self::$_type = strtolower(
  88. substr($options['dsn'], 0, strpos($options['dsn'], ':'))
  89. );
  90. $tableQuery = self::_getTableQuery(self::$_type);
  91. self::$_db = new PDO(
  92. $options['dsn'],
  93. $options['usr'],
  94. $options['pwd'],
  95. $options['opt']
  96. );
  97. // check if the database contains the required tables
  98. $tables = self::$_db->query($tableQuery)->fetchAll(PDO::FETCH_COLUMN, 0);
  99. // create paste table if necessary
  100. if (!in_array(self::_sanitizeIdentifier('paste'), $tables)) {
  101. self::_createPasteTable();
  102. $db_tables_exist = false;
  103. }
  104. // create comment table if necessary
  105. if (!in_array(self::_sanitizeIdentifier('comment'), $tables)) {
  106. self::_createCommentTable();
  107. $db_tables_exist = false;
  108. }
  109. // create config table if necessary
  110. $db_version = Controller::VERSION;
  111. if (!in_array(self::_sanitizeIdentifier('config'), $tables)) {
  112. self::_createConfigTable();
  113. // if we only needed to create the config table, the DB is older then 0.22
  114. if ($db_tables_exist) {
  115. $db_version = '0.21';
  116. }
  117. } else {
  118. $db_version = self::_getConfig('VERSION');
  119. }
  120. // update database structure if necessary
  121. if (version_compare($db_version, Controller::VERSION, '<')) {
  122. self::_upgradeDatabase($db_version);
  123. }
  124. } else {
  125. throw new Exception(
  126. 'Missing configuration for key dsn, usr, pwd or opt in the section model_options, please check your configuration file', 6
  127. );
  128. }
  129. }
  130. return self::$_instance;
  131. }
  132. /**
  133. * Create a paste.
  134. *
  135. * @access public
  136. * @param string $pasteid
  137. * @param array $paste
  138. * @return bool
  139. */
  140. public function create(string $pasteid, array $paste)
  141. {
  142. if (
  143. array_key_exists($pasteid, self::$_cache)
  144. ) {
  145. if (false !== self::$_cache[$pasteid]) {
  146. return false;
  147. } else {
  148. unset(self::$_cache[$pasteid]);
  149. }
  150. }
  151. $expire_date = 0;
  152. $opendiscussion = $burnafterreading = false;
  153. $attachment = $attachmentname = null;
  154. $meta = $paste['meta'];
  155. $isVersion1 = array_key_exists('data', $paste);
  156. list($createdKey) = self::_getVersionedKeys($isVersion1 ? 1 : 2);
  157. $created = (int) $meta[$createdKey];
  158. unset($meta[$createdKey], $paste['meta']);
  159. if (array_key_exists('expire_date', $meta)) {
  160. $expire_date = (int) $meta['expire_date'];
  161. unset($meta['expire_date']);
  162. }
  163. if (array_key_exists('opendiscussion', $meta)) {
  164. $opendiscussion = $meta['opendiscussion'];
  165. unset($meta['opendiscussion']);
  166. }
  167. if (array_key_exists('burnafterreading', $meta)) {
  168. $burnafterreading = $meta['burnafterreading'];
  169. unset($meta['burnafterreading']);
  170. }
  171. if ($isVersion1) {
  172. if (array_key_exists('attachment', $meta)) {
  173. $attachment = $meta['attachment'];
  174. unset($meta['attachment']);
  175. }
  176. if (array_key_exists('attachmentname', $meta)) {
  177. $attachmentname = $meta['attachmentname'];
  178. unset($meta['attachmentname']);
  179. }
  180. } else {
  181. $opendiscussion = $paste['adata'][2];
  182. $burnafterreading = $paste['adata'][3];
  183. }
  184. return self::_exec(
  185. 'INSERT INTO ' . self::_sanitizeIdentifier('paste') .
  186. ' VALUES(?,?,?,?,?,?,?,?,?)',
  187. array(
  188. $pasteid,
  189. $isVersion1 ? $paste['data'] : json_encode($paste),
  190. $created,
  191. $expire_date,
  192. (int) $opendiscussion,
  193. (int) $burnafterreading,
  194. json_encode($meta),
  195. $attachment,
  196. $attachmentname,
  197. )
  198. );
  199. }
  200. /**
  201. * Read a paste.
  202. *
  203. * @access public
  204. * @param string $pasteid
  205. * @return array|false
  206. */
  207. public function read(string $pasteid)
  208. {
  209. if (array_key_exists($pasteid, self::$_cache)) {
  210. return self::$_cache[$pasteid];
  211. }
  212. self::$_cache[$pasteid] = false;
  213. $paste = self::_select(
  214. 'SELECT * FROM ' . self::_sanitizeIdentifier('paste') .
  215. ' WHERE dataid = ?', array($pasteid), true
  216. );
  217. if ($paste === false) {
  218. return false;
  219. }
  220. // create array
  221. $data = json_decode($paste['data'], true);
  222. $isVersion2 = array_key_exists('v', $data) && $data['v'] >= 2;
  223. if ($isVersion2) {
  224. self::$_cache[$pasteid] = $data;
  225. list($createdKey) = self::_getVersionedKeys(2);
  226. } else {
  227. self::$_cache[$pasteid] = array('data' => $paste['data']);
  228. list($createdKey) = self::_getVersionedKeys(1);
  229. }
  230. $meta = json_decode($paste['meta'], true);
  231. if (!is_array($meta)) {
  232. $meta = array();
  233. }
  234. self::$_cache[$pasteid]['meta'] = $meta;
  235. self::$_cache[$pasteid]['meta'][$createdKey] = (int) $paste['postdate'];
  236. $expire_date = (int) $paste['expiredate'];
  237. if ($expire_date > 0) {
  238. self::$_cache[$pasteid]['meta']['expire_date'] = $expire_date;
  239. }
  240. if ($isVersion2) {
  241. return self::$_cache[$pasteid];
  242. }
  243. // support pre v1 attachments
  244. if (array_key_exists('attachment', $meta)) {
  245. self::$_cache[$pasteid]['attachment'] = $meta['attachment'];
  246. unset(self::$_cache[$pasteid]['meta']['attachment']);
  247. if (array_key_exists('attachmentname', $meta)) {
  248. self::$_cache[$pasteid]['attachmentname'] = $meta['attachmentname'];
  249. unset(self::$_cache[$pasteid]['meta']['attachmentname']);
  250. }
  251. }
  252. // support v1 attachments
  253. elseif (array_key_exists('attachment', $paste) && strlen($paste['attachment'])) {
  254. self::$_cache[$pasteid]['attachment'] = $paste['attachment'];
  255. if (array_key_exists('attachmentname', $paste) && strlen($paste['attachmentname'])) {
  256. self::$_cache[$pasteid]['attachmentname'] = $paste['attachmentname'];
  257. }
  258. }
  259. if ($paste['opendiscussion']) {
  260. self::$_cache[$pasteid]['meta']['opendiscussion'] = true;
  261. }
  262. if ($paste['burnafterreading']) {
  263. self::$_cache[$pasteid]['meta']['burnafterreading'] = true;
  264. }
  265. return self::$_cache[$pasteid];
  266. }
  267. /**
  268. * Delete a paste and its discussion.
  269. *
  270. * @access public
  271. * @param string $pasteid
  272. */
  273. public function delete(string $pasteid)
  274. {
  275. self::_exec(
  276. 'DELETE FROM ' . self::_sanitizeIdentifier('paste') .
  277. ' WHERE dataid = ?', array($pasteid)
  278. );
  279. self::_exec(
  280. 'DELETE FROM ' . self::_sanitizeIdentifier('comment') .
  281. ' WHERE pasteid = ?', array($pasteid)
  282. );
  283. if (
  284. array_key_exists($pasteid, self::$_cache)
  285. ) {
  286. unset(self::$_cache[$pasteid]);
  287. }
  288. }
  289. /**
  290. * Test if a paste exists.
  291. *
  292. * @access public
  293. * @param string $pasteid
  294. * @return bool
  295. */
  296. public function exists(string $pasteid)
  297. {
  298. if (
  299. !array_key_exists($pasteid, self::$_cache)
  300. ) {
  301. self::$_cache[$pasteid] = $this->read($pasteid);
  302. }
  303. return (bool) self::$_cache[$pasteid];
  304. }
  305. /**
  306. * Create a comment in a paste.
  307. *
  308. * @access public
  309. * @param string $pasteid
  310. * @param string $parentid
  311. * @param string $commentid
  312. * @param array $comment
  313. * @return bool
  314. */
  315. public function createComment(string $pasteid, string $parentid, string $commentid, array $comment)
  316. {
  317. if (array_key_exists('data', $comment)) {
  318. $version = 1;
  319. $data = $comment['data'];
  320. } else {
  321. $version = 2;
  322. $data = json_encode($comment);
  323. }
  324. list($createdKey, $iconKey) = self::_getVersionedKeys($version);
  325. $meta = $comment['meta'];
  326. unset($comment['meta']);
  327. foreach (array('nickname', $iconKey) as $key) {
  328. if (!array_key_exists($key, $meta)) {
  329. $meta[$key] = null;
  330. }
  331. }
  332. return self::_exec(
  333. 'INSERT INTO ' . self::_sanitizeIdentifier('comment') .
  334. ' VALUES(?,?,?,?,?,?,?)',
  335. array(
  336. $commentid,
  337. $pasteid,
  338. $parentid,
  339. $data,
  340. $meta['nickname'],
  341. $meta[$iconKey],
  342. $meta[$createdKey],
  343. )
  344. );
  345. }
  346. /**
  347. * Read all comments of paste.
  348. *
  349. * @access public
  350. * @param string $pasteid
  351. * @return array
  352. */
  353. public function readComments(string $pasteid)
  354. {
  355. $rows = self::_select(
  356. 'SELECT * FROM ' . self::_sanitizeIdentifier('comment') .
  357. ' WHERE pasteid = ?', array($pasteid)
  358. );
  359. // create comment list
  360. $comments = array();
  361. if (count($rows)) {
  362. foreach ($rows as $row) {
  363. $i = $this->getOpenSlot($comments, (int) $row['postdate']);
  364. $data = json_decode($row['data'], true);
  365. if (array_key_exists('v', $data) && $data['v'] >= 2) {
  366. $version = 2;
  367. $comments[$i] = $data;
  368. } else {
  369. $version = 1;
  370. $comments[$i] = array('data' => $row['data']);
  371. }
  372. list($createdKey, $iconKey) = self::_getVersionedKeys($version);
  373. $comments[$i]['id'] = $row['dataid'];
  374. $comments[$i]['parentid'] = $row['parentid'];
  375. $comments[$i]['meta'] = array($createdKey => (int) $row['postdate']);
  376. foreach (array('nickname' => 'nickname', 'vizhash' => $iconKey) as $rowKey => $commentKey) {
  377. if (array_key_exists($rowKey, $row) && !empty($row[$rowKey])) {
  378. $comments[$i]['meta'][$commentKey] = $row[$rowKey];
  379. }
  380. }
  381. }
  382. ksort($comments);
  383. }
  384. return $comments;
  385. }
  386. /**
  387. * Test if a comment exists.
  388. *
  389. * @access public
  390. * @param string $pasteid
  391. * @param string $parentid
  392. * @param string $commentid
  393. * @return bool
  394. */
  395. public function existsComment(string $pasteid, string $parentid, string $commentid)
  396. {
  397. return (bool) self::_select(
  398. 'SELECT dataid FROM ' . self::_sanitizeIdentifier('comment') .
  399. ' WHERE pasteid = ? AND parentid = ? AND dataid = ?',
  400. array($pasteid, $parentid, $commentid), true
  401. );
  402. }
  403. /**
  404. * Returns up to batch size number of paste ids that have expired
  405. *
  406. * @access private
  407. * @param int $batchsize
  408. * @return array
  409. */
  410. protected function _getExpiredPastes(int $batchsize)
  411. {
  412. $pastes = array();
  413. $rows = self::_select(
  414. 'SELECT dataid FROM ' . self::_sanitizeIdentifier('paste') .
  415. ' WHERE expiredate < ? AND expiredate != ? LIMIT ?',
  416. array(time(), 0, $batchsize)
  417. );
  418. if (count($rows)) {
  419. foreach ($rows as $row) {
  420. $pastes[] = $row['dataid'];
  421. }
  422. }
  423. return $pastes;
  424. }
  425. /**
  426. * execute a statement
  427. *
  428. * @access private
  429. * @static
  430. * @param string $sql
  431. * @param array $params
  432. * @throws PDOException
  433. * @return bool
  434. */
  435. private static function _exec(string $sql, array $params)
  436. {
  437. $statement = self::$_db->prepare($sql);
  438. $result = $statement->execute($params);
  439. $statement->closeCursor();
  440. return $result;
  441. }
  442. /**
  443. * run a select statement
  444. *
  445. * @access private
  446. * @static
  447. * @param string $sql
  448. * @param array $params
  449. * @param bool $firstOnly if only the first row should be returned
  450. * @throws PDOException
  451. * @return array
  452. */
  453. private static function _select(string $sql, array $params, bool $firstOnly = false)
  454. {
  455. $statement = self::$_db->prepare($sql);
  456. $statement->execute($params);
  457. $result = $firstOnly ?
  458. $statement->fetch(PDO::FETCH_ASSOC) :
  459. $statement->fetchAll(PDO::FETCH_ASSOC);
  460. $statement->closeCursor();
  461. return $result;
  462. }
  463. /**
  464. * get version dependent key names
  465. *
  466. * @access private
  467. * @static
  468. * @param int $version
  469. * @return array
  470. */
  471. private static function _getVersionedKeys(int $version)
  472. {
  473. if ($version === 1) {
  474. return array('postdate', 'vizhash');
  475. }
  476. return array('created', 'icon');
  477. }
  478. /**
  479. * get table list query, depending on the database type
  480. *
  481. * @access private
  482. * @static
  483. * @param string $type
  484. * @throws Exception
  485. * @return string
  486. */
  487. private static function _getTableQuery($type)
  488. {
  489. switch ($type) {
  490. case 'ibm':
  491. $sql = 'SELECT tabname FROM SYSCAT.TABLES ';
  492. break;
  493. case 'informix':
  494. $sql = 'SELECT tabname FROM systables ';
  495. break;
  496. case 'mssql':
  497. $sql = 'SELECT name FROM sysobjects '
  498. . "WHERE type = 'U' ORDER BY name";
  499. break;
  500. case 'mysql':
  501. $sql = 'SHOW TABLES';
  502. break;
  503. case 'oci':
  504. $sql = 'SELECT table_name FROM all_tables';
  505. break;
  506. case 'pgsql':
  507. $sql = 'SELECT c.relname AS table_name '
  508. . 'FROM pg_class c, pg_user u '
  509. . "WHERE c.relowner = u.usesysid AND c.relkind = 'r' "
  510. . 'AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) '
  511. . "AND c.relname !~ '^(pg_|sql_)' "
  512. . 'UNION '
  513. . 'SELECT c.relname AS table_name '
  514. . 'FROM pg_class c '
  515. . "WHERE c.relkind = 'r' "
  516. . 'AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) '
  517. . 'AND NOT EXISTS (SELECT 1 FROM pg_user WHERE usesysid = c.relowner) '
  518. . "AND c.relname !~ '^pg_'";
  519. break;
  520. case 'sqlite':
  521. $sql = "SELECT name FROM sqlite_master WHERE type='table' "
  522. . 'UNION ALL SELECT name FROM sqlite_temp_master '
  523. . "WHERE type='table' ORDER BY name";
  524. break;
  525. default:
  526. throw new Exception(
  527. "PDO type $type is currently not supported.", 5
  528. );
  529. }
  530. return $sql;
  531. }
  532. /**
  533. * get a value by key from the config table
  534. *
  535. * @access private
  536. * @static
  537. * @param string $key
  538. * @throws PDOException
  539. * @return string
  540. */
  541. private static function _getConfig($key)
  542. {
  543. $row = self::_select(
  544. 'SELECT value FROM ' . self::_sanitizeIdentifier('config') .
  545. ' WHERE id = ?', array($key), true
  546. );
  547. return $row['value'];
  548. }
  549. /**
  550. * get the primary key clauses, depending on the database driver
  551. *
  552. * @access private
  553. * @static
  554. * @param string $key
  555. * @return array
  556. */
  557. private static function _getPrimaryKeyClauses($key = 'dataid')
  558. {
  559. $main_key = $after_key = '';
  560. if (self::$_type === 'mysql') {
  561. $after_key = ", PRIMARY KEY ($key)";
  562. } else {
  563. $main_key = ' PRIMARY KEY';
  564. }
  565. return array($main_key, $after_key);
  566. }
  567. /**
  568. * get the data type, depending on the database driver
  569. *
  570. * @access private
  571. * @static
  572. * @return string
  573. */
  574. private static function _getDataType()
  575. {
  576. return self::$_type === 'pgsql' ? 'TEXT' : 'BLOB';
  577. }
  578. /**
  579. * get the attachment type, depending on the database driver
  580. *
  581. * @access private
  582. * @static
  583. * @return string
  584. */
  585. private static function _getAttachmentType()
  586. {
  587. return self::$_type === 'pgsql' ? 'TEXT' : 'MEDIUMBLOB';
  588. }
  589. /**
  590. * create the paste table
  591. *
  592. * @access private
  593. * @static
  594. */
  595. private static function _createPasteTable()
  596. {
  597. list($main_key, $after_key) = self::_getPrimaryKeyClauses();
  598. $dataType = self::_getDataType();
  599. self::$_db->exec(
  600. 'CREATE TABLE ' . self::_sanitizeIdentifier('paste') . ' ( ' .
  601. "dataid CHAR(16) NOT NULL$main_key, " .
  602. "data $dataType, " .
  603. 'postdate INT, ' .
  604. 'expiredate INT, ' .
  605. 'opendiscussion INT, ' .
  606. 'burnafterreading INT, ' .
  607. 'meta TEXT, ' .
  608. 'attachment ' . self::_getAttachmentType() . ', ' .
  609. "attachmentname $dataType$after_key );"
  610. );
  611. }
  612. /**
  613. * create the paste table
  614. *
  615. * @access private
  616. * @static
  617. */
  618. private static function _createCommentTable()
  619. {
  620. list($main_key, $after_key) = self::_getPrimaryKeyClauses();
  621. $dataType = self::_getDataType();
  622. self::$_db->exec(
  623. 'CREATE TABLE ' . self::_sanitizeIdentifier('comment') . ' ( ' .
  624. "dataid CHAR(16) NOT NULL$main_key, " .
  625. 'pasteid CHAR(16), ' .
  626. 'parentid CHAR(16), ' .
  627. "data $dataType, " .
  628. "nickname $dataType, " .
  629. "vizhash $dataType, " .
  630. "postdate INT$after_key );"
  631. );
  632. self::$_db->exec(
  633. 'CREATE INDEX IF NOT EXISTS comment_parent ON ' .
  634. self::_sanitizeIdentifier('comment') . '(pasteid);'
  635. );
  636. }
  637. /**
  638. * create the paste table
  639. *
  640. * @access private
  641. * @static
  642. */
  643. private static function _createConfigTable()
  644. {
  645. list($main_key, $after_key) = self::_getPrimaryKeyClauses('id');
  646. self::$_db->exec(
  647. 'CREATE TABLE ' . self::_sanitizeIdentifier('config') .
  648. " ( id CHAR(16) NOT NULL$main_key, value TEXT$after_key );"
  649. );
  650. self::_exec(
  651. 'INSERT INTO ' . self::_sanitizeIdentifier('config') .
  652. ' VALUES(?,?)',
  653. array('VERSION', Controller::VERSION)
  654. );
  655. }
  656. /**
  657. * sanitizes identifiers
  658. *
  659. * @access private
  660. * @static
  661. * @param string $identifier
  662. * @return string
  663. */
  664. private static function _sanitizeIdentifier($identifier)
  665. {
  666. return preg_replace('/[^A-Za-z0-9_]+/', '', self::$_prefix . $identifier);
  667. }
  668. /**
  669. * upgrade the database schema from an old version
  670. *
  671. * @access private
  672. * @static
  673. * @param string $oldversion
  674. */
  675. private static function _upgradeDatabase($oldversion)
  676. {
  677. $dataType = self::_getDataType();
  678. switch ($oldversion) {
  679. case '0.21':
  680. // create the meta column if necessary (pre 0.21 change)
  681. try {
  682. self::$_db->exec('SELECT meta FROM ' . self::_sanitizeIdentifier('paste') . ' LIMIT 1;');
  683. } catch (PDOException $e) {
  684. self::$_db->exec('ALTER TABLE ' . self::_sanitizeIdentifier('paste') . ' ADD COLUMN meta TEXT;');
  685. }
  686. // SQLite only allows one ALTER statement at a time...
  687. self::$_db->exec(
  688. 'ALTER TABLE ' . self::_sanitizeIdentifier('paste') .
  689. ' ADD COLUMN attachment ' . self::_getAttachmentType() . ';'
  690. );
  691. self::$_db->exec(
  692. 'ALTER TABLE ' . self::_sanitizeIdentifier('paste') . " ADD COLUMN attachmentname $dataType;"
  693. );
  694. // SQLite doesn't support MODIFY, but it allows TEXT of similar
  695. // size as BLOB, so there is no need to change it there
  696. if (self::$_type !== 'sqlite') {
  697. self::$_db->exec(
  698. 'ALTER TABLE ' . self::_sanitizeIdentifier('paste') .
  699. ' ADD PRIMARY KEY (dataid), MODIFY COLUMN data $dataType;'
  700. );
  701. self::$_db->exec(
  702. 'ALTER TABLE ' . self::_sanitizeIdentifier('comment') .
  703. " ADD PRIMARY KEY (dataid), MODIFY COLUMN data $dataType, " .
  704. "MODIFY COLUMN nickname $dataType, MODIFY COLUMN vizhash $dataType;"
  705. );
  706. } else {
  707. self::$_db->exec(
  708. 'CREATE UNIQUE INDEX IF NOT EXISTS paste_dataid ON ' .
  709. self::_sanitizeIdentifier('paste') . '(dataid);'
  710. );
  711. self::$_db->exec(
  712. 'CREATE UNIQUE INDEX IF NOT EXISTS comment_dataid ON ' .
  713. self::_sanitizeIdentifier('comment') . '(dataid);'
  714. );
  715. }
  716. self::$_db->exec(
  717. 'CREATE INDEX IF NOT EXISTS comment_parent ON ' .
  718. self::_sanitizeIdentifier('comment') . '(pasteid);'
  719. );
  720. // no break, continue with updates for 0.22 and later
  721. default:
  722. self::_exec(
  723. 'UPDATE ' . self::_sanitizeIdentifier('config') .
  724. ' SET value = ? WHERE id = ?',
  725. array(Controller::VERSION, 'VERSION')
  726. );
  727. }
  728. }
  729. }