function socialLogin($code, $user_id, $name, $email, $photo, $accessToken, $refreshToken) { if ($this->isLogged() == true) { $check = $this->db()->select($this->table->social)->where('midx', $this->getLogged())->where('code', $code)->getOne(); if ($check == null) { $this->db()->insert($this->table->social, array('midx' => $this->getLogged(), 'code' => $code, 'user_id' => $user_id, 'email' => $email, 'access_token' => $accessToken, 'refresh_token' => $refreshToken))->execute(); } else { $this->db()->update($this->table->social, array('user_id' => $user_id, 'email' => $email, 'access_token' => $accessToken, 'refresh_token' => $refreshToken))->where('midx', $this->getLogged())->where('code', $code)->execute(); } if (file_exists($this->IM->getAttachmentPath() . '/member/' . $this->getLogged() . '.jpg') == false) { if (SaveFileFromUrl($photo, $this->IM->getAttachmentPath() . '/member/' . $this->getLogged() . '.jpg', 'image') == true) { $this->IM->getModule('attachment')->createThumbnail($this->IM->getAttachmentPath() . '/member/' . $this->getLogged() . '.jpg', $this->IM->getAttachmentPath() . '/member/' . $this->getLogged() . '.jpg', 250, 250, false, 'jpg'); } } $_SESSION['MEMBER_MODIFY_PASSWORD'] = true; } else { $check = $this->db()->select($this->table->social)->where('code', $code)->where('user_id', $user_id)->get(); if (count($check) == 0) { $checkEmail = $this->db()->select($this->table->member)->where('email', $email)->getOne(); if ($checkEmail == null) { $insert = array(); $insert['type'] = 'MEMBER'; $insert['gidx'] = 'default'; $insert['email'] = $email; $insert['password'] = ''; $insert['name'] = $insert['nickname'] = $name; $insert['reg_date'] = $insert['last_login'] = time(); $insert['status'] = 'ACTIVE'; $idx = $this->db()->insert($this->table->member, $insert)->execute(); $this->login($idx); header('location:' . $this->IM->getProcessUrl('member', $code)); exit; } elseif (strlen($checkEmail->password) == 65) { $config = new stdClass(); $config->type = 'duplicated'; $config->member = $this->getMember($checkEmail->idx); $this->IM->addSiteHeader('script', __IM_DIR__ . '/scripts/php2js.js.php?language=' . $this->IM->language); $context = $this->getContext('social', $config); $header = $this->IM->printHeader(); $footer = $this->IM->printFooter(); echo $header; echo $context; echo $footer; exit; } else { $this->login($checkEmail->idx); } } elseif (count($check) == 1) { $this->login($check[0]->midx); } else { $config = new stdClass(); $config->type = 'select'; $config->account = $check; $config->redirectUrl = Request('SOCIAL_REDIRECT_URL', 'session') != null ? Request('SOCIAL_REDIRECT_URL', 'session') : '/'; $config->photo = $photo; $this->IM->addSiteHeader('script', __IM_DIR__ . '/scripts/php2js.js.php?language=' . $this->IM->language); $context = $this->getContext('social', $config); $header = $this->IM->printHeader(); $footer = $this->IM->printFooter(); echo $header; echo $context; echo $footer; unset($_SESSION['OAUTH_ACCESS_TOKEN']); unset($_SESSION['OAUTH_REFRESH_TOKEN']); unset($_SESSION['SOCIAL_REDIRECT_URL']); exit; } } unset($_SESSION['OAUTH_ACCESS_TOKEN']); unset($_SESSION['OAUTH_REFRESH_TOKEN']); $redirectUrl = Request('SOCIAL_REDIRECT_URL', 'session') != null ? Request('SOCIAL_REDIRECT_URL', 'session') : '/'; unset($_SESSION['SOCIAL_REDIRECT_URL']); header('location:' . $redirectUrl); }
/** * login from social account * * @param string $code via social (facebook, google, github) * @param string $user_id social account id * @param string $name social account name * @param string $email social account email * @param string $photo social account photo url path * @param string $accessToken social oauth access_token * @param string $refreshToken social oauth refresh_token * @return null if run this function, just redirect url directly */ function socialLogin($code, $user_id, $name, $email, $photo, $accessToken, $refreshToken) { $siteType = $this->IM->getSites($this->IM->domain)->member; $OAUTH = $this->db()->select($this->table->social_oauth)->where('domain', $this->IM->domain)->where('code', $code)->getOne(); if ($OAUTH == null) { $this->IM->printError('OAUTH_DOMAIN_ERROR'); } $CLIENT_ID = $OAUTH->client_id; if ($this->isLogged() == true) { $check = $this->db()->select($this->table->social_token)->where('midx', $this->getLogged())->where('code', $code)->getOne(); if ($check == null) { $this->db()->insert($this->table->social_token, array('midx' => $this->getLogged(), 'code' => $code, 'client_id' => $CLIENT_ID, 'user_id' => $user_id, 'email' => $email, 'access_token' => $accessToken, 'refresh_token' => $refreshToken))->execute(); } else { $this->db()->update($this->table->social_token, array('client_id' => $CLIENT_ID, 'user_id' => $user_id, 'email' => $email, 'access_token' => $accessToken, 'refresh_token' => $refreshToken))->where('midx', $this->getLogged())->where('code', $code)->execute(); } // if not exists user's photo, get social photo if (file_exists($this->IM->getAttachmentPath() . '/member/' . $this->getLogged() . '.jpg') == false) { if (SaveFileFromUrl($photo, $this->IM->getAttachmentPath() . '/member/' . $this->getLogged() . '.jpg', 'image') == true) { $this->IM->getModule('attachment')->createThumbnail($this->IM->getAttachmentPath() . '/member/' . $this->getLogged() . '.jpg', $this->IM->getAttachmentPath() . '/member/' . $this->getLogged() . '.jpg', 250, 250, false, 'jpg'); } } $_SESSION['MEMBER_MODIFY_PASSWORD'] = true; } else { $check = $this->db()->select($this->table->social_token)->where('code', $code)->where('user_id', $user_id)->get(); if (count($check) == 0) { // not connected member idx and social token. $checkEmail = $this->db()->select($this->table->member)->where('email', $email)->where('domain', $siteType == 'MERGE' ? '*' : $this->IM->domain)->getOne(); // cannot find social member's email address on im_member_table, search administator if ($checkEmail == null) { $checkEmail = $this->db()->select($this->table->member)->where('email', $email)->where('type', 'ADMINISTRATOR')->getOne(); } if ($checkEmail == null) { // not found social member's email, new member register $insert = array(); $insert['type'] = 'MEMBER'; $insert['domain'] = $siteType == 'MERGE' ? '*' : $this->IM->domain; $insert['email'] = $email; $insert['password'] = ''; $insert['name'] = $insert['nickname'] = $name; $insert['reg_date'] = $insert['last_login'] = time(); $insert['status'] = 'ACTIVE'; $idx = $this->db()->insert($this->table->member, $insert)->execute(); $this->login($idx); header('location:' . $this->IM->getProcessUrl('member', $code)); exit; } elseif (strlen($checkEmail->password) == 65) { // found member and exists password, check account password $config = new stdClass(); $config->type = 'duplicated'; $config->member = $this->getMember($checkEmail->idx); $this->IM->addSiteHeader('script', __IM_DIR__ . '/scripts/php2js.js.php?language=' . $this->IM->language); $context = $this->getContext('social', $config); $header = $this->IM->printHeader(); $footer = $this->IM->printFooter(); echo $header; echo $context; echo $footer; exit; } else { // found member and not exists password(someone used social login only), login directly. $this->login($checkEmail->idx); $this->db()->update($this->table->social_token, array('client_id' => $CLIENT_ID, 'user_id' => $user_id, 'email' => $email, 'access_token' => $accessToken, 'refresh_token' => $refreshToken))->where('midx', $checkEmail->idx)->where('code', $code)->execute(); } } elseif (count($check) == 1) { // only one account connected via social login, login directly. $this->login($check[0]->midx); $this->db()->update($this->table->social_token, array('client_id' => $CLIENT_ID, 'user_id' => $user_id, 'email' => $email, 'access_token' => $accessToken, 'refresh_token' => $refreshToken))->where('midx', $check[0]->midx)->where('code', $code)->execute(); } else { // multiple account connected via social login, select account. $config = new stdClass(); $config->type = 'select'; $config->account = $check; $config->redirectUrl = Request('SOCIAL_REDIRECT_URL', 'session') != null ? Request('SOCIAL_REDIRECT_URL', 'session') : '/'; $config->photo = $photo; $this->IM->addSiteHeader('script', __IM_DIR__ . '/scripts/php2js.js.php?language=' . $this->IM->language); $context = $this->getContext('social', $config); $header = $this->IM->printHeader(); $footer = $this->IM->printFooter(); echo $header; echo $context; echo $footer; unset($_SESSION['OAUTH_ACCESS_TOKEN']); unset($_SESSION['OAUTH_REFRESH_TOKEN']); unset($_SESSION['SOCIAL_REDIRECT_URL']); exit; } } unset($_SESSION['OAUTH_ACCESS_TOKEN']); unset($_SESSION['OAUTH_REFRESH_TOKEN']); $redirectUrl = Request('SOCIAL_REDIRECT_URL', 'session') != null ? Request('SOCIAL_REDIRECT_URL', 'session') : '/'; unset($_SESSION['SOCIAL_REDIRECT_URL']); header('location:' . $redirectUrl); }