NotificationMailer.php
4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* i-Educar - Sistema de gestão escolar
*
* Copyright (C) 2006 Prefeitura Municipal de ItajaÃÂ
* <ctima@itajai.sc.gov.br>
*
* Este programa é software livre; você pode redistribuÃ-lo e/ou modificá-lo
* sob os termos da Licença Pública Geral GNU conforme publicada pela Free
* Software Foundation; tanto a versão 2 da Licença, como (a seu critério)
* qualquer versão posterior.
*
* Este programa é distribuÃÂdo na expectativa de que seja útil, porém, SEM
* NENHUMA GARANTIA; nem mesmo a garantia implÃÂcita de COMERCIABILIDADE OU
* ADEQUAÇÃO A UMA FINALIDADE ESPECÃFICA. Consulte a Licença Pública Geral
* do GNU para mais detalhes.
*
* Você deve ter recebido uma cópia da Licença Pública Geral do GNU junto
* com este programa; se não, escreva para a Free Software Foundation, Inc., no
* endereço 59 Temple Street, Suite 330, Boston, MA 02111-1307 USA.
*
* @author Lucas D'Avila <lucasdavila@portabilis.com.br>
* @category i-Educar
* @license @@license@@
* @package Mailer
* @subpackage Modules
* @since Arquivo disponÃÂvel desde a versão ?
* @version $Id$
*/
require_once 'Portabilis/Mailer.php';
require_once 'Portabilis/String/Utils.php';
require_once 'Portabilis/Utils/User.php';
require_once 'Portabilis/Utils/Database.php';
class NotificationMailer extends Portabilis_Mailer
{
static function unexpectedDataBaseError($appError, $pgError, $sql) {
if (self::canSendEmail()) {
$lastError = error_get_last();
$userId = Portabilis_Utils_User::currentUserId();
$to = self::notificationEmail();
$subject = "[Erro inesperado bd] i-Educar - " . self::host();
$message = "Olá!\n\n" .
"Ocorreu um erro inesperado no banco de dados, detalhes abaixo:\n\n" .
" ERRO APP: ' . $appError\n" .
" ERRO PHP: ' . {$lastError['message']}\n" .
" ERRO POSTGRES: $pgError\n" .
" LINHA {$lastError['line']} em {$lastError['file']}\n" .
" SQL: {$sql}\n" .
" ID USUÁRIO {$userId}\n" .
"\n\n-\n\n" .
"Você recebeu este email pois seu email foi configurado para receber " .
"notificações de erros.";
// only send email, if a notification email was set.
return ($to ? self::mail($to, $subject, $message) : false);
}
}
static function unexpectedError($appError) {
if (self::canSendEmail()) {
$lastError = error_get_last();
$user = self::tryLoadUser();
$to = self::notificationEmail();
$subject = "[Erro inesperado] i-Educar - " . self::host();
$message = "Olá!\n\n" .
"Ocorreu um erro inesperado, detalhes abaixo:\n\n" .
" ERRO APP: ' . $appError\n" .
" ERRO PHP: ' . {$lastError['message']}\n" .
" LINHA {$lastError['line']} em {$lastError['file']}\n" .
" USUÁRIO {$user['matricula']} email {$user['email']}\n" .
"\n\n-\n\n" .
"Você recebeu este email pois seu email foi configurado para receber " .
"notificações de erros.";
// only send email, if a notification email was set.
return ($to ? self::mail($to, $subject, $message) : false);
}
}
// common error mailer methods
protected static function canSendEmail() {
return $GLOBALS['coreExt']['Config']->modules->error->send_notification_email == true;
}
protected static function notificationEmail() {
$email = $GLOBALS['coreExt']['Config']->modules->error->notification_email;
if (! is_string($email)) {
error_log("Não foi definido um email para receber detalhes dos erros, por favor adicione a opção " .
"'modules.error_notification.email = email@dominio.com' ao arquivo ini de configuração.");
return false;
}
return $email;
}
protected static function tryLoadUser() {
try {
$sql = 'select matricula, email from portal.funcionario WHERE ref_cod_pessoa_fj = $1 ';
$options = array('params' => Portabilis_Utils_User::currentUserId(), 'return_only' => 'first-row');
$user = Portabilis_Utils_Database::fetchPreparedQuery($sql, $options);
} catch (Exception $e) {
$user = array('matricula' => 'Erro ao obter', 'email' => 'Erro ao obter');
}
return $user;
}
}