Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.59 KB | None | 0 0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. /**
  4. * Class untuk resource login
  5. *
  6. * @package e-Learning Dokumenary Net
  7. * @author Almazari <almazary@gmail.com>
  8. * @copyright Copyright (c) 2013 - 2016, Dokumenary Net.
  9. * @since 1.0
  10. * @link http://dokumenary.net
  11. *
  12. * INDEMNITY
  13. * You agree to indemnify and hold harmless the authors of the Software and
  14. * any contributors for any direct, indirect, incidental, or consequential
  15. * third-party claims, actions or suits, as well as any related expenses,
  16. * liabilities, damages, settlements or fees arising from your use or misuse
  17. * of the Software, or a violation of any terms of this license.
  18. *
  19. * DISCLAIMER OF WARRANTY
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR
  21. * IMPLIED, INCLUDING, BUT NOT LIMITED TO, WARRANTIES OF QUALITY, PERFORMANCE,
  22. * NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
  23. *
  24. * LIMITATIONS OF LIABILITY
  25. * YOU ASSUME ALL RISK ASSOCIATED WITH THE INSTALLATION AND USE OF THE SOFTWARE.
  26. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS OF THE SOFTWARE BE LIABLE
  27. * FOR CLAIMS, DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF, OR IN CONNECTION
  28. * WITH THE SOFTWARE. LICENSE HOLDERS ARE SOLELY RESPONSIBLE FOR DETERMINING THE
  29. * APPROPRIATENESS OF USE AND ASSUME ALL RISKS ASSOCIATED WITH ITS USE, INCLUDING
  30. * BUT NOT LIMITED TO THE RISKS OF PROGRAM ERRORS, DAMAGE TO EQUIPMENT, LOSS OF
  31. * DATA OR SOFTWARE PROGRAMS, OR UNAVAILABILITY OR INTERRUPTION OF OPERATIONS.
  32. */
  33.  
  34. class Login extends MY_Controller
  35. {
  36. function index()
  37. {
  38. if (is_login()) {
  39. redirect('welcome');
  40. }
  41.  
  42. if ($this->form_validation->run('login') == TRUE) {
  43. $email = $this->input->post('email', TRUE);
  44. $password = md5($this->input->post('password', TRUE));
  45.  
  46. $get_login = $this->login_model->retrieve(null, $email, $password);
  47. // $this->load->model('login_model');
  48. // $get_login = $this->login_model->cek_login($email);
  49.  
  50. if (empty($get_login)) {
  51. $this->session->set_flashdata('login', get_alert('warning', 'Maaf akun tidak ditemukan.'));
  52. // redirect('login');
  53. } else {
  54. # cari user yang login
  55. if (!empty($get_login['pengajar_id'])) {
  56. $user = $this->pengajar_model->retrieve($get_login['pengajar_id']);
  57.  
  58. $user_type = empty($get_login['is_admin']) ? 'pengajar' : 'admin';
  59.  
  60. } elseif (!empty($get_login['siswa_id'])) {
  61. $user = $this->siswa_model->retrieve($get_login['siswa_id']);
  62.  
  63. $user_type = 'siswa';
  64. }
  65.  
  66. # cek jika user berstatus tidak aktif
  67. if ($user['status_id'] != 1) {
  68. $this->session->set_flashdata('login', get_alert('warning', 'Maaf status anda tidak aktif.'));
  69. redirect('login');
  70. }
  71.  
  72. # cari last login
  73. $last_log = $this->login_model->retrieve_last_log($get_login['id']);
  74. if (!empty($last_log)) {
  75. # cek last_activitynya, jika kurang dari 2 menit
  76. $time_minus = strtotime("-2 minutes", time());
  77. if ($last_log['last_activity'] > $time_minus) {
  78. # ini berarti ada yang masih login, cek ip dan browsernya
  79. $last_agent = json_decode($last_log['agent'], 1);
  80. $current_ip = get_ip();
  81. $current_browser =($this->agent->is_browser()) ? $this->agent->browser() . ' ' . $this->agent->version() : '';
  82.  
  83. if ($current_ip != $last_agent['ip'] OR $current_browser != $last_agent['browser']) {
  84. # cari selisih
  85. $selisih = lama_pengerjaan(date("Y-m-d H:i:s", $last_log['last_activity']), date("Y-m-d H:i:s", $time_minus), "%i menit %s detik");
  86.  
  87. # atur pesan
  88. $error_msg = "Akun anda sedang digunakan untuk login dengan IP {$last_agent['ip']}.";
  89. if ($current_ip == $last_agent['ip'] AND $current_browser != $last_agent['browser']) {
  90. $error_msg .= "<br><br>Jika anda hanya ganti browser, mohon tunggu {$selisih} dari sekarang.";
  91. }
  92.  
  93. $this->session->set_flashdata('login', get_alert('warning', $error_msg));
  94. redirect('login');
  95. }
  96. }
  97. }
  98.  
  99. # create log
  100. $log_id = $this->login_model->create_log($get_login['id']);
  101. $get_login['log_id'] = $log_id;
  102.  
  103. $data_session[$user_type] = array(
  104. 'login' => $get_login,
  105. 'user' => $user
  106. );
  107.  
  108. # setup folder
  109. if ($user_type == 'admin') {
  110. $data_session['path_userfiles'] = 'userfiles/';
  111. } else {
  112. $data_session['path_userfiles'] = 'userfiles/uploads/' . $get_login['id'] . '/';
  113. }
  114. $data_session['last_time_activity'] = time();
  115.  
  116. $_SESSION['login_' . APP_PREFIX] = $data_session;
  117.  
  118. redirect('welcome');
  119. }
  120. }
  121.  
  122. $data['sliders'] = $this->config_model->get_all_slider_img();
  123.  
  124. if (!empty($data['sliders'])) {
  125. # panggil nivoslider
  126. $html_js = load_comp_js(array(
  127. base_url('assets/comp/nivoslider/jquery.nivo.slider.pack.js'),
  128. ));
  129. $data['comp_js'] = $html_js;
  130. $data['comp_css'] = load_comp_css(array(
  131. base_url('assets/comp/nivoslider/nivo-slider.css'),
  132. base_url('assets/comp/nivoslider/themes/light/light.css'),
  133. ));
  134. }
  135.  
  136. $this->twig->display('login.html', $data);
  137. }
  138.  
  139. function logout()
  140. {
  141. # update last activity
  142. $time_minus = strtotime("-2 minutes", time());
  143. $this->login_model->update_last_activity(get_sess_data('login', 'log_id'), $time_minus);
  144.  
  145. $this->session->set_userdata('filter_pengajar', null);
  146. $this->session->set_userdata('filter_materi', null);
  147. $this->session->set_userdata('filter_tugas', null);
  148. $this->session->set_userdata('filter_siswa', null);
  149. $this->session->set_userdata('mengerjakan_tugas', null);
  150. $this->session->set_userdata('hide_countdown', null);
  151. $_SESSION['login_' . APP_PREFIX] = null;
  152.  
  153. redirect('login');
  154. }
  155.  
  156. function pp()
  157. {
  158. must_login();
  159.  
  160. if (is_pengajar()) {
  161. # panggil colorbox
  162. $html_js = load_comp_js(array(
  163. base_url('assets/comp/colorbox/jquery.colorbox-min.js'),
  164. ));
  165. $data['comp_js'] = $html_js;
  166. $data['comp_css'] = load_comp_css(array(base_url('assets/comp/colorbox/colorbox.css')));
  167.  
  168. $data['pengajar'] = $this->pengajar_model->retrieve(get_sess_data('user', 'id'));
  169. $data['pengajar_login'] = $this->login_model->retrieve(get_sess_data('login', 'id'));
  170. $data['status_id'] = get_sess_data('user', 'status_id');
  171.  
  172. $this->twig->display('pp-pengajar.html', $data);
  173. }
  174.  
  175. if (is_siswa()) {
  176. $retrieve_siswa = $this->siswa_model->retrieve(get_sess_data('user', 'id'));
  177. $retrieve_login = $this->login_model->retrieve(get_sess_data('login', 'id'));
  178. $retrieve_all_kelas = $this->kelas_model->retrieve_all_siswa(10, 1, array('siswa_id' => $retrieve_siswa['id']));
  179.  
  180. $data['siswa'] = $retrieve_siswa;
  181. $data['siswa_login'] = $retrieve_login;
  182. $data['siswa_kelas'] = $retrieve_all_kelas;
  183. $data['status_id'] = get_sess_data('user', 'status_id');
  184.  
  185. # panggil colorbox
  186. $html_js = load_comp_js(array(
  187. base_url('assets/comp/colorbox/jquery.colorbox-min.js'),
  188. ));
  189. $data['comp_js'] = $html_js;
  190. $data['comp_css'] = load_comp_css(array(base_url('assets/comp/colorbox/colorbox.css')));
  191.  
  192. $data['show'] = !empty($_GET['show']) ? $_GET['show'] : '';
  193.  
  194. $this->twig->display('pp-siswa.html', $data);
  195. }
  196. }
  197.  
  198. function register($sebagai = 'siswa')
  199. {
  200. if (is_login()) {
  201. redirect('welcome');
  202. }
  203.  
  204. # cek fitur
  205. $registrasi_siswa = get_pengaturan('registrasi-siswa', 'value');
  206. $registrasi_pengajar = get_pengaturan('registrasi-pengajar', 'value');
  207. if (empty($registrasi_siswa) && empty($registrasi_pengajar)) {
  208. redirect('login');
  209. }
  210.  
  211. $sebagai = empty($sebagai) ? 'siswa' : $sebagai;
  212. $allow_register = array('siswa', 'pengajar');
  213. if (!in_array($sebagai, $allow_register)) {
  214. redirect('login/register');
  215. }
  216.  
  217. if (empty($registrasi_siswa) && $sebagai == 'siswa') {
  218. redirect('login/register/pengajar');
  219. }
  220.  
  221. if (empty($registrasi_pengajar) && $sebagai == 'pengajar') {
  222. redirect('login/register/siswa');
  223. }
  224.  
  225. $data = array();
  226. if ($sebagai == 'siswa') {
  227. if ($this->form_validation->run('register/siswa') == true) {
  228. $no_peserta = $this->input->post('no_peserta', TRUE);
  229. $nama = $this->input->post('nama', TRUE);
  230. $jenis_kelamin = $this->input->post('jenis_kelamin', TRUE);
  231. $tahun_masuk = $this->input->post('tahun_masuk', TRUE);
  232. $kelas_id = $this->input->post('kelas_id', TRUE);
  233. $pangkalan = $this->input->post('pangkalan', TRUE);
  234. $tgl_lahir = $this->input->post('tgl_lahir', TRUE);
  235. $bln_lahir = $this->input->post('bln_lahir', TRUE);
  236. $thn_lahir = $this->input->post('thn_lahir', TRUE);
  237. $agama = $this->input->post('agama', TRUE);
  238. $alamat = $this->input->post('alamat', TRUE);
  239. $username = $this->input->post('username', TRUE);
  240. $password = $this->input->post('password2', TRUE);
  241.  
  242. if (empty($thn_lahir)) {
  243. $tanggal_lahir = null;
  244. } else {
  245. $tanggal_lahir = $thn_lahir.'-'.$bln_lahir.'-'.$tgl_lahir;
  246. }
  247.  
  248. $foto = null;
  249.  
  250. $status_id = get_pengaturan('status-registrasi-siswa', 'value');
  251. $status_id = (int)$status_id;
  252.  
  253. # simpan data siswa
  254. $siswa_id = $this->siswa_model->create(
  255. $no_peserta,
  256. $nama,
  257. $jenis_kelamin,
  258. $pangkalan,
  259. $tanggal_lahir,
  260. $agama,
  261. $alamat,
  262. $tahun_masuk,
  263. $foto,
  264. $status_id
  265. );
  266.  
  267. # simpan data login
  268. $this->login_model->create(
  269. $username,
  270. $password,
  271. $siswa_id,
  272. null
  273. );
  274.  
  275. # simpan kelas siswa
  276. $this->kelas_model->create_siswa(
  277. $kelas_id,
  278. $siswa_id,
  279. 1
  280. );
  281.  
  282. # jika langsung aktif
  283. if ($status_id == 1) {
  284. # kirim email aktifasi
  285. @kirim_email_approve_siswa($siswa_id);
  286.  
  287. $pesan = "Registrasi sebagai siswa berhasil, silakan " . anchor('login/index', 'LOG IN') . " ke sistem.";
  288. } else {
  289. # kirim email registrasi
  290. @kirim_email('email-template-register-siswa', $username, array(
  291. 'nama' => $nama,
  292. 'nama_sekolah' => get_pengaturan('nama-sekolah', 'value')
  293. ));
  294.  
  295. $pesan = "Registrasi sebagai siswa berhasil, tunggu pengaktifan akun oleh admin.";
  296. }
  297.  
  298. $this->session->set_flashdata('register', get_alert('success', $pesan));
  299. redirect('login/register/siswa');
  300. }
  301.  
  302. $data['kelas'] = $this->kelas_model->retrieve_all_child();
  303. }
  304.  
  305. # jika pengajar
  306. elseif ($sebagai == 'pengajar') {
  307. if ($this->form_validation->run('register/pengajar') == true) {
  308. $nip = $this->input->post('nip', TRUE);
  309. $nama = $this->input->post('nama', TRUE);
  310. $jenis_kelamin = $this->input->post('jenis_kelamin', TRUE);
  311. $tempat_lahir = $this->input->post('tempat_lahir', TRUE);
  312. $tgl_lahir = $this->input->post('tgl_lahir', TRUE);
  313. $bln_lahir = $this->input->post('bln_lahir', TRUE);
  314. $thn_lahir = $this->input->post('thn_lahir', TRUE);
  315. $alamat = $this->input->post('alamat', TRUE);
  316. $username = $this->input->post('username', TRUE);
  317. $password = $this->input->post('password2', TRUE);
  318. $is_admin = 0;
  319. $foto = null;
  320.  
  321. if (empty($thn_lahir)) {
  322. $tanggal_lahir = null;
  323. } else {
  324. $tanggal_lahir = $thn_lahir.'-'.$bln_lahir.'-'.$tgl_lahir;
  325. }
  326.  
  327. $status_id = get_pengaturan('status-registrasi-pengajar', 'value');
  328. $status_id = (int)$status_id;
  329.  
  330. # simpan data pengajar
  331. $pengajar_id = $this->pengajar_model->create(
  332. $nip,
  333. $nama,
  334. $jenis_kelamin,
  335. $tempat_lahir,
  336. $tanggal_lahir,
  337. $alamat,
  338. $foto,
  339. $status_id
  340. );
  341.  
  342. # simpan data login
  343. $this->login_model->create(
  344. $username,
  345. $password,
  346. null,
  347. $pengajar_id,
  348. $is_admin
  349. );
  350.  
  351. if ($status_id == 1) {
  352. @kirim_email_approve_pengajar($pengajar_id);
  353.  
  354. $pesan = "Registrasi sebagai pengajar berhasil, silakan " . anchor('login/index', 'LOG IN') . " ke sistem.";
  355. } else {
  356. # kirim email registrasi
  357. @kirim_email('email-template-register-pengajar', $username, array(
  358. 'nama' => $nama,
  359. 'nama_sekolah' => get_pengaturan('nama-sekolah', 'value')
  360. ));
  361.  
  362. $pesan = "Registrasi sebagai pengajar berhasil, tunggu pengaktifan akun oleh admin.";
  363. }
  364.  
  365. $this->session->set_flashdata('register', get_alert('success', $pesan));
  366. redirect('login/register/pengajar');
  367. }
  368. }
  369.  
  370.  
  371. $data['sebagai'] = $sebagai;
  372.  
  373. $this->twig->display('register.html', $data);
  374. }
  375.  
  376. function lupa_password()
  377. {
  378. if (is_login()) {
  379. redirect('welcome');
  380. }
  381.  
  382. $data = array();
  383. if ($this->form_validation->run('lupa_password') == true) {
  384. # retrieve
  385. $retrieve = $this->login_model->retrieve(
  386. $id = null,
  387. $username = $this->input->post('email', true)
  388. );
  389.  
  390. $reset_kode = md5(time());
  391.  
  392. # set reset kode
  393. $this->login_model->update(
  394. $id = $retrieve['id'],
  395. $username = $retrieve['username'],
  396. $siswa_id = $retrieve['siswa_id'],
  397. $pengajar_id = $retrieve['pengajar_id'],
  398. $is_admin = $retrieve['is_admin'],
  399. $reset_kode = $reset_kode
  400. );
  401.  
  402. # kirim email disini
  403. @kirim_email('email-template-link-reset', $retrieve['username'], array(
  404. 'link_reset' => site_url('login/reset_password/' . $reset_kode)
  405. ));
  406.  
  407. $this->session->set_flashdata('lupa_password', get_alert('success', 'Link reset password telah dikirimkan keemail anda.'));
  408. redirect('login/lupa_password');
  409. }
  410.  
  411. $this->twig->display('lupa-password.html', $data);
  412. }
  413.  
  414. function reset_password($kode = '')
  415. {
  416. if (empty($kode)) {
  417. redirect('welcome/lupa_password');
  418. }
  419.  
  420. $login = $this->login_model->retrieve(
  421. $id = null,
  422. $username = null,
  423. $password = null,
  424. $siswa_id = null,
  425. $pengajar_id = null,
  426. $is_admin = null,
  427. $reset_kode = $kode
  428. );
  429.  
  430. if (empty($login)) {
  431. $this->session->set_flashdata('lupa_password', get_alert('warning', 'Reset kode tidak benar.'));
  432. redirect('login/lupa_password');
  433. }
  434.  
  435. if ($this->form_validation->run('reset_password') == true) {
  436. # update password
  437. $this->login_model->update_password(
  438. $login['id'],
  439. $this->input->post('password', true)
  440. );
  441.  
  442. # update reset kode
  443. $this->login_model->update(
  444. $id = $login['id'],
  445. $username = $login['username'],
  446. $siswa_id = $login['siswa_id'],
  447. $pengajar_id = $login['pengajar_id'],
  448. $is_admin = $login['is_admin'],
  449. $reset_kode = null
  450. );
  451.  
  452. $this->session->set_flashdata('login', get_alert('success', 'Password berhasil diperbaharui, silahkan login menggunakan password baru anda.'));
  453. redirect('login');
  454. }
  455.  
  456. $data['login'] = $login;
  457. $this->twig->display('reset-password.html', $data);
  458. }
  459.  
  460. function login_log($segment_3 = "", $segment_4 = "")
  461. {
  462. must_login();
  463.  
  464. $login_id = (int)$segment_3;
  465. if (empty($login_id)) {
  466. $login_id = get_sess_data('login', 'id');
  467. redirect('login/login_log/' . $login_id);
  468. }
  469.  
  470. $login = $this->login_model->retrieve($login_id);
  471. if (empty($login)) {
  472. show_error("Login ID tidak ditemukan.");
  473. }
  474.  
  475. if (!is_admin() AND $login['id'] != get_sess_data('login', 'id')) {
  476. redirect('login/login_log/' . get_sess_data('login', 'id'));
  477. }
  478.  
  479. $page_no = (int)$segment_4;
  480. if (empty($page_no)) {
  481. $page_no = 1;
  482. }
  483.  
  484. # ambil data login log
  485. $retrieve_all = $this->login_model->retrieve_all_log(20, $page_no, $login['id']);
  486.  
  487. # format tgl
  488. foreach ($retrieve_all['results'] as $key => $val) {
  489. if (belum_sehari($val['lasttime'])) {
  490. $retrieve_all['results'][$key]['timeago'] = iso8601($val['lasttime']);
  491. }
  492.  
  493. $retrieve_all['results'][$key]['lasttime'] = format_datetime($val['lasttime']);
  494. }
  495.  
  496. $data['log'] = $retrieve_all['results'];
  497. $data['pagination'] = $this->pager->view($retrieve_all, 'login/login_log/' . $login['id'] . '/');
  498.  
  499. $this->twig->display('list-login-log.html', $data);
  500. }
  501.  
  502. /**
  503. * Method untuk cek sudah login atau belum
  504. * @return boolean
  505. * @since 1.8
  506. */
  507. function data_onload()
  508. {
  509. if (!is_ajax()) {
  510. die;
  511. }
  512.  
  513. $return = array(
  514. 'is_user_logged_in' => is_login() ? '1' : '0',
  515. 'sedang_ujian' => $this->sedang_ujian() ? '1' : '0',
  516. );
  517.  
  518. echo json_encode($return);
  519. }
  520.  
  521. /**
  522. * Method untuk redirect karna session telah expired
  523. * @since 1.8
  524. */
  525. function sess_expired()
  526. {
  527. $this->session->set_flashdata('login', get_alert('warning', "Session login anda telah habis, silakan login kembali."));
  528. $this->logout();
  529. }
  530. function pengunjung()
  531. {
  532.  
  533. $data = array();
  534. if ($this->form_validation->run('pengunjung') == true) {
  535.  
  536. }
  537.  
  538. $this->twig->display('pengunjung.html', $data);
  539. }
  540.  
  541.  
  542. }
  543.  
  544. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement