<?php


# ******************************************* LICENCE******************************************* 
/*
	Copyright (c) PORTANEO.

	This file is part of POSH (Portaneo Open Source Homepage) http://sourceforge.net/projects/posh/.

	POSH is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version

	POSH is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Posh.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 POSH add widget through url
 widget are  saved with validation
    
    default return xml
            echo "<error>ok</error>\n";
        echo "<msg>ok</msg>\n";
    
    parameter: 

 !! be careful, this file must be saved under uft8 format, and display an e accentuated here : é
**/

$folder="";
$not_access=1;
$isScript=true;
$isPortal=false;
$granted="A";
$pagename="api/addwidget.php";
//includes
require_once("includes_api.php");
require_once("../includes/admin_tools.php");
require_once("authentication.php");

//require_once("../tutorial/addwidget.inc.php");
require_once('../includes/file.inc.php');
require_once('../includes/xml.inc.php');
require_once('../includes/http.inc.php');
require_once('../includes/log.inc.php');
require_once('../includes/misc.inc.php');

require_once('../includes/xmldom_tool.php');
include('../includes/fixmagic.php'); 
include_once('../db_layer/'.__DBTYPE.'/tutorial.php');
require_once("../includes/modules_tools.php");
require_once("../includes/tutorial-dom.inc.php");

if (isset($_GET['url'])) {
    $xmlfile = $_GET['url'];
} else {
    print "File missing";
    exit;
}
//get datas from xml
$poshdom = new PoshTutorialDom();
$ret = $poshdom->getFile($xmlfile);

$datas = array();
$datas['DB'] = $DB;

$poshdom->getModulesPrefs($datas);
$poshdom->getUserPrefs($datas);
$poshdom->getContent($datas);

$url = getWidgetUrl($datas,$datas['widget']['contents']['home']['type'],
                                $datas['widget']['contents']['home']['href'] );
                                
// Check if url exists in DB
$chkId = checkModuleInDB( $url,$DB );
if( $chkId == 0 ) {
	$datas['widget']['validated_id'] = 0;
	$datas['widget']['xmlcontent'] = $poshdom->saveXML();
	$datas['widget']['quotes'] = 'NO';
	$datas['widget']['lang'] = __LANG;
	$datas['widget']['validated'] = 1;
	//wtmPKz6t&format=JSON&url=http%3A%2F%2Flocalhost%2Ftypo3_8%2Fuploads%2Ftx_widgetportaneo%2Fpage3.xm
	addnewModule($datas);
	setBaseName($datas);
	saveContents($datas);
	//$datas['widget']['category_id'] = 2;
	addWidgetInDirCat($datas);
} else {
	updateModuleInDB( $chkId,$DB);
}
if (isset($_GET['format'])) {
    $return_format = $_GET['format'];
    if ($return_format == "json") {
       header ('Content-Type: text/javascript; charset=utf-8');
       echo "var poshwidget = {'msg':'ok','error':'ok'};";
    
    }
} else {
    $file=new xmlFile();
    $file->header("widget");
    echo "<error>ok</error>\n";
    echo "<msg>ok</msg>\n";
    $file->footer();
    $DB->close();
}



?>