<?php
ob_start();
if(!(is_readable(realpath(dirname(__FILE__).'/../').'/include/includesroutinesservice.inc'))) {
	die('Error - Could not open or read file '.realpath(dirname(__FILE__).'/../').'/include/includesroutinesservice.inc');
} else {
	include_once(realpath(dirname(__FILE__).'/../').'/include/includesroutinesservice.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');
}

function multiRequest($data, $options = array()) {
  $curly = array();
  $result = array();
  $mh = curl_multi_init();

  foreach ($data as $id => $d) {
    $curly[$id] = curl_init();
    $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
    curl_setopt($curly[$id], CURLOPT_URL,            $url);
    curl_setopt($curly[$id], CURLOPT_HEADER,         0);
    curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);

    if (is_array($d)) {
      if (!empty($d['post'])) {
        curl_setopt($curly[$id], CURLOPT_POST,       1);
        curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);
      }
    }

    if (!empty($options)) {
      curl_setopt_array($curly[$id], $options);
    }
    curl_multi_add_handle($mh, $curly[$id]);
  }

  $running = null;
  do {
    curl_multi_exec($mh, $running);
  } while($running > 0);

  foreach($curly as $id => $c) {
    $result[$id] = curl_multi_getcontent($c);
    curl_multi_remove_handle($mh, $c);
  }

  curl_multi_close($mh);
  return $result;
}

$cn = db_connect();
if (isset($_REQUEST['userid'])){

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

	$userid = $_REQUEST['userid'];
	$mailar = db_getObjectToNotificateUser($cn, $userid, $skbranch);
	$size = sizeof($mailar);
	if ($size > 0) {
		$urls = '';
		$email = $mailar[0]['user_email'];
		$userlang = $emarr[0]['lang_short'];
		$lang = db_getLanguageShortByID($cn, $mailar[0]['user_idlanguageinterface']);
		$url = getSetting('portal','url',str_replace('/service', '', getCurPageURLWO()))."/businessmodel.php?lang=".$lang."&oguid=";
		$verptrn = getResourceByLang('email.notificatemessage.versionpatern', $lang);
		$lnkptrn = getResourceByLang('email.notificatemessage.linkpatern', $lang);

		for($i=0; $i<$size; $i++){
            $ptrn = str_replace('%v', $mailar[$i]['o_ver'], $verptrn);
            $ptrn = str_replace('%d', str_replace(' 00:00', '', $mailar[$i]['o_datever']), $ptrn);
            $ptrn = str_replace('portal:url', '<a href="'.$url.$mailar[$i]['o_guid'].'">'.$mailar[$i]['o_name'].'</a>', $ptrn);
			//STU - временно убрали, пока переводим на новый вариант шаблона писем уведомлений о версии
			//$ptrn = $ptrn.' '.$mailar[$i]['statever'];
	    	$urls .= '<br/>'.$ptrn.'<br/>'.str_replace('portal:url', $url.$mailar[$i]['o_guid'], $lnkptrn).'<br/>';
	    }
        $usrname = $mailar[0]['user_name'];
		if ( sendNotificateMessage($email, getResourceByLang('email.notificatemessage.subject', $lang), $urls, $lang, $usrname) ){
			db_updateObjectNotificateUser($cn, $userid);
		    echo $email.' was sent';
		} else {
			logmessage( getResourceByLang('log.message.errorsend', $userlang).' '.$usrname);
		}
	}
	unset($mailar);
} 
db_disconnect($cn);
?>