1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13: class Hylax_Driver_hylafax extends Hylax_Driver {
14:
15: var $_states = array();
16: var $_stat_cols = array();
17: var $_cmd = array();
18:
19: public function __construct($params)
20: {
21: parent::__construct($params);
22:
23: $this->_states = Hylax::getStates();
24: $this->_stat_cols = Hylax::getStatCols();
25: $this->_cmd = array('sendfax' => '/usr/bin/sendfax');
26: }
27:
28: public function send($number, $data, $time = null)
29: {
30: $command = sprintf('%s -n -d %s',
31: $this->_cmd['sendfax'],
32: $number);
33: $descriptorspec = array(0 => array("pipe", "r"),
34: 1 => array("pipe", "w"),
35: 2 => array("pipe", "w"));
36:
37:
38: $process = proc_open($command, $descriptorspec, $pipes);
39: if (is_resource($process)) {
40: fwrite($pipes[0], $data);
41: fclose($pipes[0]);
42:
43: $output = '';
44: while (!feof($pipes[1])) {
45: $output .= fgets($pipes[1], 1024);
46: }
47: fclose($pipes[1]);
48:
49: proc_close($process);
50: }
51:
52:
53: preg_match('/request id is (\d+)/', $output, $matches);
54: if (isset($matches[1])) {
55: return $matches[1];
56: }
57: return PEAR::raiseError(sprintf(_("Could not send fax. %s"), $output));
58: }
59:
60: public function numFaxesIn()
61: {
62:
63:
64: }
65:
66: public function numFaxesOut()
67: {
68:
69:
70: }
71:
72: public function getFolder($folder, $path = null)
73: {
74: switch ($folder) {
75: case 'inbox':
76: return $this->_parseFaxStat($this->_exec('faxstat -r'));
77: break;
78:
79: case 'outbox':
80: return $this->_parseFaxStat($this->_exec('faxstat -s'));
81: break;
82:
83: case 'sent':
84: return $this->_parseFaxStat($this->_exec('faxstat -d'));
85: break;
86:
87: case 'archive':
88:
89: break;
90: }
91: }
92:
93: public function getJob($job_id, $folder, $path = null)
94: {
95: global $conf;
96:
97: $job = array();
98:
99: switch ($folder) {
100: case 'inbox':
101: break;
102:
103: case 'outbox':
104: $filename = '/var/spool/fax/sendq/q' . $job_id;
105: $job = $this->_getParseSendJob($filename);
106: break;
107:
108: case 'sent':
109: $filename = '/var/spool/fax/doneq/q' . $job_id;
110: $job = $this->_getParseSendJob($filename);
111: break;
112:
113: case 'archive':
114:
115: break;
116: }
117:
118: $job['thumbs'] = $this->getThumbs($job_id, 'docq/' . $job['postscript'], true);
119:
120: return $job;
121: }
122:
123: public function getStatus($job_id)
124: {
125: static $send_q = array();
126: static $done_q = array();
127: if (empty($send_q)) {
128: exec('/usr/bin/faxstat -s', $output);
129: $iMax = count($output);
130: for ($i = 4; $i < $iMax; $i++) {
131: $send_q[] = $output[$i];
132: }
133: }
134: if (empty($done_q)) {
135: exec('/usr/bin/faxstat -d', $output);
136: $iMax = count($output);
137: for ($i = 4; $i < $iMax; $i++) {
138: $done_q[] = $output[$i];
139: }
140: }
141:
142:
143: foreach ($send_q as $line) {
144: if ((int)substr($line, 0, 4) == $job_id) {
145: return _("Sending");
146: }
147: }
148: foreach ($done_q as $line) {
149: if ((int)substr($line, 0, 4) == $job_id) {
150: return substr($line, 51);
151: }
152: }
153: return '';
154: }
155:
156: protected function _getParseSendJob($filename)
157: {
158: $job = array();
159: $job_file = file_get_contents($filename);
160: $job_file = explode("\n", $job_file);
161: foreach ($job_file as $line) {
162: if (empty($line)) {
163: continue;
164: }
165: list($key, $value) = explode(':', $line, 2);
166: if ($key == 'postscript') {
167: $job[$key] = basename($value);
168: } else {
169: $job[$key] = $value;
170: }
171: }
172: return $job;
173: }
174:
175: public function getThumbs($job_id, $ps)
176: {
177: if ($this->_vfs->exists(HYLAX_VFS_PATH, $job_id)) {
178:
179: $images = $this->_vfs->listFolder(HYLAX_VFS_PATH . '/' . $job_id, 'doc.png');
180: if (!empty($images)) {
181: return array_keys($images);
182: }
183: }
184: $images = $this->imagesToVFS($job_id, $ps);
185: return array_keys($images);
186: }
187:
188: public function imagesToVFS($job_id, $ps)
189: {
190: global $conf;
191:
192: $this->_vfs->autoCreatePath(HYLAX_VFS_PATH . '/' . $job_id);
193:
194: $ps = '/var/spool/fax/' . $ps;
195: $vfs_path = $conf['vfs']['params']['vfsroot'] . '/' . HYLAX_VFS_PATH;
196:
197: $cmd = sprintf('convert -geometry 25%% %s %s/%s/thumb.png', $ps, $vfs_path, $job_id);
198: $result = $this->_exec($cmd);
199:
200: $cmd = sprintf('convert %s %s/%s/doc.png', $ps, $vfs_path, $job_id);
201: $result = $this->_exec($cmd);
202:
203:
204: return $this->_vfs->listFolder(HYLAX_VFS_PATH . '/' . $job_id, 'doc.png');
205: }
206:
207: protected function _exec($cmd, $input = '')
208: {
209: $spec = array(
210: 1 => array('pipe', 'w'),
211: 2 => array('file', '/tmp/error-output.txt', 'a')
212: );
213: $proc = proc_open($this->_params['base_path'] . $cmd, $spec, $pipes);
214:
215:
216: while (!feof($pipes[1])) {
217: $result[] = trim(fgets($pipes[1], 1024));
218: }
219: @fclose($pipes[1]);
220: @fclose($pipes[2]);
221: proc_close($proc);
222:
223: if (empty($result[(count($result) - 1)])) {
224: unset($result[(count($result) - 1)]);
225: }
226:
227: return $result;
228: }
229:
230: protected function _parseFaxStat($result)
231: {
232: $out = array();
233: $i = 0;
234: foreach ($result as $line) {
235:
236: if (!empty($line) && is_numeric($line[0])) {
237: $values = explode('|', $line);
238: foreach ($this->_stat_cols as $j => $key) {
239: $out[$i][$key] = $values[$j];
240: }
241: $out[$i]['state'] = $this->_states[$out[$i]['state']];
242: }
243: $i++;
244: }
245: return $out;
246: }
247:
248: }
249: