Subversion Repositories Sites.tela-botanica.org

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
420 florian 1
<?php
2
/*
3
attach.class.php
4
Code original de ce fichier : Eric FELDSTEIN
5
Copyright (c) 2002, Hendrik Mans <hendrik@mans.de>
6
Copyright 2002, 2003 David DELON
7
Copyright 2002, 2003 Charles NEPOTE
8
Copyright  2003,2004  Eric FELDSTEIN
9
Copyright  2003  Jean-Pascal MILCENT
10
All rights reserved.
11
Redistribution and use in source and binary forms, with or without
12
modification, are permitted provided that the following conditions
13
are met:
14
1. Redistributions of source code must retain the above copyright
15
notice, this list of conditions and the following disclaimer.
16
2. Redistributions in binary form must reproduce the above copyright
17
notice, this list of conditions and the following disclaimer in the
18
documentation and/or other materials provided with the distribution.
19
3. The name of the author may not be used to endorse or promote products
20
derived from this software without specific prior written permission.
21
 
22
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
*/
33
# Classe de gestion de l'action {{attach}}
34
# voir actions/attach.php ppour la documentation
35
# copyrigth Eric Feldstein 2003-2004
36
 
37
if (!defined("WIKINI_VERSION"))
38
{
39
        die ("acc&egrave;s direct interdit");
40
}
41
 
42
 
