abook_local_file.php 11 KB

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