/usr/share/horde/mnemo/lib/Driver/Sql.php is in php-horde-mnemo 4.2.12-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 | <?php
/**
* Mnemo storage implementation for Horde's Horde_Db database abstraction
* layer.
*
* Copyright 2001-2016 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (ASL). If you
* did not receive this file, see http://www.horde.org/licenses/apache.
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Michael J. Rubinsky <mrubinsk@horde.org>
* @package Mnemo
*/
class Mnemo_Driver_Sql extends Mnemo_Driver
{
/**
* The database connection object.
*
* @var Horde_Db_Adapter
*/
protected $_db;
/**
* Share table name
*
* @var string
*/
protected $_table;
/**
* Charset
*
* @var string
*/
protected $_charset;
/**
* Column definition of body column.
*
* @var Horde_Db_Adapter_Base_Column
*/
protected $_column;
/**
* Construct a new SQL storage object.
*
* @param string $notepad The name of the notepad to load/save notes from.
* @param array $params The connection parameters
*
* @throws InvalidArguementException
*/
public function __construct($notepad, $params = array())
{
if (empty($params['db']) || empty($params['table'])) {
throw new InvalidArgumentException('Missing required connection parameter(s).');
}
$this->_notepad = $notepad;
$this->_db = $params['db'];
$this->_table = $params['table'];
$this->_charset = $params['charset'];
$this->_column = $this->_db->column($params['table'], 'memo_body');
}
/**
* Retrieves all of the notes of the current notepad from the backend.
*
* @throws Mnemo_Exception
*/
public function retrieve()
{
$query = sprintf('SELECT * FROM %s WHERE memo_owner = ?', $this->_table);
$values = array($this->_notepad);
try {
$rows = $this->_db->selectAll($query, $values);
} catch (Horde_Db_Exception $e) {
throw new Mnemo_Exception($e->getMessage());
}
// Store the retrieved values in a fresh list.
$this->_memos = array();
foreach ($rows as $row) {
$this->_memos[$row['memo_id']] = $this->_buildNote($row);
}
}
/**
* Retrieves one note from the backend.
*
* @param string $noteId The ID of the note to retrieve.
* @param string $passphrase A passphrase with which this note was
* supposed to be encrypted.
*
* @return array The array of note attributes.
* @throws Mnemo_Exception
* @throws Horde_Exception_NotFound
*/
public function get($noteId, $passphrase = null)
{
$query = 'SELECT * FROM ' . $this->_table .
' WHERE memo_owner = ? AND memo_id = ?';
$values = array($this->_notepad, $noteId);
try {
$row = $this->_db->selectOne($query, $values);
} catch (Horde_Db_Exception $e) {
throw new Mnemo_Exception($e->getMessage());
}
if (!$row) {
throw new Horde_Exception_NotFound();
}
return $this->_buildNote($row, $passphrase);
}
/**
* Retrieves one note from the backend by UID.
*
* @param string $uid The UID of the note to retrieve.
* @param string $passphrase A passphrase with which this note was
* supposed to be encrypted.
*
* @return array The array of note attributes.
* @throws Mnemo_Exception
* @throws Horde_Exception_NotFound
*/
public function getByUID($uid, $passphrase = null)
{
$query = 'SELECT * FROM ' . $this->_table . ' WHERE memo_uid = ?';
$values = array($uid);
try {
$row = $this->_db->selectOne($query, $values);
} catch (Horde_Db_Exception $e) {
throw new Mnemo_Exception($e->getMessage());
}
if (!count($row)) {
throw new Horde_Exception_NotFound('Not found');
}
$this->_notepad = $row['memo_owner'];
return $this->_buildNote($row, $passphrase);
}
/**
* Adds a note to the backend storage.
*
* @param string $noteId The ID of the new note.
* @param string $desc The first line of the note.
* @param string $body The whole note body.
* @param string $tags The tags of the note.
*
* @return string The unique ID of the new note.
* @throws Mnemo_Exception
*/
protected function _add($noteId, $desc, $body, $tags)
{
$uid = strval(new Horde_Support_Uuid());
$query = 'INSERT INTO ' . $this->_table
. ' (memo_owner, memo_id, memo_desc, memo_body, memo_uid)'
. ' VALUES (?, ?, ?, ?, ?)';
$values = array(
$this->_notepad,
$noteId,
Horde_String::convertCharset($desc, 'UTF-8', $this->_charset),
Horde_String::convertCharset($body, 'UTF-8', $this->_charset),
Horde_String::convertCharset($uid, 'UTF-8', $this->_charset)
);
try {
$this->_db->insert($query, $values);
} catch (Horde_Db_Exception $e) {
throw new Mnemo_Exception($e);
}
return $uid;
}
/**
* Modifies an existing note.
*
* @param string $noteId The note to modify.
* @param string $desc The first line of the note.
* @param string $body The whole note body.
* @param string $tags The tags of the note.
*
* @throws Mnemo_Exception
*/
protected function _modify($noteId, $desc, $body, $tags)
{
$query = 'UPDATE ' . $this->_table
. ' SET memo_desc = ?, memo_body = ?'
. ' WHERE memo_owner = ? AND memo_id = ?';
$values = array(
Horde_String::convertCharset($desc, 'UTF-8', $this->_charset),
Horde_String::convertCharset($body, 'UTF-8', $this->_charset),
$this->_notepad,
$noteId
);
try {
$this->_db->update($query, $values);
} catch (Horde_Db_Exception $e) {
throw new Mnemo_Exception($e);
}
$note = $this->get($noteId);
return $note['uid'];
}
/**
* Moves a note to a new notepad.
*
* @param string $noteId The note to move.
* @param string $newNotepad The new notepad.
*
* @return string The note's UID.
* @throws Mnemo_Exception
*/
protected function _move($noteId, $newNotepad)
{
// Get the note's details for use later.
$note = $this->get($noteId);
$query = 'UPDATE ' . $this->_table .
' SET memo_owner = ?' .
' WHERE memo_owner = ? AND memo_id = ?';
$values = array($newNotepad, $this->_notepad, $noteId);
try {
$result = $this->_db->update($query, $values);
} catch (Horde_Db_Exception $e) {
throw new Mnemo_Exception($e->getMessage());
}
return $note['uid'];
}
/**
* Deletes a note permanently.
*
* @param array $note The note to delete.
*
* @return string The note's UID.
* @throws Mnemo_Exception
*/
protected function _delete($noteId)
{
// Get the note's details for use later.
$note = $this->get($noteId);
$query = 'DELETE FROM ' . $this->_table .
' WHERE memo_owner = ? AND memo_id = ?';
$values = array($this->_notepad, $noteId);
try {
$this->_db->delete($query, $values);
} catch (Horde_Db_Exception $e) {
throw new Mnemo_Exception($e->getMessage());
}
return $note['uid'];
}
/**
* Deletes all notes from the current notepad.
*
* @return array An array of uids that have been removed.
* @throws Mnemo_Exception
*/
protected function _deleteAll()
{
// Get list of notes we are removing so we can tell history about it.
$query = sprintf('SELECT memo_uid FROM %s WHERE memo_owner = ?',
$this->_table);
$values = array($this->_notepad);
try {
$ids = $this->_db->selectValues($query, $values);
} catch (Horde_Db_Exception $e) {
throw new Mnemo_Exception($e->getMessage());
}
$query = sprintf('DELETE FROM %s WHERE memo_owner = ?', $this->_table);
try {
$this->_db->delete($query, $values);
} catch (Horde_Db_Exception $e) {
throw new Mnemo_Exception($e->getMessage());
}
return $ids;
}
/**
*
* @param array $row Hash of the note data, db keys.
* @param string $passphrase The encryption passphrase.
*
* @return array a Task hash.
* @throws Mnemo_Exception
*/
protected function _buildNote($row, $passphrase = null)
{
// Make sure notes always have a UID.
if (empty($row['memo_uid'])) {
$row['memo_uid'] = strval(new Horde_Support_Guid());
$query = 'UPDATE ' . $this->_table .
' SET memo_uid = ?' .
' WHERE memo_owner = ? AND memo_id = ?';
$values = array($row['memo_uid'], $row['memo_owner'], $row['memo_id']);
try {
$this->_db->update($query, $values);
} catch (Horde_Db_Exception $e) {
throw new Mnemo_Exception($e->getMessage());
}
}
// Decrypt note if requested.
$encrypted = false;
$body = $this->_column->binaryToString($row['memo_body']);
$body = Horde_String::convertCharset($body, $this->_charset, 'UTF-8');
if (strpos($body, '-----BEGIN PGP MESSAGE-----') === 0) {
$encrypted = true;
if (empty($passphrase)) {
$passphrase = Mnemo::getPassphrase($row['memo_id']);
}
if (empty($passphrase)) {
$body = new Mnemo_Exception(_("This note has been encrypted."), Mnemo::ERR_NO_PASSPHRASE);
} else {
try {
$body = $this->_decrypt($body, $passphrase);
$body = $body->message;
} catch (Mnemo_Exception $e) {
$body = $e;
}
Mnemo::storePassphrase($row['memo_id'], $passphrase);
}
}
// Create a new note based on $row's values.
$uid = Horde_String::convertCharset(
$row['memo_uid'], $this->_charset, 'UTF-8'
);
$memo = array(
'memolist_id' => $row['memo_owner'],
'memo_id' => $row['memo_id'],
'uid' => $uid,
'desc' => Horde_String::convertCharset(
$row['memo_desc'], $this->_charset, 'UTF-8'),
'body' => $body,
'tags' => $GLOBALS['injector']->getInstance('Mnemo_Tagger')->getTags($uid, 'note'),
'encrypted' => $encrypted);
try {
$userId = $GLOBALS['registry']->getAuth();
$log = $GLOBALS['injector']->getInstance('Horde_History')
->getHistory('mnemo:' . $row['memo_owner'] . ':' . $row['memo_uid']);
foreach ($log as $entry) {
switch ($entry['action']) {
case 'add':
$memo['created'] = new Horde_Date($entry['ts']);
if ($userId != $entry['who']) {
$memo['createdby'] = sprintf(_("by %s"), Mnemo::getUserName($entry['who']));
} else {
$memo['createdby'] = _("by me");
}
break;
case 'modify':
$memo['modified'] = new Horde_Date($entry['ts']);
if ($userId != $entry['who']) {
$memo['modifiedby'] = sprintf(_("by %s"), Mnemo::getUserName($entry['who']));
} else {
$memo['modifiedby'] = _("by me");
}
break;
}
}
} catch (Horde_Exception $e) {
}
return $memo;
}
/**
* Generates a local note ID.
*
* @return string A new note ID.
*/
protected function _generateId()
{
return strval(new Horde_Support_Randomid());
}
}
|