I thought it would be useful to have a quick "generate random password" button on a few admin pages. I couldn't find editable template files for the admin pages (other than a few of the basic .tpl files), so I decided to implement it with Javascript. Basically, the JS looks for password input boxes, and then adds a "Generate" button after them.
This should be considered experimental as I just wrote it tonight, but it seems to be working very well in my tests. Here's what it looks like:
Here's what you do:
Create a new file, /whmcs_path/admin/autoPassword.js
Content:
// AddAutoPasswordButtons: Look for password boxes and add a generate password button next to tehm
function AddAutoPasswordButtons() {
// Don't add the Generate button to certain pages where it wouldn't make sense
var NoGenerate = [
'WHMCS - Servers',
'WHMCS - Support Ticket Departments'
];
for (i=0; i<NoGenerate.length; i++) if (document.title == NoGenerate[i]) return;
// Get the Password text box, if there is one
var e = document.getElementsByName("password");
if (e.length>0)
{
var b = document.createElement("input");
b.type = "button";
b.value = "Generate";
b.onclick = GeneratePassword;
e[0].parentNode.appendChild(b);
}
}
// GeneratePassword: Generate a password and place it in a textbox
function GeneratePassword() {
var p = GetRandomPassword();
var e = document.getElementsByName("password");
if (e.length>0) {
e[0].value = p;
if (e[0].type == "password") e[0].type = "text";
}
// For a "confirm password" field
e = document.getElementsByName("password2");
if (e.length>0) {
e[0].value = p;
if (e[0].type == "password") e[0].type = "text";
}
}
// GetRandomNumber: Get a random number between two specified numbers
function GetRandomNumber(lbound,ubound) {
return (Math.floor(Math.random() * (ubound-lbound)) + lbound);
}
// GetRandomCharacter: Get a random character
function GetRandomCharacter() {
var chars = ""; // Choose your character sets below
chars += "0123456789";
chars += "abcdefghijklmnopqrstuvwxyz";
chars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
return chars.charAt(GetRandomNumber(0,chars.length));
}
// GetRandomPassword: Get a random password
function GetRandomPassword() {
var p = "";
for (i=0; i<8; i++) p += GetRandomCharacter();
return p;
}
//------------------------------------------------------------------------------
// Adding events to onLoad
// Source: http://onlinetools.org/articles/unobtrusivejavascript/chapter4.html
function addEvent(obj, evType, fn){
if (obj.addEventListener){
obj.addEventListener(evType, fn, false);
return true;
} else if (obj.attachEvent){
var r = obj.attachEvent("on"+evType, fn);
return r;
} else {
return false;
}
}
// Hook AddAutoPasswordButtons to window load
addEvent(window,'load',AddAutoPasswordButtons);
Then, we just need to edit /whmcs_path/admin/templates/v4/header.tpl to reference the new .js file.
I added the following on line 39 just before </head>:
<script type="text/javascript" src="autoPassword.js"></script>
This is what it looks like in context:
});{/literal}
{$jscode}
</script>
[b]<script type="text/javascript" src="autoPassword.js"></script>[/b]
</head>
<body>
<div id="topnav">
That's it! Let me know if you find this useful or see any problems. I'll update this thread if I make any changes.