* 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= '
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: