Why am I getting an "unexpected regex" error with my code?

The goal is to set the current working directory to /clients/

and see if the user is in

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

for example the target 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 "\". The filenames are not expected to contain these characters.

Also, the bit isAdmin()

should give admins access to all directories.

right now, PHP says:

Warning: unexpected regex error (8) in C: \ Apache \ HTDOCS \ clients \ Miki \ index.php on line 69

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

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;
}

      

0


source to share


8 answers


There is no need to use regular expressions here, you are looking for a string on another string. So it would be something like this:



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

      

+6


source


Option without complex regex:

  • split the string by "/"

    or"\"

  • find the resulting array for "clients"

  • if found, check if element at next position is equal to username


In PHP (untested though):

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;
}

      

+2


source


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 need to double your escape. It's ugly, I know.

+1


source


Does your regex also account for backslashes? This is probably not necessary. Then you can split into:

'%/clients/%'

      

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

0


source


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;
   }
}

      

0


source


This fixed my PHP warning:

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 that spliti () doesn't break my cwd () return ... The cwd () return I am testing with is:

C: \ Apache \ HTDOCS \ Clients \ Miki

I want to use either "/" or "\", which forces me to use a regex (to my knowledge.)

Ruben's solution is less elegant (it looks like it limits the directory depth allowed), but might work if I can't figure it out.

I'm going to keep fighting back and see if I can figure out what's going on (seems to be a problem in my spliti () regex).

input / feedback is always welcome, even if it's not a complete solution to the problem (or even a related one). Let me know if my code sucks.)

thank! --Will (OP)

0


source


OKAY, it's decided. I needed to replace split () with preg_split () ...

0


source


Id check what DIRECTORY_SEPARATOR

is currently in use and then do a simple check strpos

:

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

      

0


source







All Articles