Commit 065f0d3d authored by Timothée Jaussoin's avatar Timothée Jaussoin

Reindent and reformat the code to follow PHP-PSR2

parent 6abd99e3
Pipeline #10613 passed with stage
in 44 seconds
......@@ -88,5 +88,3 @@ if ((!empty($data->username) || !empty($data->phone)) && (!empty($data->password
http_response_code(400);
echo json_encode(array("message" => "Unable to create account, data is incomplete."));
}
?>
\ No newline at end of file
......@@ -38,7 +38,7 @@ $data = json_decode(file_get_contents("php://input"));
if (!empty($data->id) || !empty($data->username)) {
if (!empty($data->id)) {
$account->id = $data->id;
} else if (!empty($data->username)) {
} elseif (!empty($data->username)) {
$account->username = $data->username;
if (!empty($data->domain)) {
$account->domain = $data->domain;
......@@ -77,6 +77,3 @@ if (!empty($data->id) || !empty($data->username)) {
http_response_code(400);
echo json_encode(array("message" => "Unable to delete account, data is incomplete."));
}
?>
\ No newline at end of file
......@@ -68,5 +68,3 @@ if (!empty($account->id) && !empty($account->username) && !empty($account->domai
http_response_code(404);
echo json_encode(array("message" => "Account doesn't exist"));
}
?>
\ No newline at end of file
......@@ -52,5 +52,3 @@ if ($num > 0) {
http_response_code(404);
echo json_encode(array("message" => "No account found"));
}
?>
\ No newline at end of file
......@@ -38,7 +38,7 @@ $data = json_decode(file_get_contents("php://input"));
if (!empty($data->id) || !empty($data->username)) {
if (!empty($data->id)) {
$account->id = $data->id;
} else if (!empty($data->username)) {
} elseif (!empty($data->username)) {
$account->username = $data->username;
if (!empty($data->domain)) {
$account->domain = $data->domain;
......@@ -104,5 +104,3 @@ if (!empty($data->id) || !empty($data->username)) {
http_response_code(400);
echo json_encode(array("message" => "Unable to update account, data is incomplete."));
}
?>
\ No newline at end of file
......@@ -21,21 +21,21 @@
include_once __DIR__ . '/../config/config.php';
include_once __DIR__ . '/../misc/logging.php';
class Database {
class Database
{
public $conn;
public function getConnection() {
public function getConnection()
{
$this->conn = null;
try {
$this->conn = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASSWORD);
$this->conn->exec("set names utf8");
} catch(PDOException $exception) {
} catch (PDOException $exception) {
Logger::getInstance()->error("Connection error: " . $exception->getMessage());
}
return $this->conn;
}
}
?>
\ No newline at end of file
......@@ -21,14 +21,15 @@
include_once __DIR__ . '/../config/config.php';
include_once __DIR__ . '/../misc/logging.php';
function send_email($email, $subject, $text, $html) {
function send_email($email, $subject, $text, $html)
{
$site = EMAIL_SITE;
$from = EMAIL_FROM_ADDR;
$name = EMAIL_FROM_NAME;
$to = $email;
$from = $name." <".$from.">";
$limite = "_----------=_parties_".md5(uniqid (rand()));
$limite = "_----------=_parties_".md5(uniqid(rand()));
$headers = "Reply-to: ".$from."\n";
$headers .= "From: ".$from."\n";
......@@ -64,14 +65,17 @@ function send_email($email, $subject, $text, $html) {
}
}
function send_email_with_activation_link($email, $key, $username, $algo) {
if( !EMAIL_ENABLED ){
function send_email_with_activation_link($email, $key, $username, $algo)
{
if (!EMAIL_ENABLED) {
Logger::getInstance()->warning("[EMAIL] Emails are disabled");
return "WARNING_EMAILS_DISABLED";
}
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
if ($_SERVER["HTTPS"] == "on") {
$pageURL .= "s";
}
$pageURL .= "://";
$link = $pageURL . EMAIL_ACTIVATION_LINK;
......@@ -89,8 +93,9 @@ function send_email_with_activation_link($email, $key, $username, $algo) {
Logger::getInstance()->message("[EMAIL] Email sent to email " . $email . " to activate the account");
}
function send_email_with_recover_key($email, $key) {
if( !EMAIL_ENABLED ){
function send_email_with_recover_key($email, $key)
{
if (!EMAIL_ENABLED) {
Logger::getInstance()->warning("[EMAIL] Emails are disabled");
return "WARNING_EMAILS_DISABLED";
}
......@@ -103,5 +108,3 @@ function send_email_with_recover_key($email, $key) {
send_email($email, EMAIL_RECOVERY_SUBJECT, $body, $body_html);
Logger::getInstance()->message("[EMAIL] Email sent to email " . $email . " to recover the account");
}
?>
\ No newline at end of file
......@@ -20,9 +20,11 @@
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"){
class Geoloc
{
public 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);
......@@ -32,12 +34,11 @@ class Geoloc {
curl_close($curl);
Logger::getInstance()->error('Error occured during curl exec (getting public ip of server). Additionnal info: ' . var_export($info));
return false;
} else{
} 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);
......
......@@ -20,12 +20,14 @@
include_once __DIR__ . '/../config/config.php';
class Logger {
class Logger
{
private static $instance = null;
private $log_file;
public function __construct() {
public function __construct()
{
if (!LOGS_ENABLED) {
return;
}
......@@ -48,7 +50,8 @@ class Logger {
return self::$instance;
}
private function mylog($level, $message) {
private function mylog($level, $message)
{
if (!LOGS_ENABLED) {
return;
}
......@@ -68,21 +71,23 @@ class Logger {
file_put_contents($this->log_file, $log_msg, FILE_APPEND);
}
function error($message) {
public function error($message)
{
$this->mylog("Error", $message);
}
function warning($message) {
public function warning($message)
{
$this->mylog("Warning", $message);
}
function message($message) {
public function message($message)
{
$this->mylog("Message", $message);
}
function debug($message) {
public function debug($message)
{
$this->mylog("Debug", $message);
}
}
?>
......@@ -18,66 +18,63 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
define ("OK", "OK");
define ("NOK", "NOK");
define ("OK_ALIAS", "OK_ALIAS");
define ("OK_ACCOUNT", "OK_ACCOUNT");
define("OK", "OK");
define("NOK", "NOK");
define("OK_ALIAS", "OK_ALIAS");
define("OK_ACCOUNT", "OK_ACCOUNT");
/* Parameter related */
define ("MISSING_PHONE_PARAM", "ERROR_PHONE_PARAMETER_NOT_FOUND");
define ("MISSING_USERNAME_PARAM", "ERROR_USERNAME_PARAMETER_NOT_FOUND");
define ("MISSING_EMAIL_PARAM", "ERROR_EMAIL_PARAMETER_NOT_FOUND");
define ("EMAIL_UNCHANGED", "ERROR_EMAIL_NEW_SAME_AS_OLD");
define("MISSING_PHONE_PARAM", "ERROR_PHONE_PARAMETER_NOT_FOUND");
define("MISSING_USERNAME_PARAM", "ERROR_USERNAME_PARAMETER_NOT_FOUND");
define("MISSING_EMAIL_PARAM", "ERROR_EMAIL_PARAMETER_NOT_FOUND");
define("EMAIL_UNCHANGED", "ERROR_EMAIL_NEW_SAME_AS_OLD");
/* Parameter not available because already in use */
define ("PHONE_TAKEN", "ERROR_ALIAS_ALREADY_IN_USE");
define ("USERNAME_TAKEN", "ERROR_ACCOUNT_ALREADY_IN_USE");
define ("EMAIL_TAKEN", "ERROR_EMAIL_ALREADY_IN_USE");
define("PHONE_TAKEN", "ERROR_ALIAS_ALREADY_IN_USE");
define("USERNAME_TAKEN", "ERROR_ACCOUNT_ALREADY_IN_USE");
define("EMAIL_TAKEN", "ERROR_EMAIL_ALREADY_IN_USE");
/* Object not found */
define ("ALIAS_NOT_FOUND", "ERROR_ALIAS_DOESNT_EXIST");
define ("ACCOUNT_NOT_FOUND", "ERROR_ACCOUNT_DOESNT_EXIST");
define ("PASSWORD_NOT_FOUND", "ERROR_PASSWORD_NOT_FOUND");
define ("USERINFO_NOT_FOUND", "ERROR_USERINFO_NOT_FOUND");
define("ALIAS_NOT_FOUND", "ERROR_ALIAS_DOESNT_EXIST");
define("ACCOUNT_NOT_FOUND", "ERROR_ACCOUNT_DOESNT_EXIST");
define("PASSWORD_NOT_FOUND", "ERROR_PASSWORD_NOT_FOUND");
define("USERINFO_NOT_FOUND", "ERROR_USERINFO_NOT_FOUND");
/* Equality check failure */
define ("KEY_DOESNT_MATCH", "ERROR_KEY_DOESNT_MATCH");
define ("PASSWORD_DOESNT_MATCH", "ERROR_PASSWORD_DOESNT_MATCH");
define ("EMAIL_DOESNT_MATCH", "ERROR_EMAIL_DOESNT_MATCH");
define ("ALIAS_DOESNT_MATCH", "ERROR_ALIAS_DOESNT_MATCH");
define("KEY_DOESNT_MATCH", "ERROR_KEY_DOESNT_MATCH");
define("PASSWORD_DOESNT_MATCH", "ERROR_PASSWORD_DOESNT_MATCH");
define("EMAIL_DOESNT_MATCH", "ERROR_EMAIL_DOESNT_MATCH");
define("ALIAS_DOESNT_MATCH", "ERROR_ALIAS_DOESNT_MATCH");
/* Disabled features */
define ("TEST_ACCOUNTS_DISABLED", "ERROR_NON_TEST_ACCOUNTS_UNAUTHORIZED");
define ("SMS_DISABLED", "ERROR_SMS_API_DISABLED");
define ("ALGO_NOT_SUPPORTED", "ERROR_ALGO_NOT_SUPPORTED");
define("TEST_ACCOUNTS_DISABLED", "ERROR_NON_TEST_ACCOUNTS_UNAUTHORIZED");
define("SMS_DISABLED", "ERROR_SMS_API_DISABLED");
define("ALGO_NOT_SUPPORTED", "ERROR_ALGO_NOT_SUPPORTED");
/* Unexpected state */
define ("ACCOUNT_ALREADY_ACTIVATED", "ERROR_ACCOUNT_ALREADY_ACTIVATED");
define ("ACCOUNT_NOT_YET_ACTIVATED", "ERROR_ACCOUNT_NOT_ACTIVATED");
define ("ACCOUNT_RECOVERY_IMPOSSIBLE", "ERROR_CANT_RECOVER_ACCOUNT");
define("ACCOUNT_ALREADY_ACTIVATED", "ERROR_ACCOUNT_ALREADY_ACTIVATED");
define("ACCOUNT_NOT_YET_ACTIVATED", "ERROR_ACCOUNT_NOT_ACTIVATED");
define("ACCOUNT_RECOVERY_IMPOSSIBLE", "ERROR_CANT_RECOVER_ACCOUNT");
/* Format error */
define ("PHONE_NOT_E164", "ERROR_PHONE_ISNT_E164");
define("PHONE_NOT_E164", "ERROR_PHONE_ISNT_E164");
/* SMS error */
define ("MAX_SMS_ALLOWED_EXCEEDED", "ERROR_MAX_SMS_EXCEEDED");
define ("SMS_API_FAILURE", "ERROR_CANT_SEND_SMS");
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");
define("GEOLOC_FAILED", "ERROR_GEOLOC_FAILED");
/* Other error */
define ('SHA256_PASSWORD_ALREADY_EXISTS', 'ERROR_SHA256_PASSWORD_ALREADY_EXISTS');
?>
define('SHA256_PASSWORD_ALREADY_EXISTS', 'ERROR_SHA256_PASSWORD_ALREADY_EXISTS');
......@@ -31,7 +31,8 @@ include_once __DIR__ . '/utilities.php';
// Internationalization
function get_sms_string_for_lang($lang) {
function get_sms_string_for_lang($lang)
{
global $SMS_OVH_TEMPLATE;
if (isset($SMS_OVH_TEMPLATE[$lang])) {
return $SMS_OVH_TEMPLATE[$lang];
......@@ -42,7 +43,8 @@ function get_sms_string_for_lang($lang) {
// SMS API
function send_sms_ovh($phone, $key, $lang) {
function send_sms_ovh($phone, $key, $lang)
{
if (!SMS_API_ENABLED) {
Logger::getInstance()->warning("[SMS] SMS API disabled");
return SMS_DISABLED;
......@@ -78,7 +80,7 @@ function send_sms_ovh($phone, $key, $lang) {
}
}
$message->addReceiver($phone);
$message->setIsMarketing(FALSE);
$message->setIsMarketing(false);
$text = get_sms_string_for_lang($lang);
$text = str_replace("#CODE#", $key, $text);
......@@ -90,14 +92,15 @@ function send_sms_ovh($phone, $key, $lang) {
$valid_receiver = $result['validReceivers'];
if (count($invalid_receiver) > 0) {
Logger::getInstance()->error("[SMS] phone number " . $phone . " seems invalid");
} else if (count($valid_receiver) > 0) {
} elseif (count($valid_receiver) > 0) {
Logger::getInstance()->message("[SMS] " . $text . " sent to " . $phone);
} else {
Logger::getInstance()->warning("[SMS] Both valid and invalid receiver lists are empty...");
}
}
function send_sms_legacy($phone, $password) {
function send_sms_legacy($phone, $password)
{
if (!SMS_API_ENABLED) {
Logger::getInstance()->warning("[SMS] SMS API disabled");
return SMS_DISABLED;
......@@ -121,7 +124,8 @@ function send_sms_legacy($phone, $password) {
curl_close($ch);
}
function send_sms($phone, $key, $lang) {
function send_sms($phone, $key, $lang)
{
if (!SMS_API_ENABLED) {
Logger::getInstance()->warning("[SMS] SMS API disabled");
return SMS_DISABLED;
......@@ -145,7 +149,7 @@ function send_sms($phone, $key, $lang) {
if ($sms->count >= SMS_COUNT_LIMIT_IN_PERIOD and $diff < SMS_TIME_PERIOD) {
Logger::getInstance()->error("[SMS] Last sms was sent at " . $sms->last_sms . ", time elapsed since then is " . $diff . "ms which is less than the configured time period " . SMS_TIME_PERIOD);
return MAX_SMS_ALLOWED_EXCEEDED;
} else if ($diff >= SMS_TIME_PERIOD) {
} elseif ($diff >= SMS_TIME_PERIOD) {
$sms->last_sms = $now;
$sms->count = 1;
$sms->update();
......@@ -159,14 +163,14 @@ function send_sms($phone, $key, $lang) {
$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 != "") {
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);
return OK;
} catch (Exception $e) {
Logger::getInstance()->error("[OVH-SMS] Exception: " . $e->getMessage());
}
} else if (SMS_API_URL != NULL && SMS_API_URL != "" && SMS_API_USERNAME != NULL && SMS_API_USERNAME != "" && SMS_API_PASSWORD != NULL && SMS_API_PASSWORD != "") {
} elseif (SMS_API_URL != null && SMS_API_URL != "" && SMS_API_USERNAME != null && SMS_API_USERNAME != "" && SMS_API_PASSWORD != null && SMS_API_PASSWORD != "") {
send_sms_legacy($phone, $key);
return OK;
} else {
......@@ -175,5 +179,3 @@ function send_sms($phone, $key, $lang) {
}
return SMS_API_FAILURE;
}
?>
......@@ -13,8 +13,8 @@ include_once __DIR__ . '/geoloc.php';
include_once __DIR__ . '/results_values.php';
// args = [username, ha1, firstname, lastname, gender, subscribe, [domain], [algo]]
function update_account_user_info($username, $ha1, $firstname, $lastname, $gender, $subscribe, $domain, $algo) {
function update_account_user_info($username, $ha1, $firstname, $lastname, $gender, $subscribe, $domain, $algo)
{
Logger::getInstance()->message("update_account_user_info(" . $username . ", " . $domain . " : " . $firstname . ", " . $lastname . ", " . $gender . ", " . $subscribe . ")");
$database = new Database();
......@@ -44,15 +44,15 @@ function update_account_user_info($username, $ha1, $firstname, $lastname, $gende
$user_info = new UserInfo($db);
$user_info->account_id = $account->id;
if(ENABLE_NEW_ACCOUNTS_GEOLOC){
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){
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{
else {
return GEOLOC_FAILED;
}
Logger::getInstance()->debug("Getting geoloc infos : country_code=".
......
......@@ -33,15 +33,18 @@ define('CLEAR', 'clrtxt');
define('MD5', 'MD5');
define('SHA256', 'SHA-256');
function startswith($hay, $needle) {
function startswith($hay, $needle)
{
return substr($hay, 0, strlen($needle)) === $needle;
}
function endswith($hay, $needle) {
return $needle === "" || (($temp = strlen($hay) - strlen($needle)) >= 0 and strpos($hay, $needle, $temp) !== FALSE);
function endswith($hay, $needle)
{
return $needle === "" || (($temp = strlen($hay) - strlen($needle)) >= 0 and strpos($hay, $needle, $temp) !== false);
}
function getIp() {
function getIp()
{
$ip = $_SERVER['REMOTE_ADDR'];
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
......@@ -51,28 +54,32 @@ function getIp() {
return $ip;
}
function get_trial_expiration_date() {
function get_trial_expiration_date()
{
$expiration_date = new DateTime('now +' . TRIAL_DURATION_DAYS . ' days');
$expiration = $expiration_date->getTimestamp() * 1000;
return $expiration;
}
function is_activated($activated) {
function is_activated($activated)
{
return $activated == "1" || $activated == 1 || $activated;
}
// XMLRPC parameters
function check_parameter($param, $param_name = "username") {
if ($param == NULL || $param == "") {
function check_parameter($param, $param_name = "username")
{
if ($param == null || $param == "") {
Logger::getInstance()->warning("Parameter " . $param_name . " is missing");
return false;
}
return true;
}
function get_algo($algo) {
if ($algo == NULL || $algo == "") {
function get_algo($algo)
{
if ($algo == null || $algo == "") {
Logger::getInstance()->warning("Algo parameter wasn't found, assume " . DEFAULT_ALGORITHM);
return DEFAULT_ALGORITHM;
}
......@@ -80,22 +87,24 @@ function get_algo($algo) {
return $algo;
}
Logger::getInstance()->error("Algo " . $algo . " is not supported");
return NULL;
return null;
}
function get_domain($param) {
if ($param == NULL || $param == "") {
function get_domain($param)
{
if ($param == null || $param == "") {
Logger::getInstance()->warning("Domain parameter wasn't found, assume " . SIP_DOMAIN);
$param = SIP_DOMAIN;
}
return $param;
}
function get_lang($param) {
if ($param == NULL || $param == "") {
function get_lang($param)
{
if ($param == null || $param == "") {
Logger::getInstance()->warning("lang parameter wasn't found, use US");
return 'US';
} else if (strlen($param) > 2) {
} elseif (strlen($param) > 2) {
$param = substr($param, 0, 2);
}
return strtoupper($param);
......@@ -103,25 +112,33 @@ function get_lang($param) {
// Password
function hash_password($user, $password, $domain, $algo) {
function hash_password($user, $password, $domain, $algo)
{
$hashed_password = $password;
if ($algo == "" || $algo == MD5) $hashed_password = hash("md5", $user . ":" . $domain . ":" . $password);
else if ($algo == SHA256) $hashed_password = hash("sha256", $user . ":" . $domain . ":" . $password);
else Logger::getInstance()->error("Algorithm not supported: " . $algo);
if ($algo == "" || $algo == MD5) {
$hashed_password = hash("md5", $user . ":" . $domain . ":" . $password);
} elseif ($algo == SHA256) {
$hashed_password = hash("sha256", $user . ":" . $domain . ":" . $password);
} else {
Logger::getInstance()->error("Algorithm not supported: " . $algo);
}
return $hashed_password;
}
function generate_password() {
function generate_password()
{
$generated_password = substr(str_shuffle(GENERATED_PASSWORD_CHARACTERS), 0, GENERATED_PASSWORD_LENGTH);
return $generated_password;
}
function generate_4_digits_code() {
function generate_4_digits_code()
{
$generated_password = substr(str_shuffle("0123456789"), 0, 4);
return $generated_password;
}
function password_match($pwd1, $pwd2) {
function password_match($pwd1, $pwd2)
{
if ($pwd1 != $pwd2) {
Logger::getInstance()->error("Password doesn't match");
return false;
......@@ -129,7 +146,8 @@ function password_match($pwd1, $pwd2) {
return true;
}
function is_key_matching($key, $account) {
function is_key_matching($key, $account)
{
$key_db = $account->confirmation_key;
if ($key == INVALID_CONFIRMATION_KEY || $key != $key_db) {
if ($key_db != INVALID_CONFIRMATION_KEY) {
......@@ -151,7 +169,8 @@ function is_key_matching($key, $account) {
// Time
function time_elapsed_as_string($secs) {
function time_elapsed_as_string($secs)
{
$bit = array(
'y' => $secs / 31556926 % 12,
'w' => $secs / 604800 % 52,
......@@ -161,10 +180,11 @@ function time_elapsed_as_string($secs) {
's' => $secs % 60
);
foreach($bit as $k => $v)
if($v > 0) $ret[] = $v . $k;
foreach ($bit as $k => $v) {
if ($v > 0) {
$ret[] = $v . $k;
}
}
return join(' ', $ret);
}
?>
......@@ -18,7 +18,8 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class Account {
class Account
{
private $conn;
public $id;
......@@ -33,11 +34,13 @@ class Account {
public $expire_time;
public $alias;
public function __construct($db) {
public function __construct($db)
{
$this->conn = $db;
}
public function __toString() {
public function __toString()
{
$to_string = "Account: ";
if (!empty($this->id)) {
$to_string = $to_string . "id=" . $this->id . ", ";
......@@ -66,7 +69,8 @@ class Account {
return substr($to_string, 0, -2);
}
function dropTable() {
public function dropTable()
{
$query = "DROP TABLE IF EXISTS " . ACCOUNTS_DB_TABLE;
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
......@@ -80,7 +84,8 @@ class Account {
return false;
}
function createTable() {
public function createTable()
{
$query = "CREATE TABLE IF NOT EXISTS " . ACCOUNTS_DB_TABLE . " (
id INTEGER(11) UNSIGNED NOT NULL AUTO_INCREMENT,
username VARCHAR(64) NOT NULL,
......@@ -105,7 +110,8 @@ class Account {
return false;
}
function delete() {
public function delete()
{
$query = "DELETE FROM " . ACCOUNTS_DB_TABLE . " WHERE id = ?";
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
......@@ -121,7 +127,8 @@ class Account {
return false;
}
function create() {
public function create()
{
$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";
......@@ -164,7 +171,8 @@ class Account {
return false;
}
function update() {
public function update()
{
$query = "UPDATE " . ACCOUNTS_DB_TABLE . " SET username=:username, domain=:domain, activated=:activated";
if (!empty($this->email)) {
......@@ -213,7 +221,8 @@ class Account {
return false;
}
function getCount() {
public function getCount()
{
$query = "SELECT count(*) FROM " . ACCOUNTS_DB_TABLE;
$stmt = $this->conn->prepare($query);
Logger::getInstance()->debug("GetCount " . (string)$this);
......@@ -225,7 +234,8 @@ class Account {
return -1;
}
function getAll() {
public function getAll()
{
$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);
......@@ -234,24 +244,25 @@ class Account {
return $stmt;
}
function getOne() {
public function getOne()
{
$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)) {
$query = $query . " WHERE ac.id = ?";
$this->id = htmlspecialchars(strip_tags($this->id));
} else if (!empty($this->username)) {
} elseif (!empty($this->username)) {
$query = $query . " WHERE ac.username = ?";
$this->username = htmlspecialchars(strip_tags($this->username));
if (!empty($this->domain)) {
$query = $query . " AND ac.domain = ?";
$this->domain = htmlspecialchars(strip_tags($this->domain));
}
} else if (!empty($this->email)) {
} elseif (!empty($this->email)) {
$query = $query . " WHERE ac.email = ?";
$this->email = htmlspecialchars(strip_tags($this->email));
} else if (!empty($this->confirmation_key)) {
} elseif (!empty($this->confirmation_key)) {
$query = $query . " WHERE ac.confirmation_key = ?";