/PHP/mobile responsiveness detection web application/siteMeta.class.php

https://gitlab.com/vince.omega/General-Code-Dump-From-Desktop · PHP · 184 lines · 130 code · 33 blank · 21 comment · 17 complexity · e95fe9030e048e09553f5399c63772f8 MD5 · raw file

  1. <?php
  2. ini_set('display_errors',1); error_reporting(E_ALL);
  3. /* CLASS START */
  4. class siteMeta{
  5. public /*.string.*/ $uri;
  6. public /*.string.*/ $status;
  7. public /*.string.*/ $results;
  8. public /*.object.*/ $dom;
  9. public /*.object.*/ $xp;
  10. public /*.class.*/ $janitor;
  11. /**
  12. * consutructor for class, defines global properties
  13. * @name __construct()
  14. */
  15. public function __construct(){
  16. $this->uri = "";
  17. $this->status = "";
  18. $this->results = "";
  19. $this->dom = "";
  20. $this->xp = "";
  21. $this->janitor = new maintence();
  22. }
  23. /**
  24. * pingMe
  25. * @name pingMe()
  26. * @param (url)[string] url of the site the info is going to be sent to
  27. * @return [html]
  28. */
  29. public function pingMe($url){
  30. $this->uri = $url;
  31. $curl = curl_init($this->uri);
  32. if(curl_errno($curl)){
  33. echo 'Curl error: '.curr_error($curl);
  34. }
  35. curl_setopt($curl, CURLOPT_HEADER, true);
  36. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  37. if(!$data = curl_exec($curl)){
  38. $this->status = curl_getinfo($curl);
  39. return $this->status;
  40. }
  41. curl_close($curl);
  42. $this->results = $this->scrapeMe($data);
  43. return $this->results;
  44. }
  45. /**
  46. * scrapeMe
  47. * @name scrapeMe()
  48. * @param (site)[string] HTML mark up for the site retreived by cURL
  49. * @return [html]
  50. */
  51. public function scrapeMe($site){
  52. libxml_use_internal_errors(true);
  53. $this->dom = new DOMDocument();
  54. $this->dom->loadHTML($site);
  55. libxml_clear_errors();
  56. $this->xp = new DOMXPath($this->dom);
  57. $import = array(
  58. 'meta' => ''
  59. );
  60. $metaArray = $this->dom->getElementsByTagName('meta');
  61. $viewportArrayData = array();
  62. foreach($metaArray as $key => $value){
  63. $attr = strtolower($value->attributes->item(0)->value);
  64. if($attr === 'viewport'){
  65. $viewport_text = 'Viewport has been found. <br/>';
  66. $viewport_text .= 'Here are the settings for the meta tag, Viewport <br/>';
  67. $viewportArrayData = $this->parseViewport($value);
  68. }
  69. if($attr === 'mobileoptimized'){
  70. $mobileoptimized_text = 'mobile_optimized has been found. <br/>';
  71. $mobileoptimized_text .= 'Here are the settings for meta tag, mobileOptimized <br/>';
  72. $contentMO = $value->getAttribute('content');
  73. }
  74. if($attr === 'handheldfriendly'){
  75. $handheldfriendly_text = 'handheldFriendly has been found. <br/>';
  76. $handheldfriendly_text .= 'Here are the settings for the meta tag handheldFriendly <br/>';
  77. $contentHF = $value->getAttribute('content');
  78. }
  79. if($attr === 'apple-mobile-web-app-capable'){
  80. $apple_capable_text = 'Apple Meta tag: Web App Capable, has been found. <br/>';
  81. $apple_capable_text .= 'Here are the settings for the meta tag, Apple Meta Web App Capable. <br/>';
  82. $contentAMWAC = $value->getAttribute('content');
  83. }
  84. if($attr === 'apple-mobile-web-app-status'){
  85. $apple_status_text = 'Apple Meta tag: Web App Status Box Style, has been found. <br/>';
  86. $apple_status_text .= 'Here are the settings for the meta tag, Apple Meta tag: Web App Statu Box Style <br/>';
  87. $contentAMWSBS = $value->getAttribute('content');
  88. }
  89. }
  90. $html = "<div id='meta-data-container'>";
  91. $viewportArrayData = array_filter($viewportArrayData);
  92. if(!empty($viewportArrayData)){
  93. $html .= "<div class='viewport-data-text'>$viewport_text</div>";
  94. $html .= "<ul class='viewport-data-list'>";
  95. foreach($viewportArrayData as $arraykey => $subarray){
  96. $html .= "<li class='viewport-data-attribute-value'>$subarray[attribute] = $subarray[value]</li>";
  97. // $html .= "<li class='viewport-data-value'>$</li>";
  98. }
  99. $html .= "</ul></div>";
  100. }
  101. if(isset($contentMO) != null){
  102. $html .= "<div class='mobileopt-data-text'>$mobileoptimized_text</div>";
  103. $html .= "<div class='mobileopt-data-content'>$contentMO</div>";
  104. }
  105. if(isset($contentHF) != null){
  106. $html .= "<div class='handheld-data-text'>$handheldfriendly_text</div>";
  107. $html .= "<div class='handheld-data-content'>$contentHF</div>";
  108. }
  109. if(isset($contentAMWAC) != null){
  110. $html .= "<div class='apple-capable-data-text'>$apple_capable_text</div>";
  111. $html .= "<div class='apple-capable-data-content'>$contentAMWAC</div>";
  112. }
  113. if(isset($contentAMWSBS) != null){
  114. $html .= "<div class='apple-status-data-text'>$apple_status_text</div>";
  115. $html .= "<div class='apple-status-data-content'>$contentAMWSBS</div>";
  116. }
  117. $html .="</div>";
  118. $date = str_replace("/", ".", $this->janitor->date);
  119. // $time = str_replace(":", ".", $this->janitor->time);
  120. $name = $date."_".$this->janitor->divviString($this->uri);
  121. $path = $this->janitor->profilepath."/".$name.".xml";
  122. $import['meta'] = $html;
  123. // $this->janitor->appendsProfile($this->uri, $import, $path);
  124. return $html;
  125. }
  126. public function parseViewport($data){
  127. $content = $data->getAttribute('content');
  128. $temp = explode(",", $content);
  129. $tp = "";
  130. $optionsArray = array(
  131. 0 => array(
  132. "attribute" => "",
  133. "value" => ""
  134. ));
  135. foreach($temp as $key => $value){
  136. $tp .= $value;
  137. $tmp = explode(" ", $tp);
  138. $i = 0;
  139. unset($temp);
  140. $k = sizeof($tmp);
  141. for($j = 0; $j < $k; $j++){
  142. $sml = explode("=", $tmp[$j]);
  143. $options[$j]['attribute'] = $sml[0];
  144. $options[$j]['value'] = $sml[1];
  145. }
  146. }
  147. return $options;
  148. }
  149. }
  150. /* CLASS END */
  151. ?>