示例#1
0
 protected function updateMemberships()
 {
     $db = JFactory::getDbo();
     $query = $db->getQuery(true);
     $config = RSMembershipConfig::getInstance();
     $date = JFactory::getDate();
     $unixDate = $date->toUnix();
     if ($config->get('last_check') + $config->get('interval') * 60 > $unixDate) {
         return;
     }
     // update config value Last Check
     $config->set('last_check', $unixDate);
     $offset = $config->get('delete_pending_after');
     if ($offset < 1) {
         $offset = 1;
     }
     $offset = $offset * 3600;
     // delete pending transactions
     $date->modify("-{$offset} seconds");
     $query->delete()->from($db->qn('#__rsmembership_transactions'))->where($db->qn('status') . ' = ' . $db->q('pending'))->where($db->qn('date') . ' < ' . $db->q($date->toSql()));
     $db->setQuery($query);
     $db->execute();
     $query->clear();
     // Limit 10 so we don't overload the server
     $query->select($db->qn('mu.id'))->select($db->qn('m.gid_enable'))->select($db->qn('m.gid_expire'))->select($db->qn('m.disable_expired_account'))->select($db->qn('mu.user_id'))->from($db->qn('#__rsmembership_membership_subscribers', 'mu'))->join('left', $db->qn('#__rsmembership_memberships', 'm') . ' ON ' . $db->qn('mu.membership_id') . ' = ' . $db->qn('m.id'))->where($db->qn('mu.status') . ' = ' . $db->q('0'))->where($db->qn('mu.membership_end') . ' != ' . $db->q($db->getNullDate()))->where($db->qn('mu.membership_end') . ' < ' . $db->q(JFactory::getDate()->toSql()));
     $db->setQuery($query, 0, 10);
     $updates = $db->loadObjectList('id');
     $query->clear();
     $to_update = array_keys($updates);
     if (!empty($to_update)) {
         $query->update($db->qn('#__rsmembership_membership_subscribers'))->set($db->qn('status') . ' = ' . $db->q(MEMBERSHIP_STATUS_EXPIRED))->where($db->qn('id') . ' IN (\'' . implode($db->q(','), $to_update) . '\')');
         $db->setQuery($query);
         $db->execute();
         $query->clear();
     }
     foreach ($updates as $update) {
         if ($update->gid_enable) {
             RSMembership::updateGid($update->user_id, $update->gid_expire, false, 'remove');
         }
         if ($update->disable_expired_account) {
             // Do not disable the user if he has active subscriptions.
             list($memberships, $extras) = RSMembershipHelper::getUserSubscriptions($update->user_id);
             if (!$memberships) {
                 RSMembership::disableUser($update->user_id);
             }
         }
     }
 }
示例#2
0
 function onAfterInitialise()
 {
     if (class_exists('RSMembershipHelper')) {
         $this->loadLanguage('plg_system_rsmembership');
         $db = JFactory::getDBO();
         $date = JFactory::getDate();
         $date = $date->toUnix();
         if (RSMembershipHelper::getConfig('last_check') + RSMembershipHelper::getConfig('interval') * 60 > $date) {
             return;
         }
         $db->setQuery("UPDATE #__rsmembership_configuration SET value='" . $date . "' WHERE name='last_check' LIMIT 1");
         $db->query();
         $offset = RSMembershipHelper::getConfig('delete_pending_after');
         if ($offset < 1) {
             $offset = 1;
         }
         $offset = $offset * 3600;
         $db->setQuery("DELETE FROM #__rsmembership_transactions WHERE `status`='pending' AND `date` < '" . ($date - $offset) . "'");
         $db->query();
         // Limit 10 so we don't overload the server
         $db->setQuery("SELECT mu.id, m.gid_enable, m.gid_expire, m.disable_expired_account, mu.user_id FROM #__rsmembership_membership_users mu LEFT JOIN #__rsmembership_memberships m ON (mu.membership_id=m.id) WHERE  mu.`status`='0' AND mu.`membership_end` > 0 AND mu.`membership_end` < '" . $date . "' LIMIT 10");
         $updates = $db->loadObjectList();
         $to_update = array();
         foreach ($updates as $update) {
             $to_update[] = $update->id;
             if ($update->gid_enable) {
                 RSMembership::updateGid($update->user_id, $update->gid_expire);
             }
             if ($update->disable_expired_account) {
                 RSMembership::disableUser($update->user_id);
             }
         }
         if (!empty($to_update)) {
             $db->setQuery("UPDATE #__rsmembership_membership_users SET `status`='2' WHERE `id` IN (" . implode(',', $to_update) . ")");
             $db->query();
         }
         RSMembershipHelper::checkShared();
         RSMembershipHelper::sendExpirationEmails();
     }
 }
