abook_database.php 13 KB

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