1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34:
35: class Pastie_Driver_Sql extends Pastie_Driver
36: {
37: 38: 39: 40: 41:
42: protected $_params = array();
43:
44: 45: 46: 47: 48:
49: protected $_db;
50:
51: 52: 53: 54: 55: 56:
57: protected $_write_db;
58:
59: 60: 61: 62: 63:
64: protected $_connected = false;
65:
66: 67: 68: 69: 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';
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: 117: 118: 119: 120: 121:
122: public function getPaste($params)
123: {
124:
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';
144:
145:
146: $this->_connect();
147:
148:
149: Horde::logMessage(sprintf('Pastie_Driver_Sql#getPaste(): %s', $query), 'DEBUG');
150:
151:
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';
187:
188:
189: $this->_connect();
190:
191:
192: Horde::logMessage(sprintf('Pastie_Driver_Sql#getPastes(): %s', $query), 'DEBUG');
193:
194:
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:
227: $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
228: }
229: $result->free();
230:
231: return $pastes;
232: }
233:
234: 235: 236: 237: 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: 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: