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 bitlyKutrlService extends kutrlServices |
---|
16 | { |
---|
17 | public $id = 'bitly'; |
---|
18 | public $name = 'bit.ly'; |
---|
19 | public $home = 'http://bit.ly'; |
---|
20 | |
---|
21 | private $url_api = 'http://api.bit.ly/v3/'; |
---|
22 | private $args = array( |
---|
23 | 'format' => 'xml', |
---|
24 | 'login' => '', |
---|
25 | 'apiKey' => '', |
---|
26 | 'history' => 0 |
---|
27 | ); |
---|
28 | |
---|
29 | public function __construct($core,$limit_to_blog=true) |
---|
30 | { |
---|
31 | parent::__construct($core,$limit_to_blog); |
---|
32 | |
---|
33 | $this->args['login'] = $this->s->kutrl_srv_bitly_login; |
---|
34 | $this->args['apiKey'] = $this->s->kutrl_srv_bitly_apikey; |
---|
35 | $this->args['history'] = $this->s->kutrl_srv_bitly_history ? 1 : 0; |
---|
36 | |
---|
37 | $this->url_base = 'http://bit.ly/'; |
---|
38 | $this->url_min_length = 25; |
---|
39 | } |
---|
40 | |
---|
41 | public function saveSettings() |
---|
42 | { |
---|
43 | $this->s->put('kutrl_srv_bitly_login',$_POST['kutrl_srv_bitly_login']); |
---|
44 | $this->s->put('kutrl_srv_bitly_apikey',$_POST['kutrl_srv_bitly_apikey']); |
---|
45 | $this->s->put('kutrl_srv_bitly_history',isset($_POST['kutrl_srv_bitly_history'])); |
---|
46 | } |
---|
47 | |
---|
48 | public function settingsForm() |
---|
49 | { |
---|
50 | echo |
---|
51 | '<p><label class="classic">'.__('Login:').'<br />'. |
---|
52 | form::field(array('kutrl_srv_bitly_login'),50,255,$this->s->kutrl_srv_bitly_login). |
---|
53 | '</label></p>'. |
---|
54 | '<p class="form-note">'. |
---|
55 | __('This is your login to sign up to bit.ly.'). |
---|
56 | '</p>'. |
---|
57 | '<p><label class="classic">'.__('API Key:').'<br />'. |
---|
58 | form::field(array('kutrl_srv_bitly_apikey'),50,255,$this->s->kutrl_srv_bitly_apikey). |
---|
59 | '</label></p>'. |
---|
60 | '<p class="form-note">'. |
---|
61 | __('This is your personnal bit.ly API key. You can find it on your account page.'). |
---|
62 | '</p>'. |
---|
63 | '<p><label class="classic">'. |
---|
64 | form::checkbox(array('kutrl_srv_bitly_history'),'1',$this->s->kutrl_srv_bitly_history).' '. |
---|
65 | __('Publish history'). |
---|
66 | '</label></p>'. |
---|
67 | '<p class="form-note">'. |
---|
68 | __('This publish all short links on your bit.ly public page.'). |
---|
69 | '</p>'; |
---|
70 | } |
---|
71 | |
---|
72 | public function testService() |
---|
73 | { |
---|
74 | if (empty($this->args['login']) || empty($this->args['apiKey'])) |
---|
75 | { |
---|
76 | $this->error->add(__('Service is not well configured.')); |
---|
77 | return false; |
---|
78 | } |
---|
79 | |
---|
80 | $args = $this->args; |
---|
81 | $args['hash'] = 'WP9vc'; |
---|
82 | if (!($response = self::post($this->url_api.'expand',$args,true))) |
---|
83 | { |
---|
84 | $this->error->add(__('Failed to call service.')); |
---|
85 | return false; |
---|
86 | } |
---|
87 | |
---|
88 | $rsp = simplexml_load_string($response); |
---|
89 | |
---|
90 | $err_msg = (string) $rsp->status_txt; |
---|
91 | if ($err_msg != 'OK') { |
---|
92 | $err_no = (integer) $rsp->status_code; |
---|
93 | $this->error->add(sprintf(__('An error occured with code %s and message "%s"'),$err_no,$err_msg)); |
---|
94 | return false; |
---|
95 | } |
---|
96 | return true; |
---|
97 | } |
---|
98 | |
---|
99 | public function createHash($url,$hash=null) |
---|
100 | { |
---|
101 | $args = $this->args; |
---|
102 | $args['longUrl'] = $url; |
---|
103 | |
---|
104 | if (!($response = self::post($this->url_api.'shorten',$args,true))) |
---|
105 | { |
---|
106 | $this->error->add(__('Failed to call service.')); |
---|
107 | return false; |
---|
108 | } |
---|
109 | |
---|
110 | $rsp = simplexml_load_string($response); |
---|
111 | |
---|
112 | $err_msg = (string) $rsp->status_txt; |
---|
113 | if ($err_msg != 'OK') { |
---|
114 | $err_no = (integer) $rsp->status_code; |
---|
115 | $this->error->add(sprintf(__('An error occured with code %s and message "%s"'),$err_no,$err_msg)); |
---|
116 | return false; |
---|
117 | } |
---|
118 | |
---|
119 | $rs = new ArrayObject(); |
---|
120 | $rs->hash = (string) $rsp->data[0]->hash; |
---|
121 | $rs->url = (string) $rsp->data[0]->long_url; |
---|
122 | $rs->type = $this->id; |
---|
123 | |
---|
124 | return $rs; |
---|
125 | } |
---|
126 | } |
---|
127 | ?> |
---|