public function notifyListener(Event $event) { foreach ($this->eventListener as $listener) { $event->setEventMessage($event->getEventMessage() . "<br>"); echo $event->getEventMessage(); $listener->processEvent($event); } }
/** * This method handles all form submissions. */ private function handlePost() { $session = $this->getSession(false); $parameter = $this->getParameter(); $event = null; // Check if we have submitted the right form. if ($parameter['random_key'] === $session['random_key']) { if (isset($_POST['submit_create'])) { $url = $_POST['input_field']; if (\Validator::isValidURL($url)) { // Check now if url exists and if it redirects $url = \Validator::getValidatedURL($url); // check if url even exists before we move on if ($this->isUrlExist($url)) { // check if target url redirects $allowRedirect = $this->config['shortener']['redirect'] === 'on'; if ($allowRedirect || !$this->isUrlRedirect($url)) { /* * Check if we have already a record for this target url */ if ($this->urlShortenator->targetURLExists(\Validator::getValidatedURL($url))) { $event = new Event(Event::WARNING); $event->setEventMessage("A shortened URL with of this target already exists. You can redirect a URL only once."); $this->eventManager->notifyListener($event); $this->viewController->errorAction($this->getPageInformation(), $this->eventList); } else { $shortURL = $this->urlShortenator->generateShortURL($url); $data = $this->getPageInformation(); $data["shortURL"] = $shortURL; $this->viewController->shortGeneratedAction($data, $this->eventList); } } else { $event = new Event(Event::WARNING); $event->setEventMessage("Given URL points to a redirecting node which is permitted."); $this->eventManager->notifyListener($event); $this->viewController->errorAction($this->getPageInformation(), $this->eventList); } } else { $event = new Event(Event::ERROR); $event->setEventMessage("The URL '{$url} doesn't exist or produces an error."); $this->eventManager->notifyListener($event); $this->viewController->errorAction($this->getPageInformation(), $this->eventList); } } else { $event = new Event(Event::ERROR); $event->setEventMessage("Given URL has not the right format and doesn't validate to a URL."); $this->eventManager->notifyListener($event); $this->viewController->errorAction($this->getPageInformation(), $this->eventList); } } else { // This is the case where we want to look up a shortURL or a targetURL if (isset($_POST['submit_lookup'])) { $url = \Validator::getValidatedURL($_POST['input_field']); $pos = strpos($url, $this->config['shortener']['targetURL']); // we probably have a targetURL as input and are looking for the shortURL $data = $this->getPageInformation(); $data['lookup'] = $url; if ($pos === false) { $shortURLs = $this->urlShortenator->getShortenURL($url); if ($shortURLs !== false) { $shortURL = $shortURLs; $data['match_found'] = true; $data['match'] = $shortURL; } else { $data['match_found'] = false; } } else { // get Parameter from given shortURL $param = substr($url, strlen($this->config['shortener']['targetURL']) + 1); if (strlen($param) === 0) { $data['match_found'] = false; } else { $targetURLs = $this->urlShortenator->getTargetURLObject($param); if ($targetURLs !== false) { $data['match_found'] = true; $data['match'] = $targetURLs; } else { $data['match_found'] = false; } } } $this->viewController->matchResultAction($data, $this->eventList); } } } else { $event = new Event(Event::WARNING); $event->setEventMessage("An older form was submitted. Try again"); $this->eventManager->notifyListener($event); $this->viewController->welcomeAction($this->getPageInformation(), $this->eventList); } }
/** * This method returns all objects produced by given query of type $classname. * * @param string $query * the sql statement * @param string $className * name of the type that we expect * @return multitype:|boolean */ public function fetchAllShortURLs($query, $className) { $con = $this->getConnection(); if (isset($con)) { $stmt = $con->prepare($query); $stmt->execute(); $result = $stmt->fetchObject($className); return $result; } else { $event = new Event(Event::ERROR); $event->setEventMessage("Error Code: " . $stmt->errorCode() . " | " . $stmt->errorInfo()); $this->eventManager->notifyListener($event); return false; } }