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

Benjamin Mako Hill || Want to submit a patch?