<?php
/*
 * @version $Id: HEADER 15930 2011-10-30 15:47:55Z tsmr $
 -------------------------------------------------------------------------
 Accounts plugin for GLPI
 Copyright (C) 2003-2011 by the accounts Development Team.

 https://forge.indepnet.net/projects/accounts
 -------------------------------------------------------------------------

 LICENSE

 This file is part of accounts.

 accounts is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 accounts is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with accounts. If not, see <http://www.gnu.org/licenses/>.
 --------------------------------------------------------------------------
 */

if (!defined('GLPI_ROOT')) {
	die("Sorry. You can't access directly to this file");
}

class PluginAccountsReport extends CommonDBTM {
   
	static function showAccountsList($values) {
      global $CFG_GLPI,$LANG;
      
      $ID = $values["id"];
      $aeskey = $values["aeskey"];
      
      $PluginAccountsHash = new PluginAccountsHash();
      $PluginAccountsHash->getFromDB($ID);
      $hash = $PluginAccountsHash->fields["hash"];
      
      if ($PluginAccountsHash->isRecursive()) {
            $entities = getSonsOf('glpi_entities',$PluginAccountsHash->getEntityID());
         } else {
            $entities = $PluginAccountsHash->getEntityID();
      }
      if ($aeskey) {
      
         $default_values["start"]  = $start  = 0;
         $default_values["id"]     = $id     = 0;
         $default_values["export"] = $export = false;

         foreach ($default_values as $key => $val) {
            if (isset($params[$key])) {
               $$key=$params[$key];
            }
         }
         
         // Set display type for export if define
         $output_type=HTML_OUTPUT;

         if (isset($values["display_type"]))
            $output_type=$values["display_type"];
            
         $header_num=1;
         $nbcols=4;
         $row_num=1;
         $numrows = 1;
         
         $parameters = "id=".$ID."&amp;aeskey=".$aeskey;
         if ($output_type==HTML_OUTPUT) {
            //self::printPager($start,$numrows,$_SERVER['PHP_SELF'],$parameters,"PluginAccountsReport");
         }
         $accountclass=new PluginAccountsAccount();
         $restrict = "`is_deleted`= '0' ";
         $restrict.= getEntitiesRestrictRequest(" AND ","glpi_plugin_accounts_accounts",'',$entities,$PluginAccountsHash->maybeRecursive());
         $restrict.= " ORDER BY `name` ";
         $accounts = getAllDatasFromTable("glpi_plugin_accounts_accounts",$restrict);
         
         echo Search::showHeader($output_type,1,$nbcols,1);
         
         echo Search::showNewLine($output_type);
         echo Search::showHeaderItem($output_type,$LANG['plugin_accounts']['upgrade'][4],$header_num);
         if (Session::isMultiEntitiesMode())
            echo Search::showHeaderItem($output_type,$LANG['entity'][0],$header_num);
         echo Search::showHeaderItem($output_type,$LANG['plugin_accounts'][2],$header_num);
         echo Search::showHeaderItem($output_type,$LANG['plugin_accounts']['upgrade'][5],$header_num);
         echo Search::showEndLine($output_type);
            
         if (!empty($accounts)) {

            foreach ($accounts as $account) {
               
               $row_num++;
               $item_num=1;
               echo Search::showNewLine($output_type);
               
               $ID=$account["id"];
               //echo "<tr class='tab_bg_1'>";
               $name = "<a href='".$CFG_GLPI["root_doc"]."/plugins/accounts/front/account.form.php?id=".$account["id"]."'>".$account["name"];
               if ($_SESSION["glpiis_ids_visible"]) $name .= " (".$account["id"].")";
               $name .= "</a>";
               echo Search::showItem($output_type,$name,$item_num,$row_num);
               
               if (Session::isMultiEntitiesMode()) {
                  $entity = Dropdown::getDropdownName("glpi_entities",$account['entities_id']);
                  echo Search::showItem($output_type,$entity,$item_num,$row_num);
               }
               
               $login = $account["login"];
               echo Search::showItem($output_type,$login,$item_num,$row_num); 
               $encrypted = $account["encrypted_password"];

               $password="<div id='hidden_password$$ID'></div>";

               $password.="<script language='javascript'>
               var good_hash=\"$hash\";
               var hash=SHA256(SHA256(\"$aeskey\"));
               if (hash != good_hash) {
                  pass = \"".$LANG['plugin_accounts'][22]."\";
               } else {
                  pass = AESDecryptCtr(\"$encrypted\",SHA256(\"$aeskey\"), 256);
               };
               document.getElementById(\"hidden_password$$ID\").innerHTML = pass;
               </script>";
               
               echo Search::showItem($output_type,$password,$item_num,$row_num);
               echo Search::showEndLine($output_type);
            }
         }
         
         // Display footer
         echo Search::showFooter($output_type,$LANG['plugin_accounts'][11]);
      }
   }
   
   static function printPager($start,$numrows,$target,$parameters,$item_type_output=0,$item_type_output_param=0) {
      global $CFG_GLPI, $LANG;

      $list_limit=$_SESSION['glpilist_limit'];
      // Forward is the next step forward
      $forward = $start+$list_limit;

      // This is the end, my friend
      $end = $numrows-$list_limit;

      // Human readable count starts here
      $current_start=$start+1;

      // And the human is viewing from start to end
      $current_end = $current_start+$list_limit-1;
      if ($current_end>$numrows) {
         $current_end = $numrows;
      }

      // Backward browsing
      if ($current_start-$list_limit<=0) {
         $back=0;
      } else {
         $back=$start-$list_limit;
      }

      // Print it

      echo "<table class='tab_cadre_pager'>\n";
      echo "<tr>\n";

      if (isset($_SESSION["glpiactiveprofile"])
         &&$_SESSION["glpiactiveprofile"]["interface"]=="central") {
         echo "<td class='tab_bg_2' width='30%'>" ;
         echo "<form method='GET' action=\"".$CFG_GLPI["root_doc"].
                                 "/front/report.dynamic.php\" target='_blank'>\n";
         echo "<input type='hidden' name='item_type' value='PluginAccountsReport'>";
         if ($item_type_output_param!=0)
            echo "<input type='hidden' name='item_type_param' value='".
                                             serialize($item_type_output_param)."'>";
         $explode=explode("&amp;",$parameters);
         for ($i=0;$i<count($explode);$i++) {
            $pos=strpos($explode[$i],'=');
            echo "<input type='hidden' name=\"".substr($explode[$i],0,$pos)."\" value=\"".
                                             substr($explode[$i],$pos+1)."\">";
         }
         /*echo "<select name='display_type'>";
         echo "<option value='".PDF_OUTPUT_LANDSCAPE."'>".$LANG['buttons'][27]." ".
                $LANG['common'][68]."</option>";
         echo "<option value='".PDF_OUTPUT_PORTRAIT."'>".$LANG['buttons'][27]." ".
                $LANG['common'][69]."</option>";
         echo "<option value='".SYLK_OUTPUT."'>".$LANG['buttons'][28]."</option>";
         echo "<option value='".CSV_OUTPUT."'>".$LANG['buttons'][44]."</option>";
         echo "<option value='-".PDF_OUTPUT_LANDSCAPE."'>".$LANG['buttons'][29]." ".
                $LANG['common'][68]."</option>";
         echo "<option value='-".PDF_OUTPUT_PORTRAIT."'>".$LANG['buttons'][29]." ".
                $LANG['common'][69]."</option>";
         echo "<option value='-".SYLK_OUTPUT."'>".$LANG['buttons'][30]."</option>";
         echo "<option value='-".CSV_OUTPUT."'>".$LANG['buttons'][45]."</option>";
         echo "</select>&nbsp;";
         echo "<input type='image' name='export'  src='".$CFG_GLPI["root_doc"]."/templates/odaxys/pics/greenbutton.png'
                title=\"".$LANG['buttons'][31]."\" value=\"".$LANG['buttons'][31]."\">";*/
         echo "</form>";
         echo "</td>" ;
      }

      // Forward and fast forward button
      if ($forward<$numrows) {
         echo "<th class='right'>";
         echo "<a href=\"$target?$parameters&amp;start=$forward\">";
         echo "<img src=\"".$CFG_GLPI["root_doc"]."/pics/right.png\" alt='".
               $LANG['buttons'][11]."' title='".$LANG['buttons'][11]."'>";
         echo "</a></th>\n";
         echo "<th class='right'>";
         echo "<a href=\"$target?$parameters&amp;start=$end\">";
         echo "<img src=\"".$CFG_GLPI["root_doc"]."/pics/last.png\" alt='".
               $LANG['buttons'][32]."' title='".$LANG['buttons'][32]."'>";
         echo "</a></th>\n";
      }

      // End pager
      echo "</tr>\n";
      echo "</table><br>\n";

   }
}

?>