1+ <?php
2+ /**
3+ * LICENSE
4+ *
5+ * Copyright © 2016-2018 Teclib'
6+ * Copyright © 2010-2018 by the FusionInventory Development Team.
7+ *
8+ * This file is part of Flyve MDM Plugin for GLPI.
9+ *
10+ * Flyve MDM Plugin for GLPI is a subproject of Flyve MDM. Flyve MDM is a mobile
11+ * device management software.
12+ *
13+ * Flyve MDM Plugin for GLPI is free software: you can redistribute it and/or
14+ * modify it under the terms of the GNU Affero General Public License as published
15+ * by the Free Software Foundation, either version 3 of the License, or
16+ * (at your option) any later version.
17+ * Flyve MDM Plugin for GLPI is distributed in the hope that it will be useful,
18+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+ * GNU Affero General Public License for more details.
21+ * You should have received a copy of the GNU Affero General Public License
22+ * along with Flyve MDM Plugin for GLPI. If not, see http://www.gnu.org/licenses/.
23+ * ------------------------------------------------------------------------------
24+ * @author Thierry Bugier
25+ * @copyright Copyright © 2018 Teclib
26+ * @license AGPLv3+ http://www.gnu.org/licenses/agpl.txt
27+ * @link https://github.com/flyve-mdm/glpi-plugin
28+ * @link https://flyve-mdm.com/
29+ * ------------------------------------------------------------------------------
30+ */
31+
32+ if (!defined ('GLPI_ROOT ' )) {
33+ die ("Sorry. You can't access this file directly " );
34+ }
35+
36+
37+ class PluginFlyvemdmPolicyM2m extends PluginFlyvemdmPolicyBase implements PluginFlyvemdmPolicyInterface {
38+
39+ /** @var string $server name or IP of the server to use for M2M */
40+
41+ /** @var integer $port port to use for M2M */
42+
43+ /** @var integer $tls use TLS for M2M (1 for true, 0 for false) */
44+
45+ /**
46+ * PluginFlyvemdmPolicyInteger constructor.
47+ * @param PluginFlyvemdmPolicy $policy
48+ */
49+ public function __construct (PluginFlyvemdmPolicy $ policy ) {
50+ parent ::__construct ($ policy );
51+ $ defaultProperties = [
52+ 'server ' => '' ,
53+ 'port ' => '0 ' ,
54+ 'tls ' => '0 ' ,
55+ ];
56+ $ propertyCollection = $ this ->jsonDecodeProperties ($ policy ->getField ('type_data ' ),
57+ $ defaultProperties );
58+ $ this ->server = $ propertyCollection ['server ' ];
59+ $ this ->port = $ propertyCollection ['port ' ];
60+ $ this ->tls = $ propertyCollection ['tls ' ];
61+
62+ $ this ->symbol = $ policy ->getField ('symbol ' );
63+ $ this ->unicityRequired = ($ policy ->getField ('unicity ' ) != '0 ' );
64+ $ this ->group = $ policy ->getField ('group ' );
65+ }
66+
67+ /**
68+ * @param mixed $value
69+ * @param mixed $itemtype
70+ * @param integer $itemId
71+ *
72+ * @return bool
73+ */
74+ public function integrityCheck ($ value , $ itemtype , $ itemId ) {
75+ $ keys = [
76+ 'server ' ,
77+ 'port ' ,
78+ 'tls '
79+ ];
80+ $ settingSufficient = false ;
81+ foreach ($ keys as $ key ) {
82+ if (!isset ($ value [$ key ])) {
83+ Session::addMessageAfterRedirect (__ ('All parameters must be specified ' , 'flyvemdm ' ));
84+ return false ;
85+ }
86+ }
87+
88+ if (empty ($ value ['server ' ]) && empty ($ value ['port ' ] && $ value ['tls ' ] != 0 && $ value ['tls ' ] != 1 )) {
89+ Session::addMessageAfterRedirect (__ ('At least one parameter must be set ' , 'flyvemdm ' ));
90+ return false ;
91+ }
92+
93+ return true ;
94+ }
95+
96+ /**
97+ * @param mixed $value
98+ * @param mixed $itemtype
99+ * @param integer $itemId
100+ *
101+ * @return array|boolean
102+ */
103+ public function getMqttMessage ($ value , $ itemtype , $ itemId ) {
104+ $ decodedValue = json_decode ($ value , JSON_OBJECT_AS_ARRAY );
105+ if (!$ this ->integrityCheck ($ decodedValue , $ itemtype , $ itemId )) {
106+ return false ;
107+ }
108+
109+ $ array = [
110+ $ this ->symbol => $ value ['server ' ],
111+ 'port ' => $ value ['port ' ],
112+ 'tls ' => $ value ['tls ' ],
113+ ];
114+
115+ return $ array ;
116+ }
117+
118+ public function showValueInput ($ value = '' , $ itemType = '' , $ itemId = 0 ) {
119+ if ($ value === '' ) {
120+ $ value = json_decode ($ this ->policyData ->getField ('recommended_value ' ), JSON_OBJECT_AS_ARRAY );
121+ }
122+ $ out = __ ('M2M server ' , 'flyvemdm ' ) . ' ' . Html::input ('value[server] ' , ['value ' => $ value ['server ' ]]);
123+ $ out .= '<br> ' . __ ('Port ' , 'flyvemdm ' ) . ' ' . Html::input ('value[port] ' , ['value ' => $ value ['port ' ]]);
124+ $ out .= '<br> ' . __ ('Use TLS ' , 'flyvemdm ' ) . ' ' . Dropdown::showYesNo ('value[tls] ' , $ value ['tls ' ], -1 , ['display ' => false ]);
125+
126+ return $ out ;
127+ }
128+ }
0 commit comments