Esempio n. 1
0
 /**
  * Creates a user with passed data and returns it
  *
  * @param array $details
  * @return mixed
  */
 public static function createUser($details)
 {
     // Return false if absolutely needed data is not passed or invalid
     if (empty($details['name']) || strlen($details['name']) < 3 || empty($details['username']) || !preg_match("/^[0-9a-z]{4,}\$/i", $details['username']) || empty($details['email']) || !filter_var($details['email'], FILTER_VALIDATE_EMAIL) || empty($details['password']) || strlen($details['password']) < 6) {
         return false;
     }
     // Check for already used username/email
     $errors = [];
     $user = new \Models\User();
     $user->load(array("email=?", $details['email']));
     // Email
     if ($user->id) {
         $user->reset();
         $errors[] = "A user already exists with this email address";
     }
     $user->load(array("username=?", $details['username']));
     // Username
     if ($user->id) {
         $user->reset();
         $errors[] = "A user already exists with this username.";
     }
     if (!empty($errors)) {
         // Return any errors
         return $errors;
     }
     // Generate password and salt
     // The extract makes the variables $hash and $salt
     $security = Security::instance();
     extract($security->hash($details['password']));
     // Enter user info
     $user->reset();
     // Delete all the content from the checks
     $user->name = $details['name'];
     $user->username = strtolower($details['username']);
     $user->email = strtolower($details['email']);
     $user->password = $hash;
     $user->salt = $salt;
     $user->rank = isset($details['rank']) ? $details['rank'] : 1;
     $user->email_verified = isset($details['email_verified']) ? $details['email_verified'] : 0;
     $user->age = isset($details['age']) ? $details['age'] : 0;
     $user->perms = isset($details['permissions']) ? $details['permissions'] : json_encode([]);
     $user->suspended_time = isset($details['suspended_time']) ? $details['suspended_time'] : null;
     $user->register_time = isset($details['register_time']) ? $details['register_time'] : date("Y-m-d H:i:s");
     $user->save();
     return $user;
 }