<?php

// *****************************
// *                           *
// *     Wizard_Initialize     *
// *                           *
// *****************************

//  $doc_root = $_SERVER['DOCUMENT_ROOT'];
//  include ($doc_root . "/connect/config.php");
  include ("check_session.php");
  include ("get_web_mode.php");
  include ("get_user_ID.php");
  
  if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
  	define('HTTP', 'https://');
  else
  	define('HTTP', 'http://');

  // true sets some variables or bypasses some code to make anonymous development easier
  define ('DEVELOPMENT', false);

  define ('WIZARD_STEPS', 3);

  // folder to store 'template wizard' user image uploads
  define ('USERS_IMAGE_FOLDER', '/'.(BWB_BASE_DIR?BWB_BASE_DIR.'/':'').(BWB_IMAGE_DIR?BWB_IMAGE_DIR.'/':''));

  // folder for 'template wizard' images
  define ('TEMPLATE_IMAGE_FOLDER', "/template_admin/page_images/");

   // and, set max size for user image uploads (e.g. user LOGO, etc.)
  define ('MAX_IMAGE_SIZE', 300000);

  if (!DEVELOPMENT) {
    // check for valid cPanel User
    $cPanel_user_ID = get_user_ID();
    if ($cPanel_user_ID === false) {
      exit;
    } else {
      define('HOME_PATH', '/home/'.$cPanel_user_ID.'/public_html/');
    }
  } else {
    // SET THESE FOR ADMIN DEVELOPMENT PURPOSES
    $cPanel_user_ID = 'ebconten';
    define('HOME_PATH', '/home/'.$cPanel_user_ID.'/public_html/');
  }

  define ('SCRIPT_PATH', dirname(__FILE__));

  if (DEVELOPMENT) {
    ini_set('display_errors', 1);
    ini_set('display_startup_errors',1);
    error_reporting(-1);
    ini_set('log_errors', 1);
    ini_set('error_log', SCRIPT_PATH . '/error_log.txt');
  }

  if (get_web_mode($bwb_dbh)) {
    define('LOCAL_MODE', true);
  } else {
    define('LOCAL_MODE', false);
  }

  $push_request = "";
  $menu_row_cache = "";
  $visited_steps = "";
  $wtf = "wizard_template_filter_";
  $G2P = 'wizVar_'; // added 8-13-2016 GET to POST var passthrough
  $mlnr ="menu_label_name_row";
  $mpnr ="menu_page_name_row";
  $mpr = "menu_plugin_row";
  $indx = "menu_index_num";
  $ifn = "file_name";
  $itn = "image_tag";
  $wtf_set = false;


  foreach ($_REQUEST as $key => $val) {
/*print "<pre style=\"text-align:right;\">";
echo "key = $key"."  ";
  echo "val = $val"."  ";
    echo "GO";
    print "</pre>";*/
    switch ($key) {
      case "ecommerce":
        $push_request .= "  <input type='hidden' name='$key' value='$val' />\n";
        $ecommerce = $val;
print "<pre style=\"text-align:right;\">";
      echo "ecommerce setting=".$ecommerce;
//print_r($_POST);
//print_r($menu_row_cache);

print "</pre>";
        break;
      case "website_type":
        $push_request .= "  <input type='hidden' name='$key' value='$val' />\n";
        $website_type = $val;
        break;
      case "wizard_template_ID": // new ID or none before
        $ID_request = "  <input type='hidden' name='template' value='$val' />\n";
        $wizard_template_ID_previous = $val;
      case "template":
        if ( empty($ID_request)) {
          $ID_request = "  <input type='hidden' name='$key' value='$val' />\n";
          $wizard_template_ID_previous = $val;
        }
        include_once ("get_thumbnail.php");
        $wizard_template_tn = get_thumbnail($wizard_template_ID_previous);
        break;
      case (stripos($key,$wtf)):
        if ($wizard_template_page === '1') {
          //  if on page one - preset template filters
          $wtf_array[$key] = $val;
        } else {
          // else just push it into the $_REQUEST array
          $push_request .= "  <input type='hidden' name='$key' value='$val' />\n";
        }
        // if any previous filters were set ... then we need to preset them ...
        // otherwise set all on
        $wtf_set = true;
        break;
      case (stripos($key,$mlnr)):
        if ($wizard_template_page !== '2') {
          $push_request .= "  <input type='hidden' name='$key' value='$val' />\n";
        }
        $menu_row_cache[str_replace($mlnr,'',$key)-1]['link_text'] = $val;
      
        // if menu was setup ... then we need to preset on return to page 2
        // otherwise pass data on
        break;
      case (stripos($key,$mpnr)):
        if ($wizard_template_page !== '2') {
          $push_request .= "  <input type='hidden' name='$key' value='$val' />\n";
        }
        $menu_row_cache[str_replace($mpnr,'',$key)-1]['page_name'] = $val;
        // if menu was setup ... then we need to preset on return to page 2
        // otherwise pass data on
        break;
      case (stripos($key,$mpr)):
        if ($wizard_template_page !== '2') {
          $push_request .= "  <input type='hidden' name='$key' value='$val' />\n";
        }
        $menu_row_cache[str_replace($mpr,'',$key)-1]['plugin_id'] = ($val==='yes'?1:-1);
        // if menu was setup ... then we need to preset on return to page 2
        // otherwise pass data on
        break;
      case "header_title":
        if ($wizard_template_page !== '3') {
          $push_request .= "  <input type='hidden' name='$key' value='$val' />\n";
        }
        $site_title = $val;
        break;
      case "tagline": // new ID or none before
        if ($wizard_template_page !== '3') {
          $push_request .= "  <input type='hidden' name='$key' value='$val' />\n";
        }
        $tagline = $val;
        break;
      case (stripos($key,$ifn)):
        if ($wizard_template_page !== '3') {
          $push_request .= "  <input type='hidden' name='$key' value='$val' />\n";
        }
        $cached_user_images[str_replace($ifn.'_','',$key)][$ifn] = $val;
        break;
      case (stripos($key,$itn)):
        if ($wizard_template_page !== '3') {
          $push_request .= "  <input type='hidden' name='$key' value='$val' />\n";
        }
        $cached_user_images[str_replace($itn.'_','',$key)][$itn] = ($val?$val:"0");
        break;
      case (stripos($key,'visited_steps')):
        $visited_steps = $val;
        break;
      // added 8-13-2016 djc
      // provide for GET var passthrough to POST var
      case (stripos($key,$G2P)):
        $filteredGETkey = filter_var($key, FILTER_SANITIZE_STRING);
        $filteredGETval = filter_var($val, FILTER_SANITIZE_STRING);
        if ($key === $filteredGETkey && $val === $filteredGETval && !empty($val))  {
          $push_request .= "  <input type='hidden' name='$key' value='$val' />\n";
        }
        break;
      // end of added 8-13-2016 djc
    }
   }
   // delete "new_row" (menu add) from cache array
   if (!empty($menu_row_cache) && is_array($menu_row_cache)) array_pop($menu_row_cache);


//print "<pre style=\"text-align:right;\">";
//echo "this is push_request";
//print "</pre>";
   $push_request .= "  <input type='hidden' name='visited_steps' value='". (intval($visited_steps) | intval(pow(2,($wizard_template_page-1)))) . "' />\n";

   if (!empty($ID_request)) $push_request .= $ID_request;
//   $push_request .=  "  <input type='hidden' name='cpanel_user_id_confirm' value='". $cPanel_user_ID ."' />\n";
   $push_request .=  "  <input type='hidden' name='cuic' value='". $cPanel_user_ID ."' />\n";
//print "<pre style=\"text-align:right;\">";
//echo "wizard_initialize.php END menu_row_cache =";
//print_r($menu_row_cache);
//print "</pre>";


//print "<pre style=\"text-align:right;\">";
//echo "that was push_request";
//print_r($_REQUEST);
//echo "that was $ _request";
//print_r($menu_row_cache);
//echo "that was $ menu_row_cache";
//print "</pre>";
?>