Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ManageMaillist_Controller often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ManageMaillist_Controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class ManageMaillist_Controller extends Action_Controller |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Main dispatcher. |
||
| 28 | * |
||
| 29 | * This function checks permissions and passes control to the sub action. |
||
| 30 | * |
||
| 31 | * @event integrate_sa_manage_maillist Used to add more sub actions |
||
| 32 | * @see Action_Controller::action_index() |
||
| 33 | * @uses Maillist template |
||
| 34 | */ |
||
| 35 | public function action_index() |
||
| 36 | { |
||
| 37 | global $context, $txt; |
||
| 38 | |||
| 39 | // Template & language |
||
| 40 | theme()->getTemplates()->load('Maillist'); |
||
| 41 | theme()->getTemplates()->loadLanguageFile('Maillist'); |
||
| 42 | |||
| 43 | // All the functions available |
||
| 44 | $subActions = array( |
||
| 45 | 'emaillist' => array($this, 'action_unapproved_email', 'permission' => 'approve_emails'), |
||
| 46 | 'approve' => array($this, 'action_approve_email', 'permission' => 'approve_emails'), |
||
| 47 | 'delete' => array($this, 'action_delete_email', 'permission' => 'approve_emails'), |
||
| 48 | 'bounce' => array($this, 'action_bounce_email', 'permission' => 'approve_emails'), |
||
| 49 | 'emailtemplates' => array($this, 'action_view_bounce_templates', 'permission' => 'approve_emails'), |
||
| 50 | 'view' => array($this, 'action_view_email', 'permission' => 'approve_emails'), |
||
| 51 | 'emailsettings' => array($this, 'action_settings', 'permission' => 'admin_forum'), |
||
| 52 | 'emailfilters' => array($this, 'action_list_filters', 'permission' => 'admin_forum'), |
||
| 53 | 'editfilter' => array($this, 'action_edit_filters', 'permission' => 'admin_forum'), |
||
| 54 | 'deletefilter' => array($this, 'action_delete_filters', 'permission' => 'admin_forum'), |
||
| 55 | 'emailparser' => array($this, 'action_list_parsers', 'permission' => 'admin_forum'), |
||
| 56 | 'editparser' => array($this, 'action_edit_parsers', 'permission' => 'admin_forum'), |
||
| 57 | 'deleteparser' => array($this, 'action_delete_parsers', 'permission' => 'admin_forum'), |
||
| 58 | 'sortparsers' => array($this, 'action_sort_parsers', 'permission' => 'admin_forum'), |
||
| 59 | 'sortfilters' => array($this, 'action_sort_filters', 'permission' => 'admin_forum'), |
||
| 60 | ); |
||
| 61 | |||
| 62 | // Action Controller |
||
| 63 | $action = new Action('manage_maillist'); |
||
| 64 | |||
| 65 | // Help is needed in most places, so load it up front |
||
| 66 | require_once(SUBSDIR . '/Maillist.subs.php'); |
||
| 67 | |||
| 68 | // Create the title area for the template. |
||
| 69 | $context[$context['admin_menu_name']]['tab_data'] = array( |
||
| 70 | 'title' => $txt['ml_admin_configuration'], |
||
| 71 | 'help' => 'maillist_help_short', |
||
| 72 | 'description' => $txt['ml_configuration_desc'], |
||
| 73 | ); |
||
| 74 | |||
| 75 | // Default to sub action 'emaillist' if none was given, call integrate_sa_manage_maillist |
||
| 76 | $subAction = isset($this->_req->query->sa) && isset($subActions[$this->_req->query->sa]) && (empty($subActions[$this->_req->query->sa]['permission']) || allowedTo($subActions[$this->_req->query->sa]['permission'])) ? $this->_req->query->sa : 'emaillist'; |
||
| 77 | $subAction = $action->initialize($subActions, $subAction); |
||
| 78 | |||
| 79 | // Final bits |
||
| 80 | $context['page_title'] = $txt['ml_admin_configuration']; |
||
| 81 | $context['sub_action'] = $subAction; |
||
| 82 | |||
| 83 | // If you have the permissions, then go Play |
||
| 84 | $action->dispatch($subAction); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Main listing of failed emails. |
||
| 89 | * |
||
| 90 | * What it does |
||
| 91 | * |
||
| 92 | * - Shows the sender, key and subject of the email |
||
| 93 | * - Will show the found key if it was missing or possible sender if it was wrong |
||
| 94 | * - Icons/Actions to view, bounce, delete or approve a failure |
||
| 95 | * - Accessed by ?action=admin;area=maillist;sa=emaillist |
||
| 96 | * |
||
| 97 | * @event integrate_list_view_email_errors |
||
| 98 | * @uses showlist sub template |
||
| 99 | */ |
||
| 100 | public function action_unapproved_email() |
||
| 101 | { |
||
| 102 | global $context, $scripturl, $modSettings, $txt; |
||
| 103 | |||
| 104 | // Set an id if none was supplied |
||
| 105 | $id = $this->_req->getQuery('e_id', 'intval', 0); |
||
| 106 | if (empty($id) || $id <= 0) |
||
| 107 | $id = 0; |
||
| 108 | |||
| 109 | createToken('admin-ml', 'get'); |
||
| 110 | |||
| 111 | // Build the list option array to display the email data |
||
| 112 | $listOptions = array( |
||
| 113 | 'id' => 'view_email_errors', |
||
| 114 | 'title' => $txt['ml_emailerror'], |
||
| 115 | 'items_per_page' => $modSettings['defaultMaxMessages'], |
||
| 116 | 'no_items_label' => $txt['ml_emailerror_none'], |
||
| 117 | 'base_href' => $scripturl . '?action=admin;area=maillist', |
||
| 118 | 'default_sort_col' => 'id_email', |
||
| 119 | 'get_items' => array( |
||
| 120 | 'function' => array($this, 'list_maillist_unapproved'), |
||
| 121 | 'params' => array( |
||
| 122 | $id, |
||
| 123 | ), |
||
| 124 | ), |
||
| 125 | 'get_count' => array( |
||
| 126 | 'function' => 'list_maillist_count_unapproved', |
||
| 127 | ), |
||
| 128 | 'columns' => array( |
||
| 129 | 'id_email' => array( |
||
| 130 | 'header' => array( |
||
| 131 | 'value' => $txt['id'], |
||
| 132 | 'class' => 'nowrap', |
||
| 133 | ), |
||
| 134 | 'data' => array( |
||
| 135 | 'db' => 'id_email', |
||
| 136 | ), |
||
| 137 | 'sort' => array( |
||
| 138 | 'default' => 'id_email ', |
||
| 139 | 'reverse' => 'id_email DESC', |
||
| 140 | ), |
||
| 141 | ), |
||
| 142 | 'error' => array( |
||
| 143 | 'header' => array( |
||
| 144 | 'value' => $txt['error'], |
||
| 145 | ), |
||
| 146 | 'data' => array( |
||
| 147 | 'function' => function ($rowData) { |
||
| 148 | $error = $rowData['error_code']; |
||
| 149 | if ($error === 'error_pm_not_found') |
||
| 150 | return '<span class="error">' . $rowData['error'] . '<span>'; |
||
| 151 | else |
||
| 152 | return $rowData['error']; |
||
| 153 | }, |
||
| 154 | ), |
||
| 155 | 'sort' => array( |
||
| 156 | 'default' => 'error ', |
||
| 157 | 'reverse' => 'error DESC', |
||
| 158 | ), |
||
| 159 | ), |
||
| 160 | 'subject' => array( |
||
| 161 | 'header' => array( |
||
| 162 | 'value' => $txt['subject'], |
||
| 163 | ), |
||
| 164 | 'data' => array( |
||
| 165 | 'db' => 'subject', |
||
| 166 | ), |
||
| 167 | 'sort' => array( |
||
| 168 | 'default' => 'subject', |
||
| 169 | 'reverse' => 'subject DESC', |
||
| 170 | ), |
||
| 171 | ), |
||
| 172 | 'key' => array( |
||
| 173 | 'header' => array( |
||
| 174 | 'value' => $txt['key'], |
||
| 175 | ), |
||
| 176 | 'data' => array( |
||
| 177 | 'db' => 'key', |
||
| 178 | 'class' => 'wordbreak' |
||
| 179 | ), |
||
| 180 | 'sort' => array( |
||
| 181 | 'default' => 'message_key', |
||
| 182 | 'reverse' => 'message_key DESC', |
||
| 183 | ), |
||
| 184 | ), |
||
| 185 | 'message' => array( |
||
| 186 | 'header' => array( |
||
| 187 | 'value' => $txt['message_id'], |
||
| 188 | ), |
||
| 189 | 'data' => array( |
||
| 190 | 'sprintf' => array( |
||
| 191 | 'format' => '<a href="%1$s">%2$s</a>', |
||
| 192 | 'params' => array( |
||
| 193 | 'link' => true, |
||
| 194 | 'message' => true, |
||
| 195 | ), |
||
| 196 | ), |
||
| 197 | ), |
||
| 198 | 'sort' => array( |
||
| 199 | 'default' => 'message_id', |
||
| 200 | 'reverse' => 'message_id DESC', |
||
| 201 | ), |
||
| 202 | ), |
||
| 203 | 'from' => array( |
||
| 204 | 'header' => array( |
||
| 205 | 'value' => $txt['from'], |
||
| 206 | ), |
||
| 207 | 'data' => array( |
||
| 208 | 'db' => 'from', |
||
| 209 | ), |
||
| 210 | 'sort' => array( |
||
| 211 | 'default' => 'email_from', |
||
| 212 | 'reverse' => 'email_from DESC', |
||
| 213 | ), |
||
| 214 | ), |
||
| 215 | 'type' => array( |
||
| 216 | 'header' => array( |
||
| 217 | 'value' => $txt['message_type'], |
||
| 218 | ), |
||
| 219 | 'data' => array( |
||
| 220 | 'function' => function ($rowData) { |
||
| 221 | global $txt; |
||
| 222 | |||
| 223 | // Do we have a type? |
||
| 224 | if (empty($rowData['type'])) |
||
| 225 | return $txt['not_applicable']; |
||
| 226 | // Personal? |
||
| 227 | elseif ($rowData['type'] === 'p') |
||
| 228 | return $txt['personal_message']; |
||
| 229 | // New Topic? |
||
| 230 | elseif ($rowData['type'] === 'x') |
||
| 231 | return $txt['new_topic']; |
||
| 232 | // Ah a Reply then |
||
| 233 | else |
||
| 234 | return $txt['topic'] . ' ' . $txt['reply']; |
||
| 235 | }, |
||
| 236 | ), |
||
| 237 | 'sort' => array( |
||
| 238 | 'default' => 'message_type', |
||
| 239 | 'reverse' => 'message_type DESC', |
||
| 240 | ), |
||
| 241 | ), |
||
| 242 | 'action' => array( |
||
| 243 | 'header' => array( |
||
| 244 | 'value' => $txt['message_action'], |
||
| 245 | ), |
||
| 246 | 'data' => array( |
||
| 247 | 'function' => function ($rowData) { |
||
| 248 | global $context, $txt; |
||
| 249 | |||
| 250 | $id = $rowData['id_email'] . ';'; |
||
| 251 | $commands = array(); |
||
| 252 | $security = $context['session_var'] . '=' . $context['session_id'] . ';' . $context['admin-ml_token_var'] . '=' . $context['admin-ml_token']; |
||
| 253 | |||
| 254 | if ($rowData['error_code'] === 'error_pm_not_found') |
||
| 255 | $commands[] = '<a href="?action=admin;area=maillist;sa=approve;item=' . $id . $security . '" onclick="return confirm(' . JavaScriptEscape($txt['pm_approve_warning']) . ') && submitThisOnce(this);"><i class="icon i-check" title="' . $txt['approve'] . '"></i></a> '; |
||
| 256 | else |
||
| 257 | $commands[] = '<a href="?action=admin;area=maillist;sa=approve;item=' . $id . $security . '"><i class="icon i-check" title="' . $txt['approve'] . '"></i></a> '; |
||
| 258 | |||
| 259 | $commands[] = '<a href="?action=admin;area=maillist;sa=delete;item=' . $id . $security . '" onclick="return confirm(' . JavaScriptEscape($txt['delete_warning']) . ') && submitThisOnce(this);" accesskey="d"><i class="icon i-delete" title="' . $txt['delete'] . '"></i></a><br />'; |
||
| 260 | $commands[] = '<a href="?action=admin;area=maillist;sa=bounce;item=' . $id . $security . '"><i class="icon i-sign-out" title="' . $txt['bounce'] . '"></i></a> '; |
||
| 261 | $commands[] = '<a href="?action=admin;area=maillist;sa=view;item=' . $id . $security . '"><i class="icon i-view" title="' . $txt['view'] . '"></i></a>'; |
||
| 262 | |||
| 263 | return implode('', $commands); |
||
| 264 | }, |
||
| 265 | ), |
||
| 266 | 'class' => 'listaction', |
||
| 267 | ), |
||
| 268 | ), |
||
| 269 | 'form' => array( |
||
| 270 | 'href' => $scripturl . '?action=admin;area=maillist;sa=emaillist', |
||
| 271 | 'include_sort' => true, |
||
| 272 | 'include_start' => true, |
||
| 273 | ), |
||
| 274 | 'additional_rows' => array( |
||
| 275 | array( |
||
| 276 | 'position' => 'top_of_list', |
||
| 277 | 'value' => isset($this->_req->session->email_error) ? '<div class="' . (isset($this->_req->session->email_error_type) ? 'successbox' : 'errorbox') . '">' . $this->_req->session->email_error . '</div>' : $txt['heading'], |
||
| 278 | ), |
||
| 279 | ), |
||
| 280 | ); |
||
| 281 | |||
| 282 | // Clear any errors |
||
| 283 | unset($_SESSION['email_error'], $_SESSION['email_error_type']); |
||
| 284 | |||
| 285 | // Set the context values for the template |
||
| 286 | $context['page_title'] = $txt['emailerror_title']; |
||
| 287 | $context['sub_template'] = 'show_list'; |
||
| 288 | $context['default_list'] = 'view_email_errors'; |
||
| 289 | |||
| 290 | // Create the list. |
||
| 291 | createList($listOptions); |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Show a failed email for review by the moderation team |
||
| 296 | * |
||
| 297 | * What it does: |
||
| 298 | * |
||
| 299 | * - Will not show a PM if it has been identified as such |
||
| 300 | * - Accessed by ?action=admin;area=maillist;sa=view;item=? |
||
| 301 | * |
||
| 302 | * @uses show_email sub template |
||
| 303 | */ |
||
| 304 | public function action_view_email() |
||
| 305 | { |
||
| 306 | global $txt, $context; |
||
| 307 | |||
| 308 | allowedTo('approve_emails'); |
||
| 309 | checkSession('get'); |
||
| 310 | validateToken('admin-ml', 'get'); |
||
| 311 | |||
| 312 | $id = (int) $this->_req->query->item; |
||
| 313 | if (!empty($id)) |
||
| 314 | { |
||
| 315 | // Load up the email details, no funny biz ;) |
||
| 316 | $temp_email = list_maillist_unapproved($id); |
||
| 317 | |||
| 318 | if (!empty($temp_email)) |
||
| 319 | { |
||
| 320 | if ($temp_email[0]['type'] !== 'p' && allowedTo('approve_emails')) |
||
| 321 | { |
||
| 322 | // The raw email that failed |
||
| 323 | $data = $temp_email[0]['body']; |
||
| 324 | |||
| 325 | // Read/parse this message for viewing |
||
| 326 | $controller = new Emailpost_Controller(); |
||
| 327 | $result = $controller->action_pbe_preview($data); |
||
| 328 | $text = isset($result['body']) ? $result['body'] : ''; |
||
| 329 | $email_to = isset($result['to']) ? $result['to'] : ''; |
||
| 330 | } |
||
| 331 | else |
||
| 332 | { |
||
| 333 | // PM's mean just that ... |
||
| 334 | $text = $txt['noaccess']; |
||
| 335 | $email_to = $txt['private']; |
||
| 336 | } |
||
| 337 | } |
||
| 338 | else |
||
| 339 | $text = $txt['badid']; |
||
| 340 | } |
||
| 341 | else |
||
| 342 | $text = $txt['badid']; |
||
| 343 | |||
| 344 | $parser = \BBC\ParserWrapper::instance(); |
||
| 345 | |||
| 346 | // Prep and show the template with what we found |
||
| 347 | $context['body'] = $parser->parseEmail($text); |
||
| 348 | $context['to'] = $txt['to'] . ' ' . (isset($email_to) ? $email_to : ''); |
||
| 349 | $context['notice_subject'] = isset($temp_email[0]['subject']) ? $txt['subject'] . ': ' . $temp_email[0]['subject'] : ''; |
||
| 350 | $context['notice_from'] = isset($temp_email[0]['from']) ? $txt['from'] . ': ' . $temp_email[0]['from'] : ''; |
||
| 351 | $context['page_title'] = $txt['show_notice']; |
||
| 352 | $context['error_code'] = isset($temp_email[0]['error_code']) && isset($txt[$temp_email[0]['error_code']]) ? $txt[$temp_email[0]['error_code']] : ''; |
||
| 353 | $context['sub_template'] = 'show_email'; |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Deletes an entry from the database |
||
| 358 | * |
||
| 359 | * What it does: |
||
| 360 | * |
||
| 361 | * - Flushes the moderator menu todo numbers so the menu numbers update |
||
| 362 | * - Accessed by ?action=admin;area=maillist;sa=delete;item=?' |
||
| 363 | * - Redirects to ?action=admin;area=maillist;sa=emaillist |
||
| 364 | */ |
||
| 365 | public function action_delete_email() |
||
| 366 | { |
||
| 367 | allowedTo('approve_emails'); |
||
| 368 | checkSession('get'); |
||
| 369 | validateToken('admin-ml', 'get'); |
||
| 370 | |||
| 371 | $id = (int) $this->_req->query->item; |
||
| 372 | |||
| 373 | // Remove this entry |
||
| 374 | if (!empty($id)) |
||
| 375 | maillist_delete_error_entry($id); |
||
| 376 | |||
| 377 | // Flush the cache |
||
| 378 | Cache::instance()->remove('num_menu_errors'); |
||
| 379 | |||
| 380 | // Back to the failed list we go |
||
| 381 | redirectexit('action=admin;area=maillist;sa=emaillist'); |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Attempts to approve and post a failed email |
||
| 386 | * |
||
| 387 | * What it does: |
||
| 388 | * |
||
| 389 | * - Reviews the data to see if the email error function fixed typical issues like key and wrong id |
||
| 390 | * - Submits the fixed email to the main function which will post it or fail it again |
||
| 391 | * - If successful will remove the entry from the failed log |
||
| 392 | * - Accessed by ?action=admin;area=maillist;sa=approve;item=?' |
||
| 393 | * - Redirects to action=admin;area=maillist;sa=emaillist |
||
| 394 | */ |
||
| 395 | public function action_approve_email() |
||
| 396 | { |
||
| 397 | global $txt; |
||
| 398 | |||
| 399 | allowedTo('approve_emails'); |
||
| 400 | checkSession('get'); |
||
| 401 | validateToken('admin-ml', 'get'); |
||
| 402 | |||
| 403 | // Get the id to approve |
||
| 404 | $id = (int) $this->_req->query->item; |
||
| 405 | |||
| 406 | if (!empty($id) && $id !== -1) |
||
| 407 | { |
||
| 408 | // Load up the email data |
||
| 409 | $temp_email = list_maillist_unapproved($id); |
||
| 410 | if (!empty($temp_email)) |
||
| 411 | { |
||
| 412 | // Do we have the needed data to approve this, after all it failed for a reason yes? |
||
| 413 | if (!empty($temp_email[0]['key']) && (!in_array($temp_email[0]['error_code'], array('error_no_message', 'error_not_find_board', 'error_topic_gone')))) |
||
| 414 | { |
||
| 415 | // Set up the details needed to get this posted |
||
| 416 | $force = true; |
||
| 417 | $key = $temp_email[0]['key']; |
||
| 418 | $data = $temp_email[0]['body']; |
||
| 419 | |||
| 420 | // Unknown from email? Update the message ONLY if we found an appropriate one during the error checking process |
||
| 421 | if (in_array($temp_email[0]['error_code'], array('error_not_find_member', 'error_key_sender_match'))) |
||
| 422 | { |
||
| 423 | // did we actually find a potential correct name, if so we post from the valid member |
||
| 424 | $check_emails = array_pad(explode('=>', $temp_email[0]['from']), 2, ''); |
||
| 425 | |||
| 426 | if (!empty($check_emails[1])) |
||
| 427 | $data = preg_replace('~(From: )(.*<)?(' . preg_quote(trim($check_emails[0])) . ')(>)?(\n)~i', '$1$2' . trim($check_emails[1]) . '$4$5', $data); |
||
| 428 | } |
||
| 429 | |||
| 430 | // Lets TRY AGAIN to make a post! |
||
| 431 | include_once(CONTROLLERDIR . '/Emailpost.controller.php'); |
||
| 432 | $controller = new Emailpost_Controller(); |
||
| 433 | $text = $controller->action_pbe_post($data, $force, $key); |
||
| 434 | |||
| 435 | // Assuming all went well, remove this entry and file since we are done. |
||
| 436 | if ($text === true) |
||
| 437 | { |
||
| 438 | maillist_delete_error_entry($id); |
||
| 439 | |||
| 440 | // Flush the menu count cache |
||
| 441 | Cache::instance()->remove('num_menu_errors'); |
||
| 442 | |||
| 443 | $_SESSION['email_error'] = $txt['approved']; |
||
| 444 | $_SESSION['email_error_type'] = 1; |
||
| 445 | } |
||
| 446 | else |
||
| 447 | $_SESSION['email_error'] = $txt['error_approved']; |
||
| 448 | } |
||
| 449 | else |
||
| 450 | $_SESSION['email_error'] = $txt['cant_approve']; |
||
| 451 | } |
||
| 452 | else |
||
| 453 | $_SESSION['email_error'] = $txt['badid']; |
||
| 454 | } |
||
| 455 | else |
||
| 456 | $_SESSION['email_error'] = $txt['badid']; |
||
| 457 | |||
| 458 | // back to the list we go |
||
| 459 | redirectexit('action=admin;area=maillist;sa=emaillist'); |
||
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Allows the admin to choose from predefined and custom templates |
||
| 464 | * |
||
| 465 | * What it does: |
||
| 466 | * |
||
| 467 | * - Uses the selected template to send a bounce notification with |
||
| 468 | * details as specified by the template |
||
| 469 | * - Accessed by ?action=admin;area=maillist;sa=bounce;item=?' |
||
| 470 | * - Redirects to action=admin;area=maillist;sa=bounced |
||
| 471 | * - Provides {MEMBER}, {SCRIPTURL}, {FORUMNAME}, {REGARDS}, {SUBJECT}, {ERROR}, |
||
| 472 | * {FORUMNAMESHORT}, {EMAILREGARDS} replaceable values to the template |
||
| 473 | * |
||
| 474 | * @uses bounce_email sub-template |
||
| 475 | */ |
||
| 476 | public function action_bounce_email() |
||
| 477 | { |
||
| 478 | global $context, $txt, $modSettings, $scripturl, $mbname; |
||
| 479 | |||
| 480 | if (!isset($this->_req->query->bounce)) |
||
| 481 | { |
||
| 482 | checkSession('get'); |
||
| 483 | validateToken('admin-ml', 'get'); |
||
| 484 | } |
||
| 485 | |||
| 486 | require_once(SUBSDIR . '/Mail.subs.php'); |
||
| 487 | |||
| 488 | // We should have been sent an email ID |
||
| 489 | if (isset($this->_req->query->item)) |
||
| 490 | { |
||
| 491 | // Needs to be an int! |
||
| 492 | $id = (int) $this->_req->query->item; |
||
| 493 | |||
| 494 | // Load up the email details, no funny biz yall ;) |
||
| 495 | $temp_email = list_maillist_unapproved($id); |
||
| 496 | |||
| 497 | if (!empty($temp_email)) |
||
| 498 | { |
||
| 499 | // Set the options |
||
| 500 | $this->_req->post->item = (int) $temp_email[0]['id_email']; |
||
| 501 | $fullerrortext = $txt[$temp_email[0]['error_code']]; |
||
| 502 | |||
| 503 | // Build the template selection area, first the standard ones |
||
| 504 | $bounce = array('bounce', 'inform'); |
||
| 505 | foreach ($bounce as $k => $type) |
||
| 506 | { |
||
| 507 | $context['bounce_templates'][$k]['body'] = $txt['ml_' . $type . '_body']; |
||
| 508 | $context['bounce_templates'][$k]['subject'] = $txt['ml_' . $type . '_subject']; |
||
| 509 | $context['bounce_templates'][$k]['title'] = $txt['ml_' . $type . '_title']; |
||
| 510 | } |
||
| 511 | |||
| 512 | // And now any custom ones available for this moderator |
||
| 513 | $context['bounce_templates'] += array_merge($context['bounce_templates'], maillist_templates('bnctpl', $txt['ml_bounce_template_subject_default'])); |
||
| 514 | |||
| 515 | // Replace all the variables in the templates |
||
| 516 | foreach ($context['bounce_templates'] as $k => $name) |
||
| 517 | { |
||
| 518 | $context['bounce_templates'][$k]['body'] = strtr($name['body'], array( |
||
| 519 | '{MEMBER}' => un_htmlspecialchars($temp_email[0]['name']), |
||
| 520 | '{SCRIPTURL}' => $scripturl, |
||
| 521 | '{FORUMNAME}' => $mbname, |
||
| 522 | '{REGARDS}' => replaceBasicActionUrl($txt['regards_team']), |
||
| 523 | '{SUBJECT}' => $temp_email[0]['subject'], |
||
| 524 | '{ERROR}' => $fullerrortext, |
||
| 525 | '{FORUMNAMESHORT}' => (!empty($modSettings['maillist_sitename']) ? $modSettings['maillist_sitename'] : $mbname), |
||
| 526 | '{EMAILREGARDS}' => (!empty($modSettings['maillist_sitename_regards']) ? $modSettings['maillist_sitename_regards'] : ''), |
||
| 527 | )); |
||
| 528 | } |
||
| 529 | } |
||
| 530 | else |
||
| 531 | $context['settings_message'] = $txt['badid']; |
||
| 532 | } |
||
| 533 | else |
||
| 534 | $context['settings_message'] = $txt['badid']; |
||
| 535 | |||
| 536 | // Check if they are sending the notice |
||
| 537 | if (isset($this->_req->query->bounce) && isset($temp_email)) |
||
| 538 | { |
||
| 539 | checkSession('post'); |
||
| 540 | validateToken('admin-ml'); |
||
| 541 | |||
| 542 | // They did check the box, how else could they have posted |
||
| 543 | if (isset($this->_req->post->warn_notify)) |
||
| 544 | { |
||
| 545 | // lets make sure we have the items to send it |
||
| 546 | $check_emails = explode('=>', $temp_email[0]['from']); |
||
| 547 | $to = trim($check_emails[0]); |
||
| 548 | $subject = trim($this->_req->post->warn_sub); |
||
| 549 | $body = trim($this->_req->post->warn_body); |
||
| 550 | |||
| 551 | if (empty($body) || empty($subject)) |
||
| 552 | $context['settings_message'] = $txt['bad_bounce']; |
||
| 553 | else |
||
| 554 | { |
||
| 555 | // Time for someone to get a we're so sorry message! |
||
| 556 | sendmail($to, $subject, $body, null, null, false, 5); |
||
| 557 | redirectexit('action=admin;area=maillist;bounced'); |
||
| 558 | } |
||
| 559 | } |
||
| 560 | } |
||
| 561 | |||
| 562 | // Prepare and show the template |
||
| 563 | createToken('admin-ml'); |
||
| 564 | $context['warning_data'] = array('notify' => '', 'notify_subject' => '', 'notify_body' => ''); |
||
| 565 | $context['body'] = isset($fullerrortext) ? \BBC\ParserWrapper::instance()->parseEmail($fullerrortext) : ''; |
||
| 566 | $context['item'] = isset($this->_req->post->item) ? $this->_req->post->item : ''; |
||
| 567 | $context['notice_to'] = $txt['to'] . ' ' . isset($temp_email[0]['from']) ? $temp_email[0]['from'] : ''; |
||
|
|
|||
| 568 | $context['page_title'] = $txt['bounce_title']; |
||
| 569 | $context['sub_template'] = 'bounce_email'; |
||
| 570 | } |
||
| 571 | |||
| 572 | /** |
||
| 573 | * List all the filters in the system |
||
| 574 | * |
||
| 575 | * What it does: |
||
| 576 | * |
||
| 577 | * - Allows to add/edit or delete filters |
||
| 578 | * - Filters are used to alter text in a post, to remove crud that comes with emails |
||
| 579 | * - Filters can be defined as regex, the system will check it for valid syntax |
||
| 580 | * - Accessed by ?action=admin;area=maillist;sa=emailfilters; |
||
| 581 | * |
||
| 582 | * @event integrate_list_email_filter |
||
| 583 | */ |
||
| 584 | public function action_list_filters() |
||
| 585 | { |
||
| 586 | global $context, $scripturl, $txt, $modSettings; |
||
| 587 | |||
| 588 | $id = 0; |
||
| 589 | |||
| 590 | // Build the listoption array to display the filters |
||
| 591 | $listOptions = array( |
||
| 592 | 'id' => 'email_filter', |
||
| 593 | 'title' => $txt['filters'], |
||
| 594 | 'items_per_page' => $modSettings['defaultMaxMessages'], |
||
| 595 | 'no_items_label' => $txt['no_filters'], |
||
| 596 | 'base_href' => $scripturl . '?action=admin;area=maillist;sa=emailfilters', |
||
| 597 | 'default_sort_col' => 'name', |
||
| 598 | 'get_items' => array( |
||
| 599 | 'function' => array($this, 'load_filter_parser'), |
||
| 600 | 'params' => array( |
||
| 601 | $id, |
||
| 602 | 'filter' |
||
| 603 | ), |
||
| 604 | ), |
||
| 605 | 'get_count' => array( |
||
| 606 | 'function' => array($this, 'count_filter_parser'), |
||
| 607 | 'params' => array( |
||
| 608 | $id, |
||
| 609 | 'filter' |
||
| 610 | ), |
||
| 611 | ), |
||
| 612 | 'columns' => array( |
||
| 613 | 'name' => array( |
||
| 614 | 'header' => array( |
||
| 615 | 'value' => $txt['filter_name'], |
||
| 616 | 'style' => 'white-space: nowrap;' |
||
| 617 | ), |
||
| 618 | 'data' => array( |
||
| 619 | 'db' => 'filter_name', |
||
| 620 | ), |
||
| 621 | 'sort' => array( |
||
| 622 | 'default' => 'filter_name, id_filter', |
||
| 623 | 'reverse' => 'filter_name DESC, id_filter DESC', |
||
| 624 | ), |
||
| 625 | ), |
||
| 626 | 'from' => array( |
||
| 627 | 'header' => array( |
||
| 628 | 'value' => $txt['filter_from'], |
||
| 629 | ), |
||
| 630 | 'data' => array( |
||
| 631 | 'db' => 'filter_from', |
||
| 632 | ), |
||
| 633 | 'sort' => array( |
||
| 634 | 'default' => 'filter_from, id_filter', |
||
| 635 | 'reverse' => 'filter_from DESC, id_filter DESC', |
||
| 636 | ), |
||
| 637 | ), |
||
| 638 | 'to' => array( |
||
| 639 | 'header' => array( |
||
| 640 | 'value' => $txt['filter_to'], |
||
| 641 | 'style' => 'width:10em;', |
||
| 642 | ), |
||
| 643 | 'data' => array( |
||
| 644 | 'db' => 'filter_to', |
||
| 645 | ), |
||
| 646 | 'sort' => array( |
||
| 647 | 'default' => 'filter_to, id_filter', |
||
| 648 | 'reverse' => 'filter_to DESC, id_filter DESC', |
||
| 649 | ), |
||
| 650 | ), |
||
| 651 | 'type' => array( |
||
| 652 | 'header' => array( |
||
| 653 | 'value' => $txt['filter_type'], |
||
| 654 | ), |
||
| 655 | 'data' => array( |
||
| 656 | 'db' => 'filter_type', |
||
| 657 | ), |
||
| 658 | 'sort' => array( |
||
| 659 | 'default' => 'filter_type, id_filter', |
||
| 660 | 'reverse' => 'filter_type DESC, id_filter DESC', |
||
| 661 | ), |
||
| 662 | ), |
||
| 663 | 'action' => array( |
||
| 664 | 'header' => array( |
||
| 665 | 'value' => $txt['message_action'], |
||
| 666 | 'class' => 'centertext', |
||
| 667 | ), |
||
| 668 | 'data' => array( |
||
| 669 | 'sprintf' => array( |
||
| 670 | 'format' => '<a href="?action=admin;area=maillist;sa=editfilter;f_id=%1$s;' . $context['session_var'] . '=' . $context['session_id'] . '"> |
||
| 671 | <i class="icon i-modify" title="' . $txt['modify'] . '"></i> |
||
| 672 | </a> |
||
| 673 | <a href="?action=admin;area=maillist;sa=deletefilter;f_id=%1$s;' . $context['session_var'] . '=' . $context['session_id'] . '" onclick="return confirm(' . JavaScriptEscape($txt['filter_delete_warning']) . ') && submitThisOnce(this);" accesskey="d"> |
||
| 674 | <i class="icon i-delete" title="' . $txt['delete'] . '"></i> |
||
| 675 | </a>', |
||
| 676 | 'params' => array( |
||
| 677 | 'id_filter' => true, |
||
| 678 | ), |
||
| 679 | ), |
||
| 680 | 'class' => 'centertext', |
||
| 681 | 'style' => 'white-space:nowrap;', |
||
| 682 | ), |
||
| 683 | ), |
||
| 684 | ), |
||
| 685 | 'form' => array( |
||
| 686 | 'href' => $scripturl . '?action=admin;area=maillist;sa=editfilter;', |
||
| 687 | 'include_sort' => true, |
||
| 688 | 'include_start' => true, |
||
| 689 | 'hidden_fields' => array( |
||
| 690 | $context['session_var'] => $context['session_id'], |
||
| 691 | ), |
||
| 692 | ), |
||
| 693 | 'additional_rows' => array( |
||
| 694 | array( |
||
| 695 | 'position' => isset($this->_req->query->saved) ? 'top_of_list' : 'after_title', |
||
| 696 | 'value' => isset($this->_req->query->saved) ? '<div class="successbox">' . $txt['saved'] . '</div>' : $txt['filters_title'], |
||
| 697 | ), |
||
| 698 | array( |
||
| 699 | 'position' => 'below_table_data', |
||
| 700 | 'class' => 'submitbutton', |
||
| 701 | 'value' => ' |
||
| 702 | <input type="submit" name="addfilter" value="' . $txt['add_filter'] . '" /> |
||
| 703 | <a class="linkbutton" href="' . $scripturl . '?action=admin;area=maillist;sa=sortfilters">' . $txt['sort_filter'] . '</a>', |
||
| 704 | ), |
||
| 705 | ), |
||
| 706 | ); |
||
| 707 | |||
| 708 | // Set the context values |
||
| 709 | $context['page_title'] = $txt['filters']; |
||
| 710 | $context['sub_template'] = 'show_list'; |
||
| 711 | $context['default_list'] = 'email_filter'; |
||
| 712 | |||
| 713 | // Create the list. |
||
| 714 | createList($listOptions); |
||
| 715 | } |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Show a full list of all the filters in the system for drag/drop sorting |
||
| 719 | * |
||
| 720 | * @event integrate_list_sort_email_fp |
||
| 721 | */ |
||
| 722 | public function action_sort_filters() |
||
| 723 | { |
||
| 724 | global $context, $scripturl, $txt; |
||
| 725 | |||
| 726 | $id = 0; |
||
| 727 | $token = createToken('admin-sort'); |
||
| 728 | |||
| 729 | // build the listoption array to display the data |
||
| 730 | $listOptions = array( |
||
| 731 | 'id' => 'sort_email_fp', |
||
| 732 | 'title' => $txt['sort_filter'], |
||
| 733 | 'sortable' => true, |
||
| 734 | 'items_per_page' => 0, |
||
| 735 | 'no_items_label' => $txt['no_filters'], |
||
| 736 | 'base_href' => $scripturl . '?action=admin;area=maillist;sa=sortfilters', |
||
| 737 | 'get_items' => array( |
||
| 738 | 'function' => array($this, 'load_filter_parser'), |
||
| 739 | 'params' => array( |
||
| 740 | $id, |
||
| 741 | 'filter' |
||
| 742 | ), |
||
| 743 | ), |
||
| 744 | 'get_count' => array( |
||
| 745 | 'function' => array($this, 'count_filter_parser'), |
||
| 746 | 'params' => array( |
||
| 747 | $id, |
||
| 748 | 'filter' |
||
| 749 | ), |
||
| 750 | ), |
||
| 751 | 'columns' => array( |
||
| 752 | 'filterorder' => array( |
||
| 753 | 'header' => array( |
||
| 754 | 'value' => '', |
||
| 755 | 'class' => 'hide', |
||
| 756 | ), |
||
| 757 | 'data' => array( |
||
| 758 | 'db' => 'filter_order', |
||
| 759 | 'class' => 'hide', |
||
| 760 | ), |
||
| 761 | ), |
||
| 762 | 'name' => array( |
||
| 763 | 'header' => array( |
||
| 764 | 'value' => $txt['filter_name'], |
||
| 765 | 'style' => 'white-space: nowrap;width: 10em' |
||
| 766 | ), |
||
| 767 | 'data' => array( |
||
| 768 | 'db' => 'filter_name', |
||
| 769 | ), |
||
| 770 | ), |
||
| 771 | 'from' => array( |
||
| 772 | 'header' => array( |
||
| 773 | 'value' => $txt['filter_from'], |
||
| 774 | ), |
||
| 775 | 'data' => array( |
||
| 776 | 'db' => 'filter_from', |
||
| 777 | ), |
||
| 778 | ), |
||
| 779 | 'to' => array( |
||
| 780 | 'header' => array( |
||
| 781 | 'value' => $txt['filter_to'], |
||
| 782 | 'style' => 'width:10em;', |
||
| 783 | ), |
||
| 784 | 'data' => array( |
||
| 785 | 'db' => 'filter_to', |
||
| 786 | ), |
||
| 787 | ), |
||
| 788 | 'type' => array( |
||
| 789 | 'header' => array( |
||
| 790 | 'value' => $txt['filter_type'], |
||
| 791 | ), |
||
| 792 | 'data' => array( |
||
| 793 | 'db' => 'filter_type', |
||
| 794 | ), |
||
| 795 | ), |
||
| 796 | ), |
||
| 797 | 'form' => array( |
||
| 798 | 'href' => $scripturl . '?action=admin;area=maillist;sa=sortfilters', |
||
| 799 | 'hidden_fields' => array( |
||
| 800 | $context['session_var'] => $context['session_id'], |
||
| 801 | ), |
||
| 802 | ), |
||
| 803 | 'additional_rows' => array( |
||
| 804 | array( |
||
| 805 | 'position' => 'after_title', |
||
| 806 | 'value' => $txt['filter_sort_description'], |
||
| 807 | ), |
||
| 808 | ), |
||
| 809 | 'javascript' => ' |
||
| 810 | $().elkSortable({ |
||
| 811 | sa: "parserorder", |
||
| 812 | placeholder: "ui-state-highlight", |
||
| 813 | containment: "#sort_email_fp", |
||
| 814 | error: "' . $txt['admin_order_error'] . '", |
||
| 815 | title: "' . $txt['admin_order_title'] . '", |
||
| 816 | href: "?action=admin;area=maillist;sa=sortfilters", |
||
| 817 | token: {token_var: "' . $token['admin-sort_token_var'] . '", token_id: "' . $token['admin-sort_token'] . '"} |
||
| 818 | }); |
||
| 819 | ', |
||
| 820 | ); |
||
| 821 | |||
| 822 | // Set the context values |
||
| 823 | $context['page_title'] = $txt['filters']; |
||
| 824 | $context['sub_template'] = 'show_list'; |
||
| 825 | $context['default_list'] = 'sort_email_fp'; |
||
| 826 | $context[$context['admin_menu_name']]['current_subsection'] = 'emailfilters'; |
||
| 827 | |||
| 828 | // Create the list. |
||
| 829 | createList($listOptions); |
||
| 830 | } |
||
| 831 | |||
| 832 | /** |
||
| 833 | * Returns the number of filters or parsers in the system |
||
| 834 | * |
||
| 835 | * - Callback for createList() |
||
| 836 | * |
||
| 837 | * @param int $id 0 for all of a certain style |
||
| 838 | * @param string $style one of filter or parser |
||
| 839 | */ |
||
| 840 | public function count_filter_parser($id, $style) |
||
| 841 | { |
||
| 842 | return list_count_filter_parser($id, $style); |
||
| 843 | } |
||
| 844 | |||
| 845 | /** |
||
| 846 | * Returns the details for the filters or parsers in the system |
||
| 847 | * |
||
| 848 | * - Callback for createList() |
||
| 849 | * |
||
| 850 | * @param int $start The item to start with (for pagination purposes) |
||
| 851 | * @param int $items_per_page The number of items to show per page |
||
| 852 | * @param string $sort A string indicating how to sort the results |
||
| 853 | * @param int $id |
||
| 854 | * @param string $style |
||
| 855 | */ |
||
| 856 | public function load_filter_parser($start, $items_per_page, $sort, $id, $style) |
||
| 860 | |||
| 861 | /** |
||
| 862 | * Edit or Add a filter |
||
| 863 | * |
||
| 864 | * - If regex will check for proper syntax before saving to the database |
||
| 865 | * |
||
| 866 | * @event integrate_save_filter_settings |
||
| 867 | * |
||
| 868 | */ |
||
| 869 | public function action_edit_filters() |
||
| 979 | |||
| 980 | /** |
||
| 981 | * Initialize Mailist settings form. |
||
| 982 | * |
||
| 983 | * @event integrate_modify_maillist_filter_settings Add new settings to the maillist filter area |
||
| 984 | */ |
||
| 985 | private function _filtersSettings() |
||
| 1005 | |||
| 1006 | /** |
||
| 1007 | * Deletes a filter from the system / database |
||
| 1008 | */ |
||
| 1009 | View Code Duplication | public function action_delete_filters() |
|
| 1021 | |||
| 1022 | /** |
||
| 1023 | * Show a list of all the parsers in the system |
||
| 1024 | * |
||
| 1025 | * What it does: |
||
| 1026 | * |
||
| 1027 | * - Allows to add/edit or delete parsers |
||
| 1028 | * - Parsers are used to split a message at a line of text |
||
| 1029 | * - Parsers can only be defined as regex, the system will check it for valid syntax |
||
| 1030 | * - Accessed by ?action=admin;area=maillist;sa=emailparser; |
||
| 1031 | * |
||
| 1032 | * @event integrate_list_email_parser |
||
| 1033 | */ |
||
| 1034 | public function action_list_parsers() |
||
| 1152 | |||
| 1153 | /** |
||
| 1154 | * Show a full list of all the parsers in the system for drag/drop sorting |
||
| 1155 | * |
||
| 1156 | * @event integrate_list_email_parser |
||
| 1157 | */ |
||
| 1158 | public function action_sort_parsers() |
||
| 1258 | |||
| 1259 | /** |
||
| 1260 | * Adds or Edits an existing parser |
||
| 1261 | * |
||
| 1262 | * - All parsers are assumed regex |
||
| 1263 | * |
||
| 1264 | * @event integrate_save_parser_settings |
||
| 1265 | */ |
||
| 1266 | public function action_edit_parsers() |
||
| 1374 | |||
| 1375 | /** |
||
| 1376 | * Initialize Mailist settings form. |
||
| 1377 | * |
||
| 1378 | * @event integrate_modify_maillist_parser_settings Add settings to the maillist parser screen |
||
| 1379 | */ |
||
| 1380 | private function _parsersSettings() |
||
| 1399 | |||
| 1400 | /** |
||
| 1401 | * Removes a parser from the system and database |
||
| 1402 | */ |
||
| 1403 | View Code Duplication | public function action_delete_parsers() |
|
| 1415 | |||
| 1416 | /** |
||
| 1417 | * All the post by email settings, used to control how the feature works |
||
| 1418 | * |
||
| 1419 | * @event integrate_save_maillist_settings |
||
| 1420 | * @uses Admin language |
||
| 1421 | */ |
||
| 1422 | public function action_settings() |
||
| 1555 | 6 | ||
| 1556 | 6 | /** |
|
| 1557 | 4 | * Load up the config var array for settings display etc. |
|
| 1558 | * |
||
| 1559 | * @event integrate_modify_maillist_settings |
||
| 1560 | 6 | */ |
|
| 1561 | 4 | private function _settings() |
|
| 1632 | |||
| 1633 | /** |
||
| 1634 | * Return the form settings for use in admin search |
||
| 1635 | */ |
||
| 1636 | public function settings_search() |
||
| 1640 | |||
| 1641 | /** |
||
| 1642 | * View all the custom email bounce templates. |
||
| 1643 | * |
||
| 1644 | * What it does: |
||
| 1645 | * |
||
| 1646 | * - Shows all the bounce templates in the system available to this user |
||
| 1647 | * - Provides for actions to add or delete them |
||
| 1648 | * - Accessed by ?action=admin;area=maillist;sa=emailtemplates; |
||
| 1649 | * |
||
| 1650 | * @event integrate_list_bounce_template_list |
||
| 1651 | */ |
||
| 1652 | public function action_view_bounce_templates() |
||
| 1768 | |||
| 1769 | /** |
||
| 1770 | * Edit a 'it bounced' template. |
||
| 1771 | * |
||
| 1772 | * @uses bounce_template sub template |
||
| 1773 | */ |
||
| 1774 | public function action_modify_bounce_templates() |
||
| 1871 | |||
| 1872 | /** |
||
| 1873 | * Get all the bounce templates from the system |
||
| 1874 | * |
||
| 1875 | * - Callback for createList() |
||
| 1876 | * |
||
| 1877 | * @param int $start The item to start with (for pagination purposes) |
||
| 1878 | * @param int $items_per_page The number of items to show per page |
||
| 1879 | * @param string $sort A string indicating how to sort the results |
||
| 1880 | */ |
||
| 1881 | public function list_getBounceTemplates($start, $items_per_page, $sort) |
||
| 1885 | |||
| 1886 | /** |
||
| 1887 | * Get the number of bounce templates in the system |
||
| 1888 | * |
||
| 1889 | * - Callback for createList() to warningTemplateCount |
||
| 1890 | */ |
||
| 1891 | public function list_getBounceTemplateCount() |
||
| 1895 | |||
| 1896 | /** |
||
| 1897 | * Get the number of unapproved emails |
||
| 1898 | * |
||
| 1899 | * - Callback for createList() to list_maillist_unapproved |
||
| 1900 | * |
||
| 1901 | * @param int $start The item to start with (for pagination purposes) |
||
| 1902 | * @param int $items_per_page The number of items to show per page |
||
| 1903 | * @param string $sort A string indicating how to sort the results |
||
| 1904 | * @param int $id = 0 |
||
| 1905 | */ |
||
| 1906 | public function list_maillist_unapproved($start, $items_per_page, $sort = '', $id = 0) |
||
| 1910 | } |
||
| 1911 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: