1: <?php
2: /*************************************************************************************/
3: /* */
4: /* Thelia */
5: /* */
6: /* Copyright (c) OpenStudio */
7: /* email : info@thelia.net */
8: /* web : http://www.thelia.net */
9: /* */
10: /* This program is free software; you can redistribute it and/or modify */
11: /* it under the terms of the GNU General Public License as published by */
12: /* the Free Software Foundation; either version 3 of the License */
13: /* */
14: /* This program is distributed in the hope that it will be useful, */
15: /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
16: /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
17: /* GNU General Public License for more details. */
18: /* */
19: /* You should have received a copy of the GNU General Public License */
20: /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
21: /* */
22: /*************************************************************************************/
23:
24: namespace Thelia\Log\Destination;
25:
26: use Thelia\Log\AbstractTlogDestination;
27: use Thelia\Log\TlogDestinationConfig;
28:
29: class TlogDestinationFile extends AbstractTlogDestination
30: {
31: // Nom des variables de configuration
32: // ----------------------------------
33: const VAR_PATH_FILE = "tlog_destinationfile_path";
34: const TLOG_DEFAULT_NAME = "log-thelia.txt";
35:
36: const VAR_MODE = "tlog_destinationfile_mode";
37: const VALEUR_MODE_DEFAULT = "A";
38:
39: protected $path_defaut = false;
40: protected $fh = false;
41:
42: public function __construct()
43: {
44: $this->path_defaut = THELIA_ROOT . "log/" . self::TLOG_DEFAULT_NAME;
45: parent::__construct();
46: }
47:
48: public function configure()
49: {
50: $file_path = $this->getConfig(self::VAR_PATH_FILE);
51: $mode = strtolower($this->getConfig(self::VAR_MODE)) == 'a' ? 'a' : 'w';
52:
53: if (! empty($file_path)) {
54: if (! is_file($file_path)) {
55: $dir = dirname($file_path);
56: if (! is_dir($dir)) {
57: mkdir($dir, 0777, true);
58: }
59:
60: touch($file_path);
61: chmod($file_path, 0777);
62: }
63:
64: if ($this->fh) @fclose($this->fh);
65:
66: $this->fh = fopen($file_path, $mode);
67: }
68: }
69:
70: public function getTitle()
71: {
72: return "Text File";
73: }
74:
75: public function getDescription()
76: {
77: return "Store logs into text file";
78: }
79:
80: public function getConfigs()
81: {
82: return array(
83: new TlogDestinationConfig(
84: self::VAR_PATH_FILE,
85: "Chemin du fichier",
86: "Attention, vous devez indiquer un chemin absolu.<br />Le répertoire de base de votre Thelia est ".dirname(getcwd()),
87: $this->path_defaut,
88: TlogDestinationConfig::TYPE_TEXTFIELD
89: ),
90: new TlogDestinationConfig(
91: self::VAR_MODE,
92: "Mode d'ouverture (A ou E)",
93: "Indiquez E pour ré-initialiser le fichier à chaque requête, A pour ne jamais réinitialiser le fichier. Pensez à le vider de temps en temps !",
94: self::VALEUR_MODE_DEFAULT,
95: TlogDestinationConfig::TYPE_TEXTFIELD
96: )
97: );
98: }
99:
100: public function add($texte)
101: {
102: if ($this->fh) {
103: fwrite($this->fh, $texte."\n");
104: }
105: }
106:
107: public function write(&$res)
108: {
109: if ($this->fh) @fclose($this->fh);
110:
111: $this->fh = false;
112: }
113: }
114: