GetCommonDir(); return LegacyHelper::getCommonParentFolder() .$commondir.'/cgi-bin/'; } function EncodeBase64(&$data) { vxcbase64::encode($data); } function DecodeBase64(&$data) { vxcbase64::decode($data); } function GetCommonDir() { return '/vxcommon'; } function VLog($error) { $filename=$_SERVER['DOCUMENT_ROOT']."/voxelservice.log"; $fic=@fopen($filename,"at"); if ($fic) { @chmod($filename,0666); $date=date("Y-m-d H:i:s"); @fputs($fic,"(".$date.") ".$error."\n"); @fclose($fic); } } function GetFile($filename,&$response, $commOptions=COMM_NONE) { $ftmp=$filename.'.TG'; $fworking=$filename; $compressed=@file_exists($filename.'.vwz'); if($compressed) $fworking=$filename.'.vwz'; if (!@file_exists($fworking)) { $response=null; return false; } if($compressed && !($commOptions & COMM_ZIP)) { $this->Unzip($fworking,$ftmp); $fworking=$ftmp; } if(!$compressed && ($commOptions & COMM_ZIP)) { $this->Zip($fworking,$ftmp); $fworking=$ftmp; } if($commOptions & COMM_CRYPT) { $this->CryptDES($fworking,$ftmp,"serverPw"); $fworking=$ftmp; } $response = @file_get_contents($fworking); @unlink($ftmp); if($response===FALSE) { $response=null; return false; } $this->EncodeBase64($response); return true; } function DeleteMVCFile($filename) { return $this->_deleteMVCFile($filename); } // Funcion que borra un fichero logico del servicio MVC del middleware function _deleteMVCFile($filename) { $mvcMiddlewareURL = LegacyHelper::getMiddlewareServiceURL(); if (!$mvcMiddlewareURL) return false; $statusCode = 0; $headers = array(); $url = $mvcMiddlewareURL ."/deleteFile?path=".urlencode($filename); $storageService = LegacyHelper::getStorageService(); if ($storageService) $url .= "&storageService=".urlencode($storageService); LegacyHelper::httpRequest('GET',$url,null,null,$statusCode,$headers); return ($statusCode==200); } // Funcion que obtiene un fichero logico del servicio MVC del middleware function _getMVCFile($filename,&$data) { $mvcMiddlewareURL = LegacyHelper::getMiddlewareServiceURL(); if (!$mvcMiddlewareURL) return false; $statusCode = 0; $headers = array(); $url = $mvcMiddlewareURL ."/getFile?path=".urlencode($filename); $storageService = LegacyHelper::getStorageService(); if ($storageService) $url .= "&storageService=".urlencode($storageService); $data = LegacyHelper::httpRequest('GET',$url,null,null,$statusCode,$headers); return ($statusCode==200); } function CheckMVCFile($filename) { return $this->_checkMVCFile($filename); } // Funcion que comprueba si un fichero logico existe en el servicio MVC del middleware function _checkMVCFile($filename) { $mvcMiddlewareURL = LegacyHelper::getMiddlewareServiceURL(); if (!$mvcMiddlewareURL) return false; $json = new Services_JSON(); $statusCode = 0; $headers = array(); $url = $mvcMiddlewareURL ."/existsFile?path=".urlencode($filename); $storageService = LegacyHelper::getStorageService(); if ($storageService) $url .= "&storageService=".urlencode($storageService); $responseStr = LegacyHelper::httpRequest('GET',$url,null,null,$statusCode,$headers); if ($statusCode == 200) { $response = $json->decode($responseStr); if ($response && $response->result== "ok" && $response->data && ($response->data->exists===true || $response->data->exists=="true" || $response->data->exists===TRUE)) return true; } return false; } function _GetFileFromBuffer(&$rawData,&$response,$commOptions=COMM_NONE) { $tmpFilePath = tempnam(sys_get_temp_dir(), 'tmpMVCFile'); $ftmp=$tmpFilePath.'.TG'; // Escribimos el contenido en un fichero temporal if (@file_put_contents($tmpFilePath,$rawData)===FALSE) { $response=null; return false; } $fworking = $tmpFilePath; // Lo encriptamos / zipeamos si procede $compressed = FALSE; if($compressed && !($commOptions & COMM_ZIP)) { $this->Unzip($fworking,$ftmp); $fworking=$ftmp; } if(!$compressed && ($commOptions & COMM_ZIP)) { $this->Zip($fworking,$ftmp); $fworking=$ftmp; } if($commOptions & COMM_CRYPT) { $this->CryptDES($fworking,$ftmp,"serverPw"); $fworking=$ftmp; } // Obtenemos el resultado como buffer $response = @file_get_contents($fworking); @unlink($ftmp); if (file_exists($fworking)) @unlink($fworking); if (file_exists($tmpFilePath)) @unlink($tmpFilePath); if($response===FALSE) { $response=null; return false; } // Devolvemos el resultado codificado en base64 $this->EncodeBase64($response); return true; } function _getS3Directory($bucket,$prefix,$delimiter=null) { $this->_defineS3Keys(); $s3 = new S3(awsAccessKey, awsSecretKey); return $s3->getBucket($bucket, $prefix, null, null,$delimiter); } function _getS3File($bucketURL, $path, &$response) { $statusCode = 0; $headers = array(); $url = $bucketURL. "/".$path; $response = LegacyHelper::httpRequest('GET',$url,null,null,$statusCode,$headers); return ($statusCode==200); } function _defineS3Keys() { //AWS access info can be read and redefine from an external file or other method //define('awsAccessKey', ''); //define('awsSecretKey', ''); } function _setS3File($path, $content,$mimeType) { //instantiate the class $this->_defineS3Keys(); $s3 = new S3(awsAccessKey, awsSecretKey); $requestHeaders = array( 'Content-Type'=>$mimeType , 'Cache-Control'=>'max-age=0' ); $s3->putObject($content, 'vxcommon', $path , S3::ACL_PUBLIC_READ , array(), $requestHeaders); } function file_crc($file) { $array = unpack('V', pack('H*', @hash_file('crc32b', $file))); $crc32 = $array[1]; return hexdec(sprintf("%08x", $crc32)); } /** * Obtiene una lista de archivos y directorios del repositorio VxCommon dado un path * @param string $path Path relativo logico del que se quiere obtener la informacion (sin barra al principio ni al final) * @param array $contents Devuelve un array asociativo con un array de 'files' y otro array de 'dirs'. * @param boolean $calculateCRC Indica si devuelve el CRC (solo tenemos el hash de S3) * @return boolean Devuelve true si todo ha ido bien o false en caso contrario */ function GetVxCommonDirectoryList($path,&$contents,$calculateCRC=false) { $contents = array('files'=> array(), 'dirs' => array()); // Obtenemos la lista de ficheros y ficheros de subdirectorios (ineficiente pero se deberian de pedir solo) // subcarpetas $results = $this->_getS3Directory('vxcommon',$path."/"); if (!$results) return false; // precalculamos la longitud del prefijo para no estar llamando a la funcion todo el rato $lenPrefix = strlen($path)+1; // Recorremos los resultados (ficheros y carpetas) foreach ($results as $result) { // Miramos si hay barra despues del prefijo $dirIndex = strpos($result['name'],'/',$lenPrefix); // Si no hay mas barras es que es un fichero del prefijo if ($dirIndex===FALSE) { $name = substr($result['name'],$lenPrefix); // Obtenemos el nombre sin el prefijo y lo devolvemos if ($name) $contents['files'][] = array('name'=>$name,'size'=>$result['size']); } // Si hay una barra, y dicha barra coincide con el final del nombre, es que es una carpeta de primer nivel else if ($dirIndex == (strlen($result['name'])-1)) { $name = substr($result['name'],$lenPrefix,strlen($result['name'])-$lenPrefix-1); // Obtenemos el nombre sin el prefijo y sin la barra final if ($name) $contents['dirs'][] = array('name'=>$name); } } // file_put_contents('c:\\hola.txt','Path: '.$path." " .print_r($contents,true)); return true; } function GetDirectoryList($path,&$contents,$calculateCRC=false) { $contents = array('files'=> array(), 'dirs' => array()); if ($handle = @opendir($path)) { while (false !== ($file = @readdir($handle))) if (@is_file($path.'/'.$file)) { $fileArray = array('name'=>$file); if ($calculateCRC) $fileArray['crc'] = $this->file_crc($path.'/'.$file); $fileArray['size'] = @filesize($path.'/'.$file); $contents['files'][] = $fileArray; } @closedir($handle); } else return false; if ($handle = @opendir($path)) { while (false !== ($file = @readdir($handle))) if (@is_dir($path.'/'.$file)) { $contents['dirs'][] = array('name'=>$file); } @closedir($handle); } else return false; return true; } // Obtenemos en el buffer $response el fichero con ruta 'filename' del servidor de ficheros de VxCommon function GetVxCommonFile($filename,&$response, $commOptions=COMM_NONE) { $rawData = null; if (!$this->_getS3File('vxcommon.voxelcare.com',$filename,$rawData)) { $response =null; return false; } return $this->_GetFileFromBuffer($rawData,$response,$commOptions); } // Guardamos el contenido de 'contents' en la ruta 'filename' en el servidor de ficheros de VxCommon function SetVxCommonFile($filename,$contents, $commOptions=COMM_NONE) { $dataDesc = FALSE; if (!$this->_SetFileToBuffer($contents,$commOptions,$dataDesc)) return false; $this->_setS3File($filename, $dataDesc, 'binary/octet-stream'); } // Obtiene el contenido de un fichero logico del middleware MVC function GetMVCFile($filename,&$response, $commOptions=COMM_NONE) { $fworking=$filename; $compressed = FALSE; /* // No usamos esto, lo dejo comentado por si acaso en un futuro se quiere usar $compressed=$this->_checkMVCFile($filename.'.vwz'); if($compressed) { $fworking=$filename.'.vwz'; } */ $rawData=null; // Obtenemso el archivo del servidor middleware if (!$this->_getMVCFile($fworking,$rawData)) { $response=null; return false; } return $this->_GetFileFromBuffer($rawData,$response,$commOptions); } function SetTFTPFile($filename) { if (@file_exists($_SERVER['DOCUMENT_ROOT'].'/php/tftp.php')!==FALSE) { @(include_once $_SERVER['DOCUMENT_ROOT'].'/php/tftp.php'); tftp_put($filename); } } function GetTFTPFile($filename) { if (@file_exists($_SERVER['DOCUMENT_ROOT'].'/php/tftp.php')!==FALSE) { @(include_once $_SERVER['DOCUMENT_ROOT'].'/php/tftp.php'); @mkdir(dirname($filename),0777,true); tftp_get($filename); } } function SetFTPFile($filename) { if (@file_exists($_SERVER['DOCUMENT_ROOT'].'/php/ftp.php')!==FALSE) { @(include_once $_SERVER['DOCUMENT_ROOT'].'/php/ftp.php'); ftpput($filename); } } function GetFTPFile($filename) { if (@file_exists($_SERVER['DOCUMENT_ROOT'].'/php/ftp.php')!==FALSE) { @(include_once $_SERVER['DOCUMENT_ROOT'].'/php/ftp.php'); @mkdir(dirname($filename),0777,true); ftpget($filename); } } function SetFile($filename,$contents, $commOptions=COMM_NONE) { // Cojemos la informacion de conexion de @mkdir(dirname($filename),0777,true); $this->DecodeBase64($contents); if(@file_exists($filename)) @unlink($filename); if(@file_exists($filename.'.vwz')) @unlink($filename.'.vwz'); $res = @file_put_contents($filename, $contents); if($commOptions & COMM_DECRYPT) { $this->DecryptDES($filename, $filename, 'clientPw'); } if($commOptions & COMM_UNZIP) { if( !($commOptions & COMM_ZIP) ) { $this->Unzip($filename, $filename); } else @rename($filename, $filename.'.vwz'); } if($commOptions & COMM_ZIP) { if(!($commOptions & COMM_UNZIP)) $this->Zip($filename, $filename.'.vwz'); else @rename($filename, $filename.'.vwz'); $filename=$filename.'.vwz'; } @chmod($filename,0666); return true; } function _SetFileToBuffer(&$contents,$commOptions, &$dataDesc) { // Decodificamos el contenido a binario $this->DecodeBase64($contents); // Creamos un fichero temporal y guardamos el contenido // ya los scripts de encriptacion y zipeo funcionan con archivos $tmpPath = tempnam(sys_get_temp_dir(), 'tmpFile'); // LegacyHelper::VLog(" Temp path : ". $tmpPath); if(@file_exists($tmpPath)) @unlink($tmpPath); if(@file_exists($tmpPath.'.vwz')) @unlink($tmpPath.'.vwz'); if (@file_put_contents($tmpPath, $contents)===FALSE) return false; //LegacyHelper::VLog("Hemos poddido guardar"); if($commOptions & COMM_DECRYPT) $this->DecryptDES($tmpPath, $tmpPath, 'clientPw'); if($commOptions & COMM_UNZIP) { if( !($commOptions & COMM_ZIP) ) $this->Unzip($tmpPath, $tmpPath); else @rename($tmpPath, $tmpPath.'.vwz'); } if($commOptions & COMM_ZIP) { if(!($commOptions & COMM_UNZIP)) $this->Zip($tmpPath, $tmpPath.'.vwz'); else @rename($tmpPath, $tmpPath.'.vwz'); $tmpPath=$tmpPath.'.vwz'; } @chmod($tmpPath,0666); // Leemos el contenido para ser enviado al middleware $dataDesc = @file_get_contents($tmpPath); return ($dataDesc!==FALSE); } // Guarda un fichero en el middleware MVC function SetMVCFile($filename,$contents, $commOptions=COMM_NONE) { $dataDesc = FALSE; if (!$this->_SetFileToBuffer($contents,$commOptions,$dataDesc)) return false; // Cojemos la informacion de conexion del middleware $mvcMiddlewareURL = LegacyHelper::getMiddlewareServiceURL(); // LegacyHelper::VLog(" SET MVC FILE MIddleware URL : ". $mvcMiddlewareURL); if (!$mvcMiddlewareURL) // Si no hay informacion de conexion devuelvo false return false; $url = $mvcMiddlewareURL .'/putFile?path='.urlencode($filename); $storageService = LegacyHelper::getStorageService(); if ($storageService) $url .= "&storageService=".urlencode($storageService); // LegacyHelper::VLog("URL put file $url"); $statusCode = 0; $headers = array(); // Hacemos la llamada al servidor $responseStr = LegacyHelper::httpRequest('PUT', $url, $dataDesc , null,$statusCode,$headers); // LegacyHelper::VLog("Response PutFile $responseStr"); return ($statusCode==200); } function GetDirectory($path,&$response) { $response= ""; if ($handle = @opendir($path)) { while (false !== ($file = @readdir($handle))) if (@is_file($path.'/'.$file)) $response = $response . $file . "\n"; @closedir($handle); } else return false; $response = $response . "\r\n"; if ($handle = @opendir($path)) { while (false !== ($file = @readdir($handle))) if (@is_dir($path.'/'.$file)) $response = $response . $file . "\n"; @closedir($handle); } else return false; return true; } function GetTextFile($filename) { if (!@file_exists($filename)) return false; $fic = @fopen($filename,"rb"); if (!$fic) return false; $response=@fread($fic,@filesize($filename)); @fclose($fic); return $response; } function Zip($filenameIn, $filenameOut) { $fin=$filenameIn; if($filenameIn==$filenameOut) { $fin=$filenameIn.'.TZ'; @rename($filenameIn, $fin); } $command = $this->GetCgiBinPath().'cmdZip 0 '.$fin.' '.$filenameOut; @exec($command); if($fin!=$filenameIn) @unlink($fin); } function Unzip($filenameIn, $filenameOut) { $fin=$filenameIn; if($filenameIn==$filenameOut) { $fin=$filenameIn.'.TZ'; @rename($filenameIn, $fin); } $command = $this->GetCgiBinPath().'cmdZip 1 '.$fin.' '.$filenameOut; @exec($command); if($fin!=$filenameIn) @unlink($fin); } function CryptDES($filenameIn, $filenameOut, $passwd) { $fin=$filenameIn; if($filenameIn==$filenameOut) { $fin=$filenameIn.'.TY'; @rename($filenameIn, $fin); } $command = $this->GetCgiBinPath().'cmdCrypt '.$passwd.' 0 '.$fin.' '.$filenameOut; @exec($command); if($fin!=$filenameIn) @unlink($fin); } function DecryptDES($filenameIn, $filenameOut, $passwd) { $fin=$filenameIn; if($filenameIn==$filenameOut) { $fin=$filenameIn.'.TY'; @rename($filenameIn, $fin); } $command = $this->GetCgiBinPath().'cmdCrypt '.$passwd.' 1 '.$fin.' '.$filenameOut; @exec($command); if($fin!=$filenameIn)@unlink($fin); } function DeleteFile($path) { if (!@copy($path,$path . '.bak')) return false; if (!@unlink($path)) return false; return true; } } ?>