Skip to content
Snippets Groups Projects
signup.class.php 5.21 KiB
Newer Older
root's avatar
root committed
<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php');

use Mailgun\Mailgun;
root's avatar
root committed
use MongoDB\Collection;
root's avatar
root committed

class signup
{
    private $username;
    private $password;
    private $email;
    public $userid;
    public $token;
root's avatar
root committed
    private $db;
root's avatar
root committed
    private $collection;
root's avatar
root committed


    function __construct($username, $password, $email)
    {
        $bytes = random_bytes(16);
        $this->token = bin2hex($bytes);
        $password = password_hash($password, PASSWORD_DEFAULT);
        $this->username = $username;
        $this->password = $password;
        $this->email = $email;


root's avatar
root committed
        $this->db = Database::getConnection();
root's avatar
root committed

root's avatar
root committed
        $this->collection = $this->db->auth;
root's avatar
root committed

root's avatar
root committed
        $exist = $this->collection->findOne(['username' => $username]);
        if ($exist) {
            throw new Exception("User already exists");
        }
        $result = $this->collection->insertOne([
            'username' => $username,
            'password' => $password,
            'email' => $email,
            'token' => $this->token,
            'active' => 0
        ]);

        if ($result->getInsertedCount() !== 1) {
            throw new Exception("Error inserting user");
root's avatar
root committed
            $this->userid = $result->getInsertedId();
    function sendVerificationMail()
    {
        $config_json = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/../env.json');
        $config = json_decode($config_json, true);
        $token = $this->token;
        $email = new \SendGrid\Mail\Mail();
        $email->setFrom("noreply@selfmade.ninja", "API Course by Selfmade");
        $email->setSubject("Verify your account");
        $email->addTo($this->email, $this->username);
        $email->addContent("text/plain", "Please verify your account at: https://api1.selfmade.ninja/verify?token=$token");
        $email->addContent(
            "text/html",
            "<strong>Please verify your account by <a href=\"https://api1.selfmade.ninja/verify?token=$token\">clicking here</a> or open this URL manually: <a href=\"https://api1.selfmade.ninja/verify?token=$token\">https://api1.selfmade.ninja/verify?token=$token</a></strong>"
        );
        $sendgrid = new \SendGrid($config['email_api_key']);
        try {
            $response = $sendgrid->send($email);
            // print $response->statusCode() . "\n";
            // print_r($response->headers());
            // print $response->body() . "\n";
        } catch (Exception $e) {
            echo 'Caught exception: ' . $e->getMessage() . "\n";
root's avatar
root committed
        }
    }



    public static function mail()
    {
        // Instantiate the Mailgun client with your API key
        $mgClient = Mailgun::create('755a963567ccf66107c080cfd1921dcb-2e68d0fb-d81394af');
        $domain = "mail.sanjayy.me";

        // HTML email template
        $htmlTemplate = '
        <table style="max-width: 600px; margin: 0 auto; background-color: #ffffff; font-family: Arial, sans-serif; color: #333333; padding: 20px; border-collapse: collapse;">
    <tr>
        <td style="text-align: center; padding: 10px 0;">
            <img src="https://merakiui.com/images/full-logo.svg" alt="Meraki UI" style="height: 40px;">
        </td>
    </tr>
    <tr>
        <td style="padding: 20px;">
            <h2 style="color: #1a202c; font-size: 24px;">Hi Olivia,</h2>
            <p style="font-size: 16px; line-height: 1.5; color: #4a5568;">
                Alicia has invited you to join the team on <strong>Meraki UI</strong>.
            </p>
            <a href="#" style="display: inline-block; margin-top: 20px; padding: 10px 20px; background-color: #3182ce; color: #ffffff; text-decoration: none; border-radius: 5px; font-size: 14px;">
                Accept the invite
            </a>
        </td>
    </tr>
    <tr>
        <td style="padding: 20px; text-align: center; font-size: 12px; color: #718096;">
            <p>This email was sent to <a href="#" style="color: #3182ce; text-decoration: underline;">contact@merakiui.com</a>.</p>
            <p>© 2024 Meraki UI. All Rights Reserved.</p>
        </td>
    </tr>
</table>
        ';

        try {
            // Send the email
            $mgClient->messages()->send($domain, [
                'from'    => 'Excited User <mailgun@mail.sanjayy.me>', // Adjusted sender email to match the domain
                'to'      => 'email2sanjayv@gmail.com',
                'subject' => 'The PHP SDK is awesome!',
                'html'    => $htmlTemplate // Use the HTML template
            ]);
            echo "Email sent successfully!";
        } catch (Exception $e) {
            // Handle errors
            echo "Error sending email: " . $e->getMessage();
root's avatar
root committed
        }
    }

root's avatar
root committed


    public function getinsertid()
    {
        return $this->userid;
    }

    public static function verifyAccount($token)
    {
root's avatar
root committed
        $db = Database::getConnection();
root's avatar
root committed
        $Collection = $db->auth;

        $result = $Collection->findOne(['token' => $token]);
        if ($result['active'] == 1) {
            return "Already Verified";
        } else {
            $Collection->updateOne(['token' => $token], ['$set' => ['active' => 1]]);
            return "Verified";
        }
        if ($result->getModifiedCount() == 1) {
root's avatar
root committed
            return true;
        } else {
            return false;
        }
    }
}