]> gitweb.fluxo.info Git - semanticscuttle.git/commitdiff
Move URL title method to dedicated class
authorChristian Weiske <cweiske@cweiske.de>
Mon, 9 May 2016 19:39:18 +0000 (21:39 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Mon, 9 May 2016 19:39:18 +0000 (21:39 +0200)
src/SemanticScuttle/UrlHelper.php [new file with mode: 0644]
www/ajaxGetTitle.php

diff --git a/src/SemanticScuttle/UrlHelper.php b/src/SemanticScuttle/UrlHelper.php
new file mode 100644 (file)
index 0000000..5417b9b
--- /dev/null
@@ -0,0 +1,65 @@
+<?php
+/**
+ * SemanticScuttle - your social bookmark manager.
+ *
+ * PHP version 5.
+ *
+ * @category Bookmarking
+ * @package  SemanticScuttle
+ * @author   Christian Weiske <cweiske@cweiske.de>
+ * @license  GPL http://www.gnu.org/licenses/gpl.html
+ * @link     http://sourceforge.net/projects/semanticscuttle
+ */
+
+/**
+ * Work with URLs
+ *
+ * @category Bookmarking
+ * @package  SemanticScuttle
+ * @author   Christian Weiske <cweiske@cweiske.de>
+ * @license  GPL http://www.gnu.org/licenses/gpl.html
+ * @link     http://sourceforge.net/projects/semanticscuttle
+ */
+class SemanticScuttle_UrlHelper
+{
+    function getTitle($url)
+    {
+        $fd = @fopen($url, 'r');
+        $title = '';
+        if ($fd) {
+            $html = fread($fd, 1750);
+            fclose($fd);
+
+            // Get title from title tag
+            preg_match_all('/<title[^>]*>(.*)<\/title>/si', $html, $matches);
+            $title = $matches[1][0];
+
+            $encoding = 'utf-8';
+            // Get encoding from charset attribute
+            preg_match_all('/<meta.*charset=([^;"]*)">/i', $html, $matches);
+            if (isset($matches[1][0])) {
+                $encoding = strtoupper($matches[1][0]);
+            }
+
+            // Convert to UTF-8 from the original encoding
+            if (function_exists("mb_convert_encoding")) {
+                $title = @mb_convert_encoding($title, 'UTF-8', $encoding);
+            }
+
+            $title = trim($title);
+        }
+
+        if (utf8_strlen($title) > 0) {
+            $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
+            return $title;
+        } else {
+            // No title, so return filename
+            $uriparts = explode('/', $url);
+            $filename = end($uriparts);
+            unset($uriparts);
+
+            return $filename;
+        }
+    }
+}
+?>
index e1fbe3000c86276afd5331a3625c059c55a15eb9..b95e29bb986f48ac83f517eb227d936855c98c1b 100644 (file)
@@ -27,53 +27,11 @@ require_once 'www-header.php';
 
 /* Managing all possible inputs */
 isset($_GET['url']) ? define('GET_URL', $_GET['url']): define('GET_URL', '');
+$urlhelper = new SemanticScuttle_UrlHelper();
 
-function getTitle($url) {
-       $fd = @fopen($url, 'r');
-       if ($fd) {
-               $html = fread($fd, 1750);
-               fclose($fd);
-
-               // Get title from title tag
-               preg_match_all('/<title>(.*)<\/title>/si', $html, $matches);
-               $title = $matches[1][0];
-
-               $encoding = 'utf-8';
-               // Get encoding from charset attribute
-               preg_match_all('/<meta.*charset=([^;"]*)">/i', $html, $matches);
-               if (isset($matches[1][0])) {
-                       $encoding = strtoupper($matches[1][0]);
-               }
-
-               // Convert to UTF-8 from the original encoding
-               if (function_exists("mb_convert_encoding")) {
-                       $title = @mb_convert_encoding($title, 'UTF-8', $encoding);
-               }
-
-               $title = trim($title);
-
-               if (utf8_strlen($title) > 0) {
-                       $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
-                       return $title;
-               } else {
-                       // No title, so return filename
-                       $uriparts = explode('/', $url);
-                       $filename = end($uriparts);
-                       unset($uriparts);
-
-                       return $filename;
-               }
-       } else {
-               return false;
-       }
-}
-echo '<?xml version="1.0" encoding="utf-8"?>';
+echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
 ?>
 <response>
-<method>
-getTitle
-</method>
-<result>
-<?php echo getTitle(GET_URL); ?>
-</result>
+ <method>getTitle</method>
+ <result><?php echo htmlspecialchars($urlhelper->getTitle(GET_URL)); ?></result>
 </response>