Searching a log file using PHP from input pattern - php

My problem is to create a search function that can search within a log file.
The pattern is something like this:
node-id > command
-Date-other descriptions
Output starts
Output ends
node-id > another command
Now, the problem I have is to cut the output of one command from one log file and compare it with the output from another log file for comparison. I am not able to find a way to search the content and store it into an array or file for further comparison.

You can't cut stuff from a file, you can only change the file and re-save it (you could potentially cut stuff, depending on your file system, directly manipulating the same, but asking this question here means you don't have such low-level file system experience)
In php it would be something like:
$data = file_get_contents($fileToCutFrom);
// ... modify data here, e.g. if it's xml you might wanna use SimpleXML or DOMDocument or some other xml parser, like ganon ( https://code.google.com/p/ganon/ ) - the later one is pretty good if you know you might have incomplete/invalid xml
file_put_contents($fileToCutFrom, $modifiedData);
// and now append to the other file
file_put_contents($fileToAppendTo, $dataToAppend, FILE_APPEND);
Know however that you might get into serious race conditions if you do this from more than one file, as file_get_contents is not atomic, and neither is file_put_contents, and then you also have the problem of having multiple lines to execute. So if your code might get executed several times at once, you might wanna use a single script instance which does all the "cutting" and appending opening up a socket and the other scripts just send what they need to that file.
Also, if you wanna continuesly do that whenver new data comes in, you might wanna have a look at inotify (linux) or the equivalent on your system (osx also has a file notification service, but I don't know about windows)
If you need something different, you might wanna add information to your question.

Related

Safely allowing user provided content in PHP file

Consider the following script:
file_put_contents('/var/www/html/myfile.php', $header.$_POST['users_html'].$footer);
$header and $footer are safe, however, $_POST['users_html'] is suspect.
The intent is $_POST['users_html'] is HTML, but obviously someone could maliciously post something else. The content will not be stored in a DB or used in a SMS, and /var/www/html/myfile.php will be public and only opened by Apache. While I didn't show it and am not asking about this part, after I know $_POST['users_html'] is safe, I will be replacing certain tags such as {{1}} to <?php echo(getSomething(1));?> using regex.
Assume I am not concerned with JavaScript threats, and my only concern is someone running PHP on the server which I did not intend.
Other than ensuring that $_POST['users_html'] doesn't contain any <? tags, what should be done?
If it's only going to be pure HTML, then treat it as such. DO NOT put in into a PHP file - it will end up being run like a little Bobby PHP script. Save to a separate file (outside the web-root, so it cannot be accessed directly from the website).
Never include/require it, always echo file_get_contents() or fpassthru() the file and BEFORE you save it, run the code through a Whitelist HTML filter - such as the htmlpurifier library, and then put it to disk or database.
So, probably not a great idea, but at least this way, you'll have a chance.

How to Parse Methods and Functions to Log the Code

Is there an easy way to parse the Functions and their code from a PHP file so you can log the contents of each function separately in a database?
I would like to log each separate function and the comments for the function as an individual database entry.
I started writing my own script from scratch to parse the PHP file, but it seemed like this really ought to be something someone else has already done before. So before reinventing the wheel I thought I'd ask if any of you know of another way to do it?
I found get_defined_functions() which provided the function names currently loaded. But I'm looking for the function's arguments, contents and its comments as well.
You're looking for token_get_all(), which takes a PHP source file and parses it into its components (keywords, comments, whitespace, strings, etc.).
If what you trying to do is analyze performance, such as logging slow calls, you will need a debugger like xDebug.

page has two php files with identical names how to wget the correct php file? adding identifier?

I have this problem when I'm trying to use wget to retrieve the OUTPUT of a specific php script, but it looks like this site generates 2 identical PHP files.
The 1st one is smaller and the 2nd one, in the sequence, is the correct one. The problem is every time I try the wget command, I end-up with the smallest output file, which does not contain the desired info :(
Is there a way to download the correct file, using wget, by adding some sort of identifier to the link, to make sure I'm downloading the correct file.
Here is the command I've been trying:
$ wget http://www.fernsehen.to/index.php
If your run/play this and use Fidller or Wireshark for capture, you'll end-up with two (2) "http://www.fernsehen.to/index.php" and I need the bigger file of the two.
P.S. To manually get the desired output file, you can open http://www.fernsehen.to/index.php in Firefox or chrome and view source.
Thank you in advance!
What you want is not really practically possible. When you visit that page, they first generate a small file with a load of Javascript, that detects browser features and sends them back to the server in a stateful manner in order to produce the exact code required for your browser, probably including stuff like supported codecs for video mainly. Probably they also do some session fingerprinting for DRM purposes, to stop people like you from exactly what you're trying to do.
wget cannot emulate this behaviour because it is not a full browser, and cannot execute all that Javascript, nor if it did properly supply browser-like data. You'd have to write an extensive piece of custom code that exactly mimics everything the in-between page is doing to achieve the intended effect. Possible, but not easy, and most certainly not with a basic generic-purpose tool like wget.

Stop PHP parsing but output the rest of the file

I'm developing a web application where an html page is created for the user. The page could include anything that the user puts in it. We take these pages and add a little PHP at the top to check some things before outputting the actual html. It would look kind of like this:
<?php
require 'checksomestuff.php';
// User's html below
?>
<html>
<!-- user's html -->
</html>
Is there a way to stop PHP from parsing anything after my require? I need the html to be outputted, but, since the user can add anything they want to the html, I don't want any user-added PHP to be executed. Obviously that would be a security issue. So, I want the user's html to be outputted, but not parse any PHP. I would rather not have to put the user's html into another file.
One sensible way would be to offload the user created content to another file and then you should load this file (in your main php file) and output it as is - without parsing it as PHP.
There are many other ways to do this but if creating another file does the job for you then thats probably the best way forward.
UPDATE: Really must read last line of question!
You could encode the html into a variable using base64 encoding which you then just print out the decoded string.
If you don't store the file data in a php file, say n a txt or html file, the php won't be evaluated.
Alternatively you could read the file via file_get_contents() or by some other means which doesn't involve evaluating php.
Though I'm still tempted to ask why you want to do this (particularly this way), it sounds to me like one of the only things that can help you is the special __halt_compiler() function...
That should prevent it from executing the rest of the page, and may or may not output the rest of it. If not, well, read the first (and currently only) example in the PHP's manual for that function (linked above) for how to do it manually.
The only trouble I see with this method is that you'd probably have to have that code in every file you want to do this for, after your require.

Editing files in PHP

Is there a way to append or remove a line of text from a file using PHP.
I am in the process of writing a hosting control panel for my specific web hosting stack and would like to be able to make changes to the files with minimal requirements to touch the file system, and as such would like not to have to rewrite the whole file to add or remove a configuration option.
There is no way to remove a line from a file without first parsing the file into lines and then writing it out again.
You can append to a file by using fopen with the $mode set to 'a'
$fp = fopen('myfile', 'a');
For appending, you should use fopen with the $mode of a.
See this please, on how to delete a line from the file.
Yes, you can open files in append mode:
$fh = fopen('testFile.txt', 'a');
If you now write to the file, the new content gets appended.
See fopen and from this documentation:
'a': Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
Removing the last line is not possible though.
Working with files in PHP is full of pitfalls, particularly wrt concurrency and locking. The fact that you are writing config files implies also that you are exposing tasks normally only available to a user with root privileges over the web. You did not mention these things in your post, but your question is trivial in comparison to addressing these issues.
Regarding your question - although its fairly trivial to implement what you propose (e.g. by exec'ing sed - although you did not did say what OS this is running on) I'd recommend creating a copy of the original file in some other representation - obvious candidates would be a database - where you can apply sequence numbers to the lines and easily create a gap to populate, or a PHP SplDoublyLinkedList stored in the session. Then once the user has effected as many changes as they require, regenerate the file in a single operation from the working representation.
Note that, ultimately, regardless of how you implement the solution the solution will rewrite the entire file - its just a question of how much of this process is exposed within your code.
Bear in mind, that what you are doing is just the same as most php web scripts - except while they manipulate and reqrite HTML, you're doing it with a different file type - so you might want to look at how other PHP templating systems are written, and consider whether you can create a template for your config files.
HTH
C.
If you need to change something in the middle of the file, you have to read it, parse it and save it back. Othwerwise you can only append something to the end of it.
You should not be concerned about the cost of this operation though, as it's a configuration file that won't likely be changed a hundred times every second, it should take negligible time.
If you want more flexibility in access/update/delete I think you should consider moving your configuration file to a database table.

Categories