chiron-gaming /actionService.php

Language PHP Lines 437
MD5 Hash 50f2635c704d23c10f9c6161d53264eb Estimated Cost $9,323 (why?)
Repository https://bitbucket.org/itoxable/chiron-gaming.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
<?php
include_once "general_include.php";
include_once("adodb/dbconfig.php");
//include_once "datafeed.php";

if(!isset($_SESSION['user_id'])){
	echo "session not started";
	return;
}

class WithdrawStatus{
    public static $OPEN = 'OPEN';
	public static $PENDING = 'PENDING';
	public static $COMPLETED = 'COMPLETED';
	public static $DECLINED = 'DECLINED';
	public static $REQUESTED = 'REQUESTED';
}
class NotificationTypes{
	public static $GENERAL= 0;
	public static $SCHEDULE_ALERT = 1;
	public static $UNKNOWN = 2;
	public static $INSTANT_LESSON = 3;
	public static $MESSAGE = 4;
}
function getPostOrGetParam($param){
	if(isset($_POST[$param]))
		return $_POST[$param];
	else{
		if(isset($_GET[$param]))
			return $_GET[$param];
		else
			return "";
	}
}

function insertNotifications($userId, $type=1, $origin="" , $title="", $introduction="", $text="", $date="",$link="javascript:;"){ 
	$ret = array();
	try{
		$db = new DBConnection();
		$db->getConnection();
		$sql = "INSERT INTO `nk_notification` (`user_id`,`nofication_type`";
		if($origin != "")
			$sql = $sql.",`origin`";
		if($title != "")
			$sql = $sql.",`title`";
		if($introduction != "")
			$sql = $sql.",`introduction`";
		if($text != "")
			$sql = $sql.",`text`";
		if($date != "")
			$sql = $sql.",`date_value`";
		
		$sql = $sql.",`link`,`modified_date`,`creation_date`) VALUES (".$userId.",".$type;

		if($origin != "")
			$sql = $sql.",'".$origin."'";
		if($title != "")
			$sql = $sql.",'".$title."'";
		if($introduction != "")
			$sql = $sql.",'".$introduction."'";
		if($text != "")
			$sql = $sql.",'".$text."'";
		if($date != "")
			$sql = $sql.",'".php2MySqlTime(js2PhpTime($date))."'";
			
		$sql = $sql.",'".$link."',SYSDATE(), SYSDATE());";

		if(mysql_query($sql)==false){
			$ret['IsSuccess'] = false;
			$ret['Msg'] = mysql_error();
		}else{
			$ret['IsSuccess'] = true;
			$ret['Msg'] = 'add success';
			$ret['Data'] = $date;
			$ret['Id'] = mysql_insert_id();
		}

	}catch(Exception $e){
		$ret['IsSuccess'] = false;
		$ret['Msg'] = $e->getMessage();
	}

	return $ret;
}

function addAsFriend($user){
	$ret = array();
	try{
            
                
		$db = new DBConnection();
		$db->getConnection();
                
//                $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."'))";
//                $handle = mysql_query($sqlCheck);
                
                
		$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());";
		if(mysql_query($sql)==false){
			$ret['IsSuccess'] = false;
			$ret['Msg'] = mysql_error();
		}else{
			$ret['IsSuccess'] = true;
			$ret['Msg'] = 'add success';
			$ret['Id'] = mysql_insert_id();
		}

	}catch(Exception $e){
		$ret['IsSuccess'] = false;
		$ret['Msg'] = $e->getMessage();
	}

	return $ret;
}

function removeFromFriendList($user){
	$ret = array();
	try{
		$db = new DBConnection();
		$db->getConnection();
	
		$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."')";
		
		if(mysql_query($sql)==false){
			$ret['IsSuccess'] = false;
			$ret['Msg'] = mysql_error();
		}else{
			$ret['IsSuccess'] = true;
			$ret['Msg'] = 'add success';
		}

	}catch(Exception $e){
		$ret['IsSuccess'] = false;
		$ret['Msg'] = $e->getMessage();
	}

	return $ret;
}


function changeOnlineStatus($status){
	$ret = array();
	try{
		$sql = "UPDATE ".TABLEPREFIX."_user SET online_status = ".$status." WHERE user_id = ".$_SESSION['user_id'];
		if(mysql_query($sql)==false){
			$ret['IsSuccess'] = false;
			$ret['Msg'] = mysql_error();
		}else{
			$ret['IsSuccess'] = true;
			$ret['Msg'] = 'updated';
			$ret['Data'] = $status;
		}
		
	}catch(Exception $e){
		$ret['IsSuccess'] = false;
		$ret['Msg'] = $e->getMessage();
	}

	return $ret;
}

