Projet

Général

Profil

Paste
Statistiques
| Branche: | Révision:

ryxeo-glpi-git / inc / document.function.php @ b67d8923

Historique | Voir | Annoter | Télécharger (20,2 ko)

1
<?php
2
/*
3
 * @version $Id: document.function.php 7894 2009-01-25 20:06:49Z moyo $
4
 -------------------------------------------------------------------------
5
 GLPI - Gestionnaire Libre de Parc Informatique
6
 Copyright (C) 2003-2009 by the INDEPNET Development Team.
7

8
 http://indepnet.net/   http://glpi-project.org
9
 -------------------------------------------------------------------------
10

11
 LICENSE
12

13
 This file is part of GLPI.
14

15
 GLPI is free software; you can redistribute it and/or modify
16
 it under the terms of the GNU General Public License as published by
17
 the Free Software Foundation; either version 2 of the License, or
18
 (at your option) any later version.
19

20
 GLPI is distributed in the hope that it will be useful,
21
 but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 GNU General Public License for more details.
24

25
 You should have received a copy of the GNU General Public License
26
 along with GLPI; if not, write to the Free Software
27
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28
 --------------------------------------------------------------------------
29
 */
30

    
31
// ----------------------------------------------------------------------
32
// Original Author of file: Julien Dombre
33
// Purpose of file:
34
// ----------------------------------------------------------------------
35

    
36

    
37
if (!defined('GLPI_ROOT')){
38
        die("Sorry. You can't access directly to this file");
39
        }
40

    
41

    
42

    
43

    
44
/**
45
 * Move an uploadd document (files in GLPI_DOC_DIR."/_uploads" dir)
46
 *
47
 * @param $filename filename to move
48
 * @param $old_file old file name to replace : to unlink it
49
 * @return nothing
50
 **/
51
function moveUploadedDocument($filename,$old_file=''){
52
        global $CFG_GLPI,$LANG;
53

    
54
        if (is_dir(GLPI_DOC_DIR."/_uploads")){
55
                if (is_file(GLPI_DOC_DIR."/_uploads/".$filename)){
56
                        $dir=isValidDoc($filename);
57
                        $new_path=getUploadFileValidLocationName($dir,$filename,0);
58
                        if (!empty($new_path)){
59

    
60
                                // Delete old file
61
                                if(!empty($old_file)&& is_file(GLPI_DOC_DIR."/".$old_file)&& !is_dir(GLPI_DOC_DIR."/".$old_file)) {
62
                                        if (unlink(GLPI_DOC_DIR."/".$old_file))
63
                                                addMessageAfterRedirect($LANG["document"][24]." ".GLPI_DOC_DIR."/".$old_file);
64
                                        else 
65
                                                addMessageAfterRedirect($LANG["document"][25]." ".GLPI_DOC_DIR."/".$old_file);
66
                                }
67

    
68
                                // D�lacement si droit
69
                                if (is_writable (GLPI_DOC_DIR."/_uploads/".$filename)){
70
                                        if (rename(GLPI_DOC_DIR."/_uploads/".$filename,GLPI_DOC_DIR."/".$new_path)){
71
                                                addMessageAfterRedirect($LANG["document"][39]);
72
                                                return $new_path;
73
                                        }
74
                                        else {
75
                                                addMessageAfterRedirect($LANG["document"][40]);
76
                                        }
77
                                } else { // Copi sinon
78
                                        if (copy(GLPI_DOC_DIR."/_uploads/".$filename,GLPI_DOC_DIR."/".$new_path)){
79
                                                addMessageAfterRedirect($LANG["document"][41]);
80
                                                return $new_path;
81
                                        }
82
                                        else addMessageAfterRedirect($LANG["document"][40]);
83
                                }
84
                        }
85

    
86
                } else addMessageAfterRedirect($LANG["document"][38].": ".GLPI_DOC_DIR."/_uploads/".$filename);
87

    
88
        } else addMessageAfterRedirect($LANG["document"][35]);
89

    
90
        return "";        
91
}
92

    
93
/**
94
 * Upload a new file
95
 *
96
 * @param $FILEDESC FILE descriptor
97
 * @param $old_file old file name to replace : to unlink it
98
 * @return nothing
99
 **/
