Skip to content

Commit 0dac2a7

Browse files
committed
Fix compatibility errors with ps 9
1 parent 307e65f commit 0dac2a7

File tree

5 files changed

+103
-75
lines changed

5 files changed

+103
-75
lines changed

phar/prestashopConsole.phar

-103 Bytes
Binary file not shown.

src/PrestashopConsole/Command/Admin/User/CreateCommand.php

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
use Configuration;
2424
use Employee;
25+
use PrestaShop\PrestaShop\Core\Crypto\Hashing;
2526
use PrestashopConsole\Command\PrestashopConsoleAbstractCmd as Command;
2627
use RuntimeException;
2728
use Symfony\Component\Console\Input\InputInterface;
@@ -60,7 +61,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6061
$email = $helper->ask($input, $output, $this->getEmailQuestion());
6162
}
6263

63-
if (!Validate::isPasswdAdmin($password)) {
64+
if (!$this->isValidPassword($password)) {
6465
$password = $helper->ask($input, $output, $this->getPasswordQuestion());
6566
}
6667

@@ -83,22 +84,40 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8384
$employee = new Employee();
8485
$employee->active = 1;
8586
$employee->email = $email;
86-
$employee->passwd = Tools::encrypt($password);
87+
88+
// Use PS9 Hashing class if available, fallback to Tools::encrypt for older versions
89+
if (class_exists('PrestaShop\PrestaShop\Core\Crypto\Hashing')) {
90+
$crypto = new Hashing();
91+
$employee->passwd = $crypto->hash($password);
92+
} elseif (method_exists('Tools', 'encrypt')) {
93+
$employee->passwd = Tools::encrypt($password);
94+
} else {
95+
$employee->passwd = password_hash($password, PASSWORD_DEFAULT);
96+
}
97+
8798
$employee->firstname = $firstname;
8899
$employee->lastname = $lastname;
89100
$employee->id_lang = Configuration::get('PS_LANG_DEFAULT');
90101
$employee->id_profile = _PS_ADMIN_PROFILE_;
91102
$employee->default_tab = 1;
92103
$employee->bo_theme = 'default';
93-
$employee->save();
104+
105+
if (!$employee->save()) {
106+
$output->writeln('<error>Failed to create admin user</error>');
107+
108+
return self::RESPONSE_ERROR;
109+
}
110+
111+
$output->writeln('<info>Admin user created successfully!</info>');
112+
$output->writeln(sprintf('<info>Email: %s</info>', $employee->email));
113+
$output->writeln(sprintf('<info>ID: %s</info>', $employee->id));
114+
$output->writeln(sprintf('<info>Name: %s %s</info>', $employee->firstname, $employee->lastname));
94115
} catch (\Exception $e) {
95116
$output->writeln('<error>' . $e->getMessage() . '</error>');
96117

97118
return self::RESPONSE_ERROR;
98119
}
99120

100-
$output->writeln('<info>New user ' . $email . ' created</info>');
101-
102121
return self::RESPONSE_SUCCESS;
103122
}
104123

