accounts.php 26.3 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 30 31 32 33 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259

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

include_once __DIR__ . '/results_values.php';

// args = [user, pwd, [domain], [algo]]
// /!\ This method must be used for tests purposes only /!\
function xmlrpc_get_confirmation_key($method, $args) {
	$user = $args[0];
	$pwd = $args[1];
	$domain = get_domain($args[2]);
	$algo = get_algo($args[3]);

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

	if (!check_parameter($user)) {
		return MISSING_USERNAME_PARAM;
	} else if (!ALLOW_TEST_ACCOUNTS) {
		Logger::getInstance()->error ("Non test account unauthorized");
		return TEST_ACCOUNTS_DISABLED;
	} else if ($algo == NULL) {
		return ALGO_NOT_SUPPORTED;
	}

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

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

	$password = new Password($db);
	$password->account_id = $account->id;
	$password->algorithm = $algo;
	
	if (!$password->getOne()) {
		return PASSWORD_NOT_FOUND;
	}

	if ($algo == "clrtxt") {
		$hashed_password = $pwd;
	} else {
		$hashed_password = hash_password($user, $pwd, $domain, $algo);
	}

	if (!password_match($hashed_password, $password->password) 
		&& !password_match($pwd, $password->password)) { // This condition is specific for liblinphone tester....
		return PASSWORD_DOESNT_MATCH;
	}

	$key = $account->confirmation_key;
	Logger::getInstance()->debug("[XMLRPC] returning key = " . $key);
	return $key;
}

// args = [user, pwd, [domain], [algo]]
// /!\ This method must be used for tests purposes only /!\
function xmlrpc_delete_account($method, $args) {
	$user = $args[0];
	$pwd = $args[1];
	$domain = get_domain($args[2]);
	$algo = get_algo($args[3]);

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

	if ($algo == NULL) {
		return ALGO_NOT_SUPPORTED;
	} else if (!check_parameter($user)) {
		return MISSING_USERNAME_PARAM;
	} else if (!ALLOW_TEST_ACCOUNTS) {
		return TEST_ACCOUNTS_DISABLED;
	}

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

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

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

	if (!$password->getOne()) {
		return PASSWORD_NOT_FOUND;
	}

	if ($algo == "clrtxt") {
		$hashed_password = $pwd;
	} else {
		$hashed_password = hash_password($user, $pwd, $domain, $algo);
	}
	if (!password_match($hashed_password, $password->password) 
		&& !password_match($pwd, $password->password)) { // This condition is specific for liblinphone tester....
		return PASSWORD_DOESNT_MATCH;
	}

	$alias = new Alias($db);
	$alias->account_id = $account->id;

	$account->delete();
	$password->delete();
	$alias->delete();

	return OK;
}

// 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;
}

// args = [phone, [domain]]
function xmlrpc_is_phone_number_used($method, $args) {
	$phone = $args[0];
	$domain = get_domain($args[1]);

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

	if (!check_parameter($phone, "phone")) {
		return MISSING_PHONE_PARAM;
	} else if (!startswith($phone, "+")) {
		return PHONE_NOT_E164;
	}

	$database = new Database();
	$db = $database->getConnection();

	$alias = new Alias($db);
	$alias->alias = $phone;
	$alias->domain = $domain;
	
	if ($alias->getOne()) {
		return OK_ALIAS;
	}
	
	$account = new Account($db);
	$account->username = $phone;
	$account->domain = $domain;

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

	return NOK;
}

