1<?php
2
3/**
4 * Group-Office
5 *
6 * Copyright Intermesh BV.
7 * This file is part of Group-Office. You should have received a copy of the
8 * Group-Office license along with Group-Office. See the file /LICENSE.TXT
9 *
10 * If you have questions write an e-mail to info@intermesh.nl
11 *
12 * @license AGPL/Proprietary http://www.group-office.com/LICENSE.TXT
13 * @link http://www.group-office.com
14 * @package GO.modules.zpushadmin.model
15 * @version $Id: Device.php 21647 2016-09-22 13:34:46Z mschering $
16 * @copyright Copyright Intermesh BV.
17 * @author <<FIRST_NAME>> <<LAST_NAME>> <<EMAIL>>@intermesh.nl
18 */
19
20/**
21 * The Device model
22 *
23 * @package GO.modules.zpushadmin.model
24 * @property string $device_id
25 * @property string $device_type
26 * @property string $remote_addr
27 * @property boolean $can_connect
28 * @property int $ctime
29 * @property int $mtime
30 * @property boolean $new
31 * @property string $username
32 * @property string $comment
33 * @property string $as_version
34 */
35
36
37namespace GO\Zpushadmin\Model;
38
39
40class Device extends \GO\Base\Db\ActiveRecord {
41
42	public $devicePhoneNumber;
43	public $deviceModel;
44	public $deviceImei;
45	public $deviceName;
46	public $deviceOS;
47	public $deviceOSLanguage;
48	public $deviceOperator;
49	public $deviceOutboundSMS;
50	public $deviceASVersion;
51	public $deviceWiperequestOn;
52	public $deviceWiperequestBy;
53	public $deviceWiped;
54	public $deviceErrors="";
55
56	public function primaryKey() {
57		return 'id';
58	}
59
60	/**
61	 * Returns a static model of itself
62	 *
63	 * @param String $className
64	 * @return \GO\Workflow\Model\Process
65	 */
66	public static function model($className=__CLASS__)
67	{
68		return parent::model($className);
69	}
70
71	protected function afterLoad() {
72		$isLowerCaseNeeded = \GO\Zpushadmin\ZpushadminModule::checkZPushVersion('2.3');
73		if($isLowerCaseNeeded)
74			$this->device_id=strtolower($this->device_id);
75
76		return parent::afterLoad();
77	}
78
79	/**
80	 * Returns the table name
81	 */
82	 public function tableName() {
83		 return 'zpa_devices';
84	 }
85
86	public function resync(){
87		\GO\Zpushadmin\ZpushadminModule::includeZpushFiles();
88		return \ZPushAdmin::ResyncDevice($this->username,$this->device_id);
89	}
90
91	public function remove(){
92		\GO\Zpushadmin\ZpushadminModule::includeZpushFiles();
93		return \ZPushAdmin::RemoveDevice($this->username,$this->device_id);
94	}
95
96	public function wipe(){
97		\GO::debug('Zpushadmin::Device->wipe() called');
98		\GO\Zpushadmin\ZpushadminModule::includeZpushFiles();
99		\ZPushAdmin::WipeDevice(\GO::user()->username,$this->username,$this->device_id);
100		return \ZPushAdmin::RemoveDevice($this->username,$this->device_id);
101	}
102
103	public function loadDetails(){
104		\GO\Zpushadmin\ZpushadminModule::includeZpushFiles();
105		\GO::debug('Zpushadmin::Device->loadDetails() called');
106		\GO::debug('Zpushadmin::DeviceID = '.$this->device_id);
107		\GO::debug('Zpushadmin::Username = '.$this->username);
108
109		$data = \ZPushAdmin::GetDeviceDetails($this->device_id,$this->username);
110		if($data){
111			$this->devicePhoneNumber = $data->GetDevicePhoneNumber();
112			$this->deviceModel = $data->GetDeviceModel();
113			$this->deviceImei = $data->GetDeviceIMEI();
114			$this->deviceName = $data->GetDeviceFriendlyName();
115			$this->deviceOS = $data->GetDeviceOS();
116			$this->deviceOSLanguage = $data->GetDeviceOSLanguage();
117			$this->deviceOperator = $data->GetDeviceMobileOperator();
118			$this->deviceOutboundSMS = $data->GetDeviceEnableOutboundSMS();
119			$this->deviceASVersion = $data->GetASVersion();
120			$this->deviceWiperequestOn = $data->GetWipeRequestedOn();
121			$this->deviceWiperequestBy = $data->GetWipeRequestedBy();
122			$this->deviceWiped = $data->GetWipeActionOn();
123			$this->deviceErrors = $data->GetDeviceError();
124
125
126			if ($data->GetDeviceError())
127					$this->deviceErrors = $data->GetDeviceError()."\n";
128			else if (!isset($data->ignoredmessages) || empty($data->ignoredmessages)) {
129					$this->deviceErrors = "No errors known\n";
130			}
131			else {
132					$this->deviceErrors .= sprintf("%d messages need attention because they could not be synchronized\n", count($data->ignoredmessages));
133					foreach ($data->ignoredmessages as $im) {
134							$info = "";
135							if (isset($im->asobject->subject))
136									$info .= sprintf("Subject: '%s'", $im->asobject->subject);
137							if (isset($im->asobject->fileas))
138									$info .= sprintf("FileAs: '%s'", $im->asobject->fileas);
139							if (isset($im->asobject->from))
140									$info .= sprintf(" - From: '%s'", $im->asobject->from);
141							if (isset($im->asobject->starttime))
142									$info .= sprintf(" - On: '%s'", strftime("%Y-%m-%d %H:%M", $im->asobject->starttime));
143							$reason = $im->reasonstring;
144							if ($im->reasoncode == 2)
145									$reason = "Message was causing loop";
146							$this->deviceErrors .= sprintf("\tBroken object:\t'%s' ignored on '%s'\n", $im->asclass,  strftime("%Y-%m-%d %H:%M", $im->timestamp));
147							$this->deviceErrors .= sprintf("\tInformation:\t%s\n", $info);
148							$this->deviceErrors .= sprintf("\tReason: \t%s (%s)\n", $reason, $im->reasoncode);
149							$this->deviceErrors .= sprintf("\tItem/Parent id: %s/%s\n", $im->id, $im->folderid);
150							$this->deviceErrors .= "\n";
151					}
152			}
153
154
155
156
157			// Save the active sync version that is used to the database
158			if($this->deviceASVersion != $this->as_version){
159				$this->as_version = $this->deviceASVersion;
160				$this->save();
161			}
162
163		}
164		return true;
165	}
166
167	protected function afterDelete() {
168		\GO\Zpushadmin\ZpushadminModule::includeZpushFiles();
169		return \ZPushAdmin::RemoveDevice($this->username,$this->device_id);
170	}
171}
172