| Conditions | 44 |
| Paths | > 20000 |
| Total Lines | 340 |
| Lines | 18 |
| Ratio | 5.29 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 168 | function xnewsletter_executeTasks($xn_send_in_packages, $letter_id = 0) |
||
| 169 | { |
||
| 170 | require_once XOOPS_ROOT_PATH . '/modules/xnewsletter/include/functions.php'; |
||
| 171 | // require_once XNEWSLETTER_ROOT_PATH . '/class/class.xnewslettermailer.php'; |
||
| 172 | |||
| 173 | global $XoopsTpl, $xoopsDB, $xoopsUser; |
||
| 174 | $helper = Xnewsletter\Helper::getInstance(); |
||
| 175 | |||
| 176 | View Code Duplication | if (!isset($xoopsTpl) || !is_object($xoopsTpl)) { |
|
| 177 | require_once XOOPS_ROOT_PATH . '/class/template.php'; |
||
| 178 | $xoopsTpl = new \XoopsTpl(); |
||
| 179 | } |
||
| 180 | |||
| 181 | // get once template path |
||
| 182 | $template_path = XNEWSLETTER_ROOT_PATH . '/language/' . $GLOBALS['xoopsConfig']['language'] . '/templates/'; |
||
| 183 | if (!is_dir($template_path)) { |
||
| 184 | $template_path = XNEWSLETTER_ROOT_PATH . '/language/english/templates/'; |
||
| 185 | } |
||
| 186 | if (!is_dir($template_path)) { |
||
| 187 | return str_replace('%p', $template_path, _AM_XNEWSLETTER_SEND_ERROR_INALID_TEMPLATE_PATH); |
||
| 188 | } |
||
| 189 | |||
| 190 | $uid = (is_object($xoopsUser) && isset($xoopsUser)) ? $xoopsUser->uid() : 0; |
||
| 191 | $count_total = 0; |
||
| 192 | $count_err = 0; |
||
| 193 | |||
| 194 | //get letters ready to send groups by letter_id |
||
| 195 | $sql = "SELECT `task_letter_id` FROM {$xoopsDB->prefix('xnewsletter_task')}"; |
||
| 196 | if ($letter_id > 0) { |
||
| 197 | $sql .= " WHERE (`task_letter_id`={$letter_id})"; |
||
| 198 | } |
||
| 199 | $sql .= ' GROUP BY `task_letter_id`'; |
||
| 200 | if (!$task_letters = $xoopsDB->query($sql)) { |
||
| 201 | return _AM_XNEWSLETTER_SEND_ERROR_NO_LETTERID; |
||
| 202 | } |
||
| 203 | |||
| 204 | while (false !== ($task_letter = $xoopsDB->fetchArray($task_letters))) { |
||
| 205 | $letter_id = $task_letter['task_letter_id']; |
||
| 206 | $letterObj = $helper->getHandler('Letter')->get($letter_id); |
||
| 207 | if (0 == count($letterObj)) { |
||
| 208 | return _AM_XNEWSLETTER_SEND_ERROR_NO_LETTERID; |
||
| 209 | } |
||
| 210 | |||
| 211 | // read categories |
||
| 212 | $letter_cats = $letterObj->getVar('letter_cats'); |
||
| 213 | if ('' == $letter_cats) { |
||
| 214 | //no cats |
||
| 215 | return _MA_XNEWSLETTER_LETTER_NONEAVAIL; |
||
| 216 | } |
||
| 217 | |||
| 218 | // read data of account |
||
| 219 | $letter_account = $letterObj->getVar('letter_account'); |
||
| 220 | if ('' == $letter_account || 0 == $letter_account) { |
||
| 221 | return _MA_XNEWSLETTER_ACCOUNTS_NONEAVAIL; |
||
| 222 | } |
||
| 223 | $accountObj = $helper->getHandler('Accounts')->get($letter_account); |
||
| 224 | $account_type = $accountObj->getVar('accounts_type'); |
||
| 225 | $account_yourname = $accountObj->getVar('accounts_yourname'); |
||
| 226 | $account_yourmail = $accountObj->getVar('accounts_yourmail'); |
||
| 227 | $account_username = $accountObj->getVar('accounts_username'); |
||
| 228 | $account_password = $accountObj->getVar('accounts_password'); |
||
| 229 | $account_server_out = $accountObj->getVar('accounts_server_out'); |
||
| 230 | $account_port_out = $accountObj->getVar('accounts_port_out'); |
||
| 231 | $account_securetype_out = $accountObj->getVar('accounts_securetype_out'); |
||
| 232 | |||
| 233 | // create basic mail body |
||
| 234 | $letter_title = $letterObj->getVar('letter_title'); |
||
| 235 | $letter_content = $letterObj->getVar('letter_content', 'n'); |
||
| 236 | |||
| 237 | $letterTpl = new \XoopsTpl(); |
||
| 238 | // letter data |
||
| 239 | $letterTpl->assign('content', $letter_content); |
||
| 240 | $letterTpl->assign('title', $letter_title); // new from v1.3 |
||
| 241 | // letter attachments as link |
||
| 242 | $attachmentAslinkCriteria = new \CriteriaCompo(); |
||
| 243 | $attachmentAslinkCriteria->add(new \Criteria('attachment_letter_id', $letter_id)); |
||
| 244 | $attachmentAslinkCriteria->add(new \Criteria('attachment_mode', _XNEWSLETTER_ATTACHMENTS_MODE_ASLINK)); |
||
| 245 | $attachmentAslinkCriteria->setSort('attachment_id'); |
||
| 246 | $attachmentAslinkCriteria->setOrder('ASC'); |
||
| 247 | $attachmentObjs = $helper->getHandler('Attachment')->getObjects($attachmentAslinkCriteria, true); |
||
| 248 | View Code Duplication | foreach ($attachmentObjs as $attachment_id => $attachmentObj) { |
|
| 249 | $attachment_array = $attachmentObj->toArray(); |
||
| 250 | $attachment_array['attachment_url'] = XNEWSLETTER_URL . "/attachment.php?attachment_id={$attachment_id}"; |
||
| 251 | $attachment_array['attachment_link'] = XNEWSLETTER_URL . "/attachment.php?attachment_id={$attachment_id}"; |
||
| 252 | $letterTpl->append('attachments', $attachment_array); |
||
| 253 | } |
||
| 254 | // extra data |
||
| 255 | $letterTpl->assign('date', time()); // new from v1.3 |
||
| 256 | $letterTpl->assign('xoops_url', XOOPS_URL); // new from v1.3 |
||
| 257 | $letterTpl->assign('xoops_langcode', _LANGCODE); // new from v1.3 |
||
| 258 | $letterTpl->assign('xoops_charset', _CHARSET); // new from v1.3 |
||
| 259 | |||
| 260 | // get emails of subscribers |
||
| 261 | $recipients = []; |
||
| 262 | $sql_tasklist = "SELECT `task_id`, `task_subscr_id` FROM {$xoopsDB->prefix('xnewsletter_task')}"; |
||
| 263 | $sql_tasklist .= " WHERE ((`task_letter_id`= {$letter_id}) AND (`task_starttime` < " . time() . '))'; |
||
| 264 | if (!$task_letters = $xoopsDB->query($sql_tasklist)) { |
||
| 265 | return $task_letters->getErrors(); |
||
| 266 | } |
||
| 267 | $recipients = []; |
||
| 268 | while (false !== ($task_letter = $xoopsDB->fetchArray($task_letters))) { |
||
| 269 | $subscr_id = $task_letter['task_subscr_id']; |
||
| 270 | $task_id = $task_letter['task_id']; |
||
| 271 | if (0 == $subscr_id) { |
||
| 272 | $recipients[] = [ |
||
| 273 | 'task_id' => $task_id, |
||
| 274 | 'address' => $letterObj->getVar('letter_email_test'), |
||
| 275 | 'firstname' => _AM_XNEWSLETTER_SUBSCR_FIRSTNAME_PREVIEW, |
||
| 276 | 'lastname' => _AM_XNEWSLETTER_SUBSCR_LASTNAME_PREVIEW, |
||
| 277 | 'subscr_sex' => _AM_XNEWSLETTER_SUBSCR_SEX_PREVIEW, |
||
| 278 | 'subscriber_id' => '0', |
||
| 279 | 'catsubscr_id' => '0', |
||
| 280 | 'subscriber_actkey' => 'Test', |
||
| 281 | ]; |
||
| 282 | } else { |
||
| 283 | $sql_subscr = "SELECT * FROM {$xoopsDB->prefix('xnewsletter_subscr')}"; |
||
| 284 | $sql_subscr .= " WHERE `subscr_id`= {$subscr_id}"; |
||
| 285 | if (!$task_subscrs = $xoopsDB->query($sql_subscr)) { |
||
| 286 | return $task_subscrs->getErrors(); |
||
| 287 | } |
||
| 288 | |||
| 289 | $subscr = $xoopsDB->fetchArray($task_subscrs); |
||
| 290 | $recipients[] = [ |
||
| 291 | 'task_id' => $task_id, |
||
| 292 | 'address' => $subscr['subscr_email'], |
||
| 293 | 'firstname' => $subscr['subscr_firstname'], |
||
| 294 | 'lastname' => $subscr['subscr_lastname'], |
||
| 295 | 'subscr_sex' => $subscr['subscr_sex'], |
||
| 296 | 'subscriber_id' => $subscr['subscr_id'], |
||
| 297 | 'subscriber_actkey' => $subscr['subscr_actkey'], |
||
| 298 | ]; |
||
| 299 | } |
||
| 300 | if ($xn_send_in_packages > 0 && count($recipients) == $xn_send_in_packages) { |
||
| 301 | break; |
||
| 302 | } |
||
| 303 | } |
||
| 304 | |||
| 305 | if (0 == count($recipients)) { |
||
| 306 | return null; |
||
| 307 | } |
||
| 308 | |||
| 309 | // get letter attachments as attachment |
||
| 310 | $attachmentAsattachmentCriteria = new \CriteriaCompo(); |
||
| 311 | $attachmentAsattachmentCriteria->add(new \Criteria('attachment_letter_id', $letter_id)); |
||
| 312 | $attachmentAsattachmentCriteria->add(new \Criteria('attachment_mode', _XNEWSLETTER_ATTACHMENTS_MODE_ASATTACHMENT)); |
||
| 313 | $attachmentAsattachmentCriteria->setSort('attachment_id'); |
||
| 314 | $attachmentAsattachmentCriteria->setOrder('ASC'); |
||
| 315 | $attachmentObjs = $helper->getHandler('Attachment')->getObjects($attachmentAsattachmentCriteria, true); |
||
| 316 | $attachmentsPath = []; |
||
| 317 | View Code Duplication | foreach ($attachmentObjs as $attachment_id => $attachmentObj) { |
|
| 318 | $attachmentsPath[] = XOOPS_UPLOAD_PATH . $helper->getConfig('xn_attachment_path') . $letter_id . '/' . $attachmentObj->getVar('attachment_name'); |
||
| 319 | } |
||
| 320 | |||
| 321 | try { |
||
| 322 | if (_XNEWSLETTER_ACCOUNTS_TYPE_VAL_PHP_SENDMAIL == $account_type) { |
||
| 323 | $pop = new POP3(); |
||
| 324 | $pop->Authorise($account_server_out, $account_port_out, 30, $account_username, $account_password, 1); |
||
| 325 | } |
||
| 326 | |||
| 327 | //$mail = new PHPMailer(); |
||
| 328 | $mail = new Xnewsletter\XnewsletterMailer(); |
||
| 329 | |||
| 330 | $mail->CharSet = _CHARSET; //use xoops default character set |
||
| 331 | |||
| 332 | if (_XNEWSLETTER_ACCOUNTS_TYPE_VAL_PHP_SENDMAIL == $account_type) { |
||
| 333 | //$mail->IsSendmail(); Fix Error |
||
| 334 | } |
||
| 335 | |||
| 336 | $mail->Username = $account_username; // SMTP account username |
||
| 337 | $mail->Password = $account_password; // SMTP account password |
||
| 338 | |||
| 339 | if (_XNEWSLETTER_ACCOUNTS_TYPE_VAL_POP3 == $account_type) { |
||
| 340 | $mail->isSMTP(); |
||
| 341 | //$mail->SMTPDebug = 2; |
||
| 342 | $mail->Host = $account_server_out; |
||
| 343 | } |
||
| 344 | |||
| 345 | View Code Duplication | if (_XNEWSLETTER_ACCOUNTS_TYPE_VAL_SMTP == $account_type |
|
| 346 | || _XNEWSLETTER_ACCOUNTS_TYPE_VAL_GMAIL == $account_type) { |
||
| 347 | $mail->Port = $account_port_out; // set the SMTP port |
||
| 348 | $mail->Host = $account_server_out; //sometimes necessary to repeat |
||
| 349 | } |
||
| 350 | |||
| 351 | if ('' != $account_securetype_out) { |
||
| 352 | $mail->SMTPAuth = true; |
||
| 353 | $mail->SMTPSecure = $account_securetype_out; // sets the prefix to the server |
||
| 354 | } |
||
| 355 | |||
| 356 | $mail->setFrom($account_yourmail, $account_yourname); |
||
| 357 | $mail->addReplyTo($account_yourmail, $account_yourname); |
||
| 358 | $mail->Subject = html_entity_decode($letter_title, ENT_QUOTES); |
||
| 359 | |||
| 360 | foreach ($recipients as $recipient) { |
||
| 361 | $subscr_id = $recipient['subscriber_id']; |
||
| 362 | // subscr data |
||
| 363 | $letterTpl->assign('sex', $recipient['subscr_sex']); |
||
| 364 | $letterTpl->assign('salutation', $recipient['subscr_sex']); // new from v1.3 |
||
| 365 | $letterTpl->assign('firstname', $recipient['firstname']); |
||
| 366 | $letterTpl->assign('lastname', $recipient['lastname']); |
||
| 367 | $letterTpl->assign('subscr_email', $recipient['address']); |
||
| 368 | $letterTpl->assign('email', $recipient['address']); // new from v1.3 |
||
| 369 | // extra data |
||
| 370 | $act = [ |
||
| 371 | XOOPS_URL, |
||
| 372 | $subscr_id, |
||
| 373 | $recipient['subscriber_actkey'], |
||
| 374 | $recipient['address'], |
||
| 375 | ]; |
||
| 376 | $activationKey = base64_encode(implode('||', $act)); |
||
| 377 | $letterTpl->assign('unsubscribe_link', XOOPS_URL . "/modules/xnewsletter/subscription.php?op=unsub&email={$recipient['address']}&actkey={$activationKey}"); |
||
| 378 | $letterTpl->assign('unsubscribe_url', XOOPS_URL . "/modules/xnewsletter/subscription.php?op=unsub&email={$recipient['address']}&actkey={$activationKey}"); // new from v1.3 |
||
| 379 | |||
| 380 | $templateObj = $helper->getHandler('Template')->get($letterObj->getVar('letter_templateid')); |
||
| 381 | if (is_object($templateObj)) { |
||
| 382 | if ( (int)$templateObj->getVar('template_type') === _XNEWSLETTER_MAILINGLIST_TPL_CUSTOM_VAL) { |
||
| 383 | // get template from database |
||
| 384 | $htmlBody = $letterTpl->fetchFromData($templateObj->getVar('template_content', 'n')); |
||
| 385 | } else { |
||
| 386 | $template = $template_path . $templateObj->getVar('template_title') . '.tpl'; |
||
| 387 | $htmlBody = $letterTpl->fetch($template); |
||
| 388 | } |
||
| 389 | try { |
||
| 390 | $textBody = xnewsletter_html2text($htmlBody); |
||
| 391 | } |
||
| 392 | catch (Html2TextException $e) { |
||
| 393 | $helper->addLog($e); |
||
| 394 | } |
||
| 395 | } else { |
||
| 396 | $htmlBody = _AM_XNEWSLETTER_TEMPLATE_ERR; |
||
| 397 | } |
||
| 398 | |||
| 399 | $mail->addAddress($recipient['address'], $recipient['firstname'] . ' ' . $recipient['lastname']); |
||
| 400 | $mail->msgHTML($htmlBody); // $mail->Body = $htmlBody; |
||
| 401 | $mail->AltBody = $textBody; |
||
| 402 | |||
| 403 | foreach ($attachmentsPath as $attachmentPath) { |
||
| 404 | if (file_exists($attachmentPath)) { |
||
| 405 | $mail->addAttachment($attachmentPath); |
||
| 406 | } |
||
| 407 | } |
||
| 408 | ++$count_total; |
||
| 409 | |||
| 410 | if ($mail->send()) { |
||
| 411 | if (0 == $subscr_id) { |
||
| 412 | $protocol_status = _AM_XNEWSLETTER_SEND_SUCCESS_TEST . ' (' . $recipient['address'] . ')'; // old style |
||
| 413 | $protocol_status_str_id = _XNEWSLETTER_PROTOCOL_STATUS_OK_SEND_TEST; // new from v1.3 |
||
| 414 | $protocol_status_vars = ['recipient' => $recipient['address']]; // new from v1.3 |
||
| 415 | } else { |
||
| 416 | $protocol_status = _AM_XNEWSLETTER_SEND_SUCCESS; // old style |
||
| 417 | $protocol_status_str_id = _XNEWSLETTER_PROTOCOL_STATUS_OK_SEND; // new from v1.3 |
||
| 418 | $protocol_status_vars = []; // new from v1.3 |
||
| 419 | } |
||
| 420 | $protocol_success = true; |
||
| 421 | } else { |
||
| 422 | $protocol_status = _AM_XNEWSLETTER_FAILED . '-> ' . $mail->ErrorInfo; // old style |
||
| 423 | $protocol_status_str_id = _XNEWSLETTER_PROTOCOL_STATUS_ERROR_SEND; // new from v1.3 |
||
| 424 | $protocol_status_vars = ['error' => $mail->ErrorInfo]; // new from v1.3 |
||
| 425 | |||
| 426 | $protocol_success = 0; //must be 0, because 'false' cause error when inserting protokol item |
||
| 427 | ++$count_err; |
||
| 428 | } |
||
| 429 | //create item in protocol for this email |
||
| 430 | $text_clean = ['<strong>', '</strong>', '<br>', '<br>']; |
||
| 431 | $protocol_status = str_replace($text_clean, '', $protocol_status); |
||
| 432 | |||
| 433 | $mail->clearAddresses(); |
||
| 434 | |||
| 435 | //delete item in table task, if not from cron |
||
| 436 | if (0 < $uid) { |
||
| 437 | $sql_delete = "DELETE FROM {$xoopsDB->prefix('xnewsletter_task')}"; |
||
| 438 | $sql_delete .= " WHERE `task_id`= {$recipient['task_id']}"; |
||
| 439 | $result = $xoopsDB->queryF($sql_delete); |
||
| 440 | } |
||
| 441 | |||
| 442 | $protocolObj = $helper->getHandler('Protocol')->create(); |
||
| 443 | $protocolObj->setVar('protocol_letter_id', $letter_id); |
||
| 444 | $protocolObj->setVar('protocol_subscriber_id', $subscr_id); |
||
| 445 | $protocolObj->setVar('protocol_status', $protocol_status); // old style |
||
| 446 | $protocolObj->setVar('protocol_status_str_id', $protocol_status_str_id); // new from v1.3 |
||
| 447 | $protocolObj->setVar('protocol_status_vars', $protocol_status_vars); // new from v1.3 |
||
| 448 | $protocolObj->setVar('protocol_success', $protocol_success); |
||
| 449 | $protocolObj->setVar('protocol_submitter', $uid); |
||
| 450 | $protocolObj->setVar('protocol_created', time()); |
||
| 451 | if ($helper->getHandler('Protocol')->insert($protocolObj)) { |
||
| 452 | // create protocol is ok |
||
| 453 | } else { |
||
| 454 | echo $protocolObj->getHtmlErrors();die; |
||
| 455 | } |
||
| 456 | unset($protocolObj); |
||
| 457 | } |
||
| 458 | |||
| 459 | unset($mail); |
||
| 460 | } |
||
| 461 | catch (phpmailerException $e) { |
||
| 462 | // IN PROGRESS |
||
| 463 | $protocol_status = _AM_XNEWSLETTER_SEND_ERROR_PHPMAILER . $e->errorMessage(); //error messages from PHPMailer |
||
| 464 | ++$count_err; |
||
| 465 | $protocol_success = false; |
||
| 466 | } |
||
| 467 | catch (\Exception $e) { |
||
| 468 | // IN PROGRESS |
||
| 469 | $protocol_status = _AM_XNEWSLETTER_SEND_ERROR_PHPMAILER . $e->getMessage(); //error messages from anything else! |
||
| 470 | ++$count_err; |
||
| 471 | $protocol_success = false; |
||
| 472 | } |
||
| 473 | } |
||
| 474 | |||
| 475 | //create final protocol item |
||
| 476 | if ($count_err > 0) { |
||
| 477 | // IN PROGRESS |
||
| 478 | $protocol_status = xnewsletter_sprintf(_AM_XNEWSLETTER_SEND_ERROR_NUMBER, ['%e' => $count_err, '%t' => $count_total]); |
||
| 479 | $protocol_success = 0; //must be 0, because 'false' cause error when inserting protokol item |
||
| 480 | } else { |
||
| 481 | $protocol_success = true; |
||
| 482 | if ($count_total > 0) { |
||
| 483 | // IN PROGRESS |
||
| 484 | $protocol_status = xnewsletter_sprintf(_AM_XNEWSLETTER_SEND_SUCCESS_NUMBER, ['%t' => $count_total]); |
||
| 485 | } else { |
||
| 486 | // IN PROGRESS |
||
| 487 | $protocol_status = ''; |
||
| 488 | } |
||
| 489 | } |
||
| 490 | $protocolObj = $helper->getHandler('Protocol')->create(); |
||
| 491 | $protocolObj->setVar('protocol_letter_id', $letter_id); |
||
| 492 | $protocolObj->setVar('protocol_subscriber_id', 0); |
||
| 493 | $protocolObj->setVar('protocol_status', $protocol_status); |
||
| 494 | $protocolObj->setVar('protocol_status_str_id', 0); // new from v1.3 |
||
| 495 | $protocolObj->setVar('protocol_status_vars', []); // new from v1.3 |
||
| 496 | $protocolObj->setVar('protocol_success', $protocol_success); |
||
| 497 | $protocolObj->setVar('protocol_submitter', $uid); |
||
| 498 | $protocolObj->setVar('protocol_created', time()); |
||
| 499 | if ($helper->getHandler('Protocol')->insert($protocolObj)) { |
||
| 500 | // create protocol is ok |
||
| 501 | } else { |
||
| 502 | echo $protocolObj->getHtmlErrors();die; |
||
| 503 | } |
||
| 504 | unset($protocolObj); |
||
| 505 | |||
| 506 | return $protocol_status; |
||
| 507 | } |
||
| 508 |
This check marks calls to
isset(...)orempty(...)that are found before the variable itself is defined. These will always have the same result.This is likely the result of code being shifted around. Consider removing these calls.