TreasureChest handles AJAX requests?!
TreasureChest performs several Ajax requests though "treasurechest.js".
This file is automatically added by the server component of TreasureChest by using the MODx API to register treasurechest.js as startup script.
The Ajax requests are performed trough the jQuery library, which is itself included in treasurechest.js. (Of course it can be removed from the file if you import it another way.)
Many Ajax requests were already present in TreasureChest 1.0. They were used to initialize the cart, to build the cart view in the thickbox, to remove one product from the cart view, to handle changes in product options, to ask if the "View cart" and "Empty cart" buttons should be visible or not, a.s.o. Most Ajax requests were performed through the $.post() jQuery command which is a shortcut for $.ajax().
With TreasureChest 1.1 and upper, most commands were switched from $.post() to $.ajax() as $.post() presented two major draw major drawbacksgs embedded carts. Firstly, it $.post() runs Ajax requests asynchroneously, which is problematic in certain situations. Secondly, it caches the returned content and this was also problematic.
TreasureChest 1.1 and 1.2 also added new Ajax requests, especially for the in-page embedded cart and the button "-1" (i.e. "Remove one").
If you’re interested to see what TreasureChest 1.2 can do, here are several screenshots. The tif and png images are the same, but the png ones are faster to load.
http://www.altipoint.ch/preview/copies_ecran_treasurechest/
I would assume you would have to create your own AJAX-handling snippet. Did you make sure the Content Type was correct? And that there is no template corrupting whatever AJAX results you are attempting to return?
Yes, absolutely, the content is correct. The best proof if that the returned content is OK when I’m logged into the MODx manager. It was also easy to check it with an "alert();" displaying the data returned. Lasty, I’ve configured Firebug to show Ajax things in its "Console" tab. So, I can easily check the returned data.
The problem is server side. I’m absolutely sure about it. I observed it by making my script write a file on server side. In that file, it writes the data that should be returned, just before the json_encode() PHP function is called.
I’ll tell at the end of this message what’s going wrong when I’m not connected to MODx.
IMO, you should never ever create AJAX requests as external files that way; you are asking for problems, including the potential to create an attack vector into your MODx system.
Do you mean that I should not use an external PHP file to handle the Ajax request and put all code in the snippet? I totally agree, but for debugging purposes, it can be useful to isolate things temporarily outside of MODx.
As for TreasureChest, I am not familiar, so afraid I’m not going to be any help on specifics there.
I understand, but the problem is not specific to TreasureChest.
Now, what I observed is that the records of the database table are read several times.
After the table is read, something loops and the records are read again and again.
This leads to a buffer overflow.
For the debugging, I exported the content of the problematic function in a distinct PHP file and added the necessary files for the API to work. Here’s this PHP file (with modified credentials!).
<?php
define('MODX_MANAGER_PATH', '../../../manager/'); //relative path for manager folder
require_once(MODX_MANAGER_PATH . 'includes/config.inc.php');//config
// Setup the MODx API
define('MODX_API_MODE', true);
// initiate a new document parser (maybe not necessary)
include_once(MODX_MANAGER_PATH.'/includes/document.parser.class.inc.php');
$modx = new DocumentParser;
$database_server = 'localhost';
$database_name = 'serveraccount_modxsite';
$database_user = 'serveraccount_thedbaseuser';
$database_password = 'somepassword';
$modx->db->connect($database_server, $database_name, $database_user, $database_password, true);
// Return a PHP associative key-value array built from the data in the general TreasureChest Configuration settings
// Retrieve the full table of general TreasureChest settings.
$config_table = $modx->getFullTableName('treasure_chest_config'); //Add prefix
$config = $modx->db->query("SELECT * FROM ".$config_table); // For simplicity :-)
$config_array = array();
$simpleOutput = '';
while ($row = $modx->db->getRow($config, 'assoc'))
{
// The table column for keys :
// some values of the "setting_name" column have a "store_" prefix. We remove it.
$key = str_replace('store_', '', $row['setting_name']);
// The table column for values: decode values.
$val = htmlspecialchars_decode($row['setting_value']);
// Sets the php "$config_array[]" associative array from the table.
$config_array[$key] = $val;
$toto .= $toto.$key.$val;
}
$modx->db->disconnect();
// $json_string = json_encode($config_array); ' fails
echo $simpleOutput;
?>
Looks like the while() starts again at top of table when the user is not logged in the MODx manager.