Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 61 additions & 44 deletions src/BMLT/Integration.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?php
// Copyright (C) 2022 nigel.bmlt@gmail.com
//
//
// This file is part of bmlt-workflow.
//
//
// bmlt-workflow is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//
// bmlt-workflow is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//
// You should have received a copy of the GNU General Public License
// along with bmlt-workflow. If not, see <http://www.gnu.org/licenses/>.

Expand All @@ -24,7 +24,7 @@ class Integration
{

use \bmltwf\BMLTWF_Debug;

/**
* Wrapper for wp_remote_request that logs both request and response
*
Expand All @@ -41,10 +41,10 @@ private function bmltwf_wp_remote_request($url, $args)
if ($body) {
$this->debug_log("REQUEST BODY: " . $body);
}

// Make the request
$response = \wp_remote_request($url, $args);

// Log the response
if (is_wp_error($response)) {
$this->debug_log("RESPONSE ERROR: " . $response->get_error_message());
Expand All @@ -53,21 +53,21 @@ private function bmltwf_wp_remote_request($url, $args)
$response_body = \wp_remote_retrieve_body($response);
$this->debug_log("RESPONSE CODE: " . $response_code);
$this->debug_log("RESPONSE BODY: " . $response_body);

// Log error responses with their body
if ($response_code >= 400) {
$this->debug_log("ERROR RESPONSE ($response_code): " . $response_body);

// Use the new debug_http_error function for 500 errors
if ($response_code >= 500) {
$this->debug_http_error($response);
}
}
}

return $response;
}

/**
* Wrapper for wp_remote_get that logs both request and response
*
Expand All @@ -80,7 +80,7 @@ private function bmltwf_wp_remote_get($url, $args = array())
$args['method'] = 'GET';
return $this->bmltwf_wp_remote_request($url, $args);
}

/**
* Wrapper for wp_remote_post that logs both request and response
*
Expand Down Expand Up @@ -250,18 +250,18 @@ function updateMeeting($change)
{
// Validate meeting data
$filtered_change = $this->validateMeetingData($change);

// Workaround for timeZone required
$filtered_change["timeZone"] = null;

// If validation returned an error, return it
if (is_wp_error($filtered_change)) {
return $filtered_change;
}

$this->debug_log("updateMeeting change");
$this->debug_log($filtered_change);

if (array_key_exists('formatIds', $filtered_change)) {
$filtered_change['formatIds'] = $this->removeLocations($filtered_change['formatIds']);
}
Expand All @@ -283,7 +283,7 @@ function updateMeeting($change)
$url = get_option('bmltwf_bmlt_server_address') . 'api/v1/meetings/' . $filtered_change['id'];

$this->debug_bmlt_payload($url, 'PATCH', $filtered_change);

$response = $this->bmltwf_wp_remote_request($url, array(
'method' => 'PATCH',
'headers' => array(
Expand Down Expand Up @@ -387,7 +387,7 @@ public function getAllMeetings()
'longitude',
'comments'
];

$body = json_decode(\wp_remote_retrieve_body($response),true);
if ($body === null) {
return new \WP_Error('bmltwf', 'Invalid JSON response from BMLT server');
Expand Down Expand Up @@ -584,9 +584,13 @@ public function getMeetingStates()
if ($server_info === false) {
return new \WP_Error('bmltwf', __('BMLT Configuration Error - Unable to retrieve server info', 'bmlt-workflow'));
}

if (!empty($server_info['meeting_states_and_provinces'])) {
$states = explode(',', $server_info['meeting_states_and_provinces']);
// $this->debug_log(\wp_remote_retrieve_body($response));
$arr = json_decode(\wp_remote_retrieve_body($response), true);
if ($arr === null || !isset($arr[0])) {
return new \WP_Error('bmltwf', __('Invalid server info response', 'bmlt-workflow'));
}
if (!empty($arr[0]['meeting_states_and_provinces'])) {
$states = explode(',', $arr[0]['meeting_states_and_provinces']);
return $states;
}
return false;
Expand Down Expand Up @@ -616,7 +620,7 @@ public function getMeetingCounties()
* getGmapsKey
*
* workaround for client/server side maps key issues
*
*
* @return \WP_Error|string
*/
public function getGmapsKey()
Expand Down Expand Up @@ -648,20 +652,28 @@ public function getGmapsKey()
$this->debug_log("*** ADMIN URL " . $url);

$response = $this->getv2($url, $this->cookies);

preg_match('/"google_api_key":"(.*?)",/', \wp_remote_retrieve_body($response), $matches);
$this->debug_log("retrieved gmaps key");
$gmaps_key = isset($matches[1]) ? $matches[1] : '';

\update_option('bmltwf_bmlt_google_maps_key', $gmaps_key);
$body = \wp_remote_retrieve_body($response);
$this->debug_log("bmlt gmaps response - ".$body);
preg_match('/"google_api_key":"(.*?)",/', $body, $matches);
$gmaps_key = isset($matches[1]) ? $matches[1] : '';
if ($gmaps_key == '') {
preg_match('/googleApiKey: *\'(.*?)\',/', $body, $matches);
$gmaps_key = isset($matches[1]) ? $matches[1] : '';
}
if ($gmaps_key != '') {
$this->debug_log("retrieved gmaps key: '" . $gmaps_key . "'");
\update_option('bmltwf_bmlt_google_maps_key', $gmaps_key);
} else {
$this->debug_log("failed to retrieve gmaps key");
}

return $gmaps_key;
}


/**
* Get the schema for meeting data validation
*
*
* @return array Schema with field names and their expected types
*/
private function getMeetingSchema()
Expand Down Expand Up @@ -707,10 +719,10 @@ private function getMeetingSchema()
'id' => 'integer'
];
}

/**
* Validate meeting data against schema
*
*
* @param array $meeting Meeting data to validate
* @return array|\WP_Error Filtered meeting data or error
*/
Expand All @@ -719,12 +731,12 @@ private function validateMeetingData($meeting)
$allowed_keys = $this->getMeetingSchema();
$filtered_meeting = [];
$errors = [];

foreach ($meeting as $key => $value) {
if (array_key_exists($key, $allowed_keys)) {
$expected_type = $allowed_keys[$key];
$valid = false;

switch ($expected_type) {
case 'integer':
$valid = is_int($value) || (is_string($value) && ctype_digit($value));
Expand Down Expand Up @@ -756,34 +768,34 @@ private function validateMeetingData($meeting)
default:
$valid = true; // Unknown type, accept as is
}

if ($valid) {
$filtered_meeting[$key] = $value;
} else {
$errors[] = "Invalid type for '$key'. Expected $expected_type.";
}
}
}

if (!empty($errors)) {
$this->debug_log("Type validation errors:");
$this->debug_log($errors);
return new \WP_Error('bmltwf', __('Invalid meeting data format', 'bmlt-workflow'), $errors);
}

return $filtered_meeting;
}

function createMeeting($meeting)
{
// Validate meeting data
$filtered_meeting = $this->validateMeetingData($meeting);

// If validation returned an error, return it
if (is_wp_error($filtered_meeting)) {
return $filtered_meeting;
}

$this->debug_log("createMeeting change");
$this->debug_log($filtered_meeting);

Expand All @@ -794,7 +806,7 @@ function createMeeting($meeting)
$this->debug_log("formatIds missing or not an array");
return new \WP_Error('bmltwf', __('formatIds is required and must be an array', 'bmlt-workflow'));
}

// Workaround for timeZone required
$filtered_meeting["timeZone"] = null;

Expand Down Expand Up @@ -874,9 +886,14 @@ public function getDefaultLatLong()
if ($server_info === false) {
return new \WP_Error('bmltwf', __('BMLT Configuration Error - Unable to retrieve server info', 'bmlt-workflow'));
}

if ((!empty($server_info['centerLongitude'])) && (!empty($server_info['centerLatitude']))) {
return array('longitude' => $server_info['centerLongitude'], 'latitude' => $server_info['centerLatitude']);
// $this->debug_log(\wp_remote_retrieve_body($response));
$arr = json_decode(\wp_remote_retrieve_body($response), true);
if ($arr === null || !isset($arr[0])) {
return new \WP_Error('bmltwf', __('Invalid server info response', 'bmlt-workflow'));
}
if ((!empty($arr[0]['centerLongitude'])) && (!empty($arr[0]['centerLatitude']))) {

return array('longitude' => $arr[0]['centerLongitude'], 'latitude' => $arr[0]['centerLatitude']);
}
return false;
}
Expand Down Expand Up @@ -990,7 +1007,7 @@ function authenticateRootServer()
$this->debug_log("inside authenticateRootServer v3 auth");
$url = get_option('bmltwf_bmlt_server_address') . "api/v1/auth/token";
$response = \wp_remote_post($url, array('body' => http_build_query($postargs)));

if (is_wp_error($response)) {
return $response;
}
Expand Down Expand Up @@ -1160,4 +1177,4 @@ public function postUnauthenticatedRootServerRequest($url, $postargs)
return $val;
}

}
}
Loading