17 require_once SQ_PACKAGES_PATH.
'/bulkmail/bulkmail_post_office/bulkmail_post_office.inc';
18 require_once SQ_PACKAGES_PATH.
'/bulkmail/bulkmail_constant.inc';
19 require_once SQ_FUDGE_PATH.
'/general/www.inc';
47 $this->_ser_attrs = TRUE;
48 parent::__construct($assetid);
63 return $GLOBALS[
'SQ_SYSTEM']->am->getTypeInfo($this->
type(),
'name');
79 require_once SQ_CORE_PACKAGE_PATH.
'/system/system_asset_fns.inc';
80 if (!system_asset_fns_create_pre_check($this)) {
83 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
84 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
86 if ($linkid = parent::create($link)) {
87 if (!system_asset_fns_create_cleanup($this)) {
93 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
95 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
98 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
114 SQ_LINK_TYPE_1 => Array(),
115 SQ_LINK_TYPE_2 => Array(),
116 SQ_LINK_TYPE_3 => Array(),
117 SQ_LINK_NOTICE => Array(
118 'image' => Array(
'card' => 1,
'exclusive' => FALSE),
119 'design' => Array(
'card' =>
'M',
'exclusive' => FALSE),
153 trigger_localised_error(
'CORE0118', E_USER_WARNING, $GLOBALS[
'SQ_SYSTEM']->am->getTypeInfo($this->type(),
'name'));
225 function createAdHocJob($from, $recipients, $subject, $content_id, $content_gen_as, $headers=Array(), $post_office_id=0, $server_details=Array(), $design_id=0, $layout_id=0, $subscriptions=Array())
227 $am = $GLOBALS[
'SQ_SYSTEM']->am;
229 if (!is_array($recipients)) {
230 $recipients = Array($recipients);
233 $recipients = $am->assetExists($recipients);
234 if (empty($recipients)) {
235 trigger_localised_error(
'BML0010', E_USER_ERROR);
239 $am->includeAsset(
'bulkmail_job');
242 $bulkmail_job->setAttrValue(
'from', $from);
243 $bulkmail_job->setAttrValue(
'recipients', $recipients);
244 $bulkmail_job->setAttrValue(
'subject', $subject);
245 $bulkmail_job->setAttrValue(
'content_id', $content_id);
246 $bulkmail_job->setAttrValue(
'content_gen_as', $content_gen_as);
247 $bulkmail_job->setAttrValue(
'header_details', $headers);
249 $bulkmail_job->setAttrValue(
'content_design', $design_id);
250 $bulkmail_job->setAttrValue(
'content_layout', $layout_id);
252 $subscription_status = empty($subscriptions) ? FALSE : TRUE;
253 $bulkmail_job->setAttrValue(
'user_subscriptions_status', $subscription_status);
254 $bulkmail_job->setAttrValue(
'subscriptions', $subscriptions);
257 if (empty($post_office_id) && !empty($server_details)) {
259 $post_office->setAttrValue(
'server_details', $server_details);
260 }
else if ($post_office_id) {
261 $post_office = $am->getAsset($post_office_id,
'bulkmail_post_office');
262 if (is_null($post_office))
return FALSE;
265 return $this->
addJob($bulkmail_job, $post_office);
284 if (is_null($post_office)) {
286 $post_office = $this;
288 $post_office = $job->getPostOffice();
294 $errors = $this->
isValidJob($details_info, TRUE);
295 if (!empty($errors)) {
296 trigger_localised_error(
'BML0001', E_USER_WARNING, $errors[0]);
304 $unique_id = $post_office->id.
':'.md5(uniqid(
''));
306 }
while (is_dir($data_dir));
308 $unique_id = $job->id;
312 if (is_dir($data_dir)) delete_directory($data_dir);
316 create_directory($data_dir);
324 'po_id' => $post_office->id,
325 'status' => BML_JOB_STATE_NOT_RUNNING,
328 }
catch (Exception $e) {
329 throw new Exception(
"Unable to add bulkmail job (#$unique_id) due to database error: ".$e->getMessage());
350 $bind_vars[
'job_id'] = $job_id;
352 }
catch (Exception $e) {
353 throw new Exception(
"Unable to delete bulkmail job (#$job_id) due to database error: ".$e->getMessage());
357 $job_id_parts = explode(
':', $job_id);
358 if (isset($job_id_parts[1])) {
359 $post_office = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($job_id_parts[0]);
360 $path = $post_office->data_path.
'/.data/'.$job_id_parts[1];
361 if (is_dir($path)) delete_directory($path);
382 if ($state == BML_JOB_STATE_NOT_RUNNING ) {
384 $progress_info[
'last_updated'] = 0;
386 if (!array_to_file($progress_info,
'progress_info', $data_dir.
'/progress_info')) {
387 trigger_localised_error(
'BML0008', E_USER_WARNING, $data_dir.
'/progress_info');
398 }
catch (Exception $e) {
399 throw new Exception(
"Unable to update bulkmail job (#$job_id) due to database error: ".$e->getMessage());
417 if (is_null($job_id) && is_null($post_office_id)) {
420 }
catch (Exception $e) {
421 throw new Exception(
'Unable to get queued bulkmail jobs: '.$e->getMessage());
423 }
else if (!is_null($job_id)) {
425 $bind_vars[
'job_id'] = $job_id;
427 }
catch (Exception $e) {
428 throw new Exception(
'Unable to get queued bulkmail jobs: '.$e->getMessage());
430 }
else if (!is_null($post_office_id)) {
432 $bind_vars[
'post_office_id'] = $post_office_id;
434 }
catch (Exception $e) {
435 throw new Exception(
'Unable to get queued bulkmail jobs: '.$e->getMessage());
440 foreach ($results as $job_id => $result) {
441 $job_id_actual = strtok($job_id,
':');
442 if ($GLOBALS[
'SQ_SYSTEM']->am->assetExists($job_id_actual)) {
444 $results[$job_id][
'progress'] = $progress_info;
446 $results[$job_id][
'details'] = $details_info;
448 $results[$job_id][
'po_id'] = $result[0][0];
449 $results[$job_id][
'status'] = $result[0][1];
471 $job_id_parts = explode(
':', $job_id);
473 if (isset($job_id_parts[1])) {
474 $post_office = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($job_id_parts[0]);
475 $path = $post_office->data_path.
'/.data/'.$job_id_parts[1];
476 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($job);
478 $job = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($job_id);
479 $post_office = $job->getPostOffice();
480 $path = $post_office->data_path.
'/.data/'.$job_id;
481 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($job);
483 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($post_office);
500 $job_id_parts = explode(
':', $job_id);
501 if (isset($job_id_parts[1])) {
502 $post_office = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($job_id_parts[0]);
504 $job = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($job_id,
'bulkmail_job');
505 $post_office = $job->getPostOffice();
539 $progress_info = Array(
540 'current_count' => 0,
541 'current_recip_id' => 0,
542 'current_chunk' => 0,
545 'time_per_chunk' => 0,
546 'problematic' => Array(),
549 if (!array_to_file($progress_info,
'progress_info', $data_dir.
'/progress_info')) {
550 trigger_localised_error(
'BML0007', E_USER_WARNING, $data_dir.
'/progress_info');
570 $progress_path = $job_path.
'/progress_info';
571 $progress_info = Array();
572 if (file_exists($progress_path)) {
573 include $progress_path;
576 return $progress_info;
592 $details_info = Array();
595 $details_info[
'job_id'] = $job->id;
597 foreach ($job->vars as $var_name => $var_info) {
601 case 'header_details':
602 if ($job->attr(
'use_post_office_header')) {
603 $details_info[$var_name] = $post_office->attr($var_name);
605 $details_info[$var_name] = $job->attr($var_name);
608 case 'server_details':
609 $details_info[$var_name] = $post_office->attr($var_name);
613 if ($this->
attr(
'use_bm_threshold')) {
614 $details_info[$var_name] = $this->
attr($var_name);
616 $details_info[$var_name] = $post_office->attr($var_name);
619 case 'bulkmail_mode':
621 $mode = $post_office->attr(
'bulkmail_mode');
623 $details_info[$var_name] = $mode;
625 $details_info[$var_name] = $this->
attr($var_name);
629 $details_info[$var_name] = $job->attr($var_name);
634 return $details_info;
651 $details_info[
'queue_details'] = Array(
652 'type' =>
'flatfile',
653 'dir' => $data_dir.
'/queue',
656 if (!array_to_file($details_info,
'details_info', $data_dir.
'/details_info')) {
657 trigger_localised_error(
'BML0007', E_USER_WARNING, $data_dir.
'/details_info');
677 $details_path = $job_path.
'/details_info';
678 $details_info = Array();
679 if (file_exists($details_path)) include $details_path;
681 return $details_info;
702 $driver = array_get_index($details_info[
'server_details'],
'driver',
'');
704 $mail_object = $mf->factory($driver, $details_info[
'server_details']);
705 if ($mail_object instanceof PEAR_Error) {
706 $errors[] = translate(
'bm_warning_server_details_error', $mail_object->getMessage());
708 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($mail_object);
711 $recipients = $details_info[
'recipients'];
712 if (empty($recipients)) {
713 $errors[] = translate(
'bm_warning_no_recipient');
717 $public_user = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'public_user');
718 if (empty($details_info[
'content_id'])) {
719 $errors[] = translate(
'bm_warning_no_asset_to_send');
722 $content_page = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($details_info[
'content_id']);
723 if ($content_page->status != SQ_STATUS_LIVE) {
724 $errors[] = translate(
'bm_warning_asset_to_send_not_live', $content_page->name, $content_page->id);
727 if (empty($details_info[
'content_gen_as'])) {
728 $errors[] = translate(
'bm_warning_no_generate_as');
731 $user = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($details_info[
'content_gen_as']);
732 if (!($user instanceof
Public_User) && !$user->canLogin()) {
733 $errors[] = translate(
'bm_warning_generate_as_login_error', $user->name, $user->id);
736 $ids = $GLOBALS[
'SQ_SYSTEM']->am->getParents($user->id,
'user_group', FALSE);
737 $ids[$user->id] =
'content_generate_as';
738 $content_gen_as_read_access = $content_page->readAccess(array_keys($ids));
739 $public_read_access = $content_page->readAccess(Array($public_user->id));
740 if (!$content_gen_as_read_access && !$public_read_access) {
741 $errors[] = translate(
'bm_warning_generate_as_no_read_access', $user->name, $user->id, $content_page->name, $content_page->id);
743 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($user);
748 $problematic_list = Array();
749 $permission_denied = $GLOBALS[
'SQ_SYSTEM']->am->getPermission($details_info[
'content_id'], SQ_PERMISSION_READ, FALSE, FALSE);
750 $public_user = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'public_user');
751 $public_user_id = $public_user->id;
752 foreach ($recipients as $recipient_key => $recipient_id) {
753 if (isset($recipient_id[
'email']) && preg_match(
'/(.*)\@(.*)/is', $recipient_id[
'email'])) {
755 if (!$content_page->readAccess(Array($public_user_id))) {
756 $problematic_list[] = $recipients[$recipient_key];
760 $ids = $GLOBALS[
'SQ_SYSTEM']->am->getParents($recipient_id,
'user_group', FALSE);
761 $ids[$recipient_id] =
'recipient_id';
762 if (!$content_page->readAccess(array_keys($ids))) {
763 $problematic_list[] = $recipient_id;
765 if (in_array($recipient_id, $permission_denied)) {
766 $problematic_list[] = $recipient_id;
770 if (!empty($problematic_list)) {
771 asort($problematic_list);
772 $errors[] = translate(
'bm_warning_recipient_no_read_access', $content_page->name, $content_page->id, implode(
', ', $problematic_list));
775 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($content_page);
779 if (!empty($details_info[
'content_design'])) {
780 $design = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($details_info[
'content_design']);
781 $public_read_access = $design->readAccess(Array($public_user->id));
782 if (!$public_read_access) {
783 $errors[] = translate(
'bm_warning_no_public_read_access', $design->name, $design->id);
785 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($design);
789 if (!empty($details_info[
'content_layout'])) {
790 $layout = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($details_info[
'content_layout']);
791 $public_read_access = $layout->readAccess(Array($public_user->id));
792 if (!$public_read_access) {
793 $errors[] = translate(
'bm_warning_no_public_read_access', $layout->name, $layout->id);
795 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($layout);
798 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($public_user);
801 if (empty($details_info[
'from'])) {
802 $errors[] = translate(
'bm_warning_no_from_field');
805 if (!valid_email($details_info[
'from'])) {
806 $errors[] = translate(
'bm_warning_from_field_invalid_email');
811 if (empty($details_info[
'subject'])) {
812 $errors[] = translate(
'bm_warning_no_subject_field');
818 return (empty($errors) ? TRUE : FALSE);