abook_local_file.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. /**
  3. * abook_local_file.php
  4. *
  5. * Copyright (c) 1999-2002 The SquirrelMail Project Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * Backend for addressbook as a pipe separated file
  9. *
  10. * An array with the following elements must be passed to
  11. * the class constructor (elements marked ? are optional):
  12. *
  13. * filename => path to addressbook file
  14. * ? create => if true: file is created if it does not exist.
  15. * ? umask => umask set before opening file.
  16. *
  17. * NOTE. This class should not be used directly. Use the
  18. * "AddressBook" class instead.
  19. *
  20. * $Id$
  21. */
  22. class abook_local_file extends addressbook_backend {
  23. var $btype = 'local';
  24. var $bname = 'local_file';
  25. var $filename = '';
  26. var $filehandle = 0;
  27. var $create = false;
  28. var $umask;
  29. /* ========================== Private ======================= */
  30. /* Constructor */
  31. function abook_local_file($param) {
  32. $this->sname = _("Personal address book");
  33. $this->umask = Umask();
  34. if(is_array($param)) {
  35. if(empty($param['filename'])) {
  36. return $this->set_error('Invalid parameters');
  37. }
  38. if(!is_string($param['filename'])) {
  39. return $this->set_error($param['filename'] . ': '.
  40. _("Not a file name"));
  41. }
  42. $this->filename = $param['filename'];
  43. if($param['create']) {
  44. $this->create = true;
  45. }
  46. if(isset($param['umask'])) {
  47. $this->umask = $param['umask'];
  48. }
  49. if(!empty($param['name'])) {
  50. $this->sname = $param['name'];
  51. }
  52. $this->open(true);
  53. } else {
  54. $this->set_error('Invalid argument to constructor');
  55. }
  56. }
  57. /* Open the addressbook file and store the file pointer.
  58. * Use $file as the file to open, or the class' own
  59. * filename property. If $param is empty and file is
  60. * open, do nothing. */
  61. function open($new = false) {
  62. $this->error = '';
  63. $file = $this->filename;
  64. $create = $this->create;
  65. /* Return true is file is open and $new is unset */
  66. if($this->filehandle && !$new) {
  67. return true;
  68. }
  69. /* Check that new file exitsts */
  70. if((!(file_exists($file) && is_readable($file))) && !$create) {
  71. return $this->set_error("$file: " . _("No such file or directory"));
  72. }
  73. /* Close old file, if any */
  74. if($this->filehandle) { $this->close(); }
  75. /* Open file. First try to open for reading and writing,
  76. * but fall back to read only. */
  77. umask($this->umask);
  78. $fh = @fopen($file, 'a+');
  79. if($fh) {
  80. $this->filehandle = &$fh;
  81. $this->filename = $file;
  82. $this->writeable = true;
  83. } else {
  84. $fh = @fopen($file, 'r');
  85. if($fh) {
  86. $this->filehandle = &$fh;
  87. $this->filename = $file;
  88. $this->writeable = false;
  89. } else {
  90. return $this->set_error("$file: " . _("Open failed"));
  91. }
  92. }
  93. return true;
  94. }
  95. /* Close the file and forget the filehandle */
  96. function close() {
  97. @fclose($this->filehandle);
  98. $this->filehandle = 0;
  99. $this->filename = '';
  100. $this->writable = false;
  101. }
  102. /* Lock the datafile - try 20 times in 5 seconds */
  103. function lock() {
  104. for($i = 0 ; $i < 20 ; $i++) {
  105. if(flock($this->filehandle, 2 + 4))
  106. return true;
  107. else
  108. usleep(250000);
  109. }
  110. return false;
  111. }
  112. /* Lock the datafile */
  113. function unlock() {
  114. return flock($this->filehandle, 3);
  115. }
  116. /* Overwrite the file with data from $rows
  117. * NOTE! Previous locks are broken by this function */
  118. function overwrite(&$rows) {
  119. $newfh = @fopen($this->filename, 'w');
  120. if(!$newfh) {
  121. return $this->set_error("$file: " . _("Open failed"));
  122. }
  123. for($i = 0 ; $i < sizeof($rows) ; $i++) {
  124. if(is_array($rows[$i])) {
  125. fwrite($newfh, join('|', $rows[$i]) . "\n");
  126. }
  127. }
  128. fclose($newfh);
  129. $this->unlock();
  130. $this->open(true);
  131. return true;
  132. }
  133. /* ========================== Public ======================== */
  134. /* Search the file */
  135. function search($expr) {
  136. /* To be replaced by advanded search expression parsing */
  137. if(is_array($expr)) { return; }
  138. /* Make regexp from glob'ed expression
  139. * May want to quote other special characters like (, ), -, [, ], etc. */
  140. $expr = str_replace('?', '.', $expr);
  141. $expr = str_replace('*', '.*', $expr);
  142. $res = array();
  143. if(!$this->open()) {
  144. return false;
  145. }
  146. @rewind($this->filehandle);
  147. while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
  148. $line = join(' ', $row);
  149. if(eregi($expr, $line)) {
  150. array_push($res, array('nickname' => $row[0],
  151. 'name' => $row[1] . ' ' . $row[2],
  152. 'firstname' => $row[1],
  153. 'lastname' => $row[2],
  154. 'email' => $row[3],
  155. 'label' => $row[4],
  156. 'backend' => $this->bnum,
  157. 'source' => &$this->sname));
  158. }
  159. }
  160. return $res;
  161. }
  162. /* Lookup alias */
  163. function lookup($alias) {
  164. if(empty($alias)) {
  165. return array();
  166. }
  167. $alias = strtolower($alias);
  168. $this->open();
  169. @rewind($this->filehandle);
  170. while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
  171. if(strtolower($row[0]) == $alias) {
  172. return array('nickname' => $row[0],
  173. 'name' => $row[1] . ' ' . $row[2],
  174. 'firstname' => $row[1],
  175. 'lastname' => $row[2],
  176. 'email' => $row[3],
  177. 'label' => $row[4],
  178. 'backend' => $this->bnum,
  179. 'source' => &$this->sname);
  180. }
  181. }
  182. return array();
  183. }
  184. /* List all addresses */
  185. function list_addr() {
  186. $res = array();
  187. $this->open();
  188. @rewind($this->filehandle);
  189. while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
  190. array_push($res, array('nickname' => $row[0],
  191. 'name' => $row[1] . ' ' . $row[2],
  192. 'firstname' => $row[1],
  193. 'lastname' => $row[2],
  194. 'email' => $row[3],
  195. 'label' => $row[4],
  196. 'backend' => $this->bnum,
  197. 'source' => &$this->sname));
  198. }
  199. return $res;
  200. }
  201. /* Add address */
  202. function add($userdata) {
  203. if(!$this->writeable) {
  204. return $this->set_error(_("Addressbook is read-only"));
  205. }
  206. /* See if user exists already */
  207. $ret = $this->lookup($userdata['nickname']);
  208. if(!empty($ret)) {
  209. return $this->set_error(sprintf(_("User '%s' already exist"),
  210. $ret['nickname']));
  211. }
  212. /* Here is the data to write */
  213. $data = $userdata['nickname'] . '|' . $userdata['firstname'] . '|' .
  214. $userdata['lastname'] . '|' . $userdata['email'] . '|' .
  215. $userdata['label'];
  216. /* Strip linefeeds */
  217. $data = ereg_replace("[\r\n]", ' ', $data);
  218. /* Add linefeed at end */
  219. $data = $data . "\n";
  220. /* Reopen file, just to be sure */
  221. $this->open(true);
  222. if(!$this->writeable) {
  223. return $this->set_error(_("Addressbook is read-only"));
  224. }
  225. /* Lock the file */
  226. if(!$this->lock()) {
  227. return $this->set_error(_("Could not lock datafile"));
  228. }
  229. /* Write */
  230. $r = fwrite($this->filehandle, $data);
  231. /* Unlock file */
  232. $this->unlock();
  233. /* Test write result and exit if OK */
  234. if($r > 0) return true;
  235. /* Fail */
  236. $this->set_error(_("Write to addressbook failed"));
  237. return false;
  238. }
  239. /* Delete address */
  240. function remove($alias) {
  241. if(!$this->writeable) {
  242. return $this->set_error(_("Addressbook is read-only"));
  243. }
  244. /* Lock the file to make sure we're the only process working
  245. * on it. */
  246. if(!$this->lock()) {
  247. return $this->set_error(_("Could not lock datafile"));
  248. }
  249. /* Read file into memory, ignoring nicknames to delete */
  250. @rewind($this->filehandle);
  251. $i = 0;
  252. $rows = array();
  253. while($row = @fgetcsv($this->filehandle, 2048, '|')) {
  254. if(!in_array($row[0], $alias)) {
  255. $rows[$i++] = $row;
  256. }
  257. }
  258. /* Write data back */
  259. if(!$this->overwrite($rows)) {
  260. $this->unlock();
  261. return false;
  262. }
  263. $this->unlock();
  264. return true;
  265. }
  266. /* Modify address */
  267. function modify($alias, $userdata) {
  268. if(!$this->writeable) {
  269. return $this->set_error(_("Addressbook is read-only"));
  270. }
  271. /* See if user exists */
  272. $ret = $this->lookup($alias);
  273. if(empty($ret)) {
  274. return $this->set_error(sprintf(_("User '%s' does not exist"),
  275. $alias));
  276. }
  277. /* Lock the file to make sure we're the only process working
  278. * on it. */
  279. if(!$this->lock()) {
  280. return $this->set_error(_("Could not lock datafile"));
  281. }
  282. /* Read file into memory, modifying the data for the
  283. * user identified by $alias */
  284. $this->open(true);
  285. @rewind($this->filehandle);
  286. $i = 0;
  287. $rows = array();
  288. while($row = @fgetcsv($this->filehandle, 2048, '|')) {
  289. if(strtolower($row[0]) != strtolower($alias)) {
  290. $rows[$i++] = $row;
  291. } else {
  292. $rows[$i++] = array(0 => $userdata['nickname'],
  293. 1 => $userdata['firstname'],
  294. 2 => $userdata['lastname'],
  295. 3 => $userdata['email'],
  296. 4 => $userdata['label']);
  297. }
  298. }
  299. /* Write data back */
  300. if(!$this->overwrite($rows)) {
  301. $this->unlock();
  302. return false;
  303. }
  304. $this->unlock();
  305. return true;
  306. }
  307. } /* End of class abook_local_file */
  308. ?>