Overview

Packages

  • None
  • Pastie

Classes

  • PasteForm
  • Pastie
  • Pastie_Driver
  • Pastie_Driver_Sql
  • Overview
  • Package
  • Class
  • Tree
  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-2012 Horde LLC (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:     /**
 45:      * Handle for the current database connection.
 46:      *
 47:      * @var DB
 48:      */
 49:     protected $_db;
 50: 
 51:     /**
 52:      * Handle for the current database connection, used for writing. Defaults
 53:      * to the same handle as $_db if a separate write database is not required.
 54:      *
 55:      * @var DB
 56:      */
 57:     protected $_write_db;
 58: 
 59:     /**
 60:      * Boolean indicating whether or not we're connected to the SQL server.
 61:      *
 62:      * @var boolean
 63:      */
 64:     protected $_connected = false;
 65: 
 66:     /**
 67:      * Constructs a new SQL storage object.
 68:      *
 69:      * @param array $params  A hash containing connection parameters.
 70:      */
 71:     public function __construct($params = array())
 72:     {
 73:         $this->_params = $params;
 74:     }
 75: 
 76:     public function savePaste($bin, $paste, $syntax = 'none', $title = '')
 77:     {
 78:         $this->_connect();
 79: 
 80:         $id = $this->_db->nextId('mySequence');
 81:         if (PEAR::isError($id)) {
 82:             throw new Horde_Exception_Wrapped($id);
 83:         }
 84: 
 85:         $uuid = new Horde_Support_Uuid();
 86: 
 87:         $bin = 'default'; // FIXME: Allow bins to be Horde_Shares
 88: 
 89:         $query = 'INSERT INTO pastie_pastes (paste_id, paste_uuid, ' .
 90:                  'paste_bin, paste_title, paste_syntax, paste_content, ' .
 91:                  'paste_owner, paste_timestamp) VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
 92:         $values = array(
 93:                       $id,
 94:                       $uuid,
 95:                       $bin,
 96:                       $title,
 97:                       $syntax,
 98:                       $paste,
 99:                       $GLOBALS['registry']->getAuth(),
100:                       time()
101:         );
102: 
103:         Horde::logMessage(sprintf('Pastie_Driver_Sql#savePaste(): %s', $query), 'DEBUG');
104:         Horde::logMessage(print_r($values, true), 'DEBUG');
105: 
106:         $result = $this->_write_db->query($query, $values);
107:         if ($result instanceof PEAR_Error) {
108:             Horde::logMessage($result, 'err');
109:             throw new Horde_Exception_Wrapped($result);
110:         }
111: 
112:         return $uuid;
113:     }
114: 
115:     /**
116:      * Retrieves the paste from the database.
117:      *
118:      * @param array $params  Array of selectors to find the paste.
119:      *
120:      * @return array  Array of paste information
121:      */
122:     public function getPaste($params)
123:     {
124:         // Right now we will accept 'id' or 'uuid'
125:         if (!isset($params['id']) && !isset($params['uuid'])) {
126:             Horde::logMessage('Error: must specify some kind of unique id.', 'err');
127:             throw new Pastie_Exception(_("Internal error.  Details have been logged for the administrator."));
128:         }
129: 
130:         $query = 'SELECT paste_id, paste_uuid, paste_bin, paste_title, ' .
131:                  'paste_syntax, paste_content, paste_owner, paste_timestamp ' .
132:                  'FROM pastie_pastes ';
133:         $values = array();
134:         if (isset($params['id'])) {
135:             $query .= 'WHERE paste_id = ? ';
136:             $values[] = $params['id'];
137:         } elseif (isset($params['uuid'])) {
138:             $query .= 'WHERE paste_uuid = ? ';
139:             $values[] = $params['uuid'];
140:         }
141: 
142:         $query .= 'AND paste_bin = ?';
143:         $values[] = 'default'; // FIXME: Horde_Share
144: 
145:         /* Make sure we have a valid database connection. */
146:         $this->_connect();
147: 
148:         /* Log the query at a DEBUG log level. */
149:         Horde::logMessage(sprintf('Pastie_Driver_Sql#getPaste(): %s', $query), 'DEBUG');
150: 
151:         /* Execute the query. */
152:         $result = $this->_db->query($query, $values);
153: 
154:         if ($result instanceof PEAR_Error) {
155:             throw new Horde_Exception_Wrapped($result);
156:         }
157: 
158:         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
159:         if ($row instanceof PEAR_Error) {
160:             throw new Horde_Exception_Wrapped($row);
161:         }
162:         $result->free();
163: 
164:         if ($row) {
165:             return array(
166:                 'id' => $row['paste_id'],
167:                 'uuid' => $row['paste_uuid'],
168:                 'bin' => $row['paste_bin'],
169:                 'title' => $row['paste_title'],
170:                 'syntax' => $row['paste_syntax'],
171:                 'paste' => $row['paste_content'],
172:                 'owner' => $row['paste_owner'],
173:                 'timestamp' => new Horde_Date($row['paste_timestamp'])
174:             );
175:         } else {
176:             throw new Pastie_Exception(_("Invalid paste ID."));
177:         }
178:     }
179: 
180:     public function getPastes($bin, $limit = null, $start = null)
181:     {
182:         $query = 'SELECT paste_id, paste_uuid, paste_bin, paste_title, ' .
183:                  'paste_syntax, paste_content, paste_owner, paste_timestamp ' .
184:                  'FROM pastie_pastes WHERE paste_bin = ? ' .
185:                  'ORDER BY paste_timestamp DESC';
186:         $values[] = 'default'; // FIXME: Horde_Share
187: 
188:         /* Make sure we have a valid database connection. */
189:         $this->_connect();
190: 
191:         /* Log the query at a DEBUG log level. */
192:         Horde::logMessage(sprintf('Pastie_Driver_Sql#getPastes(): %s', $query), 'DEBUG');
193: 
194:         /* Execute the query. */
195:         if ($limit !== null) {
196:             if ($start === null) {
197:                 $start = 0;
198:             }
199:             $result = $this->_db->limitQuery($query, $start, $limit, $values);
200:         } else {
201:             $result = $this->_db->query($query, $values);
202:         }
203: 
204:         if ($result instanceof PEAR_Error) {
205:             throw new Horde_Exception_Wrapped($result);
206:         }
207: 
208:         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
209:         if ($row instanceof PEAR_Error) {
210:             throw new Horde_Exception_Wrapped($row);
211:         }
212: 
213:         $pastes = array();
214:         while ($row && !($row instanceof PEAR_Error)) {
215:             $pastes[$row['paste_uuid']] = array(
216:                 'id' => $row['paste_id'],
217:                 'uuid' => $row['paste_uuid'],
218:                 'bin' => $row['paste_bin'],
219:                 'title' => $row['paste_title'],
220:                 'syntax' => $row['paste_syntax'],
221:                 'paste' => $row['paste_content'],
222:                 'owner' => $row['paste_owner'],
223:                 'timestamp' => new Horde_Date($row['paste_timestamp'])
224:             );
225: 
226:             /* Advance to the new row in the result set. */
227:             $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
228:         }
229:         $result->free();
230: 
231:         return $pastes;
232:     }
233: 
234:     /**
235:      * Attempts to open a persistent connection to the SQL server.
236:      *
237:      * @throws Horde_Exception
238:      */
239:     protected function _connect()
240:     {
241:         if (!$this->_connected) {
242:             $this->_db = $GLOBALS['injector']->getInstance('Horde_Core_Factory_DbPear')->create('read', 'pastie', 'storage');
243:             $this->_write_db = $GLOBALS['injector']->getInstance('Horde_Core_Factory_DbPear')->create('rw', 'pastie', 'storage');
244:             $this->_connected = true;
245:         }
246:     }
247: 
248:     /**
249:      * Disconnects from the SQL server and cleans up the connection.
250:      */
251:     protected function _disconnect()
252:     {
253:         if ($this->_connected) {
254:             $this->_connected = false;
255:             $this->_db->disconnect();
256:             $this->_write_db->disconnect();
257:         }
258:     }
259: 
260: }
261: 
API documentation generated by ApiGen