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

/actionService.php

https://bitbucket.org/itoxable/chiron-gaming
PHP | 436 lines | 365 code | 64 blank | 7 comment | 54 complexity | 50f2635c704d23c10f9c6161d53264eb MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. <?php
  2. include_once "general_include.php";
  3. include_once("adodb/dbconfig.php");
  4. //include_once "datafeed.php";
  5. if(!isset($_SESSION['user_id'])){
  6. echo "session not started";
  7. return;
  8. }
  9. class WithdrawStatus{
  10. public static $OPEN = 'OPEN';
  11. public static $PENDING = 'PENDING';
  12. public static $COMPLETED = 'COMPLETED';
  13. public static $DECLINED = 'DECLINED';
  14. public static $REQUESTED = 'REQUESTED';
  15. }
  16. class NotificationTypes{
  17. public static $GENERAL= 0;
  18. public static $SCHEDULE_ALERT = 1;
  19. public static $UNKNOWN = 2;
  20. public static $INSTANT_LESSON = 3;
  21. public static $MESSAGE = 4;
  22. }
  23. function getPostOrGetParam($param){
  24. if(isset($_POST[$param]))
  25. return $_POST[$param];
  26. else{
  27. if(isset($_GET[$param]))
  28. return $_GET[$param];
  29. else
  30. return "";
  31. }
  32. }
  33. function insertNotifications($userId, $type=1, $origin="" , $title="", $introduction="", $text="", $date="",$link="javascript:;"){
  34. $ret = array();
  35. try{
  36. $db = new DBConnection();
  37. $db->getConnection();
  38. $sql = "INSERT INTO `nk_notification` (`user_id`,`nofication_type`";
  39. if($origin != "")
  40. $sql = $sql.",`origin`";
  41. if($title != "")
  42. $sql = $sql.",`title`";
  43. if($introduction != "")
  44. $sql = $sql.",`introduction`";
  45. if($text != "")
  46. $sql = $sql.",`text`";
  47. if($date != "")
  48. $sql = $sql.",`date_value`";
  49. $sql = $sql.",`link`,`modified_date`,`creation_date`) VALUES (".$userId.",".$type;
  50. if($origin != "")
  51. $sql = $sql.",'".$origin."'";
  52. if($title != "")
  53. $sql = $sql.",'".$title."'";
  54. if($introduction != "")
  55. $sql = $sql.",'".$introduction."'";
  56. if($text != "")
  57. $sql = $sql.",'".$text."'";
  58. if($date != "")
  59. $sql = $sql.",'".php2MySqlTime(js2PhpTime($date))."'";
  60. $sql = $sql.",'".$link."',SYSDATE(), SYSDATE());";
  61. if(mysql_query($sql)==false){
  62. $ret['IsSuccess'] = false;
  63. $ret['Msg'] = mysql_error();
  64. }else{
  65. $ret['IsSuccess'] = true;
  66. $ret['Msg'] = 'add success';
  67. $ret['Data'] = $date;
  68. $ret['Id'] = mysql_insert_id();
  69. }
  70. }catch(Exception $e){
  71. $ret['IsSuccess'] = false;
  72. $ret['Msg'] = $e->getMessage();
  73. }
  74. return $ret;
  75. }
  76. function addAsFriend($user){
  77. $ret = array();
  78. try{
  79. $db = new DBConnection();
  80. $db->getConnection();
  81. // $sqlCheck = "SELECT * FROM nk_user_friends_list WHERE friend_status = 2 AND ((nk_user_id_owner='".$_SESSION['user_id']."' or nk_user_id_friend='".$_SESSION['user_id']."') and (nk_user_id_owner='".$user."' or nk_user_id_friend='".$user."'))";
  82. // $handle = mysql_query($sqlCheck);
  83. $sql = "INSERT INTO `nk_user_friends_list` (`nk_user_id_owner`,`nk_user_id_friend`,`friend_status`, `cdate`) VALUES('".$_SESSION['user_id']."','".$user."', 2, SYSDATE());";
  84. if(mysql_query($sql)==false){
  85. $ret['IsSuccess'] = false;
  86. $ret['Msg'] = mysql_error();
  87. }else{
  88. $ret['IsSuccess'] = true;
  89. $ret['Msg'] = 'add success';
  90. $ret['Id'] = mysql_insert_id();
  91. }
  92. }catch(Exception $e){
  93. $ret['IsSuccess'] = false;
  94. $ret['Msg'] = $e->getMessage();
  95. }
  96. return $ret;
  97. }
  98. function removeFromFriendList($user){
  99. $ret = array();
  100. try{
  101. $db = new DBConnection();
  102. $db->getConnection();
  103. $sql = "DELETE FROM `nk_user_friends_list` WHERE (nk_user_id_owner='".$_SESSION['user_id']."' or nk_user_id_friend='".$_SESSION['user_id']."') and (nk_user_id_owner='".$user."' or nk_user_id_friend='".$user."')";
  104. if(mysql_query($sql)==false){
  105. $ret['IsSuccess'] = false;
  106. $ret['Msg'] = mysql_error();
  107. }else{
  108. $ret['IsSuccess'] = true;
  109. $ret['Msg'] = 'add success';
  110. }
  111. }catch(Exception $e){
  112. $ret['IsSuccess'] = false;
  113. $ret['Msg'] = $e->getMessage();
  114. }
  115. return $ret;
  116. }
  117. function changeOnlineStatus($status){
  118. $ret = array();
  119. try{
  120. $sql = "UPDATE ".TABLEPREFIX."_user SET online_status = ".$status." WHERE user_id = ".$_SESSION['user_id'];
  121. if(mysql_query($sql)==false){
  122. $ret['IsSuccess'] = false;
  123. $ret['Msg'] = mysql_error();
  124. }else{
  125. $ret['IsSuccess'] = true;
  126. $ret['Msg'] = 'updated';
  127. $ret['Data'] = $status;
  128. }
  129. }catch(Exception $e){
  130. $ret['IsSuccess'] = false;
  131. $ret['Msg'] = $e->getMessage();
  132. }
  133. return $ret;
  134. }
  135. function addPhonoSessionId($sessionId){
  136. $ret = array();
  137. try{
  138. $sql = "UPDATE ".TABLEPREFIX."_user SET phono_session = '".$sessionId."' WHERE user_id = ".$_SESSION['user_id'];
  139. if(mysql_query($sql)==false){
  140. $ret['IsSuccess'] = false;
  141. $ret['Msg'] ="Error: ".mysql_error();
  142. }else{
  143. $ret['IsSuccess'] = true;
  144. $ret['Msg'] = $_SESSION['user_id'].'==>'.$sessionId;
  145. }
  146. }catch(Exception $e){
  147. $ret['IsSuccess'] = false;
  148. $ret['Msg'] = $e->getMessage();
  149. }
  150. //print_r($ret);
  151. return $ret;
  152. }
  153. function updateUserPoints($points){
  154. $ret = array();
  155. try{
  156. $sql = "UPDATE ".TABLEPREFIX."_user SET total_points = '".$points."' WHERE user_id = ".$_SESSION['user_id'];
  157. if(mysql_query($sql)==false){
  158. $ret['IsSuccess'] = false;
  159. $ret['Msg'] ="Error: ".mysql_error();
  160. }else{
  161. $ret['IsSuccess'] = true;
  162. $ret['Msg'] = "";
  163. }
  164. $_SESSION['ch_user'] = getUser($_SESSION['user_id']);
  165. }catch(Exception $e){
  166. $ret['IsSuccess'] = false;
  167. $ret['Msg'] = $e->getMessage();
  168. }
  169. return $ret;
  170. }
  171. function getUser($userId){
  172. $ret = array();
  173. try{
  174. $sql = "SELECT * FROM ".TABLEPREFIX."_user WHERE user_id = ".$userId;
  175. $handle = mysql_query($sql);
  176. return mysql_fetch_array($handle);
  177. }catch(Exception $e){
  178. $ret['IsSuccess'] = false;
  179. $ret['Msg'] = $e->getMessage();
  180. }
  181. return $ret;
  182. }
  183. function validateWithdraw($key, $quant, $txtCaptcha){
  184. $code = trim($txtCaptcha);
  185. if($code != $_SESSION['captcha']){
  186. $ret['message'] = 'Enter security code correctly';
  187. $ret['IsSuccess'] = false;
  188. return $ret;
  189. }
  190. $ret = array();
  191. $ret['IsSuccess'] = true;
  192. $user = getUser($_SESSION['user_id']);
  193. $password = $user['password'];
  194. $password=md5($password);
  195. $totalPoints = $user['total_points'];
  196. if($quant > $totalPoints){
  197. $ret['message'] = 'You dont have enough points';
  198. $ret['IsSuccess'] = false;
  199. return $ret;
  200. }
  201. if($key != $password){
  202. $ret['message'] = 'Wrong password';
  203. $ret['IsSuccess'] = false;
  204. return $ret;
  205. }
  206. $ret['totalPoints'] = $totalPoints;
  207. return $ret;
  208. }
  209. function doWithdraw($quant, $paypalId, $points){
  210. $ret = array();
  211. try{
  212. include('transaction_status.class.php');
  213. $db = new DBConnection();
  214. $db->getConnection();
  215. $txDate = date("d/m/Y : H:i:s", time());
  216. $txId = md5($_SESSION['user_id'].$txDate);
  217. $sql = "INSERT INTO nk_user_transaction(nk_user_id, tx_date, rate_point_id, status, nk_tx_id,transaction_type,method,method_detail,qty_points, description) VALUES(". $_SESSION['user_id'] .", STR_TO_DATE('". $txDate ."', '%d/%m/%Y : %H:%i:%s'),-1, ". TransactionStatus::WAITING_CONF .",'". $txId ."',0,'Paypal', '".$paypalId."', ".$quant.",'Points Withdraw')";
  218. if(mysql_query($sql)==false){
  219. $ret['IsSuccess'] = false;
  220. $ret['Msg'] = mysql_error();
  221. }else{
  222. $ret['IsSuccess'] = true;
  223. $ret['Msg'] = 'add success';
  224. $ret['Id'] = mysql_insert_id();
  225. $updateResult = updateUserPoints($points-$quant);
  226. return $updateResult;
  227. }
  228. }catch(Exception $e){
  229. $ret['IsSuccess'] = false;
  230. $ret['Msg'] = $e->getMessage();
  231. }
  232. return $ret;
  233. }
  234. function getMessages(){
  235. $ret = array();
  236. $ids = "";
  237. $idsArray = array();
  238. $groupedMessages = array();
  239. try{
  240. $db = new DBConnection();
  241. $db->getConnection();
  242. $sql = "SELECT * FROM `frei_chat` WHERE `to` = ".$_SESSION['user_id']." OR `from` = ".$_SESSION['user_id']." ORDER BY `sent` DESC";
  243. $handle = mysql_query($sql);
  244. while ($message = mysql_fetch_object($handle)) {
  245. $to = $message->to;
  246. $from = $message->from;
  247. if($to != $_SESSION['user_id']){
  248. if(!in_array($to, $idsArray)){
  249. $idsArray[]=$to;
  250. }
  251. $groupedMessages[$to][] = $message;
  252. }else if($from != $_SESSION['user_id']){
  253. if(!in_array($from, $idsArray)){
  254. $idsArray[]=$from;
  255. }
  256. $groupedMessages[$from][] = $message;
  257. }
  258. }
  259. $ret['groupedMessages'] = $groupedMessages;
  260. $idsSize = count($idsArray);
  261. for($i=0; $i<$idsSize; $i++){
  262. $id = $idsArray[$i];
  263. if($i == ($idsSize-1))
  264. $ids = $ids.$id."";
  265. else
  266. $ids = $ids.$id.",";
  267. }
  268. $userssql = "SELECT * FROM `nk_user` WHERE `user_id` in (".$ids.") ORDER BY FIELD(`user_id`,".$ids.")";
  269. //echo "\n".$userssql."\n";
  270. $userhandle = mysql_query($userssql);
  271. while ($user = mysql_fetch_object($userhandle)) {
  272. //echo "\n".'AA'."\n";
  273. $ret['users'][] = $user;
  274. }
  275. $ret['IsSuccess'] = true;
  276. }catch(Exception $e){
  277. $ret['error'] = $e->getMessage();
  278. }
  279. return $ret;
  280. }
  281. function bigintval($value) {
  282. $value = trim($value);
  283. if (ctype_digit($value)) {
  284. return $value;
  285. }
  286. $value = preg_replace("/[^0-9](.*)$/", '', $value);
  287. if (ctype_digit($value)) {
  288. return $value;
  289. }
  290. return 0;
  291. }
  292. function sendMessage($message, $toid, $fromname, $gmttime, $toname=""){
  293. $ret = array();
  294. try{
  295. if($toname == ""){
  296. $user = getUser($toid);
  297. $toname = $user['username'];
  298. }
  299. $gmttime = bigintval($gmttime);
  300. $db = new DBConnection();
  301. $db->getConnection();
  302. $time = time() . str_replace(" ", "", microtime());
  303. $sql = "INSERT INTO frei_chat (`from`, `from_name`, `to`, `to_name`, `message`, `sent`, `recd`, `time`, `GMT_time`, `message_type`, `room_id`) VALUES (".$_SESSION['user_id'].", '".$fromname."', ".$toid.", '".$toname."', '".nl2br($message)."',NOW(),1, ".$time.", ".$gmttime.", 0, -1)";
  304. if(mysql_query($sql)==false){
  305. $ret['IsSuccess'] = false;
  306. $ret['Msg'] = mysql_error();
  307. }else{
  308. $ret['IsSuccess'] = true;
  309. $sql = "SELECT * FROM `frei_chat` where `id` = ".mysql_insert_id();
  310. $handle = mysql_query($sql);
  311. $msg = mysql_fetch_object($handle);
  312. $ret['msg'] = $msg;
  313. //$ret['sent'] = mysql_insert_id();
  314. insertNotifications($toid, NotificationTypes::$MESSAGE,$_SESSION['user_id'] , 'New Message', ($fromname." sent you a new message"), ($fromname." sent you a new message"), "", "/messages.php");
  315. }
  316. }catch(Exception $e){
  317. $ret['IsSuccess'] = false;
  318. $ret['Msg'] = $e->getMessage();
  319. }
  320. return $ret;
  321. }
  322. function pointsTransfer($id,$quant){
  323. $ret = array();
  324. $user = getUser($id);
  325. $ret['status'] = getUserStatus($user);
  326. return $ret;
  327. }
  328. if(isset($_GET['method']) || isset($_POST['method'])){
  329. header('Content-type:text/javascript;charset=UTF-8');
  330. $method = getPostOrGetParam("method");
  331. switch ($method) {
  332. case "addAsFriend":
  333. $ret = addAsFriend(getPostOrGetParam('userid'));
  334. break;
  335. case "changeOnlineStatus":
  336. $ret = changeOnlineStatus(getPostOrGetParam('status'));
  337. break;
  338. case "addPhonoSessionId":
  339. $ret = addPhonoSessionId(getPostOrGetParam('sessionId'));
  340. break;
  341. case "getUser":
  342. $ret = getUser(getPostOrGetParam('userId'));
  343. break;
  344. case "removeFriend":
  345. $ret = removeFromFriendList(getPostOrGetParam('userid'));
  346. break;
  347. case "validateWithdraw":
  348. $ret = validateWithdraw(getPostOrGetParam('key'), getPostOrGetParam('quant'), getPostOrGetParam('txtCaptcha'));
  349. break;
  350. case "doWithdraw":
  351. $ret = doWithdraw(getPostOrGetParam('quant'), getPostOrGetParam('paypalId'), getPostOrGetParam('totalpoints'));
  352. break;
  353. case "doTest":
  354. $ret = doTest(getPostOrGetParam('id'));
  355. break;
  356. case "sendMessage":
  357. $ret = sendMessage(getPostOrGetParam('message'),getPostOrGetParam('toid'),getPostOrGetParam('fromname'),getPostOrGetParam('gmttime'),getPostOrGetParam('toname'));
  358. break;
  359. case "getMessages":
  360. $ret = getMessages();
  361. break;
  362. case "pointsTransfer":
  363. $ret = pointsTransfer(getPostOrGetParam('userid'),getPostOrGetParam('quant'));
  364. break;
  365. case "getTime":
  366. $ret = array();
  367. $ret['DATE'] = date('l jS \of F Y h:i:s A');
  368. break;
  369. }
  370. echo json_encode($ret);
  371. }
  372. ?>