abook_database.php 11 KB

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