functions.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. function tomb($size, $precision = 2)
  8. {
  9. $base = log($size) / log(1024);
  10. $suffixes = array('', 'KB', 'MB', 'GB', 'TB');
  11. return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
  12. }
  13. function getRandString($n) {
  14. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  15. $randomString = '';
  16. for ($i = 0; $i < $n; $i++) {
  17. $index = rand(0, strlen($characters) - 1);
  18. $randomString .= $characters[$index];
  19. }
  20. return $randomString;
  21. }
  22. function searchForUserTag($str) {
  23. if(preg_match("/\s*@(.+?)\s/", $str, $matches)) {
  24. return $matches["0"];
  25. } else {
  26. return false;
  27. }
  28. }
  29. function loginForm() {
  30. ?>
  31. <br />
  32. <div class="login">
  33. <a class="button" href="<?php echo $_SERVER['PHP_SELF']; ?>?forms=register">Register</a>
  34. <form action="<?php echo $_SERVER['PHP_SELF']; ?>?do=login" method="post">
  35. <table><tr><td>
  36. Username:</td><td> <input class="text" type="text" name="username"></td></tr><tr><td>
  37. Password:</td><td> <input class="text" type="password" name="password"></td></tr></table>
  38. <input style="padding: 2px;" class="text" type="submit" name="submitBtn" value="Login">
  39. </form>
  40. </div>
  41. <?php
  42. }
  43. function changePassForm() {
  44. ?>
  45. <br />
  46. <div class="chgpass">
  47. <form action="<?php echo $_SERVER['PHP_SELF']; ?>?do=changepass" method="post">
  48. <table><tr><td>
  49. Old password:</td><td> <input class="text" type="password" name="oldpass"></td></tr><tr><td>
  50. Password:</td><td> <input class="text" type="password" name="password"></td></tr><tr><td>
  51. Password Again:</td><td> <input class="text" type="password" name="password_again"></td></tr>
  52. </table>
  53. <input class="text" type="submit" name="submitBtn" value="Change">
  54. </form>
  55. </div>
  56. <?php
  57. }
  58. function uploadAvatarForm() {
  59. ?>
  60. <br />
  61. <div class="upload">
  62. <form action="<?php echo $_SERVER['PHP_SELF']; ?>?do=avatarupload" method="post" enctype="multipart/form-data">
  63. Choose profile avatar: <br />
  64. <label class="input-upload">
  65. <input type="file" name="file[]" id="file" multiple>
  66. <i class="fa fa-cloud-upload"></i> Upload image
  67. </label>
  68. <input class="text" type="submit" name="submit" value="Set">
  69. </form>
  70. </div>
  71. <?php
  72. }
  73. /*function uploadForm() {
  74. print <<<EOD
  75. Upload
  76. <table style="margin:auto;">
  77. <form action="upload.php" method="post" enctype="multipart/form-data">
  78. <tr>
  79. <td>
  80. <input type="file" name="file[]" id="file" multiple><br>
  81. </td>
  82. <td>
  83. <input type="submit" name="submit" value="Upload">
  84. </td>
  85. </tr>
  86. </form>
  87. </table>
  88. EOD;
  89. }*/
  90. function registerForm() {
  91. ?>
  92. <br />
  93. <div class="login">
  94. <form action="<?php echo $_SERVER['PHP_SELF']; ?>?do=register" method="post">
  95. <table><tr><td>
  96. Username:</td><td> <input class="text" type="text" name="username"></td></tr><tr><td>
  97. Full name:</td><td> <input class="text" type="text" name="fullname"></td></tr><tr><td>
  98. Password:</td><td> <input class="text" type="password" name="password"></td></tr><tr><td>
  99. Password Again:</td><td> <input class="text" type="password" name="password-again"></td></tr><tr><td>
  100. <label for="acct">Profile type:</label>
  101. <select id="acct" name="acct">
  102. <option value="private">Private</option>
  103. <option value="public">Public</option>
  104. </select></td><td>
  105. <input class="text" type="submit" name="submitBtn" value="Register">
  106. </td></tr></table>
  107. </form>
  108. </div>
  109. <?php
  110. }
  111. function postForm() {
  112. print <<<EOD
  113. <button onclick="javascript:wrapBBCode('i');">Italic</button>
  114. <button onclick="javascript:wrapBBCode('u');">Underline</button>
  115. <button onclick="javascript:wrapBBCode('b');">Bold</button>
  116. <button onclick="javascript:wrapBBCode('url');">URL</button>
  117. <form action="?do=post" method="post" enctype="multipart/form-data">
  118. <label class="input-upload">
  119. <input type="file" name="file[]" id="file" multiple>
  120. <i class="fa fa-cloud-upload"></i> Upload image
  121. </label>
  122. <br /><textarea rows="5" cols="60" id="msg" name="body"></textarea><br />
  123. <input type="submit" name="post" value="Post">
  124. </form>
  125. EOD;
  126. }
  127. function replyForm($id, $puser) {
  128. ?>
  129. <button onclick="javascript:wrapBBCode('i');">Italic</button>
  130. <button onclick="javascript:wrapBBCode('u');">Underline</button>
  131. <button onclick="javascript:wrapBBCode('b');">Bold</button>
  132. <button onclick="javascript:wrapBBCode('url');">URL</button>
  133. <form action="?do=reply&pid=<?php echo $id; ?>&user=<?php echo $puser; ?>" method="post">
  134. <textarea rows="7" cols="60" id="msg" name="body"></textarea><br />
  135. <input type="submit" name="reply" value="Reply">
  136. </form>
  137. <?php
  138. }
  139. function cleanForm() {
  140. ?>
  141. <br />
  142. <form action="?do=clean" method="post">
  143. Password: <input type="password" name="password" id="password"> <br />
  144. <input type="submit" name="post" value="Post">
  145. </form>
  146. <?php
  147. }
  148. function friendReqForm() {
  149. ?>
  150. <h2>Request friendship!</h2>
  151. <form action="?do=sendfr" method="post">
  152. Username: <input type="text" name="user" id="user"> <br />
  153. <input type="submit" name="post" value="Send">
  154. </form>
  155. <?php
  156. }
  157. function sendFriendRequest($user, $friend) {
  158. $friendLocation = "ssb_db/friends/" . $friend . ".pending";
  159. $handle = fopen($friendLocation, "r");
  160. if ($handle) {
  161. while (($line = fgets($handle)) !== false) {
  162. if($line == $user) { break; } // request already pending
  163. }
  164. fclose($handle);
  165. }
  166. // Check if user is itself
  167. if($user == $friend) { header("Location: index.php?do=friends"); exit(1); } // dont request from self.
  168. $friendc = file_get_contents("ssb_db/friends/" . $user . ".count");
  169. $friendcount = file_get_contents("ssb_db/friends/" . $user . ".count");
  170. include "ssb_db/friends/" . $user . ".php";
  171. for($x = 1; $x <= $friendcount; $x++)
  172. {
  173. if(${"friend" . $x} == $friend) { header("Location: index.php?do=friends"); echo "Already following!"; exit(1); }
  174. }
  175. if(file_exists($friendLocation)) {
  176. $pending = file_get_contents("ssb_db/friends/" . $friend . ".pending");
  177. file_put_contents("ssb_db/friends/" . $friend . ".pending", $pending . "\n" . $user);
  178. } else {
  179. file_put_contents("ssb_db/friends/" . $friend . ".pending", $user);
  180. }
  181. }
  182. function acceptPublicFriendRequest($user, $friend) {
  183. $friendpending = "ssb_db/friends/" . $user . ".pending";
  184. $friendlist = file_get_contents("ssb_db/friends/" . $user . ".php");
  185. $frienddb = file_get_contents("ssb_db/friends/" . $friend . ".php");
  186. // check if already on friends list.
  187. $friendc = file_get_contents("ssb_db/friends/" . $user . ".count");
  188. $friendcount = file_get_contents("ssb_db/friends/" . $user . ".count");
  189. include "ssb_db/friends/" . $user . ".php";
  190. for($x = 1; $x <= $friendcount; $x++)
  191. {
  192. if(${"friend" . $x} == $friend) { echo "Already following!"; exit(1); }
  193. }
  194. // populate both users databases with each other.
  195. $friendcountFriend = file_get_contents("ssb_db/friends/" . $friend . ".count");
  196. $friendcountFriend = $friendcountFriend + 1;
  197. //echo $friendcountFriend;
  198. file_put_contents("ssb_db/friends/" . $friend . ".php", $frienddb . "\n <?php \$friend" . $friendcountFriend ." = \"" . $user . "\";\n\$friend_chat_db" . $friendcountFriend . " = \"" . $user . $friend . "\";?>");
  199. $friendcount = file_get_contents("ssb_db/friends/" . $user . ".count");
  200. $friendcount = $friendcount + 1;
  201. //echo $friendcount;
  202. file_put_contents("ssb_db/friends/" . $user . ".php", $friendlist . "\n <?php \$friend" . $friendcount . " = \"" . $friend . "\";\n\$friend_chat_db" . $friendcount . " = \"" . $user . $friend . "\";?>");
  203. file_put_contents("ssb_db/friends/" . $user . ".count", $friendcount);
  204. file_put_contents("ssb_db/friends/" . $friend . ".count", $friendcountFriend);
  205. file_put_contents("ssb_db/friends/" . $user . $friend . ".count", "1");
  206. file_put_contents("ssb_db/friends/" . $user . $friend . ".php", "<?php \$msg1 = \"" . $user . " and " . $friend . " are now friends!<br />\";?>");
  207. }
  208. function acceptFriendRequest($user, $friend) {
  209. $friendpending = "ssb_db/friends/" . $user . ".pending";
  210. $friendlist = file_get_contents("ssb_db/friends/" . $user . ".php");
  211. $frienddb = file_get_contents("ssb_db/friends/" . $friend . ".php");
  212. // check if friend request is really pending.
  213. $friendc = file_get_contents("ssb_db/friends/" . $user . ".count");
  214. include "ssb_db/friends/" . $user . ".php";
  215. for($x = 1; $x <= $friendc; $x++)
  216. {
  217. if(${"friend" . $x} == $friend) { echo "Already following!"; exit(1); }
  218. }
  219. $handle = fopen($friendpending, "r");
  220. if ($handle) {
  221. $xx = 0;
  222. while (($line = fgets($handle)) !== false) {
  223. if($xx >= "1") {
  224. $line = str_replace("\n","",$line);
  225. }
  226. $xx++;
  227. //echo $line . "<br />";
  228. //echo $friend . "<br />";
  229. if($friend == $line)
  230. {
  231. // populate both users databases with each other.
  232. $friendcountFriend = file_get_contents("ssb_db/friends/" . $friend . ".count");
  233. $friendcountFriend = $friendcountFriend + 1;
  234. //echo $friendcountFriend;
  235. file_put_contents("ssb_db/friends/" . $friend . ".php", $frienddb . "\n <?php \$friend" . $friendcountFriend ." = \"" . $user . "\";\n\$friend_chat_db" . $friendcountFriend . " = \"" . $user . $friend . "\";?>");
  236. $friendcount = file_get_contents("ssb_db/friends/" . $user . ".count");
  237. $friendcount = $friendcount + 1;
  238. //echo $friendcount;
  239. file_put_contents("ssb_db/friends/" . $user . ".php", $friendlist . "\n <?php \$friend" . $friendcount . " = \"" . $friend . "\";\n\$friend_chat_db" . $friendcount . " = \"" . $user . $friend . "\";?>");
  240. file_put_contents("ssb_db/friends/" . $user . ".count", $friendcount);
  241. file_put_contents("ssb_db/friends/" . $friend . ".count", $friendcountFriend);
  242. file_put_contents("ssb_db/friends/" . $user . $friend . ".count", "1");
  243. file_put_contents("ssb_db/friends/" . $user . $friend . ".php", "<?php \$msg1 = \"" . $user . " and " . $friend . " are now friends!<br />\";?>");
  244. }
  245. }
  246. fclose($handle);
  247. } else {
  248. echo "ERROR: Friend: " . $friend . " not found in friend pending database.<br />";
  249. }
  250. }
  251. ?>