/src/AppBundle/Controller/PageController.php
https://gitlab.com/stanislav95/subdomains · PHP · 257 lines · 205 code · 43 blank · 9 comment · 20 complexity · 287f7a4a14ae9430734bed4d6a7a6d6a MD5 · raw file
- <?php
- /**
- * Created by PhpStorm.
- * User: yks2
- * Date: 24.11.16
- * Time: 11:20
- */
- namespace AppBundle\Controller;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Symfony\Component\HttpFoundation\Request;
- use AppBundle\Entity\Pages;
- class PageController extends Controller
- {
- public function viewPageAction(Request $request)
- {
- if ($this->findInDb("page/1", 1, $request->attributes->get('pageId'))['disallow'] == 1) {
- $current = 1;
- } else {
- $current = 0;
- }
- if ($this->findInDb("page/1", 2, $request->attributes->get('pageId'))['disallow'] == 1) {
- $all = 1;
- } else {
- $all = 0;
- }
- return $this->render('pages/1.html.twig', [
- 'url' => "page/1",
- 'current' => $current,
- 'all' => $all,
- ]);
- }
- public function handlePageAction(Request $request)
- {
- if ($request->get('disallowcurrent') == 1) {
- $this->insertInDb($request->get('urlpage'), 1, 1, $request->attributes->get('pageId'));
- $this->writeDataCurrentRobot($request, "page/1", $request->attributes->get('pageId'));
- } else {
- $this->deleteFromDb($request->get('urlpage'), 1, $request->attributes->get('pageId'));
- $this->deleteDataCurrentRobot($request, "page/1", $request->attributes->get('pageId'));
- }
- if ($request->get('disallowall') == 1) {
- $this->insertInDb($request->get('urlpage'), 1, 2, $request->attributes->get('pageId'));
- $this->writeDataAllRobot($request, "page/1", $request->attributes->get('pageId'));
- } else {
- $this->deleteFromDb($request->get('urlpage'), 2, $request->attributes->get('pageId'));
- $this->deleteDataAllRobot($request, "page/1", $request->attributes->get('pageId'));
- }
- return $this->redirect($request->headers->get('referer'));
- }
- public function getDomainName($id)
- {
- $repository = $this->getDoctrine()
- ->getRepository('AppBundle:Domains');
- $query = $repository->createQueryBuilder('d')
- ->select('d.domain')
- ->where("d.id = $id")
- ->getQuery();
- return $query->setMaxResults(1)->getOneOrNullResult()['domain'];
- }
- public function getDomainNames()
- {
- $repository = $this->getDoctrine()
- ->getRepository('AppBundle:Domains');
- $query = $repository->createQueryBuilder('d')
- ->select('d.domain')
- ->getQuery();
- return $query->getResult();
- }
- public function insertInDb($currentPage, $disallow, $type, $domainId)
- {
- if (!$this->withoutType($currentPage, $domainId)) {
- $page = new Pages();
- $page->setPage($currentPage);
- $page->setDisallow($disallow);
- $page->setType($type);
- $page->setDomainId(intval($domainId));
- $em = $this->getDoctrine()->getManager();
- $em->persist($page);
- $em->flush();
- } else {
- $em = $this->getDoctrine()->getEntityManager();
- $repository = $em->getRepository('AppBundle:Pages');
- $page = $repository->findOneBy(array('page' => $currentPage, 'domain_id' => $domainId));
- $page->setDisallow($disallow);
- $page->setType($type);
- $em->flush();
- }
- }
- public function deleteFromDb($currentPage, $type, $domainId)
- {
- $em = $this->getDoctrine()->getEntityManager();
- $repository = $em->getRepository('AppBundle:Pages');
- $product = $repository->findOneBy(array('page' => $currentPage, 'type' => $type, 'domain_id' => $domainId));
- if ($product != NULL) {
- $product->setType(0);
- $product->setDisallow(0);
- $em->flush();
- }
- }
- public function writeDataCurrentRobot($request, $url, $domain)
- {
- $file = fopen($request->server->get('DOCUMENT_ROOT') . '/robots.txt', 'r+');
- $robotData = $this->readDataCurrentRobot($request, $url);
- $currentPage = $this->currentPage($url, $this->getDomainName($domain));
- if (!in_array($currentPage[1], $robotData)) {
- foreach ($currentPage as $row) {
- array_push($robotData, $row);
- }
- }
- foreach ($robotData as $row) {
- fwrite($file, $row);
- }
- fclose($file);
- }
- public function deleteDataCurrentRobot($request, $url, $domain)
- {
- $robotData = $this->readDataCurrentRobot($request, $url);
- $currentPage = $this->currentPage($url, $this->getDomainName($domain));
- if (in_array($currentPage[1], $robotData)) {
- $key = array_search($currentPage[1], $robotData);
- unset($robotData[$key]);
- unset($robotData[$key - 1]);
- }
- $file = fopen($request->server->get('DOCUMENT_ROOT') . '/robots.txt', 'w+');
- foreach ($robotData as $row) {
- fwrite($file, $row);
- }
- fclose($file);
- return $robotData;
- }
- public function deleteDataAllRobot($request, $url, $domainId)
- {
- $domains = $this->getDomainNames();
- foreach ($domains as $domain) {
- $robotData = $this->readDataRobot($domain, $request);
- $currentPage = $this->currentPage($url, $this->getDomainName($domainId));
- // var_dump($currentPage[1]);
- // var_dump($robotData);
- // die();
- if (in_array($currentPage[1], $robotData)) {
- $key = array_search($currentPage[1], $robotData);
- unset($robotData[$key]);
- unset($robotData[$key - 1]);
- }
- $file = fopen($request->server->get('DOCUMENT_ROOT') . '/' . $domain["domain"] . '/' . 'robots.txt', 'w+');
- foreach ($robotData as $row) {
- fwrite($file, $row);
- }
- fclose($file);
- }
- return $robotData;
- }
- public function writeDataAllRobot($request, $url, $domainId)
- {
- $this->writeDataCurrentRobot($request, $url, $domainId);
- $domains = $this->getDomainNames();
- foreach ($domains as $domain) {
- $file = fopen($request->server->get('DOCUMENT_ROOT') . '/' . $domain["domain"] . '/' . 'robots.txt', 'r+');
- $robotData = $this->readDataRobot($domain, $request);
- $currentPage = $this->currentPage($url, $this->getDomainName($domainId));
- if (!in_array($currentPage[1], $robotData)) {
- foreach ($currentPage as $row) {
- array_push($robotData, $row);
- }
- }
- foreach ($robotData as $row) {
- fwrite($file, $row);
- }
- fclose($file);
- }
- }
- public function readDataCurrentRobot($request, $url)
- {
- $robotPath = $request->server->get('DOCUMENT_ROOT') . '/robots.txt';
- $robotData = file($robotPath);
- return $robotData;
- }
- public function readDataRobot($domain, $request)
- {
- $robotPath = $request->server->get('DOCUMENT_ROOT') . '/' . $domain['domain'] . '/' . 'robots.txt';
- $robotData = file($robotPath);
- return $robotData;
- }
- public function withoutType($page, $domainId)
- {
- $repository = $this->getDoctrine()
- ->getRepository('AppBundle:Pages');
- $query = $repository->createQueryBuilder('p')
- ->select('p.page, p.disallow')
- ->where("p.page = '$page' AND p.domain_id = $domainId")
- ->getQuery();
- return $query->setMaxResults(1)->getOneOrNullResult();
- }
- public function findInDb($page, $type, $domainId)
- {
- $repository = $this->getDoctrine()
- ->getRepository('AppBundle:Pages');
- $query = $repository->createQueryBuilder('p')
- ->select('p.page, p.disallow')
- ->where("p.page = '$page' AND p.type = $type AND p.domain_id = $domainId")
- ->getQuery();
- return $query->setMaxResults(1)->getOneOrNullResult();
- }
- private function currentPage($url, $domain)
- {
- $baseUrl = $this->container->get('router')->getContext()->getScheme() . '://' . $domain . '.' . $this->container->get('router')->getContext()->getHost();
- return ["User-agent: *\n", "Disallow: $baseUrl/$url\n"];
- }
- private function allPages($url, $domain)
- {
- $baseUrl = $this->container->get('router')->getContext()->getScheme() . '://' . $domain . '.' . $this->container->get('router')->getContext()->getHost();
- return ["User-agent: *\n", "Disallow: $baseUrl/$url\n"];
- }
- }