From 40a723d7a17c4efc8f2ed02042a439b499407472 Mon Sep 17 00:00:00 2001 From: Peio Rigaux Date: Thu, 19 Sep 2019 13:55:42 +0200 Subject: [PATCH] Fills new fields in db (country_code and country_name) on userinfo creation with a call to api.ipapi.com --- conf/accounts.conf | 17 +++++- conf/db.conf | 2 +- conf/logs.conf | 2 +- src/misc/geoloc.php | 60 +++++++++++++++++++ src/misc/logging.php | 10 ++-- src/{xmlrpc => misc}/results_values.php | 5 +- src/misc/sms.php | 18 +++--- src/misc/user_info.php | 76 +++++++++++++++++++++++++ src/objects/account.php | 23 +++++--- src/objects/user_info.php | 55 +++++++++++++----- src/xmlrpc/accounts.php | 2 +- src/xmlrpc/accounts_email.php | 40 +++++++++---- src/xmlrpc/accounts_phone.php | 32 +++++++---- src/xmlrpc/aliases.php | 18 +++--- src/xmlrpc/devices.php | 6 +- src/xmlrpc/liblinphone_tester.php | 10 ++-- src/xmlrpc/passwords.php | 8 +-- src/xmlrpc/user_info.php | 65 +++++---------------- 18 files changed, 313 insertions(+), 136 deletions(-) create mode 100644 src/misc/geoloc.php rename src/{xmlrpc => misc}/results_values.php (97%) create mode 100644 src/misc/user_info.php diff --git a/conf/accounts.conf b/conf/accounts.conf index 04f4304..6238889 100644 --- a/conf/accounts.conf +++ b/conf/accounts.conf @@ -62,4 +62,19 @@ define('ALLOW_SAME_EMAILS_ON_MULTILPLE_ACCOUNTS', True); */ define('RECOVER_ACCOUNT_IF_EXISTS', False); -?> \ No newline at end of file +/* + * Enabling geoloc of accounts in user_info table. + * When this option is set, the fields coutry_name and country_code will be filled + * with a call to api.ipapi.com + * + * Default value: False + */ +define("ENABLE_NEW_ACCOUNTS_GEOLOC", False); + +/* API key for geoloc. If you need geoloc and don't have a key, + * ask it on ipapi.com + */ + +define("GEOLOC_ACCESS_KEY", ""); + +?> diff --git a/conf/db.conf b/conf/db.conf index 469c9fa..e0ce19a 100644 --- a/conf/db.conf +++ b/conf/db.conf @@ -99,4 +99,4 @@ define("EXPIRATION_DELAY", 180); */ define ("INVALID_CONFIRMATION_KEY", "ERROR"); -?> \ No newline at end of file +?> diff --git a/conf/logs.conf b/conf/logs.conf index 22c9087..8ba157c 100644 --- a/conf/logs.conf +++ b/conf/logs.conf @@ -32,4 +32,4 @@ define("LOG_FILE", "/var/opt/belledonne-communications/log/account-manager.log") */ define("LOG_DIR", "/var/opt/belledonne-communications/log/"); -?> \ No newline at end of file +?> diff --git a/src/misc/geoloc.php b/src/misc/geoloc.php new file mode 100644 index 0000000..0dd3bc0 --- /dev/null +++ b/src/misc/geoloc.php @@ -0,0 +1,60 @@ +. +*/ + +include_once __DIR__ . '/../config/config.php'; + +class Geoloc { + static function getGeolocInfosFromIp($ip_address){ + if($ip_address == "::1" || $ip_address == "127.0.0.1" || $ip_address == "localhost"){ + $service_url = 'https://ipecho.net/plain'; + $curl = curl_init($service_url); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + $curl_response = curl_exec($curl); + if ($curl_response === false) { + $info = curl_getinfo($curl); + curl_close($curl); + Logger::getInstance()->error('Error occured during curl exec (getting public ip of server). Additionnal info: ' . var_export($info)); + return false; + } else{ + Logger::getInstance()->debug("Getting external public ip from ipecho.net= " . $curl_response); + $ip_address = $curl_response; + } + curl_close($curl); + + } + $service_url = 'http://api.ipapi.com/' . $ip_address .'?access_key='. GEOLOC_ACCESS_KEY .'&fields=country_code,country_name'; + Logger::getInstance()->debug("Getting geoloc infos for ip after parse if=" . $ip_address); + Logger::getInstance()->debug("Geoloc url = " . $service_url); + $curl = curl_init($service_url); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + $curl_response = curl_exec($curl); + if ($curl_response === false) { + $info = curl_getinfo($curl); + curl_close($curl); + Logger::getInstance()->error('Error occured during curl exec (geoloc). Additionnal info: ' . var_export($info)); + return false; + } + curl_close($curl); + Logger::getInstance()->debug('geoloc, curl response : ' . $curl_response); + $decoded = json_decode($curl_response); + + return $decoded; + } +} diff --git a/src/misc/logging.php b/src/misc/logging.php index fa0a653..cba3709 100644 --- a/src/misc/logging.php +++ b/src/misc/logging.php @@ -24,7 +24,7 @@ class Logger { private static $instance = null; private $log_file; - + public function __construct() { if (!LOGS_ENABLED) { return; @@ -38,17 +38,17 @@ class Logger { $this->log_file = LOG_DIR . "/" . date('d-M-Y') . '.logs'; } } - + public static function getInstance() { if (!self::$instance) { self::$instance = new Logger(); } - + return self::$instance; } - function mylog($level, $message) { + private function mylog($level, $message) { if (!LOGS_ENABLED) { return; } @@ -85,4 +85,4 @@ class Logger { } } -?> \ No newline at end of file +?> diff --git a/src/xmlrpc/results_values.php b/src/misc/results_values.php similarity index 97% rename from src/xmlrpc/results_values.php rename to src/misc/results_values.php index 14a3100..45b940b 100644 --- a/src/xmlrpc/results_values.php +++ b/src/misc/results_values.php @@ -70,5 +70,8 @@ define ("PHONE_NOT_E164", "ERROR_PHONE_ISNT_E164"); define ("MAX_SMS_ALLOWED_EXCEEDED", "ERROR_MAX_SMS_EXCEEDED"); define ("SMS_API_FAILURE", "ERROR_CANT_SEND_SMS"); +/* Geoloc error */ +define ("GEOLOC_FAILED", "ERROR_GEOLOC_FAILED"); -?> \ No newline at end of file + +?> diff --git a/src/misc/sms.php b/src/misc/sms.php index cce3b90..766c723 100644 --- a/src/misc/sms.php +++ b/src/misc/sms.php @@ -26,7 +26,7 @@ use \Ovh\Sms\SmsApi; include_once __DIR__ . '/../database/database.php'; include_once __DIR__ . '/../objects/sms.php'; include_once __DIR__ . '/../misc/logging.php'; -include_once __DIR__ . '/../xmlrpc/results_values.php'; +include_once __DIR__ . '/results_values.php'; include_once __DIR__ . '/utilities.php'; // Internationalization @@ -47,13 +47,13 @@ function send_sms_ovh($phone, $key, $lang) { Logger::getInstance()->warning("[SMS] SMS API disabled"); return SMS_DISABLED; } - + $sms = new SmsApi(SMS_OVH_API_KEY, SMS_OVH_API_SECRET, SMS_OVH_ENDPOINT, SMS_OVH_CONSUMER_KEY); $accounts = $sms->getAccounts(); $sms->setAccount($accounts[0]); if (SMS_USE_SENDER) { $senders = $sms->getSenders(); - + /* The account must be validated in the OVH interface and by OVH itself */ if (count($senders) == 0) { Logger::getInstance()->warning("[SMS] No sender found, creating one " . SMS_OVH_SENDER . " / " . SMS_OVH_REASON . " : " . SMS_OVH_DESC); @@ -83,7 +83,7 @@ function send_sms_ovh($phone, $key, $lang) { $text = get_sms_string_for_lang($lang); $text = str_replace("#CODE#", $key, $text); $result = $message->send($text); - + $credits_removed = $result['totalCreditsRemoved']; Logger::getInstance()->message("[SMS] " . $credits_removed . " credit removed"); $invalid_receiver = $result['invalidReceivers']; @@ -126,15 +126,15 @@ function send_sms($phone, $key, $lang) { Logger::getInstance()->warning("[SMS] SMS API disabled"); return SMS_DISABLED; } - + if (startswith($phone, TESTS_PHONE_PREFIX)) { Logger::getInstance()->error("[SMS] Not sending sms to fake number used for tests purposes: " . $phone); return TEST_ACCOUNTS_DISABLED; } - + $now_date = new DateTime('now'); $now = $now_date->getTimestamp() * 1000; - + $database = new Database(); $db = $database->getConnection(); $sms = new SMS($db); @@ -158,7 +158,7 @@ function send_sms($phone, $key, $lang) { $sms->count = 1; $sms->create(); } - + if (SMS_OVH_API_KEY != NULL && SMS_OVH_API_KEY != "" && SMS_OVH_API_SECRET != NULL && SMS_OVH_API_SECRET != "" && SMS_OVH_CONSUMER_KEY != NULL && SMS_OVH_CONSUMER_KEY != "" && SMS_OVH_ENDPOINT != NULL && SMS_OVH_ENDPOINT != "") { try { send_sms_ovh($phone, $key, $lang); @@ -176,4 +176,4 @@ function send_sms($phone, $key, $lang) { return SMS_API_FAILURE; } -?> \ No newline at end of file +?> diff --git a/src/misc/user_info.php b/src/misc/user_info.php new file mode 100644 index 0000000..97d8fa9 --- /dev/null +++ b/src/misc/user_info.php @@ -0,0 +1,76 @@ +message("update_account_user_info(" . $username . ", " . $domain . " : " . $firstname . ", " . $lastname . ", " . $gender . ", " . $subscribe . ")"); + + $database = new Database(); + $db = $database->getConnection(); + + $account = new Account($db); + $account->username = $username; + $account->domain = $domain; + + if (!$account->getOne()) { + return ACCOUNT_NOT_FOUND; + } + Logger::getInstance()->debug("userInfo : Account after get one " . $account); + + $password = new Password($db); + $password->account_id = $account->id; + $password->algorithm = $algo; + + if (!$password->getOne()) { + return PASSWORD_NOT_FOUND; + } + + if (!password_match($ha1, $password->password)) { + return PASSWORD_DOESNT_MATCH; + } + + $user_info = new UserInfo($db); + $user_info->account_id = $account->id; + + if(ENABLE_NEW_ACCOUNTS_GEOLOC){ + Logger::getInstance()->debug("userInfo : Account ip after enable geoloc if " . $account->ip_address); + $country_infos = Geoloc::getGeolocInfosFromIp($account->ip_address); + if($country_infos){ + $user_info->country_code = $country_infos->country_code; + $user_info->country_name = $country_infos->country_name; + } + //error message is displayed from geoloc method. + else{ + return GEOLOC_FAILED; + } + Logger::getInstance()->debug("Getting geoloc infos : country_code=". + $country_infos->country_code . ' country_name=' . $country_infos->country_name); + } + + $update = $user_info->getOne(); + + $user_info->firstname = $firstname; + $user_info->lastname = $lastname; + $user_info->gender = $gender; + $user_info->subscribe = $subscribe; + + if ($update) { + $user_info->update(); + } else { + $user_info->create(); + } + + return OK; +} diff --git a/src/objects/account.php b/src/objects/account.php index 86f48d5..7304706 100644 --- a/src/objects/account.php +++ b/src/objects/account.php @@ -23,7 +23,7 @@ class Account { public $id; public $username; - public $domain; + public $domain; public $email; public $activated; public $confirmation_key; @@ -32,7 +32,7 @@ class Account { public $creation_time; public $expire_time; public $alias; - + public function __construct($db) { $this->conn = $db; } @@ -54,6 +54,9 @@ class Account { if (!empty($this->activated)) { $to_string = $to_string . "activated=" . $this->activated . ", "; } + if (!empty($this->ip_address)) { + $to_string = $to_string . "ip_address=" . $this->ip_address . ", "; + } if (!empty($this->confirmation_key)) { $to_string = $to_string . "confirmation_key=" . $this->confirmation_key . ", "; } @@ -119,7 +122,7 @@ class Account { } function create() { - $query = "INSERT INTO " . ACCOUNTS_DB_TABLE . " SET username=:username, domain=:domain, email=:email, activated=:activated, + $query = "INSERT INTO " . ACCOUNTS_DB_TABLE . " SET username=:username, domain=:domain, email=:email, activated=:activated, confirmation_key=:confirmation_key, ip_address=:ip_address, user_agent=:user_agent, creation_time=:creation_time"; if (USE_IN_APP_PURCHASES) { @@ -163,7 +166,7 @@ class Account { function update() { $query = "UPDATE " . ACCOUNTS_DB_TABLE . " SET username=:username, domain=:domain, activated=:activated"; - + if (!empty($this->email)) { $query = $query . ", email=:email"; } @@ -188,7 +191,7 @@ class Account { $stmt->bindParam(":domain", $this->domain); $stmt->bindParam(":activated", $this->activated); $stmt->bindParam(":id", $this->id); - + if (!empty($this->email)) { $this->email = htmlspecialchars(strip_tags($this->email)); $stmt->bindParam(":email", $this->email); @@ -223,7 +226,7 @@ class Account { } function getAll() { - $query = "SELECT ac.id, ac.username, ac.domain, ac.activated, ac.confirmation_key, ac.email, al.alias FROM " . ACCOUNTS_DB_TABLE . + $query = "SELECT ac.id, ac.username, ac.domain, ac.activated, ac.confirmation_key, ac.email, al.alias FROM " . ACCOUNTS_DB_TABLE . " ac LEFT JOIN " . ALIAS_DB_TABLE . " al ON ac.id = al.account_id"; $stmt = $this->conn->prepare($query); Logger::getInstance()->debug("GetAll " . (string)$this); @@ -232,7 +235,7 @@ class Account { } function getOne() { - $query = "SELECT ac.id, ac.username, ac.domain, ac.activated, ac.confirmation_key, ac.email, al.alias FROM " . ACCOUNTS_DB_TABLE . + $query = "SELECT ac.id, ac.username, ac.domain, ac.activated, ac.confirmation_key, ac.email, ac.ip_address, al.alias FROM " . ACCOUNTS_DB_TABLE . " ac LEFT JOIN " . ALIAS_DB_TABLE . " al ON ac.id = al.account_id"; if (!empty($this->id)) { @@ -270,7 +273,7 @@ class Account { $stmt->bindParam(1, $this->email); } else if (!empty($this->confirmation_key)) { $stmt->bindParam(1, $this->confirmation_key); - } + } Logger::getInstance()->debug("GetOne " . (string)$this); if ($stmt->execute()) { @@ -286,7 +289,9 @@ class Account { $this->email = $row['email']; $this->activated = $row['activated']; $this->confirmation_key = $row['confirmation_key']; + $this->ip_address = $row['ip_address']; $this->alias = $row['alias']; + return true; } Logger::getInstance()->error($stmt->errorInfo()); @@ -294,4 +299,4 @@ class Account { } } -?> \ No newline at end of file +?> diff --git a/src/objects/user_info.php b/src/objects/user_info.php index 3d46364..daa9bb6 100644 --- a/src/objects/user_info.php +++ b/src/objects/user_info.php @@ -26,8 +26,10 @@ class UserInfo { public $firstname; public $lastname; public $gender; + public $country_code; + public $country_name; public $subscribe; - + public function __construct($db) { $this->conn = $db; } @@ -35,22 +37,28 @@ class UserInfo { public function __toString() { $to_string = "UserInfo: "; if (!empty($this->id)) { - $to_string = $to_string . "id=" . $this->id . ", "; + $to_string .= "id=" . $this->id . ", "; } if (!empty($this->account_id)) { - $to_string = $to_string . "account_id=" . $this->account_id . ", "; + $to_string .= "account_id=" . $this->account_id . ", "; } if (!empty($this->firstname)) { - $to_string = $to_string . "firstname=" . $this->firstname . ", "; + $to_string .= "firstname=" . $this->firstname . ", "; } if (!empty($this->lastname)) { - $to_string = $to_string . "lastname=" . $this->lastname . ", "; + $to_string .= "lastname=" . $this->lastname . ", "; } if (!empty($this->gender)) { - $to_string = $to_string . "gender=" . $this->gender . ", "; + $to_string .= "gender=" . $this->gender . ", "; + } + if (!empty($this->country_code)) { + $to_string .= "country_code=" . $this->country_code . ", "; + } + if (!empty($this->country_name)) { + $to_string .= "country_name=" . $this->country_name . ", "; } if (!empty($this->subscribe)) { - $to_string = $to_string . "subscribe=" . $this->subscribe . ", "; + $to_string .= "subscribe=" . $this->subscribe . ", "; } return substr($to_string, 0, -2); } @@ -71,13 +79,15 @@ class UserInfo { function createTable() { $query = "CREATE TABLE IF NOT EXISTS " . USER_INFO_DB_TABLE . " ( - id INTEGER(11) UNSIGNED NOT NULL AUTO_INCREMENT, - account_id INTEGER(11) UNSIGNED NOT NULL, - firstname VARCHAR(128) NOT NULL, - lastname VARCHAR(128) NOT NULL, - gender enum('male','female') NOT NULL, - subscribe enum('0','1') NOT NULL DEFAULT '0', - PRIMARY KEY (id))"; + id INTEGER(11) UNSIGNED NOT NULL AUTO_INCREMENT, + account_id INTEGER(11) UNSIGNED NOT NULL, + firstname VARCHAR(128) NOT NULL, + lastname VARCHAR(128) NOT NULL, + gender enum('male','female') NOT NULL, + country_code VARCHAR(32), + country_name VARCHAR(512), + subscribe enum('0','1') NOT NULL DEFAULT '0', + PRIMARY KEY (id))"; $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); $stmt = $this->conn->prepare($query); @@ -109,6 +119,10 @@ class UserInfo { function create() { $query = "INSERT INTO " . USER_INFO_DB_TABLE . " SET account_id=:account_id, firstname=:firstname, lastname=:lastname, gender=:gender, subscribe=:subscribe"; + if(ENABLE_NEW_ACCOUNTS_GEOLOC){ + $query .= ", country_code=:country_code, country_name=:country_name"; + } + $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); $stmt = $this->conn->prepare($query); @@ -124,6 +138,15 @@ class UserInfo { $stmt->bindParam(":gender", $this->gender); $stmt->bindParam(":subscribe", $this->subscribe); + if(ENABLE_NEW_ACCOUNTS_GEOLOC){ + + $this->country_code = htmlspecialchars(strip_tags($this->country_code)); + $this->country_name = htmlspecialchars(strip_tags($this->country_name)); + + $stmt->bindParam(":country_code", $this->country_code); + $stmt->bindParam(":country_name", $this->country_name); + } + Logger::getInstance()->debug("Creating " . (string)$this); if ($stmt->execute()) { $this->id = $this->conn->lastInsertId(); @@ -217,6 +240,8 @@ class UserInfo { $this->firstname = $row['firstname']; $this->lastname = $row['lastname']; $this->gender = $row['gender']; + $this->country_code = $row['country_code']; + $this->country_name = $row['country_name']; $this->subscribe = $row['subscribe']; return true; } @@ -225,4 +250,4 @@ class UserInfo { } } -?> \ No newline at end of file +?> diff --git a/src/xmlrpc/accounts.php b/src/xmlrpc/accounts.php index 34a63a9..88f4496 100644 --- a/src/xmlrpc/accounts.php +++ b/src/xmlrpc/accounts.php @@ -30,7 +30,7 @@ include_once __DIR__ . '/../misc/utilities.php'; include_once __DIR__ . '/accounts_email.php'; include_once __DIR__ . '/accounts_phone.php'; -include_once __DIR__ . '/results_values.php'; +include_once __DIR__ . '/../misc/results_values.php'; // args = [username, [domain]] function xmlrpc_is_account_used($method, $args) { diff --git a/src/xmlrpc/accounts_email.php b/src/xmlrpc/accounts_email.php index 0ace833..09409fc 100644 --- a/src/xmlrpc/accounts_email.php +++ b/src/xmlrpc/accounts_email.php @@ -26,8 +26,8 @@ include_once __DIR__ . '/../objects/alias.php'; include_once __DIR__ . '/../objects/user_info.php'; include_once __DIR__ . '/../misc/utilities.php'; - -include_once __DIR__ . '/results_values.php'; +include_once __DIR__ . '/../misc/user_info.php'; +include_once __DIR__ . '/../misc/results_values.php'; // args = [username, email, [hash], useragent, [domain], [algo]] function xmlrpc_create_email_account($method, $args) { @@ -53,7 +53,7 @@ function xmlrpc_create_email_account($method, $args) { $account = new Account($db); $account->username = $user; $account->domain = $domain; - + if ($account->getOne()) { return USERNAME_TAKEN; } @@ -74,6 +74,7 @@ function xmlrpc_create_email_account($method, $args) { $account->email = $email; $account->user_agent = $user_agent; $account->ip_address = getIp(); + $account->activated = AUTO_ACTIVATE_ACCOUNT ? "1" : "0"; $account->create(); @@ -97,7 +98,18 @@ function xmlrpc_create_email_account($method, $args) { }*/ } - return OK; + // args = [username, email, [hash], useragent, [domain], [algo]] + // args needed = [username, ha1, firstname, lastname, gender, subscribe, [domain], [algo]] + //need username + domain + + //We call this function to set the geoloc if enabled + if(ENABLE_NEW_ACCOUNTS_GEOLOC){ + return update_account_user_info($account->username, $hashed_password, NULL, NULL, "unknown", '0', $account->domain, $algo); + } + else { + return OK; + } + } // args = [username, email, md5_hash, sha256_hash, useragent, [domain]], return OK @@ -122,7 +134,7 @@ function xmlrpc_create_email_md5_sha256_account($method, $args) { $account = new Account($db); $account->username = $user; $account->domain = $domain; - + if ($account->getOne()) { return USERNAME_TAKEN; } @@ -174,7 +186,15 @@ function xmlrpc_create_email_md5_sha256_account($method, $args) { }*/ } - return OK; + //We call this function to set the geoloc if enabled + // args needed = [username, ha1, firstname, lastname, gender, subscribe, [domain], [algo]] + //need username + domain + if(ENABLE_NEW_ACCOUNTS_GEOLOC){ + return update_account_user_info($account->username, $md5_hash, NULL, NULL, "unknown", '0', $account->domain, MD5); + } + else { + return OK; + } } // args = [username, key, [domain], [algo]] @@ -210,7 +230,7 @@ function xmlrpc_activate_email_account($method, $args) { $account->activated = "1"; $account->update(); - + $expiration = NULL; // TODO /*if (USE_IN_APP_PURCHASES) { @@ -287,7 +307,7 @@ function xmlrpc_update_email($method, $args) { $account = new Account($db); $account->username = $user; $account->domain = $domain; - + if (!$account->getOne()) { return ACCOUNT_NOT_FOUND; } @@ -342,7 +362,7 @@ function xmlrpc_delete_email_account($method, $args) { $account = new Account($db); $account->username = $username; $account->domain = $domain; - + if (!$account->getOne()) { return ACCOUNT_NOT_FOUND; } @@ -389,4 +409,4 @@ function xmlrpc_accounts_email_register_methods($server) { xmlrpc_server_register_method($server, 'delete_email_account', 'xmlrpc_delete_email_account');// args = [username, email, ha1, [domain], [algo]] } -?> \ No newline at end of file +?> diff --git a/src/xmlrpc/accounts_phone.php b/src/xmlrpc/accounts_phone.php index 9b778e5..3e5db54 100644 --- a/src/xmlrpc/accounts_phone.php +++ b/src/xmlrpc/accounts_phone.php @@ -27,7 +27,9 @@ include_once __DIR__ . '/../objects/user_info.php'; include_once __DIR__ . '/../misc/utilities.php'; -include_once __DIR__ . '/results_values.php'; +include_once __DIR__ . '/../misc/user_info.php'; + +include_once __DIR__ . '/../misc/results_values.php'; // args = [phone, [username], [password], useragent, [domain], [lang], [algo]] function xmlrpc_create_phone_account($method, $args) { @@ -44,7 +46,7 @@ function xmlrpc_create_phone_account($method, $args) { if (!check_parameter($phone, "phone")) { return MISSING_PHONE_PARAM; } else if (!startswith($phone, "+")) { - mylog("[ERROR] Phone doesn't start by +"); + Logger::getInstance()->error("Phone doesn't start by +"); return PHONE_NOT_E164; } else if ($algo == NULL) { return ALGO_NOT_SUPPORTED; @@ -131,7 +133,15 @@ function xmlrpc_create_phone_account($method, $args) { } } - return OK; + //We call this function to set the geoloc if enabled + // args needed = [username, ha1, firstname, lastname, gender, subscribe, [domain], [algo]] + //need username + domain + if (ENABLE_NEW_ACCOUNTS_GEOLOC){ + return update_account_user_info($account->username, $hashed_password, NULL, NULL, "unknown", '0', $account->domain, $algo); + } + else { + return OK; + } } // args = [phone, username, key, [domain], [algo]] @@ -288,7 +298,7 @@ function xmlrpc_delete_phone_account($method, $args) { $account = new Account($db); $account->username = $username; $account->domain = $domain; - + if (!$account->getOne()) { return ACCOUNT_NOT_FOUND; } @@ -345,11 +355,11 @@ function xmlrpc_is_phone_number_used($method, $args) { $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; @@ -386,10 +396,10 @@ function xmlrpc_get_phone_number_for_account($method, $args) { if ($alias->getOne()) { return $user; } - + return ACCOUNT_NOT_FOUND; } - + $phone = $account->alias; if ($phone == NULL) { return ALIAS_NOT_FOUND; @@ -399,7 +409,7 @@ function xmlrpc_get_phone_number_for_account($method, $args) { return ACCOUNT_NOT_FOUND; } - return $phone; + return $phone; } function xmlrpc_accounts_phone_register_methods($server) { @@ -407,9 +417,9 @@ function xmlrpc_accounts_phone_register_methods($server) { 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, 'recover_phone_account', 'xmlrpc_recover_phone_account');// args = [phone, [domain], [lang]], return username xmlrpc_server_register_method($server, 'delete_phone_account', 'xmlrpc_delete_phone_account');// args = [username, phone, ha1, [domain], [algo]] - + xmlrpc_server_register_method($server, 'is_phone_number_used', 'xmlrpc_is_phone_number_used');// args = [phone], return OK_ACCOUNT, OK_ALIAS or NOK 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 } -?> \ No newline at end of file +?> diff --git a/src/xmlrpc/aliases.php b/src/xmlrpc/aliases.php index b9a6c3b..b8324a6 100644 --- a/src/xmlrpc/aliases.php +++ b/src/xmlrpc/aliases.php @@ -26,7 +26,7 @@ include_once __DIR__ . '/../objects/alias.php'; include_once __DIR__ . '/../misc/utilities.php'; -include_once __DIR__ . '/results_values.php'; +include_once __DIR__ . '/../misc/results_values.php'; // args = [phone, [domain]] function xmlrpc_is_alias_used($method, $args) { @@ -46,7 +46,7 @@ function xmlrpc_is_alias_used($method, $args) { $alias = new Alias($db); $alias->alias = $phone; $alias->domain = $domain; - + if (!$alias->getOne()) { return ALIAS_NOT_FOUND; } @@ -72,7 +72,7 @@ function xmlrpc_link_phone_number_with_account($method, $args) { /*} else if (db_alias_is_in_use($phone, $domain)) { return PHONE_TAKEN;*/ } - + $database = new Database(); $db = $database->getConnection(); $account = new Account($db); @@ -105,9 +105,9 @@ function xmlrpc_activate_phone_number_link($method, $args) { $ha1 = $args[3]; $domain = get_domain($args[4]); $algo = get_algo($args[5]); - + Logger::getInstance()->message("[XMLRPC] xmlrpc_activate_phone_number_link(" . $user . ", " . $domain . ", " . $phone . ", " . $key . ", " . $algo . ")"); - + if (!check_parameter($phone, "phone")) { return MISSING_PHONE_PARAM; } else if (!check_parameter($user)) { @@ -121,7 +121,7 @@ function xmlrpc_activate_phone_number_link($method, $args) { $account = new Account($db); $account->username = $user; $account->domain = $domain; - + if (!$account->getOne()) { return ACCOUNT_NOT_FOUND; } @@ -147,7 +147,7 @@ function xmlrpc_activate_phone_number_link($method, $args) { $alias = new Alias($db); $alias->alias = $phone; $alias->domain = $domain; - + if ($alias->getOne()) { $alias->account_id = $account->id; $alias->update(); @@ -177,7 +177,7 @@ function xmlrpc_get_alias($method, $args) { $alias = new Alias($db); $alias->alias = $phone; $alias->domain = $domain; - + if (!$alias->getOne()) { return ALIAS_NOT_FOUND; } @@ -198,4 +198,4 @@ function xmlrpc_aliases_register_methods($server) { xmlrpc_server_register_method($server, 'get_alias', 'xmlrpc_get_alias');// args = [phone, [domain]], return username } -?> \ No newline at end of file +?> diff --git a/src/xmlrpc/devices.php b/src/xmlrpc/devices.php index 8bcc848..1379ce4 100644 --- a/src/xmlrpc/devices.php +++ b/src/xmlrpc/devices.php @@ -20,7 +20,7 @@ include_once __DIR__ . '/../database/database.php'; include_once __DIR__ . '/../objects/device.php'; -include_once __DIR__ . '/results_values.php'; +include_once __DIR__ . '/../misc/results_values.php'; // args = [manufacturer, model, status, delay, hasHEC] function xmlrpc_add_ec_calibration_result($method, $args) { @@ -44,7 +44,7 @@ function xmlrpc_add_ec_calibration_result($method, $args) { $device->delay = $delay; $device->hardware_echo_canceller = $hasHEC; $device->create(); - + return OK; } @@ -52,4 +52,4 @@ function xmlrpc_devices_register_methods($server) { xmlrpc_server_register_method($server, 'add_ec_calibration_result', 'xmlrpc_add_ec_calibration_result');// args = [manufacturer, model, status, delay, hasHEC] } -?> \ No newline at end of file +?> diff --git a/src/xmlrpc/liblinphone_tester.php b/src/xmlrpc/liblinphone_tester.php index 51cd5b0..5e74fd8 100644 --- a/src/xmlrpc/liblinphone_tester.php +++ b/src/xmlrpc/liblinphone_tester.php @@ -26,7 +26,7 @@ include_once __DIR__ . '/../objects/alias.php'; include_once __DIR__ . '/../misc/utilities.php'; -include_once __DIR__ . '/results_values.php'; +include_once __DIR__ . '/../misc/results_values.php'; // args = [user, pwd, [domain], [algo]] // /!\ This method must be used for tests purposes only /!\ @@ -60,7 +60,7 @@ function xmlrpc_get_confirmation_key($method, $args) { $password = new Password($db); $password->account_id = $account->id; $password->algorithm = $algo; - + if (!$password->getOne()) { return PASSWORD_NOT_FOUND; } @@ -71,13 +71,13 @@ function xmlrpc_get_confirmation_key($method, $args) { $hashed_password = hash_password($user, $pwd, $domain, $algo); } - if (!password_match($hashed_password, $password->password) + if (!password_match($hashed_password, $password->password) && !password_match($pwd, $password->password)) { // This condition is specific for liblinphone tester.... return PASSWORD_DOESNT_MATCH; } if ($account->confirmation_key == INVALID_CONFIRMATION_KEY) { - // We have to generate a new one because + // We have to generate a new one because $account->confirmation_key = uniqid(); $account->update(); } @@ -128,7 +128,7 @@ function xmlrpc_delete_account($method, $args) { } else { $hashed_password = hash_password($user, $pwd, $domain, $algo); } - if (!password_match($hashed_password, $password->password) + if (!password_match($hashed_password, $password->password) && !password_match($pwd, $password->password)) { // This condition is specific for liblinphone tester.... return PASSWORD_DOESNT_MATCH; } diff --git a/src/xmlrpc/passwords.php b/src/xmlrpc/passwords.php index 0010cb5..15da893 100644 --- a/src/xmlrpc/passwords.php +++ b/src/xmlrpc/passwords.php @@ -27,7 +27,7 @@ include_once __DIR__ . '/../objects/user_info.php'; include_once __DIR__ . '/../misc/utilities.php'; -include_once __DIR__ . '/results_values.php'; +include_once __DIR__ . '/../misc/results_values.php'; // args = [username, old hash, new hash, [domain], [algo]] function xmlrpc_update_password($method, $args) { @@ -50,7 +50,7 @@ function xmlrpc_update_password($method, $args) { $account = new Account($db); $account->username = $username; $account->domain = $domain; - + if (!$account->getOne()) { return ACCOUNT_NOT_FOUND; } @@ -69,7 +69,7 @@ function xmlrpc_update_password($method, $args) { Logger::getInstance()->message("Password updated successfully"); return OK; } - + return NOK; } @@ -213,4 +213,4 @@ function xmlrpc_passwords_register_methods($server) { xmlrpc_server_register_method($server, 'check_authentication_and_upgrade_password', 'xmlrpc_check_authentication_and_upgrade_password');// args = [username, md5_hash, sha256_hash, [domain]] } -?> \ No newline at end of file +?> diff --git a/src/xmlrpc/user_info.php b/src/xmlrpc/user_info.php index f4d2d1a..a424c2b 100644 --- a/src/xmlrpc/user_info.php +++ b/src/xmlrpc/user_info.php @@ -26,8 +26,11 @@ include_once __DIR__ . '/../objects/alias.php'; include_once __DIR__ . '/../objects/user_info.php'; include_once __DIR__ . '/../misc/utilities.php'; +include_once __DIR__ . '/../misc/geoloc.php'; -include_once __DIR__ . '/results_values.php'; +include_once __DIR__ . '/../misc/results_values.php'; + +include_once __DIR__ . '/../misc/user_info.php'; // args = [username, ha1, [domain], [algo]] function xmlrpc_get_email_account($method, $args) { @@ -48,7 +51,7 @@ function xmlrpc_get_email_account($method, $args) { if (!$account->getOne()) { return ACCOUNT_NOT_FOUND; } - + $password = new Password($db); $password->account_id = $account->id; $password->algorithm = $algo; @@ -66,8 +69,8 @@ function xmlrpc_get_email_account($method, $args) { $user_info->getOne(); $result = array( - "id" => $account->id, - "username" => $account->username, + "id" => $account->id, + "username" => $account->username, "domain" => $account->domain, "email" => $account->email, "alias" => $account->alias, @@ -77,7 +80,7 @@ function xmlrpc_get_email_account($method, $args) { "gender" => $user_info->gender, "subscribe" => $user_info->subscribe ); - + return $result; } @@ -109,7 +112,7 @@ function xmlrpc_get_phone_account($method, $args) { if (!$account->getOne()) { return ACCOUNT_NOT_FOUND; } - + $password = new Password($db); $password->account_id = $account->id; $password->algorithm = $algo; @@ -127,8 +130,8 @@ function xmlrpc_get_phone_account($method, $args) { $user_info->getOne(); $result = array( - "id" => $account->id, - "username" => $account->username, + "id" => $account->id, + "username" => $account->username, "domain" => $account->domain, "email" => $account->email, "alias" => $account->alias, @@ -138,7 +141,7 @@ function xmlrpc_get_phone_account($method, $args) { "gender" => $user_info->gender, "subscribe" => $user_info->subscribe ); - + return $result; } @@ -154,47 +157,7 @@ function xmlrpc_update_account_user_info($method, $args) { $algo = get_algo($args[7]); Logger::getInstance()->message("[XMLRPC] xmlrpc_update_account_user_info(" . $username . ", " . $domain . " : " . $firstname . ", " . $lastname . ", " . $gender . ", " . $subscribe . ")"); - - $database = new Database(); - $db = $database->getConnection(); - - $account = new Account($db); - $account->username = $username; - $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 (!password_match($ha1, $password->password)) { - return PASSWORD_DOESNT_MATCH; - } - - $user_info = new UserInfo($db); - $user_info->account_id = $account->id; - - $update = $user_info->getOne(); - - $user_info->firstname = $firstname; - $user_info->lastname = $lastname; - $user_info->gender = $gender; - $user_info->subscribe = $subscribe; - - if ($update) { - $user_info->update(); - } else { - $user_info->create(); - } - - return OK; + return update_account_user_info($username, $ha1, $firstname, $lastname, $gender, $subscribe, $domain, $algo); } function xmlrpc_user_info_register_methods($server) { @@ -203,4 +166,4 @@ function xmlrpc_user_info_register_methods($server) { xmlrpc_server_register_method($server, 'update_account_user_info', 'xmlrpc_update_account_user_info'); // args = [username, ha1, firstname, lastname, gender, subscribe, [domain], [algo]] } -?> \ No newline at end of file +?>