abook_local_file.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. $this->unlock();
  120. $newfh = @fopen($this->filename, 'w');
  121. if(!$newfh) {
  122. return $this->set_error("$file: " . _("Open failed"));
  123. }
  124. for($i = 0 ; $i < sizeof($rows) ; $i++) {
  125. if(is_array($rows[$i])) {
  126. fwrite($newfh, join('|', $rows[$i]) . "\n");
  127. }
  128. }
  129. fclose($newfh);
  130. $this->unlock();
  131. $this->open(true);
  132. return true;
  133. }
  134. /* ========================== Public ======================== */
  135. /* Search the file */
  136. function search($expr) {
  137. /* To be replaced by advanded search expression parsing */
  138. if(is_array($expr)) { return; }
  139. /* Make regexp from glob'ed expression
  140. * May want to quote other special characters like (, ), -, [, ], etc. */
  141. $expr = str_replace('?', '.', $expr);
  142. $expr = str_replace('*', '.*', $expr);
  143. $res = array();
  144. if(!$this->open()) {
  145. return false;
  146. }
  147. @rewind($this->filehandle);
  148. while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
  149. $line = join(' ', $row);
  150. if(eregi($expr, $line)) {
  151. array_push($res, array('nickname' => $row[0],
  152. 'name' => $row[1] . ' ' . $row[2],
  153. 'firstname' => $row[1],
  154. 'lastname' => $row[2],
  155. 'email' => $row[3],
  156. 'label' => $row[4],
  157. 'backend' => $this->bnum,
  158. 'source' => &$this->sname));
  159. }
  160. }
  161. return $res;
  162. }
  163. /* Lookup alias */
  164. function lookup($alias) {
  165. if(empty($alias)) {
  166. return array();
  167. }
  168. $alias = strtolower($alias);
  169. $this->open();
  170. @rewind($this->filehandle);
  171. while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
  172. if(strtolower($row[0]) == $alias) {
  173. return array('nickname' => $row[0],
  174. 'name' => $row[1] . ' ' . $row[2],
  175. 'firstname' => $row[1],
  176. 'lastname' => $row[2],
  177. 'email' => $row[3],
  178. 'label' => $row[4],
  179. 'backend' => $this->bnum,
  180. 'source' => &$this->sname);
  181. }
  182. }
  183. return array();
  184. }
  185. /* List all addresses */
  186. function list_addr() {
  187. $res = array();
  188. $this->open();
  189. @rewind($this->filehandle);
  190. while ($row = @fgetcsv($this->filehandle, 2048, '|')) {
  191. array_push($res, array('nickname' => $row[0],
  192. 'name' => $row[1] . ' ' . $row[2],
  193. 'firstname' => $row[1],
  194. 'lastname' => $row[2],
  195. 'email' => $row[3],
  196. 'label' => $row[4],
  197. 'backend' => $this->bnum,
  198. 'source' => &$this->sname));
  199. }
  200. return $res;
  201. }
  202. /* Add address */
  203. function add($userdata) {
  204. if(!$this->writeable) {
  205. return $this->set_error(_("Addressbook is read-only"));
  206. }
  207. /* See if user exists already */
  208. $ret = $this->lookup($userdata['nickname']);
  209. if(!empty($ret)) {
  210. return $this->set_error(sprintf(_("User '%s' already exist"),
  211. $ret['nickname']));
  212. }
  213. /* Here is the data to write */
  214. $data = $userdata['nickname'] . '|' . $userdata['firstname'] . '|' .
  215. $userdata['lastname'] . '|' . $userdata['email'] . '|' .
  216. $userdata['label'];
  217. /* Strip linefeeds */
  218. $data = ereg_replace("[\r\n]", ' ', $data);
  219. /* Add linefeed at end */
  220. $data = $data . "\n";
  221. /* Reopen file, just to be sure */
  222. $this->open(true);
  223. if(!$this->writeable) {
  224. return $this->set_error(_("Addressbook is read-only"));
  225. }
  226. /* Lock the file */
  227. if(!$this->lock()) {
  228. return $this->set_error(_("Could not lock datafile"));
  229. }
  230. /* Write */
  231. $r = fwrite($this->filehandle, $data);
  232. /* Unlock file */
  233. $this->unlock();
  234. /* Test write result and exit if OK */
  235. if($r > 0) return true;
  236. /* Fail */
  237. $this->set_error(_("Write to addressbook failed"));
  238. return false;
  239. }
  240. /* Delete address */
  241. function remove($alias) {
  242. if(!$this->writeable) {
  243. return $this->set_error(_("Addressbook is read-only"));
  244. }
  245. /* Lock the file to make sure we're the only process working
  246. * on it. */
  247. if(!$this->lock()) {
  248. return $this->set_error(_("Could not lock datafile"));
  249. }
  250. /* Read file into memory, ignoring nicknames to delete */
  251. @rewind($this->filehandle);
  252. $i = 0;
  253. $rows = array();
  254. while($row = @fgetcsv($this->filehandle, 2048, '|')) {
  255. if(!in_array($row[0], $alias)) {
  256. $rows[$i++] = $row;
  257. }
  258. }
  259. /* Write data back */
  260. if(!$this->overwrite($rows)) {
  261. $this->unlock();
  262. return false;
  263. }
  264. $this->unlock();
  265. return true;
  266. }
  267. /* Modify address */
  268. function modify($alias, $userdata) {
  269. if(!$this->writeable) {
  270. return $this->set_error(_("Addressbook is read-only"));
  271. }
  272. /* See if user exists */
  273. $ret = $this->lookup($alias);
  274. if(empty($ret)) {
  275. return $this->set_error(sprintf(_("User '%s' does not exist"),
  276. $alias));
  277. }
  278. /* Lock the file to make sure we're the only process working
  279. * on it. */
  280. if(!$this->lock()) {
  281. return $this->set_error(_("Could not lock datafile"));
  282. }
  283. /* Read file into memory, modifying the data for the
  284. * user identified by $alias */
  285. $this->open(true);
  286. @rewind($this->filehandle);
  287. $i = 0;
  288. $rows = array();
  289. while($row = @fgetcsv($this->filehandle, 2048, '|')) {
  290. if(strtolower($row[0]) != strtolower($alias)) {
  291. $rows[$i++] = $row;
  292. } else {
  293. $rows[$i++] = array(0 => $userdata['nickname'],
  294. 1 => $userdata['firstname'],
  295. 2 => $userdata['lastname'],
  296. 3 => $userdata['email'],
  297. 4 => $userdata['label']);
  298. }
  299. }
  300. /* Write data back */
  301. if(!$this->overwrite($rows)) {
  302. $this->unlock();
  303. return false;
  304. }
  305. $this->unlock();
  306. return true;
  307. }
  308. } /* End of class abook_local_file */
  309. ?>