Пример #1
0
 /**
  * Create new user
  *
  * @param   string
  * @param   string
  * @param   string  must contain valid email address
  * @param   int     group id
  * @param   Array
  * @return  bool
  */
 public function create_user($username, $password, $email, $group = 1, array $profile_fields = array())
 {
     // prep the password
     $password = trim($password);
     // and validate the email address
     $email = filter_var(trim($email), FILTER_VALIDATE_EMAIL);
     // bail out if we're missing username, password or email address
     if (empty($username) or empty($password) or empty($email)) {
         throw new \SimpleUserUpdateException('Username, password or email address is not given, or email address is invalid', 1);
     }
     // check if we already have an account with this email address or username
     $duplicate = \Model\Auth_User::query()->select(\Config::get('ormauth.table_columns', array()))->where('username', '=', $username)->or_where('email', '=', $email)->get_one();
     // did we find one?
     if ($duplicate) {
         // bail out with an exception
         if (strtolower($email) == strtolower($duplicate->email)) {
             throw new \SimpleUserUpdateException('Email address already exists', 2);
         } else {
             throw new \SimpleUserUpdateException('Username already exists', 3);
         }
     }
     // do we have a logged-in user?
     if ($currentuser = \Auth::get_user_id()) {
         $currentuser = $currentuser[1];
     } else {
         $currentuser = 0;
     }
     // create the new user record
     $user = \Model\Auth_User::forge(array('username' => (string) $username, 'password' => $this->hash_password((string) $password), 'email' => $email, 'group_id' => (int) $group, 'last_login' => 0, 'previous_login' => 0, 'login_hash' => '', 'user_id' => $currentuser, 'created_at' => \Date::forge()->get_timestamp(), 'updated_at' => 0));
     // we don't use profile fields, store the data in the metadata table instead
     foreach ($profile_fields as $field => $value) {
         $user->metadata[] = \Model\Auth_Metadata::forge(array('key' => $field, 'value' => $value));
     }
     // save the new user record
     try {
         $result = $user->save();
     } catch (\Exception $e) {
         $result = false;
     }
     // and the id of the created user, or false if creation failed
     return $result ? $user->id : false;
 }
Пример #2
0
 public static function shortenit($url, $custom = null, $api = null)
 {
     if (empty($api) === false || Input::is_ajax() === true) {
         $ajax = true;
     } else {
         $ajax = false;
     }
     if ($url[strlen($url) - 1] == "/") {
         $url = substr($url, 0, -1);
     }
     if (!preg_match("/^(ht|f)t(p|ps)\\:\\/\\//si", $url)) {
         $url = "http://" . $url;
     }
     $length = strlen($url);
     $count = 0;
     $user_id = 0;
     if (empty($api) === false && $api !== true) {
         $user = \Model\Auth_Metadata::query()->where('key', 'api_key')->where('value', $api)->get_one();
         if (empty($user) === false) {
             $user_id = $user->id;
         } else {
             echo 'Not a valid API Key!';
         }
     } else {
         $user_id = static::$user_id;
     }
     if (empty($user_id) === true) {
         $user_id = 0;
     }
     if (empty($custom) === false) {
         $short_url = str_replace(" ", "", $custom);
         $found_url = Model_Url::query()->where('short_url', $short_url)->get_one();
         if (empty($found_url) === false) {
             if ($ajax === true) {
                 echo 'Custom Short URL already taken , choose a differnt name';
                 die;
             } else {
                 Session::set('error', 'Custom Short URL already taken , choose a differnt name');
                 Response::Redirect(Uri::Base());
             }
         }
         $custom = true;
     } else {
         $custom = false;
         $success = false;
         while ($success === false) {
             $short_url = Controller_Url::generate_url();
             $found_url = Model_Url::query()->where('short_url', $short_url)->get_one();
             if (empty($found_url) === true) {
                 $success = true;
             }
         }
     }
     $new_url = Model_Url::Forge(array('short_url' => $short_url, 'url' => $url, 'hits' => 0, 'custom' => $custom, 'user_id' => $user_id));
     if ($new_url->save()) {
         if ($ajax === false) {
             Session::set('success', 'Url has been shortened!');
         }
     } else {
         if ($ajax === true) {
             echo 'Unknown Error';
             die;
         } else {
             Session::set('error', 'Unknown Error');
             Response::Redirect(Uri::Base());
         }
     }
     return $new_url;
 }