1 | <?php |
---|
2 | session_start(); |
---|
3 | |
---|
4 | // change the following paths if necessary |
---|
5 | $config = dirname(__FILE__) . '/../../hybridauth/config.php'; |
---|
6 | require_once( "../../hybridauth/Hybrid/Auth.php" ); |
---|
7 | |
---|
8 | // check for erros and whatnot |
---|
9 | $error = ""; |
---|
10 | |
---|
11 | if( isset( $_GET["error"] ) ){ |
---|
12 | $error = '<b style="color:red">' . trim( strip_tags( $_GET["error"] ) ) . '</b><br /><br />'; |
---|
13 | } |
---|
14 | |
---|
15 | // if user select a provider to login with |
---|
16 | // then inlcude hybridauth config and main class |
---|
17 | // then try to authenticate te current user |
---|
18 | // finally redirect him to his profile page |
---|
19 | if( isset( $_GET["provider"] ) && $_GET["provider"] ): |
---|
20 | try{ |
---|
21 | // create an instance for Hybridauth with the configuration file path as parameter |
---|
22 | $hybridauth = new Hybrid_Auth( $config ); |
---|
23 | |
---|
24 | // set selected provider name |
---|
25 | $provider = @ trim( strip_tags( $_GET["provider"] ) ); |
---|
26 | |
---|
27 | // try to authenticate the selected $provider |
---|
28 | $adapter = $hybridauth->authenticate( $provider ); |
---|
29 | |
---|
30 | // if okey, we will redirect to user profile page |
---|
31 | $hybridauth->redirect( "profile.php?provider=$provider" ); |
---|
32 | } |
---|
33 | catch( Exception $e ){ |
---|
34 | // In case we have errors 6 or 7, then we have to use Hybrid_Provider_Adapter::logout() to |
---|
35 | // let hybridauth forget all about the user so we can try to authenticate again. |
---|
36 | |
---|
37 | // Display the recived error, |
---|
38 | // to know more please refer to Exceptions handling section on the userguide |
---|
39 | switch( $e->getCode() ){ |
---|
40 | case 0 : $error = "Unspecified error."; break; |
---|
41 | case 1 : $error = "Hybriauth configuration error."; break; |
---|
42 | case 2 : $error = "Provider not properly configured."; break; |
---|
43 | case 3 : $error = "Unknown or disabled provider."; break; |
---|
44 | case 4 : $error = "Missing provider application credentials."; break; |
---|
45 | case 5 : $error = "Authentication failed. The user has canceled the authentication or the provider refused the connection."; break; |
---|
46 | case 6 : $error = "User profile request failed. Most likely the user is not connected to the provider and he should to authenticate again."; |
---|
47 | $adapter->logout(); |
---|
48 | break; |
---|
49 | case 7 : $error = "User not connected to the provider."; |
---|
50 | $adapter->logout(); |
---|
51 | break; |
---|
52 | } |
---|
53 | |
---|
54 | // well, basically your should not display this to the end user, just give him a hint and move on.. |
---|
55 | $error .= "<br /><br /><b>Original error message:</b> " . $e->getMessage(); |
---|
56 | $error .= "<hr /><pre>Trace:<br />" . $e->getTraceAsString() . "</pre>"; |
---|
57 | } |
---|
58 | endif; |
---|
59 | ?> |
---|
60 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
---|
61 | <html lang="en"> |
---|
62 | <head> |
---|
63 | <link rel="stylesheet" href="public/css.css" type="text/css"> |
---|
64 | </head> |
---|
65 | <body> |
---|
66 | <center> |
---|
67 | <br /> |
---|
68 | <h1>Hybridauth Tiny Social Hub</h1> |
---|
69 | |
---|
70 | <?php |
---|
71 | // if we got an error then we display it here |
---|
72 | if( $error ){ |
---|
73 | echo '<p><h3 style="color:red">Error!</h3>' . $error . '</p>'; |
---|
74 | echo "<pre>Session:<br />" . print_r( $_SESSION, true ) . "</pre><hr />"; |
---|
75 | } |
---|
76 | ?> |
---|
77 | <br /> |
---|
78 | |
---|
79 | <table width="500" border="0" cellpadding="2" cellspacing="2"> |
---|
80 | <tr> |
---|
81 | <td align="left" valign="top"> |
---|
82 | <fieldset> |
---|
83 | <legend>Sign-in with one of these providers</legend> |
---|
84 | <a href="?provider=Google">Sign-in with Google</a><br /> |
---|
85 | <a href="?provider=Yahoo">Sign-in with Yahoo</a><br /> |
---|
86 | <a href="?provider=Facebook">Sign-in with Facebook</a><br /> |
---|
87 | <a href="?provider=Twitter">Sign-in with Twitter</a><br /> |
---|
88 | <a href="?provider=MySpace">Sign-in with MySpace</a><br /> |
---|
89 | <a href="?provider=Live">Sign-in with Windows Live</a><br /> |
---|
90 | <a href="?provider=LinkedIn">Sign-in with LinkedIn</a><br /> |
---|
91 | <a href="?provider=Foursquare">Sign-in with Foursquare</a><br /> |
---|
92 | <a href="?provider=AOL">Sign-in with AOL</a><br /> |
---|
93 | </fieldset> |
---|
94 | </td> |
---|
95 | <?php |
---|
96 | // try to get already authenticated provider list |
---|
97 | try{ |
---|
98 | $hybridauth = new Hybrid_Auth( $config ); |
---|
99 | |
---|
100 | $connected_adapters_list = $hybridauth->getConnectedProviders(); |
---|
101 | |
---|
102 | if( count( $connected_adapters_list ) ){ |
---|
103 | ?> |
---|
104 | <td align="left" valign="top"> |
---|
105 | <fieldset> |
---|
106 | <legend>Providers you are logged with</legend> |
---|
107 | <?php |
---|
108 | foreach( $connected_adapters_list as $adapter_id ){ |
---|
109 | echo ' <a href="profile.php?provider=' . $adapter_id . '">Switch to <b>' . $adapter_id . '</b> account</a><br />'; |
---|
110 | } |
---|
111 | ?> |
---|
112 | </fieldset> |
---|
113 | </td> |
---|
114 | <?php |
---|
115 | } |
---|
116 | } |
---|
117 | catch( Exception $e ){ |
---|
118 | echo "Ooophs, we got an error: " . $e->getMessage(); |
---|
119 | |
---|
120 | echo " Error code: " . $e->getCode(); |
---|
121 | |
---|
122 | echo "<br /><br />Please try again."; |
---|
123 | |
---|
124 | echo "<hr /><h3>Trace</h3> <pre>" . $e->getTraceAsString() . "</pre>"; |
---|
125 | } |
---|
126 | ?> |
---|
127 | </tr> |
---|
128 | </table> |
---|
129 | |
---|
130 | <br /> |
---|
131 | <br /> |
---|
132 | |
---|
133 | <table width="60%" border="0" cellspacing="10"> |
---|
134 | <tr> |
---|
135 | <td> |
---|
136 | <hr /> |
---|
137 | This exapmle show how users can login with providers using <b>HybridAuth</b>. It also show how to grab their profile, update their status or to grab their freinds list from services like facebook, twitter, myspace. |
---|
138 | <br /> |
---|
139 | <br /> |
---|
140 | If you want even more providers please goto to HybridAuth web site and download the <a href="http://hybridauth.sourceforge.net/download.html">Additional Providers Package</a>. |
---|
141 | </td> |
---|
142 | </tr> |
---|
143 | </table> |
---|
144 | </html> |
---|