$versions{'urls_cats_subcats_and_pages-ext_lib.pl'} = '06.6.00.0000';

# Contains all sub routines (functions) to manage page titles, URLs, display titles
# for subcats (sub-categories).
# Allows for dynamic token-based titles, also on/off for
# mod_rewrite htaccess url re-writing.
#
# Formerly the SEO settings manager: seo_manager-ext_lib.pl

#######################################################################
#
# AgoraCart and all associated files, except where noted, are
# Copyright 2001 to Present jointly by K-Factor Technologies, Inc.
# and by C E Mayo (aka Mister Ed) at AgoraCart.com & K-Factor.net
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This copyright notice may not be removed or altered in any way.
#
#######################################################################

{
    local ($modname) = 'urls_cats_subcats_and_pages';
    register_extension( $modname, "URL Manager for Cats, Subcat, Pages and Titles", $versions{$modname} );
    register_menu( 'display_seo_settings_screen', 'display_seo_settings_screen',
        $modname, 'Subcat Titles and URLs Settings' );
    add_settings_choice(
        'URLs for Categories, Subcats &amp; HTML Pages',
        ' URLs for Categories, Subcats &amp; HTML Pages ',
        'display_seo_settings_screen'
    );

    register_menu( 'UpdateSubcatTitlesAndURLs', "write_update_seo_settings",
        $modname, 'Write - Update URLS for Category, Subcats, Pages' );
}

#######################################################################

