<?php
include_once(_DIR_TO_CLASS.'/c_page.php') ;
class c_page_cab_test_pdf_sign extends c_page
{
  public $h1='Тестирование подписи PDF файлов' ;

  function set_head_tags()
  { parent::set_head_tags() ;
    //$this->HEAD['js'][]=_PATH_TO_EXT.'/jQuery-autoComplete/jquery.auto-complete.js' ;
    //$this->HEAD['css'][]=_PATH_TO_ADDONS.'/html5-file-uploader-master/Content/styles/styles.css' ;
  }


  function block_main()
  {
      $signature=file_get_contents(_DIR_TO_ROOT.'/cp/debug_sign/test.txt.encode.sig') ;
     //echo $signature ;
      $signature = base64_decode(trim($signature));
      
      //echo $signature ;
      file_put_contents(_DIR_TO_ROOT.'/cp/debug_sign/test.txt.decode.sig',$signature) ;


      return ;

    $this->page_title() ;
    //include_once(_DIR_EXT.'/aspmo_rmv/report_mo_rmv.php') ;
    $report_obj=new report_check_sign() ;
    //$report_obj->is_signed=1 ;
    $report_obj->sign_debug_dir='/var/www/dev.kvzrm.ru/cp/debug_sign/' ;
    if (!is_dir($report_obj->sign_debug_dir)) mkdir($report_obj->sign_debug_dir) ;
    $doc_name='test_PDF' ;
    $report_title='Тестовый PDF' ;
    $html=file_get_contents(_DIR_TO_ROOT.'/cp/test.txt') ;
    $options=array() ;
    $sign_method='ssl' ;
    //$sign_method='crypto' ; $options['fname_exist_sign_data']=_DIR_TO_ROOT.'/cp/test_sign.txt' ;
    $options['sign_method']=$sign_method ;

    $report_obj->sign_debug_dir.=$sign_method.'_'.date('d_m_Y_H_i_s') ;
    if (!is_dir($report_obj->sign_debug_dir)) mkdir($report_obj->sign_debug_dir) ;


    $doc_file_name=$report_obj->TCPDF_create_PDF_file($doc_name,$report_title,$html,'',$options) ;
    $doc_file_path=_TEMP_PATH.$doc_file_name;
    ?><iframe src="<?echo $doc_file_path?>"  style="width:100%; height:700px;margin-top:20px;" frameborder="0"></iframe><?
  }
}


class report_check_sign extends c_report
{   var $sign_debug_dir ;
    function sign_pdf($pdf,$options=array())
    { //echo 'sign_pdf' ;
        /*
        NOTES:
         - To create self-signed signature: openssl req -x509 -nodes -days 365000 -newkey rsa:1024 -keyout tcpdf.crt -out tcpdf.crt
         - To export crt to p12: openssl pkcs12 -export -in tcpdf.crt -out tcpdf.p12
         - To convert pfx certificate to pem: openssl pkcs12 -in tcpdf.pfx -out tcpdf.crt -nodes
        */

        // set certificate file
        //$certificate = 'file://data/cert/tcpdf.crt';
        //$certificate = 'file://brw.cer';
        $certificate = 'file:///var/www/dev.kvzrm.ru/cp/cert/tcpdf.crt';
        //$certificate_priv = 'file://brw.private';
        echo  $certificate.'<br>' ;

        // set additional information
        $info = array(
        	'Name' => 'Веселова Татьяна Владимировна',
        	'Location' => 'ESMO',
        	'Reason' => 'Testing TCPDF to ESMO',
        	'ContactInfo' => 'http://dev.kvzrm.ru',
        	);

        //$fname_exist_sign_data=_DIR_TO_ROOT.'/cp/test_sign.txt' ;
        //$fname_exist_sign_data='' ;

        // подпись документа tcpdf:7595


        // set document signature
        $pdf->sign_debug_dir=$this->sign_debug_dir ;
        $pdf->setSignature($certificate, $certificate, 'tcpdfdemo esmo', '', 2, $info);
        if ($options['fname_exist_sign_data'])
        {  $cont=file_get_contents($options['fname_exist_sign_data']) ;
           $cont=str_replace("\r",'',$cont) ;
           $pdf->exist_sign_data=$cont ;
        }

        // set font
        $pdf->SetFont('helvetica', '', 12);
        /*
        // add a page
        $pdf->AddPage();

        // print a line of text
        $text = 'This is a <b color="#FF0000">digitally signed document</b> using the default (example) <b>tcpdf.crt</b> certificate.<br />To validate this signature you have to load the <b color="#006600">tcpdf.fdf</b> on the Arobat Reader to add the certificate to <i>List of Trusted Identities</i>.<br /><br />For more information check the source code of this example and the source code documentation for the <i>setSignature()</i> method.<br /><br /><a href="http://www.tcpdf.org">www.tcpdf.org</a>';
        $pdf->writeHTML($text, true, 0, true, 0);

        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        // *** set signature appearance ***

        // create content for signature (image and/or text)
        $pdf->Image(_DIR_TO_ROOT.'/AddOns/tcpdf/examples/images/tcpdf_signature.png', 180, 60, 15, 15, 'PNG');

        // define active area for signature appearance
        $pdf->setSignatureAppearance(180, 60, 15, 15);

        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        // *** set an empty signature appearance ***
        $pdf->addEmptySignatureAppearance(180, 80, 15, 15);
        */
    }
}

?>