43
class attach {
44
	var $wiki = '';					//objet wiki courant
45
   var $attachConfig = array();	//configuration de l'action
46
   var $file = '';					//nom du fichier
47
   var $desc = '';					//description du fichier
48
   var $link = '';					//url de lien (image sensible)
49
   var $isPicture = 0;				//indique si c'est une image
50
   var $isAudio = 0;				//indique si c'est un fichier audio
51
   var $isFreeMindMindMap = 0;				//indique si c'est un fichier miindmpa freemind
52
   var $classes = '';				//classe pour afficher une image
53
   var $attachErr = '';				//message d'erreur
54
   var $pageId = 0;					//identifiant de la page
55
   var $isSafeMode = false;		//indicateur du safe mode de PHP
56
   /**
57
   * Constructeur. Met les valeurs par defaut aux param�tres de configuration
58
   */
59
	function attach(&$wiki){
60
   	$this->wiki = $wiki;
61
		$this->attachConfig = $this->wiki->GetConfigValue("attach_config");
62
		if (empty($this->attachConfig["ext_images"])) $this->attachConfig["ext_images"] = "gif|jpeg|png|jpg";
63
		if (empty($this->attachConfig["ext_audio"])) $this->attachConfig["ext_audio"] = "mp3";
64
		if (empty($this->attachConfig["ext_freemind"])) $this->attachConfig["ext_freemind"] = "mm";
65
		if (empty($this->attachConfig["ext_script"])) $this->attachConfig["ext_script"] = "php|php3|asp|asx|vb|vbs|js";
66
		if (empty($this->attachConfig['upload_path'])) $this->attachConfig['upload_path'] = 'files';
67
		if (empty($this->attachConfig['update_symbole'])) $this->attachConfig['update_symbole'] = '*';
68
		if (empty($this->attachConfig['max_file_size'])) $this->attachConfig['max_file_size'] = 1024*10000;	//10000ko max
69
		if (empty($this->attachConfig['fmDelete_symbole'])) $this->attachConfig['fmDelete_symbole'] = 'Supr';
70
		if (empty($this->attachConfig['fmRestore_symbole'])) $this->attachConfig['fmRestore_symbole'] = 'Rest';
71
		if (empty($this->attachConfig['fmTrash_symbole'])) $this->attachConfig['fmTrash_symbole'] = 'Poubelle';
72
		$this->isSafeMode = ini_get("safe_mode");
73
	}
74
/******************************************************************************
75
*	FONCTIONS UTILES
76
*******************************************************************************/
77
	/**
78
	* Cr�ation d'une suite de r�pertoires r�cursivement
79
	*/
80
	function mkdir_recursif ($dir) {
81
		if (strlen($dir) == 0) return 0;
82
		if (is_dir($dir)) return 1;
83
		elseif (dirname($dir) == $dir) return 1;
84
		return ($this->mkdir_recursif(dirname($dir)) and mkdir($dir,0755));
85
	}
86
	/**
87
	* Renvois le chemin du script
88
	*/
89
	function GetScriptPath () {
90
		if (preg_match("/.(php)$/i",$_SERVER["PHP_SELF"])){
91
			$a = explode('/',$_SERVER["PHP_SELF"]);
92
			$a[count($a)-1] = '';
93
			$path = implode('/',$a);
94
		}else{
95
			$path = $_SERVER["PHP_SELF"];
96
		}
97
		return !empty($_SERVER["HTTP_HOST"])? 'http://'.$_SERVER["HTTP_HOST"].$path : 'http://'.$_SERVER["SERVER_NAME"].$path ;
98
	}
99
	/**
100
	* Calcul le repertoire d'upload en fonction du safe_mode
101
	*/
102
	function GetUploadPath(){
103
		if ($this->isSafeMode) {
104
			$path = $this->attachConfig['upload_path'];
105
		}else{
106
         $path = $this->attachConfig['upload_path'].'/'.$this->wiki->GetPageTag();
107
			if (! is_dir($path)) $this->mkdir_recursif($path);
108
		}
109
		return $path;
110
	}
111
	/**
112
	* Calcule le nom complet du fichier attach� en fonction du safe_mode, du nom et de la date de
113
	* revision la page courante.
114
	* Le nom du fichier "mon fichier.ext" attache � la page "LaPageWiki"sera :
115
	*  mon_fichier_datepage_update.ext
116
	*     update : date de derniere mise a jour du fichier
117
	*     datepage : date de revision de la page � laquelle le fichier a ete li�/mis a jour
118
	*  Si le fichier n'est pas une image un '_' est ajoute : mon_fichier_datepage_update.ext_
119
	*  Selon la valeur de safe_mode :
120
	*  safe_mode = on : 	LaPageWiki_mon_fichier_datepage_update.ext_
121
	*  safe_mode = off: 	LaPageWiki/mon_fichier_datepage_update.ext_ avec "LaPageWiki" un sous-repertoire du r�pertoire upload
122
	*/
123
	function GetFullFilename($newName = false){
124
		$pagedate = $this->convertDate($this->wiki->page['time']);
125
		//decompose le nom du fichier en nom+extension
126
		if (preg_match('`^(.*)\.(.*)$`', str_replace(' ','_',$this->file), $match)){
127
			list(,$file['name'],$file['ext'])=$match;
128
			if(!$this->isPicture() && !$this->isAudio() && !$this->isFreeMindMindMap()) $file['ext'] .= '_';
129
		}else{
130
			return false;
131
		}
132
		//recuperation du chemin d'upload
133
		$path = $this->GetUploadPath($this->isSafeMode);
134
		//generation du nom ou recherche de fichier ?
135
		if ($newName){
136
			$full_file_name = $file['name'].'_'.$pagedate.'_'.$this->getDate().'.'.$file['ext'];
137
			if($this->isSafeMode){
138
				$full_file_name = $path.'/'.$this->wiki->GetPageTag().'_'.$full_file_name;
139
			}else{
140
				$full_file_name = $path.'/'.$full_file_name;
141
			}
142
		}else{
143
			//recherche du fichier
144
			if($this->isSafeMode){
145
				//TODO Recherche dans le cas ou safe_mode=on
146
				$searchPattern = '`^'.$this->wiki->GetPageTag().'_'.$file['name'].'_\d{14}_\d{14}\.'.$file['ext'].'$`';
147
			}else{
148
				$searchPattern = '`^'.$file['name'].'_\d{14}_\d{14}\.'.$file['ext'].'$`';
149
			}
150
			$files = $this->searchFiles($searchPattern,$path);
151
 
152
			$unedate = 0;
153
			foreach ($files as $file){
154
				//recherche du fichier qui une datepage <= a la date de la page
155
				if($file['datepage']<=$pagedate){
156
					//puis qui a une dateupload la plus grande
157
					if ($file['dateupload']>$unedate){
158
						$theFile = $file;
159
						$unedate = $file['dateupload'];
160
					}
161
				}
162
			}
163
			if (is_array($theFile)){
164
				$full_file_name = $path.'/'.$theFile['realname'];
165
			}
166
		}
167
		return $full_file_name;
168
	}
169
	/**
170
	* Test si le fichier est une image
171
	*/
172
	function isPicture(){
173
		return preg_match("/.(".$this->attachConfig["ext_images"].")$/i",$this->file)==1;
174
	}
175
	/**
176
	* Test si le fichier est un fichier audio
177
	*/
178
	function isAudio(){
179
		return preg_match("/.(".$this->attachConfig["ext_audio"].")$/i",$this->file)==1;
180
	}
181
	/**
182
	* Test si le fichier est un fichier freemind mind map
183
	*/
184
	function isFreeMindMindMap(){
185
		return preg_match("/.(".$this->attachConfig["ext_freemind"].")$/i",$this->file)==1;
186
	}
187
 
188
	/**
189
	* Renvoie la date courante au format utilise par les fichiers
190
	*/
191
	function getDate(){
192
		return date('YmdHis');
193
	}
194
	/**
195
	* convertie une date yyyy-mm-dd hh:mm:ss au format yyyymmddhhmmss
196
	*/
197
	function convertDate($date){
198
		$date = str_replace(' ','', $date);
199
		$date = str_replace(':','', $date);
200
		return str_replace('-','', $date);
201
	}
202
	/**
203
	* Parse une date au format yyyymmddhhmmss et renvoie un tableau assiatif
204
	*/
205
	function parseDate($sDate){
206
		$pattern = '`^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$`';
207
		$res = '';
208
		if (preg_match($pattern, $sDate, $m)){
209
			//list(,$res['year'],$res['month'],$res['day'],$res['hour'],$res['min'],$res['sec'])=$m;
210
			$res = $m[1].'-'.$m[2].'-'.$m[3].' '.$m[4].':'.$m[5].':'.$m[6];
211
		}
212
		return ($res?$res:false);
213
	}
214
	/**
215
	* Decode un nom long de fichier
216
	*/
217
	function decodeLongFilename($filename){
218
		$afile = array();
219
		$afile['realname'] = basename($filename);
220
		$afile['size'] = filesize($filename);
221
		$afile['path'] = dirname($filename);
222
		if(preg_match('`^(.*)_(\d{14})_(\d{14})\.(.*)(trash\d{14})?$`', $afile['realname'], $m)){
223
			$afile['name'] = $m[1];
224
			//suppression du nom de la page si safe_mode=on
225
			if ($this->isSafeMode){
226
				$afile['name'] = preg_replace('`^('.$this->wiki->tag.')_(.*)$`i', '$2', $afile['name']);
227
			}
228
			$afile['datepage'] = $m[2];
229
			$afile['dateupload'] = $m[3];
230
			$afile['trashdate'] = preg_replace('`(.*)trash(\d{14})`', '$2', $m[4]);
231
			//suppression de trashxxxxxxxxxxxxxx eventuel
232
			$afile['ext'] = preg_replace('`^(.*)(trash\d{14})$`', '$1', $m[4]);
233
			$afile['ext'] = rtrim($afile['ext'],'_');
234
			//$afile['ext'] = rtrim($m[4],'_');
235
		}
236
		return $afile;
237
	}
238
	/**
239
	* Renvois un tableau des fichiers correspondant au pattern. Chaque element du tableau est un
240
	* tableau associatif contenant les informations sur le fichier
241
	*/
242
	function searchFiles($filepattern,$start_dir){
243
		$files_matched = array();
244
		$start_dir = rtrim($start_dir,'\/');
245
		$fh = opendir($start_dir);
246
		while (($file = readdir($fh)) !== false) {
247
			if (strcmp($file, '.')==0 || strcmp($file, '..')==0 || is_dir($file)) continue;
248
			if (preg_match($filepattern, $file)){
249
				$files_matched[] = $this->decodeLongFilename($start_dir.'/'.$file);
250
			}
251
		}
252
		return $files_matched;
253
	}
254
/******************************************************************************
255
*	FONCTIONS D'ATTACHEMENTS
256
*******************************************************************************/
257
	/**
258
	* Test les param�tres pass� � l'action
259
	*/
260
	function CheckParams(){
261
		//recuperation des parametres necessaire
262
		$this->file = $this->wiki->GetParameter("attachfile");
263
		if (empty($this->file)) $this->file = $this->wiki->GetParameter("file");
264
		$this->desc = $this->wiki->GetParameter("attachdesc");
265
		if (empty($this->desc)) $this->desc = $this->wiki->GetParameter("desc");
266
		$this->link = $this->wiki->GetParameter("attachlink");//url de lien - uniquement si c'est une image
267
		if (empty($this->link)) $this->link = $this->wiki->GetParameter("link");
268
		//test de validit� des parametres
269
		if (empty($this->file)){
270
			$this->attachErr = $this->wiki->Format("//action attach : param�tre **file** manquant//---");
271
		}
272
		if ($this->isPicture() && empty($this->desc)){
273
			$this->attachErr .= $this->wiki->Format("//action attach : param�tre **desc** obligatoire pour une image//---");
274
		}
275
		if ($this->wiki->GetParameter("class")) {
276
   		$array_classes = explode(" ", $this->wiki->GetParameter("class"));
277
   		foreach ($array_classes as $c) { $this->classes = $this->classes . "attach_" . $c . " "; }
278
   		$this->classes = trim($this->classes);
279
		}
280
	}
281
	/**
282
	* Affiche le fichier li� comme une image
283
	*/
284
	function showAsImage($fullFilename){
285
		//c'est une image : balise <IMG..../>
286
		$img =	"<img src=\"".$this->GetScriptPath().$fullFilename."\" ".
287
					"alt=\"".$this->desc.($this->link?"\nLien vers: $this->link":"")."\" />";
288
		//test si c'est une image sensible
289
		if(!empty($this->link)){
290
			//c'est une image sensible
291
			//test si le lien est un lien interwiki
292
			if (preg_match("/^([A-Z][A-Z,a-z]+)[:]([A-Z,a-z,0-9]*)$/s", $this->link, $matches))
293
			{  //modifie $link pour �tre un lien vers un autre wiki
294
				$this->link = $this->wiki->GetInterWikiUrl($matches[1], $matches[2]);
295
			}
296
			//calcule du lien
297
			$output = $this->wiki->Format('[['.$this->link." $this->file]]");
298
			$output = eregi_replace(">$this->file<",">$img<",$output);//insertion du tag <img...> dans le lien
299
		}else{
300
			//ce n'est pas une image sensible
301
			$output = $img;
302
		}
303
		$output = ($this->classes?"<span class=\"$this->classes\">$output</span>":$output);
304
		echo $output;
305
		$this->showUpdateLink();
306
	}
307
	/**
308
	* Affiche le fichier li� comme un lien
309
	*/
310
	function showAsLink($fullFilename){
311
		$url = $this->wiki->href("download",$this->wiki->GetPageTag(),"file=$this->file");
312
		echo '<a href="'.$url.'">'.($this->desc?$this->desc:$this->file)."</a>";
313
		$this->showUpdateLink();
314
	}
315
	// Affiche le fichier liee comme un fichier audio
316
	function showAsAudio($fullFilename){
317
		$output = "<object type=\"application/x-shockwave-flash\" data=\"tools/player/dewplayer.swf?son=$fullFilename&amp;bgcolor=FFFFFF\" width=\"200\"
318
 height=\"20\"><param name=\"movie\" value=\"tools/player/dewplayer.swf?son=$fullFilename&amp;bgcolor=FFFFFF\"/></object>";
319
		$output .= "<br></br>";
320
		$output .="[<a href=\"$fullFilename\">mp3</a>]";
321
		echo $output;
322
		$this->showUpdateLink();
323
	}
324
 
325
		// Affiche le fichier liee comme un fichier mind map  freemind
326
	function showAsFreeMindMindMap($fullFilename){
327
        $haut=$this->haut;
328
        $large=$this->large;
329
        if (!$haut) $haut = "650";
330
        if (!$large) $large = "100%";
331
        $mindmap_url = $this->wiki->href("download",$this->wiki->GetPageTag(),"file=$this->file");
332
     	$output = "<object type=\"application/x-shockwave-flash\" data=\"tools/freemind/visorFreemind.swf?initLoadFile=$fullFilename&amp;bgcolor=FFFFFF\" width=\"100%\"
333
 height=\"650\"><param name=\"mindmap\" value=\"tools/freemind/visorFreemind.swf?initLoadFile=$fullFilename&amp;bgcolor=FFFFFF\"/></object>";
334
		$output .= "<br></br>";
335
		$output .="[<a href=\"$fullFilename\">mm</a>]";
336
		echo $output;
337
		$this->showUpdateLink();
338
      }
339
 
340
 
341
	// End Paste
342
 
343
 
344
 
345
	/**
346
	* Affiche le lien de mise � jour
347
	*/
348
	function showUpdateLink(){
349
		echo	" <a href=\"".
350
				$this->wiki->href("upload",$this->wiki->GetPageTag(),"file=$this->file").
351
				"\" title='Mise � jour'>".$this->attachConfig['update_symbole']."</a>";
352
	}
353
	/**
354
	* Affiche un liens comme un fichier inexistant
355
	*/
356
	function showFileNotExits(){
357
		echo $this->file."<a href=\"".$this->wiki->href("upload",$this->wiki->GetPageTag(),"file=$this->file")."\">?</a>";
358
	}
359
	/**
360
	* Affiche l'attachement
361
	*/
362
	function doAttach(){
363
		$this->CheckParams();
364
		if ($this->attachErr) {
365
			echo $this->attachErr;
366
			return;
367
		}
368
		$fullFilename = $this->GetFullFilename();
369
		//test d'existance du fichier
370
		if((!file_exists($fullFilename))||($fullFilename=='')){
371
			$this->showFileNotExits();
372
			return;
373
		}
374
      //le fichier existe : affichage en fonction du type
375
      if($this->isPicture()){
376
      	$this->showAsImage($fullFilename);
377
      }else{
378
	      if($this->isAudio()){
379
	      		$this->showAsAudio($fullFilename);
380
	      	}
381
	      	else {
382
	      		  if($this->isFreeMindMindMap()){
383
			      	$this->showAsFreeMindMindMap($fullFilename);
384
	      		  }
385
	      		  else {
386
			      	$this->showAsLink($fullFilename);
387
	      		  }
388
	      	}
389
      }
390
	}
391
/******************************************************************************
392
*	FONTIONS D'UPLOAD DE FICHIERS
393
*******************************************************************************/
394
	/**
395
	* Traitement des uploads
396
	*/
397
	function doUpload(){
398
		$HasAccessWrite=$this->wiki->HasAccess("write");
399
		if ($HasAccessWrite){
400
         switch ($_SERVER["REQUEST_METHOD"]) {
401
         	case 'GET' : $this->showUploadForm(); break;
402
         	case 'POST': $this->performUpload(); break;
403
         	default : echo $this->wiki->Format("//Methode de requete invalide//---");
404
			}
405
		}else{
406
			echo $this->wiki->Format("//Vous n'avez pas l'acc�s en �criture � cette page//---");
407
			echo $this->wiki->Format("Retour � la page ".$this->wiki->GetPageTag());
408
		}
409
	}
410
	/**
411
	* Formulaire d'upload
412
	*/
413
	function showUploadForm(){
414
		echo $this->wiki->Format("====Formulaire d'envois de fichier====\n---");
415
		$this->file = $_GET['file'];
416
		echo 	$this->wiki->Format("**Envois du fichier $this->file :**\n")
417
				."<form enctype=\"multipart/form-data\" name=\"frmUpload\" method=\"POST\" action=\"".$_SERVER["PHP_SELF"]."\">\n"
418
				."	<input type=\"hidden\" name=\"wiki\" value=\"".$this->wiki->GetPageTag()."/upload\" />\n"
419
				."	<input TYPE=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"".$this->attachConfig['max_file_size']."\" />\n"
420
				."	<input type=\"hidden\" name=\"file\" value=\"$this->file\" />\n"
421
				."	<input type=\"file\" name=\"upFile\" size=\"50\" /><br />\n"
422
				."	<input type=\"submit\" value=\"Envoyer\" />\n"
423
				."</form>\n";
424
	}
425
	/**
426
	* Execute l'upload
427
	*/
428
	function performUpload(){
429
		$this->file = $_POST['file'];
430
 
431
		$destFile = $this->GetFullFilename(true);	//nom du fichier destination
432
		//test de la taille du fichier recu
433
		if($_FILES['upFile']['error']==0){
434
			$size = filesize($_FILES['upFile']['tmp_name']);
435
			if ($size > $this->attachConfig['max_file_size']){
436
				$_FILES['upFile']['error']=2;
437
			}
438
		}
439
		switch ($_FILES['upFile']['error']){
440
			case 0:
441
				$srcFile = $_FILES['upFile']['tmp_name'];
442
				if (move_uploaded_file($srcFile,$destFile)){
443
					chmod($destFile,0644);
444
					header("Location: ".$this->wiki->href("",$this->wiki->GetPageTag(),""));
445
				}else{
446
					echo $this->wiki->Format("//Erreur lors du d�placement du fichier temporaire//---");
447
				}
448
				break;
449
			case 1:
450
				echo $this->wiki->Format("//Le fichier t�l�charg� exc�de la taille de upload_max_filesize, configur� dans le php.ini.//---");
451
				break;
452
			case 2:
453
				echo $this->wiki->Format("//Le fichier t�l�charg� exc�de la taille de MAX_FILE_SIZE, qui a �t� sp�cifi�e dans le formulaire HTML.//---");
454
				break;
455
			case 3:
456
				echo $this->wiki->Format("//Le fichier n'a �t� que partiellement t�l�charg�.//---");
457
				break;
458
			case 4:
459
				echo $this->wiki->Format("//Aucun fichier n'a �t� t�l�charg�.//---");
460
				break;
461
		}
462
		echo $this->wiki->Format("Retour � la page ".$this->wiki->GetPageTag());
463
	}
464
/******************************************************************************
465
*	FUNCTIONS DE DOWNLOAD DE FICHIERS
466
*******************************************************************************/
467
	function doDownload(){
468
		$this->file = $_GET['file'];
469
		$fullFilename = $this->GetUploadPath().'/'.basename(realpath($this->file).$this->file);
470
//		$fullFilename = $this->GetUploadPath().'/'.$this->file;
471
		if(!file_exists($fullFilename)){
472
			$fullFilename = $this->GetFullFilename();
473
			$dlFilename = $this->file;
474
			$size = filesize($fullFilename);
475
		}else{
476
			$file = $this->decodeLongFilename($fullFilename);
477
			$size = $file['size'];
478
			$dlFilename =$file['name'].'.'.$file['ext'];
479
		}
480
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
481
        header("Content-type: application/force-download");
482
        header('Pragma: public');
483
        header("Pragma: no-cache");// HTTP/1.0
484
        header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
485
        header('Cache-Control: no-store, no-cache, must-revalidate'); // HTTP/1.1
486
        header('Cache-Control: pre-check=0, post-check=0, max-age=0'); // HTTP/1.1
487
        header('Content-Transfer-Encoding: none');
488
        header('Content-Type: application/octet-stream; name="' . $dlFilename . '"'); //This should work for the rest
489
        header('Content-Type: application/octetstream; name="' . $dlFilename . '"'); //This should work for IE & Opera
490
        header('Content-Type: application/download; name="' . $dlFilename . '"'); //This should work for IE & Opera
491
        header('Content-Disposition: attachment; filename="'.$dlFilename.'"');
492
        header("Content-Description: File Transfer");
493
        header("Content-length: $size".'bytes');
494
		readfile($fullFilename);
495
	}
496
/******************************************************************************
497
*	FONTIONS DU FILEMANAGER
498
*******************************************************************************/
499
	function doFileManager(){
500
		$do = $_GET['do']?$_GET['do']:'';
501
		switch ($do){
502
			case 'restore' :
503
				$this->fmRestore();
504
				$this->fmShow(true);
505
				break;
506
			case 'erase' :
507
				$this->fmErase();
508
				$this->fmShow(true);
509
				break;
510
			case 'del' :
511
				$this->fmDelete();
512
				$this->fmShow();
513
				break;
514
			case 'trash' :
515
				$this->fmShow(true); break;
516
			case 'emptytrash' :
517
				$this->fmEmptyTrash();	//pas de break car apres un emptytrash => retour au gestionnaire
518
			default :
519
				$this->fmShow();
520
		}
521
	}
522
	/**
523
	* Affiche la liste des fichiers
524
	*/
525
	function fmShow($trash=false){
526
		$fmTitlePage = $this->wiki->Format("====Gestion des fichiers attach�s �  la page ".$this->wiki->tag."====\n---");
527
		if($trash){
528
			//Avertissement
529
			$fmTitlePage .= '<div class="prev_alert">Les fichiers effac�s sur cette page le sont d�finitivement</div>';
530
			//Pied du tableau
531
			$url = $this->wiki->Link($this->wiki->tag,'filemanager','Gestion des fichiers');
532
      	$fmFootTable =	'	<tfoot>'."\n".
533
      						'		<tr>'."\n".
534
      						'			<td colspan="6">'.$url.'</td>'."\n";
535
			$url = $this->wiki->Link($this->wiki->tag,'filemanager&do=emptytrash','Vider la poubelle');
536
      	$fmFootTable.=	'			<td>'.$url.'</td>'."\n".
537
      						'		</tr>'."\n".
538
      						'	</tfoot>'."\n";
539
		}else{
540
			//pied du tableau
541
         $url = '<a href="'.$this->wiki->href('filemanager',$this->wiki->GetPageTag(),'do=trash').'" title="Poubelle">'.$this->attachConfig['fmTrash_symbole']."</a>";
542
      	$fmFootTable =	'	<tfoot>'."\n".
543
      						'		<tr>'."\n".
544
      						'			<td colspan="6">'.$url.'</td>'."\n".
545
      						'		</tr>'."\n".
546
      						'	</tfoot>'."\n";
547
		}
548
		//entete du tableau
549
		$fmHeadTable = '	<thead>'."\n".
550
							'		<tr>'."\n".
551
							'			<td>&nbsp;</td>'."\n".
552
							'			<td>Nom du fichier</td>'."\n".
553
							'			<td>Nom r�el du fichier</td>'."\n".
554
							'			<td>Taille</td>'."\n".
555
							'			<td>R�vision de la page</td>'."\n".
556
							'			<td>R�vison du fichier</td>'."\n";
557
		if($trash){
558
         $fmHeadTable.= '			<td>Suppression</td>'."\n";
559
		}
560
		$fmHeadTable.= '		</tr>'."\n".
561
							'	</thead>'."\n";
562
		//corps du tableau
563
		$files = $this->fmGetFiles($trash);
564
  		$files = $this->sortByNameRevFile($files);
565
 
566
		$fmBodyTable =	'	<tbody>'."\n";
567
		$i = 0;
568
		foreach ($files as $file){
569
			$i++;
570
			$color= ($i%2?"tableFMCol1":"tableFMCol2");
571
			//lien de suppression
572
			if ($trash){
573
				$url = $this->wiki->href('filemanager',$this->wiki->GetPageTag(),'do=erase&file='.$file['realname']);
574
			}else{
575
				$url = $this->wiki->href('filemanager',$this->wiki->GetPageTag(),'do=del&file='.$file['realname']);
576
			}
577
			$dellink = '<a href="'.$url.'" title="Supprimer">'.$this->attachConfig['fmDelete_symbole']."</a>";
578
			//lien de restauration
579
			$restlink = '';
580
			if ($trash){
581
				$url = $this->wiki->href('filemanager',$this->wiki->GetPageTag(),'do=restore&file='.$file['realname']);
582
				$restlink = '<a href="'.$url.'" title="Restaurer">'.$this->attachConfig['fmRestore_symbole']."</a>";
583
			}
584
 
585
			//lien pour downloader le fichier
586
			$url = $this->wiki->href("download",$this->wiki->GetPageTag(),"file=".$file['realname']);
587
			$dlLink = '<a href="'.$url.'">'.$file['name'].'.'.$file['ext']."</a>";
588
			$fmBodyTable .= 	'		<tr class="'.$color.'">'."\n".
589
									'			<td>'.$dellink.' '.$restlink.'</td>'."\n".
590
									'			<td>'.$dlLink.'</td>'."\n".
591
									'			<td>'.$file['realname'].'</td>'."\n".
592
									'			<td>'.$file['size'].'</td>'."\n".
593
									'			<td>'.$this->parseDate($file['datepage']).'</td>'."\n".
594
									'			<td>'.$this->parseDate($file['dateupload']).'</td>'."\n";
595
			if($trash){
596
         	$fmBodyTable.= '			<td>'.$this->parseDate($file['trashdate']).'</td>'."\n";
597
			}
598
			$fmBodyTable .= 	'		</tr>'."\n";
599
		}
600
		$fmBodyTable .= '	</tbody>'."\n";
601
		//pied de la page
602
		$fmFooterPage = "---\n-----\n[[".$this->wiki->tag." Retour � la page ".$this->wiki->tag."]]\n";
603
		//affichage
604
		echo $fmTitlePage."\n";
605
		echo '<table class="tableFM" border="0" cellspacing="0">'."\n".$fmHeadTable.$fmFootTable.$fmBodyTable.'</table>'."\n";
606
		echo $this->wiki->Format($fmFooterPage);
607
	}
608
	/**
609
	* Renvoie la liste des fichiers
610
	*/
611
	function fmGetFiles($trash=false){
612
		$path = $this->GetUploadPath();
613
		if($this->isSafeMode){
614
			$filePattern = '^'.$this->wiki->GetPageTag().'_.*_\d{14}_\d{14}\..*';
615
		}else{
616
			$filePattern = '^.*_\d{14}_\d{14}\..*';
617
		}
618
		if($trash){
619
			$filePattern .= 'trash\d{14}';
620
		}else{
621
			$filePattern .= '[^(trash\d{14})]';
622
		}
623
		return $this->searchFiles('`'.$filePattern.'$`', $path);
624
	}
625
	/**
626
	* Vide la poubelle
627
	*/
628
	function fmEmptyTrash(){
629
		$files = $this->fmGetFiles(true);
630
		foreach ($files as $file){
631
			$filename = $file['path'].'/'.$file['realname'];
632
			if(file_exists($filename)){
633
				unlink($filename);
634
			}
635
		}
636
	}
637
	/**
638
	* Effacement d'un fichier dans la poubelle
639
	*/
640
	function fmErase(){
641
		$path = $this->GetUploadPath();
642
		$filename = $path.'/'.($_GET['file']?$_GET['file']:'');
643
		if (file_exists($filename)){
644
			unlink($filename);
645
		}
646
	}
647
	/**
648
	* Met le fichier a la poubelle
649
	*/
650
	function fmDelete(){
651
		$path = $this->GetUploadPath();
652
		$filename = $path.'/'.($_GET['file']?$_GET['file']:'');
653
		if (file_exists($filename)){
654
			$trash = $filename.'trash'.$this->getDate();
655
			rename($filename, $trash);
656
		}
657
	}
658
	/**
659
	* Restauration d'un fichier mis a la poubelle
660
	*/
661
	function fmRestore(){
662
		$path = $this->GetUploadPath();
663
		$filename = $path.'/'.($_GET['file']?$_GET['file']:'');
664
		if (file_exists($filename)){
665
			$restFile = preg_replace('`^(.*\..*)trash\d{14}$`', '$1', $filename);
666
			rename($filename, $restFile);
667
		}
668
	}
669
	/**
670
	* Tri tu tableau liste des fichiers par nom puis par date de revision(upload) du fichier, ordre croissant
671
	*/
672
	function sortByNameRevFile($files){
673
		if (!function_exists('ByNameByRevFile')){
674
			function ByNameByRevFile($f1,$f2){
675
				$f1Name = $f1['name'].'.'.$f1['ext'];
676
				$f2Name = $f2['name'].'.'.$f2['ext'];
677
				$res = strcasecmp($f1Name, $f2Name);
678
				if($res==0){
679
					//si meme nom => compare la revision du fichier
680
					$res = strcasecmp($f1['dateupload'], $f2['dateupload']);
681
				}
682
				return $res;
683
			}
684
		}
685
		usort($files,'ByNameByRevFile');
686
		return $files;
687
	}
688
}
689
?>