function addPhonoSessionId($sessionId){
	$ret = array();
	try{
		$sql = "UPDATE ".TABLEPREFIX."_user SET phono_session = '".$sessionId."' WHERE user_id = ".$_SESSION['user_id'];
		if(mysql_query($sql)==false){
			$ret['IsSuccess'] = false;
			$ret['Msg'] ="Error: ".mysql_error();
		}else{
			$ret['IsSuccess'] = true;
			$ret['Msg'] = $_SESSION['user_id'].'==>'.$sessionId;
		}
	
	}catch(Exception $e){
		$ret['IsSuccess'] = false;
		$ret['Msg'] = $e->getMessage();
	}
	//print_r($ret);
	return $ret;
}

function updateUserPoints($points){
    $ret = array();
	try{
		$sql = "UPDATE ".TABLEPREFIX."_user SET total_points = '".$points."' WHERE user_id = ".$_SESSION['user_id'];
		if(mysql_query($sql)==false){
			$ret['IsSuccess'] = false;
			$ret['Msg'] ="Error: ".mysql_error();
		}else{
			$ret['IsSuccess'] = true;
			$ret['Msg'] = "";
		}
	
                $_SESSION['ch_user'] = getUser($_SESSION['user_id']);
                
                
	}catch(Exception $e){
		$ret['IsSuccess'] = false;
		$ret['Msg'] = $e->getMessage();
	}
	return $ret;
}

function getUser($userId){
	$ret = array();
	try{
		$sql = "SELECT * FROM ".TABLEPREFIX."_user WHERE user_id = ".$userId;
		$handle = mysql_query($sql);
		return mysql_fetch_array($handle);
	
	}catch(Exception $e){
		$ret['IsSuccess'] = false;
		$ret['Msg'] = $e->getMessage();
	}
	return $ret;
}

function validateWithdraw($key, $quant, $txtCaptcha){
    
    $code = trim($txtCaptcha);
    if($code != $_SESSION['captcha']){
        $ret['message'] = 'Enter security code correctly';
        $ret['IsSuccess'] = false;
        return $ret;    
    }
    
    $ret = array();
    $ret['IsSuccess'] = true;
    $user = getUser($_SESSION['user_id']);
    $password = $user['password'];
    $password=md5($password);
    $totalPoints = $user['total_points'];

    if($quant > $totalPoints){
        $ret['message'] = 'You dont have enough points';
        $ret['IsSuccess'] = false;
        return $ret;
    }
    if($key != $password){
        $ret['message'] = 'Wrong password';
        $ret['IsSuccess'] = false;
        return $ret;
    }
    $ret['totalPoints'] = $totalPoints;
    return $ret;
}

function doWithdraw($quant, $paypalId, $points){

    $ret = array();
    try{
        include('transaction_status.class.php');
		$db = new DBConnection();
		$db->getConnection();
        
        $txDate = date("d/m/Y : H:i:s", time());
        $txId = md5($_SESSION['user_id'].$txDate);
	
        $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')";
        
		if(mysql_query($sql)==false){
			$ret['IsSuccess'] = false;
			$ret['Msg'] = mysql_error();
		}else{
			$ret['IsSuccess'] = true;
			$ret['Msg'] = 'add success';
			$ret['Id'] = mysql_insert_id();
						
            $updateResult = updateUserPoints($points-$quant);
			
			return $updateResult;
		}

	}catch(Exception $e){
		$ret['IsSuccess'] = false;
		$ret['Msg'] = $e->getMessage();
	}
	return $ret;
}

 
 function getMessages(){
	$ret = array();
	$ids = "";
	$idsArray = array();
	$groupedMessages = array();
	
	try{
		$db = new DBConnection();
		$db->getConnection();
		$sql = "SELECT * FROM `frei_chat` WHERE `to` = ".$_SESSION['user_id']." OR `from` = ".$_SESSION['user_id']." ORDER BY `sent` DESC";
	
		$handle = mysql_query($sql);
		while ($message = mysql_fetch_object($handle)) {
			$to = $message->to;
			$from = $message->from;
			
			if($to != $_SESSION['user_id']){
				if(!in_array($to, $idsArray)){
					$idsArray[]=$to;
				}
				$groupedMessages[$to][] = $message;
			}else if($from != $_SESSION['user_id']){
				if(!in_array($from, $idsArray)){
					$idsArray[]=$from;
				}
				$groupedMessages[$from][] = $message;
			}	
		}
		
		$ret['groupedMessages'] = $groupedMessages;
		$idsSize = count($idsArray);
		for($i=0; $i<$idsSize; $i++){
			$id = $idsArray[$i];
			if($i == ($idsSize-1))
				$ids = $ids.$id."";
			else
				$ids = $ids.$id.",";
		}
		
		$userssql = "SELECT * FROM `nk_user` WHERE `user_id` in (".$ids.") ORDER BY FIELD(`user_id`,".$ids.")";
		//echo "\n".$userssql."\n";
		$userhandle = mysql_query($userssql);
		while ($user = mysql_fetch_object($userhandle)) {
			//echo "\n".'AA'."\n";
			$ret['users'][] = $user;
		}
		$ret['IsSuccess'] = true;
	}catch(Exception $e){
		$ret['error'] = $e->getMessage();
	}
	return $ret;
}
 
