*/ class class_redmine { protected $redmine_key = ''; protected $redmine_url = ''; /** * Uploads $filecontent and returns token. * * @param mixed $filecontent - content of file * @return array 0 => http response, 1 => $token is object, string can be obtained in $token->upload->token */ function get_upload_token($filecontent) { $upload_url = $this->redmine_url.'uploads.json?key='.$this->redmine_key; $request['type'] = 'post'; $request['content_type'] = 'application/octet-stream'; return $this->curl_redmine($upload_url,$request,$filecontent); } /** * Uploads an attachment, attaches it to an issue and returns json-decoded updated issue * * @param string $issue_id numeric id of issue to attach file * @param string $filename name of file * @param string $filetype type of content i.e. "image/png" * @param mixed $filecontent content of file. * @return array 0 => http response, 1 => $issue updated issue */ function upload_attachment(string $issue_id, string $filename, string $filetype, $filecontent){ $token = $this->get_upload_token($filecontent)[1]; if (!$token) { return null; } $upload_token = $token->upload->token; return $this->attach_to_issue($issue_id,$upload_token, $filename, $filetype); } /** * Download an attachment or thumbnail * * @param string $attachment_id numeric id of attachment * @param bool $thumbnail if true downloading thumbnail * * @return array 0 => http response, 1 => $filecontent - content of file */ function download_attachment(string $attachment_id, bool $thumbnail=false) { if ($thumbnail) { $file_url = $this->redmine_url.'attachments/thumbnail/'.$attachment_id.'?key='.$this->redmine_key; return file_get_contents($file_url); } else { $at_url = $this->redmine_url.'attachments/'.$attachment_id.'.json?key='.$this->redmine_key; $api_resp = $this->curl_redmine($at_url); $at_descr = $api_resp[0]; if (!$at_descr) { return null; } $file_url = $this->redmine_url.'attachments/download/'. $attachment_id.'/'.$at_descr->attachment->filename.'?key='.$this->redmine_key; return file_get_contents($file_url); } } /** * Create issue * * @param $post_data - string to post to redmine (needs to update) * @return array 0 => http response, 1 => $issue - new data */ function create_issue($post_data) { $issue_url = $this->redmine_url.'issues.json?key='.$this->redmine_key; $request['type'] = 'post'; $request['content_type'] = 'application/json'; return $this->curl_redmine($issue_url,$request,$post_data); } /** * get issue * * @param string $issue_id numeric issue id * @param string $project_id numeric project id * @param array $inc array of string with options to include. Example: array( 0 => "journals", 1 => "attachments" ); * possible options: * *children * *attachments * *relations * *changesets * *journals * *watchers * * @return array 0 => http response, 1 => $issue - json data of issue */ function get_issue(string $issue_id='',string $project_id='', array $inc=array()) { if (count($inc) > 0) { $inc = implode(",", $inc); $inc = '&include='.$inc; var_dump($inc); } else { $inc = ''; } if($project_id!='') { $issue_url = $this->redmine_url.'issues.json?key='.$this->redmine_key.'&project_id='.$project_id.$inc; }else{ $issue_url = ($issue_id=='') ?$this->redmine_url.'issues.json?key='.$this->redmine_key.$inc : $this->redmine_url.'issues/'.$issue_id.'.json?key='.$this->redmine_key.$inc; } return $this->curl_redmine($issue_url); } /** * attach to issue * * @param string $issue_id digits only issue id * @param string $upload_token digits only project id * @param string $upload_token * @param array $inc array of string with options to include. Example: array( 0 => "journals", 1 => "attachments" ); * @return array 0 => http responce; 1 => issue - new data */ function attach_to_issue(string $issue_id,string $upload_token, string $filename, string $filetype) { $issue_url = $this->redmine_url.'issues/'.$issue_id.'.json?key='.$this->redmine_key; $update = array( "issue" => array( "uploads"=> array( 0 => array("token" => $upload_token, "filename" => $filename, "content_type"=> $filetype ) ) ) ); $request['type'] = 'put'; $request['content_type'] = 'application/json'; $post_data = json_encode($update); return $this->curl_redmine($issue_url, $request , $post_data); } /** * add comment to issue * * @param string $issue_id digits only issue id * @param string $comment comment to add to the issue * @return mixed array 0 => http response, 1 => $issue - new data */ function post_issue_comment(string $issue_id,string $comment) { $issue_url = $this->redmine_url.'issues/'.$issue_id.'.json?key='.$this->redmine_key; $update = array( "issue" => array( "notes"=> $comment, ) ); $request['type'] = 'put'; $request['content_type'] = 'application/json'; $post_data = json_encode($update); return $this->curl_redmine($issue_url, $request , $post_data); } //Projects, nowadays we don't use it. function get_projects($project_id='') { $proj_url = ($project_id=='')?$this->redmine_url.'projects.json?key='.$this->redmine_key : $this->redmine_url.'projects/'.$project_id.'.json?key='.$this->redmine_key; return $this->curl_redmine($proj_url); } //Contacts /** * returns json structure with all contacts * * @return mixed array 0 => http response, 1 => $contacts - data */ function get_contacts() { $cont_url = $this->redmine_url.'contacts.json?key='.$this->redmine_key; return $this->curl_redmine($cont_url); } /** * search contacts by string. Returns contacts. * * @param string $search_str string to search by * @return mixed array 0 => http response, 1 => $contacts - new data */ function search_contacts_str(string $search_str) { $cont_url = $this->redmine_url.'contacts.json?key='.$this->redmine_key.'&search='.$search_str; return $this->curl_redmine($cont_url); } /** * search contacts by id. Returns one contact. * * @param string $id digits only contact id * @param bool $issues if true, includes issues, connected to contact * @return mixed array 0 => http response, 1 => $contact - data */ function get_contact_id(string $id, bool $issues) { $cont_url = ($issues) ? $this->redmine_url."contacts/$id.json?key=".$this->redmine_key.'&include=issues' : $this->redmine_url."contacts/$id.json?key=".$this->redmine_key; echo($cont_url); return $this->curl_redmine($cont_url); } /** * Creates contact from array. * * @param array $contact array with all needed contact fields * @return mixed array 0 => http response, 1 => $contact - data */ function create_contact(array $contact) { $cont_url = $this->redmine_url.'contacts.json?key='.$this->redmine_key; $request['type'] = 'post'; $request['content_type'] = 'application/json'; $post_data = json_encode($contact); return $this->curl_redmine($cont_url,$request,$post_data); } /** * attach contact to issue * * @param string $issue_id digits only issue id * @param string $contact_id digits only contact id * @return mixed array 0 => http response, 1 => $issue - new data */ function attach_issue_to_contact(string $issue_id,string $contact_id) { $contact_url = $this->redmine_url."contacts/".$contact_id."?key=".$this->redmine_key; $update = array( "contact" => array( "issues"=> array( 0=> array("id" => $issue_id)) ) ); $request['type'] = 'put'; $request['content_type'] = 'application/json'; $post_data = json_encode($update); return $this->curl_redmine($contact_url, $request , $post_data); } /** * create_ticket creates issue with connected contact. Needs contact email. * * @param string $issue_id digits only issue id * @param string $contact_email contact email * @return mixed array 0 => http response, 1 => $issue - new data */ function create_ticket(string $contact_email, string $subject, string $description) { $issue_url = $this->redmine_url."helpdesk/create_ticket.xml?key=".$this->redmine_key; $issue_xml = " 14 3 $subject $description $contact_email "; $request['type'] = 'post'; $request['content_type'] = 'application/xml'; return $this->curl_redmine($issue_url, $request, $issue_xml ); } //Curl function curl_redmine(string $redmine_url,array $request=array(),string $post_data='') { if(!isset($request['type'])){ $request['type']=null; } if(!isset($request['content_type'])){ $request['content_type']=null; } //Create a curl object $ch = curl_init(); //Set the useragent //$agent = $_SERVER["HTTP_USER_AGENT"]; //curl_setopt($ch, CURLOPT_USERAGENT, $agent); //Set the URL curl_setopt($ch, CURLOPT_URL, $redmine_url ); if($request['type'] == 'post'){ //This is a POST query curl_setopt($ch, CURLOPT_POST,1); curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: '.$request['content_type'], 'Content-Length: ' . strlen($post_data)) ); } elseif ($request['type'] == 'put') { //This is a PUT query curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: '.$request['content_type'], 'Content-Length: ' . strlen($post_data)) ); } //We want the content after the query curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //Follow Location redirects curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //curl_setopt($ch, CURLOPT_HEADER, true); /* Set the cookie storing files Cookie files are necessary since we are logging and session data needs to be saved */ //curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); //curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); //Execute the action to login $postResult = curl_exec($ch); // echo(curl_getinfo($ch, CURLINFO_HTTP_CODE)); // for debug purposes // if($postResult == false){ return $info = curl_getinfo($ch);} $response = array( 0 => curl_getinfo($ch, CURLINFO_HTTP_CODE), 1 => json_decode($postResult)); return $response; } }//class_redmine class class_bitrix_to_redmine extends class_redmine { protected $redmine_key = '68692662158cb38c8fddb8e084b6dfd55d77b85c'; protected $redmine_url = 'https://tracker.a-real.ru/'; function send_contact($data){ $contact = array("contact" => array("first_name" => $data['last_name'], "project_id" => "managers", "phone" => $data["phone_work"], "email" => $data['webtolead_email1'] ) ); return $this->create_contact($contact); } } ?>