* 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! ***************************************************************/ /** * Plugin 'nG6' for the 'nG6' extension. * * @author PF bioinformatique de Toulouse <> */ //require_once(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('saltedpasswords').'/classes/salts/class.tx_saltedpasswords_salts_factory.php'); class tx_nG6_utils { /** * Describe all links between analyzes (and their ancestor) and build a tree. The main Array is the root. * Example of result : Array( [a] => Array( ["data"] => Array(), ["child"] => Array( ... ) ) ) * * @param Array $analyzes_map analyze array (for example, result of tx_nG6_db::get_project_analysis(...)) * @return a tree describing the links children-parents of all analyzes */ static function trace_hierarchy($analyzes_map) { $base_tree = array(); foreach(array_values($analyzes_map) as $id => $analyze) { $base_tree[$analyze['id']] = $analyze['parent_id']; //$tree_analyze[$analyze['id']] = $analyze; $tree_analyze[$analyze['id']] = array("data" => $analyze, "child" => array()); } // Create relationships $h_analysis = array(); foreach($tree_analyze as $analyze_id => $analyze_infos) { if ($analyze_infos["data"]["parent_id"] == 0) { $h_analysis[$analyze_id] = &$tree_analyze[$analyze_id]; } else { $tree_analyze[$analyze_infos["data"]["parent_id"]]["child"][$analyze_id] = &$tree_analyze[$analyze_id]; } } return $h_analysis; } /** * Test if the substring ends the string * * @param $str : the string to test * @param $sub : the substring that may ends $str * @return boolean */ static function ends_with($str, $sub) { return ( substr( $str, strlen( $str ) - strlen( $sub ) ) == $sub ); } /** * Returns a string generated randomly * * @param $size : the random key size * @return string */ static function create_random_key($size) { $keyset = "abcdefghijklmABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $randkey = ""; for ($i=0; $i<$size; $i++) $randkey .= substr($keyset, rand(0, strlen($keyset)-1), 1); return $randkey; } /** * Delete directories * * @param string $user_login * @param string $user_pwd * @param string $directory * @return 0=>everything ok, 1=>user right problem, 2=>wrong authentification, 3=>connection error */ static function delete_directory($user_login, $user_pwd, $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; if ($directory != 'undefined' && $directory != '') { // And process the directories structure //TODO BUG NE RETOURNE PAS D'ERREUR si pas supprimer $stream = ssh2_exec($connection, 'rm -rf '.$directory."/"); $errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR); // Enable blocking for both streams stream_set_blocking($errorStream, true); stream_set_blocking($stream, true); // Whichever of the two below commands is listed first will receive its appropriate output. The second command receives nothing if(stream_get_contents($errorStream)) { // Close the streams fclose($errorStream); fclose($stream); return 1; }else{ // Close the streams fclose($errorStream); fclose($stream); return 0; } } return 0; } /** * Purge directories * * @param string $user_login * @param string $user_pwd * @param string $directory * @return 0=>everything ok, 1=>user right problem, 2=>wrong authentification, 3=>connection error */ static function purge_directory($user_login, $user_pwd, $directory) { if ($directory != 'undefined' && $directory != '') { // And process the directories structure, remove all files except .html ans .png $connection = ssh2_connect('127.0.0.1', 22); if (!$connection) { return 3; } if (!ssh2_auth_password($connection, $user_login,$user_pwd )){ return 2; } $stream = ssh2_exec($connection, 'ls -d '.$directory.'/' ); $errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR); // Enable blocking for both streams stream_set_blocking($errorStream, true); stream_set_blocking($stream, true); //if error is raised, $directory does not exist>no problem (in case of an analysis which do not have associated files if(stream_get_contents($errorStream)) { // Close the streams fclose($errorStream); fclose($stream); return 0; } $stream = ssh2_exec($connection, 'find '.$directory.' -maxdepth 1 -not -name "*.html" -not -name "*.png" -type f -exec rm -f {} \;'); $errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR); // Enable blocking for both streams stream_set_blocking($errorStream, true); stream_set_blocking($stream, true); //if error is raised, connected user does not have the right to delete the files if(stream_get_contents($errorStream)) { // Close the streams fclose($errorStream); fclose($stream); return 1; }else{ // Close the streams fclose($errorStream); fclose($stream); return 0; } } } /** * Return the string representation of a byte * * @param string $size */ static function get_octet_string_representation($size) { $octets_link = array("Bytes", "kB", "MB", "GB", "TB", "PB", "EB", "ZB"); $p = (int)(ceil((float)(strlen(strval($size)))/(float)(3) - (float)(1))); $pow_needed = $p * 10; $pow_needed = pow(2, $pow_needed); $value = (float)($size)/(float)($pow_needed); $tmp = explode(".", $value); $value = $tmp[0].".".substr($tmp[1], 0, 2); $value = $value." ".$octets_link[$p]; return $value; } static function get_purge_mail($project_name, $project_id, $nb_run_purgeable,$nb_analyse_purgeable, $demand_id, $purgeable_size, $project_size, $purge_delay, $url_price, $min_extention_duration, $min_extention_size, $emails, $runs_list, $analyse_list) { $template_mail= ' Data Purge on nG6

