/** * Check if enabled */ public function enable() { if ($this->json() && !zbase_is_json()) { return false; } return zbase_value_get($this->getAttributes(), 'enable', false); }
protected function _contactUs() { $success = false; if ($this->isPost()) { if (!zbase_captcha_verify()) { return $this->buildFailedValidationResponse(zbase_request(), ['ReCAPTCHA Validation failed.']); } $validatorMessages = ['email.required' => _zt('Email Address is required.'), 'email.email' => _zt('Invalid email address.'), 'comment.required' => _zt('Message is required.'), 'name.required' => _zt('Name is required.')]; $rules = ['email' => 'required|email', 'comment' => 'required', 'name' => 'required']; $valid = $this->validateInputs(zbase_request_inputs(), $rules, $validatorMessages); if (!empty($valid)) { $data = zbase_request_inputs(); $success = zbase_messenger_email('contactus', zbase_request_input('email'), _zt(zbase_site_name() . ' - Contact Us Form - ' . zbase_request_input('name')), zbase_view_file_contents('email.contactus'), $data); if (!empty($success)) { zbase_alert('success', _zt('Message sent!')); zbase()->json()->setVariable('contact_success', 1); if (!zbase_is_json()) { return redirect(zbase_url_previous()); } } else { zbase_alert('error', _zt('There was a problem sending your message. Kindly try again!')); } } } }
/** * Check if we are searching * * @return boolean */ public function isSearching() { if (zbase_request_is_post() && zbase_is_json() && !empty(zbase_request_input($this->getWidgetPrefix('search_query')))) { return true; } return false; }
public function controllerIndex() { if (!$this->getModule()->hasAccess()) { if (zbase_auth_has()) { return $this->unathorized(_zt('You don\'t have enough access to the resource.')); } else { return redirect()->to(zbase_url_from_route('login')); } } /** * Check for widgets */ $widgetsAction = $action = str_replace('.', '-', $this->getRouteParameter('action', 'index')); $requestMethod = zbase_request_method(); if (!empty($this->nodeName)) { $widgetsAction = $requestMethod . '-node-' . $this->nodeName . '-' . $action; $htmls = []; } $isAjax = zbase_request_is_ajax(); if ($isAjax) { $widgetsAction = (!empty($this->nodeName) ? $requestMethod . '-node-' . $this->nodeName . '-' : '') . 'json-' . $action; $htmls = []; } if ($this->getModule()->hasAction($requestMethod . '-' . $action)) { $widgetsAction = $requestMethod . '-' . $action; $action = $widgetsAction; $htmls = []; } $widgets = $this->getModule()->pageProperties($action)->widgetsByControllerAction($widgetsAction); if (count($widgets) == 1) { $firstWidget = collect($widgets)->first(); if ($firstWidget instanceof \Zbase\Widgets\WidgetInterface) { $firstWidget->pageProperties($widgetsAction); } } if (!is_array($widgets) && $widgets instanceof \Illuminate\Http\RedirectResponse) { return $widgets; } zbase()->json()->addVariable('_widget', $this->getModule()->id() . '_' . str_replace('-', '', $action)); if (zbase_is_dev()) { zbase()->json()->addVariable(__METHOD__, $widgetsAction); if (zbase_request_is_post()) { zbase()->json()->addVariable('_POST_PARAMETERS', zbase_request_inputs()); } zbase()->json()->addVariable('_ROUTE_PARAMETERS', zbase_route_inputs()); zbase()->json()->addVariable('_GET_PARAMETERS', zbase_request_query_inputs()); } // dd($this->getModule(), $widgetsAction, $widgets); if (empty($widgets)) { return zbase_abort(404); } foreach ($widgets as $widget) { if (!empty($this->nodeName)) { zbase()->json()->addVariable('node', ['prefix' => $this->getModule()->nodeNamespace(), 'name' => $this->nodeName, 'support' => 1]); $widget->setNodename($this->nodeName)->setNodeSupport(true); } if ($widget instanceof \Zbase\Widgets\ControllerInterface) { $v = $widget->validateWidget($action); if ($v instanceof \Illuminate\Contracts\Validation\Validator) { if ($isAjax) { zbase()->json()->addVariable('errors', $v->errors()->getMessages()); return new \Illuminate\Http\JsonResponse($v->errors()->getMessages(), 422); } else { return redirect()->to($this->getRedirectUrl())->withInput(zbase_request_inputs())->withErrors($v->errors()->getMessages()); } } $ret = $widget->controller($this->getRouteParameter('action', 'index')); if ($ret instanceof \Zbase\Exceptions\NotFoundHttpException) { return $this->notFound(); } if ($ret instanceof \Zbase\Exceptions\UnauthorizedException) { return $this->unathorized(); } if ($ret instanceof \Zbase\Exceptions\Exception) { return $this->error(); } if ($ret instanceof \Illuminate\Http\RedirectResponse) { if ($isAjax) { zbase()->json()->addVariable('redirect', $ret->getTargetUrl()); } else { return $ret; } } if (zbase_is_json()) { zbase_response_format_set('json'); $jsonIndexName = $widget->getWidgetPrefix(); if (zbase_is_angular()) { if ($widget instanceof \Zbase\Widgets\Type\Datatable) { $angularTemplate = zbase_angular_widget_datatable($this->getModule(), $widget); $jsonIndexName = $angularTemplate['serviceName']; } } if (zbase_is_dev()) { zbase()->json()->addVariable('$jsonIndexName', $jsonIndexName); } if (!$widget->isExporting()) { zbase()->json()->addVariable($jsonIndexName, $widget->toArray()); } } else { if ($isAjax) { $htmls[str_replace('-', '_', $widget->id())] = $widget->render(); } } $widget->pageProperties($widgetsAction); } } if (!empty($isAjax)) { zbase()->json()->addVariable('_widgets', 1); zbase()->json()->addVariable('html', $htmls); } else { return $this->view(zbase_view_file('module.index'), array('module' => $this->getModule(), 'widgets' => $widgets)); } }
/** * Return the Response * @param mixed $response * @return mixed */ function zbase_response($response) { // HTTP/1.1 204 No Content // $apiResponse = zbase()->json()->getVariable('api'); // if(!empty($apiResponse) && $apiResponse instanceof \Zbase\Exceptions\HttpException && $apiResponse->getStatusCode() == 204) // { // $response->header('HTTP/1.1 204 No Content'); // return $response; // } $returnNoContent = ''; $errorResponse = false; $xmlResponse = false; $responseFormat = zbase_response_format(); if (zbase_is_json()) { $responseFormat = 'json'; } if ($responseFormat == 'json' || zbase_request_is_ajax()) { $jsonResponse = true; } if ($responseFormat == 'xml') { $xmlResponse = true; } if (zbase_is_angular_template()) { $responseFormat = 'html'; $jsonResponse = false; } if (!empty($jsonResponse)) { $formId = zbase_request_input('_formId', null); if (!empty($formId)) { zbase()->json()->setVariable('_formId', $formId); } zbase()->json()->setVariable('_route', zbase_route_name()); zbase()->json()->setVariable('_package', zbase_view_template_package()); $code = 200; if ($response instanceof \RuntimeException) { $code = $response->getStatusCode(); zbase()->json()->setVariable('statusCode', $code); if ($code !== 200) { $errorResponse = true; zbase()->json()->setVariable('statusMessage', $response->getStatusMessage()); } } /** * its ajax, but method is GET */ if (empty($errorResponse)) { $tokenResponse = zbase_request_input('token', zbase_request_query_input('token', false)); if (!$tokenResponse) { zbase()->json()->setVariable('_token', zbase_csrf_token()); } } zbase()->json()->setVariable('_alerts', ['errors' => zbase_alerts('error'), 'messages' => zbase_alerts('success'), 'info' => zbase_alerts('info'), 'warning' => zbase_alerts('warning')]); $jsonScripts = zbase()->json()->getVariable('_html_script'); $jsonScriptReturn = ''; if (!empty($jsonScripts)) { foreach ($jsonScripts as $jScripts) { if (!empty($jScripts[0])) { $jScripts[0] = str_replace(array('<script type="text/javascript">', '</script>'), '', $jScripts[0]); $jsonScriptReturn .= $jScripts[0] . ';'; } } } zbase()->json()->setVariable('_html_script', str_replace(';;', ';', $jsonScriptReturn)); $forceResponse = zbase_request_input('forceResponse', zbase_request_query_input('forceResponse', false)); /** * JSONP Callback */ $jsonCallback = zbase_request_query_input('callback', zbase_request_query_input('jsonp', false)); if (!$forceResponse) { zbase_alerts_render(); if (!empty($jsonCallback)) { return response()->json(zbase()->json()->getVariables(), $code)->setCallback($jsonCallback); } else { return response()->json(zbase()->json()->getVariables(), $code); } } } if ($response instanceof \RuntimeException) { if ($response->getStatusCode() == '302') { if (zbase_is_json()) { zbase_alerts_render(); if (!empty($jsonCallback)) { return response()->json(zbase()->json()->getVariables(), 302)->setCallback($jsonCallback); } else { return response()->json(zbase()->json()->getVariables(), 302); } } } return $response->render(zbase_request(), $response); } /** * REsponse with a javascript code */ if ($responseFormat == 'javascript') { $response = \Response::make($response, 200); $response->header('Content-Type', 'application/javascript'); } return $response; }
/** * Send a reset link to the given user. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function postEmail(Request $request) { // $this->validate($request, ['email' => 'required|email|exists:' . zbase_config_get('entity.user.table.name') . ',email']); $entity = zbase()->entity('user', [], true); $user = $entity->repo()->by('email', zbase_request_input('email'))->first(); if (!empty($user)) { $broker = $this->getBroker(); $response = \Password::broker($broker)->sendResetLink($this->getSendResetLinkEmailCredentials($request), $this->resetEmailBuilder()); $user->lostPassword(); } zbase()->json()->setVariable('password_success', 1); zbase()->json()->setVariable('_redirect', zbase_url_previous()); if (!zbase_is_json()) { return redirect()->back()->with('status', trans(\Password::RESET_LINK_SENT)); } }
/** * Handle a login request to the application. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function postLogin(Request $request) { try { if (!$this->authEnabled()) { return $this->notfound('User authentication is disabled.'); } $rules = [$this->loginUsername() => 'required', 'password' => 'required']; $messages = []; $this->validate($request, $rules, $messages); // If the class is using the ThrottlesLogins trait, we can automatically throttle // the login attempts for this application. We'll key this by the username and // the IP address of the client making these requests into this application. $throttles = $this->isUsingThrottlesLoginsTrait(); if ($throttles && $this->hasTooManyLoginAttempts($request)) { if (zbase_is_json()) { zbase()->json()->setVariable('login_lock', 1); } return $this->sendLockoutResponse($request); } $credentials = $this->getCredentials($request); if (\Auth::attempt($credentials, $request->has('remember'))) { if (\Auth::guard($this->getGuard())->user()->isAdmin()) { $this->redirectTo = zbase_url_from_route('admin'); if (zbase_is_json()) { zbase()->json()->setVariable('_redirect', zbase_url_from_route('admin')); } return $this->handleUserWasAuthenticated($request, $throttles); } if (zbase_route_username()) { $user = \Auth::guard($this->getGuard())->user(); $usernameRoutePrefix = zbase_route_username_prefix(); $this->redirectTo = zbase_url_from_route('home', [$usernameRoutePrefix => $user->username()]); } else { $this->redirectTo = zbase_url_from_route('home'); } if (!empty($redirect)) { $this->redirectTo = $redirect; } return $this->handleUserWasAuthenticated($request, $throttles); } if ($throttles) { $this->incrementLoginAttempts($request); } $this->message('error', $this->getFailedLoginMessage()); return redirect($this->loginPath())->withInput($request->only($this->loginUsername(), 'remember'))->withErrors([$this->loginUsername() => $this->getFailedLoginMessage()]); } catch (\Zbase\Exceptions\RuntimeException $e) { zbase_exception_throw($e); return $this->error(); } }
/** * Check if to return JSON * @return boolean */ public function isJson() { return zbase_is_json(); }