100
function uploadDocument($FILEDESC,$old_file=''){
101
        global $CFG_GLPI,$LANG;
102

    
103
        // Is a file uploaded ?
104
        if (count($FILEDESC)>0&&!empty($FILEDESC['name'])){
105
                // Clean is name
106
                $filename=cleanFilenameDocument($FILEDESC['name']);
107
                $force=0;
108
                // Is it a valid file ?
109
                $dir=isValidDoc($filename);
110
                if (!empty($old_file)&&$dir."/".$filename==$old_file) $force=1;
111

    
112
                $new_path=getUploadFileValidLocationName($dir,$filename,$force);
113

    
114
                if (!empty($new_path)){
115
                        // Delete old file
116
                        if(!empty($old_file)&& is_file(GLPI_DOC_DIR."/".$old_file)&& !is_dir(GLPI_DOC_DIR."/".$old_file)) {
117
                                if (unlink(GLPI_DOC_DIR."/".$old_file))
118
                                        addMessageAfterRedirect($LANG["document"][24]." ".GLPI_DOC_DIR."/".$old_file);
119
                                else 
120
                                        addMessageAfterRedirect($LANG["document"][25]." ".GLPI_DOC_DIR."/".$old_file);
121
                        }
122

    
123
                        // Move uploaded file
124
                        if (rename($FILEDESC['tmp_name'],GLPI_DOC_DIR."/".$new_path)) {
125
                                addMessageAfterRedirect($LANG["document"][26]);
126
                                return $new_path;
127
                        } else {
128
                                addMessageAfterRedirect($LANG["document"][27]);
129
                        }
130
                }
131

    
132

    
133
        } 
134
        return "";        
135
}
136

    
137
/**
138
 * Find a valid path for the new file
139
 *
140
 * @param $dir dir to search a free path for the file
141
 * @param $filename new filename
142
 * @param $force may replace an existing doc ?
143
 * @return nothing
144
 **/
145
function getUploadFileValidLocationName($dir,$filename,$force){
146

    
147
        global $CFG_GLPI,$LANG;
148

    
149
        if (!empty($dir)){
150
                // Test existance repertoire DOCS
151
                if (is_dir(GLPI_DOC_DIR)){
152
                        // Test existance sous-repertoire type dans DOCS -> sinon cr�tion
153
                        if (!is_dir(GLPI_DOC_DIR."/".$dir)){
154
                                addMessageAfterRedirect($LANG["document"][34]." ".GLPI_DOC_DIR."/".$dir);
155
                                @mkdir(GLPI_DOC_DIR."/".$dir);
156
                        }
157
                        // Copy du fichier upload�si r�ertoire existe
158
                        if (is_dir(GLPI_DOC_DIR."/".$dir)){
159
                                if (!$force){
160
                                        if (is_file(GLPI_DOC_DIR."/".$dir."/".$filename)){
161
                                                $original_split=explode('.',$filename);
162
                                                $where_to_add=count($original_split)-2;
163
                                                $splitted=$original_split;
164
                                                $number=2;
165
                                                $splitted[$where_to_add]=preg_replace('/_[0-9]*$/','',$splitted[$where_to_add])."_".$number;
166
                                                $filename=implode('.',$splitted);
167
                                                // Rename file if exists
168
                                                while (is_file(GLPI_DOC_DIR."/".$dir."/".$filename)){
169
                                                        $number++;
170
                                                        $splitted=$original_split;
171
                                                        
172
                                                        $splitted[$where_to_add]=preg_replace('/_[0-9]*$/','',$splitted[$where_to_add])."_".$number;
173
                                                        $filename=implode('.',$splitted);
174
                                                }
175
                                        }
176
                                }
177
                                if ($force||!is_file(GLPI_DOC_DIR."/".$dir."/".$filename)){
178
                                        return $dir."/".$filename;
179
                                } else addMessageAfterRedirect($LANG["document"][28]);
180

    
181
                        } else addMessageAfterRedirect($LANG["document"][29]." ".GLPI_DOC_DIR."/".$dir." ".$LANG["document"][30]);
182

    
183
                } else addMessageAfterRedirect($LANG["document"][31]." ".GLPI_DOC_DIR);
184

    
185
        } else addMessageAfterRedirect($LANG["document"][32]);
186

    
187
        return "";
188
}
189

    
190
/**
191
 * Show devices links to a document
192
 *
193
 * @param $instID document ID
194
 * @return nothing
195
 **/
