SRL Archive - Web Development

PHP anyone know whats wrong with this?


sm0k3

2011-05-25 00:20:46

heres a php function from a script it works perfectly (almost)

basicly it does server side checking for a string then carries out an action

case '/join': is looking for the text /join
all of the cases work and get picked up by the script (they were there already.)
exept for /wipe (i added this .. i think its possibly the SQL statement thats the problem)

$db->query($sql); is for sending sql query to predefined sql server
i know this works as it is used in many places in the script

[PHP]
function insertParsedMessage($text) {

// If a queryUserName is set, sent all messages as private messages to this userName:
if($this->getQueryUserName() !== null && strpos($text, '/') !== 0) {
$text = '/msg '.$this->getQueryUserName().' '.$text;
}

// Parse IRC-style commands:
if(strpos($text, '/') === 0) {
$textParts = explode(' ', $text);

switch($textParts[0]) {

// Channel switch:
case '/join':
$this->insertParsedMessageJoin($textParts);
break;

//clearChat
case '/wipe':
$sql = 'TRUNCATE TABLE
`ajax_chat_messages`;
ALTER TABLE
`ajax_chat_messages`
AUTO_INCREMENT = 1';
$result = $db->query($sql);
break;
// Logout:
case '/quit':
$this->logout();
break;

// Private message:
case '/msg':
case '/describe':
$this->insertParsedMessagePrivMsg($textParts);
break;

// Invitation:
case '/invite':
$this->insertParsedMessageInvite($textParts);
break;

// Uninvitation:
case '/uninvite':
$this->insertParsedMessageUninvite($textParts);
break;

// Private messaging:
case '/query':
$this->insertParsedMessageQuery($textParts);
break;

// Kicking offending users from the chat:
case '/kick':
$this->insertParsedMessageKick($textParts);
break;

// Listing banned users:
case '/bans':
$this->insertParsedMessageBans($textParts);
break;

// Unban user (remove from ban list):
case '/unban':
$this->insertParsedMessageUnban($textParts);
break;

// Describing actions:
case '/me':
case '/action':
$this->insertParsedMessageAction($textParts);
break;


// Listing online Users:
case '/who':
$this->insertParsedMessageWho($textParts);
break;

// Listing available channels:
case '/list':
$this->insertParsedMessageList($textParts);
break;

// Retrieving the channel of a User:
case '/whereis':
$this->insertParsedMessageWhereis($textParts);
break;

// Listing information about a User:
case '/whois':
$this->insertParsedMessageWhois($textParts);
break;

// Rolling dice:
case '/roll':
$this->insertParsedMessageRoll($textParts);
break;

// Switching userName:
case '/nick':
$this->insertParsedMessageNick($textParts);
break;

// Custom or unknown command:
default:
if(!$this->parseCustomCommands($text, $textParts)) {
$this->insertChatBotMessage(
$this->getPrivateMessageID(),
'/error UnknownCommand '.$textParts[0]
);
}
}

} else {
// No command found, just insert the plain message:
$this->insertCustomMessage(
$this->getUserID(),
$this->getUserName(),
$this->getUserRole(),
$this->getChannel(),
$text
);
}
}
[/PHP]

superuser

2011-05-25 02:30:28

From: sm0k3
heres a php function from a script it works perfectly (almost)

basicly it does server side checking for a string then carries out an action

case '/join': is looking for the text /join
all of the cases work and get picked up by the script (they were there already.)
exept for /wipe (i added this .. i think its possibly the SQL statement thats the problem)

$db->query($sql); is for sending sql query to predefined sql server
i know this works as it is used in many places in the script

