Jump to content

File edit help... again


darkeye

Recommended Posts

I am looking to update one of my php scripts with another. For Convenience, the one that needs to be updated is script A, and the one doing the updating is script B.

 

Script A is for a IM bot, based off the API found on imified.com. Basically, the entire file consists of if/else if statements which check for a certain message before responding.

 

Example:

else if('hey' == $_REQUEST['msg']) {
echo "Hi."; 
}

 

This works fine. Recently on these forums I asked how to update script A with another script, so I made this:

<?php
$haystack=$_REQUEST['msg'];
$needle='<reply>';
$pos1=stripos($haystack, $needle);
if($pos1!==false){
$pos2=$pos1+7;
$condition=substr($haystack, 0, $pos1);
$reply=substr($haystack, $pos2);
$file='var.php';
$filedata=file_get_contents('./var.php', FILE_USE_INCLUDE_PATH);
$len=strlen($filedata);
$len=$len-3;
$arr1=str_split($filedata, $len);
$arr1[0]=$arr1[0]."\n";
file_put_contents($file, $arr1[0]);
//Here is the injection into the php file.
$writedata='else if(\''.$condition.'\'==$_POST[\'msg\']){ echo \''.$reply.'\'; }';
$writedata=$writedata."\n";
file_put_contents($file, $writedata, FILE_APPEND);
file_put_contents($file, $arr1[1], FILE_APPEND);
echo 'File has been successfully been written to.';
}

 

This also works fine, and is tripped whenever <reply> is in ['msg']. (I know my code isn't most efficient... I'm new to this.)

 

Now here is the thing. What if I add a reply to the bot a reply that is already in the bot, or just a little bit different, but with the same condition. For example, if I added another 'Hi' reply.

It is useless to stick this at the bottom of the document, since it will never activate.

Instead, I would like the PHP file to search $filedata (see above)to see if a reply with the same condition is already in there. (Stripos?) Then, if it is in there, add a random generator (if there isn't already) to choose between the responses, or something like that.

 

Here is example of a randomly generated response in script A:

else if('make me a sandwich' == $_REQUEST['msg']) {
$number=rand(1,2);
if($number==1){
echo '/me makes you a ham and cheese sandwich.';
}
else if($number==2){
echo '/me makes you a peanut butter and jam sandwich.';
}
}

 

In this example the Script B would just add an additional response to the bottom. [else if($number==3){ ...]

 

But if adding to the first section I posted, it would need to add a generator, and add the response to the list to choose between them.

 

My Question is: How would I go about creating a script that can do this?

 

Sorry If this was confusing, and thanks for you time! :)

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/184031-file-edit-help-again/
Share on other sites

That seems overly complicated for what you are trying to accomplish. What you should do is set up a database with message terms to look for and responses to those answers. The responses could be set up with a many-to-many relationship to the message terms so that you can simple query the answers with a term ID for a random answer that matches. You might even be able to do the matching and retrieving of the response in one query.

Link to comment
https://forums.phpfreaks.com/topic/184031-file-edit-help-again/#findComment-971607
Share on other sites

Yes, I would suggest a MySQL database to do this.

 

Is it possible to edit the PHP code in this way... even though it is complicated?

I'm not sure what you mean by this. The PHP code should be a lot simpler after setting up the database.

 

Depending on how complex the strings are that you are trying to match, it could as simple as a query like this:

"SELECT r.response FROM responses r, terms t WHERE t.msg LIKE '%$term%' AND r.termID = t.termID ORDER BY RAND() LIMIT 1"

 

That query would return a single random response that relates to the term that was found.

Link to comment
https://forums.phpfreaks.com/topic/184031-file-edit-help-again/#findComment-971653
Share on other sites

The strings are relatively simple, e.g. "bot do this."

I just have little to no experience using MySQL, which is why I was hesitating.

 

I have created a database called responses (columns: condition, reply), and have imported some various values into it, but I'm not really sure how to use what you posted.[Total noob here. :(]

 

If I gathered $_POST['msg'] from the user as a string to match, how would I use that query?

I can't add identical queries into the table, so would I just have to alter the condition name slightly in order for the &term search to trigger others? [e.g. hi1 instead of hi, so that both will be found and randomed by that query?]

 

Sorry for any inconvenience, and thanks again!

 

 

Link to comment
https://forums.phpfreaks.com/topic/184031-file-edit-help-again/#findComment-971725
Share on other sites

You probably want to set up the database something like this:

 

conditions

ConditionID  Condition

 

responses

ConditionID  Response

 

This way you can have multiple responses for one condition. For strings that you want to match exactly, your PHP could be as simple as this:

<?php

mysql_query("SELECT r.Response FROM responses r, conditions c WHERE r.ConditionID = c.ConditionID AND c.Condition = '".$_POST['msg']."' ORDER BY RAND() LIMIT 1) or die(mysql_error());

?>

 

If you want to match strings inside of the 'msg' you can use LIKE instead of =.

For more complex patterns, you can use regex to parse the string first, and then do the query based on the results.

Link to comment
https://forums.phpfreaks.com/topic/184031-file-edit-help-again/#findComment-973775
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.