abook_local_file.php 12 KB

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