<?php // Website Membership Code Generator Functions

//
function permalink_codeGenerator_showPage() {

  // Support default "Go Back >>" button
  if (array_key_exists('_showCode', $_REQUEST)) { redirectBrowserToURL('?menu=_codeGenerator'); }

  //
  $GENERATOR_FILENAMES_TO_DESCRIPTIONS = array(
    '.htaccess'                 => ".htaccess code for redirecting requests",
    //'user-login.php'            => "Login Page",
    //'user-password-request.php' => "Password Request (Request reset email)",
    //'user-password-reset.php'   => "Password Reset (Linked from reset email)",
    //'user-profile.php'          => "Edit Profile",
  );

  // show header
  $function = "_permalink_codeGenerator";
  $menuName = t("Permalinks");
  echo cg2_header($function, $menuName);

  // show page menu
  print "<ul style='padding-top: 0; padding-bottom: 0;'>\n";
  foreach ($GENERATOR_FILENAMES_TO_DESCRIPTIONS as $filename => $description) {
    $url = thisPageUrl(array('filename' => $filename));
    print  "  <li><a href='$url'>" .htmlencode($description).  "</a></li>\n";
  }
  print "</ul>\n";

  // show page
  $filename = @$_REQUEST['filename'];
  if ($filename && @$GENERATOR_FILENAMES_TO_DESCRIPTIONS[$filename]) {

    // show separator bar
    print adminUI_separator(t('Source Code'));

    // get function name
    $getCodeFunction = "permalinkcode_" . preg_replace("/[^a-z\_]+/s", "_", $filename);
    if (!function_exists($getCodeFunction)) { die("Function $getCodeFunction doesn't exist!"); }

    // show instructions
    $instructions   = array(); // show as bullet points
    $instructions[] = t(sprintf("Save this code as <b>%s</b> (or choose your own name)", $filename));
    #$instructions[] = sprintf('Update the <a href="%s">Archive Url</a> under Newsletter Settings', '?menu=_nlb_settings');
    $filenameSuffix = 'list'; // eg: tablename-FILENAMESUFFIX.php
    cg2_showCode(null, null, $instructions, null, $getCodeFunction());
    exit;
  }

  // show footer (cg2_showCode sends it automatically)
  echo cg2_footer();
  exit;
}

//
function permalinkcode__htaccess() {
  // generate code
  $dispatcherFile = 'permalinks_dispatcher.php';
  list($dispatcherPath, $dispatcherUrl) = getPluginPathAndUrl($dispatcherFile);
  $dispatcherUrl = str_replace('%20', '\\ ', $dispatcherUrl); // For urls with spaces in them: http://stackoverflow.com/questions/410811/mod-rewrite-with-spaces-in-the-urls

  ob_start();
?>
# Add this to an .htaccess file in your website root folder
# Your website root folder is usually named: htdocs, httpdocs, www, html, or just / in FTP

# START: CMS Permalink Code
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /

  ### Set URL_SCHEME (REQUEST_SCHEME isn't available before Apache < 2.4 and can include hostname)
  RewriteCond %{HTTPS} off
  RewriteRule .* - [E=URL_SCHEME:http]
  RewriteCond %{HTTPS} on
  RewriteRule .* - [E=URL_SCHEME:https]

  ### Force www. prefix - (RECOMMENDED/OPTIONAL) SEO to prevent duplicate urls (uncomment to use)
  #RewriteCond %{HTTP_HOST} !^www\. [NC]
  #RewriteRule ^(.*)$ %{ENV:URL_SCHEME}://www.%{HTTP_HOST}/$1 [R=301,L]

  ### Remove www prefix - Optional SEO to prevent duplicate urls (uncomment to use)
  #RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
  #RewriteRule ^(.*)$ %{ENV:URL_SCHEME}://%1/$1 [R=301,L]
  
  ### Force HTTPS secure connections - Optional security (uncomment to use)
  #RewriteCond %{HTTPS} off
  #RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

  ### Pass URL to permalinks dispatcher (do this last after any www or https redirects)
  RewriteRule ^<?php echo addcslashes($dispatcherFile, '.'); ?>$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . <?php echo $dispatcherUrl ?> [L]
</IfModule>
# END: CMS Permalink Code

<?php
  // return code
  $code = ob_get_clean();
  return $code;
}
?>