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.

336 lines
12 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
  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 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. * @return array 0 => http response, 1 => $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 array 0 => http response, 1 => $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 nowadays we don't use it.
  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. /**
  148. * returns json structure with all contacts
  149. *
  150. * @return mixed array 0 => http response, 1 => $contacts - data
  151. */
  152. function get_contacts() {
  153. $cont_url = $this->redmine_url.'contacts.json?key='.$this->redmine_key;
  154. return $this->curl_redmine($cont_url);
  155. }
  156. /**
  157. * search contacts by string. Returns contacts.
  158. *
  159. * @param string $search_str string to search by
  160. * @return mixed array 0 => http response, 1 => $contacts - new data
  161. */
  162. function search_contacts_str(string $search_str) {
  163. $cont_url = $this->redmine_url.'contacts.json?key='.$this->redmine_key.'&search='.$search_str;
  164. return $this->curl_redmine($cont_url);
  165. }
  166. /**
  167. * search contacts by id. Returns one contact.
  168. *
  169. * @param string $id digits only contact id
  170. * @param bool $issues if true, includes issues, connected to contact
  171. * @return mixed array 0 => http response, 1 => $contact - data
  172. */
  173. function get_contact_id(string $id, bool $issues) {
  174. $cont_url = ($issues)
  175. ? $this->redmine_url."contacts/$id.json?key=".$this->redmine_key.'&include=issues'
  176. : $this->redmine_url."contacts/$id.json?key=".$this->redmine_key;
  177. echo($cont_url);
  178. return $this->curl_redmine($cont_url);
  179. }
  180. /**
  181. * Creates contact from array.
  182. *
  183. * @param array $contact array with all needed contact fields
  184. * @return mixed array 0 => http response, 1 => $contact - data
  185. */
  186. function create_contact(array $contact) {
  187. $cont_url = $this->redmine_url.'contacts.json?key='.$this->redmine_key;
  188. $request['type'] = 'post';
  189. $request['content_type'] = 'application/json';
  190. $post_data = json_encode($contact);
  191. return $this->curl_redmine($cont_url,$request,$post_data);
  192. }
  193. /**
  194. * attach contact to issue
  195. *
  196. * @param string $issue_id digits only issue id
  197. * @param string $contact_id digits only contact id
  198. * @return mixed array 0 => http response, 1 => $issue - new data
  199. */
  200. function attach_issue_to_contact(string $issue_id,string $contact_id) {
  201. $contact_url = $this->redmine_url."contacts/".$contact_id."?key=".$this->redmine_key;
  202. $update = array( "contact" => array(
  203. "issues"=> array( 0=> array("id" => $issue_id))
  204. )
  205. );
  206. $request['type'] = 'put';
  207. $request['content_type'] = 'application/json';
  208. $post_data = json_encode($update);
  209. return $this->curl_redmine($contact_url, $request , $post_data);
  210. }
  211. /**
  212. * create_ticket creates issue with connected contact. Needs contact email.
  213. *
  214. * @param string $issue_id digits only issue id
  215. * @param string $contact_id digits only contact id
  216. * @return mixed array 0 => http response, 1 => $issue - new data
  217. */
  218. function create_ticket(string $contact_email, string $subject, string $description) {
  219. $issue_url = $this->redmine_url."helpdesk/create_ticket.xml?key=".$this->redmine_key;
  220. $issue_xml = "<?xml version=\"1.0\"?>
  221. <ticket>
  222. <issue>
  223. <project_id>14</project_id>
  224. <tracker_id>3</tracker_id>
  225. <subject>$subject</subject>
  226. <description>$description</description>
  227. </issue>
  228. <contact>
  229. <email>$contact_email</email>
  230. </contact>
  231. </ticket>";
  232. $request['type'] = 'post';
  233. $request['content_type'] = 'application/xml';
  234. return $this->curl_redmine($issue_url, $request, $issue_xml );
  235. }
  236. //Curl
  237. function curl_redmine(string $redmine_url,array $request=array(),string $post_data='') {
  238. if(!isset($request['type'])){ $request['type']=null; }
  239. if(!isset($request['content_type'])){ $request['content_type']=null; }
  240. //Create a curl object
  241. $ch = curl_init();
  242. //Set the useragent
  243. //$agent = $_SERVER["HTTP_USER_AGENT"];
  244. //curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  245. //Set the URL
  246. curl_setopt($ch, CURLOPT_URL, $redmine_url );
  247. if($request['type'] == 'post'){
  248. //This is a POST query
  249. curl_setopt($ch, CURLOPT_POST,1);
  250. curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
  251. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  252. 'Content-Type: '.$request['content_type'],
  253. 'Content-Length: ' . strlen($post_data))
  254. );
  255. } elseif ($request['type'] == 'put') {
  256. //This is a PUT query
  257. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  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. }
  264. //We want the content after the query
  265. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  266. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  267. //Follow Location redirects
  268. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  269. //curl_setopt($ch, CURLOPT_HEADER, true);
  270. /*
  271. Set the cookie storing files
  272. Cookie files are necessary since we are logging and session data needs to be saved
  273. */
  274. //curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
  275. //curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
  276. //Execute the action to login
  277. $postResult = curl_exec($ch);
  278. // echo(curl_getinfo($ch, CURLINFO_HTTP_CODE));
  279. // for debug purposes
  280. // if($postResult == false){ return $info = curl_getinfo($ch);}
  281. $response = array( 0 => curl_getinfo($ch, CURLINFO_HTTP_CODE),
  282. 1 => json_decode($postResult));
  283. return $response;
  284. }
  285. }//class_redmine
  286. class class_bitrix_to_redmine extends class_redmine {
  287. protected $redmine_key = '68692662158cb38c8fddb8e084b6dfd55d77b85c';
  288. protected $redmine_url = 'https://tracker.a-real.ru/';
  289. function send_contact($data){
  290. $contact = array("contact" => array("first_name" => $data['last_name'],
  291. "project_id" => "managers",
  292. "phone" => $data["phone_work"],
  293. "email" => $data['webtolead_email1']
  294. )
  295. );
  296. return $this->create_contact($contact);
  297. }
  298. }
  299. ?>

Powered by TurnKey Linux.