* 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! ***************************************************************/ require_once(t3lib_extMgm::extPath('nG6').'/lib/class.tx_nG6_utils.php'); /** * Class_nG6_db' for the 'nG6' extension. * This class is in charge of all communication with the database * * @author PF bioinformatique de Toulouse <> */ class tx_nG6_db { /* * Global functions *------------------------------------------------------------*/ /** * Update the specified field of the table * * @param string $entity the entity to change * @param string $id the element id to update * @param string $field the field to change * @param string $value the new value */ function update_field($entity, $id, $field, $value) { if( $field == 'uid' ) { throw new Exception ("The function 'update_field' cannot change an id.") ; } $GLOBALS['TYPO3_DB']-> exec_UPDATEquery ("tx_nG6_$entity", 'uid='.$id, array($field => $value)); } /* * Project functions *------------------------------------------------------------*/ /** * Select all project for the current user * * @param string $user_group the user group * @return table with all projects */ function select_all_user_projects($user_group, $orderby='', $limit='') { $projects = array(); // If the user is not logged on display demonstration project if (!$GLOBALS['TSFE']->loginUser) { $where = 'public=0'; } else { $where = 'fe_group IN ('.tx_nG6_db::get_user_groups($user_group).')'; } //First, get the analyzis project-scale $queryParts = array( 'SELECT' => 'tx_nG6_project.uid AS project_id, '. 'tx_nG6_project.name AS project_name, '. 'tx_nG6_project.description AS project_description, '. 'tx_nG6_project.fe_group AS project_fe_group, '. 'tx_nG6_project.public AS project_public, '. 'tx_nG6_project.crdate AS project_crdate, '. 'tx_nG6_project.hidden AS project_hidden', 'FROM' => 'tx_nG6_project', 'WHERE' => $where, 'GROUPBY' => '', 'ORDERBY' => $orderby, 'LIMIT' => $limit ); $res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts); $user_groups = t3lib_div::intExplode(',',tx_nG6_db::get_user_groups($user_group)); while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) { $project_id = $row['project_id']; // Is the user allowed to see this project $user_allowed = false; if ($row['project_hidden'] == 0) { $user_allowed = true; } else { foreach(tx_nG6_db::get_super_groups_of($row['project_fe_group']) AS $valeur) { if (in_array($valeur, $user_groups)) { $user_allowed = true; } } } if (!isset ($projects['project_'.$project_id]) and $user_allowed) { $projects['project_'.$project_id] = array( 'id' => $project_id, 'name' => $row['project_name'], 'hidden' => $row['project_hidden'], 'date' => $row['project_crdate'], 'public' => $row['project_public'], 'group' => $row['project_fe_group'], 'description' => $row['project_description']); } } return $projects; } /** * Select a run from the database * * @param string $project_id the project id to return * @return hash table with all project information */ function select_project($project_id) { $queryParts = array( 'SELECT' => '*', 'FROM' => 'tx_nG6_project', 'WHERE' => 'tx_nG6_project.uid = '.$project_id, 'GROUPBY' => '', 'ORDERBY' => '', 'LIMIT' => '' ); $res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts); $vals = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res); return array('id' => $vals['uid'], 'name' => $vals['name'], 'hidden' => $vals['hidden'], 'date' => $vals['crdate'], 'public' => $vals['public'], 'group' => $vals['fe_group'], 'description' => $vals['description']); } /** * Select all runs linked to the specified project * * @param string $user_group the user group * @param string $project_id the project id * @return hash table with all runs information */ function get_project_runs($user_group, $project_id, $orderby='', $limit='') { // First select all runs from the database $queryParts = Array( 'SELECT' => 'tx_nG6_run.uid AS run_id,'. 'tx_nG6_run.directory AS run_directory,'. 'tx_nG6_run.species AS run_species,'. 'tx_nG6_run.description AS run_description, '. 'tx_nG6_run.data_nature AS run_data_nature, '. 'tx_nG6_run.sequencer AS run_sequencer, '. 'tx_nG6_run.type AS run_type, '. 'tx_nG6_run.name AS run_name, '. 'tx_nG6_project.uid AS project_id, '. 'tx_nG6_run.nb_sequences AS run_nb_sequences, '. 'tx_nG6_run.full_seq_size AS run_full_seq_size, '. 'tx_nG6_run.date AS run_date, '. 'tx_nG6_run.hidden AS run_hidden, '. 'tx_nG6_project.name AS project_name, '. 'tx_nG6_project.fe_group AS project_fe_group', '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', 'WHERE' => 'tx_nG6_project.uid='.$project_id, 'GROUPBY' => '', 'ORDERBY' => $orderby, 'LIMIT' => $limit ); // Then create the result hash table $results = array(); $user_groups = t3lib_div::intExplode(',',tx_nG6_db::get_user_groups($user_group)); $res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts); while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) { $run_id = $row['run_id']; // Is the user allowed to see this run $user_allowed = false; if ($row['run_hidden'] == 0) { $user_allowed = true; } else { foreach(tx_nG6_db::get_super_groups_of($row['project_fe_group']) AS $valeur) { if (in_array($valeur, $user_groups)) { $user_allowed = true; } } } if (!isset($results['run_'.$run_id]) and $user_allowed) { $results['run_'.$run_id] = array( 'id' => $run_id, 'directory' => $row['run_directory'], 'name' => $row['run_name'], 'project_name' => $row['project_name'], 'fe_group' => $row['project_fe_group'], 'hidden' => $row['run_hidden'], 'species' => $row['run_species'], 'nb_sequences' => $row['run_nb_sequences'], 'full_seq_size' => $row['run_full_seq_size'], 'project_id' => $row['project_id'], 'date' => $row['run_date'], 'data_nature' => $row['run_data_nature'], 'sequencer' => $row['run_sequencer'], 'type' => $row['run_type'], 'description' => $row['run_description'] ); } } return $results; } /** * Select all analysis linked to the specified project * * @param string $user_group the user group * @param string $project_id the project id * @return hash table with all analysis information */ function get_project_analysis($user_group, $project_id, $orderby='', $limit='') { // First select all analysis from the database $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.class AS analyze_class, '. 'tx_nG6_analyze.date AS analyze_date, '. 'tx_nG6_analyze.software AS analyze_software, '. 'tx_nG6_analyze.version AS analyze_version, '. 'tx_nG6_analyze.params AS analyze_params, '. 'tx_nG6_analyze.description AS analyze_description, '. 'tx_nG6_analyze.parent_uid AS analyze_parent_uid, '. 'tx_nG6_analyze.hidden AS analyze_hidden, '. 'tx_nG6_project.fe_group AS project_fe_group', '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_project_analyze.project_id='.$project_id, 'GROUPBY' => '', 'ORDERBY' => $orderby, 'LIMIT' => $limit ); // Then create the result hash table $results = array(); $res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts); $user_groups = t3lib_div::intExplode(',',tx_nG6_db::get_user_groups($user_group)); while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) { $analyze_id = $row['analyze_id']; // Is the user allowed to see this analyse $user_allowed = false; if ($row['analyze_hidden'] == 0) { $user_allowed = true; } else { foreach(tx_nG6_db::get_super_groups_of($row['project_fe_group']) AS $valeur) { if (in_array($valeur, $user_groups)) { $user_allowed = true; } } } if (!isset ($results['analyse_'.$analyze_id]) and $user_allowed) { $results['analyse_'.$analyze_id] = array( 'directory' => $row['analyze_directory'], 'name' => $row['analyze_name'], 'class' => $row['analyze_class'], 'id' => $row['analyze_id'], 'hidden' => $row['analyze_hidden'], 'params' => $row['analyze_params'], 'software' => $row['analyze_software'], 'version' => $row['analyze_version'], 'date' => $row['analyze_date'], 'description' => $row['analyze_description'], 'parent_id' => $row['analyze_parent_uid'] ); } } return $results; } /** * Hide a project and sublevels (runs & analyzes) datas from database project * * @param string $p_id the project id to hide * @param string $user_group the group of the current user */ function hide_project($p_id, $user_group) { // First change all project runs $project_runs = tx_nG6_db::get_project_runs($user_group, $p_id); foreach($project_runs as $run_id => $run_values) { tx_nG6_db::hide_run($run_values['id'], $user_group); } // Then all project analysis $project_analysis = tx_nG6_db::get_project_analysis($user_group, $p_id); foreach($project_analysis as $analysis_id => $analysis_values) { tx_nG6_db::hide_analysis($analysis_values['id']); } // Finaly change the project itself $GLOBALS['TYPO3_DB']-> exec_UPDATEquery ('tx_nG6_project', 'uid='.$p_id, array('hidden' => '1')); } /** * Unhide a project and sublevels (runs & analyzes) datas * * @param string $p_id the project id to hide * @param string $user_group the group of the current user */ function unhide_project($p_id, $user_group) { // First change all project runs $project_runs = tx_nG6_db::get_project_runs($user_group, $p_id); foreach($project_runs as $run_id => $run_values) { tx_nG6_db::unhide_run($run_values['id'], $user_group); } // Then all project analysis $project_analysis = tx_nG6_db::get_project_analysis($user_group, $p_id); foreach($project_analysis as $analysis_id => $analysis_values) { tx_nG6_db::unhide_analysis($analysis_values['id']); } // Finaly change the project itself $GLOBALS['TYPO3_DB']-> exec_UPDATEquery ('tx_nG6_project', 'uid='.$p_id, array('hidden' => '0')); } /** * Publish the project * * @param string $p_id the project id to publish */ function publish_project($p_id, $user_group) { // First unhide the project tx_nG6_db::unhide_project($p_id, $user_group); // The set the project as visible $GLOBALS['TYPO3_DB']-> exec_UPDATEquery ('tx_nG6_project', 'uid='.$p_id, array('public' => '0')); } /** * Unpublish the project * * @param string $p_id the project id to publish */ function unpublish_project($p_id) { $GLOBALS['TYPO3_DB']-> exec_UPDATEquery ('tx_nG6_project', 'uid='.$p_id, array('public' => '1')); } /** * Delete a project and sublevels (runs & analyzes) datas from database project * * @param string $user_group the user group * @param string $p_id the project id * @param string $user_login the user login * @param string $user_pwd the user password * @param string $data_folder the data folder */ function delete_project($user_group, $p_id, $user_login, $user_pwd, $data_folder) { $res = 0; // All runs $project_runs = tx_nG6_db::get_project_runs($user_group, $p_id); foreach($project_runs as $run_id => $run_values) { if ($run_values['id'] != 'undefined' && $run_values['id'] != '') { //delete the run sublevels $res = tx_nG6_db::delete_run($user_group, $run_values['id'], $user_login, $user_pwd, $data_folder); if ($res != 0) { break; } } } // All project_analysis if ($res == 0) { $project_analysis = tx_nG6_db::get_project_analysis($user_group, $p_id); foreach($project_analysis as $analyse_id => $analyze_values) { if ($analyze_values['id'] != 'undefined' && $analyze_values['id'] != '') { // delete table 'analyze' entries $res = tx_nG6_db::delete_analysis($analyze_values['id'], $user_login, $user_pwd, $data_folder); if ($res != 0) { break; } } } } // Delete DB project entry if ($res == 0) { $table='tx_nG6_project'; $where='tx_nG6_project.uid='.$p_id; $res1 = $GLOBALS['TYPO3_DB']-> exec_DELETEquery ($table, $where); if ($res1 != 1) {$res = 1;} } return $res; } /** * Return the size of the project * * @param string $p_id the project id to return the size * @param string $data_folder the data folder * @param string $user_group the user group */ function get_project_size($p_id, $data_folder, $user_group) { $full_size = 0; // All runs $project_runs = tx_nG6_db::get_project_runs($user_group, $p_id); foreach($project_runs as $run_id => $run_values) { $full_size += tx_nG6_db::get_run_size($run_values['id'], $data_folder, $user_group); } // All analysis $project_analysis = tx_nG6_db::get_project_analysis($user_group, $p_id); foreach($project_analysis as $analyse_id => $analyze_values) { $full_size += tx_nG6_db::get_analysis_size($analyze_values['id'], $data_folder); } return $full_size; } /* * Run functions *------------------------------------------------------------*/ /** * Select all run for the specified user * * @param string $user_group the user group * @return table with all projects */ function select_all_user_runs($user_group, $orderby='', $limit='') { // If the user is not logged on display demonstration project if (!$GLOBALS['TSFE']->loginUser) { $where = 'public=0'; } else { $where = 'fe_group IN ('.tx_nG6_db::get_user_groups($user_group).')'; } // Execute the request $queryParts = Array( 'SELECT' => 'tx_nG6_run.uid AS run_id,'. 'tx_nG6_run.directory AS run_directory,'. 'tx_nG6_run.species AS run_species,'. 'tx_nG6_run.description AS run_description, '. 'tx_nG6_run.data_nature AS run_data_nature, '. 'tx_nG6_run.sequencer AS run_sequencer, '. 'tx_nG6_run.type AS run_type, '. 'tx_nG6_run.name AS run_name, '. 'tx_nG6_run.nb_sequences AS run_nb_sequences, '. 'tx_nG6_run.full_seq_size AS run_full_seq_size, '. 'tx_nG6_run.date AS run_date, '. 'tx_nG6_run.hidden AS run_hidden, '. 'tx_nG6_project.uid AS project_id, '. 'tx_nG6_project.name AS project_name, '. 'tx_nG6_project.fe_group AS project_fe_group', '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', 'WHERE' => $where, 'GROUPBY' => '', 'ORDERBY' => $orderby, 'LIMIT' => $limit ); // Then create the result hash table $results = array(); $user_groups = t3lib_div::intExplode(',',tx_nG6_db::get_user_groups($user_group)); $res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts); while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) { $run_id = $row['run_id']; // Is the user allowed to see this run $user_allowed = false; if ($row['run_hidden'] == 0) { $user_allowed = true; } else { foreach(tx_nG6_db::get_super_groups_of($row['project_fe_group']) AS $valeur) { if (in_array($valeur, $user_groups)) { $user_allowed = true; } } } if (!isset($results['run_'.$run_id]) and $user_allowed) { $results['run_'.$run_id] = array( 'id' => $run_id, 'directory' => $row['run_directory'], 'name' => $row['run_name'], 'project_name' => $row['project_name'], 'fe_group' => $row['project_fe_group'], 'project_id' => $row['project_id'], 'hidden' => $row['run_hidden'], 'species' => $row['run_species'], 'nb_sequences' => $row['run_nb_sequences'], 'full_seq_size' => $row['run_full_seq_size'], 'date' => $row['run_date'], 'data_nature' => $row['run_data_nature'], 'sequencer' => $row['run_sequencer'], 'type' => $row['run_type'], 'description' => $row['run_description'] ); } } return $results; } /** * Select all information for the specified run * * @param string $run_id the run id * @return hash table with all the run information */ function select_run($run_id) { // First select the run from the database $queryParts = Array( 'SELECT' => 'tx_nG6_project.uid AS project_id,'. 'tx_nG6_project.name AS project_name,'. 'tx_nG6_run.uid AS run_id,'. 'tx_nG6_run.directory AS run_directory,'. 'tx_nG6_run.description AS run_description, '. 'tx_nG6_run.species AS run_species, '. 'tx_nG6_run.name AS run_name, '. 'tx_nG6_run.data_nature AS run_data_nature, '. 'tx_nG6_run.sequencer AS run_sequencer, '. 'tx_nG6_run.type AS run_type, '. 'tx_nG6_run.full_seq_size, '. 'tx_nG6_run.hidden AS run_hidden, '. 'tx_nG6_run.nb_sequences, '. 'tx_nG6_run.date AS run_date', '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', 'WHERE' => 'tx_nG6_run.uid='.$run_id, 'GROUPBY' => '', 'ORDERBY' => 'tx_nG6_run.date DESC', 'LIMIT' => '' ); // Then create the result hash table $result = array(); $res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts); while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) { $result = array( 'id' => $row['run_id'], 'project_id' => $row['project_id'], 'project_name' => $row['project_name'], 'directory' => $row['run_directory'], 'name' => $row['run_name'], 'hidden' => $row['run_hidden'], 'species' => $row['run_species'], 'nb_sequences' => $row['nb_sequences'], 'full_seq_size' => $row['full_seq_size'], 'date' => $row['run_date'], 'data_nature' => $row['run_data_nature'], 'sequencer' => $row['run_sequencer'], 'type' => $row['run_type'], 'description' => $row['run_description'] ); } return $result; } /** * Select all mids linked to the specified run * * @param string $run_id the run id * @return hash table with all mids description */ function select_mid_descriptions($run_id) { // First select all analysis from the database $queryParts = array( 'SELECT' => 'mid, description ', 'FROM' => 'tx_nG6_sample ', 'WHERE' => 'run_id='.$run_id, 'GROUPBY' => '', 'ORDERBY' => '', 'LIMIT' => '' ); $result = array(); $res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts); while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) { $result[$row["mid"]] = $row["description"]; } return $result; } /** * Select all analysis linked to the specified run * * @param string $user_group the user group * @param string $run_id the run id * @return hash table with all analysis information */ function get_run_analysis($user_group, $run_id, $orderby='', $limit='') { // First select all analysis from the database $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.params AS analyze_params, '. 'tx_nG6_analyze.class AS analyze_class, '. 'tx_nG6_analyze.date AS analyze_date, '. 'tx_nG6_analyze.software AS analyze_software, '. 'tx_nG6_analyze.version AS analyze_version, '. 'tx_nG6_analyze.hidden AS analyze_hidden, '. 'tx_nG6_analyze.description AS analyze_description, '. 'tx_nG6_analyze.parent_uid AS analyze_parent_uid, '. 'tx_nG6_project.fe_group AS project_fe_group', '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_run_analyze.run_id='.$run_id, 'GROUPBY' => '', 'ORDERBY' => $orderby, 'LIMIT' => $limit ); // Then create the result hash table $results = array(); $res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts); $user_groups = t3lib_div::intExplode(',',tx_nG6_db::get_user_groups($user_group)); while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) { $analyze_id = $row['analyze_id']; // Is the user allowed to see this analyse $user_allowed = false; if ($row['analyze_hidden'] == 0) { $user_allowed = true; } else { foreach(tx_nG6_db::get_super_groups_of($row['project_fe_group']) AS $valeur) { if (in_array($valeur, $user_groups)) { $user_allowed = true; } } } if (!isset ($results['analyse_'.$analyze_id]) and $user_allowed) { $results['analyse_'.$analyze_id] = array( 'directory' => $row['analyze_directory'], 'name' => $row['analyze_name'], 'params' => $row['analyze_params'], 'class' => $row['analyze_class'], 'id' => $row['analyze_id'], 'hidden' => $row['analyze_hidden'], 'software' => $row['analyze_software'], 'version' => $row['analyze_version'], 'date' => $row['analyze_date'], 'description' => $row['analyze_description'], 'parent_id' => $row['analyze_parent_uid'] ); } } return $results; } /** * Delete a run, sample and sublevel (analyzes) datas from database project * * @param string $user_group the user group * @param string $r_id the run id * @param string $user_login the user login * @param string $user_pwd the user password * @param string $data_folder the data folder */ function delete_run($user_group, $r_id, $user_login, $user_pwd, $data_folder) { $res = 0; $run_analysis = tx_nG6_db::get_run_analysis($user_group, $r_id); foreach($run_analysis as $analyse_id => $analyze_values) { if ($analyze_values['id'] != 'undefined' && $analyze_values['id'] != '') { // delete table 'analyze' entries $res = tx_nG6_db::delete_analysis($analyze_values['id'], $user_login, $user_pwd, $data_folder); if ($res != 0) { break; } } } // First select the run $my_run = tx_nG6_db::select_run($r_id); // delete run if ($res == 0) { $table='tx_nG6_project_run'; $where='run_id='.$r_id; $GLOBALS['TYPO3_DB']-> exec_DELETEquery ($table, $where); $table='tx_nG6_run'; $where='tx_nG6_run.uid='.$r_id; $res1 = $GLOBALS['TYPO3_DB']-> exec_DELETEquery ($table, $where); if ($res1 != 1) {$res = 1;} } // delete run samples if ($res == 0) { $table='tx_nG6_sample'; $where='tx_nG6_sample.run_id='.$r_id; $GLOBALS['TYPO3_DB']-> exec_DELETEquery ($table, $where); } if ($res == 0) { // Delete the run directory $res = tx_nG6_utils::delete_directory($user_login, $user_pwd, $data_folder.$my_run['directory']); } return $res; } /** * Return the size of the run * * @param string $r_id the run id to return the size * @param string $data_folder the data folder * @param string $user_group the user group */ function get_run_size($r_id, $data_folder, $user_group) { $full_size = 0; $run_analysis = tx_nG6_db::get_run_analysis($user_group, $r_id); foreach($run_analysis as $analyse_id => $analyze_values) { $full_size += tx_nG6_db::get_analysis_size($analyze_values['id'], $data_folder); } // Then select the run $my_run = tx_nG6_db::select_run($r_id); foreach(scandir($data_folder.$my_run['directory']) as $file) { if (is_file($data_folder.$my_run['directory']."/".$file)) { $full_size += filesize($data_folder.$my_run['directory']."/".$file); } } return $full_size; } /** * Hide a run and sublevels (analysis) * * @param string $r_id the run id to hide * @param string $user_group the group of the current user */ function hide_run($r_id, $user_group) { // First select all run analysis $run_analysis = tx_nG6_db::get_run_analysis($user_group, $r_id); foreach($run_analysis as $analysis_id => $analysis_values) { tx_nG6_db::hide_analysis($analysis_values['id']); } // Finaly change the project itself $GLOBALS['TYPO3_DB']-> exec_UPDATEquery ('tx_nG6_run', 'uid='.$r_id, array('hidden' => '1')); } /** * Unhide a run and sublevels (analysis) * * @param string $r_id the run id to hide * @param string $user_group the group of the current user */ function unhide_run($r_id, $user_group) { // Then all run analysis $run_analysis = tx_nG6_db::get_run_analysis($user_group, $r_id); foreach($run_analysis as $analysis_id => $analysis_values) { tx_nG6_db::unhide_analysis($analysis_values['id']); } // Unhide the project $GLOBALS['TYPO3_DB']-> exec_UPDATEquery ( 'tx_nG6_project, tx_nG6_project_run', 'tx_nG6_project.uid = tx_nG6_project_run.project_id AND tx_nG6_project_run.run_id = '.$r_id , array('tx_nG6_project.hidden' => '0') ) ; // Finaly change the run itself $GLOBALS['TYPO3_DB']-> exec_UPDATEquery ('tx_nG6_run', 'uid='.$r_id, array('hidden' => '0')); } /* * Analysis functions *------------------------------------------------------------*/ /** * Select all information on an analyse * * @param string $analyse_id the analyse id * @return hash table with all analyse information */ function select_analyse($analyse_id) { // First select all analyse information $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.params AS analyze_params, '. 'tx_nG6_analyze.software AS analyze_software, '. 'tx_nG6_analyze.class AS analyze_class, '. 'tx_nG6_analyze.date AS analyze_date, '. 'tx_nG6_analyze.description AS analyze_description, '. 'tx_nG6_analyze.version AS analyze_version, '. 'tx_nG6_analyze.parent_uid AS analyze_parent_uid ', '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, add project information 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'], 'class' => $row['analyze_class'], 'params' => $row['analyze_params'], 'date' => $row['analyze_date'], 'description' => $row['analyze_description'], 'parent_id' => $row['analyze_parent_uid'], 'software' => $row['analyze_software'], 'version' => $row['analyze_version'] ); } return $result; } else { // If it's a run analyse, add run information $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.params AS analyze_params, '. 'tx_nG6_analyze.class AS analyze_class, '. 'tx_nG6_analyze.date AS analyze_date, '. 'tx_nG6_analyze.software AS analyze_software, '. 'tx_nG6_analyze.version AS analyze_version, '. 'tx_nG6_analyze.description AS analyze_description,'. 'tx_nG6_analyze.parent_uid AS analyze_parent_uid ', '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'], 'params' => $row['analyze_params'], 'class' => $row['analyze_class'], 'date' => $row['analyze_date'], 'description' => $row['analyze_description'], 'software' => $row['analyze_software'], 'parent_id' => $row['analyze_parent_uid'], 'version' => $row['analyze_version'] ); } return $result; } } /** * Select all results of an analyse * * @param string $analyse_id the analyse id * @return hash table with all analyse results */ function select_analyse_results($analyse_id) { // First select all analyse information $queryParts = array( 'SELECT' => 'uid,file,rkey,rvalue,rgroup ', 'FROM' => 'tx_nG6_result ', 'WHERE' => 'analyze_id='.$analyse_id, 'GROUPBY' => '', 'ORDERBY' => '', 'LIMIT' => '' ); $res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts); $result = array(); while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) { if (array_key_exists($row['file'], $result)) { if (array_key_exists($row['rgroup'], $result[$row['file']])) { $result[$row['file']][$row['rgroup']][$row['rkey']] = $row['rvalue']; } else { $result[$row['file']][$row['rgroup']] = array($row['rkey'] => $row['rvalue']); } } else { $result[$row['file']] = array($row['rgroup'] => array($row['rkey'] => $row['rvalue'])); } } return $result; } /** * Return the size of the analysis * * @param string $a_id the analysis id to return the size * @param string $data_folder the data folder */ function get_analysis_size($a_id, $data_folder) { // First select the analyse $my_analysis = tx_nG6_db::select_analyse($a_id); $full_size = 0; foreach(scandir($data_folder.$my_analysis['directory']) as $file) { if (is_file($data_folder.$my_analysis['directory']."/".$file)) { $full_size += filesize($data_folder.$my_analysis['directory']."/".$file); } } return $full_size; } /** * Hide an analysis * * @param string $a_id the analysis id to hide */ function hide_analysis($a_id) { // Change the analysis itself $GLOBALS['TYPO3_DB']-> exec_UPDATEquery ('tx_nG6_analyze', 'uid='.$a_id, array('hidden' => '1')); } /** * Unhide an analysis * * @param string $a_id the analysis id to hide * @param string $user_group the group of the current user */ function unhide_analysis($a_id) { $queryParts = array( 'SELECT' => 'tx_nG6_project_analyze.uid', 'FROM' => 'tx_nG6_project_analyze', 'WHERE' => 'tx_nG6_project_analyze.analyze_id = '.$a_id, 'GROUPBY' => '', 'ORDERBY' => '', 'LIMIT' => '' ); $res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts) ; $is_project_analyze = $GLOBALS['TYPO3_DB']->sql_num_rows( $res ) ; // If the analyse isn't link to run if( $is_project_analyze ) { // unhide the project $GLOBALS['TYPO3_DB']-> exec_UPDATEquery ( 'tx_nG6_project, tx_nG6_project_analyze', 'tx_nG6_project.uid = tx_nG6_project_analyze.project_id AND tx_nG6_project_analyze.analyze_id = '.$a_id , array('tx_nG6_project.hidden' => '0') ) ; } // If the analyse is link to run else { // unhide the project and the run $GLOBALS['TYPO3_DB']->exec_UPDATEquery ( 'tx_nG6_project, tx_nG6_project_run, tx_nG6_run, tx_nG6_run_analyze', 'tx_nG6_project.uid = tx_nG6_project_run.project_id AND tx_nG6_project_run.run_id = tx_nG6_run.uid AND tx_nG6_run.uid = tx_nG6_run_analyze.run_id AND tx_nG6_run_analyze.analyze_id = '.$a_id , array('tx_nG6_project.hidden' => '0', 'tx_nG6_run.hidden' => '0') ) ; } // Change the analysis itself $GLOBALS['TYPO3_DB']-> exec_UPDATEquery ('tx_nG6_analyze', 'uid='.$a_id, array('hidden' => '0')); } /** * Delete an analyze and results from database project * * @param string $a_id the analyze id * @param string $user_login the user login * @param string $user_pwd the user password * @param string $data_folder the data folder * @return 0=>everything ok, 1=>user right problem, 2=>wrong authentification, 3=>connection error */ function delete_analysis($a_id, $user_login, $user_pwd, $data_folder) { $res = 0; // First select the analyse $my_analysis = tx_nG6_db::select_analyse($a_id); // Delete the analysis results from the database $table='tx_nG6_result'; $where='tx_nG6_result.analyze_id='.$a_id; $GLOBALS['TYPO3_DB']-> exec_DELETEquery ($table, $where); // Intermediate tables $table='tx_nG6_project_analyze'; $where='analyze_id='.$a_id; $GLOBALS['TYPO3_DB']-> exec_DELETEquery ($table, $where); $table='tx_nG6_run_analyze'; $where='analyze_id='.$a_id; $GLOBALS['TYPO3_DB']-> exec_DELETEquery ($table, $where); // Finaly the analysis itself $table='tx_nG6_analyze'; $where='tx_nG6_analyze.uid='.$a_id; $res1 = $GLOBALS['TYPO3_DB']-> exec_DELETEquery ($table, $where); if ($res1 != 1) { $res = 1; } if ($res == 0) { // Delete the analyse directory $res = tx_nG6_utils::delete_directory($user_login, $user_pwd, $data_folder.$my_analysis['directory']); } return $res; } /* * User functions *------------------------------------------------------------*/ /** * Return the supergroups list of the specified group * * @param string $group the group * @return array */ function get_super_groups_of ($group) { // First get the super group of the given group $queryParts = array( 'SELECT' => 'uid, subgroup', 'FROM' => 'fe_groups', 'WHERE' => '', 'GROUPBY' => '', 'ORDERBY' => '', 'LIMIT' => '' ); $res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts); $reslist = array(); $i = 0; while($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) { if (in_array($group, t3lib_div::intExplode(',',$row['subgroup']))) { $reslist[$i] = $row['uid']; $i++; } } return $reslist; } /** * Return the recursive supergroups list of the specified group * * @param string $group the group id * @param string $level the search depth, can be 'all' or 'first' (default) * @return array */ function get_all_super_groups_of ($group, $level = 'first') { if ($level == 'all') { // return the recursive list of all $group super groups $first_list = tx_nG6_db::get_super_groups_of($group); $super_list = $first_list; foreach ($first_list as $first_list_group_id) { $next_list = tx_nG6_db::get_all_super_groups_of($first_list_group_id, $level); $super_list = array_merge($super_list,$next_list); } return $super_list; } else { return tx_nG6_db::get_super_groups_of($group); } } /** * Returns a string with user groups separated by comas * * @param string $user_group the user group * @return string list of user groups splited by a coma */ function get_user_groups($user_group) { $tab_groups = array(); tx_nG6_db::get_sub_groups($user_group, '', $tab_groups); $groups = ''; foreach($tab_groups as $valeur) { $groups .= $valeur.','; } return substr($groups, 0, -1); } /** * Fetches subgroups of groups. Function is called recursively for each subgroup. * Function was previously copied from t3lib_userAuthGroup->fetchGroups and has been slightly modified. * * @param string $grList commalist of fe_groups uid numbers * @param string $idList list of already processed fe_groups-uids so the function will not fall into a eternal recursion. * @return array */ function get_sub_groups($grList, $idList='', &$groups) { // Fetching records of the groups in $grList (which are not blocked by lockedToDomain either): $lockToDomain_SQL = ' AND (lockToDomain=\'\' OR lockToDomain IS NULL)'; $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,subgroup', 'fe_groups', 'deleted=0 AND hidden=0 AND uid IN ('.$grList.')'.$lockToDomain_SQL); $groupRows = array(); // Internal group record storage // The groups array is filled while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) { if(!in_array($row['uid'], $groups)) { $groups[] = $row['uid']; } $groupRows[$row['uid']] = $row; } // Traversing records in the correct order $include_staticArr = t3lib_div::intExplode(',', $grList); foreach($include_staticArr as $uid) { // traversing list // Get row: $row=$groupRows[$uid]; if (is_array($row) && !t3lib_div::inList($idList,$uid)) { // Must be an array and $uid should not be in the idList, because then it is somewhere previously in the grouplist // Include sub groups if (trim($row['subgroup'])) { $theList = implode(',',t3lib_div::intExplode(',',$row['subgroup'])); // Make integer list tx_nG6_db::get_sub_groups($theList, $idList.','.$uid, $groups); // Call recursively, pass along list of already processed groups so they are not recursed again. } } } } /** * Return if the user is authorized or not to access the specified project and run * * @param string $user_group the user group * @param string $project_id the project id * @param string $project_id the run id * @return boolean */ function user_is_authorized ($user_group, $project_id, $run_id) { $authorized = false; if ($project_id) { $queryParts = array( 'SELECT' => 'fe_group,public', 'FROM' => 'tx_nG6_project', 'WHERE' => 'uid='.$project_id, 'GROUPBY' => '', 'ORDERBY' => '', 'LIMIT' => '' ); $res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts); $val = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res); if ($GLOBALS['TSFE']->loginUser) { $authorized = in_array($val['fe_group'], preg_split("/,/", tx_nG6_db::get_user_groups($user_group))); } else { $authorized = ($val['public'] == 0); } } elseif ($run_id) { $queryParts = array( 'SELECT' => 'project_id', 'FROM' => 'tx_nG6_project_run', 'WHERE' => 'run_id='.$run_id, 'GROUPBY' => '', 'ORDERBY' => '', 'LIMIT' => '' ); $res1 = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts); $val = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res1); $queryParts = array( 'SELECT' => 'fe_group,public', 'FROM' => 'tx_nG6_project', 'WHERE' => 'uid='.$val['project_id'], 'GROUPBY' => '', 'ORDERBY' => '', 'LIMIT' => '' ); $res2 = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts); $val = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res2); if ($GLOBALS['TSFE']->loginUser) { $authorized = in_array($val['fe_group'], preg_split("/,/", tx_nG6_db::get_user_groups($user_group))); } else { $authorized = ($val['public'] == 0); } } else { $authorized = true; } return $authorized; } /** * Return if the user is a superuser for the specified project/run/analyse * * @param string $user_group the user group * @param string $type can be project/run/analyse * @param string $uid the uid * @return boolean {1 = ok, 0 = nok} */ function is_super_user($user_group, $type, $uid) { if ($type == 'project') { $queryParts = array( 'SELECT' => 'fe_group', 'FROM' => 'tx_nG6_project', 'WHERE' => 'uid='.$uid, 'GROUPBY' => '', 'ORDERBY' => '', 'LIMIT' => '' ); $res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts); $val = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res); $authorized_group = $val['fe_group']; } elseif ($type == 'run') { $queryParts = array( 'SELECT' => 'project_id', 'FROM' => 'tx_nG6_project_run', 'WHERE' => 'run_id='.$uid, 'GROUPBY' => '', 'ORDERBY' => '', 'LIMIT' => '' ); $res1 = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts); $val = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res1); $queryParts = array( 'SELECT' => 'fe_group', 'FROM' => 'tx_nG6_project', 'WHERE' => 'uid='.$val['project_id'], 'GROUPBY' => '', 'ORDERBY' => '', 'LIMIT' => '' ); $res2 = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts); $val = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res2); $authorized_group = $val['fe_group']; } elseif ($type == 'analyze') { $queryParts = array( 'SELECT' => 'run_id', 'FROM' => 'tx_nG6_run_analyze', 'WHERE' => 'analyze_id='.$uid, 'GROUPBY' => '', 'ORDERBY' => '', 'LIMIT' => '' ); $res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts); if ($GLOBALS['TYPO3_DB']->sql_num_rows($res) > 0) { $val = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res); $queryParts = array( 'SELECT' => 'project_id', 'FROM' => 'tx_nG6_project_run', 'WHERE' => 'run_id='.$val['run_id'], 'GROUPBY' => '', 'ORDERBY' => '', 'LIMIT' => '' ); $res2 = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts); $val = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res2); $queryParts = array( 'SELECT' => 'fe_group', 'FROM' => 'tx_nG6_project', 'WHERE' => 'uid='.$val['project_id'], 'GROUPBY' => '', 'ORDERBY' => '', 'LIMIT' => '' ); $res3 = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts); $val = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res3); $authorized_group = $val['fe_group']; } else { $queryParts = array( 'SELECT' => 'project_id', 'FROM' => 'tx_nG6_project_analyze', 'WHERE' => 'analyze_id='.$uid, 'GROUPBY' => '', 'ORDERBY' => '', 'LIMIT' => '' ); $res = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts); $val = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res); $queryParts = array( 'SELECT' => 'fe_group', 'FROM' => 'tx_nG6_project', 'WHERE' => 'uid='.$val['project_id'], 'GROUPBY' => '', 'ORDERBY' => '', 'LIMIT' => '' ); $res2 = $GLOBALS['TYPO3_DB']->exec_SELECT_queryArray($queryParts); $val = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res2); $authorized_group = $val['fe_group']; } } else { return true; } if (!$GLOBALS['TSFE']->loginUser) { // If the user is not logged on return false; } else { // super groups string => supergroup1,supergroup2,... $all_list = tx_nG6_db::get_all_super_groups_of($authorized_group,'all'); $groups_str=''; foreach ($all_list as $asd) { $groups_str .= $asd.','; } $effective_supergroups = substr($groups_str, 0, -1); //delete the last comma if (strlen($effective_supergroups) == 0) { $is_super_user = true; // means : current user is a super user (no super groups exists) } else { $is_super_user = in_array($user_group, preg_split("/,/", $effective_supergroups)); } return $is_super_user; } } } if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/nG6/class.tx_nG6_db.php']) { include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/nG6/class.tx_nG6_db.php']); } ?>