/lib/iCon/Link.php

https://github.com/sbuckpesch/Quiz_App2 · PHP · 194 lines · 119 code · 38 blank · 37 comment · 15 complexity · 82db5dd2d4cc5d8f57b0046bc053782c MD5 · raw file

  1. <?php
  2. /**
  3. * Link functions for facebook app
  4. *
  5. * functions:
  6. * 1, craete link
  7. * 2, get parameter
  8. * 3, create link html
  9. *
  10. * @author fred
  11. *
  12. */
  13. class Link
  14. {
  15. //current params
  16. protected $_params=array();
  17. /**
  18. * Creates a link to a file which should be shown in the Fanpage tab
  19. * @param String $file Filename which should be loaded to the Fanpage-Tab
  20. * @return String Link to show the commited file in the Fanpage Tab
  21. */
  22. public function createLink($file,$param=array())
  23. {
  24. global $global;
  25. $param['page']=$file;
  26. $param_str='';
  27. foreach($param as $k=>$v)
  28. {
  29. $param_str.=$k.'='.urlencode($v).'&';
  30. }
  31. //$app_data=base64_encode($param_str);
  32. $app_data=base64_encode($param_str);
  33. $link = $global->instance['fb_page_url'] . "?sk=app_" . $global->instance['fb_app_id'] . "&app_data=" . $app_data;
  34. return $link;
  35. }
  36. public function createCanvasLink($file,$param=array())
  37. {
  38. global $global;
  39. $param['page']=$file;
  40. $param_str='';
  41. foreach($param as $k=>$v)
  42. {
  43. $param_str.=$k.'='.($v).'&';
  44. }
  45. $param_str=trim($param_str,"&");
  46. $url='http://apps.facebook.com/'.$global->instance['fb_app_url'].'/index.php?'.$param_str;
  47. return $url;
  48. }
  49. public function createBaseLink($file,$param=array())
  50. {
  51. global $global;
  52. $param['page']=$file;
  53. $param_str='';
  54. foreach($param as $k=>$v)
  55. {
  56. $param_str.=$k.'='.($v).'&';
  57. }
  58. $param_str=trim($param_str,"&");
  59. $url=$global->baseurl.'/index.php?'.$param_str;
  60. return $url;
  61. }
  62. /*
  63. * only for the welcome fanpage , this page does not need any parameter
  64. *
  65. */
  66. public function createIndexLink()
  67. {
  68. global $global;
  69. $link = $global->instance['fb_page_url'] . "?sk=app_" . $global->instance['fb_app_id'] ;
  70. return $link;
  71. }
  72. /*
  73. * get param from current page, the param is store in _GET['app_data']
  74. *
  75. * @param key string parameter key
  76. * @param default the default value if not have parameter
  77. */
  78. public function getParam($key,$default=false)
  79. {
  80. if(isset($_GET[$key]))
  81. return $_GET[$key];
  82. if(isset($this->_params[$key]))
  83. return $this->_params[$key];
  84. else
  85. return $default;
  86. }
  87. /*
  88. * get current page
  89. * @return current page name or false if not exists
  90. */
  91. public function getPage($default)
  92. {
  93. return self::getParam('page',$default);
  94. }
  95. /*
  96. * parse params from signed_request
  97. */
  98. function parseRequest()
  99. {
  100. global $global;
  101. if(!isset($_REQUEST['signed_request']) || isset($global->instance) == false)
  102. {
  103. $this->_params=array();
  104. return false;
  105. }
  106. $secret=$global->instance['fb_app_secret'];
  107. $request=$_REQUEST['signed_request'];
  108. if($request == false)
  109. {
  110. $this->_params=array();
  111. return false;
  112. }
  113. $data = $this->parse_signed_request($request, $secret);
  114. if(!isset($data['app_data']))
  115. return false;
  116. $app_data=base64_decode($data['app_data']);
  117. $param_str=explode("&",$app_data);
  118. $params=array();
  119. foreach($param_str as $v)
  120. {
  121. $arr=explode("=",$v);
  122. if(is_array($arr) && count($arr) == 2)
  123. {
  124. list($kk,$vv)=$arr;
  125. if($kk != false)
  126. $params[$kk]=$vv;
  127. }
  128. }
  129. $this->_params=$params;
  130. }
  131. function parse_signed_request($signed_request, $secret)
  132. {
  133. list($encoded_sig, $payload) = explode('.', $signed_request, 2);
  134. // decode the data
  135. $sig = $this->base64_url_decode($encoded_sig);
  136. $data = json_decode($this->base64_url_decode($payload), true);
  137. if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
  138. error_log('Unknown algorithm. Expected HMAC-SHA256');
  139. return null;
  140. }
  141. // check sig
  142. $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
  143. if ($sig !== $expected_sig) {
  144. error_log('Bad Signed JSON signature!');
  145. return null;
  146. }
  147. return $data;
  148. }
  149. function display()
  150. {
  151. print_r($this->_params);
  152. }
  153. function base64_url_decode($input) {
  154. return base64_decode(strtr($input, '-_', '+/'));
  155. }
  156. }