jeferson-lsouza /
isilog
| 1 | <?php |
||||
| 2 | |||||
| 3 | /* |
||||
| 4 | *********************************************** |
||||
| 5 | LOG.PHP - |
||||
| 6 | Class responsible for creating .txt log files to store information about operations such as login, logout, monitoring. |
||||
| 7 | Classe responsável pela criação de arquivos de log .txt para armazenar informações sobre operações como login, logout, monitoramento. |
||||
| 8 | |||||
| 9 | You can create a login with personalized information or use a method already structured to meet the minimum needs of a Log. |
||||
| 10 | Você pode criar um login com as informações personalizadas ou utilizar uma método já estruturado para suprir as necessidades mínimas de um Log. |
||||
| 11 | *********************************************** |
||||
| 12 | |||||
| 13 | Copyright (c) 2020, Jeferson L. Souza INTERLIG SOLUÇÕES INTELIGENTES |
||||
| 14 | E-mail: [email protected] |
||||
| 15 | Site: http://interligsolucoes.com.br/ |
||||
| 16 | */ |
||||
| 17 | |||||
| 18 | namespace Developers; |
||||
| 19 | |||||
| 20 | class Log |
||||
| 21 | {
|
||||
| 22 | /** @var */ |
||||
| 23 | public $text; |
||||
| 24 | |||||
| 25 | /** @var */ |
||||
| 26 | public $Name; |
||||
| 27 | |||||
| 28 | /** @var */ |
||||
| 29 | public $Data; |
||||
| 30 | |||||
| 31 | /** @var */ |
||||
| 32 | public $Status; |
||||
| 33 | |||||
| 34 | /** @var */ |
||||
| 35 | public $Folder; |
||||
| 36 | |||||
| 37 | |||||
| 38 | /** |
||||
| 39 | * Method responsible for creating folders and subfolders, by default the folder name is Logs |
||||
| 40 | * Método responsável pela criação de pastas e subpastas, por padrão, o nome da pasta é Logs |
||||
| 41 | * |
||||
| 42 | * Create the folder and subfolders with today's date and in the "IF" we check if the folders and subfolders exist. |
||||
| 43 | * Cria a pasta e subpastas com a data de hoje e no "IF" verificamos se as pastas e subpastas existem. |
||||
| 44 | */ |
||||
| 45 | public function CreateFolder($Folder = 'Logs/') |
||||
| 46 | {
|
||||
| 47 | $this->Folder = $Folder; |
||||
| 48 | |||||
| 49 | $FolderDir = $Folder . '/' . date('Y') . '/' . date('m') . '/' . date('d') . '/';
|
||||
| 50 | if (!is_dir($FolderDir) && !file_exists($FolderDir)) {
|
||||
| 51 | if (!is_dir($Folder) && !file_exists($Folder)) {
|
||||
| 52 | mkdir($Folder, 0755); |
||||
| 53 | } |
||||
| 54 | |||||
| 55 | if (!is_dir($Folder . '/' . date('Y')) && !file_exists($Folder . '/' . date('Y'))) {
|
||||
| 56 | mkdir($Folder . '/' . date('Y'), 0755);
|
||||
| 57 | } |
||||
| 58 | |||||
| 59 | if (!is_dir($Folder . '/' . date('Y') . '/' . date('m'))
|
||||
| 60 | && !file_exists($Folder . '/' . date('Y') . '/' . date('m'))) {
|
||||
| 61 | mkdir($Folder . '/' . date('Y') . '/' . date('m'), 0755);
|
||||
| 62 | } |
||||
| 63 | |||||
| 64 | if (!is_dir($Folder . '/' . date('Y') . '/' . date('m') . '/' . date('d'))
|
||||
| 65 | && !file_exists($Folder . '/' . date('Y') . '/' . date('m') . '/' . date('d'))) {
|
||||
| 66 | mkdir($Folder . '/' . date('Y') . '/' . date('m') . '/' . date('d'), 0755);
|
||||
| 67 | } |
||||
| 68 | } |
||||
| 69 | } |
||||
| 70 | |||||
| 71 | /** |
||||
| 72 | * Method responsible for structuring the information that will be printed in the log .txt file. |
||||
| 73 | * Método responsável pela estruturação das informações que serão impressas no arquivo log .txt. |
||||
| 74 | * |
||||
| 75 | * @param $Name |
||||
| 76 | * @param $Status |
||||
| 77 | * @param $Data |
||||
| 78 | */ |
||||
| 79 | public function CreateText() |
||||
| 80 | {
|
||||
| 81 | |||||
| 82 | //Predefined and standard text |
||||
| 83 | //Texto pré-definido e padrão |
||||
| 84 | $this->text = "[LOG - ". date("d/m/Y H:i:s")."]
|
||||
| 85 | |||||
| 86 | URL: ".__DIR__." |
||||
| 87 | IP: ".$_SERVER['REMOTE_ADDR']." |
||||
| 88 | NAVEGADOR: ".$_SERVER['HTTP_USER_AGENT']." |
||||
| 89 | PORTA: ".$_SERVER['REMOTE_PORT'].""; |
||||
| 90 | |||||
| 91 | //We retrieve the data reported through the array |
||||
| 92 | //Resgatamos os dados informados através do array |
||||
| 93 | $this->text .= " |
||||
| 94 | |||||
| 95 | Resultados do {$this->Name}:
|
||||
| 96 | Usuário: {$this->Data['usuario']}
|
||||
| 97 | E-mail: {$this->Data['email']}
|
||||
| 98 | Perfil: {$this->Data['perfil']}
|
||||
| 99 | Sessão: {$this->Data['sessao']}
|
||||
| 100 | |||||
| 101 | Status: {$this->Status}";
|
||||
| 102 | } |
||||
| 103 | |||||
| 104 | /* |
||||
| 105 | * Method responsible for handling and generating the log file. |
||||
| 106 | * Método responsável por tratar e gerar o arquivo do log. |
||||
| 107 | * |
||||
| 108 | * @param $folderDir |
||||
| 109 | * @param $text |
||||
| 110 | */ |
||||
| 111 | public function CreateFile($FolderDir, $text){
|
||||
| 112 | |||||
| 113 | $this->CreateText(); |
||||
| 114 | |||||
| 115 | $this->$FolderDir = $FolderDir; |
||||
| 116 | |||||
| 117 | //Log File Creation |
||||
| 118 | //Criação de Arquivo para o Log |
||||
| 119 | $file = $this->Name.'-' . date('Y-m-d') . '-'.time() .'.txt';
|
||||
| 120 | |||||
| 121 | $dir = $this->$FolderDir . $file; |
||||
| 122 | |||||
| 123 | //We generate the file name and the content that will be printed on the file. |
||||
| 124 | //Geramos o nome do arquivo e o conteúdo do mesmo. |
||||
| 125 | $fopen = fopen($dir, "w+"); |
||||
| 126 | fwrite($fopen, $text); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 127 | fclose($fopen); |
||||
|
0 ignored issues
–
show
It seems like
$fopen can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 128 | } |
||||
| 129 | |||||
| 130 | |||||
| 131 | /* |
||||
| 132 | * Method responsible for creating the Automatic Log every time it is called |
||||
| 133 | * Método responsável por criar um Log automático |
||||
| 134 | * |
||||
| 135 | * @param string $Name |
||||
| 136 | * @param string $Status |
||||
| 137 | * @param $Data |
||||
| 138 | * @return bool |
||||
| 139 | */ |
||||
| 140 | public function LogCreate($Name, $Status, $Data){
|
||||
| 141 | $this->Data = $Data; |
||||
| 142 | $this->Status= $Status; |
||||
| 143 | $this->Name= $Name; |
||||
| 144 | |||||
| 145 | //We call the method to create the Log folder and subfolder, if it was not generated |
||||
| 146 | //Chamamos o método para criar a pasta Log e subpasta, se não foi gerado |
||||
| 147 | $this->CreateFolder(); |
||||
| 148 | |||||
| 149 | //We define the name of the folder and its subfolders. |
||||
| 150 | //Definimos o nome da pasta e suas subpastas. |
||||
| 151 | $FolderDir = $this->Folder . '/' . date('Y') . '/' . date('m') . '/' . date('d') . '/';
|
||||
| 152 | |||||
| 153 | $this->CreateText(); |
||||
| 154 | $this->CreateFile($FolderDir, $this->text); |
||||
| 155 | |||||
| 156 | return true; |
||||
| 157 | |||||
| 158 | } |
||||
| 159 | |||||
| 160 | public function CreateManual($FolderDir, $text){
|
||||
| 161 | |||||
| 162 | $this->$FolderDir = $FolderDir; |
||||
| 163 | $this->text = $text; |
||||
| 164 | |||||
| 165 | $dir = $this->$FolderDir; |
||||
| 166 | |||||
| 167 | //Log File Creation |
||||
| 168 | //Criação de Arquivo para o Log |
||||
| 169 | $file = $this->Name.'-' . date('Y-m-d') . '-'.time() .'.txt';
|
||||
| 170 | |||||
| 171 | //We generate the file name and the content that will be printed on the file. |
||||
| 172 | //Geramos o nome do arquivo e o conteúdo do mesmo. |
||||
| 173 | $fopen = fopen($dir . $file, "w+"); |
||||
| 174 | fwrite($fopen, $this->text); |
||||
|
0 ignored issues
–
show
It seems like
$fopen can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 175 | fclose($fopen); |
||||
|
0 ignored issues
–
show
It seems like
$fopen can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 176 | } |
||||
| 177 | |||||
| 178 | public function LogManual($Name, $Data){
|
||||
| 179 | $this->Data= $Data; |
||||
| 180 | $this->Name= $Name; |
||||
| 181 | |||||
| 182 | //We call the method to create the Log folder and subfolder, if it was not generated |
||||
| 183 | //Chamamos o método para criar a pasta Log e subpasta, se não foi gerado |
||||
| 184 | $this->CreateFolder(); |
||||
| 185 | |||||
| 186 | //Texto Pré-definido |
||||
| 187 | $content = "[LOG - ". date("d/m/Y H:i:s")."]";
|
||||
| 188 | |||||
| 189 | //Texto personalizado e encaminhado via parâmetro. |
||||
| 190 | $content.= "{$this->Data}";
|
||||
| 191 | |||||
| 192 | //We define the name of the folder and its subfolders. |
||||
| 193 | //Definimos o nome da pasta e suas subpastas. |
||||
| 194 | $FolderDir = $this->Folder . '/' . date('Y') . '/' . date('m') . '/' . date('d') . '/';
|
||||
| 195 | |||||
| 196 | $this->CreateManual($FolderDir, $content); |
||||
| 197 | |||||
| 198 | return true; |
||||
| 199 | |||||
| 200 | } |
||||
| 201 | |||||
| 202 | } |
||||
| 203 |