class.tx_nG6_eid.php 17.3 KB
Newer Older
Jerome Mariette's avatar
Jerome Mariette committed
1
<?php
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/***************************************************************
*  Copyright notice
*
*  (c) 2009 PF bioinformatique de Toulouse <>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project 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.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script 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.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
Jerome Mariette's avatar
Jerome Mariette committed
24
25
26
27
28

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');
29
30
31
require_once(t3lib_extMgm::extPath('nG6').'/lib/class.tx_nG6_db.php');
require_once(t3lib_extMgm::extPath('nG6').'/lib/class.tx_nG6_utils.php');
require_once(t3lib_extMgm::extPath('nG6').'/lib/class.tx_nG6_process.php');
Jerome Mariette's avatar
Jerome Mariette committed
32

33
34
35
36
37
38
/**
 * Class 'tx_nG6_eid' for the 'nG6' extension.
 * This class is in charge of all communication between the client and the server using AJAX
 *
 * @author	PF bioinformatique de Toulouse <>
 */
Jerome Mariette's avatar
Jerome Mariette committed
39
40
class tx_nG6_eid {

41
42
43
    /**
     * The main function of the tx_nG6_eid, this function is the one called when using ajax
     */
Jerome Mariette's avatar
Jerome Mariette committed
44
45
    function main() {

46
    	// Connect to the databse
Jerome Mariette's avatar
Jerome Mariette committed
47
48
    	tslib_eidtools::connectDB();
    	$type = trim(t3lib_div::_GP('type'));
49
    	$user_group = trim(t3lib_div::_GP('user_group'));
Jerome Mariette's avatar
Jerome Mariette committed
50
    	
51
52
53
54
    	// If the type of data requested is project
    	if ($type == 'project') {
    		$project_id = trim(t3lib_div::_GP('id'));
	    	// Select all runs linked to the project
Jerome Mariette's avatar
Jerome Mariette committed
55
	    	$project_runs = tx_nG6_db::get_project_runs($user_group, $project_id, 'tx_nG6_run.date DESC');
56
57
58
	    	// for each run
	    	$html_tree = '';
			foreach($project_runs as $run_id => $run_values) {
Jerome Mariette's avatar
Jerome Mariette committed
59
				$html_tree .= '<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('run_short_description'))).'\' 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_description'))).'</a></li>';
Jerome Mariette's avatar
Jerome Mariette committed
60
61
			}
	    	// Then all project analysis
Jerome Mariette's avatar
Jerome Mariette committed
62
	    	$project_analysis = tx_nG6_db::get_project_analysis($user_group, $project_id, 'tx_nG6_analyze.name');
63
64
			foreach($project_analysis as $analyse_id => $analyse_values) {  
				$html_tree .= '<li name=\''.str_replace(array('###SPAN_NAME###'), array($analyse_values["name"]), trim(t3lib_div::_GP('analyse_description'))).'\' id=\''.$analyse_id.'\'> <a href=\'#\'>'.str_replace(array('###SPAN_NAME###'), array($analyse_values["name"]), trim(t3lib_div::_GP('analyse_description'))).'</a></li>';
Jerome Mariette's avatar
Jerome Mariette committed
65
			}
66
67
			// Return the html_tree
	    	print $html_tree;
Jerome Mariette's avatar
Jerome Mariette committed
68
	    	
69
	    // If the type of data requested is run
