Files
fdn2/private/lib/backup/cbsClient/rapi/messages/RAPI_Message.php
T

256 lines
6.3 KiB
PHP

<?php
/*
Author: Riccardo Di Dato
Creation Date: 24/giu/2015
*/
/**
* Message example:
* {
* version: ${protocol_version},
* time: ${timestamp_microseconds},
* digest: ${digest},
* details: ${request}
* }
* @author Riccardo Di Dato
*
*/
abstract class RAPI_Message {
private $encryptedCache = null;
/*
* Attributi, setters e getters
*/
/**
* Versione del protocollo RAPI
* @var string
*/
private $protocolVersion;
public function getProtocolVersion(){
return $this->protocolVersion;
}
/**
* Timestamp in microsecondi
* @var int
*/
private $time;
public function getTime(){
return $this->time;
}
/**
* Digest per valutare l'autenticità
* @var string
*/
private $digest;
public function getDigest(){
return $this->digest;
}
/**
* Imposta il digest per il messaggio
* @param string $digest il digest da impostare
*/
public function setDigest($digest){
$this->digest = $digest;
}
/**
* Una classe contenente i dettagli per generare l'operazione
* @var RAPI_MessageDetails
*/
private $details;
/**
* @param string $encoded
* @return string|RAPI_MessageDetails
*/
public function getDetails($encoded = false){
if ($encoded){
return json_encode($this->details);
}
else {
return $this->details;
}
}
/**
* Metodo di utility che richiama il metodo omonimo del message details.
* @return stdClass
*/
public function getAttachmentData($name){
return $this->getDetails()->getAttachmentData($name);
}
/**
* Metodo di utility che richiama il metodo omonimo del message details per l'istanza utilizzata all'interno
* @return stdClass
*/
public function getAttachmentList(){
return $this->getDetails()->getAttachmentList();
}
/**
* Metodo di utility che richiama il metodo omonimo del message details per l'istanza utilizzata all'interno
*/
public function setAttachmentLocalFileReference($name, $path){
$this->getDetails()->setAttachmentLocalFileReference($name,$path);
}
/**
* Metodo di utility che richiama il metodo omonimo del message details per l'istanza utilizzata all'interno
*/
public function getAttachmentLocalFileReference($name){
return $this->details->getAttachmentLocalFileReference($name);
}
/*
* Costruttore
*/
/**
* @param string $protocolVersion La versione del protocollo RAPI
* @param RAPI_MessageDetails $details Il contenuto del messaggio
* @param int $time Timestamp in microsecondi
* @param string $digest Digest per valutare l'autenticità del messaggio
*/
public function __construct($protocolVersion, RAPI_MessageDetails $details, $time = null, $digest = null){
$this->protocolVersion = $protocolVersion;
$this->time = is_null($time)?array_sum( explode( ' ' , microtime() ) ):$time;
$this->details = $details;
$this->digest = $digest;
}
/*
* Metodi per la SERIALIZZAZIONE
*/
/**
* Genera un oggetto standard da utilizzare per
* la serializzazione.
* Questa versione genera solo gli attributi
* relativi a RAPI_Message
* @return stdClass
*/
protected function generateEncryptionBaseObject(){
$rval = new stdClass();
$rval->version = $this->protocolVersion;
$rval->time = $this->time;
$rval->digest = $this->digest;
$rval->details = $this->details->generateEncryptionObject();
return $rval;
}
/**
* Restituisce la serializzazione dell'oggetto
* @return string
*/
public function getEncryption(){
if (is_null($this->encryptedCache)){
$this->testEncryption();
}
return $this->encryptedCache;
}
public function testEncryption(){
$this->encryptedCache = null;
$enc = json_encode($this->generateEncryptionObject());
if ($enc === false){
throw new RAPI_Exception("JSON Encoding failed. Message '".json_last_error_msg()."'");
}
$this->encryptedCache = $enc;
}
/*
* Metodi di DE-SERIALIZZAZIONE
*/
/**
* Deserializza la stringa e ritorna un oggetto RAPI_Message
* valido.
* @param string $string
* @param array $additionalData Parametro opzionale per aggiungere attributi (già decodificati) all'oggetto dopo la decodifica
* @throws RAPI_Exception Se non riesce a parsare la stringa
* @return RAPI_Message
*/
public static function fromEncrypted($string, array $additionalData = array()){
$appoggio = json_decode(trim($string));
if (!is_null($appoggio)){
if (sizeof($additionalData)>0){
foreach ($additionalData as $key => $val){
$appoggio->$key = $val;
}
}
}
else {
$errMsg = "Invalid request format ";
switch (json_last_error()) {
case JSON_ERROR_NONE:
$errMsg.= ' (No JSON errors)';
break;
case JSON_ERROR_DEPTH:
$errMsg.= ' (JSON - Maximum stack depth exceeded)';
break;
case JSON_ERROR_STATE_MISMATCH:
$errMsg.= ' (JSON - Underflow or the modes mismatch)';
break;
case JSON_ERROR_CTRL_CHAR:
$errMsg.= ' (JSON - Unexpected control character found)';
break;
case JSON_ERROR_SYNTAX:
$errMsg.= ' (JSON - Syntax error, malformed JSON)';
break;
case JSON_ERROR_UTF8:
$errMsg.= ' (JSON - Malformed UTF-8 characters, possibly incorrectly encoded)';
break;
default:
$errMsg.= ' (JSON - Unknown error)';
break;
}
throw new RAPI_Exception($errMsg);
}
return static::createFromObject($appoggio);
}
/**
* Ritorna true se l'oggetto ha gli
* attributi per costruire un RAPI_Message.
* @param stdClass $obj
* @return bool
*/
protected static function isValidObjectForCreationBase($obj){
return property_exists($obj, "version") &&
property_exists($obj, "time") &&
property_exists($obj, "digest") &&
property_exists($obj, "details");
}
/*
* Abstract
*/
/**
* Genera un oggetto che contiene tutti
* i valori da encodare (con JSON).
* Utilizza generateEncryptionBaseObject e gli aggiunge
* tutti gli attributi da includere nel messaggio
* @return stdClass
*/
protected abstract function generateEncryptionObject();
/**
* Ritorna true se l'oggetto ha gli
* attributi per costruire un RAPI_Message.
* Utilizzare isValidObjectForCreationBase e dopo
* valutare gli attributi della sottoclasse
* @param stdClass $obj
* @return bool
*/
protected static abstract function isValidObjectForCreation($obj);
/**
* Costruisce un message a partire da una stdClass.
* @param stdClass $obj
* @return RAPI_Message
* @throws RAPI_Exception
*/
public static abstract function createFromObject($obj);
}
?>