Skip to content

Commit

Permalink
Merge branch '1.1' into 1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
pschuele committed Sep 7, 2016
2 parents 9d0797c + 325ec3e commit 55c80c3
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 46 deletions.
56 changes: 28 additions & 28 deletions lib/Syncroton/Backend/ABackend.php
Expand Up @@ -26,8 +26,8 @@ abstract class Syncroton_Backend_ABackend implements Syncroton_Backend_IBackend

protected $_tablePrefix;

protected $_tableName;

protected $_tableName;

protected $_modelClassName;

protected $_modelInterfaceName;
Expand All @@ -47,19 +47,19 @@ public function __construct(Zend_Db_Adapter_Abstract $_db, $_tablePrefix = 'Sync
/**
* create new device
*
* @param Syncroton_Model_IDevice $_device
* @return Syncroton_Model_IDevice
* @param Syncroton_Model_AEntry $model
* @return Syncroton_Model_AEntry
*/
public function create($model)
{
if (! $model instanceof $this->_modelInterfaceName) {
throw new InvalidArgumentException('$model must be instanace of ' . $this->_modelInterfaceName);
throw new InvalidArgumentException('$model must be instance of ' . $this->_modelInterfaceName);
}

$data = $this->_convertModelToArray($model);

$data['id'] = sha1(mt_rand(). microtime());

$this->_db->insert($this->_tablePrefix . $this->_tableName, $data);

return $this->get($data['id']);
Expand All @@ -68,24 +68,24 @@ public function create($model)
/**
* convert iteratable object to array
*
* @param unknown $model
* @param Syncroton_Model_AEntry $model
* @return array
*/
protected function _convertModelToArray($model)
{
$data = array();

foreach ($model as $key => $value) {
if ($value instanceof DateTime) {
$value = $value->format('Y-m-d H:i:s');
} elseif (is_object($value) && isset($value->id)) {
$value = $value->id;
}

$data[$this->_fromCamelCase($key)] = $value;
foreach ($model as $key => $value) {
if ($value instanceof DateTime) {
$value = $value->format('Y-m-d H:i:s');
} elseif (is_object($value) && isset($value->id)) {
$value = $value->id;
}

$data[$this->_fromCamelCase($key)] = $value;
}

return $data;
return $data;
}

/**
Expand Down Expand Up @@ -124,17 +124,17 @@ public function get($id)
*/
protected function _getObject($data)
{
foreach ($data as $key => $value) {
foreach ($data as $key => $value) {
unset($data[$key]);

if (!empty($value) && preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $value)) { # 2012-08-12 07:43:26
$value = new DateTime($value, new DateTimeZone('utc'));

if (!empty($value) && preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $value)) { # 2012-08-12 07:43:26
$value = new DateTime($value, new DateTimeZone('utc'));
}

$data[$this->_toCamelCase($key, false)] = $value;
}

return new $this->_modelClassName($data);

$data[$this->_toCamelCase($key, false)] = $value;
}

return new $this->_modelClassName($data);
}

/**
Expand All @@ -156,9 +156,9 @@ public function delete($id)
*/
public function update($model)
{
if (! $model instanceof $this->_modelInterfaceName) {
throw new InvalidArgumentException('$model must be instanace of ' . $this->_modelInterfaceName);
}
if (! $model instanceof $this->_modelInterfaceName) {
throw new InvalidArgumentException('$model must be instanace of ' . $this->_modelInterfaceName);
}

$data = $this->_convertModelToArray($model);

Expand Down
2 changes: 1 addition & 1 deletion lib/Syncroton/Command/Ping.php
Expand Up @@ -209,7 +209,7 @@ public function handle()
// break if there are less than PingTimeout + 10 seconds left for the next loop
// otherwise the response will be returned after the client has finished his Ping
// request already maybe
} while ($secondsLeft > (Syncroton_Registry::getPingTimeout() + 10));
} while (Syncroton_Server::validateSession() && $secondsLeft > (Syncroton_Registry::getPingTimeout() + 10));
}

if ($this->_logger instanceof Zend_Log)
Expand Down
2 changes: 1 addition & 1 deletion lib/Syncroton/Command/Sync.php
Expand Up @@ -521,7 +521,7 @@ public function getResponse()
// break if there are less than PingTimeout + 10 seconds left for the next loop
// otherwise the response will be returned after the client has finished his Ping
// request already maybe
} while (time() - $intervalStart < $this->_heartbeatInterval - (Syncroton_Registry::getPingTimeout() + 10));
} while (Syncroton_Server::validateSession() && time() - $intervalStart < $this->_heartbeatInterval - (Syncroton_Registry::getPingTimeout() + 10));
}