示例#3
0
 public function save($data)
 {
     $membership_fields = JRequest::getVar('rsm_membership_fields', array(), 'post', 'array');
     $last_transaction_id = JRequest::getVar('last_transaction_id', '', 'post');
     if (count($membership_fields)) {
         $transaction = JTable::getInstance('Transaction', 'RSMembershipTable');
         $transaction->load($last_transaction_id);
         $user_data = $transaction->user_data ? (object) unserialize($transaction->user_data) : (object) array();
         $user_data->membership_fields = $membership_fields;
         $transaction->user_data = serialize($user_data);
         if ($transaction->check()) {
             $transaction->store();
         }
     }
     if (!isset($data['extras'])) {
         $data['extras'] = '';
     } else {
         $data['extras'] = implode(',', $data['extras']);
     }
     // Updating ? Make sure we check the status
     if ($data['id']) {
         $current = $this->getTable();
         $current->load($data['id']);
         if ($current->status == MEMBERSHIP_STATUS_ACTIVE && $data['status'] != MEMBERSHIP_STATUS_ACTIVE) {
             $data['notified'] = 0;
         }
     }
     // Handle dates
     $offset = JFactory::getApplication()->getCfg('offset');
     $data['membership_start'] = JFactory::getDate($data['membership_start'], $offset)->toSql();
     if (isset($data['unlimited'])) {
         $data['membership_end'] = JFactory::getDbo()->getNullDate();
         unset($data['unlimited']);
     } else {
         $data['membership_end'] = JFactory::getDate($data['membership_end'], $offset)->toSql();
     }
     $result = parent::save($data);
     // Save was successful
     if ($result) {
         $membership = JTable::getInstance('Membership', 'RSMembershipTable');
         $membership->load($data['membership_id']);
         if ($data['status'] == MEMBERSHIP_STATUS_ACTIVE) {
             if ($membership->gid_enable) {
                 RSMembership::updateGid($data['user_id'], $membership->gid_subscribe, true);
             }
             if ($membership->disable_expired_account) {
                 RSMembership::enableUser($data['user_id']);
             }
         } elseif ($data['status'] == MEMBERSHIP_STATUS_EXPIRED || $data['status'] == MEMBERSHIP_STATUS_CANCELLED) {
             if ($membership->gid_enable) {
                 RSMembership::updateGid($data['user_id'], $membership->gid_expire, false, 'remove');
             }
             if ($membership->disable_expired_account) {
                 RSMembership::disableUser($data['user_id']);
             }
         }
     }
     return $result;
 }
示例#4
0
 function cancel()
 {
     $user = JFactory::getUser();
     $id = $user->get('id');
     $cid = $this->getCid();
     $this->_db->setQuery("SELECT * FROM #__rsmembership_transactions WHERE `id`='" . $this->_data->from_transaction_id . "'");
     $transaction = $this->_db->loadObject();
     $this->_db->setQuery("SELECT * FROM #__rsmembership_memberships WHERE `id`='" . $this->_data->membership_id . "'");
     $membership = $this->_db->loadObject();
     if ($transaction->gateway == 'Authorize.Net') {
         $className = 'plgSystemRSMembershipAuthorize';
         if (class_exists($className)) {
             $dispatcher =& JDispatcher::getInstance();
             $plugin = new $className($dispatcher, array());
             $args = array('plugin' => 'rsmembershipauthorize', 'data' => &$this->_data, 'membership' => $membership, 'transaction' => &$transaction);
             if (method_exists($plugin, 'onMembershipCancelPayment')) {
                 call_user_func_array(array($plugin, 'onMembershipCancelPayment'), $args);
             }
         }
     }
     $this->_db->setQuery("UPDATE #__rsmembership_membership_users SET `status`='3' WHERE `id`='" . $cid . "' LIMIT 1");
     $this->_db->query();
     if ($membership->gid_enable) {
         RSMembership::updateGid($user->get('id'), $membership->gid_expire);
     }
     if ($membership->disable_expired_account) {
         RSMembership::disableUser($user->get('id'));
         $mainframe =& JFactory::getApplication();
         $mainframe->logout();
     }
 }
示例#5
0
 public function cancel()
 {
     $db = JFactory::getDbo();
     $query = $db->getQuery(true);
     $id = $this->user->id;
     $cid = $this->getCid();
     $transaction = JTable::getInstance('Transaction', 'RSMembershiptable');
     $transaction->load($this->_data->from_transaction_id);
     $membership = JTable::getInstance('Membership', 'RSMembershiptable');
     $membership->load($this->_data->membership_id);
     $plugins = RSMembership::getPlugins();
     // Keep a legacy mode for Authorize.net
     if (in_array($transaction->gateway, $plugins) || $transaction->gateway == 'Authorize.Net') {
         $plugin = array_search($transaction->gateway, $plugins);
         if ($plugin === false) {
             $plugin = 'rsmembershipauthorize';
         }
         $args = array('plugin' => $plugin, 'data' => &$this->_data, 'membership' => $membership, 'transaction' => &$transaction);
         JFactory::getApplication()->triggerEvent('onMembershipCancelPayment', $args);
     }
     $query->clear();
     $query->update($db->qn('#__rsmembership_membership_subscribers'))->set($db->qn('status') . ' = ' . $db->q('3'))->where($db->qn('id') . ' = ' . $db->q($cid));
     $db->setQuery($query);
     $db->execute();
     if (!is_array($membership->gid_expire)) {
         $membership->gid_expire = explode(',', $membership->gid_expire);
     }
     if ($membership->gid_enable) {
         RSMembership::updateGid($id, $membership->gid_expire, false, 'remove');
     }
     if ($membership->disable_expired_account) {
         list($memberships, $extras) = RSMembershipHelper::getUserSubscriptions($id);
         if (!$memberships) {
             RSMembership::disableUser($id);
             $app = JFactory::getApplication();
             $app->logout();
         }
     }
 }
