1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Debug mode log option. |
4
|
|
|
* |
5
|
|
|
* @package ThreemaGateway |
6
|
|
|
* @author rugk |
7
|
|
|
* @copyright Copyright (c) 2016 rugk |
8
|
|
|
* @license MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
class ThreemaGateway_Option_DebugModeLog |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string Default file path |
15
|
|
|
*/ |
16
|
|
|
const DEFAULT_PATH = 'internal_data/threemagateway/receivedmsgs.log'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Renders the debug mode log setting. |
20
|
|
|
* |
21
|
|
|
* Basically it just hides the setting if the debug mode of XenForo is disabled. |
22
|
|
|
* |
23
|
|
|
* @param XenForo_View $view View object |
24
|
|
|
* @param string $fieldPrefix Prefix for the HTML form field name |
25
|
|
|
* @param array $preparedOption Prepared option info |
26
|
|
|
* @param bool $canEdit True if an "edit" link should appear |
27
|
|
|
* |
28
|
|
|
* @return XenForo_Template_Abstract Template object |
29
|
|
|
*/ |
30
|
|
|
public static function renderOption(XenForo_View $view, $fieldPrefix, array $preparedOption, $canEdit) |
31
|
|
|
{ |
32
|
|
|
$preparedOption['option_value'] = self::correctOption($preparedOption['option_value']); |
33
|
|
|
|
34
|
|
|
// hide option when disabled and debug mode is off (so that users are not confused) |
35
|
|
View Code Duplication |
if (!XenForo_Application::debugMode() && !$preparedOption['option_value']['enabled']) { |
|
|
|
|
36
|
|
|
return XenForo_ViewAdmin_Helper_Option::renderOptionTemplateInternal('threemagateway_option_list_option_hidden', $view, $fieldPrefix, $preparedOption, $canEdit); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// set options |
40
|
|
|
$preparedOption['edit_format'] = 'onofftextbox'; |
41
|
|
|
$preparedOption['formatParams'] = [ |
42
|
|
|
'onoff' => 'enabled', |
43
|
|
|
'value' => 'path', |
44
|
|
|
'type' => 'textbox', |
45
|
|
|
'default' => self::DEFAULT_PATH, |
46
|
|
|
'placeholder' => self::DEFAULT_PATH |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
//pass this to the default handler |
50
|
|
|
return XenForo_ViewAdmin_Helper_Option::renderPreparedOptionHtml($view, $preparedOption, $canEdit); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Verifies whether the dir of the file is valid (can be created) and is writable. |
55
|
|
|
* |
56
|
|
|
* @param string $filepath Input |
57
|
|
|
* @param XenForo_DataWriter $dataWriter |
58
|
|
|
* @param string $fieldName Name of field/option |
59
|
|
|
* |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public static function verifyOption(&$filepath, XenForo_DataWriter $dataWriter, $fieldName) |
63
|
|
|
{ |
64
|
|
|
$filepath = self::correctOption($filepath); |
|
|
|
|
65
|
|
|
|
66
|
|
|
// check path & (create) dir |
67
|
|
|
$dirpath = dirname($filepath['path']); |
68
|
|
|
$absoluteDir = XenForo_Application::getInstance()->getRootDir() . '/' . $dirpath; |
69
|
|
|
if (!ThreemaGateway_Handler_Validation::checkDir($absoluteDir)) { |
70
|
|
|
$dataWriter->error(new XenForo_Phrase('threemagw_invalid_debuglogpath'), $fieldName); |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// auto-remove existing file if necessary |
75
|
|
|
self::removeLog($filepath); |
76
|
|
|
|
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Remove the log file. |
82
|
|
|
* |
83
|
|
|
* @param array $option option setting |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public static function removeLog($option) |
87
|
|
|
{ |
88
|
|
|
// to be sure check the path again |
89
|
|
|
$option = self::correctOption($option); |
90
|
|
|
|
91
|
|
|
// check pre-conditions |
92
|
|
|
if (!$option['enabled'] || !file_exists($option['path'])) { |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// remove file |
97
|
|
|
return unlink(realpath($option['path'])); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Corrects the option array. |
102
|
|
|
* |
103
|
|
|
* @param array $option |
104
|
|
|
* @return string |
|
|
|
|
105
|
|
|
*/ |
106
|
|
|
protected static function correctOption($option) |
107
|
|
|
{ |
108
|
|
|
// correct value |
109
|
|
|
if (empty($option)) { |
110
|
|
|
/** @var XenForo_Options $xenOptions */ |
111
|
|
|
$xenOptions = XenForo_Application::getOptions(); |
112
|
|
|
|
113
|
|
|
// save file path even if disabled |
114
|
|
|
$option['enabled'] = 0; |
115
|
|
|
$option['path'] = $xenOptions->threema_gateway_logreceivedmsgs['path']; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// set default value |
119
|
|
|
if (empty($option['path'])) { |
120
|
|
|
$option['path'] = self::DEFAULT_PATH; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
// correct path |
124
|
|
View Code Duplication |
if (substr($option['path'], 0, 1) == '/') { |
|
|
|
|
125
|
|
|
$option['path'] = substr($option['path'], 1); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $option; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.