PageRenderTime 56ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/pastie/lib/Driver/Sql.php

https://github.com/sgtcarneiro/horde
PHP | 260 lines | 145 code | 33 blank | 82 comment | 19 complexity | 11c42f0e29ac8ce2b07fb918555ebb8a MD5 | raw file
  1. <?php
  2. /**
  3. * Pastie storage implementation for PHP's PEAR database abstraction layer.
  4. *
  5. * Required values for $params:
  6. * <pre>
  7. * 'phptype' - The database type (e.g. 'pgsql', 'mysql', etc.).
  8. * 'table' - The name of the foo table in 'database'.
  9. * 'charset' - The database's internal charset.
  10. * </pre>
  11. *
  12. * Required by some database implementations:
  13. * <pre>
  14. * 'database' - The name of the database.
  15. * 'hostspec' - The hostname of the database server.
  16. * 'protocol' - The communication protocol ('tcp', 'unix', etc.).
  17. * 'username' - The username with which to connect to the database.
  18. * 'password' - The password associated with 'username'.
  19. * 'options' - Additional options to pass to the database.
  20. * 'tty' - The TTY on which to connect to the database.
  21. * 'port' - The port on which to connect to the database.
  22. * </pre>
  23. *
  24. * The table structure can be created by the scripts/sql/pastie_foo.sql
  25. * script.
  26. *
  27. * Copyright 2007-2011 The Horde Project (http://www.horde.org/)
  28. *
  29. * See the enclosed file COPYING for license information (BSD). If you
  30. * did not receive this file, see http://www.fsf.org/copyleft/bsd.html.
  31. *
  32. * @author Ben Klang <ben@alkaloid.net>
  33. * @package Pastie
  34. */
  35. class Pastie_Driver_Sql extends Pastie_Driver
  36. {
  37. /**
  38. * Hash containing connection parameters.
  39. *
  40. * @var array
  41. */
  42. protected $_params = array();
  43. /**
  44. * Handle for the current database connection.
  45. *
  46. * @var DB
  47. */
  48. protected $_db;
  49. /**
  50. * Handle for the current database connection, used for writing. Defaults
  51. * to the same handle as $_db if a separate write database is not required.
  52. *
  53. * @var DB
  54. */
  55. protected $_write_db;
  56. /**
  57. * Boolean indicating whether or not we're connected to the SQL server.
  58. *
  59. * @var boolean
  60. */
  61. protected $_connected = false;
  62. /**
  63. * Constructs a new SQL storage object.
  64. *
  65. * @param array $params A hash containing connection parameters.
  66. */
  67. public function __construct($params = array())
  68. {
  69. $this->_params = $params;
  70. }
  71. public function savePaste($bin, $paste, $syntax = 'none', $title = '')
  72. {
  73. $this->_connect();
  74. $id = $this->_db->nextId('mySequence');
  75. if (PEAR::isError($id)) {
  76. throw new Horde_Exception_Wrapped($id);
  77. }
  78. $uuid = new Horde_Support_Uuid();
  79. $bin = 'default'; // FIXME: Allow bins to be Horde_Shares
  80. $query = 'INSERT INTO pastie_pastes (paste_id, paste_uuid, ' .
  81. 'paste_bin, paste_title, paste_syntax, paste_content, ' .
  82. 'paste_owner, paste_timestamp) VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
  83. $values = array(
  84. $id,
  85. $uuid,
  86. $bin,
  87. $title,
  88. $syntax,
  89. $paste,
  90. $GLOBALS['registry']->getAuth(),
  91. time()
  92. );
  93. Horde::logMessage(sprintf('Pastie_Driver_Sql#savePaste(): %s', $query), 'DEBUG');
  94. Horde::logMessage(print_r($values, true), 'DEBUG');
  95. $result = $this->_write_db->query($query, $values);
  96. if ($result instanceof PEAR_Error) {
  97. Horde::logMessage($result, 'err');
  98. throw new Horde_Exception_Wrapped($result);
  99. }
  100. return $uuid;
  101. }
  102. /**
  103. * Retrieves the paste from the database.
  104. *
  105. * @param array $params Array of selectors to find the paste.
  106. *
  107. * @return array Array of paste information
  108. */
  109. public function getPaste($params)
  110. {
  111. // Right now we will accept 'id' or 'uuid'
  112. if (!isset($params['id']) && !isset($params['uuid'])) {
  113. Horde::logMessage('Error: must specify some kind of unique id.', 'err');
  114. throw new Pastie_Exception(_("Internal error. Details have been logged for the administrator."));
  115. }
  116. $query = 'SELECT paste_id, paste_uuid, paste_bin, paste_title, ' .
  117. 'paste_syntax, paste_content, paste_owner, paste_timestamp ' .
  118. 'FROM pastie_pastes ';
  119. $values = array();
  120. if (isset($params['id'])) {
  121. $query .= 'WHERE paste_id = ? ';
  122. $values[] = $params['id'];
  123. } elseif (isset($params['uuid'])) {
  124. $query .= 'WHERE paste_uuid = ? ';
  125. $values[] = $params['uuid'];
  126. }
  127. $query .= 'AND paste_bin = ?';
  128. $values[] = 'default'; // FIXME: Horde_Share
  129. /* Make sure we have a valid database connection. */
  130. $this->_connect();
  131. /* Log the query at a DEBUG log level. */
  132. Horde::logMessage(sprintf('Pastie_Driver_Sql#getPaste(): %s', $query), 'DEBUG');
  133. /* Execute the query. */
  134. $result = $this->_db->query($query, $values);
  135. if ($result instanceof PEAR_Error) {
  136. throw new Horde_Exception_Wrapped($result);
  137. }
  138. $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
  139. if ($row instanceof PEAR_Error) {
  140. throw new Horde_Exception_Wrapped($row);
  141. }
  142. $result->free();
  143. if ($row) {
  144. return array(
  145. 'id' => $row['paste_id'],
  146. 'uuid' => $row['paste_uuid'],
  147. 'bin' => $row['paste_bin'],
  148. 'title' => $row['paste_title'],
  149. 'syntax' => $row['paste_syntax'],
  150. 'paste' => $row['paste_content'],
  151. 'owner' => $row['paste_owner'],
  152. 'timestamp' => new Horde_Date($row['paste_timestamp'])
  153. );
  154. } else {
  155. throw new Pastie_Exception(_("Invalid paste ID."));
  156. }
  157. }
  158. public function getPastes($bin, $limit = null, $start = null)
  159. {
  160. $query = 'SELECT paste_id, paste_uuid, paste_bin, paste_title, ' .
  161. 'paste_syntax, paste_content, paste_owner, paste_timestamp ' .
  162. 'FROM pastie_pastes WHERE paste_bin = ? ' .
  163. 'ORDER BY paste_timestamp DESC';
  164. $values[] = 'default'; // FIXME: Horde_Share
  165. /* Make sure we have a valid database connection. */
  166. $this->_connect();
  167. /* Log the query at a DEBUG log level. */
  168. Horde::logMessage(sprintf('Pastie_Driver_Sql#getPastes(): %s', $query), 'DEBUG');
  169. /* Execute the query. */
  170. if ($limit !== null) {
  171. if ($start === null) {
  172. $start = 0;
  173. }
  174. $result = $this->_db->limitQuery($query, $start, $limit, $values);
  175. } else {
  176. $result = $this->_db->query($query, $values);
  177. }
  178. if ($result instanceof PEAR_Error) {
  179. throw new Horde_Exception_Wrapped($result);
  180. }
  181. $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
  182. if ($row instanceof PEAR_Error) {
  183. throw new Horde_Exception_Wrapped($row);
  184. }
  185. $pastes = array();
  186. while ($row && !($row instanceof PEAR_Error)) {
  187. $pastes[$row['paste_uuid']] = array(
  188. 'id' => $row['paste_id'],
  189. 'uuid' => $row['paste_uuid'],
  190. 'bin' => $row['paste_bin'],
  191. 'title' => $row['paste_title'],
  192. 'syntax' => $row['paste_syntax'],
  193. 'paste' => $row['paste_content'],
  194. 'owner' => $row['paste_owner'],
  195. 'timestamp' => new Horde_Date($row['paste_timestamp'])
  196. );
  197. /* Advance to the new row in the result set. */
  198. $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
  199. }
  200. $result->free();
  201. return $pastes;
  202. }
  203. /**
  204. * Attempts to open a persistent connection to the SQL server.
  205. *
  206. * @throws Horde_Exception
  207. */
  208. protected function _connect()
  209. {
  210. if (!$this->_connected) {
  211. $this->_db = $GLOBALS['injector']->getInstance('Horde_Core_Factory_DbPear')->create('read', 'pastie', 'storage');
  212. $this->_write_db = $GLOBALS['injector']->getInstance('Horde_Core_Factory_DbPear')->create('rw', 'pastie', 'storage');
  213. $this->_connected = true;
  214. }
  215. }
  216. /**
  217. * Disconnects from the SQL server and cleans up the connection.
  218. */
  219. protected function _disconnect()
  220. {
  221. if ($this->_connected) {
  222. $this->_connected = false;
  223. $this->_db->disconnect();
  224. $this->_write_db->disconnect();
  225. }
  226. }
  227. }