|
|
- <?php # Redmine Api
-
- class class_redmine {
- protected $redmine_key = '';
- protected $redmine_url = '';
-
-
- //files
- 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 $token = $this->curl_redmine($upload_url,$request,$filecontent);
- }
-
- function uploadd_attachment(string $issue_id, string $filename, string $filetype, $filecontent){
- $token = $this->get_upload_token($filecontent);
- if (!$token) {
- return null;
- }
- $upload_token = $token->upload->token;
- return $this->attach_to_issue($issue_id,$upload_token, $filename, $filetype);
- }
-
- 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;
- $at_descr = $this->curl_redmine($at_url);
- if (!$at_descr) {
- return null;
- }
- var_dump($at_descr);
- $file_url = $this->redmine_url.'attachments/download/'.
- $attachment_id.'/'.$at_descr->attachment->filename.'?key='.$this->redmine_key;
- return file_get_contents($file_url);
- }
- }
-
- //Issue
- 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);
- }
-
- function get_issue(string $issue_id='',string $project_id='', array $inc=array()) {
- if (count($inc) > 0) {
- $inc = implode(",", $inc);
- $inc = '&include='.$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);
- }
-
- 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);
- }
-
- 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
- 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
- function get_contacts() {
- $cont_url = $this->redmine_url.'contacts.json?key='.$this->redmine_key;
- return $this->curl_redmine($cont_url);
- }
-
- 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);
- }
-
- function get_contact_id(int $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;
- return $this->curl_redmine($cont_url, array(),'');
- }
-
- 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);
- }
-
- //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 = 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);
- }
- }
-
- ?>
|