<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/
class Core_Form_Admin_Settings_Spam extends Engine_Form
{
  protected $_captcha_options = array(
        1 => 'Yes, make members complete the CAPTCHA form.',
				2 => 'Yes, enable the Google ReCaptcha Version v3.',
        0 => 'No, do not show a CAPTCHA form.',
  );

  public function init()
  {

    $maindescription = $this->getTranslator()->translate(
        'Social networks are often the target of aggressive spam tactics. This most often comes in the form of fake user accounts and spam in comments. On this page, you can manage various anti-spam and censorship features. Note: To turn on the signup image verification feature (a popular anti-spam tool), see the Signup Progress page. <br>');

    $settings = Engine_Api::_()->getApi('settings', 'core');

    if( $settings->getSetting('user.support.links', 0) == 1 ) {
      $moreinfo = $this->getTranslator()->translate(
          'More Info: <a href="%1$s" target="_blank"> KB Article</a>');
    } else {
      $moreinfo = $this->getTranslator()->translate(
          '');
    }

    $maindescription = vsprintf($maindescription.$moreinfo, array(
      'https://community.socialengine.com/blogs/597/33/spam-and-banning-tools',
    ));

    // Decorators
    $this->loadDefaultDecorators();
    $this->getDecorator('Description')->setOption('escape', false);

    // Set form attributes
    //$this->setTitle('Spam & Banning Tools');
    $this->setDescription($maindescription);

    // init ip-range ban
    $translator = $this->getTranslator();
    if( $translator ) {
      $description = sprintf($translator->translate('CORE_FORM_ADMIN_SETTINGS_SPAM_IPBANS_DESCRIPTION'), Engine_IP::normalizeAddress(Engine_IP::getRealRemoteAddress()));
    } else {
      $description = 'CORE_FORM_ADMIN_SETTINGS_SPAM_IPBANS_DESCRIPTION';
    }
    $this->addElement('Textarea', 'bannedips', array(
      'label' => 'IP Address Ban',
      'description' => $description,
    ));

    // init email bans
    $this->addElement('Textarea', 'bannedemails', array(
      'label' => 'Email Address Ban',
      'description' => 'CORE_FORM_ADMIN_SETTINGS_SPAM_EMAILBANS_DESCRIPTION',
    ));

    // init username bans
    $this->addElement('Textarea', 'bannedusernames', array(
      'label' => 'Profile Address Ban',
      'description' => 'CORE_FORM_ADMIN_SETTINGS_SPAM_USERNAMEBANS_DESCRIPTION',
    ));

    // init censored words
    $this->addElement('Textarea', 'bannedwords', array(
      'label' => 'Censored Words',
      'description' => 'CORE_FORM_ADMIN_SETTINGS_SPAM_CENSOR_DESCRIPTION',
    ));

    $this->addElement('Radio', 'signup', array(
      'label' => 'Enable CAPTCHA',
      'description' => 'Do you want to show CAPTCHA on signup, invite, login and contact?',
      'multiOptions' => $this->_captcha_options,
      'value' => 0,
			'onchange' => 'recaptchaVersion(this.value);',
    ));

    // $this->addElement('Radio', 'recaptcha_version', array(
    //   'label' => 'ReCaptcha Version v3',
    //   'description' => 'Do you want to enable ReCaptcha on your site?',
    //   'multiOptions' => array(
    //       1 => 'No',
    //       0 => 'Yes',
    //   ),
    //   'onchange' => 'recaptchaVersion(this.value);',
    //   'value' => 1,
    // ));

    // recaptcha
    if( $translator ) {
      $description = sprintf($translator->translate('You can obtain API credentials at: %1$s'),
          $this->getView()->htmlLink('https://www.google.com/recaptcha',
              'https://www.google.com/recaptcha'));
    } else {
      $description = null;
    }

    //recaptcha v3
    $this->addElement('Text', 'recaptchapublicv3', array(
      'label' => 'ReCaptcha Public Key for v3',
      'description' => $description,
      'filters' => array(
        'StringTrim',
      ),
    ));
    $this->getElement('recaptchapublicv3')
        ->getDecorator('Description')
        ->setOption('escape', false);

    $this->addElement('Text', 'recaptchaprivatev3', array(
      'label' => 'ReCaptcha Private Key for v3',
      'description' => $description,
      'filters' => array(
        'StringTrim',
      ),
    ));
    $this->getElement('recaptchaprivatev3')
        ->getDecorator('Description')
        ->setOption('escape', false);

    //lock account
    $this->addElement('Radio', 'lockaccount', array(
      'label' => 'Block Account on Unsuccessful Login Attempts',
      'multiOptions' => array(
          1 => 'Yes, block user account on unsuccessful login attempts.',
          0 => 'No, do not block user account on unsuccessful login attempts.',
      ),
      'onclick'=>'changeLock(this);',
      'value' => 0,
    ));
    
    $this->addElement('Text', 'lockattempts', array(
      'label' => 'Number of Unsuccessful Attempts',
      'description' => 'Enter the number of unsuccessful login attempts after which user accounts will be blocked for login.',
      'validators' => array(
          array('Int', true),
          array('GreaterThan', true, array(0)),
      ),
      'value' => 3,
    ));

    $this->addElement('Text', 'lockduration', array(
        'label' => 'Account Block Duration',
        'description' => 'CORE_ADMIN_FORM_SETTINGS_SPAM_LOCKDURATION_DESCRIPTION',
        'validators' => array(
            array('Int', true),
            array('GreaterThan', true, array(0)),
        ),
        'value' => 120,
    ));

    // init submit
    $this->addElement('Button', 'submit', array(
      'label' => 'Save Changes',
      'type' => 'submit',
      'ignore' => true,
    ));
  }
}
