<?php
namespace app\controllers;
use Yii;
use yii\web\controllers;
use yii\web\View;
use linslin\yii2\curl\Curl;
class SiteController extends Controller{
public function actionIndex(){
$this->registerCssFile("/css/inpage_codes/homepage_styles.css", 'homepage-ui');
$this->registerJsFile("/js/inpage_codes/homepage_script.js", View::POS_END, 'homepage-ux');
return $this->render('index');
}
public function actionAccountService($service){
$q = json_decode($_POST['serviceQuery']);
switch($service){
case "signIn":
if($_POST['serviceQuery']){
$sign = $q['asq']; //Authoriation service query
$type = $q['asqt']; //ASQ Type
Yii::app->portalUserService->SignIn->proccess($sign,$type);
}
else{ throw new HttpException(405 ,'Query conflict'); }
break;
case "signUp":
if($_POST['serviceQuery']){
$sign = $q['rsq']; //Registration service query
$type = $q['rsqt']; //RSQ Type
Yii::app->portalUserService->SignUp->proccess($sign,$type);
}
else{ throw new HttpException(405 ,'Query conflict'); }
break;
case "forgot":
if($_POST['serviceQuery']){
$sign = $q['fsq']; //Forgot service query
Yii::app->portalUserService->Forgot->proccess($sign);
}
else{ throw new HttpException(405 ,'Query conflict'); }
break;
case "autoAuth":
if($_POST['serviceQuery']){
$sign = $q['fsq']; //Forgot service query
Yii::app->portalUserService->AutoSignIn->proccess($sign);
}
else{ throw new HttpException(405 ,'Query conflict'); }
break;
case "signOut":
if(!Yii::$app->user->isGuest){ Yii::app->portalUserService->SignOut->proccess(); }
else{ throw new HttpException(405 ,'Service conflict'); }
break;
default: throw new HttpException(404 ,'Service not found'); break;
}
}
public function actionServiceCodeCenter($service){
$q = json_decode($_POST['serviceQuery']);
switch($service){
case "signUp":
if($_POST['serviceQuery']){
$sign = $q['rsq']; //Registration service query
if($sign['service'] === 'Inbox'){
$query = json_encode(['fsq' => ['svc' => 'SignUp']]);
$wsInit = new Curl();
$query = $wsInit->post((!empty($_SERVER['HTTPS'])) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] ."/accounts/accept/codeGenerator")->setOption(CURLOPT_POSTFIELDS, http_build_query(array('serviceQuery' => $query)));
$sign['code'] = $query;
}
if($sign['service'] === 'Inbox'){ Yii::app->portalCommunicationService->SMSCode->sendCode('SignUp', $sign['phone']); }
else if($sign['service'] === 'Valid'){ Yii::app->portalCommunicationService->SMSCode->validCode('SignUp', $sign['phone'], $sign['code']); }
else{ throw new HttpException(403 ,'Operation conflict'); }
}
else{ throw new HttpException(405 ,'Query conflict'); }
break;
case "forgot":
if($_POST['serviceQuery']){
$sign = $q['fsq']; //Forgot service query
if($sign['service'] === 'Inbox'){
$query = json_encode(['fsq' => ['svc' => 'Forgot']]);
$wsInit = new Curl();
$query = $wsInit->post((!empty($_SERVER['HTTPS'])) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] ."/accounts/accept/codeGenerator")->setOption(CURLOPT_POSTFIELDS, http_build_query(array('serviceQuery' => $query)));
$sign['code'] = $query;
}
if($sign['service'] === 'Inbox'){ Yii::app->portalCommunicationService->SMSCode->sendCode('Forgot', $sign['phone']); }
else if($sign['service'] === 'Valid'){ Yii::app->portalCommunicationService->SMSCode->validCode('Forgot', $sign['phone'], $sign['code']); }
else{ throw new HttpException(403 ,'Operation conflict'); }
}
else{ throw new HttpException(405 ,'Query conflict'); }
break;
case "codeGenerator":
if($_POST['serviceQuery']){
$source = $q['fsq'];
$generateCode = [
ceil(getRandomFromRange(1000,9999)),
ceil(getRandomFromRange(2000,4600))
];
$isSignUp = $source['svc'] === 'SignUp' ? TRUE : FALSE;
$isForgot = $source['svc'] === 'Forgot' ? TRUE : FALSE;
if($isSignUp){ $newCode = $generateCode[0]; }
else if($isForgot){ $newCode = $generateCode[1]; }
throw new HttpException(201 ,$newCode);
}
else{ throw new HttpException(405 ,'Query conflict'); }
break;
default: throw new HttpException(404 ,'Service not found'); break;
}
}
}
?>
<?php
namespace yii\components\SignService;
use yii\base\Component;
use yii\helpers\Json;
use app\models\UserService;
class SignUp extends Component{
public $type;
public $signQuery;
public function init(){
parent::init();
$this->signQuery = [];
$this->type = '';
}
public function proccess($signQuery = null, $type = ''){
if($signQuery != null && $type != ''){
$this->signQuery = $signQuery;
$this->type = $type;
}
$upModel = [
User::find(),
new User()
];
if($type === 'fbService'){
$token = $this->signQuery['fbToken'];
}
else{
$login = $this->signQuery['login'];
$pass = sha1($this->signQuery['password']);
$firstname = $this->signQuery['fn'];
$surname = $this->signQuery['sn'];
$mail = $this->signQuery['email'];
$phone = $this->signQuery['phone'];
$region = $this->signQuery['country'];
$isAccept = $this->signQuery['isAccept'] === TRUE;
$validLogin = $upModel[0]->where(['login' => $login])->all();
$validEMail = $upModel[0]->where(['email' => $mail])->all();
$validPassword = $upModel[0]->where(['password' => $pass])->all();
$validPhone = $upModel[0]->where(['phone' => $phone])->all();
if(!$validLogin && !$validEMail && !$validPassword && !$validPhone){
if($isAccept){
$upModel[1]->firstname = $firstname;
$upModel[1]->surname = $surname;
$upModel[1]->login = $login;
$upModel[1]->password = $pass;
$upModel[1]->email = $mail;
$upModel[1]->phone = $phone;
$upModel[1]->country = $region;
$upModel[1]->isAccept= TRUE;
if($upModel[1]->save()){ throw new HttpException(202 ,'Registration success!'); }
else{ throw new HttpException(409 ,'The portal accounting service is temporarily unavailable! Try again later;-('); }
}
else{ throw new HttpException(500 ,'New account data not is accept!'); }
}
else{
$validError = [];
header('Content-type: application/json;charset=UTF-8');
if($validLogin){ $validError[]['validError'] = 'The login you entered exists'; }
if($validEMail){ $validError[]['validError'] = 'The e-mail you entered exists'; }
if($validPassword){ $validError[]['validError'] = 'The password you entered exists'; }
if($validPhone){ $validError[]['validError'] = 'The phone number you entered exists'; }
throw new HttpException(400 ,Json::encode($validError));
}
}
}
}
class SignIn extends Component{
public $type;
public $signQuery;
public function init(){
parent::init();
$this->signQuery = [];
$this->type = '';
}
public function proccess($signQuery = null, $type = ''){
if($signQuery != null && $type != ''){
$this->signQuery = $signQuery;
$this->type = $type;
}
$inModel = User::find();
if($type === 'fbService'){
$token = $this->signQuery['fbToken'];
}
else{
$login = $this->signQuery['portalId'];
$pass = sha1($this->signQuery['password']);
$isLogin = $inModel->where(['login' => $login])->all() || $inModel->where(['email' => $login])->all() || $inModel->where(['phone' => $login])->all();
$isPass = $inModel->where(['password' => $pass]);
if($isLogin && $isPass){
$auth = User::findOne(['login' => $login]) || User::findOne('email' => $login]) || User::findOne(['phone' => $login]);
if(Yii::$app->user->login($auth)){ throw new HttpException(202 ,'Authorization success!'); }
else{ throw new HttpException(409 ,'The portal accounting service is temporarily unavailable! Try again later;-('); }
}
else{
$validError = [];
header('Content-type: application/json;charset=UTF-8');
if(!$isLogin){ $validError[]['validError'] = 'The login you entered no exists'; }
if(!$isPass){ $validError[]['validError'] = 'The password you entered exists'; }
throw new HttpException(400 ,Json::encode($validError));
}
}
}
}
class AutoSignIn extends Component{
public $signQuery;
public function init(){
parent::init();
$this->signQuery = [];
}
public function proccess($signQuery = null){
if($signQuery != null){ $this->signQuery = $signQuery; }
$inModel = User::find();
$login = $this->signQuery['portalId'];
$auth = User::findOne(['login' => $login]);
if(Yii::$app->user->login($auth)){ throw new HttpException(202 ,'First authorization success!'); }
else{ throw new HttpException(409 ,'The portal accounting service is temporarily unavailable! Try again later;-('); }
}
}
class Forgot extends Component{
public $signQuery;
public function init(){
parent::init();
$this->signQuery = [];
}
public function proccess($signQuery = null){
if($signQuery != null){
$this->signQuery = $signQuery;
}
$forgotModel = User::find();
$login = $this->signQuery['portalId'];
$newPass = sha1($this->signQuery['password']);
$isAccept = $this->signQuery['isAccept'] === TRUE;
$isLogin = $inModel->where(['login' => $login])->all() || $inModel->where(['email' => $login])->all() || $inModel->where(['phone' => $login])->all();
if($isLogin){
if($isAccept){
$forgotModel->filterWhere(['or',['login' => $login],['email' => $login],['phone' => $login]]);
$forgotModel->password = $newPass;
if($forgotModel->save()){ throw new HttpException(202 ,'Access restore success!'); }
else{ throw new HttpException(409 ,'The portal accounting service is temporarily unavailable! Try again later;-('); }
}
else{ throw new HttpException(500 ,'Account data for restore not is accept!'); }
}
else{
$validError = [];
header('Content-type: application/json;charset=UTF-8');
if(!$isLogin){ $validError[]['validError'] = 'The login you entered no exists'; }
throw new HttpException(400 ,Json::encode($validError));
}
}
}
class SignOut extends Component{
public function init(){ parent::init(); }
public function proccess(){
$out = Yii::app()->user->logout();
if($out){ throw new HttpException(202 ,'Sign account out success!'); }
else{ throw new HttpException(409 ,'The portal accounting service is temporarily unavailable! Try again later;-('); }
}
}
?>
<?php
namespace yii\components\CommunicationService;
use yii\base\Component;
use yii\db\Query;
use yii\models\UserService;
function getRandomFromRange(min, max) {
return rand() * (max - min) + min;
}
class SMSCode extends Component{
public $service;
public $code;
public $phone;
public function init(){
parent::init();
$this->service = 'signUp';
$this->code = 1234;
$this->phone = '9198298765';
}
public function sendCode($service = '', $phone = '', $code = null){
if($service != '' && $phone != '' && $code != null){
$this->service = $service;
$this->phone = $phone;
$this->code = $code;
}
$sms = new SenderCode();
$message = "";
switch($this->service){
case 'Forgot': $message = " - Restore your account access code"; break;
default: $message = " - Your account registration confirm code"; break;
}
$from = 'Investportal<9198298765@vtext.com>';
$to = $this->phone . '@vtext.com';
$content = $code . $message . "";
$sms->period = date('');
$sms->phone = $this->phone;
$sms->code = $this->code;
$sms->service = $this->service;
$smsStorage = $sms->save();
$smsMessage = mail($to,'', $content, "From: " . $from ."\n");
if($smsStorage && $smsMessage){ throw new HttpException(202 ,'SMS code send success!');}
else{ throw new HttpException(409 ,'The portal accounting service is temporarily unavailable! Try again later;-('); }
}
public function validCode($service = '', $phone = '', $code = null){
if($service != '' && $phone != '' && $code != null){
$this->service = $service;
$this->phone = $phone;
$this->code = $code;
}
$sms = SenderCode::find();
$validCode = $sms->where(['and',['code' => $this->code],['phone' => $this->phone],['service' => $this->service]])->all();
$deleteCode = (new Query)->createCommand()->delete('users', ['and',['code' => $this->code],['phone' => $this->phone],['service' => $this->service]])->execute();
foreach($validCode as $data){
if($this->code === $data->code){
if($deleteCode){ throw new HttpException(202 ,'SMS code is valid!');}
else{ throw new HttpException(409 ,'The portal accounting service is temporarily unavailable! Try again later;-('); }
}
else{ throw new HttpException(403 ,'The code is entered incorrectly and check it carefully, please!'); }
}
}
}
?>