*/
/**
* Manages SSH public keys.
*
* @package modules
*/
class ldapPublicKey extends baseModule {
/** session variable for existing keys in self service */
const SESS_KEY_LIST = 'ldapPublicKey_keyList';
/**
* Returns true if this module can manage accounts of the current type, otherwise false.
*
* @return boolean true if module fits
*/
public function can_manage() {
return in_array($this->get_scope(), array('user'));
}
/**
* Returns meta data that is interpreted by parent class
*
* @return array array with meta data
*
* @see baseModule::get_metaData()
*/
function get_metaData() {
$return = array();
// icon
$return['icon'] = 'keyBig.png';
// alias name
$return["alias"] = _("SSH public key");
// module dependencies
$return['dependencies'] = array('depends' => array(), 'conflicts' => array());
// managed object classes
$return['objectClasses'] = array('ldapPublicKey');
// managed attributes
$return['attributes'] = array('sshPublicKey');
// help Entries
$return['help'] = array(
'sshPublicKey' => array(
"Headline" => _("SSH public key"), 'attr' => 'sshPublicKey',
"Text" => _("Please enter your public SSH key.")
),
'keyList' => array(
"Headline" => _("SSH public key"), 'attr' => 'sshPublicKey',
"Text" => _("Please a comma separated list of your public SSH keys.")
),
'upload' => array(
"Headline" => _("File upload"), 'attr' => 'sshPublicKey',
"Text" => _("Upload a file with one or more keys. Each line contains one key.")
),
);
// upload fields
$return['upload_columns'] = array(
array(
'name' => 'ldapPublicKey_sshPublicKey',
'description' => _('SSH public key'),
'help' => 'keyList',
'example' => _('ssh-dss 234234 user@host')
)
);
// available PDF fields
$return['PDF_fields'] = array(
'sshPublicKey' => _('SSH public keys')
);
// self service field settings
$return['selfServiceFieldSettings'] = array(
'sshPublicKey' => _('SSH public keys'),
);
$return['selfServiceReadOnlyFields'] = array('sshPublicKey');
return $return;
}
/**
* This function fills the message array.
**/
function load_Messages() {
$this->messages['file'][0] = array('ERROR', _('No file selected.'));
}
/**
* Returns the HTML meta data for the main account page.
*
* @return htmlElement HTML meta data
*/
function display_html_attributes() {
$return = new htmlTable();
$this->addMultiValueInputTextField($return, 'sshPublicKey', _('SSH public key'), false, '16384', false, null, '50');
// file upload
$return->addElement(new htmlSpacer(null, '20px'), true);
$return->addElement(new htmlOutputText(_('Upload file')));
$uploadGroup = new htmlGroup();
$uploadGroup->addElement(new htmlInputFileUpload('sshPublicKeyFile'));
$uploadGroup->addElement(new htmlSpacer('1px', null));
$uploadGroup->addElement(new htmlButton('sshPublicKeyFileSubmit', _('Upload')));
$uploadGroup->addElement(new htmlSpacer('5px', null));
$uploadGroup->addElement(new htmlHelpLink('upload'));
$return->addElement($uploadGroup);
return $return;
}
/**
* Processes user input of the primary module page.
* It checks if all input values are correct and updates the associated LDAP attributes.
*
* @return array list of info/error messages
*/
function process_attributes() {
$messages = array();
$this->processMultiValueInputTextField('sshPublicKey', $messages);
// file upload
if (isset($_POST['sshPublicKeyFileSubmit'])) {
if ($_FILES['sshPublicKeyFile'] && ($_FILES['sshPublicKeyFile']['size'] > 0)) {
$handle = fopen($_FILES['sshPublicKeyFile']['tmp_name'], "r");
$data = fread($handle, 10000000);
fclose($handle);
$data = str_replace("\r\n", "\n", $data);
$data = str_replace("\r", "\n", $data);
$lines = explode("\n", $data);
foreach ($lines as $line) {
if (!empty($line) && !(strpos($line, '#') === 0)) {
$this->attributes['sshPublicKey'][] = $line;
}
}
}
else {
$messages[] = $this->messages['file'][0];
}
}
$this->attributes['sshPublicKey'] = array_values(array_unique($this->attributes['sshPublicKey']));
return $messages;
}
/**
* In this function the LDAP account is built up.
*
* @param array $rawAccounts list of hash arrays (name => value) from user input
* @param array $ids list of IDs for column position (e.g. "posixAccount_uid" => 5)
* @param array $partialAccounts list of hash arrays (name => value) which are later added to LDAP
* @param array $selectedModules list of selected account modules
* @return array list of error messages if any
*/
function build_uploadAccounts($rawAccounts, $ids, &$partialAccounts, $selectedModules) {
$messages = array();
for ($i = 0; $i < sizeof($rawAccounts); $i++) {
// add object class
if (!in_array("ldapPublicKey", $partialAccounts[$i]['objectClass'])) $partialAccounts[$i]['objectClass'][] = "ldapPublicKey";
// add keys
if ($rawAccounts[$i][$ids['ldapPublicKey_sshPublicKey']] != "") {
$keys = explode(',', $rawAccounts[$i][$ids['ldapPublicKey_sshPublicKey']]);
// check format
for ($m = 0; $m < sizeof($keys); $m++) {
$partialAccounts[$i]['sshPublicKey'][] = $keys[$m];
}
}
}
return $messages;
}
/**
* Returns a list of PDF entries
*/
function get_pdfEntries() {
$return = array();
if (sizeof($this->attributes['sshPublicKey']) > 0) {
$return['ldapPublicKey_sshPublicKey'][0] = '' . _('SSH public keys') . '