<?php
define("PATH_TO_ROOT", "../");
$sessionHandling = 1;
include(PATH_TO_ROOT."init.php");

/*


define("DB_WARPIT_HELP", $dbConfig['table_warpit_webhelp'] );
define("DB_WARPIT_USERSPOOL", $dbConfig['table_warpit_userspool']);
define("DB_WARPIT_WEBCATI", $dbConfig['table_warpit_webcati']);
define("DB_WARPIT_WEBCATI_BASE", $dbConfig['table_warpit_webcati_base']);
define("DB_WARPIT_WEBRECODE", $dbConfig['table_warpit_webrecode']);
define("DB_WARPIT_LIVEUPDATE", $dbConfig['table_warpit_liveupdate']);

$paths = configLoader::loadConfig('paths');

// webproject path
define("WARPIT_WEBPROJECTS", $paths['warpit_webprojects']);
define("WARPIT_WWW", $paths['warpit_www'] );
define("WARPIT_LIVEUPDATE_ZIPVERSION", $paths['live_update_zip_files'] );
// Absolute server path
define("WARPIT_ABSOLUTE", $paths['warpit_absolute']);



*/

class API_Warpit_WebUsersPool {
	
	function __construct($db, $obj)
	{
		$this->db = $db;
		$this->XMLobj = $obj;		
	}
	
	
	
	
	function portalLogin($ar) {
		if ($ar->loadData) $f = $ar->loadData; else $f = "email";
		
		$sql = "SELECT ".$f." FROM ".DB_WARPIT_USERSPOOL."._Interviewer 
					WHERE email = '".$ar->username."' AND portalPassword = '".$ar->password."'";
		$res = $this->db->SQLexecute($sql);
		$num = $this->db->NumRows($res);
		//echo $sql;
		$data = $this->db->fetchAssoc($res);
		
		
		if ($ar->action->attributes()->loggingID) $this->returnArray[ (string) $ar->action->attributes()->loggingID ] = $num;
		
		if ($ar->loadData->attributes()->loggingID)	$this->returnArray[ (string) $ar->loadData->attributes()->loggingID ] = $data;
		
		return $num;
	}
	
	
	
	function addPannelistToGroup($aGroups) {
		
		$tmp = explode(",",(string) $aGroups);
		if (!count($tmp)) return 9;
		foreach ($tmp as $val) $tmpGroup[] = "(".$val.",".$this->currentIdPannelist.")";
		$sql = "INSERT IGNORE INTO ".DB_WARPIT_USERSPOOL."._group_link (id_group,id) VALUES ".implode(",",$tmpGroup);
		$this->db->SQLexecute($sql);
	}
	
	
	function addPannelist($ar) {
		if (is_object($ar)) $ar = (array) $ar;
		elseif (is_array($ar)) return 9;
		
		unset($ar['id']);
		foreach ($ar as $key => $val) $tmp[] = $key." = '".$val."'";
		$sql = "INSERT  ".DB_WARPIT_USERSPOOL."._Interviewer SET ".implode(",",$tmp)." 
					ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id), ".implode(",",$tmp);
		$this->db->SQLexecute($sql);
		$this->currentIdPannelist = $this->db->GetLastInsertId();
		
