| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * abook_database.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2004 The SquirrelMail Project Team |
| 7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
| 8 | * |
| 9 | * Backend for personal addressbook stored in a database, |
| 10 | * accessed using the DB-classes in PEAR. |
| 11 | * |
| 12 | * IMPORTANT: The PEAR modules must be in the include path |
| 13 | * for this class to work. |
| 14 | * |
| 15 | * An array with the following elements must be passed to |
| 16 | * the class constructor (elements marked ? are optional): |
| 17 | * |
| 18 | * dsn => database DNS (see PEAR for syntax) |
| 19 | * table => table to store addresses in (must exist) |
| 20 | * owner => current user (owner of address data) |
| 21 | * ? writeable => set writeable flag (true/false) |
| 22 | * |
| 23 | * The table used should have the following columns: |
| 24 | * owner, nickname, firstname, lastname, email, label |
| 25 | * The pair (owner,nickname) should be unique (primary key). |
| 26 | * |
| 27 | * NOTE. This class should not be used directly. Use the |
| 28 | * "AddressBook" class instead. |
| 29 | * |
| 30 | * @version $Id$ |
| 31 | * @package squirrelmail |
| 32 | * @subpackage addressbook |
| 33 | */ |
| 34 | |
| 35 | /** Needs the DB functions */ |
| 36 | require_once('DB.php'); |
| 37 | |
| 38 | /** |
| 39 | * Undocumented class - stores the addressbook in a sql database |
| 40 | * @package squirrelmail |
| 41 | */ |
| 42 | class abook_database extends addressbook_backend { |
| 43 | var $btype = 'local'; |
| 44 | var $bname = 'database'; |
| 45 | |
| 46 | var $dsn = ''; |
| 47 | var $table = ''; |
| 48 | var $owner = ''; |
| 49 | var $dbh = false; |
| 50 | |
| 51 | var $writeable = true; |
| 52 | |
| 53 | /* ========================== Private ======================= */ |
| 54 | |
| 55 | /* Constructor */ |
| 56 | function abook_database($param) { |
| 57 | $this->sname = _("Personal address book"); |
| 58 | |
| 59 | if (is_array($param)) { |
| 60 | if (empty($param['dsn']) || |
| 61 | empty($param['table']) || |
| 62 | empty($param['owner'])) { |
| 63 | return $this->set_error('Invalid parameters'); |
| 64 | } |
| 65 | |
| 66 | $this->dsn = $param['dsn']; |
| 67 | $this->table = $param['table']; |
| 68 | $this->owner = $param['owner']; |
| 69 | |
| 70 | if (!empty($param['name'])) { |
| 71 | $this->sname = $param['name']; |
| 72 | } |
| 73 | |
| 74 | if (isset($param['writeable'])) { |
| 75 | $this->writeable = $param['writeable']; |
| 76 | } |
| 77 | |
| 78 | if (isset($param['listing'])) { |
| 79 | $this->listing = $param['listing']; |
| 80 | } |
| 81 | |
| 82 | $this->open(true); |
| 83 | } |
| 84 | else { |
| 85 | return $this->set_error('Invalid argument to constructor'); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /* Open the database. New connection if $new is true */ |
| 91 | function open($new = false) { |
| 92 | $this->error = ''; |
| 93 | |
| 94 | /* Return true is file is open and $new is unset */ |
| 95 | if ($this->dbh && !$new) { |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | /* Close old file, if any */ |
| 100 | if ($this->dbh) { |
| 101 | $this->close(); |
| 102 | } |
| 103 | |
| 104 | $dbh = DB::connect($this->dsn, true); |
| 105 | |
| 106 | if (DB::isError($dbh)) { |
| 107 | return $this->set_error(sprintf(_("Database error: %s"), |
| 108 | DB::errorMessage($dbh))); |
| 109 | } |
| 110 | |
| 111 | $this->dbh = $dbh; |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | /* Close the file and forget the filehandle */ |
| 116 | function close() { |
| 117 | $this->dbh->disconnect(); |
| 118 | $this->dbh = false; |
| 119 | } |
| 120 | |
| 121 | /* ========================== Public ======================== */ |
| 122 | |
| 123 | /* Search the file */ |
| 124 | function &search($expr) { |
| 125 | $ret = array(); |
| 126 | if(!$this->open()) { |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | /* To be replaced by advanded search expression parsing */ |
| 131 | if (is_array($expr)) { |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | /* Make regexp from glob'ed expression */ |
| 136 | $expr = str_replace('?', '_', $expr); |
| 137 | $expr = str_replace('*', '%', $expr); |
| 138 | $expr = $this->dbh->quoteString($expr); |
| 139 | $expr = "%$expr%"; |
| 140 | |
| 141 | $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND " . |
| 142 | "(firstname LIKE '%s' OR lastname LIKE '%s')", |
| 143 | $this->table, $this->owner, $expr, $expr); |
| 144 | $res = $this->dbh->query($query); |
| 145 | |
| 146 | if (DB::isError($res)) { |
| 147 | return $this->set_error(sprintf(_("Database error: %s"), |
| 148 | DB::errorMessage($res))); |
| 149 | } |
| 150 | |
| 151 | while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) { |
| 152 | array_push($ret, array('nickname' => $row['nickname'], |
| 153 | 'name' => "$row[firstname] $row[lastname]", |
| 154 | 'firstname' => $row['firstname'], |
| 155 | 'lastname' => $row['lastname'], |
| 156 | 'email' => $row['email'], |
| 157 | 'label' => $row['label'], |
| 158 | 'backend' => $this->bnum, |
| 159 | 'source' => &$this->sname)); |
| 160 | } |
| 161 | return $ret; |
| 162 | } |
| 163 | |
| 164 | /* Lookup alias */ |
| 165 | function &lookup($alias) { |
| 166 | if (empty($alias)) { |
| 167 | return array(); |
| 168 | } |
| 169 | |
| 170 | $alias = strtolower($alias); |
| 171 | |
| 172 | if (!$this->open()) { |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND LOWER(nickname)='%s'", |
| 177 | $this->table, $this->owner, $this->dbh->quoteString($alias)); |
| 178 | |
| 179 | $res = $this->dbh->query($query); |
| 180 | |
| 181 | if (DB::isError($res)) { |
| 182 | return $this->set_error(sprintf(_("Database error: %s"), |
| 183 | DB::errorMessage($res))); |
| 184 | } |
| 185 | |
| 186 | if ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) { |
| 187 | return array('nickname' => $row['nickname'], |
| 188 | 'name' => "$row[firstname] $row[lastname]", |
| 189 | 'firstname' => $row['firstname'], |
| 190 | 'lastname' => $row['lastname'], |
| 191 | 'email' => $row['email'], |
| 192 | 'label' => $row['label'], |
| 193 | 'backend' => $this->bnum, |
| 194 | 'source' => &$this->sname); |
| 195 | } |
| 196 | return array(); |
| 197 | } |
| 198 | |
| 199 | /* List all addresses */ |
| 200 | function &list_addr() { |
| 201 | $ret = array(); |
| 202 | if (!$this->open()) { |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | if(isset($this->listing) && !$this->listing) { |
| 207 | return array(); |
| 208 | } |
| 209 | |
| 210 | |
| 211 | $query = sprintf("SELECT * FROM %s WHERE owner='%s'", |
| 212 | $this->table, $this->owner); |
| 213 | |
| 214 | $res = $this->dbh->query($query); |
| 215 | |
| 216 | if (DB::isError($res)) { |
| 217 | return $this->set_error(sprintf(_("Database error: %s"), |
| 218 | DB::errorMessage($res))); |
| 219 | } |
| 220 | |
| 221 | while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) { |
| 222 | array_push($ret, array('nickname' => $row['nickname'], |
| 223 | 'name' => "$row[firstname] $row[lastname]", |
| 224 | 'firstname' => $row['firstname'], |
| 225 | 'lastname' => $row['lastname'], |
| 226 | 'email' => $row['email'], |
| 227 | 'label' => $row['label'], |
| 228 | 'backend' => $this->bnum, |
| 229 | 'source' => &$this->sname)); |
| 230 | } |
| 231 | return $ret; |
| 232 | } |
| 233 | |
| 234 | /* Add address */ |
| 235 | function add($userdata) { |
| 236 | if (!$this->writeable) { |
| 237 | return $this->set_error(_("Addressbook is read-only")); |
| 238 | } |
| 239 | |
| 240 | if (!$this->open()) { |
| 241 | return false; |
| 242 | } |
| 243 | |
| 244 | /* See if user exist already */ |
| 245 | $ret = $this->lookup($userdata['nickname']); |
| 246 | if (!empty($ret)) { |
| 247 | return $this->set_error(sprintf(_("User '%s' already exist"), |
| 248 | $ret['nickname'])); |
| 249 | } |
| 250 | |
| 251 | /* Create query */ |
| 252 | $query = sprintf("INSERT INTO %s (owner, nickname, firstname, " . |
| 253 | "lastname, email, label) VALUES('%s','%s','%s'," . |
| 254 | "'%s','%s','%s')", |
| 255 | $this->table, $this->owner, |
| 256 | $this->dbh->quoteString($userdata['nickname']), |
| 257 | $this->dbh->quoteString($userdata['firstname']), |
| 258 | $this->dbh->quoteString($userdata['lastname']), |
| 259 | $this->dbh->quoteString($userdata['email']), |
| 260 | $this->dbh->quoteString($userdata['label']) ); |
| 261 | |
| 262 | /* Do the insert */ |
| 263 | $r = $this->dbh->simpleQuery($query); |
| 264 | if ($r == DB_OK) { |
| 265 | return true; |
| 266 | } |
| 267 | |
| 268 | /* Fail */ |
| 269 | return $this->set_error(sprintf(_("Database error: %s"), |
| 270 | DB::errorMessage($r))); |
| 271 | } |
| 272 | |
| 273 | /* Delete address */ |
| 274 | function remove($alias) { |
| 275 | if (!$this->writeable) { |
| 276 | return $this->set_error(_("Addressbook is read-only")); |
| 277 | } |
| 278 | |
| 279 | if (!$this->open()) { |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | /* Create query */ |
| 284 | $query = sprintf("DELETE FROM %s WHERE owner='%s' AND (", |
| 285 | $this->table, $this->owner); |
| 286 | |
| 287 | $sepstr = ''; |
| 288 | while (list($undef, $nickname) = each($alias)) { |
| 289 | $query .= sprintf("%s nickname='%s' ", $sepstr, |
| 290 | $this->dbh->quoteString($nickname)); |
| 291 | $sepstr = 'OR'; |
| 292 | } |
| 293 | $query .= ')'; |
| 294 | |
| 295 | /* Delete entry */ |
| 296 | $r = $this->dbh->simpleQuery($query); |
| 297 | if ($r == DB_OK) { |
| 298 | return true; |
| 299 | } |
| 300 | |
| 301 | /* Fail */ |
| 302 | return $this->set_error(sprintf(_("Database error: %s"), |
| 303 | DB::errorMessage($r))); |
| 304 | } |
| 305 | |
| 306 | /* Modify address */ |
| 307 | function modify($alias, $userdata) { |
| 308 | if (!$this->writeable) { |
| 309 | return $this->set_error(_("Addressbook is read-only")); |
| 310 | } |
| 311 | |
| 312 | if (!$this->open()) { |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | /* See if user exist */ |
| 317 | $ret = $this->lookup($alias); |
| 318 | if (empty($ret)) { |
| 319 | return $this->set_error(sprintf(_("User '%s' does not exist"), |
| 320 | $alias)); |
| 321 | } |
| 322 | |
| 323 | /* Create query */ |
| 324 | $query = sprintf("UPDATE %s SET nickname='%s', firstname='%s', ". |
| 325 | "lastname='%s', email='%s', label='%s' ". |
| 326 | "WHERE owner='%s' AND nickname='%s'", |
| 327 | $this->table, |
| 328 | $this->dbh->quoteString($userdata['nickname']), |
| 329 | $this->dbh->quoteString($userdata['firstname']), |
| 330 | $this->dbh->quoteString($userdata['lastname']), |
| 331 | $this->dbh->quoteString($userdata['email']), |
| 332 | $this->dbh->quoteString($userdata['label']), |
| 333 | $this->owner, |
| 334 | $this->dbh->quoteString($alias) ); |
| 335 | |
| 336 | /* Do the insert */ |
| 337 | $r = $this->dbh->simpleQuery($query); |
| 338 | if ($r == DB_OK) { |
| 339 | return true; |
| 340 | } |
| 341 | |
| 342 | /* Fail */ |
| 343 | return $this->set_error(sprintf(_("Database error: %s"), |
| 344 | DB::errorMessage($r))); |
| 345 | } |
| 346 | } /* End of class abook_database */ |
| 347 | |
| 348 | ?> |