<?
include_once(_DIR_EXT.'/HTML/c_page_XML_AJAX.php') ;
include_once(_DIR_EXT.'/aspmo_rmt/i_mrt.php') ;
class c_page_ajax extends c_page_XML_AJAX
{
  function sign_doc()
  { $doc_id=$_POST['doc_id'] ;
    if ($GLOBALS['LS_evulate_ECP'] or MEMBER()->info['evulate_ECP'])
    { ?><div class="green center"><strong>Включен режим эмуляции использования ЭЦП</strong></div><br><div class=center><button class="v2" cmd="ecp/emulate_sign_doc" doc_id="<?echo $doc_id?>" sert_id="<? echo MEMBER()->info['sert_id']?>" after_sign_doc_go_url="<?echo $_POST['after_sign_doc_go_url']?>">Далее</button></div><?
      $this->add_element('show_modal_window','HTML') ;
    }
    else
    {   $window_mode=ENGINE()->get_window_mode_by_HTTP_REFERER() ;
        $this->add_element('go_url','/'.$window_mode.'/cryptopro/sign/[doc_id='.$doc_id.',sign_mode=detached]/') ;
    }
  }

  function emulate_sign_doc()
  {  // получаем MD5 текущего документа
     $doc_rec=DOCS()->get_doc_by_id($_POST['doc_id']) ;
     $cont=file_get_contents($doc_rec['__doc_dir']) ;
     $str=base64_encode($cont) ;
     $_POST['sign']=md5($str) ;
     $_POST['cert']=MEMBER()->certificate ;
     $_POST['cert']['IssuerName']='Эмуляция ЭЦП';
     $this->save_signature() ;
     if ($_POST['after_sign_doc_go_url']) $this->add_element('go_url',$_POST['after_sign_doc_go_url']) ;
     else $this->add_element('go_url','/cab/docs/pdf/'.$_POST['doc_id'].'/') ;
  }

  // вызывается из criptopro:sign.js
  function save_signature()
  {  ECP()->save_signature($_POST) ;
     if ($GLOBALS['LS_trace_sign_JS']) LOG_JSON()->flush('debug_signature_'.$_POST['sign_uid']);
     if ($_POST['doc_id'])
     { $doc_rec=DOCS()->get_doc_by_id($_POST['doc_id']) ;
       $rol_info=$_SESSION['ARR_roll'][MEMBER()->rol] ;
       if ($rol_info['use_auch_as_smena'] and strpos($doc_rec['doc_name'],$rol_info['report_template'])!==false) ACCOUNTS()->member_smena_out() ;
     }

  }



}
