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!')); } } } }
/** * Controller Action * This will be called validating the form * @param string $action */ public function controller($action) { $this->setAction($action); $repoMethod = $this->_v('repo.method', 'count'); if ($repoMethod == 'update') { $ret = $this->entity()->widgetController(zbase_request_method(), $this->getAction(), [], $this); $actionMessages = $this->entity()->getActionMessages($this->getAction()); if (!empty($actionMessages)) { foreach ($actionMessages as $alertType => $alertMessages) { if (is_array($alertMessages)) { foreach ($alertMessages as $alertMessage) { zbase_alert($alertType, $alertMessage); } } } } if (!empty($ret)) { $url = zbase_url_previous(); return zbase_redirect()->to($url); } } if (!$this->checkUrlRequest()) { return zbase_abort(404); } $this->_rows(); }
/** * Verify Recaptcha * @return boolean */ function zbase_captcha_verify() { $enable = zbase_config_get('recaptcha.enable', false); $secretKey = zbase_config_get('recaptcha.secretkey', false); $response = zbase_request_input('g-recaptcha-response', false); if ($response !== false && !empty($secretKey) && !empty($enable)) { if (!empty($response)) { $response = json_decode(file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret=' . $secretKey . '&response=' . $response . '&remoteip=' . zbase_ip()), TRUE); if ($response['success'] == FALSE) { zbase_alert(\Zbase\Zbase::ALERT_ERROR, 'ReCAPTCHA Validation Failed.'); return false; } else { return true; } } return false; } return true; }
/** * Widget entity interface. * Data should be validated first before passing it here * @param string $method post|get * @param string $action the controller action * @param array $data validated; assoc array * @param Zbase\Widgets\Widget $widget */ public function widgetController($method, $action, $data, \Zbase\Widgets\Widget $widget) { if (preg_match('/-update/', $action)) { $action = 'update'; } if (preg_match('/-create/', $action)) { $action = 'create'; } if (preg_match('/-delete/', $action)) { $action = 'delete'; } if ($action == 'update' && strtolower($method) == 'post' || $action == 'create' && strtolower($method) == 'post') { $this->nodeAttributes($data); } zbase_db_transaction_start(); try { $parent = !empty($data['category']) ? $data['category'] : null; $parentNodes = []; if (!empty($parent)) { $currentParent = $this->ancestors()->first(); if (is_array($parent)) { foreach ($parent as $p) { if ($parent instanceof Interfaces\EntityInterface) { $parentCategoryNode = $p; } else { $parentCategoryNode = $this->repository()->byAlphaId($p); } if ($parentCategoryNode instanceof Interfaces\EntityInterface) { if ($currentParent->id() != $parentCategoryNode->id()) { $parentNodes[] = $parentCategoryNode; } } else { zbase_alert('error', _zt('There was a problem performing your request.')); return false; } } } } if (empty($parentNodes)) { $parentNodes[] = self::root(); } if ($action == 'create' && strtolower($method) == 'post') { $this->save(); $this->_setParentNodes($parentNodes); $this->uploadNodeFile(); $this->log($action); zbase_db_transaction_commit(); zbase_cache_flush([$this->getTable()]); zbase_alert('success', _zt('Created "%title%"!', ['%title%' => $this->title, '%id%' => $this->id()])); return true; } if ($action == 'update' && strtolower($method) == 'post') { $this->save(); $this->_setParentNodes($parentNodes); $this->uploadNodeFile(); $this->log($action); zbase_db_transaction_commit(); zbase_cache_flush([$this->getTable()]); zbase_alert('success', _zt('Saved "%title%"!', ['%title%' => $this->title, '%id%' => $this->id()])); return true; } if ($action == 'delete' && strtolower($method) == 'post') { $this->delete(); $this->log($action); zbase_db_transaction_commit(); zbase_cache_flush([$this->getTable()]); $undoText = ''; if (!empty($this->hasSoftDelete())) { $undoText = '<a href="' . $widget->getModule()->url(zbase_section(), ['action' => 'restore', 'id' => $this->id()]) . '" title="Undo Delete" class="undodelete">Undo</a>.'; $undoText .= ' | <a href="' . $widget->getModule()->url(zbase_section(), ['action' => 'ddelete', 'id' => $this->id()]) . '" title="Delete Forever " class="ddeleteforever">Delete Forever</a>'; } zbase_alert('success', _zt('Deleted "%title%"! %undo%', ['%title%' => $this->title, '%id%' => $this->id(), '%undo%' => $undoText])); return true; } } catch (\Zbase\Exceptions\RuntimeException $e) { zbase_db_transaction_rollback(); } if ($action == 'index') { return; } if ($action == 'update') { if ($this->hasSoftDelete() && $this->trashed()) { $undoText = '<a href="' . $widget->getModule()->url(zbase_section(), ['action' => 'restore', 'id' => $this->id()]) . '" title="Restore" class="undodelete">Restore</a>'; $undoText .= ' | <a href="' . $widget->getModule()->url(zbase_section(), ['action' => 'ddelete', 'id' => $this->id()]) . '" title="Delete Forever " class="ddeleteforever">Delete Forever</a>'; zbase_alert('warning', _zt('Row "%title%" was trashed! %undo%', ['%title%' => $this->title, '%id%' => $this->id(), '%undo%' => $undoText])); return false; } } if ($action == 'delete') { if ($this->hasSoftDelete() && $this->trashed()) { $undoText = '<a href="' . $widget->getModule()->url(zbase_section(), ['action' => 'restore', 'id' => $this->id()]) . '" title="Restore" class="undodelete">Restore</a>'; $undoText .= ' | <a href="' . $widget->getModule()->url(zbase_section(), ['action' => 'ddelete', 'id' => $this->id()]) . '" title="Delete Forever " class="ddeleteforever">Delete Forever</a>'; zbase_alert('warning', _zt('Row "%title%" was trashed! %undo%', ['%title%' => $this->title, '%id%' => $this->id(), '%undo%' => $undoText])); return false; } } try { if ($action == 'move') { } if ($action == 'restore') { if ($this->trashed()) { $this->restore(); $this->log($action); zbase_db_transaction_commit(); zbase_cache_flush([$this->getTable()]); $this->_actionMessages[$action]['success'][] = _zt('Row "%title%" was restored!', ['%title%' => $this->title, '%id%' => $this->id()]); return true; } zbase_alert('error', _zt('Error restoring "%title%". Row was not trashed.!', ['%title%' => $this->title, '%id%' => $this->id()])); return false; } if ($action == 'ddelete') { if ($this->trashed()) { $this->forceDelete(); $this->log($action); zbase_db_transaction_commit(); zbase_cache_flush([$this->getTable()]); zbase_alert('success', _zt('Row "%title%" was removed from database!', ['%title%' => $this->title, '%id%' => $this->id()])); return true; } zbase_alert('error', _zt('Error restoring "%title%". Row was not trashed.!', ['%title%' => $this->title, '%id%' => $this->id()])); return false; } } catch (\Zbase\Exceptions\RuntimeException $e) { zbase_alert('error', _zt('There was a problem performing the request for "%title%".', ['%title%' => $this->title, '%id%' => $this->id()])); zbase_db_transaction_rollback(); } return false; }
/** * Validate widget */ public function validateWidget($action) { if ($this->_urlHasRequest) { if (empty($this->_entity)) { return zbase_abort(404); } if ($this->isAdmin() && $this->_entity instanceof \Zbase\Entity\Laravel\Node\Nested) { $children = $this->_entity->getImmediateDescendants(); if ($children->count()) { return zbase_abort(404); } } } $this->setAction($action); $this->prepare(); if (zbase_request_method() == 'post') { $currentTab = zbase_request_input('tab', false); if (!empty($currentTab)) { zbase_session_flash('sessiontab', $currentTab); } if ($this->isDeleting()) { return; } $validationRules = $this->getValidationRules(); if (!empty($validationRules)) { $v = \Validator::make(zbase_request_inputs(), $validationRules, $this->getValidationMessages()); if ($v->fails()) { zbase_session_flash('posted', true); $this->setHasError($v->errors()->getMessages()); $messageBag = $v->getMessageBag(); zbase_alert(\Zbase\Zbase::ALERT_ERROR, $messageBag, ['formvalidation' => true]); return $v; } $inputs = zbase_request_inputs(); foreach ($inputs as $k => $v) { $e = $this->element($k); if ($e instanceof \Zbase\Ui\Form\ElementInterface) { $e->setValue($v); } } } } }
/** * Redirect with message * @param string $to * @param string $message * @TODO Add message * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse */ function zbase_redirect_with_message($to, $message) { zbase_alert('error', $message); return zbase_redirect($to); }
/** * Verify email address * @param string $code * @return boolean */ public function verifyEmailAddress($code) { try { $verificationCode = $this->getDataOption('email_verification_code', null); if (!is_null($code) && $code == $verificationCode) { $oldEmails = $this->getDataOption('email_old'); if (is_array($oldEmails)) { $i = 0; foreach ($oldEmails as $e) { if ($e['new'] == $this->email()) { $e['verify'] = zbase_date_now(); $e['verify_ip'] = zbase_ip(); $oldEmails[$i] = $e; } $i++; } } if (!empty($oldEmails)) { $this->setDataOption('email_old', $oldEmails); } $this->unsetDataOption('email_verification_code'); $this->email_verified = 1; $this->email_verified_at = zbase_date_now(); $this->log('user::verifyEmailAddress'); $this->save(); zbase_alert('info', _zt('Your email address <strong>%email%<strong> is now verified.', ['%email%' => $this->email()])); zbase_session_flash('user_verifyEmailAddress', true); return true; } } catch (\Zbase\Exceptions\RuntimeException $e) { zbase_exception_throw($e); } return false; }
/** * Login a User * @param string|aray $username * @param string $password * * @return array */ public static function login($username, $password = '') { $ret = ['success' => false]; if (is_array($username) && !empty($username['username']) && !empty($username['password'])) { $password = $username['password']; $username = $username['username']; $entity = zbase()->entity('user', [], true); $user = $entity->repo()->by('email', $username)->first(); if (!empty($user)) { $same = zbase_bcrypt_check($password, $user->password); if (!empty($same)) { \Auth::login($user); $ret['success'] = true; return $ret; } } } zbase_alert(\Zbase\Zbase::ALERT_ERROR, 'Login error.'); return $ret; }
/** * Reset the given user's password. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function postReset(Request $request) { $messages = ['email.exists' => 'Invalid token given.']; $this->validate($request, ['token' => 'required', 'email' => 'required|email|exists:user_tokens,email,token,' . zbase_request_input('token', '_'), 'password' => 'required|confirmed|min:6|same:password_confirmation'], $messages); $credentials = $request->only('email', 'password', 'password_confirmation', 'token'); $response = \Password::reset($credentials, function ($user, $password) { $user->updatePassword($password); $this->resetPassword($user, $password); }); switch ($response) { case \Password::PASSWORD_RESET: if ($this->loginAfterReset()) { zbase_alert(\Zbase\Zbase::ALERT_SUCCESS, 'You successfully updated your password.'); } else { zbase_alert(\Zbase\Zbase::ALERT_SUCCESS, 'You successfully updated your password. You can login now.'); } zbase()->json()->setVariable('_redirect', $this->redirectPath()); zbase()->json()->setVariable('password_reset_success', 1); return redirect($this->redirectPath())->with('status', trans($response)); case 'passwords.token': zbase_alert(\Zbase\Zbase::ALERT_ERROR, 'Token doesn\'t match, expired or not found. Kindly check again.'); default: return redirect()->back()->withInput($request->only('email'))->withErrors(['email' => trans($response)]); } }
/** * Update email Address Request * Process the link that was sent into the email when * a user wanted to update his email address to a new email address * * routename: update-email-request, expect: email and token * @return */ public function emailUpdateRequestVerify() { $email = $this->getRouteParameter('email', false); $token = $this->getRouteParameter('token', false); if (!empty($email) && !empty($token)) { $user = zbase_user_by('email', $email); if ($user instanceof \Zbase\Entity\Laravel\User\User) { if (zbase_auth_has()) { $updated = $user->checkEmailRequestUpdate($token); if (!empty($updated)) { zbase_session_set('update-email-address', true); return redirect()->to(zbase_url_from_route('home')); } else { zbase_alert('error', 'There was an error updating your email address. Kindly try again.'); } } else { zbase_session_set('__loginRedirect', zbase_url_from_current()); return redirect()->to(zbase_url_from_route('login')); } } } return $this->notfound(); }
/** * Disable Telegram Notifications * * @return */ public function disableUserTelegram(User $user) { $user->unsetDataOption('telegram_chat_id'); $user->save(); $user->clearEntityCacheById(); zbase_alert('success', 'Telegram notifications disabled.'); return true; }
/** * Add Message * * @param string $type * @param string $msg * @param array $options * @return void */ public function message($type, $msg, $options = []) { zbase_alert($type, $msg, $options); }
/** * prepare rows */ protected function _rows() { if (is_null($this->_rows)) { $root = $this->_entity->getRoot(); if (!empty($root)) { $this->_rows = $root->getImmediateDescendants(); } else { zbase_alert('warning', 'No Root or No Categories found.'); } } return $this->_rows; }