Jerome Mariette's avatar
Jerome Mariette committed
70
    	} else if ($type == 'run'){
71
72
			$run_id = trim(t3lib_div::_GP('id'));
			// Select all analysis linked to the run and the run information
Jerome Mariette's avatar
Jerome Mariette committed
73
74
			$run_analysis = tx_nG6_db::get_run_analysis($user_group, $run_id, 'tx_nG6_analyze.name');
			$run_info = tx_nG6_db::select_run($run_id);
75
76
77
78
79
	    	// for each analysis
	    	$html_tree = '';
			$html_tree .= '<li name=\'run_'.$run_id.'_'.trim(t3lib_div::_GP("raw_data_description")).'\'  id=\'data_'.$run_id.'\'> <a href=\'#\'>'.trim(t3lib_div::_GP("raw_data_description")).'</a></li>';
			foreach($run_analysis as $analyse_id => $analyse_values) {   
				$html_tree .= '<li name=\'run_'.$run_id.'_'.str_replace(array('###SPAN_NAME###'), array($analyse_values["name"]), trim(t3lib_div::_GP('analyse_description'))).'\' id=\''.$analyse_id.'\'> <a href=\'#\'>'.str_replace(array('###SPAN_NAME###'), array($analyse_values["name"]), trim(t3lib_div::_GP('analyse_description'))).'</a></li>';
Jerome Mariette's avatar
Jerome Mariette committed
80
			}
81
			// Return the html_tree
Jerome Mariette's avatar
Jerome Mariette committed
82
			print $html_tree;
83
84
	    	
	    // If downloading data is requested
Jerome Mariette's avatar
Jerome Mariette committed
85
    	} else if ($type == 'download') {
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
    		$format = trim(t3lib_div::_GP('format'));
    		// If link in home directory
    		if ($format == 'home_link') {
				$ids = trim(t3lib_div::_GP('ids'));
				$data_folder = trim(t3lib_div::_GP('data_folder'));
				$user_login = trim(t3lib_div::_GP('user_login'));
				$user_pwd = trim(t3lib_div::_GP('user_pwd'));
				$user_directory = trim(t3lib_div::_GP('user_directory'));
				$retcode = $this->create_symbolic_links($user_group, $user_login, $user_pwd, $ids, $data_folder, $user_directory);
				print $retcode;
				
			// If a .tar.gz file
    		} else {
				$ids = trim(t3lib_div::_GP('ids'));
				$data_folder = trim(t3lib_div::_GP('data_folder'));
				$tmp_folder = trim(t3lib_div::_GP('tmp_folder'));
				$user_email = trim(t3lib_div::_GP('user_email'));
				$title_email = trim(t3lib_div::_GP('title_email'));
				$msg_email = trim(t3lib_div::_GP('msg_email'));
				$archive_info = $this->create_archive_script($user_group, $ids, $data_folder, $tmp_folder, "tar.gz", $user_email, $title_email, $msg_email); 
				$process = new Process("tcsh ".$archive_info[0]);
			    print "pid=".$process->getPid()."&archive_path=".$archive_info[1];
    		}
    		
    	// If asked to check a job status
    	} else if($type == 'check_status') {
    		$pid = trim(t3lib_div::_GP('pid'));
    		$archive_path = trim(t3lib_div::_GP('archive_path'));
    		$process = new Process();
    		$process->setPid($pid);
    		// If the job is done
    		if ($process->status()) {
    			print "pid=0&archive_path=".$archive_path;
    		// If the job is still runing
    		} else {
    			print "pid=".$process->getPid()."&archive_path=".$archive_path;
    		}
Jerome Mariette's avatar
Jerome Mariette committed
123
124
125
126
127
            
    	// If asked to hide a {project/run}
    	} else if ($type == 'hide') {
    		
    		$ids = trim(t3lib_div::_GP('ids'));
128
    		// {project/run}_id to array (1;2;3;...) -> [1;2;3;...]
Jerome Mariette's avatar
Jerome Mariette committed
129
    		$tab_ids = explode(";",$ids);
130
    		$hide_level = trim(t3lib_div::_GP('hide_level'));
Jerome Mariette's avatar
Jerome Mariette committed
131
    		
132
133
134
135
136
137
138
139
140
141
142
143
    		if ($hide_level == 'project') {
	    		foreach($tab_ids as $id => $value) {
		    		tx_nG6_db::hide_project($value, $user_group);
	    		}
    		} else if ($hide_level == 'run') {
	    		foreach($tab_ids as $id => $value) {
		    		tx_nG6_db::hide_run($value, $user_group);
	    		}
    		} else if ($hide_level == 'analysis') {
	    		foreach($tab_ids as $id => $value) {
		    		tx_nG6_db::hide_analysis($value, $user_group);
	    		}
Jerome Mariette's avatar
Jerome Mariette committed
144
    		}
145

Jerome Mariette's avatar
Jerome Mariette committed
146
147
148
149
        // If asked to unhide a {project/run}
    	} else if ($type == 'unhide') {
    		    		
    		$ids = trim(t3lib_div::_GP('ids'));
150
    		// {project/run}_id to array (1;2;3;...) -> [1;2;3;...]
Jerome Mariette's avatar
Jerome Mariette committed
151
    		$tab_ids = explode(";",$ids);
152
153
154
155
156
157
158
159
160
161
162
163
164
165
    		$unhide_level = trim(t3lib_div::_GP('unhide_level'));

    		if ($unhide_level == 'project') {
	    		foreach($tab_ids as $id => $value) {
		    		tx_nG6_db::unhide_project($value, $user_group);
	    		}
    		} else if ($unhide_level == 'run') {
	    		foreach($tab_ids as $id => $value) {
		    		tx_nG6_db::unhide_run($value, $user_group);
	    		}
    		} else if ($unhide_level == 'analysis') {
	    		foreach($tab_ids as $id => $value) {
		    		tx_nG6_db::unhide_analysis($value, $user_group);
	    		}
Jerome Mariette's avatar
Jerome Mariette committed
166
    		}
167
168
169
    		
    	// If asked to delete a {project/run/analyze}	
    	} else if ($type == 'delete') {
170

171
172
173
174
175
176
177
    		$ids = trim(t3lib_div::_GP('ids'));
    		// {project/run}_id to array (1;2;3;...) -> [1;2;3;...]
    		$tab_ids = explode(";",$ids);
    		$delete_level = trim(t3lib_div::_GP('del_level'));
    		$data_folder = trim(t3lib_div::_GP('data_folder'));
			$user_login = trim(t3lib_div::_GP('user_login'));
			$user_pwd = trim(t3lib_div::_GP('user_pwd'));
178

179
180
181
182
183
184
185
186
187
188
    		$res = 0;
    		// check login and password
    		if (isset($user_login) && isset($user_pwd) && $user_login != 'undefined' && $user_pwd != 'undefined') {
    			$res = 0;
		    	$connection = ssh2_connect('127.0.0.1', 22);
				if (!$connection) { $res = 3; }
				if (!ssh2_auth_password($connection, $user_login, $user_pwd)) { $res=2;}
    		} else {
    			$res = 3;
    		}
Jerome Mariette's avatar
Jerome Mariette committed
189
			
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
			if ($res == 0) {
	    		if ($delete_level == 'project') {
		    		foreach($tab_ids as $id => $value) {
			    		$res = tx_nG6_db::delete_project($user_group, $value, $user_login, $user_pwd, $data_folder);
			    		if ($res != 0) { break; }
		    		}
	    		} else if ($delete_level == 'run') {
		    		foreach($tab_ids as $id => $value) {
			    		$res = tx_nG6_db::delete_run($user_group, $value, $user_login, $user_pwd, $data_folder);
			    		if ($res != 0) { break; }
		    		}
	    		} else if ($delete_level == 'analysis') {
		    		foreach($tab_ids as $id => $value) {
			    		$res = tx_nG6_db::delete_analysis($value, $user_login, $user_pwd, $data_folder);
			    		if ($res != 0) { break; }
		    		}
	    		}
Jerome Mariette's avatar
Jerome Mariette committed
207
			}
208
    		print $res;
Jerome Mariette's avatar
Jerome Mariette committed
209

210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
        // If asked to publish a project
    	} else if ($type == 'publish') {
    		    		
    		$ids = trim(t3lib_div::_GP('ids'));
    		// project_id to array (1;2;3;...) -> [1;2;3;...]
    		$tab_ids = explode(";",$ids);
    		foreach($tab_ids as $id => $value) {
	    		tx_nG6_db::publish_project($value, $user_group);
    		}
    		
        // If asked to unpublish a project
    	} else if ($type == 'unpublish') {
    		    		
    		$ids = trim(t3lib_div::_GP('ids'));
    		// project_id to array (1;2;3;...) -> [1;2;3;...]
    		$tab_ids = explode(";",$ids);
    		foreach($tab_ids as $id => $value) {
	    		tx_nG6_db::unpublish_project($value);
    		}
    	}
