Push: Support cyrus's URI format

dev/push
Aleksander Machniak 5 years ago
parent ad5f2f0e6f
commit 77b6e2606a

@ -58,25 +58,21 @@ class push_parser_notify
protected function commonProps($data)
{
$uri = parse_url($data['uri']);
$user = $uri['user'];
list($folder, ) = explode(';', $uri['path']);
$uri = $this->parseURI($data['uri']);
$result = array(
'service' => $data['service'],
'uidset' => $data['uidset'],
'exists' => $data['messages'],
'folder_user' => urldecode($user),
'folder_name' => urldecode(ltrim($folder, '/')),
'folder_user' => $uri['user'] ?: $data['user'],
'folder_name' => $uri['folder'],
);
if (isset($data['oldMailboxID'])) {
$uri = parse_url($data['oldMailboxID']);
$user = $uri['user'];
list($folder, ) = explode(';', $uri['path']);
$uri = $this->parseURI($data['oldMailboxID']);
$result['old_folder_user'] = urldecode($user);
$result['old_folder_name'] = urldecode(ltrim($folder, '/'));
$result['old_folder_user'] = $uri['user'] ?: $data['user'];
$result['old_folder_name'] = $uri['folder'];
}
if (isset($data['vnd.cmu.unseenMessages'])) {
@ -227,4 +223,60 @@ class push_parser_notify
{
return $this->eventQuotaChange($data);
}
/**
* Parse imap folder URI to extract user and folder name
*/
protected function parseURI($uri)
{
$_uri = parse_url($uri);
list($path, $args) = explode(';', $_uri['path'], 2);
$path = ltrim($path, '/');
$params = array();
// Standard: "imap://test%40example.org@imap.example.org/INBOX";
if (!empty($_uri['user'])) {
$user = urldecode($_uri['user']);
$path = strlen($path) ? urldecode($path) : 'INBOX';
}
// (Old?) Cyrus IMAP: imap://imap.example.org/user/test.test/Sent%40example.org
else if (strpos($path, 'user/') === 0) {
$path = array_map('urldecode', explode('/', $path));
if (count($path) > 1) {
$last = count($path) - 1;
$user = $path[1];
if ($last > 1 && ($pos = strrpos($path[$last], '@'))) {
$user .= substr($path[$last], $pos);
$path[$last] = substr($path[$last], 0, $pos);
}
$path = implode('/', array_slice($path, 2));
if (!strlen($path)) {
$path = 'INBOX';
}
}
}
// E.g. for MailboxSubscribe the uri can be also just imap://imap.example.org/Folder
if (empty($user) && empty($path)) {
// invalid uri?
}
if (!empty($args)) {
foreach (explode(';', $args) as $arg) {
list($key, $val) = explode('=', $arg);
$params[urldecode($key)] = urldecode($val);
}
}
return array(
'user' => $user,
'folder' => $path,
'params' => $params,
);
}
}

@ -154,6 +154,29 @@ class push_service extends rcube
// $response->header("Content-Type", "text/plain; charset=utf-8");
$response->end();
$this->notification($request);
}
/**
* Similar to rcube::console(), writes to logs/push if debug option is enabled
*/
public function log_debug()
{
if ($this->debug) {
$msg = array();
foreach (func_get_args() as $arg) {
$msg[] = !is_string($arg) ? var_export($arg, true) : $arg;
}
rcube::write_log('push', implode(";\n", $msg));
}
}
/**
* Handles incoming notification request
*/
protected function notification($request)
{
// Read POST data, or JSON from POST body
$data = $request->post;
if (empty($data)) {
@ -175,21 +198,7 @@ class push_service extends rcube
}
// TODO: broadcast to old_folder_user
}
}
/**
* Similar to rcube::console(), writes to logs/push if debug option is enabled
*/
public function log_debug()
{
if ($this->debug) {
$msg = array();
foreach (func_get_args() as $arg) {
$msg[] = !is_string($arg) ? var_export($arg, true) : $arg;
}
rcube::write_log('push', implode(";\n", $msg));
// TODO: shared folders
}
}

Loading…
Cancel
Save