1
0
Fork 0

Adapt existing classes to new structure and PSR-12

master
Anonymous Contributor 2023-09-05 01:09:01 +02:00
parent cc240f47a2
commit 686e991266
6 changed files with 363 additions and 370 deletions

View File

@ -1,4 +1,8 @@
<?php <?php
declare(strict_types=1);
namespace Mcp;
class FormValidator { class FormValidator {
private array $fieldValidation; private array $fieldValidation;

View File

@ -1,309 +1,271 @@
<?php <?php
class OpenSim declare(strict_types=1);
namespace Mcp;
use Exception;
use PDO;
class OpenSim
{
private PDO $pdo;
private $cache = array();
public function __construct(PDO $pdo)
{ {
public function isLoginValid($name, $password) $this->pdo = $pdo;
{ }
global $RUNTIME;
$nameParts = explode(" ", trim($name)); private function getUserNameFromGridData($userID, $table, $row): ?string
if(count($nameParts) != 2) { {
return false; $statementGridUser = $this->pdo->prepare('SELECT '.$row.' FROM '.$table.' WHERE '.$row.' LIKE ?');
} $statementGridUser->execute(array($userID.';%'));
$statementUser = $RUNTIME['PDO']->prepare("SELECT PrincipalID FROM UserAccounts WHERE FirstName = ? AND LastName = ? LIMIT 1"); while ($rowGridUser = $statementGridUser->fetch()) {
$statementUser->execute($nameParts); $userData = explode(";", $rowGridUser[$row]);
while($rowUser = $statementUser->fetch()) { if (count($userData) >= 3) {
$statementAuth = $RUNTIME['PDO']->prepare("SELECT passwordHash,passwordSalt FROM auth WHERE UUID = ? LIMIT 1"); $dbUserID = $userData[0];
$statementAuth->execute(array($rowUser['PrincipalID'])); $dbUserName = $userData[2];
if ($rowAuth = $statementAuth->fetch()) { $this->cache['USERNAME'][$userID] = $dbUserName;
return md5(md5($_POST['password']).":".$rowAuth['passwordSalt']) == $rowAuth['passwordHash'];
if ($dbUserID == $userID) {
return $dbUserName;
} }
} }
return false;
} }
public function getUserName($userID) return null;
{ }
global $RUNTIME;
if ($userID == "00000000-0000-0000-0000-000000000000") {
return "Unknown User";
}
if (isset($RUNTIME['CACHE']['USERNAME'][$userID])) {
return $RUNTIME['CACHE']['USERNAME'][$userID];
}
$statementUser = $RUNTIME['PDO']->prepare("SELECT FirstName,LastName FROM UserAccounts WHERE PrincipalID = ?");
$statementUser->execute(array($userID));
if ($rowUser = $statementUser->fetch()) {
$RUNTIME['CACHE']['USERNAME'][$userID] = $rowUser['FirstName']." ".$rowUser['LastName'];
return $rowUser['FirstName']." ".$rowUser['LastName'];
}
$statementGridUser = $RUNTIME['PDO']->prepare("SELECT UserID FROM GridUser");
$statementGridUser->execute();
while ($rowGridUser = $statementGridUser->fetch()) {
$userData = explode(";", $rowGridUser['UserID']);
if (count($userData) >= 3) {
$dbUserID = $userData[0];
$dbUserName = $userData[2];
$RUNTIME['CACHE']['USERNAME'][$userID] = $dbUserName;
if ($dbUserID == $userID) {
return $dbUserName;
}
}
}
$statementFriends = $RUNTIME['PDO']->prepare("SELECT PrincipalID FROM Friends");
$statementFriends->execute();
while ($rowFriends = $statementFriends->fetch()) {
$userData = explode(";", $rowFriends['PrincipalID']);
if (count($userData) == 4) {
$dbUserID = $userData[0];
$dbUserName = $userData[2];
$RUNTIME['CACHE']['USERNAME'][$userID] = $dbUserName;
if ($dbUserID == $userID) {
return $dbUserName;
}
}
}
public function getUserName($userID): string
{
if ($userID == "00000000-0000-0000-0000-000000000000") {
return "Unknown User"; return "Unknown User";
} }
public function getUserUUID($userName) if (isset($this->cache['USERNAME'][$userID])) {
{ return $this->cache['USERNAME'][$userID];
global $RUNTIME;
$statementUser = $RUNTIME['PDO']->prepare("SELECT PrincipalID,FirstName,LastName FROM UserAccounts");
$statementUser->execute();
while ($rowUser = $statementUser->fetch()) {
$sqlUserName = $rowUser['FirstName']." ".$rowUser['LastName'];
if ($sqlUserName == $userName) {
return $rowUser['PrincipalID'];
}
}
return null;
} }
public function getRegionName($regionID) $statementUser = $this->pdo->prepare('SELECT FirstName,LastName FROM UserAccounts WHERE PrincipalID = ?');
{ $statementUser->execute(array($userID));
global $RUNTIME;
$statementRegion = $RUNTIME['PDO']->prepare("SELECT regionName FROM regions WHERE uuid = ?"); if ($rowUser = $statementUser->fetch()) {
$statementRegion->execute(array($regionID)); $this->cache['USERNAME'][$userID] = $rowUser['FirstName'].' '.$rowUser['LastName'];
return $this->cache['USERNAME'][$userID];
if ($rowRegion = $statementRegion->fetch()) {
return $rowRegion['regionName'];
}
return "Unknown Region";
} }
public function getPartner($userID) $res = $this->getUserNameFromGridData($this->pdo, $userID, 'GridUser', 'UserID');
{ if ($res == null) {
global $RUNTIME; $res = $this->getUserNameFromGridData($this->pdo, $userID, 'Friends', 'PrincipalID');
$statement = $RUNTIME['PDO']->prepare("SELECT profilePartner FROM userprofile WHERE useruuid = ?");
$statement->execute(array($userID));
while ($row = $statement->fetch()) {
if ($row['profilePartner'] != "00000000-0000-0000-0000-000000000000") {
return $row['profilePartner'];
}
}
return null;
} }
public function allowOfflineIM($userID) return $res == null ? "Unknown User" : $res;
{ }
global $RUNTIME;
$statement = $RUNTIME['PDO']->prepare("SELECT imviaemail FROM usersettings WHERE useruuid = ?"); public function getUserUUID($userName): ?string
$statement->execute(array($userID)); {
$statementUser = $this->pdo->prepare('SELECT PrincipalID,FirstName,LastName FROM UserAccounts WHERE FirstName = ? AND LastName = ?');
$statementUser->execute(explode(' ', $userName));
if ($row = $statement->fetch()) { if ($rowUser = $statementUser->fetch()) {
return strtoupper($row['imviaemail']); return $rowUser['PrincipalID'];
}
return "FALSE";
} }
public function getUserMail($userID) return null;
{ }
global $RUNTIME;
$statement = $RUNTIME['PDO']->prepare("SELECT Email FROM UserAccounts WHERE PrincipalID = ?"); public function getRegionName($regionID): string
$statement->execute(array($userID)); {
$statementRegion = $this->pdo->prepare("SELECT regionName FROM regions WHERE uuid = ?");
$statementRegion->execute(array($regionID));
if ($row = $statement->fetch()) { if ($rowRegion = $statementRegion->fetch()) {
return $row['Email']; return $rowRegion['regionName'];
}
return "";
} }
public function getUserCount() return "Unknown Region";
{ }
global $RUNTIME;
$statementUser = $RUNTIME['PDO']->prepare("SELECT COUNT(*) FROM UserAccounts"); public function getPartner($userID): string
$statementUser->execute(); {
return $statementUser->fetchColumn(); $statement = $this->pdo->prepare("SELECT profilePartner FROM userprofile WHERE useruuid = ?");
} $statement->execute(array($userID));
public function getRegionCount() while ($row = $statement->fetch()) {
{ if ($row['profilePartner'] != "00000000-0000-0000-0000-000000000000") {
global $RUNTIME; return $row['profilePartner'];
$statementUser = $RUNTIME['PDO']->prepare("SELECT COUNT(*) FROM regions");
$statementUser->execute();
return $statementUser->fetchColumn();
}
public function getOnlineCount()
{
global $RUNTIME;
$statementUser = $RUNTIME['PDO']->prepare("SELECT COUNT(*) FROM Presence");
$statementUser->execute();
return $statementUser->fetchColumn();
}
public function deleteUser($uuid): bool
{
global $RUNTIME;
try {
$RUNTIME['PDO']->beginTransaction();
$statementAuth = $RUNTIME['PDO']->prepare('DELETE FROM auth WHERE UUID = ?');
$statementAuth->execute([$uuid]);
$statementAgentPrefs = $RUNTIME['PDO']->prepare('DELETE FROM AgentPrefs WHERE PrincipalID = ?');
$statementAgentPrefs->execute([$uuid]);
$statementAvatars = $RUNTIME['PDO']->prepare('DELETE FROM Avatars WHERE PrincipalID = ?');
$statementAvatars->execute([$uuid]);
$statementGridUser = $RUNTIME['PDO']->prepare('DELETE FROM GridUser WHERE UserID = ?');
$statementGridUser->execute([$uuid]);
$statementEstateUser = $RUNTIME['PDO']->prepare('DELETE FROM estate_users WHERE uuid = ?');
$statementEstateUser->execute([$uuid]);
$statementEstateBan = $RUNTIME['PDO']->prepare('DELETE FROM estateban WHERE bannedUUID = ?');
$statementEstateBan->execute([$uuid]);
$statementHgTraveling = $RUNTIME['PDO']->prepare('DELETE FROM hg_traveling_data WHERE UserID = ?');
$statementHgTraveling->execute([$uuid]);
$statementUserIdentitys = $RUNTIME['PDO']->prepare('DELETE FROM UserIdentitys WHERE PrincipalID = ?');
$statementUserIdentitys->execute([$uuid]);
$statementFriends = $RUNTIME['PDO']->prepare('DELETE FROM Friends WHERE PrincipalID = ? OR Friend = ?');
$statementFriends->execute([$uuid, $uuid]);
$statementImOffline = $RUNTIME['PDO']->prepare('DELETE FROM im_offline WHERE PrincipalID = ?');
$statementImOffline->execute([$uuid]);
$statementInventoryFolders = $RUNTIME['PDO']->prepare('DELETE FROM inventoryfolders WHERE agentID = ?');
$statementInventoryFolders->execute([$uuid]);
$statementInventoryItems = $RUNTIME['PDO']->prepare('DELETE FROM inventoryitems WHERE avatarID = ?');
$statementInventoryItems->execute([$uuid]);
$statementGroupMembership = $RUNTIME['PDO']->prepare('DELETE FROM os_groups_membership WHERE PrincipalID = ?');
$statementGroupMembership->execute([$uuid]);
$statementGroupRoles = $RUNTIME['PDO']->prepare('DELETE FROM os_groups_rolemembership WHERE PrincipalID = ?');
$statementGroupRoles->execute([$uuid]);
$statementGroupRoles = $RUNTIME['PDO']->prepare('DELETE FROM Presence WHERE UserID = ?');
$statementGroupRoles->execute([$uuid]);
$statementMute = $RUNTIME['PDO']->prepare('DELETE FROM MuteList WHERE AgentID = ? OR MuteID = ?');
$statementMute->execute([$uuid, $uuid]);
$statementUserAccounts = $RUNTIME['PDO']->prepare('DELETE FROM UserAccounts WHERE PrincipalID = ?');
$statementUserAccounts->execute([$uuid]);
$statementUserData = $RUNTIME['PDO']->prepare('DELETE FROM userdata WHERE UserId = ?');
$statementUserData->execute([$uuid]);
$statementUserNotes = $RUNTIME['PDO']->prepare('DELETE FROM usernotes WHERE targetuuid = ?');
$statementUserNotes->execute([$uuid]);
$statementUserProfile = $RUNTIME['PDO']->prepare('DELETE FROM userprofile WHERE useruuid = ?');
$statementUserProfile->execute([$uuid]);
$statementUserSettings = $RUNTIME['PDO']->prepare('DELETE FROM usersettings WHERE useruuid = ?');
$statementUserSettings->execute([$uuid]);
$RUNTIME['PDO']->commit();
return true;
} catch (Exception $pdoException) {
$RUNTIME['PDO']->rollBack();
error_log('Could not delete account '.$uuid.': '.$pdoException->getMessage());
return false;
} }
} }
public function deleteIdentity($uuid, $identId): bool return '';
{ }
global $RUNTIME;
$statementValidate = $RUNTIME['PDO']->prepare('SELECT 1 FROM UserIdentitys WHERE PrincipalID = ? AND IdentityID = ?'); public function allowOfflineIM($userID): string
$statementValidate->execute([$uuid, $identId]); {
$statement = $this->pdo->prepare("SELECT imviaemail FROM usersettings WHERE useruuid = ?");
$statement->execute(array($userID));
if($statementValidate->fetch()) { if ($row = $statement->fetch()) {
$statementDelete = $RUNTIME['PDO']->prepare('DELETE FROM UserAccounts WHERE PrincipalID = ?'); return strtoupper($row['imviaemail']);
$statementDelete->execute([$identId]); }
return true; return "FALSE";
} }
public function getUserMail($userID): string
{
$statement = $this->pdo->prepare("SELECT Email FROM UserAccounts WHERE PrincipalID = ?");
$statement->execute(array($userID));
if ($row = $statement->fetch()) {
return $row['Email'];
}
return "";
}
private function getEntryCount($table): int
{
$statementCount = $this->pdo->prepare('SELECT COUNT(*) AS Count FROM '.$table);
$statementCount->execute();
if ($row = $statementCount->fetch()) {
return $row['Count'];
}
return 0;
}
public function getUserCount(): int
{
return $this->getEntryCount('UserAccounts');
}
public function getRegionCount(): int
{
return $this->getEntryCount('regions');
}
public function getOnlineCount(): int
{
return $this->getEntryCount('Presence');
}
public function deleteUser($uuid): bool
{
try {
$this->pdo->beginTransaction();
$statementAuth = $this->pdo->prepare('DELETE FROM auth WHERE UUID = ?');
$statementAuth->execute([$uuid]);
$statementAgentPrefs = $this->pdo->prepare('DELETE FROM AgentPrefs WHERE PrincipalID = ?');
$statementAgentPrefs->execute([$uuid]);
$statementAvatars = $this->pdo->prepare('DELETE FROM Avatars WHERE PrincipalID = ?');
$statementAvatars->execute([$uuid]);
$statementGridUser = $this->pdo->prepare('DELETE FROM GridUser WHERE UserID = ?');
$statementGridUser->execute([$uuid]);
$statementEstateUser = $this->pdo->prepare('DELETE FROM estate_users WHERE uuid = ?');
$statementEstateUser->execute([$uuid]);
$statementEstateBan = $this->pdo->prepare('DELETE FROM estateban WHERE bannedUUID = ?');
$statementEstateBan->execute([$uuid]);
$statementHgTraveling = $this->pdo->prepare('DELETE FROM hg_traveling_data WHERE UserID = ?');
$statementHgTraveling->execute([$uuid]);
$statementUserIdentitys = $this->pdo->prepare('DELETE FROM UserIdentitys WHERE PrincipalID = ?');
$statementUserIdentitys->execute([$uuid]);
$statementFriends = $this->pdo->prepare('DELETE FROM Friends WHERE PrincipalID = ? OR Friend = ?');
$statementFriends->execute([$uuid, $uuid]);
$statementImOffline = $this->pdo->prepare('DELETE FROM im_offline WHERE PrincipalID = ?');
$statementImOffline->execute([$uuid]);
$statementInventoryFolders = $this->pdo->prepare('DELETE FROM inventoryfolders WHERE agentID = ?');
$statementInventoryFolders->execute([$uuid]);
$statementInventoryItems = $this->pdo->prepare('DELETE FROM inventoryitems WHERE avatarID = ?');
$statementInventoryItems->execute([$uuid]);
$statementGroupMembership = $this->pdo->prepare('DELETE FROM os_groups_membership WHERE PrincipalID = ?');
$statementGroupMembership->execute([$uuid]);
$statementGroupRoles = $this->pdo->prepare('DELETE FROM os_groups_rolemembership WHERE PrincipalID = ?');
$statementGroupRoles->execute([$uuid]);
$statementGroupRoles = $this->pdo->prepare('DELETE FROM Presence WHERE UserID = ?');
$statementGroupRoles->execute([$uuid]);
$statementMute = $this->pdo->prepare('DELETE FROM MuteList WHERE AgentID = ? OR MuteID = ?');
$statementMute->execute([$uuid, $uuid]);
$statementUserAccounts = $this->pdo->prepare('DELETE FROM UserAccounts WHERE PrincipalID = ?');
$statementUserAccounts->execute([$uuid]);
$statementUserData = $this->pdo->prepare('DELETE FROM userdata WHERE UserId = ?');
$statementUserData->execute([$uuid]);
$statementUserNotes = $this->pdo->prepare('DELETE FROM usernotes WHERE targetuuid = ?');
$statementUserNotes->execute([$uuid]);
$statementUserProfile = $this->pdo->prepare('DELETE FROM userprofile WHERE useruuid = ?');
$statementUserProfile->execute([$uuid]);
$statementUserSettings = $this->pdo->prepare('DELETE FROM usersettings WHERE useruuid = ?');
$statementUserSettings->execute([$uuid]);
$this->pdo->commit();
return true;
} catch (Exception $pdoException) {
$this->pdo->rollBack();
error_log('Could not delete account '.$uuid.': '.$pdoException->getMessage());
return false; return false;
} }
public function gen_uuid()
{
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
// 16 bits for "time_mid"
mt_rand( 0, 0xffff ),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand( 0, 0x0fff ) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand( 0, 0x3fff ) | 0x8000,
// 48 bits for "node"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
} }
public function deleteIdentity($uuid, $identId): bool
{
$statementValidate = $this->pdo->prepare('SELECT 1 FROM UserIdentitys WHERE PrincipalID = ? AND IdentityID = ?');
$statementValidate->execute([$uuid, $identId]);
if($statementValidate->fetch()) {
$statementDelete = $this->pdo->prepare('DELETE FROM UserAccounts WHERE PrincipalID = ?');
$statementDelete->execute([$identId]);
return true;
}
return false;
}
public function generateUuid(): string
{
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
// 16 bits for "time_mid"
mt_rand( 0, 0xffff ),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand( 0, 0x0fff ) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand( 0, 0x3fff ) | 0x8000,
// 48 bits for "node"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
}

View File

@ -1,5 +1,11 @@
<?php <?php
function sendMessageToWebhook($webhook, $title, $message) declare(strict_types=1);
namespace Mcp\Util;
class DiscordUtil
{
public static function sendMessageToWebhook($webhook, $title, $message): void
{ {
$rawMessage = file_get_contents("style/discordMessage.json"); $rawMessage = file_get_contents("style/discordMessage.json");
$rawMessage = str_replace("%%message%%", $message, $rawMessage); $rawMessage = str_replace("%%message%%", $message, $rawMessage);
@ -16,3 +22,4 @@
file_get_contents($webhook, false, stream_context_create($options)); file_get_contents($webhook, false, stream_context_create($options));
} }
}

62
app/util/SmtpClient.php Normal file
View File

@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace Mcp\Util;
use PHPMailer\PHPMailer\PHPMailer;
use Exception;
use Mcp\TemplateBuilder;
class SmtpClient
{
private PHPMailer $mailer;
public function __construct(string $host, int $port, string $username, string $password)
{
$mailer = new PHPMailer(true);
$mailer->isSMTP();
$mailer->Host = $host;
$mailer->Port = $port;
$mailer->Username = $username;
$mailer->Password = $password;
$mailer->SMTPAuth = true;
$mailer->SMTPSecure = $port == 465 ? PHPMailer::ENCRYPTION_SMTPS : PHPMailer::ENCRYPTION_STARTTLS;
$this->mailer = $mailer;
}
public function sendHtml(string $fromAddr, string $fromName, string $to, string $subject, TemplateBuilder $tpl): bool
{
try {
$this->mailer->setFrom($fromAddr, $fromName);
$this->mailer->addAddress($to);
} catch (Exception $e) {
error_log('Failed to prepare mail client (from: '.$fromAddr.', to: '.$to.')');
return false;
}
$this->mailer->isHTML(true);
$this->mailer->Subject = $subject;
ob_start();
$tpl->render();
$tplOut = ob_end_clean();
$this->mailer->Body = $tplOut;
$this->mailer->AltBody = $this::htmlToPlain($tplOut);
try {
$this->mailer->send();
return true;
} catch (Exception $e) {
error_log('Could not send email: '.$this->mailer->ErrorInfo);
return false;
}
}
private static function htmlToPlain($message): string
{
$messageNew = str_replace('<br/>', "\n", $message);
$messageNew = preg_replace('/<a href="(.*)">(.*)<\\/a>/', "$2: $1", $messageNew);
return $messageNew;
}
}

59
app/util/Util.php Normal file
View File

@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace Mcp\Util;
use Exception;
class Util
{
public static function fillString($string, $targetlength)
{
while(strlen($string) < $targetlength)
{
$string = "0".$string;
}
return $string;
}
public static function left($str, $length)
{
return substr($str, 0, $length);
}
public static function right($str, $length)
{
return substr($str, -$length);
}
public static function generateToken($length): string
{
$chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$res = "";
for($i = 0; $i < $length; $i++) {
$index = random_int(0, strlen($chars) - 1);
$res = $res.substr($chars, $index, 1);
}
return $res;
}
public static function getDataFromHTTP($url, $content = "", $requestTyp = "application/text")
{
try {
if ($content != "") {
return file_get_contents($url, true, stream_context_create(array('http' => array('header' => 'Content-type: '.$requestTyp, 'method' => 'POST', 'timeout' => 0.5, 'content' => $content))));
} else {
return file_get_contents($url);
}
} catch (Exception $e) {
echo "(HTTP REQUEST) error while conntect to remote server. : ".$url;
}
}
public static function sendInworldIM($fromUUID, $toUUID, $fromName, $targetURL, $text)
{
$rawXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?><methodCall><methodName>grid_instant_message</methodName><params><param><value><struct><member><name>position_x</name><value><string>0</string></value></member><member><name>position_y</name><value><string>0</string></value></member><member><name>position_z</name><value><string>0</string></value></member><member><name>to_agent_id</name><value><string>".$toUUID."</string></value></member><member><name>from_agent_session</name><value><string>00000000-0000-0000-0000-000000000000</string></value></member><member><name>im_session_id</name><value><string>".$fromUUID."</string></value></member><member><name>from_agent_name</name><value><string>".$fromName."</string></value></member><member><name>from_agent_id</name><value><string>".$fromUUID."</string></value></member><member><name>binary_bucket</name><value><string>AA==</string></value></member><member><name>region_handle</name><value><i4>0</i4></value></member><member><name>region_id</name><value><string>00000000-0000-0000-0000-000000000000</string></value></member><member><name>parent_estate_id</name><value><string>1</string></value></member><member><name>timestamp</name><value><string>".time()."</string></value></member><member><name>dialog</name><value><string>AA==</string></value></member><member><name>offline</name><value><string>AA==</string></value></member><member><name>from_group</name><value><string>FALSE</string></value></member><member><name>message</name><value><string>".$text."</string></value></member></struct></value></param></params></methodCall>";
Util::getDataFromHTTP($targetURL, $rawXML, "text/xml");
}
}

View File

@ -1,101 +0,0 @@
<?php
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
function fillString($string, $targetlength)
{
while(strlen($string) < $targetlength)
{
$string = "0".$string;
}
return $string;
}
function left($str, $length)
{
return substr($str, 0, $length);
}
function right($str, $length)
{
return substr($str, -$length);
}
function generateToken($length): string
{
$chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$res = "";
for($i = 0; $i < $length; $i++) {
$index = random_int(0, strlen($chars) - 1);
$res = $res.substr($chars, $index, 1);
}
return $res;
}
function htmlToPlain($message): string
{
$messageNew = str_replace('<br/>', "\n", $message);
$messageNew = preg_replace('/<a href="(.*)">(.*)<\\/a>/', "$2: $1", $messageNew);
return $messageNew;
}
function sendMail($email, $message, $subject, $title, $preheader): bool
{
include_once 'lib/phpmailer/Exception.php';
include_once 'lib/phpmailer/PHPMailer.php';
include_once 'lib/phpmailer/SMTP.php';
include_once 'app/HTML.php';
global $RUNTIME;
$mailer = new PHPMailer(true);
try {
$mailer->isSMTP();
$mailer->Host = $RUNTIME['SMTP']['SERVER'];
$mailer->Port = $RUNTIME['SMTP']['PORT'];
$mailer->Username = $RUNTIME['SMTP']['ADDRESS'];
$mailer->Password = $RUNTIME['SMTP']['PASS'];
$mailer->SMTPAuth = true;
$mailer->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mailer->setFrom($RUNTIME['SMTP']['ADDRESS'], $RUNTIME['SMTP']['NAME']);
$mailer->addAddress($email);
$mailer->isHTML(true);
$mailer->Subject = $subject;
$mailHtml = new HTML();
$mailHtml->importHTML("mail.html");
$mailHtml->setHTMLTitle($title);
$mailHtml->ReplaceLayoutInhalt('%%MESSAGE%%', $message);
$mailHtml->ReplaceLayoutInhalt('%%PREHEADER%%', $preheader);
$mailHtml->build();
$mailer->Body = $mailHtml->ausgabe();
$mailer->AltBody = htmlToPlain($message);
$mailer->send();
return true;
} catch(Exception $e) {
error_log('Could not send email: '.$mailer->ErrorInfo);
return false;
}
}
function getDataFromHTTP($url, $content = "", $requestTyp = "application/text")
{
try {
if ($content != "") {
return file_get_contents($url, true, stream_context_create(array('http' => array('header' => 'Content-type: '.$requestTyp, 'method' => 'POST', 'timeout' => 0.5, 'content' => $content))));
} else {
return file_get_contents($url);
}
} catch (Exception $e) {
echo "(HTTP REQUEST) error while conntect to remote server. : ".$url;
}
}
function sendInworldIM($fromUUID, $toUUID, $fromName, $targetURL, $text)
{
$rawXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?><methodCall><methodName>grid_instant_message</methodName><params><param><value><struct><member><name>position_x</name><value><string>0</string></value></member><member><name>position_y</name><value><string>0</string></value></member><member><name>position_z</name><value><string>0</string></value></member><member><name>to_agent_id</name><value><string>".$toUUID."</string></value></member><member><name>from_agent_session</name><value><string>00000000-0000-0000-0000-000000000000</string></value></member><member><name>im_session_id</name><value><string>".$fromUUID."</string></value></member><member><name>from_agent_name</name><value><string>".$fromName."</string></value></member><member><name>from_agent_id</name><value><string>".$fromUUID."</string></value></member><member><name>binary_bucket</name><value><string>AA==</string></value></member><member><name>region_handle</name><value><i4>0</i4></value></member><member><name>region_id</name><value><string>00000000-0000-0000-0000-000000000000</string></value></member><member><name>parent_estate_id</name><value><string>1</string></value></member><member><name>timestamp</name><value><string>".time()."</string></value></member><member><name>dialog</name><value><string>AA==</string></value></member><member><name>offline</name><value><string>AA==</string></value></member><member><name>from_group</name><value><string>FALSE</string></value></member><member><name>message</name><value><string>".$text."</string></value></member></struct></value></param></params></methodCall>";
getDataFromHTTP($targetURL, $rawXML, "text/xml");
}