Jump to content

Can you please tell me what you see wrong with this code?


Chrisj
 Share

Recommended Posts

			$insert = $db->insert(T_MESSAGES, $insert_message);
			if ($insert) {
				$pt->message = PT_GetMessageData($insert);
				$data = array(
					'status' => 200,
					'message_id' => $_POST['message_id'],
					'message' => PT_LoadPage('messages/ajax/outgoing', array(
						'ID' => $pt->message->id,
						'TEXT' => $pt->message->text
					))
);
				if ($pt->config->validation == 'on') {
				$link = $email_code . '/' . $email;
				$data['EMAIL_CODE'] = $link;
				$data['USERNAME'] = $username;
				$send_email_data = array(
				'from_email' => $pt->config->email,
				'from_name' => $pt->config->name,
				'to_email' => $pt->user_two->e-mail,
				'to_name' => $username,
				'subject' => 'Message Waiting',
				'charSet' => 'UTF-8',
				'message_body' => PT_LoadPage('emails/message-alert', $data),
				'is_html' => true
				);
				$send_message = PT_SendMessage($send_email_data;

 

Edited by Chrisj
Link to comment
Share on other sites

Thanks for your replies.

The php web script that I'm using allows Users' to send messages internally. I received some guidance as to how to add the ability to notify a User, via email, when he receives a web message. I was told to add this code, after line 44 (to the existing code below) but it doesn't currently work, any help/suggestions will be appreciated.:

if ($pt->config->validation == 'on') {
$link = $email_code . '/' . $email; 
$data['EMAIL_CODE'] = $link;
$data['USERNAME'] = $username;
$send_email_data = array(
'from_email' => $pt->config->email,
'from_name' => $pt->config->name,
'to_email' => $pt->user_two->e-mail 
'to_name' => $username,
'subject' => 'Message Waiting',
'charSet' => 'UTF-8',
'message_body' => PT_LoadPage('emails/confirm-account', $data),
'is_html' => true
);
$send_message = PT_SendMessage($send_email_data);

Existing code:

<?php  
if (IS_LOGGED == false) {
    $data = array(
        'status' => 400,
        'error' => 'Not logged in'
    );
    echo json_encode($data);
    exit();
}

if ($first == 'new') {
    if (!empty($_POST['id']) && !empty($_POST['new-message'])) {
        $link_regex = '/(http\:\/\/|https\:\/\/|www\.)([^\ ]+)/i';
        $i          = 0;
        preg_match_all($link_regex, PT_Secure($_POST['new-message']), $matches);
        foreach ($matches[0] as $match) {
            $match_url           = strip_tags($match);
            $syntax              = '[a]' . urlencode($match_url) . '[/a]';
            $_POST['new-message'] = str_replace($match, $syntax, $_POST['new-message']);
        }
        $new_message = PT_Secure($_POST['new-message']);
        $id = PT_Secure($_POST['id']);
        if ($id != $pt->user->id) {
            $chat_exits = $db->where("user_one", $pt->user->id)->where("user_two", $id)->getValue(T_CHATS, 'count(*)');
            if (!empty($chat_exits)) {
                $db->where("user_two", $pt->user->id)->where("user_one", $id)->update(T_CHATS, array('time' => time()));
                $db->where("user_one", $pt->user->id)->where("user_two", $id)->update(T_CHATS, array('time' => time()));
                if ($db->where("user_two", $pt->user->id)->where("user_one", $id)->getValue(T_CHATS, 'count(*)') == 0) {
                    $db->insert(T_CHATS, array('user_two' => $pt->user->id, 'user_one' => $id,'time' => time()));
                }
            } else {
                $db->insert(T_CHATS, array('user_one' => $pt->user->id, 'user_two' => $id,'time' => time()));
                if (empty($db->where("user_two", $pt->user->id)->where("user_one", $id)->getValue(T_CHATS, 'count(*)'))) {
                    $db->insert(T_CHATS, array('user_two' => $pt->user->id, 'user_one' => $id,'time' => time()));
                }
            }
            $insert_message = array(
                'from_id' => $pt->user->id,
                'to_id' => $id,
                'text' => $new_message,
                'time' => time()
            );
            $insert = $db->insert(T_MESSAGES, $insert_message);
            if ($insert) {
                $pt->message = PT_GetMessageData($insert);
                $data = array(
                    'status' => 200,
                    'message_id' => $_POST['message_id'],
                    'message' => PT_LoadPage('messages/ajax/outgoing', array(
                        'ID' => $pt->message->id,
                        'TEXT' => $pt->message->text
                    ))
                );
            }
        }
    }
}

if ($first == 'fetch') {
    if (empty($_POST['last_id'])) {
        $_POST['last_id'] = 0;
    }
    if (empty($_POST['id'])) {
        $_POST['id'] = 0;
    }
    if (empty($_POST['first_id'])) {
        $_POST['first_id'] = 0;
    }
    $messages_html = PT_GetMessages($_POST['id'], array('last_id' => $_POST['last_id'], 'first_id' => $_POST['first_id'], 'return_method' => 'html'));
    if (!empty($messages_html)) {
        $html = PT_LoadPage("messages/{$pt->config->server}/messages", array('MESSAGES' => $messages_html));
    } else {
        $html = PT_LoadPage("messages/ajax/no-messages");
    }

    $users_html = PT_GetMessagesUserList(array('return_method' => 'html'));

    if (!empty($messages_html) || !empty($users_html)) {
        $data = array('status' => 200, 'message' => $messages_html, 'users' => $users_html);
    }
}

if ($first == 'search') {
    $keyword = '';
    $users_html = '<p class="text-center">' . $lang->no_match_found . '</p>';
    if (isset($_POST['keyword'])) {
        $users_html = PT_GetMessagesUserList(array('return_method' => 'html', 'keyword' => $_POST['keyword']));
    }
    $data = array('status' => 200, 'users' => $users_html);
}

if ($first == 'delete_chat') {
    if (!empty($_POST['id'])) {
        $id = PT_Secure($_POST['id']);
        $messages = $db->where("(from_id = {$pt->user->id} AND to_id = {$id}) OR (from_id = {$id} AND to_id = {$pt->user->id})")->get(T_MESSAGES);
        $update1 = array();
        $update2 = array();
        $erase = array();
        foreach ($messages as $key => $message) {
            if ($message->from_deleted == 1 || $message->to_deleted == 1) {
                $erase[] = $message->id;
            } else {
                if ($message->to_id == $pt->user->id) {
                    $update2[] = $message->id;
                } else {
                    $update1[] = $message->id;
                }
            }
        }
        if (!empty($erase)) {
            $erase = implode(',', $erase);
            $final_query = "DELETE FROM " . T_MESSAGES . " WHERE id IN ($erase)";
            $db->rawQuery($final_query);
        }
        if (!empty($update1)) {
            $update1 = implode(',', $update1);
            $final_query = "UPDATE " . T_MESSAGES . " set `from_deleted` = '1' WHERE `id` IN({$update1}) ";
            $db->rawQuery($final_query);
        }
        if (!empty($update2)) {
            $update2 = implode(',', $update2);
            $final_query = "UPDATE " . T_MESSAGES . " set `to_deleted` = '1' WHERE `id` IN({$update2}) ";
            $db->rawQuery($final_query);
        }
        $delete_chats = $db->rawQuery("DELETE FROM " . T_CHATS . " WHERE user_one = {$pt->user->id} AND user_two = $id");
    }
}
?>

 

Link to comment
Share on other sites

1 - Did you write this web script?  Or did you get it from somewhere/someone?  If you wrote it great.  If not, why are you asking us for help with it and not the author?

2 - So - what does "not work" mean to you?  A blank screen?  A response but no emails being sent?  A response indicating an error?

3 - What debugging efforts have you - the programmer - done to try and solve this dilemma?  Any echo statements added to help track what occurs during execution or how far it actually gets in the lengthy script that you want US to debug for you? Do you know that the call to send the email is actually occurring?

Link to comment
Share on other sites

Thanks for your reply and great questions.

1. No, I didn't write this script. The author is weeks out for any modifications.

2. "not work" means when (and where) I added in the code the internal messaging was no longer sending or receiving messages, and no emails were sent. When I removed the added code, internal messaging resumed working successfully.

3. No debugging done. No, I don't know if the call to send email is actually working. I don't know how to do those things. Any guidance will be appreciated.

Link to comment
Share on other sites

The simple answer is that you probably introduced a syntax error because you are missing a closing block. 

There are also other potential issues 

As for debugging the first issue -- you should validate that the block you were given is complete.  Indentation helps make this obvious:

if ($pt->config->validation == 'on') {
    $link = $email_code . '/' . $email; 
    $data['EMAIL_CODE'] = $link;
    $data['USERNAME'] = $username;
    $send_email_data = array(
    'from_email' => $pt->config->email,
    'from_name' => $pt->config->name,
    'to_email' => $pt->user_two->e-mail 
    'to_name' => $username,
    'subject' => 'Message Waiting',
    'charSet' => 'UTF-8',
    'message_body' => PT_LoadPage('emails/confirm-account', $data),
    'is_html' => true
    );
    $send_message = PT_SendMessage($send_email_data);

Note that this block of code is missing the closing curly brace and thus would break the entire script.  Add the closing curly brace.

if ($pt->config->validation == 'on') {
    $link = $email_code . '/' . $email; 
    $data['EMAIL_CODE'] = $link;
    $data['USERNAME'] = $username;
    $send_email_data = array(
    'from_email' => $pt->config->email,
    'from_name' => $pt->config->name,
    'to_email' => $pt->user_two->e-mail 
    'to_name' => $username,
    'subject' => 'Message Waiting',
    'charSet' => 'UTF-8',
    'message_body' => PT_LoadPage('emails/confirm-account', $data),
    'is_html' => true
    );
    $send_message = PT_SendMessage($send_email_data);
}

 

The other issue is that we have no way of knowing where variables like $pt, $link or $username are defined, or even IF they are defined.  I didn't see them in the pre-existing code, and if they don't exist, the assignments using them will generate notices.  If $pt isn't an object as expected, the code will generate a runtime error. 

Probably your production server suppresses notices, but they can break a working system if the errorlevel does not suppress them.  Since we have no idea what those variables do, should they exist, we have no idea if they are even required in the context of this code.

 

Link to comment
Share on other sites

Thanks again for the replies.

Your "missing a curly bracket" helped, now this no longer prevents internal messaging (but no emails sent, yet):

$insert = $db->insert(T_MESSAGES, $insert_message);
			if ($insert) {
				$pt->message = PT_GetMessageData($insert);
				$data = array(
					'status' => 200,
					'message_id' => $_POST['message_id'],
					'message' => PT_LoadPage('messages/ajax/outgoing', array(
						'ID' => $pt->message->id,
						'TEXT' => $pt->message->text
					))
);



				if ($pt->config->validation == 'on') {
				$link = $email_code . '/' . $email;
				$data['EMAIL_CODE'] = $link;
				$data['USERNAME'] = $username;
				$send_email_data = array(
				'from_email' => $pt->config->email,
				'from_name' => $pt->config->name,
				'to_email' => $pt->user_two->e-mail,
				'to_name' => $username,
				'subject' => 'Message Waiting',
				'charSet' => 'UTF-8',
				'message_body' => PT_LoadPage('emails/message-alert', $data),
				'is_html' => true
				);
				$send_message = PT_SendMessage($send_email_data);
}
			}
		}
	}
}

So, that is a step in the right direction.

Because this script's registration file confirms the registering Users' email, I had taken this code:

                if ($pt->config->validation == 'on') {
                     $link = $email_code . '/' . $email; 
                     $data['EMAIL_CODE'] = $link;
                     $data['USERNAME']   = $username;
                     $send_email_data = array(
                        'from_email' => $pt->config->email,
                        'from_name' => $pt->config->name,
                        'to_email' => $email,
                        'to_name' => $username,
                        'subject' => 'Confirm your account',
                        'charSet' => 'UTF-8',
                        'message_body' => PT_LoadPage('emails/confirm-account', $data),
                        'is_html' => true
                    );
                    $send_message = PT_SendMessage($send_email_data);

from the registration file code (starting at line 111), to attempt to add in to the messages code, to get a message to (possibly) send a notification, here's the registration code:

<?php
if (IS_LOGGED == true || $pt->config->user_registration != 'on') {
    header("Location: " . PT_Link(''));
    exit();
}

$color1      = '2ec0bc';
$color2      = '8ef9f6';
$errors      = array();
$erros_final = '';
$username    = '';
$email       = '';
$success     = '';
$recaptcha   = '<div class="g-recaptcha" data-sitekey="' . $pt->config->recaptcha_key . '"></div>';
$pt->custom_fields = $db->where('registration_page','1')->get(T_FIELDS);
$field_data        = array();
if ($pt->config->recaptcha != 'on') {
    $recaptcha = '';
}
if (!empty($_POST)) {
    if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['email']) || empty($_POST['c_password']) || empty($_POST['gender'])) {
        $errors[] = $lang->please_check_details;
    } else {
        $username        = PT_Secure($_POST['username']);
        $password        = PT_Secure($_POST['password']);
        $c_password      = PT_Secure($_POST['c_password']);
        $password_hashed = sha1($password);
        $email           = PT_Secure($_POST['email']);
        $gender          = PT_Secure($_POST['gender']);
        if ($gender != 'female' && $gender != 'male') {
            $errors[] = $lang->gender_is_invalid;
        }
        if (PT_UsernameExists($_POST['username'])) {
            $errors[] = $lang->username_is_taken;
        }
        if (strlen($_POST['username']) < 4 || strlen($_POST['username']) > 32) {
            $errors[] = $lang->username_characters_length;
        }
        if (!preg_match('/^[\w]+$/', $_POST['username'])) {
            $errors[] = $lang->username_invalid_characters;
        }
        if (PT_UserEmailExists($_POST['email'])) {
            $errors[] = $lang->email_exists;
        }
        if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errors[] = $lang->email_invalid_characters;
        }
        if ($password != $c_password) {
            $errors[] = $lang->password_not_match;
        }
        if (strlen($password) < 4) {
            $errors[] = $lang->password_is_short;
        }
        if ($pt->config->recaptcha == 'on') {
            if (!isset($_POST['g-recaptcha-response']) || empty($_POST['g-recaptcha-response'])) {
                $errors[] = $lang->reCaptcha_error;
            }
        }

        if (empty($_POST['terms'])) {
            $errors[] = $lang->terms_accept;
        } elseif ($_POST['terms'] != 'on') {
            $errors[] = $lang->terms_accept;
        }
        

        if (!empty($pt->custom_fields) && count($pt->custom_fields) > 0) {
            foreach ($pt->custom_fields as $field) {
                $field_id   = $field->id;
                $field->fid = "fid_$field_id";

                if (!empty($_POST[$field->fid])) {
                    $name = $field->fid;
                    if (!empty($_POST[$name])) {
                        $field_data[] = array(
                            $name => $_POST[$name]
                        );
                    }
                }
            }
        }


        $active = ($pt->config->validation == 'on') ? 0 : 1;
        if (empty($errors)) {
            $email_code = sha1(time() + rand(111,999));
            $insert_data = array(
                'username' => $username,
                'password' => $password_hashed,
                'email' => $email,
                'ip_address' => get_ip_address(),
                'gender' => $gender,
                'active' => $active,
                'email_code' => $email_code,
                'last_active' => time(),
                'registered' => date('Y') . '/' . intval(date('m'))
            );
            $insert_data['language'] = $pt->config->language;
            if (!empty($_SESSION['lang'])) {
                if (in_array($_SESSION['lang'], $langs)) {
                    $insert_data['language'] = $_SESSION['lang'];
                }
            }
            $user_id             = $db->insert(T_USERS, $insert_data);
            if (!empty($user_id)) {
                if (!empty($field_data)) {
                    PT_UpdateUserCustomData($user_id,$field_data,false);
                }


                if ($pt->config->validation == 'on') {
                     $link = $email_code . '/' . $email; 
                     $data['EMAIL_CODE'] = $link;
                     $data['USERNAME']   = $username;
                     $send_email_data = array(
                        'from_email' => $pt->config->email,
                        'from_name' => $pt->config->name,
                        'to_email' => $email,
                        'to_name' => $username,
                        'subject' => 'Confirm your account',
                        'charSet' => 'UTF-8',
                        'message_body' => PT_LoadPage('emails/confirm-account', $data),
                        'is_html' => true
                    );
                    $send_message = PT_SendMessage($send_email_data);
                    $success = $success_icon . $lang->successfully_joined_desc;
                } 

                else {
                    $session_id          = sha1(rand(11111, 99999)) . time() . md5(microtime());
                    $insert_data         = array(
                        'user_id' => $user_id,
                        'session_id' => $session_id,
                        'time' => time()
                    );
                    $insert              = $db->insert(T_SESSIONS, $insert_data);
                    $_SESSION['user_id'] = $session_id;
                    setcookie("user_id", $session_id, time() + (10 * 365 * 24 * 60 * 60), "/");
                    $pt->loggedin = true;
                    header("Location: $site_url");
                    exit();
                }
            }
        }
    }
}
$pt->page          = 'login';
$pt->title         = $lang->register . ' | ' . $pt->config->title;
$pt->description   = $pt->config->description;
$pt->keyword       = $pt->config->keyword;
$custom_fields     = "";
if (!empty($errors)) {
    foreach ($errors as $key => $error) {
        $erros_final .= $error_icon . $error . "<br>";
    }
}
if (!empty($pt->custom_fields)) {
    foreach ($pt->custom_fields as $field) {
        $field_id       = $field->id;
        $fid            = "fid_$field_id";
        $pt->filed      = $field;
        $custom_fields .= PT_LoadPage('auth/register/custom-fields',array(
            'NAME'      => $field->name,
            'FID'       => $fid
        ));
    }
}

$pt->content     = PT_LoadPage('auth/register/content', array(
    'COLOR1' => $color1,
    'COLOR2' => $color2,
    'ERRORS' => $erros_final,
    'USERNAME' => $username,
    'EMAIL' => $email,
    'SUCCESS' => $success,
    'RECAPTCHA' => $recaptcha,
    'CUSTOM_FIELDS' => $custom_fields
));

Any additional guidance will be welcomed. Much thanks again.

 

Edited by Chrisj
Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • 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.