PageRenderTime 57ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/editor/view-form.php

https://bitbucket.org/3tierlogic/3tl.tabbuilder
PHP | 272 lines | 187 code | 62 blank | 23 comment | 31 complexity | bbdddacac06464aebf41879c504fb9c4 MD5 | raw file
  1. <?php
  2. // this file is responsible for displaying "contest form" on facebook
  3. //facebook application
  4. //@author: Muhammad Nouman
  5. header('P3P: CP="CAO PSA OUR"'); // this is IE session issue fix
  6. // includes
  7. require_once('config.php');
  8. require_once('functions.php');
  9. session_start();
  10. $appId = $_SESSION['appId'];
  11. $secret = $_SESSION['appSecret'];
  12. $page_id = $_SESSION['page_id'];
  13. $app_link = $_SESSION['appLink'];
  14. if ( ! isset( $appId ) || ! isset( $secret ) || ! isset( $page_id ) ) exit();
  15. // get values from the sting and then search the db to get correct app secret
  16. $fbconfig['appid' ] = $appId;
  17. $fbconfig['api'] = $appId;
  18. $fbconfig['secret'] = $secret;
  19. //try to include the facebook base file
  20. try{
  21. include_once "facebook/fb_sdk/facebook.php";
  22. }
  23. catch ( Exception $o ) {
  24. error_log($o);
  25. }
  26. $user = null; //facebook user uid
  27. // Create our Application instance.
  28. $facebook = new Facebook(array(
  29. 'appId' => $fbconfig['appid'],
  30. 'secret' => $fbconfig['secret'],
  31. 'cookie' => true
  32. ));
  33. // If the user is from safari and session is not valid, this the session bug, fixed here
  34. if ( ! count($_COOKIE) > 0 && strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') && empty($data) )
  35. {
  36. $signed_var = $_GET["request_var"];
  37. $secret = $fbconfig['secret']; //get app secret from db
  38. include_once('facebook/fb_sdk/sig_control.php');
  39. $data=parse_signed_request($signed_var, $secret);
  40. $page_id=$data["page"]["id"];
  41. $page_data = json_decode(file_get_contents("https://graph.facebook.com/$page_id"));
  42. $page_link=$page_data->link;
  43. echo $page_id;
  44. echo $page_link;
  45. $fbconfig['pageUrl'] = $page_data->link;
  46. $fbconfig['appPageUrl'] = "{$fbconfig['pageUrl']}?sk=app_{$fbconfig['appid' ]}&app_data=true";
  47. $loginUrl = $facebook->getLoginUrl(
  48. array(
  49. 'scope' => 'user_location,user_birthday,email,publish_actions',
  50. 'redirect_uri' => $fbconfig['appPageUrl'],
  51. )
  52. );
  53. // if the user session is not valid on safari then we will redirect to get the authorization
  54. echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
  55. }
  56. // We get user info from API using this code
  57. $user = $facebook->getUser();
  58. if ( $user ) {
  59. try {
  60. $user_info = $facebook->api('/me');
  61. }
  62. catch ( FacebookApiException $e ) {
  63. //print_r( $user_info );
  64. $user = null;
  65. }
  66. }
  67. // get tab to show
  68. $query = "SELECT id FROM pages WHERE fan_page_id = '" . $page_id . "' AND app_id = '" . $appId . "'
  69. AND published = '1' LIMIT 1 ";
  70. $query = mysql_query( $query ) or exit( mysql_error() );
  71. if ( mysql_num_rows( $query ) > 0 ) {
  72. $tabId = mysql_result( $query, 0 );
  73. $tab = ars_getPageInfo( $tabId );
  74. }
  75. // get contest properties
  76. $query = "SELECT * FROM sp_campaign_info WHERE campaign_sid = '".$tab['id']."' LIMIT 1 ";
  77. $query = mysql_query( $query ) or exit( mysql_error() );
  78. if ( mysql_num_rows( $query ) > 0 )
  79. $contestProperties = mysql_fetch_assoc( $query );
  80. // FUNCTION CHECKS, IF A USER CAN ENTER A CONTEST
  81. function contestAvailable( $user_info, $contestProperties ) {
  82. $allow_child = $allow_start = $allow_end = $allow_entries = false;
  83. // get user birthday
  84. function getMinDate( $birthday ) {
  85. $year = (int) substr( $birthday, 6 ) + 18;
  86. $month = substr( $birthday, 0, 2 );
  87. $day = substr( $birthday, 3, 2 );
  88. return strtotime( $year . '-' . $month . '-' . $day );
  89. }
  90. // check age (over 18 years old)
  91. if ( $contestProperties['campaign_is_allow_child'] == '0' ) // children are allowed
  92. $allow_child = true;
  93. elseif ( strlen( $user_info['birthday'] ) < 10 ) // birthday wasn't set
  94. $allow_child = true;
  95. elseif ( getMinDate( $user_info['birthday'] ) < time() ) // user is over 18
  96. $allow_child = true;
  97. else
  98. $allow_child = false;
  99. // check contest start date
  100. if ( strtotime( $contestProperties['campaign_start_time'] ) < time() )
  101. $allow_start = true;
  102. // check contest end date
  103. if ( strtotime( $contestProperties['campaign_end_time'] ) > time() )
  104. $allow_end = true;
  105. // check entries number
  106. $query = "SELECT * FROM sp_campaign_result WHERE cpresult_user_id = '".$user_info['id']."'
  107. AND campaign_sid = '".$contestProperties['campaign_sid']."' ";
  108. $query = mysql_query( $query ) or exit( mysql_error() );
  109. if ( mysql_num_rows( $query ) < $contestProperties['campaign_is_check_reentry'] )
  110. $allow_entries = true;
  111. // return result
  112. if ( ! $allow_entries ) $error = 'We have already received your entry.<br /> Thanks for participating!';
  113. elseif ( ! $allow_start ) $error = 'Sorry, the contest hasn\'t started yet.';
  114. elseif ( ! $allow_end ) $error = 'Sorry, the contest has already ended.';
  115. elseif ( ! $allow_child ) $error = 'Sorry, you must be over 18 years old to participate in the contest.';
  116. else $error = '';
  117. return $error;
  118. } // contestAvailable()
  119. ?><!doctype html>
  120. <html lang="en-US" xmlns:fb="https://www.facebook.com/2008/fbml">
  121. <head>
  122. <meta charset="UTF-8" />
  123. <title><?php echo $tab['name']; ?></title>
  124. <link rel="stylesheet" href="css/style.css?v=1.01" />
  125. <script src="js/jquery.1.7.js"></script>
  126. <script src="js/jquery.countdown.min.js"></script>
  127. <script src="js/fb.js"></script>
  128. <script src="js/both.js"></script>
  129. <script src="js/jquery.validate.js"></script>
  130. <script src="https://widgets.twimg.com/j/2/widget.js"></script>
  131. <script src="https://apis.google.com/js/plusone.js"></script>
  132. <script src="https://maps.google.com/maps/api/js?sensor=false"></script>
  133. <style>* { margin: 0; padding: 0; }</style>
  134. <script>
  135. $(window).load( function() {
  136. // DISPLAY WIDGETS
  137. displayWidgetsOnload( $('.pageBody') );
  138. // FORM AUTOFILLING
  139. var $birthDate = $('#user_fb_birthday').html();
  140. if ( $birthDate != '' ) {
  141. var $birthMonth = $birthDate.substr( 0, 2 );
  142. var $birthDay = $birthDate.substr( 3, 2 );
  143. var $birthYear = $birthDate.substr( 6 );
  144. if ( $('[name="Birthday_Month"]').length )
  145. $('[name="Birthday_Month"]').val( $birthMonth );
  146. if ( $('[name="Birthday_Day"]').length )
  147. $('[name="Birthday_Day"]').val( $birthDay );
  148. if ( $('[name="Birthday_Year"]').length )
  149. $('[name="Birthday_Year"]').val( $birthYear );
  150. } // birthdate
  151. if ( $('[name="Gender"]').length )
  152. $('[name="Gender"]').val( $('#user_fb_gender').html() );
  153. if ( $('[name="First_Name"]').length )
  154. $('[name="First_Name"]').val( $('#user_fb_first_name').html() );
  155. if ( $('[name="Last_Name"]').length )
  156. $('[name="Last_Name"]').val( $('#user_fb_last_name').html() );
  157. if ( $('[name="City"]').length )
  158. $('[name="City"]').val( $('#user_fb_location').html() );
  159. if ( $('[name="Email"]').length )
  160. $('[name="Email"]').val( $('#user_fb_email').html() );
  161. // CONTEST IS NOT AVAILABLE
  162. if ( $('#contest_available').html() != '' ) {
  163. var $myAlert = new myAlert();
  164. $myAlert.show( $('body'), $('.appName').html(), $('#contest_available').html(), true );
  165. }
  166. }); // onload
  167. </script>
  168. </head>
  169. <body class="fb-view">
  170. <!-- include Javascript SDK -->
  171. <div id="fb-root"></div>
  172. <script>jsSDK();</script>
  173. <div id="includes">
  174. <div class="appId"><?php echo $appId; ?></div>
  175. <div class="pageId"><?php echo $page_id; ?></div>
  176. <div class="signedRequest"><?php echo $signed_request; ?></div>
  177. <div class="appLink"><?php echo $_SESSION['appLink']; ?></div>
  178. <div class="appName"><?php echo $tab['name']; ?></div>
  179. <div id="contest_available"><?php echo contestAvailable( $user_info, $contestProperties ); ?></div>
  180. <div id="user_fb_first_name"><?php echo $user_info["first_name"]; ?></div>
  181. <div id="user_fb_last_name"><?php echo $user_info["last_name"]; ?></div>
  182. <div id="user_fb_birthday"><?php echo $user_info["birthday"]; ?></div>
  183. <div id="user_fb_location"><?php echo $user_info["location"]["name"]; ?></div>
  184. <div id="user_fb_gender"><?php echo $user_info["gender"]; ?></div>
  185. <div id="user_fb_email"><?php echo $user_info["email"]; ?></div>
  186. </div><!-- / .includes -->
  187. <form method="post"
  188. action="<?php if ( $tab['ugc'] == '1' ) echo 'view-ugc-form.php'; else echo 'view-thank.php'; ?>">
  189. <?php echo $tab['content_contest_form']; ?>
  190. <input type="hidden" name="user_fb_id" value="<?php echo $user_info["id"]; ?>" />
  191. <input type="hidden" name="campaign_id" value="<?php echo $tab['id']; ?>" />
  192. </form>
  193. <script>
  194. window.fbAsyncInit = function() {
  195. FB.init({
  196. appId : $appId,
  197. status : true,
  198. cookie : true,
  199. xfbml : true
  200. });
  201. };
  202. (function() {
  203. var e = document.createElement('script');
  204. e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js#xfbml=1&appId=<?php echo $appId; ?>';
  205. e.async = true;
  206. document.getElementById('fb-root').appendChild(e);
  207. }());
  208. </script>
  209. </body>
  210. </html>