<?php
if (!function_exists('mb_str_replace')) {
	function mb_str_replace($search, $replace, $subject, &$count = 0) {
		if (!is_array($subject)) {
			$searches = is_array($search) ? array_values($search) : array($search);
			$replacements = is_array($replace) ? array_values($replace) : array($replace);
			$replacements = array_pad($replacements, count($searches), '');

			foreach ($searches as $key => $search) {
				$parts = mb_split(preg_quote($search), $subject);
				$count += count($parts) - 1;
				$subject = implode($replacements[$key], $parts);
			}
		} else {
			foreach ($subject as $key => $value) {
				$subject[$key] = mb_str_replace($search, $replace, $value, $count);
			}
		}

		return $subject;
	}
}

ob_end_clean();
ignore_user_abort();
ob_start();
header("Connection: close");
header("Content-Length: " . ob_get_length());
ob_end_flush();
flush();
echo ' ';
if (isset($_REQUEST['key']) && $_REQUEST['key'] == md5 ($_REQUEST['uid'].$_REQUEST['oguid'].$_REQUEST['id'].$_REQUEST['idforummessageparetn'])) {
    echo ' ';

	if (isset($_REQUEST['skbranch'])) {
		$skbranch = $_REQUEST['skbranch'];
	} else {
		$skbranch = db_getMainSKBranch($cn);
	}

	if(!(is_readable(realpath(dirname(__FILE__)).'/include/includesroutines.inc'))) {
		die('Error - Could not open or read file '.realpath(dirname(__FILE__)).'/include/includesroutines.inc');
	} else {
		include_once(realpath(dirname(__FILE__)).'/include/includesroutines.inc');
	}

	if(!(is_readable(realpath(dirname(__FILE__)).'/include/mail.inc'))) {
	        die('Error - Could not open or read file '.realpath(dirname(__FILE__)).'/include/mail.inc');
	} else {
	        include_once(realpath(dirname(__FILE__)).'/include/mail.inc');
	}

	$cn = db_connect();
	if  (!isset($_REQUEST['oguid']) || !db_isObjectExits($cn, $_REQUEST['oguid'], $skbranch) || db_getUserRightsToObject($cn, GetUserIDs(), $_REQUEST['oguid'], $skbranch) === null ){
        db_disconnect($cn);
        ob_end_flush();
        exit;
	}

	$id = $_REQUEST['id'];
	
	$usrlink = db_getUsersToObjectlinks($cn,$_REQUEST['oguid'], $skbranch);
	$oguid = $_REQUEST['oguid'];
	$idforummessageparetn = $_REQUEST['idforummessageparetn'];
    $user_id = $_REQUEST['uid'];
	$userdata = array();

	if (isset($idforummessageparetn) && $idforummessageparetn != 0) {
	        $userdata = db_getUser_emailAndLangByIdForummessage($cn, $idforummessageparetn, $user_id);
	}

	if (isset($id) && $id != 0) {
	        $idobject = db_getIDObjectByGuid($cn, $oguid, $skbranch);
			$userdata = array_merge( $userdata, db_getUser_emailAndLangByToAllowUsers($cn, $idobject, $user_id));
	}

	$size = sizeof($userdata);
	if ($userdata && $size > 0 ) {
		$unique = array();
		foreach ($userdata as $elem) {
	    	if (!in_array($elem, $unique))
	        	array_push($unique, $elem);
		}
	    $size = sizeof($unique);

		if (!isset($_REQUEST['idbranch'])){
			$url = getCurPageURLWO()."/businessmodel.php?oguid=".$oguid."&forumid=".$id;
		} else {
			$url = getCurPageURLWO()."/pollobject.php?skpollingobject=".$_REQUEST['skpollingobject']."&skpolltype=".$_REQUEST['skpolltype'].(isset($_REQUEST['rguid']) ? '&rguid='.$_REQUEST['rguid'] : '').(isset($_REQUEST['skproject']) ? '&skproject='.$_REQUEST['skproject'] : '').(isset($_REQUEST['skbranch']) ? '&skbranch='.$_REQUEST['skbranch'] : '').(isset($_REQUEST['idbranch']) ? '&idbranch='.$_REQUEST['idbranch'] : '').''.(isset($_REQUEST['idpolltype']) ? '&idpolltype='.$_REQUEST['idpolltype'] : '')."&oguid=".$oguid."&forumid=".$id;
		} 

	   	for($i=0; $i<$size; $i++){
			$usermail = $unique[$i]['user_email'];
			$message = db_getForumMessage($cn, $id, $unique[$i]['user_idlanguagecontent']);
			$userlang = db_getLanguageShortByID($cn, $unique[$i]['user_idlanguageinterface']);
			$objname = db_getObjectName($cn, $oguid, $unique[$i]['user_idlanguagecontent'], $skbranch );
			$usrname = db_getUserNameByID($cn, $unique[$i]['iduser']);
			if (!sendNewForumMessage($usermail, getResourceByLang('email.newforummessage.subject', $userlang), $objname, $url, $userlang, $usrname, $message[0]['frmm_body'], $message[0]['user_name'], $message[0]['frmm_datechange'])){
					logmessage('Failed to send email to user '.$usrname);
			}
		}
	}
	db_disconnect($cn);
}
exit;
?>

