public function postGif(Gif $gif) { $gifUrl = $this->router->generate('gif', ['permalink' => $gif->getPermalink()], true); $gifUrl = Util::fixSymfonyGeneratedURLs($gifUrl); $tweetContent = $gif->generateTweet($gifUrl); return $this->postTweet($tweetContent); }
/** * Find one gif from its URL in a Gif array * @param $gifs * @param $url * @return Gif|null */ private static function findGif($gifs, $url) { /** @var Gif $gif */ foreach ($gifs as $gif) { if (Util::endsWith($url, $gif->getPermalink())) { return $gif; } } return null; }
public function postGif(Gif $gif) { $appId = $this->container->getParameter('facebook_app_id'); $appSecret = $this->container->getParameter('facebook_app_secret'); $accessToken = $this->container->getParameter('facebook_access_token'); FacebookSession::setDefaultApplication($appId, $appSecret); // Open Facebook SDK session $session = FacebookSession::newAppSession(); // To validate the session: try { $session->validate(); } catch (FacebookRequestException $ex) { // Session not valid, Graph API returned an exception with the reason. //echo $ex->getMessage(); return false; } catch (\Exception $ex) { // Graph API returned info, but it may mismatch the current app or have expired. //echo $ex->getMessage(); return false; } $link = $this->router->generate('gif', ['permalink' => $gif->getPermalink()], true); $link = Util::fixSymfonyGeneratedURLs($link); try { $requestParaps = ['access_token' => $accessToken, 'link' => $link, 'message' => $gif->getCaption()]; // Only provide picture if this is a gif if ($gif->getFileType() == 'gif') { $requestParaps['picture'] = $gif->getGifUrl(); } $facebookRequest = new FacebookRequest($session, 'POST', '/joiesDeSupinfo/feed', $requestParaps); /*$response = */ $facebookRequest->execute()->getGraphObject(); //echo "Posted with id: " . $response->getProperty('id'); } catch (FacebookRequestException $e) { //echo "Exception occured, code: " . $e->getCode(); //echo " with message: " . $e->getMessage(); return false; } return true; }
/** * Downloads a gif on the server. Useful if our domain is not accepted as referrer (avoids hotlinking) * @param Gif $gif * @return string */ public function download(Gif $gif) { $downloadDir = $this->getDownloadDir(); $gifUrl = $gif->getGifUrl(); // Generate filename $fileName = $gif->getPermalink() . '.' . Util::getFileExtension($gifUrl); $i = 0; while (file_exists($downloadDir . $fileName)) { $fileName = $gif->getPermalink() . '_' . $i . '.' . Util::getFileExtension($gifUrl); $i++; } // Download file file_put_contents($downloadDir . $fileName, fopen($gifUrl, 'r')); // Check if file has been successfully downloaded (permissions issues) if (file_exists($downloadDir . $fileName)) { // Generate client-side URL $url = $this->requestContextScheme . '://' . $this->requestContextHost . $this->requestContextBaseUrl . '/gifs/' . $fileName; $gif->setOriginalGifUrl($gifUrl); $gif->setGifUrl($url); return $url; } else { return false; } }
public function getFileType() { return Util::getFileExtension($this->getGifUrl()); }
public function relativeDate(\DateTime $datetime) { return Util::relativeTime($datetime); }
/** * @Route("/submit", name="submit", options={"sitemap"=true}) */ public function submitAction(Request $request) { $em = $this->getDoctrine()->getManager(); $response = new Response(); $gifSubmitted = false; $gifSubmittedError = false; // Form is submitted $post = $request->request; if ($post->has('caption')) { // Check if mandatory fields are filled up if (trim($post->get('submittedBy')) == '' || trim($post->get('caption')) == '' || trim($post->get('gifUrl')) == '') { $gifSubmittedError = "un des champs requis n'est pas renseigné, veuillez rééssayer."; } // Check if URL is a gif/mp4 video $allowedFilesTypes = ['gif', 'mp4', 'webm', 'ogg']; $gifUrl = $post->get('gifUrl'); if (!in_array(Util::getFileExtension($gifUrl), $allowedFilesTypes)) { $gifSubmittedError = "l'URL ne semble pas être celle d'un fichier gif. Les types autorisés sont : gif, mp4, webm et ogg."; } $gifSubmitted = true; $submittedBy = $post->get('submittedBy'); $caption = $post->get('caption'); $source = $post->get('source'); $label = $post->get('label'); // Create cookie with submittedBy value $cookie = new Cookie('submittedBy', $submittedBy, time() + 60 * 60 * 24 * 30); $response->headers->setCookie($cookie); if ($gifSubmittedError === false) { $gif = new Gif(); $gif->setCaption($caption); $gif->setGifUrl($gifUrl); $gif->setReportStatus(ReportState::NONE); $gif->setGifStatus(GifState::SUBMITTED); $gif->generateUrlReadyPermalink(); $gif->setSubmissionDate(new \DateTime()); $gif->setSubmittedBy($submittedBy); $gif->setSource($source); $gif->setLabel($label); $em->persist($gif); $em->flush(); $gifRepo = $this->getDoctrine()->getRepository('LjdsBundle:Gif'); $params['estimatedPublishDate'] = $gifRepo->getEstimatedPublicationDate(); } else { $params['submitError'] = $gifSubmittedError; } } $params['submittedBy'] = $request->cookies->has('submittedBy') ? $request->cookies->get('submittedBy') : ''; $params['submitted'] = $gifSubmitted; $response->setContent($this->renderView('LjdsBundle:Gifs:submit.html.twig', $params)); return $response; }