File: functions_check.php

package info (click to toggle)
ocsinventory-server 2.5%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,088 kB
  • sloc: php: 27,462; perl: 8,241; sh: 1,680; sql: 1,355; xml: 1,041; makefile: 34
file content (223 lines) | stat: -rw-r--r-- 7,276 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
<?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.
 */

/**
 * This function extracts the archive content and
 * puts it in the ocs reports for initialization.
 */
function install($archiveName) {
    if (file_exists(PLUGINS_DL_DIR . $archiveName)) {
        $archive = new ZipArchive();
        $archive->open(PLUGINS_DL_DIR . $archiveName);
        $arrayplugin = explode(".", $archiveName);
        $plugindir = $arrayplugin[0];
        if (!file_exists(MAIN_SECTIONS_DIR . "ms_" . $plugindir)) {
            $archive->extractTo(MAIN_SECTIONS_DIR . "ms_" . $plugindir);
            $archive->close();
            return true;
        } else {
            return false;
        }
    }
}

/**
 * This function checks if the plugin is already in the DATABASE and installed.
 * If not an entry is created in the table "plugins" with the plugins infos.
 */
function check($plugarray) {

    $conn = new PDO('mysql:host=' . SERVER_WRITE . ';dbname=' . DB_NAME . ';charset=utf8', COMPTE_BASE, PSWD_BASE);

    foreach ($plugarray as $value) {

        $query = $conn->query("SELECT EXISTS( SELECT * FROM `plugins` WHERE name = '" . $value . "' ) AS name_exists");
        $anwser = $query->fetch();

        // If the plugin isn't in the database ... add it
        if ($anwser[0] == false) {
            if (file_exists(MAIN_SECTIONS_DIR . "ms_" . $value . "/install.php")) {
                require MAIN_SECTIONS_DIR . "ms_" . $value . "/install.php";

                if (!function_exists("plugin_version_" . $value) || !function_exists("plugin_init_" . $value) || !function_exists("plugin_delete_" . $value)) {

                    rrmdir(MAIN_SECTIONS_DIR . "ms_" . $value);
                    return false;
                }

                // Retrieve infos from the plugin_version_plugname functions and add it to the database

                $fonc = "plugin_version_" . $value;
                $infoplugin = $fonc();

                $conn->query("INSERT INTO `" . DB_NAME . "`.`plugins` (`id`, `name`, `version`, `licence`, `author`, `verminocs`, `activated`, `reg_date`)
					VALUES (NULL, '" . $infoplugin['name'] . "', '" . $infoplugin['version'] . "', '" . $infoplugin['license'] . "', '" . $infoplugin['author'] . "', '" . $infoplugin['verMinOcs'] . "', '1', CURRENT_TIMESTAMP);");

                // Initialize the plugins requirement (New menus, Set permissions etc etc)
                $init = "plugin_init_" . $value;
                $infoplugin = $init();

                return true;
            } else {
                rrmdir(MAIN_SECTIONS_DIR . "ms_" . $value);

                return false;
            }
        }
    }
}

/**
 * Scan the DL plugins dir.
 * If the plugin isn't installed in the ocsreports, this function call the "install" function
 */
function scan_downloaded_plugins() {
    if (!file_exists(PLUGINS_DL_DIR)) {
        mkdir(PLUGINS_DL_DIR, '0775', true);
    }

    // Scan plugins download directory
    $directory = PLUGINS_DL_DIR;
    $scanned_directory = array_diff(scandir($directory), array('..', '.', 'README'));

    return $scanned_directory;
}

/**
 * This function checks and returns all plugins installed in the OCS Reports...
 *
 * @return ArrayObject
 */
function scan_for_plugins() {
    $scanned_plugins = array_diff(scandir(MAIN_SECTIONS_DIR), array('img', 'ms_all_soft', 'ms_debug', 'ms_groups', 'ms_multi_search', 'ms_stats', 'ms_computer', 'ms_dict', 'ms_help', 'ms_plugins', 'ms_search_soft', 'ms_teledeploy', 'ms_admininfo', 'ms_config', 'ms_doubles', 'ms_ipdiscover', 'ms_regconfig', 'ms_server_infos', 'ms_upload_file', 'ms_all_computers', 'ms_console', 'ms_export', 'ms_logs', 'ms_repart_tag', 'ms_snmp', 'ms_users', '.', '..'));

    $plugins_name = array();

    foreach ($scanned_plugins as $value) {
        $exp = explode("_", $value);
        $plugins_name[] = $exp[1];
    }

    return $plugins_name;
}

/**
 * This function checks if a cd_pluginame dir exists.
 * In that case the cd_pluginame dir is moved into computer_detail directory
 */
function mv_computer_detail($name) {
    if (file_exists($old = MAIN_SECTIONS_DIR . "ms_" . $name . "/cd_" . $name)) {
        $new = PLUGINS_DIR . "/computer_detail/cd_" . $name;
        rename($old, $new);
    }
}

/**
 * This function creates a plugin archive for the server side part of a plugin which contains the map.pm
 * This part will be downloaded and installed by the communication server.
 */
function mv_server_side($name) {
    $dir = MAIN_SECTIONS_DIR . "ms_" . $name . "/APACHE/";

    if (file_exists($dir)) {

        $archive = new ZipArchive();
        $archive->open(PLUGINS_SRV_SIDE . $name . ".zip", ZipArchive::CREATE);

        $scanned_directory = array_diff(scandir($dir), array('..', '.', 'README'));

        foreach ($scanned_directory as $value) {
            $archive->addFile($dir . $value, $value);
        }

        $archive->close();
        rrmdir($dir);

        return true;
    } else {
        return false;
    }
}

/**
 * This function checks for required php dependencies.
 * Can't install plugin if not installed
 */
function checkDependencies() {
    global $l;

    $missing_module = false;
    $str_msg = "";

    $modules_to_check = array(
        "ZipArchive",
        "SOAPClient"
    );

    foreach ($modules_to_check as $value) {
        if (!class_exists($value)) {
            $missing_module = true;
            $str_msg .= " - " . $value . "<br>";
        }
    }

    if ($missing_module == true) {
        msg_error($l->g(6007) . "<br>" . $str_msg);
        return false;
    } else {
        return true;
    }
}

/**
 * This function checks if directories are writable.
 * Can't install plugin if not writable
 */
function checkWritable() {
    global $l;

    $missing_permissions = false;
    $str_msg = "";

    $sup_writable_dir = array(
        CD_CONFIG_DIR,
        CONFIG_DIR,
        PLUGINS_DIR . "language",
        MAIN_SECTIONS_DIR,
        PLUGINS_SRV_SIDE
    );

    foreach ($sup_writable_dir as $value) {
        if (!is_writable($value)) {
            $missing_permissions = true;
            $str_msg .= " - " . $value . "<br>";
        }
    }

    if ($missing_permissions == true) {
        msg_error($l->g(6008) . "<br>" . $str_msg);
        return false;
    } else {
        return true;
    }
}