<?
  include('../../config/cfg.php');
  $debug=true;
  $mac=$_GET["mac"];
  $ip=$_SERVER["REMOTE_ADDR"]; //.":".$_SERVER["REMOTE_PORT"];
  if(trim($mac)=="") printDebug("ERROR (mac value is empty)",true);
  $submac= substr($mac,0,8);
  //Extract phone firmware version
  $useragent=substr($_SERVER["HTTP_USER_AGENT"],strpos($_SERVER["HTTP_USER_AGENT"],"(")+1,strpos($_SERVER["HTTP_USER_AGENT"],")")-strpos($_SERVER["HTTP_USER_AGENT"],"(")-1);
  $useragentArr=explode(" ",$useragent);
  if(!is_array($useragentArr)) printDebug("ERROR (unable to retrieve firmware version)",true);
	$firmware=trim(str_replace(array(";"),"",$useragentArr[2]));
	if(substr($firmware,0,1)=="6") $prov_method=1;
  else if(substr($firmware,0,1)=="7") $prov_method=2;
  else if(substr($firmware,0,1)=="8") $prov_method=2;
  else printDebug("ERROR (unknown firmware value \"".substr($firmware,0,1).".x\")",true);
  //Check if mac address has been already linked to an extension 
  $qry="SELECT id FROM cw_prov_client WHERE mac='".$mac."'";
  $res=$db->Execute($qry);
  if($res===false) printDebug("ERROR (mysql is unable to retrieve client from mac)",true);
  $count=$res->RecordCount();
  if($count!=0){
  	$ar=0;
    //extract id 
    if($arr=$res->FetchRow()){
    	$id=$arr["id"];
      $ar=1;
    }
  }else{
    //retrieve phone's vendor and model from mac address
    $phone_type=0; 
    $qry="SELECT * FROM `cw_prov_phone_type` WHERE `mac` LIKE '%".$submac."%' LIMIT 1";
    $res=$db->Execute($qry);
    if($res===false) printDebug("ERROR (unable to retrive phone type)",true);
    $arr=$res->FetchRow();
    $phone_type=$arr["id"];
    $phone_name=$arr["vendor"].$arr["model"];
    if(trim($phone_type)==0) printDebug("ERROR (wrong mac value)",true);
    //get client id
    $qry="SELECT pc.`id` ";
    $qry.="FROM `cw_prov_client` AS pc ";
    $qry.="JOIN `cw_client` AS c ON c.`id` = pc.`id_client` ";
    $qry.="JOIN `cw_prov_template` AS pt ON pt.`id`=pc.`id_template` ";
    $qry.="WHERE pc.`mac` = '' ";
    $qry.="AND pt.`id_phone` = '".$phone_type."' ";
    $qry.="ORDER BY c.`extension` ";
    $qry.="LIMIT 1";
    $res=$db->Execute($qry);
    if($res===false) printDebug("ERROR (mysql is unable to retrieve next free extension)",true);
    $arr=$res->FetchRow();
    $id=$arr["id"];
    if(!isset($id)||($id==0)||($id=="")) printDebug("ERROR (no free extension is associated to $phone_name)",true);
    //Link the phone to the first unlinked extension for this phone type
    $qry="UPDATE `cw_prov_client` SET `mac`='".$mac."' WHERE `id`='".$id."' ";
    $res=$db->Execute($qry);
    if($res===false) printDebug("ERROR (mysql is unable to link phone to extension)",true);
    $ar=$db->Affected_Rows();
  }
  //Print linked client options
  if($ar==0) printDebug("ERROR (script end in error state)",true);
  else{
    if($prov_method==1){
    	//ini format
  		include("format_ini.php");
      $log_mode="TEXT";
    }else{
    	//xml format
    	include("format_xml.php");
      $log_mode="XML";
    }
    //update ip
    $qry="UPDATE `cw_prov_client` SET `ip`='".$ip."' WHERE `id`='".$id."' ";
    $res=$db->Execute($qry);
    if($res===false) printDebug("ERROR (mysql is unable to update client ip)",true); 
  }
  printDebug("Extension ".$client["extension"]." has been successfully updated in ".$log_mode." mode (".$ip.")".$f_log,false);
  
  /***** FUNCTIONS *****/
  function sanitizeXml($label){
  	return htmlentities(html_entity_decode($label));
  }
  function printDebug($text,$die){
  	global $debug,$mac;
    if($debug===true) error_log("[PROV]Request for mac ".$mac.": ".$text);
  	if($die===true) die("[PROV]Request for mac ".$mac.": ".$text);
  }
?>