Bookmark: Cleanup libs remove lonely files and use existing core functions

This commit is contained in:
Brice Maron 2012-12-18 21:31:11 +00:00
parent 0f0fa25144
commit dcae167eef
7 changed files with 36 additions and 133 deletions

View File

@ -27,15 +27,13 @@
OCP\User::checkLoggedIn();
OCP\App::checkAppEnabled('bookmarks');
require_once 'bookmarksHelper.php';
// Prep screen if we come from the bookmarklet
$url ='';
if(isset($_GET['url'])) {
$url = $_GET['url'];
}
if(!isset($_GET['title']) || trim($_GET['title']) == '') {
$datas = getURLMetadata($url);
$datas = OC_Bookmarks_Bookmarks::getURLMetadata($url);
$title = isset($datas['title']) ? $datas['title'] : '';
}
else{

View File

@ -1,37 +0,0 @@
<?php
/**
* ownCloud - bookmarks plugin
*
* @author Arthur Schiwon
* @copyright 2011 Arthur Schiwon blizzz@arthur-schiwon.de
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
//no apps or filesystem
$RUNTIME_NOSETUPFS=true;
// Check if we are a user
OCP\JSON::checkLoggedIn();
OCP\JSON::callCheck();
OCP\JSON::checkAppEnabled('bookmarks');
require_once OC_App::getAppPath('bookmarks').'/bookmarksHelper.php';
$id = addBookmark($_POST['url'], $_POST['title'], $_POST['tags']);
OCP\JSON::success(array('data' => $id));

View File

@ -26,7 +26,6 @@ OCP\JSON::checkLoggedIn();
OCP\JSON::callCheck();
OCP\JSON::checkAppEnabled('bookmarks');
require_once OC_App::getAppPath('bookmarks').'/bookmarksHelper.php';
// If we go the dialog form submit
if(isset($_POST['url'])) {
@ -41,7 +40,7 @@ if(isset($_POST['url'])) {
}
else {
if(isset($_POST['from_own'])) {
$datas = getURLMetadata($_POST['url']);
$datas = OC_Bookmarks_Bookmarks::getURLMetadata($_POST['url']);
if(isset($datas['title'])) $title = $datas['title'];
}
$bm = OC_Bookmarks_Bookmarks::addBookmark($_POST['url'], $title, $tags, $_POST['description'], $pub);

View File

@ -24,12 +24,10 @@
OCP\JSON::checkLoggedIn();
OCP\JSON::checkAppEnabled('bookmarks');
require_once OC_App::getAppPath('bookmarks').'/bookmarksHelper.php';
$req_type=isset($_GET['type']) ? $_GET['type'] : '';
if($req_type == 'url_info' && $_GET['url']) {
$datas = getURLMetadata($_GET['url']);
$datas = OC_Bookmarks_Bookmarks::getURLMetadata($_GET['url']);
$title = isset($datas['title']) ? $datas['title'] : '';
OCP\JSON::success(array('title' => $title));
exit();

View File

@ -25,18 +25,17 @@
// Check if we are a user
OCP\JSON::checkLoggedIn();
OCP\JSON::checkAppEnabled('bookmarks');
require_once OC_App::getAppPath('bookmarks').'/bookmarksHelper.php';
$req_type= isset($_GET['type']) ? $_GET['type'] : 'bookmark';
if($req_type == 'rel_tags') {
$tags = analyzeTagRequest(isset($_GET['tag']) ? $_GET['tag'] : '');
$tags = OC_Bookmarks_Bookmarks::analyzeTagRequest(isset($_GET['tag']) ? $_GET['tag'] : '');
$qtags = OC_Bookmarks_Bookmarks::findTags($tags);
OCP\JSON::success(array('data' => $qtags));
}
else { // type == bookmark
$filterTag = analyzeTagRequest(isset($_GET['tag']) ? $_GET['tag'] : '');
$filterTag = OC_Bookmarks_Bookmarks::analyzeTagRequest(isset($_GET['tag']) ? $_GET['tag'] : '');
$offset = isset($_GET['page']) ? intval($_GET['page']) * 10 : 0;

View File

@ -1,85 +0,0 @@
<?php
// Source: http://www.php.net/manual/de/function.curl-setopt.php#102121
// This works around a safe_mode/open_basedir restriction
function curl_exec_follow(/*resource*/ $ch, /*int*/ &$maxredirect = null) {
$mr = $maxredirect === null ? 5 : intval($maxredirect);
if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0);
curl_setopt($ch, CURLOPT_MAXREDIRS, $mr);
} else {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
if ($mr > 0) {
$newurl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$rch = curl_copy_handle($ch);
curl_setopt($ch, CURLOPT_USERAGENT, "Owncloud Bookmark Crawl");
curl_setopt($rch, CURLOPT_HEADER, true);
curl_setopt($rch, CURLOPT_NOBODY, true);
curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
curl_setopt($rch, CURLOPT_RETURNTRANSFER, true);
do {
curl_setopt($rch, CURLOPT_URL, $newurl);
$header = curl_exec($rch);
if (curl_errno($rch)) {
$code = 0;
} else {
$code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
if ($code == 301 || $code == 302) {
preg_match('/Location:(.*?)\n/', $header, $matches);
$newurl = trim(array_pop($matches));
} else {
$code = 0;
}
}
} while ($code && --$mr);
curl_close($rch);
if (!$mr) {
if ($maxredirect === null) {
OCP\Util::writeLog(
'bookmark',
'Too many redirects. When following redirects, libcurl hit the maximum amount on bookmark',
OCP\Util::ERROR);
} else {
$maxredirect = 0;
}
return false;
}
curl_setopt($ch, CURLOPT_URL, $newurl);
}
}
return curl_exec($ch);
}
function getURLMetadata($url) {
//allow only http(s) and (s)ftp
$protocols = '/^[hs]{0,1}[tf]{0,1}tp[s]{0,1}\:\/\//i';
//if not (allowed) protocol is given, assume http
if(preg_match($protocols, $url) == 0) {
$url = 'http://' . $url;
}
$metadata['url'] = $url;
if (!function_exists('curl_init')) {
return $metadata;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec_follow($ch);
curl_close($ch);
@preg_match( "/<title>(.*)<\/title>/sUi", $page, $match );
$metadata['title'] = htmlspecialchars_decode(@$match[1]);
return $metadata;
}
function analyzeTagRequest($line) {
$tags = explode(',', $line);
$filterTag = array();
foreach($tags as $tag){
if(trim($tag) != '')
$filterTag[] = trim($tag);
}
return $filterTag;
}

View File

@ -375,4 +375,35 @@ class OC_Bookmarks_Bookmarks{
OCP\DB::commit();
return array();
}
public static function getURLMetadata($url) {
//allow only http(s) and (s)ftp
$protocols = '/^[hs]{0,1}[tf]{0,1}tp[s]{0,1}\:\/\//i';
//if not (allowed) protocol is given, assume http
if(preg_match($protocols, $url) == 0) {
$url = 'http://' . $url;
}
$metadata['url'] = $url;
if (!function_exists('curl_init')) {
return $metadata;
}
$page = OC_Util::getUrlContent($url);
if($page) {
if(preg_match( "/<title>(.*)<\/title>/sUi", $page, $match ) !== false)
$metadata['title'] = htmlspecialchars_decode($match[1]);
}
return $metadata;
}
public static function analyzeTagRequest($line) {
$tags = explode(',', $line);
$filterTag = array();
foreach($tags as $tag){
if(trim($tag) != '')
$filterTag[] = trim($tag);
}
return $filterTag;
}
}