'mysql', 'database_name' => $database['name'], 'server' => $database['host'], 'username' => $database['user'], 'password' => $database['password'], 'charset' => 'utf8' ] ); \R::setup( 'mysql:host=' . $database['host'] . ';dbname=' . $database['name'], $database['user'], $database['password'] ); \R::ext( 'xdispense', function( $type ) { 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' ) { if ( !$mdb -> count( 'domain_tester', [ 'AND' => [ 'url' => \S::get( 'domain' ), 'hidden' => 0 ] ] ) ) { $mdb -> insert( 'domain_tester', [ 'url' => \S::get( 'domain' ) ] ); } echo json_encode( ['result' => 'ok'] ); exit; } // Open Page Rank - pobieranie domeny if ( \S::get( 'action' ) == 'domain_opr_check' ) { $mdb -> delete( 'domain_tester', [ 'url' => '' ] ); $domain = $mdb -> get( 'domain_tester', '*', [ 'opr' => null, 'ORDER' => [ 'date_add' => 'DESC' ] ] ); if ( $domain ) { $result['id'] = $domain['id']; $result['url'] = $domain['url']; $result['domains_left'] = $mdb -> count( 'domain_tester', [ 'opr' => null ] ); $result['result'] = 'ok'; echo json_encode( $result ); } else echo json_encode( ['result' => 'bad'] ); exit; } // Dodawanie komentarza do kampanii przez API (z Claude Code) if ( \S::get( 'action' ) == 'campaign_comment_add' ) { $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; } $external_campaign_id = trim( \S::get( 'campaign_id' ) ); $client_id_param = trim( \S::get( 'client_id' ) ); $comment = trim( \S::get( 'comment' ) ); $date = \S::get( 'date' ) ?: date( 'Y-m-d' ); if ( !$external_campaign_id || !$client_id_param || !$comment ) { echo json_encode( [ 'result' => 'error', 'message' => 'Missing required params: campaign_id, client_id, comment' ] ); exit; } $client_id_clean = str_replace( '-', '', $client_id_param ); $local_campaign = $mdb -> query( 'SELECT c.id FROM campaigns c JOIN clients cl ON c.client_id = cl.id WHERE c.campaign_id = :campaign_id AND REPLACE( cl.google_ads_customer_id, \'-\', \'\' ) = :client_id LIMIT 1', [ ':campaign_id' => $external_campaign_id, ':client_id' => $client_id_clean ] ) -> fetch( \PDO::FETCH_ASSOC ); if ( !$local_campaign ) { echo json_encode( [ 'result' => 'error', 'message' => 'Campaign not found' ] ); exit; } \factory\Campaigns::add_campaign_comment( $local_campaign['id'], $comment, $date ); echo json_encode( [ 'result' => 'ok' ] ); exit; } // Zmiana custom_label_4 dla produktu przez API if ( \S::get( 'action' ) == 'product_custom_label_4_set' ) { api_validate_api_key( $mdb ); $offer_id = trim( \S::get( 'offer_id' ) ); $client_id_param = trim( \S::get( 'client_id' ) ); $custom_label_4 = trim( \S::get( 'custom_label_4' ) ); if ( !$offer_id || !$client_id_param ) { 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, (int) $client_id_param ); if ( !$product ) { 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)' ); 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 if ( \S::get( 'action' ) == 'domain_opr_save' ) { $mdb -> update( 'domain_tester', [ 'opr' => str_replace( ',', '.', \S::get( 'page_rank' ) ), 'opr_date' => date( 'Y-m-d H:i:s' ) ], [ 'id' => \S::get( 'domain_id' ) ] ); echo json_encode( ['result' => 'ok'] ); exit; }