query->get('url'); $format = (string)$request->query->get('format'); $___parsedUrl = parse_url($url); if ($___parsedUrl['path'] == '' || $___parsedUrl['path'] == '/') { $templateName = 'index'; } else { $templateName = $this->staticSiteBuilder->convertPathToFilename($___parsedUrl['path']); } $path = $this->staticSiteBuilder->projectDir . $this->staticSiteBuilder->staticVarPath . $this->staticSiteBuilder->oembedJsonPath . $templateName; $contentType = 'application/json'; if ($format == 'xml') { $path = $this->staticSiteBuilder->projectDir . $this->staticSiteBuilder->staticVarPath . $this->staticSiteBuilder->oembedXmlPath . $templateName; $contentType = 'text/xml'; } if (is_file($path)) { return $response = new Response( file_get_contents($path), Response::HTTP_OK, [ 'content-type' => $contentType ] ); } return throw $this->createNotFoundException(); } #[Route('/', name: 'static_home')] public function index(Request $request): Response { $___response = $this->checkPost($request); if ($___response != null) { return $___response; } //check for search param... homepage currently only page with search bar $searchPhrase = (string)$request->query->get('s'); //wordpress search query if ($searchPhrase != '') { $currentPage = (int)$request->query->get('p'); //our search pagination query if ($currentPage == 0) { $currentPage = 1; } return $this->redirectToRoute( 'search_page', [ 's' => $searchPhrase, 'p' => $currentPage ] ); } //check for shortlink url $id = (int)$request->query->get('p'); //wordpress page id query if ($id > 0) { //lookup id in meta id directory and redirect if found $metaIdFile = $this->staticSiteBuilder->projectDir . $this->staticSiteBuilder->metaIdsPath . $id; if (file_exists($metaIdFile)) { $templateName = trim(file_get_contents($metaIdFile)); //convert templateName into url $localUrl = str_replace($this->staticSiteBuilder->slashReplace, '/', $templateName); return $this->handleLocalRedirect($localUrl); } else { return $this->redirectToRoute('static_home'); } } return $this->render('/static/index.html'); } #[Route('/{path1}/', name: 'static_path1')] public function path1(Request $request, $path1): Response { $___response = $this->checkPost($request); if ($___response != null) { return $___response; } $localPath = '/static/' . $this->staticSiteBuilder->convertPathToFilename($path1); if ($this->loader->exists($localPath . '.html')) { return $this->render($localPath . '.html'); } $___path = $path1 . '/'; return $this->handleNotFoundException($___path); } #[Route('/{path1}/{path2}/', name: 'static_path2')] public function path2(Request $request, $path1, $path2): Response { $___response = $this->checkPost($request); if ($___response != null) { return $___response; } $localPath = '/static/' . $this->staticSiteBuilder->convertPathToFilename($path1 . '/' . $path2); if ($this->loader->exists($localPath . '.html')) { return $this->render($localPath . '.html'); } $___path = $path1 . '/' . $path2 . '/'; return $this->handleNotFoundException($___path); } #[Route('/{path1}/{path2}/{path3}/', name: 'static_path3')] public function path3(Request $request, $path1, $path2, $path3): Response { $___response = $this->checkPost($request); if ($___response != null) { return $___response; } $localPath = '/static/' . $this->staticSiteBuilder->convertPathToFilename( $path1 . '/' . $path2 . '/' . $path3 ); if ($this->loader->exists($localPath . '.html')) { return $this->render($localPath . '.html'); } $___path = $path1 . '/' . $path2 . '/' . $path3 . '/'; return $this->handleNotFoundException($___path); } #[Route('/{path1}/{path2}/{path3}/{path4}/', name: 'static_path4')] public function path4(Request $request, $path1, $path2, $path3, $path4): Response { $___response = $this->checkPost($request); if ($___response != null) { return $___response; } $localPath = '/static/' . $this->staticSiteBuilder->convertPathToFilename( $path1 . '/' . $path2 . '/' . $path3 . '/' . $path4 ); if ($this->loader->exists($localPath . '.html')) { return $this->render($localPath . '.html'); } $___path = $path1 . '/' . $path2 . '/' . $path3 . '/' . $path4 . '/'; return $this->handleNotFoundException($___path); } #[Route('/{path1}/{path2}/{path3}/{path4}/{path5}/', name: 'static_path5')] public function path5(Request $request, $path1, $path2, $path3, $path4, $path5): Response { $___response = $this->checkPost($request); if ($___response != null) { return $___response; } $localPath = '/static/' . $this->staticSiteBuilder->convertPathToFilename( $path1 . '/' . $path2 . '/' . $path3 . '/' . $path4 . '/' . $path5 ); if ($this->loader->exists($localPath . '.html')) { return $this->render($localPath . '.html'); } $___path = $path1 . '/' . $path2 . '/' . $path3 . '/' . $path4 . '/' . $path5 . '/'; return $this->handleNotFoundException($___path); } public function handleNotFoundException(string $path): Response { //look up requested url on original website $origUrl = $this->staticSiteBuilder->contentDomain . $path; $ch = $this->curlService->initCurlWithHeaders($origUrl, true); // $ch = curl_init(); // curl_setopt($ch, CURLOPT_URL, $origUrl); // curl_setopt($ch, CURLOPT_HEADER, 1); // curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['CURLOPT_USERAGENT']); // curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); // curl_setopt($ch, CURLOPT_TIMEOUT, 30); // $cookies = tempnam('/tmp','cookie.txt'); // curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies); // curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies); $response = curl_exec($ch); $effectiveUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); //DA LOGIC! if ($origUrl == $effectiveUrl && (int)$httpcode == 200) { //page was found on content domain but not on this static domain... //this means we haven't created a template for it... //maybe it wasn't in sitemap, otherwise hidden, whatever... //grab it now, and try to load it //NOTE: unless permissions on public and template folders are correct this may not work correctly! //this is a web process so different user to build site command $this->staticSiteBuilder->fetchPage($effectiveUrl, true); } else if ($origUrl != $effectiveUrl && (int)$httpcode == 200) { //we were redirected successfully on the content domain //this means a redirect is in place on content domain that doesn't correspond to a template on this static domain //so just redirect } else { return throw $this->createNotFoundException(); } return $this->handleLocalRedirect($effectiveUrl); } public function createLocalRedirectParams(string $contentDomainUrl) { $parsedUrl = parse_url($contentDomainUrl); $arr = [ 'request' => null ]; $counter = 1; foreach (explode('/', $parsedUrl['path']) as $val) { $key = 'path' . $counter; $val = trim($val); if ($val) { $arr[$key] = $val; $counter++; } } return $arr; } public function handleLocalRedirect(string $effectiveUrl) { $redirectParams = $this->createLocalRedirectParams($effectiveUrl); //NOTE: count of redirect is one higher due to request object being first key switch (count($redirectParams)) { default: //if this happens we need more path depths! (5 seems to be enough though...) return throw $this->createNotFoundException(); break; case 1: return $this->redirectToRoute('static_home'); break; case 2: return $this->redirectToRoute('static_path1', $redirectParams); break; case 3: return $this->redirectToRoute('static_path2', $redirectParams); break; case 4: return $this->redirectToRoute('static_path3', $redirectParams); break; case 5: return $this->redirectToRoute('static_path4', $redirectParams); break; case 6: return $this->redirectToRoute('static_path5', $redirectParams); break; } } }