<?php
/*
   The Index software may be freely distributed.
   See license.txt for details.
*/

define('NOTESFILE', ROOT . 'notes/notes.lst');

function ReadNotes()
{
    global $notes;
    // Make sure notes are only written if read correctly
    global $notesread;
    $notesread = 0;
    // The included file will set notesread to true, iff it gets read
    // completely.
    if (file_exists(NOTESFILE))
    {
        include(NOTESFILE);
    }
    else
    {
        // Make sure we can write it for the first time
        $notesread = 1;
    }
}

function MakeNote($email, $notetext)
{
    $notetext = addlocallinks(text2html($notetext));
    $email = text2html($email);
    $note = array('email'=> "$email",
                  'note' => "$notetext",
                  'date' => time()
                 );
    return $note;
}


function StoreNote($key, $note, $index)
{
    global $notes;

    if ($index == "")
        $notes[$key][] = $note;
    else
        $notes[$key][$index] = $note;

    WriteNotes();
}

function RemoveNotes($key)
{
    // Manual page for unset says this is the only proper way
    unset($GLOBALS['notes'][$key]);
    WriteNotes();
    // To make sure no unset items exist in the array
    ReadNotes();
}

function RemoveNote($key, $index)
{
    // Manual page for unset says this is the only proper way
    unset($GLOBALS['notes'][$key][$index]);
    WriteNotes();
    // To make sure no unset items exist in the array
    ReadNotes();
}


function WriteNotes()
{
    global $notes;
    global $notesread;
    if ($notesread)
    {
        $f = fopen(NOTESFILE, "w");
        fwrite($f, "<?php\n");
        fwrite($f, "\$notes = array (\n"); // Notes array
        reset($notes);
        $first = true;
        while (list($key, $value) = each($notes))
        {
            if ($first)
                $first = false;
            else
                fwrite($f, ",\n");

            fwrite($f, "'$key' => array (\n");
            // Write item's notes
            reset($value);
            $firstnote = true;
            while (list(, $noterec) = each($value))
            {
                if (!isset($noterec))
                    continue;
                if ($firstnote)
                    $firstnote = false;
                else
                    fwrite($f, "    ,\n");

                // Write record
                fwrite($f, "    array (\n");
                fwrite($f, "        'date' => '");
                fwrite($f, $noterec['date']);
                fwrite($f, "',\n");
                fwrite($f, "        'email' => '");
                fwrite($f, escapequotes($noterec['email']));
                fwrite($f, "',\n");
                fwrite($f, "        'note' => '");
                fwrite($f, escapequotes($noterec['note']));
                fwrite($f, "'\n");
                fwrite($f, "    )\n");
            }
            fwrite($f, ")\n");
        }
        fwrite($f, ");\n\n"); // Notes array
        
        fwrite($f, "// If we read all the way, this variable is set.\n");
        fwrite($f, "// If something goes wrong, this variable is not set\n");
        fwrite($f, "// and we will never write.\n");
        fwrite($f, "\$notesread = 1;\n");
        
        fwrite($f, "?>\n");
        fclose($f);
    }
}

function NrNotes($key)
{
    global $notes;
    if (!is_array($notes[$key]))
        return 0;
    else
        return count($notes[$key]);
}


function NoteExistsContaining($key, $needle)
{
    global $notes;
    if ($needle == "")
        return true;
    if (!is_array($notes[$key]))
        return false;
    else
    {
        $value = $notes[$key];
        reset($value);
        while (list(, $noterec) = each($value))
        {
            if (eregi($needle, $noterec['email'])
                    or eregi($needle, $noterec['note']))
                return true;
        }
    }
    return false;
}

?>