<?php

defined('_JEXEC') or die('Restricted access');
$jAp = JFactory::getApplication();
$AsposePdfImporterInput = $jAp->input;
$base_url = JURI::base();
$module_path = dirname(__FILE__);
// Get current document object
$document = JFactory::getDocument();
// Add stylesheets and javascripts specific to module
$AsposeParams = "var insert_pdf_url = '" . $base_url . "index.php?option=com_ajax&module=aspose_pdf_importer&format=json';\nvar uploadpath = '';\n";
$document->addScriptDeclaration($AsposeParams);
$document->addStyleSheet($base_url . 'modules/mod_aspose_pdf_importer/tmpl/css/jquery-ui.css');
$document->addScript($base_url . 'modules/mod_aspose_pdf_importer/tmpl/js/jquery.form.js');
$document->addScript($base_url . 'modules/mod_aspose_pdf_importer/tmpl/js/jquery-ui.js');
$document->addScript($base_url . 'modules/mod_aspose_pdf_importer/tmpl/js/pdf_importer.js');
require_once $module_path . '/helper.php';
$query_string_option = $AsposePdfImporterInput->get('option');
$query_string_view = $AsposePdfImporterInput->get('view');
$query_string_layout = $AsposePdfImporterInput->get('layout');
if ($query_string_option == 'com_content' && $query_string_view == 'article' && $query_string_layout == 'edit') {
    // Include the syndicate functions only once
    $pdf_files_uploaded = ModAsposePdfImporterHelper::getAsposeFiles();
    require JModuleHelper::getLayoutPath('mod_aspose_pdf_importer', $params->get('layout', 'default'));
}
 public static function getAjax()
 {
     $module_path = dirname(__FILE__);
     JLoader::registerNamespace('Aspose', $module_path . '/Aspose_Cloud_SDK_For_PHP-master/src');
     $jAp = JFactory::getApplication();
     $myJoomlaInput = $jAp->input;
     // Get Module Configurations
     $module_params = static::getParams();
     // Set Application ID and Application Key for Aspose SDK
     $app_id = $module_params->get('pdf_importer_app_sid', false);
     $app_key = $module_params->get('pdf_importer_app_key', false);
     $aspose_pdf_importer_option = $module_params->get('aspose_pdf_importer_option', false);
     if (!$app_id || !$app_key || empty($app_id) || empty($app_key)) {
         die("APP Id/ APP Key for Aspose SDK not found");
     }
     \Aspose\Cloud\Common\AsposeApp::$appSID = $app_id;
     \Aspose\Cloud\Common\AsposeApp::$appKey = $app_key;
     /*
      * Assign Base Product URL
      */
     \Aspose\Cloud\Common\Product::$baseProductUri = 'http://api.aspose.com/v1.1';
     $uploadpath = JPATH_ADMINISTRATOR . "/" . "modules" . "/" . "mod_aspose_pdf_importer" . "/" . "uploads/";
     \Aspose\Cloud\Common\AsposeApp::$outPutLocation = $uploadpath;
     $file_received = $myJoomlaInput->files->get('filePath');
     if (is_array($file_received) && count($file_received) > 0) {
         jimport('joomla.filesystem.file');
         //Clean up filename to get rid of strange characters like spaces etc
         $uploadfilename = time() . '_' . JFile::makeSafe($file_received['name']);
         //Set up the source and destination of the file
         $src = $file_received['tmp_name'];
         $dest = JPATH_ADMINISTRATOR . "/" . "modules" . "/" . "mod_aspose_pdf_importer" . "/" . "uploads" . "/" . $uploadfilename;
         //First check if the file has the right extension, we need pdf only
         if (strtolower(JFile::getExt($uploadfilename)) == 'pdf') {
             if (JFile::upload($src, $dest)) {
                 $folder = new \Aspose\Cloud\Storage\Folder();
                 $uploadpath = str_replace("\\", "/", $dest);
                 $uploadFile = $dest;
                 $folder->uploadFile($uploadFile, '');
             } else {
                 die("There is some problem in uploading File.");
             }
         } else {
             die("Wrong File was selected!");
         }
     }
     if (isset($_REQUEST['filename']) && !empty($_REQUEST['filename'])) {
         $filename = $_REQUEST['filename'];
     } else {
         $filename = $uploadfilename;
     }
     $ext = pathinfo($filename, PATHINFO_EXTENSION);
     $ext = strtolower($ext);
     if ($ext == 'pdf') {
         \Aspose\Cloud\Common\AsposeApp::$outPutLocation = $uploadpath;
         // 'F:\\xampp\htdocs\\wordpress\\uploads\\';
         $filename = trim($filename);
         if ($aspose_pdf_importer_option == 'html') {
             $converter = new \Aspose\Cloud\Pdf\Converter($filename);
             $converter->saveFormat = 'html';
             $converter->fileName = $filename;
             $saved_file = $converter->convert();
             $targetdir_arr = pathinfo($saved_file);
             ModAsposePdfImporterHelper::unzip($targetdir_arr['dirname'] . '/' . str_replace('.zip', '', $targetdir_arr['basename']), $targetdir_arr['dirname'] . '/' . $targetdir_arr['basename']);
             $file_name = $targetdir_arr['dirname'] . '/' . str_replace('.zip', '', $targetdir_arr['basename']) . '/' . \Aspose\Cloud\Common\Utils::getFileName($filename) . '.html';
             $file_name = str_replace("\\", "/", $file_name);
             $file_html = file_get_contents($file_name);
             $file_html_folder = \Aspose\Cloud\Common\Utils::getFileName($filename) . '_files';
             $destination = JURI::base() . "modules" . "/" . "mod_aspose_pdf_importer" . "/" . "uploads/";
             $file_html = str_replace($file_html_folder, $destination . '/' . \Aspose\Cloud\Common\Utils::getFileName($filename) . '/' . $file_html_folder, $file_html);
             $css_file = @file_get_contents($uploadpath . '/' . \Aspose\Cloud\Common\Utils::getFileName($filename) . '/' . $file_html_folder . '/style.css');
             $css_array = ModAsposePdfImporterHelper::parse($css_file);
             preg_match_all('/class="([^"]+)"/i', $file_html, $matches);
             foreach ($matches[1] as $key => $class_name) {
                 $classes_arr = explode(' ', $class_name);
                 $style_text = '';
                 if (is_array($classes_arr) && count($classes_arr) > 0) {
                     foreach ($classes_arr as $c) {
                         $key_index = @$css_array['.' . $c];
                         if (@is_array($css_array['.' . $c])) {
                             foreach ($css_array['.' . $c] as $style_key => $style_value) {
                                 if ($style_key != '' && $style_value != '' && $style_key != 'font-family') {
                                     $style_text .= $style_key . ':' . $style_value . ';';
                                 }
                             }
                         }
                     }
                 }
                 if ($style_text != '') {
                     $replace_string = 'style="' . $style_text . '"';
                     $c_n = 'class="' . $class_name . '"';
                     $file_html = str_replace($c_n, $replace_string, $file_html);
                 }
             }
             preg_match_all('/style="([^"]+)" style="([^"]+)"/', $file_html, $match_arr);
             foreach ($match_arr['0'] as $key => $m) {
                 $file_html = str_replace($m, 'style="' . $match_arr['1'][$key] . $match_arr['2'][$key] . '"', $file_html);
             }
             $content = $file_html;
         } else {
             $func = new \Aspose\Cloud\Pdf\TextEditor($filename);
             $output = $func->getText();
             $output_arr = explode('.', $output);
             $content = '';
             foreach ($output_arr as $output) {
                 $content .= '<p>' . $output . '</p>';
             }
         }
         die($content);
     } else {
         die("Wrong File was selected!");
     }
 }