Config::$EDOK_API_LOCATION, "uri" => "eDokumentyAPI", 'encoding'=>'UTF-8' ); $client = new EDokApiClient(null, $ops); $client->setUser(Config::$EDOK_API_LOGIN); $client->setPass(md5(Config::$EDOK_API_PASSWORD)); $header = new SoapHeader('eDokumentyAPI', 'entity_symbol', Config::$DEFAULT_ENTITY_SYMBOL); $client->__setSoapHeaders($header); try { $params = array(); $file_name = trim($file_name); $res = preg_match(Config::$NAME_REGEX, $file_name, $params); if (!$res) { throw new Exception($file_name.' nie pasuje do wzorca "'.Config::$NAME_REGEX.'"'); } $pi = pathinfo($file_name); $fpath = $pi['dirname']; $file_name = $pi['basename']; $fpath = $cdir.$fpath.DIRECTORY_SEPARATOR.$file_name; if (file_exists($fpath) && is_readable($fpath)) { if (preg_match('/\.pdf$/i', $fpath)) { $eof = FALSE; $timeout = 15; while (--$timeout && !($fp = @fopen($fpath, 'rb'))) { sleep(1); } if ($fp) { $timeout = 20; while (--$timeout) { if (fseek($fp, -10, SEEK_END) > -1) { $contents = fread($fp, 10); if (strpos($contents, '%%EOF') !== FALSE) { $eof = TRUE; break; } } sleep(1); } fclose($fp); } if (!$eof) { throw new Exception('%%EOF not found in '.$fpath); } } } else { throw new Exception('can\'t open '.$fpath.' for reading'); } //kazdy tekst musi być w UTF-8 foreach($params as &$value) { if (!is_numeric($value)) { $value = iconv('Windows-1250', 'UTF-8', $value); } } $data = array(); $params['dctpid'] = 2; if (isset($params['dctptp']) AND !empty($params['dctptp'])) { $data['dctptp'] = $params['dctptp']; } if (isset($params['dctpid']) AND !empty($params['dctpid'])) { $data['dctpid'] = 2; } if (!empty($data)) { $dtyp = $client->getDocumentTypeData($data); $params['dctpid'] = NULL; if (isset($dtyp[0]) && is_array($dtyp[0])) $dtyp = $dtyp[0]; if (isset($dtyp['dctpid'])) $params['dctpid'] = $dtyp['dctpid']; } if (!isset($params['dctpid'])) { throw new Exception(''); } $dscrpt = ''; if ($dtyp['dctptp'] != 'Other') $dscrpt = $dtyp['dctpnm']; if (isset($params['type'])) $dscrpt .= (empty($dscrpt) ? '' : ' ').$params['type']; if (isset($params['name'])) $dscrpt .= ' od '.$params['name']; $target = (isset($params['target']) and $params['target']) ? $params['target'] : (isset(Config::$TARGET_ORUNID) ? Config::$TARGET_ORUNID : 1); $data = array( 'dscrpt' => 'Dokument ('.iconv('Windows-1250', 'UTF-8', $file_name).')', 'fixinf' => str_replace('.pdf', '', iconv('Windows-1250', 'UTF-8', $file_name)), 'target' => $target, 'dctpid' => isset($params['dctpid']) ? $params['dctpid'] : 2, ); if ($params['client'] !== '0_BEZ_KLIENTA') { $data['to_contact_symbol'] = $params['client']; } // data wplywu if (isset($params['adddat'])) { $data['adddat'] = preg_replace('/(\d{4})(\d{2})(\d{2})/', '$1-$2-$3', $params['adddat']); } // data wystawienia if (isset($params['crtdat'])) { $data['crtdat'] = preg_replace('/(\d{4})(\d{2})(\d{2})/', '$1-$2-$3', $params['crtdat']); } //file_put_contents('c:/log.txt', implode(";", $data)); //exit(0); try { $doc_id = $client->createDocument($data); } catch(SoapFault $fault) { throw new Exception("[{$fault->faultcode}]: {$fault->faultstring}"); } if (isset($doc_id) AND $doc_id) { $dd = 0; try { $file = base64_encode(file_get_contents($fpath)); $file_name = iconv('Windows-1250', 'UTF-8', $file_name); $dd = $client->addAttachmentToDocument($file, $file_name, $doc_id); } catch(SoapFault $fault) { throw new Exception("[{$fault->faultcode}]: {$fault->faultstring}"); } if ($dd > 0) { // po udanych operacjach usuń plik rename($fpath, 'C:\\ED-SKANY\\WYSLANE\\'.$file_name); } else { throw new Exception('Attachment not created'); } } else { throw new Exception('Document not created (unknown error)'); } } catch(Exception $e) { // oznacz błędny plik //rename($fpath, $fpath.'_'); trigger_error($e->getMessage(), E_USER_ERROR); } exit(0); ?>