addressbook.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /**
  3. ** addressbook.php
  4. **
  5. ** Functions and classes for the addressbook system.
  6. **
  7. **/
  8. $addressbook_php = true;
  9. // Include backends here.
  10. include("../functions/abook_local_file.php");
  11. include("../functions/abook_ldap_server.php");
  12. // Create and initialize an addressbook object.
  13. // Returns the created object
  14. function addressbook_init($showerr = true, $onlylocal = false) {
  15. global $data_dir, $username, $ldap_server;
  16. // Create a new addressbook object
  17. $abook = new AddressBook;
  18. // Always add a local backend
  19. $filename = sprintf("%s%s.abook", $data_dir, $username);
  20. $r = $abook->add_backend("local_file", Array("filename" => $filename,
  21. "create" => true));
  22. if(!$r && $showerr) {
  23. printf(_("Error opening file %s"), $filename);
  24. exit;
  25. }
  26. if($onlylocal)
  27. return $abook;
  28. // Load configured LDAP servers
  29. reset($ldap_server);
  30. while(list($key,$param) = each($ldap_server)) {
  31. if(is_array($param)) {
  32. $r = $abook->add_backend("ldap_server", $param);
  33. if(!$r && $showerr) {
  34. printf("&nbsp;"._("Error initializing LDAP server %s:")."<BR>\n",
  35. $param["host"]);
  36. printf("&nbsp;".$abook->error);
  37. exit;
  38. }
  39. }
  40. }
  41. // Return the initialized object
  42. return $abook;
  43. }
  44. /**
  45. ** This is the main address book class that connect all the
  46. ** backends and provide services to the functions above.
  47. **
  48. **/
  49. class AddressBook {
  50. var $backends = array();
  51. var $numbackends = 0;
  52. var $error = "";
  53. var $localbackend = 0;
  54. // Constructor function.
  55. function AddressBook() {
  56. }
  57. // Return an array of backends of a given type,
  58. // or all backends if no type is specified.
  59. function get_backend_list($type = "") {
  60. $ret = array();
  61. for($i = 1 ; $i <= $this->numbackends ; $i++) {
  62. if(empty($type) || $type == $this->backends[$i]->btype) {
  63. array_push($ret, &$this->backends[$i]);
  64. }
  65. }
  66. return $ret;
  67. }
  68. // ========================== Public ========================
  69. // Add a new backend. $backend is the name of a backend
  70. // (without the abook_ prefix), and $param is an optional
  71. // mixed variable that is passed to the backend constructor.
  72. // See each of the backend classes for valid parameters.
  73. function add_backend($backend, $param = "") {
  74. $backend_name = "abook_".$backend;
  75. eval("\$newback = new $backend_name(\$param);");
  76. if(!empty($newback->error)) {
  77. $this->error = $newback->error;
  78. return false;
  79. }
  80. $this->numbackends++;
  81. $newback->bnum = $this->numbackends;
  82. $this->backends[$this->numbackends] = $newback;
  83. // Store ID of first local backend added
  84. if($this->localbackend == 0 && $newback->btype == "local")
  85. $this->localbackend = $this->numbackends;
  86. return $this->numbackends;
  87. }
  88. // Return a list of addresses matching expression in
  89. // all backends of a given type.
  90. function search($expression, $btype = "") {
  91. $ret = array();
  92. $this->error = "";
  93. $sel = $this->get_backend_list($btype);
  94. $failed = 0;
  95. for($i = 0 ; $i < sizeof($sel) ; $i++) {
  96. $backend = &$sel[$i];
  97. $backend->error = "";
  98. $res = $backend->search($expression);
  99. if(is_array($res)) {
  100. $ret = array_merge($ret, $res);
  101. } else {
  102. $this->error = $this->error . "<br>\n". $backend->error;
  103. $failed++;
  104. }
  105. }
  106. // Only fail if all backends failed
  107. if($failed >= sizeof($sel))
  108. return false;
  109. return $ret;
  110. }
  111. // Return a sorted search
  112. function s_search($expression, $btype = "") {
  113. $ret = $this->search($expression, $btype);
  114. if(!is_array($ret))
  115. return $ret;
  116. // Inline function - Not nice, but still..
  117. function cmp($a,$b) {
  118. if($a["backend"] > $b["backend"])
  119. return 1;
  120. else if($a["backend"] < $b["backend"])
  121. return -1;
  122. return (strtolower($a["name"]) > strtolower($b["name"])) ? 1 : -1;
  123. }
  124. usort($ret, 'cmp');
  125. return $ret;
  126. }
  127. // Lookup an address by alias. Only possible in
  128. // local backends.
  129. function lookup($alias) {
  130. $ret = array();
  131. $sel = $this->get_backend_list("local");
  132. for($i = 0 ; $i < sizeof($sel) ; $i++) {
  133. $backend = &$sel[$i];
  134. $backend->error = "";
  135. $res = $backend->lookup($alias);
  136. if(is_array($res)) {
  137. return $res;
  138. } else {
  139. $this->error = $backend->error;
  140. return false;
  141. }
  142. }
  143. return $ret;
  144. }
  145. // Return all addresses
  146. function list_addr() {
  147. $ret = array();
  148. $sel = $this->get_backend_list("local");
  149. for($i = 0 ; $i < sizeof($sel) ; $i++) {
  150. $backend = &$sel[$i];
  151. $backend->error = "";
  152. $res = $backend->list_addr();
  153. if(is_array($res)) {
  154. $ret = array_merge($ret, $res);
  155. } else {
  156. $this->error = $backend->error;
  157. return false;
  158. }
  159. }
  160. return $ret;
  161. }
  162. // Create a new address from $userdata, in backend $bnum.
  163. // Return the backend number that the/ address was added
  164. // to, or false if it failed.
  165. function add($userdata, $bnum) {
  166. // Validate data
  167. if(!is_array($userdata)) {
  168. $this->error = _("Invalid input data");
  169. return false;
  170. }
  171. if(empty($userdata["fullname"]) &&
  172. empty($userdata["lastname"])) {
  173. $this->error = _("Name is missing");
  174. return false;
  175. }
  176. if(empty($userdata["email"])) {
  177. $this->error = _("E-mail address is missing");
  178. return false;
  179. }
  180. if(empty($userdata["nickname"])) {
  181. $userdata["nickname"] = $userdata["email"];
  182. }
  183. // Check that specified backend accept new entries
  184. if(!$this->backends[$bnum]->writeable) {
  185. $this->error = _("Addressbook is read-only");
  186. return false;
  187. }
  188. // Add address to backend
  189. $res = $this->backends[$bnum]->add($userdata);
  190. if($res) {
  191. return $bnum;
  192. } else {
  193. $this->error = $this->backends[$bnum]->error;
  194. return false;
  195. }
  196. return false; // Not reached
  197. }
  198. }
  199. /**
  200. ** Generic backend that all other backends extend
  201. **/
  202. class addressbook_backend {
  203. // Variables that all backends must provide.
  204. var $btype = "dummy";
  205. var $bname = "dummy";
  206. var $sname = "Dummy backend";
  207. // Variables common for all backends, but that
  208. // should not be changed by the backends.
  209. var $bnum = -1;
  210. var $error = "";
  211. var $writeable = false;
  212. function set_error($string) {
  213. $this->error = "[" . $this->sname . "] " . $string;
  214. return false;
  215. }
  216. // ========================== Public ========================
  217. function search($expression) {
  218. $this->set_error("search not implemented");
  219. return false;
  220. }
  221. function lookup($alias) {
  222. $this->set_error("lookup not implemented");
  223. return false;
  224. }
  225. function list_addr() {
  226. $this->set_error("list_addr not implemented");
  227. return false;
  228. }
  229. function add($userdata) {
  230. $this->set_error("add not implemented");
  231. return false;
  232. }
  233. }
  234. ?>