sub display_seo_settings_screen {
    print &$manager_page_header( 'Subcats: Page Titles &amp; URL Settings Manager', '', '', '', '' );
    my $user_options = q{};
    if ( $sc_seo_friendly_cats_have_dashes eq '' ) {
        $sc_seo_friendly_cats_have_dashes = 'no';
    }
    if ( $sc_userfields_available =~ /10|20/ ) {
        $user_options = qq~
<option value="12-user6">user6</option>
<option value="13-user7">user7</option>
<option value="14-user8">user8</option>
<option value="15-user9">user9</option>
<option value="16-user10">user10</option>
~;
    }
    if ( $sc_userfields_available eq '20' ) {
        $user_options .= qq~
<option value="17-user11">user11</option>
<option value="18-user12">user12</option>
<option value="19-user13">user13</option>
<option value="20-user14">user14</option>
<option value="21-user15">user15</option>
<option value="22-user16">user16</option>
<option value="23-user17">user17</option>
<option value="24-user18">user18</option>
<option value="25-user19">user19</option>
<option value="26-user20">user20</option>
~;
    }

    print <<ENDOFTEXT;
<h1 class='mgr-page-title'>Subcats, Page titles, URLs Settings Manager</h1>
<p><br></p>

<p class="well">This manager allows you to control the text composition for category and sub-category titles, switch to "user friendly URLs", and set up to 3 additional artificial sub-category levels beyond the 1st level of subcats (Sub-categories) that the cart normally handles. The additional subcat levels require additional product database userfields to be used, one for each level desired. When using this manager, category lists are rebuilt (if more than one sub-category level is enabled) and the .htaccess file is created or altered to handle the new URL routing for "user friendly URLs" and/or additional sub-category levels IF user-friendly URLs are enabled.</p>
<p><br></p>
ENDOFTEXT

    if ( $in{'system_edit_success'} ) {
        print <<ENDOFTEXT;
<div class="row col-xs-12 col-sm-12 col-md-12 col-lg-12 hidden-xs">
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 bg-success">
        <h4 class="text-primary mgr-center">Your SEO &amp; URL settings have been successfully updated.</h4>
        <p>New Category Links have been rebuilt as well.  <b>NOTE</b>: You will need to save your settings in the Best Sellers, New Products, and Featured Product managers if you switched to or from "user friendly URLs" as the current URLs entered may or may not work as desired.</p>
        </div>
    </div>
<div class="row col-xs-12 col-sm-12"><p><br></p></div>
ENDOFTEXT
    }


    print qq~
<h3 class="mgr-page-title"><b>Product Page Titles</b></h3>
<p><br></p>

<p class="well">Product Pages are the single product pages generated by the cart when you link to or search for a product by its database ID or "p_id". However, as of Route 66 (version 6.6), some templates may not link to individual pages and item information might be displayed within a CSS 3 / HTML 5 modal overlay instead.</p>
<p><br></p>

<form action="manager.cgi" method="post">
<input type="hidden" name="mgr_seshun" value="$in{'mgr_seshun'}">

<p><b>Place a sub-category userfield value in product page titles? &nbsp; <a data-toggle="collapse" href="#prodPageTitleUser" aria-expanded="false" aria-controls="prodPageTitleUser"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><select name="seoindex">
<option value="$sc_seo_index-$sc_seo_index_disp">$sc_seo_index_disp</option>
<option value="">none</option>
<option value="8-user2">user2</option>
<option value="9-user3">user3</option>
<option value="10-user4">user4</option>
<option value="11-user5">user5</option>
$user_options
</select></p>
<div class="collapse" id="prodPageTitleUser">
    <div class="well">
          <p>Select which userfield to use.</p>
          <p>Normally you will want to use the same userfield that is enabled for subcats (the first level).</p>
          <p><b>NOTE</b>: The userfield for sub-categories is set in "Main / Core Store Settings" under the "database" section.</p>
    </div>
</div>
<p><br></p>

<p><b>Product Page Title: &nbsp; <a data-toggle="collapse" href="#prodPageTitleTokens" aria-expanded="false" aria-controls="prodPageTitleTokens"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><input type="text" value="$sc_pid_title" name="pid_title" class="form-control" maxlength="50"></p>
<div class="collapse" id="prodPageTitleTokens">
    <div class="well">
          <p>This is the tokenized string that will be used to substitute values with inside the Product Page Titles (in the HTML header).</p>
          <p>Default: [[category]] [[name]] [[userfield]]</p>
          <p>Use the following tokens: [[name]], [[userfield]], [[pid]], [[category]], [[subcategory]]</p>
          <p><b>NOTE</b>: Titles set in the <b>Category productPage and Metadata manager</b> for the 1st level of sub-categories will overwrite these setting IF you only have a single level of sub-categories.</p>
          <p><b>NOTE</b>: The userfield for sub-categories is set in "Main / Core Store Settings" under the "database" section.</p>
    </div>
</div>
<p><hr></p>


<h3 class="mgr-page-title"><b>Category Page Titles</b></h3>
<p><br></p>

<p class="well">Category Pages are the pages with multiple products that are generated by the cart when you link to products by their category and sub-category names ( i.e.: when you see "product="  and/or the "userX=" for your sub-categories in the URLs -- AND NO p_id in URL ).</p>
<p><br></p>

<p><b>Category Page Title: &nbsp; <a data-toggle="collapse" href="#catPageTitleTokens" aria-expanded="false" aria-controls="catPageTitleTokens"><i class="fa fa-info-circle"></i></a></b><span style="padding-left: 25px;"></span><input type="text" value="$sc_cat_title" name="cat_title" class="form-control" maxlength="50"></p>
<div class="collapse" id="catPageTitleTokens">
    <div class="well">
          <p>This is the tokenized string that will be used to substitute values with inside the Category Page Titles (in the HTML header).</p>
          <p>Default: [[category]] [[user4]]</p>
          <p>Use the following tokens: [[category]] and/or [[user2]] thru [[user$sc_userfields_available]]</p>
          <p><b>NOTE</b>: Titles set for product categories in the <b>Category productPage and Metadata manager</b> will overwrite these setting IF set for a particular ccatgory.</p>
    </div>
</div>
<p><hr></p>

<h3 class="mgr-page-title"><b>Switch to User Friendly URLs</b></h3>
<p><br></p>

<p class="well"><b>What This Does:</b><br>
This setting switches your dynamic store URLs for product categories, product pages and links to pages to URLs that are a little more friendly to the eye.<br><br>
<b>Benefits:</b><br>
There are small SEO benefits in using these types of URLs, but these benefits are diminished as of Version 6.0 and above.
Google has improved the way it sees dynamic URLs and as of Version 6 and improved yet again with the Route 66 version, the cart URLs are even more search engine friendly.
<br><br>As a result, <font color="red">there is <b>NO</b> significant technical SEO need to switch to these types of URLs other than personal preferences, so we advise against using this as it adds more complication to managing your storefront.</font><br><br>
<b>How URLs Change:</b><br>Dynamic store URLs change from something like this:<br>
<br>http://www.storename.com/store/agora.cgi?product=Books&user4=Entrepreneurship
<br><b>to</b>
<br>http://www.storename.com/store/Books-Entrepreneurship<br><br>
However, both of the above URLs would be indexed almost identically by Google, as tested as of AgoraCart version 6.<br><br>
<b>Requirements &amp; Warnings:</b><br>
SERVER: <b>mod_rewrite</b> must enabled on your server.<br><br>
PRODUCTS: database fields for product category name and sub-categories should not contain any single or double quotes, nor any other meta-characters ( <i>non-alphanumeric characters and those that you would use the "shift key" plus a key to type</i> such as \$,\#,\%,&amp;,\@,*,',",-,<,>,?,/, etc ). If used, any category or sub-category that contains these characters will fail to show properly.</p>
<p><br></p>

<p><b>Switch to User & SEO friendly URLs?</b><span style="padding-left: 25px;"></span><select name="seo_friendly"><option value="$sc_seo_friendly">$sc_seo_friendly</option>
<option value="yes">yes</option>
<option value="no">no</option>
</select></p>
<p><hr></p>

<p><b>Customize Categories in User &amp;SEO friendly URLs: &nbsp; <a data-toggle="collapse" href="#catOrderinUserURLs" aria-expanded="false" aria-controls="catOrderinUserURLs"><i class="fa fa-info-circle"></i></a></b><br><select name="catlev1">
<option value="$sc_catlev1">$sc_catlev1</option>
<option value=""></option>
<option value="product">product ( the main category )</option>
<option value="user2">user2</option>
<option value="user3">user3</option>
<option value="user4">user4</option>
<option value="user5">user5</option>
<option value="$sc_subcat_index_field">Sub-Catagory: $sc_subcat_index_field</option>
$user10_option_thingy
$user20_option_thingy
</select> -->>
<select name="catlev2">
<option value="$sc_catlev2">$sc_catlev2</option>
<option value=""></option>
<option value="$sc_subcat_index_field">Sub-Catagory: $sc_subcat_index_field</option>
<option value="user2">user2</option>
<option value="user3">user3</option>
<option value="user4">user4</option>
<option value="user5">user5</option>
<option value="product">product</option>
$user10_option_thingy
$user20_option_thingy
</select>
-->><select name="catlev3">
<option value="$sc_catlev3">$sc_catlev3</option>
<option value=""></option>
<option value="user2">user2</option>
<option value="user3">user3</option>
<option value="user4">user4</option>
<option value="user5">user5</option>
$user10_option_thingy
$user20_option_thingy
</select>
-->><select name="catlev4">
<option value="$sc_catlev4">$sc_catlev4</option>
<option value=""></option>
<option value="user2">user2</option>
<option value="user3">user3</option>
<option value="user4">user4</option>
<option value="user5">user5</option>
$user10_option_thingy
$user20_option_thingy
</select></p>
<div class="collapse" id="catOrderinUserURLs">
    <div class="well">
          <p>If using user/SEO friendly URLs, specify category and sub-category fields for the actual user/SEO friendly URLs used by the cart (max of 4 levels deep).</p>
          <p><b>Sugggested Use:</b><br><br>
          <b>First field</b> select product for the main product category name ( dynamically used per product category shown ).
          <br><b>Second Field</b>, chose the user field from product database that holds the sub-catgories in your product database.
          <br><b>Third and Fourth fields</b>, chose the user field from product database that holds a quasi-subcatgory name, additional keywords, or any other wording that refines the product categories your store holds.
          <br><br>URLs using more than the first two options above must be added manullay  to any category list or
          other links your store uses because they are the only fields recognized by the store's category list builders.
          If user fields are selected above, they must not be populated with other data and must remain open on each product, even if not used.</p>
          <p><b>Product Categories</b> in your product database should not contain the following characters: + , . : ; / \ | = < > ? ( ) { } [ ] ^ % \$ # @ ! \~ nor single or double quotes. Dashes, underscores and spaces are okay. Underscores are replaced by spaces when looking up categories. If Dashes are in the Category names, set the above setting to change the category separators from a dash to a comma, but we HIGHLY recommend taking the dashes out of your categories before using this method.  If your categories use these types of characters, then you should not use the user friendly URLs for SEO.</p>
          <p></p>
          <p>Meta Description Tags for individual product pages are generated using the first two sentences of the product description from the database (all text preceeding the first two period characters in the description).</p>
    </div>
</div>
<p><br></p>

<input type="hidden" name="system_edit_success" value="yes">
<input name="UpdateSubcatTitlesAndURLs" type="hidden" value="Submit">
<button class="btn btn-primary btn-lg btn-block mgr-center">Update SEO Settings</button>
<div class="row col-sm-12"><p><br><br><br></p></div>

</form>
~;
    print &$manager_page_footer;
}

#######################################################################

sub write_update_seo_settings {
    local ($myset) = q{};
    my (
        $htac,   $htacStart, $storepath, $newpath,
        $stuffs, $myalias,   $my301,     $ref,
        $myali,  $myquery,   $sep
    ) = q{};
    my ( $mc_rewrite_found, $catlev_holder, $htacEnd, ) = q{};
    my $tempLine01 = q{############  SEO BEGIN  ############};
    my $tempLine02 = q{# display cartlink pages};
    my $tempLine03 = q{############  SEO END  ############};
    my $tempLine04 = q{RewriteEngine};
    my $htacOn     = $tempLine04 . ' On' . "\n";
    my $startLine  = 'no';

    $sep = ',';


    #if ($in{'seo_friendly'} =~ /yes/igx) {
    my @catlevels = ( "$in{'catlev1'}", "$in{'catlev2'}", "$in{'catlev3'}",
        "$in{'catlev4'}" );
    my $count = 0;
    my @levs  = q{};
    $storepath = $sc_store_url;
    $storepath =~ s/agora\.cgi//gi;
    $htac = $tempLine01;

    # display cartlink pages
    $htac .=
        "\n"
      . $tempLine02 . "\n"
      . 'RewriteRule ^(cl[\/])([\w\d\._\-]*)(\.htm|\.html)$ agora.cgi?cartlink=$2$3 [L]'
      . "\n";

    # display static product pages
    $htac .=
        "\n"
      . '# display static product pages' . "\n"
      . 'RewriteRule ^(page[\/])([\w\d\._\-]*)(\.htm|\.html)$ agora.cgi?page=$2$3 [L]'
      . "\n";
    $htac .= "\n\n\n";
    $htac .= '############  SEARCHES  ############ ';
    $htac .= "\n\n";
    $htac .= '# keywords - next - ppinc';
    $htac .= "\n";
    $htac .=
'RewriteCond %{QUERY_STRING} ^keywords=([\w\d\._\-\+]*).*&amp;next=([\d]+)&amp;ppinc=([\w\d_]+).*$'
      . "\n"
      . 'RewriteRule ^(.*)agora\.cgi$ '
      . $storepath
      . '-k-%1-n%2-i-%3? [R=301,L]' . "\n"
      . 'RewriteRule ^/?-k-([\w\d\._\-\+]*)-n([\d]+)-i-([\w\d_]*)$ agora.cgi?keywords=$1&next=$2&ppinc=$3&xm=on [L]'
      . "\n"
      . 'RewriteRule ^/?-k-([\w\d\._\-\+]*)-i-([\w\d_]*)$ agora.cgi?keywords=$1&ppinc=$2&xm=on [L]'
      . "\n";
    $htac .= "\n\n";

#Put our parameters in order.  We are keeping: user fields and next in url string.
#Bring to front - pid. order userfields.
#send to back, ppinc, next, done
#strip xm, cart_id. XM is ALWAYS ON
    codehook('seo_order_parameters');
    $htac .= "\n\n";

    #now, for pid pages, rewrite to /name-p-pid-i-ppinc
    $htac .= '############  P_IDs  ############ ';
    $htac .= "\n\n";
    $htac .= '# pid - name - ppinc';
    $htac .= "\n";
    $htac .=
'RewriteCond %{QUERY_STRING} ^p_id=([\w\d\._\-]*).*&amp;name=(.*)&amp;ppinc=([\w\d_]+).*$'
      . "\n"
      . 'RewriteRule ^(.*)agora\.cgi$ '
      . $storepath
      . '%2-p-%1-i-%3? [R=301,L]' . "\n"
      . 'RewriteRule ^/?(.*)-p-([\w\d\._\-]*)-i-([\w\d_]*)$ agora.cgi?p_id=$2&ppinc=$3&xm=on [L]'
      . "\n" . "\n"
      . '# pid - ppic' . "\n"
      . 'RewriteCond %{QUERY_STRING} ^p_id=([\w\d\._\-]*).*&amp;ppinc=([\w\d_]+).*$'
      . "\n"
      . 'RewriteRule ^(.*)agora\.cgi$ '
      . $storepath
      . 'p-%1-i-%2? [R=301,L]' . "\n"
      . 'RewriteRule ^/?p-([\w\d\._\-]*)-i-([\w\d_]*)$ agora.cgi?p_id=$1&ppinc=$2&xm=on [L]'
      . "\n" . "\n"
      . '# pid - name' . "\n"
      . 'RewriteCond %{QUERY_STRING} ^p_id=([\w\d\._\-]*).*&amp;name=([\w\d_\&\-\,\.]+).*$'
      . "\n"
      . 'RewriteRule ^(.*)agora\.cgi$ '
      . $storepath
      . '%2-p-%1? [R=301,L]' . "\n"
      . 'RewriteRule ^/?(.*)-p-([\w\d\._\-]*)$ agora.cgi?p_id=$2&name=$1&xm=on [L]'
      . "\n" . "\n"
      . '# pid only' . "\n"
      . 'RewriteCond %{QUERY_STRING} ^p_id=([\w\d\._\-]*)$' . "\n"
      . 'RewriteRule ^(.*)agora\.cgi$ '
      . $storepath
      . 'p-%1? [R=301,L]' . "\n"
      . 'RewriteRule ^/?p-([\w\d\._\-]*)$ agora.cgi?p_id=$1&xm=on [L]'
      . "\n\n\n"
      . '############  CATEGORIES  ############ '
      . "\n\n\n";

    #now the main beast - category rewrites.  Run thru array of categories
    #build the queries we want to rewrite.
    foreach $catlev (@catlevels) {
        if ( $catlev ne '' ) {
            $ref = $count + 1;
            if ( $ref > 1 ) {
                $catlev_holder .= " - subcat level$ref ";
            }
            push( @levs, $ref );
            $stuffs  = scalar(@levs);
            $stuffs2 = $stuffs + 1;
            if ( $count > 0 ) {
                $myquery .= '&amp;' . $catlev . '=([\w\d_\-\&]*)';
                $myali   .= '&' . $catlev . '=$' . $ref;
            }
            else {
                $myquery .= $catlev . '=([\w\d_\-\&]*)';
                $myali .= $catlev . '=$' . $ref;
            }
            $newpath = q{};
            foreach $refe (@levs) {
                if ( $refe ne '' ) { $newpath .= '%' . $refe; }
                $newpath .= $sep;
            }
            chop $newpath;
            $newpath =~ s/^-//g;
            $newpath =~ s/^,//g;
###if I have ppinc AND next############################################
            $my301 = "# product  $catlev_holder- next - ppinc" . "\n";
            $my301 .=
                'RewriteCond %{QUERY_STRING} ^'
              . $myquery
              . '&amp;next=([\d]+)'
              . '&amp;ppinc=([\w\d_]+)$' . "\n";
            $my301 .=
                'RewriteRule ^(.*)agora\.cgi$ '
              . $storepath
              . $newpath . '-n%'
              . $stuffs2 . '-i-%'
              . $stuffs
              . '? [R=301,L]' . "\n";
            $htac .= $my301;
############ 301 is done, now alias back to cart page
            $myalias = 'RewriteRule ^/?';
            my $inncount = '0';

            while ( $inncount <= $count ) {
                if ( $inncount == $count ) {
                    if ( $count == 0 ) {
                        $myalias .= '([\w\d_\-\&]*),';
                    }
                    else {
                        $myalias .= '([\w\d_\-\&]*)-';
                    }
                }
                elsif ( $inncount < $count ) {
                    $myalias .= '([\w\d_\-\&]*),';
                }
                else {
                    $myalias .= '([\w\d_\-\&]*)-';
                }
                $inncount++;
            }
            chop $myalias;
            $myalias .= '-n([\d]*)-i-([\w\d_]*)';
            my $matchno  = $inncount + 1;
            my $matchno2 = $matchno + 1;
            $myalias .=
                '$ agora.cgi?'
              . $myali
              . '&next=$'
              . $matchno2
              . '&ppinc=$'
              . $matchno
              . '&xm=on [L]' . "\n\n";
            $htac .= $myalias;
###if I have just ppinc#################################################
            $my301 = "# product  $catlev_holder- ppinc" . "\n";
            $my301 .=
                'RewriteCond %{QUERY_STRING} ^'
              . $myquery
              . '&amp;ppinc=([\w\d_]*).*$' . "\n";
            $my301 .=
                'RewriteRule ^(.*)agora\.cgi$ '
              . $storepath
              . $newpath . '-i-%'
              . $stuffs
              . '? [R=301,L]' . "\n";
            $htac .= $my301;
############ 301 is done, now alias back to cart page.
            $myalias  = 'RewriteRule ^/?';
            $inncount = "0";

            while ( $inncount <= $count ) {
                if ( $inncount == $count ) {
                    if ( $count == 0 ) {
                        $myalias .= '([\w\d_\-\&]*),';
                    }
                    else {
                        $myalias .= '([\w\d_\-\&]*)-';
                    }
                }
                elsif ( $inncount < $count ) {
                    $myalias .= '([\w\d_\-\&]*),';
                }
                else {
                    $myalias .= '([\w\d_\-\&]*)-';
                }
                $inncount++;
            }
            chop $myalias;
            $myalias .= '-i-([\w\d_]*)';
            $matchno = $inncount + 1;
            $myalias .=
                '$ agora.cgi?'
              . $myali
              . '&ppinc=$'
              . $matchno
              . '&xm=on [L]' . "\n\n";
            $htac .= $myalias;
###if I have just next###########################################################
            $my301 = "# product  $catlev_holder- next" . "\n";
            $my301 .=
                'RewriteCond %{QUERY_STRING} ^'
              . $myquery
              . '&amp;next=([\d]*)$' . "\n";
            $my301 .=
                'RewriteRule ^(.*)agora\.cgi$ '
              . $storepath
              . $newpath . '-n%'
              . $stuffs
              . '? [R=301,L]' . "\n";
            $htac .= $my301;
############ 301 is done, now alias back to cart page
            $myalias  = 'RewriteRule ^/?';
            $inncount = "0";

            while ( $inncount <= $count ) {
                if ( $inncount == $count ) {
                    if ( $count == 0 ) {
                        $myalias .= '([\w\d_\-\&]*),';
                    }
                    else {
                        $myalias .= '([\w\d_\-\&]*)-';
                    }
                }
                elsif ( $inncount < $count ) {
                    $myalias .= '([\w\d_\-\&]*),';
                }
                else {
                    $myalias .= '([\w\d_\-\&]*)-';
                }
                $inncount++;
            }
            chop $myalias;
            $myalias .= '-n([\d]*)';
            my $matchno = $inncount + 1;
            $myalias .=
                '$ agora.cgi?'
              . $myali
              . '&next=$'
              . $matchno
              . '&xm=on [L]' . "\n\n";
            $htac .= $myalias;
### 301 for just cat params.#########################################
            $my301 = "# product  $catlev_holder" . "\n";
            $my301 .= 'RewriteCond %{QUERY_STRING} ^' . $myquery . '$' . "\n";
            $my301 .= 'RewriteRule ^(.*)agora\.cgi$ ' . $storepath;
            $my301 .= $newpath . '? [R=301,L]' . "\n";
            $htac  .= $my301;
############ 301 is done for just cat params, now alias back to cart page
            $myalias = 'RewriteRule ^/?';
            my $inncount = "0";

            while ( $inncount <= $count ) {
                if ( $inncount == $count ) {
                    if ( $count == 0 ) {
                        $myalias .= '([\w\d_\-\&]*),';
                    }
                    else {
                        $myalias .= '([\w\d_\-\&]*)-';
                    }
                }
                elsif ( $inncount < $count ) {
                    $myalias .= '([\w\d_\-\&]*),';
                }
                else {
                    $myalias .= '([\w\d_\-\&]*)-';
                }
                $inncount++;
            }
            chop $myalias;
            $myalias .= '$ agora.cgi?' . $myali . '&xm=on [L]' . "\n\n";
            $htac .= $myalias;
            $count++;
        } # end if ( $catlev ne '' )
    } # end foreach @catlevels
    $htac .= "\n" . $tempLine03 . "\n";
    $my_saveumask = umask $original_umask;

    # fix this on some servers. Mister Ed June 26, 2006
    if ( $my_saveumask =~ /18|63/ ) {
        umask 0022;
    }

    # checks for existing SEO rules so that it's not a total rewrite of file.
    if ( -e "./.htaccess" ) {
        open( HTACCFILE, "./.htaccess" );
        while (<HTACCFILE>) {
            if ( $_ =~ /$tempLine04/ ) {
                $mc_rewrite_found = 1;
            }
            if ( ( $_ =~ /$tempLine01/ ) || ( $_ =~ /$tempLine02/ ) ) {
                $startLine = 'yes';
            }
            elsif ( $startLine eq 'yes' ) {
                if ( $_ =~ /$tempLine03/ ) {
                    $startLine = 'done';
                }

                # ignore old SEO lines by not writing them
            }
            elsif ( $startLine eq 'no' ) {
                $htacStart .= $_;
            }
            elsif ( $startLine eq 'done' ) {
                $htacEnd .= $_;
            }
        }
        close(HTACCFILE);
        if ($mc_rewrite_found) {
            $htac = $htacStart . $htac . $htacEnd;
        }
        else {
            $htac = $htacOn . $htacStart . $htac . $htacEnd;
        }
    }
    else {
        $htac = $htacOn . $htac;
    }
    #if ( $in{'seo_friendly'} !~ /yes/i ) {
    #    $htac = $htacStart . $htacEnd;
    #}
    open( HTACC, ">./.htaccess" ) || print 'yikes';
    print HTACC $htac;
    close(HTACC);
    umask $my_saveumask;

    #}
    ( $sc_seo_index, $sc_seo_index_disp ) = split( /-/, $in{'seoindex'} );
    $myset .= "\$sc_seo_index = \"$sc_seo_index\";\n";
    $myset .= "\$sc_seo_index_disp = \"$sc_seo_index_disp\";\n";
    $myset .= "\$sc_pid_title = \"$in{'pid_title'}\";\n";
    $myset .= "\$sc_cat_title = \"$in{'cat_title'}\";\n";
    $myset .= "\$sc_seo_friendly = \"$in{'seo_friendly'}\";\n";
    $myset .= "\$sc_catlev1 = \"$in{'catlev1'}\";\n";
    $myset .= "\$sc_catlev2 = \"$in{'catlev2'}\";\n";
    $myset .= "\$sc_catlev3 = \"$in{'catlev3'}\";\n";
    $myset .= "\$sc_catlev4 = \"$in{'catlev4'}\";\n";
    $myset .=
      "\$sc_subcatinprodheaders = \"$in{'sc_subcatinprodheaders'}\";\n";
    $sc_pid_title                     = $in{'pid_title'};
    $sc_cat_title                     = $in{'cat_title'};
    $sc_seo_friendly                  = $in{'seo_friendly'};
    $sc_catlev1                       = $in{'catlev1'};
    $sc_catlev2                       = $in{'catlev2'};
    $sc_catlev3                       = $in{'catlev3'};
    $sc_catlev4                       = $in{'catlev4'};
    $sc_seo_friendly_cats_have_dashes = $in{'seo_friendly_dashes'};
    $sc_subcatinprodheaders           = $in{'sc_subcatinprodheaders'};
    update_store_settings( 'SEOUPDATEMANAGER', $myset );
    category_list_builder_for_store();
    display_seo_settings_screen();
}

#######################################################################

1;
