. * * ------------------------------------------------------------------------ * * This file is used to manage the deploy mirror depend on location of * computer. * * ------------------------------------------------------------------------ * * @package FusionInventory * @author Walid Nouh * @author David Durieux * @copyright Copyright (c) 2010-2016 FusionInventory team * @license AGPL License 3.0 or (at your option) any later version * http://www.gnu.org/licenses/agpl-3.0-standalone.html * @link http://www.fusioninventory.org/ * @link https://github.com/fusioninventory/fusioninventory-for-glpi * */ if (!defined('GLPI_ROOT')) { die("Sorry. You can't access directly to this file"); } /** * Manage the deploy mirror depend on location of computer. */ class PluginFusioninventoryDeployMirror extends CommonDBTM { const MATCH_LOCATION = 0; const MATCH_ENTITY = 1; const MATCH_BOTH = 2; /** * We activate the history. * * @var boolean */ public $dohistory = true; /** * The right name for this class * * @var string */ static $rightname = 'plugin_fusioninventory_deploymirror'; /** * Get name of this type by language of the user connected * * @param integer $nb number of elements * @return string name of this type */ static function getTypeName($nb = 0) { return __('Mirror servers', 'fusioninventory'); } /** * Define tabs to display on form page * * @param array $options * @return array containing the tabs name */ function defineTabs($options = []) { $ong=[]; $this->addDefaultFormTab($ong) ->addStandardTab('Log', $ong, $options); return $ong; } /** * Get and filter mirrors list by computer agent and location. * Location is retrieved from the computer data. * * @global array $PF_CONFIG * @param integer $agents_id * @return array */ static function getList($agents_id) { global $PF_CONFIG, $DB; if (is_null($agents_id)) { return []; } $pfAgent = new PluginFusioninventoryAgent(); $pfAgent->getFromDB($agents_id); $agent = $pfAgent->fields; if (!isset($agent) || !isset($agent['computers_id'])) { return []; } $computer = new Computer(); $computer->getFromDB($agent['computers_id']); //If no configuration has been done in the plugin's configuration //then use location for mirrors as default //!!this should not happen!! if (!isset($PF_CONFIG['mirror_match'])) { $mirror_match = self::MATCH_LOCATION; } else { //Get mirror matching from plugin's general configuration $mirror_match = $PF_CONFIG['mirror_match']; } //Get all mirrors for the agent's entity, or for entities above //sorted by entity level in a descending way (going to the closest, //deepest entity, to the highest) $query = "SELECT `mirror`.*, `glpi_entities`.`level` FROM `glpi_plugin_fusioninventory_deploymirrors` AS mirror LEFT JOIN `glpi_entities` ON (`mirror`.`entities_id`=`glpi_entities`.`id`) WHERE `mirror`.`is_active`='1'"; $query .= getEntitiesRestrictRequest(' AND ', 'mirror', 'entities_id', $agent['entities_id'], true); $query .= " ORDER BY `glpi_entities`.`level` DESC"; //The list of mirrors to return $mirrors = []; foreach ($DB->request($query) as $result) { //First, check mirror by location if (in_array($mirror_match, [self::MATCH_LOCATION, self::MATCH_BOTH]) && $computer->fields['locations_id'] > 0 && $computer->fields['locations_id'] == $result['locations_id']) { $mirrors[] = $result['url']; } //Second, check by entity if (in_array($mirror_match, [self::MATCH_ENTITY, self::MATCH_BOTH])) { $entities = $result['entities_id']; //If the mirror is visible in child entities then get all child entities //and check it the agent's entity is one of it if ($result['is_recursive']) { $entities = getSonsOf('glpi_entities', $result['entities_id']); } $add_mirror = false; if (is_array($entities) && in_array($computer->fields['entities_id'], $entities)) { $add_mirror = true; } else if ($computer->fields['entities_id'] == $result['entities_id']) { $add_mirror = true; } if (!in_array($result['url'], $mirrors) && $add_mirror) { $mirrors[] = $result['url']; } } } //add default mirror (this server) if enabled in config $entities_id = 0; if (isset($agent['entities_id'])) { $entities_id = $agent['entities_id']; } //If option is set to yes in general plugin configuration //Add the server's url as the last url in the list if (isset($PF_CONFIG['server_as_mirror']) && $PF_CONFIG['server_as_mirror'] == true) { $mirrors[] = PluginFusioninventoryAgentmodule::getUrlForModule('DEPLOY', $entities_id) ."?action=getFilePart&file="; } return $mirrors; } /** * Display form * * @global array $CFG_GLPI * @param integer $id * @param array $options * @return true */ function showForm($id, $options = []) { global $CFG_GLPI; $this->initForm($id, $options); $this->showFormHeader($options); echo "