| 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.$file_name.'', 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 | $args = array(); |
|---|
| 31 | if (isset($_SERVER['argv'])) { |
|---|
| 32 | $args = $_SERVER['argv']; |
|---|
| 33 | array_shift($args); |
|---|
| 34 | |
|---|
| 35 | $file_name = NULL; |
|---|
| 36 | $action = NULL; |
|---|
| 37 | |
|---|
| 38 | if (($k = array_search('-f', $args)) !== FALSE) { |
|---|
| 39 | $file_name = $args[++$k]; |
|---|
| 40 | } |
|---|
| 41 | if (($k = array_search('-a', $args)) !== FALSE) { |
|---|
| 42 | $action = $args[++$k]; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | if (empty($file_name) OR (array_search('-h', $args) !== FALSE)) { |
|---|
| 46 | printHelp(); |
|---|
| 47 | } |
|---|
| 48 | if (!file_exists($cdir.$file_name)) trigger_error('Plik '.$cdir.$file_name.' nie istnieje', E_USER_ERROR); |
|---|
| 49 | if (!is_readable($cdir.$file_name)) trigger_error('Nie można czytać pliku '.$cdir.$file_name.'', E_USER_ERROR); |
|---|
| 50 | |
|---|
| 51 | } else { |
|---|
| 52 | printHelp(); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | require_once('config.inc'); |
|---|
| 56 | require_once('../EDokApiClient.inc'); |
|---|
| 57 | if (!isset(Config::$NAME_REGEX)) trigger_error('NAME_REGEX not defined', E_USER_ERROR); |
|---|
| 58 | |
|---|
| 59 | // w location |
|---|
| 60 | |
|---|
| 61 | $ops = array( |
|---|
| 62 | 'location' => Config::$EDOK_API_LOCATION, |
|---|
| 63 | "uri" => "eDokumentyAPI", |
|---|
| 64 | 'encoding'=>'UTF-8' |
|---|
| 65 | ); |
|---|
| 66 | |
|---|
| 67 | $client = new EDokApiClient(null, $ops); |
|---|
| 68 | $client->setUser(Config::$EDOK_API_LOGIN); |
|---|
| 69 | $client->setPass(md5(Config::$EDOK_API_PASSWORD)); |
|---|
| 70 | $header = new SoapHeader('eDokumentyAPI', 'entity_symbol', Config::$DEFAULT_ENTITY_SYMBOL); |
|---|
| 71 | $client->__setSoapHeaders($header); |
|---|
| 72 | |
|---|
| 73 | try { |
|---|
| 74 | $params = array(); |
|---|
| 75 | $file_name = trim($file_name); |
|---|
| 76 | $res = preg_match(Config::$NAME_REGEX, $file_name, $params); |
|---|
| 77 | if (!$res) { |
|---|
| 78 | throw new Exception($file_name.' nie pasuje do wzorca "'.Config::$NAME_REGEX.'"'); |
|---|
| 79 | } |
|---|
| 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 | if (preg_match('/\.pdf$/i', $fpath)) { |
|---|
| 88 | $eof = FALSE; |
|---|
| 89 | $timeout = 15; |
|---|
| 90 | while (--$timeout && !($fp = @fopen($fpath, 'rb'))) { |
|---|
| 91 | sleep(1); |
|---|
| 92 | } |
|---|
| 93 | if ($fp) { |
|---|
| 94 | $timeout = 20; |
|---|
| 95 | while (--$timeout) { |
|---|
| 96 | if (fseek($fp, -10, SEEK_END) > -1) { |
|---|
| 97 | $contents = fread($fp, 10); |
|---|
| 98 | if (strpos($contents, '%%EOF') !== FALSE) { |
|---|
| 99 | $eof = TRUE; |
|---|
| 100 | break; |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | sleep(1); |
|---|
| 104 | } |
|---|
| 105 | fclose($fp); |
|---|
| 106 | } |
|---|
| 107 | if (!$eof) { |
|---|
| 108 | throw new Exception('%%EOF not found in '.$fpath); |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | } else { |
|---|
| 112 | throw new Exception('can\'t open '.$fpath.' for reading'); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | //kazdy tekst musi być w UTF-8 |
|---|
| 116 | foreach($params as &$value) { |
|---|
| 117 | if (!is_numeric($value)) { |
|---|
| 118 | $value = iconv('Windows-1250', 'UTF-8', $value); |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | $data = array(); |
|---|
| 123 | $params['dctpid'] = 2; |
|---|
| 124 | if (isset($params['dctptp']) AND !empty($params['dctptp'])) { |
|---|
| 125 | $data['dctptp'] = $params['dctptp']; |
|---|
| 126 | } |
|---|
| 127 | if (isset($params['dctpid']) AND !empty($params['dctpid'])) { |
|---|
| 128 | $data['dctpid'] = 2; |
|---|
| 129 | } |
|---|
| 130 | if (!empty($data)) { |
|---|
| 131 | $dtyp = $client->getDocumentTypeData($data); |
|---|
| 132 | |
|---|
| 133 | $params['dctpid'] = NULL; |
|---|
| 134 | if (isset($dtyp[0]) && is_array($dtyp[0])) $dtyp = $dtyp[0]; |
|---|
| 135 | if (isset($dtyp['dctpid'])) $params['dctpid'] = $dtyp['dctpid']; |
|---|
| 136 | } |
|---|
| 137 | if (!isset($params['dctpid'])) { |
|---|
| 138 | throw new Exception(''); |
|---|
| 139 | } |
|---|
| 140 | $dscrpt = ''; |
|---|
| 141 | if ($dtyp['dctptp'] != 'Other') $dscrpt = $dtyp['dctpnm']; |
|---|
| 142 | |
|---|
| 143 | if (isset($params['type'])) $dscrpt .= (empty($dscrpt) ? '' : ' ').$params['type']; |
|---|
| 144 | |
|---|
| 145 | if (isset($params['name'])) $dscrpt .= ' od '.$params['name']; |
|---|
| 146 | |
|---|
| 147 | $target = (isset($params['target']) and $params['target']) ? $params['target'] : (isset(Config::$TARGET_ORUNID) ? Config::$TARGET_ORUNID : 1); |
|---|
| 148 | |
|---|
| 149 | $data = array( |
|---|
| 150 | 'dscrpt' => 'Dokument ('.iconv('Windows-1250', 'UTF-8', $file_name).')', |
|---|
| 151 | 'fixinf' => str_replace('.pdf', '', iconv('Windows-1250', 'UTF-8', $file_name)), |
|---|
| 152 | 'target' => $target, |
|---|
| 153 | 'dctpid' => isset($params['dctpid']) ? $params['dctpid'] : 2, |
|---|
| 154 | ); |
|---|
| 155 | if ($params['client'] !== '0_BEZ_KLIENTA') { |
|---|
| 156 | $data['to_contact_symbol'] = $params['client']; |
|---|
| 157 | } |
|---|
| 158 | // data wplywu |
|---|
| 159 | if (isset($params['adddat'])) { |
|---|
| 160 | $data['adddat'] = preg_replace('/(\d{4})(\d{2})(\d{2})/', '$1-$2-$3', $params['adddat']); |
|---|
| 161 | } |
|---|
| 162 | // data wystawienia |
|---|
| 163 | if (isset($params['crtdat'])) { |
|---|
| 164 | $data['crtdat'] = preg_replace('/(\d{4})(\d{2})(\d{2})/', '$1-$2-$3', $params['crtdat']); |
|---|
| 165 | } |
|---|
| 166 | //file_put_contents('c:/log.txt', implode(";", $data)); |
|---|
| 167 | //exit(0); |
|---|
| 168 | try { |
|---|
| 169 | $doc_id = $client->createDocument($data); |
|---|
| 170 | |
|---|
| 171 | } catch(SoapFault $fault) { |
|---|
| 172 | throw new Exception("[{$fault->faultcode}]: {$fault->faultstring}"); |
|---|
| 173 | } |
|---|
| 174 | if (isset($doc_id) AND $doc_id) { |
|---|
| 175 | $dd = 0; |
|---|
| 176 | try { |
|---|
| 177 | $file = base64_encode(file_get_contents($fpath)); |
|---|
| 178 | $file_name = iconv('Windows-1250', 'UTF-8', $file_name); |
|---|
| 179 | |
|---|
| 180 | $dd = $client->addAttachmentToDocument($file, $file_name, $doc_id); |
|---|
| 181 | |
|---|
| 182 | } catch(SoapFault $fault) { |
|---|
| 183 | throw new Exception("[{$fault->faultcode}]: {$fault->faultstring}"); |
|---|
| 184 | } |
|---|
| 185 | if ($dd > 0) { |
|---|
| 186 | // po udanych operacjach usuń plik |
|---|
| 187 | rename($fpath, 'C:\\ED-SKANY\\WYSLANE\\'.$file_name); |
|---|
| 188 | } else { |
|---|
| 189 | throw new Exception('Attachment not created'); |
|---|
| 190 | } |
|---|
| 191 | } else { |
|---|
| 192 | throw new Exception('Document not created (unknown error)'); |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | } catch(Exception $e) { |
|---|
| 196 | // oznacz błędny plik |
|---|
| 197 | //rename($fpath, $fpath.'_'); |
|---|
| 198 | |
|---|
| 199 | trigger_error($e->getMessage(), E_USER_ERROR); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | exit(0); |
|---|
| 203 | |
|---|
| 204 | ?> |
|---|