/** * Initialize the controller. * * @return $this Method allows chiaining * * @since 1.0 * @throws \RuntimeException */ public function initialize() { $this->debug = $this->getContainer()->get('app')->get('debug.hooks'); // Initialize the logger $this->logger = new Logger('JTracker'); $this->logger->pushHandler(new StreamHandler($this->getContainer()->get('app')->get('debug.log-path') . '/github_' . strtolower($this->type) . '.log')); // Get the event dispatcher $this->dispatcher = $this->getContainer()->get('app')->getDispatcher(); // Get a database object $this->db = $this->getContainer()->get('db'); // Get the payload data $data = $this->getContainer()->get('app')->input->post->get('payload', null, 'raw'); if (!$data) { $this->logger->error('No data received.'); $this->getContainer()->get('app')->close(); } // Decode it $this->hookData = json_decode($data); // Get the project data $this->getProjectData(); // If we have a bot defined for the project, prefer it over the DI object if ($this->project->gh_editbot_user && $this->project->gh_editbot_pass) { $this->github = GithubFactory::getInstance($this->getContainer()->get('app'), true, $this->project->gh_editbot_user, $this->project->gh_editbot_pass); } else { $this->github = $this->getContainer()->get('gitHub'); } // Check the request is coming from GitHub $validIps = $this->github->meta->getMeta(); if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $parts = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); $myIP = $parts[0]; } elseif (strpos($_SERVER['SCRIPT_NAME'], 'cli/tracker.php') !== false) { $myIP = '127.0.0.1'; } else { $myIP = $this->getContainer()->get('app')->input->server->getString('REMOTE_ADDR'); } if (!IpHelper::ipInRange($myIP, $validIps->hooks, 'cidr') && '127.0.0.1' != $myIP) { // Log the unauthorized request $this->logger->error('Unauthorized request from ' . $myIP); $this->getContainer()->get('app')->close(); } // Set up the event listener $this->addEventListener(); return $this; }
/** * Tests the \JTracker\Helper\IpHelper::ipInRange method for a bad type parameter * * @return void * * @covers \JTracker\Helper\IpHelper::ipInRange * @covers \JTracker\Helper\IpHelper::convertValues * @expectedException \InvalidArgumentException * @since 1.0 */ public function testIpInRangeException() { IpHelper::ipInRange('192.168.0.32', array('192.168.0.0/24'), 'decimal'); }