// args = [phone, username, key, [domain], [algo]]
function xmlrpc_activate_phone_account($method, $args) {
	$phone = $args[0];
	$user = $args[1];
	$key = $args[2];
	$domain = get_domain($args[3]);
	$algo = get_algo($args[4]);

	Logger::getInstance()->message("[XMLRPC] xmlrpc_activate_phone_account(" . $user . ", " . $domain . ", " . $phone . ", " . $key . ", " . $algo . ")");

	if (!check_parameter($phone, "phone")) {
		return MISSING_PHONE_PARAM;
	} else if (!check_parameter($user)) {
		return MISSING_USERNAME_PARAM;
	} else if (!startswith($phone, "+")) {
		Logger::getInstance()->error("Phone doesn't start by +");
		return PHONE_NOT_E164;
	} else if ($algo == NULL) {
		return ALGO_NOT_SUPPORTED;
	}

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

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

260
	if (!is_key_matching($key, $account)) {
261 262
		return KEY_DOESNT_MATCH;
	}
263

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
	// If this is a recovery, account is already activated, don't go through the following again
	if (!is_activated($account->activated)) {
		$expiration = NULL;
		$account->activated = "1";
		$account->update();

		$alias = new Alias($db);
		$alias->account_id = $account->id;
		$alias->alias = $phone;
		$alias->domain = $account->domain;
		$alias->create();

		if (USE_IN_APP_PURCHASES) {
			$expiration = get_trial_expiration_date();
			//db_inapp_add_account($user, $domain, $expiration);
			//TODO
		}

		if (CUSTOM_HOOKS) {
			hook_on_account_activated($account);
		}
	}

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

	if ($password->getOne()) {
		return $password->password;
	}

	return PASSWORD_NOT_FOUND;
}

// args = [phone, [username], [password], useragent, [domain], [lang], [algo]]
function xmlrpc_create_phone_account($method, $args) {
	$phone = $args[0];
	$user = $args[1];
	$hashed_password = $args[2];
	$user_agent = $args[3];
	$domain = get_domain($args[4]);
	$lang = get_lang($args[5]);
	$algo = get_algo($args[6]);

	Logger::getInstance()->message("[XMLRPC] xmlrpc_create_phone_account(" . $user . ", " . $domain . ", " . $phone . ", " . $lang . ", " . $algo . ")");

	if (!check_parameter($phone, "phone")) {
		return MISSING_PHONE_PARAM;
	} else if (!startswith($phone, "+")) {
		mylog("[ERROR] Phone doesn't start by +");
		return PHONE_NOT_E164;
	} else if ($algo == NULL) {
		return ALGO_NOT_SUPPORTED;
	}

	if (!check_parameter($user)) {
		$user = $phone;
	}

	$recover_params  = array(
		0 => $phone,
		1 => $domain,
		2 => $lang,
	);

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

	$alias = new Alias($db);
	$alias->alias = $phone;
	$alias->domain = $domain;

	if ($account->getOne()) {
		if (RECOVER_ACCOUNT_IF_EXISTS) {
			$recovered_user = xmlrpc_recover_phone_account($method, $recover_params);
			if ($recovered_user == $user) {
				return OK;
			}

			return ACCOUNT_RECOVERY_IMPOSSIBLE;
		}

		return USERNAME_TAKEN;
	} else if ($alias->getOne()) {
		if (RECOVER_ACCOUNT_IF_EXISTS) {
			$recovered_user = xmlrpc_recover_phone_account($method, $recover_params);
			if ($recovered_user == $user) {
				return OK;
			}

			return ACCOUNT_RECOVERY_IMPOSSIBLE;
		}

		return PHONE_TAKEN;
	}

	$pwd = $hashed_password;
	if (!check_parameter($hashed_password, "hashed password")) {
		$pwd = generate_password();
		$hashed_password = hash_password($user, $pwd, $domain, $algo);
	}

	$account->confirmation_key = generate_4_digits_code();
	$account->user_agent = $user_agent;
	$account->ip_address = getIp();
    $account->activated = AUTO_ACTIVATE_ACCOUNT ? "1" : "0";
	$account->create();

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

	if ($user != $phone) {
		$alias->account_id = $account->id;
		$alias->create();
	}

	if (CUSTOM_HOOKS) {
		hook_on_account_created($account);
	}

	if (SEND_ACTIVATION_SMS) {
		if (!SMS_API_ENABLED) {
			// This is a hack to allow testing without sending SMS
			return OK;
		}
395
		$ok = send_sms($phone, $account->confirmation_key, $lang);
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
		return $ok;
	} else if (AUTO_ACTIVATE_ACCOUNT) {
		if (USE_IN_APP_PURCHASES) {
			//TODO
			/*$expiration = get_trial_expiration_date();
			db_inapp_add_account($user, $domain, $expiration);*/
		}
	}

	return OK;
}

