1: <?php
2:
3: namespace Budabot\Core;
4:
5: 6: 7:
8: class Text {
9:
10:
11: public $chatBot;
12:
13:
14: public $settingManager;
15:
16:
17: public $logger;
18:
19: public function makeHeaderLinks($links) {
20: $output = '';
21: forEach ($links as $title => $command){
22: $output .= " ::: " . $this->makeChatcmd($title, $command, 'style="text-decoration:none;"') . " ::: ";
23: }
24: return $output;
25: }
26:
27: 28: 29: 30:
31: public function makeBlob($name, $content, $header = null) {
32: if ($header === null) {
33: $header = $name;
34: }
35:
36:
37: $content = trim($content);
38:
39:
40: $content = str_replace('"', '"', $content);
41: $header = str_replace('"', '"', $header);
42:
43: $content = $this->formatMessage($content);
44:
45:
46: if ($content == '') {
47: $content = ' ';
48: }
49:
50: $pages = $this->paginate($content, $this->settingManager->get("max_blob_size"), array("<pagebreak>", "\n", " "));
51: $num = count($pages);
52:
53: if ($num == 1) {
54: $page = $pages[0];
55: $headerMarkup = "<header>$header<end>\n\n";
56: $page = "<a href=\"text://".$this->settingManager->get("default_window_color").$headerMarkup.$page."\">$name</a>";
57: return $page;
58: } else {
59: $i = 1;
60: forEach ($pages as $key => $page) {
61: $headerMarkup = "<header>$header (Page $i / $num)<end>\n\n";
62: $page = "<a href=\"text://".$this->settingManager->get("default_window_color").$headerMarkup.$page."\">$name</a> (Page <highlight>$i / $num<end>)";
63: $pages[$key] = $page;
64: $i++;
65: }
66: return $pages;
67: }
68: }
69:
70: public function makeLegacyBlob($name, $content) {
71:
72: $content = str_replace('"', '"', $content);
73:
74: $content = $this->formatMessage($content);
75:
76: $pages = $this->paginate($content, $this->settingManager->get("max_blob_size"), array("<pagebreak>", "\n", " "));
77: $num = count($pages);
78:
79: if ($num == 1) {
80: $page = $pages[0];
81: $page = "<a href=\"text://".$this->settingManager->get("default_window_color").$page."\">$name</a>";
82: return $page;
83: } else {
84: $i = 1;
85: forEach ($pages as $key => $page) {
86: if ($i > 1) {
87: $header = "<header>$name (Page $i / $num)<end>\n\n";
88: } else {
89: $header = '';
90: }
91: $page = "<a href=\"text://".$this->settingManager->get("default_window_color").$header.$page."\">$name</a> (Page <highlight>$i / $num<end>)";
92: $pages[$key] = $page;
93: $i++;
94: }
95: return $pages;
96: }
97: }
98:
99: public function paginate($input, $maxLength, $symbols) {
100: if (count($symbols) == 0) {
101: $this->logger->log('ERROR', "Could not successfully page blob due to lack of paging symbols");
102: return $input;
103: }
104:
105: $pageSize = 0;
106: $currentPage = '';
107: $result = array();
108: $symbol = array_shift($symbols);
109:
110: $lines = explode($symbol, $input);
111: forEach ($lines as $line) {
112:
113: if ($symbol == "\n" || $symbol == " ") {
114: $line .= $symbol;
115: }
116:
117: $lineLength = strlen($line);
118: if ($lineLength > $maxLength) {
119: if ($pageSize != 0) {
120: $result []= $currentPage;
121: $currentPage = '';
122: $pageSize = 0;
123: }
124:
125: $newResult = $this->paginate($line, $maxLength, $symbols);
126: $result = array_merge($result, $newResult);
127: } else if ($pageSize + $lineLength < $maxLength) {
128: $currentPage .= $line;
129: $pageSize += $lineLength;
130: } else {
131: $result []= $currentPage;
132: $currentPage = $line;
133: $pageSize = $lineLength;
134: }
135: }
136:
137: if ($pageSize > 0) {
138: $result []= $currentPage;
139: }
140:
141: return $result;
142: }
143:
144: 145: 146: 147: 148: 149: 150:
151: public function makeChatcmd($name, $content, $style = null) {
152: $content = str_replace("'", ''', $content);
153: return "<a $style href='chatcmd://$content'>$name</a>";
154: }
155:
156: 157: 158: 159: 160: 161:
162: public function makeUserlink($user, $style = null) {
163: return "<a $style href='user://$user'>$user</a>";
164: }
165:
166: 167: 168: 169:
170: public function makeItem($lowId, $highId, $ql, $name) {
171: return "<a href='itemref://{$lowId}/{$highId}/{$ql}'>{$name}</a>";
172: }
173:
174: 175: 176: 177: 178: 179:
180: public function makeImage($imageId, $db = "rdb") {
181: return "<img src='{$db}://{$imageId}'>";
182: }
183:
184: 185: 186: 187:
188: public function formatMessage($message) {
189: $array = array(
190: "<header>" => $this->settingManager->get('default_header_color'),
191: "<header2>" => $this->settingManager->get('default_header2_color'),
192: "<highlight>" => $this->settingManager->get('default_highlight_color'),
193: "<black>" => "<font color='#000000'>",
194: "<white>" => "<font color='#FFFFFF'>",
195: "<yellow>" => "<font color='#FFFF00'>",
196: "<blue>" => "<font color='#8CB5FF'>",
197: "<green>" => "<font color='#00DE42'>",
198: "<red>" => "<font color='#FF0000'>",
199: "<orange>" => "<font color='#FCA712'>",
200: "<grey>" => "<font color='#C3C3C3'>",
201: "<cyan>" => "<font color='#00FFFF'>",
202: "<violet>" => "<font color='#8F00FF'>",
203:
204: "<neutral>" => $this->settingManager->get('default_neut_color'),
205: "<omni>" => $this->settingManager->get('default_omni_color'),
206: "<clan>" => $this->settingManager->get('default_clan_color'),
207: "<unknown>" => $this->settingManager->get('default_unknown_color'),
208:
209: "<myname>" => $this->chatBot->vars["name"],
210: "<myguild>" => $this->chatBot->vars["my_guild"],
211: "<tab>" => " ",
212: "<end>" => "</font>",
213: "<symbol>" => $this->settingManager->get("symbol"),
214: "<br>" => "\n");
215:
216: $message = str_ireplace(array_keys($array), array_values($array), $message);
217:
218: return $message;
219: }
220: }
221: