· 9 years ago · Jun 21, 2017, 10:06 PM
1<?php
2/*
3 Designed by xDark and HellsCoder (C)
4 Copyright HellChat 2017.
5*/
6
7session_start();
8
9
10
11//ПЕРЕФЕРИЯ
12$server = "localhost";
13$user = "root";
14$pass = "vertrigo";
15$db = "net";
16$link = mysqli_connect($server, $user, $pass, $db);
17
18$emdir = "../img/emoj"; //ДИРЕКТОРИЯ С СМÐЙЛÐМИ
19
20
21$defrule['page-wrap'] = 'true'; //ПОСТЫ ÐРСТЕÐЕ
22
23
24date_default_timezone_set("Europe/Moscow");
25
26mysqli_query($link, "SET NAMES utf-8");
27mysqli_query($link, 'CREATE TABLE IF NOT EXISTS `users` (
28 `user_id` int(15) NOT NULL AUTO_INCREMENT,
29 `user_name` varchar(255) NOT NULL,
30 `user_password` varchar(255) NOT NULL,
31 `user_online` int(1) NOT NULL,
32 `user_date` varchar(255) NOT NULL,
33 `user_admin` varchar(255) NOT NULL,
34 `user_hash` varchar(255) NOT NULL,
35 `user_avatar` varchar(255) NOT NULL,
36 `user_status` text(5000) NOT NULL,
37 PRIMARY KEY (`user_id`)
38) ENGINE=MyISAM DEFAULT CHARSET=cp1251 AUTO_INCREMENT=0 ;');
39mysqli_set_charset($link, 'utf8');
40
41
42/*
43СТÐТУС МОЖЕТ ПРИÐИМÐТЬ ЗÐÐÐЧЕÐИЯ:
441 - Друг
452 - ПодпиÑчик
463 - Ðе обработан пользователем
47
48*/
49mysqli_query($link, 'CREATE TABLE IF NOT EXISTS `friends` (
50 `user_id_caller` int(15) NOT NULL,
51 `user_id_friend` int(15) NOT NULL,
52 `status` int(1) NOT NULL
53) ENGINE=MyISAM DEFAULT CHARSET=cp1251;');
54
55//КОРОÐЫ
56mysqli_query($link, 'CREATE TABLE IF NOT EXISTS `crows` (
57 `user_id_crowed` int(15) NOT NULL,
58 `user_hash_crower` varchar(255) NOT NULL
59) ENGINE=MyISAM DEFAULT CHARSET=cp1251;');
60
61//ПОСТЫ и ЛÐЙКИ К ÐИМ, РТÐК ЖЕ ÐОВОСТИ, ОБРÐБÐТЫВÐЮТСЯ ИЗ ÐТОЙ ТÐБЛИЦЫ
62mysqli_query($link, 'CREATE TABLE IF NOT EXISTS `posts` (
63 `post_id` int(15) NOT NULL AUTO_INCREMENT,
64 `user_id_hoster` int(15) NOT NULL,
65 `user_hash_poster` varchar(255) NOT NULL,
66 `time` varchar(15) NOT NULL,
67 `text` text(65535) NOT NULL,
68 PRIMARY KEY (`post_id`)
69) ENGINE=MyISAM DEFAULT CHARSET=cp1251 AUTO_INCREMENT=0;');
70
71// STATE: 1 - лайк, 2 - дизлайк
72mysqli_query($link, 'CREATE TABLE IF NOT EXISTS `likes` (
73 `user_hash_liker` varchar(255) NOT NULL,
74 `post_id` int(15) NOT NULL,
75 `state` varchar(15) NOT NULL
76) ENGINE=MyISAM DEFAULT CHARSET=cp1251;');
77
78
79// <<ГÐЛОЧКИ>>
80
81mysqli_query($link, 'CREATE TABLE IF NOT EXISTS `verify` (
82 `user_id` varchar(255) NOT NULL
83) ENGINE=MyISAM DEFAULT CHARSET=cp1251;');
84
85
86//ПРÐВИЛÐ
87mysqli_query($link, 'CREATE TABLE IF NOT EXISTS `rule` (
88 `user_id` int(15) NOT NULL,
89 `user_hash` varchar(255) NOT NULL,
90 `rule` varchar(255) NOT NULL,
91 `value` varchar(255) NOT NULL
92) ENGINE=MyISAM DEFAULT CHARSET=cp1251;');
93
94
95//ГЛОБÐЛЬÐЫЙ МОДУЛЬ - СООБЩЕÐИЯ READ: 1 - прочитан, 0 - не прочитан
96mysqli_query($link, 'CREATE TABLE IF NOT EXISTS `mails` (
97 `user_hash_from` int(15) NOT NULL,
98 `user_id_to` int(15) NOT NULL,
99 `read` int(1) NOT NULL,
100 `text` text(65535) NOT NULL
101) ENGINE=MyISAM DEFAULT CHARSET=cp1251;');
102
103
104
105function newRules($hash){
106 global $defrule;
107 foreach($defrule as $key => $value){
108 setRule($hash, $key, $value);
109 }
110}
111
112
113//ПРÐВИЛÐ
114
115
116
117
118function getOfflineColor(){
119 return "#767676";
120}
121
122function getOnlineColor(){
123 return "#43cb5d";
124}
125
126function getFriendBoxIMGSize(){
127 return 94;
128}
129
130function isAdmin($hash){
131 global $link;
132 $query = mysqli_query($link, "SELECT * FROM `users` WHERE `user_hash` = '$hash'");
133 $data = mysqli_fetch_array($query);
134 $admin = $data['user_admin'];
135 updateActive($hash);
136 if($admin == 1){
137 return true;
138 }else{
139 return false;
140 }
141}
142
143/*
144POST Request Handelrs
145*/
146
147if(!empty($_POST)){
148 $data = $_POST;
149 if(!empty($data['act'])){
150 switch($data['act']){
151 case "crow":
152 $hash = $data['hash'];
153 $crowed = $data['crowed'];
154 setCrow($hash, $crowed);
155 break;
156 case "friend":
157 $hash = $data['hash'];
158 $friend = $data['id'];
159 callFriend($hash, $friend);
160 break;
161 case "delcrow":
162 $hash = $data['hash'];
163 $crowed = $data['crowed'];
164 delCrow($hash, $crowed);
165 break;
166 case "delfriend":
167 $hash = $data['hash'];
168 $id = $data['id'];
169 removeFriend($hash, $id);
170 break;
171 case "tosub":
172 $hash = $data['hash'];
173 $id = $data['sub'];
174 toScriber($hash, $id);
175 break;
176 case "accept":
177 $hash = $data['hash'];
178 $id = $data['acc'];
179 acceptFriend($hash, $id);
180 break;
181 case "delacc":
182 $hash = $data['hash'];
183 $id = $data['userid'];
184 delAccount($hash, $id);
185 break;
186 case "post":
187 $hash = $data['hash'];
188 $hoster = $data['hoster'];
189 $text = $data['text'];
190 postNew($hash, $hoster, $text);
191 break;
192 case "delpost":
193 $hash = $data['hash'];
194 $postId = $data['pid'];
195 deletePost($postId, $hash);
196 break;
197 case "lcls":
198 $hash = $data['hash'];
199 $postId = $data['pid'];
200 clearLikes($hash, $postId);
201 break;
202 case "like":
203 $hash = $data['hash'];
204 $postId = $data['pid'];
205 like($hash, $postId);
206 break;
207 case "dislike":
208 $hash = $data['hash'];
209 $postId = $data['pid'];
210 dislike($hash, $postId);
211 break;
212 case "rule":
213 $hash = $data['hash'];
214 $rule = $data['rule'];
215 $value = $data['value'];
216 setRule($hash, $rule, $value);
217 break;
218 case "refdel": // не трогай Ñука !1111!!111
219 $del = $data['del'];
220 $hash = $data['hash'];
221 if($del == 1){
222 deleteAllPosts($hash);
223 }
224 if($del == 2){
225 delAccount($hash, getIdByHash($hash));
226 }
227 break;
228 case "status":
229 $hash = $data['hash'];
230 $text = $data['text'];
231 setStatus($hash, $text);
232 break;
233 case "change":
234 $hash = $data['hash'];
235 $password = $data['password'];
236 $new_pass = $data['new_pass'];
237 $r_pass = $data['r_pass'];
238 changePassword($hash, $password, $new_pass, $r_pass);
239 break;
240 case "verif":
241 $hash = $data['hash'];
242 $id = $data['id'];
243 verify($hash, $id);
244 break;
245 case "deverif":
246 $hash = $data['hash'];
247 $id = $data['id'];
248 toggleVerif($hash, $id);
249 break;
250 }
251 }
252}
253
254/*ÐМОЦИИ*/
255
256function toEmojParse($text){
257 global $emdir;
258 $f = glob("img/emoj/*.gif");
259 foreach($f as $file){
260 $info = pathinfo($file);
261 $filename = basename($file,'.'.$info['extension']);
262 $text = str_replace(":$filename:", '<img src="img/emoj/'.$filename.'.gif" height="20" width="20">', $text);
263 }
264 return $text;
265}
266
267
268/*ÐМОЦИИ*/
269
270/*Отправка модального ÑообщениÑ*/
271
272function sendMessage($title, $text){
273 echo "<script>
274 window.onload = function(){
275 MessageModule.show('$title', '$text');
276 return false;
277 }
278 </script>";
279}
280
281
282
283function reload(){
284 echo '<script>location.reload();</script>';
285}
286
287function setCss($objectID, $rule, $value){
288 echo '<script>
289 window.onload = function(){
290 $("#'.$objectID.'").css("'.$rule.'", "'.$value.'");
291 return false;
292 }
293 </script>';
294}
295
296function generateCode($length=6) {
297 $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPRQSTUVWXYZ0123456789";
298 $code = "";
299 $clen = strlen($chars) - 1;
300 while (strlen($code) < $length) {
301 $code .= $chars[mt_rand(0,$clen)];
302 }
303 return $code;
304}
305
306function isOnline($id){
307 global $link;
308 $id = mysqli_real_escape_string($link, $id);
309 $id = htmlspecialchars($id);
310 $query = mysqli_query($link, "SELECT * FROM `users` WHERE `user_id` = '$id'");
311 $data = mysqli_fetch_array($query);
312 $lasttime = $data['user_date'];
313 if($lasttime + 60 < time()){
314 setOnlineByID($id, 0);
315 return false;
316 }else{
317 return true;
318 }
319}
320
321function getLastTime($id){
322 global $link;
323 $id = mysqli_real_escape_string($link, $id);
324 $id = htmlspecialchars($id);
325 $query = mysqli_query($link, "SELECT * FROM `users` WHERE `user_id` = '$id'");
326 $data = mysqli_fetch_array($query);
327 $lasttime = $data['user_date'];
328 return $lasttime;
329}
330
331function updateActive($hash){
332 global $link;
333 $hash = mysqli_real_escape_string($link, $hash);
334 $hash = htmlspecialchars($hash);
335 $time = time();
336 mysqli_query($link, "UPDATE `users` SET `user_date` = '$time' WHERE `user_hash` = '$hash'");
337}
338
339function setOnline($hash, $status){
340 global $link;
341 $hash = mysqli_real_escape_string($link, $hash);
342 $hash = htmlspecialchars($hash);
343 mysqli_query($link, "UPDATE `users` SET `user_online` = '$status' WHERE `user_hash` = '$hash'");
344}
345
346function setOnlineByID($id, $status){
347 global $link;
348 $id = mysqli_real_escape_string($link, $id);
349 $id = htmlspecialchars($id);
350 mysqli_query($link, "UPDATE `users` SET `user_online` = '$status' WHERE `user_id` = '$id'");
351}
352
353
354function registration($user, $password){
355 global $link;
356 $user = mysqli_real_escape_string($link, $user);
357 $user = htmlspecialchars($user);
358 $hash = generateCode(56);
359 $password = md5(md5(md5($password)));
360 $query = mysqli_query($link, "SELECT * FROM `users` WHERE `user_name` = '$user'");
361 if(mysqli_num_rows($query) != 0){
362 return false;
363 }
364 $time = time();
365 $response = "INSERT INTO `users` (user_name, user_password, user_online, user_date, user_admin, user_hash, user_avatar, user_status) VALUES ('$user', '$password', '0', '$time', '0', '$hash', 'default.jpg', 'no')";
366 mysqli_query($link, $response);
367 newRules($hash);
368 return true;
369}
370
371function delAccount($hash, $id){
372 global $link;
373 $id = mysqli_real_escape_string($link, $id);
374 $id = htmlspecialchars($id);
375 $hash = htmlspecialchars($hash);
376 $hash = mysqli_real_escape_string($link, $hash);
377 updateActive($hash);
378 if(getIdByHash($hash) != $id and !isAdmin($hash)){
379 return false;
380 }else{
381 $hQuery = "SELECT * FROM `users` WHERE `user_id` = '$id'";
382 $result = mysqli_query($link, $uQuery);
383 $arrHash = mysqli_fetch_array($result);
384 $uHash = $arrHash['user_hash'];
385 $query = "DELETE FROM `users` WHERE `user_id` = '$id'";
386 $queryPosts = "DELETE FROM `posts` WHERE `user_hash_poster` = '$uHash'";
387 $queryRule = "DELETE FROM `rule` WHERE `user_id` = '$id'";
388 $queryLikes = "DELETE FROM `likes` WHERE `user_hash_liker` = '$uHash'";
389 $queryCrows = "DELETE FROM `crows` WHERE `user_id_crowed` = '$id' OR `user_hash_crower` = '$uHash'";
390 $queryFriends = "DELETE FROM `friends` WHERE `user_id_caller` = '$id' OR `user_id_friend` = '$id'";
391 mysqli_query($link, $query);
392 mysqli_query($link, $queryPosts);
393 mysqli_query($link, $queryLikes);
394 mysqli_query($link, $queryCrows);
395 mysqli_query($link, $queryFriends);
396 mysqli_query($link, $queryRule);
397 }
398}
399
400function changePassword($hash, $password, $new_pass, $r_pass){
401 global $link;
402 $hash = htmlspecialchars($hash);
403 $hash = mysqli_real_escape_string($link, $hash);
404 $password = htmlspecialchars($password);
405 $password = mysqli_real_escape_string($link, $password);
406 $new_pass = htmlspecialchars($new_pass);
407 $new_pass = mysqli_real_escape_string($link, $new_pass);
408 $arr = array();
409 if(getPwdByHash($hash) != md5(md5(md5($password)))){
410 $arr['msg'][] = "Старый пароль не Ñовпадает!";
411 echo json_encode($arr);
412 return;
413 }else if($new_pass != $r_pass){
414 $arr['msg'][] = "Ðеверный повтор паролÑ!";
415 echo json_encode($arr);
416 return;
417 }else{
418 $new_pass = md5(md5(md5($new_pass)));
419 $query = "UPDATE `users` SET `user_password` = '$new_pass' WHERE `user_hash` = '$hash'";
420 mysqli_query($link, $query);
421 $arr['msg'][] = "Ваш пароль изменен, вы можете входить Ñ Ð½Ð¾Ð²Ñ‹Ð¼!";
422 echo json_encode($arr);
423 return;
424 }
425}
426
427function auth($user, $password){
428 global $link;
429 $user = mysqli_real_escape_string($link, $user);
430 $user = htmlspecialchars($user);
431 $password = md5(md5(md5($password)));
432 $query = mysqli_query($link, "SELECT * FROM `users` WHERE `user_name` = '$user'");
433 $data = mysqli_fetch_array($query);
434 if($data['user_password'] != $password){
435 return false;
436 }
437 $hash = $data['user_hash'];
438 updateActive($hash);
439 setcookie("hash", $hash, time()+3600);
440 return true;
441}
442
443function isRegistred($id){
444 global $link;
445 $id = htmlspecialchars($id);
446 $id = mysqli_real_escape_string($link, $id);
447 $query = "SELECT * FROM `users` WHERE `user_id` = '$id'";
448 $result = mysqli_query($link, $query);
449 if(mysqli_num_rows($result) != 0){
450 return true;
451 }
452 return false;
453}
454
455function isRegHash($hash){
456 global $link;
457 $hash = htmlspecialchars($hash);
458 $hash = mysqli_real_escape_string($link, $hash);
459 $query = "SELECT * FROM `users` WHERE `user_hash` = '$hash'";
460 $result = mysqli_query($link, $query);
461 if(mysqli_num_rows($result) != 0){
462 return true;
463 }
464 return false;
465}
466
467function getAvatar($hash){
468 global $link;
469 $query = mysqli_query($link, "SELECT * FROM `users` WHERE `user_hash` = '$hash'");
470 $data = mysqli_fetch_array($query);
471 $avatar = $data['user_avatar'];
472 updateActive($hash);
473 return $avatar;
474}
475
476function setAvatar($token, $file){
477 global $link;
478 $response = "UPDATE `users` SET `user_avatar` = '$file' WHERE `user_hash` = '$token'";
479 updateActive($token);
480 mysqli_query($link, $response);
481}
482
483function getName($hash){
484 global $link;
485 $query = mysqli_query($link, "SELECT * FROM `users` WHERE `user_hash` = '$hash'");
486 $data = mysqli_fetch_array($query);
487 $name = $data['user_name'];
488 updateActive($hash);
489 return $name;
490}
491
492function getNameById($id){
493 global $link;
494 $query = mysqli_query($link, "SELECT * FROM `users` WHERE `user_id` = '$id'");
495 $data = mysqli_fetch_array($query);
496 $name = $data['user_name'];
497 return $name;
498}
499
500function getAvatarById($id){
501 global $link;
502 $query = mysqli_query($link, "SELECT * FROM `users` WHERE `user_id` = '$id'");
503 $data = mysqli_fetch_array($query);
504 $avatar = $data['user_avatar'];
505 return $avatar;
506}
507
508function getIdByHash($hash){
509 global $link;
510 $query = mysqli_query($link, "SELECT * FROM `users` WHERE `user_hash` = '$hash'");
511 $data = mysqli_fetch_array($query);
512 $id = $data['user_id'];
513 updateActive($hash);
514 return $id;
515}
516
517function getPwdByHash($hash){
518 global $link;
519 $query = mysqli_query($link, "SELECT * FROM `users` WHERE `user_hash` = '$hash'");
520 $data = mysqli_fetch_array($query);
521 $id = $data['user_password'];
522 updateActive($hash);
523 return $id;
524}
525
526function getStatusById($id){
527 global $link;
528 $query = mysqli_query($link, "SELECT * FROM `users` WHERE `user_id` = '$id'");
529 $data = mysqli_fetch_array($query);
530 $status = $data['user_status'];
531 return $status;
532}
533
534
535
536function getStatus($hash){
537 global $link;
538 $query = mysqli_query($link, "SELECT * FROM `users` WHERE `user_hash` = '$hash'");
539 $data = mysqli_fetch_array($query);
540 $status = $data['user_status'];
541 updateActive($hash);
542 return $status;
543}
544
545function setStatus($hash, $status){
546 global $link;
547 $status = mysqli_real_escape_string($link, $status);
548 $status = htmlspecialchars($status);
549 $query = "UPDATE `users` SET `user_status` = '$status' WHERE `user_hash` = '$hash'";
550 updateActive($hash);
551 mysqli_query($link, $query);
552}
553
554
555/*ДРУЗЬЯ, ПОДПИСЧИКИ и ВСЕ ЧТО С ÐИМИ СВЯЗÐÐО*/
556
557function getFriends($id){
558 global $link;
559 $id = htmlspecialchars($id);
560 $id = mysqli_real_escape_string($link, $id);
561 $query = "SELECT * FROM `friends` WHERE `user_id_caller` = '$id' AND `status`='1'
562 OR `user_id_friend` = '$id' AND `status`='1'";
563 $result = mysqli_query($link, $query);
564 $friends = mysqli_num_rows($result);
565 return $friends;
566}
567
568function callFriend($token, $friend){
569 global $link;
570 $id = getIdByHash($token);
571 $friend = mysqli_real_escape_string($link, $friend);
572 $friend = htmlspecialchars($friend);
573 $query = "INSERT INTO `friends` (user_id_caller, user_id_friend, status) VALUES ('$id', '$friend', '3')";
574 updateActive($token);
575 mysqli_query($link, $query);
576}
577
578function getStatusFriend($token, $friend){
579 global $link;
580 $id = getIdByHash($token);
581 $friend = mysqli_real_escape_string($link, $friend);
582 $friend = htmlspecialchars($friend);
583 $query = "SELECT * FROM `friends` WHERE `user_id_caller` = '$id' AND `user_id_friend` = '$friend' OR
584 `user_id_caller` = '$friend' AND `user_id_friend` = '$id'";
585 $result = mysqli_query($link, $query);
586 $data = mysqli_fetch_array($result);
587 updateActive($token);
588 return $data['status'];
589}
590
591function toScriber($token, $friend){
592 global $link;
593 $id = getIdByHash($token);
594 $friend = mysqli_real_escape_string($link, $friend);
595 $friend = htmlspecialchars($friend);
596 $query = "UPDATE `friends` SET `status` = '2' WHERE `user_id_caller` = '$friend' AND `user_id_friend` = '$id'";
597 updateActive($token);
598 mysqli_query($link, $query);
599}
600
601function acceptFriend($token, $friend){
602 global $link;
603 $id = getIdByHash($token);
604 $friend = mysqli_real_escape_string($link, $friend);
605 $friend = htmlspecialchars($friend);
606 $query = "UPDATE `friends` SET `status` = '1' WHERE `user_id_caller` = '$friend' AND `user_id_friend` = '$id'";
607 updateActive($token);
608 mysqli_query($link, $query);
609}
610
611function getSunbscribers($id){
612 global $link;
613 $id = htmlspecialchars($id);
614 $id = mysqli_real_escape_string($link, $id);
615 $query = "SELECT * FROM `friends` WHERE `user_id_friend` = '$id' AND `status`='2'";
616 $result = mysqli_query($link, $query);
617 $friends = mysqli_num_rows($result);
618 return $friends;
619}
620
621function getNotHandle($id){
622 global $link;
623 $id = htmlspecialchars($id);
624 $id = mysqli_real_escape_string($link, $id);
625 $query = "SELECT * FROM `friends` WHERE `user_id_friend` = '$id' AND `status`='3'";
626 $result = mysqli_query($link, $query);
627 $friends = mysqli_num_rows($result);
628 return $friends;
629}
630
631function getScr($id){
632 global $link;
633 $id = htmlspecialchars($id);
634 $id = mysqli_real_escape_string($link, $id);
635 $query = "SELECT * FROM `friends` WHERE `user_id_caller` = '$id' AND `status`='2' OR `user_id_caller` = '$id' AND `status` = '3'";
636 $result = mysqli_query($link, $query);
637 $friends = mysqli_num_rows($result);
638 return $friends;
639}
640
641//ТРУ ЕСЛИ TOKEN ПОДПИСÐÐ ÐÐ ID
642function isSubscr($token, $id){
643 global $link;
644 $myId = getIdByHash($token);
645 $id = htmlspecialchars($id);
646 $id = mysqli_real_escape_string($link, $id);
647 $query = "SELECT * FROM `friends` WHERE `user_id_friend` = '$id' AND `user_id_caller` = '$myId' AND `status` = '2' OR
648 `user_id_friend` = '$id' AND `user_id_caller` = '$myId' AND `status` = '3'";
649 $result = mysqli_query($link, $query);
650 $st = mysqli_num_rows($result);
651 updateActive($token);
652 if($st != 0){
653 return true;
654 }
655 return false;
656}
657
658function removeFriend($token, $friend){
659 global $link;
660 $id = getIdByHash($token);
661 $friend = mysqli_real_escape_string($link, $friend);
662 $friend = htmlspecialchars($friend);
663 $query = "DELETE FROM `friends` WHERE `user_id_caller` = '$id' AND `user_id_friend` = '$friend' OR
664 `user_id_caller` = '$friend' AND `user_id_friend` = '$id'
665 ";
666 updateActive($token);
667 mysqli_query($link, $query);
668}
669
670/*ДРУЗЬЯ*/
671
672/*КОРОÐЫ*/
673
674function isCrowed($hash, $crowed){
675 global $link;
676 $hash = htmlspecialchars($hash);
677 $hash = mysqli_real_escape_string($link, $hash);
678 if(isRegHash($hash)){
679 $crowed = htmlspecialchars($crowed);
680 $crowed = mysqli_real_escape_string($link, $crowed);
681 $query = "SELECT * FROM `crows` WHERE `user_hash_crower` = '$hash' AND `user_id_crowed` = '$crowed'";
682 $result = mysqli_query($link, $query);
683 updateActive($hash);
684 if(mysqli_num_rows($result) != 0){
685 return true;
686 }
687 return false;
688 }else{
689 return false;
690 }
691}
692
693function setCrow($hash, $crowed){
694 global $link;
695 $hash = htmlspecialchars($hash);
696 $hash = mysqli_real_escape_string($link, $hash);
697 if(isRegHash($hash)){
698 $crowed = htmlspecialchars($crowed);
699 $crowed = mysqli_real_escape_string($link, $crowed);
700 updateActive($hash);
701 if(isCrowed($hash, $crowed)){
702 return;
703 }
704 $query = "INSERT INTO `crows` (user_id_crowed, user_hash_crower) VALUES ('$crowed', '$hash')";
705 mysqli_query($link, $query);
706 }else{
707 return false;
708 }
709}
710
711
712function delCrow($hash, $crowed){
713 global $link;
714 $hash = htmlspecialchars($hash);
715 $hash = mysqli_real_escape_string($link, $hash);
716 if(isRegHash($hash)){
717 $crowed = htmlspecialchars($crowed);
718 $crowed = mysqli_real_escape_string($link, $crowed);
719 $query = "DELETE FROM `crows` WHERE `user_hash_crower` = '$hash' AND `user_id_crowed` = '$crowed'";
720 mysqli_query($link, $query);
721 updateActive($hash);
722 }else{
723 return false;
724 }
725}
726
727function getCrows($id){
728 global $link;
729 $id = htmlspecialchars($id);
730 $id = mysqli_real_escape_string($link, $id);
731 $query = "SELECT * FROM `crows` WHERE `user_id_crowed` = '$id'";
732 $result = mysqli_query($link, $query);
733 return mysqli_num_rows($result);
734}
735
736/*КОРОÐЫ*/
737
738/*ПОСТЫ ÐРСТРÐÐИЦЕ*/
739
740function postNew($hash, $hoster, $text){
741 global $link;
742 $hoster = htmlspecialchars($hoster);
743 $hoster = mysqli_real_escape_string($link, $hoster);
744 $text = htmlspecialchars($text);
745 $text = mysqli_real_escape_string($link, $text);
746 $hash = htmlspecialchars($hash);
747 $hash = mysqli_real_escape_string($link, $hash);
748 if(isRegHash($hash)){
749 $time = time();
750 updateActive($hash);
751 $query = "INSERT INTO `posts` (user_id_hoster, user_hash_poster, time, text) VALUES ('$hoster', '$hash', '$time', '$text')";
752 if(getStatusFriend($hash, $hoster) != 1 and getIdByHash($hash) != $hoster){
753 return false;
754 }else{
755 mysqli_query($link, $query);
756 return true;
757 }
758 }else{
759 return false;
760 }
761}
762
763function isAccPost($hash, $hoster){
764 global $link;
765 updateActive($hash);
766 $hash = htmlspecialchars($hash);
767 $hash = mysqli_real_escape_string($link, $hash);
768 if(getStatusFriend($hash, $hoster) != 1 or !getRuleById($hoster, "page-wrap")){
769 return false;
770 }else{
771 return true;
772 }
773}
774
775function like($hash, $idPost){
776 global $link;
777 $idPost = htmlspecialchars($idPost);
778 updateActive($hash);
779 $idPost = mysqli_real_escape_string($link, $idPost);
780 $hash = htmlspecialchars($hash);
781 $hash = mysqli_real_escape_string($link, $hash);
782 if(isLiked($hash, $idPost) or isDisliked($hash, $idPost)){
783 return false;
784 }else{
785 $query = "INSERT INTO `likes` (user_hash_liker, post_id, state) VALUES ('$hash', '$idPost', '1')";
786 mysqli_query($link, $query);
787 return true;
788 }
789
790}
791
792function dislike($hash, $idPost){
793 global $link;
794 $idPost = htmlspecialchars($idPost);
795 $idPost = mysqli_real_escape_string($link, $idPost);
796 $hash = htmlspecialchars($hash);
797 $hash = mysqli_real_escape_string($link, $hash);
798 updateActive($hash);
799 if(isLiked($hash, $idPost) or isDisliked($hash, $idPost)){
800 return false;
801 }else{
802 $query = "INSERT INTO `likes` (user_hash_liker, post_id, state) VALUES ('$hash', '$idPost', '2')";
803 mysqli_query($link, $query);
804 return true;
805 }
806
807}
808
809function clearLikes($hash, $idPost){
810 global $link;
811 $idPost = htmlspecialchars($idPost);
812 $idPost = mysqli_real_escape_string($link, $idPost);
813 $hash = htmlspecialchars($hash);
814 $hash = mysqli_real_escape_string($link, $hash);
815 updateActive($hash);
816 $query = "DELETE FROM `likes` WHERE `post_id` = '$idPost' AND `user_hash_liker` = '$hash'";
817 mysqli_query($link, $query);
818 return true;
819}
820
821function isLiked($hash, $idPost){
822 global $link;
823 $idPost = htmlspecialchars($idPost);
824 $idPost = mysqli_real_escape_string($link, $idPost);
825 $hash = htmlspecialchars($hash);
826 $hash = mysqli_real_escape_string($link, $hash);
827 $query = "SELECT * FROM `likes` WHERE `post_id` = '$idPost' AND `user_hash_liker` = '$hash' AND `state` = '1'";
828 $result = mysqli_query($link, $query);
829 updateActive($hash);
830 if(mysqli_num_rows($result) != 0){
831 return true;
832 }else{
833 return false;
834 }
835}
836
837function isDisliked($hash, $idPost){
838 global $link;
839 $idPost = htmlspecialchars($idPost);
840 $idPost = mysqli_real_escape_string($link, $idPost);
841 $hash = htmlspecialchars($hash);
842 $hash = mysqli_real_escape_string($link, $hash);
843 $query = "SELECT * FROM `likes` WHERE `post_id` = '$idPost' AND `user_hash_liker` = '$hash' AND `state` = '2'";
844 $result = mysqli_query($link, $query);
845 updateActive($hash);
846 if(mysqli_num_rows($result) != 0){
847 return true;
848 }else{
849 return false;
850 }
851}
852
853function getLikes($postId){
854 global $link;
855 $idPost = htmlspecialchars($postId);
856 $idPost = mysqli_real_escape_string($link, $idPost);
857 $query = "SELECT * FROM `likes` WHERE `post_id` = '$idPost' AND state = '1'";
858 $result = mysqli_query($link, $query);
859 $count = mysqli_num_rows($result);
860 return $count;
861}
862
863function getDislikes($postId){
864 global $link;
865 $idPost = htmlspecialchars($postId);
866 $idPost = mysqli_real_escape_string($link, $idPost);
867 $query = "SELECT * FROM `likes` WHERE `post_id` = '$idPost' AND state = '2'";
868 $result = mysqli_query($link, $query);
869 $count = mysqli_num_rows($result);
870 return $count;
871}
872
873function deletePost($postId, $hash){
874 global $link;
875 $idPost = htmlspecialchars($postId);
876 $idPost = mysqli_real_escape_string($link, $idPost);
877 $hash = htmlspecialchars($hash);
878 $hash = mysqli_real_escape_string($link, $hash);
879 updateActive($hash);
880 $query = "SELECT * FROM `posts` WHERE `post_id` = '$idPost'";
881 $result = mysqli_query($link, $query);
882 $array = mysqli_fetch_array($result);
883 if($array['user_hash_poster'] == $hash or $array['user_id_hoster'] == getIdByHash($hash)){
884 $toDel = "DELETE FROM `posts` WHERE `post_id` = '$idPost'";
885 $toLikes = "DELETE FROM `likes` WHERE `post_id` = '$idPost'";
886 mysqli_query($link, $toDel);
887 mysqli_query($link, $toLikes);
888 return true;
889 }else{
890 return false;
891 }
892}
893
894function isDel($postId, $hash){
895 global $link;
896 $idPost = htmlspecialchars($postId);
897 $idPost = mysqli_real_escape_string($link, $idPost);
898 $hash = htmlspecialchars($hash);
899 $hash = mysqli_real_escape_string($link, $hash);
900 updateActive($hash);
901 $query = "SELECT * FROM `posts` WHERE `post_id` = '$idPost'";
902 $result = mysqli_query($link, $query);
903 $array = mysqli_fetch_array($result);
904 if($array['user_hash_poster'] == $hash or $array['user_id_hoster'] == getIdByHash($hash)){
905 return true;
906 }else{
907 return false;
908 }
909}
910
911function deleteAllPosts($hash){
912 global $link;
913 $hash = htmlspecialchars($hash);
914 $hash = mysqli_real_escape_string($link, $hash);
915 updateActive($hash);
916 $id = getIdByHash($hash);
917 $query = "DELETE FROM `posts` WHERE `user_id_hoster` = '$id'";
918 mysqli_query($link, $query);
919}
920/*ПОСТЫ ÐРСТРÐÐИЦЕ*/
921
922
923/*ПРÐВИЛРПОЛЬЗОВÐТЕЛЕЙ*/
924
925function setRule($hash, $rule, $value){
926 global $link;
927 $hash = htmlspecialchars($hash);
928 $hash = mysqli_real_escape_string($link, $hash);
929 $rule = htmlspecialchars($rule);
930 $rule = mysqli_real_escape_string($link, $rule);
931 $value = htmlspecialchars($value);
932 $value = mysqli_real_escape_string($link, $value);
933 $id = getIdByHash($hash);
934 if(isRegHash($hash)){
935 if($value == 'true'){
936 $query = "INSERT INTO `rule` (user_id, user_hash, rule, value) VALUES ('$id', '$hash', '$rule', 'yes')";
937 }else{
938 $query = "INSERT INTO `rule` (user_id, user_hash, rule, value) VALUES ('$id', '$hash', '$rule', 'no')";
939 }
940 $queryToDelOldRule = "DELETE FROM `rule` WHERE `user_hash` = '$hash' AND `rule` = '$rule'";
941 mysqli_query($link, $queryToDelOldRule);
942 mysqli_query($link, $query);
943 return true;
944 }else{
945 return false;
946 }
947}
948
949function getRule($hash, $rule){
950 global $link;
951 $hash = htmlspecialchars($hash);
952 $hash = mysqli_real_escape_string($link, $hash);
953 $rule = htmlspecialchars($rule);
954 $rule = mysqli_real_escape_string($link, $rule);
955 $query = "SELECT * FROM `rule` WHERE `user_hash` = '$hash' AND `rule` = '$rule'";
956 updateActive($hash);
957 $result = mysqli_query($link, $query);
958 if(mysqli_num_rows($result) == 0){
959 return false;
960 }else{
961 $array = mysqli_fetch_array($result);
962 if($array['value'] == 'yes'){
963 return true;
964 }else{
965 return false;
966 }
967 }
968}
969
970function getRuleById($id, $rule){
971 global $link;
972 $id = htmlspecialchars($id);
973 $id = mysqli_real_escape_string($link, $id);
974 $rule = htmlspecialchars($rule);
975 $rule = mysqli_real_escape_string($link, $rule);
976 $query = "SELECT * FROM `rule` WHERE `user_id` = '$id' AND `rule` = '$rule'";
977 $result = mysqli_query($link, $query);
978 if(mysqli_num_rows($result) == 0){
979 return false;
980 }else{
981 $array = mysqli_fetch_array($result);
982 if($array['value'] == 'yes'){
983 return true;
984 }else{
985 return false;
986 }
987 }
988}
989
990/*ПРÐВИЛРПОЛЬЗОВÐТЕЛЕЙ*/
991
992/*КОРОÐКИ И ПОДТВЕРЖДЕÐÐЫЕ СТРÐÐИЦЫ*/
993
994function verify($hash, $verif){
995 global $link;
996 $verif = htmlspecialchars($verif);
997 $verif = mysqli_real_escape_string($link, $verif);
998 $hash = htmlspecialchars($hash);
999 $hash = mysqli_real_escape_string($link, $hash);
1000 if(!isAdmin($hash)){
1001 return false;
1002 }else{
1003 $query = "INSERT INTO `verify` (user_id) VALUES ('$verif')";
1004 mysqli_query($link, $query);
1005 }
1006}
1007
1008function isVerifid($verif){
1009 global $link;
1010 $verif = htmlspecialchars($verif);
1011 $verif = mysqli_real_escape_string($link, $verif);
1012 $query = "SELECT * FROM `verify` WHERE `user_id` = '$verif'";
1013 $result = mysqli_query($link, $query);
1014 if(mysqli_num_rows($result) != 0){
1015 return true;
1016 }else{
1017 return false;
1018 }
1019}
1020
1021function toggleVerif($hash, $verif){
1022 global $link;
1023 $verif = htmlspecialchars($verif);
1024 $verif = mysqli_real_escape_string($link, $verif);
1025 $hash = htmlspecialchars($hash);
1026 $hash = mysqli_real_escape_string($link, $hash);
1027 if(!isAdmin($hash)){
1028 return false;
1029 }else{
1030 $query = "DELETE FROM `verify` WHERE `user_id` = '$verif'";
1031 mysqli_query($link, $query);
1032 }
1033}
1034
1035/*КОРОÐКИ И ПОДТВЕРЖДЕÐÐЫЕ СТРÐÐИЦЫ*/
1036
1037/*СООБЩЕÐИЯ, ДИÐЛОГИ МЕЖДУ ПОЛЬЗОВÐТЕЛЯМИ*/
1038function sendMail($from, $to, $mail){
1039 global $link;
1040 $from = mysqli_real_escape_string($link, $from);
1041 $to = mysqli_real_escape_string($link, $to);
1042 $from = htmlspecialchars($from);
1043 $to = htmlspecialchars($to);
1044 $mail = mysqli_real_escape_string($link, $query);
1045 $mail = htmlspecialchars($mail);
1046 $read = 0;
1047 if(isRegHash($from)){
1048 $query = "INSERT INTO `mails` (user_hash_from, user_id_to, read, text) VALUES ('$from', '$to', '$read', '$mail')";
1049 mysqli_query($link, $query);
1050 return true;
1051 }else{
1052 return false;
1053 }
1054}
1055
1056?>