feat: Add API functions for product management and validation, including JSON response handling

This commit is contained in:
2026-03-08 16:39:49 +01:00
parent f39f216409
commit 17a9665cf2
3 changed files with 482 additions and 33 deletions

225
api.php
View File

@@ -37,6 +37,52 @@ $mdb = new medoo( [
return R::getRedBean() -> dispense( $type );
} );
function api_json_response( $data, $http_code = 200 )
{
http_response_code( (int) $http_code );
echo json_encode( $data );
exit;
}
function api_validate_api_key( $mdb )
{
$api_key = trim( (string) \S::get( 'api_key' ) );
$stored_key = trim( (string) $mdb -> get( 'settings', 'setting_value', [ 'setting_key' => 'api_key' ] ) );
if ( $api_key === '' || $stored_key === '' || !hash_equals( $stored_key, $api_key ) )
{
api_json_response( [ 'result' => 'error', 'message' => 'Invalid api_key' ], 401 );
}
}
function api_get_product_by_offer_and_client( $mdb, $offer_id, $client_id )
{
return $mdb -> query(
'SELECT p.id, p.name, p.title, p.google_product_category
FROM products p
JOIN clients cl ON p.client_id = cl.id
WHERE p.offer_id = :offer_id
AND cl.id = :client_id
LIMIT 1',
[
':offer_id' => (string) $offer_id,
':client_id' => (int) $client_id
]
) -> fetch( \PDO::FETCH_ASSOC );
}
function api_normalize_product_text( $value )
{
$value = trim( (string) $value );
if ( $value === '' )
{
return null;
}
return $value;
}
// dodawanie domeny przez API
if ( \S::get( 'action' ) == 'domain_tester_add' )
{
@@ -124,14 +170,7 @@ if ( \S::get( 'action' ) == 'campaign_comment_add' )
// Zmiana custom_label_4 dla produktu przez API
if ( \S::get( 'action' ) == 'product_custom_label_4_set' )
{
$api_key = trim( \S::get( 'api_key' ) );
$stored_key = $mdb -> get( 'settings', 'setting_value', [ 'setting_key' => 'api_key' ] );
if ( !$api_key || !$stored_key || $api_key !== $stored_key )
{
echo json_encode( [ 'result' => 'error', 'message' => 'Invalid api_key' ] );
exit;
}
api_validate_api_key( $mdb );
$offer_id = trim( \S::get( 'offer_id' ) );
$client_id_param = trim( \S::get( 'client_id' ) );
@@ -139,34 +178,166 @@ if ( \S::get( 'action' ) == 'product_custom_label_4_set' )
if ( !$offer_id || !$client_id_param )
{
echo json_encode( [ 'result' => 'error', 'message' => 'Missing required params: offer_id, client_id' ] );
exit;
api_json_response( [ 'result' => 'error', 'message' => 'Missing required params: offer_id, client_id' ], 422 );
}
$product = $mdb -> query(
'SELECT p.id
FROM products p
JOIN clients cl ON p.client_id = cl.id
WHERE p.offer_id = :offer_id
AND cl.id = :client_id
LIMIT 1',
[
':offer_id' => $offer_id,
':client_id' => (int) $client_id_param
]
) -> fetch( \PDO::FETCH_ASSOC );
$product = api_get_product_by_offer_and_client( $mdb, $offer_id, (int) $client_id_param );
if ( !$product )
{
echo json_encode( [ 'result' => 'error', 'message' => 'Product not found' ] );
exit;
api_json_response( [ 'result' => 'error', 'message' => 'Product not found' ], 404 );
}
\factory\Products::set_product_data( $product['id'], 'custom_label_4', $custom_label_4 );
\factory\Products::add_product_comment( $product['id'], 'Zmiana etykiety 4 na: ' . $custom_label_4 . ' (API)' );
echo json_encode( [ 'result' => 'ok' ] );
exit;
api_json_response( [ 'result' => 'ok' ] );
}
// Zmiana tytulu produktu przez API
if ( \S::get( 'action' ) == 'product_title_set' )
{
api_validate_api_key( $mdb );
$offer_id = trim( (string) \S::get( 'offer_id' ) );
$client_id_param = (int) \S::get( 'client_id' );
$new_title = api_normalize_product_text( \S::get( 'title' ) );
if ( $offer_id === '' || $client_id_param <= 0 )
{
api_json_response( [ 'result' => 'error', 'message' => 'Missing required params: offer_id, client_id' ], 422 );
}
$product = api_get_product_by_offer_and_client( $mdb, $offer_id, $client_id_param );
if ( !$product )
{
api_json_response( [ 'result' => 'error', 'message' => 'Product not found' ], 404 );
}
$old_title = (string) ( $product['title'] ?? '' );
\factory\Products::set_product_data( (int) $product['id'], 'title', $new_title );
$old_title_for_log = trim( $old_title ) !== '' ? $old_title : '[pusty]';
$new_title_for_log = $new_title !== null ? $new_title : '[pusty]';
\factory\Products::add_product_comment(
(int) $product['id'],
'Zmiana tytulu przez API: ' . $old_title_for_log . ' -> ' . $new_title_for_log
);
api_json_response( [
'result' => 'ok',
'product_id' => (int) $product['id'],
'offer_id' => $offer_id,
'client_id' => $client_id_param,
'title' => $new_title
] );
}
// Sprawdzenie, czy tytul produktu byl juz zmieniony
if ( \S::get( 'action' ) == 'product_title_changed_check' )
{
api_validate_api_key( $mdb );
$offer_id = trim( (string) \S::get( 'offer_id' ) );
$client_id_param = (int) \S::get( 'client_id' );
if ( $offer_id === '' || $client_id_param <= 0 )
{
api_json_response( [ 'result' => 'error', 'message' => 'Missing required params: offer_id, client_id' ], 422 );
}
$product = api_get_product_by_offer_and_client( $mdb, $offer_id, $client_id_param );
if ( !$product )
{
api_json_response( [ 'result' => 'error', 'message' => 'Product not found' ], 404 );
}
$base_name = trim( (string) ( $product['name'] ?? '' ) );
$custom_title = trim( (string) ( $product['title'] ?? '' ) );
$is_changed = $custom_title !== '' && $custom_title !== $base_name;
api_json_response( [
'result' => 'ok',
'product_id' => (int) $product['id'],
'offer_id' => $offer_id,
'client_id' => $client_id_param,
'title_changed' => $is_changed,
'default_name' => $base_name,
'custom_title' => $custom_title !== '' ? $custom_title : null
] );
}
// Zmiana Google Product Category przez API
if ( \S::get( 'action' ) == 'product_google_category_set' )
{
api_validate_api_key( $mdb );
$offer_id = trim( (string) \S::get( 'offer_id' ) );
$client_id_param = (int) \S::get( 'client_id' );
$google_category = api_normalize_product_text( \S::get( 'google_product_category' ) );
if ( $offer_id === '' || $client_id_param <= 0 )
{
api_json_response( [ 'result' => 'error', 'message' => 'Missing required params: offer_id, client_id' ], 422 );
}
$product = api_get_product_by_offer_and_client( $mdb, $offer_id, $client_id_param );
if ( !$product )
{
api_json_response( [ 'result' => 'error', 'message' => 'Product not found' ], 404 );
}
$old_category = (string) ( $product['google_product_category'] ?? '' );
\factory\Products::set_product_data( (int) $product['id'], 'google_product_category', $google_category );
$old_category_for_log = trim( $old_category ) !== '' ? $old_category : '[pusty]';
$new_category_for_log = $google_category !== null ? $google_category : '[pusty]';
\factory\Products::add_product_comment(
(int) $product['id'],
'Zmiana Google Product Category przez API: ' . $old_category_for_log . ' -> ' . $new_category_for_log
);
api_json_response( [
'result' => 'ok',
'product_id' => (int) $product['id'],
'offer_id' => $offer_id,
'client_id' => $client_id_param,
'google_product_category' => $google_category
] );
}
// Odczyt Google Product Category przez API
if ( \S::get( 'action' ) == 'product_google_category_get' )
{
api_validate_api_key( $mdb );
$offer_id = trim( (string) \S::get( 'offer_id' ) );
$client_id_param = (int) \S::get( 'client_id' );
if ( $offer_id === '' || $client_id_param <= 0 )
{
api_json_response( [ 'result' => 'error', 'message' => 'Missing required params: offer_id, client_id' ], 422 );
}
$product = api_get_product_by_offer_and_client( $mdb, $offer_id, $client_id_param );
if ( !$product )
{
api_json_response( [ 'result' => 'error', 'message' => 'Product not found' ], 404 );
}
$google_category = trim( (string) ( $product['google_product_category'] ?? '' ) );
api_json_response( [
'result' => 'ok',
'product_id' => (int) $product['id'],
'offer_id' => $offer_id,
'client_id' => $client_id_param,
'google_product_category' => $google_category !== '' ? $google_category : null
] );
}
// Open Page Rank - zapis
@@ -181,4 +352,4 @@ if ( \S::get( 'action' ) == 'domain_opr_save' )
echo json_encode( ['result' => 'ok'] );
exit;
}
}