Advertisement
Guest User

getmata_fopen.php

a guest
Mar 7th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.18 KB | None | 0 0
  1. <?php
  2. // ---------------------------------------------------------------
  3. // ---------------- file: getmata_fopen.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_fopen.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.  
  36. $formular1 = '
  37. <form name="form1" method="post" action="">
  38.  <br /> URL: <input type="text" name="url" size="48" maxlength="180" value="'.$go_url.'" />
  39.  <br />
  40.  <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>
  41.  <br />
  42.  <br /> Titel: <input type="text" name="title" size="48" maxlength="90" />
  43.  <br />
  44.  <br /> Beschreibung:
  45.  <br /> <textarea name="description" cols="48" rows="4" wrap="VIRTUAL"></textarea>
  46.  <br />
  47.  <br /> Keywords:
  48.  <br /> <textarea name="keywords" cols="48" rows="4" wrap="VIRTUAL"></textarea>
  49.  <br />
  50.  <br /> OK:  <input type="submit" name="save" value="Speichern" />
  51.  <br />
  52. </form>
  53. ';
  54.  
  55.  
  56. // ----------------------------------------------------------------
  57.  
  58. function str_utf8_encode($str) {
  59.     if (mb_detect_encoding($str, 'UTF-8', true) === FALSE) {
  60.     $str = utf8_encode($str);
  61.     }
  62.     return $str;
  63. }
  64.  
  65. // ----------------------------------------------------------------
  66. // ----------------------------------------------------------------
  67.  
  68. function get_meta_from_url($url) {
  69.  
  70. $html = '';
  71.  
  72. if($fh = @fopen($url,"r")){
  73.     while (!feof($fh)){
  74.         $buffer = fgets($fh, 4096);
  75.         if (strpos($buffer,'/head') !== FALSE) { break; }
  76.         $html .= $buffer;
  77.     }
  78.     fclose($fh);
  79. }
  80. else {
  81.  
  82. print "<br />ERROR ... fopen\n";
  83.  
  84. $online = false;
  85. return $online;
  86.  
  87. ## exit;
  88. }
  89.  
  90.  
  91.  
  92. $hsize = strlen($html);
  93.  
  94. print "<br />\n";
  95. print "<br />Size: ".$hsize."\n";
  96. print "<br />\n";
  97.  
  98.  
  99. // --------------------------------------------
  100.  
  101. $pattern1 = '/<title>(.*)<\/title>/iU';
  102. preg_match_all ($pattern1, $html, $matches_title);
  103.  
  104. ## print "<pre>\n";
  105. ## print_r($matches_title);
  106. ## print "</pre>\n";
  107.  
  108. // --------------------------------------------
  109.  
  110. $pattern2 = '/<meta name=(["\']?)(description|keywords)\\1\\s+content=(["\']?)(.+?)\\3[ \/>]/i';
  111. preg_match_all ($pattern2, $html, $matches_meta, PREG_SET_ORDER);
  112.  
  113. ## print "<pre>\n";
  114. ## print_r($matches_meta);
  115. ## print "</pre>\n";
  116.  
  117. // --------------------------------------------
  118.  
  119. $matchdata = array();
  120.  
  121. $matchdata['title'] = (isset($matches_title[1][0])) ? str_utf8_encode($matches_title[1][0]) : '';
  122.  
  123. foreach($matches_meta as $matchitem) {  
  124.     $mkey = strtolower($matchitem[2]);
  125.     $matchdata[$mkey] = (isset($matchitem[4])) ? str_utf8_encode($matchitem[4]) : '';
  126. }
  127.  
  128. // --------------------------------------------
  129.  
  130. return $matchdata;
  131.  
  132.  
  133. }
  134.  
  135. // ----------------------------------------------------------------
  136. // ----------------------------------------------------------------
  137.  
  138.  
  139. // ------------------------------------------------------------
  140.  
  141. if ( !empty($_REQUEST['save']) && !empty($go_url) && $go_url != 'http://' ) {
  142.  
  143. print "<br />\n";
  144. print "<br /> To-Do ........ Speichern \n";
  145. print "<br />\n";
  146. print "<br /> ###### DEBUG ###### _POST ###### \n";
  147. print "<br />\n";
  148. print "<br />\n";
  149.  
  150. print "<pre style=\"margin:4px; padding:4px; background:#DEDEDE; text-align:left;\">\n";
  151. print_r($_POST);
  152. print "</pre>\n";
  153.  
  154. print "<br />\n";
  155. print "<br />\n";
  156. print "<br />\n";
  157.  
  158. }
  159. else {
  160. // ------------------------------------------------------------
  161.  
  162.  
  163. print "<br />\n";
  164. print "<br />\n";
  165.  
  166. print $formular1;
  167.  
  168. print "<br />\n";
  169. print "<br />\n";
  170.  
  171. }
  172. // ------------------------------------------------------------
  173.  
  174.  
  175. $jscode = '';
  176.  
  177.  
  178. // ----------------------------------------------------
  179. if ( !empty($_REQUEST['meta']) ) {
  180.  
  181.  
  182. // ---------------------------------
  183. if ( !empty($go_url) && $go_url != 'http://' ) {
  184.  
  185. $data = get_meta_from_url($go_url);
  186.  
  187. // -----------------------------
  188.  
  189. if ($data === false) {
  190.  
  191. print "<br />\n";
  192. 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";
  193. print "<br />\n";
  194.  
  195. }
  196. else {
  197.  
  198. // -----------------------------
  199.  
  200. print "<p>###### DEBUG ######</p>\n";
  201. print "<br />URL: ".$go_url."\n";
  202. print "<br />\n";
  203.  
  204. print "<pre style=\"margin:4px; padding:4px; background:#DEDEDE; text-align:left;\">\n";
  205. print_r($data);
  206. print "</pre>\n";
  207.  
  208. print "<br />\n";
  209. print "<br />\n";
  210.  
  211.  
  212. $jscode .= '
  213. <script type="text/javascript">
  214. ';
  215.  
  216. $jscode .= '
  217. document.form1.title.value = "'.$data['title'].'";
  218. document.form1.description.value = "'.$data['description'].'";
  219. document.form1.keywords.value = "'.$data['keywords'].'";
  220. ';
  221.  
  222. $jscode .= '
  223. document.getElementById("minfo").innerHTML = "OK ... MetaTags wurden gelesen.";
  224. ';
  225.  
  226. $jscode .= '
  227. </script>
  228. ';
  229.  
  230.  
  231. }
  232. // -----------------------------
  233.  
  234. }
  235. else {
  236. // ---------------------------------
  237.  
  238. $jscode .= '
  239. <script type="text/javascript">
  240. document.getElementById("minfo").innerHTML = "Kannst Du nicht Lesen? ... zuerst eine URL eintragen !!!";
  241. </script>
  242. ';
  243.  
  244. }
  245. // ---------------------------------
  246.  
  247. }
  248. // ----------------------------------------------------
  249.  
  250.  
  251.  
  252. print $jscode;
  253.  
  254. print '<p>&nbsp;</p>'."\n";
  255.  
  256. // ------------------------------------------------------------
  257.  
  258. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement