get_properties(); $config = $properties['config']; $reply = get_transient('tcxwm_webinar_data_'.$id); if ($reply===false) { $reply = array('result'=>false,'error'=>'unspecified error'); $args = array( 'timeout' => '10', 'redirection' => '5', 'httpversion' => '1.1', 'blocking' => true, 'headers' => array('3CX-ApiKey'=>$config['apitoken']), 'cookies' => array() ); $url = $config['portalfqdn'].'/webmeeting/api/v1/meetings/list?isWebinar=true&isScheduled=false'; if ($config['extension']!=''){ $url.='&extension='.$config['extension']; } if (!empty($config['subject'])) { $url.='&subjectContains='.$config['subject']; } if (!empty($config['days'])) { $url.='&daysLimit='.$config['days']; } $response = wp_remote_get($url, $args ); $http_code = wp_remote_retrieve_response_code( $response ); $body = wp_remote_retrieve_body( $response ); switch($http_code){ case 200: $res = @json_decode($body, true); if (!is_array($res) || empty($res) || $res['status']!='success' || empty($res['result'])) { $reply['error'] = 'Invalid reply'; } else { $meetings = $res['result']['scheduledMeetings']; $reply['meetings'] = array(); $reply['result']=true; $reply['error']=''; // filter out already expired webinars $tz = new DateTimeZone('UTC'); $now = new DateTime('now', $tz); foreach($meetings as $v) { $dt = new DateTime($v['datetime'].':00Z', $tz); // server returns UTC date $dt = $dt->add(new DateInterval('PT'.$v['duration'].'M')); if ($dt>$now){ $reply['meetings'][]=$v; } } } break; case 404: $reply['meetings']='[]'; $reply['result']=true; $reply['error']=''; break; default: $reply['error']='Request failed: '.$http_code; break; } if (!$reply['result']) { error_log('3CX WebMeeting API error: '.$res['error']); } else { set_transient('tcxwm_webinar_data_'.$id, $reply, $config['cache_expiry']*60); } } return $reply; } function wp3cxw_subscribe_request(WP_REST_Request $request) { $body = $request->get_body_params(); $status = 403; $reply=array( 'result'=>false, 'error'=>'Invalid Request' ); $id=false; $name=false; $email=false; // check and sanitize params if ($body) { if ($body['id'] && intval($body['id']>0)) { $id = intval($body['id']); } if ($body['name']){ $name=wp3cxw_sanitize_query_var($body['name']); } if ($body['email'] && is_email($body['email'])) { $email=$body['email']; } } if ($id && $name && $email) { $webinar=false; $form = wp3cxw_webinar_form($id); if ($form) { $properties = $form->get_properties(); $config = $properties['config']; $res = wp3cxw_GetWebinarTransient($id); if ($res && $res['meetings']) { foreach($res['meetings'] as $meet) { if (md5($id.$meet['meetingid'])==$body['subscribe']) { $webinar=$meet; break; } } } } if ($webinar) { $parts=array(); $parts[]=array( 'name' => $body['name'], 'email' => $body['email'] ); // send subscribe API request to PBX $args = array( 'timeout' => '20', 'redirection' => '5', 'httpversion' => '1.1', 'blocking' => true, 'headers' => array( '3CX-ApiKey' => $config['apitoken'], 'Content-Type' => 'application/json', ), 'cookies' => array(), 'body' => json_encode($parts) ); $maxpart=''; if ($config['maxparticipants']>0) { $maxpart = '?maxparticipants='.$config['maxparticipants']; } $response = wp_remote_post($config['portalfqdn'].'/webmeeting/api/v1/participants/'.$meet['meetingid'].$maxpart, $args ); $http_code = wp_remote_retrieve_response_code( $response ); $body = wp_remote_retrieve_body( $response ); $status=$http_code; $res = @json_decode($body, true); print_r($res,true); if ($http_code==200){ if (!empty($res) && is_array($res) && $res['status']=='success') { $reply['result']=true; $reply['error']=''; } } if ($res && $res['errorInformation']){ $reply['error']=$res['errorInformation']; } } else { $reply['error']="Webinar Not Found"; $status=404; } } $response = new WP_REST_Response($reply); $response->set_status($status); return $response; } function wp3cxw_delete_cache($id) { delete_transient('tcxwm_webinar_data_'.$id); } ?>