|
@@ -4,9 +4,11 @@ namespace Drupal\statusmessage\Controller;
|
|
|
require_once(DRUPAL_ROOT .'/vendor/autoload.php');
|
|
|
|
|
|
use Drupal\Core\Controller\ControllerBase;
|
|
|
+use Drupal\Core\Render\Markup;
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
|
use GuzzleHttp\Client;
|
|
|
+use Drupal\statusmessage\MarkupGenerator;
|
|
|
|
|
|
|
|
|
|
|
@@ -42,95 +44,102 @@ class StatusPreviewController extends ControllerBase {
|
|
|
public function generate($url) {
|
|
|
|
|
|
if ($url == 'build') {
|
|
|
- $url = \Drupal::request()->get('data');
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- $this->dom = new \DOMDocument;
|
|
|
- $contents = file_get_contents('http://' . $url);
|
|
|
- $this->dom->loadHTML($contents);
|
|
|
-
|
|
|
- $xpath = new \DomXpath($this->dom);
|
|
|
-
|
|
|
- $anchorAttributes = $this->getAnchorNodeNames();
|
|
|
- $imgAttributes = $this->getImgNodeNames();
|
|
|
- $imgLogos = $this->searchDom('img', 'logo');
|
|
|
- $anchorLogos = $this->searchDom('a', 'logo');
|
|
|
|
|
|
+ $url = \Drupal::request()->get('data');
|
|
|
|
|
|
+ $generator = new MarkupGenerator();
|
|
|
|
|
|
- $contents = file_get_contents('http://' . $url);
|
|
|
- $response = new Response();
|
|
|
- $response->setContent(\GuzzleHttp\json_encode(array('data' => $contents)));
|
|
|
- $response->headers->set('Content-Type', 'application/json');
|
|
|
-
|
|
|
- return $response;
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
+ if ($generator->parseMarkup($url)) {
|
|
|
|
|
|
+ $preview = $generator->generatePreview();
|
|
|
|
|
|
- private function getAnchorNodeNames() {
|
|
|
- if ($this->dom) {
|
|
|
- $names = array();
|
|
|
- $attrXpath = new \DomXpath($this->dom);
|
|
|
+ $response = new Response();
|
|
|
+ $response->setContent(\GuzzleHttp\json_encode(array('data' => $preview)));
|
|
|
+ $response->headers->set('Content-Type', 'application/json');
|
|
|
|
|
|
- $nodes = $attrXpath->query('//a/@*');
|
|
|
- $i = 0;
|
|
|
- foreach ($nodes as $node) {
|
|
|
- $names[$i] = new \stdClass();
|
|
|
- $names[$i]->name = $node->nodeName;
|
|
|
- $names[$i]->value = $node->nodeValue;
|
|
|
- $i++;
|
|
|
+ return $response;
|
|
|
}
|
|
|
|
|
|
- return $names;
|
|
|
- }
|
|
|
- }
|
|
|
+// $contents = file_get_contents('http://' . $url);
|
|
|
+// $response = new Response();
|
|
|
|
|
|
- private function getImgNodeNames() {
|
|
|
- if ($this->dom) {
|
|
|
- $names = array();
|
|
|
- $attrXpath = new \DomXpath($this->dom);
|
|
|
-
|
|
|
- $nodes = $attrXpath->query('//img/@*');
|
|
|
- $i = 0;
|
|
|
- foreach ($nodes as $node) {
|
|
|
- $names[$i] = new \stdClass();
|
|
|
- $names[$i]->name = $node->nodeName;
|
|
|
- $names[$i]->value = $node->nodeValue;
|
|
|
- $i++;
|
|
|
- }
|
|
|
+// $this->dom = new \DOMDocument;
|
|
|
+// $this->dom->loadHTML($contents);
|
|
|
+//
|
|
|
+// $xpath = new \DomXpath($this->dom);
|
|
|
+//
|
|
|
+// $anchorAttributes = $this->getAnchorNodeNames();
|
|
|
+// $imgAttributes = $this->getImgNodeNames();
|
|
|
+// $imgLogos = $this->searchDom('img', 'logo');
|
|
|
+// $anchorLogos = $this->searchDom('a', 'logo');
|
|
|
+//
|
|
|
|
|
|
- return $names;
|
|
|
}
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
- private function searchDom($tag, $string) {
|
|
|
-
|
|
|
- if ($this->dom) {
|
|
|
-
|
|
|
- $results = array();
|
|
|
- $tags = $this->dom->getElementsByTagName($tag);
|
|
|
-
|
|
|
|
|
|
- for ($i = 0; $i < $tags->length; $i++) {
|
|
|
- $results[$i] = new \stdClass();
|
|
|
|
|
|
- $src = $tags->item($i)->getAttribute('src');
|
|
|
- if (strpos($src, 'logo')) {
|
|
|
- $results[$i]->src = $src;
|
|
|
- }
|
|
|
-
|
|
|
- $href = $tags->item($i)->getAttribute('href');
|
|
|
- if (strpos($href, 'logo')) {
|
|
|
- $results[$i]->href = $href;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return $results;
|
|
|
- }
|
|
|
- }
|
|
|
+// private function getAnchorNodeNames() {
|
|
|
+// if ($this->dom) {
|
|
|
+// $names = array();
|
|
|
+// $attrXpath = new \DomXpath($this->dom);
|
|
|
+//
|
|
|
+// $nodes = $attrXpath->query('//a/@*');
|
|
|
+// $i = 0;
|
|
|
+// foreach ($nodes as $node) {
|
|
|
+// $names[$i] = new \stdClass();
|
|
|
+// $names[$i]->name = $node->nodeName;
|
|
|
+// $names[$i]->value = $node->nodeValue;
|
|
|
+// $i++;
|
|
|
+// }
|
|
|
+//
|
|
|
+// return $names;
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// private function getImgNodeNames() {
|
|
|
+// if ($this->dom) {
|
|
|
+// $names = array();
|
|
|
+// $attrXpath = new \DomXpath($this->dom);
|
|
|
+//
|
|
|
+// $nodes = $attrXpath->query('//img/@*');
|
|
|
+// $i = 0;
|
|
|
+// foreach ($nodes as $node) {
|
|
|
+// $names[$i] = new \stdClass();
|
|
|
+// $names[$i]->name = $node->nodeName;
|
|
|
+// $names[$i]->value = $node->nodeValue;
|
|
|
+// $i++;
|
|
|
+// }
|
|
|
+//
|
|
|
+// return $names;
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// private function searchDom($tag, $string) {
|
|
|
+//
|
|
|
+// if ($this->dom) {
|
|
|
+//
|
|
|
+// $results = array();
|
|
|
+// $tags = $this->dom->getElementsByTagName($tag);
|
|
|
+//
|
|
|
+//
|
|
|
+// for ($i = 0; $i < $tags->length; $i++) {
|
|
|
+// $results[$i] = new \stdClass();
|
|
|
+//
|
|
|
+// $src = $tags->item($i)->getAttribute('src');
|
|
|
+// if (strpos($src, 'logo')) {
|
|
|
+// $results[$i]->src = $src;
|
|
|
+// }
|
|
|
+//
|
|
|
+// $href = $tags->item($i)->getAttribute('href');
|
|
|
+// if (strpos($href, 'logo')) {
|
|
|
+// $results[$i]->href = $href;
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// return $results;
|
|
|
+// }
|
|
|
+// }
|
|
|
|
|
|
}
|