Database.php 24 KB

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