flagDir = $filesystem->getDirectoryWrite(self::FLAG_DIR); } /** * Checks whether mode is on * * Optionally specify an IP-address to compare against the white list * * @param string $remoteAddr * @return bool */ public function isOn($remoteAddr = '') { if (!$this->flagDir->isExist(self::FLAG_FILENAME)) { return false; } $info = $this->getAddressInfo(); return !in_array($remoteAddr, $info); } /** * Sets maintenance mode "on" or "off" * * @param bool $isOn * @return bool */ public function set($isOn) { if ($isOn) { return $this->flagDir->touch(self::FLAG_FILENAME); } if ($this->flagDir->isExist(self::FLAG_FILENAME)) { return $this->flagDir->delete(self::FLAG_FILENAME); } return true; } /** * Sets list of allowed IP addresses * * @param string $addresses * @return bool * @throws \InvalidArgumentException */ public function setAddresses($addresses) { $addresses = (string)$addresses; if (empty($addresses)) { if ($this->flagDir->isExist(self::IP_FILENAME)) { return $this->flagDir->delete(self::IP_FILENAME); } return true; } if (!preg_match('/^[^\s,]+(,[^\s,]+)*$/', $addresses)) { throw new \InvalidArgumentException("One or more IP-addresses is expected (comma-separated)\n"); } $result = $this->flagDir->writeFile(self::IP_FILENAME, $addresses); return false !== $result ? true : false; } /** * Get list of IP addresses effective for maintenance mode * * @return string[] */ public function getAddressInfo() { if ($this->flagDir->isExist(self::IP_FILENAME)) { $temp = $this->flagDir->readFile(self::IP_FILENAME); return explode(',', trim($temp)); } else { return []; } } }