]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/urlshortenerplugin.php
Merge branch 'master' of gitorious.org:social/mainline
[quix0rs-gnu-social.git] / lib / urlshortenerplugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Superclass for plugins that do URL shortening
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category Plugin
23  * @package  StatusNet
24  * @author   Craig Andrews <candrews@integralblue.com>
25  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
26  * @link     http://status.net/
27  */
28
29 if (!defined('STATUSNET') && !defined('LACONICA')) {
30     exit(1);
31 }
32
33 /**
34  * Superclass for plugins that do URL shortening
35  *
36  * @category Plugin
37  * @package  StatusNet
38  * @author   Craig Andrews <candrews@integralblue.com>
39  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
40  * @link     http://status.net/
41  */
42
43 abstract class UrlShortenerPlugin extends Plugin
44 {
45     public $shortenerName;
46     public $freeService = false;
47
48     // Url Shortener plugins should implement some (or all)
49     // of these methods
50
51     /**
52      * Make an URL shorter.
53      *
54      * @param string $url URL to shorten
55      *
56      * @return string shortened version of the url, or null on failure
57      */
58
59     protected abstract function shorten($url);
60
61     /**
62      * Utility to get the data at an URL
63      *
64      * @param string $url URL to fetch
65      *
66      * @return string response body
67      *
68      * @todo rename to code-standard camelCase httpGet()
69      */
70
71     protected function http_get($url)
72     {
73         $request  = HTTPClient::start();
74         $response = $request->get($url);
75         return $response->getBody();
76     }
77
78     /**
79      * Utility to post a request and get a response URL
80      *
81      * @param string $url  URL to fetch
82      * @param array  $data post parameters
83      *
84      * @return string response body
85      *
86      * @todo rename to code-standard httpPost()
87      */
88
89     protected function http_post($url, $data)
90     {
91         $request  = HTTPClient::start();
92         $response = $request->post($url, null, $data);
93         return $response->getBody();
94     }
95
96     // Hook handlers
97
98     /**
99      * Called when all plugins have been initialized
100      *
101      * @return boolean hook value
102      */
103
104     function onInitializePlugin()
105     {
106         if (!isset($this->shortenerName)) {
107             throw new Exception("must specify a shortenerName");
108         }
109         return true;
110     }
111
112     /**
113      * Called when a showing the URL shortener drop-down box
114      *
115      * Properties of the shortening service currently only
116      * include whether it's a free service.
117      *
118      * @param array &$shorteners array mapping shortener name to properties
119      *
120      * @return boolean hook value
121      */
122
123     function onGetUrlShorteners(&$shorteners)
124     {
125         $shorteners[$this->shortenerName] =
126           array('freeService' => $this->freeService);
127         return true;
128     }
129
130     /**
131      * Called to shorten an URL
132      *
133      * @param string $url           URL to shorten
134      * @param string $shortenerName Shortening service. Don't handle if it's
135      *                              not you!
136      * @param string &$shortenedUrl URL after shortening; out param.
137      *
138      * @return boolean hook value
139      */
140
141     function onStartShortenUrl($url, $shortenerName, &$shortenedUrl)
142     {
143         if ($shortenerName == $this->shortenerName) {
144             $result = $this->shorten($url);
145             if (isset($result) && $result != null && $result !== false) {
146                 $shortenedUrl = $result;
147                 common_log(LOG_INFO,
148                            __CLASS__ . ": $this->shortenerName ".
149                            "shortened $url to $shortenedUrl");
150                 return false;
151             }
152         }
153         return true;
154     }
155 }