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);
86 set_error_handler("exception_error_handler", E_ALL);
88 /* the Exceptionhandler for Syntax-Errors, throwed by PHP itself */
90 * Superclass for plugins that perform a url shortening
94 * @author Leon Giesenkaemper <leon@darksider3.de>
95 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
96 * @link http://darksider3.de/sn/urlshortenerplugin2.html
99 abstract class UrlShortenerPlugin2
101 public $shortenerName;
102 public $freeService = true;
104 * Make an URL shorter.
106 * @param string $url URL to shorten
108 * @return string shortened version of the url/Null/Error
109 * @todo dont support old "shorten", name it to "shortUrl" to get well-named code.
111 protected abstract function shorten($url);
115 * Utility to get the data at an URL
117 * @param string $url URL to fetch
118 * @exception PluginErrorExcpetion $e If HTTPClient throws an exception, print out.
119 * @return string response body
120 * @todo throw an exception in HTTPClient and make the PluginErrorException more generally
121 * so that we can use it more then ones, in every libfile.
123 protected function httpGet($url)
127 //Or create exception here, by looking for 'false'...? Hmm..
128 //No, let HTTPClient throw it. But then we need a more generally
130 $request = HTTPClient::start();
131 $response = $request->get($url);
133 } catch(PluginErrorException $e)
138 return $response->getBody();
141 * Utility to post a request and get a response URL
143 * @param string $url URL to fetch
144 * @param array $data post parameters
146 * @return string response body
148 * @todo self with exceptions as in httpGet()
150 protected function httpPost($url, $data)
153 $request = HTTPClient::start();
154 $response = $request->post($url, null, $data);
156 } catch(PluginErrorException $e)
161 return $response->getBody();
166 * Called when all plugins have been initialized
168 * @return boolean hook value
171 function onInitializePlugin()
173 if (!isset($this->shortenerName))
175 throw new Exception("@Admin must specifiy $this->shortenerName");
180 * Called when a showing the URL shortener drop-down box
182 * Properties of the shortening service currently only
183 * include whether it's a free service.
185 * @param array &$shorteners array mapping shortener name to properties
187 * @return boolean hook value
189 function onGetUrlShorteners(&$shorteners)
191 $shorteners[$this->shortenerName] =array('freeService' => $this->freeService);
196 * Called to shorten an URL
198 * @param string $url URL to shorten
199 * @param string $shortenerName Shortening service. Don't handle if it's
201 * @param string &$shortenedUrl URL after shortening; out param.
203 * @return boolean hook value
206 function onStartShortenUrl($url, $shortenerName, &$shortenedUrl)
208 if ($shortenerName == $this->shortenerName)
210 $result = $this->shorten($url);
211 if (isset($result) && $result != null && $result !== false)
213 $shortenedUrl = $result;
214 //dont create an exception, so we can do that without.
215 //If needed, i can code done..but actually it seems useless to do that.
217 __CLASS__ . ": $this->shortenerName ".
218 "shortened $url to $shortenedUrl");
227 $foo->bar(); // Purposely cause error
236 } catch (PluginErrorException $e) {
237 echo $e->printFull();