Dotclear

source: plugins/kUtRL/inc/services/class.bitly.service.php @ 1949

Revision 1949, 3.2 KB checked in by JcDenis, 14 years ago (diff)

kUtRL 0.1:

  • First lab release
Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3# This file is part of kUtRL, a plugin for Dotclear 2.
4#
5# Copyright (c) 2009 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
13if (!defined('DC_RC_PATH')){return;}
14
15class 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/';
22     private $args = array(
23          'version' => '2.0.1',
24          'format' => 'xml',
25          'login' => '',
26          'apiKey' => '',
27          'history' => 0
28     );
29
30     public function __construct($core,$limit_to_blog=true)
31     {
32          parent::__construct($core,$limit_to_blog);
33
34          $this->args['login'] = $this->s->kutrl_srv_bitly_login;
35          $this->args['apiKey'] = $this->s->kutrl_srv_bitly_apikey;
36          $this->args['history'] = $this->s->kutrl_srv_bitly_history ? 1 : 0;
37
38          $this->url_base = 'http://bit.ly/';
39          $this->url_min_length = 25;
40     }
41
42     public function saveSettings()
43     {
44          $this->s->setNameSpace('kUtRL');
45          $this->s->put('kutrl_srv_bitly_login',$_POST['kutrl_srv_bitly_login']);
46          $this->s->put('kutrl_srv_bitly_apikey',$_POST['kutrl_srv_bitly_apikey']);
47          $this->s->put('kutrl_srv_bitly_history',isset($_POST['kutrl_srv_bitly_history']));
48          $this->s->setNameSpace('system');
49     }
50
51     public function settingsForm()
52     {
53          echo 
54          '<p><label class="classic">'.__('Login:').'<br />'.
55          form::field(array('kutrl_srv_bitly_login'),50,255,$this->s->kutrl_srv_bitly_login).
56          '</label></p>'.
57          '<p class="form-note">'.
58          __('This is your login to sign up to bit.ly.').
59          '</p>'.
60          '<p><label class="classic">'.__('API Key:').'<br />'.
61          form::field(array('kutrl_srv_bitly_apikey'),50,255,$this->s->kutrl_srv_bitly_apikey).
62          '</label></p>'.
63          '<p class="form-note">'.
64          __('This is your personnal bit.ly API key. You can find it on your account page.').
65          '</p>'.
66          '<p><label class="classic">'.
67          form::checkbox(array('kutrl_srv_bitly_history'),'1',$this->s->kutrl_srv_bitly_history).' '.
68          __('Publish history').
69          '</label></p>'.
70          '<p class="form-note">'.
71          __('This publish all short links on your bit.ly public page.').
72          '</p>';
73     }
74
75     public function testService()
76     {
77          if (empty($this->args['login']) || empty($this->args['apiKey'])) return false;
78
79          $args = $this->args;
80          $args['hash'] = 'WP9vc';
81          if (!($rs = self::post($this->url_api.'info',$args,true)))
82          {
83               return false;
84          }
85          $rsp = simplexml_load_string($rs);
86          if ($rsp->errorCode == 203)
87          {
88               return false;
89          }
90          return true;
91     }
92
93     public function createHash($url,$hash=null)
94     {
95          $args = $this->args;
96          $args['longUrl'] = $url;
97
98          if (!($response = self::post($this->url_api.'shorten',$args,true)))
99          {
100               return false;
101          }
102          $rsp = simplexml_load_string($response);
103
104          if (0 != $rsp->errorCode)
105          {
106               return false;
107          }
108
109          $rs = new ArrayObject();
110          $rs->hash = (string) $rsp->results->nodeKeyVal->userHash[0]; //!?
111          $rs->url = $url;
112          $rs->type = $this->id;
113
114          return $rs;
115     }
116}
117?>
Note: See TracBrowser for help on using the repository browser.

Sites map