// args = [username, key, [domain], [algo]]
function xmlrpc_activate_email_account($method, $args) {
	$user = $args[0];
	$key = $args[1];
	$domain = get_domain($args[2]);
	$algo = get_algo($args[3]);

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

	if (!check_parameter($user)) {
		return MISSING_USERNAME_PARAM;
	} else if ($algo == NULL) {
		return ALGO_NOT_SUPPORTED;
	}

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

	if (!$account->getOne()) {
		return ACCOUNT_NOT_FOUND;
	} else if ($account->activated != "0") {
		return ACCOUNT_ALREADY_ACTIVATED;
	}

435
	if (!is_key_matching($key, $account)) {
436 437 438 439 440 441
		return KEY_DOESNT_MATCH;
	}

	$account->activated = "1";
	$account->update();
	
Sylvain Berfini's avatar
Sylvain Berfini committed
442
	$expiration = NULL;
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
	// TODO
	/*if (USE_IN_APP_PURCHASES) {
		$expiration = get_trial_expiration_date();
		db_inapp_add_account($user, $domain, $expiration);
	}*/

	if (CUSTOM_HOOKS) {
		hook_on_account_activated($account);
	}

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

	if ($password->getOne()) {
		return $password->password;
	}

	return PASSWORD_NOT_FOUND;
}

// args = [username, email, [hash], useragent, [domain], [algo]]
function xmlrpc_create_email_account($method, $args) {
	$user = $args[0];
	$email = $args[1];
	$hashed_password = $args[2];
	$user_agent = $args[3];
	$domain = get_domain($args[4]);
	$algo = get_algo($args[5]);

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

	if (!check_parameter($user)) {
		return MISSING_USERNAME_PARAM;
	} else if (!check_parameter($email, "email")) {
		return MISSING_EMAIL_PARAM;
	} else if ($algo == NULL) {
		return ALGO_NOT_SUPPORTED;
	}

	$database = new Database();
	$db = $database->getConnection();
	$account = new Account($db);
	$account->username = $user;
	$account->domain = $domain;
	
	if ($account->getOne()) {
		return USERNAME_TAKEN;
	}

	if (!ALLOW_SAME_EMAILS_ON_MULTILPLE_ACCOUNTS) {
		$email_account = new Account($db);
		$email_account->email = $email;
		if ($email_account->getOne()) {
			return EMAIL_TAKEN;
		}
	}

	if (GENERATE_PASSWORD_ENABLED) {
		$hashed_password = hash_password($user, generate_password(), $domain, $algo);
	}

	$account->confirmation_key = uniqid();
	$account->email = $email;
	$account->user_agent = $user_agent;
	$account->ip_address = getIp();
    $account->activated = AUTO_ACTIVATE_ACCOUNT ? "1" : "0";
	$account->create();

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

	if (CUSTOM_HOOKS) {
		hook_on_account_created($account);
	}

	if (SEND_ACTIVATION_EMAIL && EMAIL_ENABLED) {
523
		send_email_with_activation_link($email, $account->confirmation_key, $account->username);
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
	} else if (AUTO_ACTIVATE_ACCOUNT) {
		//TODO
		/*if (USE_IN_APP_PURCHASES) {
			$expiration = get_trial_expiration_date();
			db_inapp_add_account($user, $domain, $expiration);
		}*/
	}

	return OK;
}

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

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

	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()) {
		$alias = new Alias($db);
		$alias->alias = $user;
		$alias->domain = $domain;

		if ($alias->getOne()) {
			return $user;
		}
		
		return ACCOUNT_NOT_FOUND;
	}
	
	$phone = $account->alias;
	if ($phone == NULL) {
		return ALIAS_NOT_FOUND;
	}

	if (RECOVER_ACCOUNT_IF_EXISTS) {
		return ACCOUNT_NOT_FOUND;
	}

	return $phone;
}

