Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

314 lines
7.6 KiB

  1. <?php
  2. /**
  3. * @file include/text.php
  4. */
  5. use Friendica\App;
  6. use Friendica\Content\ContactSelector;
  7. use Friendica\Content\Feature;
  8. use Friendica\Content\Smilies;
  9. use Friendica\Content\Text\BBCode;
  10. use Friendica\Core\Addon;
  11. use Friendica\Core\Config;
  12. use Friendica\Core\L10n;
  13. use Friendica\Core\PConfig;
  14. use Friendica\Core\Protocol;
  15. use Friendica\Core\System;
  16. use Friendica\Database\DBA;
  17. use Friendica\Model\Contact;
  18. use Friendica\Model\Event;
  19. use Friendica\Model\Item;
  20. use Friendica\Render\FriendicaSmarty;
  21. use Friendica\Util\DateTimeFormat;
  22. use Friendica\Util\Map;
  23. use Friendica\Util\Proxy as ProxyUtils;
  24. use Friendica\Core\Logger;
  25. use Friendica\Core\Renderer;
  26. use Friendica\Model\FileTag;
  27. use Friendica\Util\Strings;
  28. use Friendica\Util\XML;
  29. use Friendica\Content\Text\HTML;
  30. /**
  31. * Turn user/group ACLs stored as angle bracketed text into arrays
  32. *
  33. * @param string $s
  34. * @return array
  35. */
  36. function expand_acl($s) {
  37. // turn string array of angle-bracketed elements into numeric array
  38. // e.g. "<1><2><3>" => array(1,2,3);
  39. $ret = [];
  40. if (strlen($s)) {
  41. $t = str_replace('<', '', $s);
  42. $a = explode('>', $t);
  43. foreach ($a as $aa) {
  44. if (intval($aa)) {
  45. $ret[] = intval($aa);
  46. }
  47. }
  48. }
  49. return $ret;
  50. }
  51. /**
  52. * Wrap ACL elements in angle brackets for storage
  53. * @param string $item
  54. */
  55. function sanitise_acl(&$item) {
  56. if (intval($item)) {
  57. $item = '<' . intval(Strings::escapeTags(trim($item))) . '>';
  58. } else {
  59. unset($item);
  60. }
  61. }
  62. /**
  63. * Convert an ACL array to a storable string
  64. *
  65. * Normally ACL permissions will be an array.
  66. * We'll also allow a comma-separated string.
  67. *
  68. * @param string|array $p
  69. * @return string
  70. */
  71. function perms2str($p) {
  72. $ret = '';
  73. if (is_array($p)) {
  74. $tmp = $p;
  75. } else {
  76. $tmp = explode(',', $p);
  77. }
  78. if (is_array($tmp)) {
  79. array_walk($tmp, 'sanitise_acl');
  80. $ret = implode('', $tmp);
  81. }
  82. return $ret;
  83. }
  84. /**
  85. * for html,xml parsing - let's say you've got
  86. * an attribute foobar="class1 class2 class3"
  87. * and you want to find out if it contains 'class3'.
  88. * you can't use a normal sub string search because you
  89. * might match 'notclass3' and a regex to do the job is
  90. * possible but a bit complicated.
  91. * pass the attribute string as $attr and the attribute you
  92. * are looking for as $s - returns true if found, otherwise false
  93. *
  94. * @param string $attr attribute value
  95. * @param string $s string to search
  96. * @return boolean True if found, False otherwise
  97. */
  98. function attribute_contains($attr, $s) {
  99. $a = explode(' ', $attr);
  100. return (count($a) && in_array($s,$a));
  101. }
  102. /**
  103. * Compare activity uri. Knows about activity namespace.
  104. *
  105. * @param string $haystack
  106. * @param string $needle
  107. * @return boolean
  108. */
  109. function activity_match($haystack,$needle) {
  110. return (($haystack === $needle) || ((basename($needle) === $haystack) && strstr($needle, NAMESPACE_ACTIVITY_SCHEMA)));
  111. }
  112. /**
  113. * quick and dirty quoted_printable encoding
  114. *
  115. * @param string $s
  116. * @return string
  117. */
  118. function qp($s) {
  119. return str_replace("%", "=", rawurlencode($s));
  120. }
  121. /**
  122. * @brief Find any non-embedded images in private items and add redir links to them
  123. *
  124. * @param App $a
  125. * @param array &$item The field array of an item row
  126. */
  127. function redir_private_images($a, &$item)
  128. {
  129. $matches = false;
  130. $cnt = preg_match_all('|\[img\](http[^\[]*?/photo/[a-fA-F0-9]+?(-[0-9]\.[\w]+?)?)\[\/img\]|', $item['body'], $matches, PREG_SET_ORDER);
  131. if ($cnt) {
  132. foreach ($matches as $mtch) {
  133. if (strpos($mtch[1], '/redir') !== false) {
  134. continue;
  135. }
  136. if ((local_user() == $item['uid']) && ($item['private'] == 1) && ($item['contact-id'] != $a->contact['id']) && ($item['network'] == Protocol::DFRN)) {
  137. $img_url = 'redir?f=1&quiet=1&url=' . urlencode($mtch[1]) . '&conurl=' . urlencode($item['author-link']);
  138. $item['body'] = str_replace($mtch[0], '[img]' . $img_url . '[/img]', $item['body']);
  139. }
  140. }
  141. }
  142. }
  143. /**
  144. * @brief Given a text string, convert from bbcode to html and add smilie icons.
  145. *
  146. * @param string $text String with bbcode.
  147. * @return string Formattet HTML.
  148. */
  149. function prepare_text($text) {
  150. if (stristr($text, '[nosmile]')) {
  151. $s = BBCode::convert($text);
  152. } else {
  153. $s = Smilies::replace(BBCode::convert($text));
  154. }
  155. return trim($s);
  156. }
  157. /**
  158. * return array with details for categories and folders for an item
  159. *
  160. * @param array $item
  161. * @return array
  162. *
  163. * [
  164. * [ // categories array
  165. * {
  166. * 'name': 'category name',
  167. * 'removeurl': 'url to remove this category',
  168. * 'first': 'is the first in this array? true/false',
  169. * 'last': 'is the last in this array? true/false',
  170. * } ,
  171. * ....
  172. * ],
  173. * [ //folders array
  174. * {
  175. * 'name': 'folder name',
  176. * 'removeurl': 'url to remove this folder',
  177. * 'first': 'is the first in this array? true/false',
  178. * 'last': 'is the last in this array? true/false',
  179. * } ,
  180. * ....
  181. * ]
  182. * ]
  183. */
  184. function get_cats_and_terms($item)
  185. {
  186. $categories = [];
  187. $folders = [];
  188. $matches = false;
  189. $first = true;
  190. $cnt = preg_match_all('/<(.*?)>/', $item['file'], $matches, PREG_SET_ORDER);
  191. if ($cnt) {
  192. foreach ($matches as $mtch) {
  193. $categories[] = [
  194. 'name' => XML::escape(FileTag::decode($mtch[1])),
  195. 'url' => "#",
  196. 'removeurl' => ((local_user() == $item['uid'])?'filerm/' . $item['id'] . '?f=&cat=' . XML::escape(FileTag::decode($mtch[1])):""),
  197. 'first' => $first,
  198. 'last' => false
  199. ];
  200. $first = false;
  201. }
  202. }
  203. if (count($categories)) {
  204. $categories[count($categories) - 1]['last'] = true;
  205. }
  206. if (local_user() == $item['uid']) {
  207. $matches = false;
  208. $first = true;
  209. $cnt = preg_match_all('/\[(.*?)\]/', $item['file'], $matches, PREG_SET_ORDER);
  210. if ($cnt) {
  211. foreach ($matches as $mtch) {
  212. $folders[] = [
  213. 'name' => XML::escape(FileTag::decode($mtch[1])),
  214. 'url' => "#",
  215. 'removeurl' => ((local_user() == $item['uid']) ? 'filerm/' . $item['id'] . '?f=&term=' . XML::escape(FileTag::decode($mtch[1])) : ""),
  216. 'first' => $first,
  217. 'last' => false
  218. ];
  219. $first = false;
  220. }
  221. }
  222. }
  223. if (count($folders)) {
  224. $folders[count($folders) - 1]['last'] = true;
  225. }
  226. return [$categories, $folders];
  227. }
  228. /**
  229. * return number of bytes in size (K, M, G)
  230. * @param string $size_str
  231. * @return number
  232. */
  233. function return_bytes($size_str) {
  234. switch (substr ($size_str, -1)) {
  235. case 'M': case 'm': return (int)$size_str * 1048576;
  236. case 'K': case 'k': return (int)$size_str * 1024;
  237. case 'G': case 'g': return (int)$size_str * 1073741824;
  238. default: return $size_str;
  239. }
  240. }
  241. function bb_translate_video($s) {
  242. $matches = null;
  243. $r = preg_match_all("/\[video\](.*?)\[\/video\]/ism",$s,$matches,PREG_SET_ORDER);
  244. if ($r) {
  245. foreach ($matches as $mtch) {
  246. if ((stristr($mtch[1], 'youtube')) || (stristr($mtch[1], 'youtu.be'))) {
  247. $s = str_replace($mtch[0], '[youtube]' . $mtch[1] . '[/youtube]', $s);
  248. } elseif (stristr($mtch[1], 'vimeo')) {
  249. $s = str_replace($mtch[0], '[vimeo]' . $mtch[1] . '[/vimeo]', $s);
  250. }
  251. }
  252. }
  253. return $s;
  254. }
  255. function undo_post_tagging($s) {
  256. $matches = null;
  257. $cnt = preg_match_all('/([!#@])\[url=(.*?)\](.*?)\[\/url\]/ism', $s, $matches, PREG_SET_ORDER);
  258. if ($cnt) {
  259. foreach ($matches as $mtch) {
  260. if (in_array($mtch[1], ['!', '@'])) {
  261. $contact = Contact::getDetailsByURL($mtch[2]);
  262. $mtch[3] = empty($contact['addr']) ? $mtch[2] : $contact['addr'];
  263. }
  264. $s = str_replace($mtch[0], $mtch[1] . $mtch[3],$s);
  265. }
  266. }
  267. return $s;
  268. }
  269. /// @TODO Rewrite this
  270. function is_a_date_arg($s) {
  271. $i = intval($s);
  272. if ($i > 1900) {
  273. $y = date('Y');
  274. if ($i <= $y + 1 && strpos($s, '-') == 4) {
  275. $m = intval(substr($s, 5));
  276. if ($m > 0 && $m <= 12) {
  277. return true;
  278. }
  279. }
  280. }
  281. return false;
  282. }