Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * This file was created to be used as an "include" in the remove_old_tickets.php mod.
- */
- class TDJ_Attachment_File {
- public $uri, $str, $type, $ext;
- function TDJ_Attachment_File($attachment) {
- $this->uri = array ();
- $this->str = '';
- $id = $attachment ['attach_id'];
- // Make the folder if it doesn't exist.
- $dir = URI_FOLDER;
- if (! is_dir ( $dir )) {
- error_log ( "Making dir: $dir" );
- mkdir ( $dir );
- }
- //Locate osTickets attachment object for this id
- $a = Attachment::lookup ( $id );
- //Get the file object itself, which is also an osTicket object/db entity etc.
- $f = $a->getFile ();
- // Route by attachment I wish I could remember where I found this majestic bitty.. ;-)
- $this->ext = $ext = strtolower ( substr ( strrchr ( $f->getName (), '.' ), 1 ) );
- $filename = $dir . DIRECTORY_SEPARATOR . "t_$id.$ext";
- switch ($ext) {
- case 'pdf' :
- {
- if (file_exists ( $filename ) != TRUE) {
- file_put_contents ( $filename, $f->getData () );
- }
- $this->uri = $filename;
- $this->type = 'pdf';
- break;
- }
- case 'txt' :
- case 'htm' :
- case 'html' :
- {
- $this->type = 'text';
- $this->str = ($ext == 'txt') ? '<pre>' . $f->getData () . '</pre>' : Format::safe_html ( $f->getData () );
- // we don't set the URI here, as we are using the string value itself. No need to create a file.
- $this->uri = false;
- break;
- }
- case 'png' :
- {
- if(!GD_ENABLED)
- break;
- //PDF doesn't support alpha channels for some reason, so we need to quickly and painlessly remove them.
- $tmp = tempnam('/tmp/osticket','_png_converter_');
- $jpeg_version = $this->png2jpg($filename, $tmp);
- //need to change its file-extension to match its new colorful format ;-)
- $this->ext = $ext = 'jpg';
- $filename = str_replace('.png', '.jpg', $filename);
- rename($tmp, $filename);
- //don't break, let the image section continue to work.
- }
- case 'jpg' :
- case 'gif' :
- case 'jpeg' :
- case 'tif' :
- {
- $this->type = 'image';
- if (file_exists ( $filename ) != TRUE) {
- file_put_contents ( $filename, $f->getData () );
- }
- $this->uri = $filename;
- break;
- }
- case 'doc' :
- case 'docx' : // stupid word docs
- case 'xls' :
- case 'xlsx' : // stupid excel docs
- {
- $this->type = 'text';
- if (file_exists ( $filename ) != TRUE) {
- file_put_contents ( $filename, $f->getData () );
- }
- // We don't really have time to format this shite, but we can at least grok some of the contents.. ;-)
- $docObj = new DocxConversion ( $filename );
- $this->str = $docObj->convertToText ();
- break;
- }
- default :
- }
- }
- /**
- * I'm starting to wonder how much is original code, and how much I just ripped off from StackOverflow!
- *
- * from: <a href="http://stackoverflow.com/a/1201823/276663">StackOverflow!</a>
- * @param string $originalFile The full path to the PNG file.
- * @param string $outputFile where does he go?
- * @param int $quality (default should be 75 on imagejpeg anyway)
- */
- function png2jpg($originalFile, $outputFile, $quality=75) {
- $image = imagecreatefrompng($originalFile);
- imagejpeg($image, $outputFile, $quality);
- imagedestroy($image);
- }
- }
- /**
- *
- * 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.
- * http://www.phpclasses.org/package/7934-PHP-Convert-MS-Word-Docx-files-to-text.html
- * http://stackoverflow.com/a/19503654
- *
- */
- class DocxConversion{
- private $filename;
- public function __construct($filePath) {
- $this->filename = $filePath;
- }
- private function read_doc() {
- $fileHandle = fopen($this->filename, "r");
- $line = @fread($fileHandle, filesize($this->filename));
- $lines = explode(chr(0x0D),$line);
- $outtext = "";
- foreach($lines as $thisline)
- {
- $pos = strpos($thisline, chr(0x00));
- if (($pos !== FALSE)||(strlen($thisline)==0))
- {
- } else {
- $outtext .= $thisline." ";
- }
- }
- $outtext = preg_replace("/[^a-zA-Z0-9\s\,\.\-\n\r\t@\/\_\(\)]/","",$outtext);
- return $outtext;
- }
- private function read_docx(){
- $striped_content = '';
- $content = '';
- $zip = zip_open($this->filename);
- if (!$zip || is_numeric($zip)) return false;
- while ($zip_entry = zip_read($zip)) {
- if (zip_entry_open($zip, $zip_entry) == FALSE) continue;
- if (zip_entry_name($zip_entry) != "word/document.xml") continue;
- $content .= zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
- zip_entry_close($zip_entry);
- }// end while
- zip_close($zip);
- $content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content);
- $content = str_replace('</w:r></w:p>', "\r\n", $content);
- $striped_content = strip_tags($content);
- return $striped_content;
- }
- /************************excel sheet************************************/
- function xlsx_to_text($input_file){
- $xml_filename = "xl/sharedStrings.xml"; //content file name
- $zip_handle = new ZipArchive;
- $output_text = "";
- if(true === $zip_handle->open($input_file)){
- if(($xml_index = $zip_handle->locateName($xml_filename)) !== false){
- $xml_datas = $zip_handle->getFromIndex($xml_index);
- $xml_handle = DOMDocument::loadXML($xml_datas, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
- $output_text = strip_tags($xml_handle->saveXML());
- }else{
- $output_text .="";
- }
- $zip_handle->close();
- }else{
- $output_text .="";
- }
- return $output_text;
- }
- /*************************power point files*****************************/
- function pptx_to_text($input_file){
- $zip_handle = new ZipArchive;
- $output_text = "";
- if(true === $zip_handle->open($input_file)){
- $slide_number = 1; //loop through slide files
- while(($xml_index = $zip_handle->locateName("ppt/slides/slide".$slide_number.".xml")) !== false){
- $xml_datas = $zip_handle->getFromIndex($xml_index);
- $xml_handle = DOMDocument::loadXML($xml_datas, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
- $output_text .= strip_tags($xml_handle->saveXML());
- $slide_number++;
- }
- if($slide_number == 1){
- $output_text .="";
- }
- $zip_handle->close();
- }else{
- $output_text .="";
- }
- return $output_text;
- }
- public function convertToText() {
- if(isset($this->filename) && !file_exists($this->filename)) {
- return "File Not exists";
- }
- $fileArray = pathinfo($this->filename);
- $file_ext = $fileArray['extension'];
- if($file_ext == "doc" || $file_ext == "docx" || $file_ext == "xlsx" || $file_ext == "pptx")
- {
- if($file_ext == "doc") {
- return $this->read_doc();
- } elseif($file_ext == "docx") {
- return $this->read_docx();
- } elseif($file_ext == "xlsx") {
- return $this->xlsx_to_text();
- }elseif($file_ext == "pptx") {
- return $this->pptx_to_text();
- }
- } else {
- return "Invalid File Type";
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement