Dotclear

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

Revision 1949, 2.9 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 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->setNameSpace('kUtRL');
41          $this->s->put('kutrl_srv_trim_username',$_POST['kutrl_srv_trim_username']);
42          $this->s->put('kutrl_srv_trim_password',$_POST['kutrl_srv_trim_password']);
43          $this->s->setNameSpace('system');
44     }
45
46     public function settingsForm()
47     {
48          echo 
49          '<p><label class="classic">'.__('Login:').'<br />'.
50          form::field(array('kutrl_srv_trim_username'),50,255,$this->s->kutrl_srv_trim_username).
51          '</label></p>'.
52          '<p class="form-note">'.
53          __('This is your login to sign up to tr.im.').
54          '</p>'.
55          '<p><label class="classic">'.__('Password:').'<br />'.
56          form::field(array('kutrl_srv_trim_password'),50,255,$this->s->kutrl_srv_trim_password).
57          '</label></p>'.
58          '<p class="form-note">'.
59          __('This is your password to sign up to tr.im.').
60          '</p>';
61     }
62
63     public function testService()
64     {
65          if (empty($this->args['username']) || empty($this->args['password'])) return false;
66          if (time() < $this->api_rate_time + 300) return false; // bloc service within 5min on API rate limit
67
68          if (!($rsp = self::post($this->url_api.'verify.xml',$this->args,true,true)))
69          {
70               return false;
71          }
72          $r = simplexml_load_string($rsp);
73
74          if ($r['code'] == 200)
75          {
76               return true;
77          }
78          return false;
79     }
80
81     public function createHash($url,$hash=null)
82     {
83          $arg = $this->args;
84          $arg['url'] = $url;
85
86          if (!($rsp = self::post($this->url_api.'trim_url.xml',$arg,true,true)))
87          {
88               return false;
89          }
90
91          $r = simplexml_load_string($rsp);
92
93          # API rate limit
94          if ($r['code'] == 425)
95          {
96               $this->s->setNameSpace('kUtRL');
97               $this->s->put('kutrl_srv_trim_apiratetime',time());
98               $this->s->setNameSpace('system');
99               return false;
100          }
101
102          if (isset($r->trimpath))
103          {
104               $rs = new ArrayObject();
105               $rs->hash = $r->trimpath;
106               $rs->url = $url;
107               $rs->type = $this->id;
108               return $rs;
109          }
110          return false;
111     }
112}
113?>
Note: See TracBrowser for help on using the repository browser.

Sites map