Example #1
0
 public function login($welcome = null)
 {
     if ($user = panel()->site()->user()) {
         go(panel()->urls()->index());
     }
     $message = l('login.error');
     $error = false;
     $form = panel()->form('login');
     $form->cancel = false;
     $form->save = l('login.button');
     $form->centered = true;
     if (r::is('post') and get('_csfr') and csfr(get('_csfr'))) {
         $data = $form->serialize();
         $user = site()->user(str::lower($data['username']));
         if (!$user) {
             $error = true;
         } else {
             if (!$user->hasPanelAccess()) {
                 $error = true;
             } else {
                 if (!$user->login(get('password'))) {
                     $error = true;
                 } else {
                     go(panel()->urls()->index());
                 }
             }
         }
     }
     if ($username = s::get('username')) {
         $form->fields->username->value = html($username, false);
     }
     return layout('login', array('meta' => new Snippet('meta'), 'welcome' => $welcome ? l('login.welcome') : '', 'form' => $form, 'error' => $error ? $message : false));
 }
Example #2
0
 protected function sort($page)
 {
     // handle sorting
     if (r::is('post') and $action = get('action') and $id = get('id')) {
         $subpage = $this->page($page->id() . '/' . $id);
         switch ($action) {
             case 'sort':
                 try {
                     $subpage->sort(get('to'));
                 } catch (Exception $e) {
                     // no error handling, because if sorting
                     // breaks, the refresh will fix it.
                 }
                 break;
             case 'toggle':
                 try {
                     $subpage->toggle('last');
                 } catch (Exception $e) {
                     // no error handling, because if sorting
                     // breaks, the refresh will fix it.
                 }
                 break;
             case 'hide':
                 try {
                     $subpage->hide();
                 } catch (Exception $e) {
                     // no error handling, because if sorting
                     // breaks, the refresh will fix it.
                 }
                 break;
         }
         $this->redirect($page, 'subpages');
     }
 }
Example #3
0
 public function __construct()
 {
     $endpoint = $this;
     if ($page = page('webmention') and kirby()->path() == $page->uri()) {
         if (r::is('post')) {
             try {
                 $endpoint->start();
                 header::status(202);
                 tpl::set('status', 'success');
                 tpl::set('alert', null);
             } catch (Exception $e) {
                 header::status(400);
                 tpl::set('status', 'error');
                 tpl::set('alert', $e->getMessage());
             }
         } else {
             tpl::set('status', 'idle');
         }
     } else {
         kirby()->routes(array(array('pattern' => 'webmention', 'method' => 'GET|POST', 'action' => function () use($endpoint) {
             try {
                 $endpoint->start();
                 echo response::success('Yay', 202);
             } catch (Exception $e) {
                 echo response::error($e->getMessage());
             }
         })));
     }
 }
Example #4
0
 /**
  * Dashboard /login action
  *
  * @return void
  */
 public function login()
 {
     if (site()->user()) {
         go('dashboard');
     }
     // save the flashed redirect
     flash('login.redirect', flash('login.redirect'));
     if (r::is('post')) {
         if ($this->form->validates()) {
             $user = site()->user($this->form->data('username'));
             if ($user && $user->login($this->form->data('password'))) {
                 flash('messages.success', 'Logged in!');
                 if (get('_redirect')) {
                     go(get('_redirect'));
                 }
             } else {
                 $this->form->addErrors(['login' => 'Invalid username and password']);
             }
         }
         go('login');
     }
     $this->render('dashboard/login', ['pageTitle' => 'Login', 'form' => $this->form, 'page' => page()]);
 }
Example #5
0
 /**
  * Generate the controller response.
  *
  * @param   string  $msg   Optional message to send with the response.
  * @param   integer $code  Response code to send.
  * @param   array   $data  Data to return.
  *
  * @return  Response
  */
 protected function response($msg, $code, $data)
 {
     if (!r::is('ajax')) {
         return $this->redirect('back', $data);
     }
     $response = array('status' => 'error', 'data' => $data, 'code' => $code, 'message' => $msg);
     return response::json($response, $code);
 }
Example #6
0
<?php

return function ($site, $pages, $page) {
    // handle the form submission
    if (r::is('post') and get('register')) {
        try {
            $user = $site->users()->create(array('username' => get('username'), 'email' => get('email'), 'password' => get('password'), 'language' => 'en'));
            // make sure the alert is being
            // displayed in the template
            $success = true;
        } catch (Exception $e) {
            // make sure the alert is being
            // displayed in the template
            $error = true;
        }
    } else {
        // nothing has been submitted
        // nothing has gone wrong
        $error = false;
    }
    return array('error' => $error, 'success' => $success);
};
Example #7
0
<?php

snippet('header');
?>

    <h1><?php 
echo $page->title()->html();
?>
</h1>

    <?php 
echo $page->text()->kirbytext();
?>

    <?php 
if (r::is('post') and get('register') !== null) {
    ?>
      <div class="uk-alert uk-alert-warning">
        <p>
          <?php 
    // Check for duplicate email addresses
    $duplicate = $site->users()->findBy('email', trim(get('email')));
    if (count($duplicate) === 0) {
        try {
            $user = $site->users()->create(array('username' => trim(get('username')), 'email' => trim(get('email')), 'password' => get('password'), 'firstName' => trim(get('firstname')), 'lastName' => trim(get('lastname')), 'language' => 'en', 'country' => get('country')));
            echo l::get('register-success');
        } catch (Exception $e) {
            echo l::get('register-failure');
        }
    } else {
        echo l::get('register-failure');
Example #8
0
<?php

use Email;
use r as Request;
use Jevets\Kirby\Form;
return function ($site, $pages, $page) {
    $form = new Form(['name' => ['rules' => ['required'], 'message' => 'Name is required.'], 'email' => ['rules' => ['required', 'email'], 'message' => 'Valid email is required'], 'phone' => [], 'message' => []]);
    if (Request::is('post')) {
        if ($form->validates()) {
            // send an email
            $body = snippet('emails/contact', $form->data(), true);
            $email = new Email(['to' => c::get('app.email.to'), 'from' => c::get('app.email.from'), 'subject' => 'Contact Form Submission (' . $form->data('name') . ')', 'replyto' => $form->data('email'), 'body' => $body]);
            $email->send();
            // notify the user
            flash('messages.success', ["We got your message! We'll be back in touch very soon."]);
            go('contact');
        } else {
            flash('messages.errors', ["There were errors with your submission..."]);
        }
        go($page->url());
    }
    return compact('form');
};
Example #9
0
 public function on($action, $callback)
 {
     // auto-trigger the submit event when the form is being echoed
     if (r::is('post')) {
         $callback($this);
     }
     $this->fields->append('csrf', static::field('hidden', array('name' => 'csrf', 'value' => panel()->csrf())));
 }
Example #10
0
 protected function sort($page)
 {
     if (!r::is('post') or get('action') != 'sort') {
         return;
     }
     $filenames = get('filenames');
     $counter = 0;
     foreach ($filenames as $filename) {
         if ($file = $page->file($filename)) {
             $counter++;
             try {
                 $file->update('sort', $counter);
             } catch (Exception $e) {
             }
         }
     }
     $this->redirect($page, 'files');
 }
Example #11
0
<?php

return function ($site, $pages, $page) {
    // handle the form submission
    if (r::is('post') and get('update')) {
        try {
            $user = $site->user()->update(array('firstname' => get('firstname'), 'lastname' => get('lastname'), 'email' => get('email'), 'bio' => get('bio'), 'link' => get('link'), 'language' => 'en'));
            if (get('password') === '') {
                // No password change
            } else {
                // Update password
                $user = $site->user()->update(array('password' => get('password')));
            }
            // make sure the alert is being
            // displayed in the template
            $success = true;
        } catch (Exception $e) {
            // make sure the alert is being
            // displayed in the template
            $error = true;
        }
    } else {
        // nothing has been submitted
        // nothing has gone wrong
        $error = false;
    }
    return array('error' => $error, 'success' => $success);
};
Example #12
0
 public function testIs()
 {
     $this->assertTrue(r::is('GET'));
     $this->assertFalse(r::is('ajax'));
 }
Example #13
0
<?php

return function ($site, $pages, $page) {
    // get source page
    $source = $_GET["source"];
    // form submission
    if (r::is("post") and get("login")) {
        // fetch the user, run login method
        if ($user = $site->user(get("username")) and $user->login(get("password"))) {
            if ($user->hasRole("external")) {
                // check source page
                switch ($source) {
                    case "Google":
                        // check user
                        switch ($user->username()) {
                            case "okgoogle":
                                // approved access
                                go($source);
                                break;
                            default:
                                // prevent page access
                                $accessError = true;
                        }
                        break;
                    case "Learnmetrics":
                        // check user
                        switch ($user->username()) {
                            case "fromdata":
                                // approved access
                                go($source);
                                break;
Example #14
0
<?php

// Honeypot trap for robots
if (r::is('post') and get('subject') != '') {
    go(url('error'));
}
$cart = Cart::getCart();
$shipping = s::get('shipping');
$discount = getDiscount($cart);
// Set transaction status
if (get('giftCertificatePaid') == 'true') {
    $status = 'paid';
} else {
    $status = 'pending';
}
// Set the timestamp so txn-id and txn-date use the same value
$timestamp = date('U');
// Create a YAML-structured list of products
$items = [];
foreach ($cart->getItems() as $i => $item) {
    // Look for downloads
    foreach (page($item->uri)->variants()->toStructure() as $variant) {
        if (str::slug($variant->name()) == $item->variant) {
            if ($variant->download_files()->isNotEmpty()) {
                // Build full URLs for downloads
                $files = [];
                foreach (explode(',', $variant->download_files()) as $filename) {
                    $files[] = url($item->uri) . '/' . $filename;
                }
                // Add downloads properties
                $item->downloads = ['files' => $files, 'expires' => $variant->download_days()->isEmpty() ? NULL : $timestamp + $variant->download_days()->value * 60 * 60 * 24];
Example #15
0
<?php

use Crazy\Form;
return function ($site, $pages, $page) {
    $event = $page->parent();
    $order = new CrazyEventOrder($event);
    $form = new Form(['guest' => []]);
    if (r::is('post')) {
        // Add a new guest
        if (get('add_guest') && ($guest = get('guest'))) {
            if ($event->seatsAvailable(1 + count($order->guests()))) {
                $order->addGuest(htmlspecialchars($guest));
            } else {
                $form->addError('guest', 'Sorry, there are no more seats available.');
            }
        }
        // Remove a guest
        if ($removeId = get('remove_guest')) {
            // the field is submitted as "id-[$id]"
            $id = substr($removeId, 3);
            $order->removeGuest($id);
        }
        go($page->url());
    }
    return ['event' => $event, 'studio' => $order->studio(), 'form' => $form, 'order' => $order];
};
Example #16
0
<?php

return function ($site, $pages, $page) {
    // Honeypot trap for robots
    if (r::is('post') and get('subject') != '') {
        go(url('error'));
    }
    // Process reset form
    if (r::is('post') and get('reset') !== null) {
        if (resetPassword(get('email'))) {
            $reset_message = l::get('reset-success');
        } else {
            $reset_message = l::get('reset-error');
        }
    } else {
        $reset_message = false;
    }
    // Pass variables to the template
    return ['reset_message' => $reset_message];
};
Example #17
0
<?php

return function ($site, $pages, $page) {
    // don't show the login screen to already logged in users
    if ($site->user()) {
        go('/');
    }
    // handle the form submission
    if (r::is('post') and get('login')) {
        // fetch the user by username and run the
        // login method with the password
        if ($user = $site->user(get('username')) and $user->login(get('password'))) {
            // redirect to the homepage
            // if the login was successful
            go('/');
        } else {
            // make sure the alert is being
            // displayed in the template
            $error = true;
        }
    } else {
        // nothing has been submitted
        // nothing has gone wrong
        $error = false;
    }
    return array('error' => $error);
};
 public function __construct($kirby, $root)
 {
     // check requirements
     $this->requirements();
     // store the instance as a singleton
     static::$instance = $this;
     $this->kirby = $kirby;
     $this->roots = new \Kirby\Panel\Roots($this, $root);
     $this->urls = new \Kirby\Panel\Urls($this, $root);
     // add the panel default options
     $this->kirby->options = array_merge($this->defaults(), $this->kirby->options);
     // setup the blueprints roots
     UserBlueprint::$root = $this->kirby->roots()->blueprints() . DS . 'users';
     PageBlueprint::$root = $this->kirby->roots()->blueprints();
     // load the site object
     $this->site = $this->site();
     // setup the session
     $this->session();
     // setup the multilang site stuff
     $this->multilang();
     // load all Kirby extensions (methods, tags, smartypants)
     $this->kirby->extensions();
     $this->kirby->plugins();
     // setup the form plugin
     form::$root = array('default' => $this->roots->fields, 'custom' => $this->kirby->roots()->fields());
     // load all available routes
     $this->routes = array_merge($this->routes, require $this->roots->config . DS . 'routes.php');
     // start the router
     $this->router = new Router($this->routes);
     // register router filters
     $this->router->filter('auth', function () use($kirby) {
         try {
             $user = panel()->user();
         } catch (Exception $e) {
             panel()->redirect('login');
         }
     });
     // check for a completed installation
     $this->router->filter('isInstalled', function () use($kirby) {
         if (panel()->users()->count() == 0) {
             panel()->redirect('install');
         }
     });
     // check for valid csrf tokens. Can be used for get requests
     // since all post requests are blocked anyway
     $this->router->filter('csrf', function () {
         panel()->csrfCheck();
     });
     // csrf protection for every post request
     if (r::is('post')) {
         $this->csrfCheck();
     }
 }
Example #19
0
            // Update password
            $user = $site->user()->update(array('password' => get('password')));
        }
        echo l::get('account-success');
    } catch (Exception $e) {
        echo l::get('account-failure');
    }
    ?>
        </p>
      </div>
    <?php 
}
?>

    <?php 
if (r::is('post') and get('delete')) {
    ?>
      <div class="uk-alert uk-alert-danger">
        <p>
          <?php 
    try {
        $user = $site->user();
        $user->logout();
        $site->user($user->username())->delete();
        go('/register');
    } catch (Exception $e) {
        echo l::get('account-delete-error');
    }
    ?>
        </p>
      </div>
Example #20
0
<?php

return function ($site, $pages, $page) {
    $completed = false;
    $error = false;
    // handle the form submission
    if (r::is('post') and get('donation')) {
        $amount = get('amount');
        if ($amount) {
            if ($amount == 'custom') {
                $amount = get('customInt');
            }
            if (is_numeric($amount)) {
                $amount = (double) $amount;
            } else {
                $error = 'Please enter a valid number for the donation amount.';
                goto errored;
            }
        } else {
            $error = 'Please specify an amount for the donation.';
            goto errored;
        }
        $name = get('name');
        if (!$name) {
            $error = 'Name is required.';
            goto errored;
        }
        $email = get('email');
        if (!$email) {
            $error = 'Email is required.';
            goto errored;