A moan, a groan, my life story and how me, a technically minded person just can't seem to fit in with anybody.

Archive for the 'PHP' Category

Remove/edit WordPress admin footer links and version number

function change_footer_admin () {return ' ';}
add_filter('admin_footer_text', 'change_footer_admin', 9999);
function change_footer_version() {return ' ';}
add_filter( 'update_footer', 'change_footer_version', 9999);

Place the above code in your themes function file. The above code allows you to edit the admin footer version number and text links. Just alter the html in the return of each function. I recommend removing the version number for security reasons. There are a lot of tutorials out there that tell you how to remove the generator tag from your WordPress header to hide the version number put they forget if a user registers they can see the version number in the admin footer page. If you build WordPress installations for your clients this footer area is good for displaying your own support links etc.

WordPress – Show images attached to post or page in order

I’m currently building a website where a custom post type needs to show a gallery of images per post. Below image is what I was after:

To do this I would upload the images when creating the post so that the images would be attached to the post, or in other words the attachments where child posts of this post I was creating. I found some code that grabs the attachments but the code didn’t retrieve the results by the order I have specified in the upload manager when uploading the image. To get the images in order I had to write a little more code to what I had found.

Anyway here is the finished code It’s tailored to my needs but here you can see how the image attachments are being pulled and then how im sorting the results to go in the correct order. Hope it is some use to someone.

PHP Send Email With High Importance Set

Just posting this for reference really. You need to add some extra headers to your PHP send mail script to send the email with high importance. This will show a red exclamation mark on the recipitents recieved mail if their mail client supports this feature.

PHP – Get users SID from Active Directory via LDAP (objectsid)

Have you ever needed to read an AD users SID so you can use it as the unique identifier it is?

Well I did but when I read out objectsid from AD for a user I found a load of weird symbols. It turned out that the data outputted was binary data. So after some digging around to see how to translate the binary data to a human readable SID like S-1-5-21-823795046-756116320-56781596-16683 I got to work to write a PHP script that could do just that.

This is what I came up with:

[PHP]$suffix =””;
$base_dn = “dc=sub,dc=mydomain,dc=co,dc=uk”;
$server = “″;

$USERNAME = “username”;
$PASSWORD = “password”;

$USERNAMETOSEARCH = “user to get sid”;

$ds = ldap_connect($server);
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
$r = ldap_bind($ds, $USERNAME.$suffix, $PASSWORD);
$sr = ldap_search($ds, $base_dn, “(samaccountname=”.$USERNAMETOSEARCH.”)”);
$entries = ldap_get_entries($ds, $sr);

