Esempio n. 1
0
public function send ($data)
{
	if (!coren::have($this->privilege_for_send))
	{
		if (!$this->silent)
		{
			coren::event($this->event_for_not_authorized);
		}
		if (!$this->hidden)
		{
			if (!coren::depend('_dom_builder_0'))
				throw new exception("Tool '_dom_builder_0' missed.");

			$doc = coren::xml();
			$ele = _dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'send', null);
			$ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'not-authorized', null));
		}
//???		do nto throw here. see file ...inject... for reasoning.
//???		throw new exception("Not authorized to flush queue of messages.");
		return;
	}

	$recipient = isset($data['recipient']) ? $data['recipient'] : ''; if (is_scalar($recipient)) $recipient = (string ) $recipient; else throw new exception("Bad recipient for message mail (must be string).");
	$subject   = isset($data['subject'  ]) ? $data['subject'  ] : ''; if (is_scalar($subject  )) $subject   = (string ) $subject  ; else throw new exception("Bad subject for message mail (must be string).");
	$message   = isset($data['message'  ]) ? $data['message'  ] : ''; if (is_scalar($message  )) $message   = (string ) $message  ; else throw new exception("Bad message for message mail (must be string).");
	$headers   = (string) $this->headers;

	$recipient = trim(preg_replace("/(\\r|\\n)+/"   , " "      , $recipient));//NB: strip all newlines at all.
	$subject   = trim(preg_replace("/(\\r|\\n)+/"   , " "      , $subject  ));//NB: strip all newlines at all.
	$message   = trim(preg_replace("/(\\r(\\n?))/"  , "\n"     , $message  ));//NB: force   LF (  \n).
	$headers   = trim(preg_replace("/([^\\r]|^)\\n/", "\\1\r\n", $headers  ));//NB: force CRLF (\r\n).

	$result = @mail($recipient, $subject, $message, $headers);
	$errormsg = isset($php_errormsg) ? $php_errormsg : "Error in mail(); \$php_errormsg isn't set.";
	if (!$result) throw new exception($errormsg);

	if (!$this->silent)
	{
		coren::event($this->event_for_send_success);
	}
	if (!$this->hidden)
	{
		if (!coren::depend('_dom_builder_0'))
			throw new exception("Tool '_dom_builder_0' missed.");

		$doc = coren::xml();
		$ele = _dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'send', null);
		$ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'recipient', $recipient));
		$ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'subject'  , $subject  ));
		$ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'message'  , $message  ));
		coren::slot(coren::name($doc->documentElement->appendChild($ele), $this->name), $this->slot);
	}
}
Esempio n. 2
0
public function flush ($data)
{
	if (!coren::have($this->privilege_for_flush))
	{
		if (!$this->silent)
		{
			coren::event($this->event_for_not_authorized);
		}
		if (!$this->hidden)
		{
			if (!coren::depend('_dom_builder_0'))
				throw new exception("Tool '_dom_builder_0' missed.");

			$doc = coren::xml();
			$ele = _dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'flush', null);
			$ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'not-authorized', null));
			coren::slot(coren::name($doc->documentElement->appendChild($ele), $this->name_for_not_authorized), $this->slot_for_not_authorized);
		}
