Database.php 25 KB

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