PHP Email Confirmation?

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
MiLO83
Registered User
Posts: 5
Joined: Fri Feb 19, 2016 11:38 pm

PHP Email Confirmation?

Post by MiLO83 » Fri Feb 19, 2016 11:52 pm

Hi, I just Installed 3.1.7-PL1 on Godaddy, and am new. (first post here..),
I have little knowledge of PHP or SQL. I however code C# so I have learned before.
I have made a WebGL game in Unity3D, which currently connects to a SQL database on my local machine and am trying to convert my code to register and login for/to the game via PHP POST using PHPBB users/passwords.

I have a few questions, yet I will inquire them 1 at a time :)

For starters, I cobbled together this code, (I had forum registration working WITHOUT email verification, but am attempting to revise the script to $message the verification email)

Here is my scrambled up code: :/

Code: Select all

<?php
    define('IN_PHPBB', true);
$phpbb_root_path = './forums/';

$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
include($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
$user_inactive_reason = INACTIVE_REGISTER;
$user_actkey = gen_rand_string(10);
$data = array();
$data['username'] = utf8_normalize_nfc(request_var('Username', '', true))
$data['user_password'] = phpbb_hash(request_var('Password', ''));
$data['new_password'] = request_var('Password', '');
$data['user_email'] = request_var('Email', '');
$data['group_id'] = '2';
$data['user_type'] = USER_INACTIVE; 
$data['user_actkey'] => $user_actkey,
$data['user_inactive_reason'] => $user_inactive_reason,
global $phpbb_root_path,$phpEx,$data,$email_template,$user;
user_add($data);

$user->session_begin();
$auth->acl($user->data);
$user->setup();
               $message = $user->lang['ACCOUNT_INACTIVE'];
               $email_template = 'user_welcome_inactive';
              
               $messenger = new messenger(false);

               $messenger->template($email_template, $data['lang']);

               $messenger->to($data['user_email'], $data['username']);

               $messenger->assign_vars(array(
                  'WELCOME_MSG'   => htmlspecialchars_decode(sprintf($user->lang['WELCOME_SUBJECT'], $config['sitename'])),
                  'USERNAME'      => htmlspecialchars_decode($data['username']),
                  'PASSWORD'      => htmlspecialchars_decode($data['new_password']),
                  'U_ACTIVATE'   => "http://www.cookieschronicle.com/forums/ucp.$phpEx?mode=activate&u=$user_id&k=$user_actkey")
               );

               $messenger->send(NOTIFY_EMAIL);
?>
<!DOCTYPE html>
<html>
<body>

<form action="http://www.cookieschronicle.com/gameSignup.php">
  User:<br>
  <input type="text" name="Username" value="Mickey">
  <br>
  Pass:<br>
  <input type="text" name="Password" value="TheMouse">
  <br>
Email:<br>
<input type="text" name="Email" value="edisonzghost@gmail.com">
  <br><br>
  <input type="submit" value="Submit">
</form> 

</body>
</html>
I have tried several variations and am now thinking it will just be faster to ask an expert.
I'm sure I am either missing something or it is in the wrong sequence.

Thanks!
- MiLO83

User avatar
MarkDHamill
Registered User
Posts: 3805
Joined: Fri Aug 02, 2002 12:36 am
Location: Florence, MA USA
Contact:

Re: PHP Email Confirmation?

Post by MarkDHamill » Sat Feb 20, 2016 2:52 am

I think you are in the wrong forum. It does not look like you are trying to write an extension but some sort of standalone program.

There is a web page for learning the basics about writing extensions here:

https://www.phpbb.com/extensions/writing/

Since you are a novice, this will be a huge learning curve. I started by analyzing the Acme demo extension.
Get the latest versions of my Digests and Smartfeed extensions.
Need phpBB services or a phpBB consultant? I offer most phpBB services.

MiLO83
Registered User
Posts: 5
Joined: Fri Feb 19, 2016 11:38 pm

Re: PHP Email Confirmation?

Post by MiLO83 » Sat Feb 20, 2016 3:11 am

Sorry if I posted in the wrong area, it seems similar code was in the MODS section for past versions.
I am going over everything i can find on the matter, but getting confused over which advice is applicable to 3.1.x?

I basically can only GET and POST from the game engine. I guess I need to read on extensions as you suggest,
I have spent 2 days reading about MODS not realizing they are no longer applicable?

Is GET and POST / SUBMIT receivable from an extension?
That's the easy way to do it from Unity3D WebGL. Otherwise I will have to do a lot more research and c# code something to interface, which I can do, but it would prob take me a few days. I would like to POST to a PHP on the same server.

I have made a more complex script from stripping apart ucp_register, I wont post it yet as its quite long.
Let me know if I should post it here or on hastebin please.

In the mean time I will be reading up on extensions :)
Wish me luck!

EDIT : Trying not to Double-Post...

Thank you for reading the 25 people who have read my posts,
I am really needing to know if I'm completely lost about this? does anyone have any insight?
How can I go about this? Really any reply would be so greatly appreciated.
I'm sitting here refreshing the forum every 5 or 10 minutes and just waiting for a reply with more information on whether this needs to be written as an extension or just a good ol' php?