196
function showDeviceDocument($instID) {
197
        global $DB,$CFG_GLPI, $LANG,$INFOFORM_PAGES,$LINK_ID_TABLE;
198

    
199
        if (!haveRight("document","r"))        return false;
200

    
201
        $doc=new Document();
202
        if ($doc->getFromDB($instID)){
203
                $canedit=$doc->can($instID,'w');
204

    
205
                // for a document,
206
                // don't show here others documents associated to this one,
207
                // it's done for both directions in showDocumentAssociated
208
                $query = "SELECT DISTINCT device_type FROM glpi_doc_device WHERE glpi_doc_device.FK_doc = '$instID' AND glpi_doc_device.device_type != ".DOCUMENT_TYPE." ORDER BY device_type";
209
                
210
                $result = $DB->query($query);
211
                $number = $DB->numrows($result);
212
                $i = 0;
213
        
214
                echo "<form method='post' name='document_form' id='document_form'  action=\"".$CFG_GLPI["root_doc"]."/front/document.form.php\">";
215
        
216
                echo "<br><br><div class='center'><table class='tab_cadre_fixe'>";
217
                echo "<tr><th colspan='".($canedit?6:5)."'>".$LANG["document"][19].":</th></tr><tr>";
218
                if ($canedit) {
219
                        echo "<th>&nbsp;</th>";
220
                }
221
                echo "<th>".$LANG["common"][17]."</th>";
222
                echo "<th>".$LANG["common"][16]."</th>";
223
                echo "<th>".$LANG["entity"][0]."</th>";
224
                echo "<th>".$LANG["common"][19]."</th>";
225
                echo "<th>".$LANG["common"][20]."</th>";
226
                echo "</tr>";
227
                $ci=new CommonItem();
228
                while ($i < $number) {
229
                        $type=$DB->result($result, $i, "device_type");
230
                        if (haveTypeRight($type,"r")){
231
                                $column="name";
232
                                if ($type==TRACKING_TYPE) $column="ID";
233
                                if ($type==KNOWBASE_TYPE) $column="question";
234
        
235
                                $query = "SELECT ".$LINK_ID_TABLE[$type].".*, glpi_doc_device.ID AS IDD, glpi_entities.ID AS entity "
236
                                        ." FROM glpi_doc_device, ".$LINK_ID_TABLE[$type]
237
                                        ." LEFT JOIN glpi_entities ON (glpi_entities.ID=".$LINK_ID_TABLE[$type].".FK_entities) "
238
                                        ." WHERE ".$LINK_ID_TABLE[$type].".ID = glpi_doc_device.FK_device  AND glpi_doc_device.device_type='$type' AND glpi_doc_device.FK_doc = '$instID' "
239
                                        . getEntitiesRestrictRequest(" AND ",$LINK_ID_TABLE[$type],'','',isset($CFG_GLPI["recursive_type"][$type])); 
240
                                if (in_array($LINK_ID_TABLE[$type],$CFG_GLPI["template_tables"])){
241
                                        $query.=" AND ".$LINK_ID_TABLE[$type].".is_template='0'";
242
                                }
243
                                $query.=" ORDER BY glpi_entities.completename, ".$LINK_ID_TABLE[$type].".$column";
244
                                
245
                                if ($result_linked=$DB->query($query))
246
                                        if ($DB->numrows($result_linked)){
247
                                                $ci->setType($type);
248
                                                while ($data=$DB->fetch_assoc($result_linked)){
249
                                                        $ID="";
250
                                                        if ($type==TRACKING_TYPE) $data["name"]=$LANG["job"][38]." ".$data["ID"];
251
                                                        if ($type==KNOWBASE_TYPE) $data["name"]=$data["question"];
252
                                                        
253
                                                        if($CFG_GLPI["view_ID"]||empty($data["name"])) $ID= " (".$data["ID"].")";
254
                                                        $name= "<a href=\"".$CFG_GLPI["root_doc"]."/".$INFOFORM_PAGES[$type]."?ID=".$data["ID"]."\">"
255
                                                                .$data["name"]."$ID</a>";
256
        
257
                                                        echo "<tr class='tab_bg_1'>";
258

    
259
                                                        if ($canedit){
260
                                                                echo "<td width='10'>";
261
                                                                $sel="";
262
                                                                if (isset($_GET["select"])&&$_GET["select"]=="all") $sel="checked";
263
                                                                echo "<input type='checkbox' name='item[".$data["IDD"]."]' value='1' $sel>";
264
                                                                echo "</td>";
265
                                                        }
266
                                                        echo "<td class='center'>".$ci->getType()."</td>";
267
                                                        
268
                                                        echo "<td ".(isset($data['deleted'])&&$data['deleted']?"class='tab_bg_2_2'":"").">".$name."</td>";
269
                                                        echo "<td class='center'>".getDropdownName("glpi_entities",$data['entity'])."</td>";
270
                                                        echo "<td class='center'>".(isset($data["serial"])? "".$data["serial"]."" :"-")."</td>";
271
                                                        echo "<td class='center'>".(isset($data["otherserial"])? "".$data["otherserial"]."" :"-")."</td>";
272
                                                        
273
                                                        echo "</tr>";
274
                                                }
275
                                        }
276
                        }
277
                        $i++;
278
                }
279
        
280
                if ($canedit)        {
281
                        echo "<tr class='tab_bg_1'><td colspan='4' class='center'>";
282
        
283
                        echo "<input type='hidden' name='conID' value='$instID'>";
284
                        echo "<input type='hidden' name='right' value='doc'>";
285
                        $types=$CFG_GLPI["state_types"];
286
                        $types[]=ENTERPRISE_TYPE;
287
                        $types[]=CARTRIDGE_TYPE;
288
                        $types[]=CONSUMABLE_TYPE;
289
                        $types[]=CONTRACT_TYPE;
290

    
291
                        dropdownAllItems("item",0,0,($doc->fields['recursive']?-1:$doc->fields['FK_entities']),$types);
292
                        
293
                        echo "</td>";
294
                        echo "<td colspan='2' class='center'>";
295
                        echo "<input type='submit' name='additem' value=\"".$LANG["buttons"][8]."\" class='submit'>";
296
                        echo "</td></tr>";
297
                        echo "</table></div>" ;
298
                        
299
                        echo "<div class='center'>";
300
                        echo "<table width='950px'>";
301
                        echo "<tr><td><img src=\"".$CFG_GLPI["root_doc"]."/pics/arrow-left.png\" alt=''></td><td class='center'><a onclick= \"if ( markAllRows('document_form') ) return false;\" href='".$_SERVER['PHP_SELF']."?ID=$instID&amp;select=all'>".$LANG["buttons"][18]."</a></td>";
302
                
303
                        echo "<td>/</td><td class='center'><a onclick= \"if ( unMarkAllRows('document_form') ) return false;\" href='".$_SERVER['PHP_SELF']."?ID=$instID&amp;select=none'>".$LANG["buttons"][19]."</a>";
304
                        echo "</td><td align='left' width='80%'>";
305
                        echo "<input type='submit' name='deleteitem' value=\"".$LANG["buttons"][6]."\" class='submit'>";
306
                        echo "</td>";
307
                        echo "</table>";
308
                
309
                        echo "</div>";
310

    
311

    
312
                }else{
313
        
314
                        echo "</table></div>"    ;
315
                }
316
                echo "</form>";
317
        }
318

    
319
}
320

    
321
/**
322
 * Add a document to an item
323
 *
324
 * @param $docID document ID
325
 * @param $ID item ID
326
 * @param $type item type
327
 **/
