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); } }
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); } }
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); } }
public function identify ($data) { $identifier = coren::event($this->event_for_account_detect_identifier, null); if (is_null($identifier)) { if (!$this->silent) { coren::event($this->event_for_account_detect_skipped); } 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, 'nothing', null); coren::slot(coren::name($doc->documentElement->appendChild($ele), $this->name_for_account_detect_skipped), $this->slot_for_account_detect_skipped); } return; } $information = coren::event($this->event_for_account_detect_information, compact('identifier')); $code = (is_null($information) ? 'absent' : ($information['disabled'] ? 'disabled' : (null))); if (is_null($code)) { $this->identifier = $identifier ; $this->information = $information; if (!$this->silent) { coren::event($this->event_for_account_detect_successed, compact('identifier', 'information')); } 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, 'account-detect-success', null); $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'identifier' , $identifier )); $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'information', $information)); coren::slot(coren::name($doc->documentElement->appendChild($ele), $this->name_for_account_detect_successed), $this->slot_for_account_detect_successed); } return true;//NB: this stops futher handling of current event. } else { if (!$this->silent) { coren::event($this->event_for_account_detect_failed, compact('identifier', 'information', 'code')); } 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, 'account-detect-failure', null); $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'code' , $code )); $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'identifier' , $identifier )); $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'information', $information)); coren::slot(coren::name($doc->documentElement->appendChild($ele), $this->name_for_account_detect_failed), $this->slot_for_account_detect_failed); } } }
public function start ($data) { $requisites = array('origin'); $requisites = coren::event($this->event_for_session_start_context, compact('requisites')); $origin = isset($requisites['origin']) ? $requisites['origin'] : null; $requisites = array('logname', 'password', 'remember'); $requisites = coren::event($this->event_for_session_start_credentials, compact('requisites')); $logname = isset($requisites['logname' ]) ? $requisites['logname' ] : null; $password = isset($requisites['password']) ? $requisites['password'] : null; $remember = isset($requisites['remember']) ? $requisites['remember'] : null; $credentials = compact('logname', 'password'); if (!is_null($logname) && !is_null($password)) { $acknowledge = coren::event($this->event_for_session_start_acknowledge, $credentials); $code = (is_null($acknowledge) ? 'wrong' : ($acknowledge['disabled'] ? 'disabled' : (null))); } else { $acknowledge = null; $code = 'nothing'; } if (is_null($code)) { $trycount = max($this->trycount, 1); $account = $acknowledge['account']; $remote = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null; $secure = 0;//!!!todo later: configurable default secure mode; selectable loginfo secure mode; account info with secure field. $period = $remember ? 0 : 60*60;//!!!todo later: configurable default period; from account settings; from credentials for ($i = 1; $i <= $trycount; $i++) { $identifier = coren::event($this->event_for_session_start_identifier, null); if (is_null($identifier)) throw new exception("Noone have generated identifier for new session."); try { coren::db('session_start', compact('identifier', 'account', 'remote', 'secure', 'period')); break; } catch (identify_session_start_0_exception_duplicate $exception) { if ($i >= $trycount) throw $exception; } } if (!$this->silent) { coren::event($this->event_for_session_start_successed, compact('origin', 'identifier', 'credentials', 'acknowledge', 'remote', 'secure', 'period')); } 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, 'session-start-success', null); $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'identifier' , $identifier )); $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'origin' , $origin )); $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'credentials', $credentials)); $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'acknowledge', $acknowledge)); $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'remote' , $remote )); $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'secure' , $secure )); $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'period' , $period )); coren::slot(coren::name($doc->documentElement->appendChild($ele), $this->name_for_session_start_successed), $this->slot_for_session_start_successed); } } else { if (!$this->silent) { coren::event($this->event_for_session_start_failed, compact('identifier', 'origin', 'credentials', 'acknowledge', 'code')); } 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, 'session-start-failure', null); $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'code' , $code )); $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'origin' , $origin )); $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'credentials', $credentials)); $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'acknowledge', $acknowledge)); coren::slot(coren::name($doc->documentElement->appendChild($ele), $this->name_for_session_start_failed), $this->slot_for_session_start_failed); } } }
public function close ($data) { $requisites = array('origin'); $requisites = coren::event($this->event_for_session_close_context, compact('requisites')); $origin = isset($requisites['origin']) ? $requisites['origin'] : null; $identifier = coren::event($this->event_for_session_close_identifier, null); $code = (is_null($identifier) ? 'already' : (null)); if (is_null($code)) { coren::db('close_session', compact('identifier')); if (!$this->silent) { coren::event($this->event_for_session_close_successed, compact('origin', 'identifier')); } 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, 'session-close-success', null); $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'identifier' , $identifier )); $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'origin' , $origin )); coren::slot(coren::name($doc->documentElement->appendChild($ele), $this->name_for_session_close_successed), $this->slot_for_session_close_successed); } } else { if (!$this->silent) { coren::event($this->event_for_session_close_failed, compact('identifier', 'origin', 'code')); } 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, 'session-close-failure', null); $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'code' , $code )); $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'identifier' , $identifier )); $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'origin' , $origin )); coren::slot(coren::name($doc->documentElement->appendChild($ele), $this->name_for_session_close_failed), $this->slot_for_session_close_failed); } } }
public function detect ($data) { $requisites = array('identifier'); $requisites = coren::event($this->event_for_session_detect_identifier, compact('requisites')); $identifier = isset($requisites['identifier']) ? $requisites['identifier'] : null; if (is_null($identifier) || !is_scalar($identifier) || ($identifier == '')) { if (!$this->silent) { coren::event($this->event_for_session_detect_skipped); } 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, 'nothing', null); coren::slot(coren::name($doc->documentElement->appendChild($ele), $this->name_for_session_detect_skipped), $this->slot_for_session_detect_skipped); } return; } $information = coren::db('select_information', compact('identifier')); $code = (is_null($information) ? 'absent' : (is_null($information['account']) ? 'orphan' : ($information['status'] == 1 ? 'expired' : ($information['status'] == 2 ? 'closed' : ($information['status'] == 3 ? 'halted' : ($information['status'] != 0 ? 'status' : (null))))))); if (is_null($code)) { $this->identifier = $identifier ; $this->information = $information; if (!$this->silent) { coren::event($this->event_for_session_detect_successed, compact('identifier', 'information')); } 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, 'session-detect-success', null); $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'identifier' , $identifier )); $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'information', $information)); coren::slot(coren::name($doc->documentElement->appendChild($ele), $this->name_for_session_detect_successed), $this->slot_for_session_detect_successed); } return $information['account'];//NB: this stops futher handling of current event. } else { if (!$this->silent) { coren::event($this->event_for_session_detect_failed, compact('identifier', 'information', 'code')); } 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, 'session-detect-failure', null); $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'code' , $code )); $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'identifier' , $identifier )); $ele->appendChild(_dom_builder_0::build_nodetree($doc, self::namespace, $this->prefix, 'information', $information)); coren::slot(coren::name($doc->documentElement->appendChild($ele), $this->name_for_session_detect_failed), $this->slot_for_session_detect_failed); } } }