/
facebookController.php
180 lines (162 loc) · 9.12 KB
/
facebookController.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
<?php
##################################################
#
# Copyright (c) 2004-2013 OIC Group, Inc.
#
# This file is part of Exponent
#
# Exponent is free software; you can redistribute
# it and/or modify it under the terms of the GNU
# General Public License as published by the Free
# Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# GPL: http://www.gnu.org/licenses/gpl.txt
#
##################################################
/**
* @subpackage Controllers
* @package Modules
*/
class facebookController extends expController {
public $useractions = array(
'showall' => 'Facebook Like'
);
public $remove_configs = array(
'aggregation',
'categories',
'comments',
'ealerts',
'facebook',
'files',
'pagination',
'rss',
'tags',
'twitter',
); // all options: ('aggregation','categories','comments','ealerts','facebook','files','pagination','rss','tags''twitter',)
public $codequality = 'beta';
static function displayname() {
return gt("Facebook");
}
static function description() {
return gt("Display Facebook Widgets");
}
static function author() {
return "Dave Leffler";
}
public function showall() {
global $router;
expHistory::set('viewable', $this->params);
if (!empty($this->config['url_type'])) {
if ($this->config['url_type'] == 1) {
$url = $router->current_url;
} else {
$url = $this->config['facebook_url'];
}
} else $url = URL_FULL;
assign_to_template(array(
'facebook_url'=>$url
));
}
public static function postStatus($params=array()) {
if (!empty($params)) {
// Include facebook class
require_once(BASE . "external/facebook-php-sdk-3.2.2/src/facebook.php");
// configuration
// $desc = 'Facebook constantly changes their SDK and methods for communicating with Facebook. The script in this post supports the latest Facebook authentication changes that will be implemented i October 2012.';
// $pic = 'http://blog.phpinfinite.com/wp-content/uploads/2012/11/post_to_facebook_from_php.jpg';
// $action_name = 'Go to PHP Infinite';
// $action_link = 'http://blog.phpinfinte.com';
$post = new $params['model']($params['id']);
$facebook = new Facebook(array(
'appId' => $params['config']['app_id'],
'secret' => $params['config']['app_secret'],
'cookie' => true,
// 'fileUpload' => true // this is important !
));
$accesstoken = $facebook->getAccessToken();
if (!empty($accesstoken)) {
try {
$attachment = array(
'access_token' => $accesstoken,
'message' => expString::summarize($post->body),
'name' => $post->title,
'link' => expCore::makeLink(array('controller'=>$params['orig_controller'], 'action'=>'show','title'=>$post->sef_url)),
// 'description' => $desc,
// 'picture'=>$pic,
// 'actions' => json_encode(array('name' => $action_name,'link' => $action_link))
);
$status = $facebook->api("/".$params['config']['facebook_page']."/feed", "post", $attachment);
if (!empty($status)) $status = gt('New Facebook Status posted');
flash('message', $status);
} catch (FacebookApiException $e) {
// header("Location:{$facebook->getLoginUrl(array('scope' => 'photo_upload,user_status,publish_stream,user_photos,manage_pages'))}");
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id=". $params['config']['app_id'] . "&redirect_uri=" . urlencode(URL_FULL) . "&scope=publish_stream,user_about_me,read_friendlists,offline_access,publish_actions,friends_photos,,user_photos,photo_upload,user_status,manage_pages". "&state=" . $_SESSION['fb_state'];
echo("<script> window.location.href='" . $dialog_url . "'</script>");
error_log($e);
flash('error', $e->getMessage());
}
} else {
// you're not logged in, the application will try to log in to get a access token
// header("Location:{$facebook->getLoginUrl(array('scope' => 'photo_upload,user_status,publish_stream,user_photos,manage_pages'))}");
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id=". $params['config']['app_id'] . "&redirect_uri=" . urlencode(URL_FULL) . "&scope=publish_stream,user_about_me,read_friendlists,offline_access,publish_actions,friends_photos,,user_photos,photo_upload,user_status,manage_pages". "&state=" . $_SESSION['fb_state'];
echo("<script> window.location.href='" . $dialog_url . "'</script>");
$status = gt('Permissions were not yet set on your Facebook page, please try again');
flash('error', $status);
}
}
}
public static function postEvent($params=array()) {
if (!empty($params)) {
// Include facebook class
require_once(BASE . "external/facebook-php-sdk-3.2.2/src/facebook.php");
// configuration
// $desc = 'Facebook constantly changes their SDK and methods for communicating with Facebook. The script in this post supports the latest Facebook authentication changes that will be implemented i October 2012.';
// $pic = 'http://blog.phpinfinite.com/wp-content/uploads/2012/11/post_to_facebook_from_php.jpg';
// $action_name = 'Go to PHP Infinite';
// $action_link = 'http://blog.phpinfinte.com';
$eventdate = new eventdate($params['id']);
$eventdate->event = new event($eventdate->event_id);
$facebook = new Facebook(array(
'appId' => $params['config']['app_id'],
'secret' => $params['config']['app_secret'],
'cookie' => true,
// 'fileUpload' => true // this is important !
));
$accesstoken = $facebook->getAccessToken();
if (!empty($accesstoken)) {
try {
$attachment = array(
'access_token' => $accesstoken,
"name"=>$eventdate->event->title,
"description"=>expString::summarize($eventdate->event->body) . ' ' . expCore::makeLink(array('controller'=>$params['orig_controller'], 'action'=>'show','date_id'=>$eventdate->id)),
"start_time"=>date('c',$eventdate->date + $eventdate->event->eventstart),
// "end_time"=>date('c',$eventdate->date + $eventdate->event->eventend),
// "location"=>$location,
// 'description' => $desc,
// 'picture'=>$pic,
// 'actions' => json_encode(array('name' => $action_name,'link' => $action_link))
);
$result = $facebook->api("/".$params['config']['facebook_page']."/events", "post", $attachment);
$facebookEventId = $result['id'];
$status = gt('New Facebook Event posted') . ' - ' . $facebookEventId;
flash('message', $status);
} catch (FacebookApiException $e) {
// header("Location:{$facebook->getLoginUrl(array('scope' => 'photo_upload,user_status,publish_stream,user_photos,manage_pages,create_event'))}");
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id=". $params['config']['app_id'] . "&redirect_uri=" . urlencode(URL_FULL) . "&scope=publish_stream,user_about_me,read_friendlists,offline_access,publish_actions,friends_photos,,user_photos,photo_upload,user_status,manage_pages,create_event". "&state=" . $_SESSION['fb_state'];
echo("<script> window.location.href='" . $dialog_url . "'</script>");
error_log($e);
flash('error', $e->getMessage());
}
} else {
// you're not logged in, the application will try to log in to get a access token
// header("Location:{$facebook->getLoginUrl(array('scope' => 'photo_upload,user_status,publish_stream,user_photos,manage_pages,create_event'))}");
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id=". $params['config']['app_id'] . "&redirect_uri=" . urlencode(URL_FULL) . "&scope=publish_stream,user_about_me,read_friendlists,offline_access,publish_actions,friends_photos,,user_photos,photo_upload,user_status,manage_pages,create_event". "&state=" . $_SESSION['fb_state'];
echo("<script> window.location.href='" . $dialog_url . "'</script>");
$status = gt('Permissions were not yet set on your Facebook page, please try again');
flash('error', $status);
}
}
}
}
?>