abook_database.php 12 KB

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