1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK ---------------------------------- |
---|
3 | # This file is part of kUtRL, a plugin for Dotclear 2. |
---|
4 | # |
---|
5 | # Copyright (c) 2009-2010 JC Denis and contributors |
---|
6 | # jcdenis@gdwd.com |
---|
7 | # |
---|
8 | # Licensed under the GPL version 2.0 license. |
---|
9 | # A copy of this license is available in LICENSE file or at |
---|
10 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
11 | # -- END LICENSE BLOCK ------------------------------------ |
---|
12 | |
---|
13 | if (!defined('DC_RC_PATH')){return;} |
---|
14 | |
---|
15 | class trimKutrlService extends kutrlServices |
---|
16 | { |
---|
17 | public $core; |
---|
18 | |
---|
19 | public $id = 'trim'; |
---|
20 | public $name = 'tr.im'; |
---|
21 | public $home = 'http://tr.im'; |
---|
22 | |
---|
23 | private $url_api = 'http://api.tr.im/v1/'; |
---|
24 | private $api_rate_time = 0; |
---|
25 | |
---|
26 | public function __construct($core,$limit_to_blog=true) |
---|
27 | { |
---|
28 | parent::__construct($core,$limit_to_blog); |
---|
29 | |
---|
30 | $this->args['username'] = $this->s->kutrl_srv_trim_username; |
---|
31 | $this->args['password'] = $this->s->kutrl_srv_trim_password; |
---|
32 | |
---|
33 | $this->url_base = 'http://tr.im/'; |
---|
34 | $this->url_min_length = 25; |
---|
35 | $this->api_rate_time = (integer) $this->s->kutrl_srv_trim_apiratetime; |
---|
36 | } |
---|
37 | |
---|
38 | public function saveSettings() |
---|
39 | { |
---|
40 | $this->s->put('kutrl_srv_trim_username',$_POST['kutrl_srv_trim_username']); |
---|
41 | $this->s->put('kutrl_srv_trim_password',$_POST['kutrl_srv_trim_password']); |
---|
42 | } |
---|
43 | |
---|
44 | public function settingsForm() |
---|
45 | { |
---|
46 | echo |
---|
47 | '<p><label class="classic">'.__('Login:').'<br />'. |
---|
48 | form::field(array('kutrl_srv_trim_username'),50,255,$this->s->kutrl_srv_trim_username). |
---|
49 | '</label></p>'. |
---|
50 | '<p class="form-note">'. |
---|
51 | __('This is your login to sign up to tr.im.'). |
---|
52 | '</p>'. |
---|
53 | '<p><label class="classic">'.__('Password:').'<br />'. |
---|
54 | form::field(array('kutrl_srv_trim_password'),50,255,$this->s->kutrl_srv_trim_password). |
---|
55 | '</label></p>'. |
---|
56 | '<p class="form-note">'. |
---|
57 | __('This is your password to sign up to tr.im.'). |
---|
58 | '</p>'; |
---|
59 | } |
---|
60 | |
---|
61 | public function testService() |
---|
62 | { |
---|
63 | if (empty($this->args['username']) || empty($this->args['password'])) |
---|
64 | { |
---|
65 | $this->error->add(__('Service is not well configured.')); |
---|
66 | return false; |
---|
67 | } |
---|
68 | if (time() < $this->api_rate_time + 300) // bloc service within 5min on API rate limit |
---|
69 | { |
---|
70 | $this->error->add(__('Prevent service rate limit.')); |
---|
71 | return false; |
---|
72 | } |
---|
73 | |
---|
74 | if (!($rsp = self::post($this->url_api.'verify.xml',$this->args,true,true))) |
---|
75 | { |
---|
76 | $this->error->add(__('Service is unavailable.')); |
---|
77 | return false; |
---|
78 | } |
---|
79 | $r = simplexml_load_string($rsp); |
---|
80 | |
---|
81 | if ($r['code'] == 200) |
---|
82 | { |
---|
83 | return true; |
---|
84 | } |
---|
85 | $this->error->add(__('Authentication to service failed.')); |
---|
86 | return false; |
---|
87 | } |
---|
88 | |
---|
89 | public function createHash($url,$hash=null) |
---|
90 | { |
---|
91 | $arg = $this->args; |
---|
92 | $arg['url'] = $url; |
---|
93 | |
---|
94 | if (!($rsp = self::post($this->url_api.'trim_url.xml',$arg,true,true))) |
---|
95 | { |
---|
96 | $this->error->add(__('Service is unavailable.')); |
---|
97 | return false; |
---|
98 | } |
---|
99 | |
---|
100 | $r = simplexml_load_string($rsp); |
---|
101 | |
---|
102 | # API rate limit |
---|
103 | if ($r['code'] == 425) |
---|
104 | { |
---|
105 | $this->s->put('kutrl_srv_trim_apiratetime',time()); |
---|
106 | |
---|
107 | $this->error->add(__('Service rate limit exceeded.')); |
---|
108 | return false; |
---|
109 | } |
---|
110 | |
---|
111 | if (isset($r->trimpath)) |
---|
112 | { |
---|
113 | $rs = new ArrayObject(); |
---|
114 | $rs->hash = $r->trimpath; |
---|
115 | $rs->url = $url; |
---|
116 | $rs->type = $this->id; |
---|
117 | return $rs; |
---|
118 | } |
---|
119 | $this->error->add(__('Unreadable service response.')); |
---|
120 | return false; |
---|
121 | } |
---|
122 | } |
---|
123 | ?> |
---|