source: trunk/bookmarks/ajaxGetTitle.php @ 103

Revision 103, 2.0 KB checked in by admin, 4 years ago (diff)

Merged 1.003 into Trunk

Line 
1<?php
2/*
3       
4        This file is part of omCollab.
5       
6        Copyright (c) 2008 omCollab (Openmethodology Project)
7        http://www.openmethodology.org
8        http://mike2.openmethodology.org/wiki/OmCollab
9       
10        And parts copyright of Scuttle project
11        (http://sourceforge.net/projects/scuttle/, http://scuttle.org/)
12       
13        omCollab is free software: you can redistribute it and/or modify
14        it under the terms of the GNU General Public License as published by
15        the Free Software Foundation, either version 3 of the License, or
16        (at your option) any later version.
17       
18        omCollab is distributed in the hope that it will be useful,
19        but WITHOUT ANY WARRANTY; without even the implied warranty of
20        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21        GNU General Public License for more details.
22
23*/
24
25header('Content-Type: text/xml; charset=UTF-8');
26header("Last-Modified: ". gmdate("D, d M Y H:i:s") ." GMT");
27header("Cache-Control: no-cache, must-revalidate");
28
29require_once('header.inc.php');
30
31function getTitle($url) {
32    $fd = @fopen($url, 'r');
33    if ($fd) {
34        $html = fread($fd, 1750);
35        fclose($fd);
36
37        // Get title from title tag
38        preg_match_all('/<title>(.*)<\/title>/si', $html, $matches);
39        $title = $matches[1][0];
40
41        // Get encoding from charset attribute
42        preg_match_all('/<meta.*charset=([^;"]*)">/i', $html, $matches);
43        $encoding = strtoupper($matches[1][0]);
44
45        // Convert to UTF-8 from the original encoding
46        if (function_exists('mb_convert_encoding') ) {
47            $title = @mb_convert_encoding($title, 'UTF-8', $encoding);
48        }
49
50        if (utf8_strlen($title) > 0) {
51            return $title;
52        } else {
53            // No title, so return filename
54            $uriparts = explode('/', $url);
55            $filename = end($uriparts);
56            unset($uriparts);
57
58            return $filename;
59        }
60    } else {
61        return false;
62    }
63}
64echo '<?xml version="1.0" encoding="utf-8"?>';
65?>
66<response>
67  <method>getTitle</method>
68  <result><?php echo getTitle($_GET['url']); ?></result>
69</response>
Note: See TracBrowser for help on using the repository browser.