// args = [phone, [domain], [lang]]
function xmlrpc_recover_phone_account($method, $args) {
578 579 580 581 582 583

	// Is this function overloaded
	if (XMLRPC_RECOVER_PHONE_ACCOUNT_OVERLOAD === TRUE) {
		return xmlrpc_recover_phone_account_overload($method, $args);
	}

584 585 586 587 588 589 590 591 592 593 594 595 596 597
	$phone = $args[0];
	$domain = get_domain($args[1]);
	$lang = get_lang($args[2]);

	Logger::getInstance()->message("[XMLRPC] xmlrpc_recover_phone_account(" . $phone . ", " . $domain . ", " . $lang . ")");

	if (!check_parameter($phone, "phone")) {
		return MISSING_PHONE_PARAM;
	} else if (!startswith($phone, "+")) {
		return PHONE_NOT_E164;
	}

	$database = new Database();
	$db = $database->getConnection();
598

599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
	$account = new Account($db);
	$account->username = $phone;
	$account->domain = $domain;

	$alias = new Alias($db);
	$alias->alias = $phone;
	$alias->domain = $domain;

	if (!$account->getOne()) {
		if ($alias->getOne()) {
			$account->id = $alias->account_id;
			// This time the search will be done on the id instead of couple username / domain
			if (!$account->getOne()) {
				return ACCOUNT_NOT_FOUND;
			}
		} else {
			return ACCOUNT_NOT_FOUND;
		}
	}

	if (SEND_ACTIVATION_SMS) {
620
		$account->confirmation_key = generate_4_digits_code();
621 622 623 624 625 626
		$account->update();

		if (!SMS_API_ENABLED) {
			// This is a hack to allow testing without sending SMS
			return $account->username;
		}
627
		$ok = send_sms($phone, $account->confirmation_key, $lang);
628 629 630 631 632 633 634 635
		if ($ok != OK) {
			return $ok;
		}
	}

	return $account->username;
}

636 637 638 639 640 641 642 643 644 645 646 647
// args = [username, email, [domain]]
function xmlrpc_recover_email_account($method, $args) {
	$username = $args[0];
	$email = $args[1];
	$domain = get_domain($args[2]);

	Logger::getInstance()->message("[XMLRPC] xmlrpc_recover_email_account(" . $username . ", " . $email . ", " . $domain . ")");

	$database = new Database();
	$db = $database->getConnection();

	$account = new Account($db);
Sylvain Berfini's avatar
Sylvain Berfini committed
648
	$account->username = $username;
649 650 651 652 653 654
	$account->domain = $domain;

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

655
	if (strcasecmp($email, $account->email) != 0) { // Email case insensitive compare
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
		return EMAIL_DOESNT_MATCH;
	}

	$account->confirmation_key = uniqid();
	$account->update();

	if (SEND_ACTIVATION_EMAIL && EMAIL_ENABLED) {
		send_email_with_recover_key($email, $account->confirmation_key);
	}

	return OK;
}

// 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;
	}

688
	if (!is_key_matching($key, $account)) {
689 690 691 692 693 694 695 696 697 698 699 700 701 702
		return KEY_DOESNT_MATCH;
	}

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

	if ($password->getOne()) {
		return $password->password;
	}

	return PASSWORD_NOT_FOUND;
}

703
function update_password($username, $domain, $hashed_old_password, $hashed_new_password, $algo) {
704 705 706
	$database = new Database();
	$db = $database->getConnection();
	$account = new Account($db);
707
	$account->username = $username;
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
	$account->domain = $domain;
	
	if (!$account->getOne()) {
		return ACCOUNT_NOT_FOUND;
	}

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

	if (!$password->getOne()) {
		return PASSWORD_NOT_FOUND;
	}

	$db_hashed_password = $password->password;
	if (!password_match($db_hashed_password, $hashed_old_password)) {
		return PASSWORD_DOESNT_MATCH;
	}

	$password->password = $hashed_new_password;
	if ($password->update()) {
		Logger::getInstance()->message("Password updated successfully");
		return OK;
	}
	
	return NOK;
}

