为什么我的代码出现“意外的正则表达式”错误?

时间:2022-08-22 23:18:35

The goal is to take the current working directory split it at /clients/ and see if the user is in

目标是将当前工作目录拆分为/ clients /并查看用户是否在

[something]/clients/[their username]/[something]

for example, the goal would be for input:

例如,目标是输入:

cwd = "/volumes/raid0/www/clients/mikey/test_folder/"
$session->username = "mikey"

to return with

回来

$authorized = true

I would like this to recognize both UNIX and Windows paths, so it should look for "/" or "\". It is assumed that filenames won't contain these characters.

我希望这能识别UNIX和Windows路径,因此它应该寻找“/”或“\”。假设文件名不包含这些字符。

Also, the isAdmin() bit is supposed to give admins access to all directories.

此外,isAdmin()位应该为管理员提供对所有目录的访问权限。

right now, PHP says:

现在,PHP说:

Warning: unexpected regex error (8) in c:\apache\htdocs\clients\mikey\index.php on line 69

警告:第69行的c:\ apache \ htdocs \ clients \ mikey \ index.php中出现意外的正则表达式错误(8)

here's the code as it stands. (line 69 is noted in the comments.)

这是现在的代码。 (第69行在评论中注明。)

if($session->isAdmin())
{
    $authorized = true;
} 
else 
{
  // split cwd at the first instance of /clients/
  $dir = spliti('%(\/|\\)clients(\/|\\)%',getcwd(),2); //this is line 69
  if(count($dir) == 2) // if /clients/ was in cwd
  {
    // check if the second piece of cwd starts with the username.
    $authorized = (preg_match('/^'.$session->username.'//*.$/', $dir[1]));
  } 
  else 
    $authorized = false;
}

8 个解决方案

#1


6  

There is no need to use regular expressions here, you're looking for a string in another string. So that would be something like:

这里不需要使用正则表达式,而是在另一个字符串中查找字符串。所以这将是这样的:

$isAuthorized = strpos(str_replace('\\', '/', getcwd()), "/clients/$username/") !== FALSE;

#2


2  

The no-complicated-regex variant of this would be:

这种无复杂的正则表达式变体是:

  • split the string at "/" or "\"
  • 将字符串拆分为“/”或“\”

  • search the resulting array for "clients"
  • 在结果数组中搜索“clients”

  • if found, check if the element at the next position is equal to the username
  • 如果找到,检查下一个位置的元素是否等于用户名

In PHP (untested, though):

在PHP中(虽然未经测试):

function isAuthorized($session)
{
  $authorized = $session->isAdmin();

  if(!$authorized)
  {
    $parts = split("[/\\\\]", $path);

    // find "clients", compare the following bit to the $session->username
    $clients_pos = array_search("clients", $parts);
    if ($clients_pos && count($parts) >= $clients_pos) 
      $authorized = ($parts[$clients_pos + 1] == $session->username);
    else
      $authorized = false;
  }
  return $authorized;
}

#3


1  

Something like this might get you started:

这样的事情可能会让你开始:

<?php
$regex = '%(.+)(/|\\\\)clients(/|\\\\)(.+)(/|\\\\)(.+)(/|\\\\)%';

preg_match($regex, "/volumes/raid0/www/clients/mikey/test_folder/", &$matches);
var_dump($matches);
?>

Outputs:

 array(8) {
  [0]=>
  string(45) "/volumes/raid0/www/clients/mikey/test_folder/"
  [1]=>
  string(18) "/volumes/raid0/www"
  [2]=>
  string(1) "/"
  [3]=>
  string(1) "/"
  [4]=>
  string(5) "mikey"
  [5]=>
  string(1) "/"
  [6]=>
  string(11) "test_folder"
  [7]=>
  string(1) "/"
}

Should allow you to write this piece of code much shorter. Keep in mind that you have to double escape. It's ugly, I know.

应该允许你写这段代码要短得多。请记住,你必须双重逃脱。我知道,这很难看。

#4


0  

Does your regex try to account for backslashes in the paths too? That's probably not necessary. Then you can split on:

你的正则表达式是否也试图在路径中考虑反斜杠?这可能没有必要。然后你可以拆分:

'%/clients/%'