function bigintval($value) {
	$value = trim($value);
	if (ctype_digit($value)) {
		return $value;
	}
	$value = preg_replace("/[^0-9](.*)$/", '', $value);
	if (ctype_digit($value)) {
		return $value;
	}
	return 0;
}
function sendMessage($message, $toid, $fromname, $gmttime, $toname=""){

	$ret = array();
    try{
		if($toname == ""){
			$user = getUser($toid);
			$toname = $user['username'];
		}
		$gmttime = bigintval($gmttime);
		$db = new DBConnection();
		$db->getConnection();
		$time = time() . str_replace(" ", "", microtime());
		
		$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)";
		if(mysql_query($sql)==false){
			$ret['IsSuccess'] = false;
			$ret['Msg'] = mysql_error();
		}else{
			$ret['IsSuccess'] = true;
			$sql = "SELECT * FROM `frei_chat` where `id` = ".mysql_insert_id();
			$handle = mysql_query($sql);
			$msg = mysql_fetch_object($handle);
			
			$ret['msg'] = $msg;
			//$ret['sent'] = mysql_insert_id();
			insertNotifications($toid, NotificationTypes::$MESSAGE,$_SESSION['user_id'] , 'New Message', ($fromname." sent you a new message"), ($fromname." sent you a new message"), "", "/messages.php");
		}
	
	}catch(Exception $e){
		$ret['IsSuccess'] = false;
		$ret['Msg'] = $e->getMessage();
	}
	
	return $ret;
}

function pointsTransfer($id,$quant){
    $ret = array();
	$user = getUser($id);	
	$ret['status'] = getUserStatus($user);
    return $ret;
}

if(isset($_GET['method']) || isset($_POST['method'])){
	header('Content-type:text/javascript;charset=UTF-8');

	$method = getPostOrGetParam("method");

	switch ($method) {
		case "addAsFriend":
			$ret = addAsFriend(getPostOrGetParam('userid'));	
			break;
		case "changeOnlineStatus":
			$ret = changeOnlineStatus(getPostOrGetParam('status'));	
			break;
		case "addPhonoSessionId":
			$ret = addPhonoSessionId(getPostOrGetParam('sessionId'));
			break;
		case "getUser":
			$ret = getUser(getPostOrGetParam('userId'));
			break;
		case "removeFriend":
			$ret = removeFromFriendList(getPostOrGetParam('userid'));
			break;
        case "validateWithdraw":
            
    		$ret = validateWithdraw(getPostOrGetParam('key'), getPostOrGetParam('quant'), getPostOrGetParam('txtCaptcha'));
			break;
        case "doWithdraw":
        	$ret = doWithdraw(getPostOrGetParam('quant'), getPostOrGetParam('paypalId'), getPostOrGetParam('totalpoints'));
			break;  
		case "doTest":
        	$ret = doTest(getPostOrGetParam('id'));
			break;  
        case "sendMessage":
        	$ret = sendMessage(getPostOrGetParam('message'),getPostOrGetParam('toid'),getPostOrGetParam('fromname'),getPostOrGetParam('gmttime'),getPostOrGetParam('toname'));
			break;    
        case "getMessages":
        	$ret = getMessages();
			break;   
		case "pointsTransfer":
        	$ret = pointsTransfer(getPostOrGetParam('userid'),getPostOrGetParam('quant'));
			break;  
		case "getTime":
			$ret = array();
			$ret['DATE'] = date('l jS \of F Y h:i:s A');
			break;
	}
	echo json_encode($ret);
}
?>
Back to Top