class.tx_nG6_eid.php 20.5 KB
Newer Older
Jerome Mariette's avatar
Jerome Mariette committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php

// Inclusion des librairies nécessaires
require_once(PATH_t3lib.'class.t3lib_befunc.php');
require_once(PATH_t3lib.'stddb/tables.php');
require_once(t3lib_extMgm::extPath('cms','ext_tables.php'));
require_once(PATH_t3lib.'class.t3lib_db.php');

class tx_nG6_eid {


    /* Méthode principale */
    function main() {

    	/* connect to the databse */
    	tslib_eidtools::connectDB();
Jerome Mariette's avatar
Jerome Mariette committed
17
    	
Jerome Mariette's avatar
Jerome Mariette committed
18
19
20
21
22
23
24
25
    	$html = '';
    	$id = trim(t3lib_div::_GP('id'));
    	$type = trim(t3lib_div::_GP('type'));
    	
    	if ($type == 'project') {    	
	    	// First all project runs
	    	$p_runs = $this->get_project_runs($id);
			foreach($p_runs as $run_id => $run_values) {
Jerome Mariette's avatar
Jerome Mariette committed
26
				$html .= '<li class="jstree-closed" name=\''.str_replace(array('###SPAN_NAME###', '###SPAN_DATE###'), array($run_values["name"], strftime('%d-%m-%y',$run_values["date"])), trim(t3lib_div::_GP('short_run_printing'))).'\' id=\''.$run_id.'\'> <a href=\'#\'>'.str_replace(array('###SPAN_NAME###', '###SPAN_DATE###', '###SPAN_NB_SEQ###', '###SPAN_SPECIES###'), array($run_values["name"], strftime('%d-%m-%y',$run_values["date"]), $run_values["nb_sequences"], $run_values["species"]), trim(t3lib_div::_GP('run_printing'))).'</a></li>';
Jerome Mariette's avatar
Jerome Mariette committed
27
28
29
30
			}
	    	// Then all project analysis
	    	$p_analysis = $this->get_project_analysis($id);
			foreach($p_analysis as $analyse_id => $analyse_values) {  
Jerome Mariette's avatar
Jerome Mariette committed
31
				$html .= '<li name=\''.str_replace(array('###SPAN_NAME###'), array($analyse_values["name"]), trim(t3lib_div::_GP('analyse_printing'))).'\' id=\''.$analyse_id.'\'> <a href=\'#\'>'.str_replace(array('###SPAN_NAME###'), array($analyse_values["name"]), trim(t3lib_div::_GP('analyse_printing'))).'</a></li>';
Jerome Mariette's avatar
Jerome Mariette committed
32
33
34
35
36
			}
	    	print $html;
    	} else if ($type == 'run'){
			
			$r_analysis = $this->get_run_analysis($id);
Jerome Mariette's avatar
Jerome Mariette committed
37
			$run_info = $this->get_run($id);
Jerome Mariette's avatar
Jerome Mariette committed
38
			
Jerome Mariette's avatar
Jerome Mariette committed
39
			$html .= '<li name=\'run_'.$id.'_'.trim(t3lib_div::_GP("raw_data_label")).'\'  id=\'data_'.$id.'\'> <a href=\'#\'>'.trim(t3lib_div::_GP("raw_data_label")).'</a></li>';
Jerome Mariette's avatar
Jerome Mariette committed
40
			foreach($r_analysis as $analyse_id => $analyse_values) {   
Jerome Mariette's avatar
Jerome Mariette committed
41
				$html .= '<li name=\'run_'.$id.'_'.str_replace(array('###SPAN_NAME###'), array($analyse_values["name"]), trim(t3lib_div::_GP('analyse_printing'))).'\' id=\''.$analyse_id.'\'> <a href=\'#\'>'.str_replace(array('###SPAN_NAME###'), array($analyse_values["name"]), trim(t3lib_div::_GP('analyse_printing'))).'</a></li>';
Jerome Mariette's avatar
Jerome Mariette committed
42
43
			}
	    	print $html;
Jerome Mariette's avatar
Jerome Mariette committed
44
45
46
47
    	} else if ($type == 'download') {
			$ids = trim(t3lib_div::_GP('ids'));
			$data_folder = trim(t3lib_div::_GP('data_folder'));
			$tmp_folder = trim(t3lib_div::_GP('tmp_folder'));
48
			print $this->get_download_file($ids, $data_folder, $tmp_folder);
49
    	} else if ($type == 'getFileSize') {
50
51
52
                 $ids = trim(t3lib_div::_GP('ids'));
                 $data_folder = trim(t3lib_div::_GP('data_folder'));
                print $this->get_file_size($ids, $data_folder);
53
        } 
54
55

    }
56
57
58
59
60
61
    /**
     * for each $id, get the total size of all files in the corresponding folder 
     * @param	string		$ids			: names of run or analysis for which we want to get the sizes of the contained files
     * @param	string		$data_folder 	: root directory where the folders of the runs and analysis can be found
	 * @return	integer		$fileSize		: total size of the files
     */
62
63
	function get_file_size($ids, $data_folder) {
		$src_directories = Array();
Jerome Mariette's avatar
Jerome Mariette committed
64
		$vals = preg_split("/;/", $ids);
65
66
67
		$fileSize = 0;
		foreach($vals as $val) {
			if ($val != "undefined" && $val != "") {
Jerome Mariette's avatar
Jerome Mariette committed
68
				$id = preg_split("/_/", $val);
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
				if ($id[0] == "data") {
					$run = $this->get_run($id[1]);
					if (!in_array($data_folder.$run["directory"], $src_directories)) {
						$src_directories[] = $data_folder.$run["directory"];
						$fileSize += $this->getSize($data_folder.$run["directory"]);
					}
				} else if ($id[0] == "run") {
					$run = $this->get_run($id[1]);
					if (!in_array($data_folder.$run["directory"], $src_directories)) {
						$src_directories[] = $data_folder.$run["directory"];
						$fileSize += $this->getSize($data_folder.$run["directory"]);
					}
					foreach($this->get_run_analysis($id[1]) as $analyse_id => $analyse_values) {
						if (!in_array($data_folder.$analyse_values["directory"], $src_directories)) {
							$src_directories[] = $data_folder.$analyse_values["directory"];
							$fileSize += $this->getSize($data_folder.$analyse_values["directory"]);
						}
					}
				} else if ($id[0] == "analyse") {
					$analyse = $this->get_analyse($id[1]);
					if (!in_array($data_folder.$analyse["directory"], $src_directories)) {
						$src_directories[] = $data_folder.$analyse["directory"];
						$fileSize += $this->getSize($data_folder.$analyse["directory"]);
					}
				}
			}
		} 
	return $fileSize;
Jerome Mariette's avatar
Jerome Mariette committed
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
	/**
	 * get total size of files contained in $dir_src 
	 *
	 * @param	string		$dir_src: name of a file or directory for wich we want the size
	 * @return	integer		total size of the files
	 */
	function getSize($dir_src)	
	{			
		$filesSize = 0;
		##Do an exhaustive list of files from the directories list
		$iterator = new DirectoryIterator($dir_src);
                foreach ($iterator as $fileinfo)
               	{
               		$filename = $fileinfo->getFilename() ;
					$isNotRecursiveLink = ($filename!="." and $filename!="..") ;
                	$isNotCrap = ($isNotRecursiveLink and $filename!="index.html" and $filename!="run.cfg" );
                    if ($fileinfo->isDir() and $isNotCrap)
                    {  // get recursively subdirectories files
                                        $dirpathname =  $this->dirSlash($fileinfo->getPathname());
                                        // then actualize the list of file to zip
                                        $filesSize += $this->getSize( $dirpathname) ;
                     }
                     elseif ($fileinfo->isFile() and $isNotCrap)
                     {  
						$filesSize = $fileinfo->getSize();
						//echo "Nom du fichier : " . $filename . "<br />";
						//echo "taille du fichiers : $fileSize <br />";	
						//echo "taille des fichiers : $filesSize <br />";	
                      }
                }

		return $filesSize; 
	} 	 
Jerome Mariette's avatar
Jerome Mariette committed
131
132
133
134
	
	function get_download_file($ids, $data_folder, $tmp_folder) {
		$src_directories = Array();
		$dest_directories = Array();
Jerome Mariette's avatar
Jerome Mariette committed
135
		$vals = preg_split("/;/", $ids);
Jerome Mariette's avatar
Jerome Mariette committed
136
		$temporary_dir = $this->createRandomArchiveName($tmp_folder);
137
		$charToBeSubstituted = array(' ','/'); //characters to be replaced in the names of the run, the analyses....
Jerome Mariette's avatar
Jerome Mariette committed
138
139
		foreach($vals as $val) {
			if ($val != "undefined" && $val != "") {
Jerome Mariette's avatar
Jerome Mariette committed
140
				$id = preg_split("/_/", $val);
Jerome Mariette's avatar
Jerome Mariette committed
141
142
143
144
				if ($id[0] == "data") {
					$run = $this->get_run($id[1]);
					if (!in_array($data_folder.$run["directory"], $src_directories)) {
						$src_directories[] = $data_folder.$run["directory"];
145
						$dest_directories[] = $temporary_dir."/Project_".str_replace($charToBeSubstituted, '_', $run["project_name"]).'.'.$run["project_id"]."/Run_".str_replace($charToBeSubstituted, '_', $run["name"]).".".$id[1]."/RawData";
Jerome Mariette's avatar
Jerome Mariette committed
146
147
148
149
150
					}
				} else if ($id[0] == "run") {
					$run = $this->get_run($id[1]);
					if (!in_array($data_folder.$run["directory"], $src_directories)) {
						$src_directories[] = $data_folder.$run["directory"];
151
						$dest_directories[] = $temporary_dir."/Project_".str_replace($charToBeSubstituted, '_', $run["project_name"]).'.'.$run["project_id"]."/Run_".str_replace($charToBeSubstituted, '_', $run["name"]).".".$id[1]."/RawData";
Jerome Mariette's avatar
Jerome Mariette committed
152
153
154
155
					}
					foreach($this->get_run_analysis($id[1]) as $analyse_id => $analyse_values) {
						if (!in_array($data_folder.$analyse_values["directory"], $src_directories)) {
							$src_directories[] = $data_folder.$analyse_values["directory"];
Jerome Mariette's avatar
Jerome Mariette committed
156
							$aid = preg_split("/_/", $analyse_id);
157
							$dest_directories[] = $temporary_dir."/Project_".str_replace($charToBeSubstituted, '_', $run["project_name"]).'.'.$run["project_id"]."/Run_".str_replace($charToBeSubstituted, '_', $run["name"]).".".$id[1]."/Analyse_".str_replace($charToBeSubstituted, '_', $analyse_values["name"]).".".$aid;
Jerome Mariette's avatar
Jerome Mariette committed
158
159
160
161
162
163
164
165
						}
					}
				} else if ($id[0] == "analyse") {
					$analyse = $this->get_analyse($id[1]);
					if (!in_array($data_folder.$analyse["directory"], $src_directories)) {
						$src_directories[] = $data_folder.$analyse["directory"];
						// If it's a project analyse
						if ($analyse["run_id"] == 'None') {
166
							$dest_directories[] = $temporary_dir."/Project_".str_replace($charToBeSubstituted, '_', $analyse["project_name"]).".".$analyse["project_id"]."/Project_analyses/".str_replace($charToBeSubstituted, '_', $analyse["name"]).".".$id[1];
Jerome Mariette's avatar
Jerome Mariette committed
167
						} else {
168
							$dest_directories[] = $temporary_dir."/Project_".str_replace($charToBeSubstituted, '_', $analyse["project_name"]).".".$analyse["project_id"]."/Run_".str_replace($charToBeSubstituted, '_', $analyse["run_name"]).".".$analyse["run_id"]."/Analyse_".str_replace($charToBeSubstituted, '_', $analyse["name"]).".".$id[1];
Jerome Mariette's avatar
Jerome Mariette committed
169
170
171
172
173
						}
					}
				}
			}
		} 
Jerome Mariette's avatar
Jerome Mariette committed
174
		return $this->archiveZip($src_directories, $dest_directories, $temporary_dir, 'tar.gz');
Jerome Mariette's avatar
Jerome Mariette committed
175
176
    }

Jerome Mariette's avatar
Jerome Mariette committed
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
    function get_run_analysis($run_id) {

		$results = array();
		$queryParts = array( 
			'SELECT' => 'tx_nG6_analyze.uid AS analyze_id,'. 
						'tx_nG6_analyze.directory AS analyze_directory,'. 
						'tx_nG6_analyze.name AS analyze_name, '.
						'tx_nG6_analyze.date AS analyze_date, '.
						'tx_nG6_analyze.description AS analyze_description',
			'FROM' => 'tx_nG6_run_analyze JOIN tx_nG6_analyze ON '.
					  'tx_nG6_analyze.uid=tx_nG6_run_analyze.analyze_id',
			'WHERE' => 'tx_nG6_run_analyze.run_id='.$run_id,
			'GROUPBY' => '',
			'ORDERBY' => 'tx_nG6_analyze.name',
			'LIMIT' => ''
		);

		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
		{
			$analyze_id = $row['analyze_id'];
			if (! isset ($results['analyse_'.$analyze_id]) )
			{				
				$results['analyse_'.$analyze_id] = array( 
					'directory'					=> $row['analyze_directory'], 
					'name' 						=> $row['analyze_name'],
					'date'						=> $row['analyze_date'],
					'description' 				=> $row['analyse_description']				
				);
			}						
		}
		return $results;
    }
Jerome Mariette's avatar
Jerome Mariette committed
210

Jerome Mariette's avatar
Jerome Mariette committed
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
    function get_project_analysis($project_id) {

		$results = array();
		$queryParts = array( 
			'SELECT' => 'tx_nG6_analyze.uid AS analyze_id,'. 
						'tx_nG6_analyze.directory AS analyze_directory,'. 
						'tx_nG6_analyze.name AS analyze_name, '.
						'tx_nG6_analyze.date AS analyze_date, '.
						'tx_nG6_analyze.description AS analyze_description',
			'FROM' => 'tx_nG6_project_analyze JOIN tx_nG6_analyze ON '.
					  'tx_nG6_analyze.uid=tx_nG6_project_analyze.analyze_id',
			'WHERE' => 'tx_nG6_project_analyze.project_id='.$project_id,
			'GROUPBY' => '',
			'ORDERBY' => 'tx_nG6_analyze.name',
			'LIMIT' => ''
		);

		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
		{
			$analyze_id = $row['analyze_id'];
			if (! isset ($results['analyse_'.$analyze_id]) )
			{				
				$results['analyse_'.$analyze_id] = array( 
					'directory'					=> $row['analyze_directory'], 
					'name' 						=> $row['analyze_name'],
					'date'						=> $row['analyze_date'],
					'description' 				=> $row['analyse_description']				
				);
			}						
		}
		return $results;
    }
Jerome Mariette's avatar
Jerome Mariette committed
244
245
246
247
248

    function get_run($run_id) {
		
		$result = array();
		$queryParts = Array( 
249
250
251
			'SELECT' => 'tx_nG6_project.uid AS project_id,'.
						'tx_nG6_project.name AS project_name,'.
						'tx_nG6_run.uid AS run_id,'.
Jerome Mariette's avatar
Jerome Mariette committed
252
253
254
255
256
					 	'tx_nG6_run.directory AS run_directory,'.
						'tx_nG6_run.description AS run_description, '.
						'tx_nG6_run.name AS run_name, '.
						'tx_nG6_run.nb_sequences, '.
						'tx_nG6_run.date AS run_date',
257
258
			'FROM' => 'tx_nG6_project INNER JOIN ( tx_nG6_project_run INNER JOIN tx_nG6_run ON tx_nG6_project_run.run_id=tx_nG6_run.uid '.
					  ' ) ON tx_nG6_project.uid=tx_nG6_project_run.project_id',
Jerome Mariette's avatar
Jerome Mariette committed
259
260
261
262
263
264
265
266
267
268
269
270
271
			'WHERE' => 'tx_nG6_run.uid='.$run_id,
			'GROUPBY' => '',
			'ORDERBY' => 'tx_nG6_run.date DESC',
			'LIMIT' 	=> ''
		); 

		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
		{
			$run_id = $row['run_id'];
			if (! isset ($results['run_'.$run_id]) )
			{				
				$result = array( 
272
273
					'project_id'				=> $row['project_id'], 
					'project_name'				=> $row['project_name'], 
Jerome Mariette's avatar
Jerome Mariette committed
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
					'directory'					=> $row['run_directory'], 
					'name' 						=> $row['run_name'],
					'nb_sequences' 				=> $row['nb_sequences'],
					'date'						=> $row['run_date'],
					'description' 				=> $row['run_description']				
				);
			}						
		}
		return $result;
    }

    function get_analyse($analyse_id) {
		
		$queryParts = array( 
			'SELECT' => 'tx_nG6_project.uid AS project_id,'. 
						'tx_nG6_project.name AS project_name,'. 
						'tx_nG6_analyze.uid AS analyze_id,'. 
						'tx_nG6_analyze.directory AS analyze_directory,'. 
						'tx_nG6_analyze.name AS analyze_name, '.
						'tx_nG6_analyze.date AS analyze_date, '.
						'tx_nG6_analyze.description AS analyze_description ',
			'FROM' => 'tx_nG6_project INNER JOIN (tx_nG6_project_analyze INNER JOIN tx_nG6_analyze ON ' .
					  'tx_nG6_analyze.uid=tx_nG6_project_analyze.analyze_id) ON tx_nG6_project.uid = tx_nG6_project_analyze.project_id ',
			'WHERE' => 'tx_nG6_analyze.uid='.$analyse_id,
			'GROUPBY' => '',
			'ORDERBY' => '',
			'LIMIT' => ''
		);
		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		
		// If it's a project analyse
		if ($GLOBALS['TYPO3_DB']->sql_num_rows($res) > 0) {
			$result = array();
			while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
			{	
				$result = array( 
					'project_name'				=> $row['project_name'], 
					'project_id'				=> $row['project_id'],
					'run_name'					=> 'None', 
					'run_id'					=> 'None',					 
					'directory'					=> $row['analyze_directory'], 
					'name' 						=> $row['analyze_name'],
					'date'						=> $row['analyze_date'],
					'description' 				=> $row['analyze_description']				
				);				
			}
			return $result;
		} else {
			$queryParts = array( 
				'SELECT' => 'tx_nG6_project.name AS project_name,'.
							'tx_nG6_project.uid AS project_id,'.
							'tx_nG6_run.uid AS run_id,'. 
							'tx_nG6_run.name AS run_name,'.
							'tx_nG6_analyze.uid AS analyze_id,'. 
							'tx_nG6_analyze.directory AS analyze_directory,'. 
							'tx_nG6_analyze.name AS analyze_name, '.
							'tx_nG6_analyze.date AS analyze_date, '.
							'tx_nG6_analyze.description AS analyze_description',
				'FROM' => 'tx_nG6_project INNER JOIN ( tx_nG6_project_run INNER JOIN ( tx_nG6_run INNER JOIN ( tx_nG6_run_analyze INNER JOIN '.
						  'tx_nG6_analyze ON tx_nG6_analyze.uid = tx_nG6_run_analyze.analyze_id) ON tx_nG6_run_analyze.run_id = tx_nG6_run.uid '.
						  ') ON tx_nG6_run.uid = tx_nG6_project_run.run_id) ON tx_nG6_project_run.project_id = tx_nG6_project.uid ',
				'WHERE' => 'tx_nG6_analyze.uid='.$analyse_id,
				'GROUPBY' => '',
				'ORDERBY' => 'tx_nG6_analyze.name',
				'LIMIT' => ''
			);
			$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
			$result = array();
			while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
			{	
				$result = array( 
					'project_name'				=> $row['project_name'], 
					'project_id'				=> $row['project_id'],
					'run_name'					=> $row['run_name'], 
					'run_id'					=> $row['run_id'], 			 
					'directory'					=> $row['analyze_directory'], 
					'name' 						=> $row['analyze_name'],
					'date'						=> $row['analyze_date'],
					'description' 				=> $row['analyze_description']				
				);				
			}
			return $result;
		}
    }

Jerome Mariette's avatar
Jerome Mariette committed
359
360
361
362
363
364
    function get_project_runs($project_id) {
		
		$results = array();
		$queryParts = Array( 
			'SELECT' => 'tx_nG6_run.uid AS run_id,'.
					 	'tx_nG6_run.directory AS run_directory,'.
365
					 	'tx_nG6_run.species AS run_species,'.
Jerome Mariette's avatar
Jerome Mariette committed
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
						'tx_nG6_run.description AS run_description, '.
						'tx_nG6_run.name AS run_name, '.
						'tx_nG6_run.nb_sequences, '.
						'tx_nG6_run.date AS run_date',
			'FROM' => 'tx_nG6_project_run JOIN tx_nG6_run ON '.
					  'tx_nG6_run.uid=tx_nG6_project_run.run_id',
			'WHERE' => 'tx_nG6_project_run.project_id='.$project_id,
			'GROUPBY' => '',
			'ORDERBY' => 'tx_nG6_run.date DESC',
			'LIMIT' 	=> ''
		); 

		$res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts);
		while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))
		{
			$run_id = $row['run_id'];
			if (! isset ($results['run_'.$run_id]) )
			{				
				$results['run_'.$run_id] = array( 
					'directory'					=> $row['run_directory'], 
					'name' 						=> $row['run_name'],
387
					'species' 					=> $row['run_species'],
Jerome Mariette's avatar
Jerome Mariette committed
388
389
390
391
392
393
394
395
396
397
					'nb_sequences' 				=> $row['nb_sequences'],
					'date'						=> $row['run_date'],
					'description' 				=> $row['run_description']				
				);
			}						
		}
		return $results;
    }

	//Return archive's filepath.random_name
