static function addVariable($app)
 {
     if (!v::key('name', v::stringType())->validate($app->request->post()) || !v::key('value', v::stringType())->validate($app->request->post())) {
         // Validate input parameters
         return $app->render(400, array('msg' => 'Save failed. Check your parameters and try again.'));
     }
     // Check if a variable with this name already exists
     $existing = ConfigData::getVariableByName($app->request->post('name'));
     if ($existing) {
         return $app->render(400, array('msg' => 'A system config variable with that name already exists.'));
     }
     $disabled = 0;
     if (v::key('disabled')->validate($app->request->post())) {
         // TODO: Implement cusitom boolean Respect\Validator
         // Converting to boolean did not work well,
         // This allows a wider range of true false values
         $disabled = $app->request->post('disabled') === 1 || $app->request->post('disabled') === '1' || $app->request->post('disabled') === true || $app->request->post('disabled') === 'true' ? 1 : 0;
     }
     // Add Cariable
     $data = array(":name" => $app->request->post('name'), ":value" => $app->request->post('value'), ':disabled' => $disabled, ":created_user_id" => APIAuth::getUserId(), ":last_updated_by" => APIAuth::getUserId());
     $variableId = ConfigData::insertVariable($data);
     if ($variableId) {
         $config = ConfigData::getVariableById($variableId);
         return $app->render(200, array('variable' => $config));
     } else {
         return $app->render(400, array('msg' => 'Could not select system config variable.'));
     }
 }
 private static function forgotpassword_validateFoundUser($post, $app)
 {
     $user = AuthData::selectUserAndPasswordByEmail($post['email']);
     if (!$user) {
         // Validate existing user
         return array('frgtauthenticated' => false, 'msg' => 'Forgotpassword failed. A user with that email could not be found.');
     }
     $usertoken = md5(date('Y-m-d H:i:s') * rand(9, 99999));
     $fortgotpassword_duration = date('Y-m-d H:i:s');
     $userforgotupdate = AuthData::updateforgotpassworddata(array(':email' => $post['email'], ':usertoken' => $usertoken, ':fortgotpassword_duration' => $fortgotpassword_duration));
     $mail = new \PHPMailer();
     $mail->IsSMTP();
     $mail_variables = array("SMTP_SERVER_HOST" => "Host", "SMTP_SERVER_PORT" => "Port", "SMTP_SERVER_USERNAME" => "Username", "SMTP_SERVER_PASSWORD" => "Password", "SMTP_SMTP_DEBUG" => 'SMTPDebug', "SMTP_DEBUGOUTPUT" => 'Debugoutput', "SMTP_SECURE" => "SMTPSecure", "SMTP_AUTH" => "SMTPAuth", "PASSWORD_RESET_EMAIL_FROM" => "From", "PASSWORD_RESET_EMAIL_SUBJECT" => "Subject", "PASSWORD_RESET_EMAIL_BODY" => "Body");
     foreach ($mail_variables as $name => $value) {
         $config_data = ConfigData::getVariableByName($name);
         if ($config_data && $config_data->disabled != 1) {
             $mail->{$value} = $config_data->value;
         }
     }
     $config_data = ConfigData::getVariableByName("PASSWORD_RESET_ROOT_URL");
     $root_url = $config_data->value == '' ? ApiConfig::get('WEBSITE_URL') : $config_data->value;
     $mail->setFrom($mail->From, 'Triviajoint');
     $mail->addAddress($user->email, $user->nameFirst . " " . $user->nameLast);
     $mail->isHTML(true);
     $fields = array("!@FIRSTNAME@!", "!@LASTNAME@!", '!@WEBSITEURL@!', '!@LINKID@!');
     $values = array($user->nameFirst, $user->nameLast, $root_url, $usertoken);
     $mail->Body = str_replace($fields, $values, $mail->Body);
     if (!$mail->send()) {
         return array('frgtauthenticated' => false, 'msg' => 'Email could not be sent for reset password. Please try again later.');
     } else {
         return array('frgtauthenticated' => true, 'msg' => 'Email has been sent to your email address for reset password.');
     }
 }