		//echo $sql."<br />"; 
		//echo "Trenutni user = ".$this->currentIdPannelist."<br />";
	}
	
	function UpdatePannelist($ar) {
		
		if (is_object($ar)) $ar = (array) $ar;
		elseif (is_array($ar)) return 9;
		
		if ($id = $ar['id']) {
			
			unset($ar['id']);
			foreach ($ar as $key => $val) $tmp[] = $key." = '".$val."'";
			
			$sql = "UPDATE ".DB_WARPIT_USERSPOOL."._Interviewer SET ".implode(",",$tmp)." WHERE id = ".$id;
			$this->db->SQLexecute($sql);
			$this->currentIdPannelist = $id;
		}
		
	}
	
	function UpdateTable($aXMLObj) {
		foreach ((array)$aXMLObj->fields as $key=>$val) $tmp[] = $key." = '".$val."'";
		$sql = "UPDATE ".DB_WARPIT_USERSPOOL.".".$aXMLObj->table." SET ".implode(",",$tmp)." WHERE ".$aXMLObj->condition;
		$this->db->SQLexecute($sql);
	}
	
	function CheckEmail($aEmail,$attr = "") {
		$sql = "SELECT email FROM ".DB_WARPIT_USERSPOOL."._Interviewer WHERE email = '".$aEmail."'";
		$res = $this->db->SQLexecute($sql);
		$num = $this->db->NumRows($res);
		if ($attr->loggingID) $this->returnArray[ (string) $attr->loggingID] = $num;
		return $num;
		
	}
	
	
	function SendEmail($aObject) {
		
		
		if ( $aObject->mailSet &&  $this->currentIdPannelist) {
			$sql = "INSERT INTO ".DB_WARPIT_USERSPOOL."._MailStatistic 
						SET 
							mail_set = ".$aObject->mailSet.",
							interviewer_id = ".$this->currentIdPannelist.",
							mail_status = 9,
							mail_address = ( SELECT email FROM ".DB_WARPIT_USERSPOOL."._Interviewer WHERE id = ".$this->currentIdPannelist.")";
			//echo $sql."<Br>";
			$this->db->SQLexecute($sql);
		} elseif ($aObject->mailSet && $aObject->email) {
			
			$sql = "INSERT INTO ".DB_WARPIT_USERSPOOL."._MailStatistic 
						SET 
							mail_set = ".$aObject->mailSet.",
							interviewer_id = ( SELECT id FROM ".DB_WARPIT_USERSPOOL."._Interviewer WHERE email = '".$aObject->email."' ),
							mail_status = 9,
							mail_address = '".$aObject->email."'";
			//echo $sql."<Br>";
			$this->db->SQLexecute($sql);
		}
		
			
			$url = WARPIT_WWW.'webuserspool/_ajax/ajax.sendMail.php';	
			$fields = array(
			            'mailSet'=>urlencode($aObject->mailSet),
			            'appID'=>urlencode("SoulskinerHardCodedAndVerySecure"),
			            'mailStatus'=>urlencode("9")	        
			        );
			
			//url-ify the data for the POST
			foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
			rtrim($fields_string,'&');
			
			//open connection
			$ch = curl_init();
			
			//set the url, number of POST vars, POST data
			curl_setopt($ch,CURLOPT_URL,$url);
			curl_setopt($ch,CURLOPT_POST,count($fields));
			curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
			
			//execute post
			$result = curl_exec($ch);
			
			//close connection
			curl_close($ch);
		
		
	}
	
	function runParser() {
		
	
		if ($inter = $this->XMLobj->interviewer) {
		
			if ($inter->action == "add") {
				if ( (($inter->checkEmail == 1 || $inter->checkEmail == "true") AND $this->CheckEmail($inter->fields->email,$inter->checkEmail->attributes()) == 0) || !$inter->checkEmail) {
					$this->addPannelist($inter->fields);				
					if ($inter->groups) $this->addPannelistToGroup($inter->groups);				
					if ($inter->sendEmail) $this->SendEmail($inter->sendEmail);
					
					if ($inter->addFriend) {
						$sql = "INSERT IGNORE INTO ".DB_WARPIT_USERSPOOL."._friendPoints  
									SET pannelist_id = ".$inter->addFriend.", friend_id = ".$this->currentIdPannelist;
						
						$this->db->SQLexecute($sql);
					}
				}
			} 
			elseif ($inter->action == "update") {				
				$this->UpdatePannelist($inter->fields);
				if ($inter->sendEmail) $this->SendEmail($inter->sendEmail);
				if ($inter->updateTable) $this->UpdateTable($inter->updateTable);
					
				
			}
			elseif ($inter->action == "portal-login") {				
				$this->portalLogin($inter);
			} elseif ($inter->action == "send") {
				if ($inter->email && $this->CheckEmail($inter->email)) {
					//echo "posiljamo mail";
					$this->SendEmail($inter);
				} 
			}
				
		}
		
	}
	
}

if ($_REQUEST['XML']) {

	
	/*echo $_REQUEST['XML'];
  exit;*/
	$xml = new SimpleXMLElement( $_REQUEST['XML'] );
  
 
  $returnData = array();
  if ($xml->warpit) {
  		 
  		foreach ($xml->warpit as $warpitObject) {
  			
  			if ($warpitObject->webuserspool) {
  			  $obj = new API_Warpit_WebUsersPool($db, $warpitObject->webuserspool);
  			  $obj->runParser();
  			  if ($obj->returnArray) $returnData = array_merge($returnData, $obj->returnArray);
  			}
  		}
  		
  	
  }

  echo json_encode($returnData);
  
}

?>