<?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/>.
*/
# ***************************************
# Favorites module PHP scripts
#
# !! be careful, this file must be saved under uft8 format, and display an e accentuated here : é
# ***************************************

header("content-type: application/xml"); 
$folder="";
$not_access=0;
$pagename="modules/wid_links.php";
$granted="I,N";
//includes
require_once('includes.php');
require_once('../includes/misc.inc.php');
require_once('../includes/xml.inc.php');
$shareditemkey = isset($_REQUEST["shareditemkey"])?$_REQUEST["shareditemkey"]:"";
$acceptwriteshare = isset($_REQUEST["acceptwriteshare"])?$_REQUEST["acceptwriteshare"]:"";
$sharedMD5 = isset($_REQUEST["shareditemkey"])?$_REQUEST["shareditemkey"]:"";
$linklistid = isset($_REQUEST["modid"]) ? $_REQUEST["modid"] : 0;
$sharedmd5key=isset($_REQUEST['sharedmd5key'])?$_REQUEST['sharedmd5key']:'';
$act=$_REQUEST["act"];

$file=new xmlFile();

$file->header("channel");


if (isset($_REQUEST['getText']) && isset($_REQUEST['linkid']) && isset($_SESSION['user_id'])) {

    $linkid = $_REQUEST["linkid"];
    
    $widgetid = isset($_REQUEST['widgetid'])?$_REQUEST['widgetid']:0;
    $favid = isset($_REQUEST['favid'])?$_REQUEST['favid']:0;
    if ($linkid!=0) {
        if( (empty($sharedmd5key))
        || (!isset($sharedmd5key)) 
        || ($sharedmd5key=='undefined') ) {
        $DB->getResults($wid_getUserLinks,
                            $DB->escape($favid),
                            $DB->escape($_SESSION['user_id'])
                       );
        }
        else {
            $DB->getResults($wid_getUserLinksShared,
                                $DB->quote("%linkid=".$DB->escape($linkid)."%"),
                                $DB->quote($sharedmd5key),
                                $DB->escape($_SESSION['user_id']),
                                $DB->escape($widgetid),
                                $DB->escape($favid)
                            );
        }
        $inc=0;
        while ($row = $DB->fetch(0)){
            $db_link_id = $row['link_id'];
            $db_name = $row['name'];
            $db_url = $row['url'];
            $db_tags = $row['tags'];
            echo "<link>";
            echo "<linkid>".$db_link_id."</linkid>";
            echo "<name><![CDATA[".$db_name."]]></name>";
            echo "<url><![CDATA[".$db_url."]]></url>";
            echo "<tags><![CDATA[".$db_tags."]]></tags>";
            echo "</link>";
            $inc++;
        }
        $DB->freeResults();
    }    
} elseif ( 
        isset($_REQUEST["shareditemkey"])  
        && $act != 'sup'
        && $act != 'add'
        && (isset($_REQUEST['linkid']) && $_REQUEST['linkid'] > 0 ) ) {

    $linkid = $_REQUEST["linkid"];
	$shareditemkey = $_REQUEST["shareditemkey"];
	if ($linkid!=0) {
	        $DB->getResults($wid_getUserLinksSharedKey,
                                $DB->quote($shareditemkey),
                                $DB->escape($linkid)
                            );
			$inc=0;
			if ($DB->nbResults()!=0){
		        while ($row = $DB->fetch(0)){
		            $db_link_id = $row['link_id'];
		            $db_name = $row['name'];
		            $db_url = $row['url'];
		            $db_tags = $row['tags'];
		            echo "<link>";
		            echo "<linkid>".$db_link_id."</linkid>";
		            echo "<name><![CDATA[".$db_name."]]></name>";
		            echo "<url><![CDATA[".$db_url."]]></url>";
		            echo "<tags><![CDATA[".$db_tags."]]></tags>";
		            echo "</link>";
		            $inc++;
		        }    
			}
			$DB->freeResults();
	 }
} elseif( isset($_SESSION['user_id']) ) {
    
    $id=$_REQUEST["modid"];
    $linkid = 0;
    if ($act=="sup")
    {
    	$DB->execute($widlink_removeLink,
                    $DB->escape($_REQUEST["linkid"]),
                    $DB->escape($_SESSION['user_id']));
    	if ($DB->nbAffected()==0)
    	{
    		$DB->execute($widlink_createNewId,
                                $DB->escape($_SESSION['user_id']));
    		$oldid=$id;
    		$id=$DB->getId();
    		$DB->execute($widlink_copyLinks,$id,
                                $DB->escape($oldid),
                                $DB->escape($_REQUEST["linkid"]));
            $linkid = $DB->getId();
            
            
    		
            
                       
    	}
        echo '<ret>'.$id.'_'.$linkid.'</ret>';
        echo '<sharedkey>'.$sharedMD5.'</sharedkey>';
        echo '<linklistid>'.$id.'</linklistid>';
        echo '<linkid>'.$linkid.'</linkid>'; 
    }
    if ($act=="add")
    {
		$tags = "";
		$tagsTemp = isset($_REQUEST["tags"])?$_REQUEST["tags"]:"";
		$tagsTemp = explode(",",$tagsTemp);
		foreach($tagsTemp as $value) {
			$tags .= trim(suppress_accent($value)).",";
		}
		$tags = substr($tags,0,-1);
    	$linkid=$_REQUEST["linkid"];
        
    	if ($id==0)
    	{
			$sharedMD5 = md5(uniqid(rand(), true));
    		$DB->execute($widlink_addNewLinkId2,
                            $DB->escape($_SESSION['user_id']),
                            $DB->quote($sharedMD5));
    		$id=$DB->getId();
    		$DB->execute($widlink_addNewLink,
                            $DB->escape($id),
                            $DB->noHTML($_REQUEST["name"]),
                            $DB->noHTML($_REQUEST["link"]),
                            $DB->noHTML($tags));
    	}
    	else
    	{
            $sharedMD5 = isset($_POST["shareditemkey"])?$_POST["shareditemkey"]:"";
    		//edition or adding
    		if ($linkid=="")
    		{
                if ($acceptwriteshare && $acceptwriteshare == 'yes') {
                    $DB->execute($widlink_addNewLinkOnExistingModSharedKey,
                                    $DB->escape($id),
                                    $DB->noHTML($_REQUEST["name"]),
                                    $DB->noHTML($_REQUEST["link"]),
                                    $DB->noHTML($tags),
                                    $DB->escape($id),
                                    $DB->quote($shareditemkey)
                                );                
                
                } else {
                    $DB->execute($widlink_addNewLinkOnExistingMod,
                                    $DB->escape($id),
                                    $DB->noHTML($_REQUEST["name"]),
                                    $DB->noHTML($_REQUEST["link"]),
                                    $DB->noHTML($tags),
                                    $DB->escape($id),
                                    $DB->escape($_SESSION['user_id']));
                }
			}
    		else
    		{
                $userid_owner = $_SESSION['user_id'];   
                if ($acceptwriteshare && $acceptwriteshare == 'yes') {
                    
                    $DB->execute($widlink_updateLinkSharedItemKey,
                                        $DB->noHTML($_REQUEST["name"]),
                                        $DB->noHTML($_REQUEST["link"]),
                                        $DB->noHTML($tags),
                                        $DB->escape($linkid),
                                        $DB->quote($shareditemkey)
                            );

                } else {
                    $DB->execute($widlink_updateLink,
                                        $DB->noHTML($_REQUEST["name"]),
                                        $DB->noHTML($_REQUEST["link"]),
                                        $DB->noHTML($tags),
                                        $DB->escape($linkid),
                                        $DB->escape($_SESSION['user_id']));
                }
			}
    		if ($DB->nbAffected()==0)
    		{
				$sharedMD5 = md5(uniqid(rand(), true));
    			$DB->execute($widlink_createNewModule,
                                        $DB->escape($_SESSION['user_id']),
                                        $DB->quote($sharedMD5));
    			$oldid=$id;
    			$id=$DB->getId();
    			$DB->execute($widlink_insertExistingLink,
                                        $DB->escape($id),
                                        $DB->escape($oldid));

    			if ($linkid=="")
    			{
    				$DB->execute($widlink_newModInsertLink,
                                        $DB->escape($id),
                                        $DB->noHTML($_REQUEST["name"]),
                                        $DB->noHTML($_REQUEST["link"]),
                                        $DB->noHTML($tags));
    			}
    			else
    			{
    				$DB->execute($widlink_newModUpdateLink,
                                        $DB->noHTML($_REQUEST["name"]),
                                        $DB->noHTML($_REQUEST["link"]),
                                        $DB->noHTML($tags),
                                        $DB->escape($linkid));
    			}
    		}
    	}
        $linkid = $DB->getId();
        echo '<sharedkey>'.$sharedMD5.'</sharedkey>';
    	echo '<ret>'.$id.'_'.$linkid.'</ret>';
        echo '<linklistid>'.$id.'</linklistid>';
        echo '<linkid>'.$linkid.'</linkid>';
    }
	if ($act == 'init')
	{
		$sharedMD5 = md5(uniqid(rand(), true));
		
		//create new entry
		$DB->execute($widlink_addNewLinkId2,
                            $DB->escape($_SESSION['user_id']),
                            $DB->quote($sharedMD5));

		echo '<sharedkey>' . $sharedMD5 . '</sharedkey>';
		echo '<linklistid>' . $DB->getId() . '</linklistid>';
	}
}

$DB->close();

$file->status(1);

$file->footer();
?>