328
function addDeviceDocument($docID,$type,$ID){
329
        global $DB;
330
        if ($docID>0&&$ID>0&&$type>0){
331
                // Do not insert auto link for document
332
                if ($type==DOCUMENT_TYPE && $ID == $docID){
333
                        return;
334
                }
335
                $query="INSERT INTO glpi_doc_device (FK_doc,FK_device, device_type) VALUES ('$docID','$ID','$type');";
336
                $result = $DB->query($query);
337
        }
338
}
339
/**
340
 * Delete a document to an item
341
 *
342
 * @param $ID doc_device ID
343
 **/
344
function deleteDeviceDocument($ID){
345

    
346
        global $DB;
347
        $query="DELETE FROM glpi_doc_device WHERE ID= '$ID';";
348
        $result = $DB->query($query);
349
}
350

    
351
/**
352
 * Show documents associated to an item
353
 *
354
 * @param $device_type item type
355
 * @param $ID item ID
356
 * @param $withtemplate if 3 -> view via helpdesk -> no links
357
 **/
358
function showDocumentAssociated($device_type,$ID,$withtemplate=''){
359

    
360
        global $DB, $CFG_GLPI, $LANG, $LINK_ID_TABLE;
361

    
362
        if ($device_type!=KNOWBASE_TYPE)
363
                if (!haveRight("document","r")||!haveTypeRight($device_type,"r"))        return false;
364

    
365
        if (empty($withtemplate)) $withtemplate=0;
366

    
367
        $ci=new CommonItem();
368
        $ci->getFromDB($device_type,$ID);
369
        $canread=$ci->obj->can($ID,'r');
370
        $canedit=$ci->obj->can($ID,'w');
371

    
372
        $needed_fields=array('ID','name','filename','mime','rubrique','link','deleted','FK_entities','recursive');
373

    
374

    
375
        $query = "SELECT glpi_doc_device.ID AS assocID, glpi_entities.ID AS entity, 
376
                        glpi_docs.name AS assocName, glpi_docs.* FROM glpi_doc_device
377
                        LEFT JOIN glpi_docs ON (glpi_doc_device.FK_doc=glpi_docs.ID) 
378
                        LEFT JOIN glpi_entities ON (glpi_docs.FK_entities=glpi_entities.ID)
379
                        WHERE glpi_doc_device.FK_device = '$ID' AND glpi_doc_device.device_type = '$device_type' ";
380

    
381
        if (isset($_SESSION["glpiID"])){
382
                $query .= getEntitiesRestrictRequest(" AND","glpi_docs",'','',true);
383
        } else {
384
                // Anonymous access from FAQ
385
                $query .= " AND glpi_docs.FK_entities=0 ";
386
        }
387
        
388
        // Document : search links in both order using union
389
        if ($device_type==DOCUMENT_TYPE){
390
                $query .= "UNION 
391
                        SELECT glpi_doc_device.ID as assocID, glpi_entities.ID AS entity, 
392
                                glpi_docs.name AS assocName, glpi_docs.* FROM glpi_doc_device
393
                                LEFT JOIN glpi_docs ON (glpi_doc_device.FK_device=glpi_docs.ID)
394
                                LEFT JOIN glpi_entities ON (glpi_docs.FK_entities=glpi_entities.ID)
395
                                WHERE glpi_doc_device.FK_doc = '$ID' AND glpi_doc_device.device_type = '$device_type' ";
396
                if (isset($_SESSION["glpiID"])){
397
                        $query .= getEntitiesRestrictRequest(" AND","glpi_docs",'','',true);
398
                } else {
399
                        // Anonymous access from FAQ
400
                        $query .= " AND glpi_docs.FK_entities=0 ";
401
                }
402
        } 
403
        $query .= " ORDER BY assocName";
404
                
405
        //echo $query;
406
        
407
        $result = $DB->query($query);
408
        $number = $DB->numrows($result);
409
        $i = 0;
410

    
411
        if ($withtemplate!=2) {
412
                echo "<form method='post' action=\"".$CFG_GLPI["root_doc"]."/front/document.form.php\" enctype=\"multipart/form-data\">";
413
        }
414
        echo "<br><br><div class='center'><table class='tab_cadre_fixe'>";
415
        echo "<tr><th colspan='7'>".$LANG["document"][21].":</th></tr>";
416
        echo "<tr><th>".$LANG["common"][16]."</th>";
417
        echo "<th>".$LANG["entity"][0]."</th>";
418
        echo "<th width='100px'>".$LANG["document"][2]."</th>";
419
        echo "<th>".$LANG["document"][33]."</th>";
420
        echo "<th>".$LANG["document"][3]."</th>";
421
        echo "<th>".$LANG["document"][4]."</th>";
422
        if ($withtemplate<2)echo "<th>&nbsp;</th>";
423
        echo "</tr>";
424
        $used=array();
425
        if ($number){
426
                while ($data=$DB->fetch_assoc($result)) {
427
                        $docID=$data["ID"];
428
                        $used[$docID]=$docID;
429
                        $assocID=$data["assocID"];
430
        
431
                        echo "<tr class='tab_bg_1".($data["deleted"]?"_2":"")."'>";
432
                        if ($withtemplate!=3 && $canread 
433
                                && (in_array($data['FK_entities'],$_SESSION['glpiactiveentities']) || $data["recursive"])
434
                        ){
435
                                echo "<td class='center'><a href='".$CFG_GLPI["root_doc"]."/front/document.form.php?ID=$docID'><strong>".$data["name"];
436
                                if ($CFG_GLPI["view_ID"]) echo " (".$docID.")";
437
                                echo "</strong></a></td>";
438
                        } else {
439
                                echo "<td class='center'><strong>".$data["name"];
440
                                if ($CFG_GLPI["view_ID"]) echo " (".$docID.")";
441
                                echo "</strong></td>";
442
                        }
443
                        echo "<td class='center'>".getDropdownName("glpi_entities",$data['entity'])."</td>";
444
                        
445
                        echo "<td align='center'  width='100px'>".getDocumentLink($data["filename"])."</td>";
446
        
447
                        echo "<td class='center'>";
448
                        if (!empty($data["link"]))
449
                                echo "<a target=_blank href='".$data["link"]."'>".$data["link"]."</a>";
450
                        else echo "&nbsp;";
451
                        echo "</td>";
452
                        echo "<td class='center'>".getDropdownName("glpi_dropdown_rubdocs",$data["rubrique"])."</td>";
453
                        echo "<td class='center'>".$data["mime"]."</td>";
454
        
455
                        if ($withtemplate<2) {
456
                                echo "<td align='center' class='tab_bg_2'>";
457
                                if ($canedit)
458
                                        echo "<a href='".$CFG_GLPI["root_doc"]."/front/document.form.php?deleteitem=deleteitem&amp;ID=$assocID&amp;devtype=$device_type&amp;devid=$ID&amp;docid=$docID'><strong>".$LANG["buttons"][6]."</strong></a>";
459
                                else echo "&nbsp;";
460
                                echo "</td>";
461
                        }
462
                        echo "</tr>";
463
                        $i++;
464
                }
465
        }
466

    
467
        if ($canedit){
468
                // Restrict entity for knowbase
469
                $ci=new CommonItem();
470
                $entities="";
471
                $entity=$_SESSION["glpiactive_entity"];
472
                if ($ci->getFromDB($device_type,$ID) && isset($ci->obj->fields["FK_entities"])) {                
473
                        $entity=$ci->getField('FK_entities');
474
                        
475
                        if (isset($ci->obj->fields["recursive"]) && $ci->obj->fields["recursive"]) {
476
                                $entities = getEntitySons($ci->obj->fields["FK_entities"]);
477
                        } else {
478
                                $entities = $ci->obj->fields["FK_entities"];
479
                        }
480
                }
481
                $limit = getEntitiesRestrictRequest(" AND ","glpi_docs",'',$entities,true);
482
                $q="SELECT count(*) FROM glpi_docs WHERE deleted='0' $limit";
483
                        
484
                $result = $DB->query($q);
485
                $nb = $DB->result($result,0,0);
486
        
487
                if ($withtemplate<2){
488
        
489
                        echo "<tr class='tab_bg_1'><td align='center' colspan='3'>" .
490
                                "<input type='hidden' name='FK_entities' value='$entity'>" .
491
                                "<input type='hidden' name='item' value='$ID'>" .
492
                                "<input type='hidden' name='type' value='$device_type'>" .
493
                                "<input type='file' name='filename' size='25'>&nbsp;&nbsp;" .
494
                                "<input type='submit' name='add' value=\"".$LANG["buttons"][8]."\" class='submit'>" .
495
                                "</td>";
496

    
497
                        if ($device_type==DOCUMENT_TYPE){
498
                                $used[$ID]=$ID;
499
                        }
500

    
501
                        if ($nb>count($used)) {
502
                                echo "<td align='left' colspan='2'>";
503
                                echo "<div class='software-instal'>";
504
                                echo "<input type='hidden' name='right' value='item'>";
505
                                dropdownDocument("conID",$entities,$used);
506
                                echo "</div></td><td class='center'>";
507
                                echo "<input type='submit' name='additem' value=\"".$LANG["buttons"][8]."\" class='submit'>";
508
                                echo "</td><td>&nbsp;</td>";
509
                        }
510
                        else {
511
                                echo "<td colspan='4'>&nbsp;</td>";
512
                        }        
513
                        echo "</tr>";
514
                }
515
        }
516

    
517
        echo "</table></div>"    ;
518
        echo "</form>";
519

    
520
}
521

    
522
/**
523
 * Get download link for a document
524
 *
525
 * @param filename filename of the document
526
 * @param $params additonal parameters to be added to the link
527
 **/
