/** * Efetua consulta de cep. * * @param type $cep * @return type * @throws Exception */ public function addressFromCep($cep) { if (!file_exists(self::CONFIG_FILE)) { throw new Exception("Arquivo de configurações do cliente BYJG não existe"); } $config = (include self::CONFIG_FILE); $result = array('found' => false); try { // Requisicao ao BYJG que prove base de dados gratuita para CEP $byJg = new Client(self::HOST); $byJg->setMethod(Request::METHOD_POST); $byJg->setParameterPost(array('httpmethod' => 'obterlogradouroauth', 'cep' => $cep, 'usuario' => $config['username'], 'senha' => $config['password'])); $response = $byJg->send(); if ($response->isOk()) { // captura resultado e organiza dados $body = preg_replace("/^OK\\|/", "", $response->getBody()); // Separa as partes do CEP $parts = explode(", ", $body); if (count($parts) <= 1) { throw new Exception("Resposta ByJG não pode suprir CEP como esperado"); } $result['found'] = true; list($result['logradouro'], $result['bairro'], $result['cidade'], $result['estado'], $result['codIbge']) = $parts; } } catch (Exception $e) { Firephp::getInstance()->err($e->__toString()); } return $result; }
/** * Metodo para envio de email pelo sistema. As configuracoes de conexao com o servidor de email * devem estar no arquivo 'config/autoload/'.$type.'.local.php'. * Ps.: Este arquivo, por conter senhas, é ignorado pelo git. * * @param string|array $emailTo * @param string $subject * @param string $htmlBody * @param string $type * @param string|array $replyTo * @throws Exception */ protected function email($emailTo, $subject, $htmlBody, $type = 'mail', $replyTo = null) { $result = true; /** * O arquivo *.local.php nao eh incluido no git, por isso as informacoes contidas * nele sao seguras, diferente do arquivo global.php. */ $auth = 'config/autoload/' . $type . '.local.php'; $authConf = file_exists($auth) ? require $auth : false; $global = (require 'config/autoload/global.php'); $options = array_merge_recursive(isset($global[$type]) ? $global[$type] : array(), $authConf); try { if ($options) { // Codifica o tipo de mensagem HTML $mimePart = new Mime\Part($htmlBody); $mimePart->type = Mime\Mime::TYPE_HTML; $mimeMsg = new Mime\Message(); $mimeMsg->setParts(array($mimePart)); // Cria mensagem efetivamente $message = new Message(); $message->setBody($mimeMsg); $message->setEncoding('UTF-8'); // Informacoes do e-mail em si $message->addFrom($options['connection_config']['username'], 'Braghim Sistemas'); if ($replyTo) { $message->setReplyTo($replyTo); } $message->addTo($emailTo); $message->setSubject($subject); // Transportador de mensagem $transport = new SmtpTransport(); $transport->setOptions(new SmtpOptions($options)); $transport->send($message); } else { throw new \Exception("Configurações de e-mail não foram definidas ou arquivo '{$type}.local.php' não existe"); } } catch (\Exception $e) { Firephp::getInstance()->err($e->__toString()); $result = false; } return $result; }