Advertisement
Grizly

class.attachment.file.php

Dec 10th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.05 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * This file was created to be used as an "include" in the remove_old_tickets.php mod.
  5.  */
  6.  
  7. class TDJ_Attachment_File {
  8.     public $uri, $str, $type, $ext;
  9.     function TDJ_Attachment_File($attachment) {
  10.         $this->uri = array ();
  11.         $this->str = '';
  12.        
  13.         $id = $attachment ['attach_id'];
  14.                                          // Make the folder if it doesn't exist.
  15.         $dir = URI_FOLDER;
  16.         if (! is_dir ( $dir )) {
  17.             error_log ( "Making dir: $dir" );
  18.             mkdir ( $dir );
  19.         }
  20.         //Locate osTickets attachment object for this id
  21.         $a = Attachment::lookup ( $id );
  22.         //Get the file object itself, which is also an osTicket object/db entity etc.
  23.         $f = $a->getFile ();
  24.        
  25.         // Route by attachment I wish I could remember where I found this majestic bitty.. ;-)
  26.         $this->ext = $ext = strtolower ( substr ( strrchr ( $f->getName (), '.' ), 1 ) );
  27.  
  28.         $filename = $dir . DIRECTORY_SEPARATOR . "t_$id.$ext";
  29.        
  30.         switch ($ext) {
  31.             case 'pdf' :
  32.                 {
  33.                     if (file_exists ( $filename ) != TRUE) {
  34.                        
  35.                         file_put_contents ( $filename, $f->getData () );
  36.                     }
  37.                     $this->uri = $filename;
  38.                     $this->type = 'pdf';
  39.                     break;
  40.                 }
  41.             case 'txt' :
  42.             case 'htm' :
  43.             case 'html' :
  44.                 {
  45.                     $this->type = 'text';
  46.                     $this->str = ($ext == 'txt') ? '<pre>' . $f->getData () . '</pre>' : Format::safe_html ( $f->getData () );
  47.                     // we don't set the URI here, as we are using the string value itself. No need to create a file.
  48.                     $this->uri = false;
  49.                     break;
  50.                 }
  51.             case 'png' :
  52.                 {
  53.                     if(!GD_ENABLED)
  54.                         break;
  55.                     //PDF doesn't support alpha channels for some reason, so we need to quickly and painlessly remove them.
  56.                     $tmp = tempnam('/tmp/osticket','_png_converter_');
  57.                     $jpeg_version = $this->png2jpg($filename, $tmp);
  58.                    
  59.                     //need to change its file-extension to match its new colorful format ;-)
  60.                     $this->ext = $ext = 'jpg';
  61.                     $filename = str_replace('.png', '.jpg', $filename);
  62.                     rename($tmp, $filename);
  63.                     //don't break, let the image section continue to work.
  64.                 }
  65.             case 'jpg' :
  66.             case 'gif' :
  67.             case 'jpeg' :
  68.             case 'tif' :
  69.                 {
  70.                     $this->type = 'image';
  71.                     if (file_exists ( $filename ) != TRUE) {
  72.                         file_put_contents ( $filename, $f->getData () );
  73.                     }
  74.                     $this->uri = $filename;
  75.                     break;
  76.                 }
  77.             case 'doc' :
  78.             case 'docx' : // stupid word docs
  79.             case 'xls' :
  80.             case 'xlsx' : // stupid excel docs
  81.                 {
  82.                     $this->type = 'text';
  83.                     if (file_exists ( $filename ) != TRUE) {
  84.                         file_put_contents ( $filename, $f->getData () );
  85.                     }
  86.                     // We don't really have time to format this shite, but we can at least grok some of the contents.. ;-)
  87.                     $docObj = new DocxConversion ( $filename );
  88.                     $this->str = $docObj->convertToText ();
  89.                     break;
  90.                 }
  91.             default :
  92.         }
  93.     }
  94.    
  95.     /**
  96.      * I'm starting to wonder how much is original code, and how much I just ripped off from StackOverflow!
  97.      *
  98.      * from: <a href="http://stackoverflow.com/a/1201823/276663">StackOverflow!</a>
  99.      * @param string $originalFile The full path to the PNG file.
  100.      * @param string $outputFile where does he go?
  101.      * @param int $quality (default should be 75 on imagejpeg anyway)
  102.      */
  103.     function png2jpg($originalFile, $outputFile, $quality=75) {
  104.             $image = imagecreatefrompng($originalFile);
  105.             imagejpeg($image, $outputFile, $quality);
  106.             imagedestroy($image);
  107.     }
  108. }
  109.  
  110.  
  111. /**
  112.  *
  113.  * Here is simple and easily understandable class which does the right job for .doc/.docx , PHP docx reader: Convert MS Word Docx files to text.
  114.  * http://www.phpclasses.org/package/7934-PHP-Convert-MS-Word-Docx-files-to-text.html
  115.  * http://stackoverflow.com/a/19503654
  116.  *
  117.  */
  118. class DocxConversion{
  119.     private $filename;
  120.  
  121.     public function __construct($filePath) {
  122.         $this->filename = $filePath;
  123.     }
  124.  
  125.     private function read_doc() {
  126.         $fileHandle = fopen($this->filename, "r");
  127.         $line = @fread($fileHandle, filesize($this->filename));
  128.         $lines = explode(chr(0x0D),$line);
  129.         $outtext = "";
  130.         foreach($lines as $thisline)
  131.         {
  132.             $pos = strpos($thisline, chr(0x00));
  133.             if (($pos !== FALSE)||(strlen($thisline)==0))
  134.             {
  135.             } else {
  136.                 $outtext .= $thisline." ";
  137.             }
  138.         }
  139.         $outtext = preg_replace("/[^a-zA-Z0-9\s\,\.\-\n\r\t@\/\_\(\)]/","",$outtext);
  140.         return $outtext;
  141.     }
  142.  
  143.     private function read_docx(){
  144.  
  145.         $striped_content = '';
  146.         $content = '';
  147.  
  148.         $zip = zip_open($this->filename);
  149.  
  150.         if (!$zip || is_numeric($zip)) return false;
  151.  
  152.         while ($zip_entry = zip_read($zip)) {
  153.  
  154.             if (zip_entry_open($zip, $zip_entry) == FALSE) continue;
  155.  
  156.             if (zip_entry_name($zip_entry) != "word/document.xml") continue;
  157.  
  158.             $content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
  159.  
  160.             zip_entry_close($zip_entry);
  161.         }// end while
  162.  
  163.         zip_close($zip);
  164.  
  165.         $content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content);
  166.         $content = str_replace('</w:r></w:p>', "\r\n", $content);
  167.         $striped_content = strip_tags($content);
  168.  
  169.         return $striped_content;
  170.     }
  171.  
  172.     /************************excel sheet************************************/
  173.  
  174.     function xlsx_to_text($input_file){
  175.         $xml_filename = "xl/sharedStrings.xml"; //content file name
  176.         $zip_handle = new ZipArchive;
  177.         $output_text = "";
  178.         if(true === $zip_handle->open($input_file)){
  179.             if(($xml_index = $zip_handle->locateName($xml_filename)) !== false){
  180.                 $xml_datas = $zip_handle->getFromIndex($xml_index);
  181.                 $xml_handle = DOMDocument::loadXML($xml_datas, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
  182.                 $output_text = strip_tags($xml_handle->saveXML());
  183.             }else{
  184.                 $output_text .="";
  185.             }
  186.             $zip_handle->close();
  187.         }else{
  188.             $output_text .="";
  189.         }
  190.         return $output_text;
  191.     }
  192.  
  193.     /*************************power point files*****************************/
  194.     function pptx_to_text($input_file){
  195.         $zip_handle = new ZipArchive;
  196.         $output_text = "";
  197.         if(true === $zip_handle->open($input_file)){
  198.             $slide_number = 1; //loop through slide files
  199.             while(($xml_index = $zip_handle->locateName("ppt/slides/slide".$slide_number.".xml")) !== false){
  200.                 $xml_datas = $zip_handle->getFromIndex($xml_index);
  201.                 $xml_handle = DOMDocument::loadXML($xml_datas, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
  202.                 $output_text .= strip_tags($xml_handle->saveXML());
  203.                 $slide_number++;
  204.             }
  205.             if($slide_number == 1){
  206.                 $output_text .="";
  207.             }
  208.             $zip_handle->close();
  209.         }else{
  210.             $output_text .="";
  211.         }
  212.         return $output_text;
  213.     }
  214.  
  215.  
  216.     public function convertToText() {
  217.  
  218.         if(isset($this->filename) && !file_exists($this->filename)) {
  219.             return "File Not exists";
  220.         }
  221.  
  222.         $fileArray = pathinfo($this->filename);
  223.         $file_ext  = $fileArray['extension'];
  224.         if($file_ext == "doc" || $file_ext == "docx" || $file_ext == "xlsx" || $file_ext == "pptx")
  225.         {
  226.             if($file_ext == "doc") {
  227.                 return $this->read_doc();
  228.             } elseif($file_ext == "docx") {
  229.                 return $this->read_docx();
  230.             } elseif($file_ext == "xlsx") {
  231.                 return $this->xlsx_to_text();
  232.             }elseif($file_ext == "pptx") {
  233.                 return $this->pptx_to_text();
  234.             }
  235.         } else {
  236.             return "Invalid File Type";
  237.         }
  238.     }
  239.  
  240. }
  241. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement