abook_database.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /**
  3. * abook_database.php
  4. *
  5. * Copyright (c) 1999-2003 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. * $Id$
  30. */
  31. require_once('DB.php');
  32. class abook_database extends addressbook_backend {
  33. var $btype = 'local';
  34. var $bname = 'database';
  35. var $dsn = '';
  36. var $table = '';
  37. var $owner = '';
  38. var $dbh = false;
  39. var $writeable = true;
  40. /* ========================== Private ======================= */
  41. /* Constructor */
  42. function abook_database($param) {
  43. $this->sname = _("Personal address book");
  44. if (is_array($param)) {
  45. if (empty($param['dsn']) ||
  46. empty($param['table']) ||
  47. empty($param['owner'])) {
  48. return $this->set_error('Invalid parameters');
  49. }
  50. $this->dsn = $param['dsn'];
  51. $this->table = $param['table'];
  52. $this->owner = $param['owner'];
  53. if (!empty($param['name'])) {
  54. $this->sname = $param['name'];
  55. }
  56. if (isset($param['writeable'])) {
  57. $this->writeable = $param['writeable'];
  58. }
  59. $this->open(true);
  60. }
  61. else {
  62. return $this->set_error('Invalid argument to constructor');
  63. }
  64. }
  65. /* Open the database. New connection if $new is true */
  66. function open($new = false) {
  67. $this->error = '';
  68. /* Return true is file is open and $new is unset */
  69. if ($this->dbh && !$new) {
  70. return true;
  71. }
  72. /* Close old file, if any */
  73. if ($this->dbh) {
  74. $this->close();
  75. }
  76. $dbh = DB::connect($this->dsn, true);
  77. if (DB::isError($dbh)) {
  78. return $this->set_error(sprintf(_("Database error: %s"),
  79. DB::errorMessage($dbh)));
  80. }
  81. $this->dbh = $dbh;
  82. return true;
  83. }
  84. /* Close the file and forget the filehandle */
  85. function close() {
  86. $this->dbh->disconnect();
  87. $this->dbh = false;
  88. }
  89. /* ========================== Public ======================== */
  90. /* Search the file */
  91. function &search($expr) {
  92. $ret = array();
  93. if(!$this->open()) {
  94. return false;
  95. }
  96. /* To be replaced by advanded search expression parsing */
  97. if (is_array($expr)) {
  98. return;
  99. }
  100. /* Make regexp from glob'ed expression */
  101. $expr = str_replace('?', '_', $expr);
  102. $expr = str_replace('*', '%', $expr);
  103. $expr = $this->dbh->quoteString($expr);
  104. $expr = "%$expr%";
  105. $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND " .
  106. "(firstname LIKE '%s' OR lastname LIKE '%s')",
  107. $this->table, $this->owner, $expr, $expr);
  108. $res = $this->dbh->query($query);
  109. if (DB::isError($res)) {
  110. return $this->set_error(sprintf(_("Database error: %s"),
  111. DB::errorMessage($res)));
  112. }
  113. while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
  114. array_push($ret, array('nickname' => $row['nickname'],
  115. 'name' => "$row[firstname] $row[lastname]",
  116. 'firstname' => $row['firstname'],
  117. 'lastname' => $row['lastname'],
  118. 'email' => $row['email'],
  119. 'label' => $row['label'],
  120. 'backend' => $this->bnum,
  121. 'source' => &$this->sname));
  122. }
  123. return $ret;
  124. }
  125. /* Lookup alias */
  126. function &lookup($alias) {
  127. if (empty($alias)) {
  128. return array();
  129. }
  130. $alias = strtolower($alias);
  131. if (!$this->open()) {
  132. return false;
  133. }
  134. $query = sprintf("SELECT * FROM %s WHERE owner='%s' AND nickname='%s'",
  135. $this->table, $this->owner, $alias);
  136. $res = $this->dbh->query($query);
  137. if (DB::isError($res)) {
  138. return $this->set_error(sprintf(_("Database error: %s"),
  139. DB::errorMessage($res)));
  140. }
  141. if ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
  142. return array('nickname' => $row['nickname'],
  143. 'name' => "$row[firstname] $row[lastname]",
  144. 'firstname' => $row['firstname'],
  145. 'lastname' => $row['lastname'],
  146. 'email' => $row['email'],
  147. 'label' => $row['label'],
  148. 'backend' => $this->bnum,
  149. 'source' => &$this->sname);
  150. }
  151. return array();
  152. }
  153. /* List all addresses */
  154. function &list_addr() {
  155. $ret = array();
  156. if (!$this->open()) {
  157. return false;
  158. }
  159. $query = sprintf("SELECT * FROM %s WHERE owner='%s'",
  160. $this->table, $this->owner);
  161. $res = $this->dbh->query($query);
  162. if (DB::isError($res)) {
  163. return $this->set_error(sprintf(_("Database error: %s"),
  164. DB::errorMessage($res)));
  165. }
  166. while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
  167. array_push($ret, array('nickname' => $row['nickname'],
  168. 'name' => "$row[firstname] $row[lastname]",
  169. 'firstname' => $row['firstname'],
  170. 'lastname' => $row['lastname'],
  171. 'email' => $row['email'],
  172. 'label' => $row['label'],
  173. 'backend' => $this->bnum,
  174. 'source' => &$this->sname));
  175. }
  176. return $ret;
  177. }
  178. /* Add address */
  179. function add($userdata) {
  180. if (!$this->writeable) {
  181. return $this->set_error(_("Addressbook is read-only"));
  182. }
  183. if (!$this->open()) {
  184. return false;
  185. }
  186. /* See if user exist already */
  187. $ret = $this->lookup($userdata['nickname']);
  188. if (!empty($ret)) {
  189. return $this->set_error(sprintf(_("User '%s' already exist"),
  190. $ret['nickname']));
  191. }
  192. /* Create query */
  193. $query = sprintf("INSERT INTO %s (owner, nickname, firstname, " .
  194. "lastname, email, label) VALUES('%s','%s','%s'," .
  195. "'%s','%s','%s')",
  196. $this->table, $this->owner,
  197. $this->dbh->quoteString($userdata['nickname']),
  198. $this->dbh->quoteString($userdata['firstname']),
  199. $this->dbh->quoteString($userdata['lastname']),
  200. $this->dbh->quoteString($userdata['email']),
  201. $this->dbh->quoteString($userdata['label']) );
  202. /* Do the insert */
  203. $r = $this->dbh->simpleQuery($query);
  204. if ($r == DB_OK) {
  205. return true;
  206. }
  207. /* Fail */
  208. return $this->set_error(sprintf(_("Database error: %s"),
  209. DB::errorMessage($r)));
  210. }
  211. /* Delete address */
  212. function remove($alias) {
  213. if (!$this->writeable) {
  214. return $this->set_error(_("Addressbook is read-only"));
  215. }
  216. if (!$this->open()) {
  217. return false;
  218. }
  219. /* Create query */
  220. $query = sprintf("DELETE FROM %s WHERE owner='%s' AND (",
  221. $this->table, $this->owner);
  222. $sepstr = '';
  223. while (list($undef, $nickname) = each($alias)) {
  224. $query .= sprintf("%s nickname='%s' ", $sepstr,
  225. $this->dbh->quoteString($nickname));
  226. $sepstr = 'OR';
  227. }
  228. $query .= ')';
  229. /* Delete entry */
  230. $r = $this->dbh->simpleQuery($query);
  231. if ($r == DB_OK) {
  232. return true;
  233. }
  234. /* Fail */
  235. return $this->set_error(sprintf(_("Database error: %s"),
  236. DB::errorMessage($r)));
  237. }
  238. /* Modify address */
  239. function modify($alias, $userdata) {
  240. if (!$this->writeable) {
  241. return $this->set_error(_("Addressbook is read-only"));
  242. }
  243. if (!$this->open()) {
  244. return false;
  245. }
  246. /* See if user exist */
  247. $ret = $this->lookup($alias);
  248. if (empty($ret)) {
  249. return $this->set_error(sprintf(_("User '%s' does not exist"),
  250. $alias));
  251. }
  252. /* Create query */
  253. $query = sprintf("UPDATE %s SET nickname='%s', firstname='%s', ".
  254. "lastname='%s', email='%s', label='%s' ".
  255. "WHERE owner='%s' AND nickname='%s'",
  256. $this->table,
  257. $this->dbh->quoteString($userdata['nickname']),
  258. $this->dbh->quoteString($userdata['firstname']),
  259. $this->dbh->quoteString($userdata['lastname']),
  260. $this->dbh->quoteString($userdata['email']),
  261. $this->dbh->quoteString($userdata['label']),
  262. $this->owner,
  263. $this->dbh->quoteString($alias) );
  264. /* Do the insert */
  265. $r = $this->dbh->simpleQuery($query);
  266. if ($r == DB_OK) {
  267. return true;
  268. }
  269. /* Fail */
  270. return $this->set_error(sprintf(_("Database error: %s"),
  271. DB::errorMessage($r)));
  272. }
  273. } /* End of class abook_database */
  274. ?>