// All SID’s begin with S-
$sid = “S-”;
// Convert Bin to Hex and split into byte chunks
$sidinhex = str_split(bin2hex($entries[0]['objectsid'][0]), 2);
// Byte 0 = Revision Level
$sid = $sid.hexdec($sidinhex[0]).”-”;
// Byte 1-7 = 48 Bit Authority
$sid = $sid.hexdec($sidinhex[6].$sidinhex[5].$sidinhex[4].$sidinhex[3].$sidinhex[2].$sidinhex[1]);
// Byte 8 count of sub authorities – Get number of sub-authorities
$subauths = hexdec($sidinhex[7]);
//Loop through Sub Authorities
for($i = 0; $i < $subauths; $i++) { $start = 8 + (4 * $i); // X amount of 32Bit (4 Byte) Sub Authorities $sid = $sid.”-”.hexdec($sidinhex[$start+3].$sidinhex[$start+2].$sidinhex[$start+1].$sidinhex[$start]); } echo $sid;[/PHP] Now this is written in PHP but I’m sure this code can be pretty much be translated to any other language.

PHP – Active Directory – Reading UserAccountControl

At the moment I’m doing a lot of work with Microsoft Active Directory and PHP. I’m building a few tools in PHP which reads data out of AD.

Now one of the things I wanted to read out was to see if the account was locked or if its password never expired. I couldn’t find these entries in AD but after searching those entries are stored in “useraccountcontrol”. But when I looked it was just a number. How does that number tell you if the account is locked?

On the above link is how that number is generated and once I understood that I got to work on writing a PHP script which translates that number into something more usable. Once I wrote the script I put it up on the Hot Scripts Forum to see if the code I wrote could be improved on and optimised. Here is what they came back with:

$userAccountArray = array(
‘NOT_USED_8000′ => 0,
‘NOT_USED_4000′ => 0,
‘NOT_USED_4′ => 0,

function ADUserAccountControl($val) {
global $userAccountArray;
$x = pow(2, count($userAccountArray) – 1);
foreach($userAccountArray as $k => $v) {
if(($val – $x) >= 0){
$userAccountArray[$k] = 1;
$val -= $x;
} else {
$userAccountArray[$k] = 0;
$x = $x / 2;

Simply pass your useraccountcontrol value to the function and read the results out of the array.

Getting Windows HDD usage with PHP and SNMP

At work a couple of months back I was faced with a problem that many of our servers would run out of hard drive space without us knowing and that would cause the server to hang and/or stop responding. So I did a bit of research and came across. This little utilility would run on the server in command line and tell you what drives were available and some info of the drives like space used and available. Well with this I would echo out the data to a text file and send that across to a common holding ground. Then from there get my web server to constantly scan this folder for new logs. This method worked but it had its issues.

Well yesterday at work I was playing around with SNMP and after finding a site with W2K MIBS I found lots of info you can pull from the server. Now I was already familiar with SNMP in PHP thanks to their documentation on their site. So using their snmpwalk function I was able to pull the relevant data to get HDD usage info.

$names = snmpwalk($row['ip'], “public”, ‘.′);
$used = snmpwalk($row['ip'], “public”, ‘.′);
$total = snmpwalk($row['ip'], “public”, ‘.′);
$alloc = snmpwalk($row['ip'], “public”, ‘.′);

Now that would give me an array of drive names, space used on those drives, total space available on those drives and the allocation units. Now the allocation units I didn’t realize I needed as I thought the numbers given in used and total were just measured in bytes but they aren’t. The numbers in used and total are how many blocks of allocation size (if that makes sense). So to get the true byte value from used and total you need to multiply them by the allocation size number.

25599569 * 4096 = 104855834624B = 97.65GB

So there you have it. Now you know how to get HDD usage from your server using PHP and SNMP.

PHP File Upload Progress Bar Part 2

After further investigation PHP 5.2.0 now allows you to use the APC (Alternative PHP Cache) addition with the ability to track the progress of uploads. Within windows it is as simply adding the dll extension to your php.ini and putting some configs there to. With Linux I’m not too sure how you add this extension as I don’t know Linux well. But anyway below is some nice and easy documentation on this.

PHP File Upload Progress Bar

PHP File Upload Progress Bar

As you may be aware in php it doesn’t have the functionality to report back the status of file uploads. For example upload percent complete, speed of upload and estimated time of completion. To be able to provide a progress bar the easiest way would be to use a perl script to do the uploading for you as perl has this functionality. Now I build a file uploader for the public and I looked at providing a perl uploader but because I know very little about the language of perl I couldn’t get any examples to work for me, but the ones that did work needed some server side changes. These server side changes would be no good for most of my downloader’s as many of them don’t have root access to their server.

There is another solution out there but once again it involves making server side changes which hacks php and adds the additional functions to track the upload progress of a file.

The above hack for PHP can be found at There isn’t much documentation out there for it but if you do a Google search you will find one or two examples.

The above hack I’m thinking about putting it in my module so that those that have the modified php will be able to run my uploader with the progress bar but those without would not get the upload progress bar. The thing in to build my module to use this hack I need a server that is running the hacked version of php. Thing is I’m stuck with a shared hosting account and I don’t have access to modify php. So if there is anyone out there that can lend me some space for a short time and is running the above hack please contact me so I can’t continue building my file uploader script