| @@ 330-420 (lines=91) @@ | ||
| 327 | $letterObj->setVar('letter_submitter', \Xmf\Request::getInt('letter_submitter', 0)); |
|
| 328 | $letterObj->setVar('letter_created', \Xmf\Request::getInt('letter_created', time())); |
|
| 329 | ||
| 330 | if ($helper->getHandler('Letter')->insert($letterObj)) { |
|
| 331 | $letter_id = $letterObj->getVar('letter_id'); |
|
| 332 | // update existing_attachments |
|
| 333 | $existing_attachments_mode = \Xmf\Request::getArray('existing_attachments_mode', []); |
|
| 334 | foreach ($existing_attachments_mode as $attachment_id => $attachment_mode) { |
|
| 335 | $attachmentObj = $helper->getHandler('Attachment')->get($attachment_id); |
|
| 336 | $attachmentObj->setVar('attachment_mode', $attachment_mode); |
|
| 337 | $helper->getHandler('Attachment')->insert($attachmentObj); |
|
| 338 | } |
|
| 339 | // upload attachments |
|
| 340 | $uploadedFiles = []; |
|
| 341 | require_once XOOPS_ROOT_PATH . '/class/uploader.php'; |
|
| 342 | $uploaddir = XOOPS_UPLOAD_PATH . $helper->getConfig('xn_attachment_path') . $letter_id . '/'; |
|
| 343 | // check upload_dir |
|
| 344 | if (!is_dir($uploaddir)) { |
|
| 345 | $indexFile = XOOPS_UPLOAD_PATH . '/index.html'; |
|
| 346 | if (!mkdir($uploaddir, 0777) && !is_dir($uploaddir)) { |
|
| 347 | throw new \RuntimeException(sprintf('Directory "%s" was not created', $uploaddir)); |
|
| 348 | } |
|
| 349 | chmod($uploaddir, 0777); |
|
| 350 | copy($indexFile, $uploaddir . 'index.html'); |
|
| 351 | } |
|
| 352 | $new_attachments_mode = \Xmf\Request::getArray('new_attachments_mode', []); |
|
| 353 | for ($upl = 0; $upl < $helper->getConfig('xn_maxattachments'); ++$upl) { |
|
| 354 | $uploader = new \XoopsMediaUploader($uploaddir, $helper->getConfig('xn_mimetypes'), $helper->getConfig('xn_maxsize'), null, null); |
|
| 355 | if ($uploader->fetchMedia(@$_POST['xoops_upload_file'][$upl])) { |
|
| 356 | //$uploader->setPrefix("xn_") ; keep original name |
|
| 357 | $uploader->fetchMedia($_POST['xoops_upload_file'][$upl]); |
|
| 358 | if (!$uploader->upload()) { |
|
| 359 | $errors = $uploader->getErrors(); |
|
| 360 | redirect_header('<script>javascript:history.go(-1)</script>', 3, $errors); |
|
| 361 | } else { |
|
| 362 | preg_match('/ne\w_attachment_index=([0-9]+)/', $_POST['xoops_upload_file'][$upl], $matches); |
|
| 363 | $index = $matches[1]; |
|
| 364 | $uploadedFiles[] = [ |
|
| 365 | 'name' => $uploader->getSavedFileName(), |
|
| 366 | 'type' => $uploader->getMediaType(), |
|
| 367 | 'size' => $uploader->getMediaSize(), |
|
| 368 | 'mode' => $new_attachments_mode[$index], |
|
| 369 | ]; |
|
| 370 | } |
|
| 371 | } |
|
| 372 | } |
|
| 373 | // create items in attachments |
|
| 374 | foreach ($uploadedFiles as $file) { |
|
| 375 | $attachmentObj = $helper->getHandler('Attachment')->create(); |
|
| 376 | $attachmentObj->setVar('attachment_letter_id', $letter_id); |
|
| 377 | $attachmentObj->setVar('attachment_name', $file['name']); |
|
| 378 | $attachmentObj->setVar('attachment_type', $file['type']); |
|
| 379 | $attachmentObj->setVar('attachment_submitter', $xoopsUser->uid()); |
|
| 380 | $attachmentObj->setVar('attachment_created', time()); |
|
| 381 | $attachmentObj->setVar('attachment_size', $file['size']); |
|
| 382 | $attachmentObj->setVar('attachment_mode', $file['mode']); |
|
| 383 | ||
| 384 | $helper->getHandler('Attachment')->insert($attachmentObj); |
|
| 385 | } |
|
| 386 | // create item in protocol |
|
| 387 | $protocolObj = $helper->getHandler('Protocol')->create(); |
|
| 388 | $protocolObj->setVar('protocol_letter_id', $letter_id); |
|
| 389 | $protocolObj->setVar('protocol_subscriber_id', 0); |
|
| 390 | $protocolObj->setVar('protocol_success', true); |
|
| 391 | $action = \Xmf\Request::getInt('letter_action', _XNEWSLETTER_LETTER_ACTION_VAL_NO); |
|
| 392 | switch ($action) { |
|
| 393 | case _XNEWSLETTER_LETTER_ACTION_VAL_PREVIEW: |
|
| 394 | $redirectUrl = "?op=show_preview&letter_id={$letter_id}"; |
|
| 395 | break; |
|
| 396 | case _XNEWSLETTER_LETTER_ACTION_VAL_SEND: |
|
| 397 | $redirectUrl = "sendletter.php?op=send_letter&letter_id={$letter_id}"; |
|
| 398 | break; |
|
| 399 | case _XNEWSLETTER_LETTER_ACTION_VAL_SENDTEST: |
|
| 400 | $redirectUrl = "sendletter.php?op=send_test&letter_id={$letter_id}"; |
|
| 401 | break; |
|
| 402 | default: |
|
| 403 | $redirectUrl = '?op=list_letters'; |
|
| 404 | break; |
|
| 405 | } |
|
| 406 | $protocolObj->setVar('protocol_status', _AM_XNEWSLETTER_LETTER_ACTION_SAVED); // old style |
|
| 407 | $protocolObj->setVar('protocol_status_str_id', _XNEWSLETTER_PROTOCOL_STATUS_SAVED); // new from v1.3 |
|
| 408 | $protocolObj->setVar('protocol_status_vars', []); // new from v1.3 |
|
| 409 | $protocolObj->setVar('protocol_submitter', $xoopsUser->uid()); |
|
| 410 | $protocolObj->setVar('protocol_created', time()); |
|
| 411 | ||
| 412 | if ($helper->getHandler('Protocol')->insert($protocolObj)) { |
|
| 413 | // create protocol is ok |
|
| 414 | redirect_header($redirectUrl, 3, _AM_XNEWSLETTER_FORMOK); |
|
| 415 | } else { |
|
| 416 | echo 'Error create protocol: ' . $protocolObj->getHtmlErrors(); |
|
| 417 | } |
|
| 418 | } else { |
|
| 419 | echo 'Error create letter: ' . $letterObj->getHtmlErrors(); |
|
| 420 | } |
|
| 421 | break; |
|
| 422 | case 'clone_letter': |
|
| 423 | case 'copy_letter': |
|
| @@ 455-545 (lines=91) @@ | ||
| 452 | $letterObj->setVar('letter_submitter', \Xmf\Request::getInt('letter_submitter', 0)); |
|
| 453 | $letterObj->setVar('letter_created', \Xmf\Request::getInt('letter_created', time())); |
|
| 454 | ||
| 455 | if ($helper->getHandler('Letter')->insert($letterObj)) { |
|
| 456 | $letter_id = $letterObj->getVar('letter_id'); |
|
| 457 | // update existing_attachments |
|
| 458 | $existing_attachments_mode = \Xmf\Request::getArray('existing_attachments_mode', []); |
|
| 459 | foreach ($existing_attachments_mode as $attachment_id => $attachment_mode) { |
|
| 460 | $attachmentObj = $helper->getHandler('Attachment')->get($attachment_id); |
|
| 461 | $attachmentObj->setVar('attachment_mode', $attachment_mode); |
|
| 462 | $helper->getHandler('Attachment')->insert($attachmentObj); |
|
| 463 | } |
|
| 464 | // upload attachments |
|
| 465 | $uploadedFiles = []; |
|
| 466 | require_once XOOPS_ROOT_PATH . '/class/uploader.php'; |
|
| 467 | $uploaddir = XOOPS_UPLOAD_PATH . $helper->getConfig('xn_attachment_path') . $letter_id . '/'; |
|
| 468 | // check upload_dir |
|
| 469 | if (!is_dir($uploaddir)) { |
|
| 470 | $indexFile = XOOPS_UPLOAD_PATH . '/index.html'; |
|
| 471 | if (!mkdir($uploaddir, 0777) && !is_dir($uploaddir)) { |
|
| 472 | throw new \RuntimeException(sprintf('Directory "%s" was not created', $uploaddir)); |
|
| 473 | } |
|
| 474 | chmod($uploaddir, 0777); |
|
| 475 | copy($indexFile, $uploaddir . 'index.html'); |
|
| 476 | } |
|
| 477 | $new_attachments_mode = \Xmf\Request::getArray('new_attachments_mode', []); |
|
| 478 | for ($upl = 0; $upl < $helper->getConfig('xn_maxattachments'); ++$upl) { |
|
| 479 | $uploader = new \XoopsMediaUploader($uploaddir, $helper->getConfig('xn_mimetypes'), $helper->getConfig('xn_maxsize'), null, null); |
|
| 480 | if ($uploader->fetchMedia(@$_POST['xoops_upload_file'][$upl])) { |
|
| 481 | //$uploader->setPrefix("xn_") ; keep original name |
|
| 482 | $uploader->fetchMedia($_POST['xoops_upload_file'][$upl]); |
|
| 483 | if (!$uploader->upload()) { |
|
| 484 | $errors = $uploader->getErrors(); |
|
| 485 | redirect_header('<script>javascript:history.go(-1)</script>', 3, $errors); |
|
| 486 | } else { |
|
| 487 | preg_match('/ne\w_attachment_index=([0-9]+)/', $_POST['xoops_upload_file'][$upl], $matches); |
|
| 488 | $index = $matches[1]; |
|
| 489 | $uploadedFiles[] = [ |
|
| 490 | 'name' => $uploader->getSavedFileName(), |
|
| 491 | 'type' => $uploader->getMediaType(), |
|
| 492 | 'size' => $uploader->getMediaSize(), |
|
| 493 | 'mode' => $new_attachments_mode[$index], |
|
| 494 | ]; |
|
| 495 | } |
|
| 496 | } |
|
| 497 | } |
|
| 498 | // create items in attachments |
|
| 499 | foreach ($uploadedFiles as $file) { |
|
| 500 | $attachmentObj = $helper->getHandler('Attachment')->create(); |
|
| 501 | $attachmentObj->setVar('attachment_letter_id', $letter_id); |
|
| 502 | $attachmentObj->setVar('attachment_name', $file['name']); |
|
| 503 | $attachmentObj->setVar('attachment_type', $file['type']); |
|
| 504 | $attachmentObj->setVar('attachment_submitter', $xoopsUser->uid()); |
|
| 505 | $attachmentObj->setVar('attachment_created', time()); |
|
| 506 | $attachmentObj->setVar('attachment_size', $file['size']); |
|
| 507 | $attachmentObj->setVar('attachment_mode', $file['mode']); |
|
| 508 | ||
| 509 | $helper->getHandler('Attachment')->insert($attachmentObj); |
|
| 510 | } |
|
| 511 | // create item in protocol |
|
| 512 | $protocolObj = $helper->getHandler('Protocol')->create(); |
|
| 513 | $protocolObj->setVar('protocol_letter_id', $letter_id); |
|
| 514 | $protocolObj->setVar('protocol_subscriber_id', 0); |
|
| 515 | $protocolObj->setVar('protocol_success', true); |
|
| 516 | $action = \Xmf\Request::getInt('letter_action', _XNEWSLETTER_LETTER_ACTION_VAL_NO); |
|
| 517 | switch ($action) { |
|
| 518 | case _XNEWSLETTER_LETTER_ACTION_VAL_PREVIEW: |
|
| 519 | $redirectUrl = "?op=show_preview&letter_id={$letter_id}"; |
|
| 520 | break; |
|
| 521 | case _XNEWSLETTER_LETTER_ACTION_VAL_SEND: |
|
| 522 | $redirectUrl = "sendletter.php?op=send_letter&letter_id={$letter_id}"; |
|
| 523 | break; |
|
| 524 | case _XNEWSLETTER_LETTER_ACTION_VAL_SENDTEST: |
|
| 525 | $redirectUrl = "sendletter.php?op=send_test&letter_id={$letter_id}"; |
|
| 526 | break; |
|
| 527 | default: |
|
| 528 | $redirectUrl = '?op=list_letters'; |
|
| 529 | break; |
|
| 530 | } |
|
| 531 | $protocolObj->setVar('protocol_status', _AM_XNEWSLETTER_LETTER_ACTION_SAVED); |
|
| 532 | $protocolObj->setVar('protocol_status_str_id', _XNEWSLETTER_PROTOCOL_STATUS_SAVED); // new from v1.3 |
|
| 533 | $protocolObj->setVar('protocol_status_vars', []); // new from v1.3 |
|
| 534 | $protocolObj->setVar('protocol_submitter', $xoopsUser->uid()); |
|
| 535 | $protocolObj->setVar('protocol_created', time()); |
|
| 536 | ||
| 537 | if ($helper->getHandler('Protocol')->insert($protocolObj)) { |
|
| 538 | // create protocol is ok |
|
| 539 | redirect_header($redirectUrl, 3, _AM_XNEWSLETTER_FORMOK); |
|
| 540 | } else { |
|
| 541 | echo 'Error create protocol: ' . $protocolObj->getHtmlErrors(); |
|
| 542 | } |
|
| 543 | } else { |
|
| 544 | echo 'Error create letter: ' . $letterObj->getHtmlErrors(); |
|
| 545 | } |
|
| 546 | break; |
|
| 547 | case 'copy_letter': |
|
| 548 | case 'clone_letter': |
|