Particularly, as I mentioned, I have successfully created new users from the Game Engine, using the php above minus the $message. However, I would really like it to send email confirmation.

I hate to say this, but as I have no users on my forum yet, tho I have made sub-forums with semi detailed topics, should I perhaps uninstall 3.1.x and install an older version which would be easier to interface with without having to code my own extension? It seems I may be writing a MOD?

I'm giving it 110% but the documentation and forum references are mostly for older versions as far as searching with google goes, i'm having problems as mentioned with relevance.

I don't want to give up on the latest version, but I think there is too many changes for me to pick up against most topics I have researched, this isnt a bad thing and if you say so I will pursue the 3.1.7 version, just in that case I will need more active response as reply in my questioning, 'cause i'm referencing mostly old material i fear.

Please help a C# coder out!
I really need just registering/log in/log out synced between my game and its forum, I could have sworn the code in my first post was very close!

User avatar
gn#36
Translator
Posts: 185
Joined: Fri Oct 13, 2006 1:16 pm
Contact:

Re: PHP Email Confirmation?

Post by gn#36 » Sat Feb 20, 2016 5:07 am

MiLO83 wrote:I basically can only GET and POST from the game engine. I guess I need to read on extensions as you suggest,
I have spent 2 days reading about MODS not realizing they are no longer applicable?
I don't think that is wasted time. If you want to use this only in your own forum and not publish an extension, you can certainly still modify phpBB code if you wish. But usually, upgrades are much easier if you don't. Additionally, many old interfaces are still available and used in basically the same way (e.g. user, template and config classes), so by reading about mods, you will still be able to use most of the interfaces on phpBB 3.1.x as well.
Is GET and POST / SUBMIT receivable from an extension?
Yes, certainly. There is a request class \phpbb\request\request that you can use to receive GET or POST, that also takes care of data validation: https://www.phpbb.de/infos/3.1/xref/nav ... t.php.html The old way would be request_var, which you should not use, though, as it is deprecated and will eventually be removed.
You will probably have to write a controller that takes care of the data handling. Essentially, the phpBB will do most of the basic initialization for you before calling the controller (so you won't need all the phpBB initialization stuff), you only need your own functionality inside an object and tell phpBB what URI to listen on (routing.yml configuration file) and what it needs (service.yml, dependency injection). Inside the controller, you can do the same stuff you could do before, too, if you want to. So writing an extension does not really limit you, especially since you do not really want to change phpBB behavior but only add something.

So you can initialize the messenger class and use that to send the confirmation message, just as is done in includes/ucp/ucp_register.php You can use the same includes if you wish, you can also use global variables like $user or $config (although it is usually better to use dependency injection instead to avoid trouble).
I hate to say this, but as I have no users on my forum yet, tho I have made sub-forums with semi detailed topics, should I perhaps uninstall 3.1.x and install an older version which would be easier to interface with without having to code my own extension? It seems I may be writing a MOD?
No. Since you just started and are adding functionality, it should be pretty easy to switch, because you basically only need to remove the surroundings, populate variables in your code that are usually global phpBB Variables (e.g. by using global, but preferably by using dependency injection), create service.yml, routing.yml and composer.json (which are all done in only a few minutes) and you should have a service that does the same work as your Mod.
just in that case I will need more active response as reply in my questioning, 'cause i'm referencing mostly old material i fear.
There is a bit of documentation out there, the most significant being: So basically anything that is listed here. If you post in this forum, you probably also saw the helpful tools list, and I would like to specifically point you to download and use a php development studio of some sort, so you can benefit from the autocomplete suggestions, which I think are really helpful for writing extensions.
I really need just registering/log in/log out synced between my game and its forum, I could have sworn the code in my first post was very close!
On the first look it is. And you don't necessarily have to write an extension for this to work. If you write a Mod for phpBB 3.1, that certainly does not mean that it won't work, if you write it for 3.1. But its outdated and you wouldn't be permitted into the Extension Database with it. I would still suggest writing it as an extension, because once you really understand them, it gets much easier and you will have all your changes to phpBB in one place (the ext/ folder, not scattered across the whole installation). Additionally for quite some changes, it is less work to write an extension than to write the whole PHP code yourself. You could, for example, create a new authentication plugin and use your games cookies to login to phpBB.

To help you better with your problem, you will still have to state what exactly is going wrong, though. What PHP errors do you get, what is the false behavior, etc.?

Regarding the form you have at the bottom of the file, you should probably add some XSRF protection to it.
German Support Team Member • http://www.phpbb.deMy Extensions in the CDBMy Extensions on Github • Contact with caos is inavoidable but no catastrophy if you keep an overview.

MiLO83
Registered User
Posts: 5
Joined: Fri Feb 19, 2016 11:38 pm

Re: PHP Email Confirmation?

Post by MiLO83 » Sat Feb 20, 2016 5:20 am

THANK YOU! (For your very long and quite detailed reply!)

"To help you better with your problem, you will still have to state what exactly is going wrong, though. What PHP errors do you get, what is the false behavior, etc.?"

Very Simply,
I never receive the confirmation email, I waited hours and checked spam folders. I don't know what log files I may have hidden somewhere (where to look?) but from the Form, I receive a blank white page and no visible error.
If you read the code in the first post can you spot the issue?

User avatar
3Di
Former Team Member
Posts: 13772
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: PHP Email Confirmation?

Post by 3Di » Sat Feb 20, 2016 5:49 am

$messenger = new messenger(false);
should be
$messenger = new \messenger(false);
at a first glance.

edit: the false doesn't allow the use of the queue else you can avoid it.
Please PM me only to request paid works. Thx.
Want to compensate me for my interest? Donate
My development's activity º PhpStorm's proud user
Extensions, Scripts, MOD porting, Update/Upgrades
👨‍🏫 | Take a tour to | The Studio | 👨‍🏫

MiLO83
Registered User
Posts: 5
Joined: Fri Feb 19, 2016 11:38 pm

Re: PHP Email Confirmation?

Post by MiLO83 » Sat Feb 20, 2016 7:02 am

hmmm, that wasn't enough to make it work...
Can you look more thoroughly? It's my first day with phpbb and I'm sure its a simple error :(
Does request_var still work in 3.1.7-PL1?

EDIT (Updated Code!)

Code: Select all

<?php
    define('IN_PHPBB', true);
$phpbb_root_path = './forums/';

$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
if (!class_exists('messenger'))
		{
			include($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
		}
global $phpbb_root_path,$phpEx,$data,$email_template,$user;
$user->session_begin();
$user_inactive_reason = INACTIVE_REGISTER;
$user_actkey = gen_rand_string(10);
$data = array();
$data['username'] = utf8_normalize_nfc(request_var('Username', '', true))
$data['user_password'] = phpbb_hash(request_var('Password', ''));
$data['new_password'] = request_var('Password', '');
$data['user_email'] = request_var('Email', '');
$data['group_id'] = '2';
$data['user_type'] = USER_INACTIVE;
$data['user_actkey'] => $user_actkey,
$data['user_inactive_reason'] => $user_inactive_reason,
$data['email'] => strtolower(request_var('Email', '')),
user_add($data);
$auth->acl($user->data);
$user->setup();

				$message = $user->lang['ACCOUNT_INACTIVE'];
				$email_template = 'user_welcome_inactive';
             
				$messenger = new \messenger(false);
				$messenger->template($email_template, $data['lang']);
				
				// Email headers
				$messenger->headers('X-AntiAbuse: Board servername - ' . $config['server_name']);
				$messenger->headers('X-AntiAbuse: User_id - ' . $user->data['user_id']);
				$messenger->headers('X-AntiAbuse: Username - ' . $user->data['username']);
				$messenger->headers('X-AntiAbuse: User IP - ' . $user->ip);

				$messenger->to($data['email'], $data['username']);
				$server_url = generate_board_url();
				$messenger->assign_vars(array(
                  'WELCOME_MSG'   => htmlspecialchars_decode(sprintf($user->lang['WELCOME_SUBJECT'], $config['sitename'])),
                  'USERNAME'      => htmlspecialchars_decode($data['username']),
                  'PASSWORD'      => htmlspecialchars_decode($data['new_password']),
                  'U_ACTIVATE'   => "$server_url/ucp.$phpEx?mode=activate&u=$user_id&k=$user_actkey")
               );

				$messenger->send(NOTIFY_EMAIL);
			   
			   
?>


User avatar
gn#36
Translator
Posts: 185
Joined: Fri Oct 13, 2006 1:16 pm
Contact:

Re: PHP Email Confirmation?

Post by gn#36 » Sat Feb 20, 2016 1:16 pm

If you haven't done so, enable debug mode (look into config.php). request_var still works.

But I am not surprised to see no output, your code doesn't generate any, so that could also mean it works and your email settings in the board are wrong.

You should generate some debug outputs at critical points (print_r($data) to ensure the data is correct, for example) Also, you can check your server error log for errors. I am not sure the function phpbb_hash still exists. You should use the passwords manager:

Code: Select all

$passwords_manager = $phpbb_container->get('passwords.manager');
$hashed_pw = $passwords_manager->hash($data['new_password']),
I don't think the namespace of the messenger class was a problem, because it is defined in global namespace and the script does not contain any namespace definition. Its not wrong, either, though and will avoid problems if you move to a controller.
German Support Team Member • http://www.phpbb.deMy Extensions in the CDBMy Extensions on Github • Contact with caos is inavoidable but no catastrophy if you keep an overview.

MiLO83
Registered User
Posts: 5
Joined: Fri Feb 19, 2016 11:38 pm

Re: PHP Email Confirmation?

Post by MiLO83 » Mon Feb 22, 2016 8:17 am

Hi, Thank you so much!
Sorry I have been AFK at my grandpas 89th birthday party.
I am out of town, but working on it here a lil bit.
I'll be back at 'er full swing when I return home in 2 or 3 days :geek:

Post Reply

Return to “Extension Writers Discussion”