/ajaxGetTitle.php

https://github.com/lucanos/scuttle · PHP · 59 lines · 30 code · 7 blank · 22 comment · 5 complexity · 4a80c55fc677c6212e42a6b8f8fdd413 MD5 · raw file

  1. <?php
  2. /***************************************************************************
  3. Copyright (c) 2005 - 2010 Marcus Campbell
  4. http://scuttle.org/
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. ***************************************************************************/
  17. header('Content-Type: text/plain; charset=UTF-8');
  18. header('Last-Modified: '. gmdate("D, d M Y H:i:s") .' GMT');
  19. header('Cache-Control: no-cache, must-revalidate');
  20. require_once 'header.inc.php';
  21. function getTitle($url) {
  22. $fd = @fopen($url, 'r');
  23. if ($fd) {
  24. $html = fread($fd, 1750);
  25. fclose($fd);
  26. // Get title from title tag
  27. preg_match_all('/<title>(.*)<\/title>/si', $html, $matches);
  28. $title = $matches[1][0];
  29. // Get encoding from charset attribute
  30. preg_match_all('/<meta.*charset=([^;"]*)">/i', $html, $matches);
  31. $encoding = strtoupper($matches[1][0]);
  32. // Convert to UTF-8 from the original encoding
  33. if (function_exists('mb_convert_encoding')) {
  34. $title = @mb_convert_encoding($title, 'UTF-8', $encoding);
  35. }
  36. if (utf8_strlen($title) > 0) {
  37. return $title;
  38. } else {
  39. // No title, so return filename
  40. $uriparts = explode('/', $url);
  41. $filename = end($uriparts);
  42. unset($uriparts);
  43. return $filename;
  44. }
  45. } else {
  46. return false;
  47. }
  48. }
  49. echo getTitle($_GET['url']);