MantisBT-Discord – Diff between revs 2 and 3

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 2 Rev 3
Line 1... Line 1...
1 <?php 1 <?php
-   2  
2 /** 3 /**
3 * Discord Integration 4 * Discord Integration
4 * Copyright (C) Robin van Nunen (robin@vnunen.nl) for Discord modification 5 * Copyright (C) Robin van Nunen (robin@vnunen.nl) for Discord modification
5 * Copyright (C) Karim Ratib (karim@meedan.com) for original source 6 * Copyright (C) Karim Ratib (karim@meedan.com) for original source
6 * 7 *
Line 30... Line 31...
30 $this->page = 'config'; 31 $this->page = 'config';
31 $this->version = '1.0'; 32 $this->version = '1.0';
32 $this->requires = array( 33 $this->requires = array(
33 'MantisCore' => '1.3.0', 34 'MantisCore' => '1.3.0',
34 ); 35 );
35 $this->author = 'Robin van Nunen'; 36 $this->author = 'Robin van Nunen & Wizardry and Steamworks';
36 $this->contact = 'robin@vnunen.nl'; 37 $this->contact = 'robin@vnunen.nl / office@grimore.org';
37 $this->url = 'https://github.com/TechGuard/MantisBT-Discord'; 38 $this->url = 'https://github.com/TechGuard/MantisBT-Discord';
38 } 39 }
Line 39... Line 40...
39   40  
40 function install() 41 function install()
41 { 42 {
42 if(version_compare(PHP_VERSION, '5.3.0', '<')) -  
43 { 43 if (version_compare(PHP_VERSION, '5.3.0', '<')) {
Line 44... Line 44...
44 plugin_error(ERROR_PHP_VERSION, ERROR); 44 plugin_error(ERROR_PHP_VERSION, ERROR);
45   45  
46 return false; 46 return false;
47 } -  
48 if(!extension_loaded('curl')) 47 }
Line 49... Line 48...
49 { 48 if (!extension_loaded('curl')) {
50 plugin_error(ERROR_NO_CURL, ERROR); 49 plugin_error(ERROR_NO_CURL, ERROR);
Line 62... Line 61...
62 'url_webhook' => '', 61 'url_webhook' => '',
63 'skip_bulk' => true, 62 'skip_bulk' => true,
64 'link_names' => true, 63 'link_names' => true,
65 'language' => 'english', 64 'language' => 'english',
66 'usernames' => array(), 65 'usernames' => array(),
67 'hook_bug_report' => true, 66 'hook_bug_report' => true,
68 'hook_bug_update' => true, 67 'hook_bug_update' => true,
69 'hook_bug_deleted' => true, 68 'hook_bug_deleted' => true,
70 'hook_bugnote_add' => true, 69 'hook_bugnote_add' => true,
71 'hook_bugnote_edit' => true, 70 'hook_bugnote_edit' => true,
72 'hook_bugnote_deleted' => true, 71 'hook_bugnote_deleted' => true,
73 'columns' => array( 72 'columns' => array(
74 'status', 73 'status',
75 'handler_id', 74 'handler_id',
76 'priority', 75 'priority',
77 'severity', 76 'severity',
Line 94... Line 93...
94 ); 93 );
95 } 94 }
Line 96... Line 95...
96   95  
97 function bugnote_add_form($event, $bug_id) 96 function bugnote_add_form($event, $bug_id)
98 { 97 {
99 if($_SERVER['PHP_SELF'] !== '/bug_update_page.php') -  
100 { 98 if ($_SERVER['PHP_SELF'] !== '/bug_update_page.php') {
101 return; 99 return;
102 } 100 }
103 echo '<tr>'; 101 echo '<tr>';
104 echo '<th class="category">' . plugin_lang_get('skip') . '</th>'; 102 echo '<th class="category">' . plugin_lang_get('skip') . '</th>';
Line 110... Line 108...
110 echo '</td></tr>'; 108 echo '</td></tr>';
111 } 109 }
Line 112... Line 110...
112   110  
113 function bug_report_update($event, $bug, $bug_id) 111 function bug_report_update($event, $bug, $bug_id)
114 { 112 {
115 lang_push( plugin_config_get('language') ); 113 lang_push(plugin_config_get('language'));
116 $this->skip = $this->skip || gpc_get_bool('slack_skip') || $bug->view_state == VS_PRIVATE; 114 $this->skip = $this->skip || gpc_get_bool('slack_skip') || $bug->view_state == VS_PRIVATE;
117 $project = project_get_name($bug->project_id); 115 $project = project_get_name($bug->project_id);
118 $url = string_get_bug_view_url_with_fqdn($bug_id); 116 $url = string_get_bug_view_url_with_fqdn($bug_id);
119 $summary = $this->format_summary($bug); 117 $summary = $this->format_summary($bug);
120 $reporter = $this->get_user_name(auth_get_current_user_id()); 118 $reporter = $this->get_user_name(auth_get_current_user_id());
-   119 $handler = $this->format_value($bug, 'handler_id');
-   120 // TODO: The page anchors do not work.
121 $handler = $this->format_value($bug, 'handler_id'); 121 $msg = sprintf(
-   122 plugin_lang_get($event === 'EVENT_REPORT_BUG' ? 'bug_created' : 'bug_updated'),
122 $msg = sprintf(plugin_lang_get($event === 'EVENT_REPORT_BUG' ? 'bug_created' : 'bug_updated'), 123 $project,
-   124 $reporter,
-   125 $url,
-   126 $summary,
123 $project, $reporter, $url, $summary, $handler 127 $handler
-   128 );
-   129  
-   130 $attachments = array('color' => hexdec('ef2929'));
-   131 $t_columns = (array) plugin_config_get('columns');
-   132 foreach ($t_columns as $t_column) {
-   133 $title = column_get_title($t_column);
-   134 $value = $this->format_value($bug, $t_column);
-   135 if ($title && $value) {
-   136 $attachments['fallback'] .= $title . ': ' . $value . "\n";
-   137 $attachments['fields'][] = array(
-   138 'name' => $title,
-   139 'value' => $value,
-   140 'inline' => !column_is_extended($t_column),
-   141 );
-   142 }
-   143 }
124 ); 144  
125 $this->notify($msg, $this->get_webhook($project), $this->get_attachment($bug)); 145 $this->notify($msg, $this->get_webhook($project), $attachments);
126 lang_pop(); 146 lang_pop();
Line 127... Line 147...
127 } 147 }
128   148  
129 function bug_report($event, $bug, $bug_id) 149 function bug_report($event, $bug, $bug_id)
130 { -  
131 if(plugin_config_get('hook_bug_report', false)) 150 {
132 { 151 if (plugin_config_get('hook_bug_report', false)) {
133 $this->bug_report_update($event, $bug, $bug_id); 152 $this->bug_report_update($event, $bug, $bug_id);
Line 134... Line 153...
134 } 153 }
135 } 154 }
136   155  
137 function bug_update($event, $bug_existing, $bug_updated) -  
138 { 156 function bug_update($event, $bug_existing, $bug_updated)
139 if(plugin_config_get('hook_bug_update', false)) 157 {
140 { 158 if (plugin_config_get('hook_bug_update', false)) {
Line 141... Line 159...
141 $this->bug_report_update($event, $bug_updated, $bug_updated->id); 159 $this->bug_report_update($event, $bug_updated, $bug_updated->id);
142 } 160 }
143 } 161 }
144   162  
145 function bug_action($event, $action, $bug_id) -  
146 { 163 function bug_action($event, $action, $bug_id)
147 $this->skip = $this->skip || gpc_get_bool('slack_skip') || plugin_config_get('skip_bulk'); 164 {
148 if($action !== 'DELETE') 165 $this->skip = $this->skip || gpc_get_bool('slack_skip') || plugin_config_get('skip_bulk');
149 { 166 if ($action !== 'DELETE') {
Line 150... Line 167...
150 $bug = bug_get($bug_id); 167 $bug = bug_get($bug_id);
151 $this->bug_update('EVENT_UPDATE_BUG', null, $bug); 168 $this->bug_update('EVENT_UPDATE_BUG', null, $bug);
152 } 169 }
153 } -  
154   170 }
155 function bug_deleted($event, $bug_id) 171  
Line 156... Line 172...
156 { 172 function bug_deleted($event, $bug_id)
157 if(!plugin_config_get('hook_bug_deleted', false)) 173 {
158 { 174 if (!plugin_config_get('hook_bug_deleted', false)) {
159 return; 175 return;
160 } 176 }
161   177  
-   178 lang_push(plugin_config_get('language'));
162 lang_push( plugin_config_get('language') ); 179 $bug = bug_get($bug_id);
-   180 $this->skip = $this->skip || gpc_get_bool('slack_skip') || $bug->view_state == VS_PRIVATE;
-   181 $project = project_get_name($bug->project_id);
163 $bug = bug_get($bug_id); 182 $reporter = $this->get_user_name(auth_get_current_user_id());
164 $this->skip = $this->skip || gpc_get_bool('slack_skip') || $bug->view_state == VS_PRIVATE; 183 $summary = $this->format_summary($bug);
165 $project = project_get_name($bug->project_id); 184 // TODO: The page anchors do not work.
Line 166... Line 185...
166 $reporter = $this->get_user_name(auth_get_current_user_id()); 185 $msg = sprintf(plugin_lang_get('bug_deleted'), $project, $reporter, $summary);
167 $summary = $this->format_summary($bug); 186 $attachments = array('color' => hexdec('ef2929'));
168 $msg = sprintf(plugin_lang_get('bug_deleted'), $project, $reporter, $summary); 187 $attachments['title'] = $msg;
169 $this->notify($msg, $this->get_webhook($project)); 188 $this->notify(null, $this->get_webhook($project), $attachments);
170 lang_pop(); -  
171 } 189 lang_pop();
172   190 }
Line 173... Line 191...
173 function bugnote_add_edit($event, $bug_id, $bugnote_id) 191  
174 { 192 function bugnote_add_edit($event, $bug_id, $bugnote_id)
175 $type = ($event === 'EVENT_BUGNOTE_ADD') ? 'add' : 'edit'; 193 {
176 if(!plugin_config_get('hook_bugnote_' . $type, false)) 194 $type = ($event === 'EVENT_BUGNOTE_ADD') ? 'add' : 'edit';
177 { 195 if (!plugin_config_get('hook_bugnote_' . $type, false)) {
178 return; 196 return;
179 } 197 }
180   198  
181 lang_push( plugin_config_get('language') ); 199 lang_push(plugin_config_get('language'));
-   200 $bug = bug_get($bug_id);
-   201 $bugnote = bugnote_get($bugnote_id);
182 $bug = bug_get($bug_id); 202 $this->skip = $this->skip || gpc_get_bool('slack_skip') || $bug->view_state == VS_PRIVATE || $bugnote->view_state == VS_PRIVATE;
-   203 $url = string_get_bugnote_view_url_with_fqdn($bug_id, $bugnote_id);
183 $bugnote = bugnote_get($bugnote_id); 204 $project = project_get_name($bug->project_id);
-   205 $summary = $this->format_summary($bug);
-   206 $reporter = $this->get_user_name(auth_get_current_user_id());
184 $this->skip = $this->skip || gpc_get_bool('slack_skip') || $bug->view_state == VS_PRIVATE || $bugnote->view_state == VS_PRIVATE; 207 $note = bugnote_get_text($bugnote_id);
-   208 // TODO: The page anchors do not work.
-   209 $msg = sprintf(
185 $url = string_get_bugnote_view_url_with_fqdn($bug_id, $bugnote_id); 210 plugin_lang_get($event === 'EVENT_BUGNOTE_ADD' ? 'bugnote_created' : 'bugnote_updated'),
186 $project = project_get_name($bug->project_id); 211 $project,
187 $summary = $this->format_summary($bug); 212 $reporter,
Line 188... Line -...
188 $reporter = $this->get_user_name(auth_get_current_user_id()); -  
189 $note = bugnote_get_text($bugnote_id); -  
190 $msg = sprintf(plugin_lang_get($event === 'EVENT_BUGNOTE_ADD' ? 'bugnote_created' : 'bugnote_updated'), -  
191 $project, $reporter, $url, $summary -  
192 ); -  
193 $this->notify($msg, $this->get_webhook($project), $this->get_text_attachment($this->bbcode_to_slack($note))); -  
194 lang_pop(); -  
195 } -  
196   -  
197 function get_text_attachment($text) 213 $url,
198 { 214 $summary
199 $attachment = array('color' => '#3AA3E3', 'mrkdwn_in' => array('pretext', 'text', 'fields')); 215 );
200 $attachment['fallback'] = "$text\n"; -  
201 $attachment['text'] = $text; 216 $attachments = array('color' => hexdec("3366ff"));
202   217 $attachments['title'] = $msg . ' : ' . $this->bbcode_to_discord($note);
Line 203... Line 218...
203 return $attachment; 218 $this->notify(null, $this->get_webhook($project), $attachments);
204 } 219 lang_pop();
205   220 }
206 function bugnote_deleted($event, $bug_id, $bugnote_id) 221  
207 { 222 function bugnote_deleted($event, $bug_id, $bugnote_id)
208 if(!plugin_config_get('hook_bugnote_deleted', false)) 223 {
209 { 224 if (!plugin_config_get('hook_bugnote_deleted', false)) {
210 return; 225 return;
-   226 }
211 } 227  
-   228 lang_push(plugin_config_get('language'));
-   229 $bug = bug_get($bug_id);
212   230 $bugnote = bugnote_get($bugnote_id);
213 lang_push( plugin_config_get('language') ); 231 $this->skip = $this->skip || gpc_get_bool('slack_skip') || $bug->view_state == VS_PRIVATE || $bugnote->view_state == VS_PRIVATE;
214 $bug = bug_get($bug_id); 232 $project = project_get_name($bug->project_id);
Line 215... Line 233...
215 $bugnote = bugnote_get($bugnote_id); 233 $url = string_get_bug_view_url_with_fqdn($bug_id);
216 $this->skip = $this->skip || gpc_get_bool('slack_skip') || $bug->view_state == VS_PRIVATE || $bugnote->view_state == VS_PRIVATE; 234 $summary = $this->format_summary($bug);
Line 230... Line 248...
230 return strip_tags(html_entity_decode($summary)); 248 return strip_tags(html_entity_decode($summary));
231 } 249 }
Line 232... Line 250...
232   250  
233 function format_text($bug, $text) 251 function format_text($bug, $text)
234 { 252 {
Line 235... Line 253...
235 $t = string_display_line_links($this->bbcode_to_slack($text)); 253 $t = string_display_line_links($this->bbcode_to_discord($text));
236   254  
Line 237... Line 255...
237 return strip_tags(html_entity_decode($t)); 255 return strip_tags(html_entity_decode($t));
238 } 256 }
-   257  
-   258 function format_value($bug, $field_name)
239   259 {
-   260 $self = $this;
240 function get_attachment($bug) 261 $values = array(
241 { 262 'id' => function ($bug) {
-   263 return sprintf('<%s|%s>', string_get_bug_view_url_with_fqdn($bug->id), $bug->id);
242 if($bug->status == FEEDBACK) 264 },
243 { 265 'project_id' => function ($bug) {
-   266 return project_get_name($bug->project_id);
244 $color = '#75507b'; 267 },
245 } 268 'reporter_id' => function ($bug) {
-   269 return $this->get_user_name($bug->reporter_id, true);
246 else if($bug->status == ACKNOWLEDGED) 270 },
247 { 271 'handler_id' => function ($bug) {
-   272 return empty($bug->handler_id) ? plugin_lang_get('no_user') : $this->get_user_name($bug->handler_id, true);
248 $color = '#f57900'; 273 },
249 } 274 'duplicate_id' => function ($bug) {
-   275 return sprintf('<%s|%s>', string_get_bug_view_url_with_fqdn($bug->duplicate_id), $bug->duplicate_id);
250 else if($bug->status == CONFIRMED) 276 },
251 { 277 'priority' => function ($bug) {
-   278 return get_enum_element('priority', $bug->priority);
-   279 },
-   280 'severity' => function ($bug) {
-   281 return get_enum_element('severity', $bug->severity);
252 $color = '#fce94f'; 282 },
253 } 283 'reproducibility' => function ($bug) {
-   284 return get_enum_element('reproducibility', $bug->reproducibility);
254 else if($bug->status == ASSIGNED) 285 },
255 { 286 'status' => function ($bug) {
-   287 return get_enum_element('status', $bug->status);
256 $color = '#729fcf'; 288 },
257 } 289 'resolution' => function ($bug) {
-   290 return get_enum_element('resolution', $bug->resolution);
258 else if($bug->status == RESOLVED) 291 },
259 { 292 'projection' => function ($bug) {
-   293 return get_enum_element('projection', $bug->projection);
260 $color = '#8ae234'; 294 },
261 } 295 'category_id' => function ($bug) {
-   296 return category_full_name($bug->category_id, false);
262 else if($bug->status == CLOSED) 297 },
-   298 'eta' => function ($bug) {
-   299 return get_enum_element('eta', $bug->eta);
263 { 300 },
-   301 'view_state' => function ($bug) {
-   302 return $bug->view_state == VS_PRIVATE ? lang_get('private') : lang_get('public');
264 $color = '#8ae234'; 303 },
-   304 'sponsorship_total' => function ($bug) {
265 } 305 return sponsorship_format_amount($bug->sponsorship_total);
266 else 306 },
-   307 'os' => function ($bug) {
-   308 return $bug->os;
267 { 309 },
268 $color = '#ef2929'; 310 'os_build' => function ($bug) {
-   311 return $bug->os_build;
-   312 },
-   313 'platform' => function ($bug) {
-   314 return $bug->platform;
-   315 },
269 } 316 'version' => function ($bug) {
270 317 return $bug->version;
271 $attachment = array('fallback' => '', 'color' => $color, 'mrkdwn_in' => array('pretext', 'text', 'fields')); 318 },
272 $t_columns = (array) plugin_config_get('columns'); 319 'fixed_in_version' => function ($bug) {
273 foreach($t_columns as $t_column) 320 return $bug->fixed_in_version;
-   321 },
-   322 'target_version' => function ($bug) {
274 { 323 return $bug->target_version;
275 $title = column_get_title($t_column); 324 },
276 $value = $this->format_value($bug, $t_column); 325 'build' => function ($bug) {
277 if($title && $value) 326 return $bug->build;
-   327 },
-   328 'summary' => function ($bug) use ($self) {
-   329 return $self->format_summary($bug);
-   330 },
278 { 331 'last_updated' => function ($bug) {
-   332 return date(config_get('short_date_format'), $bug->last_updated);
-   333 },
279 $attachment['fallback'] .= $title . ': ' . $value . "\n"; 334 'date_submitted' => function ($bug) {
-   335 return date(config_get('short_date_format'), $bug->date_submitted);
-   336 },
280 $attachment['fields'][] = array( 337 'due_date' => function ($bug) {
-   338 return date(config_get('short_date_format'), $bug->due_date);
-   339 },
-   340 'description' => function ($bug) use ($self) {
-   341 return $self->format_text($bug, $bug->description);
-   342 },
-   343 'steps_to_reproduce' => function ($bug) use ($self) {
-   344 return $self->format_text($bug, $bug->steps_to_reproduce);
-   345 },
281 'title' => $title, 346 'additional_information' => function ($bug) use ($self) {
-   347 return $self->format_text($bug, $bug->additional_information);
-   348 },
-   349 );
-   350 // Discover custom fields.
-   351 $t_related_custom_field_ids = custom_field_get_linked_ids($bug->project_id);
-   352 foreach ($t_related_custom_field_ids as $t_id) {
282 'value' => $value, 353 $t_def = custom_field_get_definition($t_id);
283 'short' => !column_is_extended($t_column), 354 $values['custom_' . $t_def['name']] = function ($bug) use ($t_id) {
-   355 return custom_field_get_value($t_id, $bug->id);
-   356 };
Line 284... Line 357...
284 ); 357 }
-   358 if (isset($values[$field_name])) {
-   359 $func = $values[$field_name];
-   360  
285 } 361 return $func($bug);
Line 286... Line -...
286 } -  
287   -  
288 return $attachment; -  
289 } -  
290   -  
291 function format_value($bug, $field_name) { -  
292 $self = $this; -  
293 $values = array( -  
294 'id' => function($bug) { return sprintf('<%s|%s>', string_get_bug_view_url_with_fqdn($bug->id), $bug->id); }, -  
295 'project_id' => function($bug) { return project_get_name($bug->project_id); }, -  
296 'reporter_id' => function($bug) { return $this->get_user_name($bug->reporter_id, true); }, -  
297 'handler_id' => function($bug) { return empty($bug->handler_id) ? plugin_lang_get('no_user') : $this->get_user_name($bug->handler_id, true); }, -  
298 'duplicate_id' => function($bug) { return sprintf('<%s|%s>', string_get_bug_view_url_with_fqdn($bug->duplicate_id), $bug->duplicate_id); }, -  
299 'priority' => function($bug) { return get_enum_element( 'priority', $bug->priority ); }, -  
300 'severity' => function($bug) { return get_enum_element( 'severity', $bug->severity ); }, -  
301 'reproducibility' => function($bug) { return get_enum_element( 'reproducibility', $bug->reproducibility ); }, -  
302 'status' => function($bug) { return get_enum_element( 'status', $bug->status ); }, -  
303 'resolution' => function($bug) { return get_enum_element( 'resolution', $bug->resolution ); }, -  
304 'projection' => function($bug) { return get_enum_element( 'projection', $bug->projection ); }, -  
305 'category_id' => function($bug) { return category_full_name( $bug->category_id, false ); }, -  
306 'eta' => function($bug) { return get_enum_element( 'eta', $bug->eta ); }, -  
307 'view_state' => function($bug) { return $bug->view_state == VS_PRIVATE ? lang_get('private') : lang_get('public'); }, -  
308 'sponsorship_total' => function($bug) { return sponsorship_format_amount( $bug->sponsorship_total ); }, -  
309 'os' => function($bug) { return $bug->os; }, -  
310 'os_build' => function($bug) { return $bug->os_build; }, -  
311 'platform' => function($bug) { return $bug->platform; }, -  
312 'version' => function($bug) { return $bug->version; }, -  
313 'fixed_in_version' => function($bug) { return $bug->fixed_in_version; }, -  
314 'target_version' => function($bug) { return $bug->target_version; }, -  
315 'build' => function($bug) { return $bug->build; }, -  
316 'summary' => function($bug) use($self) { return $self->format_summary($bug); }, -  
317 'last_updated' => function($bug) { return date( config_get( 'short_date_format' ), $bug->last_updated ); }, -  
318 'date_submitted' => function($bug) { return date( config_get( 'short_date_format' ), $bug->date_submitted ); }, -  
319 'due_date' => function($bug) { return date( config_get( 'short_date_format' ), $bug->due_date ); }, -  
320 'description' => function($bug) use($self) { return $self->format_text( $bug, $bug->description ); }, -  
321 'steps_to_reproduce' => function($bug) use($self) { return $self->format_text( $bug, $bug->steps_to_reproduce ); }, -  
322 'additional_information' => function($bug) use($self) { return $self->format_text( $bug, $bug->additional_information ); }, -  
323 ); -  
324 // Discover custom fields. -  
325 $t_related_custom_field_ids = custom_field_get_linked_ids($bug->project_id); -  
326 foreach($t_related_custom_field_ids as $t_id) -  
327 { -  
328 $t_def = custom_field_get_definition($t_id); -  
329 $values[ 'custom_' . $t_def['name'] ] = function($bug) use ($t_id) -  
330 { -  
331 return custom_field_get_value($t_id, $bug->id); -  
332 }; -  
333 } -  
334 if(isset($values[ $field_name ])) -  
335 { -  
336 $func = $values[ $field_name ]; -  
337   -  
338 return $func($bug); -  
339 } -  
340 else -  
341 { 362 } else {
342 return false; 363 return false;
343 } 364 }
Line 344... Line 365...
344 } 365 }
345   366  
Line 346... Line 367...
346 function get_webhook($project) 367 function get_webhook($project)
347 { 368 {
348 $webhooks = plugin_config_get('url_webhooks'); 369 $webhooks = plugin_config_get('url_webhooks');
349   -  
350 return array_key_exists($project, $webhooks) ? $webhooks[ $project ] : plugin_config_get('url_webhook'); 370  
351 } 371 return array_key_exists($project, $webhooks) ? $webhooks[$project] : plugin_config_get('url_webhook');
352   372 }
353 function notify($msg, $webhook, $attachment = false) -  
354 { 373  
355 if($this->skip) 374 function notify($msg, $webhook, $attachments)
356 { -  
357 return; -  
358 } -  
359 if(empty($webhook)) -  
360 { -  
Line 361... Line -...
361 return; -  
362 } -  
363 $url = sprintf('%s', trim($webhook)); -  
364 if(substr($url, -strlen('/slack')) != '/slack') -  
365 { -  
366 $url .= '/slack'; 375 {
367 } -  
368   376 if ($this->skip) {
369 $ch = curl_init(); -  
370 curl_setopt($ch, CURLOPT_URL, $url); 377 return;
371 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); 378 }
-   379 if (empty($webhook)) {
372 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 380 return;
373 $payload = array( 381 }
-   382  
374 'text' => $msg, 383 $payload['content'] = $msg;
375 ); 384 if ($attachments != null) {
-   385 $payload['embeds'] = array($attachments);
-   386 }
376 if($attachment) 387  
377 { 388 $ch = curl_init($webhook);
378 $payload['attachments'] = array($attachment); -  
379 } 389 curl_setopt($ch, CURLOPT_POST, 1);
-   390 curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json; charset=utf-8']);
-   391 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
380 $data = array('payload' => json_encode($payload)); 392 curl_setopt($ch, CURLOPT_VERBOSE, 1);
381 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 393 curl_setopt($ch, CURLOPT_HEADER, 1);
382 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); 394 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
383 curl_setopt($ch, CURLOPT_TIMEOUT, 5); 395 $result = curl_exec($ch);
Line 384... Line 396...
384 $result = curl_exec($ch); 396 if (curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 204 && $result !== 'ok') {
385 if($result !== 'ok') 397 trigger_error(curl_errno($ch) . ': ' . curl_error($ch) . ": ", E_USER_WARNING);
-   398 echo '<div class="center"><pre>' . json_encode($payload, JSON_PRETTY_PRINT) . '</pre></div>';
-   399 echo '<div class="center">' . $result . '</div>';
-   400 plugin_error('ERROR_CURL', E_USER_ERROR);
-   401 }
-   402 curl_close($ch);
-   403 }
-   404  
-   405 function bbcode_to_discord($bbtext)
-   406 {
-   407 $bbextended = array(
-   408 "/\[code(.*?)\](.*?)\[\/code\]/is" => "`$2`",
-   409 "/\[list(.*?)\](.*?)\[\/list\]/is" => "$2",
-   410 "/\[color(.*?)\](.*?)\[\/color\]/is" => "$2",
-   411 "/\[size=(.*?)\](.*?)\[\/size\]/is" => "$2",
386 { 412 "/\[highlight(.*?)\](.*?)\[\/highlight\]/is" => "$2",
387 trigger_error(curl_errno($ch) . ': ' . curl_error($ch), E_USER_WARNING); 413 "/\[url](.*?)\[\/url]/i" => "$1",
388 plugin_error('ERROR_CURL', E_USER_ERROR); 414 "/\[url=(.*?)\](.*?)\[\/url\]/i" => "$1",
389 } 415 "/\[email=(.*?)\](.*?)\[\/email\]/i" => "$1",
390 curl_close($ch); 416 "/\[img\]([^[]*)\[\/img\]/i" => "$1",
391 } 417 );
392   418 foreach ($bbextended as $match => $replacement) {
393 function bbcode_to_slack($bbtext) 419 $bbtext = preg_replace($match, $replacement, $bbtext);
394 { 420 }
395 $bbtags = array( 421 $bbtags = array(
396 '[b]' => '*','[/b]' => '* ', 422 '[b]' => '*', '[/b]' => '** ',
397 '[i]' => '_','[/i]' => '_ ', 423 '[i]' => '*', '[/i]' => '* ',
398 '[u]' => '_','[/u]' => '_ ', 424 '[u]' => '__', '[/u]' => '__ ',
399 '[s]' => '~','[/s]' => '~ ', 425 '[s]' => '~~', '[/s]' => '~~ ',
400 '[sup]' => '','[/sup]' => '', 426 '[sup]' => '', '[/sup]' => '',
401 '[sub]' => '','[/sub]' => '', 427 '[sub]' => '', '[/sub]' => '',
402   428  
403 '[list]' => '','[/list]' => "\n", 429 '[list]' => '', '[/list]' => "\n",
404 '[*]' => '• ', 430 '[*]' => '• ',
405   -  
406 '[hr]' => "\n———\n", -  
407   -  
408 '[left]' => '','[/left]' => '', -  
409 '[right]' => '','[/right]' => '', -  
410 '[center]' => '','[/center]' => '', -  
411 '[justify]' => '','[/justify]' => '', -  
412 ); -  
413 $bbtext = str_ireplace(array_keys($bbtags), array_values($bbtags), $bbtext); -  
414 $bbextended = array( -  
415 "/\[code(.*?)\](.*?)\[\/code\]/is" => "```$2```", -  
416 "/\[color(.*?)\](.*?)\[\/color\]/is" => "$2", -  
417 "/\[size=(.*?)\](.*?)\[\/size\]/is" => "$2", 431  
418 "/\[highlight(.*?)\](.*?)\[\/highlight\]/is" => "$2", -  
419 "/\[url](.*?)\[\/url]/i" => "<$1>", 432 '[hr]' => "\n———\n",
420 "/\[url=(.*?)\](.*?)\[\/url\]/i" => "<$1|$2>", 433  
421 "/\[email=(.*?)\](.*?)\[\/email\]/i" => "<mailto:$1|$2>", -  
422 "/\[img\]([^[]*)\[\/img\]/i" => "<$1>", 434 '[left]' => '', '[/left]' => '',
423 ); -  
424 foreach($bbextended as $match => $replacement) 435 '[right]' => '', '[/right]' => '',
425 { 436 '[center]' => '', '[/center]' => '',
426 $bbtext = preg_replace($match, $replacement, $bbtext); 437 '[justify]' => '', '[/justify]' => '',
427 } 438 );
428 $bbtext = preg_replace_callback("/\[quote(=)?(.*?)\](.*?)\[\/quote\]/is", -  
429 function($matches) 439 $bbtext = str_ireplace(array_keys($bbtags), array_values($bbtags), $bbtext);
430 { 440 $bbtext = preg_replace_callback(
431 if(!empty($matches[2])) 441 "/\[quote(=)?(.*?)\](.*?)\[\/quote\]/is",
432 { 442 function ($matches) {
-   443 if (!empty($matches[2])) {
433 $result = "\n> _*" . $matches[2] . "* wrote:_\n> \n"; 444 $result = "\n> _*" . $matches[2] . "* wrote:_\n> \n";
-   445 }
Line 434... Line 446...
434 } 446 $lines = explode("\n", $matches[3]);
435 $lines = explode("\n", $matches[3]); 447 foreach ($lines as $line) {
Line 436... Line 448...
436 foreach($lines as $line) 448 $result .= "> " . $line . "\n";
437 { 449 }
438 $result .= "> " . $line . "\n"; 450  
439 } 451 return $result;
440   452 },
441 return $result; -  
442 }, $bbtext); 453 $bbtext
443   454 );
444 return $bbtext; 455  
445 } 456 return $bbtext;
446   -  
447 function get_user_name($user_id, $discord = false) 457 }
448 { 458  
449 $user = user_get_row($user_id); 459 function get_user_name($user_id, $discord = false)
450 $username = $user['username']; 460 {
451 if(!$discord || !plugin_config_get('link_names')) -  
452 { -  
453 return $username; 461 $user = user_get_row($user_id);
-   462 $username = $user['username'];