Overview

Namespaces

  • Kotchasan
    • Cache
    • Database
    • Http
    • Log
    • Orm
  • None
  • PHP
  • Psr
    • Cache
    • Http
      • Message
    • Log

Classes

  • ArrayIterator
  • DateInterval
  • DOMNode
  • Kotchasan
  • Kotchasan\Accordion
  • Kotchasan\ApiController
  • Kotchasan\ArrayTool
  • Kotchasan\Cache\ApcCache
  • Kotchasan\Cache\Cache
  • Kotchasan\Cache\CacheItem
  • Kotchasan\Cache\FileCache
  • Kotchasan\CKEditor
  • Kotchasan\Collection
  • Kotchasan\Config
  • Kotchasan\Controller
  • Kotchasan\Country
  • Kotchasan\Csv
  • Kotchasan\Curl
  • Kotchasan\Currency
  • Kotchasan\Database
  • Kotchasan\Database\Db
  • Kotchasan\Database\DbCache
  • Kotchasan\Database\Driver
  • Kotchasan\Database\PdoMysqlDriver
  • Kotchasan\Database\Query
  • Kotchasan\Database\QueryBuilder
  • Kotchasan\Database\Schema
  • Kotchasan\Database\Sql
  • Kotchasan\DataTable
  • Kotchasan\Date
  • Kotchasan\DOMNode
  • Kotchasan\DOMParser
  • Kotchasan\Email
  • Kotchasan\File
  • Kotchasan\Files
  • Kotchasan\Form
  • Kotchasan\Grid
  • Kotchasan\Html
  • Kotchasan\Htmldoc
  • Kotchasan\HtmlTable
  • Kotchasan\Http\AbstractMessage
  • Kotchasan\Http\AbstractRequest
  • Kotchasan\Http\Message
  • Kotchasan\Http\NotFound
  • Kotchasan\Http\Request
  • Kotchasan\Http\Response
  • Kotchasan\Http\Stream
  • Kotchasan\Http\UploadedFile
  • Kotchasan\Http\Uri
  • Kotchasan\Image
  • Kotchasan\InputItem
  • Kotchasan\Inputs
  • Kotchasan\KBase
  • Kotchasan\Language
  • Kotchasan\ListItem
  • Kotchasan\Log\AbstractLogger
  • Kotchasan\Log\Logger
  • Kotchasan\Login
  • Kotchasan\Menu
  • Kotchasan\Mime
  • Kotchasan\Model
  • Kotchasan\Number
  • Kotchasan\ObjectTool
  • Kotchasan\Orm\Field
  • Kotchasan\Orm\Recordset
  • Kotchasan\Password
  • Kotchasan\Pdf
  • Kotchasan\Province
  • Kotchasan\Router
  • Kotchasan\Session
  • Kotchasan\Singleton
  • Kotchasan\Tab
  • Kotchasan\TableRow
  • Kotchasan\Template
  • Kotchasan\Text
  • Kotchasan\Validator
  • Kotchasan\View
  • PHPMailer
  • Psr\Log\AbstractLogger
  • Psr\Log\LogLevel
  • Psr\Log\NullLogger
  • SMTP

Interfaces

  • ArrayAccess
  • Countable
  • DateTimeInterface
  • Iterator
  • IteratorAggregate
  • Psr\Cache\CacheItemInterface
  • Psr\Cache\CacheItemPoolInterface
  • Psr\Http\Message\MessageInterface
  • Psr\Http\Message\RequestInterface
  • Psr\Http\Message\ResponseInterface
  • Psr\Http\Message\ServerRequestInterface
  • Psr\Http\Message\StreamInterface
  • Psr\Http\Message\UploadedFileInterface
  • Psr\Http\Message\UriInterface
  • Psr\Log\LoggerAwareInterface
  • Psr\Log\LoggerInterface
  • SeekableIterator
  • Serializable
  • Traversable

Traits

  • Psr\Log\LoggerTrait

Exceptions

  • Exception
  • InvalidArgumentException
  • Kotchasan\ApiException
  • Kotchasan\Cache\Exception
  • Kotchasan\Database\Exception
  • Kotchasan\InputItemException
  • LogicException
  • phpmailerException
  • RuntimeException

