/
twit.php
executable file
·192 lines (166 loc) · 6.25 KB
/
twit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/usr/bin/php
<?php
/**
* Send Twitter message from command line
*
* PHP version 5
*
* Copyright © 2011 Remi Collet
*
* This file is part of twit.
*
* rpmphp is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* twit is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with twit. If not, see <http://www.gnu.org/licenses/>.
*
* @category Main
* @package twit
*
* @author Remi Collet <unknown@unknwown.com>
* @copyright 2011 Remi Collet
* @license http://www.gnu.org/licenses/agpl-3.0-standalone.html AGPL License 3.0 or (at your option) any later version
* @link http://github.com/remicollet/twit/
* @since The begining of times.
*/
define('VERSION', '0.1');
function getConf($need=0) {
$file = getenv('HOME').'/.config/phptwit/account';
$conf = @simplexml_load_file($file);
if (!$conf || !$need) {
$conf = new SimpleXMLElement("<?xml version='1.0' standalone='yes'?><config></config>");
$conf->addChild('Version', VERSION);
}
if ($need>=1 && !isset($conf->ConsumerKey)) {
die("Application is not set. Run with 'register' option\n");
}
if ($need>=2 && !isset($conf->UserId)) {
die("Access is not set. Run with 'access' option\n");
}
return $conf;
}
function saveConf(SimpleXMLElement $conf) {
$dir = getenv('HOME').'/.config/phptwit';
@mkdir($dir, 0700, true);
$conf->saveXML($dir.'/account');
}
function getStatus() {
$conf = getConf(2);
try {
$oauth = new OAuth($conf->ConsumerKey, $conf->ConsumerSecret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
$oauth->setToken($conf->UserToken, $conf->UserSecret);
$oauth->fetch('https://twitter.com/account/verify_credentials.json');
$json = json_decode($oauth->getLastResponse());
if (isset($json->name)) {
echo "Name: ".$json->name."\n";
} else {
echo "User: ".$conf->UserName."\n";
}
if (isset($json->status->text)) {
echo "Last: ".$json->status->text."\n";
echo "Date: ".$json->status->created_at."\n";
}
} catch(OAuthException $E) {
echo "Error: ".$E->getMessage()."\n";
}
}
function setStatus($msg) {
$conf = getConf(2);
try {
$oauth = new OAuth($conf->ConsumerKey, $conf->ConsumerSecret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_FORM);
$oauth->setToken($conf->UserToken, $conf->UserSecret);
$args = array(
'status' => $msg,
'empty_param' => NULL
);
$oauth->fetch('https://twitter.com/statuses/update.json',$args, OAUTH_HTTP_METHOD_POST);
$json = json_decode($oauth->getLastResponse());
if (isset($json->id)) {
echo "Tweet sent for ".$conf->UserName." !\n";
}
} catch(OAuthException $E) {
echo "Error: ".$E->getMessage()."\n";
}
}
function register($ckey, $csecret) {
try {
$oauth = new OAuth($ckey, $csecret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
$info = $oauth->getRequestToken('https://twitter.com/oauth/request_token');
if (isset($info['oauth_token']) && isset($info['oauth_token_secret'])) {
echo "Please visit https://twitter.com/oauth/authorize?oauth_token=".$info['oauth_token']."\n";
echo "Then run again with 'access' option\n\n";
$conf = getConf();
$conf->addChild('ConsumerKey', $ckey);
$conf->addChild('ConsumerSecret', $csecret);
$conf->addChild('OauthToken', $info['oauth_token']);
$conf->addChild('OauthSecret', $info['oauth_token_secret']);
saveConf($conf);
}
} catch(OAuthException $E) {
echo "Error: ".$E->getMessage()."\n";
die("Have you properly register the application ? Visit http://twitter.com/oauth_clients/new\n");
}
}
function getAccess() {
$conf = getConf(1);
try {
$oauth = new OAuth($conf->ConsumerKey, $conf->ConsumerSecret, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
$oauth->setToken($conf->OauthToken, $conf->OauthSecret);
$info = $oauth->getAccessToken('https://twitter.com/oauth/access_token');
if (isset($info['oauth_token'])) {
echo "Access granted !\n\n";
$conf->addChild('UserToken', $info['oauth_token']);
$conf->addChild('UserSecret', $info['oauth_token_secret']);
$conf->addChild('UserId', $info['user_id']);
$conf->addChild('UserName', $info['screen_name']);
saveConf($conf);
}
} catch(OAuthException $E) {
echo "Error: ".$E->getMessage()."\n";
die("Have you allow the application to acces to your account ? Run 'register' again.\n");
}
}
function Help() {
$cmd = $_SERVER['argv'][0];
echo "Usage:\n";
echo "\t$cmd\n\t\twithout option, display current status\n\n";
echo "\t$cmd help\n\t\tdisplay this text\n\n";
echo "\t$cmd register CONSUMER_KEY CONSUMER_SECRET\n\t\tregister twitter application,\n\t\tsee http://twitter.com/oauth_clients/new\n\n";
echo "\t$cmd access\n\t\tverify the registration, must be call only once after 'register'\n\n";
echo "\t$cmd 'any message'\n\t\tchange you online status\n\n";
}
echo "\ntwit version ".VERSION."\n\n";
if (!class_exists('Oauth')) {
die("oauth extension is mising\n");
}
if ($_SERVER['argc']==1) {
getStatus();
} else switch($_SERVER['argv'][1]) {
case 'register':
if ($_SERVER['argc']==4) {
register($_SERVER['argv'][2], $_SERVER['argv'][3]);
} else {
Help();
}
break;
case 'access':
getAccess();
break;
case 'help':
Help();
break;
default:
if ($_SERVER['argc']==2) {
setStatus($_SERVER['argv'][1]);
} else {
Help();
}
}