copy an existing message

@param   int                             $id     number of message
@param   string|Zend_Mail_Storage_Folder $folder name or instance of targer folder
@return  null
@throws  Zend_Mail_Storage_Exception
356423
public function copyMessage($id, $folder)
    {
        if ($this->_quota && $this->checkQuota()) {
            /**
             * @see Zend_Mail_Storage_Exception
             */
            // require_once 'Zend/Mail/Storage/Exception.php';
            throw new Zend_Mail_Storage_Exception('storage is over quota!');
        }

        if (!($folder instanceof Zend_Mail_Storage_Folder)) {
            $folder = $this->getFolders($folder);
        }

        $filedata = $this->_getFileData($id);
        $old_file = $filedata['filename'];
        $flags = $filedata['flags'];

        // copied message can't be recent
        while (($key = array_search(Zend_Mail_Storage::FLAG_RECENT, $flags)) !== false) {
            unset($flags[$key]);
        }
        $info = $this->_getInfoString($flags);

        // we're creating the copy as temp file before moving to cur/
        $temp_file = $this->_createTmpFile($folder->getGlobalName());
        // we don't write directly to the file
        fclose($temp_file['handle']);

        // we're adding the size to the filename for maildir++
        $size = filesize($old_file);
        if ($size !== false) {
            $info = ',S=' . $size . $info;
        }

        $new_file = $temp_file['dirname'] . DIRECTORY_SEPARATOR . 'cur' . DIRECTORY_SEPARATOR . $temp_file['uniq'] . $info;

        // we're throwing any exception after removing our temp file and saving it to this variable instead
        $exception = null;

        if (!copy($old_file, $temp_file['filename'])) {
            /**
             * @see Zend_Mail_Storage_Exception
             */
            // require_once 'Zend/Mail/Storage/Exception.php';
            $exception = new Zend_Mail_Storage_Exception('cannot copy message file');
        } else if (!link($temp_file['filename'], $new_file)) {
            /**
             * @see Zend_Mail_Storage_Exception
             */
            // require_once 'Zend/Mail/Storage/Exception.php';
            $exception = new Zend_Mail_Storage_Exception('cannot link message file to final dir');
        }
        @unlink($temp_file['filename']);

        if ($exception) {
            throw $exception;
        }

        if ($folder->getGlobalName() == $this->_currentFolder
            || ($this->_currentFolder == 'INBOX' && $folder->getGlobalName() == '/')) {
            $this->_files[] = array('uniq'     => $temp_file['uniq'],
                                    'flags'    => $flags,
                                    'filename' => $new_file);
        }

        if ($this->_quota) {
            $this->_addQuotaEntry((int)$size, 1);
        }
    }
490720
public function getMessage($type, $id = null)
    {
        $result = null;
        if (null === $id) {
            if (array_key_exists($type, $this->messages)) {
                $result = $this->messages[$type];

                if (!$this->persistMessages) {
                    unset($this->messages[$type]);
                }
            }
        }

        if (null !== $id) {
            if (array_key_exists($type, $this->messages)) {
                if (array_key_exists($id, $this->messages[$type])) {
                    $result = $this->messages[$type][$id];
                    if (!$this->persistMessages) {
                        unset($this->messages[$type][$id]);
                    }
                }
            }
        }

        return $result;
    }
582149
protected function _file($id)
    {
        $instance = self::getInstance();
        $fileNmae = $instance->_idToFileName($id);
        return self::cacheDir() . $fileNmae;
    }
611322
private function activateFolderById($id, FolderInterface $folder)
    {
        foreach ($folder->getChildren() as $child) {
            if ($child->getId() == $id) {
                $child->setActive(true);
                return true;
            }
            if ($this->activateFolderById($id, $child)) {
                return true;
            }
        }
        return false;
    }
328252
public function allocate($name, $folder = null)
    {
        return new Upload($this, isset($_FILES[$name])?$_FILES[$name]:array(), $folder);
    }
626699
private function setFile(string $id)
    {
        if (!$this->id || $id != $this->id) {
            $this->id = $id;
            $id = Check::name($id, ["#"]);
            $this->file = (preg_match("/^" . preg_quote(PATH_HOME, '/') . "/i", $id) ? $id : PATH_HOME . "_cdn/" . parent::getFolder() . "/{$id}");

            // Verifica se é final .json
            if (!preg_match("/\.json$/i", $id))
                $this->file .= ".json";
        }
    }
