Skip to content
This repository has been archived by the owner on Dec 16, 2019. It is now read-only.

Commit

Permalink
Move URL title method to dedicated class
Browse files Browse the repository at this point in the history
  • Loading branch information
cweiske committed May 9, 2016
1 parent 27e154e commit 8335376
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 46 deletions.
65 changes: 65 additions & 0 deletions src/SemanticScuttle/UrlHelper.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
?>
50 changes: 4 additions & 46 deletions www/ajaxGetTitle.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,53 +27,11 @@

/* 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>

0 comments on commit 8335376

Please sign in to comment.