| Current Path : /var/www/html/dosm_backup/backend/controllers/ |
| Current File : /var/www/html/dosm_backup/backend/controllers/SiteController.php |
<?php
namespace backend\controllers;
use Yii;
// use backend\modules\mimin\components\AccessControl;
use common\models\User;
use yii\web\Controller;
use common\models\LoginForm;
use backend\models\PasswordResetRequestForm;
use backend\models\User as UserValidate;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use backend\modules\pageVisitLog\models\PageVisitLog;
use backend\modules\pageVisitLog\models\Detect;
use backend\modules\pageVisitLog\models\Mobile_Detect;
use backend\models\AuditTrailEntry;
//use backend\models\FileSurvey;
//use yii\web\AssetBundle;
/**
* Site controller
*/
class SiteController extends Controller {
/**
* @inheritdoc
*/
public function behaviors() {
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['login', 'error', 'ad-test'],
'allow' => true,
],
[
'actions' => ['logout', 'index'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
/**
* @inheritdoc
*/
public function actions() {
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
public function actionIndex() {
//if module dashboard has been setting to specific module
// config in backend/params
$config = \Yii::$app->params['dashboardModule'];
if (!empty($config)) {
return Yii::$app->runAction($config['route'], $config['param']);
}
return $this->render('index');
}
public function actionLogin() {
$this->layout = 'empty';
if (!Yii::$app->user->isGuest) {
$this->goBack();
}
$model = new LoginForm();
$modelUser = new User();
$modelForgotPass = new PasswordResetRequestForm;
$modelValidate = new UserValidate();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
$modelAudit = new AuditTrailEntry();
$modelAudit->model_type = 'backend\models\AuditTrailEntry';
$modelAudit->happened_at = time();
$modelAudit->foreign_pk = '{"id":' . Yii::$app->user->identity->id . '}';
$modelAudit->user_id = Yii::$app->user->identity->id;
$modelAudit->type = 'login';
$modelAudit->save(false);
$modellog = new PageVisitLog();
$modellog->content_type = 'cms-login';
$modellog->table_related = 'dmg_user';
$modellog->id_column_name = 'id';
$modellog->title_column_name = 'username';
$modellog->data_id = Yii::$app->user->identity->id;
$modellog->action_type = 'cms-login';
$modellog->action_datetime = date("Y-m-d H:i:s");
$modellog->visitor_ip = Detect::ip();
$modellog->visitor_os = Detect::os();
$modellog->visitor_device = Detect::deviceType();
$modellog->visitor_location_country = $modellog->ip_info("Visitor", "Country");
$modellog->visitor_location_state = $modellog->ip_info("Visitor", "State");
$modellog->visitor_location_city = $modellog->ip_info("Visitor", "City");
$modellog->visitor_browser = Detect::browser();
$log_id = 0;
if ($modellog->save())
$log_id = $modellog->id;
$checking_details = '';
//get last login details
$modelLastlogin = PageVisitLog::find()->where(['=', 'content_type', 'cms-login'])->andwhere(['=', 'data_id', Yii::$app->user->identity->id])->andwhere(['!=', 'id', $log_id])->orderby(['id' => SORT_DESC])->one();
if (!empty($modelLastlogin)) {
if ($modelLastlogin->visitor_ip != $modellog->visitor_ip) {
$checking_details .= '<tr>
<td>IP Address</td>
<td>' . $modelLastlogin->visitor_ip . '</td>
<td>' . $modellog->visitor_ip . '</td>
</tr>';
}
if ($modelLastlogin->visitor_os != $modellog->visitor_os) {
$checking_details .= '<tr>
<td>Operating System</td>
<td>' . $modelLastlogin->visitor_os . '</td>
<td>' . $modellog->visitor_os . '</td>
</tr>';
}
if ($modelLastlogin->visitor_device != $modellog->visitor_device) {
$checking_details .= '<tr>
<td>Device</td>
<td>' . $modelLastlogin->visitor_device . '</td>
<td>' . $modellog->visitor_device . '</td>
</tr>';
}
if ($modelLastlogin->visitor_browser != $modellog->visitor_browser) {
$checking_details .= '<tr>
<td>Web Browser</td>
<td>' . $modelLastlogin->visitor_browser . '</td>
<td>' . $modellog->visitor_browser . '</td>
</tr>';
}
if ($modelLastlogin->visitor_location_country != $modellog->visitor_location_country) {
$checking_details .= '<tr>
<td>Country</td>
<td>' . $modelLastlogin->visitor_location_country . '</td>
<td>' . $modellog->visitor_location_country . '</td>
</tr>';
}
if ($modelLastlogin->visitor_location_state != $modellog->visitor_location_state) {
$checking_details .= '<tr>
<td>State</td>
<td>' . $modelLastlogin->visitor_location_state . '</td>
<td>' . $modellog->visitor_location_state . '</td>
</tr>';
}
if ($modelLastlogin->visitor_location_city != $modellog->visitor_location_city) {
$checking_details .= '<tr>
<td>City</td>
<td>' . $modelLastlogin->visitor_location_city . '</td>
<td>' . $modellog->visitor_location_city . '</td>
</tr>';
}
if (!empty($checking_details)) {
$to_mail = Yii::$app->user->identity->email;
$subject_mail = 'Security alert for CMS Portal DOSM login';
$content_mail = '<p>Hi ' . Yii::$app->user->identity->fullname . ',</p>
<p>We notice there are different login into CMS Portal DOSM using your user ID and password from your previous login</p>
<table cellpadding="5" cellspacing="0" border="1">
<tr>
<th></th>
<th>Previous</th>
<th>Latest</th>
</tr>
' . $checking_details . '
</table>
<p>If this was you, you don�t need to do anything. If not, we recommend you to change your password to secure your account.</p>
<p>Thank you.</p>';
Yii::$app->emailModule->sendemail($to_mail, $subject_mail, $content_mail);
}
}
return $this->goBack();
} else {
return $this->render('login', [
'model' => $model,
'modelUser' => $modelUser,
'modelForgotPass' => $modelForgotPass,
'validate' => $modelValidate,
]);
}
}
public function actionLogout() {
$modelAudit = new AuditTrailEntry();
$modelAudit->model_type = 'backend\models\AuditTrailEntry';
$modelAudit->happened_at = time();
$modelAudit->foreign_pk = '{"id":' . Yii::$app->user->identity->id . '}';
$modelAudit->user_id = Yii::$app->user->identity->id;
$modelAudit->type = 'logout';
$modelAudit->save(false);
Yii::$app->user->logout();
return $this->goHome();
}
public function actionRequestPasswordReset() {
$model = new PasswordResetRequestForm();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
Yii::$app->session->setFlash('success', Yii::t('app', 'Sila periksa emel anda untuk tindakan seterusnya.'));
return $this->goHome();
}
return $this->refresh();
} else {
return $this->render('requestPasswordResetToken', [
'model' => $model,
]);
}
}
public function actionForgotPassword() {
if (isset($_POST['email'])) {
$mailTo = $_POST['email'];
$checkUser = User::find()->where("email='$mailTo'")->one();
if (count($checkUser) > 0) {
Yii::$app->mailer->compose('email')
->setFrom(Yii::$app->params['adminEmail'])
->setTo($mailTo) //replace email sini untuk testing
->setSubject('Admin - Terlupa kata laluan')
->setHtmlBody('Kata Laluan baru: 12345qwer')
->send();
$checkUser->password_hash = Yii::$app->security->generatePasswordHash("12345qwer");
$checkUser->update();
return "BERJAYA\nAn emel telah dihantar.";
} else {
return "GAGAL\nEmel ini tidak wujud di dalam sistemxxxxx";
}
}
}
public function actionStub() {
return $this->render('stub');
}
public function actionAdTest() {
$fp = fsockopen("localhost", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
echo "Port Open";
}
// $server = "jolly-fog-57422.pktriot.net";
// //domain user to connect to LDAP
// $user = "superadmin";
// //user password
// $psw = "Secure92@0258";
// //FQDN path where search will be performed. OU - organizational unit / DC - domain component
// $dn = "OU=example,DC=dynawebwork,DC=com";
// //Search query. CN - common name (CN=* will return all objects)
// $search = "CN=*";
// // ------------------------------------------------------------------------
// echo "<h2>php LDAP query test</h2>";
// // connecting to LDAP server
// $ds=ldap_connect($server, 22886);
// $r=ldap_bind($ds, $user , $psw);
// // performing search
// $sr=ldap_search($ds, $dn, $search);
// $data = ldap_get_entries($ds, $sr);
// echo "Found " . $data["count"] . " entries";
// for ($i=0; $i<$data["count"]; $i++) {
// echo "
// Common Name: " . $data[$i]["cn"][0] . "
// ";
// echo "Distinguished Name: " . $data[$i]["dn"] . "
// ";
// //checking if discription exists
// if (isset($data[$i]["description"][0]))
// echo "Desription: " . $data[$i]["description"][0] . "
// ";
// else
// echo "Description not set
// ";
// //checking if email exists
// if (isset($data[$i]["mail"][0]))
// echo "Email: " . $data[$i]["mail"][0] . "
// ";
// else
// echo "Email not set
// ";
// }
// // close connection
// ldap_close($ds);
// //...
// $un = 'superadmin';
/*
There are three ways available to call Adldap2 function.
If you use more providers (multiple Active Directory connections)
you make one as default and you can call this one with Method1 or Method2
and the second one will be called with Method3.
*/
//Get the Ldap object for the user.
//$ldapObject holds a class of type Adldap\Models\User from the Adldap project!
// Method 1: uses the default provider given in the configuration above (array key defaultProvider)
// $ldapObject = \Yii::$app->ad->search()->findBy('sAMAccountname', $un);
// // Method 2: uses the default provider given in the configuration above (array key defaultProvider)
// $ldapObject = \Yii::$app->ad->getDefaultProvider()->search()->findBy('sAMAccountname', $un);
// // Method 3: get the provider by name (here name default is used).
// $ldapObject = \Yii::$app->ad->getProvider('default')->search()->findBy('sAMAccountname', $un);
// //Examples
// //Please note that all fields from ldap are arrays!
// //Access it with ..[0] if it is a single value field.
// $givenName = $ldapObject['givenname'][0];
// $surname = $ldapObject['sn'][0];
// $displayname = $ldapObject['displayname'][0];
// $telephone = $ldapObject['telephonenumber'][0];
// echo 'gn: ' . $givenName . ' sn: ' . $surname .
// ' dispname: ' . $displayname . ' phone: ' . $telephone;
// //Print all possible attributes
// echo '<pre>' . print_r($ldapObject,true) . '</pre>';
}
}