528
function getDocumentLink($filename,$params=""){
529
        global $DB,$CFG_GLPI;        
530
        if (empty($filename))
531
                return "&nbsp;";
532
        $out="";
533
        $splitter=split("/",$filename);
534
        if (count($splitter)==2)
535
                $fileout=$splitter[1];
536
        else $fileout=$filename;
537

    
538
        if (strlen($fileout)>20) $fileout=substr($fileout,0,20)."...";
539

    
540
        if (count($splitter)==2){
541

    
542
                $query="SELECT * from glpi_type_docs WHERE ext LIKE '".$splitter[0]."' AND icon <> ''";
543

    
544
                if ($result=$DB->query($query))
545
                        if ($DB->numrows($result)>0){
546
                                $icon=$DB->result($result,0,'icon');
547

    
548
                                $out="<a href=\"".$CFG_GLPI["root_doc"]."/front/document.send.php?file=$filename$params\" target=\"_blank\">&nbsp;<img style=\"vertical-align:middle; margin-left:3px; margin-right:6px;\" alt='".$fileout."' title='".$fileout."' src=\"".$CFG_GLPI["typedoc_icon_dir"]."/$icon\" ></a>";                                
549
                        }
550

    
551
        }
552

    
553
        $out.="<a href=\"".$CFG_GLPI["root_doc"]."/front/document.send.php?file=$filename$params\" target=\"_blank\"><strong>$fileout</strong></a>";        
554

    
555

    
556
        return $out;
557
}
558

    
559
/**
560
 * Clean a filename to keep alphanum chars + -_.
561
 *
562
 * @param name filename to clean
563
 **/
