Example #1
0
 public function dologinAction()
 {
     Db::connect();
     $bean = R::dispense('user');
     // the redbean model
     $required = ['Name' => 'name', 'Email' => 'email', 'User_Name' => ['rmnl', 'az_lower'], 'Password' => 'password_hash'];
     \RedBeanFVM\RedBeanFVM::registerAutoloader();
     // for future use
     $fvm = \RedBeanFVM\RedBeanFVM::getInstance();
     $fvm->generate_model($bean, $required);
     //the magic
     R::store($bean);
     $val = new validation();
     $val->addSource($_POST)->addRule('email', 'email', true, 1, 255, true)->addRule('password', 'string', true, 10, 150, false);
     $val->run();
     if (count($val->errors)) {
         Debug::r($val->errors);
         foreach ($val->errors as $error) {
             Notification::setMessage($error, Notification::TYPE_ERROR);
         }
         $this->redirect(Request::createUrl('login', 'login'));
     } else {
         Notification::setMessage("Welcome back !", Notification::TYPE_SUCCESS);
         Debug::r($val->sanitized);
         session::set('user', ['sanil']);
         $this->redirect(Request::createUrl('index', 'index'));
     }
 }
Example #2
0
 protected function __construct($config)
 {
     if (!$config) {
         throw new \exception('`$config` cant be null when constructing StarterKit');
     }
     if ($config['strict']) {
         error_reporting(E_ALL | E_NOTICE | E_STRICT);
     }
     $this->public_html = $config['public'];
     $this->debug = $config['debug'];
     date_default_timezone_set($config['timezone']);
     $this->slim = new \Slim\Slim($config['slim_args']);
     $this->twig_config = $config['twig_args'];
     $this->smtp_config = $config['smtp_args'];
     $this->files =& $_FILES;
     $this->get = $this->slim->request()->get();
     $this->post = $this->slim->request()->post();
     if ($config['session_args']['type'] === 'redis') {
         ini_set('session.save_handler', 'redis');
         ini_set('session.timeout', 18000);
         // each client should remember their session id for EXACTLY 1 hour
         session_set_cookie_params(18000);
         ini_set('session.save_path', 'tcp://' . $config['session_args']['host'] . ':' . $config['session_args']['port']);
     }
     session_name($config['session_args']['name']);
     session_start();
     $this->session =& $_SESSION;
     $this->remote_addr = $this->slim->request->getIp();
     if (!isset($this->session['notifications'])) {
         $this->session['notifications'] = [];
     }
     //sometimes you might need to set additional args. you could allow middleware to add args here, or virtually anything else you want
     $base_url = $config['scheme'] . $_SERVER['SERVER_NAME'];
     $extra_template_args = ['csrf' => $this->csrf(), 'base_url' => $base_url . '/', 'year' => date('Y'), 'styles' => '', 'scripts' => '', 'styles_external' => '', 'scripts_external' => '', 'user' => $this->is_user() ? $this->session['user'] : false, 'admin' => $this->is_admin() ? $this->session['admin'] : false, 'url' => $this->getUrl($base_url), 'canonical' => $base_url . $this->slim->request()->getResourceUri()];
     $this->args = array_merge($config['template_args'], $extra_template_args);
     //todo make these all lazy load for performance.
     $this->cache = \StarterKit\Cache::getInstance($config['cache_args']);
     $this->db = \StarterKit\DB::getInstance($config['db_args']);
     $this->filter = \RedBeanFVM\RedBeanFVM::getInstance();
     $this->smtp = \StarterKit\Email::getInstance($config['smtp_args']);
     self::$instance =& $this;
 }
<?php

require '../cron_db_connection.php';
require '../vendor/autoload.php';
$filter = \RedBeanFVM\RedBeanFVM::getInstance();
$x = $db->findAll('country', ' independence IS NOT NULL');
foreach ($x as &$y) {
    $date = explode('-', $y->independence);
    $y->year = $date[0];
    $y->month = $date[1];
    $y->day = $date[2];
    $db->store($y);
}
<?php

//example illustrates how to implement a custom validation function, such as one that validates a hash
require 'redbean/rb.php';
require 'RedBeanFVM.php';
if (!empty($_POST)) {
    R::configure();
    //configure redbean
    $fvm = \RedBeanFVM\RedBeanFVM::getInstance();
    $bean = R::dispense('automobile');
    $required = ['make' => 'min', 'model' => 'min', 'year' => 'car_year', 'vin-number' => 'car_vin_number'];
    //now we must create the custom filters ...
    $fvm->custom_filter('car_year', function ($input) {
        if (!preg_match('/^[0-9]{4}$/', $input)) {
            throw new \exception('Invalid Year entered');
        }
        return $input;
    });
    $fvm->custom_filter('car_vin_number', function ($input) {
        // vin numbers are 17 in length alphanumeric
        if (!preg_match('/^[0-9A-Za-z]{17}$/', $input)) {
            throw new \exception('Invalid VIN entered.');
        }
        return strtoupper($input);
        //we dont really care if they typed lower case. we can fix it for them.
    });
    $fvm->generate_model($bean, $required);
    //boom! now your bean is ready for storage.
    R::store($bean);
}