[PHP]
function insertParsedMessage($text) {

// If a queryUserName is set, sent all messages as private messages to this userName:
if($this->getQueryUserName() !== null && strpos($text, '/') !== 0) {
$text = '/msg '.$this->getQueryUserName().' '.$text;
}

// Parse IRC-style commands:
if(strpos($text, '/') === 0) {
$textParts = explode(' ', $text);

switch($textParts[0]) {

// Channel switch:
case '/join':
$this->insertParsedMessageJoin($textParts);
break;

//clearChat
case '/wipe':
$sql = 'TRUNCATE TABLE
`ajax_chat_messages`;
ALTER TABLE
`ajax_chat_messages`
AUTO_INCREMENT = 1';
$result = $db->query($sql);
break;
// Logout:
case '/quit':
$this->logout();
break;

// Private message:
case '/msg':
case '/describe':
$this->insertParsedMessagePrivMsg($textParts);
break;

// Invitation:
case '/invite':
$this->insertParsedMessageInvite($textParts);
break;

// Uninvitation:
case '/uninvite':
$this->insertParsedMessageUninvite($textParts);
break;

// Private messaging:
case '/query':
$this->insertParsedMessageQuery($textParts);
break;

// Kicking offending users from the chat:
case '/kick':
$this->insertParsedMessageKick($textParts);
break;

// Listing banned users:
case '/bans':
$this->insertParsedMessageBans($textParts);
break;

// Unban user (remove from ban list):
case '/unban':
$this->insertParsedMessageUnban($textParts);
break;

// Describing actions:
case '/me':
case '/action':
$this->insertParsedMessageAction($textParts);
break;


// Listing online Users:
case '/who':
$this->insertParsedMessageWho($textParts);
break;

// Listing available channels:
case '/list':
$this->insertParsedMessageList($textParts);
break;

// Retrieving the channel of a User:
case '/whereis':
$this->insertParsedMessageWhereis($textParts);
break;

// Listing information about a User:
case '/whois':
$this->insertParsedMessageWhois($textParts);
break;

// Rolling dice:
case '/roll':
$this->insertParsedMessageRoll($textParts);
break;

// Switching userName:
case '/nick':
$this->insertParsedMessageNick($textParts);
break;

// Custom or unknown command:
default:
if(!$this->parseCustomCommands($text, $textParts)) {
$this->insertChatBotMessage(
$this->getPrivateMessageID(),
'/error UnknownCommand '.$textParts[0]
);
}
}

} else {
// No command found, just insert the plain message:
$this->insertCustomMessage(
$this->getUserID(),
$this->getUserName(),
$this->getUserRole(),
$this->getChannel(),
$text
);
}
}
[/PHP]


Perhaps something like this:

                case '/wipe':
$sql = 'TRUNCATE TABLE
ajax_chat_messages;
ALTER TABLE
ajax_chat_messages
AUTO_INCREMENT = 1';

Guess you were using autogenerated SQL statement from PHPMyAdmin…

Sex

2011-05-25 02:31:56

The SQL statement does not look like a problem. It looks fine. However have you tried putting global $db at the top? :)

Edit: If you want, you can just use nameofdbclass::query($sql);

Floor66

2011-05-25 02:51:06

From: superuser
Perhaps something like this:

                case '/wipe':
$sql = 'TRUNCATE TABLE
ajax_chat_messages;
ALTER TABLE
ajax_chat_messages
AUTO_INCREMENT = 1';

Guess you were using autogenerated SQL statement from PHPMyAdmin…


So you just removed the backticks… (`)? Those should be no problem at all.
Just to test, try splitting the query up in two queries, the first truncing the table, then the AUTO_INCREMENT one.

Sex

2011-05-25 02:56:15

From: Floor66
So you just removed the backticks… (`)? Those should be no problem at all.
Just to test, try splitting the query up in two queries, the first truncing the table, then the AUTO_INCREMENT one.

Have you looked at what I posted? That seems to be the problem. That query is perfectly fine, I tested it on my own database.

Floor66

2011-05-25 03:00:45

Oh yeah that could be it. I rarely work with OOP in PHP so I don't know all that much about globals :p

sm0k3

2011-05-25 11:49:53

