Advertisement
Guest User

P7MeXML

a guest
Dec 20th, 2018
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.35 KB | None | 0 0
  1. define("PREFISSOTMP","/tmp/");
  2.  
  3. class P7MeXML {
  4.     private $stringaP7M;
  5.     private $stringaXML;
  6.     private $base64=FALSE;
  7.     private $gzip=FALSE;
  8.    
  9.     public function __construct($stringa="",$base64=FALSE,$gzip=FALSE) {
  10.         if(is_bool($gzip)) $this->gzip=$gzip;
  11.         if(is_bool($base64)) $this->base64=$base64;
  12.     $raw="";
  13.         if($this->gzip) $raw= gzuncompress ($stringa);
  14.         else $raw=$stringa;
  15.         if($base64) $this->stringaP7M= base64_decode ($raw);
  16.         else $this->stringaP7M=$raw;
  17.         $this->faiXML();
  18.        
  19.         if(! $this->controllaXML()) {
  20.             $this->base64=! $this->base64;
  21.             if($this->base64) $this->stringaP7M= base64_decode ($raw);
  22.             else $this->stringaP7M=$raw;
  23.             $this->faiXML();
  24.            
  25.         }
  26.         if($this->controllaXML()) return;
  27.         if(is_bool($base64)) $this->base64=$base64;
  28.         $this->gzip= ! $this->gzip;
  29.         if($this->gzip) $raw= gzuncompress ($stringa);
  30.         else $raw=$stringa;
  31.         if($base64) $this->stringaP7M= base64_decode ($raw);
  32.         else $this->stringaP7M=$raw;
  33.         $this->faiXML();
  34.        
  35.         if(! $this->controllaXML()) {
  36.             $this->base64=! $this->base64;
  37.             if($this->base64) $this->stringaP7M= base64_decode ($raw);
  38.             else $this->stringaP7M=$raw;
  39.             $this->faiXML();
  40.            
  41.         }
  42.     }
  43.    
  44.     public function getXML(){
  45.         if(strlen($this->stringaXML)>0) return $this->stringaXML;
  46.         else return FALSE;
  47.     }
  48.    
  49.     public function setP7MdaFile($file, $base64=FALSE){
  50.         if(file_exists($file)){
  51.             $temp= file_get_contents($file);
  52.             if($base64) $temp= base64_decode ($temp);
  53.             if(strlen($temp)>0) {
  54.                 $this->stringaP7M=$temp;
  55.                 $this->faiXML();
  56.             }
  57.         }
  58.     }
  59.    
  60.     public function setP7MdaStringa($stringa,$base64=FALSE){
  61.         if(strlen($stringa)>0) {
  62.             $temp=$stringa;
  63.             if($base64) $temp= base64_decode ($temp);
  64.             if(strlen($temp)>0){
  65.                 $this->stringaP7M=$temp;
  66.                 $this->faiXML();
  67.             }
  68.         }
  69.     }
  70.    
  71.     private function faiXML(){
  72.         if(strlen($this->stringaP7M)>0){
  73.             $ora= time();
  74.             file_put_contents(PREFISSOTMP.$ora, $this->stringaP7M);
  75.             system("openssl smime -verify -noverify -in ".PREFISSOTMP.$ora." -inform DER -out ".PREFISSOTMP
  76.                         .$ora.".xml");
  77.             if(file_exists(PREFISSOTMP.$ora)) unlink(PREFISSOTMP.$ora);
  78.             if(file_exists(PREFISSOTMP.$ora.".xml")) {
  79.                 $this->stringaXML= file_get_contents(PREFISSOTMP.$ora.".xml");
  80.                 unlink(PREFISSOTMP.$ora.".xml");
  81.             }
  82.         }
  83.     }
  84.    
  85.     public function controllaXML(){
  86.         $content = trim($this->stringaXML);
  87.         if (empty($content)) {
  88.             return false;
  89.         }
  90.    
  91.         if (stripos($content, '<!DOCTYPE html>') !== false) {
  92.             return false;
  93.         }
  94.  
  95.         libxml_use_internal_errors(true);
  96.         simplexml_load_string($content);
  97.         $errors = libxml_get_errors();          
  98.         libxml_clear_errors();  
  99.  
  100.         return empty($errors);
  101.     }
  102.    
  103.     public function getP7M(){
  104.         return $this->stringaP7M;
  105.     }
  106.            
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement