/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

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: yks2
  5. * Date: 24.11.16
  6. * Time: 11:20
  7. */
  8. namespace AppBundle\Controller;
  9. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use AppBundle\Entity\Pages;
  12. class PageController extends Controller
  13. {
  14. public function viewPageAction(Request $request)
  15. {
  16. if ($this->findInDb("page/1", 1, $request->attributes->get('pageId'))['disallow'] == 1) {
  17. $current = 1;
  18. } else {
  19. $current = 0;
  20. }
  21. if ($this->findInDb("page/1", 2, $request->attributes->get('pageId'))['disallow'] == 1) {
  22. $all = 1;
  23. } else {
  24. $all = 0;
  25. }
  26. return $this->render('pages/1.html.twig', [
  27. 'url' => "page/1",
  28. 'current' => $current,
  29. 'all' => $all,
  30. ]);
  31. }
  32. public function handlePageAction(Request $request)
  33. {
  34. if ($request->get('disallowcurrent') == 1) {
  35. $this->insertInDb($request->get('urlpage'), 1, 1, $request->attributes->get('pageId'));
  36. $this->writeDataCurrentRobot($request, "page/1", $request->attributes->get('pageId'));
  37. } else {
  38. $this->deleteFromDb($request->get('urlpage'), 1, $request->attributes->get('pageId'));
  39. $this->deleteDataCurrentRobot($request, "page/1", $request->attributes->get('pageId'));
  40. }
  41. if ($request->get('disallowall') == 1) {
  42. $this->insertInDb($request->get('urlpage'), 1, 2, $request->attributes->get('pageId'));
  43. $this->writeDataAllRobot($request, "page/1", $request->attributes->get('pageId'));
  44. } else {
  45. $this->deleteFromDb($request->get('urlpage'), 2, $request->attributes->get('pageId'));
  46. $this->deleteDataAllRobot($request, "page/1", $request->attributes->get('pageId'));
  47. }
  48. return $this->redirect($request->headers->get('referer'));
  49. }
  50. public function getDomainName($id)
  51. {
  52. $repository = $this->getDoctrine()
  53. ->getRepository('AppBundle:Domains');
  54. $query = $repository->createQueryBuilder('d')
  55. ->select('d.domain')
  56. ->where("d.id = $id")
  57. ->getQuery();
  58. return $query->setMaxResults(1)->getOneOrNullResult()['domain'];
  59. }
  60. public function getDomainNames()
  61. {
  62. $repository = $this->getDoctrine()
  63. ->getRepository('AppBundle:Domains');
  64. $query = $repository->createQueryBuilder('d')
  65. ->select('d.domain')
  66. ->getQuery();
  67. return $query->getResult();
  68. }
  69. public function insertInDb($currentPage, $disallow, $type, $domainId)
  70. {
  71. if (!$this->withoutType($currentPage, $domainId)) {
  72. $page = new Pages();
  73. $page->setPage($currentPage);
  74. $page->setDisallow($disallow);
  75. $page->setType($type);
  76. $page->setDomainId(intval($domainId));
  77. $em = $this->getDoctrine()->getManager();
  78. $em->persist($page);
  79. $em->flush();
  80. } else {
  81. $em = $this->getDoctrine()->getEntityManager();
  82. $repository = $em->getRepository('AppBundle:Pages');
  83. $page = $repository->findOneBy(array('page' => $currentPage, 'domain_id' => $domainId));
  84. $page->setDisallow($disallow);
  85. $page->setType($type);
  86. $em->flush();
  87. }
  88. }
  89. public function deleteFromDb($currentPage, $type, $domainId)
  90. {
  91. $em = $this->getDoctrine()->getEntityManager();
  92. $repository = $em->getRepository('AppBundle:Pages');
  93. $product = $repository->findOneBy(array('page' => $currentPage, 'type' => $type, 'domain_id' => $domainId));
  94. if ($product != NULL) {
  95. $product->setType(0);
  96. $product->setDisallow(0);
  97. $em->flush();
  98. }
  99. }
  100. public function writeDataCurrentRobot($request, $url, $domain)
  101. {
  102. $file = fopen($request->server->get('DOCUMENT_ROOT') . '/robots.txt', 'r+');
  103. $robotData = $this->readDataCurrentRobot($request, $url);
  104. $currentPage = $this->currentPage($url, $this->getDomainName($domain));
  105. if (!in_array($currentPage[1], $robotData)) {
  106. foreach ($currentPage as $row) {
  107. array_push($robotData, $row);
  108. }
  109. }
  110. foreach ($robotData as $row) {
  111. fwrite($file, $row);
  112. }
  113. fclose($file);
  114. }
  115. public function deleteDataCurrentRobot($request, $url, $domain)
  116. {
  117. $robotData = $this->readDataCurrentRobot($request, $url);
  118. $currentPage = $this->currentPage($url, $this->getDomainName($domain));
  119. if (in_array($currentPage[1], $robotData)) {
  120. $key = array_search($currentPage[1], $robotData);
  121. unset($robotData[$key]);
  122. unset($robotData[$key - 1]);
  123. }
  124. $file = fopen($request->server->get('DOCUMENT_ROOT') . '/robots.txt', 'w+');
  125. foreach ($robotData as $row) {
  126. fwrite($file, $row);
  127. }
  128. fclose($file);
  129. return $robotData;
  130. }
  131. public function deleteDataAllRobot($request, $url, $domainId)
  132. {
  133. $domains = $this->getDomainNames();
  134. foreach ($domains as $domain) {
  135. $robotData = $this->readDataRobot($domain, $request);
  136. $currentPage = $this->currentPage($url, $this->getDomainName($domainId));
  137. // var_dump($currentPage[1]);
  138. // var_dump($robotData);
  139. // die();
  140. if (in_array($currentPage[1], $robotData)) {
  141. $key = array_search($currentPage[1], $robotData);
  142. unset($robotData[$key]);
  143. unset($robotData[$key - 1]);
  144. }
  145. $file = fopen($request->server->get('DOCUMENT_ROOT') . '/' . $domain["domain"] . '/' . 'robots.txt', 'w+');
  146. foreach ($robotData as $row) {
  147. fwrite($file, $row);
  148. }
  149. fclose($file);
  150. }
  151. return $robotData;
  152. }
  153. public function writeDataAllRobot($request, $url, $domainId)
  154. {
  155. $this->writeDataCurrentRobot($request, $url, $domainId);
  156. $domains = $this->getDomainNames();
  157. foreach ($domains as $domain) {
  158. $file = fopen($request->server->get('DOCUMENT_ROOT') . '/' . $domain["domain"] . '/' . 'robots.txt', 'r+');
  159. $robotData = $this->readDataRobot($domain, $request);
  160. $currentPage = $this->currentPage($url, $this->getDomainName($domainId));
  161. if (!in_array($currentPage[1], $robotData)) {
  162. foreach ($currentPage as $row) {
  163. array_push($robotData, $row);
  164. }
  165. }
  166. foreach ($robotData as $row) {
  167. fwrite($file, $row);
  168. }
  169. fclose($file);
  170. }
  171. }
  172. public function readDataCurrentRobot($request, $url)
  173. {
  174. $robotPath = $request->server->get('DOCUMENT_ROOT') . '/robots.txt';
  175. $robotData = file($robotPath);
  176. return $robotData;
  177. }
  178. public function readDataRobot($domain, $request)
  179. {
  180. $robotPath = $request->server->get('DOCUMENT_ROOT') . '/' . $domain['domain'] . '/' . 'robots.txt';
  181. $robotData = file($robotPath);
  182. return $robotData;
  183. }
  184. public function withoutType($page, $domainId)
  185. {
  186. $repository = $this->getDoctrine()
  187. ->getRepository('AppBundle:Pages');
  188. $query = $repository->createQueryBuilder('p')
  189. ->select('p.page, p.disallow')
  190. ->where("p.page = '$page' AND p.domain_id = $domainId")
  191. ->getQuery();
  192. return $query->setMaxResults(1)->getOneOrNullResult();
  193. }
  194. public function findInDb($page, $type, $domainId)
  195. {
  196. $repository = $this->getDoctrine()
  197. ->getRepository('AppBundle:Pages');
  198. $query = $repository->createQueryBuilder('p')
  199. ->select('p.page, p.disallow')
  200. ->where("p.page = '$page' AND p.type = $type AND p.domain_id = $domainId")
  201. ->getQuery();
  202. return $query->setMaxResults(1)->getOneOrNullResult();
  203. }
  204. private function currentPage($url, $domain)
  205. {
  206. $baseUrl = $this->container->get('router')->getContext()->getScheme() . '://' . $domain . '.' . $this->container->get('router')->getContext()->getHost();
  207. return ["User-agent: *\n", "Disallow: $baseUrl/$url\n"];
  208. }
  209. private function allPages($url, $domain)
  210. {
  211. $baseUrl = $this->container->get('router')->getContext()->getScheme() . '://' . $domain . '.' . $this->container->get('router')->getContext()->getHost();
  212. return ["User-agent: *\n", "Disallow: $baseUrl/$url\n"];
  213. }
  214. }