]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/urlshortenerplugin2.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / lib / urlshortenerplugin2.php
1 <?php
2 error_reporting(E_ALL );
3 /**
4  * StatusNet, the distributed open-source microblogging tool
5  *
6  * Superclass(Version2) for plugins that do URL shortening
7  *
8  * PHP version >= 5.2
9  *
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.
14  *
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.
19  *
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/>.
22  *
23  * @category Plugin
24  * @package  StatusNet
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
27  * @link     http://gnu.io
28  */
29 define("STATUSNET", 1);
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
31     exit(1);
32 }
33
34
35 /**
36  * Class to handle Plugin exceptions
37  *
38  * @category Exception
39  * @package StatusNet
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
42  * @link     http://gnu.io
43  */
44 class PluginErrorException extends Exception {
45     protected $severity=0;
46     /**
47      * Setup costumizable Exceptions
48      * @var array $HTML Codes for Exceptionlists(see PluginErrorException::getFull())
49      */
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>"));
54     /**
55      * Create specific extension for PluginExceptions
56      * @
57      */
58     public function __construct($message, $code, $severity, $filename, $lineno, $HTML='') {
59         $this->message = $message;
60         $this->code = $code;
61         $this->severity = $severity;
62         $this->file = $filename;
63         $this->line = $lineno;
64     }
65
66     public function getSeverity() {
67         return $this->severity;
68     }
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> ";
78     }
79 }
80 /* Do done some exceptionrelated things done, befor
81  * we can start coding the real.
82  */
83 function exception_error_handler($errno, $errstr, $errfile, $errline ) {
84     throw new PluginErrorException($errstr, 0, $errno, $errfile, $errline);
85 }
86
87 /* the Exceptionhandler for Syntax-Errors, throwed by PHP itself */
88 /**
89  * Superclass for plugins that perform a url shortening
90  *
91  * @category Plugin
92  * @package  StatusNet
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
96  */
97
98 abstract class UrlShortenerPlugin2
99 {
100   public $shortenerName;
101   public $freeService = true;
102   /**
103   * Make an URL shorter.
104   *
105   * @param string $url URL to shorten
106   *
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.
109   */
110   protected abstract function shorten($url);
111
112
113   /**
114    * Utility to get the data at an URL
115    *
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.
121    */
122   protected function httpGet($url)
123   {
124     try
125     {
126       $request  = HTTPClient::start();
127       $response = $request->get($url);
128     } catch(PluginErrorException $e)
129     {
130       $e->printFull();
131       return false;
132     }
133     return $response->getBody();
134   }
135  /**
136   * Utility to post a request and get a response URL
137   *
138   * @param string $url  URL to fetch
139   * @param array  $data post parameters
140   *
141   * @return string response body
142   *
143   */
144   protected function httpPost($url, $data)
145   {
146     
147     $request  = HTTPClient::start();
148     $response = $request->post($url, null, $data);
149     return $response->getBody();
150   }
151   // Hook handlers
152
153   /**
154    * Called when all plugins have been initialized
155    *
156    * @return boolean hook value
157    */
158
159   function onInitializePlugin()
160   {
161     if (!isset($this->shortenerName))
162     {
163       throw new Exception("@Admin must specifiy $this->shortenerName");
164     }
165     return true;
166   }
167   /**
168    * Called when a showing the URL shortener drop-down box
169    *
170    * Properties of the shortening service currently only
171    * include whether it's a free service.
172    *
173    * @param array &$shorteners array mapping shortener name to properties
174    *
175    * @return boolean hook value
176    */
177   function onGetUrlShorteners(&$shorteners)
178   {
179     $shorteners[$this->shortenerName] =array('freeService' => $this->freeService);
180     return true;
181   }
182
183   /**
184    * Called to shorten an URL
185    *
186    * @param string $url           URL to shorten
187    * @param string $shortenerName Shortening service. Don't handle if it's
188    *                              not you!
189    * @param string &$shortenedUrl URL after shortening; out param.
190    *
191    * @return boolean hook value
192    */
193
194   function onStartShortenUrl($url, $shortenerName, &$shortenedUrl)
195   {
196     if ($shortenerName == $this->shortenerName)
197     {
198       $result = $this->shorten($url);
199       if (isset($result) && $result != null && $result !== false)
200       {
201         $shortenedUrl = $result;
202         //dont create an exception, so we can do that without.
203         common_log(LOG_INFO,
204                    __CLASS__ . ": $this->shortenerName ".
205                    "shortened $url to $shortenedUrl");
206         return false;
207       }
208     }
209     return true;
210   }
211 }
212 ?>