示例#6
0
 public static function approve($transaction_id, $force = false)
 {
     JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_rsmembership/tables');
     $app = JFactory::getApplication();
     $db = JFactory::getDBO();
     $query = $db->getQuery(true);
     $date = JFactory::getDate();
     // Load the transaction
     $query->select($db->qn('id'))->from($db->qn('#__rsmembership_transactions'))->where($db->qn('id') . '=' . $db->q($transaction_id));
     if (!$force) {
         $query->where($db->qn('status') . ' != ' . $db->q('completed'));
     }
     $db->setQuery($query);
     if ($id = $db->loadResult()) {
         $transaction = JTable::getInstance('Transaction', 'RSMembershipTable');
         $transaction->load($id);
     } else {
         return false;
     }
     $params = RSMembershipHelper::parseParams($transaction->params);
     $user_data = !empty($transaction->user_data) ? (object) unserialize($transaction->user_data) : (object) array();
     // Handle user creation
     $user_id = $transaction->user_id;
     if (!RSMembershipHelper::getConfig('create_user_instantly') && !$user_id) {
         $user_id = RSMembership::createUser($transaction->user_email, $user_data);
         // Update the transaction with the newly created user ID
         if ($user_id != $transaction->user_id) {
             $updateTransaction = JTable::getInstance('Transaction', 'RSMembershipTable');
             $updateTransaction->save(array('id' => $transaction->id, 'user_id' => $user_id));
         }
     }
     // Update user data
     if ($transaction->user_id && is_object($user_data) && isset($user_data->fields)) {
         RSMembership::createUserData($user_id, $user_data->fields);
     }
     // Create the subscriber
     $row = JTable::getInstance('Membership_Subscriber', 'RSMembershipTable');
     $row->bind(array('published' => 1, 'user_id' => $user_id, 'price' => $transaction->price, 'currency' => $transaction->currency));
     // Set some defaults
     $idev_enabled = RSMembershipHelper::getConfig('idev_enable');
     $idev_track_renewals = RSMembershipHelper::getConfig('idev_track_renewals');
     $update_gid = false;
     $update_user = false;
     $update_idev = false;
     $update_rsmail = false;
     switch ($transaction->type) {
         case 'new':
             $membership_id = $params['membership_id'];
             // Check if this membership still exists
             $membership = JTable::getInstance('Membership', 'RSMembershipTable');
             if (!$membership->load($membership_id)) {
                 JError::raiseWarning(500, JText::_('COM_RSMEMBERSHIP_COULD_NOT_APPROVE_TRANSACTION'));
                 return false;
             }
             // Trial settings
             if ($membership->use_trial_period) {
                 $period = $membership->trial_period;
                 $period_type = $membership->trial_period_type;
             } else {
                 $period = $membership->period;
                 $period_type = $membership->period_type;
             }
             // Fixed expiry
             if ($membership->fixed_expiry) {
                 $date = RSMembershipHelper::calculateFixedDate($membership->fixed_day, $membership->fixed_month, $membership->fixed_year);
                 $membership_end = $date->toSql();
             } elseif ($period) {
                 // Expire in a period of time
                 $date = JFactory::getDate();
                 $date->modify(self::getDateString($period, $period_type));
                 $membership_end = $date->toSql();
             } else {
                 // No expiry
                 $membership_end = $db->getNullDate();
             }
             $extras = !empty($params['extras']) ? implode(',', $params['extras']) : '';
             $row->bind(array('membership_id' => $membership->id, 'extras' => $extras, 'membership_start' => JFactory::getDate()->toSql(), 'membership_end' => $membership_end, 'status' => 0, 'from_transaction_id' => $transaction->id, 'last_transaction_id' => $transaction->id));
             $row->store();
             // Take care of integrations
             if ($membership->gid_enable) {
                 $update_gid = true;
             }
             if ($membership->disable_expired_account) {
                 $update_user = true;
             }
             if ($idev_enabled) {
                 $update_idev = true;
             }
             $update_rsmail = $membership->id;
             // Some values used later on
             $membership_start = $row->membership_start;
             $membership_end = $row->membership_end;
             $return = $row->id;
             break;
         case 'renew':
             $membership_id = $params['membership_id'];
             // Check if this membership still exists
             $membership = JTable::getInstance('Membership', 'RSMembershipTable');
             if (!$membership->load($membership_id)) {
                 JError::raiseWarning(500, JText::_('COM_RSMEMBERSHIP_COULD_NOT_APPROVE_TRANSACTION'));
                 return false;
             }
             // Verify if the subscription to be renewed still exists
             $current = JTable::getInstance('Membership_Subscriber', 'RSMembershipTable');
             if (!$current->load($params['id'])) {
                 JError::raiseWarning(500, JText::_('COM_RSMEMBERSHIP_COULD_NOT_APPROVE_TRANSACTION'));
                 return false;
             }
             $expired = $current->status != MEMBERSHIP_STATUS_ACTIVE;
             // 0 means active, any other value means it's not active
             $period = $membership->period;
             $period_type = $membership->period_type;
             // Renew when not expired ?
             if (!$expired) {
                 // Start the membership when the current one ends
                 $membership_start = $current->membership_end;
             } else {
                 // Start today
                 $membership_start = JFactory::getDate()->toSql();
             }
             // Fixed expiry
             if ($membership->fixed_expiry) {
                 $date = JFactory::getDate(JHtml::_('date', $membership_start, 'Y-m-d H:i:s'));
                 $date = RSMembershipHelper::calculateFixedDate($membership->fixed_day, $membership->fixed_month, $membership->fixed_year, $date);
                 $membership_end = $date->toSql();
             } elseif ($period) {
                 // Expire in a period of time
                 // If it's not expired, add the period after the membership ends
                 if (!$expired) {
                     $date = JFactory::getDate($current->membership_end);
                 } else {
                     $date = JFactory::getDate();
                 }
                 $date->modify(self::getDateString($period, $period_type));
                 $membership_end = $date->toSql();
             } else {
                 // No expiry
                 $membership_end = $db->getNullDate();
             }
             // Update the current subscription with the new values
             $current->save(array('id' => $current->id, 'membership_start' => $membership_start, 'membership_end' => $membership_end, 'price' => $transaction->price, 'currency' => $transaction->currency, 'status' => 0, 'notified' => $db->getNullDate(), 'last_transaction_id' => $transaction->id));
             // Take care of integrations
             if ($membership->gid_enable) {
                 $update_gid = true;
             }
             if ($membership->disable_expired_account) {
                 $update_user = true;
             }
             if ($idev_enabled && $idev_track_renewals) {
                 $update_idev = true;
             }
             // Some values used later on
             // $membership_start && $membership_end already computed
             $return = $params['id'];
             break;
         case 'addextra':
             // Verify if the subscription still exists
             $current = JTable::getInstance('Membership_Subscriber', 'RSMembershipTable');
             if (!$current->load($params['id'])) {
                 JError::raiseWarning(500, JText::_('COM_RSMEMBERSHIP_COULD_NOT_APPROVE_TRANSACTION'));
                 return false;
             }
             // Check if this membership still exists
             $membership = JTable::getInstance('Membership', 'RSMembershipTable');
             if (!$membership->load($current->membership_id)) {
                 JError::raiseWarning(500, JText::_('COM_RSMEMBERSHIP_COULD_NOT_APPROVE_TRANSACTION'));
                 return false;
             }
             if (empty($current->extras)) {
                 $extras = $params['extras'];
             } else {
                 $extras = explode(',', $current->extras);
                 $extras = array_merge($extras, $params['extras']);
             }
             $extras = implode(',', $extras);
             // Update the subscription so that it contains the newly added extras
             $current->save(array('id' => $current->id, 'extras' => $extras));
             // Some values used later on
             $membership_start = $current->membership_start;
             $membership_end = $current->membership_end;
             $return = $params['id'];
             break;
         case 'upgrade':
             // Get the upgraded membership
             $membership = JTable::getInstance('Membership', 'RSMembershipTable');
             if (!$membership->load($params['to_id'])) {
                 JError::raiseWarning(500, JText::_('COM_RSMEMBERSHIP_COULD_NOT_APPROVE_TRANSACTION'));
                 return false;
             }
             // Get the current subscription
             $current = JTable::getInstance('Membership_Subscriber', 'RSMembershipTable');
             if (!$current->load($params['id'])) {
                 JError::raiseWarning(500, JText::_('COM_RSMEMBERSHIP_COULD_NOT_APPROVE_TRANSACTION'));
                 return false;
             }
             // Get the old membership
             $old_membership = JTable::getInstance('Membership', 'RSMembershipTable');
             if (!$old_membership->load($current->membership_id)) {
                 JError::raiseWarning(500, JText::_('COM_RSMEMBERSHIP_COULD_NOT_APPROVE_TRANSACTION'));
                 return false;
             }
             $data = array('id' => $params['id'], 'membership_id' => $membership->id, 'last_transaction_id' => $transaction->id);
             // Get the upgrade price difference & update the price in the subscription
             $upgrade = JTable::getInstance('Upgrade', 'RSMembershipTable');
             if ($upgrade->load(array('membership_from_id' => $old_membership->id, 'membership_to_id' => $membership->id, 'published' => 1))) {
                 $data['price'] = $current->price + $upgrade->price;
             }
             $period = $membership->period;
             $period_type = $membership->period_type;
             // Fixed expiry
             if ($membership->fixed_expiry) {
                 $date = RSMembershipHelper::calculateFixedDate($membership->fixed_day, $membership->fixed_month, $membership->fixed_year);
                 $membership_end = $date->toSql();
                 // Update status & reset notification
                 if ($date->toUnix() > JFactory::getDate()->toUnix()) {
                     $data['status'] = 0;
                     $data['notified'] = $db->getNullDate();
                 }
             } elseif ($period) {
                 // Expire in a period of time
                 $date = JFactory::getDate();
                 $date->modify(self::getDateString($period, $period_type));
                 $membership_end = $date->toSql();
                 // Update status & reset notification
                 if ($date->toUnix() > JFactory::getDate()->toUnix()) {
                     $data['status'] = 0;
                     $data['notified'] = $db->getNullDate();
                 }
             } else {
                 // No expiry
                 $membership_end = $db->getNullDate();
                 // Update status & reset notification
                 $data['status'] = 0;
                 $data['notified'] = $db->getNullDate();
             }
             // Set the newly calculated end date
             $data['membership_end'] = $membership_end;
             // Update the current subscription with the new values
             $current->save($data);
             // Take care of integrations
             if ($membership->gid_enable) {
                 $update_gid = true;
             }
             if ($membership->disable_expired_account) {
                 $update_user = true;
             }
             $update_rsmail = $membership->id;
             // Some values used later on
             // $membership_end already computed
             $membership_start = $current->membership_start;
             $return = $params['id'];
             break;
     }
     // Update the groups
     if ($update_gid) {
         // workaround...
         $theuser = JUser::getInstance($row->user_id);
         unset($theuser->password);
         RSMembership::updateGid($row->user_id, $membership->gid_subscribe, true);
     }
     // Enable the user
     if ($update_user) {
         RSMembership::enableUser($row->user_id);
     }
     // Set the transaction to 'completed'
     $updateTransaction = JTable::getInstance('Transaction', 'RSMembershipTable');
     $updateTransaction->save(array('id' => $transaction->id, 'status' => 'completed'));
     $user_email = $transaction->user_email;
     $start_date = RSMembershipHelper::showDate($membership_start);
     $end_date = $membership_end == $db->getNullDate() ? JText::_('COM_RSMEMBERSHIP_UNLIMITED') : RSMembershipHelper::showDate($membership_end);
     $placeholders = array('{membership}' => $membership->name, '{price}' => RSMembershipHelper::getPriceFormat($transaction->price), '{extras}' => !empty($extras) ? RSMembershipHelper::getExtrasNames($extras) : '', '{email}' => $user_email, '{username}' => isset($user_data->username) ? $user_data->username : '', '{name}' => isset($user_data->name) ? $user_data->name : '', '{membership_start}' => $start_date, '{membership_end}' => $end_date, '{transaction_id}' => $transaction->id, '{transaction_hash}' => $transaction->hash);
     if ($transaction->type == 'upgrade') {
         $placeholders['{membership_from}'] = RSMembership::getMembershipData($params['from_id'])->name;
     }
     // Get all published fields so we can grab the values
     $membership_id_fields = $transaction->type != 'upgrade' ? $params['membership_id'] : $params['to_id'];
     $fields = RSMembership::getCustomFields();
     $membership_fields = RSMembership::getCustomMembershipFields($membership_id_fields);
     $all_fields = array_merge($fields, $membership_fields);
     foreach ($all_fields as $field) {
         $name = $field->name;
         $object = isset($user_data->fields[$name]) ? 'fields' : 'membership_fields';
         if (isset($user_data->fields[$name]) || isset($user_data->membership_fields[$name])) {
             $placeholders['{' . $name . '}'] = is_array($user_data->{$object}[$name]) ? implode("\n", $user_data->{$object}[$name]) : $user_data->{$object}[$name];
         } else {
             $placeholders['{' . $name . '}'] = '';
         }
     }
     $replace = array_keys($placeholders);
     $with = array_values($placeholders);
     if ($update_rsmail) {
         $app->triggerEvent('rsm_onSaveRegistration', array(array('membership' => $update_rsmail, 'email' => $user_email, 'data' => $user_data, 'userid' => $row->user_id)));
     }
     $userEmail = $adminEmail = array('from' => '', 'fromName' => '', 'recipient' => '', 'subject' => '', 'body' => '', 'mode' => '', 'cc' => '', 'bcc' => '', 'attachments' => '', 'replyto' => '', 'replytoname' => '');
     $membership->user_email_from_addr = $membership->user_email_use_global ? JFactory::getConfig()->get('mailfrom') : $membership->user_email_from_addr;
     $membership->user_email_from = $membership->user_email_use_global ? JFactory::getConfig()->get('fromname') : $membership->user_email_from;
     // start sending emails
     // user emails
     if ($membership->user_email_from_addr && $membership->user_email_approved_subject) {
         // attachments
         $query = $db->getQuery(true);
         $query->select($db->qn('path'))->from($db->qn('#__rsmembership_membership_attachments'))->where($db->qn('membership_id') . ' = ' . $db->q($membership->id))->where($db->qn('email_type') . ' = ' . $db->q('user_email_approved'))->where($db->qn('published') . ' = ' . $db->q(1))->order($db->qn('ordering') . ' ASC');
         $db->setQuery($query);
         $userEmail = array('from' => $membership->user_email_from_addr, 'fromName' => $membership->user_email_from, 'recipient' => $user_email, 'subject' => str_replace($replace, $with, $membership->user_email_approved_subject), 'body' => str_replace($replace, $with, $membership->user_email_approved_text), 'mode' => $membership->user_email_mode, 'cc' => null, 'bcc' => null, 'attachments' => $db->loadColumn(), 'replyto' => $membership->user_email_from_addr, 'replytoname' => $membership->user_email_from);
     }
     // admin emails
     if ($membership->admin_email_to_addr && $membership->admin_email_approved_subject) {
         $adminEmail = array('from' => trim($membership->admin_email_from_addr) != '' ? $membership->admin_email_from_addr : $user_email, 'fromName' => isset($user_data->name) ? $user_data->name : $user_email, 'recipient' => $membership->admin_email_to_addr, 'subject' => str_replace($replace, $with, $membership->admin_email_approved_subject), 'body' => str_replace($replace, $with, $membership->admin_email_approved_text), 'mode' => $membership->admin_email_mode, 'cc' => null, 'bcc' => null, 'attachments' => null, 'replyto' => $user_email, 'replytoname' => isset($user_data->name) ? $user_data->name : $user_email);
     }
     // run php code -Custom PHP code (Accepted Transaction)-
     if ($membership->custom_code_transaction) {
         eval($membership->custom_code_transaction);
     }
     // send to user
     if ($membership->user_email_from_addr && $membership->user_email_approved_subject) {
         RSMembershipHelper::sendMail($userEmail['from'], $userEmail['fromName'], $userEmail['recipient'], $userEmail['subject'], $userEmail['body'], $userEmail['mode'], $userEmail['cc'], $userEmail['bcc'], $userEmail['attachments'], $userEmail['replyto'], $userEmail['replytoname']);
     }
     // send to admin
     if ($membership->admin_email_to_addr && $membership->admin_email_approved_subject) {
         RSMembershipHelper::sendMail($adminEmail['from'], $adminEmail['fromName'], $adminEmail['recipient'], $adminEmail['subject'], $adminEmail['body'], $adminEmail['mode'], $adminEmail['cc'], $adminEmail['bcc'], $adminEmail['attachments'], $adminEmail['replyto'], $adminEmail['replytoname']);
     }
     // process stock
     if ($membership->stock > 0) {
         $membershipUpdate = JTable::getInstance('Membership', 'RSMembershipTable');
         $membershipUpdate->save(array('id' => $membership->id, 'stock' => $membership->stock > 1 ? $membership->stock - 1 : -1));
     }
     if ($update_idev) {
         RSMembership::updateIdev(array('idev_saleamt' => $transaction->price, 'idev_ordernum' => $transaction->id, 'ip_address' => $transaction->ip));
     }
     // should return the newly created/updated membership id
     return $return;
 }
