Version avec modules séparés !
This commit is contained in:
88
modules/blogs/Blogs.php
Normal file
88
modules/blogs/Blogs.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
class blogs extends Module {
|
||||
|
||||
protected static $paramsList = array(
|
||||
'visibility',
|
||||
'x',
|
||||
'y'
|
||||
);
|
||||
public $params = array();
|
||||
|
||||
public function __construct($params){
|
||||
$this->moduleName = get_class();
|
||||
$this->pathToModule = 'modules/'.$this->moduleName.'/';
|
||||
$this->setParams($params);
|
||||
echo '<script type="text/javascript" src="'.$this->pathToModule.'js/'.$this->moduleName.'.js"></script>
|
||||
<link rel="stylesheet" href="'.$this->pathToModule.'css/'.$this->moduleName.'.css" type="text/css" />';
|
||||
require($this->pathToModule.'includes/blogs_last_post.php');
|
||||
echo '<div id="blogs" style="top:'.$params['y'].'; left :'.$params['x'].';"></div>';
|
||||
echo '<a href="#blogLinksManager" id="blog-links-manager"><img src="images/interface/blogs_edit.png" /></a>';
|
||||
echo '<div style="display:none;">
|
||||
<div id="blogLinksManager">
|
||||
<h3>Blogs Management</h3><br/>
|
||||
<h4>Delete Site feed</h4>
|
||||
<ul>';
|
||||
$blogs = new Blogs_last_post();
|
||||
foreach($blogs->getLinks() as $link)
|
||||
echo '<li>'.$link['url'].' <img src="images/interface/delete.png" id="link-'.$link['name'].'"/></li>';
|
||||
echo '</ul>
|
||||
<h4>Insert Site feed</h4>
|
||||
<form action="modules/blogs/includes/addSite.php" method="POST">
|
||||
<input type="text" id="newLink" name="newLink" value="url" />
|
||||
<input type="submit" id="link-submit" class="green-button" value="Ajouter"></input>
|
||||
</form>
|
||||
</div>
|
||||
</div>';
|
||||
}
|
||||
|
||||
private function setParams($params){
|
||||
$this->params = $params;
|
||||
}
|
||||
|
||||
public static function start($params){
|
||||
$blogs = new blogs($params);
|
||||
}
|
||||
|
||||
public function setX($x){
|
||||
$xmla = simplexml_load_file('../'.AccueilModules::CONFIG_FILE);
|
||||
|
||||
$path = $xmla->xpath("//item[@id='blogs']");
|
||||
$path[0]->x = $x;
|
||||
|
||||
$xmla->asXML('../'.AccueilModules::CONFIG_FILE);
|
||||
|
||||
echo "ok";
|
||||
}
|
||||
|
||||
public function setY($y){
|
||||
$xmla = simplexml_load_file('../'.AccueilModules::CONFIG_FILE);
|
||||
|
||||
$path = $xmla->xpath("//item[@id='blogs']");
|
||||
$path[0]->y = $y;
|
||||
|
||||
$xmla->asXML('../'.AccueilModules::CONFIG_FILE);
|
||||
|
||||
echo "ok";
|
||||
}
|
||||
|
||||
public function setVisibility($visibility){
|
||||
$xmla = simplexml_load_file('../'.AccueilModules::CONFIG_FILE);
|
||||
|
||||
$path = $xmla->xpath("//item[@id='blogs']");
|
||||
$path[0]->visibility = $visibility;
|
||||
|
||||
$xmla->asXML('../'.AccueilModules::CONFIG_FILE);
|
||||
|
||||
echo "ok";
|
||||
}
|
||||
|
||||
public static function updateConfig($updated){
|
||||
foreach ($updated as $what=>$withWhat){
|
||||
if(in_array($what, self::$paramsList)){
|
||||
call_user_func(array(get_class(), "set".ucfirst($what)), $withWhat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
0
modules/blogs/css/blogs.css
Normal file
0
modules/blogs/css/blogs.css
Normal file
18
modules/blogs/db/blog_links.xml
Normal file
18
modules/blogs/db/blog_links.xml
Normal file
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0"?>
|
||||
<links>
|
||||
<link>
|
||||
<number>0</number>
|
||||
<name>PenelopeBagieu</name>
|
||||
<url>http://www.penelope-jolicoeur.com/</url>
|
||||
</link>
|
||||
<link>
|
||||
<number>12</number>
|
||||
<name>CyanideAndHapiness</name>
|
||||
<url>http://www.explosm.net/comics/</url>
|
||||
</link>
|
||||
<link>
|
||||
<number>0</number>
|
||||
<name>MargauxMotin</name>
|
||||
<url>http://margauxmotin.typepad.fr/</url>
|
||||
</link>
|
||||
<link><name>test</name><number>n-0</number><url>http://blog-de-vincent.blogspot.com/</url></link></links>
|
251
modules/blogs/includes/CURL.php
Normal file
251
modules/blogs/includes/CURL.php
Normal file
@@ -0,0 +1,251 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* OO cURL Class
|
||||
* Object oriented wrapper for the cURL library.
|
||||
* @author David Hopkins (semlabs.co.uk)
|
||||
* @version 0.3
|
||||
*/
|
||||
class CURL
|
||||
{
|
||||
|
||||
public $sessions = array();
|
||||
public $retry = 0;
|
||||
|
||||
/**
|
||||
* Adds a cURL session to stack
|
||||
* @param $url string, session's URL
|
||||
* @param $opts array, optional array of cURL options and values
|
||||
*/
|
||||
public function addSession( $url, $opts = false )
|
||||
{
|
||||
$this->sessions[] = curl_init( $url );
|
||||
if( $opts != false )
|
||||
{
|
||||
$key = count( $this->sessions ) - 1;
|
||||
$this->setOpts( $opts, $key );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an option to a cURL session
|
||||
* @param $option constant, cURL option
|
||||
* @param $value mixed, value of option
|
||||
* @param $key int, session key to set option for
|
||||
*/
|
||||
public function setOpt( $option, $value, $key = 0 )
|
||||
{
|
||||
curl_setopt( $this->sessions[$key], $option, $value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an array of options to a cURL session
|
||||
* @param $options array, array of cURL options and values
|
||||
* @param $key int, session key to set option for
|
||||
*/
|
||||
public function setOpts( $options, $key = 0 )
|
||||
{
|
||||
curl_setopt_array( $this->sessions[$key], $options );
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes as cURL session
|
||||
* @param $key int, optional argument if you only want to execute one session
|
||||
*/
|
||||
public function exec( $key = false )
|
||||
{
|
||||
$no = count( $this->sessions );
|
||||
|
||||
if( $no == 1 )
|
||||
$res = $this->execSingle();
|
||||
elseif( $no > 1 ) {
|
||||
if( $key === false )
|
||||
$res = $this->execMulti();
|
||||
else
|
||||
$res = $this->execSingle( $key );
|
||||
}
|
||||
|
||||
if( $res )
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a single cURL session
|
||||
* @param $key int, id of session to execute
|
||||
* @return array of content if CURLOPT_RETURNTRANSFER is set
|
||||
*/
|
||||
public function execSingle( $key = 0 )
|
||||
{
|
||||
if( $this->retry > 0 )
|
||||
{
|
||||
$retry = $this->retry;
|
||||
$code = 0;
|
||||
while( $retry >= 0 && ( $code[0] == 0 || $code[0] >= 400 ) )
|
||||
{
|
||||
$res = curl_exec( $this->sessions[$key] );
|
||||
$code = $this->info( $key, CURLINFO_HTTP_CODE );
|
||||
|
||||
$retry--;
|
||||
}
|
||||
}
|
||||
else
|
||||
$res = curl_exec( $this->sessions[$key] );
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a stack of sessions
|
||||
* @return array of content if CURLOPT_RETURNTRANSFER is set
|
||||
*/
|
||||
public function execMulti()
|
||||
{
|
||||
$mh = curl_multi_init();
|
||||
|
||||
#Add all sessions to multi handle
|
||||
foreach ( $this->sessions as $i => $url )
|
||||
curl_multi_add_handle( $mh, $this->sessions[$i] );
|
||||
|
||||
do
|
||||
$mrc = curl_multi_exec( $mh, $active );
|
||||
while ( $mrc == CURLM_CALL_MULTI_PERFORM );
|
||||
|
||||
while ( $active && $mrc == CURLM_OK )
|
||||
{
|
||||
if ( curl_multi_select( $mh ) != -1 )
|
||||
{
|
||||
do
|
||||
$mrc = curl_multi_exec( $mh, $active );
|
||||
while ( $mrc == CURLM_CALL_MULTI_PERFORM );
|
||||
}
|
||||
}
|
||||
|
||||
if ( $mrc != CURLM_OK )
|
||||
echo "Curl multi read error $mrc\n";
|
||||
|
||||
#Get content foreach session, retry if applied
|
||||
foreach ( $this->sessions as $i => $url )
|
||||
{
|
||||
$code = $this->info( $i, CURLINFO_HTTP_CODE );
|
||||
if( $code[0] > 0 && $code[0] < 400 )
|
||||
$res[] = curl_multi_getcontent( $this->sessions[$i] );
|
||||
else
|
||||
{
|
||||
if( $this->retry > 0 )
|
||||
{
|
||||
$retry = $this->retry;
|
||||
$this->retry -= 1;
|
||||
$eRes = $this->execSingle( $i );
|
||||
|
||||
if( $eRes )
|
||||
$res[] = $eRes;
|
||||
else
|
||||
$res[] = false;
|
||||
|
||||
$this->retry = $retry;
|
||||
echo '1';
|
||||
}
|
||||
else
|
||||
$res[] = false;
|
||||
}
|
||||
|
||||
curl_multi_remove_handle( $mh, $this->sessions[$i] );
|
||||
}
|
||||
|
||||
curl_multi_close( $mh );
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes cURL sessions
|
||||
* @param $key int, optional session to close
|
||||
*/
|
||||
public function close( $key = false )
|
||||
{
|
||||
if( $key === false )
|
||||
{
|
||||
foreach( $this->sessions as $session )
|
||||
curl_close( $session );
|
||||
}
|
||||
else
|
||||
curl_close( $this->sessions[$key] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all cURL sessions
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
foreach( $this->sessions as $session )
|
||||
curl_close( $session );
|
||||
unset( $this->sessions );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of session information
|
||||
* @param $key int, optional session key to return info on
|
||||
* @param $opt constant, optional option to return
|
||||
*/
|
||||
public function info( $key = false, $opt = false )
|
||||
{
|
||||
if( $key === false )
|
||||
{
|
||||
foreach( $this->sessions as $key => $session )
|
||||
{
|
||||
if( $opt )
|
||||
$info[] = curl_getinfo( $this->sessions[$key], $opt );
|
||||
else
|
||||
$info[] = curl_getinfo( $this->sessions[$key] );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if( $opt )
|
||||
$info[] = curl_getinfo( $this->sessions[$key], $opt );
|
||||
else
|
||||
$info[] = curl_getinfo( $this->sessions[$key] );
|
||||
}
|
||||
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of errors
|
||||
* @param $key int, optional session key to retun error on
|
||||
* @return array of error messages
|
||||
*/
|
||||
public function error( $key = false )
|
||||
{
|
||||
if( $key === false )
|
||||
{
|
||||
foreach( $this->sessions as $session )
|
||||
$errors[] = curl_error( $session );
|
||||
}
|
||||
else
|
||||
$errors[] = curl_error( $this->sessions[$key] );
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of session error numbers
|
||||
* @param $key int, optional session key to retun error on
|
||||
* @return array of error codes
|
||||
*/
|
||||
public function errorNo( $key = false )
|
||||
{
|
||||
if( $key === false )
|
||||
{
|
||||
foreach( $this->sessions as $session )
|
||||
$errors[] = curl_errno( $session );
|
||||
}
|
||||
else
|
||||
$errors[] = curl_errno( $this->sessions[$key] );
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
41
modules/blogs/includes/addSite.php
Normal file
41
modules/blogs/includes/addSite.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?
|
||||
$url = $_POST['newLink'];
|
||||
if($url == '' || !isset($_POST['newLink']))
|
||||
header('Location: index.php');
|
||||
?>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||
<link rel="SHORTCUT ICON" href="../favicon.ico"/>
|
||||
<link rel="stylesheet" type="text/css" href="css/main.css">
|
||||
<link rel="stylesheet" type="text/css" href="css/jquery.fancybox-1.3.0.css">
|
||||
<script type="text/javascript" src="js/jquery-1.3.1.min.js"></script>
|
||||
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
|
||||
|
||||
<title>Ma Page d'accueil</title>
|
||||
<style>img{max-width:100px;max-height:100px;padding:5px;} img:hover{border:5px solid #00AAFF;padding:0;}</style>
|
||||
<script>
|
||||
$(document).ready(function(){
|
||||
$('.choose').click(function(){
|
||||
var id = $(this).attr('id');
|
||||
var url = "<?=$url?>";
|
||||
var name = 'test';
|
||||
$.post('addSiteXML.php', {number: id, url: url, name: name}, function(data){
|
||||
document.location.href="index.php"
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<?
|
||||
echo 'URL : '.$url.'<br/>';
|
||||
require('blogs_last_post.php');
|
||||
$opts = array(CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_TIMEOUT => 10);
|
||||
$blogs = new Blogs_last_post();
|
||||
$blogs->addSession($url, $opts);
|
||||
$result = $blogs->exec();
|
||||
echo $blogs->getAllImagesToChoose($result);
|
||||
$blogs->clear();
|
11
modules/blogs/includes/blogs.php
Normal file
11
modules/blogs/includes/blogs.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?
|
||||
require('blogs_last_post.php');
|
||||
$opts = array(CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_TIMEOUT => 10, CURLOPT_CONNECTTIMEOUT => 10);
|
||||
$blogs = new Blogs_last_post();
|
||||
foreach($blogs->getLinks() as $link)
|
||||
{
|
||||
$blogs->addSession($link['url'], $opts);
|
||||
}
|
||||
echo $blogs->getEverything();
|
||||
$blogs->clear();
|
||||
|
177
modules/blogs/includes/blogs_last_post.php
Normal file
177
modules/blogs/includes/blogs_last_post.php
Normal file
@@ -0,0 +1,177 @@
|
||||
<?
|
||||
require('CURL.php');
|
||||
class Blogs_last_post extends CURL{
|
||||
|
||||
private $_result;
|
||||
|
||||
/*private $listeImages = array(
|
||||
0 => 0, // penelope
|
||||
1 => 12, //explosm
|
||||
2 => 0, // margaux
|
||||
);
|
||||
|
||||
private $nomImages = array(
|
||||
0 => 'PenelopeBagieu',
|
||||
1 => 'CyanideAndHapiness',
|
||||
2 => 'MargauxMotin',
|
||||
);
|
||||
|
||||
public $link = array(
|
||||
0 => 'http://www.penelope-jolicoeur.com/',
|
||||
1 => 'http://www.explosm.net/comics/',
|
||||
2 => 'http://margauxmotin.typepad.fr/',
|
||||
);*/
|
||||
private $link = array();
|
||||
|
||||
|
||||
function getResult()
|
||||
{
|
||||
return $this->_result;
|
||||
}
|
||||
|
||||
public function getLinks(){
|
||||
if($linksXML = simplexml_load_file('../db/blog_links.xml')){
|
||||
foreach($linksXML->link as $individualLink){
|
||||
$this->link[] = array('name'=>$individualLink->name, 'url'=>$individualLink->url, 'number'=>$individualLink->number);
|
||||
}
|
||||
return $this->link;
|
||||
}
|
||||
else
|
||||
return;
|
||||
}
|
||||
|
||||
function getTitles()
|
||||
{
|
||||
$xhtml = "";
|
||||
try{
|
||||
foreach($this->exec() as $result)
|
||||
{
|
||||
$xhtml .= $this->getTitle($result);
|
||||
$xhtml .= '<br/>';
|
||||
}
|
||||
}catch(Exception $e)
|
||||
{
|
||||
$xhtml .= $this->error();
|
||||
}
|
||||
return $xhtml;
|
||||
|
||||
}
|
||||
|
||||
function getTitle($result = null, $url = null)
|
||||
{
|
||||
if(isset($result))
|
||||
{
|
||||
preg_match( "/<h3 class=\"entry-header\">(.*)<\/h3>/i", $result, $match );
|
||||
|
||||
if(isset($match[1]))
|
||||
return utf8_decode(strip_tags($match[1]));
|
||||
else{
|
||||
preg_match( "/<title>(.*)<\/title>/i", $result, $title );
|
||||
if(isset($title[1]))
|
||||
return $title[1];
|
||||
else
|
||||
return 'Erreur : pas de titre de blog trouv<75>.';
|
||||
}
|
||||
|
||||
}
|
||||
//TODO en fonction de l'url et non du resultat du cURL
|
||||
}
|
||||
|
||||
function getPost()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
function createThumbnail($result, $title = 0)
|
||||
{
|
||||
if(isset($result))
|
||||
{
|
||||
|
||||
preg_match_all( "#<img[^>]+src=['|\"](.*?)['|\"][^>]*>#i", $result, $match );
|
||||
|
||||
/*$ret = file_put_contents('match.txt', var_export($match,true), FILE_APPEND);
|
||||
if ($ret === false)
|
||||
{
|
||||
echo 'erreur';
|
||||
}*/
|
||||
$number = $this->link[$title]['number'];
|
||||
$title = $this->link[$title]['name'];
|
||||
if(isset($match[1][(int)$number]))
|
||||
{
|
||||
$source = @imagecreatefromjpeg($match[1][(int)$number]);
|
||||
if($source == false)
|
||||
$source = @imagecreatefrompng($match[1][(int)$number]);
|
||||
|
||||
$wSource = @imagesx($source);
|
||||
$hSource = @imagesy($source);
|
||||
|
||||
$destination = imagecreatetruecolor(50, 50);
|
||||
@imagecopyresampled($destination, $source, 0, 0, 0, 0, 50, 50, $wSource, $hSource);
|
||||
@imagepng($destination, 'images/blogs/'.$title.'.png');
|
||||
return 'images/blogs/'.$title.'.png';
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
function getImages($notFromGetImage = false)
|
||||
{
|
||||
if($notFromGetImage){
|
||||
//TODO stuff
|
||||
}else{
|
||||
$xhtml = "";
|
||||
$i = 0;
|
||||
try{
|
||||
foreach($this->exec() as $result)
|
||||
{
|
||||
$xhtml .= '<img src="'.$this->createThumbnail($result, $i).'" />';
|
||||
$xhtml .= '<br/>';
|
||||
$i++;
|
||||
}
|
||||
}catch(Exception $e)
|
||||
{
|
||||
$xhtml .= $this->error();
|
||||
}
|
||||
return $xhtml;
|
||||
}
|
||||
}
|
||||
|
||||
function getAllImagesToChoose($result,$notFromGetImage = false)
|
||||
{
|
||||
if($notFromGetImage){
|
||||
//TODO stuff
|
||||
}else{
|
||||
preg_match_all( "#<img[^>]+src=['|\"](.*?)['|\"][^>]*>#i", $result, $match );
|
||||
$nbImages = count($match[1]);
|
||||
$xhtml = 'Liste d\'images : <br/>';
|
||||
|
||||
for($i = 0; $i<$nbImages; $i++){
|
||||
$xhtml .= '<img src="'.$match[1][$i].'" id="n-'.$i.'" class="choose"/><br/>';
|
||||
}
|
||||
return $xhtml;
|
||||
}
|
||||
}
|
||||
|
||||
function getEverything()
|
||||
{
|
||||
$temps_debut = microtime(true);
|
||||
$xhtml = "";
|
||||
$i = 0;
|
||||
try{
|
||||
foreach($this->exec() as $result)
|
||||
{
|
||||
$xhtml .= '<a href="'.$this->link[$i]['url'].'" target="_blank" class="blogLinks"><img src="'.$this->createThumbnail($result, $i).'" /> '.utf8_encode($this->getTitle($result))."</a>";
|
||||
$xhtml .= '<br/>';
|
||||
$i++;
|
||||
}
|
||||
}catch(Exception $e)
|
||||
{
|
||||
$xhtml .= $this->error();
|
||||
}
|
||||
$temps_fin = microtime(true);
|
||||
$xhtml .= 'Temps d\'execution : '.round($temps_fin - $temps_debut, 4).'s';
|
||||
return $xhtml;
|
||||
}
|
||||
|
||||
|
||||
}
|
22
modules/blogs/js/blogs.js
Normal file
22
modules/blogs/js/blogs.js
Normal file
@@ -0,0 +1,22 @@
|
||||
$(document).ready(function(){
|
||||
|
||||
$('#menu-bar').prepend($('#blog-links-manager'));
|
||||
|
||||
//affichage des blogs
|
||||
$("#blogs").html("<img src=\"images/interface/ajax_load.gif\"/> Loading Blogs...");
|
||||
var tmp;
|
||||
|
||||
/* A helper function for converting a set of elements to draggables: */
|
||||
make_draggable($('#blogs'));
|
||||
|
||||
$.ajax(
|
||||
{url : "modules/blogs/includes/blogs.php",
|
||||
timeout : 36000,
|
||||
error: function(data){
|
||||
$("#blogs").html('<img src="images/interface/error.png"/> Délai dépassé !');
|
||||
},
|
||||
success:function(data){
|
||||
$("#blogs").html(data);
|
||||
}
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user