-
Notifications
You must be signed in to change notification settings - Fork 1
/
LandiniPlugin.php
executable file
·185 lines (160 loc) · 5.93 KB
/
LandiniPlugin.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
<?php
if (!is_file(dirname(__FILE__) . '/../plugins-resources/loader.php')) {
die(
'ERROR!: need any Appgini plugins first, plugins-resources folder not found!'
);
}
include dirname(__FILE__) . '/../plugins-resources/loader.php';
class LandiniPlugin extends AppGiniPlugin
{
public $title;
public $name;
public $logo;
public $errors;
/* add any plugin-specific properties here */
public function __construct($config = [])
{
parent::__construct($config);
/* add any further plugin-specific initialization here */
}
/* add any further plugin-specific methods here */
public function add_to_file(
$file_path,
$find_function = false,
$code,
$location = 0
) {
/* Check if file exists and is writable */
$file_code = @file_get_contents($file_path);
if (filesize($file_path) > 0) {
if (!$file_code) {
return $this->error(
'add_to_file',
'Unable to access file: ' . $file_path
);
}
}
/* Find extra function */
if ($find_function) {
$search = '/(' . $find_function . ')/';
preg_match_all($search, $file_code, $matches, PREG_OFFSET_CAPTURE);
if (count($matches) < $location + 1) {
return $this->error(
'add_to_file',
'Could not determine correct function location'
);
}
/* start position of extra function */
$hf_position = $matches[0][$location][1];
/* position of next function, or EOF position if this is the last function in the file */
$nf_position = strlen($file_code);
preg_match_all(
'/(<!-- group and IP address -->)/',
$file_code,
$matches,
PREG_OFFSET_CAPTURE,
$hf_position + 10
);
if (count($matches)) {
$nf_position = $matches[0][0][1];
}
/* extras function code */
$old_function_code = substr(
$file_code,
$hf_position,
$nf_position - $hf_position
);
} else {
$old_function_code = substr($file_code, 0);
}
/* Checks $code is not already in there */
if (strpos($old_function_code, $code) !== false) {
return $this->error('add_to_file', 'Code already exists');
}
/* insert $code and save */
$code_comment =
"/* Inserted by {$this->title} on " . date('Y-m-d h:i:s') . ' */';
$new_code = "\n<?php {$code_comment} ?>\n\t\t{$code}\n<?php /* End of {$this->title} code */ ?>\n";
$new_function_code = preg_replace(
'/' . makeSafe($find_function) . '/',
$new_code,
$old_function_code,
1
);
if (!$new_function_code) {
return $this->error('add_to_file', 'Error while injecting code');
}
if ($new_function_code == $old_function_code) {
return $this->error('add_to_file', 'Nothing changed');
}
$file_code = substr_replace($file_code, $new_function_code, 0);
//$file_code = substr_replace($file_code, $new_function_code,$hf_position + strlen($find_function) ,0);
if (!@file_put_contents($file_path, $file_code)) {
return $this->error('add_to_file', 'Could not save changes');
}
return true;
}
public function my_copy_file($src, $dest, $log = false)
{
if ($log) {
$this->progress_log->add(
'Copying ' . basename($src) . ' ... ',
'text-info'
);
}
if (!is_file($src)) {
if ($log) {
$this->progress_log->failed();
}
return false;
}
$path = pathinfo($dest);
if (!file_exists($path['dirname'])) {
mkdir($path['dirname'], 0777, true);
}
if (!@copy($src, $dest)) {
if ($log) {
$this->progress_log->failed();
}
return false;
}
if ($log) {
$this->progress_log->ok();
}
return true;
}
public function check_if_exist_code($hook_file_path, $hook_function=false, $code)
{
/* Check if hook file exists and is writable */
$hook_code = @file_get_contents($hook_file_path);
if (!$hook_code) {
return $this->error('check_hook', 'Unable to access hook file');
}
if ($hook_function){
/* Find hook function */
preg_match('/function\s+' . $hook_function . '\s*\(/i', $hook_code, $matches, PREG_OFFSET_CAPTURE);
if (count($matches) != 1) {
return $this->error('check_hook', 'Could not determine correct function location');
}
/* start position of hook function */
$hf_position = $matches[0][1];
/* position of next function, or EOF position if this is the last function in the file */
$nf_position = strlen($hook_code);
preg_match('/function\s+[a-z0-9_]+\s*\(/i', $hook_code, $matches, PREG_OFFSET_CAPTURE, $hf_position + 10);
if (count($matches)) {
$nf_position = $matches[0][1];
}
$old_function_code = substr($hook_code, $hf_position, $nf_position - $hf_position);
}else{
$old_function_code = substr($hook_code, 0);
}
/* hook function code */
/* Checks $code is not already in there */
if (strpos($old_function_code, $code) !== false) {
$this->error('check_hook', 'Code already exists');
return true;
}else{
return false;
}
}
}