1 | <?php |
---|
2 | /* |
---|
3 | * Barcode document creator |
---|
4 | * eDokumenty script |
---|
5 | * Copyrights by BetaSoft 2014 |
---|
6 | * |
---|
7 | */ |
---|
8 | date_default_timezone_set('Europe/Warsaw'); |
---|
9 | error_reporting(E_ALL); |
---|
10 | ini_set('memory_limit', '512M'); |
---|
11 | set_time_limit(120); |
---|
12 | |
---|
13 | // ustawienie biezacej sciezki |
---|
14 | $aCurDir = pathinfo(__FILE__); |
---|
15 | $cdir = trim(getcwd(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; |
---|
16 | chdir($aCurDir['dirname']); |
---|
17 | |
---|
18 | //trigger_error(''.$cdir.$file_name.'', E_USER_ERROR); |
---|
19 | //exit(1); |
---|
20 | |
---|
21 | function printHelp() { |
---|
22 | echo "\nLista obslugiwanych parametrów:\n"; |
---|
23 | echo "\t-h - wyswietla pomoc - opis parametrow\n\n"; |
---|
24 | echo "\t-f plik\n"; |
---|
25 | //echo "\t-org target orunid\n"; |
---|
26 | //echo "\t-a rodzaj akcji\n"; |
---|
27 | echo "\n"; |
---|
28 | |
---|
29 | exit(1); |
---|
30 | } |
---|
31 | |
---|
32 | $args = array(); |
---|
33 | if (isset($_SERVER['argv'])) { |
---|
34 | $args = $_SERVER['argv']; |
---|
35 | array_shift($args); |
---|
36 | |
---|
37 | $file_name = NULL; |
---|
38 | $action = NULL; |
---|
39 | |
---|
40 | if (($k = array_search('-f', $args)) !== FALSE) { |
---|
41 | $file_name = $args[++$k]; |
---|
42 | } |
---|
43 | if (($k = array_search('-a', $args)) !== FALSE) { |
---|
44 | $action = $args[++$k]; |
---|
45 | } |
---|
46 | |
---|
47 | if (empty($file_name) OR (array_search('-h', $args) !== FALSE)) { |
---|
48 | printHelp(); |
---|
49 | } |
---|
50 | if (!file_exists($cdir.$file_name)) trigger_error('Plik '.$cdir.$file_name.' nie istnieje', E_USER_ERROR); |
---|
51 | if (!is_readable($cdir.$file_name)) trigger_error('Nie można czytać pliku '.$cdir.$file_name.'', E_USER_ERROR); |
---|
52 | |
---|
53 | } else { |
---|
54 | printHelp(); |
---|
55 | } |
---|
56 | require_once('config.inc'); |
---|
57 | |
---|
58 | if (!isset(Config::$NAME_REGEX)) trigger_error('NAME_REGEX not defined', E_USER_ERROR); |
---|
59 | |
---|
60 | require_once('EDokApiClient.inc'); |
---|
61 | |
---|
62 | // w location |
---|
63 | |
---|
64 | $ops = array( |
---|
65 | 'location' => Config::$EDOK_API_LOCATION, |
---|
66 | "uri" => "eDokumentyAPI", |
---|
67 | 'encoding'=>'UTF-8' |
---|
68 | ); |
---|
69 | |
---|
70 | $client = new EDokApiClient(null, $ops); |
---|
71 | $client->setUser(Config::$EDOK_API_LOGIN); |
---|
72 | $client->setPass(md5(Config::$EDOK_API_PASSWORD)); |
---|
73 | $header = new SoapHeader('eDokumentyAPI', 'entity_symbol', Config::$DEFAULT_ENTITY_SYMBOL); |
---|
74 | $client->__setSoapHeaders($header); |
---|
75 | |
---|
76 | try { |
---|
77 | $params = array(); |
---|
78 | $file_name = trim($file_name); |
---|
79 | $pi = pathinfo($file_name); |
---|
80 | $data = array( |
---|
81 | '%source_path' => $cdir.$file_name, |
---|
82 | '%source_dir' => $cdir.$pi['dirname'], |
---|
83 | '%source_file' => $pi['basename'], |
---|
84 | ); |
---|
85 | $fpath = $pi['dirname']; |
---|
86 | $file_name = $pi['basename']; |
---|
87 | |
---|
88 | $fpath = $cdir.$fpath.DIRECTORY_SEPARATOR.$file_name; |
---|
89 | |
---|
90 | if (file_exists($fpath) && is_readable($fpath)) { |
---|
91 | if (preg_match('/\.pdf$/i', $fpath)) { |
---|
92 | $eof = FALSE; |
---|
93 | $timeout = 15; |
---|
94 | while (--$timeout && !($fp = @fopen($fpath, 'rb'))) { |
---|
95 | sleep(1); |
---|
96 | } |
---|
97 | if ($fp) { |
---|
98 | $timeout = 20; |
---|
99 | while (--$timeout) { |
---|
100 | if (fseek($fp, -10, SEEK_END) > -1) { |
---|
101 | $contents = fread($fp, 10); |
---|
102 | if (strpos($contents, '%%EOF') !== FALSE) { |
---|
103 | $eof = TRUE; |
---|
104 | break; |
---|
105 | } |
---|
106 | } |
---|
107 | sleep(1); |
---|
108 | } |
---|
109 | fclose($fp); |
---|
110 | } |
---|
111 | if (!$eof) { |
---|
112 | throw new Exception('%%EOF not found in '.$fpath); |
---|
113 | } |
---|
114 | } |
---|
115 | } else { |
---|
116 | throw new Exception('can\'t open '.$fpath.' for reading'); |
---|
117 | } |
---|
118 | |
---|
119 | // jeżeli to pdf zeskanowany to zmieniamy nazwę pliku na przeczytaną z qrcode |
---|
120 | if (preg_match(Config::$SOURCE_FILE_NAME_REGEX, $file_name)) { |
---|
121 | $file_name = ''; |
---|
122 | $file_name_2 = str_replace('.pdf', '', $data['%source_path']); |
---|
123 | |
---|
124 | $cmd_path = realpath('poppler/bin'); |
---|
125 | $cmd = '"'.$cmd_path.'\\pdftocairo.exe" -png -mono -singlefile "'.$data['%source_path'].'" "'.$file_name_2.'"'.PHP_EOL; |
---|
126 | $cmd_path = realpath('ZBar/bin'); |
---|
127 | $cmd .= '"'.$cmd_path.'\\zbarimg.exe" -q --noxml --raw "'.$file_name_2.'.png"'; |
---|
128 | |
---|
129 | $bat = md5($cmd).'.bat'; |
---|
130 | |
---|
131 | file_put_contents($bat, $cmd); |
---|
132 | $res = exec($bat, $out, $ret); |
---|
133 | |
---|
134 | if (($ret !== 0) || empty($res)) { |
---|
135 | $cmd_path = realpath('poppler/bin'); |
---|
136 | $cmd = '"'.$cmd_path.'\\pdftocairo.exe" -png -gray -singlefile "'.$data['%source_path'].'" "'.$file_name_2.'"'.PHP_EOL; |
---|
137 | $cmd_path = realpath('ZBar/bin'); |
---|
138 | $cmd .= '"'.$cmd_path.'\\zbarimg.exe" -q --noxml --raw "'.$file_name_2.'.png"'; |
---|
139 | |
---|
140 | file_put_contents($bat, $cmd); |
---|
141 | $res = exec($bat, $out, $ret); |
---|
142 | } |
---|
143 | |
---|
144 | // unlink($bat); |
---|
145 | // unlink($file_name_2.'.png'); |
---|
146 | |
---|
147 | error_log(var_export($ret, true)); |
---|
148 | error_log(var_export($res, true)); |
---|
149 | error_log(var_export($out, true)); |
---|
150 | |
---|
151 | if (($ret === 0) && !empty($res)) { |
---|
152 | $data['%code'] = $res; |
---|
153 | $file_name = str_replace(array_keys($data), array_values($data), Config::$TARGET_FILE); |
---|
154 | if (!$file_name || !preg_match(Config::$NAME_REGEX, $file_name, $params)) { |
---|
155 | throw new Exception('Przeczytanna wartość kodu ('.$file_name.'), nie pasuje do wzorca "'.Config::$NAME_REGEX.'"'); |
---|
156 | } |
---|
157 | |
---|
158 | $timeout = 2; |
---|
159 | while ($timeout-- && (($resr = @rename($data['%source_path'], $file_name)) == FALSE)) { |
---|
160 | sleep(1); |
---|
161 | } |
---|
162 | } |
---|
163 | } else { |
---|
164 | exit(0); |
---|
165 | } |
---|
166 | |
---|
167 | if (!$file_name || !preg_match(Config::$NAME_REGEX, $file_name, $params)) { |
---|
168 | throw new Exception($file_name.' nie pasuje do wzorca "'.Config::$NAME_REGEX.'"'); |
---|
169 | } |
---|
170 | |
---|
171 | //kazdy tekst musi być w UTF-8 |
---|
172 | foreach($params as &$value) { |
---|
173 | if (!is_numeric($value)) { |
---|
174 | $value = iconv('Windows-1250', 'UTF-8', $value); |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | $data = array(); |
---|
179 | $params['dctpid'] = 2; |
---|
180 | if (isset($params['dctptp']) AND !empty($params['dctptp'])) { |
---|
181 | $data['dctptp'] = $params['dctptp']; |
---|
182 | } |
---|
183 | if (isset($params['dctpid']) AND !empty($params['dctpid'])) { |
---|
184 | $data['dctpid'] = 2; |
---|
185 | } |
---|
186 | //var_dump($data); |
---|
187 | if (!empty($data)) { |
---|
188 | $dtyp = $client->getDocumentTypeData($data); |
---|
189 | //var_dump($dtyp); |
---|
190 | |
---|
191 | $params['dctpid'] = NULL; |
---|
192 | if (isset($dtyp[0]) && is_array($dtyp[0])) $dtyp = $dtyp[0]; |
---|
193 | if (isset($dtyp['dctpid'])) $params['dctpid'] = $dtyp['dctpid']; |
---|
194 | } |
---|
195 | //var_dump($params); |
---|
196 | if (!isset($params['dctpid'])) { |
---|
197 | throw new Exception(''); |
---|
198 | } |
---|
199 | $dscrpt = ''; |
---|
200 | if ($dtyp['dctptp'] != 'Other') $dscrpt = $dtyp['dctpnm']; |
---|
201 | |
---|
202 | if (isset($params['type'])) $dscrpt .= (empty($dscrpt) ? '' : ' ').$params['type']; |
---|
203 | |
---|
204 | if (isset($params['name'])) $dscrpt .= ' od '.$params['name']; |
---|
205 | |
---|
206 | $target = (isset($params['target']) and $params['target']) ? $params['target'] : (isset(Config::$TARGET_ORUNID) ? Config::$TARGET_ORUNID : 1); |
---|
207 | |
---|
208 | $data = array( |
---|
209 | 'dscrpt' => 'Dokument NR '.iconv('Windows-1250', 'UTF-8', $res), |
---|
210 | 'fixinf' => str_replace('.pdf', '', iconv('Windows-1250', 'UTF-8', $file_name)), |
---|
211 | 'target' => $target, |
---|
212 | 'dctpid' => isset($params['dctpid']) ? $params['dctpid'] : 2, |
---|
213 | ); |
---|
214 | if (isset($params['client']) AND ($params['client'] !== '0_BEZ_KLIENTA')) { |
---|
215 | $data['to_contact_symbol'] = $params['client']; |
---|
216 | } |
---|
217 | // data wplywu |
---|
218 | if (isset($params['adddat'])) { |
---|
219 | $data['adddat'] = preg_replace('/(\d{2})(\d{2})(\d{2})/', '20$3-$2-$1', $params['adddat']); |
---|
220 | } |
---|
221 | // data wystawienia |
---|
222 | if (isset($params['crtdat'])) { |
---|
223 | $data['crtdat'] = preg_replace('/(\d{4})(\d{2})(\d{2})/', '$1-$2-$3', $params['crtdat']); |
---|
224 | } |
---|
225 | //var_dump($data); |
---|
226 | //exit(0); |
---|
227 | try { |
---|
228 | $doc_id = $client->createDocument($data); |
---|
229 | |
---|
230 | } catch(SoapFault $fault) { |
---|
231 | throw new Exception("[{$fault->faultcode}]: {$fault->faultstring}"); |
---|
232 | } |
---|
233 | if (isset($doc_id) AND $doc_id) { |
---|
234 | $dd = 0; |
---|
235 | try { |
---|
236 | $file = base64_encode(file_get_contents($file_name)); |
---|
237 | $name = iconv('Windows-1250', 'UTF-8', $res.".pdf"); |
---|
238 | |
---|
239 | $dd = $client->addAttachmentToDocument($file, $name, $doc_id); |
---|
240 | |
---|
241 | } catch(SoapFault $fault) { |
---|
242 | throw new Exception("[{$fault->faultcode}]: {$fault->faultstring}"); |
---|
243 | } |
---|
244 | |
---|
245 | |
---|
246 | |
---|
247 | if ($dd > 0) { |
---|
248 | // po udanych operacjach usuń plik |
---|
249 | if (isset(Config::$SENT_DIR)) { |
---|
250 | rename($file_name, rtrim(Config::$SENT_DIR, '\\/').'/'.$name); |
---|
251 | } else { |
---|
252 | unlink($file_name); |
---|
253 | } |
---|
254 | } else { |
---|
255 | throw new Exception('Attachment not created'); |
---|
256 | } |
---|
257 | } else { |
---|
258 | throw new Exception('Document not created (unknown error)'); |
---|
259 | } |
---|
260 | |
---|
261 | } catch(Exception $e) { |
---|
262 | // oznacz błędny plik |
---|
263 | if (file_exists($fpath)) { |
---|
264 | rename($fpath, $fpath.'_'); |
---|
265 | |
---|
266 | trigger_error($e->getMessage(), E_USER_ERROR); |
---|
267 | } |
---|
268 | } |
---|
269 | |
---|
270 | exit(0); |
---|
271 | |
---|
272 | ?> |
---|