Functions

  • createClass
  • debug
  • getClassPath
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * @filesource Kotchasan/Login.php
  4:  *
  5:  * @copyright 2016 Goragod.com
  6:  * @license http://www.kotchasan.com/license/
  7:  *
  8:  * @see http://www.kotchasan.com/
  9:  */
 10: 
 11: namespace Kotchasan;
 12: 
 13: use Kotchasan\Http\Request;
 14: 
 15: /**
 16:  * คลาสสำหรับตรวจสอบการ Login
 17:  *
 18:  * @author Goragod Wiriya <admin@goragod.com>
 19:  *
 20:  * @since 1.0
 21:  */
 22: class Login extends \Kotchasan\KBase
 23: {
 24:     /**
 25:      * ตัวแปรบอกว่ามาจากการ submit
 26:      * true มาจากการ submit
 27:      * false default
 28:      *
 29:      * @var bool
 30:      */
 31:     public static $from_submit = false;
 32:     /**
 33:      * ชื่อ Input ที่ต้องการให้ active
 34:      * login_username หรือ login_password
 35:      *
 36:      * @var string
 37:      */
 38:     public static $login_input;
 39:     /**
 40:      * ข้อความจาก Login Class
 41:      *
 42:      * @var string
 43:      */
 44:     public static $login_message;
 45:     /**
 46:      * ตัวแปรเก็บข้อมูลที่ส่งมา
 47:      * เช่น username, password
 48:      *
 49:      * @var array
 50:      */
 51:     public static $login_params = array();
 52: 
 53:     /**
 54:      * ฟังก์ชั่นตรวจสอบการ login
 55:      * เข้าระบบสำเร็จคืนค่าแอเรย์ข้อมูลสมาชิก, ไม่สำเร็จ คืนค่าข้อความผิดพลาด
 56:      *
 57:      * @param array $params ข้อมูลการ login ที่ส่งมา $params = array('username' => '', 'password' => '');
 58:      *
 59:      * @return string|array
 60:      */
 61:     public function checkLogin($params)
 62:     {
 63:         $field_name = reset(self::$cfg->login_fields);
 64:         if ($params['username'] !== self::$cfg->get($field_name)) {
 65:             self::$login_input = $field_name;
 66:             return Language::get('not a registered user');
 67:         } elseif ($params['password'] !== self::$cfg->get('password')) {
 68:             self::$login_input = 'password';
 69:             return Language::get('password incorrect');
 70:         } else {
 71:             return array(
 72:                 'id' => 1,
 73:                 $field_name => $params['username'],
 74:                 'password' => $params['password'],
 75:                 'status' => 1,
 76:             );
 77:         }
 78:         return 'not a registered user';
 79:     }
 80: 
 81:     /**
 82:      * ตรวจสอบการ login เมื่อมีการเรียกใช้ class new Login
 83:      * action=logout ออกจากระบบ
 84:      * มาจากการ submit ตรวจสอบการ login
 85:      * ถ้าไม่มีทั้งสองส่วนด้านบน จะตรวจสอบการ login จาก session
 86:      *
 87:      * @return \static
 88:      */
 89:     public static function create()
 90:     {
 91:         // create class
 92:         $login = new static();
 93:         // ชื่อฟิลด์สำหรับการรับค่าเป็นรายการแรกของ login_fields
 94:         $field_name = reset(self::$cfg->login_fields);
 95:         try {
 96:             // อ่านข้อมูลจากฟอร์ม login ฟิลด์ login_username
 97:             self::$login_params['username'] = self::$request->post('login_username')->username();
 98:             if (empty(self::$login_params['username'])) {
 99:                 if (isset($_SESSION['login']) && isset($_SESSION['login'][$field_name])) {
100:                     // session
101:                     self::$login_params['username'] = Text::username($_SESSION['login'][$field_name]);
102:                     if (isset($_SESSION['login']['token'])) {
103:                         self::$login_params['token'] = $_SESSION['login']['token'];
104:                     } elseif (isset($_SESSION['login']['password'])) {
105:                         self::$login_params['password'] = $_SESSION['login']['password'];
106:                     }
107:                 } else {
108:                     self::$login_params['username'] = null;
109:                 }
110:                 self::$from_submit = self::$request->post('login_username')->exists();
111:             } elseif (self::$request->post('login_password')->exists()) {
112:                 self::$login_params['password'] = self::$request->post('login_password')->password();
113:                 self::$from_submit = true;
114:             }
115:             $action = self::$request->request('action')->toString();
116:             // ตรวจสอบการ login
117:             if ($action === 'logout' && !self::$from_submit) {
118:                 // logout ลบ session และ cookie
119:                 unset($_SESSION['login']);
120:                 self::$login_message = Language::get('Logout successful');
121:             } elseif ($action === 'forgot') {
122:                 // ขอรหัสผ่านใหม่
123:                 $login = $login->forgot(self::$request);
124:             } else {
125:                 // ตรวจสอบค่าที่ส่งมา
126:                 if (empty(self::$login_params['username'])) {
127:                     if (self::$from_submit) {
128:                         self::$login_message = 'Please fill in';
129:                         self::$login_input = 'login_username';
130:                     }
131:                 } elseif (empty(self::$login_params['password']) && self::$from_submit) {
132:                     self::$login_message = 'Please fill in';
133:                     self::$login_input = 'login_password';
134:                 } elseif (!self::$from_submit || (self::$from_submit && self::$request->isReferer())) {
135:                     // ตรวจสอบการ login กับฐานข้อมูล
136:                     $login_result = $login->checkLogin(self::$login_params);
137:                     if (is_array($login_result)) {
138:                         // save login session
139:                         $_SESSION['login'] = $login_result;
140:                     } else {
141:                         if (is_string($login_result)) {
142:                             // ข้อความผิดพลาด
143:                             self::$login_input = self::$login_input == 'password' ? 'login_password' : 'login_username';
144:                             self::$login_message = $login_result;
145:                         }
146:                         // logout ลบ session และ cookie
147:                         unset($_SESSION['login']);
148:                     }
149:                 }
150:             }
151:         } catch (InputItemException $e) {
152:             self::$login_message = $e->getMessage();
153:         }
154:         return $login;
155:     }
156: 
157:     /**
158:      * ฟังก์ชั่นส่งอีเมลลืมรหัสผ่าน
159:      */
160:     public function forgot(Request $request)
161:     {
162:         return $this;
163:     }
164: 
165:     /**
166:      * ฟังก์ชั่นตรวจสอบสถานะแอดมิน
167:      * คืนค่าข้อมูลสมาชิก (แอเรย์) ถ้าเป็นผู้ดูแลระบบและเข้าระบบแล้ว ไม่ใช่คืนค่า null
168:      *
169:      * @return array|null
170:      */
171:     public static function isAdmin()
172:     {
173:         $login = self::isMember();
174:         return isset($login['status']) && $login['status'] == 1 ? $login : null;
175:     }
176: 
177:     /**
178:      * ฟังก์ชั่นตรวจสอบการเข้าระบบ
179:      * คืนค่าข้อมูลสมาชิก (แอเรย์) ถ้าเป็นสมาชิกและเข้าระบบแล้ว ไม่ใช่คืนค่า null
180:      *
181:      * @return array|null
182:      */
183:     public static function isMember()
184:     {
185:         return empty($_SESSION['login']) ? null : $_SESSION['login'];
186:     }
187: 
188:     /**
189:      * ตรวจสอบสถานะสมาชิก
190:      * แอดมินสูงสุด (status=1) ทำได้ทุกอย่าง
191:      * คืนค่าข้อมูลสมาชิก (แอเรย์) ถ้าไม่สามารถทำรายการได้คืนค่า null
192:      *
193:      * @param array        $login
194:      * @param array|int $statuses
195:      *
196:      * @return array|null
197:      */
198:     public static function checkStatus($login, $statuses)
199:     {
200:         if (!empty($login)) {
201:             if ($login['status'] == 1) {
202:                 // แอดมิน
203:                 return $login;
204:             } elseif (is_array($statuses)) {
205:                 if (in_array($login['status'], $statuses)) {
206:                     return $login;
207:                 }
208:             } elseif ($login['status'] == $statuses) {
209:                 return $login;
210:             }
211:         }
212:         // ไม่มีสิทธิ
213:         return null;
214:     }
215: }
216: 
Kotchasan API documentation generated by ApiGen