示例#7
0
 function membershipSave()
 {
     $row =& JTable::getInstance('RSMembership_Membership_Users', 'Table');
     $post = JRequest::get('post', JREQUEST_ALLOWHTML);
     if (!empty($post['noextra'])) {
         $post['extras'] = '';
     } elseif (isset($post['extras'])) {
         $post['extras'] = implode(',', $post['extras']);
     }
     if (!empty($post['unlimited'])) {
         $post['membership_end'] = 0;
     }
     if (!$row->bind($post)) {
         return JError::raiseWarning(500, $row->getError());
     }
     // Updating ?
     if ($row->id > 0) {
         $this->_db->setQuery("SELECT status FROM #__rsmembership_membership_users WHERE id='" . (int) $row->id . "'");
         $status = $this->_db->loadResult();
         if ($status > 0 && $row->status == 0) {
             $row->notified = 0;
         }
     }
     $this->_db->setQuery("SELECT gid_enable, gid_subscribe, gid_expire, disable_expired_account FROM #__rsmembership_memberships WHERE `id`='" . (int) $row->membership_id . "'");
     $membership = $this->_db->loadObject();
     if ($row->status == 0) {
         if ($membership->gid_enable) {
             RSMembership::updateGid($row->user_id, $membership->gid_subscribe, true);
         }
     } else {
         if ($membership->gid_enable) {
             RSMembership::updateGid($row->user_id, $membership->gid_expire);
         }
         if (($row->status == 2 || $row->status == 3) && $membership->disable_expired_account) {
             RSMembership::disableUser($row->user_id);
         }
     }
     if (strpos($row->membership_start, '.') !== false) {
         unset($row->membership_start);
     }
     if (strpos($row->membership_end, '.') !== false) {
         unset($row->membership_end);
     }
     if ($row->store()) {
         $this->_id = $row->id;
         return true;
     } else {
         JError::raiseWarning(500, $row->getError());
         return false;
     }
 }
