<?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_addressbook.php";
$granted="I,N";
//includes
require_once('includes.php');
require_once('../includes/xml.inc.php');

$file=new xmlFile();

$file->header("channel");

$act = isset($_POST["act"])?$_POST["act"]:"";
$id = isset($_POST["modid"])?$_POST["modid"]:0;
$addid = isset($_POST["addid"])?$_POST["addid"]:0;
$shareditemkey = isset($_POST["shareditemkey"])?$_POST["shareditemkey"]:"";
$acceptwriteshare = isset($_POST["acceptwriteshare"])?$_POST["acceptwriteshare"]:"";

if ($act=="sup")
{

	$userid_owner = $_SESSION['user_id'];
	if ($acceptwriteshare && $acceptwriteshare == 'yes') {
	        $DB->getResults($widCalendar_getinfo, $DB->escape($id) );
	        $oldjsonacl = '';
	        if ($DB->nbResults()>0) {
				while ($row = $DB->fetch(0)) {
	                $userid_owner = $row['user_id'];
	            }
	            $DB->freeResults();
	        }
	}
	
	$DB->execute($widAddr_RemoveContact,$DB->escape($_POST["addid"]),$DB->escape($userid_owner));
	if ($DB->nbAffected()==0)
	{
		$sharedMD5 = md5(uniqid(rand(), true));
		$DB->execute($widAddr_createNewId2,$DB->escape($_SESSION['user_id']),$DB->quote($sharedMD5));
		$oldid=$id;
		$id=$DB->getId();
		$DB->execute($widAddr_copyContact,$id,$DB->escape($oldid),$DB->escape($_POST["addid"]));
		echo '<ret>'.$id.'_'.($DB->getId()).'</ret>';
	}
	echo '<sharedkey>'.$shareditemkey.'</sharedkey>';
    echo '<addid>'.$id.'</addid>';
}
if ($act=="add")
{
	if ($id==0 && isset($_SESSION['user_id']) )
	{
		$sharedMD5 = md5(uniqid(rand(), true));
		$DB->execute($widAddr_addNewContactId2,$DB->escape($_SESSION['user_id']),$DB->quote($sharedMD5));
		$id=$DB->getId();
		$DB->execute($widAddr_addNewContact,$DB->escape($id),$DB->noHTML($_POST["fn"]),$DB->noHTML($_POST["ln"]),$DB->noHTML($_POST["m"]),$DB->noHTML($_POST["c"]),$DB->noHTML($_POST["f"]),$DB->noHTML($_POST["p1"]),$DB->noHTML($_POST["p2"]),$DB->noHTML($_POST["o"]),$DB->noHTML($_POST["tags"]));
	}
	elseif( isset($_SESSION['user_id'])  )
	{
	
	    $userid_owner = $_SESSION['user_id'];
        if ($acceptwriteshare && $acceptwriteshare == 'yes') {
            $DB->getResults($widAddresBook_getinfo, $DB->escape($id) );
            $oldjsonacl = '';
            if ($DB->nbResults()>0) {
				$sharedMD5 = $shareditemkey;
                while ($row = $DB->fetch(0)) {
                    $userid_owner = $row['user_id'];
                }
                $DB->freeResults();
            }
            if ($userid_owner != $_SESSION['user_id']) {
                echo "<createdby>another</createdby>";
            } else {
                echo "<createdby>myself</createdby>";
            }
        }
		
		//edition or adding
		if ($addid=="")
		{
			$DB->execute($widAddr_addNewContactOnExistingMod,$DB->escape($id),$DB->noHTML($_POST["fn"]),$DB->noHTML($_POST["ln"]),$DB->noHTML($_POST["m"]),$DB->noHTML($_POST["c"]),$DB->noHTML($_POST["f"]),$DB->noHTML($_POST["p1"]),$DB->noHTML($_POST["p2"]),$DB->noHTML($_POST["o"]),$DB->noHTML($_POST["tags"]),$DB->escape($id),$DB->escape($userid_owner));
		}
		else
		{
			$DB->execute($widAddr_updateContact,$DB->noHTML($_POST["fn"]),$DB->noHTML($_POST["ln"]),$DB->noHTML($_POST["m"]),$DB->noHTML($_POST["c"]),$DB->noHTML($_POST["f"]),$DB->noHTML($_POST["p1"]),$DB->noHTML($_POST["p2"]),$DB->noHTML($_POST["o"]),$DB->noHTML($_POST["tags"]),$DB->escape($addid),$DB->escape($userid_owner));
		}
		if ($DB->nbAffected()==0)
		{
			$DB->execute($widAddr_createNewMod,$DB->escape($_SESSION['user_id']));
			$oldid=$id;
			$id=$DB->getId();
			$DB->execute($widAddr_insertExistingContact,$DB->escape($id),$DB->escape($oldid));
			if ($addid=="")
			{
				$DB->execute($widAddr_newModInsertContact,$DB->escape($id),$DB->noHTML($_POST["fn"]),$DB->noHTML($_POST["ln"]),$DB->noHTML($_POST["m"]),$DB->noHTML($_POST["c"]),$DB->noHTML($_POST["f"]),$DB->noHTML($_POST["p1"]),$DB->noHTML($_POST["p2"]),$DB->noHTML($_POST["o"]),$DB->noHTML($_POST["tags"]));
			}
			else
			{
				$DB->execute($widAddr_newModUpdateContact,$DB->noHTML($_POST["fn"]),$DB->noHTML($_POST["ln"]),$DB->noHTML($_POST["m"]),$DB->noHTML($_POST["c"]),$DB->noHTML($_POST["f"]),$DB->noHTML($_POST["p1"]),$DB->noHTML($_POST["p2"]),$DB->noHTML($_POST["o"]),$DB->noHTML($_POST["tags"]),$DB->escape($addid));
			}
		}
	}
	
	echo '<sharedkey>'.$sharedMD5.'</sharedkey>';
	echo '<ret>'.$id.'_'.($DB->getId()).'</ret>';
    echo '<addid>'.$id.'</addid>';
}
if ($act == 'init')
{
	$sharedMD5 = md5(uniqid(rand(), true));
	
	//create new entry
	$DB->execute($widAddr_addNewContactId2,$DB->escape($_SESSION['user_id']),$DB->quote($sharedMD5));

	echo '<sharedkey>' . $sharedMD5 . '</sharedkey>';
    echo '<addid>' . $DB->getId() . '</addid>';
}
if ($act=="get")
{
	if(  isset($_SESSION['user_id']) ) {
		
		$userid_owner = $_SESSION['user_id'];
	    if ($acceptwriteshare && $acceptwriteshare == 'yes') {
	        $DB->getResults($widAddresBook_getinfo, $DB->escape($id) );
	        $oldjsonacl = '';
	        if ($DB->nbResults()>0) {
				$sharedMD5 = $shareditemkey;
	            while ($row = $DB->fetch(0)) {
	               $userid_owner = $row['user_id'];
				}
				$DB->freeResults();
	        }
	        if ($userid_owner != $_SESSION['user_id']) {
				echo "<createdby>another</createdby>";
	        } else {
				echo "<createdby>myself</createdby>";
			}
		}
			
		$DB->getResults($widAddr_getAddressBook,$DB->escape($id),$DB->escape($userid_owner));
		while ($row = $DB->fetch(0))
		{
			echo '<addr>';
			echo '<add_id>'.$row["add_id"].'</add_id>';
			echo '<firstname><![CDATA['.$row["firstname"].']]></firstname>';
			echo '<lastname><![CDATA['.$row["lastname"].']]></lastname>';
			echo '<email><![CDATA['.$row["email"].']]></email>';
			echo '<company><![CDATA['.$row["company"].']]></company>';
	        echo '<func><![CDATA['.$row["func"].']]></func>';
	        echo '<phone1><![CDATA['.$row["phone1"].']]></phone1>';
	        echo '<phone2><![CDATA['.$row["phone2"].']]></phone2>';
	        echo '<other><![CDATA['.$row["other"].']]></other>';
	        echo '<tags><![CDATA['.$row["tags"].']]></tags>';
			echo '</addr>';
		}
		$DB->freeResults();
	} else {
		$DB->getResults($widAddr_getAddressBookSharedKey,$DB->quote($shareditemkey),$DB->escape($id));
		while ($row = $DB->fetch(0))
		{
			echo '<addr>';
			echo '<add_id>'.$row["add_id"].'</add_id>';
			echo '<firstname><![CDATA['.$row["firstname"].']]></firstname>';
			echo '<lastname><![CDATA['.$row["lastname"].']]></lastname>';
			echo '<email><![CDATA['.$row["email"].']]></email>';
			echo '<company><![CDATA['.$row["company"].']]></company>';
	        echo '<func><![CDATA['.$row["func"].']]></func>';
	        echo '<phone1><![CDATA['.$row["phone1"].']]></phone1>';
	        echo '<phone2><![CDATA['.$row["phone2"].']]></phone2>';
	        echo '<other><![CDATA['.$row["other"].']]></other>';
	        echo '<tags><![CDATA['.$row["tags"].']]></tags>';
			echo '</addr>';
		}
		$DB->freeResults();
	}
}
$DB->close();

$file->status("1");

$file->footer();
?>