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.

310 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
  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 $token = $this->curl_redmine($upload_url,$request,$filecontent);
  23. }
  24. /**
  25. * Uploads an attachment, attaches it to an issue and returns 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. $at_descr = $this->curl_redmine($at_url);
  55. if (!$at_descr) {
  56. return null;
  57. }
  58. var_dump($at_descr);
  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 (need 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 - new data
  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. } else {
  89. $inc = '';
  90. }
  91. if($project_id!='') {
  92. $issue_url = $this->redmine_url.'issues.json?key='.$this->redmine_key.'&project_id='.$project_id.$inc;
  93. }else{
  94. $issue_url = ($issue_id=='')
  95. ?$this->redmine_url.'issues.json?key='.$this->redmine_key.$inc
  96. : $this->redmine_url.'issues/'.$issue_id.'.json?key='.$this->redmine_key.$inc;
  97. }
  98. return $this->curl_redmine($issue_url);
  99. }
  100. /**
  101. * get issue
  102. *
  103. * @param string $issue_id digits only issue id
  104. * @param string $upload_token digits only project id
  105. * @param string $upload_token
  106. * @param array $inc array of string with options to include. Example: array( 0 => "journals", 1 => "attachments" );
  107. * @return mixed $issue - new data
  108. */
  109. function attach_to_issue(string $issue_id,string $upload_token, string $filename, string $filetype) {
  110. $issue_url = $this->redmine_url.'issues/'.$issue_id.'.json?key='.$this->redmine_key;
  111. $update = array( "issue" => array(
  112. "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_id, string $subject, string $description) {
  188. $issue_url = $this->redmine_url."helpdesk/create_ticket.xml?key=".$this->redmine_key;
  189. $issue = array( "ticket" => array (
  190. "issue" => array(
  191. "project_id" => "xtesx",
  192. "tracker_id" => "3",
  193. "subject" => $subject,
  194. "description"=> $description,
  195. ),
  196. "contact" => array ("id" => $contact_id),
  197. ));
  198. $issue_xml = "<?xml version=\"1.0\"?>
  199. <ticket>
  200. <issue>
  201. <project_id></project_id>
  202. <tracker_id>3</tracker_id>
  203. <subject>$subject</subject>
  204. <description>$description</description>
  205. </issue>
  206. <contact>
  207. <id>$contact_id</id>
  208. </contact>
  209. </ticket>";
  210. $request['type'] = 'post';
  211. $request['content_type'] = 'application/xml';
  212. return $this->curl_redmine($issue_url, $request, $issue_xml );
  213. }
  214. //Curl
  215. function curl_redmine(string $redmine_url,array $request=array(),string $post_data='') {
  216. if(!isset($request['type'])){ $request['type']=null; }
  217. if(!isset($request['content_type'])){ $request['content_type']=null; }
  218. //Create a curl object
  219. $ch = curl_init();
  220. //Set the useragent
  221. //$agent = $_SERVER["HTTP_USER_AGENT"];
  222. //curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  223. //Set the URL
  224. curl_setopt($ch, CURLOPT_URL, $redmine_url );
  225. if($request['type'] == 'post'){
  226. //This is a POST query
  227. curl_setopt($ch, CURLOPT_POST,1);
  228. curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
  229. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  230. 'Content-Type: '.$request['content_type'],
  231. 'Content-Length: ' . strlen($post_data))
  232. );
  233. } elseif ($request['type'] == 'put') {
  234. //This is a PUT query
  235. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  236. curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
  237. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  238. 'Content-Type: '.$request['content_type'],
  239. 'Content-Length: ' . strlen($post_data))
  240. );
  241. }
  242. //We want the content after the query
  243. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  244. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  245. //Follow Location redirects
  246. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  247. //curl_setopt($ch, CURLOPT_HEADER, true);
  248. /*
  249. Set the cookie storing files
  250. Cookie files are necessary since we are logging and session data needs to be saved
  251. */
  252. //curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
  253. //curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
  254. //Execute the action to login
  255. $postResult = curl_exec($ch);
  256. echo(curl_getinfo($ch, CURLINFO_HTTP_CODE));
  257. // for debug purposes
  258. // if($postResult == false){ return $info = curl_getinfo($ch);}
  259. $response = json_decode($postResult);
  260. return $response;
  261. }
  262. }//class_redmine
  263. class class_bitrix_to_redmine extends class_redmine {
  264. protected $redmine_key = '68692662158cb38c8fddb8e084b6dfd55d77b85c';
  265. protected $redmine_url = 'https://tracker.a-real.ru/';
  266. function send_contact($data){
  267. $contact = array("contact" => array("first_name" => $data['last_name'],
  268. "project_id" => "managers",
  269. "phone" => $data["phone_work"],
  270. "email" => $data['webtolead_email1']
  271. )
  272. );
  273. return $this->create_contact($contact);
  274. }
  275. }
  276. ?>

Powered by TurnKey Linux.