Skip to content

Commit 8bf6032

Browse files
committed
Rewritten rewrite module, /api/ in nginx location not needed anymore
1 parent 26d45c6 commit 8bf6032

File tree

9 files changed

+618
-472
lines changed

9 files changed

+618
-472
lines changed

.htaccess

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Enable rewrite engine
44
RewriteEngine on
55
# Passthroughs
6-
RewriteRule ^(css|js|app|test|api)/.*$ - [L,QSA]
6+
RewriteRule ^(css|js|app|test)/.*$ - [L,QSA]
77

88
# Rewrites
99
RewriteRule ^index\.php$ - [L]

api/.htaccess

-19
Original file line numberDiff line numberDiff line change
@@ -1,19 +0,0 @@
1-
RewriteEngine On
2-
Options +FollowSymLinks
3-
4-
# redirect errors
5-
ErrorDocument 400 '{"code":409,"success":0,"message":"Bad Request"}'
6-
ErrorDocument 401 '{"code":401,"success":0,"message":"Unauthorized"}'
7-
ErrorDocument 403 '{"code":403,"success":0,"message":"Forbidden"}'
8-
ErrorDocument 404 '{"code":404,"success":0,"message":"Invalid URL"}'
9-
ErrorDocument 405 '{"code":404,"success":0,"message":"Method Not Allowed"}'
10-
ErrorDocument 500 '{"code":500,"success":0,"message":"Internal server error"}'
11-
ErrorDocument 501 '{"code":501,"success":0,"message":"Not Implemented"}'
12-
ErrorDocument 503 '{"code":503,"success":0,"message":"Service Unavailable"}'
13-
ErrorDocument 505 '{"code":505,"success":0,"message":"HTTP Version Not Supported"}'
14-
15-
# Rewrites
16-
RewriteRule ^index\.php$ - [L]
17-
RewriteCond %{REQUEST_FILENAME} !-d
18-
RewriteCond %{REQUEST_FILENAME} !-f
19-
RewriteRule ^ index.php [L]

api/index.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
*
1818
*/
1919

20-
# include funtions
20+
# include functions
21+
if(!function_exists("create_link"))
2122
require( dirname(__FILE__) . '/../functions/functions.php'); // functions and objects from phpipam
23+
24+
# include common API controllers
2225
require( dirname(__FILE__) . '/controllers/Common.php'); // common methods
2326
require( dirname(__FILE__) . '/controllers/Responses.php'); // exception, header and response handling
2427

app/tools/search/index.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
$User->check_user_session();
77

