/** * Execute the Process * * @todo execute * @param $callback * @return mixed */ public function execute($callback = "") { $request = new LoginRequest($_POST); if ($request->validate()) { $attempt = User::where('username', $request->get('username'))->where('password', Hash::encode($request->get('password')))->where('active', 'yes'); if ($attempt->exists()) { $user = $attempt->first(); $user->remember_token = Token::create(); $user->save(); $_SESSION['user'] = $user(); return Route::redirect('welcome'); } else { Session::setFlash('flash', 'username/password is incorrect.<br><br>'); return $callback(); } } else { return $callback(); } }
/** * Actual validation of request with rules implied * from its child classes. * * @param null $route * @return bool|void */ public function validate($route = null) { for ($i = 0; $i < count($this->request); $i++) { $field = array_keys($this->request); if (array_key_exists($field[$i], $this->rules)) { $rule = explode('|', $this->rules[$field[$i]]); for ($z = 0; $z < count($rule); $z++) { if ($rule[$z] == 'required') { if (strlen($this->request[$field[$i]]) == 0) { $this->errors[$field[$i]] = str_replace('_', ' ', $field[$i]) . " is required."; break; } } if (preg_match('/unique/i', $rule[$z])) { $db = new Database(); $value = $this->request[$field[$i]]; foreach ($rule as $item) { if ($item == 'password') { $value = Hash::encode($value); break; } } if ($db->table(explode(':', $rule[$z])[1])->where($field[$i], $value)->exists()) { $this->errors[$field[$i]] = str_replace('_', ' ', $field[$i]) . " not available."; break; } } if ($rule[$z] == 'email') { if (!preg_match('/@/', $this->request[$field[$i]])) { $this->errors[$field[$i]] = "Enter a valid e-mail."; break; } } if ($rule[$z] == 'alphanumeric') { if (!preg_match('/[^A-Za-z0-9]/i', $this->request[$field[$i]])) { $this->errors[$field[$i]] = "Only alphanumeric characters are allowed."; break; } } if ($rule[$z] == 'letters') { if (!preg_match('/^[A-Za-z]/i', $this->request[$field[$i]])) { $this->errors[$field[$i]] = str_replace('_', ' ', $field[$i]) . " accepts letters only."; break; } } if ($rule[$z] == 'number' || $rule[$z] == 'numeric') { if (!preg_match('/[0-9]/', $this->request[$field[$i]])) { $this->errors[$field[$i]] = str_replace('_', ' ', $field[$i]) . " should be numeric."; break; } } if (preg_match('/match/i', $rule[$z])) { $compare = explode(':', $rule[$z])[1]; if ($this->request[$field[$i]] !== $this->request[$compare]) { $this->errors[$field[$i]] = "Field did not match to {$compare}."; $this->errors[$compare] = "Field did not match to {$field[$i]}."; break; } } if (preg_match('/min/i', $rule[$z])) { $min = explode(':', $rule[$z])[1]; if (strlen($this->request[$field[$i]]) < $min) { $this->errors[$field[$i]] = str_replace('_', ' ', $field[$i]) . " requires a minimum of {$min} characters."; break; } } if (preg_match('/max/i', $rule[$z])) { $max = explode(':', $rule[$z])[1]; if (strlen($this->request[$field[$i]]) > $max) { $this->errors[$field[$i]] = str_replace('_', ' ', $field[$i]) . " requires a maximum of {$max} characters."; break; } } } } } $fileRules = array_keys($this->rules); for ($f = 0; $f < count($fileRules); $f++) { if (array_key_exists($fileRules[$f], $_FILES)) { if (empty($_FILES[$fileRules[$f]]['name'])) { $this->errors[$fileRules[$f]] = str_replace('_', ' ', $fileRules[$f]) . " is required."; } } } $redirectRoute = !is_null($route) ? $route : $this->route; if (is_null($this->errors)) { return true; } else { $_SESSION['__ERRORS__'] = $this->errors; $_SESSION['__FIELDS__'] = $this->request; return header("location: {$redirectRoute}"); } }
/** * Command Parser * * @return mixed */ private function parseCommand() { /** * Cleaning */ if ($this->command[1] == 'clear:all') { $this->clear('sessions'); $this->clear('logs'); return die("\nall trash cleared.\n"); } elseif ($this->command[1] == 'clear:sessions') { $this->clear('sessions'); return die("\nsessions directory cleared.\n"); } elseif ($this->command[1] == 'clear:logs') { $this->clear('logs'); return die("\nlogs directory cleared.\n"); } elseif ($this->command[1] == 'clear:backups') { $this->clear('backups'); return die("\nbackups directory cleared.\n"); } elseif ($this->command[1] == 'create:model') { if (isset($this->command[2])) { $option = isset($this->command[3]) ? $this->command[3] : strtolower($this->command[2]); return $this->createModel($this->command[2], $option); } else { die("\ntoo few arguments, create:model expects [name], [table] is optional\n"); } } elseif ($this->command[1] == 'create:controller') { if (isset($this->command[2])) { $option = isset($this->command[3]) ? $this->command[3] : null; return $this->createController($this->command[2], $option); } else { die("\ntoo few arguments, create:controller expects [name], [empty] is optional\n"); } } elseif ($this->command[1] == 'create:migration') { if (isset($this->command[2]) && isset($this->command[3])) { return $this->createMigration($this->command[2], $this->command[3]); } else { die("\ntoo few arguments, create:migration expects [name] [table]\n"); } } elseif ($this->command[1] == 'create:request') { if (isset($this->command[2])) { return $this->createRequest($this->command[2]); } else { die("\ncreate:request expects parameter [name]\n"); } } elseif ($this->command[1] == 'create:process') { if (isset($this->command[2])) { return $this->createProcess($this->command[2]); } else { die("\ncreate:process expects parameter [name]\n"); } } elseif ($this->command[1] == 'create:seeder') { if (isset($this->command[2]) && isset($this->command[3])) { return $this->createSeeder($this->command[2], $this->command[3]); } else { die("\ntoo few arguments, create:seeder expects [name] [table]\n"); } } elseif ($this->command[1] == 'create:key') { return die("\n" . Hash::generateSalt() . "\n"); } elseif ($this->command[1] == 'db:migrate') { return $this->migrate('up'); } elseif ($this->command[1] == 'db:rollback') { return $this->migrate('down'); } elseif ($this->command[1] == 'db:table:up') { return $this->tableMigration($this->command[2], 'up'); } elseif ($this->command[1] == 'db:table:down') { return $this->tableMigration($this->command[2], 'down'); } elseif ($this->command[1] == 'db:backup') { return $this->backup(); } elseif ($this->command[1] == 'db:restore') { return $this->restore(); } elseif ($this->command[1] == 'db:seed') { return $this->seed(); } elseif ($this->command[1] == 'hash:encode') { if (!isset($this->command[2])) { die("\nhash:verify expects [data]\n"); } return die("\n" . Hash::encode(trim($this->command[2], ' '))); } elseif ($this->command[1] == 'hash:verify') { if (!isset($this->command[2]) || !isset($this->command[3])) { die("\ntoo few arguments, hash:verify expects [data] and [hashed] value\n"); } return Hash::verify($this->command[2], $this->command[3]) ? die("\ntrue\n") : die("\nfalse\n"); } elseif ($this->command[1] == 'cipher:encrypt') { if (!isset($this->command[2])) { die("\ncipher:encrypt expects [string]\n"); } return die("\n" . Cipher::encrypt($this->command[2]) . "\n"); } elseif ($this->command[1] == 'cipher:decrypt') { if (!isset($this->command[2])) { die("\ncipher:encrypt expects [string]\n"); } return die("\n" . Cipher::decrypt($this->command[2]) . "\n"); } else { die("\nerror: unknown command '{$this->command[1]}' type 'help' for information.\n"); } }
/** * Seed the database table */ public function __construct() { User::insert(['firstname' => 'John', 'lastname' => 'Doe', 'username' => 'username', 'password' => Hash::encode('password'), 'email' => '*****@*****.**', 'number' => 010000040120, 'avatar' => 'default.jpg', 'role' => 'superadmin', 'active' => 'yes', 'date_added' => date_now(), 'time_added' => time_now()]); }
/** * Compare string to hashed string * * @param $string * @param $hashed * @return mixed */ function hash_verify($string, $hashed) { return Hash::verify($string, $hashed); }