<?php
defined('_JEXEC') or die('Restricted access');

/**
* @package OS CCK
* @copyright 2020 OrdaSoft.
* @author Andrey Kvasnevskiy(akbet@mail.ru),Roman Akoev (akoevroman@gmail.com)
* @link http://ordasoft.com/cck-content-construction-kit-for-joomla.html
* @description OrdaSoft Content Construction Kit
*/
if(!isset($db)){
    $db = new os_cckDb();
}
$app = JFactory::getApplication();
$fName = $field->db_field_name;
$task = protectInjectionWithoutQuote('task', '', 'STRING');
$icon_alias_prefix = (isset($field_from_params[$fName.'_add_icon_alias_prefix'])) ? $field_from_params[$fName.'_add_icon_alias_prefix'] : '';
if($icon_alias_prefix != ''){
    $layout_html = str_replace($field->db_field_name.'_label_icon_prefix_hidden', 'fa '.$icon_alias_prefix, $layout_html);
}
$icon_alias_suffix = (isset($field_from_params[$fName.'_add_icon_alias_suffix'])) ? $field_from_params[$fName.'_add_icon_alias_suffix'] : '';
if($icon_alias_suffix != ''){
    $layout_html = str_replace($field->db_field_name.'_label_icon_suffix_hidden', 'fa '.$icon_alias_suffix, $layout_html);
}
$use_google_captcha = (isset($field_from_params[$fName.'_use_google_captcha'])) ? $field_from_params[$fName.'_use_google_captcha'] : 0;
//var_dump($use_google_captcha);
if($use_google_captcha > 0 && checkOsCckVersionProFree() == 'pro'){ 
  if(isset($GLOBALS['recaptcha_count'])){
    $recaptcha_count = $GLOBALS['recaptcha_count'] + 1;
  }else{
    $recaptcha_count = 0;
  }
  $GLOBALS['recaptcha_count'] = $recaptcha_count;
  $config = JFactory::getConfig();
  $captha_type = $config->get('captcha', '');
  if($captha_type != 'recaptcha_invisible'){
//  var_dump($captha_type);
  ?>
      
    <div class="recaptcha_field <?php echo $layout_params['custom_class']?>" <?php echo $offset_animation; ?> recaptcha_count="<?php echo $recaptcha_count; ?>">
    <?php
    $captcha_plugin = JPluginHelper::getPlugin('captcha', $captha_type);
  //  var_dump($captcha_plugin);
    $captcha_key = rand();
  //  var_dump($captha_type);
    $captcha = JCaptcha::getInstance($captha_type, array('namespace' => 'captcha_keystring_'.$captcha_key ));
  //  $captcha = JPluginHelper::importPlugin( 'captcha',$captha_type);
  //   $app->triggerEvent('onDisplay', array('namespace' => 'captcha_keystring_'.$captcha_key ));
  //  $captcha = JPluginHelper::importPlugin( 'captcha',$captha_type);
  //   var_dump($captcha);
    echo $captcha->display('recaptcha', 'recaptcha', 'required');
    echo '</div>';

    $captcha_plugin = JPluginHelper::getPlugin('captcha', $captha_type);
  //  var_dump($captcha_plugin);
    $captcha_plugin_params = new JRegistry($captcha_plugin->params);
  //   var_dump($captcha_plugin_params);
    $recaptcha_pub_key = $captcha_plugin_params->get('public_key');
    $recaptcha_private_key = $captcha_plugin_params->get('private_key');
    echo '<input type="hidden" name="recaptcha_namespace" value="captcha_keystring_'.$captcha_key.'">';
  }
}else{
  $st = "      ";
  $algoritm = mt_rand(1, 2);
  switch ($algoritm) {
    case 1:
      for ($j = 0; $j < 6; $j += 2) {
          $st = substr_replace($st, chr(mt_rand(97, 122)), $j, 1); //abc
          $st = substr_replace($st, chr(mt_rand(50, 57)), $j + 1, 1); //23456789
      }
      break;
    case 2:
      for ($j = 0; $j < 6; $j += 2) {
          $st = substr_replace($st, chr(mt_rand(50, 57)), $j, 1); //23456789
          $st = substr_replace($st, chr(mt_rand(97, 122)), $j + 1, 1); //abc
      }
      break;
  }
  //****** begin search in $st simbol 'o, l, i, j, t, f'   ************
  $st_validator = "olijtf";
  for ($j = 0; $j < 6; $j++) {
    for ($i_cap = 0; $i_cap < strlen($st_validator); $i_cap++) {
      if ($st[$j] == $st_validator[$i_cap]) {
        $st[$j] = chr(mt_rand(117, 122)); //uvwxyz
      }
    }
  }
  //*****   end search in $st simbol 'o, l, i, j, t, f'  *****************
  $session = JFactory::getSession();

  // if($task != 'onCloseDatapicker' && $task != 'ajax_rent_calcualete')
  // {
      $session->set('captcha_'.$layout_type.'_keystring', $st);
  // }


  if(!isset($layout_params['custom_class']))
    $layout_params['custom_class'] = '';
  if(isset($field) && isset($layout_params['fields']['showName_' . $fName]) &&
    $layout_params['fields']['showName_' . $fName] == 'on'){
    $layout_html = str_replace($fName . '-label-hidden', '', $layout_html);
  }
  $offset_animation = get_field_offset_animation($field, $layout);
  ?>
  <div class="<?php echo $layout_params['custom_class']?>" <?php echo $offset_animation; ?> >
    <script>
      var reload_captcha_<?php echo $layout_type?> = null;
      (function ($) {
        reload_captcha_<?php echo $layout_type?> = function (moduleId,type) {
          var status = ""
          var fName = "<?php echo $fName ?>";
          $.post("index.php?option=com_os_cck&captcha_type="+type+"&task=reload_captcha&captchafName="+fName+"&lid=<?php echo $layout->lid?>&format=raw",
          function (data) {
            jQuerOs("#captcha-block-<?php echo $layout_type?>").html(data.html);
          } , "json" )
        }
      })(jQuerOs)
    </script>
    <div id="captcha-block-<?php echo $layout_type?>">
      <?php
      if(isset($_REQUEST['error']) && $_REQUEST['error'] != ""){?>
        <font style='color:red'><?php echo protectInjectionWithoutQuote('error')?></font><br />
        <?php
      }
      $name_user = "";
      if (isset($_REQUEST['name_user'])) $name_user = protectInjectionWithoutQuote('name_user','','STRING');?>

        <?php //$link = JRoute::_("index.php?option=com_os_cck&task=secret_image&type=$layout_type&ttt=" . rand(0,15000));?>
        <?php $link = "index.php?option=com_os_cck&task=secret_image&type=$layout_type&ttt=" . rand(0,15000);?>
        <img id="captcha_img_<?php echo $layout_type?>" 
              src="<?php echo $link;?>"
              alt="CAPTCHA_picture"/>
        <img id="captcha_img_<?php echo $layout_type?>" 
            src="<?php echo JUri::root()?>/components/com_os_cck/images/reload.png" 
            onClick="reload_captcha_<?php echo $layout_type.'(0,'.$db->Quote($layout_type).')'?>"
            alt="reload" />
    </div>
    <?php
    if($task != "reload_captcha"){?>
      <div>
        <?php echo cck_lang_text("COM_OS_CCK_LABEL_REVIEW_KEYGUEST")?>
      </div>
      <span <?php if(isset($layout_params['fields']['description_'.$fName]) && $layout_params['fields']['description_'.$fName]=='on' && !empty($layout_params['fields'][$fName.'_tooltip']))
          {?>
  rel="tooltip" data-toggle="tooltip" data-placement="top" title="<?php echo $layout_params['fields'][$fName.'_tooltip'];?>"
      <?php } ?> >
      <div>
        <input <?php echo $layout_params['field_styling']?> class="inputbox <?php echo $field->db_field_name; ?>" type="text" name="keyguest" size="6" maxlength="6" autocomplete="off"/>
        <input class="inputbox" type="hidden" name="type" value="<?php echo $layout_type;?>"/>
      </div>
      </span>
    <?php 
    }?>
  </div>
<?php }