toutpratique/rss.php
2016-11-03 12:33:57 +01:00

122 lines
3.8 KiB
PHP

<?php
if (php_sapi_name() != 'cli' && $_GET['token'] != 'd41d8cd98f00b204e9800998ecf8427e') {
die;
}
$_SERVER['HTTP_HOST'] = 'www.toutpratique.com';
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_SERVER['SERVER_PORT'] = 80;
require dirname(__FILE__).'/config/config.inc.php';
require dirname(__FILE__).'/init.php';
require dirname(__FILE__).'/modules/cmsps/classes/CmsPsPost.php';
require dirname(__FILE__).'/modules/cmsps/classes/CmsPsCategory.php';
generateRSS('boutique');
generateRSS('cms');
function generateRSS($name)
{
switch ($name) {
case 'boutique':
createBoutiqueRSS($name);
break;
case 'cms':
createCmsRSS($name);
break;
default:
break;
}
}
function createBoutiqueRSS($name)
{
$products = Product::getProducts(Context::getContext()->language->id, null, 0, 'id_product', 'DESC');
$link = Context::getContext()->link;
foreach ($products as &$product) {
$category = new Category((int)$product['id_category_default'], Context::getContext()->language->id);
$product['product_link'] = str_replace(' ', '%20', $link->getProductLink($product['id_product']));
$product['category_name'] = $category->name;
$product['description_short'] = strip_tags($product['description_short']);
$date = date('D, d M Y H:i:s', strtotime($product['date_add']));
$product['publish_date'] = $date . ' GMT';
}
createRSS($name, $products, array('id_product' => 'guid', 'product_link' => 'link', 'name' => 'title', 'description_short' => 'description', 'category_name' => 'category', 'publish_date' => 'pubDate'));
}
function createCmsRSS($name)
{
$collection_post = new Collection('CmsPsPost', Context::getContext()->language->id);
$collection_post->where('active', '=', 1);
$collection_post->orderBy('date_add', 'DESC');
$posts = $collection_post->getResults();
$posts = CmsPsPost::injectsData($posts);
$link = Context::getContext()->link;
$dataPosts = array();
foreach ($posts as $post) {
$category = new CmsPsCategory((int)$post->id_category, Context::getContext()->language->id);
$dataPost = array();
$dataPost['title'] = $post->title;
$dataPost['post_link'] = str_replace(' ', '%20', $link->getPostCmsLink($post->id_post));
$dataPost['category_name'] = $category->title;
$dataPost['intro'] = strip_tags(str_replace("\n", '', $post->intro));
$dataPost['id'] = $post->id_post;
$date = date('D, d M Y H:i:s', strtotime($post->date_add));
$dataPost['publish_date'] = $date . ' GMT';
$dataPosts[] = $dataPost;
}
createRSS($name, $dataPosts, array('id' => 'guid', 'title' => 'title', 'post_link' => 'link', 'category_name' => 'category', 'intro' => 'description', 'publish_date' => 'pubDate'));
}
function createRSS($name, $datas, $keys)
{
$xml = '<?xml version="1.0" encoding="utf-8"?>';
$xml .= '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">';
$xml .= '<channel>';
$xml .= '<title>'.$name.'</title>';
$xml .= '<link>'._PS_BASE_URL_.'/rss/'.$name.'.xml</link>';
$xml .= '<description>'.$name.' RSS</description>';
foreach ($datas as $data) {
$xml .= '<item>';
foreach ($keys as $key => $label) {
if (!empty($data[$key])) {
if ($label == 'guid') {
$xml .= '<'.$label.' isPermaLink="false">';
} else {
$xml .= '<'.$label.'>';
}
$xml .= '<![CDATA[' . $data[$key] . ']]>';
$xml .= '</'.$label.'>';
}
}
$xml .= '</item>';
}
$xml .= '</channel>';
$xml .= '</rss>';
file_put_contents(__DIR__ . '/rss/' . $name . '.xml', $xml);
}