Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Curl support via configuration page #114

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,37 @@ function getHTTP($url,$timeout=30)
}
}

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);
$data = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
curl_close($ch);

if (!$data) { return array('HTTP Error',array(),''); }

$header = substr($data, 0, $header_size);
$body = substr($data, $header_size);

$header = explode ("\r\n", $header);
array_filter($header);

$responseHeaders=http_parse_headers_shaarli($header);

return array($header[0],$responseHeaders,$data);
}
catch (Exception $e)
{
return array($e->getMessage(),'','');
}
}

// Extract title from an HTML document.
// (Returns an empty string if not found.)
function html_extract_title($html)
Expand Down Expand Up @@ -1376,6 +1407,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 '<script language="JavaScript">alert("Configuration was saved.");document.location=\'?do=tools\';</script>';
exit;
Expand Down Expand Up @@ -1540,13 +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')
// 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')))
{
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.
// FIXME: Decode charset according to specified in either 1) HTTP response headers or 2) <head> 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);
Expand Down Expand Up @@ -1985,6 +2019,7 @@ function lazyThumbnail($url,$href=false)
else
$html.='<img class="lazyimage" src="#" data-original="'.htmlspecialchars($t['src']).'"';

$html.='<img class="lazyimage" src="#" data-original="'.htmlspecialchars($t['src']).'"';
if (!empty($t['width'])) $html.=' width="'.htmlspecialchars($t['width']).'"';
if (!empty($t['height'])) $html.=' height="'.htmlspecialchars($t['height']).'"';
if (!empty($t['style'])) $html.=' style="'.htmlspecialchars($t['style']).'"';
Expand Down Expand Up @@ -2419,4 +2454,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();
?>
?>
3 changes: 3 additions & 0 deletions tpl/configure.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
</td></tr>
<tr><td valign="top"><b>New link:</b></td><td>
<input type="checkbox" name="privateLinkByDefault" id="privateLinkByDefault" {if="!empty($GLOBALS['privateLinkByDefault'])"}checked{/if}/><label for="privateLinkByDefault">&nbsp;All new link are private by default</label></td>
</tr>
<tr><td valign="top"><b>Use Curl (instead get_content_file):</b></td><td>
<input type="checkbox" name="useCurl" id="useCurl" {if="!empty($GLOBALS['useCurl'])"}checked{/if}/><label for="useCurl">&nbsp;Use Curl if get_content_file doesn't work fine.</label></td>
</tr>
<tr><td></td><td align="right"><input type="submit" name="Save" value="Save config" class="bigbutton"></td></tr>
</table>
Expand Down