[i/177] Reduce SN load

Old commit messages:

[i/177] optimising sql queries

[i/177] moving $sn_u_permissions out of loop, sanatising $username