| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- <?php
- /*
- * URLYarLib Name: urlyar.shared - Shared JZ Shortener Components
- * URLYarLib Description: This class contains the request gateway for shortening
- * classname: urlyar_interface
- * version: 1.1.0
- * link: https://salamzadeh.net
- * author: Sasan Salamzadeh <me@salamzadeh.net>
- */
- /*
- Full Description:
- This class contains the request gateway for shortening..
- Where possible, it makes use of the WP_HTTP class, but will fall back on basic methods
- when the WP_HTTP class is not available.
- It's meant to be extended / inherited, but can be used as is.
- */
- if ( !class_exists('urlyar_shared') ) :
- class urlyar_shared {
-
- /*
- *****************************************
- *
- * Class Variables
- *
- *****************************************
- */
- protected $status = 'stable', //stable, beta, dev, trial?
- $user = '', //Username if required
- $key = '', //API key if required
- $url = '', //URL to shorten
- $loaded_api = '', //API details and other definitions
- $api_name = '', //Name of service
- $generic = ''; //generic fields (Optional)
-
- /*
- *****************************************
- *
- * Constructors
- *
- *****************************************
- */
- //php 5.3.3
- public function __construct() {
- $this->urlyar_shared($status);
- }
-
- //backward compatibility
- public function urlyar_shared($status){
- $this->status = $status;
- }
-
-
- /*
- *****************************************
- *
- * Configuration
- *
- *****************************************
- */
- public function config($key, $user, $loaded_api, $generic = ''){
- $this->user = $user;
- $this->key = $key;
- $this->loaded_api = $loaded_api;
- $this->generic = $generic;
- }
- /*
- *****************************************
- *
- * Data Processing
- *
- *****************************************
- */
- /*
- *****************************************
- * JSON Parser
- * Use PHP built in fx where available
- *****************************************
- */
- public function json_process($item){
- if ( class_exists('Services_JSON') ){
- $json = new Services_JSON();
- $result = $json->decode($item);
- }else{
- $result = json_decode($item, false);
- }
- return $result;
- }
- /*
- *****************************************
- * XML Parser
- *****************************************
- */
- public function xml_process($item){
- if ( class_exists('SimpleXMLElement') ){
- $result = new SimpleXMLElement($item);
- }
- return $result;
- }
- /*
- *****************************************
- *
- * Data Request
- *
- *****************************************
- */
- /*
- *****************************************
- * "Normal" Gateway
- *
- * By default, uses CURL for POST, GET
- * Fallback on file_get_contents for GET
- *****************************************
- */
- protected function request_ALT($request_url, $post_opt = array(), $method = 'GET', $fields = '') {
-
- $data = NULL;
-
- if ( function_exists('curl_init') ) {
- if ( !empty($post_opt['useragent']) ){ ini_set($useragent); }
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $request_url);
- curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HEADER, false);
- if ( !empty($post_opt['useragent']) ){ curl_setopt($ch, CURLOPT_USERAGENT, $post_opt['useragent']); }
- if ($method == 'POST'){
-
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
- }
- if ( !empty($post_opt['httpheader']) ){
- curl_setopt($ch, CURLOPT_HTTPHEADER, $post_opt['httpheader']);
- }
- $data = curl_exec($ch);
- curl_close($ch);
- if ( !empty($post_opt['useragent']) ){ ini_restore('user_agent'); }
- } elseif($method = 'GET') {
- $data = file_get_contents($request_url);
- }
- return $data;
- }
- /*
- *****************************************
- * WordPress Specific Gateway
- * Uses WP_HTTP class methods
- *****************************************
- */
- protected function request_WP($request_url, $post_opt, $method, $body){
- $data = NULL;
-
- if ( class_exists('WP_Http') ){
- $request = new WP_Http;
- $details = array();
- if ($method == 'GET'){
- $result = $request->request( $request_url );
- }else{
- $details['method'] = $method;
- $details['body'] = $body;
- if ( empty($post_opt['useragent']) ){
- $details['user-agent'] = $useragent;
- }
- $result = $request->request( $request_url, $details );
- }
- if($result['body']){
- $data = $result['body'];
- }
-
- }
- return $data;
- }
- /*
- *****************************************
- * Overall Gateway
- *
- * Switches between WordPress or Universal
- * where appropriate
- *
- *****************************************
- */
- public function request_gateway($request_url, $override = false, $method='POST', $post_opt = array(), $body = ''){
- $data = '';
- //check status
- switch ($this->status){
- case 'trial' :
- case 'stable' :
- //check for WP Http
- if ( class_exists('WP_Http') && !$override ){
- $data = $this->request_WP( $request_url, $post_opt, $method, $body);
- } else {
- //use backup methods
-
- $data = $this->request_ALT($request_url, $post_opt, $method, $body);
-
- } //end if WP_HTTP
- break;
- case 'beta' :
- case 'dev' :
- $data = '[ '.$request_url.' ]-[ '.$body.' ]';
- break;
- default:
- break;
- } //end switch
- return $data;
- }
- /*
- *****************************************
- *
- * Misc
- *
- *****************************************
- */
- /*
- *****************************************
- * URL Encoding
- *****************************************
- */
- public function cleanurl(){
- return urlencode($this->url);
- }
- /*
- *****************************************
- * Supported API List generation
- *****************************************
- */
- public function api_list($list){
- $compiled = array();
- foreach ($list as $keys => $values){
- if ( is_int($values['type']) ){
- $compiled[$keys] = array($values['name'], $values['type'], $values['sticky']);
- }
- }
- return $compiled;
- }
- /*
- *****************************************
- * Setting service
- *****************************************
- */
- public function set_service($service, $api_config){
- $this->api_name = $service;
- $this->loaded_api = $api_config;
- }
- //end class
- }
- endif;
-
- ?>
|