44 public function __construct($user, $password, $loginURL, $uploadURL, $downloadURL)
47 $this->password = $password;
48 $this->loginURL = $loginURL;
49 $this->downloadURL = $downloadURL;
50 $this->uploadURL = $uploadURL;
52 $this->curlEnabled = function_exists(
'curl_version');
65 $context = stream_context_create(
67 'http' => array(
'header' =>
'Accept: application/xml')
70 $url = $this->loginURL.
'?u='.$this->user.
'&pw='.$this->password;
71 $xmlString = file_get_contents($url,
false, $context);
73 if ($xmlString ===
false)
75 throw new \Exception(
"No puede alcanzarse la URL de login: ".$this->loginURL);
79 $xml = simplexml_load_string($xmlString);
83 $mensaje =
"Error en el parseo del XML. ".$e->getMessage();
84 throw new \Exception($mensaje);
88 if (isset($xml->response))
90 $errorCode = (integer) $xml->response->status->code;
91 $descError = (
string) $xml->response->status->name .
'-' . (string) $xml->response->status->description;
92 throw new \
Exception(
"Error del login: ".$descError, $errorCode);
95 $ticket = (string) $xml[0];
115 $context = stream_context_create(
117 'http' => array(
'header' =>
'Accept: application/xml')
120 $url = $this->downloadURL.
'?alf_ticket='.$ticket.
'&referenciaGDE='.$refGDE;
121 $doc = file_get_contents($url,
false, $context);
124 $mensaje =
"No existe el documento con referencia GDE: $refGDE";
125 throw new \Exception($mensaje);
141 public function insertDocument($idApp, $rutaFicheroEnServer, $rutaDestinoAlf =
'/', $nombreDoc =
'', $descDoc =
'')
143 $vFileParts = pathinfo($rutaFicheroEnServer);
145 $nombreDoc = empty($nombreDoc)?$vFileParts[
'basename']:$nombreDoc;
146 $descDoc = empty($descDoc)?$vFileParts[
'basename']:$descDoc;
150 if ($this->curlEnabled)
152 $respuesta = $this->insertDocumentCURL($idApp, $rutaFicheroEnServer, $rutaDestinoAlf, $nombreDoc, $descDoc);
156 $respuesta = $this->insertDocumentHTTPRequest($idApp, $rutaFicheroEnServer, $rutaDestinoAlf, $nombreDoc, $descDoc);
167 $respuesta =
' '.$respuesta;
168 $ini = strpos($respuesta,
'Referencia GDE: ');
171 $mensaje =
"No se obtuvo referencia GDE";
172 throw new \Exception($mensaje);
174 $ini += strlen(
'Referencia GDE: ');
175 $len = stripos($respuesta,
'</td></tr>', $ini) - $ini;
176 $refGDE = substr($respuesta, $ini, $len);
183 public function getLoginUrl() {
184 return $this->loginUrl;
187 public function setLoginUrl($loginUrl) {
188 $this->loginUrl = $loginUrl;
191 public function getUser() {
195 public function setUser($user) {
199 public function getPassword() {
203 public function setPassword($password) {
204 $this->password = $password;
207 public function getUploadUrl() {
208 return $this->uploadUrl;
211 public function setUploadUrl($uploadUrl) {
212 $this->uploadUrl = $uploadUrl;
215 public function getDownloadUrl() {
216 return $this->downloadUrl;
219 public function setDownloadUrl($downloadUrl) {
220 $this->downloadUrl = downloadUrl;
223 public function getIdApp() {
227 public function setIdApp($idApp) {
228 $this->idApp = $idApp;
244 private function insertDocumentCURL($idApp, $rutaFicheroEnServer, $rutaDestinoAlf, $nombreDoc, $descDoc)
249 if (function_exists(
'curl_file_create'))
251 $cFile = curl_file_create($rutaFicheroEnServer);
256 $cFile =
'@' . realpath($rutaFicheroEnServer).
';filename='.$nombreDoc;
259 $url = $this->uploadURL.
'?alf_ticket='.$ticket;
260 $postFields = array (
262 'alf_ticket' => $ticket,
264 'title' => $nombreDoc,
266 'rutaDestino' => $rutaDestinoAlf
270 curl_setopt($curl, CURLOPT_URL,$url);
271 curl_setopt($curl, CURLOPT_RETURNTRANSFER,
true);
272 curl_setopt($curl, CURLOPT_POST,
true);
273 curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields);
274 $response = curl_exec($curl);
275 $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
285 $mensaje =
"Error: la llamada a URL $url ha fallado con estado $status, respuesta $response, Error CURL: " . curl_error($curl);
286 throw new \Exception($mensaje, $status);
311 private function insertDocumentHTTPRequest($idApp, $rutaFicheroEnServer, $rutaDestinoAlf, $nombreDoc, $descDoc)
314 $url = $this->uploadURL.
'?alf_ticket='.$ticket;
317 $postFields = array (
319 'title' => $nombreDoc,
321 'rutaDestino' => $rutaDestinoAlf,
324 $vURL = parse_url($url);
325 if (! (($vURL[
'scheme'] ==
'http') || ($vURL[
'scheme'] ==
'https')) )
327 $mensaje =
"Solo se soporta POST hacia HTTP/HTTPS.";
328 throw new \Exception ($mensaje);
330 $port = (isset($vURL[
'port']) ? $vURL[
'port'] : 80);
332 $hostURL = $vURL[
'host'];
333 $pathURL = $vURL[
'path'];
334 $queryURL = $vURL[
'query'];
336 $errno = null; $errstr =
'';
337 $fp = fsockopen($hostURL, $port, $errno, $errstr, 60);
340 $mensaje =
"Error al acceder a $url. $errstr ($errno)";
341 throw new \Exception($mensaje);
344 $boundary =
'--'.uniqid();
347 $httpRequest =
"POST $pathURL?$queryURL HTTP/1.1" . $crlf;
348 $httpRequest.=
"Host: $hostURL" . $crlf;
349 $httpRequest.=
"User-Agent: " . $_SERVER[
'HTTP_USER_AGENT'] . $crlf;
350 $httpRequest.= empty($ticket)?
"(Authorization: Basic ".base64_encode($this->user.
':'.$this->password) . $crlf:
'';
351 $httpRequest.=
"Content-type: multipart/form-data; boundary=$boundary" . $crlf;
357 foreach($postFields as $key => $value)
359 $valueLength = strlen($value);
360 $body .=
'--'.$boundary.$crlf
361 .
'Content-Disposition: form-data; name="'.$key.
'"'.$crlf
362 .
'Content-Length: '.$valueLength.$crlf
367 $mimetype = self::getMimeType(basename($rutaFicheroEnServer));
368 $file_contents = file_get_contents($rutaFicheroEnServer);
369 if($file_contents ===
false)
371 $mensaje =
"Error al leer el fichero $rutaFicheroEnServer";
372 throw new \Exception($mensaje);
374 $body .=
'--'.$boundary.$crlf
375 .
'Content-Disposition: form-data; name="file"; filename="'.$nombreDoc.
'"'.$crlf
376 .
'Content-Type: '.$mimetype.$crlf
377 .
'Content-Transfer-Encoding : base64'.$crlf
378 .
'Content-Length: '.strlen($file_contents).$crlf
379 .$crlf.$file_contents.$crlf;
380 $body .=
'--'.$boundary.
'--';
381 unset($file_contents); $file_contents = null; gc_collect_cycles();
382 $httpRequest.=
"Content-length: ". strlen($body) . $crlf;
383 $httpRequest.=
"Connection: close\r\n\r\n";
384 $httpRequest .= $body . $crlf.$crlf;
387 $write = fwrite($fp, $httpRequest);
390 $mensaje =
"Error al enviar la REQUEST";
391 throw new \Exception($mensaje);
395 unset($httpRequest); $httpRequest = null; gc_collect_cycles();
401 $response .= fgets($fp);
406 $result = explode($crlf.$crlf, $response, 2);
407 $content = isset($result[1]) ? $result[1] :
'';
421 $fileExtension = trim(strtolower(array_pop(explode(
'.',$fileName))));
426 'txt' =>
'text/plain',
427 'htm' =>
'text/html',
428 'html' =>
'text/html',
429 'php' =>
'text/html',
431 'js' =>
'application/javascript',
432 'json' =>
'application/json',
433 'xml' =>
'application/xml',
437 'png' =>
'image/png',
438 'jpe' =>
'image/jpeg',
439 'jpeg' =>
'image/jpeg',
440 'jpg' =>
'image/jpeg',
441 'gif' =>
'image/gif',
442 'bmp' =>
'image/bmp',
443 'ico' =>
'image/vnd.microsoft.icon',
444 'tiff' =>
'image/tiff',
445 'tif' =>
'image/tiff',
446 'svg' =>
'image/svg+xml',
447 'svgz' =>
'image/svg+xml',
450 'zip' =>
'application/zip',
451 'rar' =>
'application/x-rar-compressed',
452 'exe' =>
'application/x-msdownload',
453 'msi' =>
'application/x-msdownload',
454 'cab' =>
'application/vnd.ms-cab-compressed',
455 'tgz' =>
'application/tar+gzip',
456 'tar.gz' =>
'application/tar+gzip',
457 'tar' =>
'application/tar',
458 'gz' =>
'application/gzip',
459 '7z' =>
'application/x-7z-compressed',
460 's7z' =>
'application/x-7z-compressed',
463 'mp3' =>
'audio/mpeg',
464 'qt' =>
'video/quicktime',
465 'mov' =>
'video/quicktime',
466 'mpeg' =>
'video/mpeg',
467 'avi' =>
'video/x-msvideo',
468 'swf' =>
'application/x-shockwave-flash',
469 'flv' =>
'video/x-flv',
472 'pdf' =>
'application/pdf',
473 'psd' =>
'image/vnd.adobe.photoshop',
474 'ai' =>
'application/postscript',
475 'eps' =>
'application/postscript',
476 'ps' =>
'application/postscript',
479 'doc' =>
'application/msword',
480 'dot' =>
'application/msword',
481 'docx' =>
'application/msword',
482 'rtf' =>
'application/rtf',
484 'xls' =>
'application/vnd.ms-excel',
485 'xlsx' =>
'application/vnd.ms-excel',
486 'xlm' =>
'application/vnd.ms-excel',
487 'xla' =>
'application/vnd.ms-excel',
488 'xlc' =>
'application/vnd.ms-excel',
489 'xlt' =>
'application/vnd.ms-excel',
490 'xlw' =>
'application/vnd.ms-excel',
492 'ppt' =>
'application/vnd.ms-powerpoint',
493 'pptx' =>
'application/vnd.ms-powerpoint',
494 'pps' =>
'application/vnd.ms-powerpoint',
495 'pot' =>
'application/vnd.ms-powerpoint',
498 'odc' =>
'application/vnd.oasis.opendocument.chart',
499 'otc' =>
'application/vnd.oasis.opendocument.chart-template',
500 'odf' =>
'application/vnd.oasis.opendocument.formula',
501 'otf' =>
'application/vnd.oasis.opendocument.formula-template',
502 'odg' =>
'application/vnd.oasis.opendocument.graphics',
503 'otg' =>
'application/vnd.oasis.opendocument.graphics-template',
504 'odi' =>
'application/vnd.oasis.opendocument.image',
505 'oti' =>
'application/vnd.oasis.opendocument.image-template',
506 'odp' =>
'application/vnd.oasis.opendocument.presentation',
507 'otp' =>
'application/vnd.oasis.opendocument.presentation-template',
508 'ods' =>
'application/vnd.oasis.opendocument.spreadsheet',
509 'ots' =>
'application/vnd.oasis.opendocument.spreadsheet-template',
510 'odt' =>
'application/vnd.oasis.opendocument.text',
511 'otm' =>
'application/vnd.oasis.opendocument.text-master',
512 'ott' =>
'application/vnd.oasis.opendocument.text-template',
513 'oth' =>
'application/vnd.oasis.opendocument.text-web',
516 'vcf' =>
'text/vcard',
517 'ics' =>
'text/calendar',
521 if (array_key_exists($fileExtension, $mime_types))
523 $tipoMime = $mime_types[$fileExtension];
527 $tipoMime =
'application/octet-stream';
insertDocument($idApp, $rutaFicheroEnServer, $rutaDestinoAlf= '/', $nombreDoc= '', $descDoc= '')
static getMimeType($fileName)
__construct($user, $password, $loginURL, $uploadURL, $downloadURL)