forked from mingcheng/php-readability
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
393 lines (357 loc) · 15 KB
/
functions.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?php
/*
* Copyright 2011-2012 Evernote Corporation.
*
* This file contains functions used by Evernote's PHP OAuth samples.
*/
// Include the Evernote API from the lib subdirectory.
// lib simply contains the contents of /php/lib from the Evernote API SDK
define("EVERNOTE_LIBS", dirname(__FILE__) . DIRECTORY_SEPARATOR . "lib");
ini_set("include_path", ini_get("include_path") . PATH_SEPARATOR . EVERNOTE_LIBS);
require_once 'Evernote/Client.php';
require_once 'packages/Types/Types_types.php';
// Import the classes that we're going to be using
use EDAM\Error\EDAMSystemException,
EDAM\Error\EDAMUserException,
EDAM\Error\EDAMErrorCode,
EDAM\Error\EDAMNotFoundException;
use EDAM\Types\NoteAttributes,
EDAM\Types\Note;
use Evernote\Client;
// Verify that you successfully installed the PHP OAuth Extension
if (!class_exists('OAuth')) {
die("<span style=\"color:red\">The PHP OAuth Extension is not installed</span>");
}
// Verify that you have configured your API key
if (strlen(OAUTH_CONSUMER_KEY) == 0 || strlen(OAUTH_CONSUMER_SECRET) == 0) {
$configFile = dirname(__FILE__) . '/config.php';
die("<span style=\"color:red\">Before using this sample code you must edit the file $configFile " .
"and fill in OAUTH_CONSUMER_KEY and OAUTH_CONSUMER_SECRET with the values that you received from Evernote. " .
"If you do not have an API key, you can request one from " .
"<a href=\"http://dev.evernote.com/documentation/cloud/\">http://dev.evernote.com/documentation/cloud/</a></span>");
}
/*
* The first step of OAuth authentication: the client (this application)
* obtains temporary credentials from the server (Evernote).
*
* After successfully completing this step, the client has obtained the
* temporary credentials identifier, an opaque string that is only meaningful
* to the server, and the temporary credentials secret, which is used in
* signing the token credentials request in step 3.
*
* This step is defined in RFC 5849 section 2.1:
* http://tools.ietf.org/html/rfc5849#section-2.1
*
* @return boolean TRUE on success, FALSE on failure
*/
function getTemporaryCredentials()
{
global $lastError, $currentStatus;
try {
$client = new Client(array(
'consumerKey' => OAUTH_CONSUMER_KEY,
'consumerSecret' => OAUTH_CONSUMER_SECRET,
'sandbox' => SANDBOX
));
$requestTokenInfo = $client->getRequestToken(getCallbackUrl());
if ($requestTokenInfo) {
$_SESSION['requestToken'] = $requestTokenInfo['oauth_token'];
$_SESSION['requestTokenSecret'] = $requestTokenInfo['oauth_token_secret'];
$currentStatus = 'Obtained temporary credentials';
return TRUE;
} else {
$lastError = 'Failed to obtain temporary credentials.';
}
} catch (OAuthException $e) {
$lastError = 'Error obtaining temporary credentials: ' . $e->getMessage();
}
return FALSE;
}
/*
* The completion of the second step in OAuth authentication: the resource owner
* authorizes access to their account and the server (Evernote) redirects them
* back to the client (this application).
*
* After successfully completing this step, the client has obtained the
* verification code that is passed to the server in step 3.
*
* This step is defined in RFC 5849 section 2.2:
* http://tools.ietf.org/html/rfc5849#section-2.2
*
* @return boolean TRUE if the user authorized access, FALSE if they declined access.
*/
function handleCallback()
{
global $lastError, $currentStatus;
if (isset($_GET['oauth_verifier'])) {
$_SESSION['oauthVerifier'] = $_GET['oauth_verifier'];
$currentStatus = 'Content owner authorized the temporary credentials';
return TRUE;
} else {
// If the User clicks "decline" instead of "authorize", no verification code is sent
$lastError = 'Content owner did not authorize the temporary credentials';
return FALSE;
}
}
/*
* The third and final step in OAuth authentication: the client (this application)
* exchanges the authorized temporary credentials for token credentials.
*
* After successfully completing this step, the client has obtained the
* token credentials that are used to authenticate to the Evernote API.
* In this sample application, we simply store these credentials in the user's
* session. A real application would typically persist them.
*
* This step is defined in RFC 5849 section 2.3:
* http://tools.ietf.org/html/rfc5849#section-2.3
*
* @return boolean TRUE on success, FALSE on failure
*/
function getTokenCredentials()
{
global $lastError, $currentStatus;
if (isset($_SESSION['accessToken'])) {
$lastError = 'Temporary credentials may only be exchanged for token credentials once';
return FALSE;
}
try {
$client = new Client(array(
'consumerKey' => OAUTH_CONSUMER_KEY,
'consumerSecret' => OAUTH_CONSUMER_SECRET,
'sandbox' => SANDBOX
));
$accessTokenInfo = $client->getAccessToken($_SESSION['requestToken'], $_SESSION['requestTokenSecret'], $_SESSION['oauthVerifier']);
if ($accessTokenInfo) {
$_SESSION['accessToken'] = $accessTokenInfo['oauth_token'];
$currentStatus = 'Exchanged the authorized temporary credentials for token credentials';
return TRUE;
} else {
$lastError = 'Failed to obtain token credentials.';
}
} catch (OAuthException $e) {
$lastError = 'Error obtaining token credentials: ' . $e->getMessage();
}
return FALSE;
}
/*
* Once you have obtained the token credentials identifier via OAuth, you can use it
* as the auth token in any call to an Evernote API function.
*
* @return boolean TRUE on success, FALSE on failure
*/
/*
* getUser()
*
*/
function getUser()
{
global $lastError, $currentStatus;
try {
$accessToken = $_SESSION['accessToken'];
$client = new Client(array(
'token' => $accessToken,
'sandbox' => SANDBOX
));
$userinfo = $client->getUserStore()->getUser();
$_COOKIE['username'] = $userinfo->username;
$currentStatus = 'Successfully get user name';
return $_COOKIE['username'] ;
} catch (EDAMSystemException $e) {
if (isset(EDAMErrorCode::$__names[$e->errorCode])) {
$lastError = 'Error getting username: ' . EDAMErrorCode::$__names[$e->errorCode] . ": " . $e->parameter;
} else {
$lastError = 'Error getting username: ' . $e->getCode() . ": " . $e->getMessage();
}
} catch (EDAMUserException $e) {
if (isset(EDAMErrorCode::$__names[$e->errorCode])) {
$lastError = 'Error getting username: ' . EDAMErrorCode::$__names[$e->errorCode] . ": " . $e->parameter;
} else {
$lastError = 'Error getting username: ' . $e->getCode() . ": " . $e->getMessage();
}
} catch (EDAMNotFoundException $e) {
if (isset(EDAMErrorCode::$__names[$e->errorCode])) {
$lastError = 'Error getting username: ' . EDAMErrorCode::$__names[$e->errorCode] . ": " . $e->parameter;
} else {
$lastError = 'Error getting username: ' . $e->getCode() . ": " . $e->getMessage();
}
} catch (Exception $e) {
$lastError = 'Error getting username: ' . $e->getMessage();
}
return FALSE;
}
/*
* Create note
* *
*/
function addNote($title,$content,$tags,$surl)
{
global $lastError, $currentStatus;
$noteAttr = new NoteAttributes();
$noteAttr->sourceApplication = "RNhelper";
$noteAttr->sourceURL = $surl;
$note = new Note();
$note->title = $title;
$nBody = '<?xml version="1.0" encoding="UTF-8"?>';
$nBody .= '<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">';
$nBody .= '<en-note>' . $content. '</en-note>';
$note->content = $nBody;
$note->tagNames = $tags;
try {
$accessToken = $_SESSION['accessToken'];
$client = new Client(array(
'token' => $accessToken,
'sandbox' => SANDBOX
));
$userinfo = $client->getNoteStore()->createNote($note);
$currentStatus = 'Successfully create new note';
return TRUE;
} catch (EDAMSystemException $e) {
if (isset(EDAMErrorCode::$__names[$e->errorCode])) {
$lastError = 'Error : ' . EDAMErrorCode::$__names[$e->errorCode] . ": " . $e->parameter;
} else {
$lastError = 'Error creating note: ' . $e->getCode() . ": " . $e->getMessage();
}
} catch (EDAMUserException $e) {
if (isset(EDAMErrorCode::$__names[$e->errorCode])) {
$lastError = 'Error creating note: ' . EDAMErrorCode::$__names[$e->errorCode] . ": " . $e->parameter;
} else {
$lastError = 'Error creating note: ' . $e->getCode() . ": " . $e->getMessage();
}
} catch (EDAMNotFoundException $e) {
if (isset(EDAMErrorCode::$__names[$e->errorCode])) {
$lastError = 'Error creating note: ' . EDAMErrorCode::$__names[$e->errorCode] . ": " . $e->parameter;
} else {
$lastError = 'Error creating note: ' . $e->getCode() . ": " . $e->getMessage();
}
} catch (Exception $e) {
$lastError = 'Error creating note: ' . $e->getMessage();
}
return FALSE;
}
function listNotebooks()
{
global $lastError, $currentStatus;
try {
$accessToken = $_SESSION['accessToken'];
$client = new Client(array(
'token' => $accessToken,
'sandbox' => SANDBOX
));
$notebooks = $client->getNoteStore()->listNotebooks();
$username = $client->getUserStore()->getUser();
$result = array();
if (!empty($notebooks)) {
foreach ($notebooks as $notebook) {
$result[] = $notebook->name;
}
}
$_SESSION['notebooks'] = $result;
$_SESSION['user'] = $username;
$currentStatus = 'Successfully listed content owner\'s notebooks';
return TRUE;
} catch (EDAMSystemException $e) {
if (isset(EDAMErrorCode::$__names[$e->errorCode])) {
$lastError = 'Error listing notebooks: ' . EDAMErrorCode::$__names[$e->errorCode] . ": " . $e->parameter;
} else {
$lastError = 'Error listing notebooks: ' . $e->getCode() . ": " . $e->getMessage();
}
} catch (EDAMUserException $e) {
if (isset(EDAMErrorCode::$__names[$e->errorCode])) {
$lastError = 'Error listing notebooks: ' . EDAMErrorCode::$__names[$e->errorCode] . ": " . $e->parameter;
} else {
$lastError = 'Error listing notebooks: ' . $e->getCode() . ": " . $e->getMessage();
}
} catch (EDAMNotFoundException $e) {
if (isset(EDAMErrorCode::$__names[$e->errorCode])) {
$lastError = 'Error listing notebooks: ' . EDAMErrorCode::$__names[$e->errorCode] . ": " . $e->parameter;
} else {
$lastError = 'Error listing notebooks: ' . $e->getCode() . ": " . $e->getMessage();
}
} catch (Exception $e) {
$lastError = 'Error listing notebooks: ' . $e->getMessage();
}
return FALSE;
}
/*
* Reset the current session.
*/
function resetSession()
{
if (isset($_SESSION['requestToken'])) {
unset($_SESSION['requestToken']);
}
if (isset($_SESSION['requestTokenSecret'])) {
unset($_SESSION['requestTokenSecret']);
}
if (isset($_SESSION['oauthVerifier'])) {
unset($_SESSION['oauthVerifier']);
}
if (isset($_SESSION['accessToken'])) {
unset($_SESSION['accessToken']);
}
if (isset($_SESSION['accessTokenSecret'])) {
unset($_SESSION['accessTokenSecret']);
}
if (isset($_SESSION['noteStoreUrl'])) {
unset($_SESSION['noteStoreUrl']);
}
if (isset($_SESSION['webApiUrlPrefix'])) {
unset($_SESSION['webApiUrlPrefix']);
}
if (isset($_SESSION['tokenExpires'])) {
unset($_SESSION['tokenExpires']);
}
if (isset($_SESSION['userId'])) {
unset($_SESSION['userId']);
}
if (isset($_SESSION['notebooks'])) {
unset($_SESSION['notebooks']);
}
}
/*
* Get the URL of this application. This URL is passed to the server (Evernote)
* while obtaining unauthorized temporary credentials (step 1). The resource owner
* is redirected to this URL after authorizing the temporary credentials (step 2).
*/
function getCallbackUrl()
{
//$thisUrl = (empty($_SERVER['HTTPS'])) ? "http://" : "https://";
//$thisUrl .= $_SERVER['SERVER_NAME'];
//$thisUrl .= ($_SERVER['SERVER_PORT'] == 80 || $_SERVER['SERVER_PORT'] == 443) ? "" : (":".$_SERVER['SERVER_PORT']);
//$thisUrl .= $_SERVER['SCRIPT_NAME'];
//$thisUrl .= '?action=callback';
$thisUrl = preg_replace('/[?&]action=[^&]*/i', '', curPageURL());
// use count($_GET) instead next
if(isset($_GET["url"])){
$thisUrl .= "&action=callback";
}else{
$thisUrl .= "?action=callback";
}
return $thisUrl;
}
/*
* Get current url!
*/
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
/*
* Get the Evernote server URL used to authorize unauthorized temporary credentials.
*/
function getAuthorizationUrl()
{
$client = new Client(array(
'consumerKey' => OAUTH_CONSUMER_KEY,
'consumerSecret' => OAUTH_CONSUMER_SECRET,
'sandbox' => SANDBOX
));
return $client->getAuthorizeUrl($_SESSION['requestToken']);
}