urlyar.shared.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. /*
  3. * URLYarLib Name: urlyar.shared - Shared JZ Shortener Components
  4. * URLYarLib Description: This class contains the request gateway for shortening
  5. * classname: urlyar_interface
  6. * version: 1.1.0
  7. * link: https://salamzadeh.net
  8. * author: Sasan Salamzadeh <me@salamzadeh.net>
  9. */
  10. /*
  11. Full Description:
  12. This class contains the request gateway for shortening..
  13. Where possible, it makes use of the WP_HTTP class, but will fall back on basic methods
  14. when the WP_HTTP class is not available.
  15. It's meant to be extended / inherited, but can be used as is.
  16. */
  17. if ( !class_exists('urlyar_shared') ) :
  18. class urlyar_shared {
  19. /*
  20. *****************************************
  21. *
  22. * Class Variables
  23. *
  24. *****************************************
  25. */
  26. protected $status = 'stable', //stable, beta, dev, trial?
  27. $user = '', //Username if required
  28. $key = '', //API key if required
  29. $url = '', //URL to shorten
  30. $loaded_api = '', //API details and other definitions
  31. $api_name = '', //Name of service
  32. $generic = ''; //generic fields (Optional)
  33. /*
  34. *****************************************
  35. *
  36. * Constructors
  37. *
  38. *****************************************
  39. */
  40. //php 5.3.3
  41. public function __construct() {
  42. $this->urlyar_shared($status);
  43. }
  44. //backward compatibility
  45. public function urlyar_shared($status){
  46. $this->status = $status;
  47. }
  48. /*
  49. *****************************************
  50. *
  51. * Configuration
  52. *
  53. *****************************************
  54. */
  55. public function config($key, $user, $loaded_api, $generic = ''){
  56. $this->user = $user;
  57. $this->key = $key;
  58. $this->loaded_api = $loaded_api;
  59. $this->generic = $generic;
  60. }
  61. /*
  62. *****************************************
  63. *
  64. * Data Processing
  65. *
  66. *****************************************
  67. */
  68. /*
  69. *****************************************
  70. * JSON Parser
  71. * Use PHP built in fx where available
  72. *****************************************
  73. */
  74. public function json_process($item){
  75. if ( class_exists('Services_JSON') ){
  76. $json = new Services_JSON();
  77. $result = $json->decode($item);
  78. }else{
  79. $result = json_decode($item, false);
  80. }
  81. return $result;
  82. }
  83. /*
  84. *****************************************
  85. * XML Parser
  86. *****************************************
  87. */
  88. public function xml_process($item){
  89. if ( class_exists('SimpleXMLElement') ){
  90. $result = new SimpleXMLElement($item);
  91. }
  92. return $result;
  93. }
  94. /*
  95. *****************************************
  96. *
  97. * Data Request
  98. *
  99. *****************************************
  100. */
  101. /*
  102. *****************************************
  103. * "Normal" Gateway
  104. *
  105. * By default, uses CURL for POST, GET
  106. * Fallback on file_get_contents for GET
  107. *****************************************
  108. */
  109. protected function request_ALT($request_url, $post_opt = array(), $method = 'GET', $fields = '') {
  110. $data = NULL;
  111. if ( function_exists('curl_init') ) {
  112. if ( !empty($post_opt['useragent']) ){ ini_set($useragent); }
  113. $ch = curl_init();
  114. curl_setopt($ch, CURLOPT_URL, $request_url);
  115. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
  116. curl_setopt($ch, CURLOPT_HEADER, false);
  117. if ( !empty($post_opt['useragent']) ){ curl_setopt($ch, CURLOPT_USERAGENT, $post_opt['useragent']); }
  118. if ($method == 'POST'){
  119. curl_setopt($ch, CURLOPT_POST, 1);
  120. curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
  121. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
  122. }
  123. if ( !empty($post_opt['httpheader']) ){
  124. curl_setopt($ch, CURLOPT_HTTPHEADER, $post_opt['httpheader']);
  125. }
  126. $data = curl_exec($ch);
  127. curl_close($ch);
  128. if ( !empty($post_opt['useragent']) ){ ini_restore('user_agent'); }
  129. } elseif($method = 'GET') {
  130. $data = file_get_contents($request_url);
  131. }
  132. return $data;
  133. }
  134. /*
  135. *****************************************
  136. * WordPress Specific Gateway
  137. * Uses WP_HTTP class methods
  138. *****************************************
  139. */
  140. protected function request_WP($request_url, $post_opt, $method, $body){
  141. $data = NULL;
  142. if ( class_exists('WP_Http') ){
  143. $request = new WP_Http;
  144. $details = array();
  145. if ($method == 'GET'){
  146. $result = $request->request( $request_url );
  147. }else{
  148. $details['method'] = $method;
  149. $details['body'] = $body;
  150. if ( empty($post_opt['useragent']) ){
  151. $details['user-agent'] = $useragent;
  152. }
  153. $result = $request->request( $request_url, $details );
  154. }
  155. if($result['body']){
  156. $data = $result['body'];
  157. }
  158. }
  159. return $data;
  160. }
  161. /*
  162. *****************************************
  163. * Overall Gateway
  164. *
  165. * Switches between WordPress or Universal
  166. * where appropriate
  167. *
  168. *****************************************
  169. */
  170. public function request_gateway($request_url, $override = false, $method='POST', $post_opt = array(), $body = ''){
  171. $data = '';
  172. //check status
  173. switch ($this->status){
  174. case 'trial' :
  175. case 'stable' :
  176. //check for WP Http
  177. if ( class_exists('WP_Http') && !$override ){
  178. $data = $this->request_WP( $request_url, $post_opt, $method, $body);
  179. } else {
  180. //use backup methods
  181. $data = $this->request_ALT($request_url, $post_opt, $method, $body);
  182. } //end if WP_HTTP
  183. break;
  184. case 'beta' :
  185. case 'dev' :
  186. $data = '[ '.$request_url.' ]-[ '.$body.' ]';
  187. break;
  188. default:
  189. break;
  190. } //end switch
  191. return $data;
  192. }
  193. /*
  194. *****************************************
  195. *
  196. * Misc
  197. *
  198. *****************************************
  199. */
  200. /*
  201. *****************************************
  202. * URL Encoding
  203. *****************************************
  204. */
  205. public function cleanurl(){
  206. return urlencode($this->url);
  207. }
  208. /*
  209. *****************************************
  210. * Supported API List generation
  211. *****************************************
  212. */
  213. public function api_list($list){
  214. $compiled = array();
  215. foreach ($list as $keys => $values){
  216. if ( is_int($values['type']) ){
  217. $compiled[$keys] = array($values['name'], $values['type'], $values['sticky']);
  218. }
  219. }
  220. return $compiled;
  221. }
  222. /*
  223. *****************************************
  224. * Setting service
  225. *****************************************
  226. */
  227. public function set_service($service, $api_config){
  228. $this->api_name = $service;
  229. $this->loaded_api = $api_config;
  230. }
  231. //end class
  232. }
  233. endif;
  234. ?>