Jerome Mariette's avatar
Jerome Mariette committed
230

231
    }
Jerome Mariette's avatar
Jerome Mariette committed
232

233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
	/**
	 * Creates an archiving script to archive data from ids
	 *
	 * @param	string	$user_group		the user group 
	 * @param	array	$ids			list of ids to download 
	 * @param	string	$data_folder	the data folder where are stored the data
	 * @param	string	$tmp_folder		the output directory
	 * @param	string	$format			the archiving format (tar.gz)
	 * @param	string	$user_email		the user email
	 * @param	string	$title_email		the email title to send
	 * @param	string	$msg_email		the email message to send
	 * @return	array (the path to the script, the url path to the final archive)
	 */
	private function create_archive_script($user_group, $ids, $data_folder, $tmp_folder, $format, $user_email, $title_email, $msg_email) {
		// First get the archive name where will be stored the data
		$random_key = tx_nG6_utils::create_random_key(10);
		$archive_path = $tmp_folder."/ng6_".$random_key;
		while( file_exists($archive_path) )
			$archive_path = $tmp_folder."/ng6_".$random_key;
		
		// Then get the directories structure
		$directories = $this->get_directories_structure_and_content($user_group, $ids, $data_folder, $archive_path);
		$archive_script_path = $tmp_folder."/ng6_".$random_key.".sh";
		
		// And write down the script in charge to create the archive
		$fp = fopen($archive_script_path, 'w');
		foreach($directories[0] as $index => $src_directory) {
			if (!file_exists( $directories[1][$index])) {
				fwrite($fp, "mkdir -p ".$directories[1][$index]."\n");
			}
			// For each files in the source directory
			foreach(scandir($src_directory) as $file) {
				if (is_file($src_directory."/".$file) and $file != "analyse.xml" and $file != "index.html" and $file != "run.cfg") {
					fwrite($fp, "cp ".$src_directory."/".$file." ".$directories[1][$index]."\n");
				}
			}
		}
		fwrite($fp, "cd ".$tmp_folder."\n");
		fwrite($fp, "tar -czf ng6_".$random_key.".tar.gz ng6_".$random_key."\n");
		fwrite($fp, "rm -rf ".$archive_path."\n");
		fwrite($fp, 'echo "'.str_replace(array('SPAN_ARCHIVE_NAME'), array("ng6_".$random_key.".tar.gz"), $msg_email).'" | mail -s "'.$title_email.'" '.$user_email."\n");
		fclose($fp);
		return array($archive_script_path, "ng6_".$random_key.".tar.gz");
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

	/**
	 * Creates symbolic links between ids from data_folder into user_directory
	 *
	 * @param	string	$user_group		the user group 
	 * @param	string	$user_login		the user login to make ssh
	 * @param	string	$user_pwd		the user pwd to make ssh
	 * @param	array	$ids			list of ids to download 
	 * @param	string	$data_folder	the data folder where are stored the data
	 * @param	string	$user_directory	the output directory
	 * @return	0=>everything ok, 1=>user rigth problem, 2=>wrong authentification, 3=>connection error
	 */
	private function create_symbolic_links($user_group, $user_login, $user_pwd, $ids, $data_folder, $user_directory) {
		// First try to connect the specified user using ssh
		$connection = ssh2_connect('127.0.0.1', 22);
		if (!$connection) return 3;
		if (!ssh2_auth_password($connection, $user_login, $user_pwd)) return 2;
		
		// Then get the directories structure
		$directories = $this->get_directories_structure_and_content($user_group, $ids, $data_folder, $user_directory);
		// And process the directories structure
		$creation = true;
		foreach($directories[0] as $index => $src_directory) {
			if (!file_exists("ssh2.sftp://".$connection.$directories[1][$index])) {
				if (!mkdir("ssh2.sftp://".$connection.$directories[1][$index], 0755, true)) {
					$creation = false;
					break;
				}
				// For each files in the source directory
306
307
				foreach(scandir($src_directory) as $file) {
					if (is_file($src_directory."/".$file) and $file != "analyse.xml" and $file != "index.html" and $file != "run.cfg") {
308
309
310
						if (!ssh2_exec($connection, 'ln -s '.$src_directory.'/'.$file.' '.$directories[1][$index].'/'.$file)) {
							$creation = false;
							break;
311
312
313
314
						}
					}
				}
			}
315
316
317
318
319
		}
		if (!$creation) return 1;
		return 0;
    }

320
	/**
321
	 * Returns a table with the right directories structure considering ids
322
	 *
323
324
325
326
327
	 * @param	string	$user_group	the user group
	 * @param	array	$ids			List of ids to download 
	 * @param	string	$data_folder	The data folder where are stored the data
	 * @param	string	$output_folder	The output directory
	 * @return	array	[0] => source_directories, [1] => dest_directories
328
	 */
329
	private function get_directories_structure_and_content($user_group, $ids, $data_folder, $output_folder) {
330

331
332
		$src_directories = array();
		$dest_directories = array();
Jerome Mariette's avatar
Jerome Mariette committed
333
		$vals = preg_split("/;/", $ids);
334
335
336
337
338
		
		// Characters to be replaced in the names of the run, the analyses ...
		$char_to_replace = array(' ','/');
		
		// For each project/run/analysis
Jerome Mariette's avatar
Jerome Mariette committed
339
340
		foreach($vals as $val) {
			if ($val != "undefined" && $val != "") {
Jerome Mariette's avatar
Jerome Mariette committed
341
				$id = preg_split("/_/", $val);
Jerome Mariette's avatar
Jerome Mariette committed
342
				if ($id[0] == "data") {
Jerome Mariette's avatar
Jerome Mariette committed
343
					$run = tx_nG6_db::select_run($id[1]);
Jerome Mariette's avatar
Jerome Mariette committed
344
345
					if (!in_array($data_folder.$run["directory"], $src_directories)) {
						$src_directories[] = $data_folder.$run["directory"];
346
347
348
						$ddest = $output_folder."/Project_".str_replace($char_to_replace, '_', $run["project_name"]);
						$ddest .= '.'.$run["project_id"]."/Run_".str_replace($char_to_replace, '_', $run["name"]).".".$id[1]."/RawData";
						$dest_directories[] = $ddest;
Jerome Mariette's avatar
Jerome Mariette committed
349
350
					}
				} else if ($id[0] == "run") {
Jerome Mariette's avatar
Jerome Mariette committed
351
					$run = tx_nG6_db::select_run($id[1]);
Jerome Mariette's avatar
Jerome Mariette committed
352
353
					if (!in_array($data_folder.$run["directory"], $src_directories)) {
						$src_directories[] = $data_folder.$run["directory"];
354
355
356
						$ddest = $output_folder."/Project_".str_replace($char_to_replace, '_', $run["project_name"]);
						$ddest .= '.'.$run["project_id"]."/Run_".str_replace($char_to_replace, '_', $run["name"]).".".$id[1]."/RawData";
						$dest_directories[] = $ddest;
Jerome Mariette's avatar
Jerome Mariette committed
357
					}
Jerome Mariette's avatar
Jerome Mariette committed
358
					foreach(tx_nG6_db::get_run_analysis($user_group, $id[1], 'tx_nG6_analyze.name') as $analyse_id => $analyse_values) {
359
						$aid = preg_split("/_/", $analyse_id);
Jerome Mariette's avatar
Jerome Mariette committed
360
361
						if (!in_array($data_folder.$analyse_values["directory"], $src_directories)) {
							$src_directories[] = $data_folder.$analyse_values["directory"];
362
363
364
365
							$ddest = $output_folder."/Project_".str_replace($char_to_replace, '_', $run["project_name"]);
							$ddest .= '.'.$run["project_id"]."/Run_".str_replace($char_to_replace, '_', $run["name"]);
							$ddest .= ".".$id[1]."/Analyse_".str_replace($char_to_replace, '_', $analyse_values["name"]).".".$aid[1];
							$dest_directories[] = $ddest;
Jerome Mariette's avatar
Jerome Mariette committed
366
367
368
						}
					}
				} else if ($id[0] == "analyse") {
Jerome Mariette's avatar
Jerome Mariette committed
369
					$analyse = tx_nG6_db::select_analyse($id[1]);
Jerome Mariette's avatar
Jerome Mariette committed
370
371
372
373
					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') {
374
375
376
							$ddest = $output_folder."/Project_".str_replace($char_to_replace, '_', $analyse["project_name"]);
							$ddest .= ".".$analyse["project_id"]."/Project_analyses/".str_replace($char_to_replace, '_', $analyse["name"]).".".$id[1];
							$dest_directories[] = $ddest;
Jerome Mariette's avatar
Jerome Mariette committed
377
						} else {
378
379
380
381
							$ddest = $output_folder."/Project_".str_replace($char_to_replace, '_', $analyse["project_name"]);
							$ddest .= ".".$analyse["project_id"]."/Run_".str_replace($char_to_replace, '_', $analyse["run_name"]);
							$ddest .= ".".$analyse["run_id"]."/Analyse_".str_replace($char_to_replace, '_', $analyse["name"]).".".$id[1];
							$dest_directories[] = $ddest;
Jerome Mariette's avatar
Jerome Mariette committed
382
383
384
385
						}
					}
				}
			}
Jerome Mariette's avatar
Jerome Mariette committed
386
		}
387
		return array($src_directories, $dest_directories);
Jerome Mariette's avatar
Jerome Mariette committed
388
389
390
391
392
393
394
395
    }
    
}

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']);
}

396
// Instanciation
Jerome Mariette's avatar
Jerome Mariette committed
397
398
399
$SOBE = t3lib_div::makeInstance('tx_nG6_eid');
$SOBE->main();

400
?>