abook_database.php 11 KB

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