/voteApi/limfunction.php

https://gitlab.com/earthm/vote_api_full · PHP · 332 lines · 259 code · 53 blank · 20 comment · 44 complexity · b98e50b60ecddcaab44413641872630a MD5 · raw file

  1. <?php
  2. error_reporting(E_ALL);
  3. ini_set("display_errors", 1);
  4. include("config.php");
  5. function curlget($url,$header=''){
  6. try{
  7. $curl = curl_init();
  8. $curl_options = array(
  9. CURLOPT_URL => $url,
  10. CURLOPT_CUSTOMREQUEST => 'GET',
  11. CURLOPT_RETURNTRANSFER => TRUE,
  12. CURLOPT_HTTPHEADER => $header
  13. );
  14. curl_setopt_array($curl,$curl_options);
  15. if($result = curl_exec($curl)) {
  16. $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  17. $a['status'] = $status;
  18. $a['result'] = $result;
  19. }else{
  20. $a['status'] = FALSE;
  21. $a['result'] = curl_error($curl);
  22. }
  23. return $a;
  24. }catch(Exception $e){
  25. }
  26. }
  27. function connect_db(){
  28. $servername = db_HOSTNAME;
  29. $username = db_USERNAME;
  30. $password = db_PASSWORD;
  31. $dbname = db_DBNAME;
  32. $conn = mysqli_connect($servername, $username, $password,$dbname);
  33. //mysqli_select_db($conn, $dbname);
  34. if (!$conn) {
  35. die("Connection failed: " . mysqli_connect_error());
  36. }
  37. return $conn;
  38. }
  39. function badgeCheck($uid, $action_ref){
  40. $db = connect_db();
  41. if($action_ref == 'all'){
  42. $actions_type_id = -1;
  43. }else{
  44. $sql = "SELECT actions_type_id FROM actions_type WHERE actions_ref_name = '".$action_ref."' AND status = 1";
  45. $query = mysqli_query($db, $sql);
  46. $row = mysqli_fetch_array($query);
  47. $actions_type_id = $row['actions_type_id'];
  48. }
  49. $badgePointlist = conditionsCheck($uid, $actions_type_id);
  50. $updateUserBadge = updateUserBadge($uid, $badgePointlist[0]);
  51. $updateUserPoint = updateUserPoint($uid, $badgePointlist[1]);
  52. echo json_encode($badgePointlist);
  53. }
  54. function conditionsCheck($uid, $actions_type){ ////get all action
  55. $db = connect_db();
  56. $sql = "SELECT * FROM actions LEFT JOIN conditions
  57. ON actions_conditions = conditions_id
  58. LEFT JOIN badge
  59. ON actions_badge = badge_id
  60. LEFT JOIN point
  61. ON actions_point = point_id";
  62. if($actions_type > 0){
  63. $sql .= " WHERE actions_type = ".$actions_type."";
  64. }
  65. //$sql .= " GROUP BY actions_id";
  66. //echo $sql;
  67. $query = mysqli_query($db, $sql);
  68. $return = array();
  69. $return2 = array();
  70. while($row = mysqli_fetch_array($query)){
  71. ////// check if this badge already got
  72. $badgegot = 0;
  73. $pointgot = 0;
  74. if($row['badge_stackable'] == 0 && $row['actions_badge'] != '-1'){
  75. $sql_count = "SELECT count(*) as count FROM users_badge
  76. WHERE users_badge_uid = '".$uid."'
  77. AND users_badge_badge = '".$row['badge_id']."'";
  78. $query_count = mysqli_query($db, $sql_count);
  79. $row_count = mysqli_fetch_array($query_count);
  80. //$badgegot = $row_count['count'];
  81. if($row_count['count'] == 0){
  82. $badgegot = -1;
  83. }
  84. }
  85. ////// check if this point already got
  86. if($row['actions_point'] != '-1'){
  87. $sql_count2 = "SELECT count(*) as count FROM users_point
  88. WHERE uid = '".$uid."'
  89. AND point_id = '".$row['actions_point']."'";
  90. $query_count2 = mysqli_query($db, $sql_count2);
  91. $row_count2 = mysqli_fetch_array($query_count2);
  92. $pointgot = $row_count2['count'];
  93. if($row_count2['count'] == 0){
  94. $pointgot = -1;
  95. }
  96. }
  97. if($badgegot == -1 || $pointgot == -1){
  98. $url = $row['conditions_api'].'?uid='.$uid;
  99. for($i=1;$i<=10;$i++){
  100. $actions_var[$i] = $row['conditions_label_type'.$i] == 4? strtotime($row['actions_var'.$i]) : $row['actions_var'.$i];
  101. if($row['conditions_var'.$i] != ""){
  102. $url .= '&'.$row['conditions_var'.$i].'='.$actions_var[$i];
  103. }
  104. }
  105. $header = array(
  106. 'X-LIMP-SESSION:'.session_id()
  107. );
  108. $result = curlget($url,$header);
  109. $json = json_decode($result['result']);
  110. $decode_result = json_decode($result['result'],TRUE);
  111. $result['result'] = $decode_result;
  112. if($result['status'] == 200){
  113. $xx = json_encode($result['result']);
  114. if($xx == 'true'){
  115. if($badgegot == -1){
  116. array_push($return,$row['badge_id']);
  117. }
  118. if($pointgot == -1){
  119. array_push($return2,$row['point_id']);
  120. }
  121. }
  122. }else{
  123. //echo json_encode($result['result']);
  124. }
  125. }
  126. }
  127. //echo $sql_count2." ".$pointgot;
  128. //echo $url;
  129. //echo json_encode($return);
  130. //echo json_encode($return2);
  131. $join[0] = $return;
  132. $join[1] = $return2;
  133. //echo json_encode($join);
  134. return $join;
  135. }
  136. function updateUserBadge($uid,$badgelist){
  137. $db = connect_db();
  138. $return = true;
  139. foreach($badgelist as $val){
  140. $sql = "INSERT INTO users_badge SET
  141. users_badge_uid = '".$uid."',
  142. users_badge_badge = '".$val."',
  143. users_badge_status = '1'";
  144. $query = mysqli_query($db, $sql);
  145. if(!$query){
  146. $return = false;
  147. }
  148. }
  149. return $return;
  150. }
  151. function updateUserPoint($uid,$pointlist){
  152. $db = connect_db();
  153. $return = true;
  154. foreach($pointlist as $val){
  155. $sql = "INSERT INTO users_point SET
  156. uid = '".$uid."',
  157. point_id = '".$val."',
  158. status = '1'";
  159. $query = mysqli_query($db, $sql);
  160. if(!$query){
  161. $return = false;
  162. }
  163. }
  164. return $return;
  165. }
  166. //////****** Badge Condition ******
  167. ////////// ---- check has friend over $friend
  168. function friends($uid, $friend = 0){
  169. $url = base_URL."friends/index.php?uid=".$uid; //// get friend data from limpanzee db
  170. $header = array(
  171. 'X-LIMP-SESSION:'.session_id()
  172. );
  173. $result = curlget($url,$header);
  174. //$result = json_encode($result);
  175. //print_r($result);
  176. if($result['result'] >= $friend){
  177. return true;
  178. }else{
  179. return false;
  180. }
  181. }
  182. ////////// ---- check user's been at the location $count times between date $de and $ds
  183. function location($uid, $location_id, $count = 0, $ds = 0, $de = 0){
  184. $url = base_URL."location/index.php?uid=".$uid."&l=".$location_id; //// get location data from limpanzee db
  185. $header = array(
  186. 'X-LIMP-SESSION:'.session_id()
  187. );
  188. $result = curlget($url,$header);
  189. $result['result'] = json_decode($result['result'],true);
  190. $tempcount = 0;
  191. foreach($result['result'] as $val){
  192. if($de !=0){
  193. $datetime = strtotime($val['datetime']);
  194. if($datetime >= $ds && $datetime <= $de){
  195. $tempcount++;
  196. }
  197. }else if($de ==0){
  198. $datetime = strtotime($val['datetime']);
  199. if($datetime >= $ds){
  200. $tempcount++;
  201. }
  202. }else{
  203. $tempcount++;
  204. }
  205. }
  206. if($tempcount >= $count){
  207. return true;
  208. }else{
  209. return false;
  210. }
  211. }
  212. ////////// ---- event
  213. function event($uid, $event = 0, $win = 0){
  214. $url = base_URL."event/index.php?uid=".$uid."&e=".$event."&w=".$win;
  215. $header = array(
  216. 'X-LIMP-SESSION:'.session_id()
  217. );
  218. $result = curlget($url,$header);
  219. $result['result'] = json_decode($result['result'],true);
  220. $tempcount = count($result['result']);
  221. if($tempcount > 0){
  222. return true;
  223. }else{
  224. return false;
  225. }
  226. }
  227. ////////// ---- role
  228. function role($uid, $role = 0){
  229. $url = base_URL."role/index.php?uid=".$uid."&r=".$role;
  230. $header = array(
  231. 'X-LIMP-SESSION:'.session_id()
  232. );
  233. $result = curlget($url,$header);
  234. $result['result'] = json_decode($result['result'],true);
  235. $tempcount = count($result['result']);
  236. if($tempcount > 0 && $role != 0){
  237. return true;
  238. }else{
  239. return false;
  240. }
  241. }
  242. //////****** Badge List ******
  243. function badgelist($uid){
  244. $db = connect_db();
  245. $sql = "SELECT * FROM users_badge
  246. WHERE users_badge_uid = ".$uid."
  247. AND users_badge_status = 1";
  248. $query = mysqli_query($db, $sql);
  249. $result = array();
  250. while($row = mysqli_fetch_array($query,MYSQLI_ASSOC)){
  251. array_push($result, $row);
  252. }
  253. return $result;
  254. }
  255. function pointlist($uid){
  256. $db = connect_db();
  257. $sql = "SELECT * FROM users_point as u, point as p
  258. WHERE uid = ".$uid."
  259. AND u.point_id = p.point_id
  260. AND status = 1";
  261. $query = mysqli_query($db, $sql);
  262. $result = array();
  263. while($row = mysqli_fetch_array($query,MYSQLI_ASSOC)){
  264. array_push($result, $row);
  265. }
  266. return $result;
  267. }
  268. function pointsum($uid){
  269. $db = connect_db();
  270. $sql = "SELECT sum(point_point) as sum FROM users_point as u, point as p
  271. WHERE u.uid = ".$uid."
  272. AND u.point_id = p.point_id
  273. AND status = 1";
  274. $query = mysqli_query($db, $sql);
  275. $result = array();
  276. $row = mysqli_fetch_array($query,MYSQLI_ASSOC);
  277. return $row['sum'];
  278. }
  279. ?>