<?
  include ("../../admin/config/cfg.php");
  global $cwlang, $cwLanguage, $absBasePath;
  
  //================ PAGE LOGIC ================
  $alert=getAlert();

  //this function get all the plugin informations
  function getPluginInfo($file) {
    $pluginArr=xml2array($file);
    //errArr($ret);
    $ret=Array();
    if ($pluginArr[0]["name"]=="plugin") {
      foreach ($pluginArr[0]["elements"] as $el) {
        $ret[$el["name"]]=$el["text"];
      }
    }
    if (count($ret)>0) return $ret;
    else return false;
  }
  
  $dir = $absBasePath."../sandbox/";

  //upload a plugin
  if ($_POST["act"]=="upload") {
    $ftmp = $_FILES['filename']['tmp_name'];
    $fname = $_FILES['filename']['name'];
    errArr($_FILES);
    if ($_FILES["filename"]["type"]=="text/xml") $ret=move_uploaded_file($ftmp, $dir.$fname);  
    if ($ret==1) $alert="<div class=\"AlertGood\">".$cwlang["isdn"][""]."Plugin uploaded</div>";
    else $alert="<div class=\"AlertBad\">".$cwlang["isdn"][""]."Plugin not uploaded</div>";
    goToMainPage($alert,"./");
  }
  
  //activate a plugin
  if ($_GET["act"]=="activate") {
    $filename=$dir.$_GET["name"];
    $xml=file_get_contents($filename);
    $ret=$asterisk->putPlugin($xml);
    if ($ret==1) $alert="<div class=\"AlertGood\">".$cwlang["isdn"][""]."Plugin installed</div>";
    else $alert="<div class=\"AlertBad\">".$cwlang["isdn"][""]."Plugin not installed</div>";
    goToMainPage($alert,"./");
  }

  //deactivate a plugin
  if ($_GET["act"]=="deactivate") {
    $ret=$asterisk->delPlugin($_GET["id"]);
    if ($ret==1) $alert="<div class=\"AlertGood\">".$cwlang["isdn"][""]."Plugin turned off</div>";
    else $alert="<div class=\"AlertBad\">".$cwlang["isdn"][""]."Plugin not turned off</div>";
    goToMainPage($alert,"./");
  }

  //upgrade a plugin
  if ($_GET["act"]=="upgrade") {
    $ret=$asterisk->delPlugin($_GET["id"]);
    $filename=$dir.$_GET["name"];
    $xml=file_get_contents($filename);
    $ret=$asterisk->putPlugin($xml);
    if ($ret==1) $alert="<div class=\"AlertGood\">".$cwlang["isdn"][""]."Plugin upgraded</div>";
    else $alert="<div class=\"AlertBad\">".$cwlang["isdn"][""]."Plugin not upgraded</div>";
    goToMainPage($alert,"./");
  }


  //read the list of already installed plugin
  $installedPlugin=$asterisk->getPlugin(0);
  
  //read the list of available plugins
  if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
      while (($file = readdir($dh)) !== false) {
        $found=false;
        if (substr($file,-4)==".xml") {
          $desc=getPluginInfo($dir.$file);
          if ($desc!==false) 
            //search if already installed
            foreach ($installedPlugin as $p) if($p["name"]==$desc["name"]) {
              $found=true;
              $version=$p["version"];
            }
            if ($found and floatval($version)>floatval($desc["version"]) ) continue;
            else if ($found and $version==$desc["version"]) {
              $button="<td><a href=\"$PHP_SELF?act=deactivate&id=".$p["id"]."\" class=\"red\">DEACTIVATE</a></td>";
              $style=" style='background: lightgreen; color: black;'";
            } else if ($found) {   
              $button="<td><a href=\"$PHP_SELF?act=upgrade&name=$file&id=".$p["id"]."\" class=\"green\">UPGRADE</a></td>";
              $style=" style='background: yellow; color: black;'";
            } else {
              $button="<td><a href=\"$PHP_SELF?act=activate&name=$file\" class=\"green\">ACTIVATE</a></td>";
              $style=" style=''";
            }

            $html.="<tr class=\"firstofgroup rule\" $style>";
            $html.="<td><a href=\"".$desc["uri"]."\" target=\"_blank\">".$desc["name"]."</a></td>";
            $html.="<td>".$desc["version"]."</td>";
            $html.="<td>".$desc["author"]."</td>";
            $html.="<td>".$desc["description"]."</td>";
            $html.=$button;              
            $html.="</tr>\n";
        }
      }
      closedir($dh);
    }
  }

  
  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  
  <div class="SectionHeader">
    <?=$alert?>
    <h1><?=$cwlang["isdn"][""]?>Plugins</h1>
  </div>
  <p class="blockintro">These are the plugin available. You can activate each plugin by clicking on the activate link.</p>
  <div class="block">
  <table class="todolist" cellspacing="0" >
    <tr>
      <th>Name</th><th>Version</th><th>Author</th><th>Description</th><th>Activate</th>
    </tr>
    <?=$html?>
  </table>
  </div>
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
        <div class="Sidebar">
          <h1><?=$cwlang["isdn"][""]?>Plugin</h1>
            <?=$cwlang["isdn"][""]?>
            <p>Upload a plugin
            <form enctype="multipart/form-data" action="<?=$PHP_SELF?>" method="post">
              <table>
                <tr><td>File: </td><td><input type="file" name="filename" size="10" /></td></tr>
                <tr><td></td><td><input type="submit" value="Send" /></td></tr>
              </table>
              <input type="hidden" name="act" value="upload" />
            </form>
            </p>
        </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================

  $smarty->assign("section", "plugin");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('settings.tpl');
?>

