functions.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. // SSB - Simple Social Board
  3. // (C) Chris Dorman, 2012 - 2020
  4. // License: CC-BY-NC-SA version 3.0
  5. // http://github.com/Pentium44/SSB
  6. // get filesize for uploaded files
  7. session_start();
  8. function tomb($size, $precision = 2)
  9. {
  10. $base = log($size) / log(1024);
  11. $suffixes = array('', 'KB', 'MB', 'GB', 'TB');
  12. return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
  13. }
  14. function getRandString($n) {
  15. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_';
  16. $randomString = '';
  17. for ($i = 0; $i < $n; $i++) {
  18. $index = rand(0, strlen($characters) - 1);
  19. $randomString .= $characters[$index];
  20. }
  21. return $randomString;
  22. }
  23. function searchForUserTag($str) {
  24. if(preg_match("/\s*@(.+?)\s/", $str, $matches)) {
  25. return $matches["0"];
  26. } else {
  27. return false;
  28. }
  29. }
  30. function checkLogin() {
  31. $username = $_SESSION['ssb-user'];
  32. $passSession = $_SESSION['ssb-pass'];
  33. if(!isset($username) || !isset($passSession)) { return false; }
  34. include "ssb_db/users/" . $username . ".php";
  35. if($user_password === $passSession) {
  36. return true;
  37. } else {
  38. return false;
  39. }
  40. }
  41. function loginForm() {
  42. ?>
  43. <br />
  44. <div class="login">
  45. <h3>Login</h3>
  46. New to <?php echo $ssbtitle; ?>? No problem,
  47. <a href="<?php echo $_SERVER['PHP_SELF']; ?>?forms=register">register</a>
  48. here and get started!<br /><br />
  49. <form action="<?php echo $_SERVER['PHP_SELF']; ?>?do=login" method="post">
  50. <table><tr><td>
  51. Username:</td><td> <input class="text" type="text" name="username"></td></tr><tr><td>
  52. Password:</td><td> <input class="text" type="password" name="password"></td></tr></table>
  53. <input style="padding: 2px;" class="text" type="submit" name="submitBtn" value="Login">
  54. </form>
  55. </div>
  56. <?php
  57. }
  58. function changePassForm() {
  59. ?>
  60. <br />
  61. <div class="chgpass">
  62. <h3>Change password</h3>
  63. <form action="<?php echo $_SERVER['PHP_SELF']; ?>?do=changepass" method="post">
  64. <table><tr><td>
  65. Old password:</td><td> <input class="text" type="password" name="oldpass"></td></tr><tr><td>
  66. Password:</td><td> <input class="text" type="password" name="password"></td></tr><tr><td>
  67. Password Again:</td><td> <input class="text" type="password" name="password_again"></td></tr>
  68. </table>
  69. <input class="text" type="submit" name="submitBtn" value="Change">
  70. </form>
  71. </div>
  72. <?php
  73. }
  74. function getUserCount() {
  75. $user_count = "0";
  76. foreach(array_reverse(glob("ssb_db/users/*.name")) as $postfile) {
  77. $user_count++;
  78. }
  79. echo "$user_count users";
  80. }
  81. function getPostCount() {
  82. $post_count = "0";
  83. foreach(array_reverse(glob("ssb_db/posts/*.post")) as $postfile) {
  84. $post_count++;
  85. }
  86. echo "$post_count posts";
  87. }
  88. function getUploadFileCount() {
  89. $file_count = "0";
  90. foreach(array_reverse(glob("ssb_db/uploads/*")) as $postfile) {
  91. $file_count++;
  92. }
  93. echo "$file_count uploads";
  94. }
  95. function uploadAvatarForm() {
  96. ?>
  97. <br />
  98. <div class="upload">
  99. <form action="<?php echo $_SERVER['PHP_SELF']; ?>?do=avatarupload" method="post" enctype="multipart/form-data">
  100. Choose profile avatar: <br />
  101. <label class="input-upload">
  102. <input type="file" name="file[]" id="file" multiple>
  103. <i class="fa fa-cloud-upload"></i> Upload image
  104. </label>
  105. <input class="text" type="submit" name="submit" value="Set">
  106. </form>
  107. </div>
  108. <?php
  109. }
  110. /*function uploadForm() {
  111. print <<<EOD
  112. Upload
  113. <table style="margin:auto;">
  114. <form action="upload.php" method="post" enctype="multipart/form-data">
  115. <tr>
  116. <td>
  117. <input type="file" name="file[]" id="file" multiple><br>
  118. </td>
  119. <td>
  120. <input type="submit" name="submit" value="Upload">
  121. </td>
  122. </tr>
  123. </form>
  124. </table>
  125. EOD;
  126. }*/
  127. function registerForm() {
  128. ?>
  129. <br />
  130. <div class="login">
  131. <h3>Register</h3>
  132. <form action="<?php echo $_SERVER['PHP_SELF']; ?>?do=register" method="post">
  133. <table><tr><td>
  134. Username:</td><td> <input class="text" type="text" name="username"></td></tr><tr><td>
  135. Full name:</td><td> <input class="text" type="text" name="fullname"></td></tr><tr><td>
  136. Password:</td><td> <input class="text" type="password" name="password"></td></tr><tr><td>
  137. Password Again:</td><td> <input class="text" type="password" name="password-again"></td></tr><tr><td>
  138. <label for="acct">Profile type:</label>
  139. <select id="acct" name="acct">
  140. <option value="private">Private</option>
  141. <option value="public">Public</option>
  142. </select></td><td>
  143. <input class="text" type="submit" name="submitBtn" value="Register">
  144. </td></tr></table>
  145. </form>
  146. </div>
  147. <?php
  148. }
  149. function postForm() {
  150. print <<<EOD
  151. <h3>Create a post</h3>
  152. <button onclick="javascript:wrapBBCode('i');">Italic</button>
  153. <button onclick="javascript:wrapBBCode('u');">Underline</button>
  154. <button onclick="javascript:wrapBBCode('b');">Bold</button>
  155. <button onclick="javascript:wrapBBCode('url');">URL</button>
  156. <button onclick="javascript:wrapBBCode('youtube');">Youtube</button>
  157. <form action="?do=post" method="post" enctype="multipart/form-data">
  158. <label class="input-upload">
  159. <input type="file" name="file[]" id="file" multiple>
  160. <i class="fa fa-cloud-upload"></i> Upload photo / video
  161. </label>
  162. <br /><textarea rows="5" cols="60" id="msg" name="body"></textarea><br />
  163. <input type="submit" name="post" value="Post">
  164. </form>
  165. EOD;
  166. }
  167. function replyForm($id, $puser) {
  168. ?>
  169. <button onclick="javascript:wrapBBCode('i');">Italic</button>
  170. <button onclick="javascript:wrapBBCode('u');">Underline</button>
  171. <button onclick="javascript:wrapBBCode('b');">Bold</button>
  172. <button onclick="javascript:wrapBBCode('url');">URL</button>
  173. <button onclick="javascript:wrapBBCode('youtube');">Youtube</button>
  174. <form action="?do=reply&pid=<?php echo $id; ?>&user=<?php echo $puser; ?>" method="post">
  175. <textarea rows="7" cols="60" id="msg" name="body"></textarea><br />
  176. <input type="submit" name="reply" value="Reply">
  177. </form>
  178. <?php
  179. }
  180. function cleanForm() {
  181. ?>
  182. <br />
  183. <form action="?do=clean" method="post">
  184. Password: <input type="password" name="password" id="password"> <br />
  185. <input type="submit" name="post" value="Post">
  186. </form>
  187. <?php
  188. }
  189. function friendReqForm() {
  190. ?>
  191. <h3>Send a friend request</h3>
  192. <form action="?do=sendfr" method="post">
  193. Username: <input type="text" name="user" id="user"> <br />
  194. <input type="submit" name="post" value="Send">
  195. </form>
  196. <?php
  197. }
  198. function sendFriendRequest($user, $friend) {
  199. $friendLocation = "ssb_db/friends/" . $friend . ".notifications";
  200. $friendPending = "ssb_db/friends/" . $friend . ".pending";
  201. $handle = file_get_contents($friendLocation);
  202. if (strpos($handle, $user) !== FALSE) {
  203. echo "Friend request is send already and pending accept!<br />";
  204. exit(1);
  205. }
  206. // Check if user is itself
  207. if($user == $friend) { header("Location: index.php?do=friends"); exit(1); } // dont request from self.
  208. $friendc = file_get_contents("ssb_db/friends/" . $user . ".count");
  209. $friendcount = file_get_contents("ssb_db/friends/" . $user . ".count");
  210. include "ssb_db/friends/" . $user . ".php";
  211. for($x = 1; $x <= $friendcount; $x++)
  212. {
  213. if(${"friend" . $x} == $friend) { header("Location: index.php?do=friends"); echo "Already following!"; exit(1); }
  214. }
  215. if(file_exists($friendLocation)) {
  216. $notifications = file_get_contents($friendLocation);
  217. file_put_contents("ssb_db/friends/" . $friend . ".notifications", "Friend request from " . $user . "! <a class='button' href='?do=accfr&friend=" . $user . "&user=" . $friend . "'>Accept?</a>" . "\n" . $notifications);
  218. } else {
  219. file_put_contents("ssb_db/friends/" . $friend . ".notifications", "Friend request from " . $user . "! <a class='button' href='?do=accfr&friend=" . $user . "&user=" . $friend . "'>Accept?</a>");
  220. }
  221. if(file_exists($friendPending)) {
  222. $pending = file_get_contents($friendPending);
  223. file_put_contents("ssb_db/friends/" . $friend . ".pending", $user . "\n" . $pending);
  224. } else {
  225. file_put_contents("ssb_db/friends/" . $friend . ".pending", $user);
  226. }
  227. }
  228. function acceptPublicFriendRequest($user, $friend) {
  229. $friendpending = "ssb_db/friends/" . $user . ".pending";
  230. $friendlist = file_get_contents("ssb_db/friends/" . $user . ".php");
  231. $frienddb = file_get_contents("ssb_db/friends/" . $friend . ".php");
  232. // check if already on friends list.
  233. $friendc = file_get_contents("ssb_db/friends/" . $user . ".count");
  234. $friendcount = file_get_contents("ssb_db/friends/" . $user . ".count");
  235. include "ssb_db/friends/" . $user . ".php";
  236. for($x = 1; $x <= $friendcount; $x++)
  237. {
  238. if(${"friend" . $x} == $friend) { echo "Already following!"; exit(1); }
  239. }
  240. // populate both users databases with each other.
  241. $friendcountFriend = file_get_contents("ssb_db/friends/" . $friend . ".count");
  242. $friendcountFriend = $friendcountFriend + 1;
  243. //echo $friendcountFriend;
  244. file_put_contents("ssb_db/friends/" . $friend . ".php", $frienddb . "\n <?php \$friend" . $friendcountFriend ." = \"" . $user . "\";\n\$friend_chat_db" . $friendcountFriend . " = \"" . $user . $friend . "\";?>");
  245. $friendcount = file_get_contents("ssb_db/friends/" . $user . ".count");
  246. $friendcount = $friendcount + 1;
  247. //echo $friendcount;
  248. file_put_contents("ssb_db/friends/" . $user . ".php", $friendlist . "\n <?php \$friend" . $friendcount . " = \"" . $friend . "\";\n\$friend_chat_db" . $friendcount . " = \"" . $user . $friend . "\";?>");
  249. file_put_contents("ssb_db/friends/" . $user . ".count", $friendcount);
  250. file_put_contents("ssb_db/friends/" . $friend . ".count", $friendcountFriend);
  251. file_put_contents("ssb_db/friends/" . $user . $friend . ".count", "1");
  252. file_put_contents("ssb_db/friends/" . $user . $friend . ".php", "<?php \$msg1 = \"" . $user . " and " . $friend . " are now friends!<br />\";?>");
  253. }
  254. function acceptFriendRequest($user, $friend) {
  255. $friendpending = "ssb_db/friends/" . $user . ".pending";
  256. $friendlist = file_get_contents("ssb_db/friends/" . $user . ".php");
  257. $frienddb = file_get_contents("ssb_db/friends/" . $friend . ".php");
  258. // check if friend request is really pending.
  259. $friendc = file_get_contents("ssb_db/friends/" . $user . ".count");
  260. include "ssb_db/friends/" . $user . ".php";
  261. for($x = 1; $x <= $friendc; $x++)
  262. {
  263. if(${"friend" . $x} == $friend) { echo "Already following!"; exit(1); }
  264. }
  265. $handle = fopen($friendpending, "r");
  266. if ($handle) {
  267. $xx = 0;
  268. while (($line = fgets($handle)) !== false) {
  269. if($xx >= "1") {
  270. $line = str_replace("\n","",$line);
  271. }
  272. $xx++;
  273. //echo $line . "<br />";
  274. //echo $friend . "<br />";
  275. if($friend == $line)
  276. {
  277. // populate both users databases with each other.
  278. $friendcountFriend = file_get_contents("ssb_db/friends/" . $friend . ".count");
  279. $friendcountFriend = $friendcountFriend + 1;
  280. //echo $friendcountFriend;
  281. file_put_contents("ssb_db/friends/" . $friend . ".php", $frienddb . "\n <?php \$friend" . $friendcountFriend ." = \"" . $user . "\";\n\$friend_chat_db" . $friendcountFriend . " = \"" . $user . $friend . "\";?>");
  282. $friendcount = file_get_contents("ssb_db/friends/" . $user . ".count");
  283. $friendcount = $friendcount + 1;
  284. //echo $friendcount;
  285. file_put_contents("ssb_db/friends/" . $user . ".php", $friendlist . "\n <?php \$friend" . $friendcount . " = \"" . $friend . "\";\n\$friend_chat_db" . $friendcount . " = \"" . $user . $friend . "\";?>");
  286. file_put_contents("ssb_db/friends/" . $user . ".count", $friendcount);
  287. file_put_contents("ssb_db/friends/" . $friend . ".count", $friendcountFriend);
  288. file_put_contents("ssb_db/friends/" . $user . $friend . ".count", "1");
  289. file_put_contents("ssb_db/friends/" . $user . $friend . ".php", "<?php \$msg1 = \"" . $user . " and " . $friend . " are now friends!<br />\";?>");
  290. }
  291. }
  292. fclose($handle);
  293. } else {
  294. echo "ERROR: Friend: " . $friend . " not found in friend pending database.<br />";
  295. }
  296. }
  297. ?>