Page MenuHomePhorge

kolab_sync_backend_device.php
No OneTemporary

Size
5 KB
Referenced Files
None
Subscribers
None

kolab_sync_backend_device.php

<?php
/**
*
*/
class kolab_sync_backend_device implements Syncroton_Backend_IDevice
{
/**
* Kolab Sync backend
*
* @var kolab_sync_backend
*/
protected $backend;
/**
* the database adapter
*
* @var rcube_mdb2
*/
protected $db;
public function __construct()
{
$this->backend = kolab_sync_backend::get_instance();
$this->db = rcube::get_instance()->get_dbh();
}
/**
* create new device
*
* @param Syncroton_Model_IDevice $_device
* @return Syncroton_Model_IDevice
*/
public function create(Syncroton_Model_IDevice $device)
{
$id = sha1(mt_rand() . microtime());
// Create device entry in kolab backend
$created = $this->backend->device_create(array(
'ID' => $id,
'TYPE' => $device->devicetype,
), $device->deviceid);
if (!$created) {
throw new Syncroton_Exception_NotFound('device creation failed');
}
$insert[$this->db->quote_identifier('id')] = $this->db->quote($id);
$insert[$this->db->quote_identifier('deviceid')] = $this->db->quote($device->deviceid);
$insert[$this->db->quote_identifier('devicetype')] = $this->db->quote($device->devicetype);
$insert[$this->db->quote_identifier('owner_id')] = $this->db->quote($device->owner_id);
$insert[$this->db->quote_identifier('policy_id')] = isset($device->policy_id) ? $this->db->quote($device->policy_id) : 1;
$insert[$this->db->quote_identifier('policykey')] = isset($device->policykey) ? $this->db->quote($device->policykey) : 'NULL';
$insert[$this->db->quote_identifier('useragent')] = isset($device->useragent) ? $this->db->quote($device->useragent) : "''";
$insert[$this->db->quote_identifier('acsversion')] = isset($device->acsversion) ? $this->db->quote($device->acsversion) : "''";
$insert[$this->db->quote_identifier('remotewipe')] = isset($device->remotewipe) ? $this->db->quote($device->remotewipe) : 'NULL';
// Add entry to database
$this->db->query('INSERT INTO syncroton_device'
. ' (' . implode(', ', array_keys($insert)) . ')' . ' VALUES(' . implode(', ', $insert) . ')');
return $this->get($id);
}
/**
* @param string $_id
* @throws Syncroton_Exception_NotFound
* @return Syncroton_Model_IDevice
*/
public function get($id)
{
$select = $this->db->query('SELECT * FROM syncroton_device WHERE id = ?', array($id));
if ($device = $this->db->fetch_assoc($select)) {
$device = new Syncroton_Model_Device($device);
}
if (! $device instanceof Syncroton_Model_IDevice) {
throw new Syncroton_Exception_NotFound('id not found');
}
return $device;
}
/**
* return device for this user
*
* @param string $ownerId
* @param string $deviceId
* @throws Syncroton_Exception_NotFound
* @return Syncroton_Model_Device
*/
public function getUserDevice($ownerId, $deviceId)
{
$where[] = $this->db->quote_identifier('deviceid') . ' = ' . $this->db->quote($deviceId);
$where[] = $this->db->quote_identifier('owner_id') . ' = ' . $this->db->quote($ownerId);
$select = $this->db->query('SELECT * FROM syncroton_device WHERE ' . implode(' AND ', $where));
if ($device = $this->db->fetch_assoc($select)) {
$device = new Syncroton_Model_Device($device);
}
if (! $device instanceof Syncroton_Model_IDevice) {
throw new Syncroton_Exception_NotFound('id not found');
}
// Make sure device exists (could be deleted by the user)
$dev = $this->backend->device_get($deviceId);
if (empty($dev)) {
// Remove the device (and related cached data) from database
$this->delete($device);
throw new Syncroton_Exception_NotFound('device not found');
}
return $device;
}
public function delete($device)
{
$result = $this->backend->device_delete($device->deviceid);
if ($result) {
$result = $this->db->query('DELETE FROM syncroton_device'
. ' WHERE ' . $this->db->quote_identifier('id') . ' = ' . $this->db->quote($device->id));
$result = (bool) $this->db->affected_rows($result);
}
return $result;
}
public function update(Syncroton_Model_IDevice $device)
{
$set[] = $this->db->quote_identifier('policykey') . ' = ' . (isset($device->policykey) ? $this->db->quote($device->policykey) : 'NULL');
$set[] = $this->db->quote_identifier('useragent') . ' = ' . (isset($device->useragent) ? $this->db->quote($device->useragent) : "''");
$set[] = $this->db->quote_identifier('acsversion') . ' = ' . (isset($device->acsversion) ? $this->db->quote($device->acsversion) : "''");
$set[] = $this->db->quote_identifier('remotewipe') . ' = ' . (isset($device->remotewipe) ? $this->db->quote($device->remotewipe) : 'NULL');
$set[] = $this->db->quote_identifier('pinglifetime') . ' = ' . (isset($device->pinglifetime) ? $this->db->quote($device->pinglifetime) : 'NULL');
$set[] = $this->db->quote_identifier('pingfolder') . ' = ' . (isset($device->pingfolder) ? $this->db->quote($device->pingfolder) : 'NULL');
$this->db->query('UPDATE syncroton_device SET ' . implode(', ', $set)
. ' WHERE ' . $this->db->quote_identifier('id') . ' = ' . $this->db->quote($device->id));
return $this->get($device->id);
}
}

File Metadata

Mime Type
text/x-php
Expires
Thu, Jul 9, 12:42 AM (20 h, 29 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1052366
Default Alt Text
kolab_sync_backend_device.php (5 KB)

Event Timeline