Projekt

Obecné

Profil

Stáhnout (30.6 KB) Statistiky
| Větev: | Tag: | Revize:
8baed187 Michal Kliment
<?php defined('SYSPATH') or die('No direct script access.');
/*
* This file is part of open source system FreenetIS
* and it is released under GPLv3 licence.
*
* More info about licence can be found:
* http://www.gnu.org/licenses/gpl-3.0.html
*
* More info about project can be found:
* http://www.freenetis.org/
*
*/

/**
* Messages for notifications and actions for activating redirection and
* sending email and SMS messages for notigfication.
*
* @package Model
*
* @property integer $id
* @property string $name
* @property string $text
* @property string $email_text
* @property string $sms_text
* @property integer $type
* @property integer $self_cancel
* @property boolean $ignore_whitelist
*/
class Message_Model extends ORM
{
// type constants
/**
* user message, can be added and deleted by user
*/
c1bdc1c4 Michal Kliment
const USER_MESSAGE = 0;
8baed187 Michal Kliment
/**
* not exactly message, it is content of side panel,
* should be used for information for all redirections
*/
c1bdc1c4 Michal Kliment
const CONTACT_INFORMATION = 1;
8baed187 Michal Kliment
/**
* content of page shown after canceling redirection
*/
c1bdc1c4 Michal Kliment
const CANCEL_MESSAGE = 2;
8baed187 Michal Kliment
/**
* content of page with text for unknown device
*/
c1bdc1c4 Michal Kliment
const UNKNOWN_DEVICE_MESSAGE = 3;
8baed187 Michal Kliment
/**
* content of page for interrupted member,
* this redirection can be set in system
*/
c1bdc1c4 Michal Kliment
const INTERRUPTED_MEMBERSHIP_MESSAGE = 4;
8baed187 Michal Kliment
/**
* content of page for debtor, this redirection can be set in system
*/
c1bdc1c4 Michal Kliment
const DEBTOR_MESSAGE = 5;
8baed187 Michal Kliment
/**
* content of page for payment notice, this redirection can be set
* in system and can be canceled by user
*/
c1bdc1c4 Michal Kliment
const PAYMENT_NOTICE_MESSAGE = 6;
8baed187 Michal Kliment
/**
* content of page for unallowed connecting place, depends on allowed subnets
*/
c1bdc1c4 Michal Kliment
const UNALLOWED_CONNECTING_PLACE_MESSAGE = 7;
/**
* content of page for received payment notice
*/
const RECEIVED_PAYMENT_NOTICE_MESSAGE = 8;
/**
* content of page for approved application for membership
*/
const APPLICANT_APPROVE_MEMBERSHIP = 9;
/**
* content of page for refused application for membership
*/
const APPLICANT_REFUSE_MEMBERSHIP = 10;
/**
* content of page for approved request for connection
*/
const CONNECTION_REQUEST_APPROVE = 11;
/**
* content of page for refused request for connection
*/
const CONNECTION_REQUEST_REFUSE = 12;
/**
* content of page for information about request for connection
*/
const CONNECTION_REQUEST_INFO = 13;
/**
* content of page for information about host down in monitoring
*/
const MONITORING_HOST_DOWN = 14;
/**
* content of page for information about host up in monitoring
*/
const MONITORING_HOST_UP = 15;
/**
* content of page for information about connection test expiration
*/
const CONNECTION_TEST_EXPIRED = 16;
/**
* notification e-mail message that is sended to a interupted member
* at the start of his interuption
*/
const INTERRUPTED_MEMBERSHIP_BEGIN_NOTIFY_MESSAGE = 17;
/**
* notification e-mail message that is sended to a interupted member
* at the end of his interuption
*/
const INTERRUPTED_MEMBERSHIP_END_NOTIFY_MESSAGE = 18;
/**
* content of page for information about former membership
* e-mail content of this message is send to the former member at the day
* when the end membership begins
*/
const FORMER_MEMBER_MESSAGE = 19;
8baed187 Michal Kliment
74a7dbca Michal Kliment
/**
* content of e-mail for contact verification
*/
const VERIFY_CONTACT_MESSAGE = 20;
8baed187 Michal Kliment
// self-cancel constants
/**
* self cancel disabled, remote computer cannot cancel this message
*/
c1bdc1c4 Michal Kliment
const SELF_CANCEL_DISABLED = 0;
8baed187 Michal Kliment
/**
* self cancel enabled, every member's IP address will have cancelled
* given redirection
*/
c1bdc1c4 Michal Kliment
const SELF_CANCEL_MEMBER = 1;
8baed187 Michal Kliment
/**
* self cancel enabled, redirection is canceled only for current remote computer
*/
c1bdc1c4 Michal Kliment
const SELF_CANCEL_IP = 2;
8baed187 Michal Kliment
c1bdc1c4 Michal Kliment
/**
* Message's types
*
* @author Michal Kliment
* @var type
*/
private static $types = array
(
self::USER_MESSAGE => 'User message',
self::CONTACT_INFORMATION => 'Contact information',
self::CANCEL_MESSAGE => 'Page after canceling redirection',
self::UNKNOWN_DEVICE_MESSAGE => 'Unknown device',
self::INTERRUPTED_MEMBERSHIP_MESSAGE => 'Interrupted membership message',
self::DEBTOR_MESSAGE => 'Debtor message',
self::PAYMENT_NOTICE_MESSAGE => 'Payment notice',
self::UNALLOWED_CONNECTING_PLACE_MESSAGE => 'Unallowed connecting place',
self::RECEIVED_PAYMENT_NOTICE_MESSAGE => 'Received payment notice',
self::APPLICANT_APPROVE_MEMBERSHIP => 'Application for membership approved',
self::APPLICANT_REFUSE_MEMBERSHIP => 'Application for membership rejected',
self::CONNECTION_REQUEST_APPROVE => 'Request for connection approved',
self::CONNECTION_REQUEST_REFUSE => 'Request for connection rejected',
self::CONNECTION_REQUEST_INFO => 'Notice of adding connection request',
self::MONITORING_HOST_DOWN => 'Monitoring host down',
self::MONITORING_HOST_UP => 'Monitoring host up',
self::CONNECTION_TEST_EXPIRED => 'Test connection has expired',
self::INTERRUPTED_MEMBERSHIP_BEGIN_NOTIFY_MESSAGE => 'Membership interrupt begins notification',
self::INTERRUPTED_MEMBERSHIP_END_NOTIFY_MESSAGE => 'Membership interrupt ends notification',
self::FORMER_MEMBER_MESSAGE => 'Former member message',
74a7dbca Michal Kliment
self::VERIFY_CONTACT_MESSAGE => 'Verify contact message'
c1bdc1c4 Michal Kliment
);
/**
* Self cancel messages
*
* @author Ondřej Fibich
* @var array
*/
private static $self_cancel_messages = array
(
self::SELF_CANCEL_DISABLED => 'Disabled',
self::SELF_CANCEL_MEMBER => 'Possibility of canceling redirection to all IP addresses of member',
self::SELF_CANCEL_IP => 'Possibility of canceling redirection to only current IP address'
);
/**
* Returns all message's types
*
* @author Michal Kliment
* @return type
*/
public static function get_types()
{
return array_map('__', self::$types);
}


/**
* Gets set of self cancel messages
*
* @author Ondřej Fibich
* @param bool $translate Translate messages
* @return array
*/
public static function get_self_cancel_messages($translate = TRUE)
{
if ($translate)
{
return array_map('__', self::$self_cancel_messages);
}
return self::$self_cancel_messages;
}
/**
* Check if message may be self cancable.
*
* @author Ondřej Fibich
* @param integer $type Type of message
* @return boolean
*/
public static function is_self_cancable($type)
{
return (
$type == self::USER_MESSAGE ||
$type == self::PAYMENT_NOTICE_MESSAGE ||
$type == self::RECEIVED_PAYMENT_NOTICE_MESSAGE
);
}
/**
* Check if message may ignore the white list.
*
* @author Ondřej Fibich
* @param integer $type Type of message
* @return boolean
*/
public static function is_white_list_ignorable($type)
{
return (
$type == self::USER_MESSAGE ||
$type == self::INTERRUPTED_MEMBERSHIP_MESSAGE ||
$type == self::DEBTOR_MESSAGE ||
$type == self::PAYMENT_NOTICE_MESSAGE ||
$type == self::UNALLOWED_CONNECTING_PLACE_MESSAGE ||
$type == self::INTERRUPTED_MEMBERSHIP_BEGIN_NOTIFY_MESSAGE ||
$type == self::INTERRUPTED_MEMBERSHIP_END_NOTIFY_MESSAGE ||
$type == self::FORMER_MEMBER_MESSAGE
);
}
/**
* Check if message is special message.
*
* @author Ondřej Fibich
* @param integer $type Type of message
* @return boolean
*/
public static function is_special_message($type)
{
return (
$type == self::RECEIVED_PAYMENT_NOTICE_MESSAGE ||
$type == self::APPLICANT_APPROVE_MEMBERSHIP ||
$type == self::APPLICANT_REFUSE_MEMBERSHIP ||
$type == self::CONNECTION_REQUEST_APPROVE ||
$type == self::CONNECTION_REQUEST_REFUSE ||
$type == self::CONNECTION_REQUEST_INFO ||
$type == self::MONITORING_HOST_DOWN ||
$type == self::MONITORING_HOST_UP ||
$type == self::CONNECTION_TEST_EXPIRED ||
$type == self::INTERRUPTED_MEMBERSHIP_BEGIN_NOTIFY_MESSAGE ||
$type == self::INTERRUPTED_MEMBERSHIP_END_NOTIFY_MESSAGE
);
}
/**
* Check if message contains redirection content.
*
* @author Ondřej Fibich
* @param integer $type Type of message
* @return boolean
*/
public static function has_redirection_content($type)
{
return (
$type != self::RECEIVED_PAYMENT_NOTICE_MESSAGE &&
$type != self::APPLICANT_APPROVE_MEMBERSHIP &&
$type != self::APPLICANT_REFUSE_MEMBERSHIP &&
$type != self::CONNECTION_REQUEST_APPROVE &&
$type != self::CONNECTION_REQUEST_REFUSE &&
$type != self::CONNECTION_REQUEST_INFO &&
$type != self::MONITORING_HOST_DOWN &&
$type != self::MONITORING_HOST_UP &&
$type != self::INTERRUPTED_MEMBERSHIP_BEGIN_NOTIFY_MESSAGE &&
74a7dbca Michal Kliment
$type != self::INTERRUPTED_MEMBERSHIP_END_NOTIFY_MESSAGE &&
$type != self::VERIFY_CONTACT_MESSAGE
c1bdc1c4 Michal Kliment
);
}
/**
* Check if message contains email content.
*
* @author Ondřej Fibich
* @param integer $type Type of message
* @return boolean
*/
public static function has_email_content($type)
{
return (
$type == self::USER_MESSAGE ||
$type == self::DEBTOR_MESSAGE ||
$type == self::PAYMENT_NOTICE_MESSAGE ||
$type == self::RECEIVED_PAYMENT_NOTICE_MESSAGE ||
$type == self::APPLICANT_APPROVE_MEMBERSHIP ||
$type == self::APPLICANT_REFUSE_MEMBERSHIP ||
$type == self::CONNECTION_REQUEST_APPROVE ||
$type == self::CONNECTION_REQUEST_REFUSE ||
$type == self::CONNECTION_REQUEST_APPROVE ||
$type == self::CONNECTION_REQUEST_INFO ||
$type == self::MONITORING_HOST_DOWN ||
$type == self::MONITORING_HOST_UP ||
$type == self::INTERRUPTED_MEMBERSHIP_BEGIN_NOTIFY_MESSAGE ||
$type == self::INTERRUPTED_MEMBERSHIP_END_NOTIFY_MESSAGE ||
74a7dbca Michal Kliment
$type == self::FORMER_MEMBER_MESSAGE ||
$type == self::VERIFY_CONTACT_MESSAGE
c1bdc1c4 Michal Kliment
);
}
/**
* Check if message contains sms content.
*
* @author Ondřej Fibich
* @param integer $type Type of message
* @return boolean
*/
public static function has_sms_content($type)
{
return (
$type == self::USER_MESSAGE ||
$type == self::DEBTOR_MESSAGE ||
$type == self::PAYMENT_NOTICE_MESSAGE ||
$type == self::RECEIVED_PAYMENT_NOTICE_MESSAGE ||
$type == self::CONNECTION_REQUEST_APPROVE ||
$type == self::CONNECTION_REQUEST_REFUSE ||
$type == self::INTERRUPTED_MEMBERSHIP_BEGIN_NOTIFY_MESSAGE ||
$type == self::INTERRUPTED_MEMBERSHIP_END_NOTIFY_MESSAGE ||
$type == self::FORMER_MEMBER_MESSAGE
);
}
/**
* Check if message can be activated directly.
*
* @author Ondřej Fibich
* @param integer $type Type of message
* @return boolean
*/
public static function can_be_activate_directly($type)
{
return (
$type != self::CONTACT_INFORMATION &&
$type != self::CANCEL_MESSAGE &&
$type != self::UNKNOWN_DEVICE_MESSAGE &&
$type != self::RECEIVED_PAYMENT_NOTICE_MESSAGE &&
$type != self::APPLICANT_APPROVE_MEMBERSHIP &&
$type != self::APPLICANT_REFUSE_MEMBERSHIP &&
$type != self::CONNECTION_REQUEST_APPROVE &&
$type != self::CONNECTION_REQUEST_REFUSE &&
$type != self::CONNECTION_REQUEST_INFO &&
$type != self::UNALLOWED_CONNECTING_PLACE_MESSAGE &&
$type != self::MONITORING_HOST_DOWN &&
$type != self::MONITORING_HOST_UP &&
$type != self::CONNECTION_TEST_EXPIRED &&
$type != self::INTERRUPTED_MEMBERSHIP_MESSAGE &&
$type != self::INTERRUPTED_MEMBERSHIP_BEGIN_NOTIFY_MESSAGE &&
$type != self::INTERRUPTED_MEMBERSHIP_END_NOTIFY_MESSAGE &&
74a7dbca Michal Kliment
$type != self::FORMER_MEMBER_MESSAGE &&
$type != self::VERIFY_CONTACT_MESSAGE
c1bdc1c4 Michal Kliment
);
}
/**
* Check if message can be activated manually.
*
* @param integer $type Message type
* @return boolean
*/
public static function can_be_activate_automatically($type)
{
return (
$type == self::DEBTOR_MESSAGE ||
$type == self::PAYMENT_NOTICE_MESSAGE
);
}
8baed187 Michal Kliment
/**
* Counts all activated redirections from junction table messages_ip_addresses.
*
* @author Jiri Svitak, Ondrej Fibich
* @return integer
*/
public function count_all_redirections($filter_sql = '')
{
if (!empty($filter_sql))
{
$where = "WHERE $filter_sql";
}
else
{
// Optimalization:
// don't want search throught staff down here because of filter
// which will be still unused
return $this->db->count_records('messages_ip_addresses');
}
return $this->db->query("
SELECT COUNT(*) AS total FROM
(
SELECT mip.*, mm.name AS member_name FROM
(
SELECT mip.ip_address_id, ip.ip_address, ms.name AS message,
mip.datetime, mip.comment, ms.self_cancel, ms.type,
IFNULL(u.member_id,ip.member_id) AS member_id
FROM messages_ip_addresses mip
LEFT JOIN ip_addresses ip ON mip.ip_address_id = ip.id
LEFT JOIN ifaces i ON ip.iface_id = i.id
LEFT JOIN messages ms ON mip.message_id = ms.id
LEFT JOIN devices d ON d.id = i.device_id
LEFT JOIN users u ON u.id = d.user_id
) mip
LEFT JOIN members mm ON mm.id = mip.member_id
) mip
$where
")->current()->total;
}
/**
* Gets all activated redirections from junction table messages_ip_addresses.
*
* @return Mysql_Result
*/
public function get_all_redirections(
$limit_from = 0, $limit_results = 20, $order_by = 'ip_address',
$order_by_direction = 'ASC', $filter_sql = '')
{
// direction
if (strtolower($order_by_direction) != 'asc')
{
$order_by_direction = 'DESC';
}
// order by check
if ($order_by == 'ip_address')
{
//$order_by = 'inet_aton(ip_address)';
$order_by = 'inet_aton(ip_address) ASC, self_cancel DESC, mip.datetime ASC';
$order_by_direction = '';
}
else
{
$order_by = $this->db->escape_column($order_by);
}
$where = '';
if ($filter_sql)
$where = "WHERE $filter_sql";
// query
return $this->db->query("
SELECT * FROM
(
SELECT mip.*, mm.name AS member_name FROM
(
SELECT mip.ip_address_id, ip.ip_address, ms.name AS message,
mip.datetime, mip.comment, ms.self_cancel, ms.type,
IFNULL(u.member_id,ip.member_id) AS member_id
FROM messages_ip_addresses mip
LEFT JOIN ip_addresses ip ON mip.ip_address_id = ip.id
LEFT JOIN ifaces i ON ip.iface_id = i.id
LEFT JOIN messages ms ON mip.message_id = ms.id
LEFT JOIN devices d ON d.id = i.device_id
LEFT JOIN users u ON u.id = d.user_id
) mip
LEFT JOIN members mm ON mm.id = mip.member_id
) mip
$where
ORDER BY $order_by $order_by_direction
LIMIT " . intval($limit_from) . ", " . intval($limit_results) . "
");
}
/**
* Counts all messages.
*
* @return integer
*/
c1bdc1c4 Michal Kliment
public function count_all_messages($filter_sql = '')
8baed187 Michal Kliment
{
c1bdc1c4 Michal Kliment
return $this->get_all_messages(NULL, NULL, '', '', $filter_sql)
->count();
8baed187 Michal Kliment
}
/**
* Gets all redirection messages.
*
* @author Jiri Svitak
* @param integer $limit_from
* @param integer $limit_results
* @param string $order_by
* @param string $order_by_direction
* @param array $filter_values
* @return Mysql_Result
*/
public function get_all_messages(
$limit_from = 0, $limit_results = 20,
$order_by = 'id', $order_by_direction = 'asc',
c1bdc1c4 Michal Kliment
$filter_sql = '')
8baed187 Michal Kliment
{
// order by check
if (!$this->has_column($order_by))
{
$order_by = 'id';
}
// order by direction check
if (strtolower($order_by_direction) != 'desc')
{
$order_by_direction = 'asc';
}
c1bdc1c4 Michal Kliment
$where = array();
if (!Settings::get('finance_enabled'))
{
$where[] = "m.type <> " . intval(self::PAYMENT_NOTICE_MESSAGE);
$where[] = "m.type <> " . intval(self::DEBTOR_MESSAGE);
$where[] = "m.type <> " . intval(self::RECEIVED_PAYMENT_NOTICE_MESSAGE);
}
if (!Settings::get('membership_interrupt_enabled'))
{
$where[] = "m.type <> " . intval(self::INTERRUPTED_MEMBERSHIP_MESSAGE);
$where[] = "m.type <> " . intval(self::INTERRUPTED_MEMBERSHIP_BEGIN_NOTIFY_MESSAGE);
$where[] = "m.type <> " . intval(self::INTERRUPTED_MEMBERSHIP_END_NOTIFY_MESSAGE);
}
if (!Settings::get('monitoring_enabled'))
{
$where[] = "m.type <> " . intval(self::MONITORING_HOST_UP);
$where[] = "m.type <> " . intval(self::MONITORING_HOST_DOWN);
}
if (!Settings::get('connection_request_enable'))
{
$where[] = "m.type <> " . intval(self::CONNECTION_REQUEST_APPROVE);
$where[] = "m.type <> " . intval(self::CONNECTION_REQUEST_INFO);
$where[] = "m.type <> " . intval(self::CONNECTION_REQUEST_REFUSE);
}
if (!Settings::get('allowed_subnets_enabled'))
{
$where[] = "m.type <> " . intval(self::UNALLOWED_CONNECTING_PLACE_MESSAGE);
}
if (!Settings::get('self_registration'))
{
$where[] = "m.type <> " . intval(self::APPLICANT_APPROVE_MEMBERSHIP);
$where[] = "m.type <> " . intval(self::APPLICANT_REFUSE_MEMBERSHIP);
$where[] = "m.type <> " . intval(self::CONNECTION_TEST_EXPIRED);
}
else if (Settings::get('applicant_connection_test_duration') <= 0)
{
$where[] = "m.type <> " . intval(self::CONNECTION_TEST_EXPIRED);
}
if ($filter_sql != '')
$where[] = $filter_sql;
$where_sql = (count($where)) ? 'WHERE ' . implode(' AND ', $where) : '';
$limit = '';
if (!is_null($limit_from) && !is_null($limit_results))
{
$limit = "LIMIT " . intval($limit_from) . ", " . intval($limit_results);
}
8baed187 Michal Kliment
// query
return $this->db->query("
SELECT m.id, m.name AS message, m.type, m.self_cancel,
m.ignore_whitelist
FROM messages m
c1bdc1c4 Michal Kliment
$where_sql
8baed187 Michal Kliment
ORDER BY $order_by $order_by_direction
c1bdc1c4 Michal Kliment
$limit
8baed187 Michal Kliment
");
}
c1bdc1c4 Michal Kliment
/**
* Deny ip addresses with expired test connections
*
* @see Scheduler_Controller#update_applicant_connection_test
* @author Ondrej Fibich
* @param integer $user_id Who redirects
*/
public function activate_test_connection_end_message($user_id)
{
$mm = new Message_Model();
try
{
$mm->transaction_start();
// preparation
$message = ORM::factory('message')->where(array
(
'type' => self::CONNECTION_TEST_EXPIRED
))->find();

// message do not exists
if (!$message || !$message->id)
{
throw new Exception('Connection test expired message not founded');
}

// deletes old redirections
Database::instance()->delete('messages_ip_addresses', array
(
'message_id' => $message->id
));

// find IP addresses with interrupted membership
$ip_model = new Ip_address_Model();

$ips = $ip_model->get_ip_addresses_with_expired_connection_test();

// activate
$result = self::activate_redirection($message, $ips, $user_id);
$mm->transaction_commit();
return $result;
}
catch (Exception $e)
{
$mm->transaction_rollback();
Log::add_exception($e);
return 0;
}
}
8baed187 Michal Kliment
/**
* Activates notifications for ip addresses with unallowed connecting place
*
* @author Michal Kliment
* @param integer $user_id Who redirects
* @return integer Number of ip addresses activated
*/
public function activate_unallowed_connecting_place_message($user_id)
{
$mm = new Message_Model();
try
{
$mm->transaction_start();
// preparation
$message = ORM::factory('message')->where(array
(
'type' => self::UNALLOWED_CONNECTING_PLACE_MESSAGE
))->find();

// message do not exists
if (!$message || !$message->id)
{
throw new Exception('Unallowed connecting place message not founded');
}

// deletes old redirections
Database::instance()->delete('messages_ip_addresses', array
(
'message_id' => $message->id
));

// find IP addresses with interrupted membership
$ip_model = new Ip_address_Model();

$ips = $ip_model->get_ip_addresses_with_unallowed_connecting_place();

// activate
$result = self::activate_redirection($message, $ips, $user_id);
$mm->transaction_commit();
return $result;
}
catch (Exception $e)
{
$mm->transaction_rollback();
Log::add_exception($e);
return 0;
}
}
/**
c1bdc1c4 Michal Kliment
* Activates notice messages (only e-mail and sometimes also SMS) for accept
* payment notice, approoving/refusing of application for membership
* to a single member.
8baed187 Michal Kliment
*
* @author Ondřej Fibich
c1bdc1c4 Michal Kliment
* @param integer $type 8 => payment, 9 => application approve, 10 => application refuse
* @param integer $member_id Member ID
8baed187 Michal Kliment
* @param integer $user_id Who redirects
* @param integer $email E-mail state
* @param integer $sms SMS state
c1bdc1c4 Michal Kliment
* @param string $comment Comment for member
* @return boolean Notification made?
8baed187 Michal Kliment
*/
c1bdc1c4 Michal Kliment
public static function activate_special_notice(
$type, $member_id, $user_id, $email, $sms, $comment = NULL)
8baed187 Michal Kliment
{
c1bdc1c4 Michal Kliment
if (!self::is_special_message($type))
{
throw new Exception('Wrong type');
}
8baed187 Michal Kliment
c1bdc1c4 Michal Kliment
// preparation
$message = ORM::factory('message')->where(array('type' => $type))->find();
8baed187 Michal Kliment
$uc_model = new Users_contacts_Model();
// message do not exists
if (!$message || !$message->id)
{
c1bdc1c4 Michal Kliment
Log::add('error', 'Notice message (' . $type . ') not exists in messages table');
return false;
8baed187 Michal Kliment
}
c1bdc1c4 Michal Kliment
// member notification settings only enabled for self cancel messages
$ignore_mnotif_settings = !$message->self_cancel;
8baed187 Michal Kliment
// send emails
c1bdc1c4 Michal Kliment
if (Settings::get('email_enabled') &&
$email == Notifications_Controller::ACTIVATE)
8baed187 Michal Kliment
{
// find email addresses of debtors
c1bdc1c4 Michal Kliment
$emails = $uc_model->get_contacts_by_member_and_type(
$member_id, Contact_Model::TYPE_EMAIL,
$message->ignore_whitelist, FALSE, FALSE,
$ignore_mnotif_settings
8baed187 Michal Kliment
);
// send emails for finded emails
c1bdc1c4 Michal Kliment
self::send_emails($message, $emails, $comment);
8baed187 Michal Kliment
}
// send SMS messages
c1bdc1c4 Michal Kliment
if (Settings::get('sms_enabled') &&
$sms == Notifications_Controller::ACTIVATE)
8baed187 Michal Kliment
{
// find phone numbers of debtors
c1bdc1c4 Michal Kliment
$smss = $uc_model->get_contacts_by_member_and_type(
$member_id, Contact_Model::TYPE_PHONE,
$message->ignore_whitelist, FALSE, FALSE,
$ignore_mnotif_settings
8baed187 Michal Kliment
);
// send SMS messages for finded phone numbers
c1bdc1c4 Michal Kliment
self::send_sms_messages($message, $smss, $user_id, $comment);
8baed187 Michal Kliment
}
// return stats array
c1bdc1c4 Michal Kliment
return true;
8baed187 Michal Kliment
}

/**
* Deactivates all redirections of given message.
*
* @author Jiri Svitak
* @param integer $message_id
c1bdc1c4 Michal Kliment
* @return integer Count of deactivated IPs
8baed187 Michal Kliment
*/
public function deactivate_message($message_id)
{
c1bdc1c4 Michal Kliment
$count = $this->db->query("
SELECT COUNT(*) AS count
FROM messages_ip_addresses
WHERE message_id = ?
", $message_id)->current()->count;
8baed187 Michal Kliment
$this->db->query("
DELETE FROM messages_ip_addresses
WHERE message_id = ?
", $message_id);
c1bdc1c4 Michal Kliment
return $count;
}

/**
* Returns message by given type
*
* @param integer $type
* @return Message_Model
*/
public function get_message_by_type($type)
{
if (!$type)
{
return NULL;
}
$message = ORM::factory('message')->where('type', $type)->find();
if ($message->id)
{
return $message;
}
else
{
return NULL;
}
8baed187 Michal Kliment
}

/**
* Returns id of message by given type
*
* @author Michal Kliment
* @param integer $type
* @return integer
*/
public function get_message_id_by_type($type)
{
if (!$type)
{
return NULL;
}
$message = $this->where('type', $type)->find();
if ($message->id)
{
return $message->id;
}
else
{
return NULL;
}
}
/**
* Activate redirection for given IPs
*
* @param Message_Model $message Message to redirect
* @param Mysql_Result $ips IPs to redirect
* @param mixed $user_id ID of who added redirection or NULL
* @param string $comment Comment
* @return integer Number of redirected IPs
* @throws Exception On error
*/
public static function activate_redirection(
Message_Model $message, $ips, $user_id = NULL, $comment = NULL)
{
// param check
if (!$message->id || !is_object($ips))
{
throw new Exception('Wrong args');
}
c1bdc1c4 Michal Kliment
// empty message
if (trim($message->text) == '')
{
return 0;
}
8baed187 Michal Kliment
// preparations
$datetime = date('Y-m-d H:i:s');
$user_id = (intval($user_id)) ? intval($user_id) : 'NULL';
$comment = Database::instance()->escape($comment);
// ip count stats
$ip_count = 0;

// first sql for inserting transfers
$sql_insert = "REPLACE messages_ip_addresses "
. "(message_id, ip_address_id, user_id, comment, datetime) "
. "VALUES ";

$values = array();
// set new redirections in junction table
foreach($ips as $ip)
{
// insert values
$values[] = "($message->id, $ip->id, $user_id, $comment, '$datetime')";
$ip_count++;
}

// any redirection?
if (count($values) > 0)
{
$sql_insert .= implode(',', $values);

if (!Database::instance()->query($sql_insert))
{
throw new Exception();
}
}
return $ip_count;
}
/**
* Send emails with redirection message to given contacts
*
* @param Message_Model $message Message to send
* @param Mysql_Result $contacts Contacts to send
* @param string $comment Comment
* @return integer Number of sended emails
* @throws Exception On error
*/
public static function send_emails(
Message_Model $message, $contacts, $comment = NULL)
{
// param check
if (!$message->id || !is_object($contacts))
{
throw new Exception('Wrong args');
}
// emails counter
$email_count = 0;
// if default email ans subject prefix is set
if (!Settings::get('email_default_email') ||
!Settings::get('email_subject_prefix'))
{
throw new Exception('Email not configured properly');
}
// continue
try
{
// Email queues model
$eq_model = new Email_queue_Model();
// start transaction
$eq_model->transaction_start();
c1bdc1c4 Michal Kliment
8baed187 Michal Kliment
// for each contact
foreach ($contacts as $contact)
{
// text of message
$text = $message->email_text;

// replace tags
foreach ($contact as $key => $value)
{
if ($key != 'email_text' && $key != 'country_code')
{
$text = str_replace('{'.$key.'}', $value, $text);
}
}
// replace comment
$text = str_replace('{comment}', $comment, $text);

// if empty message do not send
if (empty($text))
{
continue;
}

// subject
$subject = Settings::get('email_subject_prefix')
. ': ' . __($message->name);

c1bdc1c4 Michal Kliment
// add Email to queue
8baed187 Michal Kliment
$eq_model->clear();
$eq_model->from = Settings::get('email_default_email');
$eq_model->to = $contact->value;
$eq_model->subject = $subject;
$eq_model->body = $text;
$eq_model->state = Email_queue_Model::STATE_NEW;
$eq_model->save_throwable();
c1bdc1c4 Michal Kliment
// add E-mail to counter
8baed187 Michal Kliment
$email_count++;
}
c1bdc1c4 Michal Kliment
8baed187 Michal Kliment
// commit
$eq_model->transaction_commit();
}
catch (Exception $e)
{
$eq_model->transaction_rollback();
Log::add_exception($e);
throw $e;
}

return $email_count;
}
c1bdc1c4 Michal Kliment
/**
* Generic function to send e-mail
*
* @author Michal Kliment <kliment@freenetis.org>
* @param Message_Model $message Message which will be send
* @param string $email E-mail address where send e-mail
* @param mixed $object Generic object to autofill message variables
* @param boolean $push Whether message will be push on top of queue
* @throws Exception
*/
public static function send_email (Message_Model $message, $email, $object = NULL, $push = FALSE)
{
if (!Settings::get('email_enabled'))
{
throw new Exception('E-mail is not enabled');
}
// param check
if (!$message->id)
{
throw new Exception('Wrong args');
}
// if default email ans subject prefix is set
if (!Settings::get('email_default_email'))
{
throw new Exception('Email not configured properly');
}
$subject = $message->name;
$text = $message->email_text;
foreach ($object as $key => $value)
{
$subject = str_replace('{'.$key.'}', $value, $subject);
$text = str_replace('{'.$key.'}', $value, $text);
}
try
{
$email_queue_model = new Email_queue_Model();
$email_queue_model->transaction_start();
// push e-mail on top of queue?
if ($push)
{
$email_queue_model->push(
Settings::get('email_default_email'),
$email, $subject, $text
);
}
else
{
$email_queue_model->from = Settings::get('email_default_email');
$email_queue_model->to = $email;
$email_queue_model->subject = $subject;
$email_queue_model->body = $text;
$email_queue_model->state = Email_queue_Model::STATE_NEW;
$email_queue_model->save_throwable();
}
$email_queue_model->transaction_commit();
}
catch (Exception $e)
{
$email_queue_model->transaction_rollback();
Log::add_exception($e);
throw $e;
}
}
8baed187 Michal Kliment
/**
* Send SMS messages with redirection message to given contacts
*
* @param Message_Model $message Message to send
* @param Mysql_Result $contacts Contacts to send
* @param mixed $user_id ID of who added redirection or NULL
* @param string $comment Comment
* @return integer Number of sended SMS
* @throws Exception On error
*/
public static function send_sms_messages(
Message_Model $message, $contacts, $user_id = NULL, $comment = NULL)
{
// param check
if (!$message->id || !is_object($contacts))
{
throw new Exception('Wrong args');
}
c1bdc1c4 Michal Kliment
// check if enabled
if (!Settings::get('sms_enabled'))
{
throw new Exception('SMS are not enabled');
}
8baed187 Michal Kliment
// preparations
$datetime = date('Y-m-d H:i:s');
$user_id = (intval($user_id)) ? intval($user_id) : 'NULL';
$comment = strip_tags($comment);
// ip count stats
$sms_count = 0;
// if SMS enabled, is any active SMS driver
// SMS sender number is set and SMS default driver is set
if (!Sms::enabled() ||
!Sms::has_active_driver() ||
!Settings::get('sms_sender_number') ||
!Settings::get('sms_driver'))
{
return 0;
}
// continue
try
{
// SMS model
$sms = new Sms_message_Model();
// start transaction
$sms->transaction_start();

// for each contact
foreach ($contacts as $contact)
{
// text of message
$text = $message->sms_text;
// number
$number = $contact->country_code . $contact->value;

// replace tags
foreach ($contact as $key => $value)
{
if ($key != 'sms_text' && $key != 'country_code')
{
$text = str_replace('{'.$key.'}', $value, $text);
}
}
// replace comment
$text = str_replace('{comment}', $comment, $text);

// if empty message or invalid number for sending SMS do not send
if (empty($text) || !Phone_operator_Model::is_sms_enabled_for($number))
{
continue;
}

// add SMS
$sms->clear();
$sms->user_id = $user_id;
$sms->sms_message_id = NULL;
$sms->stamp = $datetime;
$sms->send_date = $datetime;
$sms->text = text::cs_utf2ascii($text);
$sms->sender = Settings::get('sms_sender_number');
$sms->receiver = $number;
$sms->driver = Settings::get('sms_driver');
$sms->type = Sms_message_Model::SENT;
$sms->state = Sms_message_Model::SENT_UNSENT;
$sms->save_throwable();

// add SMS to counter
$sms_count++;
}

// end transaction
$sms->transaction_commit();
}
catch (Exception $e)
{
$sms->transaction_rollback();
Log::add_exception($e);
throw $e;
}
return $sms_count;
}
}