PageRenderTime 63ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/lighthouse/helpers/lighthouse.php

https://github.com/codebase/MoPCMSv1.5
PHP | 219 lines | 135 code | 42 blank | 42 comment | 34 complexity | fd9d841cc9b294c0051943787062fec1 MD5 | raw file
  1. <?
  2. /*
  3. * Class: lighthouse
  4. * Controller which implements remote filing of tickets with a configured lighthouseapp account
  5. */
  6. Class lighthouse {
  7. /*
  8. * Function: newTicket($title, $description, [$options] )
  9. * Files a new ticket with lighthouse using the configured account
  10. * Parameters:
  11. * $title - title for the ticket
  12. * $description - body of the ticket
  13. * $options - array of options as supported by the lighthouseapp api
  14. */
  15. public function newTicket($title, $description, $options = array()){
  16. // Assemble the account url
  17. $url = "http://" . Kohana::config('lighthouse.account') . ".lighthouseapp.com/projects/" . Kohana::config('lighthouse.project_id') . "/tickets.xml";
  18. // Setup the cURL object
  19. $curl = curl_init();
  20. curl_setopt( $curl, CURLOPT_POST, 1);
  21. curl_setopt( $curl, CURLOPT_URL, $url );
  22. curl_setopt( $curl, CURLOPT_USERPWD, (Kohana::config('lighthouse.user') . ":" . Kohana::config('lighthouse.password')) );
  23. // Create the XML to post
  24. $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" .
  25. "<ticket>" .
  26. "<title>" . $title . "</title>" .
  27. "<body>" . $description . "</body>";
  28. foreach($options as $key => $value){
  29. $xml .= "<$key>$value</$key>";
  30. }
  31. $xml .= "</ticket>";
  32. // Setup the right headers for content-type etc.
  33. $header = "X-LighthouseToken: " . Kohana::config('lighthouse.token') . "\r\n";
  34. $header .= "Content-type: application/xml\r\n";
  35. $header .= "Content-length: " . strlen( $xml ) . "\r\n\n"; // Important! Two linebreaks.
  36. $header .= $xml;
  37. curl_setopt( $curl, CURLOPT_HTTPHEADER, array( $header ) );
  38. // Execute the request and get the result
  39. ob_start();
  40. $result = curl_exec( $curl );
  41. ob_end_clean();
  42. curl_close($curl);
  43. }
  44. public function getOpenTicketsWithTag($tag, $page = 1, $previousTickets = array())
  45. {
  46. // Assemble the account url
  47. $url = "http://" . Kohana::config('lighthouse.account') . ".lighthouseapp.com/projects/" . Kohana::config('lighthouse.project_id') . "/tickets.xml";
  48. //add query string to url...
  49. $url .= "?limit=100&page=" . urlencode($page) . "&q=" . urlencode("state:open tagged:" . $tag);
  50. // Setup the cURL object
  51. $curl = curl_init();
  52. curl_setopt( $curl, CURLOPT_HTTPGET, 1);
  53. curl_setopt( $curl, CURLOPT_URL, $url );
  54. curl_setopt( $curl, CURLOPT_USERPWD, (Kohana::config('lighthouse.user') . ":" . Kohana::config('lighthouse.password')) );
  55. // Setup the right headers for content-type etc.
  56. $header = "X-LighthouseToken: " . Kohana::config('lighthouse.token') . "\r\n\n";
  57. curl_setopt( $curl, CURLOPT_HTTPHEADER, array( $header ) );
  58. // Execute the request and get the result
  59. ob_start();
  60. $success = curl_exec($curl);
  61. $result = lighthouse::xml2array(ob_get_clean());
  62. curl_close($curl);
  63. if (isset($result['tickets']))
  64. return array_merge($previousTickets, lighthouse::getOpenTicketsWithTag($tag, $page + 1, $result['tickets']['ticket']));
  65. else
  66. return $previousTickets;
  67. }
  68. /**
  69. * xml2array() will convert the given XML text to an array in the XML structure.
  70. * Link: http://www.bin-co.com/php/scripts/xml2array/
  71. * Arguments : $contents - The XML text
  72. * $get_attributes - 1 or 0. If this is 1 the function will get the attributes as well as the tag values - this results in a different array structure in the return value.
  73. * $priority - Can be 'tag' or 'attribute'. This will change the way the resulting array sturcture. For 'tag', the tags are given more importance.
  74. * Return: The parsed XML in an array form. Use print_r() to see the resulting array structure.
  75. * Examples: $array = xml2array(file_get_contents('feed.xml'));
  76. * $array = xml2array(file_get_contents('feed.xml', 1, 'attribute'));
  77. */
  78. public function xml2array($contents, $get_attributes=1, $priority = 'tag')
  79. {
  80. if(!$contents) return array();
  81. if(!function_exists('xml_parser_create')) {
  82. //print "'xml_parser_create()' function not found!";
  83. return array();
  84. }
  85. //Get the XML parser of PHP - PHP must have this module for the parser to work
  86. $parser = xml_parser_create('');
  87. xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
  88. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  89. xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  90. xml_parse_into_struct($parser, trim($contents), $xml_values);
  91. xml_parser_free($parser);
  92. if(!$xml_values) return;//Hmm...
  93. //Initializations
  94. $xml_array = array();
  95. $parents = array();
  96. $opened_tags = array();
  97. $arr = array();
  98. $current = &$xml_array; //Refference
  99. //Go through the tags.
  100. $repeated_tag_index = array();//Multiple tags with same name will be turned into an array
  101. foreach($xml_values as $data) {
  102. unset($attributes,$value);//Remove existing values, or there will be trouble
  103. //This command will extract these variables into the foreach scope
  104. // tag(string), type(string), level(int), attributes(array).
  105. extract($data);//We could use the array by itself, but this cooler.
  106. $result = array();
  107. $attributes_data = array();
  108. if(isset($value)) {
  109. if($priority == 'tag') $result = $value;
  110. else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
  111. }
  112. //Set the attributes too.
  113. if(isset($attributes) and $get_attributes) {
  114. foreach($attributes as $attr => $val) {
  115. if($priority == 'tag') $attributes_data[$attr] = $val;
  116. else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
  117. }
  118. }
  119. //See tag status and do the needed.
  120. if($type == "open") {//The starting of the tag '<tag>'
  121. $parent[$level-1] = &$current;
  122. if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
  123. $current[$tag] = $result;
  124. if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
  125. $repeated_tag_index[$tag.'_'.$level] = 1;
  126. $current = &$current[$tag];
  127. } else { //There was another element with the same tag name
  128. if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
  129. $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
  130. $repeated_tag_index[$tag.'_'.$level]++;
  131. } else {//This section will make the value an array if multiple tags with the same name appear together
  132. $current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
  133. $repeated_tag_index[$tag.'_'.$level] = 2;
  134. if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
  135. $current[$tag]['0_attr'] = $current[$tag.'_attr'];
  136. unset($current[$tag.'_attr']);
  137. }
  138. }
  139. $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
  140. $current = &$current[$tag][$last_item_index];
  141. }
  142. } elseif($type == "complete") { //Tags that ends in 1 line '<tag />'
  143. //See if the key is already taken.
  144. if(!isset($current[$tag])) { //New Key
  145. $current[$tag] = $result;
  146. $repeated_tag_index[$tag.'_'.$level] = 1;
  147. if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
  148. } else { //If taken, put all things inside a list(array)
  149. if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...
  150. // ...push the new element into that array.
  151. $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
  152. if($priority == 'tag' and $get_attributes and $attributes_data) {
  153. $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
  154. }
  155. $repeated_tag_index[$tag.'_'.$level]++;
  156. } else { //If it is not an array...
  157. $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
  158. $repeated_tag_index[$tag.'_'.$level] = 1;
  159. if($priority == 'tag' and $get_attributes) {
  160. if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
  161. $current[$tag]['0_attr'] = $current[$tag.'_attr'];
  162. unset($current[$tag.'_attr']);
  163. }
  164. if($attributes_data) {
  165. $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
  166. }
  167. }
  168. $repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
  169. }
  170. }
  171. } elseif($type == 'close') { //End of tag '</tag>'
  172. $current = &$parent[$level-1];
  173. }
  174. }
  175. return($xml_array);
  176. }
  177. }