public function isValidUrl(Url $url) { $parseUrl = parse_url($url->getUrl()); //if url is for this domain - deny if ($parseUrl['host'] == $this->hostName) { return false; } return true; }
/** * {@inheritdoc} */ public function shorten($url) { $entity = $this->em->getRepository('AppBundle:Url')->findOneByTarget($url); if (!$entity) { $entity = new Url(); $entity->setId($this->generateId($url)); $entity->setTarget($url); $this->em->persist($entity); $this->em->flush(); } return $this->router->generate($this->redirectRoute, ['id' => $entity->getId()], true); }
public function reverseTransform($urlString) { if (!$urlString) { return null; } $url = $this->em->getRepository('AppBundle:Url')->findOneBy(array('urlKey' => md5($urlString))); if (!$url instanceof Url) { $url = new Url(); $url->setUrl($urlString); } return $url; }
/** * @Route("/add", name="bookmark_add") */ public function addAction(Request $request) { $em = $this->getDoctrine()->getManager(); $bookmark = new Bookmark(); $bookmark->setUser($this->getUser()); // Url-Parameter $urlParameter = $request->get('url', null); $titleParameter = $request->get('title', null); if (!is_null($urlParameter)) { $url = new Url(); $url->setUrl($urlParameter); $bookmark->setUrl($url); $validator = $this->get('validator'); $errors = $validator->validateValue($urlParameter, new Constraints\Url()); if ($errors->count() != 0) { foreach ($errors as $error) { $this->addFlash('danger', $error); } } else { $tagTransformer = new TagTransformer($em); $bookmark->setTitle($titleParameter); $url->setUrl($urlParameter); $tags = $tagTransformer->reverseTransform($tagTransformer->titleToTags($titleParameter)); if (count($tags) != 0) { foreach ($tags as $tag) { $bookmark->addTag($tag); } } } } $bookmarkForm = $this->createForm('app_bookmark', $bookmark, array('action' => $this->generateUrl('bookmark_add', array('nowindow' => $request->get('nowindow', null))), 'nowindow' => $request->get('nowindow', null)))->handleRequest($request); if ($bookmarkForm->isValid()) { $em->persist($bookmark); $em->flush(); if ($bookmarkForm->has('nowindow') && !is_null($bookmarkForm->get('nowindow')->getData())) { return new Response('<script>window.close();</script>'); } $this->addFlash('success', 'flashbag_bookmark_add_success'); return $this->redirectToRoute('homepage'); } return $this->render('Default/add.html.twig', array('bookmarkForm' => $bookmarkForm->createView())); }
/** * For the URL entity, customize the FormBuilder with dynamic elements. * * {@inheritdoc} * * @see JavierEguiluz\Bundle\EasyAdminBundle\Controller\AdminController::createEntityFormBuilder */ public function createUrlEntityFormBuilder($entity, $view) { $builder = parent::createEntityFormBuilder($entity, $view); // Add the dynamic choices for the redirection codes $redirectionCodeField = $builder->get('redirectionCode'); $type = $redirectionCodeField->getType(); $options = $redirectionCodeField->getOptions(); $options['choices'] = array_combine(Url::getAvailableRedirectionCodes(), Url::getAvailableRedirectionCodes()); unset($options['choice_list']); $builder->add($redirectionCodeField->getName(), $type->getName(), $options); return $builder; }
/** * @Route("/{slug}", name="minified_url") * * @param Url $url * @param Request $request * * @return Response */ public function minifiedUrlAction(Url $url, Request $request) { $now = new \DateTime(); // We need to check that: // * The url is active // * If there's a start or end date, the date corresponds (that "now" is between "start" and "end") // * If there's a host, it matches the current host (the app can be multidomain) if ($url->isActive() && (!$url->getHost() || $url->getHost() && $url->getHost() === $request->getHost()) && (!$url->getDateStart() || $url->getDateStart() && $now > $url->getDateStart()) && (!$url->getDateEnd() || $url->getDateEnd() && $now < $url->getDateEnd())) { return $this->redirect($url->getRedirectUrl(), $url->getRedirectionCode()); } // If the url has to redirect to home, we do it if ($url->redirectToHomeIfInactive()) { return $this->redirect($this->getParameter('home_redirect_url')); } // If nothing is ok, it means that the url should return a 404 error. throw $this->createNotFoundException(); }