@@ -131,7 +150,7 @@ protected function getPasswordQuestion(): Question
131150
$question = new Question('admin password :', 'admin123456');
132151
$question->setHidden(true);
133152
$question->setValidator(function ($answer) {
134-
if (!Validate::isPasswdAdmin($answer)) {
153+
if (!$this->isValidPassword($answer)) {
135154
throw new RuntimeException('Your password is not valid');
136155
}
137156

@@ -141,6 +160,29 @@ protected function getPasswordQuestion(): Question
141160
return $question;
142161
}
143162

163+
/**
164+
* Validate password (compatible with PS9)
165+
*
166+
* @param string $password
167+
*
168+
* @return bool
169+
*/
170+
protected function isValidPassword($password): bool
171+
{
172+
// Try PS9 method first
173+
if (method_exists('Validate', 'isAcceptablePasswordLength')) {
174+
return Validate::isAcceptablePasswordLength($password);
175+
}
176+
177+
// Fallback to old method for older PS versions
178+
if (method_exists('Validate', 'isPasswdAdmin')) {
179+
return Validate::isPasswdAdmin($password);
180+
}
181+
182+
// Basic validation if neither exists
183+
return strlen($password) >= 8;
184+
}
185+
144186
/**
145187
* Get Customer Firstname or lastname question
146188
*

src/PrestashopConsole/Command/Cache/ClearAllCommand.php

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/PrestashopConsole/Command/Customer/CreateCommand.php

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,20 @@ public function execute(InputInterface $input, OutputInterface $output): int
6464
if (null === $email || !Validate::isEmail($email)) {
6565
$email = $questionHelper->ask($input, $output, $this->_getEmailQuestion());
6666
}
67-
if (null === $password || empty($password) || !Validate::isPlaintextPassword($password)) {
67+
68+
// Ask for password if not provided or invalid
69+
if (null === $password || empty($password) || !$this->isValidPassword($password)) {
6870
$password = $questionHelper->ask($input, $output, $this->_getPasswordQuestion());
69-
if (version_compare(_PS_VERSION_, '1.7', '<')) {
70-
$password = Tools::encrypt($password);
71-
} else {
72-
$hashing = new \PrestaShop\PrestaShop\Core\Crypto\Hashing();
73-
$password = $hashing->hash($password);
74-
}
7571
}
72+
73+
// Hash password for storage
74+
if (version_compare(_PS_VERSION_, '1.7', '<')) {
75+
$hashedPassword = Tools::encrypt($password);
76+
} else {
77+
$hashing = new \PrestaShop\PrestaShop\Core\Crypto\Hashing();
78+
$hashedPassword = $hashing->hash($password);
79+
}
80+
7681
if (null === $firstname || !$this->validateCustomerName($firstname)) {
7782
$firstname = $questionHelper->ask($input, $output, $this->_getFirstnameQuestion());
7883
}
@@ -87,19 +92,27 @@ public function execute(InputInterface $input, OutputInterface $output): int
8792
try {
8893
$customer = new Customer();
8994
$customer->email = $email;
90-
$customer->passwd = $password;
95+
$customer->passwd = $hashedPassword;
9196
$customer->firstname = $firstname;
9297
$customer->lastname = $lastname;
9398
$customer->id_shop = $id_shop;
94-
$customer->save();
99+
100+
if (!$customer->save()) {
101+
$output->writeln('<error>Failed to create customer</error>');
102+
103+
return self::RESPONSE_ERROR;
104+
}
105+
106+
$output->writeln('<info>Customer created successfully!</info>');
107+
$output->writeln(sprintf('<info>Email: %s</info>', $customer->email));
108+
$output->writeln(sprintf('<info>ID: %s</info>', $customer->id));
109+
$output->writeln(sprintf('<info>Name: %s %s</info>', $customer->firstname, $customer->lastname));
95110
} catch (PrestaShopException $e) {
96-
$output->writeln('<error>Unable to create customer');
111+
$output->writeln('<error>Unable to create customer: ' . $e->getMessage() . '</error>');
97112

98113
return self::RESPONSE_ERROR;
99114
}
100115

101-
$output->writeln('<info>new customer ' . $email . ' created with success</info>');
102-
103116
return self::RESPONSE_SUCCESS;
104117
}
105118

@@ -132,7 +145,7 @@ protected function _getPasswordQuestion(): Question
132145
$question = new Question('<question>customer password :</question>');
133146
$question->setHidden(true);
134147
$question->setValidator(function ($answer) {
135-
if (null === $answer || !Validate::isPlaintextPassword($answer)) {
148+
if (null === $answer || !$this->isValidPassword($answer)) {
136149
throw new RuntimeException('Invalid password');
137150
}
138151

@@ -142,6 +155,29 @@ protected function _getPasswordQuestion(): Question
142155
return $question;
143156
}
144157

158+
/**
159+
* Validate password (compatible with PS9)
160+
*
161+
* @param string $password
162+
*
163+
* @return bool
164+
*/
165+
protected function isValidPassword($password): bool
166+
{
167+
// Try PS9 method first
168+
if (method_exists('Validate', 'isAcceptablePasswordLength')) {
169+
return Validate::isAcceptablePasswordLength($password);
170+
}
171+
172+
// Fallback to old method for older PS versions
173+
if (method_exists('Validate', 'isPlaintextPassword')) {
174+
return Validate::isPlaintextPassword($password);
175+
}
176+
177+
// Basic validation if neither exists
178+
return strlen($password) >= 8;
179+
}
180+
145181
/**
146182
* Firstname question
147183
*

src/PrestashopConsole/Command/Images/GenerateAbstract.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ protected function regenerateThumbnails($type = 'all', $deleteOldImages = true,
121121
{
122122
$languages = Language::getLanguages(false);
123123
$process = [
124-
['type' => 'categories', 'dir' => _PS_CAT_IMG_DIR_],
125-
['type' => 'manufacturers', 'dir' => _PS_MANU_IMG_DIR_],
126-
['type' => 'suppliers', 'dir' => _PS_SUPP_IMG_DIR_],
127-
['type' => 'products', 'dir' => _PS_PROD_IMG_DIR_],
128-
['type' => 'stores', 'dir' => _PS_STORE_IMG_DIR_],
124+
['type' => 'categories', 'dir' => constant('_PS_CAT_IMG_DIR_')],
125+
['type' => 'manufacturers', 'dir' => constant('_PS_MANU_IMG_DIR_')],
126+
['type' => 'suppliers', 'dir' => constant('_PS_SUPP_IMG_DIR_')],
127+
['type' => 'products', 'dir' => defined('_PS_PROD_IMG_DIR_') ? constant('_PS_PROD_IMG_DIR_') : constant('_PS_PRODUCT_IMG_DIR_')],
128+
['type' => 'stores', 'dir' => constant('_PS_STORE_IMG_DIR_')],
129129
];
130130

131131
// Launching generation process
@@ -351,7 +351,7 @@ protected function regenerateNoPictureImages($dir, $type, $languages)
351351
foreach ($languages as $language) {
352352
$file = $dir . $language['iso_code'] . '.jpg';
353353
if (!file_exists($file)) {
354-
$file = _PS_PROD_IMG_DIR_ . Language::getIsoById((int) Configuration::get('PS_LANG_DEFAULT')) . '.jpg';
354+
$file = (defined('_PS_PROD_IMG_DIR_') ? constant('_PS_PROD_IMG_DIR_') : constant('_PS_PRODUCT_IMG_DIR_')) . Language::getIsoById((int) Configuration::get('PS_LANG_DEFAULT')) . '.jpg';
355355
}
356356
if (!file_exists($dir . $language['iso_code'] . '-default-' . stripslashes($image_type['name']) . '.jpg')) {
357357
if (!ImageManager::resize($file, $dir . $language['iso_code'] . '-default-' . stripslashes($image_type['name']) . '.jpg', (int) $image_type['width'], (int) $image_type['height'])) {

0 commit comments

Comments
 (0)