right ive got the query to work but the actual script says that /wipe could not be found
(the function seems to skip the Case '/wipe': statement)

Floor66

2011-05-25 12:04:53

Try adding a break; to the default statement?

sm0k3

2011-05-25 14:00:56

right does this look right . just the general flow of the case statment.
i dont know php so i havent go a clue.

[php]
//clearChat:
case '/wipe':
$sql1 = 'TRUNCATE TABLE ajax_chat_messages;';
$sql2 = 'ALTER TABLE ajax_chat_messages AUTO_INCREMENT = 1;';
// Create a new SQL query:
$this->db->sqlQuery($sql1);
$this->db->sqlQuery($sql2);
break;
[/php]

all the other statments for example /join work just as expected but it still dosent pick up /wipe
it returns Error: Unknown command: /wipe
(and it dont touch the database)

Floor66

2011-05-25 14:16:21

You don't have to add the semicolons at the end of the queries as it's only one of them at a time.

sm0k3

2011-05-25 14:34:02

cool . i still cant figure out why all the cases work apart from mine though .
its written just like the others.?

Edit: just tryed adding the sql part to a function as most of the case statements start a function .


[php]
function ClearChat(){
// Admin Clears Chat
if($this->getUserRole() == AJAX_CHAT_ADMIN || $this->getUserRole() == AJAX_CHAT_MODERATOR) {
$sql1 = 'TRUNCATE TABLE ajax_chat_messages';
$sql2 = 'ALTER TABLE ajax_chat_messages AUTO_INCREMENT = 1';
// Create a new SQL query:
$this->db->sqlQuery($sql1);
$this->db->sqlQuery($sql2);
}
return;
}

// Return true if a custom command has been successfully parsed, else false
// $text contains the whole message, $textParts the message split up as words array
function parseCustomCommands($text, $textParts) {
switch($text) {
//clearChat:
case '/wipe':
$this->ClearChat();
return true;
break;
default:
return false;
}
}
[/php]

Still dosent think /wipe is a command

Frement

2011-05-25 15:06:30

As I see it, you are having $text as the whole text, you could try using $textParts[0] for the switch statement. As I assume the first part of the $text variable should be used, as it should contain the command? Correct me if I'm wrong.

sm0k3

2011-05-25 15:12:51

yeh problem is it was doing the same with $textParts[0]
it just refuses to accept tat /wipe is one of the cases..
not to mention or even think about the ClearChat(); function
as the case '/wipe' dosent seem to even get looked at to begin with

Sex

2011-05-25 15:21:27

How do you know? Does it run the default case when you issue a /wipe command?

sm0k3

2011-05-25 15:26:24

[php]
function ClearChat(){
// Admin Clears Chat
if($this->getUserRole() == AJAX_CHAT_ADMIN || $this->getUserRole() == AJAX_CHAT_MODERATOR) {
$sql1 = 'TRUNCATE TABLE ajax_chat_messages';
$sql2 = 'ALTER TABLE ajax_chat_messages AUTO_INCREMENT = 1';
// Create a new SQL query:
$this->sqlQuery($sql1);
$this->sqlQuery($sql2);
}
return true;
}

// Return true if a custom command has been successfully parsed, else false
// $text contains the whole message, $textParts the message split up as words array
function parseCustomCommands($text, $textParts) {
switch($textParts[0]) {
//clearChat:
case '/wipe':
$this->ClearChat();
return true;
break;
default:
$this->insertChatBotMessage(
$this->getPrivateMessageID(),
'/error UnknowncustCommand '.$textParts[0];
);
return false;
}
}
[/php]
it returns Error: Unknown command: /wipe
witch is the default case
of the first script i posted . (i addad this to the custom commands file.)
could it be that this int beiing carried out ..?

[php]
// Custom or unknown command:
default:
if(!$this->parseCustomCommands($text, $textParts)) {
$this->insertChatBotMessage(
$this->getPrivateMessageID(),
'/error UnknownCommand '.$textParts[0];
);
}
}
[/php]

