[i/177] Reduce SN load

Old commit messages:

[i/177] optimising sql queries

[i/177] moving $sn_u_permissions out of loop, sanatising $username
Merge pull request #177 from Senky/i/177

Reduce SN load