Jerome Mariette's avatar
Jerome Mariette committed
398
	function createRandomArchiveName ($tmp_dir) {
Jerome Mariette's avatar
Jerome Mariette committed
399
		$randomKey = $this->createRandomKey(10);
Jerome Mariette's avatar
Jerome Mariette committed
400
		$pathname = $tmp_dir.'/archiveNG6_'.$randomKey;
Jerome Mariette's avatar
Jerome Mariette committed
401
402
		// 1 chance over 49**10 is still a risk
		while( file_exists($pathname) )
Jerome Mariette's avatar
Jerome Mariette committed
403
			$pathname = $tmp_dir.'/archiveNG6_'.$randomKey;
Jerome Mariette's avatar
Jerome Mariette committed
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
		return $pathname;
	}

	//Create a random string for a given length ($amount) 
   	function createRandomKey($amount) {
		// From http://snipplr.com/view/6347/php-random-key-generator/
		$keyset = "abcdefghijklmABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
   		$randkey = "";
   		for ($i=0; $i<$amount; $i++)
	   		$randkey .= substr($keyset, rand(0, strlen($keyset)-1), 1);
   		return $randkey;
   	}

	//Add a / to a directory pathname 
	function dirSlash($dir){
		if (substr( $dir, -1) == "/")
			return $dir;
		return $dir."/";
	}	

