Zen Cart Logo
Forums / Features Wish List / Page not found if invalid cpath entered.

Page not found if invalid cpath entered.

Views: 95

Results 1 to 6 of 6
1 Jul 2020, 1:25 PM
#1
brittainmark avatar

brittainmark

Totally Zenned

Join Date:
Apr 2009
Posts:
507
Plugin Contributions:
1

Page not found if invalid cpath entered.

1.5.7 -
This has always been the case. if you enter and invalid &cpath zen cart displays a blank page with the message "There are no products to list in this category."
It would be more correct to display page not found.
This also stops google trying to index old invalid pages.

I have made a small change to init_sanitize.php on my server that does the job.
If you thought it is a good idea and it is the correct module to modify I would be happy to try and adjust the base code via PR.
I have never used git-hub so might need some help getting going but willing to give it a go.
mean while I will read throught the zen help on git hub to start my learning.

1 Jul 2020, 4:32 PM
#2
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: Page not found if invalid cpath entered.

What if the storeowner has run out of stock of the products in that category and has products set to be disabled (disappear) when out of stock? That doesn't necessarily mean the cPath is invalid and should be de-listed.

1 Jul 2020, 4:53 PM
#3
brittainmark avatar

brittainmark

Totally Zenned

Join Date:
Apr 2009
Posts:
507
Plugin Contributions:
1

Re: Page not found if invalid cpath entered.

No I agree. The issue is when the cpath is invalid. if there is no stock then i think the message is appropriate. I have this problem because people link in to my shop. and if i change a category and it disappears then it displays no products found on an invalid catategory. If i have no stock for that category then it displays the category with the message saying in my case awaiting stock. I might just be an oddball having invalid categories.
This is what I added/changed from line 108 v1.5.7 about line

/**
 * We do some checks here to ensure $_GET['main_page'] has a sane value
 */
  $mjfbCheck=True;
  if (empty($_GET['main_page'])) $_GET['main_page'] = 'index';

  if (!is_dir(DIR_WS_MODULES .  'pages/' . $_GET['main_page'])) {
    if (MISSING_PAGE_CHECK == 'On' || MISSING_PAGE_CHECK == 'true') {
      $_GET['main_page'] = 'index';
      $mjfbCheck=False;
    } elseif (MISSING_PAGE_CHECK == 'Page Not Found') {
      header('HTTP/1.1 404 Not Found');
      $_GET['main_page'] = FILENAME_PAGE_NOT_FOUND;
      $mjfbCheck=False;
    }

  }
  //mjfb start Check the category path is valid
  if (isset($_GET['cPath']) && $mjfbCheck) {
  	$cPath = $_GET['cPath'];
  	if (zen_not_null($cPath)) {
  		$cPath_array = zen_parse_category_path($cPath);
  		$cPath = implode('_', $cPath_array);
  		$current_category_id = $cPath_array[(sizeof($cPath_array)-1)];
  	} else {
  		$current_category_id = 0;
  	}
  	$mjfb_category_exists_query =  "SELECT count(*) AS total
                             FROM   " . TABLE_CATEGORIES . "
                             WHERE   categories_id = :categoriesID";
  	$mjfb_category_exists_query = $db->bindVars($mjfb_category_exists_query, ':categoriesID', $current_category_id, 'integer');
  	$mjfb_category_exists = $db->Execute($mjfb_category_exists_query);
  	if ($mjfb_category_exists->fields['total'] == 0) {
  		if (MISSING_PAGE_CHECK == 'On' || MISSING_PAGE_CHECK == 'true') {
  			$_GET['main_page'] = 'index';
  		} elseif (MISSING_PAGE_CHECK == 'Page Not Found') {
  			header('HTTP/1.1 404 Not Found');
  			$_GET['main_page'] = FILENAME_PAGE_NOT_FOUND;
  		}
  	}
  }
  //mjfb end

So I only return page not found for an invalid cpath.
What do you think

1 Jul 2020, 11:41 PM
#4
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: Page not found if invalid cpath entered.

Given the duplication of MISSING_PAGE_CHECK code in your example I don't think I'd do it that way in official Zen Cart core code.

As for your own site, no problem.
You might consider ignoring the MISSING_PAGE_CHECK and just send a 404 Not Found or a 410 Gone header.
Another consideration: your count(*) could be minorly sped up by selecting a specific field and then adding LIMIT 1 to your query. That way it brings back the first found record instead of finding all, counting, and sending back the count. Then just check for ->EOF or ->RecordCount > 0

26 Jul 2020, 4:37 PM
#5
brittainmark avatar

brittainmark

Totally Zenned

Join Date:
Apr 2009
Posts:
507
Plugin Contributions:
1

Re: Page not found if invalid cpath entered.

Thanks for your input I have recoded it and moved it Further up in the module. Now fits in about line 108. New code.```php
//mjfb start Check the category path is valid
if (isset($_GET['cPath']) && !isset($_GET['products_id'])) {
$cPath = $GET['cPath'];
if (zen_not_null($cPath)) {
$cPath_array = zen_parse_category_path($cPath);
$cPath = implode('
', $cPath_array);
$current_category_id = $cPath_array[(sizeof($cPath_array)-1)];
$mjfb_category_exists_query = 'SELECT count(*) AS total FROM ' . TABLE_CATEGORIES . '
WHERE categories_id = :categoriesID LIMIT 1;';
$mjfb_category_exists_query = $db->bindVars($mjfb_category_exists_query, ':categoriesID', $current_category_id, 'integer');
$mjfb_category_exists = $db->Execute($mjfb_category_exists_query);
if ($mjfb_category_exists->fields['total'] == 0) {
header('HTTP/1.1 404 Not Found');
$_GET['main_page'] = FILENAME_PAGE_NOT_FOUND;
}
} else {
header('HTTP/1.1 404 Not Found');
$_GET['main_page'] = FILENAME_PAGE_NOT_FOUND;
}
}
//mjfb end

Also changed it so that it does not check the cpath if there is a product id present.
26 Jul 2020, 6:04 PM
#6
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
177

Re: Page not found if invalid cpath entered.

You could simplify the db query even further by not doing a count(*) but rather just select categories_id, keep the where and limit clauses, and then instead of checking for 'total' == 0, check for RecordCount() or EOF.
Slightly faster db query, especially if you actually do have a lot of categories in the db.