Database.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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($pasteid, $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 = (bool) $meta['opendiscussion'];
  166. unset($meta['opendiscussion']);
  167. }
  168. if (array_key_exists('burnafterreading', $meta)) {
  169. $burnafterreading = (bool) $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. }
  182. return self::_exec(
  183. 'INSERT INTO ' . self::_sanitizeIdentifier('paste') .
  184. ' VALUES(?,?,?,?,?,?,?,?,?)',
  185. array(
  186. $pasteid,
  187. $isVersion1 ? $paste['data'] : json_encode($paste),
  188. $created,
  189. $expire_date,
  190. (int) $opendiscussion,
  191. (int) $burnafterreading,
  192. json_encode($meta),
  193. $attachment,
  194. $attachmentname,
  195. )
  196. );
  197. }
  198. /**
  199. * Read a paste.
  200. *
  201. * @access public
  202. * @param string $pasteid
  203. * @return stdClass|false
  204. */
  205. public function read($pasteid)
  206. {
  207. if (
  208. !array_key_exists($pasteid, self::$_cache)
  209. ) {
  210. self::$_cache[$pasteid] = false;
  211. $paste = self::_select(
  212. 'SELECT * FROM ' . self::_sanitizeIdentifier('paste') .
  213. ' WHERE dataid = ?', array($pasteid), true
  214. );
  215. if (false !== $paste) {
  216. // create object
  217. $data = json_decode($paste['data']);
  218. $isVersion2 = property_exists($data, 'v') && $data->v >= 2;
  219. if ($isVersion2) {
  220. self::$_cache[$pasteid] = $data;
  221. list($createdKey) = self::_getVersionedKeys(2);
  222. } else {
  223. self::$_cache[$pasteid] = new stdClass;
  224. self::$_cache[$pasteid]->data = $paste['data'];
  225. list($createdKey) = self::_getVersionedKeys(1);
  226. }
  227. $meta = json_decode($paste['meta']);
  228. if (!is_object($meta)) {
  229. $meta = new stdClass;
  230. }
  231. if (!$isVersion2) {
  232. // support pre v1 attachments
  233. if (property_exists($meta, 'attachment')) {
  234. self::$_cache[$pasteid]->attachment = $meta->attachment;
  235. unset($meta->attachment);
  236. if (property_exists($meta, 'attachmentname')) {
  237. self::$_cache[$pasteid]->attachmentname = $meta->attachmentname;
  238. unset($meta->attachmentname);
  239. }
  240. }
  241. // support v1 attachments
  242. elseif (array_key_exists('attachment', $paste) && strlen($paste['attachment'])) {
  243. self::$_cache[$pasteid]->attachment = $paste['attachment'];
  244. if (array_key_exists('attachmentname', $paste) && strlen($paste['attachmentname'])) {
  245. self::$_cache[$pasteid]->attachmentname = $paste['attachmentname'];
  246. }
  247. }
  248. }
  249. self::$_cache[$pasteid]->meta = $meta;
  250. self::$_cache[$pasteid]->meta->$createdKey = (int) $paste['postdate'];
  251. $expire_date = (int) $paste['expiredate'];
  252. if ($expire_date > 0) {
  253. self::$_cache[$pasteid]->meta->expire_date = $expire_date;
  254. }
  255. if ($paste['opendiscussion']) {
  256. self::$_cache[$pasteid]->meta->opendiscussion = true;
  257. }
  258. if ($paste['burnafterreading']) {
  259. self::$_cache[$pasteid]->meta->burnafterreading = true;
  260. }
  261. }
  262. }
  263. return self::$_cache[$pasteid];
  264. }
  265. /**
  266. * Delete a paste and its discussion.
  267. *
  268. * @access public
  269. * @param string $pasteid
  270. */
  271. public function delete($pasteid)
  272. {
  273. self::_exec(
  274. 'DELETE FROM ' . self::_sanitizeIdentifier('paste') .
  275. ' WHERE dataid = ?', array($pasteid)
  276. );
  277. self::_exec(
  278. 'DELETE FROM ' . self::_sanitizeIdentifier('comment') .
  279. ' WHERE pasteid = ?', array($pasteid)
  280. );
  281. if (
  282. array_key_exists($pasteid, self::$_cache)
  283. ) {
  284. unset(self::$_cache[$pasteid]);
  285. }
  286. }
  287. /**
  288. * Test if a paste exists.
  289. *
  290. * @access public
  291. * @param string $pasteid
  292. * @return bool
  293. */
  294. public function exists($pasteid)
  295. {
  296. if (
  297. !array_key_exists($pasteid, self::$_cache)
  298. ) {
  299. self::$_cache[$pasteid] = $this->read($pasteid);
  300. }
  301. return (bool) self::$_cache[$pasteid];
  302. }
  303. /**
  304. * Create a comment in a paste.
  305. *
  306. * @access public
  307. * @param string $pasteid
  308. * @param string $parentid
  309. * @param string $commentid
  310. * @param array $comment
  311. * @return bool
  312. */
  313. public function createComment($pasteid, $parentid, $commentid, $comment)
  314. {
  315. if (array_key_exists('data', $comment)) {
  316. $version = 1;
  317. $data = $comment['data'];
  318. } else {
  319. $version = 2;
  320. $data = json_encode($comment);
  321. }
  322. list($createdKey, $iconKey) = self::_getVersionedKeys($version);
  323. $meta = $comment['meta'];
  324. unset($comment['meta']);
  325. foreach (array('nickname', $iconKey) as $key) {
  326. if (!array_key_exists($key, $meta)) {
  327. $meta[$key] = null;
  328. }
  329. }
  330. return self::_exec(
  331. 'INSERT INTO ' . self::_sanitizeIdentifier('comment') .
  332. ' VALUES(?,?,?,?,?,?,?)',
  333. array(
  334. $commentid,
  335. $pasteid,
  336. $parentid,
  337. $data,
  338. $meta['nickname'],
  339. $meta[$iconKey],
  340. $meta[$createdKey],
  341. )
  342. );
  343. }
  344. /**
  345. * Read all comments of paste.
  346. *
  347. * @access public
  348. * @param string $pasteid
  349. * @return array
  350. */
  351. public function readComments($pasteid)
  352. {
  353. $rows = self::_select(
  354. 'SELECT * FROM ' . self::_sanitizeIdentifier('comment') .
  355. ' WHERE pasteid = ?', array($pasteid)
  356. );
  357. // create comment list
  358. $comments = array();
  359. if (count($rows)) {
  360. foreach ($rows as $row) {
  361. $i = $this->getOpenSlot($comments, (int) $row['postdate']);
  362. $data = json_decode($row['data']);
  363. if (property_exists($data, 'v') && $data->v >= 2) {
  364. $version = 2;
  365. $comments[$i] = $data;
  366. } else {
  367. $version = 1;
  368. $comments[$i] = new stdClass;
  369. $comments[$i]->data = $row['data'];
  370. }
  371. list($createdKey, $iconKey) = self::_getVersionedKeys($version);
  372. $comments[$i]->id = $row['dataid'];
  373. $comments[$i]->parentid = $row['parentid'];
  374. $comments[$i]->meta = new stdClass;
  375. $comments[$i]->meta->$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($pasteid, $parentid, $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($batchsize)
  411. {
  412. $pastes = array();
  413. $rows = self::_select(
  414. 'SELECT dataid FROM ' . self::_sanitizeIdentifier('paste') .
  415. ' WHERE expiredate < ? AND expiredate != ? LIMIT ?', array(time(), 0, $batchsize)
  416. );
  417. if (count($rows)) {
  418. foreach ($rows as $row) {
  419. $pastes[] = $row['dataid'];
  420. }
  421. }
  422. return $pastes;
  423. }
  424. /**
  425. * execute a statement
  426. *
  427. * @access private
  428. * @static
  429. * @param string $sql
  430. * @param array $params
  431. * @throws PDOException
  432. * @return bool
  433. */
  434. private static function _exec($sql, array $params)
  435. {
  436. $statement = self::$_db->prepare($sql);
  437. $result = $statement->execute($params);
  438. $statement->closeCursor();
  439. return $result;
  440. }
  441. /**
  442. * run a select statement
  443. *
  444. * @access private
  445. * @static
  446. * @param string $sql
  447. * @param array $params
  448. * @param bool $firstOnly if only the first row should be returned
  449. * @throws PDOException
  450. * @return array
  451. */
  452. private static function _select($sql, array $params, $firstOnly = false)
  453. {
  454. $statement = self::$_db->prepare($sql);
  455. $statement->execute($params);
  456. $result = $firstOnly ?
  457. $statement->fetch(PDO::FETCH_ASSOC) :
  458. $statement->fetchAll(PDO::FETCH_ASSOC);
  459. $statement->closeCursor();
  460. return $result;
  461. }
  462. /**
  463. * get version dependent key names
  464. *
  465. * @access private
  466. * @static
  467. * @param int $version
  468. * @return array
  469. */
  470. private static function _getVersionedKeys(int $version)
  471. {
  472. if ($version === 1) {
  473. return array('postdate', 'vizhash');
  474. }
  475. return array('created', 'icon');
  476. }
  477. /**
  478. * get table list query, depending on the database type
  479. *
  480. * @access private
  481. * @static
  482. * @param string $type
  483. * @throws Exception
  484. * @return string
  485. */
  486. private static function _getTableQuery($type)
  487. {
  488. switch ($type) {
  489. case 'ibm':
  490. $sql = 'SELECT tabname FROM SYSCAT.TABLES ';
  491. break;
  492. case 'informix':
  493. $sql = 'SELECT tabname FROM systables ';
  494. break;
  495. case 'mssql':
  496. $sql = 'SELECT name FROM sysobjects '
  497. . "WHERE type = 'U' ORDER BY name";
  498. break;
  499. case 'mysql':
  500. $sql = 'SHOW TABLES';
  501. break;
  502. case 'oci':
  503. $sql = 'SELECT table_name FROM all_tables';
  504. break;
  505. case 'pgsql':
  506. $sql = 'SELECT c.relname AS table_name '
  507. . 'FROM pg_class c, pg_user u '
  508. . "WHERE c.relowner = u.usesysid AND c.relkind = 'r' "
  509. . 'AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) '
  510. . "AND c.relname !~ '^(pg_|sql_)' "
  511. . 'UNION '
  512. . 'SELECT c.relname AS table_name '
  513. . 'FROM pg_class c '
  514. . "WHERE c.relkind = 'r' "
  515. . 'AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) '
  516. . 'AND NOT EXISTS (SELECT 1 FROM pg_user WHERE usesysid = c.relowner) '
  517. . "AND c.relname !~ '^pg_'";
  518. break;
  519. case 'sqlite':
  520. $sql = "SELECT name FROM sqlite_master WHERE type='table' "
  521. . 'UNION ALL SELECT name FROM sqlite_temp_master '
  522. . "WHERE type='table' ORDER BY name";
  523. break;
  524. default:
  525. throw new Exception(
  526. "PDO type $type is currently not supported.", 5
  527. );
  528. }
  529. return $sql;
  530. }
  531. /**
  532. * get a value by key from the config table
  533. *
  534. * @access private
  535. * @static
  536. * @param string $key
  537. * @throws PDOException
  538. * @return string
  539. */
  540. private static function _getConfig($key)
  541. {
  542. $row = self::_select(
  543. 'SELECT value FROM ' . self::_sanitizeIdentifier('config') .
  544. ' WHERE id = ?', array($key), true
  545. );
  546. return $row['value'];
  547. }
  548. /**
  549. * get the primary key clauses, depending on the database driver
  550. *
  551. * @access private
  552. * @static
  553. * @param string $key
  554. * @return array
  555. */
  556. private static function _getPrimaryKeyClauses($key = 'dataid')
  557. {
  558. $main_key = $after_key = '';
  559. if (self::$_type === 'mysql') {
  560. $after_key = ", PRIMARY KEY ($key)";
  561. } else {
  562. $main_key = ' PRIMARY KEY';
  563. }
  564. return array($main_key, $after_key);
  565. }
  566. /**
  567. * get the data type, depending on the database driver
  568. *
  569. * @access private
  570. * @static
  571. * @return string
  572. */
  573. private static function _getDataType()
  574. {
  575. return self::$_type === 'pgsql' ? 'TEXT' : 'BLOB';
  576. }
  577. /**
  578. * get the attachment type, depending on the database driver
  579. *
  580. * @access private
  581. * @static
  582. * @return string
  583. */
  584. private static function _getAttachmentType()
  585. {
  586. return self::$_type === 'pgsql' ? 'TEXT' : 'MEDIUMBLOB';
  587. }
  588. /**
  589. * create the paste table
  590. *
  591. * @access private
  592. * @static
  593. */
  594. private static function _createPasteTable()
  595. {
  596. list($main_key, $after_key) = self::_getPrimaryKeyClauses();
  597. $dataType = self::_getDataType();
  598. self::$_db->exec(
  599. 'CREATE TABLE ' . self::_sanitizeIdentifier('paste') . ' ( ' .
  600. "dataid CHAR(16) NOT NULL$main_key, " .
  601. "data $dataType, " .
  602. 'postdate INT, ' .
  603. 'expiredate INT, ' .
  604. 'opendiscussion INT, ' .
  605. 'burnafterreading INT, ' .
  606. 'meta TEXT, ' .
  607. 'attachment ' . self::_getAttachmentType() . ', ' .
  608. "attachmentname $dataType$after_key );"
  609. );
  610. }
  611. /**
  612. * create the paste table
  613. *
  614. * @access private
  615. * @static
  616. */
  617. private static function _createCommentTable()
  618. {
  619. list($main_key, $after_key) = self::_getPrimaryKeyClauses();
  620. $dataType = self::_getDataType();
  621. self::$_db->exec(
  622. 'CREATE TABLE ' . self::_sanitizeIdentifier('comment') . ' ( ' .
  623. "dataid CHAR(16) NOT NULL$main_key, " .
  624. 'pasteid CHAR(16), ' .
  625. 'parentid CHAR(16), ' .
  626. "data $dataType, " .
  627. "nickname $dataType, " .
  628. "vizhash $dataType, " .
  629. "postdate INT$after_key );"
  630. );
  631. self::$_db->exec(
  632. 'CREATE INDEX IF NOT EXISTS comment_parent ON ' .
  633. self::_sanitizeIdentifier('comment') . '(pasteid);'
  634. );
  635. }
  636. /**
  637. * create the paste table
  638. *
  639. * @access private
  640. * @static
  641. */
  642. private static function _createConfigTable()
  643. {
  644. list($main_key, $after_key) = self::_getPrimaryKeyClauses('id');
  645. self::$_db->exec(
  646. 'CREATE TABLE ' . self::_sanitizeIdentifier('config') .
  647. " ( id CHAR(16) NOT NULL$main_key, value TEXT$after_key );"
  648. );
  649. self::_exec(
  650. 'INSERT INTO ' . self::_sanitizeIdentifier('config') .
  651. ' VALUES(?,?)',
  652. array('VERSION', Controller::VERSION)
  653. );
  654. }
  655. /**
  656. * sanitizes identifiers
  657. *
  658. * @access private
  659. * @static
  660. * @param string $identifier
  661. * @return string
  662. */
  663. private static function _sanitizeIdentifier($identifier)
  664. {
  665. return preg_replace('/[^A-Za-z0-9_]+/', '', self::$_prefix . $identifier);
  666. }
  667. /**
  668. * upgrade the database schema from an old version
  669. *
  670. * @access private
  671. * @static
  672. * @param string $oldversion
  673. */
  674. private static function _upgradeDatabase($oldversion)
  675. {
  676. $dataType = self::_getDataType();
  677. switch ($oldversion) {
  678. case '0.21':
  679. // create the meta column if necessary (pre 0.21 change)
  680. try {
  681. self::$_db->exec('SELECT meta FROM ' . self::_sanitizeIdentifier('paste') . ' LIMIT 1;');
  682. } catch (PDOException $e) {
  683. self::$_db->exec('ALTER TABLE ' . self::_sanitizeIdentifier('paste') . ' ADD COLUMN meta TEXT;');
  684. }
  685. // SQLite only allows one ALTER statement at a time...
  686. self::$_db->exec(
  687. 'ALTER TABLE ' . self::_sanitizeIdentifier('paste') .
  688. ' ADD COLUMN attachment ' . self::_getAttachmentType() . ';'
  689. );
  690. self::$_db->exec(
  691. 'ALTER TABLE ' . self::_sanitizeIdentifier('paste') . " ADD COLUMN attachmentname $dataType;"
  692. );
  693. // SQLite doesn't support MODIFY, but it allows TEXT of similar
  694. // size as BLOB, so there is no need to change it there
  695. if (self::$_type !== 'sqlite') {
  696. self::$_db->exec(
  697. 'ALTER TABLE ' . self::_sanitizeIdentifier('paste') .
  698. ' ADD PRIMARY KEY (dataid), MODIFY COLUMN data $dataType;'
  699. );
  700. self::$_db->exec(
  701. 'ALTER TABLE ' . self::_sanitizeIdentifier('comment') .
  702. " ADD PRIMARY KEY (dataid), MODIFY COLUMN data $dataType, " .
  703. "MODIFY COLUMN nickname $dataType, MODIFY COLUMN vizhash $dataType;"
  704. );
  705. } else {
  706. self::$_db->exec(
  707. 'CREATE UNIQUE INDEX IF NOT EXISTS paste_dataid ON ' .
  708. self::_sanitizeIdentifier('paste') . '(dataid);'
  709. );
  710. self::$_db->exec(
  711. 'CREATE UNIQUE INDEX IF NOT EXISTS comment_dataid ON ' .
  712. self::_sanitizeIdentifier('comment') . '(dataid);'
  713. );
  714. }
  715. self::$_db->exec(
  716. 'CREATE INDEX IF NOT EXISTS comment_parent ON ' .
  717. self::_sanitizeIdentifier('comment') . '(pasteid);'
  718. );
  719. // no break, continue with updates for 0.22 and later
  720. default:
  721. self::_exec(
  722. 'UPDATE ' . self::_sanitizeIdentifier('config') .
  723. ' SET value = ? WHERE id = ?',
  724. array(Controller::VERSION, 'VERSION')
  725. );
  726. }
  727. }
  728. }