accounts.php 4.55 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
<?php

/*
	Flexisip Account Manager is a set of tools to manage SIP accounts.
	Copyright (C) 2019 Belledonne Communications SARL, All rights reserved.

	This program 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.

	This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
*/

include_once __DIR__ . '/../database/database.php';

include_once __DIR__ . '/../objects/account.php';
include_once __DIR__ . '/../objects/password.php';
include_once __DIR__ . '/../objects/alias.php';
26
include_once __DIR__ . '/../objects/user_info.php';
27 28 29

include_once __DIR__ . '/../misc/utilities.php';

30 31 32
include_once __DIR__ . '/accounts_email.php';
include_once __DIR__ . '/accounts_phone.php';

33
include_once __DIR__ . '/../misc/results_values.php';
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

// args = [username, [domain]]
function xmlrpc_is_account_used($method, $args) {
	$user = $args[0];
	$domain = get_domain($args[1]);

	Logger::getInstance()->message("[XMLRPC] xmlrpc_is_account_used(" . $user . ", " . $domain . ")");

	if (!check_parameter($user)) {
		return MISSING_USERNAME_PARAM;
	}

	$database = new Database();
	$db = $database->getConnection();
	$account = new Account($db);
	$account->username = $user;
	$account->domain = $domain;

	if ($account->getOne()) {
		return OK;
	}

	return NOK;
}

// args = [username, [domain]]
function xmlrpc_is_account_activated($method, $args) {
	$user = $args[0];
	$domain = get_domain($args[1]);

	Logger::getInstance()->message("[XMLRPC] xmlrpc_is_account_activated(" . $user . ", " . $domain . ")");

	if (!check_parameter($user)) {
		return MISSING_USERNAME_PARAM;
	}

	$database = new Database();
	$db = $database->getConnection();
	$account = new Account($db);
	$account->username = $user;
	$account->domain = $domain;

	if (!$account->getOne()) {
		return ACCOUNT_NOT_FOUND;
	}

	Logger::getInstance()->message("Account activation status is " . $account->activated);
	if (is_activated($account->activated)) {
		return OK;
	}

	return NOK;
}

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
// args = [username, key, [domain], [algo]]
function xmlrpc_recover_account_from_confirmation_key($method, $args) {
	$username = $args[0];
	$key = $args[1];
	$domain = get_domain($args[2]);
	$algo = get_algo($args[3]);

	Logger::getInstance()->message("[XMLRPC] xmlrpc_recover_account_from_confirmation_key(" . $username . ", " . $domain . ", " . $key . ", " . $algo . ")");

	$database = new Database();
	$db = $database->getConnection();
	$account = new Account($db);
	$account->username = $username;
	$account->domain = $domain;

	if (!$account->getOne()) {
		return ACCOUNT_NOT_FOUND;
	}

107
	if (!is_key_matching($key, $account)) {
108 109 110 111 112 113 114 115
		return KEY_DOESNT_MATCH;
	}

	$password = new Password($db);
	$password->account_id = $account->id;
	$password->algorithm = $algo;

	if ($password->getOne()) {
116 117 118 119 120
		$result = array(
			"password" => $password->password,
			"algorithm" => $password->algorithm
		);
		return $result;
121 122 123 124 125 126 127 128 129 130 131 132
	}

	// If not found, try without algo
	$password2 = new Password($db);
	$password2->account_id = $account->id;

	if ($password2->getOne()) {
		$result = array(
			"password" => $password2->password,
			"algorithm" => $password2->algorithm
		);
		return $result;
133 134
	}

135
	return PASSWORD_NOT_FOUND;
136 137
}

138 139 140 141 142 143 144 145 146 147
// args = []
function xmlrpc_get_accounts_count($method, $args) {
	Logger::getInstance()->message("[XMLRPC] xmlrpc_get_accounts_count()");

	$database = new Database();
	$db = $database->getConnection();
	$account = new Account($db);
	return $account->getCount();
}

148 149 150
function xmlrpc_accounts_register_methods($server) {
 	xmlrpc_server_register_method($server, 'is_account_used', 'xmlrpc_is_account_used');// args = [username, [domain]], return OK or NOK
	xmlrpc_server_register_method($server, 'is_account_activated', 'xmlrpc_is_account_activated');// args = [username, [domain]], return OK or NOK
151
	xmlrpc_server_register_method($server, 'recover_account_from_confirmation_key', 'xmlrpc_recover_account_from_confirmation_key');// args = [username, key, [domain], [algo]]
152

153
	xmlrpc_server_register_method($server, 'get_accounts_count', 'xmlrpc_get_accounts_count');//args = []
154 155 156

	xmlrpc_accounts_email_register_methods($server);
	xmlrpc_accounts_phone_register_methods($server);
157 158
}

johan's avatar
johan committed
159
?>