Beispiel #1
0
 /**
  * Forgery constructor
  */
 public function __forge()
 {
     $user = \Magelight\Auth\Models\User::find(\Magelight\Http\Request::getInstance()->getGet('id'));
     if ($user) {
         $this->set('user', $user->asArray());
     }
 }
Beispiel #2
0
 /**
  * Check value with rule
  * Returns:
  *    - true if rule passed.
  *    - false if value doesn`t match the rule.
  *
  * @param mixed $value
  * @return bool
  */
 public function check($value)
 {
     $reCaptcha = \Magelight\Webform\Models\Captcha\ReCaptcha::forge();
     $request = \Magelight\Http\Request::getInstance();
     $challenge = $request->getPost(\Magelight\Webform\Models\Captcha\ReCaptcha::CHALLENGE_INDEX);
     $response = $request->getPost(\Magelight\Webform\Models\Captcha\ReCaptcha::RESPONSE_INDEX);
     return $reCaptcha->recaptchaCheckAnswer($_SERVER['HTTP_HOST'], $challenge, $response)->is_valid;
 }
Beispiel #3
0
 /**
  * Get login form
  *
  * @return \Magelight\Webform\Blocks\Form
  */
 public function getLoginForm()
 {
     $form = Form::forge()->setHorizontal()->setConfigs('remindpass-form', $this->url(\Magelight\Config::getInstance()->getConfigString('global/auth/urls/login_url')));
     $fieldset = Fieldset::forge();
     $fieldset->addRowField(Elements\Input::forge()->setName('email'), __('E-Mail'));
     $fieldset->addRowField(Elements\PasswordInput::forge()->setName('password'), __('Password'));
     return $form->addFieldset($fieldset)->createResultRow(true)->addButtonsRow([Elements\Button::forge()->setContent(__('Enter'))->addClass('btn-primary'), Elements\Abstraction\Element::forge()->setTag('a')->setAttribute('href', $this->url('auth/remindpass'))->setContent(__('Remind password'))->setClass('btn')])->loadFromRequest(\Magelight\Http\Request::getInstance())->setValidator($this->getLoginFormValidator());
 }
Beispiel #4
0
 /**
  * Get form validator
  *
  * @return \Magelight\Webform\Models\Validator
  */
 public function getSampleFormValidator()
 {
     $validator = \Magelight\Webform\Models\Validator::forge();
     $validator->fieldRules('captcha')->validatePermanent()->captcha()->setCustomError(__('Protection code is incorrect'));
     $validator->fieldRules('password', __('Password'))->required()->chainRule()->minLength(3)->chainRule()->maxLength(32)->chainRule();
     $validator->fieldRules('passconf', __('Password confirmation'))->required()->chainRule()->equals(\Magelight\Http\Request::getInstance()->getPost('regform')['password'], __('entered password'));
     $validator->fieldRules('name')->required()->chainRule()->minLength(3)->chainRule()->maxLength(32)->chainRule()->pregMatch('/[a-z0-9а-я]*/i');
     $validator->fieldRules('email')->required()->chainRule()->email();
     $validator->setErrorsLimit(1);
     return $validator;
 }
Beispiel #5
0
 public function setUp()
 {
     $this->requestMock = $this->getMock(\Magelight\Http\Request::class, [], [], '', false);
     \Magelight\Http\Request::forgeMock($this->requestMock);
     $this->responseMock = $this->getMock(\Magelight\Http\Response::class, [], [], '', false);
     \Magelight\Http\Response::forgeMock($this->responseMock);
     $this->appMock = $this->getMock(\Magelight\App::class, [], [], '', false);
     \Magelight\App::forgeMock($this->appMock);
     $this->serverMock = $this->getMock(\Magelight\Http\Server::class, [], [], '', false);
     \Magelight\Http\Server::forgeMock($this->serverMock);
     $this->controller = \Magelight\Controller::forge();
 }
Beispiel #6
0
 /**
  * Run app
  *
  * @throws \Exception|\Magelight\Exception
  */
 public function run()
 {
     try {
         \Magelight\Event\Manager::getInstance()->dispatchEvent('app_start', []);
         $request = \Magelight\Http\Request::getInstance();
         $action = \Magelight\Components\Router::getInstance($this)->getAction((string) $request->getRequestRoute());
         $request->appendGet($action['arguments']);
         $this->dispatchAction($action);
     } catch (\Exception $e) {
         \Magelight\Log::getInstance()->add($e->getMessage());
         if ($this->developerMode) {
             throw $e;
         }
     }
 }