示例#8
0
 function approve($transaction_id, $force = false)
 {
     $db =& JFactory::getDBO();
     $db->setQuery("SELECT * FROM #__rsmembership_transactions WHERE `id`='" . (int) $transaction_id . "'" . ($force ? "" : " AND `status`!='completed'"));
     $transaction = $db->loadObject();
     if (empty($transaction->id)) {
         return false;
     }
     $params = RSMembershipHelper::parseParams($transaction->params);
     JTable::addIncludePath(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_rsmembership' . DS . 'tables');
     $row =& JTable::getInstance('RSMembership_Membership_Users', 'Table');
     $row->user_id = $transaction->user_id;
     if (!RSMembershipHelper::getConfig('create_user_instantly') || $row->user_id == 0) {
         $row->user_id = RSMembership::createUser($transaction->user_email, unserialize($transaction->user_data));
         $db->setQuery("UPDATE #__rsmembership_transactions SET `user_id`='" . $row->user_id . "' WHERE `id`='" . $transaction->id . "'");
         $db->query();
     }
     $row->price = $transaction->price;
     $row->currency = $transaction->currency;
     $idev_enabled = RSMembershipHelper::getConfig('idev_enable');
     $idev_track_renewals = RSMembershipHelper::getConfig('idev_track_renewals');
     $update_gid = false;
     $update_user = false;
     $update_idev = false;
     $update_rsmail = false;
     $date = JFactory::getDate();
     switch ($transaction->type) {
         case 'new':
             $row->membership_id = $params['membership_id'];
             $db->setQuery("SELECT * FROM #__rsmembership_memberships WHERE `id`='" . (int) $row->membership_id . "'");
             $membership = $db->loadObject();
             if (empty($membership)) {
                 JError::raiseWarning(500, JText::_('RSM_COULD_NOT_APPROVE_TRANSACTION'));
                 return false;
             }
             if ($membership->gid_enable) {
                 $update_gid = true;
             }
             if ($membership->disable_expired_account) {
                 $update_user = true;
             }
             $row->membership_start = $date->toUnix();
             if ($membership->use_trial_period) {
                 $membership->period = $membership->trial_period;
                 $membership->period_type = $membership->trial_period_type;
             }
             if ($membership->fixed_expiry) {
                 $row->membership_end = RSMembershipHelper::calculateFixedDate($membership);
             } else {
                 if ($membership->period > 0) {
                     switch ($membership->period_type) {
                         case 'h':
                             $offset = $membership->period * 3600;
                             break;
                         case 'd':
                             $offset = $membership->period * 86400;
                             break;
                         case 'm':
                             $offset = strtotime('+' . $membership->period . ' months', $row->membership_start) - $row->membership_start;
                             break;
                         case 'y':
                             $offset = strtotime('+' . $membership->period . ' years', $row->membership_start) - $row->membership_start;
                             break;
                     }
                     $row->membership_end = $date->toUnix() + $offset;
                 } else {
                     $row->membership_end = 0;
                 }
             }
             if (!empty($params['extras'])) {
                 $row->extras = implode(',', $params['extras']);
             }
             $row->status = 0;
             $row->from_transaction_id = $transaction->id;
             $row->last_transaction_id = $transaction->id;
             $row->store();
             $return = $row->id;
             // iDev Integration
             if ($idev_enabled) {
                 $update_idev = true;
             }
             $update_rsmail = $membership->id;
             break;
         case 'addextra':
             $db->setQuery("SELECT `extras` FROM #__rsmembership_membership_users WHERE `id`='" . (int) $params['id'] . "'");
             $extras = $db->loadResult();
             $extras = explode(',', $extras);
             if (empty($extras[0])) {
                 $extras = $params['extras'];
             } else {
                 $extras = array_merge($extras, $params['extras']);
             }
             $db->setQuery("UPDATE #__rsmembership_membership_users SET `extras`='" . implode(',', $extras) . "' WHERE `id`='" . (int) $params['id'] . "'");
             $db->query();
             $return = $params['id'];
             break;
         case 'upgrade':
             // Get the upgraded membership
             $db->setQuery("SELECT * FROM #__rsmembership_memberships WHERE `id`='" . (int) $params['to_id'] . "'");
             $membership = $db->loadObject();
             // Get the current membership
             $db->setQuery("SELECT * FROM #__rsmembership_membership_users WHERE `id`='" . (int) $params['id'] . "'");
             $current = $db->loadObject();
             $db->setQuery("SELECT * FROM #__rsmembership_memberships WHERE `id`='" . $current->membership_id . "'");
             $old_membership = $db->loadObject();
             $db->setQuery("UPDATE #__rsmembership_membership_users SET `membership_id`='" . (int) $params['to_id'] . "' WHERE `id`='" . (int) $params['id'] . "'");
             $db->query();
             $new_price = '';
             $db->setQuery("SELECT price FROM #__rsmembership_membership_upgrades WHERE `membership_from_id`='" . (int) $old_membership->id . "' AND `membership_to_id`='" . (int) $membership->id . "' AND `published`='1'");
             $upgrade = $db->loadResult();
             if ($upgrade) {
                 $new_price = ", `price`='" . $db->getEscaped($current->price + $upgrade) . "'";
             }
             if ($membership->fixed_expiry) {
                 $membership_end = RSMembershipHelper::calculateFixedDate($membership);
                 $status = '';
                 if ($membership_end > $date->toUnix()) {
                     $status = ", `status`='0', `notified`='0'";
                 }
                 $db->setQuery("UPDATE #__rsmembership_membership_users SET membership_end = '" . $membership_end . "' {$status} {$new_price} WHERE `id`='" . (int) $params['id'] . "'");
                 $db->query();
             } else {
                 if ($membership->period == 0) {
                     $db->setQuery("UPDATE #__rsmembership_membership_users SET `membership_end`='0', `status`='0', `notified`='0' {$new_price} WHERE `id`='" . (int) $params['id'] . "'");
                     $db->query();
                 } elseif ($membership->period > 0) {
                     switch ($membership->period_type) {
                         case 'h':
                             $offset = $membership->period * 3600;
                             break;
                         case 'd':
                             $offset = $membership->period * 86400;
                             break;
                         case 'm':
                             $offset = strtotime('+' . $membership->period . ' months', $current->membership_start) - $current->membership_start;
                             break;
                         case 'y':
                             $offset = strtotime('+' . $membership->period . ' years', $current->membership_start) - $current->membership_start;
                             break;
                     }
                     // $membership_end = $current->membership_start + $offset;
                     // FDS replaced the above line with the one below.  This sets the membership expiration date to now + offset.
                     $membership_end = $date->toUnix() + $offset;
                     $status = '';
                     if ($membership_end > $date->toUnix()) {
                         $status = ", `status`='0', `notified`='0'";
                     }
                     $db->setQuery("UPDATE #__rsmembership_membership_users SET membership_end = '" . $membership_end . "' {$status} {$new_price} WHERE `id`='" . (int) $params['id'] . "'");
                     $db->query();
                 }
             }
             // the last transaction
             $db->setQuery("UPDATE #__rsmembership_membership_users SET `last_transaction_id`='" . $transaction->id . "' WHERE `id`='" . (int) $params['id'] . "'");
             $db->query();
             if ($membership->gid_enable) {
                 $update_gid = true;
             }
             if ($membership->disable_expired_account) {
                 $update_user = true;
             }
             $update_rsmail = $membership->id;
             $return = $params['id'];
             break;
         case 'renew':
             $row->membership_id = $params['membership_id'];
             $db->setQuery("SELECT * FROM #__rsmembership_memberships WHERE `id`='" . (int) $row->membership_id . "'");
             $membership = $db->loadObject();
             $membership_start = $date->toUnix();
             if ($membership->fixed_expiry) {
                 $membership_end = RSMembershipHelper::calculateFixedDate($membership);
             } else {
                 // Renew when not expired ?
                 $db->setQuery("SELECT * FROM #__rsmembership_membership_users WHERE `id`='" . (int) $params['id'] . "'");
                 $current = $db->loadObject();
                 if ($current->status == 0) {
                     $membership_start = $current->membership_end;
                 }
                 if ($membership->period > 0) {
                     switch ($membership->period_type) {
                         case 'h':
                             $offset = $membership->period * 3600;
                             break;
                         case 'd':
                             $offset = $membership->period * 86400;
                             break;
                         case 'm':
                             $offset = strtotime('+' . $membership->period . ' months', $membership_start) - $membership_start;
                             break;
                         case 'y':
                             $offset = strtotime('+' . $membership->period . ' years', $membership_start) - $membership_start;
                             break;
                     }
                     $membership_end = $membership_start + $offset;
                 } else {
                     $membership_end = 0;
                 }
                 if ($current->status == 0) {
                     $membership_start = $current->membership_start;
                 }
             }
             $db->setQuery("UPDATE #__rsmembership_membership_users SET `membership_start`='" . $membership_start . "', `membership_end`='" . $membership_end . "',`price`='" . $db->getEscaped($row->price) . "', `status`='0', `notified`='0' WHERE `id`='" . (int) $params['id'] . "'");
             $db->query();
             // the last transaction
             $db->setQuery("UPDATE #__rsmembership_membership_users SET `last_transaction_id`='" . $transaction->id . "' WHERE `id`='" . (int) $params['id'] . "'");
             $db->query();
             if ($membership->gid_enable) {
                 $update_gid = true;
             }
             if ($membership->disable_expired_account) {
                 $update_user = true;
             }
             $return = $params['id'];
             // iDev Integration
             if ($idev_enabled && $idev_track_renewals) {
                 $update_idev = true;
             }
             break;
     }
     if ($update_gid) {
         RSMembership::updateGid($row->user_id, $membership->gid_subscribe, true);
     }
     if ($update_user) {
         RSMembership::enableUser($row->user_id);
     }
     $db->setQuery("UPDATE #__rsmembership_transactions SET `status`='completed' WHERE `id`='" . $transaction->id . "'");
     $db->query();
     $user_data = unserialize($transaction->user_data);
     $user_email = $transaction->user_email;
     $replace = array('{membership}', '{email}', '{username}', '{name}');
     $with = array($membership->name, $user_email, isset($user_data->username) ? $user_data->username : '', $user_data->name);
     $db->setQuery("SELECT * FROM #__rsmembership_fields WHERE published='1'");
     $fields = $db->loadObjectList();
     foreach ($fields as $field) {
         $name = $field->name;
         $replace[] = '{' . $name . '}';
         if (isset($user_data->fields[$name])) {
             $with[] = is_array($user_data->fields[$name]) ? implode("\n", $user_data->fields[$name]) : $user_data->fields[$name];
         } else {
             $with[] = '';
         }
     }
     if ($update_rsmail) {
         RSMembership::addToRSMail($update_rsmail, $row->user_id, $user_email, $user_data);
     }
     $userEmail = array('from' => '', 'fromName' => '', 'recipient' => '', 'subject' => '', 'body' => '', 'mode' => '', 'cc' => '', 'bcc' => '', 'attachments' => '', 'replyto' => '', 'replytoname' => '');
     $adminEmail = array('from' => '', 'fromName' => '', 'recipient' => '', 'subject' => '', 'body' => '', 'mode' => '', 'cc' => '', 'bcc' => '', 'attachments' => '', 'replyto' => '', 'replytoname' => '');
     $jconfig = JFactory::getConfig();
     $membership->user_email_from_addr = $membership->user_email_use_global ? $jconfig->getValue('config.mailfrom') : $membership->user_email_from_addr;
     $membership->user_email_from = $membership->user_email_use_global ? $jconfig->getValue('config.fromname') : $membership->user_email_from;
     if (!empty($membership->user_email_from_addr) && $membership->user_email_approved_subject != '') {
         // start sending emails
         // from address
         $userEmail['from'] = $membership->user_email_from_addr;
         // from name
         $userEmail['fromName'] = $membership->user_email_from;
         // recipient
         $userEmail['recipient'] = $user_email;
         // user email
         // subject
         $userEmail['subject'] = str_replace($replace, $with, $membership->user_email_approved_subject);
         // body
         $userEmail['body'] = str_replace($replace, $with, $membership->user_email_approved_text);
         // mode
         $userEmail['mode'] = $membership->user_email_mode;
         // cc
         $userEmail['cc'] = null;
         // bcc
         $userEmail['bcc'] = null;
         // attachments
         $db->setQuery("SELECT `path` FROM #__rsmembership_membership_attachments WHERE `membership_id`='" . $membership->id . "' AND `email_type`='user_email_approved' AND `published`='1' ORDER BY `ordering`");
         $userEmail['attachments'] = $db->loadResultArray();
         // reply to
         $userEmail['replyto'] = $userEmail['from'];
         // reply to name
         $userEmail['replytoname'] = $userEmail['fromName'];
     }
     // admin emails
     if (!empty($membership->admin_email_to_addr) && $membership->admin_email_approved_subject != '') {
         // from address
         $adminEmail['from'] = $user_email;
         // from name
         $adminEmail['fromName'] = $user_data->name;
         // recipient
         $adminEmail['recipient'] = $membership->admin_email_to_addr;
         // subject
         $adminEmail['subject'] = str_replace($replace, $with, $membership->admin_email_approved_subject);
         // body
         $adminEmail['body'] = str_replace($replace, $with, $membership->admin_email_approved_text);
         // mode
         $adminEmail['mode'] = $membership->admin_email_mode;
         // cc
         $adminEmail['cc'] = null;
         // bcc
         $adminEmail['bcc'] = null;
         // attachments
         $adminEmail['attachments'] = null;
         // reply to
         $adminEmail['replyto'] = $adminEmail['from'];
         // reply to name
         $adminEmail['replytoname'] = $adminEmail['fromName'];
     }
     // run php code
     eval($membership->custom_code_transaction);
     // send to user
     if (!empty($membership->user_email_from_addr) && $membership->user_email_approved_subject != '') {
         RSMembershipHelper::sendMail($userEmail['from'], $userEmail['fromName'], $userEmail['recipient'], $userEmail['subject'], $userEmail['body'], $userEmail['mode'], $userEmail['cc'], $userEmail['bcc'], $userEmail['attachments'], $userEmail['replyto'], $userEmail['replytoname']);
     }
     // send to admin
     if (!empty($membership->admin_email_to_addr) && !empty($membership->admin_email_approved_subject)) {
         RSMembershipHelper::sendMail($adminEmail['from'], $adminEmail['fromName'], $adminEmail['recipient'], $adminEmail['subject'], $adminEmail['body'], $adminEmail['mode'], $adminEmail['cc'], $adminEmail['bcc'], $adminEmail['attachments'], $adminEmail['replyto'], $adminEmail['replytoname']);
     }
     // process stock
     if ($membership->stock > 0) {
         // decrease stock
         if ($membership->stock > 1) {
             $db->setQuery("UPDATE #__rsmembership_memberships SET `stock`=`stock`-1 WHERE `id`='" . $membership->id . "'");
         } else {
             $db->setQuery("UPDATE #__rsmembership_memberships SET `stock`='-1' WHERE `id`='" . $membership->id . "'");
         }
         $db->query();
     }
     if ($update_idev) {
         RSMembership::updateIdev(array('idev_saleamt' => $transaction->price, 'idev_ordernum' => $transaction->id, 'ip_address' => $transaction->ip));
     }
     // should return the newly created/updated membership id
     return $return;
 }