File: ExtensionHook.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 (337 lines) | stat: -rw-r--r-- 11,230 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<?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 ExtensionHook{

    const XML_HOOKS_FILE = "/hook.xml";

    const LANG_HOOK = "lang";
    const MENU_HOOK = "menu";
    const SUB_MENU_HOOK = "submenu";
    const CD_DETAIL_HOOK = "cdentry";

    const IDENTIFIER = "identifier";
    const MAIN_MENU_IDENTIFIER = "mainmenuidentifier";
    const TRANSLATION = "translation";
    const AVAILABLE = "available";
    const CATEGORY = "category";
    const EXTENSION = "extension";

    public $menuExtensionsHooks = array();
    public $subMenuExtensionsHooks = array();
    public $languageExtensionsHooks = array();
    public $computerDetailExtensionsHooks = array();

    public $activatedExt = array();

    // Simple array of menu available in all loaded extension
    public $extDeclaredMenu = array();
    public $computerDeclaredMenu = array();

    private $xmlElement;

    private $currentScannedExt = "";

    function __construct($activatedExtArray) {

        $this->activatedExt = $activatedExtArray;

        foreach ($activatedExtArray as $extLabel) {
            if($this->haveHook($extLabel)){
                $this->readHookXml($extLabel);
            }
            $this->addTranslation($extLabel);
        }

    }

    /**
     *
     * @param String $hookType Constant hook type
     */
    public function needHookTrigger($hookType){
        switch ($hookType) {
            case self::LANG_HOOK:
                if(empty($this->languageExtensionsHooks)){
                    return false;
                }else{
                    return true;
                }

            case self::MENU_HOOK:
                if(empty($this->menuExtensionsHooks)){
                    return false;
                }else{
                    return true;
                }

            case self::SUB_MENU_HOOK:
                if(empty($this->subMenuExtensionsHooks)){
                    return false;
                }else{
                    return true;
                }

            case self::CD_DETAIL_HOOK:
                if(empty($this->computerDetailExtensionsHooks)){
                    return false;
                }else{
                    return true;
                }
                break;

            default:
                return false;
        }
    }

    /**
     * This method read the hook.xml in extension to create menu / lang / submenu and more to come.
     */
    private function readHookXml($extLabel){
        $this->currentScannedExt = $extLabel;
        $xmlStr = file_get_contents(EXT_DL_DIR.$extLabel.self::XML_HOOKS_FILE);
        $this->xmlElement = new SimpleXMLElement($xmlStr);
        foreach ($this->xmlElement->hook as $hooks) {
            switch ($hooks->attributes()->type) {
                case self::LANG_HOOK:
                    $this->addLangEntries($hooks->value);
                    break;

                case self::MENU_HOOK:
                    $this->extDeclaredMenu[strval($hooks->identifier)] = $this->currentScannedExt;
                    $menuHookArray = array(
                        self::IDENTIFIER => $hooks->identifier,
                        self::TRANSLATION => $hooks->translation
                    );
                    $this->addMenuEntry($menuHookArray);
                    break;

                case self::SUB_MENU_HOOK:
                    $this->extDeclaredMenu[strval($hooks->identifier)] = $this->currentScannedExt;
                    $subMenuHookArray = array(
                        self::MAIN_MENU_IDENTIFIER => $hooks->mainmenuidentifier,
                        self::IDENTIFIER => $hooks->identifier,
                        self::TRANSLATION => $hooks->translation
                    );
                    $this->addSubMenuEntry($subMenuHookArray);
                    break;

                case self::CD_DETAIL_HOOK:
                    $this->computerDeclaredMenu[strval($hooks->identifier)] = $this->currentScannedExt;
                    $computerDetailHookArray = array(
                        self::IDENTIFIER => $hooks->identifier,
                        self::TRANSLATION => $hooks->translation,
                        self::CATEGORY => $hooks->category,
                        self::AVAILABLE => $hooks->available
                    );
                    $this->addCdEntry($computerDetailHookArray);
                    break;

                default:
                    break;
            }
        }
    }

    /**
     * Return the cd details in specified category
     *
     * @param $catName : Category name
     * @return array : Values
     */
    public function getCdEntryByCategory($catName){
        return $this->computerDetailExtensionsHooks[$catName];
    }

    /**
     * Add cd computers entries in class attributes
     *
     * @param array $xmlHookRender Array for xml hooks that contains cd entries to add
     */
    private function addCdEntry(array $xmlHookRender){
        $this->computerDetailExtensionsHooks[(string)$xmlHookRender[self::CATEGORY]][(string)$xmlHookRender[self::IDENTIFIER]] = array(
            self::IDENTIFIER => (string)$xmlHookRender[self::IDENTIFIER],
            self::TRANSLATION => (string)$xmlHookRender[self::TRANSLATION],
            self::CATEGORY => (string)$xmlHookRender[self::CATEGORY],
            self::AVAILABLE => (string)$xmlHookRender[self::AVAILABLE],
            self::EXTENSION => $this->currentScannedExt
        );
    }

    /**
     * Add lang entries in the class attributes for later use
     *
     * @param array $xmlHookRender Array for xml hooks that contains all lang to add
     */
    private function addSubMenuEntry(array $xmlHookRender){
        $this->subMenuExtensionsHooks[(string)$xmlHookRender[self::MAIN_MENU_IDENTIFIER]][$this->currentScannedExt][] = array(
            self::IDENTIFIER => (string)$xmlHookRender[self::IDENTIFIER],
            self::TRANSLATION => (string)$xmlHookRender[self::TRANSLATION]
        );
    }

    /**
     * Add lang entries in the class attributes for later use
     *
     * @param array $xmlHookRender Array for xml hooks that contains all lang to add
     */
    private function addMenuEntry(array $xmlHookRender){
        $this->menuExtensionsHooks[$this->currentScannedExt][] = array(
            self::IDENTIFIER => (string)$xmlHookRender[self::IDENTIFIER],
            self::TRANSLATION => (string)$xmlHookRender[self::TRANSLATION]
        );
    }

    /**
     * Add lang entries in the class attributes for later use
     *
     * @param SimpleXMLElement $xmlElementHookRender Array for xml hooks that contains all lang to add
     */
    private function addLangEntries(SimpleXMLElement $xmlElementHookRender){
        foreach ($xmlElementHookRender as $value) {
            $this->languageExtensionsHooks[$this->currentScannedExt][] = (string)$value[0];
        }
    }

    /**
     * This method check if the extension have a hook xml file
     */
    private function haveHook($extLabel){
        return file_exists(EXT_DL_DIR.$extLabel.self::XML_HOOKS_FILE);
    }

    /**
     * @param type $lang identifier of the lang you want to extend.
     *
     * Possible values :
     * br_BR
     * cs_CZ
     * de_DE
     * en_GB
     * es_ES
     * fr_FR
     * it_IT
     * ja_JP
     * pl_PL
     * pt_PT
     * ru_RU
     * si_SI
     * tr_TR
     * ug_UY
     * uk_UA
     */
   public function addTranslation($extName){

        global $l;

        $currentLang = $_SESSION['OCS']['LANGUAGE'];
        $langFile = EXT_DL_DIR.$extName."/language/".$currentLang."/".$currentLang.".txt";

        if(file_exists($langFile)){
            $l->addExternalLangFile($langFile);
        }else{
            $langFile = EXT_DL_DIR.$extName."/language/en_GB/en_GB.txt";
            $l->addExternalLangFile($langFile);
        }
    }

    /**
     * @param String $mainMenuIdentifier identifier of the menu
     *
     * Get sub menu list for a menu
     */
    private function getSubMenu($mainMenuIdentifier){
        return $this->subMenuExtensionsHooks[$mainMenuIdentifier];
    }

    /**
     * Will generate MenuElement for each array entries.
     *
     * @param Array $menuDatas Array of values
     */
    public function generateMenuRenderer($menuDatas, $isSubMenu = false){

        global $l;

        $childrenArray = array();
        if(!$isSubMenu){
            $subMenusInfos = $this->generateMenuChildrensRenderer($menuDatas[self::IDENTIFIER]);
            if($subMenusInfos != false){
                $childrenArray = $subMenusInfos;
            }
        }

        if(!empty($childrenArray)){
            $menuElem = new MenuElem("g(".$menuDatas[self::TRANSLATION].")",$menuDatas[self::IDENTIFIER], $childrenArray);
        }else{
            $menuElem = new MenuElem("g(".$menuDatas[self::TRANSLATION].")",$menuDatas[self::IDENTIFIER]);
        }

        return $menuElem;
    }

    /**
     * Will generate MenuElement for each sub menus
     *
     * @param Array $menusArray Array of values
     */
    public function generateMenuChildrensRenderer($mainMenuIdentifier){
        $subMenus = $this->getSubMenu($mainMenuIdentifier);
        if(empty($subMenus)){
            return false;
        }else{
            $menusElemArray = array();
            foreach ($subMenus as $extKey => $subMenusInfos) {
                for ($index = 0; $index < count($subMenusInfos); $index++) {
                    $menusElemArray[$subMenusInfos[$index][self::IDENTIFIER]] = $this->generateMenuRenderer($subMenusInfos[$index], true);
                }
            }
            return $menusElemArray;
        }
    }

    /**
     * This method will check if the menu is from an extension.
     *
     * @param $menuIdentifier
     *
     * @return boolean : if is in extDeclaredMenu array
     */
    public function isMenuFromExt($menuIdentifier){
        return array_key_exists($menuIdentifier, $this->extDeclaredMenu);
    }

    /**
     * This method will check if an extension add a menu to an existing menu
     *
     * @param $mainMenuUrl : Main menu url
     * @return boolean : true if ext have sub menu
     */
    public function haveExtSubMenu($mainMenuUrl){
        return array_key_exists($mainMenuUrl, $this->subMenuExtensionsHooks);
    }



}