File: ms_csv.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 (195 lines) | stat: -rw-r--r-- 8,076 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
<?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.
 */

$values = look_config_default_values(array('EXPORT_SEP'));
if (is_defined($values['tvalue']['EXPORT_SEP'])) {
    $separator = $values['tvalue']['EXPORT_SEP'];
} else {
    $separator = ';';
}

$link = $_SESSION['OCS']["readServer"];
$toBeWritten = "";

// Export DB data
if (isset($_SESSION['OCS']['csv']['SQL'][$protectedGet['tablename']])) {

    // Gestion des entetes
    foreach ($_SESSION['OCS']['visible_col'][$protectedGet['tablename']] as $name => $nothing) {
        if ($name != 'SUP' && $name != 'CHECK' && $name != 'NAME' && $name != 'ACTIONS') {
            if ($_SESSION['OCS']['visible_col'][$protectedGet['tablename']][$name]{1} == ".") {
                $lbl = substr(strrchr($_SESSION['OCS']['visible_col'][$protectedGet['tablename']][$name], "."), 1);
            } else {
                $lbl = $_SESSION['OCS']['visible_col'][$protectedGet['tablename']][$name];
            }
            $col[$lbl] = $name;
            $toBeWritten .= $name . $separator;
        } elseif ($name == 'NAME' || $name == $l->g(23)) {
            $lbl = $_SESSION['OCS']['visible_col'][$protectedGet['tablename']][$name];
            $col[$lbl] = $name;
            $toBeWritten .= $l->g(23) . $separator;
        }
    }

    // Data fixe
    if (isset($_SESSION['OCS']['SQL_DATA_FIXE'][$protectedGet['tablename']])) {
        $i = 0;
        while ($_SESSION['OCS']['SQL_DATA_FIXE'][$protectedGet['tablename']][$i]) {
            $result = mysqli_query($link, $_SESSION['OCS']['SQL_DATA_FIXE'][$protectedGet['tablename']][$i]) or die(mysqli_error($link));
            while ($cont = mysqli_fetch_array($result)) {
                foreach ($col as $field => $lbl) {
                    if (array_key_exists($lbl, $cont)) {
                        $data_fixe[$cont['HARDWARE_ID']][$field] = $cont[$lbl];
                    }
                }
            }
            $i++;
        }
    }

    if ($_SESSION['OCS']['csv']['ARG'][$protectedGet['tablename']]) {
        $arg = $_SESSION['OCS']['csv']['ARG'][$protectedGet['tablename']];
    } else {
        $arg = '';
    }

    if (isset($protectedGet['nolimit'])) {
        $result = mysql2_query_secure($_SESSION['OCS']['csv']['SQLNOLIMIT'][$protectedGet['tablename']], $link, $arg);
    } else {
        $result = mysql2_query_secure($_SESSION['OCS']['csv']['SQL'][$protectedGet['tablename']], $link, $arg);
    }

    $i = 0;
    require_once('require/function_admininfo.php');
    $inter = interprete_accountinfo($col, array());
    while ($cont = mysqli_fetch_array($result)) {
        unset($cont['MODIF']);
        foreach ($inter as $field => $lbl) {
            if ($lbl == "name_of_machine" && !isset($cont[$field])) {
                $field = 'name';
            }
            $found = false;
            // find value case-insensitive
            foreach ($col as $key => $val) {
                if (strpos($key, ".") !== false) {
                    $exploded_key = explode(".", $key);
                    $key = $exploded_key[1];
                }
                if (array_key_exists($key, $cont)) {
                    if (($field == 'TAG' || substr($key, 0, 7) == 'fields_') && isset($inter['TAB_OPTIONS']['REPLACE_VALUE'][$val])) {
                        // administrative data
                        if(strpos($cont[$key], "&&&")){
                          $value_field = explode("&&&", $cont[$key]);
                          $value_admin = implode(" ", $value_field);
                          $inter['TAB_OPTIONS']['REPLACE_VALUE'][$val][$cont[$key]] = $value_admin;
                        }
                        $data[$i][$key] = $inter['TAB_OPTIONS']['REPLACE_VALUE'][$val][$cont[$key]];
                    } else {
                        // normal data
                        $data[$i][$key] = $cont[$key];
                    }
                    $found = true;
                } elseif (isset($_SESSION['OCS']['VALUE_FIXED'][$protectedGet['tablename']][$key][$cont['ID']])) {
                    $data[$i][$key] = $_SESSION['OCS']['VALUE_FIXED'][$protectedGet['tablename']][$key][$cont['ID']];
                    $found = true;
                } elseif (array_key_exists(strtoupper($key),$cont)){ // in the case key is in lower case and array cont is in upper case
                    $data[$i][strtoupper($key)] = $cont[strtoupper($key)];
                } elseif (strpos($key, ' AS ') !== false || strpos($key, ' as ') !== false) {
                    $key_explode  = explode(" ", $key);
                    $data[$i][$key_explode[2]] = $cont[$key_explode[2]];
                }
                if (isset($_SESSION['OCS']['csv']['REPLACE_VALUE'][$protectedGet['tablename']][$key])) {
                    $data[$i][$key] = $_SESSION['OCS']['csv']['REPLACE_VALUE'][$protectedGet['tablename']][$key][$data[$i][$key]];
                }
                if (isset($_SESSION['OCS']['csv']['REPLACE_VALUE_ALL_TIME'][$protectedGet['tablename']][$key])) {
                    $data[$i][$key] = $_SESSION['OCS']['csv']['REPLACE_VALUE_ALL_TIME'][$protectedGet['tablename']][$data[$i][$_SESSION['OCS']['csv']['FIELD_REPLACE_VALUE_ALL_TIME'][$protectedGet['tablename']]]];
                }
            }
        }
        $i++;
    }

    $i = 0;
    while ($data[$i]) {
        $toBeWritten .= "\r\n";
        foreach ($data[$i] as $field_name => $donnee) {
          if (substr($donnee, 0 , 1) != "\"") {
            $toBeWritten .= "\"";
          }
          $toBeWritten .= $donnee;
          if ($donnee[strlen($donnee)-1] != "\"") {
            $toBeWritten .= "\"";
          }
          $toBeWritten .= $separator;
        }
        $i++;
    }
}


// Get directory of log file
if (isset($protectedGet['log']) && !preg_match("/([^A-Za-z0-9.])/", $protectedGet['log'])) {
    $Directory = $_SESSION['OCS']['LOG_DIR'] . "/";
}

// Generate output page
if ($toBeWritten != "" || (isset($Directory) && file_exists($Directory . $protectedGet['log'])) ) {

    // Work around iexplorer problem
    if (ini_get("zlib.output-compression")) {
        ini_set("zlib.output-compression", "Off");
    }

    // Static headers
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-control: private", false);
    header("Content-type: application/force-download");
    header("Content-Transfer-Encoding: binary");

    if ($toBeWritten != "") {
	// Generate output page for DB data export
        header("Content-Disposition: attachment; filename=\"export.csv\"");
        header("Content-Length: " . strlen($toBeWritten));
        echo $toBeWritten;
    } else {
	// Generate output page for log export
	$filename = $Directory . $protectedGet['log'];
        header("Content-Disposition: attachment; filename=\"" . $protectedGet['log'] . "\"");
        header("Content-Length: " . filesize($filename));
        readfile($filename);
    }

} else {

    // Nothing to export
    require_once (HEADER_HTML);
    msg_error($l->g(920));
    require_once(FOOTER_HTML);

}

die();

?>