Version avec modules séparés !
This commit is contained in:
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user