コード例 #1
0
ファイル: class.EmlParser.php プロジェクト: biggtfish/cms
 public function switchAction($action, $httpVars, $filesVars)
 {
     if (!isset($this->actions[$action])) {
         return false;
     }
     $repository = ConfService::getRepository();
     if (!$repository->detectStreamWrapper(true)) {
         return false;
     }
     $streamData = $repository->streamData;
     $destStreamURL = $streamData["protocol"] . "://" . $repository->getId();
     $wrapperClassName = $streamData["classname"];
     if (empty($httpVars["file"])) {
         return;
     }
     $file = $destStreamURL . AJXP_Utils::decodeSecureMagic($httpVars["file"]);
     $mess = ConfService::getMessages();
     $node = new AJXP_Node($file);
     AJXP_Controller::applyHook("node.read", array($node));
     switch ($action) {
         case "eml_get_xml_structure":
             $params = array('include_bodies' => false, 'decode_bodies' => false, 'decode_headers' => 'UTF-8');
             $decoder = $this->getStructureDecoder($file, $wrapperClassName == "imapAccessWrapper");
             $xml = $decoder->getXML($decoder->decode($params));
             if (function_exists("imap_mime_header_decode")) {
                 $doc = new DOMDocument();
                 $doc->loadXML($xml);
                 $xPath = new DOMXPath($doc);
                 $headers = $xPath->query("//headername");
                 $changes = false;
                 foreach ($headers as $headerNode) {
                     if ($headerNode->firstChild->nodeValue == "Subject") {
                         $headerValueNode = $headerNode->nextSibling->nextSibling;
                         $value = $headerValueNode->nodeValue;
                         $elements = imap_mime_header_decode($value);
                         $decoded = "";
                         foreach ($elements as $element) {
                             $decoded .= $element->text;
                             $charset = $element->charset;
                         }
                         if ($decoded != $value) {
                             $value = SystemTextEncoding::changeCharset($charset, "UTF-8", $decoded);
                             $node = $doc->createElement("headervalue", $value);
                             $res = $headerNode->parentNode->replaceChild($node, $headerValueNode);
                             $changes = true;
                         }
                     }
                 }
                 if ($changes) {
                     $xml = $doc->saveXML();
                 }
             }
             print $xml;
             break;
         case "eml_get_bodies":
             require_once "Mail/mimeDecode.php";
             $params = array('include_bodies' => true, 'decode_bodies' => true, 'decode_headers' => false);
             if ($wrapperClassName == "imapAccessWrapper") {
                 $cache = AJXP_Cache::getItem("eml_remote", $file, null, array("EmlParser", "computeCacheId"));
                 $content = $cache->getData();
             } else {
                 $content = file_get_contents($file);
             }
             $decoder = new Mail_mimeDecode($content);
             $structure = $decoder->decode($params);
             $html = $this->_findPartByCType($structure, "text", "html");
             $text = $this->_findPartByCType($structure, "text", "plain");
             if ($html != false && isset($html->ctype_parameters) && isset($html->ctype_parameters["charset"])) {
                 $charset = $html->ctype_parameters["charset"];
             }
             if (isset($charset)) {
                 header('Content-Type: text/xml; charset=' . $charset);
                 header('Cache-Control: no-cache');
                 print '<?xml version="1.0" encoding="' . $charset . '"?>';
                 print '<email_body>';
             } else {
                 AJXP_XMLWriter::header("email_body");
             }
             if ($html !== false) {
                 print '<mimepart type="html"><![CDATA[';
                 $text = $html->body;
                 print $text;
                 print "]]></mimepart>";
             }
             if ($text !== false) {
                 print '<mimepart type="plain"><![CDATA[';
                 print $text->body;
                 print "]]></mimepart>";
             }
             AJXP_XMLWriter::close("email_body");
             break;
         case "eml_dl_attachment":
             $attachId = $httpVars["attachment_id"];
             if (!isset($attachId)) {
                 break;
             }
             require_once "Mail/mimeDecode.php";
             $params = array('include_bodies' => true, 'decode_bodies' => true, 'decode_headers' => false);
             if ($wrapperClassName == "imapAccessWrapper") {
                 $cache = AJXP_Cache::getItem("eml_remote", $file, null, array("EmlParser", "computeCacheId"));
                 $content = $cache->getData();
             } else {
                 $content = file_get_contents($file);
             }
             $decoder = new Mail_mimeDecode($content);
             $structure = $decoder->decode($params);
             $part = $this->_findAttachmentById($structure, $attachId);
             if ($part !== false) {
                 $fake = new fsAccessDriver("fake", "");
                 $fake->readFile($part->body, "file", $part->d_parameters['filename'], true);
                 exit;
             } else {
                 //var_dump($structure);
             }
             break;
         case "eml_cp_attachment":
             $attachId = $httpVars["attachment_id"];
             $destRep = AJXP_Utils::decodeSecureMagic($httpVars["destination"]);
             if (!isset($attachId)) {
                 AJXP_XMLWriter::sendMessage(null, "Wrong Parameters");
                 break;
             }
             require_once "Mail/mimeDecode.php";
             $params = array('include_bodies' => true, 'decode_bodies' => true, 'decode_headers' => false);
             if ($wrapperClassName == "imapAccessWrapper") {
                 $cache = AJXP_Cache::getItem("eml_remote", $file, null, array("EmlParser", "computeCacheId"));
                 $content = $cache->getData();
             } else {
                 $content = file_get_contents($file);
             }
             $decoder = new Mail_mimeDecode($content);
             $structure = $decoder->decode($params);
             $part = $this->_findAttachmentById($structure, $attachId);
             AJXP_XMLWriter::header();
             if ($part !== false) {
                 if (isset($httpVars["dest_repository_id"])) {
                     $destRepoId = $httpVars["dest_repository_id"];
                     if (AuthService::usersEnabled()) {
                         $loggedUser = AuthService::getLoggedUser();
                         if (!$loggedUser->canWrite($destRepoId)) {
                             throw new Exception($mess[364]);
                         }
                     }
                     $destRepoObject = ConfService::getRepositoryById($destRepoId);
                     $destRepoAccess = $destRepoObject->getAccessType();
                     $plugin = AJXP_PluginsService::findPlugin("access", $destRepoAccess);
                     $destWrapperData = $plugin->detectStreamWrapper(true);
                     $destStreamURL = $destWrapperData["protocol"] . "://{$destRepoId}";
                 }
                 $destFile = $destStreamURL . $destRep . "/" . $part->d_parameters['filename'];
                 $fp = fopen($destFile, "w");
                 if ($fp !== false) {
                     fwrite($fp, $part->body, strlen($part->body));
                     fclose($fp);
                     AJXP_XMLWriter::sendMessage(sprintf($mess["editor.eml.7"], $part->d_parameters["filename"], $destRep), NULL);
                 } else {
                     AJXP_XMLWriter::sendMessage(null, $mess["editor.eml.8"]);
                 }
             } else {
                 AJXP_XMLWriter::sendMessage(null, $mess["editor.eml.9"]);
             }
             AJXP_XMLWriter::close();
             break;
         default:
             break;
     }
 }
