Skip to content

Instantly share code, notes, and snippets.

@mysliwietzflorian
Created March 17, 2022 14:48
Show Gist options
  • Select an option

  • Save mysliwietzflorian/676b8de2e7c953593a2bf4e130e1e4af to your computer and use it in GitHub Desktop.

Select an option

Save mysliwietzflorian/676b8de2e7c953593a2bf4e130e1e4af to your computer and use it in GitHub Desktop.
<?php
namespace App\Command\Migration;
use Pimcore\Db;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @deprecated and temporary
* only used once during the PCX update process
* ============================================
*
* Updates the condition data in ecommerceframework_pricing_rule table for the ned App/ namespace (instead of AppBundle/)
*
* useful further information
* - https://stackoverflow.com/a/21544853 // replace-number-with-another-number-of-string-in-php
* - https://stackoverflow.com/questions/1182812/what-is-the-meaning-of-x00-x04-in-php
* - https://stackoverflow.com/questions/12684871/how-to-catch-unserialize-exception
* - https://stackoverflow.com/questions/4264270/unserialize-sometimes-returns-false
*/
class PricingRuleUpdateCommand extends Command
{
protected function configure()
{
parent::configure();
$this->setName("app:pricing-rule:update");
$this->setDescription("");
}
protected function execute(InputInterface $input, OutputInterface $output)
{
/** update this value if you are sure what you do, and you can write the update into the database */
$dryRun = true;
$db = Db::get();
$selectStatement = 'SELECT id, `condition` FROM ecommerceframework_pricing_rule;';
$result = $db->fetchAll($selectStatement);
/** can be used for debugging the unserialize() function below */
// error_reporting(E_ALL);
// function exception_error_handler
// set_error_handler(function($errno, $errstr, $errfile, $errline ) {
// var_dump($errno, $errstr, $errfile, $errline);
// });
foreach ($result as $pricingRule) {
$id = $pricingRule['id'];
$condition = $pricingRule['condition'];
/** modify for own need.
* Here the following logic is applied:
* - change namespace AppBundle to App
* - update the size part of the serialized string (-6 for "Bundle")
*/
$newCondition = preg_replace_callback('/:(\d+):"AppBundle/', function($matches) {
return sprintf(':%d:"App', $matches[1] - 6);
}, $condition);
/** check if unserialization was successful (check with !== false) */
$object = unserialize($newCondition);
if (!$dryRun && $object !== false) {
/** warning - potential sql injection attack target (id) */
$updateStatement = <<<SQL
UPDATE ecommerceframework_pricing_rule SET `condition` = {$db->quote($newCondition)} WHERE id = $id;
SQL;
$db->executeQuery($updateStatement);
}
/** update only the first entry in ecommerceframework_pricing_rule table while debugging - modify for you own needs */
die();
}
return Command::SUCCESS;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment