public static function run() { // get site name $result = \DB::select('config_name', 'config_value')->from('config')->where('config_name', 'site_name')->as_object()->execute(); $row = $result->current(); $site_name = $row->config_value; unset($result, $row); // get domain if (isset($_SERVER['HTTP_HOST'])) { $site_domain = $_SERVER['HTTP_HOST']; } elseif (isset($_SERVER['SERVER_NAME'])) { $site_domain = $_SERVER['SERVER_NAME']; } else { $site_domain = 'localhost'; } $sql = "CREATE TABLE IF NOT EXISTS `" . \DB::table_prefix('sites') . "` (\n `site_id` int(11) NOT NULL AUTO_INCREMENT,\n `site_name` varchar(255) DEFAULT NULL,\n `site_domain` varchar(255) DEFAULT NULL COMMENT 'ex. domain.com, sub.domain.com with out http://',\n `site_status` int(1) NOT NULL DEFAULT '0' COMMENT '0=disable, 1=enable',\n `site_create` bigint(20) DEFAULT NULL,\n `site_create_gmt` bigint(20) DEFAULT NULL,\n `site_update` bigint(20) DEFAULT NULL,\n `site_update_gmt` bigint(20) DEFAULT NULL,\n PRIMARY KEY (`site_id`)\n ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;"; \DB::query($sql)->execute(); // check if table already created before insert. $result = \DB::count_records('sites'); if ($result <= 0) { $sql = "INSERT INTO `" . \DB::table_prefix('sites') . "` (`site_id`, `site_name`, `site_domain`, `site_status`, `site_create`, `site_create_gmt`, `site_update`, `site_update_gmt`) VALUES\n (1, '" . $site_name . "', '" . $site_domain . "', 1, " . time() . ", " . \Extension\Date::localToGmt() . ", " . time() . ", " . \Extension\Date::localToGmt() . ");"; \DB::query($sql)->execute(); } unset($sql); return true; }
/** * add login session * * @param array $data */ public function addLoginSession($data = array()) { if (!isset($data['site_id'])) { $site_id = \Model_Sites::getSiteId(false); } else { $site_id = $data['site_id']; } unset($data['site_id']); // find exists last login on target site id. $result = \DB::select()->as_object()->from(static::$_table_name)->where('account_id', $data['account_id'])->where('site_id', $site_id)->execute(); if (count($result) <= 0) { // use insert $insert['account_id'] = $data['account_id']; $insert['site_id'] = $site_id; $insert['account_last_login'] = time(); $insert['account_last_login_gmt'] = \Extension\Date::localToGmt(); if (isset($data['session_id'])) { $insert['account_online_code'] = $data['session_id']; } \DB::insert(static::$_table_name)->set($insert)->execute(); unset($insert); } else { // use update $update['account_last_login'] = time(); $update['account_last_login_gmt'] = \Extension\Date::localToGmt(); if (isset($data['session_id'])) { $update['account_online_code'] = $data['session_id']; } \DB::update(static::$_table_name)->where('account_id', $data['account_id'])->where('site_id', $site_id)->set($update)->execute(); unset($update); } unset($result, $site_id); }
/** * register new account * * @param array $data * @param array $data_fields additional fields to store in account_fields table. * @return boolean|string return true when completed and return error text when error occured. */ public static function registerAccount($data = array(), $data_fields = array()) { // check required data. if (empty($data) || !is_array($data)) { return false; } // get configurations db $cfg = \Model_Config::getvalues(array('member_verification', 'member_disallow_username')); // verify disallow username. if (isset($cfg['member_disallow_username']['value'])) { $cfg['member_disallow_username']['value'] = str_replace(', ', ',', $cfg['member_disallow_username']['value']); $disallow_usernames = explode(',', $cfg['member_disallow_username']['value']); foreach ($disallow_usernames as $disallow_username) { if ($data['account_username'] == trim($disallow_username)) { unset($cfg, $disallow_username, $disallow_usernames); return \Lang::get('account_username_disallowed'); } } } // check duplicate username. $query = static::query()->select('account_username')->where('account_username', $data['account_username']); if ($query->count() > 0) { unset($query); return \Lang::get('account_username_already_exists'); } unset($query); // check duplicate email. $query = static::query()->select('account_email')->where('account_email', $data['account_email']); if ($query->count() > 0) { unset($query); return \Lang::get('account_email_already_exists'); } unset($query); if ($cfg['member_verification']['value'] != '0') { // admin config need to verify. // generate confirm code $data['account_confirm_code'] = \Str::random('alnum', 6); } // send register email $send_result = static::instance()->sendRegisterEmail($data); if ($send_result !== true) { return $send_result; } unset($send_result); $data['account_password'] = static::instance()->hashPassword($data['account_password']); $data['account_create'] = time(); $data['account_create_gmt'] = \Extension\Date::localToGmt(); if ($cfg['member_verification']['value'] == '0') { // admin config to no need to verify. $data['account_status'] = '1'; } else { $data['account_status'] = '0'; if ($cfg['member_verification']['value'] == '2') { $data['account_status_text'] = \Lang::get('account_waiting_for_admin_verification'); } else { $data['account_status_text'] = \Lang::get('account_please_confirm_registration_from_your_email'); } } // add account to db. ---------------------------------------- //list($account_id) = \DB::insert(static::$_table_name)->set($data); // query builder style. $account = static::forge($data); // add level to user for current site. $account->account_level[0] = new Model_AccountLevel(); $account->account_level[0]->level_group_id = 3; $account->save(); $account_id = $account->account_id; unset($account); // end add account to db ------------------------------------- // add level to user. // loop sites to add level of this user to each site. $site_id = \Model_Sites::getSiteId(false); $list_site_option['list_for'] = 'admin'; $list_site_option['unlimit'] = true; $sites = \Model_Sites::listSites($list_site_option); if (isset($sites['items']) && is_array($sites['items']) && !empty($sites['items'])) { foreach ($sites['items'] as $site) { if ($site->site_id != $site_id) { if ($site->site_id == '1') { $table_name = 'account_level'; } else { $table_name = $site->site_id . '_account_level'; } \DB::insert($table_name)->set(array('account_id' => $account_id, 'level_group_id' => '3'))->execute(); } } } unset($list_site_option, $site, $sites, $site_id, $table_name); // add account fields if there is any value. // to add account fields data structure shoud be like this... // array(array('field_name' => 'website', 'field_value' => 'http://domain.tld'), array('field_name' => 'fb', 'field_value' => 'http://fb.com/myprofile')); // or // $af[0]['field_name'] = 'website'; // $af[0]['field_value'] = 'http://domain.tld'; // $sf[1]['field_name'] = 'fb'; // $sf[1]['field_value'] = 'http://fb.com/myprofile'; if (!empty($data_fields) && is_array($data_fields)) { foreach ($data_fields as $field) { $account_fields = static::forge($field); $account_fields->account_id = $account_id; $account_fields->save(); } unset($account_fields, $field); } // @todo [fuelstart][account][plug] account after register plug. // after saved newly user data but not confirm (if require confirm). $plugin = new \Library\Plugins(); if ($plugin->hasAction('AccountAfterRegister') !== false) { $plugin->doAction('AccountAfterRegister', ['input_data' => $data, 'input_data_fields' => $data_fields, 'inputs_post' => \Input::post()]); } unset($plugin); return true; }
/** * record login * @param integer $account_id * @param integer $attempt 0 for failed, 1 for success * @param string $attempt_text attempt text * @return boolean */ public function recordLogin($account_id = '', $attempt = '0', $attempt_text = '') { if (!is_numeric($account_id) || !is_numeric($attempt)) { return false; } if ($attempt_text == null) { $attempt_text = null; } $site_id = \Model_Sites::getSiteId(false); // get browser class for use instead of fuelphp agent which is does not work. include_once APPPATH . 'vendor' . DS . 'browser' . DS . 'lib' . DS . 'Browser.php'; $browser = new Browser(); // set data for insertion $data['account_id'] = $account_id; $data['site_id'] = $site_id; $data['login_ua'] = \Input::user_agent(); $data['login_os'] = $browser->getPlatform(); $data['login_browser'] = $browser->getBrowser() . ' ' . $browser->getVersion(); $data['login_ip'] = \Input::real_ip(); $data['login_time'] = time(); $data['login_time_gmt'] = \Extension\Date::localToGmt(); $data['login_attempt'] = $attempt; $data['login_attempt_text'] = $attempt_text; \DB::insert(static::$_table_name)->set($data)->execute(); unset($browser, $data, $site_id); return true; }
/** * edit site. update site name to config table too. * * @param array $data * @return boolean */ public static function editSite(array $data = array()) { // check site_domain not exists in other site_id $match_sites = \DB::select()->from(static::$_table_name)->where('site_id', '!=', $data['site_id'])->where('site_domain', $data['site_domain'])->execute(); if (count($match_sites) > 0) { unset($match_sites); return \Lang::get('siteman_domain_currently_exists'); } unset($match_sites); // additional data for updating $data['site_update'] = time(); $data['site_update_gmt'] = \Extension\Date::localToGmt(); // filter data before update if ($data['site_id'] == '1') { // site 1 always enabled. $data['site_status'] = '1'; } $site_id = $data['site_id']; unset($data['site_id']); // update to db \DB::update(static::$_table_name)->where('site_id', $site_id)->set($data)->execute(); // set config for new site. $cfg_data['site_name'] = $data['site_name']; if ($site_id == '1') { $config_table = 'config'; } else { $config_table = $site_id . '_config'; } foreach ($cfg_data as $cfg_name => $cfg_value) { \DB::update($config_table)->where('config_name', $cfg_name)->value('config_value', $cfg_value)->execute(); } unset($cfg_data, $cfg_name, $cfg_value); // clear cache \Extension\Cache::deleteCache('model.sites-getSiteId'); \Extension\Cache::deleteCache('model.sites-isSiteEnabled'); \Extension\Cache::deleteCache('controller.AdminController-generatePage-fs_list_sites'); // done return true; }