0

I am working with an old Wordpress theme and when I run it I get this warning, which breaks my website:

Warning: preg_match(): Compilation failed: invalid range in character class at offset 55 in /var/www/wp-content/themes/photocrati-theme/pope/lib/class.component_registry.php on line 792

This is the code in question:

$module_content = file_get_contents($module_path);
                $match = null;
                
                print_r($module_content);
                if (preg_match('/\/(?:\*)+\s*\{\s*(?P<type>Module|Product):\s*(?P<id>[\w-_]+)\s*(?:,\s*Depends:\s*\{(?P<depends>.*)\})?\s*(,\s*Before:\s*\{(?P<before>.*)\})?\s*\}/m', $module_content, $match) > 0)
                {
                    $module_type = $match['type'];
                    $module_id = $match['id'];
                    $module_deps = isset($match['depends']) ? $match['depends'] : null;
                    $module_before = isset($match['before']) ? $match['before'] : null;
                    $module_info = array('type' => strtolower($module_type), 'id' => $module_id, 'path' => $module_path);

                    if ($module_deps != null)
                    {
                        $module_deps = array_map('trim', explode(',', $module_deps));
                        $module_info['dependency-list'] = $module_deps;
                    }

                    if ($module_before != null)
                    {
                        $module_before = array_map('trim', explode(',', $module_before));
                        $module_info['before-list'] = $module_before;
                    }

                    $scan[$module_id] = $module_info;
                }
                //else die("{$module_path} is not a valid Pope module");

And this is what $module_content is:

custom_js); } } /** * Provides a means of proxying the request of loading fonts from Google. We use Sidjs to load * the stylesheet. Sidjs checks whether the cssRules have been loaded. This check fails in Firefox * as due to the cross-domain security policies that Firefox enforces. To get around this, we * proxy the request */ function load_google_fonts() { if (isset($_GET['load_google_fonts'])) { header('Content-Type: text/css'); $url = 'http://fonts.googleapis.com/css?family='; if (isset($_SERVER['HTTPS'])) $url = str_replace('http://', 'https://', $url); $url .= str_replace(' ' , '+', $_GET['family']); die(wp_remote_fopen($url)); } } function update_cart_product_options() { $num_items = 0; require_once(get_template_directory().'/photocrati-gallery/shopping-cart.php'); if (isset($_POST['action']) && $_POST['action'] == 'update_cart_product_options' && isset($_POST['data'])) { if (wp_verify_nonce($_POST['nonce'],'update_cart_product_options')) { parse_str($_POST['data'], $_POST); $cart = Photocrati_Shopping_Cart::get_instance(); if (isset($_POST['product_id'])) { $product_id = intval($_POST['product_id']); if (isset($_POST['options'])) { foreach ($_POST['options'] as $option_id => $arr) { $option_id = intval($option_id); $quantity = 0.0; if (in_array('quantity', array_keys($arr))) { $quantity = floatval($arr['quantity']); } $num_items = $cart->add_item($product_id, $option_id, $quantity); } $cart->write_session(); } } } die(json_encode(array('number_of_cart_items' => $num_items))); } } function empty_cart() { if (isset($_REQUEST['empty_cart'])) { $cart = Photocrati_Shopping_Cart::get_instance(); $cart->destroy(); } } } new M_Photocrati_ThemeBulk();

What is wrong with my preg_match, I did try one thing and was changing [\w-_] to [\w_-] but that did not fix it at all.

UPDATE

Changed it to this:

if (preg_match('/\/(?:\*)+\s*\{\s*(?P<type>Module|Product):\s*(?P<id>[\w]+)\s*(?:,\s*Depends:\s*\{(?P<depends>.*)\})?\s*(,\s*Before:\s*\{(?P<before>.*)\})?\s*\}/', $module_content, $match) > 0)

Warning went away after changing [\w-_] to this [\w] but its still not matching, goes to this line after uncommenting it:

else die("{$module_path} is not a valid Pope module");
user979331
  • 11,039
  • 73
  • 223
  • 418
  • If the problem is with `preg_match()` then why am I looking at more than 20 lines of code from your script? You see that hyphen in the middle of the character class? `[\w-_]` That's a no-no now. Underscores are already included in `\w` -- just remove the `_`. That `m` pattern modifier serves no purpose because you don't have any `^` or `$` anchors in your pattern. – mickmackusa Nov 23 '22 at 03:32
  • `[\w]` would be the same as `\w` which is the same as `[A-Za-z0-9_]`. I think you want `[\w-]` to match alphanumerics, underscores, and hyphens https://3v4l.org/Ypngs – mickmackusa Nov 23 '22 at 03:58

0 Answers0