Advertisement
Guest User

getmata_curl.php

a guest
Mar 7th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.21 KB | None | 0 0
  1. <?php
  2. // ---------------------------------------------------------------
  3. // ----------------- file: getmata_curl.php ----------------------
  4. // ---------------------------------------------------------------
  5.  
  6. header("Content-Type: text/html; charset=utf-8");
  7.  
  8. error_reporting(E_ALL);
  9. ini_set("display_errors", 1);
  10.  
  11. ## $myscript = 'getmata_curl.php';
  12. $myscript = $_SERVER['PHP_SELF'];
  13.  
  14.  
  15. // ------------------------------------------------------------
  16.  
  17. print "<br />\n";
  18. print "<br /> --- <a href=\"".$myscript."\">HOME</a> (RESET) --- \n";
  19. print "<br />\n";
  20.  
  21. // ----------------------------------------------------
  22.  
  23. if (!empty($_REQUEST['url'])) {
  24.     $go_url = trim(chop($_REQUEST['url']));
  25.     $go_url = str_replace('http://http://','http://',$go_url);
  26.     if (strpos($go_url,'http://') === FALSE && strpos($go_url,'https://') === FALSE) { $go_url = 'http://'.$go_url; }
  27.     // ----- to-do: filters and/or escape url string -----
  28. }
  29. else {
  30.     $go_url = 'http://';
  31. }
  32.  
  33. // ----------------------------------------------------
  34.  
  35. function pingSite($url, $length=4096) {  
  36.    
  37.     $fp = curl_init($url);
  38.    
  39.     curl_setopt($fp, CURLOPT_TIMEOUT, 10);
  40.     curl_setopt($fp, CURLOPT_FAILONERROR, 1);
  41.     curl_setopt($fp, CURLOPT_RANGE, '0-4096');
  42.     curl_setopt($fp, CURLOPT_RETURNTRANSFER, 1);
  43.     curl_setopt($fp, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  44.  
  45.     $result = curl_exec($fp);
  46.    
  47. ##  if (curl_errno($fp) != 0) { $status = false; }
  48.     if (curl_errno($fp) != 0) { $status = curl_error($fp); }
  49.     else { $status = true; }
  50.    
  51.     curl_close($fp);
  52.    
  53.     return array('status' => $status, 'html' => $result);
  54.  
  55. }  
  56.  
  57. // ----------------------------------------------------
  58.  
  59.  
  60. $online = pingSite($go_url);
  61.  
  62. if ( $online['status'] !== true && !empty($go_url) && $go_url != 'http://' ) {
  63.  
  64. print "<br />\n";
  65. print "<br /> ........ Die URL <span style=\"margin:8px; padding:4px; background:#DDEEDD;\">".$go_url."</span> ist <strong style=\"color:red;\">nicht</strong> online !!! ... <a href=\"".$myscript."\">zur&uuml;ck</a>\n";
  66. print "<br />\n";
  67. print "<br />ERROR: <span style=\"margin:8px; padding:4px; background:#DEDEDE\">".$online['status']."</span>\n";
  68. print "<br />\n";
  69. exit;
  70. }
  71. else {
  72.  
  73.  
  74. $hsize = strlen($online['html']);
  75.  
  76. print "<br />\n";
  77. print "<br />Size: ".$hsize."\n";
  78. print "<br />\n";
  79.  
  80.  
  81. // ------------------------------------------------------------
  82.  
  83. if ( !empty($_REQUEST['save']) && !empty($go_url) && $go_url != 'http://' ) {
  84.  
  85. print "<br />\n";
  86. print "<br /> To-Do ........ Speichern \n";
  87. print "<br />\n";
  88. print "<br /> ###### DEBUG ###### _POST ###### \n";
  89. print "<br />\n";
  90. print "<br />\n";
  91.  
  92. print "<pre style=\"margin:4px; padding:4px; background:#DEDEDE; text-align:left;\">\n";
  93. print_r($_POST);
  94. print "</pre>\n";
  95.  
  96. print "<br />\n";
  97. print "<br />\n";
  98. print "<br />\n";
  99.  
  100. }
  101. else {
  102. // ------------------------------------------------------------
  103.  
  104. $formular1 = '
  105. <form name="form1" method="post" action="">
  106.  <br /> URL: <input type="text" name="url" size="48" maxlength="180" value="'.$go_url.'" />
  107.  <br />
  108.  <br /> RUN: <input type="submit" name="meta" value="Get MetaTags" /> &nbsp;&nbsp; INFO: <span id="minfo" style="margin:12px; padding:4px; background:#DDEEDD;">Bitte eine URL eingeben.</span>
  109.  <br />
  110.  <br /> Titel: <input type="text" name="title" size="48" maxlength="90" />
  111.  <br />
  112.  <br /> Beschreibung:
  113.  <br /> <textarea name="description" cols="48" rows="4" wrap="VIRTUAL"></textarea>
  114.  <br />
  115.  <br /> Keywords:
  116.  <br /> <textarea name="keywords" cols="48" rows="4" wrap="VIRTUAL"></textarea>
  117.  <br />
  118.  <br /> OK:  <input type="submit" name="save" value="Speichern" />
  119.  <br />
  120. </form>
  121. ';
  122.  
  123.  
  124. // ----------------------------------------------------
  125.  
  126. print "<br />\n";
  127. print "<br />\n";
  128.  
  129. print $formular1;
  130.  
  131. print "<br />\n";
  132. print "<br />\n";
  133.  
  134. }
  135.  
  136.  
  137. // ----------------------------------------------------
  138.  
  139.  
  140. function str_utf8_encode($str) {
  141.     if (mb_detect_encoding($str, 'UTF-8', true) === FALSE) {
  142.     $str = utf8_encode($str);
  143.     }
  144.     return $str;
  145. }
  146.  
  147. // ----------------------------------------------------------------
  148. // ----------------------------------------------------------------
  149.  
  150. ## function get_meta_from_url($url) {
  151.  
  152. function get_meta_from_url($html) {
  153.  
  154. /* ---------------
  155. $html = '';
  156.  
  157. if($fh = @fopen($url,"r")){
  158.     while (!feof($fh)){
  159.         $buffer = fgets($fh, 4096);
  160.         if (strpos($buffer,'/head') !== FALSE) { break; }
  161.         $html .= $buffer;
  162.     }
  163.     fclose($fh);
  164. }
  165. else {
  166. print "<br />ERROR ... fopen\n";
  167. print "<br /> ........ Die URL <span style=\"margin:8px; padding:4px; background:#DDEEDD;\">".$go_url."</span> ist <strong style=\"color:red;\">nicht</strong> online !!! ... <a href=\"".$myscript."\">zur&uuml;ck</a>\n";
  168. exit;
  169. }
  170. --------------- */
  171.  
  172.  
  173. // --------------------------------------------
  174.  
  175. $pattern1 = '/<title>(.*)<\/title>/iU';
  176. preg_match_all ($pattern1, $html, $matches_title);
  177.  
  178. ## print "<pre>\n";
  179. ## print_r($matches_title);
  180. ## print "</pre>\n";
  181.  
  182. // --------------------------------------------
  183.  
  184. $pattern2 = '/<meta name=(["\']?)(description|keywords)\\1\\s+content=(["\']?)(.+?)\\3[ \/>]/i';
  185. preg_match_all ($pattern2, $html, $matches_meta, PREG_SET_ORDER);
  186.  
  187. ## print "<pre>\n";
  188. ## print_r($matches_meta);
  189. ## print "</pre>\n";
  190.  
  191. // --------------------------------------------
  192.  
  193. $matchdata = array();
  194.  
  195. $matchdata['title'] = (isset($matches_title[1][0])) ? str_utf8_encode($matches_title[1][0]) : '';
  196.  
  197. foreach($matches_meta as $matchitem) {  
  198.     $mkey = strtolower($matchitem[2]);
  199.     $matchdata[$mkey] = (isset($matchitem[4])) ? str_utf8_encode($matchitem[4]) : '';
  200. }
  201.  
  202. // --------------------------------------------
  203.  
  204. return $matchdata;
  205.  
  206.  
  207. }
  208.  
  209. // ----------------------------------------------------------------
  210. // ----------------------------------------------------------------
  211.  
  212.  
  213. // ----------------------------------------------------
  214. if ( !empty($_REQUEST['meta']) ) {
  215.  
  216.  
  217. // ---------------------------------
  218. if ( !empty($go_url) && $go_url != 'http://' ) {
  219.  
  220. ## $data = get_meta_from_url($go_url);
  221.  
  222. $data = get_meta_from_url($online['html']);
  223.  
  224.  
  225.  
  226. print "<p>###### DEBUG ######</p>\n";
  227. print "<br />URL: ".$go_url."\n";
  228. print "<br />\n";
  229.  
  230. print "<pre style=\"margin:4px; padding:4px; background:#DEDEDE; text-align:left;\">\n";
  231. print_r($data);
  232. print "</pre>\n";
  233.  
  234. print "<br />\n";
  235. print "<br />\n";
  236.  
  237.  
  238. $jscode = '';
  239.  
  240. $jscode .= '
  241. <script type="text/javascript">
  242. ';
  243.  
  244. $jscode .= '
  245. document.form1.title.value = "'.$data['title'].'";
  246. document.form1.description.value = "'.$data['description'].'";
  247. document.form1.keywords.value = "'.$data['keywords'].'";
  248. ';
  249.  
  250. $jscode .= '
  251. document.getElementById("minfo").innerHTML = "OK ... MetaTags wurden gelesen.";
  252. ';
  253.  
  254. $jscode .= '
  255. </script>
  256. ';
  257.  
  258.  
  259. print $jscode;
  260.  
  261.  
  262. }
  263. else {
  264. // ---------------------------------
  265.  
  266. print '
  267. <script type="text/javascript">
  268. document.getElementById("minfo").innerHTML = "Kannst Du nicht Lesen? ... zuerst eine URL eintragen !!!";
  269. </script>
  270. ';
  271.  
  272. }
  273. // ---------------------------------
  274.  
  275. }
  276. // ----------------------------------------------------
  277.  
  278. }
  279. // ------------------------------------------------------------
  280.  
  281.  
  282. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement