Possible to write an Apache file handler in Php? - php

I wonder if and how it is possible to write a custom "file handler" (parsing a file and rendering it with bonuses) for Apache 2 in PHP? Files are text files, and they could be large, so I'm not thinking of loading them entirely in memory, but processing them line by line.
I'm comfortable with Java and other languages but still rookie in PHP; I chose PHP because it's light and especially deployable on every Apache-capable machine (even small NAS), and, well, I like PHP.
Thank you for your hints.

Its not possible to write a file handler in php.
However you could use the rewrite engine to redirect those files that you want to handle to a php script that, then, does the job.
the original file can be obtained from the server variables.

Related

How Zend_Log prevent the race condition while writing a log message?

I have looked into a little bit of the Zend_Log, which is a log module of Zend Framework, but I didn't see it using a flock function to prevent a race condition if there are multiple php script writing to the same file.
As per what I know, the web application based on the zend framework treat each request as a individual php process, so the state can't be shared between those process, so keep a write file action in sync is necessary.
Does anyone know the reason why?
Let me answer my own question, after check some documents, in UNIX like system a O_APPEND opened file is thread and process safe to write to the file. But Windows is an exception, maybe php did some wrapper for a windows interpreter.
If a open file is PIPE or FIFO, then it's different from a pure file. If the size bytes exceed the size of PIPE_BUF, then it's not atomic.
Understanding concurrent file writes from multiple processes
O_APPEND document

Converting inc files to php files

I am working on a project that is in asp. My client wants me to convert ASP to ASP.NET.
The project has got many .inc files which is placed inside an include folder and these inc files are called
<!-- #include file="../includes/example.inc" -->
in many pages. Most of these inc files have either variables or constants declared that are used in many pages or functions(written in VBScript). Now my client had asked me to try converting these inc files to php files(Don't have any clue about php as I have never worked on it before).
My question here is
How effective would it be converting these inc files to php.?
How can I call the functions in the php page from aspx page?
If not converting to php what would be the better method?
Thanks in advance!
This will require the re-writing of all of these flies as PHP has no VBScript support. Therefore, the 'effectiveness' is at best an objective statement being that it is contingent on the competency and skill sets of the individual performing the migration.
You cannot call functions in a php page from an aspx page (inheritly). You can (attempt) to do this, but I would highly recommend not doing this.
My only recommendation would be to start from scratch.
Abort!
Don't go gown this rabbit hole! I agree with everything that #ohgodwhy said, and I'll add some details.
First, a little background. ASP files [usually] have a .ASP file extension and are always written in VbScript. PHP files have a .PHP extension and are always written in PHP. .NET files have a .ASPX extension are usually written in C# or VB.NET. Notice that there are no overlaps between technologies or file extensions. I'll talk about INC files in a second.
When IIS interprets the code, it picks the correct processor based on the file extension and then interprets the code/script, does whatever it's programmed to do, and generates an HTML output for the browser to render. In this case, the first pass of the ASP processor is to gather up all of the include files and process them in the same language as the parent file. I'm going to assume that your INC files are actually ASP files (regardless of their file extension), so they get included and then processed as ASP files. If they contain PHP code, you're going to get an error right off the bat.
If you try to combine technologies in one project, the server has to use multiple interpreters/engines to get this all to process and get to that final HTML. IF (and this is a big IF), but IF you can get them all to work together, you still have the problem of passing data back and forth between the technologies because they're being run in different parts of the server. You can't have the ASP processor include a PHP file and have it magically work.
It is possible to have PHP, ASP, and/or .NET coexist in the same project, but not the way you want it to. I've done it a few times and the project (and server) have to be configured very carefully and then passing the data back and forth becomes the challenge because each of the technologies is working in its own memory space and is not accessible to the other. Again, it CAN be done, but it's going to be a MAJOR challenge and it's going to involve some creative programming - probably far more than what your client wants to do.
Bottom line: go tell the client that converting to PHP is a waste of time and money. You have ASP files and INC (which are really ASP) files. Why involve another technology? Your options are to rewrite the whole thing in ASP, rewrite the whole thing in PHP, or rewrite the whole thing in .NET, but don't mix and match.
(A note for some ubertechnical downvoters: I simplified this a tad to make it clearer for this example!)

Treat local files as remote / process files by webserver instead of reading them directly

I'm writing a really simple app in PHP. And I've got a PHP script creating dynamically an XML file. That XML file needs to be accessed both directly by the user's browser, and by another PHP script.
If it were a complex app, I'd have some kind of framework to do this kind of dual access for me.
But as the app is really simple, I don't want to use any framework or MVC architecture.
What I'd preferably want to do is to be able to call file_get_contents('script.php') and let the local file script.php be processed by the webserver instead of reading it locally.
I see several ways to do it, but I don't like any of them:
calling something like file_get_contents($_SERVER['HTTP_HOST']. ... . '/script.php') - this seems to be overly complex and error-prone
getting the file with curl - not all hostings have curl enabled
doing it using ob_start() and the friend functions - that's ugly
extracting the XML-generating code to a string-returning function - as the XML file grows large, the lack of streamed processing could be a bottle-neck
Do you have any other/nicer ideas?

Best practices to let my web users download custom .exe from my site using PHP

im trying to implement on my site a system who let the user download a file that have to be change before the download.
I have a master file (a .exe program), that inside have a variable who has to be change for every different user.
The most simple solution is to change a variable inside a xml file every time the user want his personalized exe and then make the exe file to read the external file. BUT i dont want the user to download more than one file.
Is this possible? using php can i change a parametter inside a compiled program? Thanks for any help and suggestions!
If you really really know what you're doing and you know exactly the bits that need to be flipped inside the file, then yes, it's possible to modify the .exe file with PHP. Otherwise you have to make changes to the source or other files the .exe is built with and compile the program on the server before sending it to the user.
In theory it's certainly possible (PHP is turing complete), but as stated in other answers it will be hardly worth the hassle (considering the fact that you have to ask whether it is possible shows you'd have to investigate at last for days into the standard exe-format).
I'd recommend one of the following:
1) Zip the program with the configuration file; either use a separate launcher (e.g. Java [a JAR is a ZIP file]) or add a configuration file that is read by the program itself. There is a number of PHP libraries for generating ZIP files.
2) compile the program with the changed source on the server itself; however this can also become quite complicated depending on your server configuration and the programming environment you use. If you have never administered a virtual server I would not even slightly recommend that as an option.
3) If you can assume that the user got somewhat stable Internet access you might also consider to let hir download a standard executable, where additional configuration will be downloaded later on by the program itself (e.g. by transmitting the username to the server). However this creates dependencies you might want to avoid (your user probably can't use it on machines without Internet access and you should assert that your server is up most of the time).
While it's probably possible, I doubt it's worth the hassle. Unless you're trying to fight piracy or something. Why don't you just serve the user a custom .zip file with the .exe and a config .xml?
[edit after OP commented]
I presume what you're trying to edit is the facebook ID/username? Well, try to compile the base file with some unique string like "THISNEEDSTOBEREPLACED", then use some binary safe function to replace it. Though remember things can and will get tricky if the string lengths don't match.

C++ Serve PHP documents?

I am writing a small web server, nothing fancy, I basically just want to be able to show some files. I would like to use PHP though, and im wondering if just putting the php code inside of the html will be fine, or if I need to actually use some type of PHP library?
http://www.adp-gmbh.ch/win/misc/webserver.html
I just downloaded that and I am going to use that to work off of. Basically I am writing a serverside game plugin that will allow game server owners to access a web control panel for their server. Some features would be possible with PHP so this is my goal. Any help would be appreciated, thanks!
The PHP won't serve itself. What happens in a web server like Apache is before the PHP is served to the user it is passed through a PHP parser. That PHP parser reads, understands and executes anything between (or even ) tags depending on configuration. The resultant output, usually still HTML, is served by the web server.
There are a number of ways to achieve this. Modules to process PHP have been written by Apache but you do not have to use these. PHP.exe on windows, installed from windows.php.net, will do this for you. Given a PHP file as an argument it will parse the PHP and spit the result back out on the standard output.
So, one option for you is to start PHP.exe from within your web server with a re-directed standard output to your program, and serve the result.
How to create a child process with re-directed IO: http://msdn.microsoft.com/en-us/library/ms682499%28VS.85%29.aspx however, you won't be writing the child process, that'll be PHP.exe
Caveat: I am not sure from a security / in production use perspective if this is the most secure approach, but it would work.
PHP needs to be processed by the PHP runtime. I'm assuming the case you're talking about is that you have a C++ server answering HTTP queries, and you want to write PHP code out with the HTML when you respond to clients.
I'm not aware of any general-purpose PHP library. The most straightforward solution is probably to use PHP as a CGI program.
Here's a link that might be useful for that: http://osdir.com/ml/php-general/2009-06/msg00473.html
This method is nice because you don't need to write the HTML+PHP out to a file first; you can stream it to PHP.
You need execute the PHP page to serve the page it generates.
The easiest thing for you to do would be to add CGI support to your webserver in some basic form. This is non-trivial, but not too difficult. Basically you need to pass PHP an environment and input, and retrieve the output.
Once you have CGI support you can just use any executable, including PHP, to generate webpages.

Categories