]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/urlshortenerplugin2.php
Add urlshortenerplugin2.php to solve some @todo's and adding exceptions
[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   Leon Giesenkaemper <leon@darksider3.de>
26  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link     http://darksider3.de/sn/urlshortenerplugin2.html
28  */
29 define("STATUSNET", 1); //for manual debugging.
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   Leon Giesenkaemper <leon@darksider3.de>
41  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
42  * @link     http://darksider3.de/sn/urlshortenerplugin2.html#exceptions
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 set_error_handler("exception_error_handler", E_ALL);
87
88 /* the Exceptionhandler for Syntax-Errors, throwed by PHP itself */
89 /**
90  * Superclass for plugins that perform a url shortening
91  *
92  * @category Plugin
93  * @package  StatusNet
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
97  */
98
99 abstract class UrlShortenerPlugin2
100 {
101   public $shortenerName;
102   public $freeService = true;
103   /**
104   * Make an URL shorter.
105   *
106   * @param string $url URL to shorten
107   *
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.
110   */
111   protected abstract function shorten($url);
112
113
114   /**
115    * Utility to get the data at an URL
116    *
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.
122    */
123   protected function httpGet($url)
124   {
125     try
126     {
127       //Or create exception here, by looking for 'false'...? Hmm..
128       //No, let HTTPClient throw it. But then we need a more generally
129       //Exception system.
130       $request  = HTTPClient::start();
131       $response = $request->get($url);
132
133     } catch(PluginErrorException $e)
134     {
135       $e->printFull();
136       return false;
137     }
138     return $response->getBody();
139   }
140  /**
141   * Utility to post a request and get a response URL
142   *
143   * @param string $url  URL to fetch
144   * @param array  $data post parameters
145   *
146   * @return string response body
147   *
148   * @todo self with exceptions as in httpGet()
149   */
150   protected function httpPost($url, $data)
151   {
152     try{
153       $request  = HTTPClient::start();
154       $response = $request->post($url, null, $data);
155
156     } catch(PluginErrorException $e)
157     {
158       $e->printFull();
159       return false;
160     }
161     return $response->getBody();
162   }
163   // Hook handlers
164
165   /**
166    * Called when all plugins have been initialized
167    *
168    * @return boolean hook value
169    */
170
171   function onInitializePlugin()
172   {
173     if (!isset($this->shortenerName))
174     {
175       throw new Exception("@Admin must specifiy $this->shortenerName");
176     }
177     return true;
178   }
179   /**
180    * Called when a showing the URL shortener drop-down box
181    *
182    * Properties of the shortening service currently only
183    * include whether it's a free service.
184    *
185    * @param array &$shorteners array mapping shortener name to properties
186    *
187    * @return boolean hook value
188    */
189   function onGetUrlShorteners(&$shorteners)
190   {
191     $shorteners[$this->shortenerName] =array('freeService' => $this->freeService);
192     return true;
193   }
194
195   /**
196    * Called to shorten an URL
197    *
198    * @param string $url           URL to shorten
199    * @param string $shortenerName Shortening service. Don't handle if it's
200    *                              not you!
201    * @param string &$shortenedUrl URL after shortening; out param.
202    *
203    * @return boolean hook value
204    */
205
206   function onStartShortenUrl($url, $shortenerName, &$shortenedUrl)
207   {
208     if ($shortenerName == $this->shortenerName)
209     {
210       $result = $this->shorten($url);
211       if (isset($result) && $result != null && $result !== false)
212       {
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.
216         common_log(LOG_INFO,
217                    __CLASS__ . ": $this->shortenerName ".
218                    "shortened $url to $shortenedUrl");
219         return false;
220       }
221     }
222     return true;
223   }
224 }
225 //@debug
226 function A() {
227     $foo->bar(); // Purposely cause error
228 }
229
230 function B($c) {
231     A();
232 }
233
234 try {
235     B('foobar');
236 } catch (PluginErrorException $e) {
237     echo $e->printFull();
238 }
239 ?>