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.

344 lines
13 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
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. * Uploads $filecontent and returns token.
  14. *
  15. * @param mixed $filecontent - content of file
  16. * @return array 0 => http response, 1 => $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 array 0 => http response, 1 => $issue updated issue
  32. */
  33. function upload_attachment(string $issue_id, string $filename, string $filetype, $filecontent){
  34. $token = $this->get_upload_token($filecontent)[1];
  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 array 0 => http response, 1 => $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 array 0 => http response, 1 => $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. * possible options:
  83. * *children
  84. * *attachments
  85. * *relations
  86. * *changesets
  87. * *journals
  88. * *watchers
  89. *
  90. * @return array 0 => http response, 1 => $issue - json data of issue
  91. */
  92. function get_issue(string $issue_id='',string $project_id='', array $inc=array()) {
  93. if (count($inc) > 0) {
  94. $inc = implode(",", $inc);
  95. $inc = '&include='.$inc;
  96. var_dump($inc);
  97. } else {
  98. $inc = '';
  99. }
  100. if($project_id!='') {
  101. $issue_url = $this->redmine_url.'issues.json?key='.$this->redmine_key.'&project_id='.$project_id.$inc;
  102. }else{
  103. $issue_url = ($issue_id=='')
  104. ?$this->redmine_url.'issues.json?key='.$this->redmine_key.$inc
  105. : $this->redmine_url.'issues/'.$issue_id.'.json?key='.$this->redmine_key.$inc;
  106. }
  107. return $this->curl_redmine($issue_url);
  108. }
  109. /**
  110. * attach to issue
  111. *
  112. * @param string $issue_id digits only issue id
  113. * @param string $upload_token digits only project id
  114. * @param string $upload_token
  115. * @param array $inc array of string with options to include. Example: array( 0 => "journals", 1 => "attachments" );
  116. * @return array 0 => http responce; 1 => issue - new data
  117. */
  118. function attach_to_issue(string $issue_id,string $upload_token, string $filename, string $filetype) {
  119. $issue_url = $this->redmine_url.'issues/'.$issue_id.'.json?key='.$this->redmine_key;
  120. $update = array( "issue" => array( "uploads"=> array( 0 => array("token" => $upload_token,
  121. "filename" => $filename,
  122. "content_type"=> $filetype )
  123. )
  124. )
  125. );
  126. $request['type'] = 'put';
  127. $request['content_type'] = 'application/json';
  128. $post_data = json_encode($update);
  129. return $this->curl_redmine($issue_url, $request , $post_data);
  130. }
  131. /**
  132. * add comment to issue
  133. *
  134. * @param string $issue_id digits only issue id
  135. * @param string $comment comment to add to the issue
  136. * @return mixed array 0 => http response, 1 => $issue - new data
  137. */
  138. function post_issue_comment(string $issue_id,string $comment) {
  139. $issue_url = $this->redmine_url.'issues/'.$issue_id.'.json?key='.$this->redmine_key;
  140. $update = array( "issue" => array(
  141. "notes"=> $comment,
  142. )
  143. );
  144. $request['type'] = 'put';
  145. $request['content_type'] = 'application/json';
  146. $post_data = json_encode($update);
  147. return $this->curl_redmine($issue_url, $request , $post_data);
  148. }
  149. //Projects, nowadays we don't use it.
  150. function get_projects($project_id='') {
  151. $proj_url = ($project_id=='')?$this->redmine_url.'projects.json?key='.$this->redmine_key : $this->redmine_url.'projects/'.$project_id.'.json?key='.$this->redmine_key;
  152. return $this->curl_redmine($proj_url);
  153. }
  154. //Contacts
  155. /**
  156. * returns json structure with all contacts
  157. *
  158. * @return mixed array 0 => http response, 1 => $contacts - data
  159. */
  160. function get_contacts() {
  161. $cont_url = $this->redmine_url.'contacts.json?key='.$this->redmine_key;
  162. return $this->curl_redmine($cont_url);
  163. }
  164. /**
  165. * search contacts by string. Returns contacts.
  166. *
  167. * @param string $search_str string to search by
  168. * @return mixed array 0 => http response, 1 => $contacts - new data
  169. */
  170. function search_contacts_str(string $search_str) {
  171. $cont_url = $this->redmine_url.'contacts.json?key='.$this->redmine_key.'&search='.$search_str;
  172. return $this->curl_redmine($cont_url);
  173. }
  174. /**
  175. * search contacts by id. Returns one contact.
  176. *
  177. * @param string $id digits only contact id
  178. * @param bool $issues if true, includes issues, connected to contact
  179. * @return mixed array 0 => http response, 1 => $contact - data
  180. */
  181. function get_contact_id(string $id, bool $issues) {
  182. $cont_url = ($issues)
  183. ? $this->redmine_url."contacts/$id.json?key=".$this->redmine_key.'&include=issues'
  184. : $this->redmine_url."contacts/$id.json?key=".$this->redmine_key;
  185. echo($cont_url);
  186. return $this->curl_redmine($cont_url);
  187. }
  188. /**
  189. * Creates contact from array.
  190. *
  191. * @param array $contact array with all needed contact fields
  192. * @return mixed array 0 => http response, 1 => $contact - data
  193. */
  194. function create_contact(array $contact) {
  195. $cont_url = $this->redmine_url.'contacts.json?key='.$this->redmine_key;
  196. $request['type'] = 'post';
  197. $request['content_type'] = 'application/json';
  198. $post_data = json_encode($contact);
  199. return $this->curl_redmine($cont_url,$request,$post_data);
  200. }
  201. /**
  202. * attach contact to issue
  203. *
  204. * @param string $issue_id digits only issue id
  205. * @param string $contact_id digits only contact id
  206. * @return mixed array 0 => http response, 1 => $issue - new data
  207. */
  208. function attach_issue_to_contact(string $issue_id,string $contact_id) {
  209. $contact_url = $this->redmine_url."contacts/".$contact_id."?key=".$this->redmine_key;
  210. $update = array( "contact" => array(
  211. "issues"=> array( 0=> array("id" => $issue_id))
  212. )
  213. );
  214. $request['type'] = 'put';
  215. $request['content_type'] = 'application/json';
  216. $post_data = json_encode($update);
  217. return $this->curl_redmine($contact_url, $request , $post_data);
  218. }
  219. /**
  220. * create_ticket creates issue with connected contact. Needs contact email.
  221. *
  222. * @param string $issue_id digits only issue id
  223. * @param string $contact_email contact email
  224. * @return mixed array 0 => http response, 1 => $issue - new data
  225. */
  226. function create_ticket(string $contact_email, string $subject, string $description) {
  227. $issue_url = $this->redmine_url."helpdesk/create_ticket.xml?key=".$this->redmine_key;
  228. $issue_xml = "<?xml version=\"1.0\"?>
  229. <ticket>
  230. <issue>
  231. <project_id>14</project_id>
  232. <tracker_id>3</tracker_id>
  233. <subject>$subject</subject>
  234. <description>$description</description>
  235. </issue>
  236. <contact>
  237. <email>$contact_email</email>
  238. </contact>
  239. </ticket>";
  240. $request['type'] = 'post';
  241. $request['content_type'] = 'application/xml';
  242. return $this->curl_redmine($issue_url, $request, $issue_xml );
  243. }
  244. //Curl
  245. function curl_redmine(string $redmine_url,array $request=array(),string $post_data='') {
  246. if(!isset($request['type'])){ $request['type']=null; }
  247. if(!isset($request['content_type'])){ $request['content_type']=null; }
  248. //Create a curl object
  249. $ch = curl_init();
  250. //Set the useragent
  251. //$agent = $_SERVER["HTTP_USER_AGENT"];
  252. //curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  253. //Set the URL
  254. curl_setopt($ch, CURLOPT_URL, $redmine_url );
  255. if($request['type'] == 'post'){
  256. //This is a POST query
  257. curl_setopt($ch, CURLOPT_POST,1);
  258. curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
  259. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  260. 'Content-Type: '.$request['content_type'],
  261. 'Content-Length: ' . strlen($post_data))
  262. );
  263. } elseif ($request['type'] == 'put') {
  264. //This is a PUT query
  265. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  266. curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
  267. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  268. 'Content-Type: '.$request['content_type'],
  269. 'Content-Length: ' . strlen($post_data))
  270. );
  271. }
  272. //We want the content after the query
  273. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  274. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  275. //Follow Location redirects
  276. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  277. //curl_setopt($ch, CURLOPT_HEADER, true);
  278. /*
  279. Set the cookie storing files
  280. Cookie files are necessary since we are logging and session data needs to be saved
  281. */
  282. //curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
  283. //curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
  284. //Execute the action to login
  285. $postResult = curl_exec($ch);
  286. // echo(curl_getinfo($ch, CURLINFO_HTTP_CODE));
  287. // for debug purposes
  288. // if($postResult == false){ return $info = curl_getinfo($ch);}
  289. $response = array( 0 => curl_getinfo($ch, CURLINFO_HTTP_CODE),
  290. 1 => json_decode($postResult));
  291. return $response;
  292. }
  293. }//class_redmine
  294. class class_bitrix_to_redmine extends class_redmine {
  295. protected $redmine_key = '68692662158cb38c8fddb8e084b6dfd55d77b85c';
  296. protected $redmine_url = 'https://tracker.a-real.ru/';
  297. function send_contact($data){
  298. $contact = array("contact" => array("first_name" => $data['last_name'],
  299. "project_id" => "managers",
  300. "phone" => $data["phone_work"],
  301. "email" => $data['webtolead_email1']
  302. )
  303. );
  304. return $this->create_contact($contact);
  305. }
  306. }
  307. ?>

Powered by TurnKey Linux.