コード例 #2
0
 function toUTF8($filesystemElement)
 {
     $enc = SystemTextEncoding::getEncoding();
     return SystemTextEncoding::changeCharset($enc, "UTF-8", $filesystemElement);
 }
コード例 #3
0
 /**
  * Decode a string from UTF8 to current Storage Charset
  * @static
  * @param string $filesystemElement
  * @param bool $test Try to detect if it's really utf8 or not
  * @return string
  */
 public static function toStorageEncoding($filesystemElement, $test = false)
 {
     if ($test && !SystemTextEncoding::isUtf8($filesystemElement)) {
         return $filesystemElement;
     }
     $enc = SystemTextEncoding::getEncoding();
     return SystemTextEncoding::changeCharset("UTF-8", $enc, $filesystemElement);
 }
コード例 #4
0
 /**
  * Transform a string from current charset to utf8
  * @static
  * @param string $filesystemElement
  * @param bool $test Test if it's already UTF8 or not, to avoid double-encoding
  * @return string
  */
 static function toUTF8($filesystemElement, $test = true)
 {
     if ($test && SystemTextEncoding::isUtf8($filesystemElement)) {
         return $filesystemElement;
     }
     $enc = SystemTextEncoding::getEncoding();
     return SystemTextEncoding::changeCharset($enc, "UTF-8", $filesystemElement);
 }