2 error_reporting(E_ALL );
4 * StatusNet, the distributed open-source microblogging tool
6 * Superclass(Version2) for plugins that do URL shortening
10 * LICENCE: This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * @author Craig Andrews <candrews@integralblue.com>
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29 define("STATUSNET", 1);
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
36 * Class to handle Plugin exceptions
40 * @author Craig Andrews <candrews@integralblue.com>
41 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44 class PluginErrorException extends Exception {
45 protected $severity=0;
47 * Setup costumizable Exceptions
48 * @var array $HTML Codes for Exceptionlists(see PluginErrorException::getFull())
50 protected $HTML=array(
51 'code'=>array("start"=>'<code>', "end"=>'</code'),
52 'ulist'=>array("start"=>'<ul>', "end"=>'</ul>' ),
53 'list'=>array("start"=>"<li>", "end"=>"</li>"));
55 * Create specific extension for PluginExceptions
58 public function __construct($message, $code, $severity, $filename, $lineno, $HTML='') {
59 $this->message = $message;
61 $this->severity = $severity;
62 $this->file = $filename;
63 $this->line = $lineno;
66 public function getSeverity() {
67 return $this->severity;
69 public function printFull() {
70 echo "Catched Exception: <ul><br />";
71 echo "<li>Message: <code>".$this->getMessage()."</code></li> ";
72 echo "<li>Code: <code>".$this->getCode()."</code></li> ";
73 echo "<li>Line: <code>".$this->getLine()."</code></li> ";
74 echo "<li>File: <code>".$this->getFile()."</code></li> ";
75 echo "<li>Severity: <code>".$this->getSeverity()."</code></li>";
76 echo "</ul><p>Code: 0 means an uncatched Exception, triggered ";
77 echo "by PHP's parsing. This is probally an Syntax-Error.</p> ";
80 /* Do done some exceptionrelated things done, befor
81 * we can start coding the real.
83 function exception_error_handler($errno, $errstr, $errfile, $errline ) {
84 throw new PluginErrorException($errstr, 0, $errno, $errfile, $errline);
87 /* the Exceptionhandler for Syntax-Errors, throwed by PHP itself */
89 * Superclass for plugins that perform a url shortening
93 * @author Craig Andrews <candrews@integralblue.com>
94 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
95 * @link http://darksider3.de/sn/urlshortenerplugin2.html
98 abstract class UrlShortenerPlugin2
100 public $shortenerName;
101 public $freeService = true;
103 * Make an URL shorter.
105 * @param string $url URL to shorten
107 * @return string shortened version of the url/Null/Error
108 * @todo dont support old "shorten", name it to "shortUrl" to get well-named code.
110 protected abstract function shorten($url);
114 * Utility to get the data at an URL
116 * @param string $url URL to fetch
117 * @exception PluginErrorExcpetion $e If HTTPClient throws an exception, print out.
118 * @return string response body
119 * @todo throw an exception in HTTPClient and make the PluginErrorException more generally
120 * so that we can use it more then ones, in every libfile.
122 protected function httpGet($url)
126 $request = HTTPClient::start();
127 $response = $request->get($url);
128 } catch(PluginErrorException $e)
133 return $response->getBody();
136 * Utility to post a request and get a response URL
138 * @param string $url URL to fetch
139 * @param array $data post parameters
141 * @return string response body
144 protected function httpPost($url, $data)
147 $request = HTTPClient::start();
148 $response = $request->post($url, null, $data);
149 return $response->getBody();
154 * Called when all plugins have been initialized
156 * @return boolean hook value
159 function onInitializePlugin()
161 if (!isset($this->shortenerName))
163 throw new Exception("@Admin must specifiy $this->shortenerName");
168 * Called when a showing the URL shortener drop-down box
170 * Properties of the shortening service currently only
171 * include whether it's a free service.
173 * @param array &$shorteners array mapping shortener name to properties
175 * @return boolean hook value
177 function onGetUrlShorteners(&$shorteners)
179 $shorteners[$this->shortenerName] =array('freeService' => $this->freeService);
184 * Called to shorten an URL
186 * @param string $url URL to shorten
187 * @param string $shortenerName Shortening service. Don't handle if it's
189 * @param string &$shortenedUrl URL after shortening; out param.
191 * @return boolean hook value
194 function onStartShortenUrl($url, $shortenerName, &$shortenedUrl)
196 if ($shortenerName == $this->shortenerName)
198 $result = $this->shorten($url);
199 if (isset($result) && $result != null && $result !== false)
201 $shortenedUrl = $result;
202 //dont create an exception, so we can do that without.
204 __CLASS__ . ": $this->shortenerName ".
205 "shortened $url to $shortenedUrl");