NXS_tmhUtilities

TmhUtilities.

Defined (1)

The class is defined in the following location(s).

/inc-cl/apis/tmhUtilities.php  
  1. class NXS_tmhUtilities { 
  2. /** 
  3. * Entifies the tweet using the given entities element 
  4. * @param array $tweet the json converted to normalised array 
  5. * @return the tweet text with entities replaced with hyperlinks 
  6. */ 
  7. function entify($tweet, &$replacements=array()) { 
  8. $encoding = mb_internal_encoding(); 
  9. mb_internal_encoding("UTF-8"); 
  10.  
  11. $keys = array(); 
  12. // $replacements = array(); 
  13. $is_retweet = false; 
  14.  
  15. if (isset($tweet['retweeted_status'])) { 
  16. $tweet = $tweet['retweeted_status']; 
  17. $is_retweet = true; 
  18.  
  19. if (!isset($tweet['entities'])) { 
  20. return $tweet['text']; 
  21.  
  22. // prepare the entities 
  23. foreach ($tweet['entities'] as $type => $things) { 
  24. foreach ($things as $entity => $value) { 
  25. $tweet_link = "<a href=\"http://twitter.com/{$tweet['user']['screen_name']}/statuses/{$tweet['id']}\">{$tweet['created_at']}</a>"; 
  26.  
  27. switch ($type) { 
  28. case 'hashtags': 
  29. $href = "<a href=\"http://twitter.com/search?q=%23{$value['text']}\">#{$value['text']}</a>"; 
  30. break; 
  31. case 'user_mentions': 
  32. $href = "@<a href=\"http://twitter.com/{$value['screen_name']}\" title=\"{$value['name']}\">{$value['screen_name']}</a>"; 
  33. break; 
  34. case 'urls': 
  35. case 'media': 
  36. $url = empty($value['expanded_url']) ? $value['url'] : $value['expanded_url']; 
  37. $display = isset($value['display_url']) ? $value['display_url'] : str_replace('http://', '', $url); 
  38. // Not all pages are served in UTF-8 so you may need to do this ... 
  39. $display = urldecode(str_replace('%E2%80%A6', '…', urlencode($display))); 
  40. $href = "<a href=\"{$value['url']}\">{$display}</a>"; 
  41. break; 
  42. $keys[$value['indices']['0']] = mb_substr( 
  43. $tweet['text'],  
  44. $value['indices']['0'],  
  45. $value['indices']['1'] - $value['indices']['0'] 
  46. ); 
  47. $replacements[$value['indices']['0']] = $href; 
  48.  
  49. ksort($replacements); 
  50. $replacements = array_reverse($replacements, true); 
  51. $entified_tweet = $tweet['text']; 
  52. foreach ($replacements as $k => $v) { 
  53. // $entified_tweet = substr_replace($entified_tweet, $v, $k, strlen($keys[$k])); 
  54. $entified_tweet = mb_substr($entified_tweet, 0, $k).$v.mb_substr($entified_tweet, $k + strlen($keys[$k])); 
  55. $replacements = array( 
  56. 'replacements' => $replacements,  
  57. 'keys' => $keys 
  58. ); 
  59.  
  60. mb_internal_encoding($encoding); 
  61. return $entified_tweet; 
  62.  
  63. /** 
  64. * Returns the current URL. This is instead of PHP_SELF which is unsafe 
  65. * @param bool $dropqs whether to drop the querystring or not. Default true 
  66. * @return string the current URL 
  67. */ 
  68. function php_self($dropqs=true) { 
  69. $url = sprintf('%s://%s%s',  
  70. empty($_SERVER['HTTPS']) ? (@$_SERVER['SERVER_PORT'] == '443' ? 'https' : 'http') : 'http',  
  71. $_SERVER['SERVER_NAME'],  
  72. $_SERVER['REQUEST_URI'] 
  73. ); 
  74.  
  75. $parts = parse_url($url); 
  76.  
  77. $port = $_SERVER['SERVER_PORT']; 
  78. $scheme = $parts['scheme']; 
  79. $host = $parts['host']; 
  80. $path = @$parts['path']; 
  81. $qs = @$parts['query']; 
  82.  
  83. $port or $port = ($scheme == 'https') ? '443' : '80'; 
  84.  
  85. if (($scheme == 'https' && $port != '443') 
  86. || ($scheme == 'http' && $port != '80')) { 
  87. $host = "$host:$port"; 
  88. $url = "$scheme://$host$path"; 
  89. if ( ! $dropqs) 
  90. return "{$url}?{$qs}"; 
  91. else 
  92. return $url; 
  93.  
  94. function is_cli() { 
  95. return (PHP_SAPI == 'cli' && empty($_SERVER['REMOTE_ADDR'])); 
  96.  
  97. /** 
  98. * Debug function for printing the content of an object 
  99. * @param mixes $obj 
  100. */ 
  101. function pr($obj) { 
  102.  
  103. if (!self::is_cli()) 
  104. echo '<pre style="word-wrap: break-word">'; 
  105. if ( is_object($obj) ) 
  106. print_r($obj); 
  107. elseif ( is_array($obj) ) 
  108. print_r($obj); 
  109. else 
  110. echo $obj; 
  111. if (!self::is_cli()) 
  112. echo '</pre>'; 
  113.  
  114. /** 
  115. * Make an HTTP request using this library. This method is different to 'request' 
  116. * because on a 401 error it will retry the request. 
  117. * When a 401 error is returned it is possible the timestamp of the client is 
  118. * too different to that of the API server. In this situation it is recommended 
  119. * the request is retried with the OAuth timestamp set to the same as the API 
  120. * server. This method will automatically try that technique. 
  121. * This method doesn't return anything. Instead the response should be 
  122. * inspected directly. 
  123. * @param string $method the HTTP method being used. e.g. POST, GET, HEAD etc 
  124. * @param string $url the request URL without query string parameters 
  125. * @param array $params the request parameters as an array of key=value pairs 
  126. * @param string $useauth whether to use authentication when making the request. Default true. 
  127. * @param string $multipart whether this request contains multipart data. Default false 
  128. */ 
  129. function auto_fix_time_request($tmhOAuth, $method, $url, $params=array(), $useauth=true, $multipart=false) { 
  130. $tmhOAuth->request($method, $url, $params, $useauth, $multipart); 
  131.  
  132. // if we're not doing auth the timestamp isn't important 
  133. if ( ! $useauth) 
  134. return; 
  135.  
  136. // some error that isn't a 401 
  137. if ($tmhOAuth->response['code'] != 401) 
  138. return; 
  139.  
  140. // some error that is a 401 but isn't because the OAuth token and signature are incorrect 
  141. // TODO: this check is horrid but helps avoid requesting twice when the username and password are wrong 
  142. if (stripos($tmhOAuth->response['response'], 'password') !== false) 
  143. return; 
  144.  
  145. // force the timestamp to be the same as the Twitter servers, and re-request 
  146. $tmhOAuth->auto_fixed_time = true; 
  147. $tmhOAuth->config['force_timestamp'] = true; 
  148. $tmhOAuth->config['timestamp'] = strtotime($tmhOAuth->response['headers']['date']); 
  149. return $tmhOAuth->request($method, $url, $params, $useauth, $multipart); 
  150.  
  151. /** 
  152. * Asks the user for input and returns the line they enter 
  153. * @param string $prompt the text to display to the user 
  154. * @return the text entered by the user 
  155. */ 
  156. function read_input($prompt) { 
  157. echo $prompt; 
  158. $handle = fopen("php://stdin", "r"); 
  159. $data = fgets($handle); 
  160. return trim($data); 
  161.  
  162. /** 
  163. * Get a password from the shell. 
  164. * This function works on *nix systems only and requires shell_exec and stty. 
  165. * @param boolean $stars Wether or not to output stars for given characters 
  166. * @return string 
  167. * @url http://www.dasprids.de/blog/2008/08/22/getting-a-password-hidden-from-stdin-with-php-cli 
  168. */ 
  169. function read_password($prompt, $stars=false) { 
  170. echo $prompt; 
  171. $style = shell_exec('stty -g'); 
  172.  
  173. if ($stars === false) { 
  174. shell_exec('stty -echo'); 
  175. $password = rtrim(fgets(STDIN), "\n"); 
  176. } else { 
  177. shell_exec('stty -icanon -echo min 1 time 0'); 
  178. $password = ''; 
  179. while (true) : 
  180. $char = fgetc(STDIN); 
  181. if ($char === "\n") : 
  182. break; 
  183. elseif (ord($char) === 127) : 
  184. if (strlen($password) > 0) { 
  185. fwrite(STDOUT, "\x08 \x08"); 
  186. $password = substr($password, 0, -1); 
  187. else 
  188. fwrite(STDOUT, "*"); 
  189. $password .= $char; 
  190. endif; 
  191. endwhile; 
  192.  
  193. // Reset 
  194. shell_exec('stty ' . $style); 
  195. echo PHP_EOL; 
  196. return $password; 
  197.  
  198. /** 
  199. * Check if one string ends with another 
  200. * @param string $haystack the string to check inside of 
  201. * @param string $needle the string to check $haystack ends with 
  202. * @return true if $haystack ends with $needle, false otherwise 
  203. */ 
  204. function endswith($haystack, $needle) { 
  205. $haylen = strlen($haystack); 
  206. $needlelen = strlen($needle); 
  207. if ($needlelen > $haylen) 
  208. return false; 
  209.  
  210. return substr_compare($haystack, $needle, -$needlelen) === 0;