HEX
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.30
System: Linux iZj6c1151k3ad370bosnmsZ 3.10.0-1160.76.1.el7.x86_64 #1 SMP Wed Aug 10 16:21:17 UTC 2022 x86_64
User: root (0)
PHP: 7.4.30
Disabled: NONE
Upload Files
File: /var/www/html/inventory.breadsecret.com/class/Controller/template.php
<?php
namespace Controller;

use Responses\Message, Responses\Action, Responses\Data;
use Database\Sql;
use Pages\FormPage;
use Routing\Route;
use Utility\WebSystem;
use Utility\Email; // Temporary for test Email

class template {

	public static function find($id, $fetchMode=\PDO::FETCH_OBJ) {
		$sql = Sql::select("emailTemplate")->where(['id', '=', $id]);
		$stm = $sql->prepare();
		$stm->execute();
		$obj = $stm->fetch($fetchMode);
		if ($obj === false) return null;
		return $obj;
	}

	public static function findName($name, $fetchMode=\PDO::FETCH_OBJ) {
		$sql = Sql::select("emailTemplate")->where(['name', '=', "?"]);
		$stm = $sql->prepare();
		$stm->execute([$name]);
		$obj = $stm->fetch($fetchMode);
		if ($obj === false) return null;
		return $obj;
	}

	public static function replaceVar($template, array $var = []) {
		$tpl = $template->content;
		foreach ($var as $name => $value) {
			$tpl = str_replace('{{'.$name.'}}', $value, $tpl);
		}
		return $tpl;
	}
	
	public function form($request) {
		if (!user::checklogin()) return new Action('redirect', WebSystem::path(Route::getRouteByName('page.login')->path(), false, false));

		$sqlTemp = Sql::select("emailTemplate")->where(['status', '=', 1]);
		$stmTemp = $sqlTemp->prepare();
		$stmTemp->execute();

		$obj = self::findName($request->get->template, \PDO::FETCH_NAMED);
		$stm = null;
		if (!is_null($obj)) {
			$sql = Sql::select("emailTemplateVariable")->where(['templateID', '=', "?"]);
			$stm = $sql->prepare();
			$stm->execute([$obj['id']]);
		}
		return new FormPage('template/form', $obj, ['stmTemp' => $stmTemp, 'stmVar' => $stm]);
	}

	public function emailform($request) {
		if (!user::checklogin()) return new Action('redirect', WebSystem::path(Route::getRouteByName('page.login')->path(), false, false));

		return new FormPage('template/emailform', null);
	}	
	
	public function save($request) {
		if (!isset($_SERVER['HTTP_PROGRAM']) && !user::checklogin()) return new Action('redirect', WebSystem::path(Route::getRouteByName('page.login')->path(), false, false));

		$userObj = unserialize($_SESSION['user']);

		$editFields = ['subject' => "?", 'content' => "?", 'modifyDate' => 'NOW()', 'modifyBy' => $userObj->id];
		$editValues = [$request->post->subject, $request->post->content];

		$sql = Sql::update('emailTemplate')->setFieldValue($editFields)->where(['id', '=', $request->post->id]);
		if ($sql->prepare()->execute($editValues)) {
			return new Message('info', 'Template Saved');
		} else {
			return new Message('alert', 'Save Failed');
		}
	}

	public function saveEmail($request) {
		if (!isset($_SERVER['HTTP_PROGRAM']) && !user::checklogin()) return new Action('redirect', WebSystem::path(Route::getRouteByName('page.login')->path(), false, false));

		$userObj = unserialize($_SESSION['user']);

		if (count($request->post->emailAddr)) {
			db()->exec("TRUNCATE TABLE `warehouseEmailAddr`;");
			$stmIns = Sql::insert('warehouseEmailAddr')->setFieldValue(['emailAddr' => '?'])->prepare();
			foreach ($request->post->emailAddr as $email) {
				if (empty($email)) continue;
				$stmIns->execute([$email]);
			}
			return new Message('info', 'Email Address Saved');
		}

		return new Message('alert', 'No Email Address Inserted');
	}

	public function sendEmail($request) {
/*
echo "<pre>";
		$mail = new Email('Mira Place - 福願春鳴 Spring Blessings');
		$mail->addAddress('hippoho2005@gmail.com', 'Email');
		$mail->addAddress('samieltsang@hotmail.com', 'Email');
		$mail->setHTMLBody('<p>Test Subject Final 福願春鳴</p>');
echo $mail->mailer->Subject.PHP_EOL;

		try {
			$mail->send();
		} catch (Exception $e) {
			echo "Send Fail";
		}
*/

//Create a new SMTP instance
$smtp = new \PHPMailer\PHPMailer\SMTP();

//Enable connection-level debug output
$smtp->do_debug = \PHPMailer\PHPMailer\SMTP::DEBUG_CONNECTION;

try {
    //Connect to an SMTP server
    if (!$smtp->connect('smtp.office365.com', 587)) {
        throw new \Exception('Connect failed');
    }
    //Say hello
    if (!$smtp->hello(gethostname())) {
        throw new \Exception('EHLO failed: ' . $smtp->getError()['error']);
    }
    //Get the list of ESMTP services the server offers
    $e = $smtp->getServerExtList();
    //If server can do TLS encryption, use it
    if (is_array($e) && array_key_exists('STARTTLS', $e)) {
        $tlsok = $smtp->startTLS();
        if (!$tlsok) {
            throw new \Exception('Failed to start encryption: ' . $smtp->getError()['error']);
        }
        //Repeat EHLO after STARTTLS
        if (!$smtp->hello(gethostname())) {
            throw new \Exception('EHLO (2) failed: ' . $smtp->getError()['error']);
        }
        //Get new capabilities list, which will usually now include AUTH if it didn't before
        $e = $smtp->getServerExtList();
    }
    //If server supports authentication, do it (even if no encryption)
    if (is_array($e) && array_key_exists('AUTH', $e)) {
        if ($smtp->authenticate('oday_noreply@hkm.salvationarmy.org', '$Army#20220915')) {
            echo 'Connected ok!';
        } else {
            throw new \Exception('Authentication failed: ' . $smtp->getError()['error']);
        }
    }
} catch (\Exception $e) {
    echo 'SMTP error: ' . $e->getMessage(), "\n";
}
//Whatever happened, close the connection.
$smtp->quit();

		echo "Send is Done";
		return null;
	}
}