From a4aa3728434f529b2d5e5a87b0168ff391336652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Martin?= Date: Mon, 29 Jul 2013 11:53:55 -0700 Subject: [PATCH 1/4] add Curl support via configuration page --- index.php | 60 +++++++++++++++++++++++++++++++++++++++++++--- tpl/configure.html | 3 +++ 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/index.php b/index.php index 1d13e55c2..5c42cbdd9 100644 --- a/index.php +++ b/index.php @@ -578,6 +578,53 @@ function getHTTP($url,$timeout=30) { return array($e->getMessage(),'',''); } +} + +function getHTTPWithCurl($url,$timeout=30){ + try + { + $ch = curl_init(); + curl_setopt ($ch, CURLOPT_URL, $url); + curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_HEADER,true); + // ob_start(); + $data = curl_exec($ch); + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); + curl_close($ch); + // $data = ob_get_contents(); + // echo "
data:".var_dump($data)."
"; //debug mma + // exit(0); //debug mma + // ob_end_clean(); + + if (!$data) { return array('HTTP Error',array(),''); } + + $header = substr($data, 0, $header_size); + $body = substr($data, $header_size); + + // $httpStatus=$httpCode; // eg. "HTTP/1.1 200 OK" + $header = explode ("\r\n", $header); + array_filter($header); + + $responseHeaders=http_parse_headers_shaarli($header); + + // echo "
";//debug mma
+		// var_dump($header);//debug mma
+		// echo "
"; //debug mma + // exit(0); //debug mma + + //var_dump($httpCode); + return array($header[0],$responseHeaders,$data); + } + catch (Exception $e) + { + return array($e->getMessage(),'',''); + } + + + + } // Extract title from an HTML document. @@ -1376,6 +1423,8 @@ function renderPage() $GLOBALS['disablesessionprotection']=!empty($_POST['disablesessionprotection']); $GLOBALS['disablejquery']=!empty($_POST['disablejquery']); $GLOBALS['privateLinkByDefault']=!empty($_POST['privateLinkByDefault']); + $GLOBALS['useCurl']=!empty($_POST['useCurl']); + writeConfig(); echo ''; exit; @@ -1541,12 +1590,16 @@ function renderPage() $description=''; $tags=''; $private=0; if (($url!='') && parse_url($url,PHP_URL_SCHEME)=='') $url = 'http://'.$url; // If this is an HTTP link, we try go get the page to extact the title (otherwise we will to straight to the edit form.) - if (empty($title) && parse_url($url,PHP_URL_SCHEME)=='http') + if (empty($title) && ((parse_url($url,PHP_URL_SCHEME)=='http') || (parse_url($url,PHP_URL_SCHEME)=='https'))) { - list($status,$headers,$data) = getHTTP($url,4); // Short timeout to keep the application responsive. + if ($GLOBALS['useCurl']) list($status,$headers,$data) = getHTTPWithCurl($url,4); // Short timeout to keep the application responsive. + list($status,$headers,$data) = getHTTPWithCurl($url,4); // Short timeout to keep the application responsive. + //$debug = getHTTPWithCurl($url,4); // Short timeout to keep the application responsive. // FIXME: Decode charset according to specified in either 1) HTTP response headers or 2) in html - if (strpos($status,'200 OK')!==false) $title=html_entity_decode(html_extract_title($data),ENT_QUOTES,'UTF-8'); + + if (strpos($status,'200 OK')!==false) $title=html_entity_decode(html_extract_title($data),ENT_QUOTES,'UTF-8'); + } if ($url=='') $url='?'.smallHash($linkdate); // In case of empty URL, this is just a text (with a link that point to itself) $link = array('linkdate'=>$linkdate,'title'=>$title,'url'=>$url,'description'=>$description,'tags'=>$tags,'private'=>0); @@ -1985,6 +2038,7 @@ function lazyThumbnail($url,$href=false) else $html.=' New link: + + Use Curl (instead get_content_file): + From 583bf44c4fa8e00ea9f0018fcbcd9ecf0bce1768 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Martin?= Date: Tue, 30 Jul 2013 18:31:28 +0200 Subject: [PATCH 2/4] remove some debug --- index.php | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/index.php b/index.php index 5c42cbdd9..17741373e 100644 --- a/index.php +++ b/index.php @@ -588,33 +588,22 @@ function getHTTPWithCurl($url,$timeout=30){ curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER,true); - // ob_start(); $data = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); curl_close($ch); - // $data = ob_get_contents(); - // echo "
data:".var_dump($data)."
"; //debug mma - // exit(0); //debug mma - // ob_end_clean(); + if (!$data) { return array('HTTP Error',array(),''); } $header = substr($data, 0, $header_size); $body = substr($data, $header_size); - // $httpStatus=$httpCode; // eg. "HTTP/1.1 200 OK" $header = explode ("\r\n", $header); array_filter($header); $responseHeaders=http_parse_headers_shaarli($header); - // echo "
";//debug mma
-		// var_dump($header);//debug mma
-		// echo "
"; //debug mma - // exit(0); //debug mma - - //var_dump($httpCode); return array($header[0],$responseHeaders,$data); } catch (Exception $e) @@ -2473,4 +2462,4 @@ function invalidateCaches() if (isset($_SERVER["QUERY_STRING"]) && startswith($_SERVER["QUERY_STRING"],'ws=')) { processWS(); exit; } // Webservices (for jQuery/jQueryUI) if (!isset($_SESSION['LINKS_PER_PAGE'])) $_SESSION['LINKS_PER_PAGE']=$GLOBALS['config']['LINKS_PER_PAGE']; renderPage(); -?> \ No newline at end of file +?> From f4dedd3c76be9a9368bb49f1a34953f6f7d6fa8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Martin?= Date: Wed, 31 Jul 2013 13:06:23 +0200 Subject: [PATCH 3/4] delete some blank lines --- index.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/index.php b/index.php index 17741373e..387f5e090 100644 --- a/index.php +++ b/index.php @@ -593,7 +593,6 @@ function getHTTPWithCurl($url,$timeout=30){ $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); curl_close($ch); - if (!$data) { return array('HTTP Error',array(),''); } $header = substr($data, 0, $header_size); @@ -610,10 +609,6 @@ function getHTTPWithCurl($url,$timeout=30){ { return array($e->getMessage(),'',''); } - - - - } // Extract title from an HTML document. From 0dd1c327ed0ea024faaf10343b69e2e5072bb8c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Martin?= Date: Wed, 31 Jul 2013 13:14:00 +0200 Subject: [PATCH 4/4] delete some blank lines && correct indent --- index.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/index.php b/index.php index 387f5e090..2779c55f3 100644 --- a/index.php +++ b/index.php @@ -1407,7 +1407,7 @@ function renderPage() $GLOBALS['disablesessionprotection']=!empty($_POST['disablesessionprotection']); $GLOBALS['disablejquery']=!empty($_POST['disablejquery']); $GLOBALS['privateLinkByDefault']=!empty($_POST['privateLinkByDefault']); - $GLOBALS['useCurl']=!empty($_POST['useCurl']); + $GLOBALS['useCurl']=!empty($_POST['useCurl']); writeConfig(); echo ''; @@ -1573,17 +1573,14 @@ function renderPage() $title = (empty($_GET['title']) ? '' : $_GET['title'] ); // Get title if it was provided in URL (by the bookmarklet). $description=''; $tags=''; $private=0; if (($url!='') && parse_url($url,PHP_URL_SCHEME)=='') $url = 'http://'.$url; - // If this is an HTTP link, we try go get the page to extact the title (otherwise we will to straight to the edit form.) - if (empty($title) && ((parse_url($url,PHP_URL_SCHEME)=='http') || (parse_url($url,PHP_URL_SCHEME)=='https'))) + // If this is an HTTP link (or HTTPS), we try go get the page to extact the title (otherwise we will to straight to the edit form.) + if (empty($title) && ((parse_url($url,PHP_URL_SCHEME)=='http') || (parse_url($url,PHP_URL_SCHEME)=='https'))) { if ($GLOBALS['useCurl']) list($status,$headers,$data) = getHTTPWithCurl($url,4); // Short timeout to keep the application responsive. list($status,$headers,$data) = getHTTPWithCurl($url,4); // Short timeout to keep the application responsive. - //$debug = getHTTPWithCurl($url,4); // Short timeout to keep the application responsive. // FIXME: Decode charset according to specified in either 1) HTTP response headers or 2) in html - - if (strpos($status,'200 OK')!==false) $title=html_entity_decode(html_extract_title($data),ENT_QUOTES,'UTF-8'); - + if (strpos($status,'200 OK')!==false) $title=html_entity_decode(html_extract_title($data),ENT_QUOTES,'UTF-8'); } if ($url=='') $url='?'.smallHash($linkdate); // In case of empty URL, this is just a text (with a link that point to itself) $link = array('linkdate'=>$linkdate,'title'=>$title,'url'=>$url,'description'=>$description,'tags'=>$tags,'private'=>0);