Can I change the Status/DHCP Leases sort order in the php file?
-
Coming from a previous relese of pfsense, when I would go into Status/Dhcp Leases in the web gui I would be presented by a page of ip addresses starting from low going to high. Since my dhcp range is low in the subnet it would show me all the dhcp entries then my static entries. This was great beause it was a starting point to start assigning static ip addresses. Loved it.
Fast forward to 2.7.2 the same menu options gives similar results with a few new twists (up and down arrows to show connection status... But the addresses are appearing high to low which now forces me everytime to have to click on the IP Address label to re-sort from low to high. I started looking into the status_dhcp_leases.php file to see if I could introduce a "sort" or "usort" to change the sort order. I'm no php programmer and just basically find my way around and with google come up with the code to do what I want. This one has me stumped. It's some pretty serious stuff. I even pulled the previous version that sorted the way I want and no luck. The newer version is a lot different. Here is the php file. If anyone is proficient at php, maybe they can help me switch the sort order. I've got a nice little test system so i can try out my changes without effecting production.
Thanks for the help. Would really love to tackle this one. Otherwise it's click click click every time I show that page.
I can't seem to post the php file here. It keeps getting flagged as spam.
-
You can - one ( !) click on :
and then the list is sorted on "IP Addresses".
Btw : auto sort on IP address out of the box, I remember that was already asked here on the forum ...
edit :
Its open source, right ;)
So it's already on the Internet.
You talked about the file, but didn't mention its name, it's this one : ( ? )
Github:pfsense/src/usr/local/www/status_dhcp_leases.phpedit again :
Open the file.
Locate ( line 134 ?):
if ($_REQUEST['order']) { usort($leases['lease'], function($a, $b) { return strcmp($a[$_REQUEST['order']], $b[$_REQUEST['order']]); }); }
Before these lines, add this :
usort($leases['lease'], function($a, $b) { return strcmp($a['ip'], $b['ip']); });
The sort now somewhat works. You'll see ^^
I give it some more thoughts tomorrow. -
My bad for not listing the file name. I was attempting to "attach code" where I would have had the file in the post, but the forum wasn't allowing me to post that message so i just eliminated the code. Yes, it was status_dhcp_leases.php. I was aware of the click on "ip address" to sort but I hate doing that every time I go into the page so I was looking for a way to have it by default. I had been poking around the usort area but wasn't seeing any sort of change. I'll look at your code tonight in my test environment and see how close it gets me. I appreciate you taking the time to look at this. It's such small change from a functionality perspective, but something that I'd really like to accomplish. Previous versions of pfsense displayed with dhcp assigned addressed first (low to high), then static (low to high) after that. For me, that's the best way to look at my addressing since it shows me the dhcp assigned addresses first and I can decide which ones I'd like to assign static.
Thanks again. Oh, there was also a bugtracker entry for this very thing but not something that anyone seemed too interested in fixing. Not really broken after all.
Roveer
-
@Gertjan said in Can I change the Status/DHCP Leases sort order in the php file?:
You can - one ( !) click on :
and then the list is sorted on "IP Addresses".
Btw : auto sort on IP address out of the box, I remember that was already asked here on the forum ...
edit :
Its open source, right ;)
So it's already on the Internet.
You talked about the file, but didn't mention its name, it's this one : ( ? )
Github:pfsense/src/usr/local/www/status_dhcp_leases.phpedit again :
Open the file.
Locate ( line 134 ?):
if ($_REQUEST['order']) { usort($leases['lease'], function($a, $b) { return strcmp($a[$_REQUEST['order']], $b[$_REQUEST['order']]); }); }
Before these lines, add this :
usort($leases['lease'], function($a, $b) { return strcmp($a['ip'], $b['ip']); });
The sort now somewhat works. You'll see ^^
I give it some more thoughts tomorrow.So I put in the code. It did some of what I'm looking to do. The result I got was the reserved leases started first (low to high) but at ip range starting at 100 and going up. It then threw in the dhcp assigned leases below that and finally the .47 to .99 leases after that. (my dhcp pool is .2 to .46) Kind of weird. I'm in the weeds on this one.
-
Unfortunately you can't string compare IP addresses as it doesn't take into account trailing nul, which is why 13 will sort with 130.
But, you can convert an IP to an integer and then compare.
usort($leases['lease'], function($a, $b) { return (ip2long($a['ip']) <=> ip2long($b['ip'])); });
This works as expected, and all rows will be sorted by IP address. However, any dynamic leases will be sorted together with static. In the original presentation, static were listed first in reverse order and then dynamic last.
If you want to preserve this separation, it's pretty easy, because the original array is already sorted - just in descending order. So we just need to turn that around and reverse the original array.
Us this instead of the code snippet above:
$leases['lease']=array_reverse($leases['lease']);
IMO, this is the better solution for a default view, as it lets you quickly see dynamic entries without having to figure out or remember where they sit in relation to your static range(s).
-
Thanks !!
-
Here's a complete list of changes I've made to the /usr/local/www/status_dhcp_leases.php file
Changes to /usr/local/www/status_dhcp_leases.php
Display lease array in ascending order - Reverse the array
Put this:$leases['lease']=array_reverse($leases['lease']);
Before this:
if ($_REQUEST['order']) {
Convert MAC to UPPERCASE
Replace this:$mac = $data['mac'];
With this:
$mac = strtoupper($data['mac']);
Add line-break between MAC and Manufacturer string
Replace this:<?php if (isset($mac_man[$mac_hi])):?>
With this:
<?php if (isset($mac_man[$mac_hi])):?></br>
Remove “n/a” text from Start and End columns
Replace two instances of this:<td><?=gettext("n/a")?></td>
With this:
<td><?=gettext(" ")?></td>
Remove “offline” down arrow icon from first column
Replace this:<i class="fa fa-arrow-down online" title="<?=htmlspecialchars($data['online'])?>"></i>
With this:
<i class="fa"></i>
And to match the UPPERCASE MAC addresses in the DHCP Service Static table...
Changes to /usr/local/www/services_dhcp.php
Convert MAC addresses to UPPERCASE
Replace this:<?=htmlspecialchars($mapent['mac'])?>
With this:
<?=strtoupper(htmlspecialchars($mapent['mac']))?>