736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
// args = [username, old password, new password, [domain], [algo]]
function xmlrpc_update_password($method, $args) {
	$username = $args[0];
	$old_password = $args[1];
	$new_password = $args[2];
	$domain = get_domain($args[3]);
	$algo = get_algo($algo[4]);

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

	if ($algo == NULL) {
		return ALGO_NOT_SUPPORTED;
	}

	$old_hash = hash_password($username, $old_password, $domain, $algo);
	$new_hash = hash_password($username, $new_password, $domain, $algo);

	return update_password($username, $domain, $old_hash, $new_hash, $algo);
}

// args = [username, old hash, new hash, [domain], [algo]]
function xmlrpc_update_hash($method, $args) {
	$ususernameer = $args[0];
	$hashed_old_password = $args[1];
	$hashed_new_password = $args[2];
	$domain = get_domain($args[3]);
	$algo = get_algo($args[4]);

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

	if (!check_parameter($username)) {
		return MISSING_USERNAME_PARAM;
	} else if ($algo == NULL) {
		return ALGO_NOT_SUPPORTED;
	}

	return update_password($username, $domain, $hashed_old_password, $hashed_new_password, $algo);
}

775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
// args = [username, password, new email, [domain], [algo]]
function xmlrpc_update_email($method, $args) {
	$user = $args[0];
	$pwd = $args[1];
	$new_email = $args[2];
	$domain = get_domain($args[3]);
	$algo = get_algo($args[4]);

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

	if (!check_parameter($user)) {
		return MISSING_USERNAME_PARAM;
	} else if ($algo == NULL) {
		return ALGO_NOT_SUPPORTED;
	}

	$database = new Database();
	$db = $database->getConnection();
	$account = new Account($db);
	$account->username = $user;
	$account->domain = $domain;
	
	if (!$account->getOne()) {
		return ACCOUNT_NOT_FOUND;
	}

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

	if (!$password->getOne()) {
		return PASSWORD_NOT_FOUND;
	}

	$hashed_old_password = hash_password($user, $pwd, $domain, $algo);
	if (!password_match($password->password, $hashed_old_password)) {
		return PASSWORD_DOESNT_MATCH;
	}

	if ($account->email == $new_email) {
		Logger::getInstance()->warning("New email same as previous one");
		return EMAIL_UNCHANGED;
	}

	if (!ALLOW_SAME_EMAILS_ON_MULTILPLE_ACCOUNTS) {
		$email_account = new Account($db);
		$email_account->email = $email;
		if ($email_account->getOne()) {
			return EMAIL_TAKEN;
		}
	}

	$account->email = $new_email;
	if ($account->update()) {
		Logger::getInstance()->message("Email updated successfully");
		return OK;
	}

	return NOK;
}

836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
// args = [username, phone, ha1, [domain], [algo]]
function xmlrpc_delete_phone_account($method, $args) {
	$username = $args[0];
	$phone = $args[1];
	$ha1 = $args[2];
	$domain = get_domain($args[3]);
	$algo = get_algo($args[4]);

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

	$database = new Database();
	$db = $database->getConnection();
	$account = new Account($db);
	$account->username = $username;
	$account->domain = $domain;
	
	if (!$account->getOne()) {
		return ACCOUNT_NOT_FOUND;
	}

	if ($phone != $username && $phone != $account->alias) {
		return ALIAS_DOESNT_MATCH;
	}

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

	if (!$password->getOne()) {
		return PASSWORD_NOT_FOUND;
	}

	if ($ha1 != $password->password) {
		return PASSWORD_DOESNT_MATCH;
	}

	if ($account->delete()) {
		if ($password->delete()) {
			$alias = new Alias($db);
			$alias->account_id = $account->id;
			$alias->delete();

			$userinfo = new UserInfo($db);
			$userinfo->account_id = $account->id;
			$userinfo->delete();

			return OK;
		}
	}

	return NOK;
}