foreach($this->_collections as $collectionData) {
Expand Down
23 changes: 22 additions & 1 deletion lib/Syncroton/Registry.php
Expand Up @@ -37,6 +37,7 @@ class Syncroton_Registry extends ArrayObject
const PING_TIMEOUT = 'ping_timeout';
const PING_INTERVAL = 'ping_interval';
const QUIET_TIME = 'quiet_time';
const SESSION_VALIDATOR = 'session_validator';

const DATABASE = 'database';
const TRANSACTIONMANAGER = 'transactionmanager';
Expand All @@ -46,6 +47,7 @@ class Syncroton_Registry extends ArrayObject
const FOLDERBACKEND = 'folderbackend';
const POLICYBACKEND = 'policybackend';
const SYNCSTATEBACKEND = 'syncstatebackend';
const LOGGERBACKEND = 'loggerBackend';

/**
* Class name of the singleton registry object.
Expand Down Expand Up @@ -304,7 +306,26 @@ public static function getQuietTime()

return self::get(self::QUIET_TIME);
}


/**
* return session validation function
*
* This function is used in long running requests like ping & sync to
* validate user session. Returns false if session is not valid any longer
*
* @return callable
*/
public static function getSessionValidator()
{
if (!self::isRegistered(self::SESSION_VALIDATOR)) {
self::set(self::SESSION_VALIDATOR, function() {
return true;
});
}

return self::get(self::SESSION_VALIDATOR);
}

/**
* returns syncstate backend
*
Expand Down
36 changes: 21 additions & 15 deletions lib/Syncroton/Server.php
Expand Up @@ -106,8 +106,8 @@ protected function _handlePost()
$className = 'Syncroton_Command_' . $requestParameters['command'];

if(!class_exists($className)) {
if ($this->_logger instanceof Zend_Log)
$this->_logger->crit(__METHOD__ . '::' . __LINE__ . " command not supported: " . $requestParameters['command']);
if ($this->_logger instanceof Zend_Log)
$this->_logger->crit(__METHOD__ . '::' . __LINE__ . " command not supported: " . $requestParameters['command']);

header("HTTP/1.1 501 not implemented");

Expand Down Expand Up @@ -189,7 +189,7 @@ protected function _handlePost()
} catch (Syncroton_Wbxml_Exception $swe) {
if ($this->_logger instanceof Zend_Log) {
$this->_logger->err(__METHOD__ . '::' . __LINE__ . " Could not encode output: " . $swe);
$this->_logDomDocument(Zend_Log::ERR, $response, __METHOD__, __LINE__);
$this->_logDomDocument(Zend_Log::WARN, $response, __METHOD__, __LINE__);
}

header("HTTP/1.1 500 Internal server error");
Expand Down Expand Up @@ -329,14 +329,14 @@ protected function _getRequestParameters(Zend_Controller_Request_Http $request)
if ($length > 0) {
$unpacked = unpack('Vstring', fread($stream, $length));
$policyKey = $unpacked['string'];
}
}

// unpack device type
// unpack device type
$length = ord(fread($stream, 1));
if ($length > 0) {
if ($length > 0) {
$unpacked = unpack('A' . $length . 'string', fread($stream, $length));
$deviceType = $unpacked['string'];
}
}

while (! feof($stream)) {
$tag = ord(fread($stream, 1));
Expand Down Expand Up @@ -387,23 +387,23 @@ protected function _getRequestParameters(Zend_Controller_Request_Http $request)
'attachmentName' => isset($attachmentName) ? $attachmentName : null,
'acceptMultipart' => isset($acceptMultiPart) ? $acceptMultiPart : false
);
} else {
} else {
$result = array(
'protocolVersion' => $request->getServer('HTTP_MS_ASPROTOCOLVERSION'),
'command' => $request->getQuery('Cmd'),
'deviceId' => $request->getQuery('DeviceId'),
'deviceType' => $request->getQuery('DeviceType'),
'policyKey' => $request->getServer('HTTP_X_MS_POLICYKEY'),
'saveInSent' => $request->getQuery('SaveInSent') == 'T',
'collectionId' => $request->getQuery('CollectionId'),
'saveInSent' => $request->getQuery('SaveInSent') == 'T',
'collectionId' => $request->getQuery('CollectionId'),
'itemId' => $request->getQuery('ItemId'),
'attachmentName' => $request->getQuery('AttachmentName'),
'acceptMultipart' => $request->getServer('HTTP_MS_ASACCEPTMULTIPART') == 'T'
'acceptMultipart' => $request->getServer('HTTP_MS_ASACCEPTMULTIPART') == 'T'
);
}

$result['userAgent'] = $request->getServer('HTTP_USER_AGENT', $result['deviceType']);
$result['contentType'] = $request->getServer('CONTENT_TYPE');

$result['userAgent'] = $request->getServer('HTTP_USER_AGENT', $result['deviceType']);
$result['contentType'] = $request->getServer('CONTENT_TYPE');

return $result;
}
Expand Down Expand Up @@ -437,10 +437,16 @@ protected function _getUserDevice($ownerId, $requestParameters)
'devicetype' => $requestParameters['deviceType'],
'useragent' => $requestParameters['userAgent'],
'acsversion' => $requestParameters['protocolVersion'],
'policyId' => Syncroton_Registry::isRegistered(Syncroton_Registry::DEFAULT_POLICY) ? Syncroton_Registry::get(Syncroton_Registry::DEFAULT_POLICY) : null
'policyId' => Syncroton_Registry::isRegistered(Syncroton_Registry::DEFAULT_POLICY) ? Syncroton_Registry::get(Syncroton_Registry::DEFAULT_POLICY) : null
)));
}

return $device;
}

public static function validateSession()
{
$validatorFunction = Syncroton_Registry::getSessionValidator();
return $validatorFunction();
}
}

0 comments on commit 55c80c3

Please sign in to comment.