Frement

2011-05-25 15:44:05

[PHP]
function ClearChat(){
// Admin Clears Chat
if($this->getUserRole() == AJAX_CHAT_ADMIN || $this->getUserRole() == AJAX_CHAT_MODERATOR) {
$sql1 = 'TRUNCATE TABLE ajax_chat_messages';
$sql2 = 'ALTER TABLE ajax_chat_messages AUTO_INCREMENT = 1';
// Create a new SQL query:
$this->sqlQuery($sql1);
$this->sqlQuery($sql2);
}
return true;
}

// Return true if a custom command has been successfully parsed, else false
// $text contains the whole message, $textParts the message split up as words array
function parseCustomCommands($text, $textParts) {
switch($textParts[0]) {
//clearChat:
case '/wipe':
$this->ClearChat();
return true;
break;
default:
$this->insertChatBotMessage(
$this->getPrivateMessageID(),
'/error UnknowncustCommand '.$textParts[0]
);
return false;
break;
}
}
[/PHP]

EDIT: Why doesn't it allow writing [0]?

EDIT2: I added the break; after the default.

EDIT3: Ran a fast test, works fine for me. Heres a link: http://www.frement.net/srl/sm0k3/test.php

sm0k3

2011-05-25 16:13:37

right
heres the main script
[php]
function insertParsedMessage($text) {

// If a queryUserName is set, sent all messages as private messages to this userName:
if($this->getQueryUserName() !== null && strpos($text, '/') !== 0) {
$text = '/msg '.$this->getQueryUserName().' '.$text;
}

// Parse IRC-style commands:
if(strpos($text, '/') === 0) {
$textParts = explode(' ', $text);

switch($textParts***91;0***93;) {

// Channel switch:
case '/join':
$this->insertParsedMessageJoin($textParts);
break;

//clearChat
case '/wipe':
$sql = 'TRUNCATE TABLE
`ajax_chat_messages`;
ALTER TABLE
`ajax_chat_messages`
AUTO_INCREMENT = 1';
$result = $db->query($sql);
break;
// Logout:
case '/quit':
$this->logout();
break;

// Private message:
case '/msg':
case '/describe':
$this->insertParsedMessagePrivMsg($textParts);
break;

// Invitation:
case '/invite':
$this->insertParsedMessageInvite($textParts);
break;

// Uninvitation:
case '/uninvite':
$this->insertParsedMessageUninvite($textParts);
break;

// Private messaging:
case '/query':
$this->insertParsedMessageQuery($textParts);
break;

// Kicking offending users from the chat:
case '/kick':
$this->insertParsedMessageKick($textParts);
break;

// Listing banned users:
case '/bans':
$this->insertParsedMessageBans($textParts);
break;

// Unban user (remove from ban list):
case '/unban':
$this->insertParsedMessageUnban($textParts);
break;

// Describing actions:
case '/me':
case '/action':
$this->insertParsedMessageAction($textParts);
break;


// Listing online Users:
case '/who':
$this->insertParsedMessageWho($textParts);
break;

// Listing available channels:
case '/list':
$this->insertParsedMessageList($textParts);
break;

// Retrieving the channel of a User:
case '/whereis':
$this->insertParsedMessageWhereis($textParts);
break;

// Listing information about a User:
case '/whois':
$this->insertParsedMessageWhois($textParts);
break;

// Rolling dice:
case '/roll':
$this->insertParsedMessageRoll($textParts);
break;

// Switching userName:
case '/nick':
$this->insertParsedMessageNick($textParts);
break;

// Custom or unknown command:
default:
if(!$this->parseCustomCommands($text, $textParts)) {
$this->insertChatBotMessage(
$this->getPrivateMessageID(),
'/error UnknownCommand '.$textParts***91;0***93;
);
}
}

} else {
// No command found, just insert the plain message:
$this->insertCustomMessage(
$this->getUserID(),
$this->getUserName(),
$this->getUserRole(),
$this->getChannel(),
$text
);
}
}
[/php]

