PHP Classes

plugins directory and regex in RegexIterator

Recommend this page to a friend!

      Flex Lorem  >  All threads  >  plugins directory and regex in...  >  (Un) Subscribe thread alerts  
Subject:plugins directory and regex in...
Summary:bug?
Messages:5
Author:smori
Date:2012-08-16 05:44:12
Update:2012-08-16 18:31:49
 

  1. plugins directory and regex in...   Reply   Report abuse  
Picture of smori smori - 2012-08-16 05:44:13
I downloaded this library and ran test.php, and some errors occurred.

1. plugins directory
$base in Lorem::loadPlugins() should be as below.

2. regex in RegexIterator
By default, regex for RegexIterator will be '/^.+[\.php]$/i', which will match '.' and '..'.


diff --git a/lorem.php b/lorem.php
index d78b0f7..9919c5a 100644
--- a/lorem.php
+++ b/lorem.php
@@ -45 +45 @@ class Lorem {
- if (!key_exists($method, $this->availableMethods)){
+ if (!array_key_exists($method, $this->availableMethods)){
@@ -64 +64 @@ class Lorem {
- $base = strtolower(dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR. str_replace("_", DIRECTORY_SEPARATOR, get_class($this)) . DIRECTORY_SEPARATOR . "plugins");
+ $base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'plugins';
@@ -93 +93 @@ class Lorem {
- $Regex = new RegexIterator($Iterator, sprintf('/^.+[%s]$/i', implode("|", $filter)), RecursiveRegexIterator::GET_MATCH);
+ $Regex = new RegexIterator($Iterator, sprintf('/^.+(?:%s)$/i', implode("|", $filter)), RecursiveRegexIterator::GET_MATCH);
@@ -555 +555 @@ class Lorem {
-}
\ No newline at end of file
+}

  2. Re: plugins directory and regex in...   Reply   Report abuse  
Picture of Tom Schaefer Tom Schaefer - 2012-08-16 09:16:26 - In reply to message 1 from smori
Which setup and environment do you use? I cannot reproduce this error!

  3. Re: plugins directory and regex in...   Reply   Report abuse  
Picture of Tom Schaefer Tom Schaefer - 2012-08-16 10:09:16 - In reply to message 1 from smori

  4. Re: plugins directory and regex in...   Reply   Report abuse  
Picture of smori smori - 2012-08-16 12:45:08 - In reply to message 3 from Tom Schaefer
My environment:
OS - Ubuntu 11.04
Server - Apache/2.2.17 (Ubuntu)
PHP - PHP 5.3.5-1ubuntu7.10 with Suhosin-Patch
* All softwares installed by package manager (synaptics).

I ran files in comment #3, but the result was same.
Nothing has been generated in browser, and
Fatal error just after <body> tag in cli.

<body>
PHP Fatal error: Uncaught exception 'UnexpectedValueException' with message 'RecursiveDirectoryIterator::__construct(/home/smori/desktop/lorem/plugins): failed to open dir: No such file or directory' in /home/smori/Desktop/lorem/lorem.php:91

I set up files in /home/smori/Desktop/lorem, and plugins directory should be
/home/smori/Desktop/lorem/plugins.

In Lorem::loadPlugins(), dirname(dirname(__FILE__)) returns /home/smori/Desktop.
As a result, plugin directory will be /home/smori/Desktop/lorem/plugins.
Finally in this case, $base will be /home/smori/desktop/lorem/plugins.

When I ran test.php first time, I downloaded files and set up files in /home/smori/php/flex-lorem.
In this case, generated path /home/smori/php/lorem/plugins will not be found.

So I think it's better to write:
$base = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'plugins';
This doesn't depend on parent directory name and class name.

Now I set up files in /home/smori/php/lorem, and fatal error above doesn't occur.
But now, Warning occurs.

PHP Warning: include_once(/home/smori/php/lorem/plugins/..php): failed to open stream: No such file or directory in /home/smori/php/lorem/lorem.php on line 69

In Lorem::getPlugins(), print_r($res) returns
Array
(
[0] => image.php
[1] => .
[2] => ..
[3] => text.php
)

'.' and '..' are unnecessary.

Anyway, your regex '/^.+[\.php]$/i' (result of sprintf in line93) matches
'a.', 'ap', 'ah' etc.
I think regex should be '/.+(?:\.php)$/i' and sprintf should be
sprintf('/^.+(?:%s)$/i', implode('|', $filter).

  5. Re: plugins directory and regex in...   Reply   Report abuse  
Picture of Tom Schaefer Tom Schaefer - 2012-08-16 18:31:49 - In reply to message 4 from smori
Thx a lot for your constructive critics. I just updated class flex lorem.
I hope it will work better now. I ran it with following setup sucessfully:
Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8r, PHP Version 5.2.17
and under Windows with php 5.3.8.