1<?php
2
3/****************************************
4 RefDB interface library
5
6 This file defines an object-oriented interface to RefDB. The current
7 implementation uses calls to the C clients instead of a direct
8 communication with refdbd through sockets.
9
10 Markus Hoenicka <mhoenicka@users.sourceforge.net> 2007-11-02
11
12   This program is free software; you can redistribute it and/or modify
13   it under the terms of the GNU General Public License as published by
14   the Free Software Foundation; either version 2 of the License, or
15   (at your option) any later version.
16
17   This program is distributed in the hope that it will be useful,
18   but WITHOUT ANY WARRANTY; without even the implied warranty of
19   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20   GNU General Public License for more details.
21
22   You should have received a copy of the GNU General Public License
23   along with this program; if not, write to the Free Software
24   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
26********************************/
27
28class RefDB {
29  var $usernamearg; /* username for db authentication */
30  var $passwordarg; /* password for db authentication */
31  var $dbarg;       /* name of database to connect to */
32  var $pdf_rootarg; /* base path of offprint repository */
33  var $client;      /* full path of refdbc or refdba binary */
34
35
36  function __construct($username, $password, $db, $pdf_root, $client) {
37    $this->usernamearg = strlen($username) ? " -u ".$username : "";
38    $this->passwordarg = strlen($password) ? " -w ".$password : "";
39    $this->dbarg = strlen($db) ? " -d ".$db : "";
40    $this->pdf_rootarg = strlen($pdf_root) ? " -R ".$pdf_root : "";
41    $this->client = $client;
42
43    /* this is the full path of the binary with all mandatory
44     arguments. We send the output to stdout to bypass any pager that
45     may lurk in the user's config files */
46    $this->clientcmd = $this->client.$this->usernamearg.$this->passwordarg.$this->dbarg." -c stdout ";
47  }
48
49  protected function _run_command($command) {
50    $lines = "";
51    $exit_state = 0;
52
53    //        echo "<p>DEBUG: running command $command</p>";
54    exec($command, $lines, $exit_state);
55    //        echo "<p>DEBUG: exit state went to $exit_state</p>";
56    switch($exit_state) {
57    case 0:
58      return $lines;
59      break;
60    default:
61      return array("ERROR");
62    }
63  }
64
65  public function addstyle_from_file($file) {
66    $command = $this->clientcmd." -C addstyle ".escapeshellarg($file);
67    return $this->_run_command($command);
68  }
69
70  public function adduser($host, $database, $newuserpasswd, $username) {
71    $command = $this->clientcmd." -C adduser -d ".escapeshellarg($database);
72
73    if (strlen($newuserpasswd) > 0) {
74      $command .= " -W ".$newuserpasswd;
75    }
76
77    if (strlen($host) > 0) {
78      $command .= " -H ".$host;
79    }
80
81    $command .= $username;
82
83    return $this->_run_command($command);
84  }
85
86  public function deleteuser($host, $database, $username) {
87    $command = $this->clientcmd." -C deleteuser -d ".escapeshellarg($database);
88
89    if (strlen($host) > 0) {
90      $command .= " -H ".$host;
91    }
92
93    $command .= $username;
94
95    return $this->_run_command($command);
96  }
97
98  public function addword($words) {
99    $command = $this->clientcmd." -C addword ".escapeshellarg($words);
100    return $this->_run_command($command);
101  }
102
103  public function deleteword($words) {
104    $command = $this->clientcmd." -C deleteword ".escapeshellarg($words);
105    return $this->_run_command($command);
106  }
107
108  public function confserv($command) {
109    $command = $this->clientcmd." -C confserv ".escapeshellarg($command);
110    return $this->_run_command($command);
111  }
112
113  public function createdb($dbname, $encoding) {
114    $command = $this->clientcmd." -C createdb ";
115
116    if (strlen($encoding) > 0) {
117      $command .= " -E ".escapeshellarg($encoding);
118    }
119
120    $command .= " ".escapeshellarg($dbname);
121
122    return $this->_run_command($command);
123  }
124
125  public function deletedb($dbname) {
126    $command = $this->clientcmd." -C deletedb ".escapeshellarg($dbname);
127    return $this->_run_command($command);
128  }
129
130  public function deletestyle($style) {
131    $command = $this->clientcmd." -C deletestyle ".escapeshellarg($style);
132    return $this->_run_command($command);
133  }
134
135  public function getstyle($style) {
136    $command = $this->clientcmd." -C getstyle ".escapeshellarg($style);
137    return $this->_run_command($command);
138  }
139
140  public function listdb($dbname) {
141    $command = $this->clientcmd." -C listdb ".escapeshellarg($dbname);
142    return $this->_run_command($command);
143  }
144
145  public function listuser($dbname, $user) {
146    $command = $this->clientcmd." -C listuser -d ".escapeshellarg($dbname)." ".escapeshellarg($user);
147    return $this->_run_command($command);
148  }
149
150  public function listword($word) {
151    $command = $this->clientcmd." -C listword ".escapeshellarg($word);
152    return $this->_run_command($command);
153  }
154
155  public function liststyle($style) {
156    $command = $this->clientcmd." -C liststyle ".escapeshellarg($style);
157    return $this->_run_command($command);
158  }
159
160  public function viewstat() {
161    $command = $this->clientcmd." -C viewstat";
162    return $this->_run_command($command);
163  }
164
165  public function scankw($dbname) {
166    $command = $this->clientcmd." -C scankw ".escapeshellarg($dbname);
167    return $this->_run_command($command);
168  }
169
170  public function addref($owner, $refdata, $type, $encoding) {
171    return array("ERROR");
172  }
173
174  public function addref_from_file($owner, $file, $type, $encoding) {
175    $command = $this->clientcmd." -C addref -A $type ".escapeshellarg($file);
176    if (strlen($encoding) > 0) {
177      $command .= " -E $encoding";
178    }
179    return $this->_run_command($command);
180  }
181
182  public function updateref($owner, $is_personal, $refdata, $type, $encoding) {
183    return array("ERROR");
184  }
185
186  public function updateref_from_file($owner, $file, $type, $encoding) {
187    $command = $this->clientcmd." -C updateref -A $type ".escapeshellarg($file);
188    if (strlen($encoding) > 0) {
189      $command .= " -E $encoding";
190    }
191    return $this->_run_command($command);
192  }
193
194  public function checkref($risdata, $type, $encoding, $outtype) {
195    return array("ERROR");
196  }
197
198  public function checkref_from_file($owner, $file, $type, $encoding) {
199    $command = $this->clientcmd." -C checkref -A $type ".escapeshellarg($file);
200    if (strlen($encoding) > 0) {
201      $command .= " -E $encoding";
202    }
203    return $this->_run_command($command);
204  }
205
206  public function deleteref($idlist) {
207    $command = $this->clientcmd." -C deleteref ".escapeshellarg($idlist);
208    return $this->_run_command($command);
209  }
210
211  public function addnote($owner, $xnotedata) {
212    return array("ERROR");
213  }
214
215  public function addnote_from_file($owner, $file) {
216    $command = $this->clientcmd." -C addnote ".escapeshellarg($file);
217    return $this->_run_command($command);
218  }
219
220  public function updatenote($owner, $xnotedata) {
221    return array("ERROR");
222  }
223
224  public function updatenote_from_file($owner, $file) {
225    $command = $this->clientcmd." -C note ".escapeshellarg($file);
226    return $this->_run_command($command);
227  }
228
229  public function deletenote($idlist) {
230    $command = $this->clientcmd." -C deletenote ".escapeshellarg($idlist);
231    return $this->_run_command($command);
232  }
233
234  public function addlink($linkspec) {
235    $command = $this->clientcmd." -C addlink ".escapeshellarg($idlist);
236    return $this->_run_command($command);
237  }
238
239  public function deletelink($linkspec) {
240    $command = $this->clientcmd." -C deletelink ".escapeshellarg($idlist);
241    return $this->_run_command($command);
242  }
243
244  public function getas($limit_string, $freq, $name) {
245    $command = $this->clientcmd." -C getas ".escapeshellarg($name);
246    if (strlen($freq) > 0) {
247      $command .= " -s $freq";
248    }
249    if (strlen($limit_string) > 0) {
250      $command .= " -N $limit_string";
251    }
252    return $this->_run_command($command);
253  }
254
255  public function getau($limit_string, $freq, $name) {
256    $command = $this->clientcmd." -C getau ".escapeshellarg($name);
257    if (strlen($freq) > 0) {
258      $command .= " -s $freq";
259    }
260    if (strlen($limit_string) > 0) {
261      $command .= " -N $limit_string";
262    }
263    return $this->_run_command($command);
264  }
265
266  public function getax($limit_string, $freq, $name) {
267    $command = $this->clientcmd." -C getax ".escapeshellarg($name);
268    if (strlen($freq) > 0) {
269      $command .= " -s $freq";
270    }
271    if (strlen($limit_string) > 0) {
272      $command .= " -N $limit_string";
273    }
274    return $this->_run_command($command);
275  }
276
277  public function geted($limit_string, $freq, $name) {
278    $command = $this->clientcmd." -C geted ".escapeshellarg($name);
279    if (strlen($freq) > 0) {
280      $command .= " -s $freq";
281    }
282    if (strlen($limit_string) > 0) {
283      $command .= " -N $limit_string";
284    }
285    return $this->_run_command($command);
286  }
287
288  public function getkw($limit_string, $freq, $name) {
289    $command = $this->clientcmd." -C getkw ".escapeshellarg($name);
290    if (strlen($freq) > 0) {
291      $command .= " -s $freq";
292    }
293    if (strlen($limit_string) > 0) {
294      $command .= " -N $limit_string";
295    }
296    return $this->_run_command($command);
297  }
298
299  public function getjf($is_all, $limit_string, $freq, $name) {
300    $command = $this->clientcmd." -C getjf ".escapeshellarg($name);
301    if (strlen($freq) > 0) {
302      $command .= " -s $freq";
303    }
304    if (strlen($limit_string) > 0) {
305      $command .= " -N $limit_string";
306    }
307    if ($is_all=="t") {
308      $command .= " -a ";
309    }
310    return $this->_run_command($command);
311  }
312
313  public function getjo($is_all, $limit_string, $freq, $name) {
314    $command = $this->clientcmd." -C getjo ".escapeshellarg($name);
315    if (strlen($freq) > 0) {
316      $command .= " -s $freq";
317    }
318    if (strlen($limit_string) > 0) {
319      $command .= " -N $limit_string";
320    }
321    if ($is_all=="t") {
322      $command .= " -a ";
323    }
324    return $this->_run_command($command);
325  }
326
327  public function getj1($is_all, $limit_string, $freq, $name) {
328    $command = $this->clientcmd." -C getj1 ".escapeshellarg($name);
329    if (strlen($freq) > 0) {
330      $command .= " -s $freq";
331    }
332    if (strlen($limit_string) > 0) {
333      $command .= " -N $limit_string";
334    }
335    if ($is_all=="t") {
336      $command .= " -a ";
337    }
338    return $this->_run_command($command);
339  }
340
341  public function getj2($is_all, $limit_string, $freq, $name) {
342    $command = $this->clientcmd." -C getj2 ".escapeshellarg($name);
343    if (strlen($freq) > 0) {
344      $command .= " -s $freq";
345    }
346    if (strlen($limit_string) > 0) {
347      $command .= " -N $limit_string";
348    }
349    if ($is_all=="t") {
350      $command .= " -a ";
351    }
352    return $this->_run_command($command);
353  }
354
355  public function getref($type, $format_string, $sort_string, $listname, $encoding, $limit_string, $namespace, $frequency, $query) {
356    $command = $this->clientcmd." -C getref ";
357    if (strlen($type) > 0) {
358      $command .= " -t ".escapeshellarg($type);
359    }
360    if (strlen($format_string) > 0) {
361      $command .= " -s ".escapeshellarg($format_string);
362    }
363    if (strlen($sort_string) > 0) {
364      $command .= "-S ".escapeshellarg($sort_string);
365    }
366    if (strlen($listname) > 0) {
367      $command .= "-b ".escapeshellarg($listname);
368    }
369    if (strlen($encoding) > 0) {
370      $command .= "-E ".escapeshellarg($encoding);
371    }
372    if (strlen($limit_string) > 0) {
373      $command .= "-N ".escapeshellarg($limit_string);
374    }
375    if (strlen($namespace) > 0) {
376      $command .= "-n ".escapeshellarg($namespace);
377    }
378    if (strlen($query) > 0) {
379      $command .= " ".escapeshellarg($query);
380      //      $command .= " ".$query;
381    }
382    if (strlen($frequency) > 0 && $frequency != 0) {
383      $command .= " -Q";
384    }
385
386    return $this->_run_command($command);
387  }
388
389  public function countref($listname, $limit_string, $query) {
390    $command = $this->clientcmd." -C countref ";
391    if (strlen($listname) > 0) {
392      $command .= "-b ".escapeshellarg($listname);
393    }
394    if (strlen($limit_string) > 0) {
395      $command .= "-N ".escapeshellarg($limit_string);
396    }
397    if (strlen($query) > 0) {
398      $command .= " ".escapeshellarg($query);
399    }
400
401    return $this->_run_command($command);
402  }
403
404  public function pickref($idlist, $listname) {
405    $command = $this->clientcmd." -C pickref ";
406    if (strlen($listname) > 0) {
407      $command .= "-b ".escapeshellarg($listname);
408    }
409
410    $command .= " ".escapeshellarg($idlist);
411
412    return $this->_run_command($command);
413  }
414
415  public function dumpref($idlist, $listname) {
416    $command = $this->clientcmd." -C dumpref ";
417    if (strlen($listname) > 0) {
418      $command .= "-b ".escapeshellarg($listname);
419    }
420
421    $command .= " ".escapeshellarg($idlist);
422
423    return $this->_run_command($command);
424  }
425
426  public function getnote($type, $format_string, $sort_string, $encoding, $limit_string, $namespace, $query) {
427    $command = $this->clientcmd." -C getnote ";
428    if (strlen($type) > 0) {
429      $command .= " -t ".escapeshellarg($type);
430    }
431    if (strlen($format_string) > 0) {
432      $command .= " -s ".escapeshellarg($format_string);
433    }
434    if (strlen($sort_string) > 0) {
435      $command .= "-S ".escapeshellarg($sort_string);
436    }
437    if (strlen($encoding) > 0) {
438      $command .= "-E ".escapeshellarg($encoding);
439    }
440    if (strlen($limit_string) > 0) {
441      $command .= "-N ".escapeshellarg($limit_string);
442    }
443    if (strlen($namespace) > 0) {
444      $command .= "-n ".escapeshellarg($namespace);
445    }
446    if (strlen($query) > 0) {
447      $command .= " ".escapeshellarg($query);
448    }
449
450    return $this->_run_command($command);
451  }
452
453  public function countnote($listname, $limit_string, $query) {
454    $command = $this->clientcmd." -C countnote ";
455    if (strlen($limit_string) > 0) {
456      $command .= "-N ".escapeshellarg($limit_string);
457    }
458    if (strlen($query) > 0) {
459      $command .= " ".escapeshellarg($query);
460    }
461
462    return $this->_run_command($command);
463  }
464
465  public function selectdb($dbname) {
466    $this->dbarg = strlen($dbname) ? " -d ".$dbname : "";
467    $this->clientcmd = $this->client.$this->usernamearg.$this->passwordarg.$this->dbarg." -c stdout ";
468    return array($dbname);
469  }
470
471  public function whichdb() {
472    $command = $this->clientcmd." -C whichdb";
473    return $this->_run_command($command);
474  }
475
476  public function updatejo($names) {
477    $command = $this->clientcmd." -C updatejo ".escapeshellarg($names);
478    return $this->_run_command($command);
479  }
480
481  public function texbib($style, $cite_data) {
482    return array("ERROR");
483  }
484
485  public function dbib($style, $encoding, $cite_data) {
486    return array("ERROR");
487  }
488
489  public function getrefx($type, $encoding, $cite_data) {
490    return array("ERROR");
491  }
492
493  public function getversion() {
494    $command = $this->clientcmd." -v";
495    return $this->_run_command($command);
496  }
497
498} /* end class RefDB */
499
500?>
501