File: ExtensionManager.php

package info (click to toggle)
ocsinventory-server 2.8.1%2Bdfsg1-1%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,684 kB
  • sloc: php: 37,167; javascript: 28,347; perl: 8,234; sql: 2,725; sh: 1,636; xml: 1,071; python: 77; makefile: 29
file content (250 lines) | stat: -rw-r--r-- 7,369 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
/*
 * Copyright 2005-2016 OCSInventory-NG/OCSInventory-ocsreports contributors.
 * See the Contributors file for more details about them.
 *
 * This file is part of OCSInventory-NG/OCSInventory-ocsreports.
 *
 * OCSInventory-NG/OCSInventory-ocsreports is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 2 of the License,
 * or (at your option) any later version.
 *
 * OCSInventory-NG/OCSInventory-ocsreports is distributed in the hope that it
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OCSInventory-NG/OCSInventory-ocsreports. if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 */

class ExtensionManager{

    /**
     * Attributes
     */
    public $installedExtensionsList = null;
    public $installableExtensionsList = null;
    public $installableExtensions_errors = array();
    public $errorMessage = null;

    /**
     * Constants
     */

    // Extensions directories
    const MAIN_SEC_EXT = 'main_sections';
    const COMP_DETAIL_EXT = 'computer_detail';
    const LANGUAGE_EXT = 'language';
    const APACHE_EXT = 'apache';
    const AGENT_EXT = 'agent';

    // Json infos of the extension
    const INFOS_EXT = 'infos.json';

    // Extensions fobiden name
    private $FORBIDEN_EXT_NAME = [
        ".",
        "..",
        "readme.md"
    ];

    // Extensions install required method
    const EXTENSION_INSTALL_METHD = 'extension_install_';
    const EXTENSION_DELETE_METHD = 'extension_delete_';
    const EXTENSION_UPGRADE_METHD = 'extension_upgrade_';
    const EXTENSION_HOOK_METHD = 'extension_hook_';

    /**
     * List query
     */
    private $selectQuery = "SELECT * FROM `extensions`";
    private $selectQueryTarget = "SELECT * FROM `extensions` WHERE id = '%s'";

    /**
     * Insert query
     */
    private $insertQuery = "INSERT INTO `extensions`(`id`, `name`, `description`, `version`, `licence`, `author`, `contributor`) VALUES ('%s','%s','%s',%s,'%s','%s','%s')";

    /**
     * Delete query
     */
    private $deleteQuery = "DELETE FROM `extensions` WHERE id = '%s'";

    /**
     * Objects
     */
    private $dbObject = null;

    /**
     * Write server.
     */
    private $dbWrite = null;

    /**
     * Constructor
     */
    function __construct()
    {
        $this->dbObject = $_SESSION['OCS']["readServer"];
        $this->dbWrite = $_SESSION['OCS']["writeServer"];
        $this->getInstalledExtensionsList();
    }

    /**
     * Will set an array of valid extensions
     */
    public function checkInstallableExtensions(){
		// reset error list
		$this->installableExtensions_errors = array();
		// check if directory exists
		if (!is_dir(EXT_DL_DIR)) {
			$this->installableExtensions_errors[] = 'Extension directory ('.EXT_DL_DIR.') does not exist!';
		}		
        // Scan dir and get all sub directory in extensions directory
        $items = scandir(EXT_DL_DIR);
        $installableExtList = [];
        foreach ($items as $index => $name) {
            if(in_array($name, $this->FORBIDEN_EXT_NAME) || !is_dir(EXT_DL_DIR.$name)){
                continue;
            }

            if($this->isExtensionCompliant($name)){
                $installableExtList[] = $name;
            }
        }

        $this->installableExtensionsList = $installableExtList;
    }

    /**
     * Check if extensions is compliant to OCS Inventory Model
     */
    private function isExtensionCompliant($name){
		global $l;
        try{
            require EXT_DL_DIR.$name."/install.php";

			if (!function_exists(self::EXTENSION_INSTALL_METHD.$name)) {
				$this->installableExtensions_errors[] = sprintf($l->g(7021), $name).': '.sprintf($l->g(7022), self::EXTENSION_INSTALL_METHD.$name, EXT_DL_DIR.$name.'/install.php');
				return false;
			}
			
			if (!function_exists(self::EXTENSION_DELETE_METHD.$name)) {
				$this->installableExtensions_errors[] = sprintf($l->g(7021), $name).': '.sprintf($l->g(7022), self::EXTENSION_DELETE_METHD.$name, EXT_DL_DIR.$name.'/install.php');
				return false;
			}
			
			if (!function_exists(self::EXTENSION_UPGRADE_METHD.$name)) {
				$this->installableExtensions_errors[] = sprintf($l->g(7021), $name).': '.sprintf($l->g(7022), self::EXTENSION_UPGRADE_METHD.$name, EXT_DL_DIR.$name.'/install.php');
				return false;
			}

            return true;
        } catch (Exception $ex) {
			$this->installableExtensions_errors[] = sprintf($l->g(7021), $name).': '.$l->g(7023);
            return false;
        }
    }

    /**
     * Will check pre-requisites for extensions installation
     */
    public function checkPrerequisites(){
        return true;
    }

    /**
     * Extension installation
     */
    public function installExtension($name){

        if($this->isInstalled($name)){
            // TODO : error already installed
            return false;
        }

        // Add plugin record in database
        $jsonStr = file_get_contents(EXT_DL_DIR.$name."/".self::INFOS_EXT);
        $jsonInfos = json_decode($jsonStr, true);

        $queryArrayArgs = [];
        $queryArrayArgs[] = $name;
        $queryArrayArgs[] = $jsonInfos['displayName'];
        $queryArrayArgs[] = $jsonInfos['description']['en'];
        $queryArrayArgs[] = $jsonInfos['version'];
        $queryArrayArgs[] = $jsonInfos['licence'];
        $queryArrayArgs[] = $jsonInfos['author'][0];
        $queryArrayArgs[] = $jsonInfos['author'][0];

        mysql2_query_secure($this->insertQuery, $this->dbWrite, $queryArrayArgs);

        try{
            $installMethod = self::EXTENSION_INSTALL_METHD.$name;
            $installMethod();
            // TODO : Successfuly instllaed
            return true;
        } catch (Exception $ex) {
            // TODO : PHP Error occured
            return false;
        }

    }

    /**
     * Extension removal
     *
     * @param $name : Name of the plugin
     * @return bool : true is ok else false
     */
    public function deleteExtension($name){

        if(!$this->isInstalled($name)){
            return false;
        }

        try{
            mysql2_query_secure($this->deleteQuery, $this->dbWrite, $name);
            $deleteMethod = self::EXTENSION_DELETE_METHD.$name;
            $deleteMethod();
            return true;
        } catch (Exception $ex) {
            return false;
        }

    }

    /**
     * Check if already installed
     */
    private function isInstalled($name){

        if(in_array($name, $this->installedExtensionsList)){
            return true;
        }else{
            return false;
        }

    }

    /**
     * Will set an array of installed extensions
     */
    private function getInstalledExtensionsList(){

        $selectResult = mysql2_query_secure($this->selectQuery, $this->dbObject);
        $installedExt = [];

        while($installedExtRows = $selectResult->fetch_array())
        {
            $installedExt[] = $installedExtRows['id'];
        }

        $this->installedExtensionsList = $installedExt;

    }

}