|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ACore\Components\MailReturnMenu; |
| 4 | + |
| 5 | +use ACore\Manager\ACoreServices; |
| 6 | +use ACore\Manager\Opts; |
| 7 | +use ACore\Components\MailReturnMenu\MailReturnView; |
| 8 | +use InvalidArgumentException; |
| 9 | + |
| 10 | +class MailReturnController |
| 11 | +{ |
| 12 | + private $view; |
| 13 | + |
| 14 | + public function __construct() |
| 15 | + { |
| 16 | + $this->view = new MailReturnView($this); |
| 17 | + } |
| 18 | + |
| 19 | + public function renderCharacters() |
| 20 | + { |
| 21 | + echo $this->view->getMailReturnRender(self::getCharactersByAcId()); |
| 22 | + } |
| 23 | + |
| 24 | + private static function validateAccountId() |
| 25 | + { |
| 26 | + $accId = ACoreServices::I()->getAcoreAccountId(); |
| 27 | + |
| 28 | + if (!isset($accId) || $accId === null || $accId === '' || trim($accId) === '' || !is_numeric($accId)) { |
| 29 | + throw new InvalidArgumentException("Invalid user account ID provided."); |
| 30 | + } |
| 31 | + |
| 32 | + return intval($accId); |
| 33 | + } |
| 34 | + |
| 35 | + private static function validateCharacterOwnership($conn, $charGuid, $accId) |
| 36 | + { |
| 37 | + $stmt = $conn->prepare( |
| 38 | + "SELECT `guid`, `name` FROM `characters` |
| 39 | + WHERE `guid` = ? AND `account` = ? AND `deleteDate` IS NULL" |
| 40 | + ); |
| 41 | + $stmt->bindValue(1, $charGuid); |
| 42 | + $stmt->bindValue(2, $accId); |
| 43 | + $result = $stmt->executeQuery(); |
| 44 | + $character = $result->fetchAssociative(); |
| 45 | + |
| 46 | + if (!$character) { |
| 47 | + throw new InvalidArgumentException("Character not found"); |
| 48 | + } |
| 49 | + |
| 50 | + return $character; |
| 51 | + } |
| 52 | + |
| 53 | + public static function getSentUnreadMails($charGuid) |
| 54 | + { |
| 55 | + if (!is_numeric($charGuid)) { |
| 56 | + throw new InvalidArgumentException("Invalid parameters"); |
| 57 | + } |
| 58 | + $charGuid = intval($charGuid); |
| 59 | + |
| 60 | + $accId = self::validateAccountId(); |
| 61 | + $conn = ACoreServices::I()->getCharacterEm()->getConnection(); |
| 62 | + |
| 63 | + self::validateCharacterOwnership($conn, $charGuid, $accId); |
| 64 | + |
| 65 | + // Get mails sent by this character that are unread by the receiver |
| 66 | + // messageType = 0 means player mail |
| 67 | + $query = "SELECT m.`id`, m.`subject`, m.`has_items`, m.`money`, |
| 68 | + m.`expire_time`, m.`deliver_time`, |
| 69 | + rc.`name` AS receiver_name, rc.`race` AS receiver_race, |
| 70 | + rc.`class` AS receiver_class, rc.`gender` AS receiver_gender, |
| 71 | + rc.`level` AS receiver_level |
| 72 | + FROM `mail` m |
| 73 | + JOIN `characters` rc ON m.`receiver` = rc.`guid` |
| 74 | + WHERE m.`sender` = ? |
| 75 | + AND m.`messageType` = 0 |
| 76 | + AND (m.`checked` & 1) = 0 |
| 77 | + ORDER BY m.`deliver_time` DESC"; |
| 78 | + |
| 79 | + $stmt = $conn->prepare($query); |
| 80 | + $stmt->bindValue(1, $charGuid); |
| 81 | + $result = $stmt->executeQuery(); |
| 82 | + $mails = $result->fetchAllAssociative(); |
| 83 | + |
| 84 | + // Fetch items for mails that have items |
| 85 | + $mailIds = []; |
| 86 | + foreach ($mails as $mail) { |
| 87 | + if ($mail['has_items'] == 1) { |
| 88 | + $mailIds[] = $mail['id']; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + $itemsByMail = []; |
| 93 | + if (!empty($mailIds)) { |
| 94 | + $placeholders = implode(',', array_fill(0, count($mailIds), '?')); |
| 95 | + $worldDb = Opts::I()->acore_db_world_name; |
| 96 | + $itemQuery = "SELECT mi.`mail_id`, ii.`itemEntry`, ii.`count`, |
| 97 | + it.`name` AS item_name |
| 98 | + FROM `mail_items` mi |
| 99 | + JOIN `item_instance` ii ON mi.`item_guid` = ii.`guid` |
| 100 | + JOIN `$worldDb`.`item_template` it ON ii.`itemEntry` = it.`entry` |
| 101 | + WHERE mi.`mail_id` IN ($placeholders)"; |
| 102 | + $stmt = $conn->prepare($itemQuery); |
| 103 | + $i = 1; |
| 104 | + foreach ($mailIds as $id) { |
| 105 | + $stmt->bindValue($i++, $id); |
| 106 | + } |
| 107 | + $itemResult = $stmt->executeQuery(); |
| 108 | + foreach ($itemResult->fetchAllAssociative() as $item) { |
| 109 | + $itemsByMail[$item['mail_id']][] = $item; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Attach items to their mails |
| 114 | + foreach ($mails as &$mail) { |
| 115 | + $mail['items'] = $itemsByMail[$mail['id']] ?? []; |
| 116 | + } |
| 117 | + |
| 118 | + return $mails; |
| 119 | + } |
| 120 | + |
| 121 | + public static function returnMail($charGuid, $mailId) |
| 122 | + { |
| 123 | + $accId = self::validateAccountId(); |
| 124 | + $conn = ACoreServices::I()->getCharacterEm()->getConnection(); |
| 125 | + |
| 126 | + // Validate charGuid and mailId are integers to prevent injection |
| 127 | + if (!is_numeric($charGuid) || !is_numeric($mailId)) { |
| 128 | + throw new InvalidArgumentException("Invalid parameters"); |
| 129 | + } |
| 130 | + $charGuid = intval($charGuid); |
| 131 | + $mailId = intval($mailId); |
| 132 | + |
| 133 | + // Verify the sender character belongs to the current user |
| 134 | + $sender = self::validateCharacterOwnership($conn, $charGuid, $accId); |
| 135 | + |
| 136 | + // Verify the mail was sent by this character and is still unread |
| 137 | + $mailQuery = "SELECT m.`id`, m.`receiver` |
| 138 | + FROM `mail` m |
| 139 | + WHERE m.`id` = ? AND m.`sender` = ? AND m.`messageType` = 0 AND (m.`checked` & 1) = 0"; |
| 140 | + $stmt = $conn->prepare($mailQuery); |
| 141 | + $stmt->bindValue(1, $mailId); |
| 142 | + $stmt->bindValue(2, $charGuid); |
| 143 | + $mailResult = $stmt->executeQuery(); |
| 144 | + $mail = $mailResult->fetchAssociative(); |
| 145 | + |
| 146 | + if (!$mail) { |
| 147 | + throw new InvalidArgumentException("Mail not found or already read"); |
| 148 | + } |
| 149 | + |
| 150 | + // Verify the receiver character actually exists and has this mail |
| 151 | + $receiverQuery = "SELECT `guid`, `name` FROM `characters` WHERE `guid` = ? AND `deleteDate` IS NULL"; |
| 152 | + $stmt = $conn->prepare($receiverQuery); |
| 153 | + $stmt->bindValue(1, $mail['receiver']); |
| 154 | + $receiverResult = $stmt->executeQuery(); |
| 155 | + $receiver = $receiverResult->fetchAssociative(); |
| 156 | + |
| 157 | + if (!$receiver) { |
| 158 | + throw new InvalidArgumentException("Receiver character not found"); |
| 159 | + } |
| 160 | + |
| 161 | + // Use the receiver name from the database, not from user input |
| 162 | + $receiverName = $receiver['name']; |
| 163 | + |
| 164 | + // Execute SOAP command to return the mail |
| 165 | + $soap = ACoreServices::I()->getGameMailSoap(); |
| 166 | + $result = $soap->executeCommand(".mail return $receiverName $mailId"); |
| 167 | + |
| 168 | + return "Mail #$mailId returned successfully"; |
| 169 | + } |
| 170 | + |
| 171 | + public static function getCharactersByAcId() |
| 172 | + { |
| 173 | + $accId = self::validateAccountId(); |
| 174 | + |
| 175 | + $query = "SELECT |
| 176 | + c.`guid`, c.`name`, c.`order`, c.`race`, c.`class`, c.`level`, c.`gender` |
| 177 | + FROM `characters` c |
| 178 | + WHERE c.`deleteDate` IS NULL |
| 179 | + AND c.`account` = ? |
| 180 | + ORDER BY COALESCE(c.`order`, c.`guid`) |
| 181 | + "; |
| 182 | + $conn = ACoreServices::I()->getCharacterEm()->getConnection(); |
| 183 | + $stmt = $conn->prepare($query); |
| 184 | + $stmt->bindValue(1, $accId); |
| 185 | + $result = $stmt->executeQuery(); |
| 186 | + return $result->fetchAllAssociative(); |
| 187 | + } |
| 188 | +} |
0 commit comments