commit 7bdfd774a601fd365629aab59f83241060350b2d Author: Mikhail Grebenkin Date: Thu Jun 25 22:29:38 2020 +0300 initial commit diff --git a/$screenshot_download.png b/$screenshot_download.png new file mode 100644 index 0000000..44cc95a Binary files /dev/null and b/$screenshot_download.png differ diff --git a/redmine.php b/redmine.php new file mode 100644 index 0000000..0b33e01 --- /dev/null +++ b/redmine.php @@ -0,0 +1,203 @@ +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); + } +} + +?> diff --git a/screenshot.png b/screenshot.png new file mode 100644 index 0000000..44cc95a Binary files /dev/null and b/screenshot.png differ diff --git a/tester.php b/tester.php new file mode 100644 index 0000000..c0e0b27 --- /dev/null +++ b/tester.php @@ -0,0 +1,72 @@ + + 'https://tracker.a-real.ru/contacts/2906.json?include=issues', +// //CURLOPT_URL => 'https://tracker.a-real.ru/issues/10259.json?include=contact', +// CURLOPT_RETURNTRANSFER => true, +// CURLOPT_POST => true, +// CURLOPT_USERNAME => 'mix', +// CURLOPT_PASSWORD => 'CC35U8TXwgztS', +// CURLOPT_CUSTOMREQUEST => 'GET' +// // CURLOPT_POSTFIELDS => http_build_query(array(/*здесь массив параметров запроса*/)) +//)); +//$response = curl_exec($myCurl); +//curl_close($myCurl); +// +//var_dump($response) + +$obj_redmine = new class_bitrix_to_redmine(); + +$test_contact = array("contact" => array("first_name" => "Test testing", + "project_id" => "managers", + //"is_company" => false, + "phone" => "+79997994838", + "email" => "mvg@a-real.ru" + ) + ); + +//$data = array("last_name"=> "Тестер тестович", +// "webtolead_email1" => "mail@a-real.ru", +// "phone_work" => "333333333333", +// "campaign_id" => "50c0d2a6-bbb7-fad1-640d-4c77b63b39e1", +// "lead_source" => "Web Site", +// "lead_source_description" => $ext_referer, +// "lead_source_description_to" => $ext_referer_to, +// "redirect_url" => "http://xserver.a-real.ru", +// "assign_user_id" => "e3eb89ec-9d10-1aba-ccbb-4b580a4cdb34", +// "req_id" => "last_name;webtolead_email1;" +//); + + +// $res = $obj_redmine->send_contact($data); +// $res = $obj_redmine->get_contact_id(2906, true); + +//$filecontent = file_get_contents("screenshot.png"); +//$token = $obj_redmine->get_upload_token($filecontent); + +//$token = $token->upload->token; + +//var_dump($token); + +//$filecontent = file_get_contents("screenshot.png"); + +//echo($obj_redmine->uploadd_attachment('10314', 'screenshot.png', 'image/png', $filecontent)); + +$inc = array( 0 => "journals", + 1 => "attachments" + ); + +$res = $obj_redmine->get_issue('10314', '', $inc); +var_dump($res); + +$res = $obj_redmine->download_attachment('5152'); +file_put_contents('$screenshot_download.png', $res); + +?> \ No newline at end of file