88
# get posted search term
9-
if(isset($_REQUEST['ip'])) {
9+
if(isset($_GET['ip'])) {
1010
// remove chars
11-
$searchTerm = $Subnets->strip_input_tags(urldecode(trim($_REQUEST['ip'])));
11+
$searchTerm = $Subnets->strip_input_tags(urldecode(trim($_GET['ip'])));
1212
}
1313
else {
1414
$searchTerm = "";
@@ -20,7 +20,7 @@
2020
if($params) {
2121
foreach ($params as $k=>$p) {
2222
if ($p=="on") {
23-
$_REQUEST[$k] = $p;
23+
$_GET[$k] = $p;
2424
}
2525
}
2626
}
@@ -42,17 +42,17 @@
4242
</div>
4343

4444
<div style="margin:5px;">
45-
<input type="checkbox" name="subnets" value="on" <?php if($_REQUEST['subnets']=="on") { print "checked='checked'"; } ?>> <?php print _('Subnets'); ?>
46-
<input type="checkbox" name="addresses" value="on" <?php if($_REQUEST['addresses']=="on") { print "checked='checked'"; } ?>> <?php print _('IP addresses'); ?>
47-
<input type="checkbox" name="vlans" value="on" <?php if($_REQUEST['vlans']=="on") { print "checked='checked'"; } ?>> <?php print _('VLANs'); ?>
45+
<input type="checkbox" name="subnets" value="on" <?php if($_GET['subnets']=="on") { print "checked='checked'"; } ?>> <?php print _('Subnets'); ?>
46+
<input type="checkbox" name="addresses" value="on" <?php if($_GET['addresses']=="on") { print "checked='checked'"; } ?>> <?php print _('IP addresses'); ?>
47+
<input type="checkbox" name="vlans" value="on" <?php if($_GET['vlans']=="on") { print "checked='checked'"; } ?>> <?php print _('VLANs'); ?>
4848
<?php if($User->settings->enableVRF==1) { ?>
49-
<input type="checkbox" name="vrf" value="on" <?php if($_REQUEST['vrf']=="on") { print "checked='checked'"; } ?>> <?php print _('VRFs'); ?>
49+
<input type="checkbox" name="vrf" value="on" <?php if($_GET['vrf']=="on") { print "checked='checked'"; } ?>> <?php print _('VRFs'); ?>
5050
<?php } ?>
5151
<?php if($User->settings->enablePSTN==1) { ?>
52-
<input type="checkbox" name="pstn" value="on" <?php if($_REQUEST['pstn']=="on") { print "checked='checked'"; } ?>> <?php print _('PSTN'); ?>
52+
<input type="checkbox" name="pstn" value="on" <?php if($_GET['pstn']=="on") { print "checked='checked'"; } ?>> <?php print _('PSTN'); ?>
5353
<?php } ?>
5454
<?php if($User->settings->enableCircuits==1) { ?>
55-
<input type="checkbox" name="circuits" value="on" <?php if($_REQUEST['circuits']=="on") { print "checked='checked'"; } ?>> <?php print _('Circuits'); ?>
55+
<input type="checkbox" name="circuits" value="on" <?php if($_GET['circuits']=="on") { print "checked='checked'"; } ?>> <?php print _('Circuits'); ?>
5656
<?php } ?>
5757
</div>
5858
</form>

app/tools/search/search-results.php

+9-10
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030

3131

3232
// all are off?
33-
if(!isset($_REQUEST['addresses']) && !isset($_REQUEST['subnets']) && !isset($_REQUEST['vlans']) && !isset($_REQUEST['vrf']) && !isset($_REQUEST['pstn']) && !isset($_REQUEST['circuits']) ) {
33+
if(!isset($_GET['addresses']) && !isset($_GET['subnets']) && !isset($_GET['vlans']) && !isset($_GET['vrf']) && !isset($_GET['pstn']) && !isset($_GET['circuits']) ) {
3434
include("search-tips.php");
3535
}
3636
// empty request
37-
elseif (strlen($_REQUEST['ip'])==0) {
37+
elseif (strlen($_GET['ip'])==0) {
3838
include("search-tips.php");
3939
}
4040
// ok, search results print
@@ -50,20 +50,19 @@
5050
#
5151

5252
// subnets
53-
if(@$_REQUEST['subnets']=="on" && strlen($_REQUEST['ip'])>0 ) { include(dirname(__FILE__).'/search_results/search-results_subnets.php'); }
53+
if(@$_GET['subnets']=="on" && strlen($_GET['ip'])>0 ) { include(dirname(__FILE__).'/search_results/search-results_subnets.php'); }
5454
// addresses
55-
if(@$_REQUEST['addresses']=="on" && strlen($_REQUEST['ip'])>0) { include(dirname(__FILE__).'/search_results/search-results_addresses.php'); }
55+
if(@$_GET['addresses']=="on" && strlen($_GET['ip'])>0) { include(dirname(__FILE__).'/search_results/search-results_addresses.php'); }
5656
// vlan
57-
if(@$_REQUEST['vlans']=="on" && strlen($_REQUEST['ip'])>0) { include(dirname(__FILE__).'/search_results/search-results_vlans.php'); }
57+
if(@$_GET['vlans']=="on" && strlen($_GET['ip'])>0) { include(dirname(__FILE__).'/search_results/search-results_vlans.php'); }
5858
// vrf
59-
if(@$_REQUEST['vrf']=="on" && strlen($_REQUEST['ip'])>0) { include(dirname(__FILE__).'/search_results/search-results_vrfs.php'); }
59+
if(@$_GET['vrf']=="on" && strlen($_GET['ip'])>0) { include(dirname(__FILE__).'/search_results/search-results_vrfs.php'); }
6060
// pstn
61-
if(@$_REQUEST['pstn']=="on" && strlen($_REQUEST['ip'])>0) { include(dirname(__FILE__).'/search_results/search-results_pstn.php'); }
61+
if(@$_GET['pstn']=="on" && strlen($_GET['ip'])>0) { include(dirname(__FILE__).'/search_results/search-results_pstn.php'); }
6262
// circuits
63-
if(@$_REQUEST['circuits']=="on" && strlen($_REQUEST['ip'])>0) { include(dirname(__FILE__).'/search_results/search-results_circuits.php'); }
63+
if(@$_GET['circuits']=="on" && strlen($_GET['ip'])>0) { include(dirname(__FILE__).'/search_results/search-results_circuits.php'); }
6464

6565

6666
// export holder
6767
print '<div class="exportDIVSearch"></div>';
68-
}
69-
?>
68+
}

functions/classes/class.Rewrite.php

+260
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
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&section=$2&subnetId=$3&sPage=$4&ipaddrid=$5&tab=$6 [L]
9+
* RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/$ index.php?page=$1&section=$2&subnetId=$3&sPage=$4&ipaddrid=$5 [L,QSA]
10+
* RewriteRule ^(.*)/(.*)/(.*)/(.*)/$ index.php?page=$1&section=$2&subnetId=$3&sPage=$4 [L,QSA]
11+
* RewriteRule ^(.*)/(.*)/(.*)/$ index.php?page=$1&section=$2&subnetId=$3 [L,QSA]
12+
* RewriteRule ^(.*)/(.*)/$ index.php?page=$1&section=$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&section=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

Comments
 (0)