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.
 
 
 
 
 
 

321 lines
8.2 KiB

  1. <?php
  2. /**
  3. * @file src/Model/FileTag.php
  4. */
  5. namespace Friendica\Model;
  6. use Friendica\Core\L10n;
  7. use Friendica\Database\DBA;
  8. use Friendica\DI;
  9. /**
  10. * @brief This class handles FileTag related functions
  11. *
  12. * post categories and "save to file" use the same item.file table for storage.
  13. * We will differentiate the different uses by wrapping categories in angle brackets
  14. * and save to file categories in square brackets.
  15. * To do this we need to escape these characters if they appear in our tag.
  16. */
  17. class FileTag
  18. {
  19. /**
  20. * @brief URL encode <, >, left and right brackets
  21. *
  22. * @param string $s String to be URL encoded.
  23. *
  24. * @return string The URL encoded string.
  25. */
  26. public static function encode($s)
  27. {
  28. return str_replace(['<', '>', '[', ']'], ['%3c', '%3e', '%5b', '%5d'], $s);
  29. }
  30. /**
  31. * @brief URL decode <, >, left and right brackets
  32. *
  33. * @param string $s The URL encoded string to be decoded
  34. *
  35. * @return string The decoded string.
  36. */
  37. public static function decode($s)
  38. {
  39. return str_replace(['%3c', '%3e', '%5b', '%5d'], ['<', '>', '[', ']'], $s);
  40. }
  41. /**
  42. * @brief Query files for tag
  43. *
  44. * @param string $table The table to be queired.
  45. * @param string $s The search term
  46. * @param string $type Optional file type.
  47. *
  48. * @return string Query string.
  49. */
  50. public static function fileQuery($table, $s, $type = 'file')
  51. {
  52. if ($type == 'file') {
  53. $str = preg_quote('[' . str_replace('%', '%%', self::encode($s)) . ']');
  54. } else {
  55. $str = preg_quote('<' . str_replace('%', '%%', self::encode($s)) . '>');
  56. }
  57. return " AND " . (($table) ? DBA::escape($table) . '.' : '') . "file regexp '" . DBA::escape($str) . "' ";
  58. }
  59. /**
  60. * Get file tags from array
  61. *
  62. * ex. given [music,video] return <music><video> or [music][video]
  63. *
  64. * @param array $array A list of tags.
  65. * @param string $type Optional file type.
  66. *
  67. * @return string A list of file tags.
  68. */
  69. public static function arrayToFile(array $array, string $type = 'file')
  70. {
  71. $tag_list = '';
  72. if ($type == 'file') {
  73. $lbracket = '[';
  74. $rbracket = ']';
  75. } else {
  76. $lbracket = '<';
  77. $rbracket = '>';
  78. }
  79. foreach ($array as $item) {
  80. if (strlen($item)) {
  81. $tag_list .= $lbracket . self::encode(trim($item)) . $rbracket;
  82. }
  83. }
  84. return $tag_list;
  85. }
  86. /**
  87. * Get tag list from file tags
  88. *
  89. * ex. given <music><video>[friends], return [music,video] or [friends]
  90. *
  91. * @param string $file File tags
  92. * @param string $type Optional file type.
  93. *
  94. * @return array List of tag names.
  95. */
  96. public static function fileToArray(string $file, string $type = 'file')
  97. {
  98. $matches = [];
  99. $return = [];
  100. if ($type == 'file') {
  101. $cnt = preg_match_all('/\[(.*?)\]/', $file, $matches, PREG_SET_ORDER);
  102. } else {
  103. $cnt = preg_match_all('/<(.*?)>/', $file, $matches, PREG_SET_ORDER);
  104. }
  105. if ($cnt) {
  106. foreach ($matches as $match) {
  107. $return[] = self::decode($match[1]);
  108. }
  109. }
  110. return $return;
  111. }
  112. /**
  113. * @brief Get file tags from list
  114. *
  115. * ex. given music,video return <music><video> or [music][video]
  116. * @param string $list A comma delimited list of tags.
  117. * @param string $type Optional file type.
  118. *
  119. * @return string A list of file tags.
  120. * @deprecated since 2019.06 use arrayToFile() instead
  121. */
  122. public static function listToFile(string $list, string $type = 'file')
  123. {
  124. $list_array = explode(',', $list);
  125. return self::arrayToFile($list_array, $type);
  126. }
  127. /**
  128. * @brief Get list from file tags
  129. *
  130. * ex. given <music><video>[friends], return music,video or friends
  131. * @param string $file File tags
  132. * @param string $type Optional file type.
  133. *
  134. * @return string Comma delimited list of tag names.
  135. * @deprecated since 2019.06 use fileToArray() instead
  136. */
  137. public static function fileToList(string $file, $type = 'file')
  138. {
  139. return implode(',', self::fileToArray($file, $type));
  140. }
  141. /**
  142. * @brief Update file tags in PConfig
  143. *
  144. * @param int $uid Unique Identity.
  145. * @param string $file_old Categories previously associated with an item
  146. * @param string $file_new New list of categories for an item
  147. * @param string $type Optional file type.
  148. *
  149. * @return boolean A value indicating success or failure.
  150. * @throws \Exception
  151. */
  152. public static function updatePconfig(int $uid, string $file_old, string $file_new, string $type = 'file')
  153. {
  154. if (!intval($uid)) {
  155. return false;
  156. } elseif ($file_old == $file_new) {
  157. return true;
  158. }
  159. $saved = DI::pConfig()->get($uid, 'system', 'filetags');
  160. if (strlen($saved)) {
  161. if ($type == 'file') {
  162. $lbracket = '[';
  163. $rbracket = ']';
  164. $termtype = TERM_FILE;
  165. } else {
  166. $lbracket = '<';
  167. $rbracket = '>';
  168. $termtype = TERM_CATEGORY;
  169. }
  170. $filetags_updated = $saved;
  171. // check for new tags to be added as filetags in pconfig
  172. $new_tags = [];
  173. foreach (self::fileToArray($file_new, $type) as $tag) {
  174. if (!stristr($saved, $lbracket . self::encode($tag) . $rbracket)) {
  175. $new_tags[] = $tag;
  176. }
  177. }
  178. $filetags_updated .= self::arrayToFile($new_tags, $type);
  179. // check for deleted tags to be removed from filetags in pconfig
  180. $deleted_tags = [];
  181. foreach (self::fileToArray($file_old, $type) as $tag) {
  182. if (!stristr($file_new, $lbracket . self::encode($tag) . $rbracket)) {
  183. $deleted_tags[] = $tag;
  184. }
  185. }
  186. foreach ($deleted_tags as $key => $tag) {
  187. $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
  188. DBA::escape($tag),
  189. intval(Term::OBJECT_TYPE_POST),
  190. intval($termtype),
  191. intval($uid));
  192. if (DBA::isResult($r)) {
  193. unset($deleted_tags[$key]);
  194. } else {
  195. $filetags_updated = str_replace($lbracket . self::encode($tag) . $rbracket, '', $filetags_updated);
  196. }
  197. }
  198. if ($saved != $filetags_updated) {
  199. DI::pConfig()->set($uid, 'system', 'filetags', $filetags_updated);
  200. }
  201. return true;
  202. } elseif (strlen($file_new)) {
  203. DI::pConfig()->set($uid, 'system', 'filetags', $file_new);
  204. }
  205. return true;
  206. }
  207. /**
  208. * @brief Add tag to file
  209. *
  210. * @param int $uid Unique identity.
  211. * @param int $item_id Item identity.
  212. * @param string $file File tag.
  213. *
  214. * @return boolean A value indicating success or failure.
  215. * @throws \Friendica\Network\HTTPException\InternalServerErrorException
  216. */
  217. public static function saveFile($uid, $item_id, $file)
  218. {
  219. if (!intval($uid)) {
  220. return false;
  221. }
  222. $item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
  223. if (DBA::isResult($item)) {
  224. if (!stristr($item['file'], '[' . self::encode($file) . ']')) {
  225. $fields = ['file' => $item['file'] . '[' . self::encode($file) . ']'];
  226. Item::update($fields, ['id' => $item_id]);
  227. }
  228. $saved = DI::pConfig()->get($uid, 'system', 'filetags');
  229. if (!strlen($saved) || !stristr($saved, '[' . self::encode($file) . ']')) {
  230. DI::pConfig()->set($uid, 'system', 'filetags', $saved . '[' . self::encode($file) . ']');
  231. }
  232. info(L10n::t('Item filed'));
  233. }
  234. return true;
  235. }
  236. /**
  237. * @brief Remove tag from file
  238. *
  239. * @param int $uid Unique identity.
  240. * @param int $item_id Item identity.
  241. * @param string $file File tag.
  242. * @param boolean $cat Optional value indicating the term type (i.e. Category or File)
  243. *
  244. * @return boolean A value indicating success or failure.
  245. * @throws \Friendica\Network\HTTPException\InternalServerErrorException
  246. */
  247. public static function unsaveFile($uid, $item_id, $file, $cat = false)
  248. {
  249. if (!intval($uid)) {
  250. return false;
  251. }
  252. if ($cat == true) {
  253. $pattern = '<' . self::encode($file) . '>';
  254. $termtype = Term::CATEGORY;
  255. } else {
  256. $pattern = '[' . self::encode($file) . ']';
  257. $termtype = Term::FILE;
  258. }
  259. $item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
  260. if (!DBA::isResult($item)) {
  261. return false;
  262. }
  263. $fields = ['file' => str_replace($pattern, '', $item['file'])];
  264. Item::update($fields, ['id' => $item_id]);
  265. $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
  266. DBA::escape($file),
  267. intval(Term::OBJECT_TYPE_POST),
  268. intval($termtype),
  269. intval($uid)
  270. );
  271. if (!DBA::isResult($r)) {
  272. $saved = DI::pConfig()->get($uid, 'system', 'filetags');
  273. DI::pConfig()->set($uid, 'system', 'filetags', str_replace($pattern, '', $saved));
  274. }
  275. return true;
  276. }
  277. }