<commit_before><?php
/*
 * Copyright 2007-2013 <NAME> - Abstrium SAS <team (at) pyd.io>
 * This file is part of Pydio.
 *
 * Pydio is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Pydio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Pydio.  If not, see <http://www.gnu.org/licenses/>.
 *
 * The latest code can be found at <https://pydio.com>.
 */
namespace Pydio\Access\Driver\StreamProvider\Imap;

use EmlParser;
use Pydio\Access\Core\IAjxpWrapper;
use Pydio\Access\Core\Model\AJXP_Node;

use Pydio\Core\Utils\Vars\UrlUtils;
use Pydio\Core\Utils\TextEncoder;
use Pydio\Log\Core\Logger;

defined('AJXP_EXEC') or die( 'Access not allowed');

function rejectEmpty($element){return !empty($element);}

/**
 * Plugin to browse a mailbox content (IMAP OR POP)
 * @package AjaXplorer_Plugins
 * @subpackage Access
 */
class ImapAccessWrapper implements IAjxpWrapper
{
    public $ih;
    public $host;
    public $port;
    public $username;
    public $password;
    public $path;
    public $pop3;
    // stuff for dir reading
    public $dir;
    public $pos;
    // stuff for file reading
    public $data;
    public $gotbody;
    public $size;
    public $time;

    public $fragment;
    public $mailbox;

    public $mailboxes;
    public $currentAttachmentData;

    protected $repositoryId;

    private static $currentStream;
    private static $currentRef;
    private static $currentCount;
    private static $delimiter;
    private static $attachmentsMetadata;

    public static function closeStreamFunc()
    {
        if (self::$currentStream) {
            Logger::debug("Closing stream now!");
            imap_close(self::$currentStream);
        }
    }

    public static function getCurrentDirCount()
    {
        return self::$currentCount;
    }

    public static function getCurrentAttachmentsMetadata()
    {
        return self::$attachmentsMetadata;
    }

