Skip to content

Commit

Permalink
updated version of submission + layer
Browse files Browse the repository at this point in the history
  • Loading branch information
slifty committed Jun 27, 2014
1 parent 8a5eb72 commit 04d90c7
Show file tree
Hide file tree
Showing 50 changed files with 17,176 additions and 1,040 deletions.
39 changes: 39 additions & 0 deletions api/external/get_external.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/* GET THE CONTENT OF THE WEB PAGE */
$url = $_GET['url'];

// Cookie Jar
$ckfile = tempnam ("/tmp", "CURLCOOKIE");

// Clean the URL
$url = isset($_GET['url'])?$_GET['url']:"";
$url = substr($url,0,7) == "http://"?$url:"http://".$url;
$url = getFinalUrl($url);

// CURL
$ch = curl_init();
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$html = curl_exec($ch);
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$data = 'data:'.$content_type.';base64,'.base64_encode($html);
curl_close($ch);


/* EXTRACT THE TEXT */
$Readability = new Readability($html); // default charset is utf-8
$readabilityData = $Readability->getContent(); // throws an exception when no suitable content is found

$collectionJSON = '{
"html": '.DBConn::clean($readabilityData['content']).'
}';

$callback = $_REQUEST['callback'];
if($callback)
echo($callback.'('.$collectionJSON.');');
else
echo($collectionJSON);
?>
2 changes: 1 addition & 1 deletion api/layers/get_layers.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php
$collectionJSON = "";
$layer = Layer::getObject($_REQUEST['l']);
$callback = $_REQUEST['callback'];

if($layer->getItemID() == $layer) {
$collectionJSON = $layer->toJSON();
}

$collectionJSON = $layer->toJSON();

$callback = $_REQUEST['callback'];
if($callback)
echo($callback.'('.$collectionJSON.');');
else
Expand Down
66 changes: 64 additions & 2 deletions api/layers/post_layers.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
$question->save();

$statement = new Statement();
$statement->setContent($contributionData['statement']);
$statement->setContent($contributionData['content']);
$statement->setContext($contributionData['context']);
$statement->setContributionID($contribution->getItemID());
$statement->save();
Expand All @@ -31,7 +31,69 @@
$argument->save();
}

$collectionJSON = $layer->toJSON();
// Is there a URL?
if($_REQUEST['url']) {
// Cookie Jar
$ckfile = tempnam ("/tmp", "CURLCOOKIE");

// Clean the URL
$url = isset($_REQUEST['url'])?$_REQUEST['url']:"";
$url = substr($url,0,7) == "http://"?$url:"http://".$url;
$url = getFinalUrl($url);

// CURL
$ch = curl_init();
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$html = curl_exec($ch);
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$data = 'data:'.$content_type.';base64,'.base64_encode($html);
curl_close($ch);

// Remove all meta tags
$html = preg_replace('/\<\s*meta[^>]*>/i',"", $html);

// Remove all iframes (force a "noscript" environment)
$html = preg_replace('/\<\s*iframe[^>]*?>.*?\<\s*\/iframe[^>]*?>/is',"", $html);

// Remove all scripts (force a "noscript" environment)
$html = preg_replace('/<\s*script[^>]*>.*?<\s*\/script[^>]*?>/is',"", $html);
$html = preg_replace('/<\s*noscript[^>]*>/i',"", $html);

$data = preg_replace('/src=\".*\"(.*)dest_src=\"(.*)\"/i',"src=\"\\2\" \\1", $data);
$data = preg_replace('/dest_src=\"(.*)\"(.*)src=\".*\"/i',"src=\"\\1\" \\2", $data);

// General Image Fixes
$html = preg_replace('/src=\"\/(.*?)\"/i', "src=\"".$url_base."/\\1\"", $html);
$html = preg_replace('/src=\'\/(.*?)\'/i', "src='".$url_base."/\\1'", $html);

// General Link Fixes
$html = preg_replace('/href=\"\/(.*?)\"/i', "href=\"".$url_base."/\\1\"", $html);
$html = preg_replace('/href=\'\/(.*?)\'/i', "href='".$url_base."/\\1'", $html);

// CSS Link Fixes
$html = preg_replace('/\<link(.*?)text\/css(.*?)src\=(.*?)\/\>/i', "<link\\1text/css\\2href=\\3/>", $html);
} elseif ($_REQUEST['text']) {
$html = "<html><head></head><body>".str_replace("\n","<br />",$_REQUEST['text'])."</body></html>";
}
$script = "
<script src='".$BASE_DIRECTORY."js/goggles.js'></script><script>truthGoggles({server: '".$GOGGLES_DIRECTORY."',layerId: ".$layer->getItemID()."});</script>";


$html = preg_replace('/<\s*\/\s*head[^>]*>/', $script."</head>", $html);

$guid = uniqid("");
$file = fopen($_SERVER['DOCUMENT_ROOT'].$BASE_DIRECTORY."layers/".$guid.".html","w");
fwrite($file, $html);

$collectionJSON = '{
"layer": '.$layer->toJSON().',
"url": "'.$BASE_DIRECTORY."layers/".$guid.'"
}';

if(isset($jsonp) && $jsonp)
echo($callback.'('.$collectionJSON.');');
Expand Down
45 changes: 45 additions & 0 deletions includes/engine/helpers/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
function getFinalUrl( $url, $timeout = 5 ) {
$url = str_replace( "&amp;", "&", urldecode(trim($url)) );

$cookie = tempnam ("/tmp", "CURLCOOKIE");
$ch = curl_init();
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_ENCODING, "" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
$content = curl_exec( $ch );
$response = curl_getinfo( $ch );
curl_close ( $ch );

if ($response['http_code'] == 301 || $response['http_code'] == 302)
{
ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");
$headers = get_headers($response['url']);

$location = "";
foreach( $headers as $value )
{
if ( substr( strtolower($value), 0, 9 ) == "location:" )
return get_final_url( trim( substr( $value, 9, strlen($value) ) ) );
}
}

if ( preg_match("/window\.location\.replace\('(.*)'\)/i", $content, $value) ||
preg_match("/window\.location\=\"(.*)\"/i", $content, $value)
)
{
return get_final_url ( $value[1] );
}
else
{
return $response['url'];
}
}
?>
Loading

0 comments on commit 04d90c7

Please sign in to comment.