Beispiel #7
0
 /**
  * Run app
  *
  * @throws \Exception|Exception
  */
 public function run()
 {
     try {
         \Magelight\Event\Manager::getInstance()->dispatchEvent('app_start', []);
         $request = \Magelight\Http\Request::getInstance();
         $resource = $request->getGet('resource');
         $staticDir = realpath($this->getAppDir() . DS . \Magelight\Config::getInstance()->getConfigString('global/view/published_static_dir', 'pub/static'));
         foreach (array_reverse($this->getModuleDirectories()) as $modulesPath) {
             $resource = str_replace('\\/', DS, $resource);
             $filename = $modulesPath . DS . $resource;
             $targetFilename = $staticDir . DS . $resource;
             if (file_exists($filename)) {
                 if (!is_dir(dirname($targetFilename))) {
                     mkdir(dirname($targetFilename), 0777, true);
                 }
                 if (\Magelight\Config::getInstance()->getConfigBool('global/app/developer_mode', false)) {
                     $pathinfo = pathinfo($filename, PATHINFO_EXTENSION);
                     if (isset($this->mimeTypes[$pathinfo])) {
                         $mimeType = $this->mimeTypes[$pathinfo];
                         header('Content-type: ' . $mimeType);
                     }
                     echo file_get_contents($filename);
                     break;
                 }
                 copy($filename, $targetFilename);
                 header('Location: ' . \Magelight\Helpers\UrlHelper::getInstance()->getUrl($resource));
                 break;
             }
         }
     } catch (\Exception $e) {
         \Magelight\Log::getInstance()->add($e->getMessage());
         if ($this->developerMode) {
             throw $e;
         }
     }
 }
Beispiel #8
0
 /**
  * Load form from request
  *
  * @param \Magelight\Http\Request $request
  * @return Form
  */
 public function loadFromRequest(\Magelight\Http\Request $request = null)
 {
     if (empty($request)) {
         $request = \Magelight\Http\Request::getInstance();
     }
     $method = $this->getAttribute('method', 'post');
     $methodName = 'get' . ucfirst(strtolower($method));
     if (!empty($this->wrapIndex)) {
         $this->requestFields = $request->{$methodName}($this->wrapIndex, []);
         $this->requestUploads = $request->getFilesNormalized($this->wrapIndex, []);
     } else {
         $methodName .= 'Array';
         $this->requestFields = $request->{$methodName}();
         $this->requestUploads = $request->getFilesArrayNormalized();
     }
     return $this->setFormValuesFromRequestFields($this->requestFields);
 }
Beispiel #9
0
 /**
  * Constructor
  * 
  * @param array $routeAction
  *
  */
 public function init(array $routeAction = [])
 {
     $this->request = \Magelight\Http\Request::getInstance();
     $this->routeAction = $routeAction;
     $this->app = \Magelight\App::getInstance();
     $this->response = \Magelight\Http\Response::forge();
 }
Beispiel #10
0
 /**
  * Dispatch action
  *
  * @param array $action
  *
  * @return App
  * @throws \Magelight\Exception
  */
 public function dispatchAction(array $action)
 {
     $this->currentAction = $action;
     $eventManager = \Magelight\Event\Manager::getInstance();
     $request = \Magelight\Http\Request::getInstance();
     $eventManager->dispatchEvent('app_dispatch_action', ['action' => $action, 'request' => $request]);
     $controllerName = str_replace('/', '\\', $action['module'] . '\\Controllers\\' . ucfirst($action['controller']));
     $controllerMethod = $action['action'] . 'Action';
     $controller = call_user_func([$controllerName, 'forge']);
     /* @var $controller \Magelight\Controller */
     $eventManager->dispatchEvent('app_controller_init', ['controller' => $controller, 'action' => $action, 'request' => $request]);
     $controller->init($action);
     $eventManager->dispatchEvent('app_controller_initialized', ['controller' => $controller, 'action' => $action, 'request' => $request]);
     $controller->beforeExecute();
     $controller->{$controllerMethod}();
     $controller->afterExecute();
     $eventManager->dispatchEvent('app_controller_executed', ['controller' => $controller, 'action' => $action, 'request' => $request]);
     return $this;
 }
Beispiel #11
0
 public function testDispatchAction()
 {
     $action = ['module' => 'Magelight', 'controller' => 'controller', 'action' => 'index'];
     $requestMock = $this->getMock(\Magelight\Http\Request::class, [], [], '', false);
     \Magelight\Http\Request::forgeMock($requestMock);
     class_alias(\Magelight\Controller::class, '\\Magelight\\Controllers\\Controller');
     $controllerMock = $this->getMockForAbstractClass(\Magelight\Controller::class, [], '', false, false, false, ['indexAction', 'init', 'beforeExecute', 'afterExecute']);
     \Magelight\Controller::forgeMock($controllerMock);
     $controllerMock->expects($this->once())->method('indexAction');
     $controllerMock->expects($this->once())->method('init')->with($action);
     $controllerMock->expects($this->once())->method('beforeExecute');
     $controllerMock->expects($this->once())->method('afterExecute');
     $this->eventManagerMock->expects($this->at(0))->method('dispatchEvent')->with('app_dispatch_action', ['action' => $action, 'request' => $requestMock]);
     $this->eventManagerMock->expects($this->at(1))->method('dispatchEvent')->with('app_controller_init', ['controller' => $controllerMock, 'action' => $action, 'request' => $requestMock]);
     $this->eventManagerMock->expects($this->at(2))->method('dispatchEvent')->with('app_controller_initialized', ['controller' => $controllerMock, 'action' => $action, 'request' => $requestMock]);
     $this->eventManagerMock->expects($this->at(3))->method('dispatchEvent')->with('app_controller_executed', ['controller' => $controllerMock, 'action' => $action, 'request' => $requestMock]);
     $this->app->dispatchAction($action);
     $this->assertEquals($action, $this->app->getCurrentAction());
 }