Page MenuHomePhorge

D4989.id14286.diff
No OneTemporary

D4989.id14286.diff

diff --git a/src/app/Backends/IMAP.php b/src/app/Backends/IMAP.php
--- a/src/app/Backends/IMAP.php
+++ b/src/app/Backends/IMAP.php
@@ -420,6 +420,55 @@
return true;
}
+ /**
+ * Rename a folder.
+ *
+ * @param string $sourceMailbox Source Mailbox
+ * @param string $targetMailbox Target Mailbox
+ *
+ * @return bool True if the mailbox was renamed successfully, False otherwise
+ * @throws \Exception
+ */
+ public static function renameMailbox($sourceMailbox, $targetMailbox): bool
+ {
+ $config = self::getConfig();
+ $imap = self::initIMAP($config);
+
+ $sourceMailbox = self::toUTF7($sourceMailbox);
+ $targetMailbox = self::toUTF7($targetMailbox);
+
+ // Rename the mailbox (only possible if we have the old folder)
+ if (!empty($targetMailbox) && $targetMailbox != $sourceMailbox) {
+ if (!$imap->renameFolder($sourceMailbox, $targetMailbox)) {
+ \Log::error("Failed to rename mailbox {$sourceMailbox} to {$targetMailbox}");
+ $imap->closeConnection();
+ return false;
+ }
+ }
+
+ $imap->closeConnection();
+
+ return true;
+ }
+
+ public static function userMailbox(string $user, string $mailbox): string
+ {
+ [$localpart, $domain] = explode('@', $user, 2);
+ return "user/{$localpart}/{$mailbox}@{$domain}";
+ }
+
+ public static function listMailboxes(string $user): array
+ {
+ $config = self::getConfig();
+ $imap = self::initIMAP($config);
+
+ $result = $imap->listMailboxes('', self::userMailbox($user, "*"));
+
+ $imap->closeConnection();
+
+ return $result;
+ }
+
/**
* Convert UTF8 string to UTF7-IMAP encoding
*/
diff --git a/src/app/Console/Commands/Imap/ListCommand.php b/src/app/Console/Commands/Imap/ListCommand.php
new file mode 100644
--- /dev/null
+++ b/src/app/Console/Commands/Imap/ListCommand.php
@@ -0,0 +1,35 @@
+<?php
+
+namespace App\Console\Commands\Imap;
+
+use App\Console\Command;
+
+class ListCommand extends Command
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'imap:list {user}';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = "List IMAP Folders";
+
+ /**
+ * Execute the console command.
+ *
+ * @return mixed
+ */
+ public function handle()
+ {
+ $user = $this->argument('user');
+ foreach (\App\Backends\IMAP::listMailboxes($user) as $mailbox) {
+ $this->info("$mailbox");
+ }
+ }
+}
diff --git a/src/app/Console/Commands/Imap/RenameCommand.php b/src/app/Console/Commands/Imap/RenameCommand.php
new file mode 100644
--- /dev/null
+++ b/src/app/Console/Commands/Imap/RenameCommand.php
@@ -0,0 +1,39 @@
+<?php
+
+namespace App\Console\Commands\Imap;
+
+use App\Console\Command;
+
+class RenameCommand extends Command
+{
+ /**
+ * The name and signature of the console command.
+ *
+ * @var string
+ */
+ protected $signature = 'imap:rename {user} {sourceMailbox} {targetMailbox}';
+
+ /**
+ * The console command description.
+ *
+ * @var string
+ */
+ protected $description = "Rename IMAP Folder";
+
+ /**
+ * Execute the console command.
+ *
+ * @return mixed
+ */
+ public function handle()
+ {
+ $user = $this->argument('user');
+ $sourceMailbox = $this->argument('sourceMailbox');
+ $targetMailbox = $this->argument('targetMailbox');
+
+ \App\Backends\IMAP::renameMailbox(
+ self::userMailbox($user, $sourceMailbox),
+ self::userMailbox($user, $targetMailbox)
+ );
+ }
+}

File Metadata

Mime Type
text/plain
Expires
Fri, Oct 18, 6:56 PM (5 h, 33 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
9827814
Default Alt Text
D4989.id14286.diff (3 KB)

Event Timeline