Jerome Mariette's avatar
Jerome Mariette committed
424
425
426
427
428
429
430
431
432
433
	function force_download($name, $path) {
		header('Content-Type: application/octet-stream');
		header('Content-disposition: attachment; filename='. $name);
		header('Pragma: no-cache');
		header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
		header('Expires: 0');
		readfile($path);
		exit();
	}

Jerome Mariette's avatar
Jerome Mariette committed
434
435
436
437
438
	/**
	 * Archivate data from $directories_src to an archive (structurated as $directories_dest)
	 *
	 * @param	array			$directories_src: list of directories to archivate
	 * @param	array			$directories_dest: list of directories to archivate
Jerome Mariette's avatar
Jerome Mariette committed
439
	 * @param   string          $method : tar.gz
Jerome Mariette's avatar
Jerome Mariette committed
440
441
	 * @return	string		HTML link to the archive
	 */
Jerome Mariette's avatar
Jerome Mariette committed
442
443
	function archiveZip($directories_src , $directories_dest, $temporary_dir, $method)	
	{			
Jerome Mariette's avatar
Jerome Mariette committed
444
445
446
		##Do an exhaustive list of files from the directories list
		foreach($directories_src as $index => $dir_src)
		{
Jerome Mariette's avatar
Jerome Mariette committed
447
			$this->copyDirectoryAndList($dir_src, $directories_dest[$index]);		
Jerome Mariette's avatar
Jerome Mariette committed
448
		}
Jerome Mariette's avatar
Jerome Mariette committed
449
450
451
452
		$normal_cwd = getcwd();
		$temporary_dir = $this->dirSlash($temporary_dir);
		chdir(dirname($temporary_dir));
		
Jerome Mariette's avatar
Jerome Mariette committed
453
		//write out zipped file 
Jerome Mariette's avatar
Jerome Mariette committed
454
		switch($method) 
Jerome Mariette's avatar
Jerome Mariette committed
455
456
		{
			case 'tar.gz': //system call
Jerome Mariette's avatar
Jerome Mariette committed
457
				$archive_name = basename($temporary_dir);			
Jerome Mariette's avatar
Jerome Mariette committed
458
459
460
461
462
463
464
465
466
				$outfile = $archive_name.".tar";
				system("tar -Pcf ".$outfile." ".$archive_name." --remove-files");  
				system("gzip ".$outfile);	
				$outfile .= '.gz';	
				system("rm -rf ".$archive_name); 
				break;
		}
		//restore cwd 
		chdir($normal_cwd);
Jerome Mariette's avatar
Jerome Mariette committed
467
		return $outfile; 
Jerome Mariette's avatar
Jerome Mariette committed
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
	} 	 

	/**
	 * Recursively copy the content of $dir_src into $dir_dest
	 *
	 * @param	string		$dir_src: name of the source directory
	 * @param	string		$dir_dest: name of the destination directory
	 * @return	None
	 */
	function copyDirectoryAndList($dir_src, $dir_dest)	
	{
		$zip = Array();
		$dir_dest = $this->dirSlash($dir_dest);
		$dir_src = $this->dirSlash($dir_src);	 
		if (! file_exists($dir_src)) {
			return $zip; //nothing to do
		}
			
		//Recursively build destination structure
		if (! file_exists($dir_dest))
			mkdir($dir_dest, 0755, true);
		
		$iterator = new DirectoryIterator($dir_src);
		foreach ($iterator as $fileinfo) 
		{ 			
			$filename = $fileinfo->getFilename() ;			
			$isNotRecursiveLink = ($filename!="." and $filename!="..") ;	
			$isNotCrap = ($isNotRecursiveLink and $filename!="index.html" and $filename!="run.cfg" );
			if ($fileinfo->isDir() and $isNotCrap)
			{  // get recursively subdirectories files
			
				$dirpathname =  $this->dirSlash($fileinfo->getPathname());
				// then actualize the list of file to zip
				$subzip = $this->copyDirectoryAndList( $dirpathname , $dir_dest.$fileinfo->getFilename() ) ;
				$zip = array_merge  ( $zip , $subzip );
			}		
			elseif ($fileinfo->isFile() and $isNotCrap) 
			{  // copy file in new structure				
				$pathname_src = $fileinfo->getPathname(); 
		    	$pathname_dest = $dir_dest . $fileinfo->getFilename(); 
		    	copy( $pathname_src , $pathname_dest );	
	    	
		    	// add to zip  
		    	$zip[] = $pathname_dest;
			}    
		} 

		return $zip; //Done		
	}
    
}

if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/nG6/class.tx_nG6_eid.php']) {
    include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/nG6/class.tx_nG6_eid.php']);
}


// instanciation
$SOBE = t3lib_div::makeInstance('tx_nG6_eid');
$SOBE->main();

529
?>