$p) { if ($u != "" && $p != "") { fputs($f, $u .":". $p ."\n"); } } fclose($f); } /* generate the htgroup content * * for each role configured (htpasswdsync_roles array), list users * and update the htgroup accordingly * * @param * @return */ function _htpasswdsync_updategroup() { firep("called _htpasswdsync_updategroup"); $file = _htpasswdsync_grpfilename(); $groups = array(); // if we overwrite, then why botter reading the previous file if (! _htpasswdsync_overwrite()) { _htpasswdsync_read_htfile($groups, $file); } foreach (_htpasswdsync_roles() as $rid) { // get role name $res = db_fetch_object(db_query('SELECT name FROM {role} WHERE rid = %d', $rid)); $name = $res->name; firep($rid, "rid"); firep($res->name, "$res->name"); //empty group $groups[$name] = ""; // add members to the group $res = db_query('SELECT name FROM {users} u, {users_roles} ur WHERE ur.rid = %d AND ur.uid = u.uid AND status = 1', $rid); while ($r = db_fetch_object($res)) { $groups[$name] .= " ". $r->name; firep($r->name, "$r->name"); firep($groups, "$groups"); } } _htpasswdsync_write_htfile($groups, $file); firep("end _htpasswdsync_updategroup"); } /* generate the htpasswd content from the database * * update the htpasswd file from the table htpasswdsync_passwd * * @param * @return */ function _htpasswdsync_updatepasswd() { firep("called _htpasswdsync_updatepasswd"); $file = _htpasswdsync_passfilename(); $passwords = array(); // if we overwrite, then why botter reading the previous file if (! _htpasswdsync_overwrite()) { _htpasswdsync_read_htfile($passwords, $file); } //get all users $res = db_query('SELECT username, passwd FROM {htpasswdsync_passwd}, {users} WHERE name=username and status = 1'); while ($r = db_fetch_object($res)) { if ($r->passwd == "****DELETED") { unset($passwords[$r->username]); } else { $passwords[$r->username] = $r->passwd; } } _htpasswdsync_write_htfile($passwords, $file); firep("end _htpasswdsync_updatepasswd"); } /* update htpassword table with the new password of the user * * @param $edit * fields that have been edited * @param $account * account of the user to update * @return */ function _htpasswdsync_update($edit, $account) { if (isset($edit["pass"]) && $edit["pass"] <> "") { // update with the $account information received // password crypted with the standard crypt (not MD5) function $user = $account->name; $pass = _htpasswdsync_crypt($edit['pass']); $passwds[$user] = $pass; //update table db_query("DELETE FROM {htpasswdsync_passwd} WHERE username = '%s'", $user); db_query("INSERT INTO {htpasswdsync_passwd} (username, passwd) VALUES('%s', '%s')", $user, $pass); _htpasswdsync_commit_to_htpasswd(); } if (isset($edit['name']) && $edit['name'] <> $account->name) { // we are changing username $r = db_query("SELECT username,passwd from {htpasswdsync_passwd} WHERE username = '%s'", $account->name); $user = db_fetch_object($r); db_query("DELETE FROM {htpasswdsync_passwd} WHERE username = '%s'", $user->username); db_query("DELETE FROM {htpasswdsync_passwd} WHERE username = '%s'", $edit['name']); db_query("INSERT INTO {htpasswdsync_passwd} (username, passwd) VALUES('%s', '%s')", $edit['name'], $user->passwd); _htpasswdsync_commit_to_htpasswd(); } } /* update htpassword file with the new password of the user * * @param $account * account of the user to update * @return */ function _htpasswdsync_commit_to_htpasswd() { // update passwd file with new status _htpasswdsync_updatepasswd(); _htpasswdsync_updategroup(); } /* remove the one user for the htpassword file * * @param $username * username of account to delete * @return */ function _htpasswdsync_delete_user($username) { db_query("DELETE FROM {htpasswdsync_passwd} WHERE username = '%s'", $username); db_query("INSERT INTO {htpasswdsync_passwd} (username, passwd) VALUES('%s', '%s')", $username, "****DELETED"); } /* remove the user for the htpassword file * * @param $account * array of account to delete * @return */ function _htpasswdsync_delete($account) { if (isset($account['accounts'])) { foreach ($account['accounts'] as $a) { $r = db_query("SELECT name FROM {users} WHERE uid = %d", $a); $user = db_fetch_object($r); _htpasswdsync_delete_user($user->name); } } elseif (isset($account['_account'])) { _htpasswdsync_delete_user($user->name); } _htpasswdsync_commit_to_htpasswd(); } /** * Validate user form input * here we refuse username with characters that are not supported * in htpasswd files * @param $edit field submited * @return none */ function _htpasswdsync_validate($edit, $account) { if (isset($edit['name'])) { if (ereg('[:[:space:]]', $edit['name'])) { form_set_error('htpasswdsync', t('The username contains an illegal character, like <space>, :')); } } } // htpasswdsync_validate /** * Display help and module information * @param path which path of the site we're displaying help * @param arg array that holds the current path as would be returned from arg() function * @return help text for the path */ function htpasswdsync_help($path, $arg) { $output = ''; //declare your output variable switch ($path) { case "admin/help#htpasswdsync": $output = '

'. t("synchronize password with a htpasswd file") .'

'; break; } return $output; } // function htpasswdsync_help /** * Valid permissions for this module * @return array An array of valid permissions for the htpasswdsync module */ function htpasswdsync_perm() { return array('administer htpasswdsync'); } // function htpasswdsync_perm() /* * Implementation of hook_user() */ function htpasswdsync_user($op, &$edit, &$account, $category = NULL) { switch ($op) { case "delete": _htpasswdsync_delete($edit); break; case "insert": _htpasswdsync_update($edit, $account); break; case "update": _htpasswdsync_update($edit, $account); break; case "after_update": _htpasswdsync_commit_to_htpasswd(); break; case "validate": _htpasswdsync_validate($edit, $account); break; case "load": _htpasswdsync_updategroup(); break; } } // function htpasswdsync_user() function htpasswdsync_admin() { $form['htpasswdsync_htpasswd'] = array( '#type' => 'textfield', '#title' => t('htpasswd file'), '#default_value' => _htpasswdsync_passfilename(), '#size' => 100, '#maxlength' => 200, '#description' => t("Where is stored the !file file we want to synchronize with.", array('!file' => 'htpasswd')), '#required' => TRUE, ); $form['htpasswdsync_htgroup'] = array( '#type' => 'textfield', '#title' => t('htgroup file'), '#default_value' => _htpasswdsync_grpfilename(), '#size' => 100, '#maxlength' => 200, '#description' => t("Where is stored the !file file we want to synchronize with.", array('!file' => 'htgroup')), '#required' => TRUE, ); $form['htpasswdsync_hash'] = array( '#type' => 'radios', '#title' => t('password hashing algorythm'), '#description' => t("How shall the password be hashed (crypt for unix, SHA1 for all)"), '#options' => _htpasswdsync_hashes(), '#default_value' => _htpasswdsync_hash(), ); $form['htpasswdsync_roles'] = array( '#type' => 'checkboxes', '#title' => t('Roles to be exported in htgroup'), '#default_value' => _htpasswdsync_roles(), '#options' => user_roles(TRUE), ); $form['htpasswdsync_overwrite'] = array( '#type' => 'checkbox', '#title' => t('is htpasswd file only managed by this module'), '#default_value' => _htpasswdsync_overwrite(), ); return system_settings_form($form); } /* Test if a file exist and is writable * * @param $filename * name of the file * @return * return false on success, otherwise an error message */ function _htpasswdsync_check_file($file) { $f = @fopen($file, "r"); if (!$f) { return t("File '!file' does not exists, you should create it.", array('!file' => $file)); } else { fclose($f); $f = @fopen($file, "a"); if (!$f) { return t("cannot write to file '!file'.", array('!file' => $file)); } else { fclose($f); } } return FALSE; } function htpasswdsync_admin_validate($form, &$form_state) { $file = $form_state['values']['htpasswdsync_htpasswd']; if ($msg = _htpasswdsync_check_file($file)) { form_set_error('htpasswdsync_htpasswd', $msg); } $file = $form_state['values']['htpasswdsync_htgroup']; if ($msg = _htpasswdsync_check_file($file)) { form_set_error('htpasswdsync_htpasswd', $msg); } } function htpasswdsync_menu() { $items = array(); $items['admin/user/htpasswdsync'] = array( 'title' => 'HTPassword Sync', 'description' => 'Preferences for the HTPasswd Sync module', 'page callback' => 'drupal_get_form', 'page arguments' => array('htpasswdsync_admin'), 'access arguments' => array('access administration pages'), 'type' => MENU_NORMAL_ITEM, ); return $items; } function htpasswdsync_requirements($phase) { $requirements = array(); // if not runtime query, exit if ($phase != 'runtime') { return $requirements; } $htpasswd_status_msg = _htpasswdsync_check_file(_htpasswdsync_passfilename()); $htpasswd_status_val = REQUIREMENT_ERROR; if (!$htpasswd_status_msg) { $htpasswd_status_msg = t('!file file is available', array('!file' => 'htpasswd')); $htpasswd_status_val = REQUIREMENT_OK; } $htgroup_status_msg = _htpasswdsync_check_file(_htpasswdsync_grpfilename()); $htgroup_status_val = REQUIREMENT_ERROR; if (!$htgroup_status_msg) { $htgroup_status_msg = t('!file file is available', array('!file' => 'htgroup')); $htgroup_status_val = REQUIREMENT_OK; } $status = $htpasswd_status_val > $htgroup_status_val ? $htpasswd_status_val : $htgroup_status_val; $requirements['htpasswdsync_status'] = array( 'title' => t('HTPasswd Sync Status'), 'value' => $htpasswd_status_msg ."
". $htgroup_status_msg ."
". t('last update !time ago.', array( '!time' => format_interval(time()-variable_get('htpasswdsync_cron_time', 0)))), 'severity' => $status, ); if ($status != REQUIREMENT_OK) { $requirements['htpasswdsync_status']['description'] = t('Cannot access files, please check configuration.', array('!url' => url('admin/user/htpasswdsync'))); } return $requirements; } function htpasswdsync_cron() { $time = variable_get('htpasswdsync_cron_time', 0); $res = db_query('DELETE FROM {htpasswdsync_passwd} WHERE username NOT IN (SELECT name from {users})'); _htpasswdsync_updatepasswd(); _htpasswdsync_updategroup(); variable_set('htpasswdsync_cron_time', time()); }