1
+ <?php
2
+
3
+ /**
4
+ * phpipam class to handle ure_rewrites for phpipam version > 1.3.1
5
+ *
6
+ * Old rules:
7
+ *
8
+ * RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/$ index.php?page=$1§ion=$2&subnetId=$3&sPage=$4&ipaddrid=$5&tab=$6 [L]
9
+ * RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/$ index.php?page=$1§ion=$2&subnetId=$3&sPage=$4&ipaddrid=$5 [L,QSA]
10
+ * RewriteRule ^(.*)/(.*)/(.*)/(.*)/$ index.php?page=$1§ion=$2&subnetId=$3&sPage=$4 [L,QSA]
11
+ * RewriteRule ^(.*)/(.*)/(.*)/$ index.php?page=$1§ion=$2&subnetId=$3 [L,QSA]
12
+ * RewriteRule ^(.*)/(.*)/$ index.php?page=$1§ion=$2 [L,QSA]
13
+ * RewriteRule ^(.*)/$ index.php?page=$1 [L]
14
+ *
15
+ *
16
+ * # IE login dashboard fix
17
+ * RewriteRule ^login/dashboard/$ dashboard/ [R]
18
+ * RewriteRule ^logout/dashboard/$ dashboard/ [R]
19
+ * # search override
20
+ * RewriteRule ^tools/search/(.*)$ index.php?page=tools§ion=search&ip=$1 [L]
21
+ *
22
+ *
23
+ * API
24
+ * # exceptions
25
+ * RewriteRule ^(.*)/addresses/search_hostname/(.*)/$ ?app_id=$1&controller=addresses&id=search_hostname&id2=$2 [L,QSA]
26
+ * RewriteRule ^(.*)/prefix/external_id/(.*)/$ ?app_id=$1&controller=prefix&id=external_id&id2=$2 [L,QSA]
27
+ * RewriteRule ^(.*)/prefix/external_id/(.*) ?app_id=$1&controller=prefix&id=external_id&id2=$2 [L,QSA]
28
+ * RewriteRule ^(.*)/(.*)/cidr/(.*)/(.*)/$ ?app_id=$1&controller=$2&id=cidr&id2=$3&id3=$4 [L,QSA]
29
+ * RewriteRule ^(.*)/(.*)/cidr/(.*)/(.*) ?app_id=$1&controller=$2&id=cidr&id2=$3&id3=$4 [L,QSA]
30
+ * # controller rewrites
31
+ * RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/$ ?app_id=$1&controller=$2&id=$3&id2=$4&id3=$5&id4=$6 [L,QSA]
32
+ * RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/$ ?app_id=$1&controller=$2&id=$3&id2=$4&id3=$5 [L,QSA]
33
+ * RewriteRule ^(.*)/(.*)/(.*)/(.*)/$ ?app_id=$1&controller=$2&id=$3&id2=$4 [L,QSA]
34
+ * RewriteRule ^(.*)/(.*)/(.*)/$ ?app_id=$1&controller=$2&id=$3 [L,QSA]
35
+ * RewriteRule ^(.*)/(.*)/$ ?app_id=$1&controller=$2 [L,QSA]
36
+ * RewriteRule ^(.*)/$ ?app_id=$1 [L,QSA]
37
+ *
38
+ */
39
+ class Rewrite {
40
+
41
+ /**
42
+ * Flag if API is used
43
+ *
44
+ * @var bool
45
+ */
46
+ private $ is_api = false ;
47
+
48
+ /**
49
+ * Array of passthroughs
50
+ *
51
+ * @var array
52
+ */
53
+ private $ uri_passthroughs = ["app " ];
54
+
55
+ /**
56
+ * URI parts from $_SERVER['REQUEST_URI']
57
+ *
58
+ * [0=>subnets, 1=>7, 2=>detals]
59
+ *
60
+ * @var array
61
+ */
62
+ private $ uri_parts = [];
63
+
64
+ /**
65
+ * Final GET params to be returned
66
+ *
67
+ * @var array
68
+ */
69
+ private $ get_params = [];
70
+
71
+
72
+
73
+
74
+ /**
75
+ * Constructior
76
+ *
77
+ * @method __construct
78
+ */
79
+ public function __construct () {
80
+ // process request URI
81
+ $ this ->process_request_uri ();
82
+ // formulate GET request
83
+ $ this ->create_get_params ();
84
+ }
85
+
86
+ /**
87
+ * Set API flag
88
+ *
89
+ * @method set_api_flag
90
+ *
91
+ * @return void
92
+ */
93
+ private function set_api_flag () {
94
+ if ($ this ->uri_parts [0 ]=="api " ) {
95
+ $ this ->is_api = true ;
96
+ }
97
+ }
98
+
99
+ /**
100
+ * [get_url_params description]
101
+ *
102
+ * @method get_url_params
103
+ *
104
+ * @return array
105
+ */
106
+ public function get_url_params () {
107
+ return $ this ->get_params ;
108
+ }
109
+
110
+ /**
111
+ * Checks if API is requested
112
+ *
113
+ * @method is_api
114
+ *
115
+ * @return bool
116
+ */
117
+ public function is_api () {
118
+ return $ this ->is_api ;
119
+ }
120
+
121
+ /**
122
+ * Process request URI
123
+ *
124
+ * Remove url and base and save raw request to array
125
+ *
126
+ * @method process_request_uri
127
+ *
128
+ * @return void
129
+ */
130
+ private function process_request_uri () {
131
+ // ignore for direct access
132
+ if (strpos ($ _SERVER ['REQUEST_URI ' ], "index.php " )===false ) {
133
+ if (BASE !="/ " ) {
134
+ $ this ->uri_parts = array_values (array_filter (explode ("/ " , str_replace (BASE , "" , $ _SERVER ['REQUEST_URI ' ]))));
135
+ }
136
+ else {
137
+ $ this ->uri_parts = array_values (array_filter (explode ("/ " , $ _SERVER ['REQUEST_URI ' ])));
138
+ }
139
+ // set api flag
140
+ $ this ->set_api_flag ();
141
+ }
142
+ // no rewrites - rewurn default
143
+ else {
144
+ $ this ->get_params = $ _GET ;
145
+ }
146
+ }
147
+
148
+ /**
149
+ * Create get parameters based on api or non-api
150
+ *
151
+ * @method create_get_params
152
+ *
153
+ * @return void
154
+ */
155
+ private function create_get_params () {
156
+ $ this ->is_api ? $ this ->create_get_params_api () : $ this ->create_get_params_ui ();
157
+ }
158
+
159
+ /**
160
+ * Create GET parameters for UI
161
+ *
162
+ * @method create_get_params_ui
163
+ *
164
+ * @return void
165
+ */
166
+ private function create_get_params_ui () {
167
+ // process uti parts
168
+ if (sizeof ($ this ->uri_parts )>0 ) {
169
+ if (!in_array ($ this ->uri_parts [0 ], $ this ->uri_passthroughs )) {
170
+ foreach ($ this ->uri_parts as $ k =>$ l ) {
171
+ switch ($ k ) {
172
+ case 0 : $ this ->get_params ['page ' ] = $ l ; break ;
173
+ case 1 : $ this ->get_params ['section ' ] = $ l ; break ;
174
+ case 2 : $ this ->get_params ['subnetId ' ] = $ l ; break ;
175
+ case 3 : $ this ->get_params ['sPage ' ] = $ l ; break ;
176
+ case 4 : $ this ->get_params ['ipaddrid ' ] = $ l ; break ;
177
+ case 5 : $ this ->get_params ['tab ' ] = $ l ; break ;
178
+ default : $ this ->get_params [$ k ] = $ l ; break ;
179
+ }
180
+ }
181
+ }
182
+ }
183
+ elseif (sizeof ($ this ->get_params )==0 ) {
184
+ $ this ->get_params ['page ' ] = "dashboard " ;
185
+ }
186
+ // apply fixes
187
+ $ this ->fix_ui_params ();
188
+ }
189
+
190
+ /**
191
+ * Fix UI parameters - exceptions
192
+ *
193
+ * @method fix_ui_params
194
+ *
195
+ * @return void
196
+ */
197
+ private function fix_ui_params () {
198
+ if (isset ($ this ->get_params ['page ' ])) {
199
+ // dashboard fix for index
200
+ if ($ this ->get_params ['page ' ]=="login " || $ this ->get_params ['page ' ]=="logout " ) {
201
+ if (isset ($ this ->get_params ['section ' ])) {
202
+ if ($ this ->get_params ['section ' ]=="dashboard " ) {
203
+ $ this ->get_params ['page ' ] = "dashboard " ;
204
+ unset($ this ->get_params ['section ' ]);
205
+ }
206
+ }
207
+ }
208
+ // search fix
209
+ elseif ($ this ->get_params ['page ' ]=="tools " ) {
210
+ if (isset ($ this ->get_params ['section ' ]) && isset ($ this ->get_params ['subnetId ' ])) {
211
+ if ($ this ->get_params ['section ' ]=="search " ) {
212
+ $ this ->get_params ['ip ' ] = $ this ->get_params ['subnetId ' ];
213
+ $ this ->get_params ['ip ' ] = $ this ->get_params ['ip ' ];
214
+ unset($ this ->get_params ['subnetId ' ]);
215
+ }
216
+ }
217
+ }
218
+ }
219
+ }
220
+
221
+ /**
222
+ * Create GET parameters for API
223
+ *
224
+ * @method create_get_params_api
225
+ *
226
+ * @return void
227
+ */
228
+ private function create_get_params_api () {
229
+ // if requested from /api/ remove it and reindex array_values
230
+ $ this ->remove_api_from_uri_params ();
231
+ // create
232
+ if (sizeof ($ this ->uri_parts )>0 ) {
233
+ foreach ($ this ->uri_parts as $ k =>$ l ) {
234
+ switch ($ k ) {
235
+ case 0 : $ this ->get_params ['app_id ' ] = $ l ; break ;
236
+ case 1 : $ this ->get_params ['controller ' ] = $ l ; break ;
237
+ case 2 : $ this ->get_params ['id ' ] = $ l ; break ;
238
+ case 3 : $ this ->get_params ['id2 ' ] = $ l ; break ;
239
+ case 4 : $ this ->get_params ['id3 ' ] = $ l ; break ;
240
+ case 5 : $ this ->get_params ['id4 ' ] = $ l ; break ;
241
+ default : $ this ->get_params [$ k ] = $ l ; break ;
242
+ }
243
+ }
244
+ }
245
+ }
246
+
247
+ /**
248
+ * Remove api from uri parameters and reindex request array
249
+ *
250
+ * @method remove_api_from_uri_params
251
+ *
252
+ * @return [type]
253
+ */
254
+ private function remove_api_from_uri_params () {
255
+ if ($ this ->uri_parts [0 ]=="api " ) {
256
+ unset($ this ->uri_parts [0 ]);
257
+ $ this ->uri_parts = array_values ($ this ->uri_parts );
258
+ }
259
+ }
260
+ }
0 commit comments