You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
7.9 KiB

4 years ago
  1. <?php # Redmine Api
  2. class class_redmine {
  3. protected $redmine_key = '';
  4. protected $redmine_url = '';
  5. //files
  6. function get_upload_token($filecontent) {
  7. $upload_url = $this->redmine_url.'uploads.json?key='.$this->redmine_key;
  8. $request['type'] = 'post';
  9. $request['content_type'] = 'application/octet-stream';
  10. return $token = $this->curl_redmine($upload_url,$request,$filecontent);
  11. }
  12. function uploadd_attachment(string $issue_id, string $filename, string $filetype, $filecontent){
  13. $token = $this->get_upload_token($filecontent);
  14. if (!$token) {
  15. return null;
  16. }
  17. $upload_token = $token->upload->token;
  18. return $this->attach_to_issue($issue_id,$upload_token, $filename, $filetype);
  19. }
  20. function download_attachment(string $attachment_id, bool $thumbnail=false) {
  21. if ($thumbnail) {
  22. $file_url = $this->redmine_url.'attachments/thumbnail/'.$attachment_id.'?key='.$this->redmine_key;
  23. return file_get_contents($file_url);
  24. } else {
  25. $at_url = $this->redmine_url.'attachments/'.$attachment_id.'.json?key='.$this->redmine_key;
  26. $at_descr = $this->curl_redmine($at_url);
  27. if (!$at_descr) {
  28. return null;
  29. }
  30. var_dump($at_descr);
  31. $file_url = $this->redmine_url.'attachments/download/'.
  32. $attachment_id.'/'.$at_descr->attachment->filename.'?key='.$this->redmine_key;
  33. return file_get_contents($file_url);
  34. }
  35. }
  36. //Issue
  37. function create_issue($post_data) {
  38. $issue_url = $this->redmine_url.'issues.json?key='.$this->redmine_key;
  39. $request['type'] = 'post';
  40. $request['content_type'] = 'application/json';
  41. return $this->curl_redmine($issue_url,$request,$post_data);
  42. }
  43. function get_issue(string $issue_id='',string $project_id='', array $inc=array()) {
  44. if (count($inc) > 0) {
  45. $inc = implode(",", $inc);
  46. $inc = '&include='.$inc;
  47. } else {
  48. $inc = '';
  49. }
  50. if($project_id!='') {
  51. $issue_url = $this->redmine_url.'issues.json?key='.$this->redmine_key.'&project_id='.$project_id.$inc;
  52. }else{
  53. $issue_url = ($issue_id=='')
  54. ?$this->redmine_url.'issues.json?key='.$this->redmine_key.$inc
  55. : $this->redmine_url.'issues/'.$issue_id.'.json?key='.$this->redmine_key.$inc;
  56. }
  57. return $this->curl_redmine($issue_url);
  58. }
  59. function attach_to_issue(string $issue_id,string $upload_token, string $filename, string $filetype) {
  60. $issue_url = $this->redmine_url.'issues/'.$issue_id.'.json?key='.$this->redmine_key;
  61. $update = array( "issue" => array(
  62. "uploads"=> array( 0 => array("token" => $upload_token,
  63. "filename" => $filename,
  64. "content_type"=> $filetype)
  65. ),
  66. )
  67. );
  68. $request['type'] = 'put';
  69. $request['content_type'] = 'application/json';
  70. $post_data = json_encode($update);
  71. return $this->curl_redmine($issue_url, $request , $post_data);
  72. }
  73. function post_issue_comment(string $issue_id,string $comment) {
  74. $issue_url = $this->redmine_url.'issues/'.$issue_id.'.json?key='.$this->redmine_key;
  75. $update = array( "issue" => array(
  76. "notes"=> $comment,
  77. )
  78. );
  79. $request['type'] = 'put';
  80. $request['content_type'] = 'application/json';
  81. $post_data = json_encode($update);
  82. return $this->curl_redmine($issue_url, $request , $post_data);
  83. }
  84. //Projects
  85. function get_projects($project_id='') {
  86. $proj_url = ($project_id=='')?$this->redmine_url.'projects.json?key='.$this->redmine_key : $this->redmine_url.'projects/'.$project_id.'.json?key='.$this->redmine_key;
  87. return $this->curl_redmine($proj_url);
  88. }
  89. //Contacts
  90. function get_contacts() {
  91. $cont_url = $this->redmine_url.'contacts.json?key='.$this->redmine_key;
  92. return $this->curl_redmine($cont_url);
  93. }
  94. function search_contacts_str(string $search_str) {
  95. $cont_url = $this->redmine_url.'contacts.json?key='.$this->redmine_key.'&search='.$search_str;
  96. return $this->curl_redmine($cont_url);
  97. }
  98. function get_contact_id(int $id, bool $issues) {
  99. $cont_url = ($issues)
  100. ? $this->redmine_url."contacts/$id.json?key=".$this->redmine_key.'&include=issues'
  101. : $this->redmine_url."contacts/$id.json?key=".$this->redmine_key;
  102. return $this->curl_redmine($cont_url, array(),'');
  103. }
  104. function create_contact(array $contact) {
  105. $cont_url = $this->redmine_url.'contacts.json?key='.$this->redmine_key;
  106. $request['type'] = 'post';
  107. $request['content_type'] = 'application/json';
  108. $post_data = json_encode($contact);
  109. return $this->curl_redmine($cont_url,$request,$post_data);
  110. }
  111. //Curl
  112. function curl_redmine(string $redmine_url,array $request=array(),string $post_data='') {
  113. if(!isset($request['type'])){ $request['type']=null; }
  114. if(!isset($request['content_type'])){ $request['content_type']=null; }
  115. //Create a curl object
  116. $ch = curl_init();
  117. //Set the useragent
  118. //$agent = $_SERVER["HTTP_USER_AGENT"];
  119. //curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  120. //Set the URL
  121. curl_setopt($ch, CURLOPT_URL, $redmine_url );
  122. if($request['type'] == 'post'){
  123. //This is a POST query
  124. curl_setopt($ch, CURLOPT_POST,1);
  125. curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
  126. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  127. 'Content-Type: '.$request['content_type'],
  128. 'Content-Length: ' . strlen($post_data))
  129. );
  130. } elseif ($request['type'] == 'put') {
  131. //This is a PUT query
  132. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  133. curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
  134. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  135. 'Content-Type: '.$request['content_type'],
  136. 'Content-Length: ' . strlen($post_data))
  137. );
  138. }
  139. //We want the content after the query
  140. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  141. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  142. //Follow Location redirects
  143. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  144. //curl_setopt($ch, CURLOPT_HEADER, true);
  145. /*
  146. Set the cookie storing files
  147. Cookie files are necessary since we are logging and session data needs to be saved
  148. */
  149. //curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
  150. //curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
  151. //Execute the action to login
  152. $postResult = curl_exec($ch);
  153. echo(curl_getinfo($ch, CURLINFO_HTTP_CODE));
  154. // for debug purposes
  155. // if($postResult == false){ return $info = curl_getinfo($ch);}
  156. $response = json_decode($postResult);
  157. return $response;
  158. }
  159. }//class_redmine
  160. class class_bitrix_to_redmine extends class_redmine {
  161. protected $redmine_key = '68692662158cb38c8fddb8e084b6dfd55d77b85c';
  162. protected $redmine_url = 'https://tracker.a-real.ru/';
  163. function send_contact($data){
  164. $contact = array("contact" => array("first_name" => $data['last_name'],
  165. "project_id" => "managers",
  166. "phone" => $data["phone_work"],
  167. "email" => $data['webtolead_email1']
  168. )
  169. );
  170. return $this->create_contact($contact);
  171. }
  172. }
  173. ?>

Powered by TurnKey Linux.