functions.php 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. <?php
  2. /* this file stores all of the functions that are used
  3. over all of the other files. */
  4. ini_set("log_errors", 1);
  5. $srv = $user_settings['instance'];
  6. //$token = ($token != false ? msc($token,'d') : false);
  7. $token = ($token != false ? $token : false);
  8. // FUNCTIONS THAT HAVE TO DO WITH INSTANCE COMMUNICATION AND RETRIEVAL
  9. /* a function to make an authenticated general GET api call to the logged-in instance.
  10. - $url is a string of an api call like "account/:id/statuses"
  11. - returns the array conversion of the json response
  12. */
  13. function api_get($url) {
  14. global $srv;
  15. global $token;
  16. $curl = curl_init();
  17. curl_setopt($curl, CURLOPT_URL, "https://$srv/api/v1/" . $url);
  18. if (!is_null($token)) {
  19. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  20. 'Authorization: Bearer ' . $token
  21. ));
  22. }
  23. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  24. $result = curl_exec($curl);
  25. curl_close($curl);
  26. return json_decode($result, true);
  27. }
  28. /* same as above but used in some newer api endpoinds (v2) */
  29. function api_getv2($url) {
  30. global $srv;
  31. global $token;
  32. $curl = curl_init();
  33. curl_setopt($curl, CURLOPT_URL, "https://$srv/api/v2/" . $url);
  34. if (!is_null($token)) {
  35. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  36. 'Authorization: Bearer ' . $token
  37. ));
  38. }
  39. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  40. $result = curl_exec($curl);
  41. curl_close($curl);
  42. return json_decode($result, true);
  43. }
  44. /* a function to make an authenticated general POST api call to the logged-in instance.
  45. - $url is a string of an api call like "account/:id/statuses"
  46. - returns the array conversion of the json response
  47. */
  48. function api_post($url, $array) {
  49. global $srv;
  50. global $token;
  51. $cSession = curl_init();
  52. curl_setopt($cSession, CURLOPT_HEADER, false);
  53. curl_setopt($cSession, CURLOPT_POST, 1);
  54. curl_setopt($cSession, CURLOPT_URL, "https://$srv/api/v1/" . $url);
  55. if (!is_null($token)) {
  56. curl_setopt($cSession, CURLOPT_HTTPHEADER, array(
  57. 'Authorization: Bearer ' . $token
  58. ));
  59. }
  60. curl_setopt($cSession, CURLOPT_POSTFIELDS, http_build_query($array));
  61. curl_setopt($cSession, CURLOPT_RETURNTRANSFER, true);
  62. $result = curl_exec($cSession);
  63. curl_close($cSession);
  64. return json_decode($result, true);
  65. }
  66. /* a function to make an authenticated general DELETE api call to the logged-in instance.
  67. - $url is a string of an api call like "statuses/delete/:id"
  68. - returns the array conversion of the json response
  69. */
  70. function api_delete($url, $array) {
  71. global $srv;
  72. global $token;
  73. $cSession = curl_init();
  74. curl_setopt($cSession, CURLOPT_HEADER, false);
  75. curl_setopt($cSession, CURLOPT_POST, 1);
  76. curl_setopt($cSession, CURLOPT_URL, "https://$srv/api/v1/" . $url);
  77. curl_setopt($cSession, CURLOPT_CUSTOMREQUEST, "DELETE");
  78. if (!is_null($token)) {
  79. curl_setopt($cSession, CURLOPT_HTTPHEADER, array(
  80. 'Authorization: Bearer ' . $token
  81. ));
  82. }
  83. curl_setopt($cSession, CURLOPT_POSTFIELDS, http_build_query($array));
  84. curl_setopt($cSession, CURLOPT_RETURNTRANSFER, true);
  85. $result = curl_exec($cSession);
  86. curl_close($cSession);
  87. return json_decode($result, true);
  88. }
  89. /* a function to make an authenticated general PATCH api call to the logged-in instance.
  90. - $url is a string of an api call like "account/:id/statuses"
  91. - returns the array conversion of the json response
  92. */
  93. function api_patch($url, $array) {
  94. global $srv;
  95. global $token;
  96. $cSession = curl_init();
  97. curl_setopt($cSession, CURLOPT_HEADER, false);
  98. curl_setopt($cSession, CURLOPT_POST, 1);
  99. curl_setopt($cSession, CURLOPT_URL, "https://$srv/api/v1/" . $url);
  100. curl_setopt($cSession, CURLOPT_CUSTOMREQUEST, "PATCH");
  101. if (!is_null($token)) {
  102. curl_setopt($cSession, CURLOPT_HTTPHEADER, array(
  103. 'Authorization: Bearer ' . $token
  104. ));
  105. }
  106. curl_setopt($cSession, CURLOPT_POSTFIELDS, http_build_query($array));
  107. curl_setopt($cSession, CURLOPT_RETURNTRANSFER, true);
  108. $result = curl_exec($cSession);
  109. curl_close($cSession);
  110. return json_decode($result, true);
  111. }
  112. /* Function to upload files to the profile of the authenticated user
  113. - $type can be 'avatar' or 'header'
  114. - returns the json of the api call
  115. */
  116. function upload_profile($file,$type){
  117. global $srv;
  118. global $token;
  119. $mime = get_mime($file);
  120. $info = pathinfo($file);
  121. $name = $info['basename'];
  122. $output = new CURLFile($file, $mime, $name);
  123. $cSession = curl_init();
  124. curl_setopt($cSession, CURLOPT_URL, "https://$srv/api/v1/accounts/update_credentials");
  125. curl_setopt($cSession, CURLOPT_RETURNTRANSFER, true);
  126. curl_setopt($cSession, CURLOPT_POST, 1);
  127. curl_setopt($cSession, CURLOPT_CUSTOMREQUEST, "PATCH");
  128. curl_setopt($cSession, CURLOPT_HTTPHEADER, array(
  129. 'Authorization: Bearer ' . $token
  130. ));
  131. curl_setopt($cSession, CURLOPT_POSTFIELDS, array(
  132. $type => $output
  133. ));
  134. $result = curl_exec($cSession);
  135. curl_close($cSession);
  136. return $result;
  137. }
  138. /* this function fetches all the data from a profile (bio, username, relationships, etc)
  139. - $user is the id of the queried user
  140. - returns the array conversion of the json response
  141. */
  142. function user_info($user) {
  143. global $user_settings;
  144. $info = api_get("accounts/" . $user);
  145. $rel = api_get("accounts/relationships?id=" . $user);
  146. return array(
  147. $info,
  148. $rel
  149. );
  150. }
  151. /* this function fetches the context (the previous posts and replies) of a specified post
  152. - $post = ID of the post queried.
  153. - returns an array conversion of the json response
  154. */
  155. function context($post) {
  156. global $srv;
  157. global $token;
  158. $curl = curl_init();
  159. curl_setopt($curl, CURLOPT_URL, "https://$srv/api/v1/statuses/$post/context");
  160. if (!is_null($token)) {
  161. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  162. 'Authorization: Bearer ' . $token
  163. ));
  164. }
  165. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  166. $result = curl_exec($curl);
  167. curl_close($curl);
  168. return $result;
  169. }
  170. /* a function to fav or unfav a specified post
  171. - $post = ID of the post queried.
  172. - $mode can be true if is a fav or false if it's an unfav
  173. - returns the number of favs of the post after the specified action or "error"
  174. if there was an error performing the action
  175. */
  176. function favourite($post, $mode) {
  177. $result = api_post(($mode == true ? "statuses/$post/favourite" : "statuses/$post/unfavourite"),array());
  178. if (isset($result['favourites_count'])) {
  179. return $result['favourites_count'];
  180. }
  181. else {
  182. return "error";
  183. }
  184. }
  185. /* a function to reblog or unreblog a specified post
  186. - $post = ID of the post queried.
  187. - $mode can be true if is a reblog or false if it's an unreblog
  188. - returns the number of reblogs of the post after the specified action or "error"
  189. if there was an error performing the action
  190. */
  191. function reblog($post, $mode) {
  192. $result = api_post(($mode == true ? "statuses/$post/reblog" : "statuses/$post/unreblog"),array());
  193. if (isset($result['reblog']['reblogs_count'])) {
  194. return $result['reblog']['reblogs_count'];
  195. }
  196. elseif (isset($result['reblogs_count'])) {
  197. return $result['reblogs_count'];
  198. }
  199. else {
  200. return "error";
  201. }
  202. }
  203. /* function to delete a post
  204. - $id is the id of the post to delete
  205. - returns 1 if the post was deleted succesfully or 0 if there was an error
  206. */
  207. function delpost($id) {
  208. global $srv;
  209. global $token;
  210. if (!is_null($token)) {
  211. $curl = curl_init();
  212. curl_setopt($curl, CURLOPT_HEADER, false);
  213. curl_setopt($curl, CURLOPT_POST, 1);
  214. curl_setopt($curl, CURLOPT_URL, "https://$srv/api/v1/statuses/$id");
  215. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
  216. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  217. 'Authorization: Bearer ' . $token
  218. ));
  219. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  220. $result = curl_exec($curl);
  221. curl_close($curl);
  222. $result = json_decode($result, true);
  223. if (empty($result)) {
  224. return "1";
  225. }
  226. else {
  227. return "0";
  228. }
  229. }
  230. }
  231. /* function to issue a vote to a poll
  232. - $id is the id of the poll to vote on
  233. - $choices is a comma separated string of the choices that will be voted.
  234. */
  235. function vote($poll, $choices) {
  236. global $srv;
  237. global $token;
  238. if (!is_null($token)) {
  239. $cSession = curl_init();
  240. curl_setopt($cSession, CURLOPT_URL, "https://$srv/api/v1/polls/$poll/votes");
  241. curl_setopt($cSession, CURLOPT_RETURNTRANSFER, true);
  242. curl_setopt($cSession, CURLOPT_POST, 1);
  243. curl_setopt($cSession, CURLOPT_HTTPHEADER, array(
  244. 'Authorization: Bearer ' . $token
  245. ));
  246. $query = "";
  247. $choicelist = explode(",",$choices);
  248. foreach($choicelist as $choice){
  249. $query .= "choices[]=$choice&";
  250. }
  251. curl_setopt($cSession, CURLOPT_POSTFIELDS, $query);
  252. $result = curl_exec($cSession);
  253. curl_close($cSession);
  254. return $result;
  255. }
  256. else {
  257. return false;
  258. }
  259. }
  260. /* function to send a new post to the logged in instance
  261. - $text = the body of the message
  262. - $media = array of uploaded media id's
  263. - $reply = the id of a post being replied to, can be null.
  264. - $markdown = specify if the post uses markdown (unused at this moment)
  265. - $scope = the scope of the post (public, private, unlisted or direct)
  266. - $sensitive = bool to specify if the post media is sensitive
  267. - $spoiler = string of the title of the post
  268. - returns the json of the api response
  269. */
  270. function sendpost($text, $media, $reply = null, $markdown = false, $scope = "public", $sensitive = false, $spoiler = false) {
  271. global $srv;
  272. global $token;
  273. if (!is_null($token)) {
  274. $cSession = curl_init();
  275. curl_setopt($cSession, CURLOPT_URL, "https://$srv/api/v1/statuses");
  276. curl_setopt($cSession, CURLOPT_RETURNTRANSFER, true);
  277. curl_setopt($cSession, CURLOPT_POST, 1);
  278. curl_setopt($cSession, CURLOPT_HTTPHEADER, array(
  279. 'Authorization: Bearer ' . $token
  280. ));
  281. $query = "";
  282. $query .= "status=" . urlencode(html_entity_decode($text, ENT_QUOTES)) . "&visibility=" . $scope;;
  283. if (!is_null($reply) && $reply != "null") {
  284. $query .= "&in_reply_to_id=" . $reply;
  285. }
  286. if ($markdown == true) {
  287. $query .= "&content_type=text/markdown";
  288. }
  289. if ($sensitive == 'true') {
  290. $query .= "&sensitive=true";
  291. }
  292. if ($spoiler == true) {
  293. $query .= "&spoiler_text=" . $spoiler;
  294. }
  295. if (!is_null($media)) {
  296. foreach ($media as $mid) {
  297. $query .= "&media_ids[]=" . $mid;
  298. }
  299. }
  300. curl_setopt($cSession, CURLOPT_POSTFIELDS, $query);
  301. $result = curl_exec($cSession);
  302. curl_close($cSession);
  303. return $result;
  304. }
  305. else {
  306. return false;
  307. }
  308. }
  309. /* uploads a file to the logged in instance.
  310. - $file = path of the file to upload on local storage
  311. - returns an array where:
  312. 0: the ID of the uploaded file
  313. 1: the url of the file (if the file isn't an image, returns a placeholder)
  314. */
  315. function uploadpic($file) {
  316. global $srv;
  317. global $token;
  318. if (!is_null($token)) {
  319. $mime = get_mime($file);
  320. $info = pathinfo($file);
  321. $name = $info['basename'];
  322. $output = new CURLFile($file, $mime, $name);
  323. $cSession = curl_init();
  324. curl_setopt($cSession, CURLOPT_URL, "https://$srv/api/v1/media");
  325. curl_setopt($cSession, CURLOPT_RETURNTRANSFER, true);
  326. curl_setopt($cSession, CURLOPT_POST, 1);
  327. curl_setopt($cSession, CURLOPT_HTTPHEADER, array(
  328. 'Authorization: Bearer ' . $token
  329. ));
  330. curl_setopt($cSession, CURLOPT_POSTFIELDS, array(
  331. 'file' => $output
  332. ));
  333. $result = curl_exec($cSession);
  334. curl_close($cSession);
  335. $array = json_decode($result, true);
  336. $ext = explode(".", $array['url']);
  337. $ext = end($ext);
  338. $ext = explode("?", $ext) [0];
  339. if (in_array($ext, array('jpg','jpeg','gif','png','svg','webm'))) {
  340. $file = $array['url'];
  341. } elseif (in_array($ext, array('mp4','webp','ogv'))) {
  342. $file = "img/vid.png";
  343. } elseif (in_array($ext, array('mp3','ogg','oga','opus'))) {
  344. $file = "img/aud.png";
  345. } else {
  346. $file = "img/doc.png";
  347. }
  348. return json_encode(array(
  349. $array['id'],
  350. $file
  351. ));
  352. }
  353. else {
  354. return false;
  355. }
  356. }
  357. /* this is used to register DashFE as an application on an instance
  358. mostly used when logging in
  359. - $instance = the url of the instance where to log in
  360. - returns the array conversion of the json response
  361. */
  362. function register_app($instance) {
  363. global $setting;
  364. $cSession = curl_init();
  365. curl_setopt($cSession, CURLOPT_URL, "https://$instance/api/v1/apps");
  366. curl_setopt($cSession, CURLOPT_RETURNTRANSFER, true);
  367. curl_setopt($cSession, CURLOPT_HEADER, false);
  368. curl_setopt($cSession, CURLOPT_POST, 1);
  369. curl_setopt($cSession, CURLOPT_POSTFIELDS, http_build_query(array(
  370. 'client_name' => $setting['appname'],
  371. 'redirect_uris' => $setting['url'],
  372. 'scopes' => 'read write follow'
  373. )));
  374. $result = curl_exec($cSession);
  375. curl_close($cSession);
  376. return json_decode($result, true);
  377. }
  378. /* this function will get all the notes (reblogs and favs) that has an specified post
  379. - $thread = the id of the post queried
  380. - returns an array with all the notes
  381. each note is another array where:
  382. 0 = type of note ("reb" reblog or "fab" favorite)
  383. 1 = array of the details of the user, converted from the json of the api response.
  384. */
  385. function getnotes($thread) {
  386. global $user_settings;
  387. global $token;
  388. global $srv;
  389. global $setting;
  390. global $logedin;
  391. @$reb = array(
  392. api_get("statuses/" . $thread . "/reblogged_by")
  393. );
  394. @$fab = array(
  395. api_get("statuses/" . $thread . "/favourited_by")
  396. );
  397. $limit = (count($reb[0]) > count($fab[0]) ? count($reb[0]) - 1 : count($fab[0]) - 1);
  398. $notes = array();
  399. $index = 0;
  400. for ($i = 0;$i <= $limit;$i++) {
  401. if (isset($reb[0][$i])) {
  402. $notes[$index][0] = "reb";
  403. $notes[$index][1] = $reb[0][$i];
  404. $index++;
  405. }
  406. if (isset($fab[0][$i])) {
  407. $notes[$index][0] = "fav";
  408. $notes[$index][1] = $fab[0][$i];
  409. $index++;
  410. }
  411. }
  412. return $notes;
  413. }
  414. /* this function will fetch replies of a post
  415. - $thread = the id of the post from where to fetch replies
  416. - $since = id of a post. If specified, the function will fetch the replies
  417. only since the specified id.
  418. - returns an array with all the replies
  419. each element is another array where:
  420. 'mode' = type of the reply (ancestor or descendant)
  421. 'content' = array of the contents of the reply, converted from the json of the api response.
  422. */
  423. function getreplies($thread, $since = false) {
  424. global $user_settings;
  425. global $token;
  426. global $srv;
  427. global $setting;
  428. global $logedin;
  429. $context = json_decode(context($thread) , true);
  430. $array = array();
  431. if (!empty($context['ancestors'])) {
  432. if ($since == false) {
  433. foreach ($context['ancestors'] as $elem) {
  434. $elem['type'] = 'ancestor';
  435. $array[] = $elem;
  436. }
  437. }
  438. }
  439. $flag = 0;
  440. if (!empty($context['descendants'])) {
  441. foreach ($context['descendants'] as $elem) {
  442. if (($since != false && $flag == 1) || $since == false) {
  443. $elem['type'] = 'descendant';
  444. $array[] = $elem;
  445. }
  446. if ($since != false && $elem['id'] == $since) {
  447. $flag = 1;
  448. }
  449. }
  450. }
  451. $replies = array();
  452. foreach ($array as $item) {
  453. $reply['mode'] = "";
  454. if ($item['type'] == 'ancestor') {
  455. $reply['mode'] = "ancestor";
  456. }
  457. $replies[] = array(
  458. 'mode' => $reply['mode'],
  459. 'content' => $item
  460. );
  461. }
  462. return $replies;
  463. }
  464. /* this function takes some options from the init.php file and the user_settings cookie
  465. and fetches all the posts of the appropiate timeline
  466. - $query = an array with a set of elements generated by the file "include/init.php"
  467. - returns an array of the posts list fetched converted from the json response of the api.
  468. */
  469. function timeline($query) {
  470. global $token;
  471. global $srv;
  472. $notes = "";
  473. $hq = array();
  474. $hq['limit'] = 10;
  475. $hq['only_media'] = ($query['text'] == "off" ? 'true' : 'false');
  476. if ($query['next']){
  477. $hq['max_id'] = $query['next'];
  478. $next = $query['next'];
  479. } elseif ($query['since']) {
  480. $hq['since_id'] = $query['since'];
  481. }
  482. switch ($query['mode']) {
  483. case "home":
  484. $array = api_get("timelines/home?".http_build_query($hq));
  485. break;
  486. case "federated":
  487. $array = api_get("timelines/public?".http_build_query($hq));
  488. break;
  489. case "tag":
  490. $array = api_get("timelines/tag/" . $query['tag'] . "?".http_build_query($hq));
  491. break;
  492. case "local":
  493. $array = api_get("timelines/public?local=true&".http_build_query($hq));
  494. break;
  495. case "user":
  496. $array = api_get("accounts/" . $query['user'] . "/statuses?".http_build_query($hq));
  497. break;
  498. case "thread":
  499. $array = array(
  500. api_get("statuses/" . $query['thread'])
  501. );
  502. break;
  503. case "favourites":
  504. $array = api_get("favourites?".http_build_query($hq));
  505. break;
  506. case "direct":
  507. $array = api_get("timelines/direct?".http_build_query($hq));
  508. break;
  509. case "list":
  510. $array = api_get("timelines/list/" . $query['list'] . "?".http_build_query($hq));
  511. break;
  512. case "bookmarks":
  513. $array = api_get("bookmarks?".http_build_query($hq));
  514. break;
  515. case "search":
  516. $array = api_getv2("search?limit=40&q=".$query['search']."{$next}")['statuses'];
  517. break;
  518. case "account":
  519. $info = api_get("accounts/verify_credentials");
  520. $array = api_get("accounts/" . $info['id'] . "/statuses?".http_build_query($hq));
  521. break;
  522. default:
  523. $array = api_get("timelines/public?".http_build_query($hq));
  524. break;
  525. }
  526. if (!is_array($array)) {
  527. return false;
  528. }
  529. $next = end($array) ['id'];
  530. $thread = array();
  531. /*
  532. foreach ($array as $elem) {
  533. if ($query['replies'] == "on" || $query['mode'] == "thread") {
  534. $thread[] = $elem;
  535. }
  536. else {
  537. if ($elem['in_reply_to_id'] == null) {
  538. $thread[] = $elem;
  539. }
  540. }
  541. }*/
  542. foreach ($array as $elem) {
  543. if ($query['replies'] == "on" || $query['mode'] == "thread") {
  544. $thread[] = $elem;
  545. }
  546. else {
  547. if ($elem['in_reply_to_id'] == null || $elem['in_reply_to_account_id'] == $query['uid']) {
  548. $thread[] = $elem;
  549. } else {
  550. $rel = api_get("accounts/relationships?id=" . $elem['in_reply_to_account_id']);
  551. if ($rel[0]['following']){
  552. $thread[] = $elem;
  553. }
  554. }
  555. }
  556. }
  557. return $thread;
  558. }
  559. // FUNCTIONS THAT HAVE TO DO WITH RENDERING STUFF FOR THE PAGE
  560. /* this function is used to generate the html code of a poll */
  561. function renderPoll($elem) {
  562. global $logedin;
  563. $output = "";
  564. $output .= "<br>";
  565. $votes = $elem['poll']['votes_count'];
  566. if ($elem['poll']['voted'] || $elem['poll']['expired']) {
  567. $output.= "<b>Votes: $votes</b><br>";
  568. foreach ($elem['poll']['options'] as $option){
  569. $percentage = ($option['votes_count'] / $votes ) * 100;
  570. $output .= "<div class='polloption fixed' title='".$option['votes_count']." votes'><div class='voteBar' style='font-weight:bold; max-width:".$percentage."%;padding:1px; height:10px;'> </div>".$option['title']."</div>";
  571. }
  572. } else {
  573. foreach ($elem['poll']['options'] as $option){
  574. $output .= "<div class='polloption'>".$option['title']."</div>";
  575. }
  576. $output .= ($logedin ? "<input type='submit' class='vote' id='".$elem['poll']['id']."' value='Send Vote' style='padding:2px;' onClick='return false;'>" : "");
  577. }
  578. return $output;
  579. }
  580. /* this function is used to generate the html code of a reply */
  581. function render_reply($item) {
  582. global $user_settings;
  583. global $logedin;
  584. global $srv;
  585. $reply['mode'] = "";
  586. if (isset($item['type']) && $item['type'] == 'ancestor') {
  587. $reply['mode'] = "ancestor";
  588. }
  589. $reply['id'] = $item['id'];
  590. $reply['uid'] = $item['account']['id'];
  591. $reply['name'] = emojify($item['account']['display_name'], $item['account']['emojis'], 20);
  592. $reply['acct'] = $item['account']['acct'];
  593. $reply['handle'] = "@".explode("@",$item['account']['acct'])[0];
  594. $reply['avatar'] = $item['account']['avatar'];
  595. $reply['menu'] = "<ul>";
  596. if ($logedin) {
  597. $reply['menu'] .= ($item['account']['id'] == $user_settings['uid'] ? "<li><a href='?action=delete&thread=" . $item['id'] . "' onClick='return false;' class='delete fontello' id=':id:'>&#xe80e; Delete Post</a></li>" : "");
  598. $reply['menu'] .= "<li><a href='?action=compose&quote=" . $item['id'] . "' onClick='return false;' class='quote fontello' id='" . $item['id'] . "' style='background-color:transparent;'>&#xf10e; Quote Post</a></li>";
  599. $reply['menu'] .= ($item['account']['id'] != $user_settings['uid'] ? "<li><a href='?action=mute&user=" . $item['account']['id'] . "' onClick='return false;' class='mute fontello' id='" . $item['account']['id'] . "' style='background-color:transparent;'>&#xe81b; Mute User</a></li>" : "");
  600. $reply['menu'] .= ($item['account']['id'] != $user_settings['uid'] ? "<li><a href='?action=mute&thread=" . $item['account']['id'] . "' onClick='return false;' class='muteconv fontello' id='" . $item['id'] . "' style='background-color:transparent;'>&#xf1f7; Drop Thread</a></li>" : "");
  601. $reply['menu'] .= (isset($user_settings['pleroma']) ? "<li><a href='?action=hide&thread=" . $item['pleroma']['conversation_id'] . "' onClick='return false;' class='hide fontello' id='" . $item['pleroma']['conversation_id'] . "' style='background-color:transparent;'>&#xf1f8; Hide Thread</a></li>" : "");
  602. $reply['menu'] .= (isset($user_settings['pleroma']) ? "<li><a href='?action=bookmark&thread=" . $item['account']['id'] . "' onClick='return false;' class='" . ($item['bookmarked'] == true ? "un" : "") . "bookmark fontello' id='" . $item['id'] . "' style='background-color:transparent;'>&#xe81e; " . ($item['bookmarked'] == true ? "Unb" : "B") . "ookmark</a></li>" : "");
  603. $reply['menu'] .= ($item['account']['id'] != $user_settings['uid'] ? "<li><a href='?action=nsfw&user=" . $item['account']['id'] . "' onClick='return false;' class='nsfw fontello' id='" . $item['account']['id'] . "' style='background-color:transparent;'>&#xe829; User is NSFW</a></li>" : "");
  604. }
  605. $reply['menu'] .= "<li><a target='_blank' href='" . $item['url'] . "' class='original link fontello' style='background-color:transparent;'>&#xf14c; Original Note</a></li>";
  606. $reply['menu'] .= "</ul>";
  607. $json['id'] = $item['id'];
  608. $json['scope'] = $item['visibility'];
  609. if ($logedin) {
  610. $json['mentions'] = "";
  611. $array = $item["mentions"];
  612. $json['mentions'] = ($user_settings['acct'] == $item["account"]['acct'] ? "" : "@" . $item["account"]['acct']) . " ";
  613. if (!empty($array)) {
  614. foreach ($array as $mnt) {
  615. if ($mnt['acct'] != $user_settings['acct']) {
  616. $json['mentions'] .= "@" . $mnt['acct'] . " ";
  617. }
  618. }
  619. }
  620. }
  621. $reply['json'] = json_encode($json);
  622. $reply['replyto'] = ($item['in_reply_to_id'] ? " <a class='fontello link preview ldr' target='_blank' id='" . $item['in_reply_to_id'] . "' href='?thread=" . $item['in_reply_to_id'] . "'>&#xf112;</a> " : "");
  623. $reply['text'] = processText($item);
  624. $reply['date'] = "<a class='ldr postAge' id='".strtotime($item['created_at'])."' style='text-decoration:none;' target='_blank' href='?thread=" . $item['id'] . "'>" . time_elapsed_string($item['created_at']) . "</a>";
  625. $reply['visibility'] = $item['visibility'];
  626. $reply['media'] = "";
  627. if (!empty($item['media_attachments'])) {
  628. $reply['media'] = "<div style='width:170px; display:inline-block; float:left; margin:15px 0px 10px 0px;'>";
  629. $images = count($item['media_attachments']);
  630. $class = ($images > 1 ? "class='icon'" : "");
  631. foreach ($item['media_attachments'] as $file) {
  632. $ext = explode(".", $file['url']);
  633. $ext = end($ext);
  634. $ext = explode("?", $ext) [0];
  635. if (in_array($ext,array('webm','mp4','ogv'))) {
  636. $reply['media'] .= "<div style='text-align:center; width:100%;'><video preload='metadata' width='100%' controls ".($user_settings['videoloop'] == "on" ? "loop" : "").">
  637. <source src='" . $file['url'] . "' type='video/".($ext == "ogv" ? "ogg" : $ext)."'>
  638. </video></div>
  639. ";
  640. }
  641. elseif (in_array($ext,array('mp3','ogg','oga','opus'))) {
  642. $reply['media'] .= "<div style='text-align:center; width:100%;'><audio controls>
  643. <source src='" . $file['url'] . "' type='audio/$ext'>
  644. Your browser does not support the audio tag.
  645. </audio> </div>";
  646. }
  647. else {
  648. if ($item['sensitive'] == true && $user_settings['explicit'] != 'off') {
  649. $reply['media'] .= "<div style='overflow:hidden; float:left; margin:2px;' $class><a target='_blank' href='" . $file['url'] . "' onClick='return false;' class='blur'><noscript><img src='" . $file['url'] . "' style='width:100%;'></noscript><img " . "data-src='" . $file['url'] . "'" . " class='' style='max-width:100%; max-height:100% vertical-align:middle;'></a><a target='_blank' href='" . $file['url'] . "' onClick='return false;' class='open-lightbox' style='display:none;'><img src='" . $file['url'] . "' class='' style='width:100%;'></a></div>";
  650. }
  651. else {
  652. $reply['media'] .= "<div style='margin:0px;' $class><a target='_blank' href='" . $file['url'] . "' onClick='return false;' class='open-lightbox'><img src='" . $file['url'] . "'" . " class='' style='max-width:100%; max-height:100%; vertical-align:middle;'><noscript><img src='" . $file['url'] . "' style='width:100%;'></noscript></a></div>";
  653. }
  654. }
  655. }
  656. $reply['media'] .= "</div>";
  657. }
  658. $reply['buttons'] = "
  659. " . ($logedin ? "<div class='felem'><a onClick='return false' class='replyform' href='?thread=" . $item['id'] . "' style='font-family:fontello'>&#xf112;</a></div>" : "") . "
  660. <div class='felem'><a onClick='return false' " . ($logedin ? "class='" . ($item['favourited'] == true ? "unfav" : "fav") . "' href='?action=fav&thread=" . $item['id'] . "'" : "") . " style='font-family:fontello'>&#xe802; <span>" . $item['favourites_count'] . "</span></a></div>
  661. <div class='felem'><a onClick='return false' " . ($logedin && ($item['visibility'] != "private" || $item['visibility'] != "direct") ? "class='" . ($item['reblogged'] == true ? "unreblog" : "reblog") . "' href='?action=reblog&thread=" . $item['id'] . "'" : "") . " style='font-family:fontello'>&#xe83a; <span>" . $item['reblogs_count'] . "</span></a></div>
  662. ";
  663. $result = themes("get","templates/reply.txt");
  664. foreach ($reply as $key => $elem) {
  665. $result = str_replace(":$key:", $elem, $result);
  666. }
  667. return $result;
  668. }
  669. /* this is the same as above but is used on other places, like user bios and places where the
  670. shortcodes and the emoji url is defined in the same place */
  671. function emojify($string, $emojis, $size = 40) {
  672. foreach ($emojis as $emoji) {
  673. $string = str_replace(":" . $emoji['shortcode'] . ":", "<img class='emoji' alt='" . $emoji['shortcode'] . "' title='" . $emoji['shortcode'] . "' src='" . $emoji['static_url'] . "' height=$size style='vertical-align: middle;'>", $string);
  674. }
  675. return $string;
  676. }
  677. /* This function displays the emoji list of an instance based on a search
  678. string given on $val */
  679. function emoji_list($val){
  680. $emojilist = api_get("/custom_emojis");
  681. $c = 0;
  682. $return = "";
  683. foreach ($emojilist as $emoji){
  684. if (starts_with($emoji['shortcode'],$val) && $c < 50){
  685. $return .= "<img style='margin:1px;' src='".$emoji['static_url']."' class='emoji' title='".$emoji['shortcode']."' height=40>";
  686. $c++;
  687. }
  688. }
  689. if ($c < 50){
  690. foreach ($emojilist as $emoji){
  691. if ((contains($emoji['shortcode'],$val) && !starts_with($emoji['shortcode'],$val)) && $c < 50){
  692. $return .= "<img style='margin:1px;' src='".$emoji['static_url']."' class='emoji' title='".$emoji['shortcode']."' height=40>";
  693. $c++;
  694. }
  695. }
  696. }
  697. return $return;
  698. }
  699. function contact_search($val){
  700. global $user_settings;
  701. $return = "";
  702. $list = api_get("/accounts/search?q=".$val);
  703. foreach ($list as $contact){
  704. $return .= "<div class='contact' title='@".$contact['acct']."' style='width:100%; clear:both; height:40px; display:inline-block;'>
  705. <div style='width:40px; height:40px; background-size:cover; background-image:url(".$contact['avatar']."); float:left;'></div>
  706. <div>
  707. <span style='font-weight:bold;'>".emojify($contact['display_name'], $contact['emojis'], 15)."</span><br>
  708. <span style='font-size:12px;'>".$contact['acct']."</span>
  709. </div>
  710. </div>";
  711. }
  712. return $return;
  713. }
  714. /* This function will fetch and render all the notifications since an $id
  715. or get a list of all past notification ($max notifications specified)
  716. */
  717. function getnotif($id = false, $max = false) {
  718. global $srv;
  719. global $token;
  720. global $user_settings;
  721. $n = "";
  722. $exclude = "";
  723. $exclude .= (str_split($user_settings['notif'])[0] == 0 ? "&exclude_types[]=favourite" : "");
  724. $exclude .= (str_split($user_settings['notif'])[1] == 0 ? "&exclude_types[]=reblog" : "");
  725. $exclude .= (str_split($user_settings['notif'])[2] == 0 ? "&exclude_types[]=mention" : "");
  726. $exclude .= (str_split($user_settings['notif'])[3] == 0 ? "&exclude_types[]=follow" : "");
  727. $notif = api_get("notifications?" . ($id == false ? "limit=9&" : "") . ($id != false ? ($max == true ? "max_id=$id" : "since_id=$id") : "").$exclude);
  728. if (!empty($notif)) {
  729. foreach ($notif as $post) {
  730. if (!isset($post["type"])){
  731. break;
  732. }
  733. $user = "<a class='link ldr uname' style='font-size:12px;' href='?user=" . $post['account']['id'] . "'>" . (empty($post['account']['display_name']) ? $post['account']['acct'] : emojify($post['account']['display_name'], $post['account']['emojis'], 10)) . "</a>";
  734. $preview = "";
  735. $buttons = "";
  736. $media = "";
  737. if (!in_array($post["type"],array("mention","favourite","reblog","follow"))){
  738. continue;
  739. }
  740. switch ($post["type"]) {
  741. case "mention":
  742. if ($post['status']['in_reply_to_id'] == null) {
  743. $type = "<span class='fontello' style='color:#62C2CC; font-size:10px;'>&#xf10d;</span>";
  744. $string = "mentioned you in a post";
  745. $preview = "<a style='text-decoration:none;' class='ldr text' href='?thread=" . $post['status']['id'] . "' target='_blank'><span style='display:block; opacity:1; font-size:10px; line-height:12px;'>" . emojify(strip_tags(trim($post['status']['content']) , '<br><br \>') , $post['status']['emojis'], 15) . "</span></a>";
  746. $media = (!empty($post['status']['media_attachments']) ? "<a style='text-decoration:none;' class='ldr' href='?thread=" . $post['status']['id'] . "' target='_blank'><div class='notifpic' style='flex: 0 0 60px; background-size:cover; background-image:url(" . $post['status']['media_attachments'][0]['url'] . ");'></div></a>" : "");
  747. }
  748. else {
  749. $type = "<span class='fontello' style='color:#62C2CC; font-size:10px;'>&#xf112;</span>";
  750. $string = "replied to your post";
  751. foreach ($post['status']['mentions'] as $mention) {
  752. if(!contains($post['status']['content'],$mention['username'])) {
  753. $post['status']['content'] = "@" . $mention['username'] . " ".$post['status']['content'];
  754. }
  755. }
  756. $preview = "<a style='text-decoration:none;' class='ldr text' href='?thread=" . $post['status']['id'] . "' target='_blank'><span style='display:block; opacity:1; font-size:10px; line-height:12px;'>" . emojify(strip_tags(trim($post['status']['content']) , '<br><br \>') , $post['status']['emojis'], 15) . "</span></a>";
  757. $media = (!empty($post['status']['media_attachments']) ? "<div class='notifpic' style='flex: 0 0 60px; background-size:cover; background-image:url(" . $post['status']['media_attachments'][0]['url'] . ");'></div>" : "");
  758. }
  759. $array = $post['status']["mentions"];
  760. $mentions = ($user_settings['acct'] == $post['status']['account']['acct'] ? "" : "@" . $post['status']['account']['acct']) . " ";
  761. if (!empty($array)) {
  762. foreach ($array as $mnt) {
  763. if ($mnt['acct'] != $user_settings['acct']) {
  764. $mentions .= "@" . $mnt['acct'] . " ";
  765. }
  766. }
  767. }
  768. $buttons = "<div class='post_buttons' id='" . $post['status']['id'] . "' style='position:absolute; right:10px; bottom:10px; border-radius:60px; padding:5px;'>
  769. <div class='felem'><a onClick='return false' class='quickreply' id='" . $post['status']['id'] . "' data-mentions='" . $mentions . "' href='?thread=" . $post['status']['id'] . "' style='font-family:fontello'>&#xe824;</a></div>
  770. <div class='felem'><a onClick='return false' class='" . ($post['status']['favourited'] == true ? "unfav" : "fav") . "' href='?action=fav&thread=" . $post['status']['id'] . "'" . " style='font-family:fontello'>&#xe802;</a></div>
  771. <div class='felem'><a onClick='return false' " . ($post['status']['visibility'] == "public" || $post['status']['visibility'] == "unlisted" ? "class='" . ($post['status']['reblogged'] == true ? "unreblog" : "reblog") . "' href='?action=reblog&thread=" . $post['status']['id'] . "'" : "") . " style='font-family:fontello'>&#xe83a;</a></div></div>";
  772. break;
  773. case "favourite":
  774. $type = "<span class='fontello' style='color:#F17022; font-size:10px;'>&#xe802;</span>";
  775. $string = "favourited your post";
  776. $preview = "<a style='text-decoration:none;' class='ldr text' href='?thread=" . $post['status']['id'] . "' target='_blank'><span style='display:block; opacity:1; font-size:10px; line-height:12px;'>" . (!empty($post['status']['content']) ? emojify(strip_tags(trim($post['status']['content']) , '<br><br \>') , $post['status']['emojis'], 15) : "Favourited your image") . "</span></a>";
  777. $media = (!empty($post['status']['media_attachments']) ? "<div class='notifpic' style='flex: 0 0 60px; background-size:cover; background-image:url(" . $post['status']['media_attachments'][0]['url'] . ");'></div>" : "");
  778. break;
  779. case "reblog":
  780. $type = "<span class='fontello' style='color:#D1DC29; font-size:10px;'>&#xe826;</span>";
  781. $string = "reblogged your post";
  782. $preview = "<a style='text-decoration:none;' class='ldr text' href='?thread=" . $post['status']['id'] . "' target='_blank'><span style='display:block; opacity:1; font-size:10px; line-height:12px;'>" . (!empty($post['status']['content']) ? emojify(strip_tags(trim($post['status']['content']) , '<br><br \>') , $post['status']['emojis'], 15) : "Reblogged your image") . "</span></a>";
  783. @$media = (!is_null($post['status']['media_attachments']) ? "<div class='notifpic' style='flex: 0 0 60px; background-size:cover; background-image:url(" . $post['status']['media_attachments'][0]['url'] . ");'></div>" : "");
  784. break;
  785. case "follow":
  786. list($info, $rel) = user_info($post["account"]["id"]);
  787. $type = "<span class='fontello' style='color:#FDB813; font-size;10px;'>&#xf234;</span>";
  788. $preview = "started following you";
  789. if ($rel[0]['following']) {
  790. $label = "&#xe80c; Following";
  791. $class = "unfollow";
  792. }
  793. else {
  794. if ($info['locked']) {
  795. if ($rel[0]['requested']) {
  796. $label = "&#xe806; Follow Requested";
  797. $class = "unfollow";
  798. }
  799. else {
  800. $label = "&#xe806; Request Follow";
  801. $class = "follow";
  802. }
  803. }
  804. else {
  805. $label = "&#xf234; Follow";
  806. $class = "follow";
  807. }
  808. }
  809. $buttons .= "<div class='post_buttons' style='position:absolute; right:10px; bottom:10px; border-radius:60px; padding:5px;'><span id='" . $info['id'] . "' class='profileButton $class' style='background-color:white; font-family:sans,fontello ;font-size:13px;'>$label</span></div>";
  810. break;
  811. }
  812. $n .= "
  813. <div class='notif " . ($id == false ? "" : "new") . "' id='" . $post['id'] . "'>
  814. <div class='notifContents'>
  815. <div style='flex: 0 0 60px; background-size:cover; background-image:url(" . $post['account']['avatar'] . "); border-radius:5px;'></div>
  816. <div style='flex: 1; padding-left:5px; padding-right:5px; word-break: break-word; overflow:hidden;'>
  817. <span>$type <span style='font-size:12px; font-weight:bold;'>$user</span></span>
  818. " . trim($preview) . "
  819. $buttons
  820. </div>
  821. $media
  822. </div>
  823. </div>
  824. ";
  825. }
  826. return $n;
  827. }
  828. }
  829. /* this function takes in a whole post entity and spits out the HTMLfied text of the post
  830. with the urls and @handles linkified and all the emojis replaced */
  831. function processText($elem) {
  832. global $user_settings;
  833. global $logedin;
  834. require_once "vendor/simple_html_dom.php";
  835. $content = trim(html_entity_decode($elem['content'],ENT_QUOTES));
  836. $content = preg_replace("/<(?=[^>]*(?:<|$))/","&lt;",$content);
  837. if (!empty($content)) {
  838. $html = str_get_html($content);
  839. foreach ($html->find('a') as $lnk) {
  840. //remove text links to media attachments
  841. foreach ($elem['media_attachments'] as $f) {
  842. if (is_numeric(strpos($f['description'],explode("…",$lnk->innertext)[0]))) {
  843. $content = str_replace($lnk->outertext . "<br/>", null, $content);
  844. $content = str_replace("<br/>" . $lnk->outertext, null, $content);
  845. $content = str_replace($lnk->outertext, null, $content);
  846. }
  847. }
  848. //modify links for hashtags and external urls
  849. if (is_numeric(strpos($lnk->href, $user_settings['instance'])) || in_array($lnk->class, array(
  850. "u-url mention",
  851. "hashtag"
  852. )) || $lnk->rel == "tag") {
  853. $content = str_replace($lnk->outertext, $lnk->innertext, $content);
  854. }
  855. else {
  856. $prv = $lnk->outertext;
  857. $lnk->target = '_blank';
  858. $lnk->class = 'link external';
  859. $content = str_replace($prv, $lnk->outertext, $content);
  860. }
  861. }
  862. }
  863. $result = strip_tags($content, '<br><p><strong><a><em><strike>');
  864. $result = str_replace('<br />', ' <br>', $result);
  865. foreach ($elem['mentions'] as $mention) {
  866. if(contains($result,"@".$mention['username'])) {
  867. $result = str_replace("@" . $mention['username'], "<span class='user' id='" . $mention['id'] . "'><a href='?user=" . $mention['id'] . "' class='link ldr' onClick='return false;'>@" . $mention['username'] . "</a></span>", $result);
  868. } else {
  869. $result = "<span class='user' id='" . $mention['id'] . "'><a href='?user=" . $mention['id'] . "' class='link ldr' onClick='return false;'>@" . $mention['username'] . "</a></span> ".$result;
  870. }
  871. }
  872. preg_match_all('/\\{:(.*?):\\}/i', $result, $matches);
  873. if (!empty($matches)){
  874. $result = str_replace($matches[0][0],"",$result);
  875. $sticker = substr($matches[0][0],1,-1);
  876. foreach ($elem['emojis'] as $emoji) {
  877. $sticker = str_replace(":" . $emoji['shortcode'] . ":", "<img class='sticker' alt='" . $emoji['shortcode'] . "' title='" . $emoji['shortcode'] . "' src='" . $emoji['static_url'] . "' height=120 style='vertical-align: middle;'>", $sticker);
  878. }
  879. $result = $sticker."<br>".$result;
  880. }
  881. $result = emojify($result, $elem['emojis']);
  882. if ($logedin){
  883. $result = preg_replace("/#([A-Za-z0-9\/\.]*)/", "<a class='ldr' href=\"./?tag=$1\"><b>#$1</b></a>", $result);
  884. } else {
  885. $result = preg_replace("/#([A-Za-z0-9\/\.]*)/", "<b>#$1</b>", $result);
  886. }
  887. return $result;
  888. }
  889. // OTHER FUNCTIONS
  890. /* the purpose of this function is to encode the auth token
  891. it is not used for now */
  892. function msc($string, $action = 'e') {
  893. // you may change these values to your own
  894. $secret_key = 'yAmfVhZwm0749FSY24dC';
  895. $secret_iv = 'm37uvAeKjYLKdI1lPkcJ';
  896. $output = false;
  897. $encrypt_method = "AES-256-CBC";
  898. $key = hash('sha256', $secret_key);
  899. $iv = substr(hash('sha256', $secret_iv) , 0, 16);
  900. if ($action == 'e') {
  901. $output = base64_encode(openssl_encrypt($string, $encrypt_method, $key, 0, $iv));
  902. }
  903. else if ($action == 'd') {
  904. $output = openssl_decrypt(base64_decode($string) , $encrypt_method, $key, 0, $iv);
  905. }
  906. return $output;
  907. }
  908. /* this function extracts the urls from a text and return them in an array */
  909. function get_urls($input) {
  910. $pattern = '$(https?://[a-z0-9_./?=&-~]+)(?![^<>]*>)$i';
  911. if (preg_match_all($pattern, $input, $matches)) {
  912. list($dummy, $links) = ($matches);
  913. return $links;
  914. }
  915. return false;
  916. }
  917. /* general function to check if one strings starts with a given search */
  918. function starts_with($string,$search){
  919. if (substr(strtolower($string),0,strlen($search)) == strtolower($search)){
  920. return true;
  921. }
  922. return false;
  923. }
  924. /* general function to check if one strings contains with a given search */
  925. function contains($string,$search){
  926. if (is_numeric(strpos(strtolower($string),strtolower($search)))){
  927. return true;
  928. }
  929. return false;
  930. }
  931. /* this function just reduces an image to a 1x1 pixel image to get the overall color.
  932. */
  933. function averageColor($url) {
  934. @$image = imagecreatefromstring(file_get_contents($url));
  935. if (!$image) {
  936. $mainColor = "CCCCCC";
  937. }
  938. else {
  939. $thumb = imagecreatetruecolor(1, 1);
  940. imagecopyresampled($thumb, $image, 0, 0, 0, 0, 1, 1, imagesx($image) , imagesy($image));
  941. $mainColor = strtoupper(dechex(imagecolorat($thumb, 0, 0)));
  942. }
  943. return $mainColor;
  944. }
  945. /* function used in the process of uploading a file */
  946. function get_mime($filename) {
  947. $result = new finfo();
  948. if (is_resource($result) === true) {
  949. return $result->file($filename, FILEINFO_MIME_TYPE);
  950. }
  951. return false;
  952. }
  953. function sanitize($text){
  954. return preg_replace("/[^a-zA-Z0-9]+/", "", $text);
  955. }
  956. function themes($mode,$name = false){
  957. global $user_settings;
  958. switch ($mode){
  959. case "list":
  960. $themes = scandir("themes/");
  961. $themelist = array();
  962. foreach ($themes as $elem){
  963. if ($elem != ".." && $elem != "." && $elem != "custom" && is_dir("themes/".$elem)){
  964. $themelist[] = $elem;
  965. }
  966. }
  967. return $themelist;
  968. case "file":
  969. $theme = sanitize($user_settings['theme']);
  970. if (file_exists("themes/$theme/$name")){
  971. return "themes/$theme/$name";
  972. } else {
  973. return "$name";
  974. }
  975. case "get":
  976. $theme = sanitize($user_settings['theme']);
  977. if (file_exists("themes/$theme/$name")){
  978. return file_get_contents("themes/$theme/$name");
  979. } else {
  980. return file_get_contents("$name");
  981. }
  982. }
  983. }
  984. function time_elapsed_string($datetime, $full = false) {
  985. $now = new DateTime;
  986. $ago = new DateTime($datetime);
  987. $diff = $now->diff($ago);
  988. $diff->w = floor($diff->d / 7);
  989. $diff->d -= $diff->w * 7;
  990. $string = array(
  991. 'y' => 'year',
  992. 'm' => 'month',
  993. 'w' => 'week',
  994. 'd' => 'day',
  995. 'h' => 'hour',
  996. 'i' => 'minute',
  997. 's' => 'second',
  998. );
  999. foreach ($string as $k => &$v) {
  1000. if ($diff->$k) {
  1001. $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
  1002. } else {
  1003. unset($string[$k]);
  1004. }
  1005. }
  1006. if (!$full) $string = array_slice($string, 0, 1);
  1007. return $string ? implode(', ', $string) . ' ago' : 'just now';
  1008. }
  1009. function getHeaders($respHeaders) {
  1010. $headers = array();
  1011. $headerText = substr($respHeaders, 0, strpos($respHeaders, "\r\n\r\n"));
  1012. foreach (explode("\r\n", $headerText) as $i => $line) {
  1013. if ($i === 0) {
  1014. $headers['http_code'] = $line;
  1015. } else {
  1016. list ($key, $value) = explode(': ', $line);
  1017. $headers[$key] = $value;
  1018. }
  1019. }
  1020. return $headerText;
  1021. }