3

I was looking to find a way to use "include" statement using a fifo (named pipe) file created with posix_mkfifo. As my expectations were high I got very depressed to see the page blocked, waiting for an action that I was sure it won't ever happen.

So... my logic dictated that as "include" surely use some kind of "fopen"-family function, and fifos can be opened this way, my only thing to do was: to create one, then write in it, maybe keep the resource alive until it was included, include the file, close the handler, delete the file. But of course it was nothing like that. I suppose that include opens the file in a blocking manner and this behavior is the cause of my problem. Having worked with fifos in c, I noticed that in a non-blocking open method the information in fifo is preserved until a reader decides to read it. I was hoping on the same behavior.

Although I tried to open another page to write to the same fifo, so that the reader can continue, that was bound to fail also. Furthermore, not only the page did not respond anymore, but it failed to answer to the user abort also, fact due to I had my sessions blocked for several minutes, as session_start() would tell me that there cannot be used by two processes at once ( I might be wrong here about the error message, I can remember well, and I have deleted the test files. )

My question is if there is any way to achieve this effect: of including using a fifo.

In the end, to answer all the questions about why the hell am I doing this I will say that my system puts itself between the real code and it's processing with the purpose of modifying it on need in a dynamic manner, completely reversible. And for that it needs to read the original file, check the code, change it when it seems to and then write it in a file, in a new "root" folder, keep the folder and files hierarchy and then "include" the file. At the moment, I replaced the use of fifos with the one of real files, but I need to filter a lot of code. And eval, although overused in my functions, is not a solution as it do not keep the current directory and the script name, or anything specific to an actual file, besides that it is very sensible with many things, especially with relative paths, and weird uses of php syntax. So I am still stuck with writing in files.

Because of that the system is a terrible time consumer and a bad resource manager, my original idea was to write in fifos that are more light, kept in memory, volatile, and really made for the only purpose of IPC.

With respect, paul

khael
  • 2,600
  • 1
  • 15
  • 36
  • This seems to be a part of the solution: http://stackoverflow.com/questions/580013/how-do-i-perform-a-non-blocking-fopen-on-a-named-pipe-mkfifo (also check out the related posts on the right) – Quasdunk Sep 03 '11 at 09:51
  • Maybe you could write a stream wrapper and then include the files via that wrapper? Do the changes to the code in the code of the wrapper then. See: http://www.php.net/manual/en/function.stream-wrapper-register.php and http://www.php.net/manual/en/class.streamwrapper.php – hakre Sep 03 '11 at 09:58
  • @Quasdunk's solution does not work as I do not have control over the way include statement handles the fopen. – khael Sep 03 '11 at 12:43

1 Answers1

0

As suggested in a comment, I'll elaborate it a bit: Write your own stream wrapper and then you can register it on top of a protocol:

stream_wrapper_register("myfilter", "WrapCode");

include('myfilter://path-to-include');

# or

include('myfilter://file://path-to-include');

That done, inside your WrapCode stream wrapper class, you can open a stream to the original resource and then process it on the file transparently. This would be on-the-fly, so you don't need to care about storing your files somewhere (but you still can do so).

The PHP Manual offers an example with a simple stream wrapper class which shows how it works.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • I was just about to answer half an hour ago but begginer in posting comments as I am, although I found in this site a great source of knowledge, I probably missed the maximum characters and my post was not registered. Anyway I wanted to thank you for the elegant solution you presented, even if I thought of it before I was to scared of wrappers to even tried it. But after playing a bit with the exemple and actually understand it I found myself mesmerized by the posibilities ahead. I will probably return with a full sample of my code to be inspiration for others to look for a similar solution. – khael Sep 03 '11 at 11:11
  • I wanted to thank you again for you answer. After testing it offline, on my own server, now I am fighting the stupid rules of my hosting service provider having a feeling that the tech guys over there doesn't even know what wrappers are. I got odd behavior on some functions, some of them doesn't even want to use the wrapper, others do, and when they do they doesn't let me use the fread function inside the wrapper, the function returns nothing, etc, etc, I am prety mad about this. Once again I must bow my head in the face of impossibility to overcome the stupidity of some people. – khael Sep 03 '11 at 12:34
  • Although I still wonder if there is any method to include named-pipes! It might be my only solution, unfortunately. Event if this by far the most elegant solution, maybe the best approach. – khael Sep 03 '11 at 12:42