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.

302 lines
11 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. /**
  3. * Class provides connection to a-real redmine api.
  4. * Use class_bitrix_to_redmine
  5. *
  6. * @package redmine_api
  7. * @author Mikhail Grebenkin <mix@m14xa.ru>
  8. */
  9. class class_redmine {
  10. protected $redmine_key = '';
  11. protected $redmine_url = '';
  12. /**
  13. * Upload $filecontent and return token.
  14. *
  15. * @param mixed $filecontent - content of file
  16. * @return mixed $token is object, string can be obtained in $token->upload->token
  17. */
  18. function get_upload_token($filecontent) {
  19. $upload_url = $this->redmine_url.'uploads.json?key='.$this->redmine_key;
  20. $request['type'] = 'post';
  21. $request['content_type'] = 'application/octet-stream';
  22. return $this->curl_redmine($upload_url,$request,$filecontent);
  23. }
  24. /**
  25. * Uploads an attachment, attaches it to an issue and returns json-decoded updated issue
  26. *
  27. * @param string $issue_id numeric id of issue to attach file
  28. * @param string $filename name of file
  29. * @param string $filetype type of content i.e. "image/png"
  30. * @param mixed $filecontent content of file.
  31. * @return mixed $issue updated issue
  32. */
  33. function upload_attachment(string $issue_id, string $filename, string $filetype, $filecontent){
  34. $token = $this->get_upload_token($filecontent);
  35. if (!$token) {
  36. return null;
  37. }
  38. $upload_token = $token->upload->token;
  39. return $this->attach_to_issue($issue_id,$upload_token, $filename, $filetype);
  40. }
  41. /**
  42. * Download an attachment or thumbnail
  43. *
  44. * @param string $attachment_id numeric id of attachment
  45. * @param bool $thumbnail if true downloading thumbnail
  46. * @return mixed $filecontent - content of file
  47. */
  48. function download_attachment(string $attachment_id, bool $thumbnail=false) {
  49. if ($thumbnail) {
  50. $file_url = $this->redmine_url.'attachments/thumbnail/'.$attachment_id.'?key='.$this->redmine_key;
  51. return file_get_contents($file_url);
  52. } else {
  53. $at_url = $this->redmine_url.'attachments/'.$attachment_id.'.json?key='.$this->redmine_key;
  54. $api_resp = $this->curl_redmine($at_url);
  55. $at_descr = $api_resp[0];
  56. if (!$at_descr) {
  57. return null;
  58. }
  59. $file_url = $this->redmine_url.'attachments/download/'.
  60. $attachment_id.'/'.$at_descr->attachment->filename.'?key='.$this->redmine_key;
  61. return file_get_contents($file_url);
  62. }
  63. }
  64. /**
  65. * Create issue
  66. *
  67. * @param $post_data - string to post to redmine (needs to update)
  68. * @return mixed $issue - new data
  69. */
  70. function create_issue($post_data) {
  71. $issue_url = $this->redmine_url.'issues.json?key='.$this->redmine_key;
  72. $request['type'] = 'post';
  73. $request['content_type'] = 'application/json';
  74. return $this->curl_redmine($issue_url,$request,$post_data);
  75. }
  76. /**
  77. * get issue
  78. *
  79. * @param string $issue_id numeric issue id
  80. * @param string $project_id numeric project id
  81. * @param array $inc array of string with options to include. Example: array( 0 => "journals", 1 => "attachments" );
  82. * @return mixed $issue - json data of issue
  83. */
  84. function get_issue(string $issue_id='',string $project_id='', array $inc=array()) {
  85. if (count($inc) > 0) {
  86. $inc = implode(",", $inc);
  87. $inc = '&include='.$inc;
  88. var_dump($inc);
  89. } else {
  90. $inc = '';
  91. }
  92. if($project_id!='') {
  93. $issue_url = $this->redmine_url.'issues.json?key='.$this->redmine_key.'&project_id='.$project_id.$inc;
  94. }else{
  95. $issue_url = ($issue_id=='')
  96. ?$this->redmine_url.'issues.json?key='.$this->redmine_key.$inc
  97. : $this->redmine_url.'issues/'.$issue_id.'.json?key='.$this->redmine_key.$inc;
  98. }
  99. return $this->curl_redmine($issue_url);
  100. }
  101. /**
  102. * attach to issue
  103. *
  104. * @param string $issue_id digits only issue id
  105. * @param string $upload_token digits only project id
  106. * @param string $upload_token
  107. * @param array $inc array of string with options to include. Example: array( 0 => "journals", 1 => "attachments" );
  108. * @return array 0 => http responce; 1 => issue - new data
  109. */
  110. function attach_to_issue(string $issue_id,string $upload_token, string $filename, string $filetype) {
  111. $issue_url = $this->redmine_url.'issues/'.$issue_id.'.json?key='.$this->redmine_key;
  112. $update = array( "issue" => array( "uploads"=> array( 0 => array("token" => $upload_token,
  113. "filename" => $filename,
  114. "content_type"=> $filetype )
  115. )
  116. )
  117. );
  118. $request['type'] = 'put';
  119. $request['content_type'] = 'application/json';
  120. $post_data = json_encode($update);
  121. return $this->curl_redmine($issue_url, $request , $post_data);
  122. }
  123. /**
  124. * add comment to issue
  125. *
  126. * @param string $issue_id digits only issue id
  127. * @param string $comment comment to add to the issue
  128. * @return mixed $issue - new data
  129. */
  130. function post_issue_comment(string $issue_id,string $comment) {
  131. $issue_url = $this->redmine_url.'issues/'.$issue_id.'.json?key='.$this->redmine_key;
  132. $update = array( "issue" => array(
  133. "notes"=> $comment,
  134. )
  135. );
  136. $request['type'] = 'put';
  137. $request['content_type'] = 'application/json';
  138. $post_data = json_encode($update);
  139. return $this->curl_redmine($issue_url, $request , $post_data);
  140. }
  141. //Projects
  142. function get_projects($project_id='') {
  143. $proj_url = ($project_id=='')?$this->redmine_url.'projects.json?key='.$this->redmine_key : $this->redmine_url.'projects/'.$project_id.'.json?key='.$this->redmine_key;
  144. return $this->curl_redmine($proj_url);
  145. }
  146. //Contacts
  147. function get_contacts() {
  148. $cont_url = $this->redmine_url.'contacts.json?key='.$this->redmine_key;
  149. return $this->curl_redmine($cont_url);
  150. }
  151. function search_contacts_str(string $search_str) {
  152. $cont_url = $this->redmine_url.'contacts.json?key='.$this->redmine_key.'&search='.$search_str;
  153. return $this->curl_redmine($cont_url);
  154. }
  155. function get_contact_id(string $id, bool $issues) {
  156. $cont_url = ($issues)
  157. ? $this->redmine_url."contacts/$id.json?key=".$this->redmine_key.'&include=issues'
  158. : $this->redmine_url."contacts/$id.json?key=".$this->redmine_key;
  159. echo($cont_url);
  160. return $this->curl_redmine($cont_url);
  161. }
  162. function create_contact(array $contact) {
  163. $cont_url = $this->redmine_url.'contacts.json?key='.$this->redmine_key;
  164. $request['type'] = 'post';
  165. $request['content_type'] = 'application/json';
  166. $post_data = json_encode($contact);
  167. return $this->curl_redmine($cont_url,$request,$post_data);
  168. }
  169. /**
  170. * attach contact to issue
  171. *
  172. * @param string $issue_id digits only issue id
  173. * @param string $contact_id digits only contact id
  174. * @return mixed $issue - new data
  175. */
  176. function attach_issue_to_contact(string $issue_id,string $contact_id) {
  177. $contact_url = $this->redmine_url."contacts/".$contact_id."?key=".$this->redmine_key;
  178. $update = array( "contact" => array(
  179. "issues"=> array( 0=> array("id" => $issue_id))
  180. )
  181. );
  182. $request['type'] = 'put';
  183. $request['content_type'] = 'application/json';
  184. $post_data = json_encode($update);
  185. return $this->curl_redmine($contact_url, $request , $post_data);
  186. }
  187. function create_ticket(string $contact_email, string $subject, string $description) {
  188. $issue_url = $this->redmine_url."helpdesk/create_ticket.xml?key=".$this->redmine_key;
  189. $issue_xml = "<?xml version=\"1.0\"?>
  190. <ticket>
  191. <issue>
  192. <project_id>14</project_id>
  193. <tracker_id>3</tracker_id>
  194. <subject>$subject</subject>
  195. <description>$description</description>
  196. </issue>
  197. <contact>
  198. <email>$contact_email</email>
  199. </contact>
  200. </ticket>";
  201. $request['type'] = 'post';
  202. $request['content_type'] = 'application/xml';
  203. return $this->curl_redmine($issue_url, $request, $issue_xml );
  204. }
  205. //Curl
  206. function curl_redmine(string $redmine_url,array $request=array(),string $post_data='') {
  207. if(!isset($request['type'])){ $request['type']=null; }
  208. if(!isset($request['content_type'])){ $request['content_type']=null; }
  209. //Create a curl object
  210. $ch = curl_init();
  211. //Set the useragent
  212. //$agent = $_SERVER["HTTP_USER_AGENT"];
  213. //curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  214. //Set the URL
  215. curl_setopt($ch, CURLOPT_URL, $redmine_url );
  216. if($request['type'] == 'post'){
  217. //This is a POST query
  218. curl_setopt($ch, CURLOPT_POST,1);
  219. curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
  220. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  221. 'Content-Type: '.$request['content_type'],
  222. 'Content-Length: ' . strlen($post_data))
  223. );
  224. } elseif ($request['type'] == 'put') {
  225. //This is a PUT query
  226. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  227. curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
  228. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  229. 'Content-Type: '.$request['content_type'],
  230. 'Content-Length: ' . strlen($post_data))
  231. );
  232. }
  233. //We want the content after the query
  234. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  235. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  236. //Follow Location redirects
  237. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  238. //curl_setopt($ch, CURLOPT_HEADER, true);
  239. /*
  240. Set the cookie storing files
  241. Cookie files are necessary since we are logging and session data needs to be saved
  242. */
  243. //curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
  244. //curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
  245. //Execute the action to login
  246. $postResult = curl_exec($ch);
  247. // echo(curl_getinfo($ch, CURLINFO_HTTP_CODE));
  248. // for debug purposes
  249. // if($postResult == false){ return $info = curl_getinfo($ch);}
  250. $response = array( 0 => curl_getinfo($ch, CURLINFO_HTTP_CODE),
  251. 1 => json_decode($postResult));
  252. return $response;
  253. }
  254. }//class_redmine
  255. class class_bitrix_to_redmine extends class_redmine {
  256. protected $redmine_key = '68692662158cb38c8fddb8e084b6dfd55d77b85c';
  257. protected $redmine_url = 'https://tracker.a-real.ru/';
  258. function send_contact($data){
  259. $contact = array("contact" => array("first_name" => $data['last_name'],
  260. "project_id" => "managers",
  261. "phone" => $data["phone_work"],
  262. "email" => $data['webtolead_email1']
  263. )
  264. );
  265. return $this->create_contact($contact);
  266. }
  267. }
  268. ?>

Powered by TurnKey Linux.