Minor fix: correct url checking

git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@196 b3834d28-1941-0410-a4f8-b48e95affb8f
This commit is contained in:
mensonge 2008-12-04 15:08:09 +00:00
parent 6b70f8355a
commit d72790690e
2 changed files with 125 additions and 118 deletions

View file

@ -1,7 +1,7 @@
<?php <?php
/* /*
Export for Google Custom Search Export for Google Custom Search
*/ */
// Force HTTP authentication first! // Force HTTP authentication first!
//require_once('httpauth.inc.php'); //require_once('httpauth.inc.php');
@ -11,22 +11,22 @@ $bookmarkservice =& ServiceFactory::getServiceInstance('BookmarkService');
$userservice =& ServiceFactory::getServiceInstance('UserService'); $userservice =& ServiceFactory::getServiceInstance('UserService');
/* /*
// Restrict to admins? // Restrict to admins?
if(!$userservice->isAdmin($userservice->getCurrentUserId())) { if(!$userservice->isAdmin($userservice->getCurrentUserId())) {
die(T_('You are not allowed to do this action (admin access)')); die(T_('You are not allowed to do this action (admin access)'));
}*/ }*/
// Check if queried format is xml // Check if queried format is xml
if (isset($_REQUEST['xml']) && (trim($_REQUEST['xml']) == 1)) if (isset($_REQUEST['xml']) && (trim($_REQUEST['xml']) == 1))
$xml = true; $xml = true;
else else
$xml = false; $xml = false;
// Check to see if a tag was specified. // Check to see if a tag was specified.
if (isset($_REQUEST['tag']) && (trim($_REQUEST['tag']) != '')) if (isset($_REQUEST['tag']) && (trim($_REQUEST['tag']) != ''))
$tag = trim($_REQUEST['tag']); $tag = trim($_REQUEST['tag']);
else else
$tag = NULL; $tag = NULL;
// Get the posts relevant to the passed-in variables. // Get the posts relevant to the passed-in variables.
$bookmarks =& $bookmarkservice->getBookmarks(0, NULL, NULL, $tag, NULL, getSortOrder()); $bookmarks =& $bookmarkservice->getBookmarks(0, NULL, NULL, $tag, NULL, getSortOrder());
@ -37,23 +37,26 @@ $currentusername = $currentuser[$userservice->getFieldName('username')];
// Set up the plain file and output all the posts. // Set up the plain file and output all the posts.
header('Content-Type: text/plain'); header('Content-Type: text/plain');
if(!$xml) { if(!$xml) {
header('Content-Type: text/plain'); header('Content-Type: text/plain');
foreach($bookmarks['bookmarks'] as $row) { foreach($bookmarks['bookmarks'] as $row) {
echo $row['bAddress']."\n"; if(checkUrl($row['bAddress'], false)) {
} echo $row['bAddress']."\n";
} else { }
header('Content-Type: application/xml');
echo '<GoogleCustomizations>'."\n";
echo ' <Annotations>'."\n";
foreach($bookmarks['bookmarks'] as $row) {
if(substr($row['bAddress'], 0, 7) == "http://") {
echo ' <Annotation about="'.filter($row['bAddress']).'">'."\n";
echo ' <Label name="include"/>'."\n";
echo ' </Annotation>'."\n";
} }
} } else {
echo ' </Annotations>'."\n"; header('Content-Type: application/xml');
echo '</GoogleCustomizations>'."\n"; echo '<GoogleCustomizations>'."\n";
echo ' <Annotations>'."\n";
foreach($bookmarks['bookmarks'] as $row) {
//if(substr($row['bAddress'], 0, 7) == "http://") {
if(checkUrl($row['bAddress'], false)) {
echo ' <Annotation about="'.filter($row['bAddress']).'">'."\n";
echo ' <Label name="include"/>'."\n";
echo ' </Annotation>'."\n";
}
}
echo ' </Annotations>'."\n";
echo '</GoogleCustomizations>'."\n";
} }
?> ?>

View file