936084
public function copy(string $id, array $params = [])
    {
        return $this->executeRequest(
            RequestMethodEnum::POST,
            ResourceMethodEnum::COPY,
            $params,
            $id
        );
    }
384655
public function renameFolder(Folder $folder, $newTitle)
	{
		$entityManager = $this->getDoctrineEntityManager();
		$entityManager->beginTransaction();
		$oldFolder = clone($folder);
		$entityManager->detach($oldFolder);

		try {
			$folder->setFileName($newTitle);

			// validating folder before renaming
			foreach ($this->folderUploadFilters as $filter) {
				$filter->validateFolder($folder);
			}

			$entityManager->flush();

			// rename folder in both file storages
			$this->renameFolderInFileSystem($oldFolder, $folder);
		} catch (\Exception $e) {
			$entityManager->detach($folder);
			$entityManager->rollback();

			throw $e;
		}

		$entityManager->commit();
	}
681374
private function findFolderById($id)
    {
        $folder = $this->getFolderRepository()->find($id);
        if (null === $folder) {
            throw new NotFoundHttpException('Folder not found.');
        }
        return $folder;
    }
229997
protected function set_messagecount($folder, $mode, $increment)
    {
        if (!is_numeric($increment)) {
            return false;
        }

        $mode = strtoupper($mode);
        $a_folder_cache = $this->get_cache('messagecount');

        if (!is_array($a_folder_cache[$folder]) || !isset($a_folder_cache[$folder][$mode])) {
            return false;
        }

        // add incremental value to messagecount
        $a_folder_cache[$folder][$mode] += $increment;

        // there's something wrong, delete from cache
        if ($a_folder_cache[$folder][$mode] < 0) {
            unset($a_folder_cache[$folder][$mode]);
        }

        // write back to cache
        $this->update_cache('messagecount', $a_folder_cache);

        return true;
    }
725562
public function actionFolderUpdate($folderId)
    {
        $this->checkRouteAccess(self::PERMISSION_ROUTE);
        
        $model = StorageFolder::findOne($folderId);
        if (!$model) {
            return false;
        }
        $model->attributes = Yii::$app->request->post();
    
        $this->flushApiCache();
        
        return $model->update();
    }
362784
public function actionSend($id)
    {
        $message = Message::findOne($id);
        if ($message !== null) {
            try {
                $message->status = $this->send($message) > 0 ? Message::STATUS_SUCCESS : Message::STATUS_ERROR;
                $message->save(true, ['status']);
            } catch (\Exception $e) {
                $message->status = Message::STATUS_ERROR;
                $message->save(true, ['status']);
                $this->stderr($e->getMessage());
            }
        } else {
            $this->stdout("Message not found\n");
            exit(1);
        }
    }
650532
public function loadMail($id)
    {
        try {

            return $this->_storage->loadMail($id);

        } catch (\Exception $e) {

            throw new MailException(
                new Phrase($e->getMessage()),
                $e)
            ;

        }
    }
910556
public function getRawContent($id, $part = null)
    {
        if ($part !== null) {
            // TODO: implement
            /**
             * @see Zend_Mail_Storage_Exception
             */
            // require_once 'Zend/Mail/Storage/Exception.php';
            throw new Zend_Mail_Storage_Exception('not implemented');
        }

        $fh = fopen($this->_getFileData($id, 'filename'), 'r');

        while (!feof($fh)) {
            $line = fgets($fh);
            if (!trim($line)) {
                break;
            }
        }

        $content = stream_get_contents($fh);
        fclose($fh);
        return $content;
    }
1014566
public function cloneContent($id, $newId) {
    $path = $this->path . '/content/';
    if (file_exists($path . $id)) {
      self::copyFileTree($path . $id, $path . $newId);
    }
  }
902401
protected function clear_message_cache($folder = null, $uids = null)
    {
        if ($mcache = $this->get_mcache_engine()) {
            $mcache->clear($folder, $uids);
        }
    }
380881
public function populate($id)
    {
        if (empty($id)) {
            throw new EntityException('No messages id supplied', EntityException::NO_MESSAGE_ID_SUPPLIED);
        }

        $this->sendApiCall(self::ACTION_RETRIEVE, $this->getCreateUrl() . '/' . $id);
    }