    public function stream_open($path, $mode, $options, &$opened_path)
    {
        // parse URL
        $node = new AJXP_Node($path);
        $parts = UrlUtils::safeParseUrl($path);
        $this->repositoryId = $node->getRepositoryId();

        $mainCacheDir = (defined('AJXP_SHARED_CACHE_DIR')?AJXP_SHARED_CACHE_DIR:AJXP_CACHE_DIR);
        if (!isset(self::$delimiter) && file_exists($mainCacheDir."/access.imap/mailbox_delim_".$this->repositoryId)) {
            self::$delimiter = file_get_contents($mainCacheDir."/access.imap/mailbox_delim_".$this->repositoryId);
        }

        $this->path = substr($parts["path"], 1);
        //$this->mailbox = "INBOX";
        $pathParts = explode("/", $this->path);
        $pathParts = array_filter($pathParts, "rejectEmpty");
        if (count($pathParts) > 1) {
            $this->path = array_pop($pathParts);
            $this->mailbox = implode("/", $pathParts);
        } else if (count($pathParts) == 1) {
            $this->mailbox = implode("/", $pathParts);
            $this->path = "";
        } else {
            $this->mailbox = "";
            $this->path = "";
        }
        $this->fragment = $parts["fragment"];
        if (empty ( $this->path ) && $mode !== 'np') {
            return false;
        }
        if (!empty($this->mailbox)) {
            $this->mailbox = mb_convert_encoding($this->mailbox, "UTF7-IMAP", TextEncoder::getEncoding());
            $this->mailbox = str_replace("__delim__", (isSet(self::$delimiter)?self::$delimiter:"/"), $this->mailbox);
        }
        if (!empty($this->fragment) && strpos($this->fragment, "attachments") === 0 && strpos($this->fragment, "/")!== false) {
            // remove fragment
            $ar = explode("#", $path);
            $mailPath = array_shift($ar);
            $ar = explode("/", $this->fragment);
            $attachmentId = array_pop($ar);
            $this->currentAttachmentData = array("realPath" => $mailPath, "attachmentId" => $attachmentId);
            // EXTRACT ATTACHMENT AND RETURN
            require_once AJXP_INSTALL_PATH."/plugins/editor.eml/class.EmlParser.php";
            $emlParser = new EmlParser("", "");<commit_msg>Fix reject empty call<commit_after><?php
/*
 * Copyright 2007-2013 <NAME> - Abstrium SAS <team (at) pyd.io>
 * This file is part of Pydio.
 *
 * Pydio is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Pydio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Pydio.  If not, see <http://www.gnu.org/licenses/>.
 *
 * The latest code can be found at <https://pydio.com>.
 */
namespace Pydio\Access\Driver\StreamProvider\Imap;

use EmlParser;
use Pydio\Access\Core\IAjxpWrapper;
use Pydio\Access\Core\Model\AJXP_Node;

use Pydio\Core\Utils\Vars\UrlUtils;
use Pydio\Core\Utils\TextEncoder;
use Pydio\Log\Core\Logger;

defined('AJXP_EXEC') or die( 'Access not allowed');

/**
 * Plugin to browse a mailbox content (IMAP OR POP)
 * @package AjaXplorer_Plugins
 * @subpackage Access
 */
class ImapAccessWrapper implements IAjxpWrapper
{
    public $ih;
    public $host;
    public $port;
    public $username;
    public $password;
    public $path;
    public $pop3;
    // stuff for dir reading
    public $dir;
    public $pos;
    // stuff for file reading
    public $data;
    public $gotbody;
    public $size;
    public $time;

    public $fragment;
    public $mailbox;

    public $mailboxes;
    public $currentAttachmentData;

    protected $repositoryId;

    private static $currentStream;
    private static $currentRef;
    private static $currentCount;
    private static $delimiter;
    private static $attachmentsMetadata;

    public static function closeStreamFunc()
    {
        if (self::$currentStream) {
            Logger::debug("Closing stream now!");
            imap_close(self::$currentStream);
        }
    }

    public static function getCurrentDirCount()
    {
        return self::$currentCount;
    }

    public static function getCurrentAttachmentsMetadata()
    {
        return self::$attachmentsMetadata;
    }

    private function rejectEmpty($element){
        return !empty($element);
    }



    public function stream_open($path, $mode, $options, &$opened_path)
    {
        // parse URL
        $node = new AJXP_Node($path);
        $parts = UrlUtils::safeParseUrl($path);
        $this->repositoryId = $node->getRepositoryId();

        $mainCacheDir = (defined('AJXP_SHARED_CACHE_DIR')?AJXP_SHARED_CACHE_DIR:AJXP_CACHE_DIR);
        if (!isset(self::$delimiter) && file_exists($mainCacheDir."/access.imap/mailbox_delim_".$this->repositoryId)) {
            self::$delimiter = file_get_contents($mainCacheDir."/access.imap/mailbox_delim_".$this->repositoryId);
        }

        $this->path = substr($parts["path"], 1);
        //$this->mailbox = "INBOX";
        $pathParts = explode("/", $this->path);
        $pathParts = array_filter($pathParts, [$this, "rejectEmpty"]);
        if (count($pathParts) > 1) {
            $this->path = array_pop($pathParts);
            $this->mailbox = implode("/", $pathParts);
        } else if (count($pathParts) == 1) {
            $this->mailbox = implode("/", $pathParts);
            $this->path = "";
        } else {
            $this->mailbox = "";
            $this->path = "";
        }
        $this->fragment = $parts["fragment"];
        if (empty ( $this->path ) && $mode !== 'np') {
            return false;
        }
        if (!empty($this->mailbox)) {
            $this->mailbox = mb_convert_encoding($this->mailbox, "UTF7-IMAP", TextEncoder::getEncoding());
            $this->mailbox = str_replace("__delim__", (isSet(self::$delimiter)?self::$delimiter:"/"), $this->mailbox);
        }
        if (!empty($this->fragment) && strpos($this->fragment, "attachments") === 0 && strpos($this->fragment, "/")!== false) {
            // remove fragment
            $ar = explode("#", $path);
            $mailPath = array_shift($ar);
            $ar = explode("/", $this->fragment);
            $attachmentId = array_pop($ar);
            $this->currentAttachmentData = array("realPath" => $mailPath, "attachmentId" => $attachmentId);
            // EXTRACT ATTACHMENT AND RETURN
            require_once AJXP_INSTALL_PATH."/plugins/editor.eml/class.EmlParser.php";
            $emlParser = new EmlParser("", "");
{'fraction_non_alphanumeric': 0.13630893775298109, 'fraction_numerical': 0.0032819166393173614, 'mean_word_length': 2.8283082077051924, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 67, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6433445', 'n_tokens_mistral': 2779, 'n_tokens_neox': 2702, 'n_words': 700}