dimaslanjaka /
universal-framework
| 1 | <?php |
||
| 2 | |||
| 3 | namespace User; |
||
| 4 | |||
| 5 | class coupon extends user |
||
| 6 | { |
||
| 7 | public $coupon = null; |
||
| 8 | |||
| 9 | public function __construct($user = 'root', $pass = '', $db, $host = 'localhost', $charset = 'utf8mb4') |
||
| 10 | { |
||
| 11 | parent::__construct($user, $pass, $db, $host, $charset); |
||
| 12 | } |
||
| 13 | |||
| 14 | public function set_pdo(\DB\pdo $pdo) |
||
| 15 | { |
||
| 16 | $this->pdo = $pdo; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 17 | } |
||
| 18 | |||
| 19 | public function pdo_required() |
||
| 20 | { |
||
| 21 | if (!$this->pdo_instance() || empty($this->pdo_instance())) { |
||
| 22 | if ('any' == \MVC\helper::HeaderAccept() && !\MVC\helper::cors()) { |
||
|
0 ignored issues
–
show
Are you sure the usage of
MVC\helper::HeaderAccept() targeting MVC\helper::HeaderAccept() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||
| 23 | throw new \MVC\Exception('PDO Required', 1); |
||
| 24 | } else { |
||
| 25 | \JSON\json::json(['error' => true, 'message' => '\DB\pdo instance is required', 'title' => __CLASS__]); |
||
| 26 | exit; |
||
|
0 ignored issues
–
show
|
|||
| 27 | } |
||
| 28 | } |
||
| 29 | |||
| 30 | return $this; |
||
| 31 | } |
||
| 32 | |||
| 33 | public function create(string $coupon, int $limit) |
||
| 34 | { |
||
| 35 | $this->pdo_required(); |
||
| 36 | $check = $this->pdo_instance()->select('coupon')->where(['code' => $coupon])->row_array(); |
||
| 37 | $result = ['error' => true]; |
||
| 38 | if (isset($check['code'])) { |
||
| 39 | $result['message'] = "Account `$coupon` already exists"; |
||
| 40 | } else { |
||
| 41 | $insert = $this->pdo_instance()->insert_not_exists('coupon', ['code' => $coupon, 'limit' => $limit])->exec(); |
||
| 42 | $result = array_replace($result, $insert); |
||
| 43 | if (!$result['error']) { |
||
| 44 | $result['message'] = 'Coupon account created'; |
||
| 45 | $result['data'] = $this->pdo_instance()->select('coupon')->where(['code' => $coupon])->row_array(); |
||
| 46 | } |
||
| 47 | } |
||
| 48 | $result['title'] = 'Coupon Creation'; |
||
| 49 | |||
| 50 | return $result; |
||
| 51 | } |
||
| 52 | |||
| 53 | public function coupon_login(string $coupon) |
||
| 54 | { |
||
| 55 | $this->pdo_required(); |
||
| 56 | $result = ['title' => 'Coupon Login', 'error' => true]; |
||
| 57 | $login = $this->pdo_instance() |
||
| 58 | ->select('coupon') |
||
| 59 | ->where(['code' => $coupon]) |
||
| 60 | ->row_array(); |
||
| 61 | \Session\session::set_session('coupon', $login); |
||
| 62 | $result = array_replace($result, $login); |
||
| 63 | if (!isset($login['limit'])) { |
||
| 64 | $result['error'] = true; |
||
| 65 | $result['message'] = 'Coupon not registered'; |
||
| 66 | } else { |
||
| 67 | $limit = $login['limit'] - $login['success']; |
||
| 68 | if ($limit < 1) { |
||
| 69 | $result['error'] = true; |
||
| 70 | $result['message'] = 'Coupon was reached limit'; |
||
| 71 | //$this->logout(); |
||
| 72 | } else { |
||
| 73 | $result['error'] = false; |
||
| 74 | $result['message'] = 'Coupon login successfully'; |
||
| 75 | /** |
||
| 76 | * Update token. |
||
| 77 | */ |
||
| 78 | $uid = session_id(); |
||
| 79 | $uid .= '_' . $this->gen_token(strlen($uid)); |
||
| 80 | \Session\session::set_session('coupon_token', $uid); |
||
| 81 | $this->pdo_instance() |
||
| 82 | ->update( |
||
| 83 | 'coupon', |
||
| 84 | ['token' => $uid], |
||
| 85 | ['code' => $this->coupon_data('code')] |
||
| 86 | )->exec(); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | //$result = array_replace($result, $this->coupon()); |
||
| 90 | |||
| 91 | return $result; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Coupon login check. |
||
| 96 | * |
||
| 97 | * @return boolean |
||
| 98 | */ |
||
| 99 | public function is_login(bool $token = true) |
||
| 100 | { |
||
| 101 | if ($token) { |
||
| 102 | return \Session\session::has('coupon_token', false) && !empty($this->coupon_data('code')); |
||
| 103 | } |
||
| 104 | |||
| 105 | return !empty($this->coupon_data('code')); |
||
| 106 | } |
||
| 107 | |||
| 108 | public function logout() |
||
| 109 | { |
||
| 110 | \Session\session::unses([ |
||
| 111 | 'coupon_token', 'coupon_admin', 'coupon', 'im3', 'telkomsel', 'msisdn', 'tokenid', |
||
| 112 | ]); |
||
| 113 | \Cookie\helper::destroy(); |
||
| 114 | } |
||
| 115 | |||
| 116 | public function is_admin() |
||
| 117 | { |
||
| 118 | return \Cookie\helper::has(str_rot13('coupon_admin'), false); |
||
| 119 | } |
||
| 120 | |||
| 121 | public function set_admin($data, int $expire = 15, string $cookie_path) |
||
| 122 | { |
||
| 123 | \Cookie\helper::mins(str_rot13('coupon_admin'), $data, $expire, $cookie_path, $_SERVER['HTTP_HOST']); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Is localhost ? |
||
| 128 | */ |
||
| 129 | public function is_local() |
||
| 130 | { |
||
| 131 | if (defined('LOCAL')) { |
||
| 132 | return LOCAL; |
||
| 133 | } |
||
| 134 | |||
| 135 | return \MVC\helper::isLocal(); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Validate coupon token with current session zone divisor. |
||
| 140 | * |
||
| 141 | * @param callable $callback return callback(true|false, $result) |
||
| 142 | * |
||
| 143 | * @return boolean|mixed |
||
| 144 | */ |
||
| 145 | public function coupon_validate(callable $callback = null) |
||
| 146 | { |
||
| 147 | $this->pdo_required(); |
||
| 148 | $result = ['title' => 'Coupon validate', 'error' => true]; |
||
| 149 | if ($this->is_admin() || $this->is_local()) { |
||
| 150 | $result['session'] = \Session\session::all(); |
||
| 151 | } |
||
| 152 | if ($this->is_login()) { |
||
| 153 | $result['status'] = 'login'; |
||
| 154 | $coupon = $this->coupon(); |
||
| 155 | $dbtoken = null; |
||
| 156 | if (isset($coupon['token'])) { |
||
| 157 | $dbtoken = $coupon['token']; |
||
| 158 | } |
||
| 159 | |||
| 160 | $result['data'] = $coupon; |
||
| 161 | $result['token']['db'] = $dbtoken; |
||
| 162 | $result['token']['session'] = \Session\session::get('coupon_token'); |
||
| 163 | |||
| 164 | if (\Session\session::get('coupon_token') == $dbtoken) { |
||
| 165 | $limit = $this->coupon_limit(); |
||
| 166 | $result['limit'] = $limit; |
||
| 167 | if ($limit > 0) { |
||
| 168 | $result['error'] = false; |
||
| 169 | } else { |
||
| 170 | $result['message'] = 'Token limit was reached'; |
||
| 171 | } |
||
| 172 | } else { |
||
| 173 | $result['message'] = 'Token coupon doesnt match'; |
||
| 174 | } |
||
| 175 | } else { |
||
| 176 | $result['message'] = 'Coupon login required'; |
||
| 177 | } |
||
| 178 | if (is_callable($callback)) { |
||
| 179 | return call_user_func($callback, (false === $result['error']), $result); |
||
| 180 | } |
||
| 181 | |||
| 182 | return false === $result['error']; //check if valid (true) |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Add Success 1 to database. |
||
| 187 | * |
||
| 188 | * @return void |
||
| 189 | */ |
||
| 190 | public function add_success(string $coupon_code) |
||
| 191 | { |
||
| 192 | $this->pdo_required(); |
||
| 193 | $this->pdo_instance()->query("UPDATE `coupon` SET `success`= `success`+1 WHERE `code` = '{$coupon_code}'")->exec(); |
||
| 194 | } |
||
| 195 | |||
| 196 | public function add_log(string $msisdn, string $coupon_code) |
||
| 197 | { |
||
| 198 | $this->pdo_required(); |
||
| 199 | $regex = '/^0|^\+62/s'; |
||
| 200 | $msisdn = preg_replace($regex, '62', trim($msisdn)); |
||
| 201 | $this->pdo_instance()->insert('coupon_log', ['msisdn' => $msisdn, 'coupon' => $coupon_code])->exec(); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Get current coupon limit value. |
||
| 206 | * |
||
| 207 | * @return int |
||
| 208 | */ |
||
| 209 | public function coupon_limit() |
||
| 210 | { |
||
| 211 | $coupon = $this->coupon(); |
||
| 212 | $ret = 0; |
||
| 213 | if (isset($coupon['limit'], $coupon['success'])) { |
||
| 214 | $ret = $coupon['limit'] - $coupon['success']; |
||
| 215 | } |
||
| 216 | if ($ret < 1) { |
||
| 217 | $this->logout(); |
||
| 218 | } |
||
| 219 | |||
| 220 | return $ret; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Get current coupon datas. |
||
| 225 | * |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | public function coupon() |
||
| 229 | { |
||
| 230 | $this->pdo_required(); |
||
| 231 | if (!$this->is_login()) { |
||
| 232 | return null; |
||
| 233 | } |
||
| 234 | $ret = []; |
||
|
0 ignored issues
–
show
|
|||
| 235 | try { |
||
| 236 | $ret = $this->pdo_instance()->select('coupon')->where(['code' => $this->coupon_data('code')])->row_array(); |
||
| 237 | \Session\session::set_session('coupon', $ret); |
||
| 238 | } catch (\Throwable $th) { |
||
| 239 | //ev($this->pdo_instance()); |
||
| 240 | //throw $th; |
||
| 241 | } |
||
| 242 | |||
| 243 | return $ret; |
||
| 244 | } |
||
| 245 | |||
| 246 | public function coupon_data(string $key) |
||
| 247 | { |
||
| 248 | if (isset($_SESSION['coupon'][$key])) { |
||
| 249 | return $_SESSION['coupon'][$key]; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | public function redirect(string $path) |
||
| 254 | { |
||
| 255 | //\MVC\router::safe_redirect($path); |
||
| 256 | if (!headers_sent()) { |
||
| 257 | header('Location: ' . $path); |
||
| 258 | } else { |
||
| 259 | echo 'Your access was blocked. please visit <a href="' . $path . '">Here ' . md5($path) . '</a>'; |
||
| 260 | } |
||
| 261 | exit; |
||
|
0 ignored issues
–
show
|
|||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Generate token. |
||
| 266 | * |
||
| 267 | * @return string |
||
| 268 | */ |
||
| 269 | public function gen_token(int $length = 10) |
||
| 270 | { |
||
| 271 | $token = ''; |
||
| 272 | $codeAlphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
||
| 273 | $codeAlphabet .= 'abcdefghijklmnopqrstuvwxyz'; |
||
| 274 | $codeAlphabet .= '0123456789'; |
||
| 275 | $max = strlen($codeAlphabet); // edited |
||
| 276 | |||
| 277 | for ($i = 0; $i < $length; ++$i) { |
||
| 278 | $token .= $codeAlphabet[random_int(0, $max - 1)]; |
||
| 279 | } |
||
| 280 | |||
| 281 | return $token; |
||
| 282 | } |
||
| 283 | |||
| 284 | public function e($data) |
||
| 285 | { |
||
| 286 | exit(\JSON\json::json($data)); |
||
|
0 ignored issues
–
show
Are you sure the usage of
JSON\json::json($data) targeting JSON\json::json() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||
| 287 | } |
||
| 288 | } |
||
| 289 |