|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
abstract class SocialIntegrationControllerBaseClass extends Controller |
|
|
|
|
|
|
5
|
|
|
{ |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* default profile pic in case none is available |
|
9
|
|
|
* @var String |
|
10
|
|
|
*/ |
|
11
|
|
|
private static $default_avatar = "http://placeholder.it/32x32"; |
|
12
|
|
|
public static function get_default_avatar() |
|
13
|
|
|
{ |
|
14
|
|
|
return self::$default_avatar; |
|
15
|
|
|
} |
|
16
|
|
|
public static function set_default_avatar($s) |
|
17
|
|
|
{ |
|
18
|
|
|
self::$default_avatar = $s; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* tells us if a class is a Social Integration API class (e.g. Facebook, Twiiter, etc....) |
|
23
|
|
|
* @param String $className |
|
24
|
|
|
* @return Boolean |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function is_social_integration_api_class($className) |
|
27
|
|
|
{ |
|
28
|
|
|
if (class_exists($className)) { |
|
29
|
|
|
$arrayOfInterfacesItImplements = class_implements($className); |
|
30
|
|
|
if (is_array($arrayOfInterfacesItImplements) && in_array("SocialIntegrationAPIInterface", $arrayOfInterfacesItImplements)) { |
|
31
|
|
|
return true; |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
return false; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* one stop shop button |
|
40
|
|
|
* @return Object ( |
|
41
|
|
|
* IsConnected, |
|
42
|
|
|
* IsLoggedIn, |
|
43
|
|
|
* Link, |
|
44
|
|
|
* ConnectedName, |
|
45
|
|
|
* ConnectedImageURL |
|
46
|
|
|
* ) |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function get_login_button($backURL = "", $member) |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
//back URL |
|
51
|
|
|
if (!$backURL) { |
|
52
|
|
|
$backURL = $_SERVER['REQUEST_URI']; |
|
53
|
|
|
} |
|
54
|
|
|
$position = strpos($backURL, "?"); |
|
55
|
|
|
if ($position) { |
|
56
|
|
|
$backURL = substr($backURL, 0, $position); |
|
57
|
|
|
} |
|
58
|
|
|
$backURL = str_replace("//", "/", $backURL); |
|
59
|
|
|
$backURL .= "#".strtolower(self::my_service_name())."_tab"; |
|
60
|
|
|
$backURL = "?BackURL=".urlencode($backURL); |
|
61
|
|
|
//security |
|
62
|
|
|
$token = SecurityToken::inst(); |
|
63
|
|
|
if ($member->exists()) { |
|
64
|
|
|
//AJAX FUNCTIONALITY |
|
65
|
|
|
//Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js'); |
|
|
|
|
|
|
66
|
|
|
//Requirements::javascript(THIRDPARTY_DIR . '/jquery-livequery/jquery.livequery.js'); |
|
|
|
|
|
|
67
|
|
|
//Requirements::javascript('social_integration/javascript/'.strtolower(self::my_service_name()).".js"); |
|
|
|
|
|
|
68
|
|
|
$method = "has".self::my_service_name(); |
|
69
|
|
|
if ($member->$method()) { |
|
70
|
|
|
$removeURL = Controller::join_links(self::my_class_name(), 'remove', $backURL); |
|
71
|
|
|
$removeURL = $token->addToUrl($removeURL); |
|
72
|
|
|
$nameField = self::my_service_name()."Name"; |
|
73
|
|
|
$pictureField = self::my_service_name()."Picture"; |
|
74
|
|
|
return new ArrayData( |
|
75
|
|
|
array( |
|
76
|
|
|
"IsConnected" => true, |
|
77
|
|
|
"IsLoggedIn" => Member::currentUserID() == $member->ID ? true : false, |
|
78
|
|
|
"Link" => $removeURL, |
|
79
|
|
|
"ConnectedName" => $member->$nameField, |
|
80
|
|
|
"ConnectedImageURL" => $member->$pictureField |
|
81
|
|
|
) |
|
82
|
|
|
); |
|
83
|
|
|
} else { |
|
84
|
|
|
$connectURL = Controller::join_links(self::my_class_name(), self::my_service_name().'Connect', $backURL); |
|
85
|
|
|
$connectURL = $token->addToUrl($connectURL); |
|
86
|
|
|
return new ArrayData( |
|
87
|
|
|
array( |
|
88
|
|
|
"IsConnected" => false, |
|
89
|
|
|
"IsLoggedIn" => Member::currentUserID() == $member->ID ? true : false, |
|
90
|
|
|
"Link" => $connectURL, |
|
91
|
|
|
"ConnectedName" => "", |
|
92
|
|
|
"ConnectedImageURL" => "" |
|
93
|
|
|
) |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
} else { |
|
97
|
|
|
$connectURL = Controller::join_links(self::my_class_name(), self::my_service_name().'Connect', $backURL); |
|
98
|
|
|
$connectURL = $token->addToUrl($connectURL); |
|
99
|
|
|
return new ArrayData( |
|
100
|
|
|
array( |
|
101
|
|
|
"IsConnected" => false, |
|
102
|
|
|
"IsLoggedIn" => false, |
|
103
|
|
|
"Link" => $connectURL, |
|
104
|
|
|
"ConnectedName" => "", |
|
105
|
|
|
"ConnectedImageURL" => "" |
|
106
|
|
|
) |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param String $returnURL |
|
114
|
|
|
* @return String |
|
115
|
|
|
*/ |
|
116
|
|
|
public static function login_url($returnURL = "") |
|
117
|
|
|
{ |
|
118
|
|
|
$backURLString = ""; |
|
119
|
|
|
if ($returnURL) { |
|
120
|
|
|
$backURLString = '?BackURL='.urlencode($returnURL); |
|
121
|
|
|
} |
|
122
|
|
|
return 'Security/login/'.$backURLString.'#'.self::my_security_form().'_LoginForm_tab'; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Link to login form |
|
127
|
|
|
* @param String $returnURL |
|
128
|
|
|
* @return String |
|
129
|
|
|
*/ |
|
130
|
|
|
public static function connect_url($returnURL = "", $existingMember = false) |
|
131
|
|
|
{ |
|
132
|
|
|
$backURLString = ""; |
|
133
|
|
|
if ($returnURL) { |
|
134
|
|
|
$backURLString = '?BackURL='.urlencode($returnURL); |
|
135
|
|
|
} |
|
136
|
|
|
$method = self::my_service_name()."Connect"; |
|
137
|
|
|
$className = self::my_class_name(); |
|
138
|
|
|
return $className."/".$method."/".$backURLString; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* redirects to login prompt, lets the user log in and returns to |
|
144
|
|
|
* the returnURL specified. |
|
145
|
|
|
* @param String $returnURL |
|
146
|
|
|
* @return REDIRECTS! |
|
|
|
|
|
|
147
|
|
|
*/ |
|
148
|
|
|
public static function redirect_to_login_prompt($returnURL = "") |
|
|
|
|
|
|
149
|
|
|
{ |
|
150
|
|
|
$className = self::my_class_name(); |
|
151
|
|
|
return self::curr()->redirect($className::login_url($returnURL)); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* The class being called |
|
156
|
|
|
* (e.g. FacebookCallback::my_class_name should return FacebookCallback) |
|
157
|
|
|
* @return String |
|
158
|
|
|
*/ |
|
159
|
|
|
protected static function my_class_name() |
|
160
|
|
|
{ |
|
161
|
|
|
return get_called_class(); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* The current ClassName without the "Callback" portion. |
|
166
|
|
|
* @return String |
|
167
|
|
|
*/ |
|
168
|
|
|
protected static function my_service_name() |
|
169
|
|
|
{ |
|
170
|
|
|
return str_replace("Callback", "", self::my_class_name()); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* The name of the security form. |
|
175
|
|
|
* @return String |
|
176
|
|
|
*/ |
|
177
|
|
|
protected function my_security_form() |
|
178
|
|
|
{ |
|
179
|
|
|
return self::my_class_name()."LoginForm"; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* returns Absolute URL to a link within this controller, |
|
184
|
|
|
* by default it is the "Connect" link, because this controller |
|
185
|
|
|
* always needs an action. |
|
186
|
|
|
* @return String |
|
187
|
|
|
*/ |
|
188
|
|
|
public function Title() |
|
189
|
|
|
{ |
|
190
|
|
|
return self::my_service_name(); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* returns Absolute URL to a link within this controller, |
|
195
|
|
|
* by default it is the "Connect" link, because this controller |
|
196
|
|
|
* always needs an action. |
|
197
|
|
|
* @return String |
|
198
|
|
|
*/ |
|
199
|
|
|
public function AbsoluteLink($action = "") |
|
200
|
|
|
{ |
|
201
|
|
|
if (!$action) { |
|
202
|
|
|
$action = $this->serviceName."Connect"; |
|
|
|
|
|
|
203
|
|
|
} |
|
204
|
|
|
return Director::absoluteURL($this->Link($action)); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* returns relative URL to a link within this controller, |
|
209
|
|
|
* by default it is the "Connect" link, because this controller |
|
210
|
|
|
* always needs an action. |
|
211
|
|
|
* @return String |
|
212
|
|
|
*/ |
|
213
|
|
|
public function Link($action = "") |
|
214
|
|
|
{ |
|
215
|
|
|
if (!$action) { |
|
216
|
|
|
$action = $this->serviceName."Connect"; |
|
|
|
|
|
|
217
|
|
|
} |
|
218
|
|
|
$className = self::my_class_name(); |
|
219
|
|
|
return self::join_links($className, $action); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
public static function is_valid_user($screen_name) |
|
223
|
|
|
{ |
|
224
|
|
|
return true; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
|
|
228
|
|
|
//========================================== HELPER METHODS ===================================== |
|
229
|
|
|
|
|
230
|
|
|
public function __construct() |
|
231
|
|
|
{ |
|
232
|
|
|
parent::__construct(); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* you need to add an action |
|
237
|
|
|
*/ |
|
238
|
|
|
public function index() |
|
239
|
|
|
{ |
|
240
|
|
|
if (Director::isDev()) { |
|
241
|
|
|
return $this->renderWith(array("SocialIntegrationControllerBaseClass")); |
|
242
|
|
|
} else { |
|
243
|
|
|
die("For security reasons, this service works in DEV move only."); |
|
|
|
|
|
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* works out best Return URL |
|
249
|
|
|
* @param Boolean $hasAbsoluteBaseURL - should it include the Base URL (e.g. http://www.mysite.com) |
|
250
|
|
|
* @return String |
|
251
|
|
|
*/ |
|
252
|
|
|
protected function returnURL($hasAbsoluteBaseURL = false) |
|
253
|
|
|
{ |
|
254
|
|
|
$returnURL = "/Security/login/"; |
|
255
|
|
|
if (!empty($this->requestParams["BackURL"])) { |
|
256
|
|
|
$returnURL = $this->requestParams["BackURL"]; |
|
257
|
|
|
} elseif (Session::get("BackURL")) { |
|
258
|
|
|
$returnURL = Session::get("BackURL"); |
|
259
|
|
|
Session::set("BackURL", ""); |
|
260
|
|
|
Session::clear("BackURL"); |
|
261
|
|
|
} |
|
262
|
|
|
$returnURL = urldecode($returnURL); |
|
263
|
|
|
if ($hasAbsoluteBaseURL) { |
|
264
|
|
|
$returnURL = Director::absoluteBaseURL().$returnURL; |
|
265
|
|
|
} |
|
266
|
|
|
return $returnURL; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
public function Tests() |
|
270
|
|
|
{ |
|
271
|
|
|
if (Director::isDev()) { |
|
272
|
|
|
$dos = new DataObjectSet(); |
|
273
|
|
|
$tests = array( |
|
274
|
|
|
"connect" => "connect to this service", |
|
275
|
|
|
"login" => "do a traditional log in to this service", |
|
276
|
|
|
"meonservice" => "what details does this service know about me right now, retrieve from service", |
|
277
|
|
|
"meondatabase" => "what data is stored with current member", |
|
278
|
|
|
"updates" => "show my latest updates", |
|
279
|
|
|
"friends" => "get a list of my friends (or the equivalent (e.g. followers))", |
|
280
|
|
|
"friendssearch" => "????", |
|
281
|
|
|
"isvaliduser" => "????", |
|
282
|
|
|
"sendmessage" => "test sending a message", |
|
283
|
|
|
"remove" => "remove this service from my account" |
|
284
|
|
|
); |
|
285
|
|
|
foreach ($tests as $test => $description) { |
|
286
|
|
|
$dos->push( |
|
287
|
|
|
new ArrayData( |
|
|
|
|
|
|
288
|
|
|
array( |
|
289
|
|
|
"Link" => "/".$this->Link("test/".$test."/"), |
|
290
|
|
|
"Name" => "<strong>".$test."</strong>".": ".$description |
|
291
|
|
|
) |
|
292
|
|
|
) |
|
293
|
|
|
); |
|
294
|
|
|
} |
|
295
|
|
|
return $dos; |
|
296
|
|
|
} |
|
297
|
|
|
return null; |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
|
|
301
|
|
|
public function menondatabase() |
|
302
|
|
|
{ |
|
303
|
|
|
//to be completed |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
public function test($request) |
|
|
|
|
|
|
307
|
|
|
{ |
|
308
|
|
|
$className = self::my_class_name(); |
|
309
|
|
|
$testType = $request->param("ID"); |
|
310
|
|
|
$IDField = self::my_service_name()."ID"; |
|
311
|
|
|
echo "<h2>TEST: $testType</h2><pre>"; |
|
312
|
|
|
switch ($testType) { |
|
313
|
|
|
case "connect": |
|
314
|
|
|
return $this->connectUser("/$className/test/meonservice/"); |
|
|
|
|
|
|
315
|
|
|
break; |
|
|
|
|
|
|
316
|
|
|
case "login": |
|
317
|
|
|
print_r($className::redirect_to_login_prompt("/$className/test/meonservice/")); |
|
318
|
|
|
break; |
|
319
|
|
|
case "meonservice": |
|
320
|
|
|
$outcome = $className::get_current_user(); |
|
321
|
|
|
if (!$outcome) { |
|
322
|
|
|
echo "NOT CONNECTED"; |
|
323
|
|
|
} else { |
|
324
|
|
|
print_r($outcome); |
|
325
|
|
|
} |
|
326
|
|
|
break; |
|
327
|
|
|
case "meondatabase": |
|
328
|
|
|
$this->meondatabase(); |
|
|
|
|
|
|
329
|
|
|
break; |
|
330
|
|
|
case "updates": |
|
331
|
|
|
print_r($className::get_updates()); |
|
332
|
|
|
break; |
|
333
|
|
|
case "friends": |
|
334
|
|
|
print_r($className::get_list_of_friends(-1)); |
|
335
|
|
|
break; |
|
336
|
|
|
case "friendssearch": |
|
337
|
|
|
print_r($className::get_list_of_friends(-1, "john")); |
|
338
|
|
|
break; |
|
339
|
|
|
case "isvaliduser": |
|
340
|
|
|
$member = Member::currentUser(); |
|
341
|
|
|
if ($member) { |
|
342
|
|
|
if (self::my_service_name() == "Twitter") { |
|
343
|
|
|
$IDField = "TwitterID"; |
|
344
|
|
|
} |
|
345
|
|
|
$id = $member->$IDField; |
|
346
|
|
|
} else { |
|
347
|
|
|
$id = ""; |
|
348
|
|
|
} |
|
349
|
|
|
print_r($className::is_valid_user($id)); |
|
350
|
|
|
break; |
|
351
|
|
|
case "sendmessage": |
|
352
|
|
|
$member = Member::currentUser(); |
|
353
|
|
|
$otherVariables = array(); |
|
354
|
|
|
if ($member) { |
|
355
|
|
|
if (self::my_service_name() == "Facebook") { |
|
356
|
|
|
$otherVariables = array( |
|
357
|
|
|
"name" => "test name", |
|
358
|
|
|
"caption" => "test caption", |
|
359
|
|
|
"description" => "test description" |
|
360
|
|
|
); |
|
361
|
|
|
} |
|
362
|
|
|
$fieldName = self::my_service_name()."ID"; |
|
363
|
|
|
$outcome = $className::send_message( |
|
364
|
|
|
$member->$fieldName, |
|
365
|
|
|
$message = "message goes here", |
|
366
|
|
|
$link = "http://www.google.com", |
|
367
|
|
|
$otherVariables |
|
368
|
|
|
); |
|
369
|
|
|
} else { |
|
370
|
|
|
$outcome = "You must be logged in to send a message."; |
|
371
|
|
|
} |
|
372
|
|
|
echo " |
|
373
|
|
|
<h1>Sending message to yourself</h1> |
|
374
|
|
|
<p>OUTCOME: <pre>...$outcome</pre>"; |
|
375
|
|
|
break; |
|
376
|
|
|
case "remove": |
|
377
|
|
|
$method = "Remove".self::my_service_name(); |
|
378
|
|
|
echo $this->$method(null); |
|
379
|
|
|
break; |
|
380
|
|
|
default: |
|
381
|
|
|
echo "no test to run"; |
|
382
|
|
|
} |
|
383
|
|
|
echo "</pre><hr /><a href=\"/$className\">back to test index</a>"; |
|
384
|
|
|
} |
|
385
|
|
|
} |
|
386
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.