Rev 283 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
<?phpdeclare(strict_types=1);namespace LeadersLinked\Plugin;use Laminas\Mvc\Controller\Plugin\AbstractPlugin;use Laminas\Db\Adapter\AdapterInterface;use Laminas\Authentication\AuthenticationService;use LeadersLinked\Model\Device;use LeadersLinked\Mapper\DeviceMapper;use LeadersLinked\Model\UserType;use LeadersLinked\Mapper\NetworkMapper;class CurrentNetworkPlugin extends AbstractPlugin{/**** @var \LeadersLinked\Plugin\CurrentNetworkPlugin*/private static $_instance;/**** @var AdapterInterface $adapter*/private $adapter;/**** @var boolean*/private $hasNetwork;/**** @return \LeadersLinked\Model\Network*/private $network;/**** @var string*/private $hostname;/**** @param AdapterInterface $adapter* @return \LeadersLinked\Plugin\CurrentNetworkPlugin*/public static function getInstance($adapter){if(self::$_instance == null) {self::$_instance = new CurrentNetworkPlugin($adapter);}return self::$_instance;}/**** @param AdapterInterface $adapter*/private function __construct($adapter){$this->adapter = $adapter;$this->hasNetwork = false;$networkMapper = NetworkMapper::getInstance($adapter);$request_uri = empty($_SERVER['REQUEST_URI']) ? '' : $_SERVER['REQUEST_URI'];if($request_uri) {if(substr($request_uri, 0, 1) == '/') {$request_uri = substr($request_uri, 1);}$parts = explode('/', $request_uri);if($parts[0] == 'services') {$this->network = $networkMapper->fetchOneByDefault();if($this->network) {$this->hostname = $this->network->main_hostname;$this->hasNetwork = true;}}}if(!$this->hasNetwork) {$hostname = empty($_SERVER['HTTP_ORIGIN']) ? '' : $_SERVER['HTTP_ORIGIN'];if(empty($hostname)) {$hostname = empty($_SERVER['HTTP_REFERER']) ? '' : $_SERVER['HTTP_REFERER'];if(empty($hostname)) {$hostname = empty($_SERVER['HTTP_HOST']) ? '' : $_SERVER['HTTP_HOST'];}}$hostname = trim(str_replace(['https://', 'http://'], '', $hostname));//echo $hostname; exit;$parts = explode('/', $hostname);$hostname = $parts > 1 ? $parts[0] : $hostname;$networkMapper = NetworkMapper::getInstance($adapter);$this->network = $networkMapper->fetchOneByHostnameForFrontend($hostname);if($this->network) {$this->hostname = $this->network->main_hostname == $hostname ? $this->network->main_hostname : $this->network->alternative_hostname;$this->hasNetwork = true;}}}/**** @return \LeadersLinked\Model\Network*/public function getNetwork(){if($this->hasNetwork) {return $this->network;} else {return null;}}/**** @return int*/public function getNetworkId(){if($this->hasNetwork) {return $this->network->id;} else {return 0;}}/**** @return boolean*/public function hasNetwork(){return $this->hasNetwork;}public function getHostname() : string{return $this->hostname;}public function fetchDefaultNetwork(){$networkMapper = NetworkMapper::getInstance($this->adapter);$this->network = $networkMapper->fetchOneByDefault();if($this->network) {$this->hasNetwork = true;$this->hostname = $this->network->main_hostname;}}}