41 public function __construct($user, $password, $loginURL, $uploadURL, $downloadURL)
44 $this->password = $password;
45 $this->loginURL = $loginURL;
46 $this->downloadURL = $downloadURL;
47 $this->uploadURL = $uploadURL;
49 $this->curlEnabled = function_exists(
'curl_version');
61 $url = $this->loginURL.
'?u='.$this->user.
'&pw='.$this->password;
72 if ($xmlString ===
false)
74 $errorMsg = error_get_last();
75 throw new \Exception(__CLASS__.
'::'.__METHOD__.
": No puede alcanzarse la URL de login: ".$url.
" Error: ".print_r($errorMsg,
true));
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];
111 $url = $this->downloadURL.
'?alf_ticket='.$ticket.
'&referenciaGDE='.$refGDE;
121 $mensaje =
"No existe el documento con referencia GDE: $refGDE";
122 throw new \Exception($mensaje);
138 public function insertDocument($idApp, $rutaFicheroEnServer, $rutaDestinoAlf =
'/', $nombreDoc =
'', $descDoc =
'')
140 $vFileParts = pathinfo($rutaFicheroEnServer);
142 $nombreDoc = empty($nombreDoc)?$vFileParts[
'basename']:$nombreDoc;
143 $descDoc = empty($descDoc)?$vFileParts[
'basename']:$descDoc;
147 if ($this->curlEnabled)
149 $respuesta = $this->insertDocumentCURL($idApp, $rutaFicheroEnServer, $rutaDestinoAlf, $nombreDoc, $descDoc);
153 $respuesta = $this->insertDocumentHTTPRequest($idApp, $rutaFicheroEnServer, $rutaDestinoAlf, $nombreDoc, $descDoc);
164 $respuesta =
' '.$respuesta;
165 $ini = strpos($respuesta,
'Referencia GDE: ');
168 $mensaje =
"No se obtuvo referencia GDE";
169 throw new \Exception($mensaje);
171 $ini += strlen(
'Referencia GDE: ');
172 $len = stripos($respuesta,
'</td></tr>', $ini) - $ini;
173 $refGDE = substr($respuesta, $ini, $len);
180 public function getLoginUrl() {
181 return $this->loginUrl;
184 public function setLoginUrl($loginUrl) {
185 $this->loginUrl = $loginUrl;
188 public function getUser() {
192 public function setUser($user) {
196 public function getPassword() {
200 public function setPassword($password) {
201 $this->password = $password;
204 public function getUploadUrl() {
205 return $this->uploadUrl;
208 public function setUploadUrl($uploadUrl) {
209 $this->uploadUrl = $uploadUrl;
212 public function getDownloadUrl() {
213 return $this->downloadUrl;
216 public function setDownloadUrl($downloadUrl) {
217 $this->downloadUrl = downloadUrl;
220 public function getIdApp() {
224 public function setIdApp($idApp) {
225 $this->idApp = $idApp;
241 private function insertDocumentCURL($idApp, $rutaFicheroEnServer, $rutaDestinoAlf, $nombreDoc, $descDoc)
246 if (function_exists(
'curl_file_create'))
248 $cFile = curl_file_create($rutaFicheroEnServer);
253 $cFile =
'@' . realpath($rutaFicheroEnServer).
';filename='.$nombreDoc;
256 $url = $this->uploadURL.
'?alf_ticket='.$ticket;
257 $postFields = array (
259 'alf_ticket' => $ticket,
261 'title' => $nombreDoc,
263 'rutaDestino' => $rutaDestinoAlf
267 curl_setopt($curl, CURLOPT_URL,$url);
268 curl_setopt($curl, CURLOPT_RETURNTRANSFER,
true);
269 curl_setopt($curl, CURLOPT_POST,
true);
270 curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields);
271 $response = curl_exec($curl);
272 $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
282 $mensaje =
"Error: la llamada a URL $url ha fallado con estado $status, respuesta $response, Error CURL: " . curl_error($curl);
283 throw new \Exception($mensaje, $status);
308 private function insertDocumentHTTPRequest($idApp, $rutaFicheroEnServer, $rutaDestinoAlf, $nombreDoc, $descDoc)
311 $url = $this->uploadURL.
'?alf_ticket='.$ticket;
314 $postFields = array (
316 'title' => $nombreDoc,
318 'rutaDestino' => $rutaDestinoAlf,
321 $vURL = parse_url($url);
322 if (! (($vURL[
'scheme'] ==
'http') || ($vURL[
'scheme'] ==
'https')) )
324 $mensaje =
"Solo se soporta POST hacia HTTP/HTTPS.";
325 throw new \Exception ($mensaje);
327 $port = (isset($vURL[
'port']) ? $vURL[
'port'] : 80);
329 $hostURL = $vURL[
'host'];
330 $pathURL = $vURL[
'path'];
331 $queryURL = $vURL[
'query'];
333 $errno = null; $errstr =
'';
334 $fp = fsockopen($hostURL, $port, $errno, $errstr, 60);
337 $mensaje =
"Error al acceder a $url. $errstr ($errno)";
338 throw new \Exception($mensaje);
341 $boundary =
'--'.uniqid();
344 $httpRequest =
"POST $pathURL?$queryURL HTTP/1.1" . $crlf;
345 $httpRequest.=
"Host: $hostURL" . $crlf;
346 $httpRequest.=
"User-Agent: " . $_SERVER[
'HTTP_USER_AGENT'] . $crlf;
347 $httpRequest.= empty($ticket)?
"(Authorization: Basic ".base64_encode($this->user.
':'.$this->password) . $crlf:
'';
348 $httpRequest.=
"Content-type: multipart/form-data; boundary=$boundary" . $crlf;
354 foreach($postFields as $key => $value)
356 $valueLength = strlen($value);
357 $body .=
'--'.$boundary.$crlf
358 .
'Content-Disposition: form-data; name="'.$key.
'"'.$crlf
359 .
'Content-Length: '.$valueLength.$crlf
364 $mimetype = self::getMimeType(basename($rutaFicheroEnServer));
365 $file_contents = file_get_contents($rutaFicheroEnServer);
366 if($file_contents ===
false)
368 $mensaje =
"Error al leer el fichero $rutaFicheroEnServer";
369 throw new \Exception($mensaje);
371 $body .=
'--'.$boundary.$crlf
372 .
'Content-Disposition: form-data; name="file"; filename="'.$nombreDoc.
'"'.$crlf
373 .
'Content-Type: '.$mimetype.$crlf
374 .
'Content-Transfer-Encoding : base64'.$crlf
375 .
'Content-Length: '.strlen($file_contents).$crlf
376 .$crlf.$file_contents.$crlf;
377 $body .=
'--'.$boundary.
'--';
378 unset($file_contents); $file_contents = null; gc_collect_cycles();
379 $httpRequest.=
"Content-length: ". strlen($body) . $crlf;
380 $httpRequest.=
"Connection: close\r\n\r\n";
381 $httpRequest .= $body . $crlf.$crlf;
384 $write = fwrite($fp, $httpRequest);
387 $mensaje =
"Error al enviar la REQUEST";
388 throw new \Exception($mensaje);
392 unset($httpRequest); $httpRequest = null; gc_collect_cycles();
398 $response .= fgets($fp);
403 $result = explode($crlf.$crlf, $response, 2);
404 $content = isset($result[1]) ? $result[1] :
'';
418 $fileExtension = trim(strtolower(array_pop(explode(
'.',$fileName))));
423 'txt' =>
'text/plain',
424 'htm' =>
'text/html',
425 'html' =>
'text/html',
426 'php' =>
'text/html',
428 'js' =>
'application/javascript',
429 'json' =>
'application/json',
430 'xml' =>
'application/xml',
434 'png' =>
'image/png',
435 'jpe' =>
'image/jpeg',
436 'jpeg' =>
'image/jpeg',
437 'jpg' =>
'image/jpeg',
438 'gif' =>
'image/gif',
439 'bmp' =>
'image/bmp',
440 'ico' =>
'image/vnd.microsoft.icon',
441 'tiff' =>
'image/tiff',
442 'tif' =>
'image/tiff',
443 'svg' =>
'image/svg+xml',
444 'svgz' =>
'image/svg+xml',
447 'zip' =>
'application/zip',
448 'rar' =>
'application/x-rar-compressed',
449 'exe' =>
'application/x-msdownload',
450 'msi' =>
'application/x-msdownload',
451 'cab' =>
'application/vnd.ms-cab-compressed',
452 'tgz' =>
'application/tar+gzip',
453 'tar.gz' =>
'application/tar+gzip',
454 'tar' =>
'application/tar',
455 'gz' =>
'application/gzip',
456 '7z' =>
'application/x-7z-compressed',
457 's7z' =>
'application/x-7z-compressed',
460 'mp3' =>
'audio/mpeg',
461 'qt' =>
'video/quicktime',
462 'mov' =>
'video/quicktime',
463 'mpeg' =>
'video/mpeg',
464 'avi' =>
'video/x-msvideo',
465 'swf' =>
'application/x-shockwave-flash',
466 'flv' =>
'video/x-flv',
469 'pdf' =>
'application/pdf',
470 'psd' =>
'image/vnd.adobe.photoshop',
471 'ai' =>
'application/postscript',
472 'eps' =>
'application/postscript',
473 'ps' =>
'application/postscript',
476 'doc' =>
'application/msword',
477 'dot' =>
'application/msword',
478 'docx' =>
'application/msword',
479 'rtf' =>
'application/rtf',
481 'xls' =>
'application/vnd.ms-excel',
482 'xlsx' =>
'application/vnd.ms-excel',
483 'xlm' =>
'application/vnd.ms-excel',
484 'xla' =>
'application/vnd.ms-excel',
485 'xlc' =>
'application/vnd.ms-excel',
486 'xlt' =>
'application/vnd.ms-excel',
487 'xlw' =>
'application/vnd.ms-excel',
489 'ppt' =>
'application/vnd.ms-powerpoint',
490 'pptx' =>
'application/vnd.ms-powerpoint',
491 'pps' =>
'application/vnd.ms-powerpoint',
492 'pot' =>
'application/vnd.ms-powerpoint',
495 'odc' =>
'application/vnd.oasis.opendocument.chart',
496 'otc' =>
'application/vnd.oasis.opendocument.chart-template',
497 'odf' =>
'application/vnd.oasis.opendocument.formula',
498 'otf' =>
'application/vnd.oasis.opendocument.formula-template',
499 'odg' =>
'application/vnd.oasis.opendocument.graphics',
500 'otg' =>
'application/vnd.oasis.opendocument.graphics-template',
501 'odi' =>
'application/vnd.oasis.opendocument.image',
502 'oti' =>
'application/vnd.oasis.opendocument.image-template',
503 'odp' =>
'application/vnd.oasis.opendocument.presentation',
504 'otp' =>
'application/vnd.oasis.opendocument.presentation-template',
505 'ods' =>
'application/vnd.oasis.opendocument.spreadsheet',
506 'ots' =>
'application/vnd.oasis.opendocument.spreadsheet-template',
507 'odt' =>
'application/vnd.oasis.opendocument.text',
508 'otm' =>
'application/vnd.oasis.opendocument.text-master',
509 'ott' =>
'application/vnd.oasis.opendocument.text-template',
510 'oth' =>
'application/vnd.oasis.opendocument.text-web',
513 'vcf' =>
'text/vcard',
514 'ics' =>
'text/calendar',
518 if (array_key_exists($fileExtension, $mime_types))
520 $tipoMime = $mime_types[$fileExtension];
524 $tipoMime =
'application/octet-stream';
540 if ($this->curlEnabled ===
true)
543 curl_setopt($ch, CURLOPT_URL, $url);
544 curl_setopt($ch, CURLOPT_RETURNTRANSFER,
true);
545 curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
546 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
547 curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
548 curl_setopt($ch, CURLOPT_TIMEOUT, 10);
549 curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
550 $output = curl_exec($ch);
552 if($output ===
false)
554 $errorMsg =
'ERROR CURL: Sin acceso a '.$url.
' : ' . curl_error($ch);
555 throw new \Exception(__CLASS__.
'::'.__METHOD__.
": $errorMsg");
561 $output = file_get_contents($url,
false, $options);
562 if ($output ===
false)
564 $errorMsg =
'ERROR file_get_content(). Sin acceso a '.$url.
' : ' .print_r($errorMsg,
true);
565 throw new \Exception(__CLASS__.
'::'.__METHOD__.
': '.$errorMsg);
insertDocument($idApp, $rutaFicheroEnServer, $rutaDestinoAlf='/', $nombreDoc='', $descDoc='')
static getMimeType($fileName)
urlGetContents($url, $options=null)
__construct($user, $password, $loginURL, $uploadURL, $downloadURL)