@ -6,152 +6,156 @@
// - direction = out: convert spaces to underscores; // - direction = out: convert spaces to underscores;
// - direction = in: convert underscores to spaces. // - direction = in: convert underscores to spaces.
function convertTag($tag, $direction = 'out') { function convertTag($tag, $direction = 'out') {
if ($direction == 'out') { if ($direction == 'out') {
$tag = str_replace(' ', '_', $tag); $tag = str_replace(' ', '_', $tag);
} else { } else {
$tag = str_replace('_', ' ', $tag); $tag = str_replace('_', ' ', $tag);
} }
return $tag; return $tag;
} }
function filter($data, $type = NULL) { function filter($data, $type = NULL) {
if (is_string($data)) { if (is_string($data)) {
$data = trim($data); $data = trim($data);
$data = stripslashes($data); $data = stripslashes($data);
switch ($type) { switch ($type) {
case 'url': case 'url':
$data = rawurlencode($data); $data = rawurlencode($data);
break; break;
default: default:
$data = htmlspecialchars($data); $data = htmlspecialchars($data);
break; break;
} }
} else if (is_array($data)) { } else if (is_array($data)) {
foreach(array_keys($data) as $key) { foreach(array_keys($data) as $key) {
$row =& $data[$key]; $row =& $data[$key];
$row = filter($row, $type); $row = filter($row, $type);
} }
} }
return $data; return $data;
} }
function getPerPageCount() { function getPerPageCount() {
global $defaultPerPage; global $defaultPerPage;
return $defaultPerPage; return $defaultPerPage;
} }
function getSortOrder($override = NULL) { function getSortOrder($override = NULL) {
global $defaultOrderBy; global $defaultOrderBy;
if (isset($_GET['sort'])) { if (isset($_GET['sort'])) {
return $_GET['sort']; return $_GET['sort'];
} else if (isset($override)) { } else if (isset($override)) {
return $override; return $override;
} else { } else {
return $defaultOrderBy; return $defaultOrderBy;
} }
} }
function multi_array_search($needle, $haystack) { function multi_array_search($needle, $haystack) {
if (is_array($haystack)) { if (is_array($haystack)) {
foreach(array_keys($haystack) as $key) { foreach(array_keys($haystack) as $key) {
$value =& $haystack[$key]; $value =& $haystack[$key];
$result = multi_array_search($needle, $value); $result = multi_array_search($needle, $value);
if (is_array($result)) { if (is_array($result)) {
$return = $result; $return = $result;
array_unshift($return, $key); array_unshift($return, $key);
return $return; return $return;
} elseif ($result == true) { } elseif ($result == true) {
$return[] = $key; $return[] = $key;
return $return; return $return;
} }
} }
return false; return false;
} else { } else {
if ($needle === $haystack) { if ($needle === $haystack) {
return true; return true;
} else { } else {
return false; return false;
} }
} }
} }
function createURL($page = '', $ending = '') { function createURL($page = '', $ending = '') {
global $cleanurls; global $cleanurls;
if (!$cleanurls && $page != '') { if (!$cleanurls && $page != '') {
$page .= '.php'; $page .= '.php';
} }
return ROOT . $page .'/'. $ending; return ROOT . $page .'/'. $ending;
} }
/* Shorten a string like a URL for example by cutting the middle of it */ /* Shorten a string like a URL for example by cutting the middle of it */
function shortenString($string, $maxSize=75) { function shortenString($string, $maxSize=75) {
$output = ''; $output = '';
if(strlen($string) > $maxSize) { if(strlen($string) > $maxSize) {
$output = substr($string, 0, $maxSize/2).'...'.substr($string, -$maxSize/2); $output = substr($string, 0, $maxSize/2).'...'.substr($string, -$maxSize/2);
} else { } else {
$output = $string; $output = $string;
} }
return $output; return $output;
} }
/* Check url format and check online if the url is a valid page (Not a 404 error for example) */ /* Check url format and check online if the url is a valid page (Not a 404 error for example) */
function checkUrl($url) { function checkUrl($url, $checkOnline = true) {
//check format //check format
if(!preg_match("#(ht|f)tp(s?)\://\S+\.\S+#i",$url)) { if(!preg_match("#(ht|f)tp(s?)\://\S+\.\S+#i",$url)) {
return false; return false;
} }
//look if the page doesn't return a void or 40X or 50X HTTP code error if($checkOnline) {
$h = @get_headers($url); //look if the page doesn't return a void or 40X or 50X HTTP code error
if(is_array($h) && strpos($h[0], '40') === false && strpos($h[0], '50') === false) { $h = @get_headers($url);
return true; if(is_array($h) && strpos($h[0], '40') === false && strpos($h[0], '50') === false) {
return true;
} else {
return false;
}
} else { } else {
return false; return true;
} }
} }
function message_die($msg_code, $msg_text = '', $msg_title = '', $err_line = '', $err_file = '', $sql = '', $db = NULL) { function message_die($msg_code, $msg_text = '', $msg_title = '', $err_line = '', $err_file = '', $sql = '', $db = NULL) {
if(defined('HAS_DIED')) if(defined('HAS_DIED'))
die(T_('message_die() was called multiple times.')); die(T_('message_die() was called multiple times.'));
define('HAS_DIED', 1); define('HAS_DIED', 1);
$sql_store = $sql; $sql_store = $sql;
// Get SQL error if we are debugging. Do this as soon as possible to prevent // Get SQL error if we are debugging. Do this as soon as possible to prevent
// subsequent queries from overwriting the status of sql_error() // subsequent queries from overwriting the status of sql_error()
if (DEBUG_MODE && ($msg_code == GENERAL_ERROR || $msg_code == CRITICAL_ERROR)) { if (DEBUG_MODE && ($msg_code == GENERAL_ERROR || $msg_code == CRITICAL_ERROR)) {
$sql_error = is_null($db) ? '' : $db->sql_error(); $sql_error = is_null($db) ? '' : $db->sql_error();
$debug_text = ''; $debug_text = '';
if ($sql_error['message'] != '') if ($sql_error['message'] != '')
$debug_text .= '<br /><br />'. T_('SQL Error') .' : '. $sql_error['code'] .' '. $sql_error['message']; $debug_text .= '<br /><br />'. T_('SQL Error') .' : '. $sql_error['code'] .' '. $sql_error['message'];
if ($sql_store != '') if ($sql_store != '')
$debug_text .= '<br /><br />'. $sql_store; $debug_text .= '<br /><br />'. $sql_store;
if ($err_line != '' && $err_file != '') if ($err_line != '' && $err_file != '')
$debug_text .= '</br /><br />'. T_('Line') .' : '. $err_line .'<br />'. T_('File') .' :'. $err_file; $debug_text .= '</br /><br />'. T_('Line') .' : '. $err_line .'<br />'. T_('File') .' :'. $err_file;
} }
switch($msg_code) { switch($msg_code) {
case GENERAL_MESSAGE: case GENERAL_MESSAGE:
if ($msg_title == '') if ($msg_title == '')
$msg_title = T_('Information'); $msg_title = T_('Information');
break; break;
case CRITICAL_MESSAGE: case CRITICAL_MESSAGE:
if ($msg_title == '') if ($msg_title == '')
$msg_title = T_('Critical Information'); $msg_title = T_('Critical Information');
break; break;
case GENERAL_ERROR: case GENERAL_ERROR:
if ($msg_text == '') if ($msg_text == '')
$msg_text = T_('An error occured'); $msg_text = T_('An error occured');
if ($msg_title == '') if ($msg_title == '')
$msg_title = T_('General Error'); $msg_title = T_('General Error');
break; break;
case CRITICAL_ERROR: case CRITICAL_ERROR:
@ -159,10 +163,10 @@ function message_die($msg_code, $msg_text = '', $msg_title = '', $err_line = '',
// available so we're going to dump out a simple echo'd statement // available so we're going to dump out a simple echo'd statement
if ($msg_text == '') if ($msg_text == '')
$msg_text = T_('An critical error occured'); $msg_text = T_('An critical error occured');
if ($msg_title == '') if ($msg_title == '')
$msg_title = T_('Critical Error'); $msg_title = T_('Critical Error');
break; break;
} }
@ -171,7 +175,7 @@ function message_die($msg_code, $msg_text = '', $msg_title = '', $err_line = '',
// set TRUE by accident (preventing confusion for the end user!) // set TRUE by accident (preventing confusion for the end user!)
if (DEBUG_MODE && ($msg_code == GENERAL_ERROR || $msg_code == CRITICAL_ERROR)) { if (DEBUG_MODE && ($msg_code == GENERAL_ERROR || $msg_code == CRITICAL_ERROR)) {
if ($debug_text != '') if ($debug_text != '')
$msg_text = $msg_text . '<br /><br /><strong>'. T_('DEBUG MODE') .'</strong>'. $debug_text; $msg_text = $msg_text . '<br /><br /><strong>'. T_('DEBUG MODE') .'</strong>'. $debug_text;
} }
echo "<html>\n<body>\n". $msg_title ."\n<br /><br />\n". $msg_text ."</body>\n</html>"; echo "<html>\n<body>\n". $msg_title ."\n<br /><br />\n". $msg_text ."</body>\n</html>";