<?php
include_once(_DIR_EXT.'/HTML/c_page_TXT.php') ;
class c_page_cab_db_terminal_ip extends c_page_TXT
{ public $title='IP адреса терминалов' ;
  function check_access(){return(1);}
  var $no_view_in_setting_right=1 ;
  public $file_csv ;
  public $file_csv2 ;

 function body()
 {   $arr=array() ;
     $recs=execSQL('select pkey,(select obj_name from obj_site_account t2 where t2.pkey=t1.parent) as group_name ,t1.obj_name,t1.IP from obj_site_account t1 where t1.clss=203 and t1.virt is null and pkey in (351,346)',0,1) ;
     if (_sizeof($recs)) foreach($recs as $i=>$rec)
      {   $arr[]='echo group: "'.$rec['group_name'].'" name: "'.$rec['obj_name'].'" IP: "'.$rec['IP'].'"' ;
          $arr[]='scp /var/www/dev.kvzrm.ru/updates/$1 root@'.$rec['IP'].':/root/' ;
          $arr[]='ssh root@'.$rec['IP'].' "tar -xvzf /root/test_update.sh.tgz"' ;
          $arr[]='ssh root@'.$rec['IP'].' "bash /root/$2"' ;
          //$arr[]='ssh root@'.$rec['IP'].' "reboot"' ;
          $arr[]='' ;
      }
     file_put_contents(_DIR_TO_ROOT.'/sh/update.sh',implode("\n",$arr)) ;
     if (_sizeof($arr)) foreach($arr as $line) echo $line.'<br>' ;
 }

 function body2()
 {
     $fname='terminal_info.csv' ;
     $fname2='terminal_ip.csv' ;
     $this->file_csv = fopen(_DIR_TO_ROOT.'/docs/'.$fname, 'w');
     $this->file_csv2 = fopen(_DIR_TO_ROOT.'/docs/'.$fname2, 'w');
     $text='' ;

     $this->page_title('IP адреса терминалов') ;
     $recs=execSQL('select pkey,(select obj_name from obj_site_account t2 where t2.pkey=t1.parent) as group_name ,t1.obj_name,t1.IP from obj_site_account t1 where t1.clss=203 and t1.virt is null',2,1) ;
     if (_sizeof($recs)) foreach($recs as $i=>$rec)
          {    $data=array() ;
               $data['group']=$rec['group_name'];
               $data['name']=$rec['obj_name'];
               $data['IP']=$rec['IP'];
               fputcsv($this->file_csv,$data,';');
               $data=array() ;
               $data['IP']=$rec['IP'];
               fputcsv($this->file_csv2,$data,';');
          }
     //ob_clean() ; // чисти вывод
     fclose($this->file_csv);
     fclose($this->file_csv2);
     file_put_contents(_DIR_TO_ROOT.'/sh/update.sh',$text) ;
 }

 function put_data_to_CSV($data)
 {
    if (_sizeof($data)) foreach($data as $fname=>$value) $data[$fname]=iconv('UTF-8','windows-1251',$value) ;
    fputcsv($this->file_csv,$data,';');
 }




}

?>
