<?php
# ID: 18
# Trunk_create_module ver.1.0
# last modification 26.3.2010
# Formulář pro vytvareni trunků
#
define ("KONTEXT","incomming-calls");
if (IsSet($_POST['TC_SUBMIT'])&& $_POST['TC_SUBMIT']=="TRUE" ){
   include "build/db_connect.inc.php"  ;
   $query="SELECT idUSER
            FROM user_account
            WHERE id=".$_POST['TC_ACCOUNT']."
            AND idTECHNOLOGY=".$_POST['TC_TECHNOLOGY']."
            ";
   $result = mysql_query($query);
    if (!$result) {
      die('Nelze dohledat uživatele:' . mysql_error());
    }
    list($idUSER) =  mysql_fetch_row($result)  ;
   // echo "iduser :".$_POST['TC_ACCOUNT'];
   $query="START TRANSACTION ";
    $result = mysql_query($query);
    
    if (!$result) {
          die('Transakci nelze spustit:' . mysql_error());
    } 
    $query="UPDATE sip_buddies SET context=\"".KONTEXT."\",host=\"".$_POST['TC_HOST']."\" WHERE id=".$_POST['TC_ACCOUNT']."";
    $result = mysql_query($query);
    
    if (!$result) {
          die('Aktualizace kontextu nebo host se nezdařila:' . mysql_error());
    }
    
   $query="INSERT INTO trunk (Name,idPROVIDER,HOST,description)
                  VALUES(\"".$_POST['TC_NAME']."\",".$_POST['TC_PROVIDER'].",\"".$_POST['TC_HOST']."\",\"".$_POST['TC_DESCRIPTION']."\")";
          $result = mysql_query($query);
          
          if (!$result) {
            die('Data nelze vložit mezi trunky:' . mysql_error());
          }
    $query="SELECT last_insert_id()";
    $result = mysql_query($query);
      if (!$result) {
        die('Nelze zjistit ID vloženého záznamu:' . mysql_error());
      }
    list($last_insert_id) = mysql_fetch_row($result)  ;
    $query="INSERT INTO trunk_detail (idTRUNK,id,idTECHNOLOGY)
            VALUES (".$last_insert_id.",".$_POST['TC_ACCOUNT'].",".$_POST['TC_TECHNOLOGY'].")
            ";
    $result = mysql_query($query);
      if (!$result) {
        die('K trunku nelze přiřadit účet:' . mysql_error());
      }
      
    $query="SELECT table_name 
            FROM technology
            WHERE idTECHNOLOGY=\"".$_POST['TC_TECHNOLOGY']."\"";
     $result = mysql_query($query);
      if (!$result) {
        die('Nelye naleznot tabulku technologie:' . mysql_error());
      }
     list($table) = mysql_fetch_row($result)  ;          
     $query="SELECT TAB.name,TAB.md5secret
                  FROM ".$table." TAB
                  WHERE TAB.id=".$_POST['TC_ACCOUNT']."
                  ";
      $result = mysql_query($query);
      if (!$result) {
          die('Účet nelze načíst:' . mysql_error());
      }
      list($trunk_user,$trunk_md5pass) = mysql_fetch_row($result)  ;
      $query="INSERT INTO ast_config (cat_metric,var_metric,filename,category,var_name,var_val)
              VALUES (2,".$last_insert_id.",\"sip.conf\",\"general\",\"register\",\"".$trunk_user.":".$_POST['TED_PASSWD']."@".$_POST['TC_HOST']."\")";
      $result = mysql_query($query);
      if (!$result) {
          die('Nelze aktualizovat konfigurační soubor sip.conf:' . mysql_error());
      }        
      
      
    $query="COMMIT";
    $result = mysql_query($query);      
    
    
  echo "Trunk byl uložen. ";
  echo "<a href=\"".selfURL()."\"> Pokračovat </a>";



}else{
############## Nacteni md5 hesla pro neviditelne pole k overeni pred odeslanim
  if(IsSet($_POST['TC_ACCOUNT'])){
          $query="SELECT Name,md5secret
                  FROM sip_buddies 
                  WHERE id=".$_POST['TC_ACCOUNT']."
                   
                  ";
        echo "pret" ;
          $result = mysql_query($query);
          if (!$result) {
            die('Účet nelze načíst:' . mysql_error());
          }
          list($user,$heslomd5)=mysql_fetch_row($result);
   }
?>

    


<script language="JavaScript">
// soubor funkcí pro kontrolu hodnot formuláře na straně klienta
  function HideIt() {
        document.getElementById('required').style.visibility= "hidden";
        document.getElementById('requiredtext').style.visibility= "hidden";
  }
 
   
  function CheckEmptyField( theField, theMessage ) {
   if( theField.value=="" ) {
      theField.focus();
      if(document.getElementById) {
        document.getElementById('required').style.visibility= "visible";
        document.getElementById('requiredtext').style.visibility= "visible";
        document.getElementById('requiredtext').innerHTML= theMessage;
        setTimeout( "HideIt()",1500);
      } else {
          alert(theMessage);
      }
      return false;
   } else {
      return true;
   }
  }
  
   function CheckPasswd( theField,theField1, theMessage ) {
   if( MD5(document.getElementById('NAME').value+":asterisk:"+theField.value)!=theField1.value ) {
      theField.focus();
      if(document.getElementById) {
        document.getElementById('required').style.visibility= "visible";
        document.getElementById('requiredtext').style.visibility= "visible";
        document.getElementById('requiredtext').innerHTML= theMessage;
        setTimeout( "HideIt()",1500);
        //document.getElementById('md5hash').value=MD5(theField.value);
      } else {
          alert(theMessage);
      }
      return false;
   } else {
      return true;
   }
  }
  
  function Validate(theForm) {
      if( !CheckEmptyField(theForm.TC_NAME,"Zadejte prosím název trunku!") ) return false;
      if( !CheckEmptyField(theForm.TC_HOST,"Zadejte prosím adresu hosta!") ) return false;
      if( !CheckEmptyField(theForm.TC_TECHNOLOGY,"Vyberte typ požadovaného účtu!") ) return false;
      if( !CheckEmptyField(theForm.TC_ACCOUNT,"Vyberte konkrétní účet!") ) return false;
      if( !CheckPasswd(theForm.TED_PASSWD,theForm.TED_PASSWD1,"hesla se nerovnaji") ) return false;
  // uspech
  return true;
}
     <!-- <![CDATA[
      var MaxLengthLock = false;
      function MaxLengthCount(fieldObj,fieldMaxLength)
      {
        if (!MaxLengthLock)
        {
          MaxLengthLock = true;
          if (fieldObj.value.length > fieldMaxLength)
          {
            alert("Text je delší než " + fieldMaxLength + " znaků!");
            fieldObj.value = fieldObj.value.substring(0,fieldMaxLength);
          }
          var percentage = parseInt(100 - (( fieldMaxLength - fieldObj.value.length) * 100)/fieldMaxLength);
          document.getElementById(fieldObj.id + "_PBar").style.width = parseInt((parseInt(fieldObj.offsetWidth)*percentage)/100)+"px";
          MaxLengthLock = false;
        }
      }
      //]]> -->

  function RefreshForm(theForm){
      document.form1.TC_SUBMIT.value="FALSE";
      document.forms.form1.submit();
  
  }  

</script>

<center>
 <div class="forms">
<h3>Vytvoření nového trunku</h3><span class="backtoform"><a href="admin_page.php?NEXTFORM=17">Zpět</a> </span>
<fieldset>
  <legend>Informace</legend>
<form name="form1" action="admin_page.php" onSubmit="return Validate(this);" method="post">
      <table >
        <tr>
          <td class="tablehead">Název: </td><td class="tableculombhead"><input type="text" name="TC_NAME" value="<?php if(IsSet($_POST['TC_NAME'])){echo $_POST['TC_NAME'];}?>" ></td>
        </tr>
        <tr>
          <td class="tablehead">HOST: </td><td class="tableculombhead"><input type="text" name="TC_HOST" value="<?php if(IsSet($_POST['TC_HOST'])){echo $_POST['TC_HOST'];}?>"> </td>
        </tr><tr>
             <?php 
        #Část která vyčítá do roletky formuláře možné uživatelské skupiny
          include "build/db_connect.inc.php"  ;
          $query="SELECT idtechnology,technology
                  FROM technology
                  WHERE validity=1
                  ORDER BY technology ASC";
          $result = mysql_query($query);
          
          if (!$result) {
            die('Could not query:' . mysql_error());
          }
          
          if (mysql_num_rows($result)!=0){
        
            
            echo ("<td class=\"tablehead\">Typ účtu: </td><td class=\"tableculombhead\"><select name=\"TC_TECHNOLOGY\" size=\"1\" onChange=\"RefreshForm()\">");
                                                echo("<option value=\"\"></option>");
                                                while (list($id_technology, $technology ) = mysql_fetch_row($result)){
                                                #podmínka pro nastavení defaultní hodnoty skupiny na User
                                                    if ($_POST['TC_TECHNOLOGY']==$id_technology){
                                                        echo("<option value=\"".$id_technology."\" selected>".$technology."</option>"); 
                                                    }else{
                                                        echo("<option value=\"".$id_technology."\">".$technology."</option>");
                                                    }
                                                }
                                                echo ("</select>");
                                                echo ("</td>");
          }     
          ?>
        </tr><tr>
           <?php 
        #Část která vyčítá do roletky formuláře možné uživatelské skupiny
         if (IsSet($_POST['TC_TECHNOLOGY']) && $_POST['TC_TECHNOLOGY']!='') {     
          include "build/db_connect.inc.php"  ;
          $query="SELECT table_name
                  FROM technology
                  WHERE validity=1 
                  AND idTECHNOLOGY=".$_POST['TC_TECHNOLOGY']."";
          $result = mysql_query($query);
          
          if (!$result) {
            die('Nelze naleznou správnou tabulku:' . mysql_error());
          }
          list($table ) = mysql_fetch_row($result);
          $query="SELECT TAB.id,TAB.name
                  FROM ".$table." TAB
                  JOIN user_account UA ON UA.id=TAB.id 
                  WHERE NOT TAB.type='user' 
                  ";
          $result = mysql_query($query);
          if (!$result) {
            die('Účet nelze načíst:' . mysql_error());
          }
          }
         
        
             if(!IsSet($_POST['TC_TECHNOLOGY']) || $_POST['TC_TECHNOLOGY']==''){$pom="disabled";} else {$pom=''; }
            echo ("<td class=\"tablehead\">Účet: </td><td class=\"tableculombhead\"><select name=\"TC_ACCOUNT\" ".$pom." size=\"1\" OnChange=\"RefreshForm();\" \">");
                                                unset ($pom);
                                                echo("<option value=\"\">vyberte</option>");
                                                while (list($id, $name ) = mysql_fetch_row($result)){
                                                #podmínka pro nastavení defaultní hodnoty skupiny na User
                                                if ($_POST['TC_ACCOUNT']==$id){
                                                    echo("<option value=\"".$id."\" selected>".$name."</option>");
                                                }else{
                                                   echo("<option value=\"".$id."\" >".$name."</option>");
                                                }
                                                    
                                                }
                                                echo ("</select>");
                                                echo ("</td>");
           
             
          ?>
        </tr><tr>
        <td class="tablehead">Ověření hesla účtu:</td><td class="tableculombhead"><input type="password" id="pass" name="TED_PASSWD" </td>
                      <input type="hidden" id="PASS" name="TED_PASSWD1" value="<?php echo $heslomd5;?>">
                      <input type="hidden" id="NAME" value="<?php echo $user;?>">
        </tr><tr> 
            <?php 
        #Část která vyčítá do roletky formuláře možné uživatelské skupiny
          include "build/db_connect.inc.php"  ;
          $query="SELECT idPROVIDER,Name
                  FROM provider
                  ORDER BY Name ASC";
          $result = mysql_query($query);
          
          if (!$result) {
            die('Could not query:' . mysql_error());
          }
          
          if (mysql_num_rows($result)!=0){
        
            
            echo ("<td class=\"tablehead\">Provider: </td><td class=\"tableculombhead\"><select name=\"TC_PROVIDER\" size=\"1\">");
                                                 
                                                while (list($id_provider, $Name ) = mysql_fetch_row($result)){
                                                #podmínka pro nastavení defaultní hodnoty skupiny na User
                          
                                                    echo("<option value=\"".$id_provider."\">".$Name."</option>");
                                                }
         
                                       echo ("</select>");
                                       echo ("</td>");
                                       
          }     
          ?>
          
          
          
          
        
        </tr><tr>
          <td class="tablehead" colspan=2>Popis: </td>
        </tr><tr>
          <td colspan=2 class="tableculombhead"><textarea name="TC_DESCRIPTION" id="TC_DESCRIPTION" accesskey="T" rows="6" cols="30" wrap="virtual"
            onpropertychange="MaxLengthCount(this,160);"
            onclick="MaxLengthCount(this,160);"
            onmousemove="MaxLengthCount(this,160);"
            onblur="MaxLengthCount(this,160);"
            onkeyup="MaxLengthCount(this,160);"
            onfocus="MaxLengthCount(this,160);"
            onchange="MaxLengthCount(this,160);"
            onafterupdate="MaxLengthCount(this,160);"></textarea>
          <div id="TC_DESCRIPTION_PBar" style="width:0px;height:10px;border:0;font-size:8px;overflow:hidden;background-color: #3399FF;"></div></td>
        </tr><tr>
         <td colspan=2 style="background-color:red;color:white">Po uložení bude účet zařazen do kontextu <b>incomming-calls</b><br> a položka host bude nastavena na novou zadanou hodnotu.</td>
        <input name="TC_SUBMIT" type="hidden" value="TRUE" >
       </tr><tr> 
        <td class="tablehead" colspan=2><input type="reset" value="Storno"><input type="submit" value="Vytvoř">
        </tr>
      </table>
  </form>
</fieldset>  



</div>
</center>
<script language="JavaScript"><!-------------
// Tuto funkci je mozno vlozit na konec hlavního dokumentu a ušetři její opakování
if(document.getElementById) {
  document.write('<img ID="required" STYLE="visibility:hidden; position:absolute; left:300; top:120" src="required.gif">');
  document.write('<p ID="requiredtext" STYLE="visibility:hidden; position:absolute; left:365; top:155" >abc</p>');
}

//-----------></script>
<?php
}
?>