(You shouldn't need to escape the regular slash since you use % as the regex delimiter.)

(您不需要转义常规斜杠,因为您使用%作为正则表达式分隔符。)

#5


0  

This code has not been tested, standard disclaimers apply:

此代码尚未经过测试,标准免责声明适用:

if($session->isAdmin()) {
   $authorized = true;
} else {
   // split cwd at the first instance of /clients/
   $dir = preg_split('/(\/|\\)clients(\\|\/)/i', getcwd());
   if(count($dir) == 2) { // if /clients/ was in cwd
      // check if the second piece of cwd starts with the username.
      $dir = preg_split('/\/|\\/', $dir[1]);
      $authorized = ($dir[0] == $session->username);
   } else {
      $authorized = false;
   }
}

#6


0  

This fixed my PHP warning:

这修复了我的PHP警告:

if($session->isAdmin())
{
    $authorized = true;
} else {
    // split cwd at the first instance of /clients/
    $dir = spliti('%(/|\\\\)clients(/|\\\\)%',getcwd(),2);
    if(count($dir) == 2) // if /clients/ was in cwd
        {
            // check if the second piece of cwd starts with the username.
            $authorized = (preg_match('%^'.$session->username.'*(/|\\\\)*.$%', $dir[1]));
    } else $authorized = false;
}

The problem is, spliti() is not splitting my cwd() return... The cwd() return I'm testing with is:

问题是,spliti()没有拆分我的cwd()返回...我正在测试的cwd()返回是:

c:\apache\htdocs\clients\mikey

I want to be able to use either "/" or "\" which kind of forces me to use regex (to my knowlege.)

我想能够使用“/”或“\”这种强迫我使用正则表达式(对我的知识。)

Ruben's solution is less elegant (it seems to limit the directory depth allowed) but might work if I can't figure this out.

Ruben的解决方案不太优雅(似乎限制了允许的目录深度),但如果我无法解决这个问题,可能会有效。

I'm going to keep hammering away and see if I can sort out what's happening (it seems to all be a problem in my spliti() regex at this point.)

我将继续努力,看看我是否可以理清正在发生的事情(此时我的spliti()正则表达式似乎都存在问题。)

input/feedback is always appreciated, even if it isn't a full solution to the problem (or even related. Let me know if my code sucks.)

输入/反馈总是受到赞赏,即使它不是问题的完整解决方案(甚至是相关的。如果我的代码很糟糕,请告诉我。)

thanks! --Will (OP)

谢谢! - 会(OP)

#7


0  

OKAY, this is solved. I needed to replace split() with preg_split()...

好的,这个问题已经解决了。我需要用preg_split()替换split()...

#8


0  

I’d check what DIRECTORY_SEPARATOR is currently used and then do a simple strpos check on it:

我将检查当前使用的DIRECTORY_SEPARATOR,然后对其进行简单的strpos检查:

$cwd = getcwd();
if (DIRECTORY_SEPARATOR != '/') {
    $cwd = str_replace(DIRECTORY_SEPARATOR, '/', $cwd);
}
$authorized = (strpos($cwd, '/clients/'.$session->username.'/') !== false);

#1


6  

There is no need to use regular expressions here, you're looking for a string in another string. So that would be something like:

这里不需要使用正则表达式,而是在另一个字符串中查找字符串。所以这将是这样的:

$isAuthorized = strpos(str_replace('\\', '/', getcwd()), "/clients/$username/") !== FALSE;

#2


2  

The no-complicated-regex variant of this would be:

这种无复杂的正则表达式变体是:

  • split the string at "/" or "\"
  • 将字符串拆分为“/”或“\”

  • search the resulting array for "clients"
  • 在结果数组中搜索“clients”

  • if found, check if the element at the next position is equal to the username
  • 如果找到,检查下一个位置的元素是否等于用户名

In PHP (untested, though):

在PHP中(虽然未经测试):

function isAuthorized($session)
{
  $authorized = $session->isAdmin();

  if(!$authorized)
  {
    $parts = split("[/\\\\]", $path);

    // find "clients", compare the following bit to the $session->username
    $clients_pos = array_search("clients", $parts);
    if ($clients_pos && count($parts) >= $clients_pos) 
      $authorized = ($parts[$clients_pos + 1] == $session->username);
    else
      $authorized = false;
  }
  return $authorized;
}

#3


1  

Something like this might get you started:

这样的事情可能会让你开始:

<?php
$regex = '%(.+)(/|\\\\)clients(/|\\\\)(.+)(/|\\\\)(.+)(/|\\\\)%';

preg_match($regex, "/volumes/raid0/www/clients/mikey/test_folder/", &$matches);
var_dump($matches);
?>

Outputs:

 array(8) {
  [0]=>
  string(45) "/volumes/raid0/www/clients/mikey/test_folder/"
  [1]=>
  string(18) "/volumes/raid0/www"
  [2]=>
  string(1) "/"
  [3]=>
  string(1) "/"
  [4]=>
  string(5) "mikey"
  [5]=>
  string(1) "/"
  [6]=>
  string(11) "test_folder"
  [7]=>
  string(1) "/"
}

Should allow you to write this piece of code much shorter. Keep in mind that you have to double escape. It's ugly, I know.

应该允许你写这段代码要短得多。请记住,你必须双重逃脱。我知道,这很难看。

#4


0  

Does your regex try to account for backslashes in the paths too? That's probably not necessary. Then you can split on:

你的正则表达式是否也试图在路径中考虑反斜杠?这可能没有必要。然后你可以拆分:

'%/clients/%'

(You shouldn't need to escape the regular slash since you use % as the regex delimiter.)

(您不需要转义常规斜杠,因为您使用%作为正则表达式分隔符。)

#5


0  

This code has not been tested, standard disclaimers apply:

此代码尚未经过测试,标准免责声明适用:

if($session->isAdmin()) {
   $authorized = true;
} else {
   // split cwd at the first instance of /clients/
   $dir = preg_split('/(\/|\\)clients(\\|\/)/i', getcwd());
   if(count($dir) == 2) { // if /clients/ was in cwd
      // check if the second piece of cwd starts with the username.
      $dir = preg_split('/\/|\\/', $dir[1]);
      $authorized = ($dir[0] == $session->username);
   } else {
      $authorized = false;
   }
}

#6


0  

This fixed my PHP warning:

这修复了我的PHP警告:

if($session->isAdmin())
{
    $authorized = true;
} else {
    // split cwd at the first instance of /clients/
    $dir = spliti('%(/|\\\\)clients(/|\\\\)%',getcwd(),2);
    if(count($dir) == 2) // if /clients/ was in cwd
        {
            // check if the second piece of cwd starts with the username.
            $authorized = (preg_match('%^'.$session->username.'*(/|\\\\)*.$%', $dir[1]));
    } else $authorized = false;
}

The problem is, spliti() is not splitting my cwd() return... The cwd() return I'm testing with is:

问题是,spliti()没有拆分我的cwd()返回...我正在测试的cwd()返回是:

c:\apache\htdocs\clients\mikey

I want to be able to use either "/" or "\" which kind of forces me to use regex (to my knowlege.)

我想能够使用“/”或“\”这种强迫我使用正则表达式(对我的知识。)

Ruben's solution is less elegant (it seems to limit the directory depth allowed) but might work if I can't figure this out.

Ruben的解决方案不太优雅(似乎限制了允许的目录深度),但如果我无法解决这个问题,可能会有效。

I'm going to keep hammering away and see if I can sort out what's happening (it seems to all be a problem in my spliti() regex at this point.)

我将继续努力,看看我是否可以理清正在发生的事情(此时我的spliti()正则表达式似乎都存在问题。)

input/feedback is always appreciated, even if it isn't a full solution to the problem (or even related. Let me know if my code sucks.)

输入/反馈总是受到赞赏,即使它不是问题的完整解决方案(甚至是相关的。如果我的代码很糟糕,请告诉我。)

thanks! --Will (OP)

谢谢! - 会(OP)

#7


0  

OKAY, this is solved. I needed to replace split() with preg_split()...

好的,这个问题已经解决了。我需要用preg_split()替换split()...

#8


0  

I’d check what DIRECTORY_SEPARATOR is currently used and then do a simple strpos check on it:

我将检查当前使用的DIRECTORY_SEPARATOR,然后对其进行简单的strpos检查:

$cwd = getcwd();
if (DIRECTORY_SEPARATOR != '/') {
    $cwd = str_replace(DIRECTORY_SEPARATOR, '/', $cwd);
}
$authorized = (strpos($cwd, '/clients/'.$session->username.'/') !== false);