564
function cleanFilenameDocument($name){
565
        // See http://en.wikipedia.org/wiki/Filename
566
        $bad_chars = array("'", "\\", ' ', '/', ':', '*', '?', '"', '<', '>', '|');
567
        $name = str_replace($bad_chars, '_', $name);
568
        $name = preg_replace("/%(\w{2})/", '_', $name);
569
        return preg_replace("/\\x00-\\x1f/u", '_', $name);
570
}
571

    
572
/**
573
 * Show dropdown of uploaded files
574
 *
575
 * @param $myname dropdown name
576
 **/
577
function showUploadedFilesDropdown($myname){
578
        global $CFG_GLPI,$LANG;
579

    
580
        if (is_dir(GLPI_DOC_DIR."/_uploads")){
581
                $uploaded_files=array();
582
                if ($handle = opendir(GLPI_DOC_DIR."/_uploads")) {
583
                        while (false !== ($file = readdir($handle))) {
584
                                if ($file != "." && $file != "..") {
585
                                        $dir=isValidDoc($file);
586
                                        if (!empty($dir))
587
                                                $uploaded_files[]=$file;
588
                                }
589
                        }
590
                        closedir($handle);
591
                }
592

    
593
                if (count($uploaded_files)){
594
                        echo "<select name='$myname'>";
595
                        echo "<option value=''>-----</option>";
596
                        foreach ($uploaded_files as $key => $val)
597
                                echo "<option value=\"$val\">$val</option>";
598
                        echo "</select>";
599
                } else echo $LANG["document"][37];
600
        } else echo $LANG["document"][35];
601
}
602

    
603
/**
604
 * Is this file a valid file ? check based on file extension
605
 *
606
 * @param $filename filename to clean
607
 **/
608
function isValidDoc($filename){
609
        global $DB;
610
        $splitter=split("\.",$filename);
611
        $ext=end($splitter);
612

    
613
        $query="SELECT * from glpi_type_docs where ext LIKE '$ext' AND upload='1'";
614
        if ($result = $DB->query($query))
615
                if ($DB->numrows($result)>0)
616
                        return strtoupper($ext);
617

    
618
        return "";
619
}
620

    
621

    
622
?>
Redmine Appliance - Powered by TurnKey Linux