if /wipe isnt found its supposed to look in function parseCustomCommands($text, $textParts)

[php]
function ClearChat(){
// Admin Clears Chat
if($this->getUserRole() == AJAX_CHAT_ADMIN || $this->getUserRole() == AJAX_CHAT_MODERATOR) {
$sql1 = 'TRUNCATE TABLE ajax_chat_messages';
$sql2 = 'ALTER TABLE ajax_chat_messages AUTO_INCREMENT = 1';
// Create a new SQL query:
$this->sqlQuery($sql1);
$this->sqlQuery($sql2);
}
return true;
}

// Return true if a custom command has been successfully parsed, else false
// $text contains the whole message, $textParts the message split up as words array
function parseCustomCommands($text, $textParts) {
switch($textParts***91;0***93;) {
//clearChat:
case '/wipe':
$this->ClearChat();
return true;
break;
default:
$this->insertChatBotMessage(
$this->getPrivateMessageID(),
'/error UnknowncustCommand '.$textParts***91;0***93;
);
return false;
break;
}
}
[/php]

now in my parsecustomcommands function
default: is supposed to do this

[php]
default:
$this->insertChatBotMessage(
$this->getPrivateMessageID(),
'/error UnknowncustCommand '.$textParts***91;0***93;
);
return false;
[/php]

im getting /error Unknown Command wich is from the first function not my custom function

i believe your php works but i dont think the parsecustomcommands function is being called by insertparsedmessage($text) because if my parse customcommands was being called it should return /error UnknowncustCommand

here is the link http://community.anarchy1.tk/chat/ /wipe should wipe the chatlist

ill be there 2 talk to in real time if anyone ttakes a look

Frement

2011-05-25 16:58:16

Could you upload complete files? Its really hard for me to see how this should work.

EDIT: Or add me on MSN: [email]frement@windowslive.com[/email]

sm0k3

2011-05-25 20:01:14

me and frement hav tryed everything . it just wont allow ANY changes.

we even changged commands that were allready there and it acts like they were never changed.

x[Warrior]x3500

2011-05-25 22:43:42

well it recognizes the command but i get the error: (19:43:13) [Anarchy1]: Error: Connection status: 500



EDIT: try out /sexy ,my own lil creation :)

x[Warrior]x3500

2011-05-26 00:17:32

UPDATE: i just resolved this issue.

/wipe works

must refresh webpage after the /wipe occurs

Sex

2011-05-26 00:22:51

Wait so it was working since the beginning? Lmao.

x[Warrior]x3500

2011-05-26 00:27:05

no, i went in and changed around smok3's code. and finally got it to work :D

Sex

2011-05-26 00:36:41

What did you do?

x[Warrior]x3500

2011-05-26 00:43:12

1, i think they were writing the code in the incorrect location (its literally a maze of files, i don't like the organizational structure of the site /rant)

2, i kept getting "Error: Connection status: 500". now, knowing little about php/databasing, i really had no clue what that meant. after a little research i decided it meant some type of connection was not established that needed to be. then i looked at the code and realized that they called to the database incorrectly. so i simply looked upon another pre-made function that called to sql and followed its template.

after that, it was fixed. took a rather long time for little work overall lol

Sex

2011-05-26 01:00:35

So, global $db should have worked…

x[Warrior]x3500

2011-05-26 01:04:50

yup!

sm0k3

2011-05-26 14:59:51

. . . . . i think i need to speek to the script developer then to let him no that the tutorials i followed in the beginning are wrong. . as i followed the tutorial to add a custom command to the T.. then just changed what the custom command did . all i gotta do now is modify it to allow wiping only the current channel or all :)

thanks you did it you figured out the problem .