Dear user,

The two years data storage period of part or whole ###PROJECT_NAME### project (###nb_run### run(s) and ###nb_analyse### analyse(s)) is ended.

This purge alert No ###DEMAND_ID### corresponds to ###SIZE_STR### see list of impacted files below.

In ###PURGE_DELAY### days, your data will be deleted. Keep us informed if you wish to extend the storage period, please have a look at our storage fees (###URL_PRICE###) and reply to this email with the following information:

----------------------------------------------

----------------------------------------------

###EMAILS###We would appreciate if you could group your demands by laboratory.

Yours sincerely

nG6 team (for GeT-Genotoul and Bioinfo-Genotoul facilities)

Files impacted of:

'; $size_str = $purgeable_size . " purgeable data on ". $project_size ." of whole project"; if ($purgeable_size == $project_size) { $size_str = "whole project (".$purgeable_size . ")"; } $search=array("###nb_run###","###nb_analyse###","###PROJECT_NAME###","###PROJET_ID###","###DEMAND_ID###", "###EMAILS###","###RUNS_LIST###","###ANALYSES_LIST###","###SIZE_STR###", "###EXTENSION_DURATION###","###EXTENSION_SIZE###","###URL_PRICE###","###PURGE_DELAY###"); $replace=array($nb_run_purgeable,$nb_analyse_purgeable,$project_name,$project_id, $demand_id , $emails, $runs_list, $analyse_list, $size_str, $min_extention_duration, $min_extention_size, $url_price, $purge_delay); $mail=str_replace($search, $replace, $template_mail); return $mail; } /** * hash password (using default encryption method) * @param string $password * @return hashed password */ static function hash_password($password){ if (\TYPO3\CMS\Saltedpasswords\Utility\SaltedPasswordsUtility::isUsageEnabled('FE')) { $objSalt = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance(NULL); if (is_object($objSalt)) { $saltedPassword = $objSalt->getHashedPassword($password); } } return $saltedPassword; } static function get_ssh_connection($user_login, $user_pwd){ $connection = ssh2_connect('127.0.0.1', 22); if (!$connection) return NULL; if (!ssh2_auth_password($connection, $user_login, $user_pwd)) return NULL; return $connection ; } static function get_config_content(){ $config_file_path = __DIR__."/../../../application.properties"; $config_file_content = file_get_contents($config_file_path); return($config_file_content); } static function get_retention_policy_from_space_id($space_id = "default"){ $config_file_content = tx_nG6_utils::get_config_content(); //To explode the file content by line, we use PHP_EOL $array_config_content = explode(PHP_EOL, $config_file_content); $space_id_retention_config = array(); $pattern_to_find = "[space_".$space_id."]"; $is_space_id_found = FALSE; //We want to return the space id config as an array foreach($array_config_content as $line_id => $line_content){ //if we found the right section if($line_content == $pattern_to_find){ $is_space_id_found = TRUE; } //If we enter a new section, we stop the line copying if($is_space_id_found && $line_content != $pattern_to_find && strpos($line_content,'[')===0 ){ $is_space_id_found = FALSE; } if($is_space_id_found){ $space_id_retention_config[] = $line_content; } } return($space_id_retention_config); } static function get_project_retention_policy( $project_id ){ $space_id = tx_nG6_db::get_project_space_id($project_id); $retention_policy = tx_nG6_utils::get_retention_policy_from_space_id($space_id); $retention_line = ""; $policy = ""; $retention_value = ""; $return_text = ""; foreach($retention_policy as $line => $line_content){ if( strpos($line_content,"retention") === 0 ){ $retention_line = $line_content; } } if( is_numeric(strpos($retention_line,"date"))){ $policy = "date"; }elseif( is_numeric(strpos($retention_line,"period"))){ $policy = "period"; } $split_retention_line = explode('=',$retention_line); $retention_value = $split_retention_line[1]; if($policy == "date"){ $date = date_create_from_format("d/m/Y", $retention_value); $nice_date = date_format($date, 'l jS \of F Y'); $return_text = " until ".$nice_date; }elseif($policy == "period"){ $return_text = " for ".$retention_value." months"; } return($return_text); } //Return a php array with array[space_name]=space_name, containing all available space_ids static function get_available_space_ids (){ $config_file_content = tx_nG6_utils::get_config_content(); //To explode the file content by line, we use PHP_EOL $array_config_content = explode(PHP_EOL, $config_file_content); $ret = array(); foreach($array_config_content as $line => $line_content){ //If the current line starts with "[space_" if( strpos($line_content,'[space_') === 0 ){ $space_id = str_replace('[space_','',$line_content); $space_id = str_replace(']','',$space_id); $space_id = trim($space_id); $ret[$space_id]=$space_id; } } return($ret); } static function convert_epoch_timestamp_to_nice_date( $epoch_date ){ //return(gmdate('D, d M Y', $epoch_date)); return(gmdate('l jS \of F Y', $epoch_date)); } } if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/nG6/class.tx_nG6_utils.php']) { include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/nG6/class.tx_nG6_utils.php']); } 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']); } ?>