<?php
# ************** LICENCE ****************
/*
	Copyright (c) PORTANEO.
*/
# ***************************************
# Add a RSS widget to the portal
#
# !! be careful, this file must be saved under uft8 format, and display an e accentuated here : é
# *************************************** 
$not_access=1;
$granted="I";

require_once('includes_mobile.php'); 
require_once('../../../../db_layer/'.__DBTYPE.'/portal.php');
require_once('../../../feed.inc.php');
require_once('../../../xml.inc.php');

$id_user = $_SESSION['user_id'];
$proxy = '';
$refreshDelay = 10;
$url = $_POST["url"];
$rssurl = $_POST["url"]; //use in include refresh feed
$tab_id = $_POST["selectedtab"];

if (substr($url, 0, 7) != 'http://') {
    $url = 'http://'.$url;
    $rssurl = 'http://'.$rssurl;
}

//check that url not empty
if ($url == 'http://') {
	header("location:portal_addmodule.php?msg= ".lg("urlnonvalid")."");
	exit();
}
//check if there is content
$h = new http($url);
$check = $h->get();
if (empty($check)) {
    echo '<span>'.lg("fluxnonvalid").'&nbsp;&nbsp;</span><a href="portal_addmodule.php">'.lg("back").'</a>';
	//header("location:portal_addmodule.php?msg= ".lg("fluxnonvalid")."");
	exit();
}
//check the XML / RSS format
if (   stripos($check,"<?xml") === false 
    && stripos($check,"<rss") === false) {
        header("location:portal_addmodule.php?msg= ".lg("fluxnonvalid")."");
        exit();
}

//Check if feed already exist in dir_rss
$DB->getResults($mobScrAddModule_checkIfFeedExist);
$stop = 0;
while ($row=$DB->fetch(0)) {
	$urlcheck = $row['url'];
	if (strtolower($urlcheck) == strtolower($url)) {
		$stop++;
	}
}
$DB->freeResults();
	
//Add feed in dir_rss
if ($stop==0) {
	$DB->execute($xmlcheckfeed_setUrl,$DB->quote($url));
	$pfid=$DB->getId();
}
else {
	$DB->getResults($mobScrAddModule_getPfid,$DB->quote($url));
	$row = $DB->fetch(0);
	$pfid = $row['id'];
	$DB->freeResults();
}
	
//Refresh of the new feed => get title
require_once('../../../refreshfeed.inc.php');
$title = formatString($title,11);

//Create DB module entry for the feed
$DB->getResults($mobScrAddModule_getModulesPosition,$DB->escape($_SESSION['user_id']));
$row = $DB->fetch(0);
$uniq = $row['uniqmax'];
$posy = $row['posymax'];
$newUniq = $uniq+1;
$newPosy = $posy+1;
$DB->freeResults();

$variables='pfid='.$pfid.'&rssurl='.$rssurl.'&icon=rss'.$pfid.'.ico&ptitl='.$title;
$DB->execute($scrconfigplace_addNewMod,
				86,$DB->escape($_SESSION['user_id']),$DB->escape($tab_id),1,$DB->escape($newPosy),
				0,150,150,$DB->quote($variables),$DB->escape($newUniq),
				$pfid);
	
header("location:../scripts/portal_configuration.php");
?>