HEX
Server: Apache
System: Linux msm5694.mjhst.com 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64
User: camjab_ssh (1000)
PHP: 5.3.29
Disabled: NONE
Upload Files
File: /home/httpd/html/baretube.com/admin/functions.email.php
<?php

    /**
     *  sendAdminMessage - Sends an email to $site_admin from settings.
     *  
     *  @param [in] $data Not yet documented.
     *  @return void
     *  
     */
    function sendAdminMessage($data) {
        global $sitename,$admin_email;

        $data += array(
            'to_email' => $admin_email,
            'to_name' => $sitename,
        );
        return _sendEmail($data);
    }
    
    /**
     *  sendForgotPasswordStep - A summary has for this function has not yet been written.
     *  
     *  @param [in] $step Not yet documented.
     *  @param [in] $id Not yet documented.
     *  @param [in] $newpass Not yet documented.
     *  @return void
     *  
     */
    function sendForgotPasswordStep($step, $id, $newpass = false) {
        global $basehttp;

        switch ($step) {
            case 2: //send new password
                if (is_numeric($id)) {
                    $result = dbRow("SELECT * FROM users WHERE record_num = '$id'",false);
                    if (!empty($result)) {
                        $row = $result;
                        sendEmail('reset-password-confirmation', $id, array('%newpassword' => "$newpass"));
                        dbQuery("UPDATE users SET forgot_pass_hash = '' WHERE record_num = {$row['record_num']} LIMIT 1");
                    }
                }

                break;

            case 1: //send change password notification email
            default:
                if (is_numeric($id)) {
                    $result = dbRow("SELECT * FROM users WHERE record_num = '$id'");
                    if (!empty($result)) {
                        $row = $result;

                        $hash = $row['record_num'] . uniqid();
                        $link = "$basehttp/forgot-pass?hash=" . $hash;

                        sendEmail('reset-password', $id, array('%password_link' => "$link"));
                        dbQuery("UPDATE users SET forgot_pass_hash = '$hash' WHERE record_num = {$row['record_num']} LIMIT 1");
                    }
                }

                break;
        }
    }

    /**
     *  sendEmail - A summary has for this function has not yet been written.
     *  
     *  @param [in] $system_name Not yet documented.
     *  @param [in] $user_id Not yet documented.
     *  @return void
     *  
     */
    function sendEmail($system_name, $user_id, $placeholders = array(), $options = array()) {
        global $basehttp, $language_default, $email_notifications_triggers, $sitename, $noreply_email, $admin_email, $dblink, $config;

        if (!isset($language_default)) {
            $language_default = 'en';
        }

        // Check, if users notifications settings allow to send this e-mail
        if ($user_id !== false) {
            $user_notifications = dbRow("SELECT * FROM `users_notifications` WHERE `user_id` = '$user_id' AND `scope` = 'email' LIMIT 1", false);
            if (is_array($email_notifications_triggers)) {
                if (isset($user_notifications[$email_notifications_triggers[$system_name]]) && $user_notifications[$email_notifications_triggers[$system_name]] == 0) {
                    return;
                }
            }
        }

        $default_placeholders = array(
            '%basehttp' => $basehttp,
            '%sitename' => $config['sitename'],
            '%noreply_email' => $config['noreply_email'],
            '%admin_email' => $config['admin_email'],
        );
        $placeholders = array_merge($default_placeholders, $placeholders);

        if ($user_id !== false) {
            $user = dbRow("SELECT * FROM `users` WHERE `record_num` = '$user_id'");
            if (!is_array($user)) {
                return;
            } else if (!isset($user['language'])) {
                $user['language'] = $language_default;
            }
        }

        $template = dbRow("SELECT `email_translations`.* FROM `email_translations` INNER JOIN `email_templates` ON `email_translations`.`template_id` = `email_templates`.`record_num` WHERE `email_templates`.`name` = '" . mysqli_real_escape_string($dblink,$system_name) . "' AND `language` = '" . mysqli_real_escape_string($dblink, $user['language']) . "'");
        if (!is_array($template)) { // default language
            $template = dbRow("SELECT `email_translations`.* FROM `email_translations` INNER JOIN `email_templates` ON `email_translations`.`template_id` = `email_templates`.`record_num` WHERE `email_templates`.`name` = '" . mysqli_real_escape_string($dblink,$system_name) . "' AND `language` = '" . mysqli_real_escape_string($dblink, $language_default) . "'");
        }

        if (!is_array($template)) {
            return;
        }

        if (is_array($user)) {
            if (filter_var($user['email'], FILTER_VALIDATE_EMAIL)) {
                foreach ($user as $key => $value) {
                    $placeholders["%$key"] = $value;
                }

                $data = array(
                    'user_id' => $user['record_num'],
                    'to_email' => $user['email'],
                    'to_name' => $user['username'],
                    'replyto_email' => $config['noreply_email'],
                    'from_email' => $config['noreply_email'],
                    'from_name' => $config['sitename'],
                );
            }
        } else {
            $data = array(
                'to_email' => $config['admin_email'],
                'to_name' => $config['sitename'],
                'replyto_email' => isset($options['replyto_email']) ? $options['replyto_email'] : $config['noreply_email'],
                'from_email' => isset($options['from_email']) ? $options['from_email'] : $config['noreply_email'],
                'from_name' => isset($options['from_name']) ? $options['from_name'] : $config['sitename'],
            );
        }
        
        $data['subject'] = strtr($template['subject'], $placeholders);
        $data['message'] = strtr($template['body'], $placeholders);

        $data = array_merge($data, $options);

        return _sendEmail($data);
    }

    // Adds HTML headers and wraps message into HTML table
    function htmlEmail(&$data) {
        global $basepath, $template_path;

        include_once "$basepath/includes/CssToInlineStyles.php";

        $data['headers'] = "From: $data[from_name] <$data[from_email]>\r\n";
        $data['headers'] .= "MIME-Version: 1.0\r\n";
        $data['headers'] .= "Content-Type: text/html; charset=utf-8\r\n";

        ob_start();
        if (isset($data['placeholders']['AVATAR'])) {
            eval('?>' . file_get_contents("$template_path/email/mail-avatar.php"));
        } elseif (isset($data['custom_theme']) && file_exists("$template_path/email/$data[custom_theme].php")) {
            eval('?>' . file_get_contents("$template_path/email/$data[custom_theme].php"));
        } else {
            eval('?>' . file_get_contents("$template_path/email/mail-regular.php"));
        }
        $data['themed'] = ob_get_contents();
        ob_end_clean();

        if (is_array($data['placeholders'])) {
            foreach ($data['placeholders'] as $key => $value) {
                $data['themed'] = str_replace('{' . $key . '}', $value, $data['themed']);
            }
        }

        if (isset($data['themed'])) {
            preg_match("'<style type=\"text/css\">(.*?)</style>'si", $data['themed'], $css);
            if (is_array($css) && $css[1] != "") {
                $data['message_css'] = $css[1];
                $htmlConvert = new CssToInlineStyles($data['themed'], $data['message_css']);
                $data['message'] = $htmlConvert->convert();
            }
        }
    }

    // Send email using mail() function or PHPMailer, if enabled in admin/config.php
    function _sendEmail($data) {
        global $basepath, $basehttp, $template_url, $sitename, $send_emails, $send_html_emails, $phpmailer_enabled, $smtp_host, $smtp_auth, $smtp_user, $smtp_pass, $smtp_port;
		
        if (!$send_emails) {
            return;
        }

        if ($send_html_emails) {
            $data['placeholders'] = (array) $data['placeholders'];
            $data['placeholders'] += array(
                'TEMPLATE_URL' => $template_url,
                'SITE_URL' => $basehttp,
                'SITE_NAME' => $sitename,
                'SUBJECT' => $data['subject'],
                'MESSAGE' => $data['message'],
            );

            htmlEmail($data);
        } else {
            $data['message'] = strip_tags($data['message']);
        }

        if ($phpmailer_enabled && $smtp_host != "") {
            require_once("$basepath/includes/phpmailer/class.phpmailer.php");
            $mail = new PHPMailer();

            $mail->IsSMTP();
            $mail->Host = $smtp_host;
			$mail->Port = $smtp_port; 
            $mail->SMTPAuth = $smtp_auth;
            $mail->CharSet = 'utf-8';
            $mail->Username = $smtp_user;
            $mail->Password = $smtp_pass;

            if ($data['important'] === true) {
                $mail->Priority = 1;
                $mail->AddCustomHeader("X-MSMail-Priority: High");
                $mail->AddCustomHeader("Importance: High");
            }

            $mail->SetFrom($data['from_email'], $data['from_name']);
            $mail->AddReplyTo($data['replyto_email']);
            $mail->Subject = $data['subject'];
            $mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
            $mail->IsHTML($send_html_emails);
            $mail->MsgHTML($data['message']);
            $mail->AddAddress($data['to_email']);

            $result = $mail->Send();

            return $result;
        } else {
            $result = mail("$data[to_name] <$data[to_email]>", $data['subject'], $data['message'], $data['headers']);

            return $result;
        }
    }
?>