<?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/>.
*/
# ***************************************
# Notes modules 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_notes.php";
$granted="I,N";
//includes
require_once('includes.php');
require_once('../includes/xml.inc.php');
//require_once('../includes/acls.inc.php');
$file=new xmlFile();
$file->header("channel");
$noteid = isset($_POST["noteid"])?$_POST["noteid"]:0;
$notes = isset($_POST["notes"])?$_POST["notes"]:"";
$shareditemkey = isset($_POST["shareditemkey"])?$_POST["shareditemkey"]:"";
$acceptwriteshare = isset($_POST["acceptwriteshare"])?$_POST["acceptwriteshare"]:"";
$act = isset($_POST["act"])?$_POST["act"]:"";

if ($act == 'init' && isset($_SESSION['user_id']) )
{
	$sharedMD5 = md5(uniqid(rand(), true));
	
	//create new entry
	$DB->execute($widnote_addNote,$DB->escape($_SESSION['user_id']),$DB->quote(""),$DB->quote($sharedMD5));

	echo '<sharedkey>' . $sharedMD5 . '</sharedkey>';
    echo '<noteid>' . $DB->getId() . '</noteid>';
}
if(!isset($_GET["getText"]) && isset($_SESSION['user_id']) ) {


	if ($noteid==0) {
		$sharedMD5 = md5(uniqid(rand(), true));
		$DB->execute($widnote_newNote,$DB->escape($_SESSION['user_id']),
                                $DB->quote($notes),
                                $DB->quote($sharedMD5)
                                );
		echo '<ret>'.$DB->getId().'</ret>';
	}
	else {
        //get share item
   
        //widget shared and user who receive share accepts it
        if ($shareditemkey != '' && $acceptwriteshare == 'yes') {
            $DB->execute($widnote_updateNotebySharedItemkey,
                                $DB->quote($notes),
                                $DB->escape($noteid),
                                $DB->quote($shareditemkey)
                               );         
        } else {
            $DB->execute($widnote_updateNote,
                                $DB->quote($notes),
                                $DB->escape($noteid),
                                $DB->escape($_SESSION['user_id'])
                               );  
        }
        //if not updates, create a new entry and a new note
		if ($DB->nbAffected()==0) {
			$sharedMD5 = md5(uniqid(rand(), true));
			$DB->execute($widnote_addNote,
                                $DB->escape($_SESSION['user_id']),
                                $DB->quote($notes),
                                $DB->quote($sharedMD5)
                        );
			echo '<ret>'.$DB->getId().'</ret>';
            
		} else {

            $DB->getResults($admin_getUsersNotesSharedmd5key,$DB->escape($noteid));
            $row=$DB->fetch(0);
            $sharedMD5 =  $row['shared']; 
            echo '<ret>'.$noteid.'</ret>';
        }
	}
    echo '<sharedkey>'.$sharedMD5.'</sharedkey>';
}
else {
    $noteid=$_GET["noteid"];
    $sharedmd5key=isset($_GET['sharedmd5key'])?$_GET['sharedmd5key']:'';
    $widgetid = isset($_GET['widgetid'])?$_GET['widgetid']:0;
	$shareditemkey=isset($_GET['shareditemkey'])?$_GET['shareditemkey']:'';

	if ($_GET["noteid"]!=0 && isset($_SESSION['user_id'])) {
        if( (empty($sharedmd5key))
        || (!isset($sharedmd5key)) 
        || ($sharedmd5key=='undefined') ) {
            $DB->getResults($xml_getUserNotes,
                                    $DB->escape($noteid),
                                    $DB->escape($_SESSION["user_id"])
                                );
			$row = $DB->fetch(0);
			if( empty($row["notes"]) ) {
				$DB->freeResults();
				$DB->getResults($xml_getUserNotesBySharedKey,
                            $DB->escape($noteid),
							$DB->quote($shareditemkey)
                        );
				$row = $DB->fetch(0);
			}
        }
        else {
            $DB->getResults($xml_getUserNotesShared,
                            $DB->quote("%noteid=".$DB->escape($noteid)."%"),
                            $DB->quote($sharedmd5key),
                            $DB->escape($_SESSION['user_id']),
                            $DB->escape($widgetid),
                            $DB->escape($noteid)
                            );
			$row = $DB->fetch(0);
        }
        $inc=0;
        
		echo '<note><![CDATA['.$row["notes"].']]></note>';
        $DB->freeResults();
    } elseif( $_GET["noteid"]!=0 && !empty($shareditemkey) ) {
        $DB->getResults($xml_getUserNotesBySharedKey,
                                $DB->escape($noteid),
								$DB->quote($shareditemkey)
                                );
		$row = $DB->fetch(0);
		if( !empty($row["notes"]) ) {
			echo '<note><![CDATA['.$row["notes"].']]></note>';
		}
		$DB->freeResults();
		$DB->close();
	}
}

$DB->close();

$file->status(1);

$file->footer();
?>