PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/editor/view.php

https://bitbucket.org/3tierlogic/3tl.tabbuilder
PHP | 219 lines | 146 code | 44 blank | 29 comment | 17 complexity | a1caef985adc867b02d8cac791859a45 MD5 | raw file
  1. <?php
  2. // this file is responsible for displaying tabs on facebook
  3. header('P3P: CP="CAO PSA OUR"');
  4. // includes
  5. require_once('config.php');
  6. require_once('functions.php');
  7. $signed_request = $_REQUEST["signed_request"];
  8. $appId = $_GET["id"];
  9. if ( ! isset( $appId ) ) exit();
  10. $secret = ars_get_secret_by_app_id( $appId, $app_details );
  11. if ( empty( $secret ) ) exit();
  12. include_once('facebook/fb_sdk/sig_control.php');
  13. $data = parse_signed_request( $signed_request, $secret );
  14. // Safari looses session in iframe, so this is the work around for that fix
  15. $u_agent = $_SERVER['HTTP_USER_AGENT'];
  16. if ( preg_match( '/Safari/i', $u_agent ) ) {
  17. $status = $data["app_data"];
  18. // in case of safari we redirect the users to authorization page, when they land back
  19. // we detect that they are coming after authorization and we take them to frorm page
  20. if ( $status ) {
  21. // redirect if safari has lost the session
  22. include_once('view-form.php');
  23. exit();
  24. }
  25. } // safari fix end
  26. $page_fan = $data["page"]["liked"];
  27. $page_admin = $data["page"]["admin"];
  28. $page_id = $data["page"]["id"];
  29. $page_data = json_decode( file_get_contents("https://graph.facebook.com/$page_id") );
  30. $page_link = $page_data->link;
  31. // store some variables for contest form page
  32. session_start();
  33. $_SESSION['appId'] = $appId;
  34. $_SESSION['appSecret'] = $secret;
  35. $_SESSION['page_id'] = $page_id;
  36. // get tab to show
  37. $query = "SELECT id FROM pages WHERE fan_page_id = '".$page_id."' AND app_id = '".$appId."'
  38. AND published = '1' LIMIT 1 ";
  39. $query = mysql_query( $query ) or exit( mysql_error() );
  40. // if tab is published - show it
  41. if ( mysql_num_rows( $query ) > 0 ) {
  42. $tabId = mysql_result( $query, 0 );
  43. $tab = ars_getPageInfo( $tabId );
  44. }
  45. // if not published, check if it was created
  46. else {
  47. $query = "SELECT id FROM pages WHERE fan_page_id = '".$page_id."' AND app_id = '".$appId."'
  48. AND published = '0' LIMIT 1 ";
  49. $query = mysql_query( $query ) or exit( mysql_error() );
  50. // if yes - show default page
  51. if ( mysql_num_rows( $query ) > 0 ) $commingSoon = true;
  52. // if invalid request - don't show anything
  53. else $commingSoon = false; //exit();
  54. }
  55. // GET USER ID FOR CONTEST
  56. //try to include the facebook base file
  57. try{
  58. include_once "facebook/fb_sdk/facebook.php";
  59. }
  60. catch ( Exception $o ) {
  61. error_log($o);
  62. }
  63. $user = null; //facebook user uid
  64. // Create our Application instance.
  65. $facebook = new Facebook(array(
  66. 'appId' => $appId,
  67. 'secret' => $secret,
  68. 'cookie' => true
  69. ));
  70. // We get user info from API using this code
  71. $user = $facebook->getUser();
  72. if ( $user ) {
  73. try {
  74. $user_info = $facebook->api('/me');
  75. }
  76. catch ( FacebookApiException $e ) {
  77. print_r( $user_info );
  78. $user = null;
  79. }
  80. }
  81. ?><!doctype html>
  82. <html lang="en-US" xmlns:fb="https://www.facebook.com/2008/fbml">
  83. <head>
  84. <meta charset="UTF-8" />
  85. <title><?php echo $tab['name']; ?></title>
  86. <link rel="stylesheet" href="css/style.css?v=1.01" />
  87. <script src="js/jquery.1.7.js"></script>
  88. <script src="js/jquery.countdown.min.js"></script>
  89. <script src="js/fb.js"></script>
  90. <script src="js/both.js"></script>
  91. <script src="https://widgets.twimg.com/j/2/widget.js"></script>
  92. <script src="https://apis.google.com/js/plusone.js"></script>
  93. <script src="https://maps.google.com/maps/api/js?sensor=false"></script>
  94. <style>* { margin: 0; padding: 0; }</style>
  95. <script>
  96. $(window).load( function() {
  97. // DISPLAY WIDGETS
  98. displayWidgetsOnload( $('.pageBody') );
  99. });
  100. </script>
  101. </head>
  102. <body class="fb-view">
  103. <!-- include Javascript SDK -->
  104. <div id="fb-root"></div>
  105. <script>jsSDK();</script>
  106. <script>
  107. window.fbAsyncInit = function() {
  108. FB.init({
  109. appId: '<?php echo $appId; ?>',
  110. status: true,
  111. cookie: true,
  112. xfbml: true,
  113. oauth: true
  114. });
  115. }; // fbAsyncInit()
  116. // Load the SDK Asynchronously
  117. (function() {
  118. var e = document.createElement('script'); e.async = true;
  119. e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
  120. document.getElementById('fb-root').appendChild(e);
  121. }());
  122. function contestEnterFunc( response ) {
  123. FB.login( function( response ) {
  124. if ( response.authResponse ) {
  125. // we have to pass $signed_request for safari fix
  126. location.href = 'view-form.php?request_var=<?php echo $signed_request; ?>';
  127. } else {
  128. //user cancelled login or did not grant authorization
  129. alert("Please agree to the Facebook permissions in order to participate in the contest.");
  130. }
  131. }, { scope:'user_location, user_birthday, email, publish_actions' } );
  132. } // contestEnterFunc()
  133. </script>
  134. <?php // show content
  135. // page was not created - show default
  136. if ( $commingSoon ) {
  137. echo '<h1 style="text-align:center;">The page is comming soon</h1>';
  138. }
  139. // page was created
  140. else {
  141. // user wants to see ugc gallery
  142. if ( strpos( $_SERVER['QUERY_STRING'], 'show=ugc_gallery' ) !== false ) {
  143. echo $tab['content_contest_ugc_gallery'];
  144. // get all ugc contest images (they will be moved to the gallery by js)
  145. echo '<div class="hiddenUgcContestImages">' . prepareUgcGalleryImages( $tab['id'], $user_info['id'] ) . '</div>';
  146. }
  147. // user is a fan and contest is set - show contest page
  148. elseif ( $page_fan && $tab['contest'] == '1' ) {
  149. echo $tab['content_contest_fans'];
  150. }
  151. // user is a fan and contest is not set - show fans page
  152. elseif ( $page_fan ) {
  153. echo $tab['content_fans'];
  154. }
  155. // user is not a fan - show nonfans page
  156. else {
  157. echo $tab['content_nonfans'];
  158. // we also can show fans content, if nonfans is not set
  159. // if ( ! strpos( $tab['content_nonfans'], '"block"' ) ) echo $tab['content_fans'];
  160. }
  161. } // page was created
  162. ?>
  163. <?php $_SESSION['appLink'] = $page_link . '?sk=app_' . $appId; ?>
  164. <div id="includes">
  165. <div class="appId"><?php echo $appId; ?></div>
  166. <div class="pageId"><?php echo $page_id; ?></div>
  167. <div class="signedRequest"><?php echo $signed_request; ?></div>
  168. <div class="appLink"><?php echo $_SESSION['appLink']; ?></div>
  169. <div class="appName"><?php echo $tab['name']; ?></div>
  170. <div class="fbUserId"><?php echo $user_info['id']; ?></div>
  171. <div class="userIP"><?php echo $_SERVER['REMOTE_ADDR']; ?></div>
  172. </div><!-- / .includes -->
  173. <script src="https://connect.facebook.net/en_US/all.js"></script>
  174. </body>
  175. </html>