-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.php
845 lines (810 loc) · 31.2 KB
/
log.php
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
<?php
/**
* Log : Classe de log par fichier ou mysql
*
* @package Library
* @subpackage Core
* @author Benoit Raux <rauxbenoit@free.fr>
* @license Open Source (GPL)
*/
namespace Slrfw;
/**
* Log : Classe de log par fichier ou mysql
*
* @package Library
* @subpackage Core
* @author Benoit Raux <rauxbenoit@free.fr>
* @license Open Source (GPL)
*
*
* Par defaut et en mode fichier, la classe op?re une rotation sur les logs.
* D?s que le fichier courant d?passe 5 Mo, il est compress? (gz).
* La visualization des logs parcours ces archives.
* La purge efface ces archives.
*
* Requis pour la rotation par archivage : gzencode, gzopen, gzread, gzclose ...
*
*
* Documentation Technique :
* -------------------------
* Instanciation : ($strToFile, $strToLog='', $strTable='logs', $booArch=true)
* - Mode fichier
* $log = new Log('Chemin vers fichier','En option une chaine ? logger')
* - Mode MySql
* $log = new Log('mysql','En option une chaine ? loguer','En option le nom de la table')
* - $booArch= true ou false : Active/Desactive le systeme de roullement en archive
* Purge des Log : purgeLog($strDateTime='0000-00-00 00:00:00')
* $log->purgeLog(); => vas purger tous les logs
* $log->purgeLog('dateTime); => vas purger tous les logs avant datetime
* Visionner les logs :
* visuLog($strDateTim:eIni='0000-00-00 00:00:00',$strDateTimeEnd='0000-00-00 00:00:00',
* $strContentToSearch='',$booDetail=true,$strIp='')
* $log->visuLog('strDateTimeIni') => vas montrer tous les logs depuis cette date
* $log->visuLog('strDateTimeIni','strDateTimeEnd') => vas montrer tous les logs entre les 2 dates
* $strContentToSearch : permet de faire une recherche sur les logs contenant $strContentToSearch
* $booDetail=true or false : Active / desactive l'affichage des d?tails (Date / Heure / Ip)
* $strIp : permet de faire une recherche sur les log de l'ip $strIp
*
*/
class Log
{
/**
* Pointeur vers le fichier ouvert
*
* @var object
*/
private $objFile;
/**
* Pointeur vers la base de donnée
*
* @var MyPDO
*/
private $db;
/**
* L'ip executant le log
*
* @var string
*/
private $strIp;
/**
* Le mode du log
*
* @var string
*/
private $strLogMode;
/**
* Le nom de la table de log
*
* @var string
*/
private $strLogTable;
/**
* la taille de fichier maximum
*
* @var int
*/
private $intFileSizeMax;
/**
* Archivage actif ou non
*
* @var bool
*/
private $booArch;
/**
* Constructeur de la classe log
*
* @param string $strToFileOrDbConnect Chemin vers le fichier de log ( ? cr?er ou ? suivre).
* Si type db, on vas loguer dans la table $strTable
* @param string $strToLog Une chaine à logguer dès
* l'intanciation (optionelle).
* @param int $idUser identifiant utilisateur ??
* @param string $strTable Un nom de table mysql
* pour les logs (optionelle, par defaut : logs).
* @param bool $booArch Active ou Desactive
* le système de rotation par archivage.
*/
public function __construct(
$strToFileOrDbConnect,
$strToLog = '',
$idUser = 0,
$strTable = 'logs',
$booArch = true
) {
//test si on a une IP (en php-cli par exemple on a pas l'ip)
if (!empty($_SERVER['REMOTE_ADDR'])) {
$this->strIp = $_SERVER['REMOTE_ADDR'];
} else {
$this->strIp = 'NO IP';
}
//On test si on doit ouvrir un fichier en écriture, sinon on test existance de la table)
if (!is_string($strToFileOrDbConnect)) {
$this->strLogMode = 'mysql';
$this->strLogTable = $strTable;
$this->db = $strToFileOrDbConnect;
//Si table n'existe pas on la cr?er
$this->createTable();
} else {
$this->booArch = $booArch;
$this->strLogMode = 'file';
$this->strToFile = $strToFile;
$this->intFileSizeMax = 5 * 1024 * 1024;
}
//test si la chaine ?logguer est pr?sente
if ($strToLog != '') {
//On log
$this->logThis($strToLog, $idUser);
}
}
/**
* mysqlQuery : Execute une requette
*
* @param string $strReq le ficher
*
* @return ressource resultat de la requête sql
*/
private function mysqlQuery($strReq)
{
$objResult = $this->db->query($strReq);
try {
if ($objResult == false) {
$strError = $reqTable . ' : ' . mysql_error() . "\n";
throw new \Slrfw\Exception\Log($strError);
}
} catch (Exception $objExpetion) {
$objExpetion->makeLogExeption();
}
return $objResult;
}
/**
* createTable : Créé la table de log si elle existe pas
*
* @return void
*/
private function createTable()
{
$reqTable = '
CREATE TABLE IF NOT EXISTS `' . $this->strLogTable . '` (
`logId` INT( 100 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`logDate` DATE NOT NULL ,
`logTime` TIME NOT NULL ,
`logIp` VARCHAR( 100 ) NOT NULL ,
`logString` VARCHAR( 1000 ) NOT NULL,
`logStrOpt` VARCHAR( 1000 ) NOT NULL,
`logIdUser` INT(100 ) NOT NULL
)';
$objResult = $this->mysqlQuery($reqTable);
}
/**
* openLogFile : Ouvre le fichier de Log
*
* @return void
*/
private function openLogFile()
{
//On vide la cache sur le syst?me de fichier
clearstatcache();
//Si le fichier existe -> Verifie la taile avant
if (file_exists($this->strToFile)) {
if ($this->booArch === true) {
$intFileSize = filesize($this->strToFile);
if ($intFileSize >= $this->intFileSizeMax) {
//On archive le fichier
$strFileContent = file_get_contents($this->strToFile);
$strToFileGz = $this->strToFile . '.' . date('Ymd_His') . '.gz';
$strFileContentGz = gzencode($strFileContent, 9);
$objFileGz = $this->fopenLogFile($strToFileGz, 'w+');
fwrite($objFileGz, $strFileContentGz);
fclose($objFileGz);
//On suprime le fichier
unlink($this->strToFile);
//On l'ouvre en ecriture
$this->objFile = $this->fopenLogFile($this->strToFile, 'w+');
} else {
//On l'ouvre en append
$this->objFile = $this->fopenLogFile($this->strToFile, 'a+');
}
} else {
//On l'ouvre en append
$this->objFile = $this->fopenLogFile($this->strToFile, 'a+');
}
} else {
$this->objFile = $this->fopenLogFile($this->strToFile, 'w+');
}
}
/**
* openLogFile : Ouvre le fichier de Log
*
* @param string $strFile : le ficher
* @param string $strMode : le mode d'ouverture
*
* @return resource file pointer
*/
private function fopenLogFile($strFile, $strMode)
{
$objFile = fopen($strFile, $strMode);
try {
if ($objFile == false) {
$strError = 'Erreur d\'ouverture du fichier ' . $strFile
. ' en mode ' . $strMode . '!' . "\n"
. 'Vérifiez l\'existance et les droits sur vos fichiers...';
throw new \Slrfw\Exception\Log($strError);
}
} catch (Exception $objExpetion) {
$objExpetion->makeLogExeption();
}
return $objFile;
}
/**
* logThis : Ecrit la chaine $strToLog
*
* @param string $strToLog chaîne a ajouter dans le log
* @param int $idUser identifiant utilisateur ???
* @param string $logStrOpt informations suplémentaires ???
*
* @return void
*/
public function logThis($strToLog, $idUser = 0, $logStrOpt = '')
{
switch ($this->strLogMode) {
case 'mysql':
$this->logThisInTable($strToLog, $idUser, $logStrOpt);
break;
case 'file':
$strToLog = str_replace(array("\n", "\r"), array(' ', ' '), $strToLog);
$this->openLogFile();
$this->logThisInFile($strToLog);
$this->closeLogFile();
break;
}
}
/**
* logThisInFile : Ecrit la chaine $strToLog dans le fichier
*
* @param string $strToLog chaîne a ajouter dans le log
*
* @return void
*/
private function logThisInFile($strToLog)
{
fwrite(
$this->objFile, date('Y-m-d') . "\t" . date('H:i:s') . "\t"
. $this->strIp . "\t" . $strToLog . "\n"
);
}
/**
* logThisInTable : Ecrit la chaine $strToLog dans la table mysql
*
* @param string $strToLog cha?ne a ajouter dans le log
* @param int $idUser identifiant utilisateur ???
* @param string $strOpt informations suplémentaires ???
*
* @return void
*/
private function logThisInTable($strToLog, $idUser, $strOpt)
{
$reqLog = 'INSERT INTO `' . $this->strLogTable . '` '
. '(`logDate`,`logTime`,`logIp`,`logString`,`logStrOpt`, `logIdUser`) '
. 'VALUES('
. '\'' . date('Y-m-d') . '\','
. '\'' . date('H:i:s') . '\','
. '\'' . $this->strIp . '\','
. '' . $this->db->quote($strToLog) . ','
. '' . $this->db->quote($strOpt) . ','
. '' . $idUser . ''
. '); ';
$objResult = $this->mysqlQuery($reqLog);
}
/**
* closeLogFile : Ferme le fichier de log en cours
*
* @return void
*/
private function closeLogFile()
{
fclose($this->objFile);
}
/**
* purgeLog : Purge le log selon une date / heure
* (supprime tous les enregistrement ant?rieurs ? la date pass?e en param?tre)
*
* @param string $strDateTime : chaine au format datetime (Y-m-d H:i:s)
* limite pour la suppr?ssion des logs
*
* @return void
*/
public function purgeLog($strDateTime = '0000-00-00 00:00:00')
{
//Si par d?faut
if ($strDateTime == '0000-00-00 00:00:00') {
$strDateTime = date('Y-m-d H:i:s');
}
//Controle datetime
if (preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$/', $strDateTime)) {
switch ($this->strLogMode) {
case 'mysql':
$this->purgeLogTable($strDateTime);
break;
case 'file':
$this->purgeLogFiles($strDateTime);
$this->purgeLogCurentFile($strDateTime);
break;
}
} else {
try {
throw new \Slrfw\Exception\Log($strDateTime . ' N\'est pas au bon format');
} catch (Exception $objExpetion) {
$objExpetion->makeLogExeption();
}
}
}
/**
* purgeLogTable : Purge la table de log selon une date / heure
* (supprime tous les enregistrement antérieurs à la date passée en paramètre
* dans la table mysql de log)
*
* @param string $strDateTime chaine au format datetime (Y-m-d H:i:s)
* limite pour la suppression des logs
*
* @return void
*/
private function purgeLogTable($strDateTime)
{
$strDate = substr($strDateTime, 0, 10);
$strTime = substr($strDateTime, 11, 8);
$reqPurge = 'DELETE FROM `' . $db->quote($this->strLogTable) . '` '
. 'WHERE `logDate` < \'' . $strDate . '\' '
. 'OR (`logDate` = \'' . $strDate . '\' AND `logTime` <= \'' . $strTime . '\') ;';
$objResult = $this->mysqlQuery($reqPurge);
}
/**
* dateToTimestamp : Renvoit le timestamp d'une date MYSQL.
*
* @param string $strDateTime : chaine au format datetime MYSQL
*
* @return int
*/
private function dateToTimestamp($strDateTime)
{
return strtotime($strDateTime);
}
/**
* purgeLogCurentFile : Purge le fichier log en cours selon une date / heure
* (supprime tous les enregistrement ant?rieurs ? la date pass?e en param?tre)
*
* @param string $strDateTime : chaine au format datetime (Y-m-d H:i:s)
* limite pour la suppression des logs
*
* @return void
*/
private function purgeLogCurentFile($strDateTime)
{
$intTimeStampeDate = $this->dateToTimestamp($strDateTime);
$strContent = '';
$arrLineContent = array();
//Ouverture du fichier en lecture
$this->objFile = $this->fopenLogFile($this->strToFile, 'r+');
//Parcour tous le fichier
while (!feof($this->objFile)) {
$strLineContent = fgets($this->objFile);
$arrLineContent = explode("\t", $strLineContent);
if (sizeof($arrLineContent) > 1) {
$strDateLine = $arrLineContent[0];
$strTimeLine = $arrLineContent[1];
$strDateTimeLine = $strDateLine . ' ' . $strTimeLine;
$intTimeStampLine = $this->dateToTimestamp($strDateTimeLine);
//si la ligne respecte les crit?res de date on la stock
if ($intTimeStampLine > $intTimeStampeDate) {
$strContent .= $strLineContent;
}
}
}
//On ferme le fichier
$this->closeLogFile();
//On suprime le fichier
unlink($this->strToFile);
//On l'ouvre en ecriture
$this->objFile = $this->fopenLogFile($this->strToFile, 'w+');
fwrite($this->objFile, $strContent);
$this->closeLogFile();
}
/**
* purgeLogFiles : Purge les fichier log archivés selon une date / heure
* (supprime tous les enregistrement antérieurs à la date passée en paramètre
* dans les fichier de log archivés)
*
* @param string $strDateTime chaine au format datetime (Y-m-d H:i:s)
* limite pour la suppression des logs
*
* @return void
*/
public function purgeLogFiles($strDateTime)
{
$intTimeStampeDate = $this->dateToTimestamp($strDateTime);
//vas chercher la listes des fichiers archiv?s
$arrArchFiles = glob($this->strToFile . '.*.gz');
foreach ($arrArchFiles as $strPathToArch) {
$strDateArch = str_replace(
array($this->strToFile . '.', '.gz'), array(''), $strPathToArch
);
$strDateTimeArch = substr($strDateArch, 0, 4) . '-'
. substr($strDateArch, 4, 2) . '-'
. substr($strDateArch, 6, 2) . ' '
. substr($strDateArch, 9, 2) . ':'
. substr($strDateArch, 11, 2) . ':'
. substr($strDateArch, 13, 2);
$intTimeStampArch = $this->dateToTimestamp($strDateTimeArch);
if ($intTimeStampArch <= $intTimeStampeDate) {
unlink($strPathToArch);
}
}
}
/**
* visuLog : Fonction pour afficher les logs dans la page
*
* @param string $strDateTimeIni dateTime (AAAA-MM-DD HH-MM-SS) de debut
* @param string $strDateTimeEnd dateTime (AAAA-MM-DD HH-MM-SS) de fin,
* si = 0000-00-00 00:00:00 : pas de date de fin
* @param string $strContentToSearch un contenu recherch? (optionel)
* @param bool $booDetail affichage des detail actif ou non (Date / Heure / Ip)
* @param string $strIp une ip recherch?e (optionel)
*
* @return void
*/
public function visuLog(
$strDateTimeIni = '0000-00-00 00:00:00',
$strDateTimeEnd = '0000-00-00 00:00:00',
$strContentToSearch = '',
$booDetail = true,
$strIp = ''
) {
//Controle datetime
$pattern = '/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$/';
if (preg_match($pattern, $strDateTimeIni)) {
} else {
try {
throw new \Slrfw\Exception\Log(
'Date Ini : ' . $strDateTimeIni . ' N\'est pas au bon format'
);
} catch (Exception $objExpetion) {
$objExpetion->makeLogExeption();
}
}
if (preg_match($pattern, $strDateTimeEnd)) {
} else {
try {
throw new \Slrfw\Exception\Log(
'Date End : ' . $strDateTimeEnd . ' N\'est pas au bon format'
);
} catch (Exception $objExpetion) {
$objExpetion->makeLogExeption();
}
}
//Si pas de date de fin -> on met la date actuel
if ('0000-00-00 00:00:00' == $strDateTimeEnd) {
$strDateTimeEnd = date('Y-m-d H:i:s');
}
$strContent = $this->returnLog(
$strDateTimeIni, $strDateTimeEnd, $strContentToSearch, $booDetail, $strIp
);
echo '<pre>' . $strContent . '</pre>';
}
/**
* returnLog : Fonction pour r?cup?rer le contenu des log dans une chaine
*
* @param string $strDateTimeIni dateTime (AAAA-MM-DD HH-MM-SS) de debut
* @param string $strDateTimeEnd dateTime (AAAA-MM-DD HH-MM-SS) de fin,
* si = 0000-00-00 00:00:00 : pas de date de fin
* @param string $strContentToSearch un contenu recherch? (optionel)
* @param bool $booDetail affichage des detail actif ou non (Date / Heure / Ip)
* @param string $strIp une ip recherch?e (optionel)
*
* @return string : contenu des logs
*/
private function returnLog(
$strDateTimeIni = '0000-00-00 00:00:00',
$strDateTimeEnd = '0000-00-00 00:00:00',
$strContentToSearch = '',
$booDetail = true,
$strIp = ''
) {
$pattern = '/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$/';
//Controle datetime
if (preg_match($pattern, $strDateTimeIni)) {
} else {
try {
throw new \Slrfw\Exception\Log(
'Date Ini : ' . $strDateTimeIni . ' N\'est pas au bon format'
);
} catch (Exception $objExpetion) {
$objExpetion->makeLogExeption();
}
}
if (preg_match($pattern, $strDateTimeEnd)) {
} else {
try {
throw new \Slrfw\Exception\Log(
'Date End : ' . $strDateTimeEnd . ' N\'est pas au bon format'
);
} catch (Exception $objExpetion) {
$objExpetion->makeLogExeption();
}
}
//Si pas de date de fin -> on met la date actuel
if ('0000-00-00 00:00:00' == $strDateTimeEnd) {
$strDateTimeEnd = date('Y-m-d H:i:s');
}
$strContent = '';
//Switch du mode
switch ($this->strLogMode) {
case 'mysql':
$strContent = $this->returnLogMysqlContent(
$strDateTimeIni, $strDateTimeEnd,
$strContentToSearch, $booDetail, $strIp
);
break;
case 'file':
$strContent .= $this->returnLogArchFilesContent(
$strDateTimeIni, $strDateTimeEnd,
$strContentToSearch, $booDetail, $strIp
);
$strContent .= $this->returnLogFileContent(
$strDateTimeIni, $strDateTimeEnd,
$strContentToSearch, $booDetail, $strIp
);
break;
}
return $strContent;
}
/**
* returnLogArchFilesContent : Fonction qui retourne le contenu des fichiers de log Archivés
*
* @param string $strDateTimeIni dateTime (AAAA-MM-DD HH-MM-SS) de debut
* @param string $strDateTimeEnd dateTime (AAAA-MM-DD HH-MM-SS) de fin,
* si = 0000-00-00 00:00:00 : pas de date de fin
* @param string $strContentToSearch un contenu recherch? (optionel)
* @param bool $booDetail affichage des detail actif ou non (Date / Heure / Ip)
* @param string $strIp une ip recherch?e (optionel)
*
* @return string : contenu des logs
*/
private function returnLogArchFilesContent(
$strDateTimeIni,
$strDateTimeEnd,
$strContentToSearch,
$booDetail,
$strIp
) {
$strContentRequired = '';
$intTimeStampDateIni = $this->dateToTimestamp($strDateTimeIni);
$intTimeStampDateEnd = $this->dateToTimestamp($strDateTimeEnd);
//Parcour des fichiers de log archiv?s
$arrArchFiles = glob($this->strToFile . '.*.gz');
//Declaration du tableau des fichiers archive ? parser
$arrFilesToParse = array();
foreach ($arrArchFiles as $strPathToArch) {
$strDateArch = str_replace(
array($this->strToFile . '.', '.gz'), array(''), $strPathToArch
);
$strDateTimeArch = substr($strDateArch, 0, 4) . '-'
. substr($strDateArch, 4, 2) . '-'
. substr($strDateArch, 6, 2) . ' '
. substr($strDateArch, 9, 2) . ':'
. substr($strDateArch, 11, 2) . ':'
. substr($strDateArch, 13, 2);
$intTimeStampArch = $this->dateToTimestamp($strDateTimeArch);
if ($intTimeStampArch >= $intTimeStampDateIni) {
$arrFilesToParse[] = $strPathToArch;
}
}
//Lecture des fichiers à parser
foreach ($arrFilesToParse as $strPathToArch) {
$objFileArch = gzopen($strPathToArch, 'r');
$strArchContent = '';
while (!feof($objFileArch)) {
$strArchContent .= gzread($objFileArch, 10000);
}
gzclose($objFileArch);
//Creation d'un tableau contenant lignes et du log archiv?
$arrArchLineContent = array();
$arrArchLineContent = explode("\n", $strArchContent);
foreach ($arrArchLineContent as $strLineArch) {
$arrArchColContent = array();
$arrArchColContent = explode("\t", $strLineArch);
//Si la ligne est pas vide
if (sizeof($arrArchColContent) > 1) {
$intTimeStampLine = $this->dateToTimestamp(
$arrArchColContent[0] . ' ' . $arrArchColContent[1], 'MYSQL'
);
if ($intTimeStampLine >= $intTimeStampDateIni
&& $intTimeStampLine <= $intTimeStampDateEnd
) {
$booLineCheck = true;
//Test notre crit?re de contenu
if ($strContentToSearch != '') {
$strLineContentToCheck = str_replace(
$arrArchColContent[0] . "\t"
. $arrArchColContent[1] . "\t"
. $arrArchColContent[2] . "\t", '', $strLineArch
) . "\n";
if (!strstr($strLineContentToCheck, $strContentToSearch)) {
$booLineCheck = false;
}
}
//Test crit?re d'ip
if ($strIp != '' && !strstr($arrArchColContent[2], $strIp)) {
$booLineCheck = false;
}
if ($booLineCheck === true) {
//La ligne corespond a notre recherhe
if ($booDetail === true) {
$strContentRequired .= $strLineArch . "\n";
} else {
$strContentRequired .= str_replace(
$arrArchColContent[0] . "\t"
. $arrArchColContent[1] . "\t"
. $arrArchColContent[2] . "\t", '', $strLineArch
) . "\n";
}
}
} elseif ($intTimeStampLine > $intTimeStampDateEnd) {
//On a d?pass? notre crit?re de date de fin
//=> on sort de la fonction pas la peine de continuer
return $strContentRequired;
}
}
}
}
//Retourne le contenu
return $strContentRequired;
}
/**
* returnLogFileContent : Fonction qui retourne le contenu du fichier de log en cours
*
* @param string $strDateTimeIni dateTime (AAAA-MM-DD HH-MM-SS) de debut
* @param string $strDateTimeEnd dateTime (AAAA-MM-DD HH-MM-SS) de fin,
* si = 0000-00-00 00:00:00 : pas de date de fin
* @param string $strContentToSearch un contenu recherch? (optionel)
* @param bool $booDetail affichage des detail actif ou non (Date / Heure / Ip)
* @param string $strIp une ip recherch?e (optionel)
*
* @return string contenu de la table de log
*/
private function returnLogFileContent(
$strDateTimeIni,
$strDateTimeEnd,
$strContentToSearch,
$booDetail,
$strIp
) {
$intTimeStampDateIni = $this->dateToTimestamp($strDateTimeIni);
$intTimeStampDateEnd = $this->dateToTimestamp($strDateTimeEnd);
$strContentRequired = '';
$arrLineContent = array();
//Ouverture du fichier en lecture
$this->objFile = $this->fopenLogFile($this->strToFile, 'r');
//Parcour tous le fichier
while (!feof($this->objFile)) {
$strLineContent = fgets($this->objFile);
$arrLineContent = explode("\t", $strLineContent);
if (sizeof($arrLineContent) > 1) {
$strDateLine = $arrLineContent[0];
$strTimeLine = $arrLineContent[1];
$strDateTimeLine = $strDateLine . ' ' . $strTimeLine;
$intTimeStampLine = $this->dateToTimestamp($strDateTimeLine);
if ($intTimeStampLine >= $intTimeStampDateIni
&& $intTimeStampLine <= $intTimeStampDateEnd
) {
$booLineCheck = true;
//Test notre crit?re de contenu
if ($strContentToSearch != '') {
$strLineContentToCheck = str_replace(
$arrLineContent[0] . "\t"
. $arrLineContent[1] . "\t"
. $arrLineContent[2] . "\t", '', $strLineContent
);
if (!strstr($strLineContentToCheck, $strContentToSearch)) {
$booLineCheck = false;
}
}
//Test crit?re d'ip
if ($strIp != '' && !strstr($arrLineContent[2], $strIp)) {
$booLineCheck = false;
}
if ($booLineCheck === true) {
//La ligne corespond a notre recherhe
if ($booDetail === true) {
$strContentRequired .= $strLineContent;
} else {
$strContentRequired .= str_replace(
$arrLineContent[0] . "\t"
. $arrLineContent[1] . "\t"
. $arrLineContent[2] . "\t", '', $strLineContent
);
}
}
} elseif ($intTimeStampLine > $intTimeStampDateEnd) {
//On a d?pass? notre crit?re de date de fin
//=> on sort de la fonction pas la peine de continuer
$this->closeLogFile();
return $strContentRequired;
}
}
}
//On ferme le fichier
$this->closeLogFile();
return $strContentRequired;
}
/**
* returnLogMysqlContent : Fonction qui retourne le contenu de la table log
*
* @param string $strDateTimeIni dateTime (AAAA-MM-DD HH-MM-SS) de debut
* @param string $strDateTimeEnd dateTime (AAAA-MM-DD HH-MM-SS) de fin,
* si = 0000-00-00 00:00:00 : pas de date de fin
* @param string $strContentToSearch un contenu recherch? (optionel)
* @param bool $booDetail affichage des detail actif ou non (Date / Heure / Ip)
* @param string $strIp une ip recherch?e (optionel)
*
* @return string contenu de la table de log
*/
private function returnLogMysqlContent(
$strDateTimeIni,
$strDateTimeEnd,
$strContentToSearch,
$booDetail,
$strIp
) {
$strContentRequired = '';
$strDateIni = substr($strDateTimeIni, 0, 10);
$strTimeIni = substr($strDateTimeIni, 11, 8);
$strDateEnd = substr($strDateTimeEnd, 0, 10);
$strTimeEnd = substr($strDateTimeEnd, 11, 8);
$reqLogContent = 'SELECT * FROM `' . $this->db->quote($this->strLogTable) . '` '
. 'WHERE ( '
. '`logDate` < \'' . $strDateEnd . '\' '
. 'OR ( '
. '`logDate` = \'' . $strDateEnd . '\' '
. 'AND `logTime` <= \'' . $strTimeEnd . '\' '
. ') '
. ') '
. 'AND ( '
. '`logDate` > \'' . $strDateIni . '\' '
. 'OR ('
. ' `logDate` = \'' . $strDateIni . '\' '
. 'AND `logTime` >= \'' . $strTimeIni . '\''
. ') '
. ') ';
if ($strIp != '') {
$reqLogContent .= 'AND `logIp` LIKE \'%'
. $this->db->quote($strIp) . '%\' ';
}
if ($strContentToSearch != '') {
$reqLogContent .= 'AND `logString` LIKE '
. '\'%' . $this->db->quote($strContentToSearch)
. '%\' ';
}
$recLogContent = $this->mysqlQuery($reqLogContent);
if (mysql_num_rows($recLogContent)) {
while ($ojbLogContent = mysql_fetch_object($recLogContent)) {
//La ligne corespond a notre recherhe
if ($booDetail === true) {
$strContentRequired .= $ojbLogContent->logDate . "\t"
. $ojbLogContent->logTime . "\t"
. $ojbLogContent->logIp . "\t"
. $ojbLogContent->logString . "\n";
} else {
$strContentRequired .= $ojbLogContent->logString . "\n";
}
}
}
return $strContentRequired;
}
/**
* _toString : Fonction "magic" qui retourne l'objet sous forme de chaine
*
* @return string
*/
public function __toString()
{
return '<pre>' . print_r($this, true) . '</pre>';
}
}