//???		do nto throw here. see file ...inject... for reasoning.
//???		throw new exception("Not authorized to flush queue of messages.");
		return;
	}

	$limit = max(1, $this->limit_per_call);//min(max(1, $this->limit_per_call)/*, max(1, $this->limit_per_*), ...*/);
	$envelopes = coren::db('select_envelopes', compact('limit'));

	$reports = array();
	if (is_array($envelopes) && !empty($envelopes))
	{
		$identifiers = array();
		foreach ($envelopes as $envelope) $identifiers[] = $envelope['template'];
		$identifiers = array_unique($identifiers);
		$templates = empty($identifiers) ? array() : coren::db('select_templates', compact('identifiers'));

		foreach ($envelopes as $index => $item)
		{
			$envelope    = $item['envelope'   ];
			$template    = $item['template'   ];
			$recipient   = $item['recipient'  ];
			$current_try = $item['current_try'];
			$current_age = $item['current_age'];
			$subject = isset($templates[$template]['subject']) ? $templates[$template]['subject'] : null;
			$message = isset($templates[$template]['message']) ? $templates[$template]['message'] : null;

			try
			{
				$status = +1;
				$error = $errno = null;
				coren::event($this->event_for_send_message, compact('recipient', 'subject', 'message', 'envelope', 'template'));
			}
			catch (exception $exception)
			{
				$status = (isset($this->max_age) && ($current_age >= $this->max_age))
					||(isset($this->max_try) && ($current_try >= $this->max_try))
					? -1 : 0;
				$error  = $exception->getMessage();
				$errno  = $exception->getCode();
			}
			$reports[] = compact('envelope', 'template', 'recipient', 'error', 'errno', 'status');

			if ($this->one_by_one)
			if ($status <= 0)
			{
				coren::db('mark_envelope_failure', compact('envelope', 'error', 'errno', 'status'));
			} else
			{
				coren::db('mark_envelope_success', compact('envelope', 'status'));
			}
		}
	}

	if (!$this->one_by_one)
	{
		$envelopes = array();//NB: this is a list of identifiers of envelopes, that were successfuly sent.
		foreach ($reports as $report)
		{
			$envelope = $report['envelope'];
			$status   = $report['status'  ];
			if ($status <= 0)
			{
				$error = $report['error'];
				$errno = $report['errno'];
				coren::db('mark_envelope_failure', compact('envelope', 'error', 'errno', 'status'));
			} else
			{
				$envelopes[] = $envelope;
			}
		}
		if (!empty($envelopes))
		{
			$status = +1;
			coren::db('mark_envelope_success', compact('envelopes', 'status'));
		}
	}

	if (!$this->silent)
	{
		//!!!??? trigger event about successful or failed send! or maybe it must be dependent
		//!!!??? on one_by_one: event one-by-one, or all at once?
		//coren::event($this->event_for_flush_success, compact(???!!!));
		coren::event($this->event_for_flush_success);
	}
	if (!$this->hidden)
	{
		if (!coren::depend('_dom_builder_0'))
			throw new exception("Tool '_dom_builder_0' missed.");

		$doc = coren::xml();
		$ele = _dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'flush', null);
		$ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'limit'  , $limit));
		$ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'max-age', $this->max_age));
		$ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'max-try', $this->max_try));

		$eler = $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'reports', null));
		foreach ($reports as $report)
		$eler->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'report', $report));

		$elet = $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'templates', null));
		foreach ($templates as $identifier => $template)
		$elet->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'template', $template))
		   ->setAttributeNodeNS(_dom_builder_0::build_attribute($doc, self::namespace, $this->prefix, 'id', $identifier));

		coren::slot(coren::name($doc->documentElement->appendChild($ele), $this->name_for_flush_success), $this->slot_for_flush_success);
	}
}
Esempio n. 3
0
public function inject ($data)
{
	if (!coren::have($this->privilege_for_inject))
	{
		if (!$this->silent)
		{
			coren::event($this->event_for_not_authorized);
		}
		if (!$this->hidden)
		{
			if (!coren::depend('_dom_builder_0'))
				throw new exception("Tool '_dom_builder_0' missed.");

			$doc = coren::xml();
			$ele = _dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'inject', null);
			$ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'not-authorized', null));
			coren::slot(coren::name($doc->documentElement->appendChild($ele), $this->name_for_not_authorized), $this->slot_for_not_authorized);
		}
//???		absence of authrization for some action is an error, but not an exception.
//???		exceptions are for unpredicted situations, and access violation is predicted.
//???		So this is an error, it has an event & data about error, and do nothing more,
//???		but it do not throw an exception, which breaks all workflow of the script
//???		and causes rollbacks in all databases.
//???		throw new exception("Not authorized to inject message into queue of messages.");
		return;
	}

	$priority = isset($data['priority']) ? $data['priority'] :  0; if (is_scalar($priority)) $priority = (integer) $priority; else throw new exception("Bad priority for message queue (must be integer).");
	$subject  = isset($data['subject' ]) ? $data['subject' ] : ''; if (is_scalar($subject )) $subject  = (string ) $subject ; else throw new exception("Bad subject for message queue (must be string).");
	$message  = isset($data['message' ]) ? $data['message' ] : ''; if (is_scalar($message )) $message  = (string ) $message ; else throw new exception("Bad message for message queue (must be string).");

	//??? if both [recipient] and [recipients] are set, should be send to both of them, or to [recipients] only?
	//??? now we use the second way: if [recipients] is set, then [recipient] is ignored.
	$recipient  = isset($data['recipient' ]) ? $data['recipient' ] : null;
	$recipients = isset($data['recipients']) ? $data['recipients'] : array($recipient);
	if (!is_array($recipients)) $recipients = array($recipients);//NB: $recipients will never be null here, so we don't recheck.
	$temp = array();
	foreach ($recipients as $recipient)
		if (is_null  ($recipient)) { continue; } else
		if (is_scalar($recipient)) { $temp[] = (string) $recipient; } else //NB: use recipient even if it is an empty string.
		throw new exception("Bad type of recipient in message queue (must be either null, or string).");
	$recipients = array_unique($temp);

	$template = coren::db('insert_template', compact('subject', 'message'));
	if (!empty($recipients)) coren::db('insert_envelopes', compact('template', 'priority', 'recipients'));

	if (!$this->silent)
	{
		coren::event($this->event_for_inject_success, compact('priority', 'subject', 'message', 'recipients'));
	}
	if (!$this->hidden)
	{
		if (!coren::depend('_dom_builder_0'))
			throw new exception("Tool '_dom_builder_0' missed.");

		$doc = coren::xml();
		$ele = _dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'inject', compact('priority', 'subject', 'message'));
		foreach ($recipients as $recipient)
		$ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'recipient', $recipient));
		coren::slot(coren::name($doc->documentElement->appendChild($ele), $this->name_for_inject_success), $this->slot_for_inject_success);
	}
}