<?
	//init configuration
  echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n";
  echo "<HTML>\n";
  echo "<pre>\n";
  echo "user_active1!: on\n";
  //client settings
  $qry="SELECT pc.*, c.`extension`, c.`password`, u.`id` AS 'user_id' ";
  $qry.="FROM cw_prov_client AS pc ";
  $qry.="JOIN cw_client AS c ON pc.`id_client`=c.`id` ";
  $qry.="JOIN cw_user AS u ON u.`entity_id`=c.`id` ";
  $qry.="WHERE mac='".$mac."' ";
  $res=$db->Execute($qry);
  if($res===false) printDebug("ERROR (unable to retrieve client settings)",true);
  $client=$res->FetchRow();
  echo "user_name1: ".$client["extension"]."\n";
  echo "user_pass1: ".$client["password"]."\n";
  //template network settings
  $qry="SELECT * FROM cw_prov_template WHERE id='".$client["id_template"]."' ";
  $res=$db->Execute($qry);
  if(($res===false)||($client["id_template"]=="")) printDebug("ERROR (unable to retrieve template settings for template ".$client["id_template"].")",true);
  $template=$res->FetchRow();
  echo "user_host1: ".$template["registrar"]."\n";
  if($template["dhcp"]==0){ //static ip configuration
  	$ip=trim($client["ip"]);
    echo "dhcp$: off\n";
    echo "ip_adr$: ".trim($client["ip"])."\n";
    if(trim($client["hostname"])!="") echo "phone_name$: ".trim($client["hostname"])."\n";
    echo "netmask$: ".trim($template["netmask"])."\n";
    echo "gateway$: ".trim($template["gateway"])."\n";
    echo "dns_server1$: ".trim($template["dns1"])."\n";
    if(trim($template["domain"])!="") echo "dns_domain$: ".trim($template["domain"])."\n";
    if(trim($template["dns2"])!="") echo "dns_server2$: ".trim($template["dns2"])."\n";
    echo "setting_server$: http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/index.php?mac=".$mac."\n";
  }
  //template HTTP authentication settings
  echo "http_user: ".trim($template["http_username"])."\n";
  echo "http_pass: ".trim($template["http_password"])."\n";
  if(($template["http_scheme"]==1)||(trim($template["http_scheme"])=="1")){
    echo "http_scheme: on\n";
  }else{
  	echo "http_scheme: off\n";
  }
  //template LDAP settings
  if(($template["ldap"]==1)||(trim($template["ldap"])=="1")){
    echo "ldap_server: ".$template["registrar"]."\n";
    echo "ldap_username: cn=".$client["user_id"].",ou=people,dc=voiceone,dc=it\n";
    echo "ldap_password: ".$client["password"]."\n";
    echo "ldap_base: dc=voiceone,dc=it\n";
    //check default for ldap options
    $qry="SELECT po.`name`,po.`default`,pto.`id_option`,pto.`value`,pto.`mode` ";
    $qry.="FROM cw_prov_option AS po ";
    $qry.="LEFT JOIN cw_prov_template_option AS pto ON pto.`id_option`=po.`id` ";
    $qry.="WHERE pto.`id_template`='".$template["id"]."' ";
    $qry.="AND po.`name` IN ('ldap_search_filter','ldap_number_filter','ldap_name_attributes','ldap_number_attributes','ldap_display_name') ";
    $res=$db->Execute($qry);
    if($res!==false){
      while($option=$res->FetchRow()){
        if(trim($option["mode"])=='') $option["mode"]="&";
        if(trim($option["id_option"])=='') $option["value"]=$option["default"]; 
  	    echo "<".trim($option["name"])." perm=\"".trim($option["mode"])."\">".sanitizeXml(trim($option["value"]))."</".trim($option["name"]).">\n";
      }
    }
  }
  //advanced settings and function keys
  $qry="SELECT po.`name`,po.`account`,pto.`value`,pto.`mode` ";
  $qry.="FROM cw_prov_option AS po ";
  $qry.="JOIN cw_prov_template_option AS pto ON pto.`id_option`=po.`id` ";
  $qry.="WHERE pto.`id_template`='".$template["id"]."' ";
  $qry.="AND po.`name` NOT IN ('ldap_search_filter','ldap_number_filter','ldap_name_attributes','ldap_number_attributes','ldap_display_name') ";
  $res=$db->Execute($qry);
  if(($res===false)||($template["id"]=="")) printDebug("ERROR (unable to retrieve options for template ".$template["id"].")",true);
  while($option=$res->FetchRow()){
  	if((in_array($option["account"],array("1","2","3","4"),true))||(in_array($option["account"],array(1,2,3,4),true))){
    	$flag="1"; 
    }else $flag=""; 
  	echo trim($option["name"]).$flag.trim($option["mode"]).": ".trim($option["value"])."\n";
  }
  //firmware settings
  $f_log="";
  if($template["firmware"]!=0){
  	echo "firmware_status: http://".$template["registrar"]."/".$voiceoneWSDirName."/utility/provisioning/firmware/index.php?mac=".$mac."\n";
    $f_log=" SW ";
  }
  //directory
  if($template["directory"]!=0){
  	$qry="SELECT dc.`type`,d.`phonenumber_prefix`,d.`mobile_prefix`,c.`cid_name`,c.`phonenumber`,c.`mobile` ";
    $qry.="FROM cw_prov_directory_contacts AS dc ";
    $qry.="JOIN cw_contacts AS c ON c.`id`=dc.`id_contact` ";
    $qry.="JOIN cw_prov_directory AS d ON d.`id`=dc.`id_directory` ";
    $qry.="WHERE dc.`id_directory`='".$template["directory"]."' ";
    $qry.="ORDER BY c.`order_field` ";
    $res=$db->Execute($qry);
    if(($res===false)||($template["directory"]=="")) printDebug("ERROR (unable to retrieve directory for template ".$template["id"].")",true);
    $d_num=0;
    while($contact=$res->FetchRow()){
    	$number="";
  		if(($contact["type"]==1)||($contact["type"]=="1")){
      	$number=trim($contact["phonenumber"]);
        if(trim($contact["phonenumber_prefix"])!="") $prefix=trim($contact["phonenumber_prefix"]); else $prefix="";
      }else if(($contact["type"]==2)||($contact["type"]=="2")){
      	$number=trim($contact["mobile"]);
        if(trim($contact["mobile_prefix"])!="") $prefix=trim($contact["mobile_prefix"]); else $prefix="";
      }
      if((trim($contact["cid_name"])!="")&&($number!="")){
        echo "tn_".$d_num."".": ".$prefix.trim($contact["cid_name"])."\n";
        echo "tu_".$d_num."".": ".$number."\n";
        $d_num++;
      }
		}
  }
  //close configuration
  echo "</pre>\n";
  echo "</HTML>";
?>