<?php
// +-------------------------------------------------------------+
// | HiveMail™ version 1.3.1
// +-------------------------------------------------------------+
// | HIVEMAIL™ IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than Hivemail or if you have otherwise received
// | this software from someone who is not a representative of
// | this organization you are involved in an illegal activity.
// +-------------------------------------------------------------+
// | $RCSfile: functions_hivepop.php,v $ - $Revision: 1.14 $
// | $Date: 2004/07/21 02:34:30 $ - $Author: tubedogg $
// +-------------------------------------------------------------+

error_reporting(E_ALL & ~E_NOTICE);

// ############################################################################
// Tries to the save the message files 5 times, if someone else is trying to save
function hivepop_savemsg($message, $datafile, $userid = 0) {
	$msgs = hivepop_listmsgs($userid);
	$lastmsg = intval(array_pop($msgs));
	for ($i = 1; $i < 5; $i++) {
		$filepath = hivepop_folder($userid).'/'.($lastmsg + $i);
		if (writetofile("$filepath.mail", $message)) {
			if (empty($datafile) or writetofile("$filepath.data", $datafile)) {
				return true;
			} else {
				@unlink($mailpath);
			}
		}
	}
	return false;
}

// ############################################################################
// Deletes the message and data files from the mailbox
function hivepop_deletemsg($msgnum, $userid = 0) {
	$mailpath = hivepop_folder($userid).'/'.intme($msgnum);
	@unlink($mailpath.'.data');
	@unlink($mailpath.'.mail');
}

// ############################################################################
// Empties the user's mailbox
function hivepop_empty($userid = 0) {
	if (is_array($userid)) {
		foreach ($userid as $id) {
			hivepop_empty($id);
		}
	} else {
		$msgs = hivepop_listmsgs($userid, true);
		foreach ($msgs as $msgnum) {
			hivepop_deletemsg($msgnum, $userid);
		}
	}
}

// ############################################################################
// Synchronize the user's mailbox with the online account
function hivepop_sync($emails, $userid = 0) {
	$folder = hivepop_folder($userid);
	$popmsgs = hivepop_listmsgs($userid);
	$uniquestrs = array();
	foreach ($popmsgs as $msgfile) {
		if (getextension($msgfile) == 'data') {
			$uniquestrs[readfromfile($folder.'/'.$msgfile)] = intval($msgfile);
		}
	}
	$DB_site->reset($emails);
	while ($email = $DB_site->fetch_array($emails)) {
		if (isset($uniquestrs["$email[uniquestr]"])) {
			hivepop_deletemsg($uniquestrs["$email[uniquestr]"]);
		}
	}
}

// ############################################################################
// Returns list of messages in the user's mailbox
function hivepop_listmsgs($userid = 0, $overridecache = false) {
	global $hiveuser;
	static $_hivepop_scan_cache = array();

	if (intme($userid) == 0) {
		$userid = $hiveuser['userid'];
	}

	if ($overridecache or !isset($_hivepop_scan_cache[$userid])) {
		$_hivepop_scan_cache[$userid] = array();
		$msgfiles = browsedir(hivepop_folder($userid));
		foreach ($msgfiles as $filename) {
			if (getextension($filename) == 'mail') {
				$_hivepop_scan_cache[$userid][] = intval($filename);
			}
		}
	}

	return $_hivepop_scan_cache[$userid];
}

// ############################################################################
// Return the total size of the user's mailbox
function hivepop_size($userid = 0) {
	return foldersize(hivepop_folder($userid), array('inc' => array('mail')));
}
	
// @############################################################################
// Return the path to the user's mailbox, and if it doesn't exist create
function hivepop_folder($userid = 0, $create = true) {
	global $hiveuser;

	if (intme($userid) == 0) {
		$userid = $hiveuser['userid'];
	}
	$folder = getop('hivepop_path', true).'/'.$userid;

	if ($create) {
		clearstatcache();
		if (!is_dir($folder)) {
			mkdir($folder, 0777);
			chmod($folder, 0777);
		}
	}

	return $folder;
}

?>