/**
  *  The loadConfigFromDB loads the config parameters form database.
  *  The config data is a serialzed string
  */
 function loadConfigFromDB()
 {
     //load config from db
     $sql = "SELECT params FROM #__byjg_provider WHERE name='{$this->_name}' LIMIT 1";
     $this->_db->setQuery($sql);
     if ($this->_db->query() === false) {
         ByJGRedirect('index.php?option=com_byjg', JText::_('BYJG_SQLQUERY_ERROR'));
         die;
     }
     $obj = $this->_db->loadObject();
     //provider does not exists in database
     if (is_null($obj)) {
         return;
     }
     $p = $this->_crypto->Decode($obj->params);
     if (is_array($p)) {
         //if not something wrong
         $this->_params = $p;
     }
 }
 /**
  * Send a sms
  *
  * The most important method form the component. Send a sms.
  *
  */
 function DoSendSms()
 {
     //check global maxsms parameter before sending
     $sql = " /*com_byjg:dosendsms select global sms litmit */\r\n\t\t\t\tSELECT value FROM #__byjg_config WHERE name='maxsms' LIMIT 1";
     $this->db->setQuery($sql);
     //check result and output a message
     if ($this->db->query($sql) == false) {
         $this->errorHandler->Alert(JText::_('BYJG_SQLQUERY_ERROR'), $this->db->getErrorMsg());
     }
     $maxsms = $this->db->loadResult();
     if (!is_null($maxsms) && $maxsms > 0) {
         $limit = $maxsms;
         $sql = "SELECT COUNT(*) AS COUNTER FROM #__byjg_sendsms";
         $this->db->setQuery($sql);
         //check result and output a message
         if ($this->db->query($sql) == false) {
             $this->errorHandler->Alert(JText::_('BYJG_SQLQUERY_ERROR'), $this->db->getErrorMsg());
         }
         $row = $this->db->loadObject();
         if ($row->COUNTER >= $limit) {
             $this->errorHandler->Alert(JText::_('BYJG_GLOBAL_LIMIT_REACHED'));
         }
     }
     $msg = '';
     //get input parameters
     $sms_body = JRequest::getVar('sms_body', '');
     //dont check body, if we want to send a empty sms it is ok
     $sms_send = JRequest::getVar('sms_send', '');
     $from = $sms_send;
     $sms_ddd = JRequest::getVar('sms_ddd', '');
     $ad = JRequest::getVar('ad', '');
     if (strlen($sms_body) == 0) {
         $this->errorHandler->Alert(JText::_('BYJG_SMS_BODY_EMPTY'));
     }
     /*
     		 *  check sms sender
     		 	
       		if( strlen( $sms_send ) < 8 )
       		{
         		$this->errorHandler->Alert( JText::_( 'BYJG_INVALID_SENDER' ) );
       		}
     */
     $sms_recv = JRequest::getVar('sms_recv', '');
     $this->Filter($sms_recv);
     //check sms recv
     if (strlen($sms_recv) < 8) {
         $this->errorHandler->Alert(JText::_('BYJG_INVALID_RECIPIENT'));
         die;
     }
     //append advertisment to sms
     if (strlen($ad) > 0) {
         $bodyLen = strlen($sms_body);
         $adLen = strlen($ad);
         $len = $bodyLen + $adLen;
         if ($len > 160) {
             $sms_body = substr($sms_body, 0, 159 - strlen($ad));
             $sms_body .= "\n" . $ad;
         } else {
             $sms_body .= "\n" . $ad;
         }
     }
     //now check if the sms body is not longer that 160
     if (strlen($sms_body) > 160) {
         $sms_body = substr($sms_body, 0, 160);
     }
     //get the current sms provider
     //create provider factory
     $factory = new ProviderFactory();
     $provider = $factory->getActiveInstance();
     //check provider here
     if ($provider === false) {
         $this->errorHandler->Alert(JText::_('BYJG_NO_ACTIVE_PROVIDER'));
         die;
     }
     $errMsg = '';
     $arr = explode(";", $sms_recv);
     foreach ($arr as $recv) {
         $recv = trim($recv);
         //trim whitespaces
         if (strlen($recv) < 8) {
             continue;
         }
         //check user balance
         if ($this->user->balance() <= 0) {
             $this->errorHandler->Alert(JText::_('BYJG_NOT_SUFFICIENT_FUNDS'));
         }
         //trigger plugins if available
         $dispatcher = JDispatcher::getInstance();
         JPluginHelper::importPlugin('byjg');
         $data = array('message' => $sms_body, 'sender' => $sms_send, 'receiver' => $recv);
         $result = $dispatcher->trigger('onSendSms', $data);
         $abort = false;
         foreach ($result as $code) {
             if ($code == false) {
                 $abort = true;
                 break;
             }
         }
         //a plugin has returned false, so we cancel the send process
         if ($abort == true) {
             ByJGRedirect($this->CreateRedirectUrl(), JText::_('BYJG_PLUGIN_ABORT'));
         }
         /*
          * Chamada da funcao alterada por BYJG para atender ao Gateway ByJG
          *         alterado em 09.10.10
          * $ret = $provider->sendSMS( $sms_body, $sms_send, $recv, $msg );
          */
         $ret = $provider->sendSMS($sms_body, $sms_ddd, $recv, $sms_send, $msg);
         //sms sending failed
         if ($ret == false) {
             $errMsg .= JText::sprintf('BYJG_SEND_SMS_FAILED', $recv);
             $errMsg = $errMsg . ' (' . $msg . ').';
         } else {
             //dont forget to update the user balance
             $bal = (int) $this->user->balance();
             $bal--;
             if ($this->user->setBalance($bal) == false) {
                 $this->errorHandler->Alert(JText::_('BYJG_CHANGE_BALANCE_FAILED'));
             }
             //sendind was ok, so archive it
             $to = $sms_ddd . $recv;
             $text = $sms_body;
             $provider->archiveSMS($text, $from, $to);
             $errMsg .= JText::sprintf('BYJG_SEND_SMS_SUCCESSFULLY', $recv);
             $errMsg = $errMsg . ' (' . $msg . ').';
         }
     }
     ByJGRedirect($this->CreateRedirectUrl(), $errMsg);
 }
 /**
  * Cancel editting global settings
  */
 function DoGlobalCancel()
 {
     ByJGRedirect('index.php?option=com_byjg');
 }