Commit aad49bd5 authored by Yorre Rajaonarivelo's avatar Yorre Rajaonarivelo
Browse files

amilio activitypub

parent 8039cda7
......@@ -2,67 +2,17 @@
namespace PixelHumain\PixelHumain\modules\news\controllers\actions;
use CacheHelper;
use DOMDocument;
use Exception;
use PixelHumain\PixelHumain\modules\news\models\UrlExtractor;
use Rest;
class GetPageContentAction extends \PixelHumain\PixelHumain\components\Action
{
public function run(){
if(isset($_POST['url'])){
$tags = $this->get_and_extract($_POST['url']);
$tags = UrlExtractor::extract($_POST['url']);
return Rest::json($tags);
}else{
return [];
}
}
private function file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
private function extract_tags($pageContents){
$tags = [];
$doc = new DOMDocument();
@$doc->loadHTML($pageContents);
//get title
$titleNodes = $doc->getElementsByTagName('title');
$tags['title'] = sizeof($titleNodes)>0?$titleNodes[0]->nodeValue:"";
//get metas
$metaNodes = $doc->getElementsByTagName('meta');
$tags["meta"] = [];
foreach($metaNodes as $meta){
if($meta->getAttribute("name") && $meta->getAttribute("content"))
$tags["meta"][$meta->getAttribute("name")] = $meta->getAttribute("content");
else if($meta->getAttribute("property") && $meta->getAttribute("content"))
$tags["meta"][$meta->getAttribute("property")] = $meta->getAttribute("content");
}
return $tags;
}
private function get_and_extract($url){
if($tags = CacheHelper::get(base64_encode($url))){
return $tags;
}else{
$content = $this->file_get_contents_curl($url);
$tags = $this->extract_tags($content);
CacheHelper::set(base64_encode($url), $tags);
return $tags;
}
}
}
\ No newline at end of file
......@@ -6,6 +6,7 @@ use News;
use Person;
use PixelHumain\PixelHumain\modules\citizenToolKit\components\activitypub\handlers\Handler;
use PixelHumain\PixelHumain\modules\citizenToolKit\models\activitypub\Activitypub;
use Preference;
use Rest;
use Yii;
......@@ -19,7 +20,7 @@ class SaveAction extends \PixelHumain\PixelHumain\components\Action
$result=News::save($_POST);
$user = Person::getById(Yii::app()->session["userId"]);
if(isset($user["preferences"]["notifications"]["desableActivitypub"]))
if(Preference::isActivitypubActivate($user["preferences"]))
Handler::handle(Activitypub::createNoteActivity($_POST), ["newsId"=>(string)$result["object"]["_id"]]);
if(!@$_REQUEST["json"]){
......
......@@ -799,7 +799,7 @@ class News {
public static function addParamsForNewsFromActivitypub($type, $id, $params){
$connectedUserId = Yii::app()->session["userId"];
$connectedUser = Person::getById($connectedUserId);
$activitypubEnable = isset($connectedUser["preferences"]["notifications"]["desableActivitypub"]);
$activitypubEnable = Preference::isActivitypubActivate($connectedUser["preferences"]);
if($type == Person::COLLECTION && $id == $connectedUserId && $activitypubEnable){
$activitypubParams = ["type" => "activitypub"];
......
<?php
namespace PixelHumain\PixelHumain\modules\news\models;
use CacheHelper;
use DOMDocument;
class UrlExtractor{
public static function extract($url){
if($tags = CacheHelper::get(base64_encode($url))){
return $tags;
}else{
$tags = self::extract_tags($url);
CacheHelper::set(base64_encode($url), $tags);
return $tags;
}
}
private static function file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
private static function extract_tags($url){
$pageContents = self::file_get_contents_curl($url);
$tags = [];
$doc = new DOMDocument();
@$doc->loadHTML($pageContents);
//get title
$titleNodes = $doc->getElementsByTagName('title');
$tags['title'] = sizeof($titleNodes)>0?$titleNodes[0]->nodeValue:"";
//get metas
$metaNodes = $doc->getElementsByTagName('meta');
$tags["meta"] = [];
foreach($metaNodes as $meta){
if($meta->getAttribute("name") && $meta->getAttribute("content"))
$tags["meta"][$meta->getAttribute("name")] = $meta->getAttribute("content");
else if($meta->getAttribute("property") && $meta->getAttribute("content"))
$tags["meta"][$meta->getAttribute("property")] = $meta->getAttribute("content");
}
//get links
$tags["link"] = [];
$headNodes = $doc->getElementsByTagName('head');
foreach($headNodes as $head){
$linkNodes = $head->getElementsByTagName("link");
foreach($linkNodes as $link){
if($link->getAttribute("rel") && $link->getAttribute("href"))
$tags["link"][$link->getAttribute("rel")] = self::getAbsoluteUrl($url, $link->getAttribute("href"));
}
}
return $tags;
}
private static function getAbsoluteUrl($baseUrl, $url){
if(preg_match("~^(?:f|ht)tps?://~i", $url))
return $url;
return $baseUrl.$url;
}
}
\ No newline at end of file
......@@ -22,6 +22,41 @@
.timeline-footer .tpap-actions li a span{
margin-left: 5px;
}
.provider-info{
display: flex;
align-items: center;
}
.provider-info div{
display: flex;
flex-direction: column;
align-items: flex-start;
}
.provider-info div span{
font-size: 11px;
background-color: #777;
color: white;
padding: 1px 4px;
border-radius: 2px;
}
.provider-info img{
height: 30px;
margin-left: 5px;
}
.provider-info a{
text-decoration: none;
font-size: 14px;
color: #777;
transition: .2s;
}
.provider-info a:hover{
color: #222;
}
</style>
<div class="timeline-heading text-center ">
<h5 class="text-left srcMedia">
......@@ -55,7 +90,7 @@
<?php } ?>
</div>
<div class="timeline-footer pull-left col-md-12 col-sm-12 col-xs-12 padding-top-5">
<div class="col-xs-12 pull-left padding-5">
<div class="col-xs-12 pull-left padding-5" style="display: flex; justify-content:space-between; align-items:center;">
<ul class="tpap-actions">
<li>
<a href="javascript:;" class="newsAddComment" data-media-id="<?= $key ?>" data-type="activitypub">
......@@ -66,6 +101,18 @@
</li>
<!-- <li><a href="javascript:;" class="letter-blue"><i class="fa fa-thumbs-up"></i><span>5</span> J’aime</span></a></li> -->
</ul>
<div class="provider-info">
<?php $providerInfo = $media["note"]["providerInfo"] ?>
<div>
<span>envoyé depuis</span>
<a href="<?= $media["note"]["url"] ?>" target="__blank">
<?= $providerInfo["title"] ?>
</a>
</div>
<?php if(isset($providerInfo["icon"])){ ?>
<img src="<?= $providerInfo["icon"] ?>" alt="">
<?php } ?>
</div>
</div>
<div class="col-md-12 col-sm-12 col-xs-12 no-padding pull-left margin-top-10" id="commentContent<?= $key ?>"></div>
</div>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment