예제 #1
0
 /**
  * Handle form processing instructions.
  *
  * @param Event $event
  */
 public function onFormProcessed(Event $event)
 {
     $form = $event['form'];
     $action = $event['action'];
     $params = $event['params'];
     $this->process($form);
     switch ($action) {
         case 'captcha':
             if (isset($params['recaptcha_secret'])) {
                 $recaptchaSecret = $params['recaptcha_secret'];
             } else {
                 if (isset($params['recatpcha_secret'])) {
                     // Included for backwards compatibility with typo (issue #51)
                     $recaptchaSecret = $params['recatpcha_secret'];
                 } else {
                     $recaptchaSecret = $this->config->get('plugins.form.recaptcha.secret_key');
                 }
             }
             // Validate the captcha
             $query = http_build_query(['secret' => $recaptchaSecret, 'response' => $form->value('g-recaptcha-response', true)]);
             $url = 'https://www.google.com/recaptcha/api/siteverify?' . $query;
             $response = json_decode(file_get_contents($url), true);
             if (!isset($response['success']) || $response['success'] !== true) {
                 $this->grav->fireEvent('onFormValidationError', new Event(['form' => $form, 'message' => $this->grav['language']->translate('PLUGIN_FORM.ERROR_VALIDATING_CAPTCHA')]));
                 $event->stopPropagation();
                 return;
             }
             break;
         case 'ip':
             $label = isset($params['label']) ? $params['label'] : 'User IP';
             $blueprint = $form->value()->blueprints();
             $blueprint->set('form/fields/ip', ['name' => 'ip', 'label' => $label]);
             $form->setFields($blueprint->fields());
             $form->setData('ip', Uri::ip());
             break;
         case 'message':
             $translated_string = $this->grav['language']->translate($params);
             $vars = array('form' => $form);
             /** @var Twig $twig */
             $twig = $this->grav['twig'];
             $processed_string = $twig->processString($translated_string, $vars);
             $form->message = $processed_string;
             break;
         case 'redirect':
             $this->grav['session']->setFlashObject('form', $form);
             $this->grav->redirect((string) $params);
             break;
         case 'reset':
             if (Utils::isPositive($params)) {
                 $form->reset();
             }
             break;
         case 'display':
             $route = (string) $params;
             if (!$route || $route[0] != '/') {
                 /** @var Uri $uri */
                 $uri = $this->grav['uri'];
                 $route = rtrim($uri->route(), '/') . '/' . ($route ?: '');
             }
             /** @var Twig $twig */
             $twig = $this->grav['twig'];
             $twig->twig_vars['form'] = $form;
             /** @var Pages $pages */
             $pages = $this->grav['pages'];
             $page = $pages->dispatch($route, true);
             if (!$page) {
                 throw new \RuntimeException('Display page not found. Please check the page exists.', 400);
             }
             unset($this->grav['page']);
             $this->grav['page'] = $page;
             break;
         case 'save':
             $prefix = !empty($params['fileprefix']) ? $params['fileprefix'] : '';
             $format = !empty($params['dateformat']) ? $params['dateformat'] : 'Ymd-His-u';
             $ext = !empty($params['extension']) ? '.' . trim($params['extension'], '.') : '.txt';
             $filename = !empty($params['filename']) ? $params['filename'] : '';
             $operation = !empty($params['operation']) ? $params['operation'] : 'create';
             if (!$filename) {
                 $filename = $prefix . $this->udate($format) . $ext;
             }
             /** @var Twig $twig */
             $twig = $this->grav['twig'];
             $vars = ['form' => $form];
             // Process with Twig
             $filename = $twig->processString($filename, $vars);
             $locator = $this->grav['locator'];
             $path = $locator->findResource('user://data', true);
             $dir = $path . DS . $form->name();
             $fullFileName = $dir . DS . $filename;
             $file = File::instance($fullFileName);
             if ($operation == 'create') {
                 $body = $twig->processString(!empty($params['body']) ? $params['body'] : '{% include "forms/data.txt.twig" %}', $vars);
                 $file->save($body);
             } elseif ($operation == 'add') {
                 if (!empty($params['body'])) {
                     // use body similar to 'create' action and append to file as a log
                     $body = $twig->processString($params['body'], $vars);
                     // create folder if it doesn't exist
                     if (!file_exists($dir)) {
                         mkdir($dir);
                     }
                     // append data to existing file
                     file_put_contents($fullFileName, $body, FILE_APPEND | LOCK_EX);
                 } else {
                     // serialize YAML out to file for easier parsing as data sets
                     $vars = $vars['form']->value()->toArray();
                     foreach ($form->fields as $field) {
                         if (isset($field['process']) && isset($field['process']['ignore']) && $field['process']['ignore']) {
                             unset($vars[$field['name']]);
                         }
                     }
                     if (file_exists($fullFileName)) {
                         $data = Yaml::parse($file->content());
                         if (count($data) > 0) {
                             array_unshift($data, $vars);
                         } else {
                             $data[] = $vars;
                         }
                     } else {
                         $data[] = $vars;
                     }
                     $file->save(Yaml::dump($data));
                 }
             }
             break;
     }
 }
예제 #2
0
 /**
  * Checks user authorization to the action.
  *
  * @param  string  $action
  * @return bool
  */
 public function authorize($action)
 {
     if (empty($this->items)) {
         return false;
     }
     return Utils::isPositive($this->get("access.{$action}"));
 }
예제 #3
0
파일: User.php 프로젝트: krsreenatha/grav
 /**
  * Checks user authorization to the action.
  *
  * @param  string  $action
  * @return bool
  */
 public function authorize($action)
 {
     if (empty($this->items)) {
         return false;
     }
     if (isset($this->state) && $this->state !== 'enabled') {
         return false;
     }
     return Utils::isPositive($this->get("access.{$action}"));
 }
예제 #4
0
 /**
  * Checks user authorization to the action.
  *
  * @param  string $action
  *
  * @return bool
  */
 public function authorize($action)
 {
     if (empty($this->items)) {
         return false;
     }
     if (isset($this->state) && $this->state !== 'enabled') {
         return false;
     }
     $return = false;
     //Check group access level
     $groups = $this->get('groups');
     if ($groups) {
         foreach ((array) $groups as $group) {
             $permission = self::getGrav()['config']->get("groups.{$group}.access.{$action}");
             $return = Utils::isPositive($permission);
             if ($return === true) {
                 break;
             }
         }
     }
     //Check user access level
     if ($this->get('access')) {
         if (Utils::resolve($this->get('access'), $action) !== null) {
             $permission = $this->get("access.{$action}");
             $return = Utils::isPositive($permission);
         }
     }
     return $return;
 }
예제 #5
0
파일: UtilsTest.php 프로젝트: getgrav/grav
 public function testIsPositive()
 {
     $this->assertTrue(Utils::isPositive(true));
     $this->assertTrue(Utils::isPositive(1));
     $this->assertTrue(Utils::isPositive('1'));
     $this->assertTrue(Utils::isPositive('yes'));
     $this->assertTrue(Utils::isPositive('on'));
     $this->assertTrue(Utils::isPositive('true'));
     $this->assertFalse(Utils::isPositive(false));
     $this->assertFalse(Utils::isPositive(0));
     $this->assertFalse(Utils::isPositive('0'));
     $this->assertFalse(Utils::isPositive('no'));
     $this->assertFalse(Utils::isPositive('off'));
     $this->assertFalse(Utils::isPositive('false'));
     $this->assertFalse(Utils::isPositive('some'));
     $this->assertFalse(Utils::isPositive(2));
 }