Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2015
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.14 KB | None | 0 0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3.  
  4. class Esignature {
  5.  
  6.     protected $url = 'https://capi-eval.signnow.com/api';
  7.     protected $encoded_client_credentials  = 'MGZjY2RiYzczNTgxY2EwZjliZjhjMzc5ZTZhOTY4MTM6MzcxOWExMjRiY2ZjMDNjNTM0ZDRmNWMwNWI1YTE5NmI=';
  8.     protected $access_token;
  9.     protected $data;
  10.     protected $document_id;
  11.  
  12.     public function __construct($params)
  13.     {
  14.         $this->data = $params;
  15.     }
  16.  
  17.     public function send()
  18.     {
  19.  
  20.         if (!$this->getToken()) {
  21.             return false;
  22.         }
  23.  
  24.         if (!$this->uploadDocument()) {
  25.             return false;
  26.         }
  27.  
  28.         if (!$this->addSignature()) {
  29.             return false;
  30.         }
  31.  
  32.  
  33.         if (!$this->sendInvite()) {
  34.             return false;
  35.         }
  36.     }
  37.  
  38.     /**
  39.      * Fetch auth token
  40.      *
  41.      * @return bool
  42.      */
  43.     protected function getToken()
  44.     {
  45.         $headers = array(
  46.             "Authorization: Basic ".$this->encoded_client_credentials."",
  47.             "Content-Type: application/x-www-form-urlencoded;charset=UTF-8"
  48.         );
  49.  
  50.         $fields = array(
  51.             'username' => urlencode('gafitescu@gmail.com'),
  52.             'password' => urlencode('pass2sign'),
  53.             'grant_type' => urlencode('password')
  54.         );
  55.  
  56.         $fields_string = '';
  57.         foreach ($fields as $key => $value) {
  58.             $fields_string .= $key.'='.$value.'&';
  59.         }
  60.         rtrim($fields_string, '&');
  61.  
  62.         $arResponse = $this->curl('/oauth2/token', $headers, $fields_string);
  63.         if (isset($arResponse->access_token)) {
  64.             $this->access_token = $arResponse->access_token;
  65.             return true;
  66.         } else {
  67.             return false;
  68.         }
  69.     }
  70.  
  71.     protected function uploadDocument()
  72.     {
  73.         $headers = array(
  74.             "Authorization: Bearer ".$this->access_token,
  75.             "Content-Type: multipart/form-data"
  76.         );
  77.  
  78.         $file = $this->getCurlValue($this->data['file']);
  79.  
  80.         $data = array('file' => $file);
  81.         $arResponse = $this->curl('/document', $headers, $data);
  82.         if (isset($arResponse->id)) {
  83.             $this->document_id = $arResponse->id;
  84.             return true;
  85.         } else {
  86.             return false;
  87.         }
  88.     }
  89.  
  90.     private function getCurlValue($filename)
  91.     {
  92.         $contentType = 'application/pdf' ;
  93.         $postname = 'contract_'.date("d-m-Y H:i:s").'.pdf';
  94.  
  95.         if (function_exists('curl_file_create')) {
  96.             return curl_file_create($filename, $contentType, $postname);
  97.         }
  98.  
  99.         // Use the old style if using an older version of PHP
  100.         $value = "@{$this->filename};filename=" . $postname;
  101.         if ($contentType) {
  102.             $value .= ';type=' . $contentType;
  103.         }
  104.  
  105.         return $value;
  106.     }
  107.  
  108.     protected function addSignature()
  109.     {
  110.         $nrPages = $this->getNrOfPagesFromPdf($this->data['file']) - 1; // 0 index pages
  111.  
  112.         $json_encoded_fields = array(
  113.             array(
  114.                 "x" => 90,
  115.                 "y" => 786,
  116.                 "width" => 285,
  117.                 "height" => 25,
  118.                 "page_number" => $nrPages,
  119.                 "required"=> true,
  120.                 "role" => "Client",
  121.                 "type" => "signature"
  122.             )
  123.         );
  124.  
  125.         $data = json_encode(
  126.             array(
  127.                 'fields' => $json_encoded_fields
  128.             )
  129.         );
  130.  
  131.         $headers = array(
  132.             "Authorization: Bearer ".$this->access_token,
  133.             "Content-Type: application/json"
  134.         );
  135.  
  136.  
  137.         $arCustomOption = array(
  138.             CURLOPT_CUSTOMREQUEST => "PUT"
  139.         );
  140.  
  141.  
  142.         $arResponse = $this->curl('/document/'.$this->document_id, $headers, $data, $arCustomOption);
  143.         if (isset($arResponse->id)) {
  144.             return true;
  145.         } else {
  146.             return false;
  147.         }
  148.     }
  149.  
  150.     protected function sendInvite()
  151.     {
  152.         $headers = array(
  153.             "Authorization: Bearer ".$this->access_token,
  154.             "Content-Type: application/json"
  155.         );
  156.  
  157.         $data = json_encode(array(
  158.             "to" =>  'receiver@gmail.com',
  159.             "from" => "gafitescu@email.com"
  160.         ));
  161.  
  162.         $arResponse = $this->curl('/document/'.$this->document_id.'/invite?email=disable', $headers, $data);
  163.         echo __METHOD__;
  164.         var_dump($arResponse);
  165.     }
  166.  
  167.     protected function fetchUser()
  168.     {
  169.         $headers = array(
  170.             "Authorization: Basic ".$this->encoded_client_credentials,
  171.             "Content-Type: application/json"
  172.         );
  173.  
  174.         $data = json_encode(
  175.                 array(
  176.                     'email' => 'usefortorrent@gmail.com',
  177.                     'password' => 'password',
  178.                     'first_name' => 'James',
  179.                     'last_name' => 'Joyce'
  180.             )
  181.         );
  182.  
  183.         $arResponse = $this->curl('/user', $headers, $data);
  184.         var_dump($arResponse);
  185.     }
  186.  
  187.     private function getNrOfPagesFromPdf($pdfname)
  188.     {
  189.         $pdftext = file_get_contents($pdfname);
  190.         $num = preg_match_all("/\/Page\W/", $pdftext, $dummy);
  191.         return $num;
  192.     }
  193.  
  194.  
  195.  
  196.     private function curl($resource, $headers, $data, $arCustomOptions = array())
  197.     {
  198.         $ch = curl_init();
  199.  
  200.         $options = array(
  201.             CURLOPT_URL => $this->url.$resource,
  202.             CURLOPT_RETURNTRANSFER => true,
  203.             CURLOPT_HTTPHEADER => $headers,
  204.             CURLINFO_HEADER_OUT => false,
  205.             CURLOPT_HEADER => false,
  206.             CURLOPT_POST => true,
  207.             CURLOPT_POSTFIELDS => $data
  208.         );
  209.  
  210.         if (!empty($arCustomOptions)) {
  211.             foreach ($arCustomOptions as $option => $value) {
  212.                 $options[$option] = $value;
  213.             }
  214.         }
  215.  
  216.         curl_setopt_array($ch, $options);
  217.         $result = curl_exec($ch);
  218.         var_dump($result);
  219.         curl_close($ch);
  220.         return json_decode($result);
  221.  
  222.     }
  223. }
  224.  
  225.  
  226. // usage:
  227. $params = array('file' => $file);
  228.         $this->load->library('Esignature', $params,'esignature');
  229.         $success = $this->esignature->send();
  230.  
  231.  
  232. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement