config->get('password_modoboa_api_token'); $IMAPhost = $_SESSION['imap_host']; $client = password::get_http_client(); $url = "https://{$IMAPhost}/api/v1/accounts/?search=" . urlencode($username); $options = [ 'http_errors' => true, 'headers' => [ 'Authorization' => "Token {$token}", 'Cache-Control' => 'no-cache', 'Content-Type' => 'application/json', ], ]; // Call GET to fetch values from modoboa server try { $response = $client->get($url, $options); $response = $response->getBody()->getContents(); } catch (Exception $e) { rcube::raise_error("Password plugin: Error fetching {$url} : {$e->getMessage()}", true); return PASSWORD_CONNECT_ERROR; } // Decode json string $decoded = json_decode($response); if (!is_array($decoded)) { return PASSWORD_CONNECT_ERROR; } // Get user ID (pk) $userid = $decoded[0]->pk; // Encode json with new password $options['body'] = json_encode([ 'username' => $decoded[0]->username, 'mailbox' => $decoded[0]->mailbox, 'role' => $decoded[0]->role, 'password' => $passwd, // new password ]); $url = "https://{$IMAPhost}/api/v1/accounts/{$userid}/"; // Call HTTP API Modoboa try { $response = $client->put($url, $options); $response = $response->getBody()->getContents(); } catch (Exception $e) { rcube::raise_error("Password plugin: Error on {$url} : {$e->getMessage()}", true); return PASSWORD_CONNECT_ERROR; } return PASSWORD_SUCCESS; } }