1 | <?php |
---|
2 | |
---|
3 | date_default_timezone_set('Europe/Warsaw'); |
---|
4 | error_reporting(E_ALL); |
---|
5 | ini_set('memory_limit', '512M'); |
---|
6 | set_time_limit(120); |
---|
7 | |
---|
8 | // ustawienie biezacej sciezki |
---|
9 | $aCurDir = pathinfo(__FILE__); |
---|
10 | |
---|
11 | $cdir = trim(getcwd(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR; |
---|
12 | |
---|
13 | chdir($aCurDir['dirname']); |
---|
14 | |
---|
15 | |
---|
16 | //trigger_error(''.$cdir.'', E_USER_ERROR); |
---|
17 | //exit(1); |
---|
18 | |
---|
19 | function printHelp() { |
---|
20 | echo "\nLista obslugiwanych parametrów:\n"; |
---|
21 | echo "\t-h - wyswietla pomoc - opis parametrow\n\n"; |
---|
22 | echo "\t-f plik\n"; |
---|
23 | //echo "\t-org target orunid\n"; |
---|
24 | //echo "\t-a rodzaj akcji\n"; |
---|
25 | echo "\n"; |
---|
26 | |
---|
27 | exit(1); |
---|
28 | } |
---|
29 | |
---|
30 | $params = array(); |
---|
31 | $args = array(); |
---|
32 | |
---|
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 | if (($k = array_search('-dctpid', $args)) !== FALSE) { |
---|
47 | $dctpid = trim($args[++$k]); |
---|
48 | if (preg_match('/^\d+$/', $dctpid)) $params['dctpid'] = $dctpid; |
---|
49 | } |
---|
50 | |
---|
51 | if (empty($file_name) OR (array_search('-h', $args) !== FALSE)) { |
---|
52 | printHelp(); |
---|
53 | } |
---|
54 | if (!file_exists($cdir.$file_name)) trigger_error('Plik '.$cdir.$file_name.' nie istnieje', E_USER_ERROR); |
---|
55 | if (!is_readable($cdir.$file_name)) trigger_error('Nie można czytać pliku '.$cdir.$file_name.'', E_USER_ERROR); |
---|
56 | |
---|
57 | } else { |
---|
58 | printHelp(); |
---|
59 | } |
---|
60 | |
---|
61 | require_once('config.inc'); |
---|
62 | require_once('../EDokApiClient.inc'); |
---|
63 | |
---|
64 | // w location |
---|
65 | |
---|
66 | $ops = array( |
---|
67 | 'location' => Config::$EDOK_API_LOCATION, |
---|
68 | "uri" => "eDokumentyAPI", |
---|
69 | 'encoding'=>'UTF-8' |
---|
70 | ); |
---|
71 | |
---|
72 | $client = new EDokApiClient(null, $ops); |
---|
73 | $client->setUser(Config::$EDOK_API_LOGIN); |
---|
74 | $client->setPass(md5(Config::$EDOK_API_PASSWORD)); |
---|
75 | $header = new SoapHeader('eDokumentyAPI', 'entity_symbol', Config::$DEFAULT_ENTITY_SYMBOL); |
---|
76 | $client->__setSoapHeaders($header); |
---|
77 | |
---|
78 | try { |
---|
79 | $file_name = trim($file_name); |
---|
80 | $pi = pathinfo($file_name); |
---|
81 | $fpath = $pi['dirname']; |
---|
82 | $file_name = $pi['basename']; |
---|
83 | |
---|
84 | $fpath = $cdir.$fpath.DIRECTORY_SEPARATOR.$file_name; |
---|
85 | |
---|
86 | if (file_exists($fpath) && is_readable($fpath)) { |
---|
87 | $eof = FALSE; |
---|
88 | $timeout = 15; |
---|
89 | while (--$timeout && !($fp = @fopen($fpath, 'rb'))) { |
---|
90 | sleep(1); |
---|
91 | } |
---|
92 | if ($fp) { |
---|
93 | fclose($fp); |
---|
94 | } |
---|
95 | } else { |
---|
96 | throw new Exception('can\'t open '.$fpath.' for reading'); |
---|
97 | } |
---|
98 | |
---|
99 | //kazdy tekst musi być w UTF-8 |
---|
100 | foreach($params as &$value) { |
---|
101 | if (!is_numeric($value)) { |
---|
102 | $value = iconv('Windows-1250', 'UTF-8', $value); |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | $dscrpt = ''; |
---|
107 | $data = array(); |
---|
108 | if (!isset($params['dctpid']) AND isset($params['dctptp']) AND !empty($params['dctptp'])) { |
---|
109 | $data['dctptp'] = $params['dctptp']; |
---|
110 | } |
---|
111 | if (!empty($data)) { |
---|
112 | $dtyp = $client->getDocumentTypeData($data); |
---|
113 | |
---|
114 | $params['dctpid'] = NULL; |
---|
115 | if (isset($dtyp['dctpid'])) { |
---|
116 | $params['dctpid'] = $dtyp['dctpid']; |
---|
117 | |
---|
118 | if ($dtyp['dctptp'] != 'Other') $dscrpt = $dtyp['dctpnm']; |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | $target = (isset($params['target']) and $params['target']) ? $params['target'] : (isset(Config::$TARGET_ORUNID) ? Config::$TARGET_ORUNID : 1); |
---|
123 | |
---|
124 | $data = array( |
---|
125 | 'dscrpt' => 'Dokument ('.iconv('Windows-1250', 'UTF-8', $file_name).')', |
---|
126 | 'fixinf' => iconv('Windows-1250', 'UTF-8', $file_name), |
---|
127 | 'target' => $target, |
---|
128 | 'dctpid' => isset($params['dctpid']) ? $params['dctpid'] : 2, |
---|
129 | ); |
---|
130 | |
---|
131 | try { |
---|
132 | $doc_id = $client->createDocument($data); |
---|
133 | |
---|
134 | } catch(SoapFault $fault) { |
---|
135 | throw new Exception("[{$fault->faultcode}]: {$fault->faultstring}"); |
---|
136 | } |
---|
137 | if (isset($doc_id) AND $doc_id) { |
---|
138 | $dd = 0; |
---|
139 | try { |
---|
140 | $file = base64_encode(file_get_contents($fpath)); |
---|
141 | $file_name = iconv('Windows-1250', 'UTF-8', $file_name); |
---|
142 | |
---|
143 | $dd = $client->addAttachmentToDocument($file, $file_name, $doc_id); |
---|
144 | |
---|
145 | } catch(SoapFault $fault) { |
---|
146 | throw new Exception("[{$fault->faultcode}]: {$fault->faultstring}"); |
---|
147 | } |
---|
148 | if ($dd > 0) { |
---|
149 | // po udanych operacjach usuń plik |
---|
150 | if (isset(Config::$SENT_DIR)) { |
---|
151 | rename($fpath, rtrim(Config::$SENT_DIR, '\\/').'/'.$file_name); |
---|
152 | } else { |
---|
153 | unlink($fpath); |
---|
154 | } |
---|
155 | } else { |
---|
156 | throw new Exception('Attachment not created'); |
---|
157 | } |
---|
158 | } else { |
---|
159 | throw new Exception('Document not created (unknown error)'); |
---|
160 | } |
---|
161 | |
---|
162 | } catch(Exception $e) { |
---|
163 | // oznacz błędny plik |
---|
164 | //rename($fpath, $fpath.'_'); |
---|
165 | |
---|
166 | trigger_error($e->getMessage(), E_USER_ERROR); |
---|
167 | } |
---|
168 | |
---|
169 | exit(0); |
---|
170 | |
---|
171 | ?> |
---|