-
Notifications
You must be signed in to change notification settings - Fork 21
/
events.php
118 lines (97 loc) · 3.57 KB
/
events.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
<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
* Sample Events Class
*
* @package PyroCMS
* @subpackage Social Module
* @category events
* @author PyroCMS Dev Team
*/
class Events_Social
{
protected $ci;
public function __construct()
{
$this->ci =& get_instance();
// register the public_controller event when this file is autoloaded
Events::register('post_user_register', array($this, 'save_authentication'));
// Post a blog to twitter and whatnot
Events::register('blog_article_published', array($this, 'post_status'));
// User deleted clean up any authentications
Events::register('user_deleted',array($this,'remove_authentications'));
}
// this will be triggered by the Events::trigger('save_authentication') code in modules/users/controllers/.php
public function save_authentication($user_id)
{
// Let's get ready to interact with users
$this->ci->load->model('social/authentication_m');
$user_hash = $this->ci->session->userdata('user_hash');
$token = $this->ci->session->userdata('token');
// Remove the user_hash now that it's been set
$this->ci->session->unset_userdata('user_hash');
$this->ci->session->unset_userdata('token');
// Attach this account to the logged in user
$this->ci->authentication_m->save(array(
'user_id' => $user_id,
'provider' => $token['provider'],
'uid' => $user_hash['uid'],
'access_token' => $token['access_token'],
'secret' => isset($token['secret']) ? $token['secret'] : null,
'expires' => isset($token['expires']) ? $token['expires'] : null,
'refresh_token' => isset($token['refresh_token']) ? $token['refresh_token'] : null,
));
}
public function post_status($article)
{
$this->ci->load->model('social/credential_m');
$url = site_url('blog/'.date('Y/m').'/'.$this->ci->input->post('slug'));
// Try and post that shit to facebook!
if (($credentials = $this->ci->credential_m->get_active_provider('facebook')))
{
$params = array(
'access_token' => $credentials->access_token,
'name'=> $this->ci->input->post('title'),
'message'=> html_entity_decode(strip_tags($this->ci->input->post('intro'))),
'link' => $url,
);
log_message('info', 'Post status with Facebook: '.json_encode($params));
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'https://graph.facebook.com/me/feed',
CURLOPT_POSTFIELDS => $params,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_VERBOSE => true
));
$result = curl_exec($ch);
}
// Twitter wants it too... yeah she does!
if (($credentials = $this->ci->credential_m->get_active_provider('twitter')))
{
$this->ci->load->library('twitter', array(
'consumer_key' => $credentials->client_key,
'consumer_secret' => $credentials->client_secret,
'oauth_token' => $credentials->access_token,
'oauth_token_secret' => $credentials->secret,
));
$message = character_limiter(strip_tags($this->ci->input->post('title')), 130).' '.$url;
log_message('info', 'Post status with Twitter: '.json_encode(array('status' => $message)));
$this->ci->twitter->post('statuses/update', array('status' => $message));
}
}
public function remove_authentications($users)
{
$this->ci->load->model('social/authentication_m');
foreach($users as $user)
{
$auths = $this->ci->authentication_m->get_many_by(array('user_id'=>$user));
if($auths)
{
foreach($auths as $auth)
{
$this->ci->authentication_m->delete($auth->id);
}
}
}
}
}
/* End of file events.php */