46 lines
2.0 KiB
PHP
Executable File
46 lines
2.0 KiB
PHP
Executable File
<?php
|
|
header('Content-Type: application/json');
|
|
|
|
$action = $_POST['action'] ?? $_GET['action'] ?? '';
|
|
$params = json_decode($_POST['params'] ?? $_GET['params'] ?? '{}', true);
|
|
|
|
$result = ['success' => false];
|
|
|
|
switch($action) {
|
|
case 'web_search':
|
|
$q = urlencode($params['query'] ?? '');
|
|
$data = @json_decode(file_get_contents("https://api.duckduckgo.com/?q=$q&format=json&no_html=1"), true);
|
|
$result = ['success' => true, 'data' => ['text' => $data['AbstractText'] ?? '', 'url' => $data['AbstractURL'] ?? '']];
|
|
break;
|
|
|
|
case 'calculate':
|
|
$expr = preg_replace('/[^0-9+\-*\/().% ]/', '', $params['expression'] ?? '');
|
|
$result = ['success' => true, 'data' => ['result' => @eval("return $expr;"), 'expr' => $expr]];
|
|
break;
|
|
|
|
case 'datetime':
|
|
$dt = new DateTime('now', new DateTimeZone('Europe/Paris'));
|
|
$result = ['success' => true, 'data' => ['date' => $dt->format('d/m/Y'), 'time' => $dt->format('H:i'), 'day' => $dt->format('l')]];
|
|
break;
|
|
|
|
case 'weather':
|
|
$city = urlencode($params['city'] ?? 'Paris');
|
|
$geo = @json_decode(file_get_contents("https://geocoding-api.open-meteo.com/v1/search?name=$city&count=1"), true);
|
|
if (!empty($geo['results'])) {
|
|
$lat = $geo['results'][0]['latitude']; $lon = $geo['results'][0]['longitude'];
|
|
$w = @json_decode(file_get_contents("https://api.open-meteo.com/v1/forecast?latitude=$lat&longitude=$lon¤t_weather=true"), true);
|
|
$result = ['success' => true, 'data' => ['city' => $params['city'], 'temp' => ($w['current_weather']['temperature'] ?? '?').'°C']];
|
|
}
|
|
break;
|
|
|
|
case 'list':
|
|
$result = ['success' => true, 'data' => ['tools' => ['web_search','calculate','datetime','weather']]];
|
|
break;
|
|
|
|
default:
|
|
$result = ['error' => 'Unknown action', 'available' => ['web_search','calculate','datetime','weather','list']];
|
|
}
|
|
|
|
echo json_encode($result);
|
|
|