?
<?php
Class My_Outils_Feeds
{
static function getdbAdapter() {
$config = new Zend_Config_Ini('./application/config/config.ini', ENV);
// Zend Objet Base de données
$dbAdapter = new Zend_Db_Adapter_Pdo_Mysql(array(
'host' => $config->db->params->host,
'username' => $config->db->params->username,
'password' => $config->db->params->password,
'dbname' => $config->db->params->dbname
));
return $dbAdapter;
}
static function listechapitre() {
$dbAdapter = self::getdbAdapter();
$dbAdapter->query('SET CHARACTER SET utf8');
//$listTables[$t] = "archivesrss2012";
$sql= "SELECT id,nom,description,link,flux,ordre,type,category,archive,in_image FROM chapitrerss ";
$sql.= "WHERE id >= 0 ";
$sql .= "ORDER BY category ASC, ordre ASC";
$commande = $dbAdapter->fetchAll($sql);
return $commande;
}
static function listetablearchive() {
$dbAdapter = self::getdbAdapter();
$sql = "show tables from marmits like 'archivesrss%' ";
$commande = $dbAdapter->fetchAll($sql);
$i = 0;
foreach ($commande as $a){
$listtables[$i] = $a['Tables_in_marmits (archivesrss%)'];
$i++;
}
return $listtables;
}
static function listeanneearchive() {
$listTables = self::listetablearchive();
$listTables = array_reverse($listTables);
$t = 0;
while($t < count($listTables))
{
$listTables[$t] = substr($listTables[$t],-4);
$t++;
}
return $listTables;
}
static function verifanneearchive($annee) {
$periode = "";
$listTables = self::listetablearchive();
$listTables = array_reverse($listTables);
$t = 0;
while($t < count($listTables))
{
$listTables[$t] = substr($listTables[$t],-4);
$t++;
}
$verif = "ko";
if (in_array ($annee, $listTables)) {
$verif = "ok";
}
// exit("test ".$verif." ".$annee);
// exit("res ".$commande[0]['periode']);
return $verif;
}
static function menu_last_periode($idchapitre) {
$periode = "";
$listTables = self::listetablearchive();
$listTables = array_reverse($listTables);
$t = 0;
while($t < count($listTables))
{
//$listTables[$t] = "archivesrss2012";
$sql= "SELECT periode FROM ".$listTables[$t]." ";
$sql.= "WHERE (id_chapitrerss = ".$idchapitre.") ";
$sql .= "ORDER BY periode desc LIMIT 1";
$dbAdapter = self::getdbAdapter();
$commande = $dbAdapter->fetchAll($sql);
if(count($commande) > 0)
{
$t = count($listTables);
$periode = $commande[0]['periode'];
}
$t++;
}
// exit("res ".$commande[0]['periode']);
return $periode;
}
static function menu_last_periode_par_annee($idchapitre) {
$periode = array();
$listTables = self::listetablearchive();
$listTables = array_reverse($listTables);
$t = 0;
$s = 0;
while($t < count($listTables))
{
//$listTables[$t] = "archivesrss2012";
$sql= "SELECT periode FROM ".$listTables[$t]." ";
$sql.= "WHERE (id_chapitrerss = ".$idchapitre.") ";
$sql .= "ORDER BY periode desc LIMIT 1";
$dbAdapter = self::getdbAdapter();
$commande = $dbAdapter->fetchAll($sql);
if(count($commande) > 0)
{
$periode[$s]['periode'] = $commande[0]['periode'];
$periode[$s]['annee'] = substr($commande[0]['periode'], 0, 4);
$s++;
}
//exit($periode[$t]['annee']);
$t++;
}
// exit("res ".$commande[0]['periode']);
return $periode;
}
static function periodebyidarchiverssSelect($idchapitre, $periode) {
$annee = substr($periode, 0, 4);
$sql= "SELECT periode FROM archivesrss".$annee." ";
$sql.= "WHERE (id_chapitrerss = ".$idchapitre.") ";
$sql .= "GROUP BY periode ORDER BY periode desc";
$dbAdapter = self::getdbAdapter();
$periode = array();
$commande = $dbAdapter->fetchAll($sql);
$i = 0;
$test = $sql;
// exit("res ".$commande[0]['periode']);
return $commande;
}
static function rssarchiveByDateSelect($date) {
$time = strtotime($date, 0);
$annee = date('Y', $time);
$sql= "SELECT id FROM archivesrss".$annee." ";
$sql.= "WHERE (date like '".$date."%') ";
$sql .= "ORDER BY date desc";
$dbAdapter = self::getdbAdapter();
$periode = array();
$commande = $dbAdapter->fetchAll($sql);
$i = 0;
return $commande;
}
static function recupfeeds($id) {
$erreurcanal = "";
$flux = "";
$chapitresrss = new tablechapitrerss();
$rssSelect = $chapitresrss->chapitrerssSelect($id);
$tabitems = array();
if(count($rssSelect) == 0)
{
$erreurflux = "1";
}
else
{
$type = $rssSelect[0]->type;
$fluxarchive = $rssSelect[0]->flux;
//$fluxarchive = "http://www.delicious.com/v2/rss/developpeur.webs";
$target="_self";
//flux live
// verifie si le lien rss est en ligne
if(My_Outils_Url::urlok($fluxarchive))
{
try
{
if($type == "rss")
{
$canal = new Zend_Feed_Rss($fluxarchive);
$description = $canal->description;
$titreflux = $canal->title." ";
$flux = $canal->item;
$description = $flux->description();
if($flux->{'link'}[0])
{
$link = $flux->{'link'}[0];
}
else
{
$link = $flux->link();
}
}
else if($type == "atom")
{
$canal = new Zend_Feed_Atom($fluxarchive);
$description = $canal->subtitle;
$titreflux = $canal->title."";
$flux = $canal;
$description = $flux->subtitle();
$link = "#";
$target="_self";
}
else
{
//exit("test ".print_r($fluxarchive));
}
}
catch (Zend_Feed_Exception $e)
{
// l'importation du flux a échoué
$erreurcanal = 'Erreur canal '.$rssSelect[0]->nom.' OUT - '. $e->getMessage().'';
$canal = "";
}
$target = $target;
$description = $rssSelect[0]->description;
// boucle sur les liens
$i = 0;
if(!empty($flux))
{
$datebdd = "";
foreach($flux as $elem)
{
if($type == "rss")
{
if($elem->{'link'}[0])
{
$link = $elem->{'link'}[0];
}
else
{
$link = $elem->link();
}
//date
if($elem->pubDate())
{
$date = $elem->pubDate(); // format Zend_Date::RFC_2822
}
else if($elem->{'dc:date'})
{
$date = $elem->{'dc:date'};
}
else
{
$date = "";
}
//erreur
$erreur = "";
if(preg_match_all('/if lt/', $elem->{'content:encoded'},$out))
//if($elem->{'content:encoded'})
{
$erreur = "erreur encoded";
}
else if(preg_match_all('#\[code\]#', $elem->{'content:encoded'},$out))
{
$erreur = "erreur code html";
}
else
{
$erreur = "";
}
//author
$author = "";
if($elem->author->name())
{
$author = $elem->author->name();
}
else if($elem->author())
{
$author = $elem->author();
}
else if($elem->{'dc:creator'})
{
$author = $elem->{'dc:creator'};
}
}// fin rss
//atom
else if($type == "atom")
{
$link = $elem->link['href'];
$date = $elem->updated(); // format Zend_Date::RFC_2822
$author = "";
if($elem->author->name())
{
$author = $elem->author->name();
}
}
// format date
if($date != "")
{
$date = str_replace("UT", "", $date);
$time = strtotime($date, 0);
$time2 = strtotime($date, 0);
$date = "Le: ".date('d m Y à H:i', $time);
$datecrit = date('Y-m-d', $time);
$date2 = date('Y-m-d', $time2);
}
else
{
if($i == 0)
{
$time = strtotime("now");
}
else
{
$time = $time + 1;
}
$time = strtotime("now");
//$time = strtotime("-12 hours");
}
$datebdd = date('Y-m-d H:i:s', $time);
$periode = date('Y-m', $time);
//$tabitems[$i]['test'] = $link;
$tabitems[$i]['author'] = "";
$tabitems[$i]['href'] = $link;
$tabitems[$i]['titre'] = $elem->title();
$tabitems[$i]['date'] = $date;
$tabitems[$i]['datecrit'] = $datecrit;
if(!isset($date2))
{
$date2 = date('Y-m-d');
}
$tabitems[$i]['date2'] = $date2;
$content = "";
if($type == "rss")
{
if($erreur == "")
{
$content = "".$elem->description();
}
}
else if($type == "atom")
{
$content = $elem->content();
}
$tabitems[$i]['content'] = "".$content;
$tabitems[$i]['author'] = "";
if($author != "")
{
$tabitems[$i]['author'] = $author;
}
$i++;
} // fin for each
}
}// fin si flux en ligne
else
{
//$tabitems = "erreur le flux n'est pas en ligne";
return false;
}
}
return $tabitems;
}
static function searchFeeds($critere) {
// $critere = addslashes($critere);
// $critere = htmlspecialchars($critere, ENT_QUOTES, "UTF-8");
$resultat = array();
$listTables = self::listetablearchive();
$listTables = array_reverse($listTables);
$t = 0;
$s = 0;
while($t < count($listTables))
{
$i = 0;
$sql= "SELECT id, id_chapitrerss, url_feeds, titre, date, periode, contenu, author FROM ".$listTables[$t]." ";
$sql.= "WHERE MATCH (titre) ";
$sql .= "AGAINST ('\"".$critere."\"' IN BOOLEAN MODE) order by date desc;";
//$sql .= "AGAINST ('".$critere."' IN BOOLEAN MODE) order by date desc;";
$dbAdapter = self::getdbAdapter();
$dbAdapter->query('SET CHARACTER SET utf8');
$commande = $dbAdapter->fetchAll($sql);
if(count($commande) > 0)
{
while($i < count($commande))
{
$resultat[$s][$i]['id'] = $commande[$i]['id'];
$resultat[$s][$i]['id_chapitrerss'] = $commande[$i]['id_chapitrerss'];
$resultat[$s][$i]['url_feeds'] = $commande[$i]['url_feeds'];
$resultat[$s][$i]['titre'] = $commande[$i]['titre'];
$resultat[$s][$i]['date'] = substr($commande[$i]['date'], 0 ,10);
$resultat[$s][$i]['periode'] = $commande[$i]['periode'];
$resultat[$s][$i]['contenu'] = $commande[$i]['contenu'];
$resultat[$s][$i]['author'] = $commande[$i]['author'];
$i++;
}
$s = $s + 1;
}
$t++;
}
return $resultat;
}
static function searchFeedsMulti($critere, $type) {
$critere = addslashes($critere);
// $critere = htmlspecialchars($critere, ENT_QUOTES, "UTF-8");
if($type == "contenu")
{
$critere = htmlspecialchars($critere, ENT_QUOTES, "UTF-8");
$crit = "SELECT id, id_chapitrerss, url_feeds, titre, date, periode, contenu, author ";
$where = "WHERE MATCH (titre, contenu) ";
$typenormal = "AGAINST ('\"".$critere."\"' IN BOOLEAN MODE) order by date desc;";
}
else if($type == "pertinence")
{
$critere = htmlspecialchars($critere, ENT_QUOTES, "UTF-8");
$crit = "SELECT id, id_chapitrerss, url_feeds, titre, date, periode, contenu, author, MATCH (titre,contenu) AGAINST ('\"".$critere."\"') AS score ";
$where = "WHERE MATCH (titre,contenu) ";
$typenormal = "AGAINST ('\"".$critere."\"');";
}
else
{
$crit = "SELECT id, id_chapitrerss, url_feeds, titre, date, periode, contenu, author ";
$where = "WHERE MATCH (titre) ";
$typenormal = "AGAINST ('\"".$critere."\"' IN BOOLEAN MODE) order by date desc;";
}
$resultat = array();
$listTables = self::listetablearchive();
$listTables = array_reverse($listTables);
$t = 0;
$s = 0;
while($t < count($listTables))
{
$i = 0;
$sql= $crit."FROM ".$listTables[$t]." ";
$sql.= $where;
$sql .= $typenormal;
//exit($sql);
//$sql .= "AGAINST ('".$critere."' IN BOOLEAN MODE) order by date desc;";
$dbAdapter = self::getdbAdapter();
$dbAdapter->query('SET CHARACTER SET utf8');
$commande = $dbAdapter->fetchAll($sql);
if(count($commande) > 0)
{
while($i < count($commande))
{
$resultat[$s][$i]['id'] = $commande[$i]['id'];
$resultat[$s][$i]['id_chapitrerss'] = $commande[$i]['id_chapitrerss'];
$resultat[$s][$i]['url_feeds'] = $commande[$i]['url_feeds'];
$resultat[$s][$i]['titre'] = $commande[$i]['titre'];
$resultat[$s][$i]['date'] = substr($commande[$i]['date'], 0 ,10);
$resultat[$s][$i]['periode'] = $commande[$i]['periode'];
$resultat[$s][$i]['contenu'] = $commande[$i]['contenu'];
$resultat[$s][$i]['author'] = $commande[$i]['author'];
$i++;
}
$s = $s + 1;
}
$t++;
}
return $resultat;
}
static function listChapitreByCat(){
$resultat = array();
$sql= "SELECT id, nom, description, link, flux, category, archive FROM chapitrerss where id > 0 order by category asc, ordre asc";
$dbAdapter = self::getdbAdapter();
$dbAdapter->query('SET CHARACTER SET utf8');
$commande = $dbAdapter->fetchAll($sql);
$i = 0;
while($i < count($commande))
{
$resultat[$i]['id'] = $commande[$i]['id'];
$resultat[$i]['nom'] = $commande[$i]['nom'];
$resultat[$i]['description'] = $commande[$i]['description'];
$resultat[$i]['link'] = $commande[$i]['link'];
$resultat[$i]['flux'] = $commande[$i]['flux'];
$resultat[$i]['category'] = $commande[$i]['category'];
$resultat[$i]['archive'] = $commande[$i]['archive'];
$i++;
}
return $resultat;
}
static function delRss($id, $annee){
$resultat = array();
$sql= "delete from archivesrss".$annee." where id = '".$id."'";
//exit($sql);
$dbAdapter = self::getdbAdapter();
$dbAdapter->query('SET CHARACTER SET utf8');
$dbAdapter->delete('archivesrss'.$annee, 'id = '.$id);
return true;
}
static function ismobile(){
//User agent
$ua = $_SERVER['HTTP_USER_AGENT'];
//test si il s'agit d'un mobile
return preg_match('/iphone/i',$ua) || preg_match('/android/i',$ua)
||preg_match('/blackberry/i',$ua) ;
}
static function udpatearchiverss($newtable){
ini_set('memory_limit', -1); // mémoire "illimitée"
$sortie = "";
$erreur = "";
$log_erreur = "";
$erreur_recup = "";
$createLog = "";
$erreurcanal = "";
$chapitresrss = new tablechapitrerss();
$allChapitre = $chapitresrss->listeChapitrerss();
//exit("allChapitre: ".count($allChapitre));
//exit("allChapitre: ".print_r($allChapitre));
$z = 0;
foreach ( $allChapitre as $row ):
$createLog = "";
$id = $row->id;
$ar = 1;
$in_image = $row->in_image;
$feeds = array();
$feeds = self::recupfeeds($id);
if($feeds) // si pas d'erreur de récupération
{
$erreur_recup = "";
$i = 0;
foreach($feeds as $elem):
//se placer dans la bonne table suivant l'année du flux à enregistré
if(isset($elem['date2']))
{
$date2 = $elem['date2'];
}
else
{
$date2 = date('Y-m-d', strtotime("now", 0));
}
$time2 = strtotime($date2, 0);
$periodeFeeds = date('Y', $time2);
$session = Zend_Registry::get('session');
$session->periode = $periodeFeeds;
//ARCHIVAGES
//verifie si le flux existe deja dans les archives
$archivesrss = new tablearchivesrss();
$archivesrssVerif = $archivesrss->verifArchive($id,stripslashes($elem['titre']),$elem['datecrit'],$elem['author']);
$chapitreVerif = $chapitresrss->verifChapitre($id);
$nbNoArchive = count($chapitreVerif);
$erreurid = 0;
if(($nbNoArchive == 1) )
{
$erreurid = 1;
}
if($erreurid == 0)
{
//traitement pour archivage
if($archivesrssVerif == 0)
{
$chainedate = str_replace("à", "", $elem['date']);
$chainedate = str_replace("Le:", "", $chainedate);
$chainedate = str_replace(":", " ", $chainedate);
$chainedate = str_replace(" ", " ", $chainedate);
$chainedate = $chainedate." 00";
$chainedate = trim($chainedate);
//exit($test);
$chainedate = preg_split('/ /', $chainedate, -1);
$jour = $chainedate[0];
$annee = $chainedate[2];
$chainedate[0] = $annee;
$chainedate[2] = $jour;
$i = 0;
$ladate = "";
while($i < count($chainedate))
{
if($i == 0)
{
$ladate .= $chainedate[$i];
}
else if(($i == 1)||($i == 2))
{
$ladate .= "-".$chainedate[$i];
}
else if($i == 3)
{
$ladate .= " ".$chainedate[$i];
}
else if(($i == 4)||($i == 5))
{
$ladate .= ":".$chainedate[$i];
}
//$ladate .= " ".$test[$i];
$i++;
}
$content = $elem['content'];
if($row = $archivesrss->createRow())
{
$row->id_chapitrerss = $id;
$row->url_feeds = $elem['href'];
//$row->titre = htmlentities($elem['titre'], ENT_QUOTES, "UTF-8");
//$row->titre = stripslashes($elem['titre']);
$row->titre = $elem['titre'];
$row->date = $ladate;
$periode = substr($ladate, 0, 7);
$row->periode = $periode;
if($in_image == 0)
{
//pour enlever les images si problèmes
$content = preg_replace('/<img[^>]*>/i', '', $content);
}
$content = htmlentities($content, ENT_QUOTES, "UTF-8");
$row->contenu = $content;
$row->author = $elem['author'];
$row->save();
$createLog = $ar;
$ar++;
}
else
{
$createLog .= "ko";
}
} // si pas déja archivé
}// si pas d'erreur
$i++;
endforeach;
}
else
{
$erreur_recup .= "erreur id:".$id." lien pas de réponses.";
}// fin recuperation
$log_erreur .= "ligne id:".$id." -> ".$erreur_recup.". create row: ".$createLog.".<br />";
$z++;
endforeach;
return $sortie .= $newtable." rssupdate(): ok.<br />".$log_erreur;
}
static function show_memory($real = false) {
printf("%.2f Ko\n", memory_get_usage($real) / 1024);
}
}
?>