= 5.2 * * LICENCE: This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . * * @category Plugin * @package StatusNet * @author Craig Andrews * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @link http://gnu.io */ define("STATUSNET", 1); if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); } /** * Class to handle Plugin exceptions * * @category Exception * @package StatusNet * @author Craig Andrews * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @link http://gnu.io */ class PluginErrorException extends Exception { protected $severity=0; /** * Setup costumizable Exceptions * @var array $HTML Codes for Exceptionlists(see PluginErrorException::getFull()) */ protected $HTML=array( 'code'=>array("start"=>'', "end"=>'array("start"=>'
    ', "end"=>'
' ), 'list'=>array("start"=>"
  • ", "end"=>"
  • ")); /** * Create specific extension for PluginExceptions * @ */ public function __construct($message, $code, $severity, $filename, $lineno, $HTML='') { $this->message = $message; $this->code = $code; $this->severity = $severity; $this->file = $filename; $this->line = $lineno; } public function getSeverity() { return $this->severity; } public function printFull() { echo "Catched Exception:

      "; echo "
    • Message: ".$this->getMessage()."
    • "; echo "
    • Code: ".$this->getCode()."
    • "; echo "
    • Line: ".$this->getLine()."
    • "; echo "
    • File: ".$this->getFile()."
    • "; echo "
    • Severity: ".$this->getSeverity()."
    • "; echo "

    Code: 0 means an uncatched Exception, triggered "; echo "by PHP's parsing. This is probally an Syntax-Error.

    "; } } /* Do done some exceptionrelated things done, befor * we can start coding the real. */ function exception_error_handler($errno, $errstr, $errfile, $errline ) { throw new PluginErrorException($errstr, 0, $errno, $errfile, $errline); } /* the Exceptionhandler for Syntax-Errors, throwed by PHP itself */ /** * Superclass for plugins that perform a url shortening * * @category Plugin * @package StatusNet * @author Craig Andrews * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @link http://darksider3.de/sn/urlshortenerplugin2.html */ abstract class UrlShortenerPlugin2 { public $shortenerName; public $freeService = true; /** * Make an URL shorter. * * @param string $url URL to shorten * * @return string shortened version of the url/Null/Error * @todo dont support old "shorten", name it to "shortUrl" to get well-named code. */ protected abstract function shorten($url); /** * Utility to get the data at an URL * * @param string $url URL to fetch * @exception PluginErrorExcpetion $e If HTTPClient throws an exception, print out. * @return string response body * @todo throw an exception in HTTPClient and make the PluginErrorException more generally * so that we can use it more then ones, in every libfile. */ protected function httpGet($url) { try { $request = HTTPClient::start(); $response = $request->get($url); } catch(PluginErrorException $e) { $e->printFull(); return false; } return $response->getBody(); } /** * Utility to post a request and get a response URL * * @param string $url URL to fetch * @param array $data post parameters * * @return string response body * */ protected function httpPost($url, $data) { $request = HTTPClient::start(); $response = $request->post($url, null, $data); return $response->getBody(); } // Hook handlers /** * Called when all plugins have been initialized * * @return boolean hook value */ function onInitializePlugin() { if (!isset($this->shortenerName)) { throw new Exception("@Admin must specifiy $this->shortenerName"); } return true; } /** * Called when a showing the URL shortener drop-down box * * Properties of the shortening service currently only * include whether it's a free service. * * @param array &$shorteners array mapping shortener name to properties * * @return boolean hook value */ function onGetUrlShorteners(&$shorteners) { $shorteners[$this->shortenerName] =array('freeService' => $this->freeService); return true; } /** * Called to shorten an URL * * @param string $url URL to shorten * @param string $shortenerName Shortening service. Don't handle if it's * not you! * @param string &$shortenedUrl URL after shortening; out param. * * @return boolean hook value */ function onStartShortenUrl($url, $shortenerName, &$shortenedUrl) { if ($shortenerName == $this->shortenerName) { $result = $this->shorten($url); if (isset($result) && $result != null && $result !== false) { $shortenedUrl = $result; //dont create an exception, so we can do that without. common_log(LOG_INFO, __CLASS__ . ": $this->shortenerName ". "shortened $url to $shortenedUrl"); return false; } } return true; } } ?>