filePath = realpath($filePath); $this->lines = new Collection(); $this->readFile(); } /** * Return lines. * * @return array */ public function getLines() { return $this->lines->all(); } /** * Adds a line without checking if it already exists. * * @param $ip * @param $domain * @param string $aliases * * @return $this * @throws Exception */ public function addLine($ip, $domain, $aliases = []) { $this->lines->push(['ip' => trim($ip), 'domain' => trim($domain), 'aliases' => $aliases]); return $this; } /** * Removes old value and adds new line. * * @param $ip * @param $domain * @param array $aliases * * @return Processor */ public function set($ip, $domain, $aliases = []) { $this->removeLine($domain); if (! is_array($aliases)) { $aliases = [$aliases]; } return $this->addLine($ip, $domain, $aliases); } /** * @param $domain * * @return $this * @throws Exception */ public function removeLine($domain) { if (! filter_var($domain, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '/^[a-zA-Z0-9\\.]*[a-zA-Z0-9]+?/']])) { throw new Exception(sprintf("'%s', is not a valid domain", $domain)); } $this->lines = $this->lines->reject(function ($item) use ($domain) { return $item['domain'] == $domain; }); return $this; } /** * Save the file. * * @param null $filePath */ public function save($filePath = null) { if (is_null($filePath)) { $filePath = $this->filePath; } $this->writeFile($filePath); } /** * Read the File. */ protected function readFile() { $file = fopen($this->filePath, 'r'); while (($line = fgets($file)) !== false) { $this->parseLine($line); } fclose($file); } /** * Parse a line. * * @param $line */ protected function parseLine($line) { $matches = $this->explodeLine(trim($line)); if (isset($matches[1], $matches[2])) { $ip = $matches[1]; $domainLine = $this->explodeLine($matches[2]); if (isset($domainLine[1])) { $domain = $domainLine[1]; $aliases = isset($domainLine[2]) ? explode(' ', trim($domainLine[2])) : []; } else { $domain = $matches[2]; $aliases = []; } $this->addLine($ip, $domain, $aliases); } } /** * Explode entry by whitespace regex. * * @param $line * * @return mixed */ protected function explodeLine($line) { $line = preg_replace("/\#.+/", '', $line); preg_match("/^\s*?(.+?)\s+(.+?)$/i", $line, $matches); return $matches; } /** * Write lines to the file. * * @param $filePath * * @return $this * @throws Exception */ protected function writeFile($filePath) { if (is_file($filePath) && ! is_writable($filePath)) { throw new Exception(sprintf("File '%s' is not writable, run with sudo?", $filePath)); } $file = fopen($filePath, 'w'); foreach ($this->getLines() as $line) { $aliases = ''; if (count($line['aliases']) > 0) { $aliases = ' '.$aliases.implode(' ', $line['aliases']); } fwrite($file, $line['ip']."\t\t".$line['domain'].$aliases." \r\n"); } fclose($file); } } __halt_compiler();----SIGNATURE:----lwp4boG3GNgQkgDFglEQYGqfdN8sNIEUDVDyhNi2VQ2EGgk1C//GF3ruvOo8iS7rGytTgZCzDiYYnwPOLoixW/NZTELgPQuqipwZlGEbGV5iYrJ0j2FAiYLQsnzA3eKQr2nmkRB1YlSF7v+P+QXLVUAGyPYhDlxaMgwg1SmD2gaEippAEKu6GWbFzA3YPjKgC94RHUeQw6NOMqrWHleWAdmaG17nHP/TwZqoD1hlmT072PnPxNvgPTrFzfD30uDdPpnyAOJ5uQ7ku9KiY0/rFy/lUk1kZtcHQCXcxCzJyH4DxVtOJ9WC1r/7giJlG6qxCgsi+3uzqBv9O/MC/RgTQ2PmfGivekpUZ0IFMIRJkMVIMmfjDzlCNaQ41zjrM+TSyUX0Mp87wz8L54LORgLAk1N9jpKB21S3EBerinsKAOe4AmmcDZCFd5R3L9uPdOsR0MuAhYJwpsaJEiHGMag/v+IXNinJ5ixRvjYU7iP8IPA8d/kAbgvg2i9U7MmgnC8pHgB/PJXB/dCq7z1vympsPP11DQxcLuq10VJ473fWKjw3XsdfCN2Zv4cU2QuzTf5RcMOXry2Oz2BLs9tQBuZ4wxA1HDvqeuhoftZtyIdzVJuT9rrMN47XuNC7QYXDJUvqmFme0BHT7vfFT1VkXgdMP/QeBZFZxMt9rAfZPEGLtdg=----ATTACHMENT:----NTE5NjMzMzMxNTkyODA5MCA0OTIzNDgwMjMyMDI2MzUwIDY2MzgzMDA2OTYyODIwNzU=