// args = [username, email, ha1, [domain], [algo]]
function xmlrpc_delete_email_account($method, $args) {
	$username = $args[0];
	$email = $args[1];
	$ha1 = $args[2];
	$domain = get_domain($args[3]);
	$algo = get_algo($args[4]);

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

	$database = new Database();
	$db = $database->getConnection();
	$account = new Account($db);
	$account->username = $username;
	$account->domain = $domain;
	
	if (!$account->getOne()) {
		return ACCOUNT_NOT_FOUND;
	}

	if ($email != $account->email) {
		return EMAIL_DOESNT_MATCH;
	}

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

	if (!$password->getOne()) {
		return PASSWORD_NOT_FOUND;
	}

	if ($ha1 != $password->password) {
		return PASSWORD_DOESNT_MATCH;
	}

	if ($account->delete()) {
		if ($password->delete()) {
			$alias = new Alias($db);
			$alias->account_id = $account->id;
			$alias->delete();

			$userinfo = new UserInfo($db);
			$userinfo->account_id = $account->id;
			$userinfo->delete();

			return OK;
		}
	}

	return NOK;
}

942 943 944 945 946 947 948 949 950 951
// 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();
}

952 953 954
function xmlrpc_accounts_register_methods($server) {
	if (ALLOW_TEST_ACCOUNTS) {
		// /!\ This methods must be used for tests purposes only /!\
Sylvain Berfini's avatar
Sylvain Berfini committed
955 956
		xmlrpc_server_register_method($server, 'get_confirmation_key', 'xmlrpc_get_confirmation_key');// args = [user, pwd, [domain], [algo]], return confirmation_key
		xmlrpc_server_register_method($server, 'delete_account', 'xmlrpc_delete_account');// args = [user, pwd, [domain], [algo]]
957 958 959 960 961
	}

 	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
	xmlrpc_server_register_method($server, 'is_phone_number_used', 'xmlrpc_is_phone_number_used');// args = [phone], return OK_ACCOUNT, OK_ALIAS or NOK
962 963
	xmlrpc_server_register_method($server, 'get_phone_number_for_account', 'xmlrpc_get_phone_number_for_account');// args = [username, [domain]], return a phone number or an error

Sylvain Berfini's avatar
Sylvain Berfini committed
964 965 966 967
	xmlrpc_server_register_method($server, 'activate_phone_account', 'xmlrpc_activate_phone_account');// args = [phone, username, key, [domain], [algo]], return ha1_password
	xmlrpc_server_register_method($server, 'create_phone_account', 'xmlrpc_create_phone_account');// args = [phone, [username], [password], useragent, [domain], [lang], [algo]], return OK
	xmlrpc_server_register_method($server, 'activate_email_account', 'xmlrpc_activate_email_account');// args = [username, key, [domain], [algo]], return ha1_password
	xmlrpc_server_register_method($server, 'create_email_account', 'xmlrpc_create_email_account');// args = [username, email, [hash], useragent, [domain], [algo]], return OK
968

969
	xmlrpc_server_register_method($server, 'recover_phone_account', 'xmlrpc_recover_phone_account');// args = [phone, [domain], [lang]], return username
970 971
	xmlrpc_server_register_method($server, 'recover_email_account', 'xmlrpc_recover_email_account');// args = [username, email, [domain]], return OK
	xmlrpc_server_register_method($server, 'recover_account_from_confirmation_key', 'xmlrpc_recover_account_from_confirmation_key');// args = [username, key, [domain], [algo]]
972

973 974 975
	xmlrpc_server_register_method($server, 'delete_phone_account', 'xmlrpc_delete_phone_account');// args = [username, phone, ha1, [domain], [algo]]
	xmlrpc_server_register_method($server, 'delete_email_account', 'xmlrpc_delete_email_account');// args = [username, email, ha1, [domain], [algo]]

Sylvain Berfini's avatar
Sylvain Berfini committed
976 977 978
	xmlrpc_server_register_method($server, 'update_password', 'xmlrpc_update_password');// args = [username, old password, new password, [domain], [algo]], return OK
	xmlrpc_server_register_method($server, 'update_hash', 'xmlrpc_update_hash');// args = [username, old hash, new hash, [domain], [algo]], return OK
	xmlrpc_server_register_method($server, 'update_email', 'xmlrpc_update_email');// args = [username, password, new email, [domain], [algo]], return OK
979 980

	xmlrpc_server_register_method($server, 'get_accounts_count', 'xmlrpc_get_accounts_count');//args = []
981 982
}

983
?>