* fix error notification displayed 3 times when no place search results

This commit is contained in:
Gudule Lapointe
2025-03-14 03:42:53 +01:00
parent 1a8478d8c2
commit dec05dfb65
3 changed files with 170 additions and 4 deletions
+42 -4
View File
@@ -378,19 +378,41 @@ function oxXmlRequest( $gatekeeper, $method, $request ) {
}
function osXmlResponse( $success = true, $errorMessage = false, $data = false ) {
global $request_key;
// Data given, output as xmlrpc
if ( is_array( $data ) ) {
if( ! $success && ! empty( $errorMessage ) ) {
# Avoid duplicate error messages
# TODO: improve to make sure we don't cache error messages for different requests
# (currently $request_key only identifies search arguments, client IP and gateway url,
# but client IP is the simulator, not the actual user, so in theory, the key could
# be identical for two simultaneous requests by different users in the same region,
# although this is unlikely to happen)
$tmp_dir = get_writable_tmp_dir();
$cache = $tmp_dir . '/cache-' . $request_key;
# Check if file exists and is not older than 5 seconds
if ( file_exists( $cache ) && ( time() - filemtime( $cache ) < 1.5 ) ) {
$errorMessage = '';
} else {
file_put_contents( $cache, $errorMessage );
}
}
$array = array(
'success' => $success,
'errorMessage' => $errorMessage,
'data' => $data,
);
if ( ! empty( $data ) ) {
$array['data'] = $data;
}
array_filter( $array );
$response_xml = xmlrpc_encode( $array );
echo $response_xml;
return;
}
// No data given, output simple boolean or error message, no change here
if ( $success ) {
$answer = new SimpleXMLElement( '<boolean>true</boolean>' );
} else {
@@ -517,6 +539,22 @@ function set_helpers_locale( $locale = null, $domain = 'messages' ) {
textdomain( $domain );
}
function get_writable_tmp_dir() {
if(isset($_GLOBALS['tmp_dir'])) {
return $_GLOBALS['tmp_dir'];
}
$dirs = array( sys_get_temp_dir(), ini_get('upload_tmp_dir'), '/tmp', '/var/tmp', '/usr/tmp', '.' );
foreach ( $dirs as $dir ) {
if ( @is_writable( $dir ) ) {
$_GLOBALS['tmp_dir'] = $dir;
return $dir;
}
}
error_log( __FILE__ . ':' . __LINE__ . ' ERROR - could not find a writable temporary directory, check web server and PHP config' );
return false;
// return '/tmp';
}
if( ! defined( 'NULL_KEY') ) define( 'NULL_KEY', '00000000-0000-0000-0000-000000000000' );
if( ! defined( 'TPLINK_LOCAL') ) {
define( 'TPLINK_LOCAL', 1 ); // seconlife://Region/x/y/z
+25
View File
@@ -629,10 +629,35 @@ function classifieds_info_query( $method_name, $params, $app_data ) {
osXmlResponse( true, '', $data );
}
$tmp_dir = get_writable_tmp_dir();
//
// Process the request
//
$request_xml = file_get_contents( 'php://input' );
// error_log( __FILE__ . ':' . __LINE__ . ' request xml ' . $request_xml );
if( ! $request_xml ) {
// Wrong request, probably called directly instead of by sending xml request,
// or no data sent, issue proper http response and error message, then die
header( 'HTTP/1.1 400 Bad Request' );
header( 'Content-Type: text/plain' );
die( '400 Bad Request' . PHP_EOL );
}
$request_array = array(
$_SERVER['REMOTE_ADDR'],
$_SERVER['REQUEST_URI'],
$request_xml
);
$request_key = md5( serialize( $request_array ) );
// error_log( __FILE__ . ':' . __LINE__
// . PHP_EOL . 'request xml ' . $request_xml
// . PHP_EOL . 'client ip ' . $_SERVER['REMOTE_ADDR']
// . PHP_EOL . 'request uri ' . $_SERVER['REQUEST_URI'] );
xmlrpc_server_call_method( $xmlrpc_server, $request_xml, '' );
xmlrpc_server_destroy( $xmlrpc_server );
Executable
+103
View File
@@ -0,0 +1,103 @@
#!/bin/bash
SearchURL="https://2do.directory/helpers/query.php"
GatekeeperURL="http://speculoos.world:8002"
PGM=$(basename $0)
searchstring="$@"
echo "Searching for: $searchstring" > /dev/stderr
url=$SearchURL?gk=$GatekeeperURL
echo "SearchURL: $url" > /dev/stderr
xml="<?xml version=\"1.0\" encoding=\"utf-8\"?>
<methodCall>
<methodName>dir_places_query</methodName>
<params>
<param>
<value>
<struct>
<member>
<name>category</name>
<value><string>-1</string></value>
</member>
<member>
<name>text</name>
<value><string>$searchstring</string></value>
</member>
<member>
<name>flags</name>
<value><string>117441536</string></value>
</member>
<member>
<name>query_start</name>
<value><string>0</string></value>
</member>
<member>
<name>sim_name</name>
<value><string/></value>
</member>
</struct>
</value>
</param>
</params>
</methodCall>"
# Sample responses:
# Search with no results:
# <?xml version="1.0" encoding="utf-8"?>
# <params><param><value><struct>
# <member>
# <name>success</name>
# <value>
# <boolean>0</boolean>
# </value>
# </member>
# <member>
# <name>errorMessage</name>
# <value>
# <string>No results</string>
# </value>
# </member>
# </struct></value></param></params>
#
# Search with results:
# <?xml version="1.0" encoding="utf-8"?>
# <params><param><value><struct>
# <member>
# <name>success</name>
# <value>
# <boolean>1</boolean>
# </value>
# </member>
# <member>
# <name>errorMessage</name>
# <value>
# <string/>
# </value>
# </member>
# <member>
# <name>data</name>
# (etc)
# </struct></value></param></params>
# Send POST request with the XML content so that query.php receives it via php://input
response=$(curl -s -X POST -H "Content-Type: text/xml" --data "$xml" "$url")
# Remove extra spaces between tags without eliminating newlines entirely
response_oneline="$(echo "$response" | sed ':a;N;$!ba;s/>\s\+</></g')"
# Extract success and errorMessage using sed
success=$(echo "$response_oneline" | sed -n 's/.*<name>success<\/name>.*<boolean>\([01]\)<\/boolean>.*/\1/p')
errorMessage=$(echo "$response_oneline" | sed -n 's/.*<name>errorMessage<\/name>.*<string>\(.*\)<\/string>.*/\1/p')
# If errorMessage is "False", change it to empty string.
if [ "$errorMessage" = "False" ]; then
errorMessage=""
fi
echo "$PGM: Success=$success, Error Message='$errorMessage'"