<?php # Redmine Api

 class class_redmine{
	    #Trackers All
		function  get_trackers(){ 
			global $redmine_url , $redmine_key;
			$tracker_url = $redmine_url.'trackers.json?key='.$redmine_key;
			return $this->curl_redmine($tracker_url);
		}
		
		#Issue
		function create_issue($post_data){ //create new issue with/without attachment,cutom fields
			global $redmine_url , $redmine_key;
			$issue_url = $redmine_url.'issues.json?key='.$redmine_key;
			$request['type'] = 'post';
			$request['content_type'] = 'application/json';
			return $this->curl_redmine($issue_url,$request,$post_data);
		}
		
		function  get_issue($issue_id='',$project_id=''){  //get all issues by issue_id or by project_id
			global $redmine_url , $redmine_key;
			if($project_id!=''){
					$issue_url = $redmine_url.'issues.json?key='.$redmine_key.'&project_id='.$project_id;
			}else{ $issue_url = ($issue_id=='')?$redmine_url.'issues.json?key='.$redmine_key : $redmine_url.'issues/'.$issue_id.'.json?key='.$redmine_key;	
			}
			return $this->curl_redmine($issue_url,'','');
		}
		
		function  delete_issue($issue_id){ //delete issue by issue_id
			global $redmine_url , $redmine_key;
			$issue_url =  $redmine_url.'issues/'.$issue_id.'.json?key='.$redmine_key;				
			$request['type'] = 'DELETE';
			$request['content_type'] = 'application/json';
			return $this->curl_redmine($issue_url,$request);
		}
		
		#Projects
		function  get_projects($project_id=''){ //get all project or project by project_id
			global $redmine_url , $redmine_key;
			$proj_url = ($project_id=='')?$redmine_url.'projects.json?key='.$redmine_key : $redmine_url.'projects/'.$project_id.'.json?key='.$redmine_key;			
			return $this->curl_redmine($proj_url,'','');
		}
		#uploade file & get token
		function get_upload_token($filecontent){
			global $redmine_url , $redmine_key;
			$upload_url = $redmine_url.'uploads.json?key='.$redmine_key;
			$request['type'] = 'post';
			$request['content_type'] = 'application/octet-stream';
			//$filecontent = file_get_contents('test.php');			
			return $token = $this->curl_redmine($upload_url,$request,$filecontent);
			//$token->upload->token;
		}
		#Curl
		function curl_redmine($redmine_url,$request='',$post_data=''){
			if($post_data !='' && $request['content_type']== 'application/json'){ $post_data = json_encode($post_data); }
			if(!isset($request['type'])){ $request['type']=null; }
			if(!isset($request['content_type'])){ $request['content_type']=null; }
			//Create a curl object
			$ch = curl_init(); 
			//Set the useragent
			$agent = $_SERVER["HTTP_USER_AGENT"];
			curl_setopt($ch, CURLOPT_USERAGENT, $agent);
			 
			//Set the URL
			curl_setopt($ch, CURLOPT_URL, $redmine_url );
			if($request['type'] == 'post' || $request['type'] =='DELETE'){ 
				if($request['type'] !='DELETE'){
				//This is a POST query
				curl_setopt($ch, CURLOPT_POST,1);
				}else{
				  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
				}
				//Set the post data
				curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);

				curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
					'Content-Type: '.$request['content_type'],                                                                               
					'Content-Length: ' . strlen($post_data))                                                                       
				);  
			}
			//We want the content after the query
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                  

			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			 
			//Follow Location redirects
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
			 
			/*
			Set the cookie storing files
			Cookie files are necessary since we are logging and session data needs to be saved
			*/
			 
			//curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
			//curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
			 
			//Execute the action to login
			$postResult = curl_exec($ch);
			//if($postResult == false){ return $info = curl_getinfo($ch);}
			$response	=	json_decode($postResult);
			//echo '<pre>'; print_r($response); echo '</pre>';
			return $response;
		}

}//class_redmine

?>