1 | <?php |
||
2 | |||
3 | //FIXME remove Deprecated template |
||
4 | |||
5 | /** |
||
6 | * convert-template.inc.php |
||
7 | * |
||
8 | * Ajax method to convert templates from the old syntax to the blade syntax |
||
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, or |
||
13 | * (at your option) any later version. |
||
14 | * |
||
15 | * This program is distributed in the hope that it will be useful, |
||
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
||
18 | * GNU General Public License for more details. |
||
19 | * |
||
20 | * You should have received a copy of the GNU General Public License |
||
21 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
22 | * |
||
23 | * @link https://www.librenms.org |
||
24 | * |
||
25 | * @copyright 2018 Neil Lathwood |
||
26 | * @copyright 2018 Tony Murray |
||
27 | * @author Tony Murray <[email protected]> |
||
28 | */ |
||
29 | |||
30 | use Illuminate\Support\Str; |
||
31 | |||
32 | header('Content-type: application/json'); |
||
33 | |||
34 | if (! Auth::user()->hasGlobalAdmin()) { |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
35 | exit(json_encode([ |
||
36 | 'status' => 'error', |
||
37 | 'message' => 'You need to be admin', |
||
38 | ])); |
||
39 | } |
||
40 | |||
41 | if (empty($vars['template'])) { |
||
42 | exit(json_encode([ |
||
43 | 'status' => 'error', |
||
44 | 'message' => 'No template to convert', |
||
45 | ])); |
||
46 | } |
||
47 | |||
48 | $new_body = ''; |
||
49 | foreach (explode(PHP_EOL, $vars['template']) as $line) { |
||
50 | $new_body .= convert_template($line) . PHP_EOL; |
||
51 | } |
||
52 | $new_title = convert_template($vars['title']); |
||
53 | |||
54 | function convert_template($line) |
||
55 | { |
||
56 | if (Str::contains($line, '{calc')) { |
||
57 | return preg_replace( |
||
58 | [ |
||
59 | '/{calc[ ]*([\w\d\s\%\.\(\)\*\/\-\+\/]+)}/', // Replaces {calc (something*100)} |
||
60 | '/%([\w\d]+)\.([\w\d]+)/', // Replaces %something.anything |
||
61 | ], |
||
62 | [ |
||
63 | "@php\necho \\1;\n@endphp ", |
||
64 | '$value[\'\2\']', |
||
65 | ], |
||
66 | $line |
||
67 | ); |
||
68 | } |
||
69 | |||
70 | $old1 = $line; |
||
71 | $find = [ |
||
72 | '/{if %([\w=\s]+)}/', // Replaces {if %something == else} |
||
73 | '/{else}/', // Replaces {else} |
||
74 | '/{\/if}/', // Replaces {/if} |
||
75 | '/{foreach %faults}/', // Replaces {foreach %faults} |
||
76 | '/{foreach %contacts}/', // Replaces {foreach %contacts} |
||
77 | '/{\/foreach}/', // Replaces {/foreach} |
||
78 | '/{calc[ ]*([\w\d\s\%\.\(\)\*\/\-\+\/]+)}/', // Replaces {calc (something*100)} |
||
79 | '/%value.string/', // Replaces %value.string |
||
80 | '/%([\w\d]+)\.([\w\d]+)/', // Replaces %something.anything |
||
81 | '/%([\w\d]+)/', // Replaces %anything |
||
82 | ]; |
||
83 | $replace = [ |
||
84 | ' @if ($alert->\1) ', |
||
85 | ' @else ', |
||
86 | ' @endif ', |
||
87 | ' @foreach ($alert->faults as $key => $value)', |
||
88 | ' @foreach ($alert->contacts as $key => $value)', |
||
89 | ' @endforeach ', |
||
90 | " @php\necho \\1;\n@endphp ", |
||
91 | '{{ $value[\'string\'] }}', |
||
92 | '{{ $\1[\'\2\'] }}', |
||
93 | '{{ $alert->\1 }}', |
||
94 | ]; |
||
95 | $old1 = preg_replace($find, $replace, $old1); |
||
96 | |||
97 | // Revert some over-zealous changes: |
||
98 | $find = [ |
||
99 | '/\$alert->key/', |
||
100 | '/\$alert->value/', |
||
101 | ]; |
||
102 | $replace = [ |
||
103 | '$key', |
||
104 | '$value', |
||
105 | ]; |
||
106 | |||
107 | return preg_replace($find, $replace, $old1); |
||
108 | } |
||
109 | |||
110 | exit(json_encode([ |
||
111 | 'status' => 'ok', |
||
112 | 'message' => 'Template converted, review and save to update', |
||
113 | 'template' => $new_body, |
||
114 | 'title' => $new_title, |
||
115 | ])); |
||
116 |