/**
  * Generate the API Key
  *
  * @param struct $args
  * @param string $args["user"]
  * @param string $args["pass"]
  * @return string
  * @access public
  */
 public function generateAPIKey($args)
 {
     $this->_setArgs($args);
     $login = $this->args[self::$userParamName];
     $pwd = $this->args['pass'];
     $user = new tlUser();
     $user->login = $login;
     $login_exists = $user->readFromDB($this->dbObj, tlUser::USER_O_SEARCH_BYLOGIN) >= tl::OK;
     $checkBD = $user->comparePassword($pwd) == tl::OK;
     $checkLDAP = ldap_authenticate($login, $pwd);
     if ($checkBD or $checkLDAP->status_ok) {
         $user_id = tlUser::doesUserExist($this->dbObj, $login);
         if (is_null($user_id)) {
             $this->errors[] = new IXR_Error(NO_USER_BY_THIS_LOGIN, 'This is a valid user, but is not on TestLink DB');
         } else {
             $op = new stdClass();
             $op->status = tl::OK;
             $op->user_feedback = null;
             $APIKey = new APIKey();
             $ak = $APIKey->getAPIKey($user_id);
             if (!is_null($ak)) {
                 return $ak;
             }
             if ($APIKey->addKeyForUser($user_id) >= tl::OK) {
                 return $APIKey->getAPIKey($user_id);
             } else {
                 $this->errors[] = new IXR_Error(NO_DEV_KEY, NO_DEV_KEY_STR);
             }
         }
     } else {
         $this->errors[] = new IXR_Error(INVALID_AUTH, INVALID_AUTH_STR);
     }
     return $this->errors;
 }
/**
 * 
 */
function createNewAPIKey(&$dbHandler, &$argsObj, &$userObj)
{
    $op = new stdClass();
    $op->user_feedback = '';
    // Try to validate mail configuration
    //
    // From Zend Documentation
    // You may find you also want to match IP addresses, Local hostnames, or a combination of all allowed types.
    // This can be done by passing a parameter to Zend_Validate_Hostname when you instantiate it.
    // The paramter should be an integer which determines what types of hostnames are allowed.
    // You are encouraged to use the Zend_Validate_Hostname constants to do this.
    // The Zend_Validate_Hostname constants are: ALLOW_DNS to allow only DNS hostnames, ALLOW_IP to allow IP addresses,
    // ALLOW_LOCAL to allow local network names, and ALLOW_ALL to allow all three types.
    //
    $validator = new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_ALL);
    $smtp_host = config_get('smtp_host');
    $op->status = tl::ERROR;
    // We need to validate at least that user mail is NOT EMPTY
    if ($validator->isValid($smtp_host)) {
        $APIKey = new APIKey();
        if ($APIKey->addKeyForUser($argsObj->user_id) >= tl::OK) {
            logAuditEvent(TLS("audit_user_apikey_set", $userObj->login), "CREATE", $userObj->login, "users");
            $op->user_feedback = lang_get('apikey_by_mail');
            $op->status = tl::OK;
            // now send by mail
            $ak = $APIKey->getAPIKey($argsObj->user_id);
            $msgBody = lang_get('your_apikey_is') . "\n\n" . $ak . "\n\n" . lang_get('contact_admin');
            $mail_op = @email_send(config_get('from_email'), $userObj->emailAddress, lang_get('mail_apikey_subject'), $msgBody);
        }
    } else {
        $op->status = tl::ERROR;
        $op->user_feedback = lang_get('apikey_cannot_be_reseted_invalid_smtp_hostname');
    }
    return $op;
}