diff --git a/controllers/admin/AdminSmartsuppAjax.php b/controllers/admin/AdminSmartsuppAjax.php old mode 100755 new mode 100644 index fd37632..bf9a8a7 --- a/controllers/admin/AdminSmartsuppAjax.php +++ b/controllers/admin/AdminSmartsuppAjax.php @@ -2,94 +2,144 @@ /** * Smartsupp Live Chat integration module. * + * @package Smartsupp * @author Smartsupp + * @link http://www.smartsupp.com * @copyright 2016 Smartsupp.com * @license GPL-2.0+ - * @package Smartsupp - * @link http://www.smartsupp.com * * Plugin Name: Smartsupp Live Chat * Plugin URI: http://www.smartsupp.com * Description: Adds Smartsupp Live Chat code to PrestaShop. * Version: 2.2.0 - * Text Domain: smartsupp * Author: Smartsupp * Author URI: http://www.smartsupp.com + * Text Domain: smartsupp * License: GPL-2.0+ * License URI: http://www.gnu.org/licenses/gpl-2.0.txt */ -use \Smartsupp\Auth\Api; +use Smartsupp\Auth\Api; +use Smartsupp\LiveChat\Validator\UserCredentialsValidator; class AdminSmartsuppAjaxController extends ModuleAdminController { const FILE_NAME = 'AdminSmartsuppAjaxController'; - public $ssl = true; + private $partnerKey = 'h4w6t8hln9'; - private $response = []; + private $api; + + const LOGIN_ACTION = 'login'; + const CREATE_ACTION = 'create'; + const DEACTIVATE_ACTION = 'deactivate'; public function init() { - $api = new Api(); - - switch (Tools::getValue('action')) { - case 'login': - $this->response = $api->login([ - 'email' => Tools::getValue('email'), - 'password' => Tools::getValue('password'), - 'platform' => 'Prestashop ' . _PS_VERSION_, - ]); - $this->updateCredentials(); - break; - case 'create': - $this->response = $api->create([ - 'email' => Tools::getValue('email'), - 'password' => Tools::getValue('password'), - 'partnerKey' => $this->partnerKey, - 'consentTerms' => 1, - 'platform' => 'Prestashop ' . _PS_VERSION_, - ]); - $this->updateCredentials(); - break; - case 'deactivate': - Configuration::updateValue('SMARTSUPP_KEY', ''); - Configuration::updateValue('SMARTSUPP_EMAIL', ''); - break; + $validator = new UserCredentialsValidator($this->module); + + $validator->validate(Tools::getAllValues()); + $action = Tools::getValue('action'); + + if ($validator->getError() && $action !== self::DEACTIVATE_ACTION) { + $this->handleError($validator->getMessage(), $validator->getError()); } - header('Content-Type: application/json'); + try { + $this->api = new Api(); - $responseData = [ - 'key' => Configuration::get('SMARTSUPP_KEY'), - 'email' => Configuration::get('SMARTSUPP_EMAIL'), - 'error' => isset($this->response['error']) ? $this->response['error'] : null, - 'message' => isset($this->response['message']) ? $this->response['message'] : null, - ]; + switch ($action) { + case self::LOGIN_ACTION: + $this->handleLoginAction(); + break; + case self::CREATE_ACTION: + $this->handleCreateAction(); + break; + case self::DEACTIVATE_ACTION: + $this->handleDeactivateAction(); + break; + default: + throw new Exception('Invalid action'); + } + } catch (Exception $e) { + $this->handleError($e->getMessage()); + } - $responseData = array_filter($responseData, function ($val) { - return $val !== null; - }); + $this->sendResponse(); + } - die(json_encode($responseData)); + private function handleLoginAction() + { + $this->response = $this->api->login([ + 'email' => Tools::getValue('email'), + 'password' => Tools::getValue('password'), + 'platform' => 'Prestashop ' . _PS_VERSION_, + ]); + + $this->updateCredentials(); } + private function handleCreateAction() + { + $this->response = $this->api->create([ + 'email' => Tools::getValue('email'), + 'password' => Tools::getValue('password'), + 'partnerKey' => $this->partnerKey, + 'consentTerms' => 1, + 'platform' => 'Prestashop ' . _PS_VERSION_, + ]); + + $this->updateCredentials(); + } + + private function handleDeactivateAction() + { + Configuration::updateValue('SMARTSUPP_KEY', ''); + Configuration::updateValue('SMARTSUPP_EMAIL', ''); + + $this->sendResponse(); + } - /** - * @return void - */ private function updateCredentials() { if (isset($this->response['account']['key'])) { Configuration::updateValue('SMARTSUPP_KEY', $this->response['account']['key']); Configuration::updateValue('SMARTSUPP_EMAIL', Tools::getValue('email')); - return; } - $this->response['error'] = isset($this->response['error']) ? $this->response['error'] : $this->module->l('Unknown Error Occurred', self::FILE_NAME); - $this->response['message'] = isset($this->response['message']) ? $this->response['message'] : $this->module->l('Unknown Error Occurred', self::FILE_NAME); + if (isset($this->response['error'])) { + $this->sendResponse(); + } - Configuration::updateValue('SMARTSUPP_KEY', ''); - Configuration::updateValue('SMARTSUPP_EMAIL', ''); + $this->handleError($this->module->l('Unknown error occurred while processing your request.', self::FILE_NAME)); + } + + private function handleError($message, $error = 'error') + { + $this->response['key'] = Configuration::get('SMARTSUPP_KEY'); + $this->response['email'] = Configuration::get('SMARTSUPP_EMAIL'); + + $this->response['error'] = $error; + $this->response['message'] = $message; + + die(json_encode($this->response)); + } + + private function sendResponse() + { + header('Content-Type: application/json'); + + $responseData = [ + 'key' => Configuration::get('SMARTSUPP_KEY'), + 'email' => Configuration::get('SMARTSUPP_EMAIL'), + 'error' => isset($this->response['error']) ? $this->response['error'] : null, + 'message' => isset($this->response['message']) ? $this->response['message'] : null, + ]; + + $responseData = array_filter($responseData, function ($val) { + return $val !== null; + }); + + die(json_encode($responseData)); } -} +} \ No newline at end of file diff --git a/smartsupp.php b/smartsupp.php index db4b092..985ff47 100755 --- a/smartsupp.php +++ b/smartsupp.php @@ -43,10 +43,7 @@ public function __construct() $this->displayName = $this->l('Smartsupp Live Chat & AI Chatbots'); $this->description = $this->l('Smartsupp is your personal online shopping assistant, built to increase conversion rates and sales via visitor engagement in real-time, at the right time.'); - $confirm = $this->l('Are you sure you want to uninstall Smartsupp Live Chat? '); - $confirm .= $this->l('You will lose all the data related to this module.'); - - $this->confirmUninstall = $this->l($confirm); + $this->confirmUninstall = $this->l('Are you sure you want to uninstall Smartsupp Live Chat? You will lose all the data related to this module.'); if (version_compare(_PS_VERSION_, '1.5', '<')) { include _PS_MODULE_DIR_ . $this->name . '/backward_compatibility/backward.php'; diff --git a/src/Validator/UserCredentialsValidator.php b/src/Validator/UserCredentialsValidator.php new file mode 100644 index 0000000..d7e5e71 --- /dev/null +++ b/src/Validator/UserCredentialsValidator.php @@ -0,0 +1,112 @@ + + * @link http://www.smartsupp.com + * @copyright 2016 Smartsupp.com + * @license GPL-2.0+ + * + * Plugin Name: Smartsupp Live Chat + * Plugin URI: http://www.smartsupp.com + * Description: Adds Smartsupp Live Chat code to PrestaShop. + * Version: 2.2.0 + * Author: Smartsupp + * Author URI: http://www.smartsupp.com + * Text Domain: smartsupp + * License: GPL-2.0+ + * License URI: http://www.gnu.org/licenses/gpl-2.0.txt + */ + +namespace Smartsupp\LiveChat\Validator; + +class UserCredentialsValidator +{ + const FILE_NAME = 'UserCredentialsValidator'; + + /** + * @var \Smartsupp $module + */ + private $module; + + /** + * @var string + */ + private $error = ''; + + /** + * @var string + */ + private $message = ''; + + public function __construct($module) + { + $this->module = $module; + } + + /** + * @param array $data + * + * @return void + */ + public function validate($data) + { + $email = isset($data['email']) ? $data['email'] : ''; + $password = isset($data['password']) ? $data['password'] : ''; + + if (empty($email) || empty($password)) { + $this->error = $this->module->l('Empty values provided', self::FILE_NAME); + $this->message = $this->module->l('Email and password fields can not be empty', self::FILE_NAME); + + return; + } + + if (!$this->validateEmail($email)) { + $this->error = $this->module->l('Invalid email format', self::FILE_NAME); + $this->message = $this->module->l('Invalid email address', self::FILE_NAME); + + return; + } + + // Validate password + if (!$this->validatePassword($password)) { + $this->error = $this->module->l('Password length is invalid', self::FILE_NAME); + $this->message = $this->module->l('Password must be between 6-255 characters long', self::FILE_NAME); + } + } + + /** + * @return string + */ + public function getError() + { + return $this->error; + } + + /** + * @return string + */ + public function getMessage() + { + return $this->message; + } + + /** + * @param string $email + * @return array|false + */ + private function validateEmail($email) + { + return filter_var($email, FILTER_VALIDATE_EMAIL); + } + + /** + * @param string $password + * @return bool + */ + private function validatePassword($password) + { + return strlen($password) >= 6 && strlen($password) <= 255; + } +}