1: <?php
2:
3: namespace Budabot\User\Modules;
4:
5: use stdClass;
6: use DOMDocument;
7:
8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:
22: class AOUController {
23:
24: 25: 26: 27:
28: public $moduleName;
29:
30:
31: public $text;
32:
33:
34: public $itemsController;
35:
36:
37: public $http;
38:
39: const AOU_URL = "https://www.ao-universe.com/mobile/parser.php?bot=budabot";
40:
41: 42: 43: 44: 45: 46:
47: public function aouView($message, $channel, $sender, $sendto, $args) {
48: $guideid = $args[1];
49:
50: $params = array(
51: 'mode' => 'view',
52: 'id' => $guideid
53: );
54: $guide = $this->http->get(self::AOU_URL)->withQueryParams($params)->waitAndReturnResponse()->body;
55:
56: $dom = new DOMDocument;
57: $dom->loadXML($guide);
58:
59: if ($dom->getElementsByTagName('error')->length > 0) {
60: $msg = "An error occurred while trying to retrieve AOU guide with id <highlight>$guideid<end>: " .
61: $dom->getElementsByTagName('text')->item(0)->nodeValue;
62: $sendto->reply($msg);
63: return;
64: }
65:
66: $content = $dom->getElementsByTagName('content')->item(0);
67: if ($content == null) {
68: $msg = "Error retrieving guide <highlight>$guideid<end> from AO-Universe.com";
69: } else {
70: $title = $content->getElementsByTagName('name')->item(0)->nodeValue;
71:
72: $blob = '';
73: $blob .= $this->text->makeChatcmd("Guide on AO-Universe.com", "/start http://www.ao-universe.com/main.php?site=knowledge&id={$guideid}") . "\n\n";
74:
75: $blob .= "Update: <highlight>" . $content->getElementsByTagName('update')->item(0)->nodeValue . "<end>\n";
76: $blob .= "Profession: <highlight>" . $content->getElementsByTagName('class')->item(0)->nodeValue . "<end>\n";
77: $blob .= "Faction: <highlight>" . $content->getElementsByTagName('faction')->item(0)->nodeValue . "<end>\n";
78: $blob .= "Level: <highlight>" . $content->getElementsByTagName('level')->item(0)->nodeValue . "<end>\n";
79: $blob .= "Author: <highlight>" . $this->processInput($content->getElementsByTagName('author')->item(0)->nodeValue) . "<end>\n\n";
80:
81: $blob .= $this->processInput($content->getElementsByTagName('text')->item(0)->nodeValue);
82:
83: $blob .= "\n\n<highlight>Powered by<end> " . $this->text->makeChatcmd("AO-Universe.com", "/start http://www.ao-universe.com");
84:
85: $msg = $this->text->makeBlob($title, $blob);
86: }
87: $sendto->reply($msg);
88: }
89:
90: 91: 92: 93: 94: 95:
96: public function aouAllSearch($message, $channel, $sender, $sendto, $args) {
97: $search = $args[1];
98:
99: $msg = $this->searchForAOUGuide($search, true);
100: $sendto->reply($msg);
101: }
102:
103: 104: 105: 106: 107: 108:
109: public function aouSearch($message, $channel, $sender, $sendto, $args) {
110: $search = $args[1];
111:
112: $msg = $this->searchForAOUGuide($search, false);
113: $sendto->reply($msg);
114: }
115:
116: public function searchForAOUGuide($search, $searchGuideText) {
117: $searchTerms = explode(" ", $search);
118:
119: $params = array(
120: 'mode' => 'search',
121: 'search' => $search
122: );
123: $results = $this->http->get(self::AOU_URL)->withQueryParams($params)->waitAndReturnResponse()->body;
124:
125: $dom = new DOMDocument;
126: $dom->loadXML($results);
127:
128: $sections = $dom->getElementsByTagName('section');
129: $blob = '';
130: $count = 0;
131: forEach ($sections as $section) {
132: $category = $this->getSearchResultCategory($section);
133:
134: $guides = $section->getElementsByTagName('guide');
135: $tempBlob = '';
136: $found = false;
137: forEach ($guides as $guide) {
138: $guideObj = $this->getGuideObject($guide);
139:
140:
141: if ($searchGuideText || $this->striposarray($category . ' ' . $guideObj->name . ' ' . $guideObj->description, $searchTerms)) {
142: $count++;
143: $tempBlob .= ' ' . $this->text->makeChatcmd("$guideObj->name", "/tell <myname> aou $guideObj->id") . " - " . $guideObj->description . "\n";
144: $found = true;
145: }
146: }
147:
148: if ($found) {
149: $blob .= "<pagebreak><header2>" . $category . "<end>\n";
150: $blob .= $tempBlob;
151: $blob .= "\n";
152: }
153: }
154:
155: $blob .= "\n<highlight>Powered by<end> " . $this->text->makeChatcmd("AO-Universe.com", "/start http://www.ao-universe.com");
156:
157: if ($count > 0) {
158: if ($searchGuideText) {
159: $title = "All AO-U Guides containing '$search' ($count)";
160: } else {
161: $title = "AO-U Guides containing '$search' ($count)";
162: }
163: $msg = $this->text->makeBlob($title, $blob);
164: } else {
165: $msg = "Could not find any guides containing: '$search'.";
166: if (!$searchGuideText) {
167: $msg .= " Try including all results with <highlight>!aou all $search<end>.";
168: }
169: }
170: return $msg;
171: }
172:
173: private function striposarray($haystack, $needles) {
174: forEach ($needles as $needle) {
175: if (stripos($haystack, $needle) === false) {
176: return false;
177: }
178: }
179: return true;
180: }
181:
182: private function getSearchResultCategory($section) {
183: $folders = $section->getElementsByTagName('folder');
184: $output = array();
185: forEach ($folders as $folder) {
186: $output []= $folder->getElementsByTagName('name')->item(0)->nodeValue;
187: }
188: return implode(" - ", array_reverse($output));
189: }
190:
191: private function getGuideObject($guide) {
192: $obj = new stdClass;
193: $obj->id = $guide->getElementsByTagName('id')->item(0)->nodeValue;
194: $obj->name = $guide->getElementsByTagName('name')->item(0)->nodeValue;
195: $obj->description = $guide->getElementsByTagName('desc')->item(0)->nodeValue;
196: return $obj;
197: }
198:
199: private function replaceItem($arr) {
200: $type = $arr[1];
201: $id = $arr[3];
202:
203: $output = '';
204:
205: $row = $this->itemsController->findById($id);
206: if ($row !== null) {
207: $output = $this->generateItemMarkup($type, $row);
208: } else {
209: $output = $id;
210: }
211: return $output;
212: }
213:
214: private function replaceWaypoint($arr) {
215: $label = $arr[2];
216: $params = explode(" ", $arr[1]);
217: forEach ($params as $param) {
218: list($name, $value) = explode("=", $param);
219: $$name = $value;
220: }
221:
222: return $this->text->makeChatcmd($label . " ({$x}x{$y})", "/waypoint $x $y $pf");
223: }
224:
225: private function replaceGuideLinks($arr) {
226: $url = $arr[2];
227: $label = $arr[3];
228:
229: if (preg_match("/pid=(\\d+)/", $url, $idArray)) {
230: return $this->text->makeChatcmd($label, "/tell <myname> aou " . $idArray[1]);
231: } else {
232: return $this->text->makeChatcmd($label, "/start $url");
233: }
234: }
235:
236: private function processInput($input) {
237: $input = preg_replace_callback("/\\[(item|itemname|itemicon)( nolink)?\\](\\d+)\\[\\/(item|itemname|itemicon)\\]/i", array($this, 'replaceItem'), $input);
238: $input = preg_replace_callback("/\\[waypoint ([^\\]]+)\\]([^\\]]*)\\[\\/waypoint\\]/", array($this, 'replaceWaypoint'), $input);
239: $input = preg_replace_callback("/\\[(localurl|url)=([^ \\]]+)\\]([^\\[]+)\\[\\/(localurl|url)\\]/", array($this, 'replaceGuideLinks'), $input);
240: $input = preg_replace("/\\[img\\]([^\\[]+)\\[\\/img\\]/", "-image-", $input);
241: $input = preg_replace("/\\[color=#([0-9A-F]+)\\]([^\\[]+)\\[\\/color\\]/", "<font color=#\\1>\\2</font>", $input);
242: $input = preg_replace("/\\[color=([^\\]]+)\\]([^\\[]+)\\[\\/color\\]/", "<\\1>\\2<end>", $input);
243: $input = str_replace("[center]", "<center>", $input);
244: $input = str_replace("[/center]", "</center>", $input);
245: $input = str_replace("[i]", "<i>", $input);
246: $input = str_replace("[/i]", "</i>", $input);
247: $input = str_replace("[b]", "<highlight>", $input);
248: $input = str_replace("[/b]", "<end>", $input);
249:
250: $pattern = "/(\\[[^\\]]+\\])/";
251: $matches = preg_split($pattern, $input, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
252:
253: $output = '';
254: forEach ($matches as $match) {
255: $output .= $this->processTag($match);
256: }
257:
258: return $output;
259: }
260:
261: private function processTag($tag) {
262: switch ($tag) {
263: case "[ts_ts]":
264: return " + ";
265: case "[ts_ts2]":
266: return " = ";
267: case "[cttd]":
268: return " | ";
269: case "[cttr]":
270: case "[br]":
271: return "\n";
272: }
273:
274: if ($tag[0] == '[') {
275: return "";
276: }
277:
278: return $tag;
279: }
280:
281: private function generateItemMarkup($type, $obj) {
282: $output = '';
283: if ($type == "item" || $type == "itemicon") {
284: $output .= $this->text->makeImage($obj->icon);
285: }
286:
287: if ($type == "item" || $type == "itemname") {
288: $output .= $this->text->makeItem($obj->lowid, $obj->highid, $obj->highql, $obj->name);
289: }
290:
291: return $output;
292: }
293: }
294: