]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Url/Shorten/Abstract.php
607d1654c4157bc9e48ff6b328c015d8f450b21b
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / Url / Shorten / Abstract.php
1 <?php
2 /**
3  * Phergie 
4  *
5  * PHP version 5
6  *
7  * LICENSE
8  *
9  * This source file is subject to the new BSD license that is bundled
10  * with this package in the file LICENSE.
11  * It is also available through the world-wide-web at this URL:
12  * http://phergie.org/license
13  *
14  * @category  Phergie 
15  * @package   Phergie_Plugin_Php
16  * @author    Phergie Development Team <team@phergie.org>
17  * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18  * @license   http://phergie.org/license New BSD License
19  * @link      http://pear.phergie.org/package/Phergie_Plugin_Php
20  */
21
22 /**
23  * URL shortener abstract class
24  *
25  * @category Phergie 
26  * @package  Phergie_Plugin_Url
27  * @author   Phergie Development Team <team@phergie.org>
28  * @license  http://phergie.org/license New BSD License
29  * @link     http://pear.phergie.org/package/Phergie_Plugin_Url
30  * @uses     Phergie_Plugin_Http pear.phergie.org
31  */
32 abstract class Phergie_Plugin_Url_Shorten_Abstract
33 {
34     protected $http;
35
36     /**
37      * Constructor
38      *
39      * @param Phergie_Plugin_Http $http instance of the http plugin
40      */
41     public function __construct(Phergie_Plugin_Http $http)
42     {
43         $this->http = $http;
44     }
45
46     /**
47      * Returns an array of request parameters given a url to shorten. The
48      * following keys are valid request parameters:
49      *
50      *  * 'uri': the URI for the request (required)
51      *  * 'query': an array of key-value pairs sent in a GET request
52      *  * 'post': an array of key-value pairs sent in a POST request
53      *  * 'callback': to be called after the request is finished. Should accept
54      *    a Phergie_Plugin_Http_Response object and return either the shortened
55      *    url or false if an error has occured.
56      *
57      * If the 'post' key is present a POST request shall be made; otherwise
58      * a GET request will be made. The 'post' key can be an empty array and
59      * a post request will still be made.
60      *
61      * If no callback is provided the contents of the response will be returned.
62      *
63      * @param string $url the url to shorten
64      *
65      * @return array the request parameters
66      */
67     protected abstract function getRequestParams($url);
68
69     /**
70      * Shortens a given url.
71      *
72      * @param string $url the url to shorten
73      *
74      * @return string the shortened url or false on a failure
75      */
76     public function shorten($url)
77     {
78         $defaults = array('get' => array(), 'post' => array(), 'callback' => null);
79         $options = array('timeout' => 2);
80         $params = $this->getRequestParams($url) + $defaults;
81
82         // Should some kind of notice be thrown? Maybe just if getRequestParams does not return an array?
83         if (!is_array($params) || empty($params['uri'])) {
84             return $url;
85         }
86
87         if (!empty($params['post'])) {
88             $response = $this->http->post($params['uri'], $params['get'], $params['post'], $options);
89         } else {
90             $response = $this->http->get($params['uri'], $params['get'], $options);
91         }
92
93         if (is_callable($params['callback'])) {
94             return call_user_func($params['callback'], $response);
95         }
96
97         $code = $response->getCode();
98         $content = trim($response->getContent);
99         if ($code < 200 || $code >= 300 || empty($content)) {
100             return false;
101         }
102
103         return $response->getContent();
104     }
105 }