1 | <?php |
---|
2 | // config and whatnot |
---|
3 | $config = dirname(__FILE__) . '/../../hybridauth/config.php'; |
---|
4 | require_once( "../../hybridauth/Hybrid/Auth.php" ); |
---|
5 | |
---|
6 | // initialise hybridauth |
---|
7 | $hybridauth = new Hybrid_Auth( $config ); |
---|
8 | |
---|
9 | // selected provider name |
---|
10 | $provider = @ trim( strip_tags( $_GET["provider"] ) ); |
---|
11 | |
---|
12 | // check if the user is currently connected to the selected provider |
---|
13 | if( ! $hybridauth->isConnectedWith( $provider ) ){ |
---|
14 | // redirect him back to login page |
---|
15 | header( "Location: login.php?error=Your are not connected to $provider or your session has expired" ); |
---|
16 | } |
---|
17 | ?> |
---|
18 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
---|
19 | <html lang="en"> |
---|
20 | <head> |
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
---|
22 | <link rel="stylesheet" href="public/css.css" type="text/css"> |
---|
23 | </head> |
---|
24 | <body> |
---|
25 | <table width="90%" border="0" cellpadding="2" cellspacing="2"> |
---|
26 | <tr> |
---|
27 | <td valign="top"> |
---|
28 | <?php |
---|
29 | include "includes/menu.php"; |
---|
30 | ?> |
---|
31 | <fieldset> |
---|
32 | <legend>Post feed to Facebook pages</legend> |
---|
33 | <?php |
---|
34 | try{ |
---|
35 | $adapter = $hybridauth->getAdapter( $provider ); |
---|
36 | |
---|
37 | // ask facebook api for the users accounts |
---|
38 | $accounts = $adapter->api()->api('/me/accounts'); |
---|
39 | |
---|
40 | if( ! count( $accounts["data"] ) ){ |
---|
41 | ?> |
---|
42 | <p> |
---|
43 | NO pages found for the current user! |
---|
44 | </p> |
---|
45 | <p> |
---|
46 | <b>Note</b>: To be able to post to facebook pages you should: |
---|
47 | </p> |
---|
48 | <ol> |
---|
49 | <li>Add <b>"manage_pages"</b> to the requested scope in the configuration,</li> |
---|
50 | <li>Logout from Facebook provider,</li> |
---|
51 | <li>Re sign-in with Facebook.</li> |
---|
52 | </ol> |
---|
53 | <?php |
---|
54 | } |
---|
55 | else{ |
---|
56 | ?> |
---|
57 | <form action="" method="post"> |
---|
58 | <table width="100%" border="0"> |
---|
59 | <tr> |
---|
60 | <td width="80">Page</td> |
---|
61 | <td> |
---|
62 | <select name="page_id" style="width:658px;"> |
---|
63 | <?php foreach( $accounts["data"] as $account ){ ?> |
---|
64 | <option value="<?php echo $account['id']; ?>"><?php echo $account['name']; ?></option> |
---|
65 | <?php } ?> |
---|
66 | </select> |
---|
67 | </td> |
---|
68 | </tr> |
---|
69 | <tr> |
---|
70 | <td valign="top">Message</td> |
---|
71 | <td><textarea name="message" style="width:650px;height:80px;"></textarea></td> |
---|
72 | </tr> |
---|
73 | <tr> |
---|
74 | <td> </td> |
---|
75 | <td><input type="submit" style="width:658px;height:32px;" value="Send" /></td> |
---|
76 | </tr> |
---|
77 | <tr> |
---|
78 | <td colspan="2" align="center"> |
---|
79 | <br /> |
---|
80 | <?php |
---|
81 | if( isset( $_POST["page_id"] ) ){ |
---|
82 | $message = trim( $_POST["message"] ); |
---|
83 | |
---|
84 | // is message not empty |
---|
85 | if( empty( $message ) ){ |
---|
86 | echo "<b style='color:red'>Write somthing on the textarea.</b>"; |
---|
87 | } |
---|
88 | else{ |
---|
89 | $page_id = $_POST["page_id"]; |
---|
90 | $page_token = ""; |
---|
91 | |
---|
92 | foreach( $accounts['data'] as $account ){ |
---|
93 | if( $account['id'] == $page_id ){ |
---|
94 | $page_token = $account['access_token']; |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | $attachment = array( |
---|
99 | 'access_token' => $page_token, |
---|
100 | 'message' => $message, |
---|
101 | 'name' => "Hybridauth Tiny Social Hub - Test", |
---|
102 | 'link' => 'http://hybridauth.sourceforge.net/' |
---|
103 | ); |
---|
104 | |
---|
105 | // ask facebook api to post the message to the selected page |
---|
106 | $adapter->api()->api( "/$page_id/feed", 'POST', $attachment ); |
---|
107 | |
---|
108 | echo "<b style='color:green'>Sent!</b>"; |
---|
109 | } |
---|
110 | } |
---|
111 | ?> |
---|
112 | </td> |
---|
113 | </tr> |
---|
114 | </table> |
---|
115 | </form> |
---|
116 | <?php |
---|
117 | } |
---|
118 | } |
---|
119 | catch( Exception $e ){ |
---|
120 | echo "<b style='color:red'>Well, got an error:</b> " . $e->getMessage(); |
---|
121 | } |
---|
122 | ?> |
---|
123 | </center> |
---|
124 | </fieldset> |
---|
125 | </td> |
---|
126 | <td valign="top" width="250" align="left"> |
---|
127 | <?php |
---|
128 | include "includes/sidebar.php"; |
---|
129 | ?> |
---|
130 | </td> |
---|
131 | </tr> |
---|
132 | </table> |
---|
133 | </body> |
---|
134 | </html> |
---|