PageRenderTime 47ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/wiki-plugins/wikiplugin_attach.php

https://gitlab.com/ElvisAns/tiki
PHP | 360 lines | 314 code | 37 blank | 9 comment | 60 complexity | 37c805f59487f1a25ae116e8349a235c MD5 | raw file
  1. <?php
  2. // (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
  3. //
  4. // All Rights Reserved. See copyright.txt for details and a complete list of authors.
  5. // Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
  6. // $Id$
  7. function wikiplugin_attach_info()
  8. {
  9. return [
  10. 'name' => tra('Attachment'),
  11. 'documentation' => 'PluginAttach',
  12. 'description' => tra('Display an attachment or a list of them'),
  13. 'prefs' => [ 'feature_wiki_attachments', 'wikiplugin_attach' ],
  14. 'body' => tra("Comment"),
  15. 'iconname' => 'attach',
  16. 'introduced' => 1,
  17. 'params' => [
  18. 'name' => [
  19. 'required' => false,
  20. 'name' => tra('Name'),
  21. 'description' => tra('File name of the attached file to link to. Either name, file, id or num can be
  22. used to identify a single attachment'),
  23. 'since' => '1',
  24. 'filter' => 'text',
  25. 'default' => '',
  26. ],
  27. 'file' => [
  28. 'required' => false,
  29. 'name' => tra('File'),
  30. 'description' => tra('Same as name'),
  31. 'since' => '1',
  32. 'filter' => 'text',
  33. 'default' => '',
  34. ],
  35. 'page' => [
  36. 'required' => false,
  37. 'name' => tra('Page'),
  38. 'description' => tra('Name of the wiki page the file is attached to. If left empty when the plugin is
  39. used on a wiki page, this defaults to that wiki page.'),
  40. 'since' => '1',
  41. 'filter' => 'pagename',
  42. 'default' => '',
  43. 'profile_reference' => 'wiki_page',
  44. ],
  45. 'showdesc' => [
  46. 'required' => false,
  47. 'name' => tra('Show Description'),
  48. 'description' => tra('Shows the description as the link text instead of the file name (not used by default)'),
  49. 'since' => '1',
  50. 'filter' => 'digits',
  51. 'default' => 0,
  52. 'options' => [
  53. ['text' => '', 'value' => ''],
  54. ['text' => tra('Yes'), 'value' => 1],
  55. ['text' => tra('No'), 'value' => 0],
  56. ],
  57. ],
  58. 'bullets' => [
  59. 'required' => false,
  60. 'name' => tra('Bullets'),
  61. 'description' => tra('Makes the list of attachments a bulleted list (not set by default)'),
  62. 'since' => '1',
  63. 'filter' => 'digits',
  64. 'default' => 0,
  65. 'options' => [
  66. ['text' => '', 'value' => ''],
  67. ['text' => tra('Yes'), 'value' => 1],
  68. ['text' => tra('No'), 'value' => 0],
  69. ],
  70. ],
  71. 'image' => [
  72. 'required' => false,
  73. 'name' => tra('Image'),
  74. 'description' => tr('Indicates that this file is an image, and should be displayed inline using the
  75. %0 tag (not set by default)', '<code>img</code>'),
  76. 'since' => '1',
  77. 'filter' => 'digits',
  78. 'default' => 0,
  79. 'options' => [
  80. ['text' => '', 'value' => ''],
  81. ['text' => tra('Yes'), 'value' => 1],
  82. ['text' => tra('No'), 'value' => 0],
  83. ],
  84. ],
  85. 'inline' => [
  86. 'required' => false,
  87. 'name' => tra('Custom Label'),
  88. 'description' => tr('Makes the text between the %0 tags the link text instead of the file name
  89. or description. Only the first attachment will be listed.', '<code>{ATTACH}</code>'),
  90. 'since' => '1',
  91. 'filter' => 'digits',
  92. 'default' => 0,
  93. 'options' => [
  94. ['text' => '', 'value' => ''],
  95. ['text' => tra('Yes'), 'value' => 1],
  96. ['text' => tra('No'), 'value' => 0],
  97. ],
  98. ],
  99. 'all' => [
  100. 'required' => false,
  101. 'name' => tra('All'),
  102. 'description' => tr('Lists links to all attachments for the entire tiki site together with pages they
  103. are attached to when set to %0 (Yes)', '<code>1</code>'),
  104. 'since' => '1',
  105. 'filter' => 'digits',
  106. 'default' => 0,
  107. 'options' => [
  108. ['text' => '', 'value' => ''],
  109. ['text' => tra('Yes'), 'value' => 1],
  110. ['text' => tra('No'), 'value' => 0],
  111. ],
  112. ],
  113. 'num' => [
  114. 'required' => false,
  115. 'name' => tra('Order Number'),
  116. 'description' => tra('Identifies the attachment to link to by the order of the attachment in the list
  117. of attachments to a page instead of by file name or ID. Either name, file, id or num can be used to
  118. identify a single attachment.'),
  119. 'since' => '1',
  120. 'default' => '',
  121. ],
  122. 'id' => [
  123. 'required' => false,
  124. 'name' => tra('ID'),
  125. 'description' => tra('Identifies the attachment to link to by id number instead of by file name or order
  126. number. Either name, file, id or num can be used to identify a single attachment.'),
  127. 'since' => '1',
  128. 'accepted' => tra('Attachment name, file, id or num'),
  129. 'filter' => 'text',
  130. ],
  131. 'dls' => [
  132. 'required' => false,
  133. 'name' => tra('Downloads'),
  134. 'description' => tr('The alt text that pops up on mouseover will include the number of downloads of the
  135. attachment at the end when set to %0 (Yes)', '<code>1</code>'),
  136. 'since' => '1',
  137. 'filter' => 'digits',
  138. 'default' => 0,
  139. 'options' => [
  140. ['text' => '', 'value' => ''],
  141. ['text' => tra('Yes'), 'value' => 1],
  142. ['text' => tra('No'), 'value' => 0],
  143. ],
  144. ],
  145. 'icon' => [
  146. 'required' => false,
  147. 'name' => tra('File Type Icon'),
  148. 'description' => tr('A file type icon is displayed in front of the attachment link when this is set to
  149. %0 (Yes)', '<code>1</code>'),
  150. 'since' => '1',
  151. 'filter' => 'digits',
  152. 'default' => 0,
  153. 'options' => [
  154. ['text' => '', 'value' => ''],
  155. ['text' => tra('Yes'), 'value' => 1],
  156. ['text' => tra('No'), 'value' => 0],
  157. ],
  158. ],
  159. ],
  160. ];
  161. }
  162. function wikiplugin_attach($data, $params)
  163. {
  164. global $atts;
  165. global $mimeextensions;
  166. global $user, $section, $section_class;
  167. $wikilib = TikiLib::lib('wiki');
  168. $tikilib = TikiLib::lib('tiki');
  169. extract($params, EXTR_SKIP);
  170. $loop = [];
  171. if (! isset($atts)) {
  172. $atts = [];
  173. }
  174. if (! is_array($atts) || ! array_key_exists("data", $atts) || count($atts["data"]) < 1) {
  175. // We're being called from a preview or something; try to build the atts ourselves.
  176. // See if we're being called from a tracker page.
  177. if ($section == 'trackers') {
  178. $trklib = TikiLib::lib('trk');
  179. $atts_item_name = $_REQUEST["itemId"];
  180. $tracker_info = $trklib->get_tracker($atts_item_name);
  181. $tracker_info = array_merge($tracker_info, $trklib->get_tracker_options($atts_item_name));
  182. $attextra = 'n';
  183. if (strstr($tracker_info["orderAttachments"], '|')) {
  184. $attextra = 'y';
  185. }
  186. $attfields = explode(',', strtok($tracker_info["orderAttachments"], '|'));
  187. $atts = $trklib->list_item_attachments($atts_item_name, 0, -1, 'comment_asc', '');
  188. }
  189. // See if we're being called from a wiki page.
  190. if (strstr($section_class, 'wiki_page')) {
  191. $atts_item_name = $_REQUEST["page"];
  192. $atts = $wikilib->list_wiki_attachments($atts_item_name, 0, -1, 'created_desc', '');
  193. }
  194. }
  195. // Save for restoration before this script ends
  196. $old_atts = $atts;
  197. $url = '';
  198. if (isset($all) && (int)$all > 0) {
  199. $atts = $wikilib->list_all_attachments(0, -1, 'page_asc', '');
  200. } elseif (! empty($page)) {
  201. if (! $tikilib->page_exists($page)) {
  202. return "''" . tr('Page "%0" does not exist', $page) . "''";
  203. }
  204. if ($tikilib->user_has_perm_on_object($user, $page, 'wiki page', 'tiki_p_wiki_view_attachments') || $tikilib->user_has_perm_on_object($user, $_REQUEST['page'], 'wiki page', 'tiki_p_wiki_admin_attachments')) {
  205. $atts = $wikilib->list_wiki_attachments($page, 0, -1, 'created_desc', '');
  206. $url = "&amp;page=$page";
  207. }
  208. }
  209. if (! array_key_exists("cant", $atts)) {
  210. if (array_key_exists("data", $atts)) {
  211. $atts['cant'] = count($atts["data"]);
  212. } else {
  213. $atts['cant'] = 0;
  214. $atts["data"] = "";
  215. }
  216. }
  217. if (! isset($num)) {
  218. $num = 0;
  219. }
  220. if (! isset($id)) {
  221. $id = 0;
  222. } else {
  223. $num = 0;
  224. }
  225. if (isset($file)) {
  226. $name = $file;
  227. }
  228. if (isset($name)) {
  229. $id = 0;
  230. $num = 0;
  231. } else {
  232. $name = '';
  233. }
  234. if (! $atts['cant']) {
  235. return "''" . tra('No such attachment on this page') . "''";
  236. } elseif ($num > 0 and $num < ($atts['cant'] + 1)) {
  237. $loop[] = $num;
  238. } else {
  239. $loop = range(1, $atts['cant']);
  240. }
  241. $out = [];
  242. if ($data) {
  243. $out[] = $data;
  244. }
  245. foreach ($loop as $n) {
  246. $n--;
  247. if ((! $name and ! $id) or $id == $atts['data'][$n]['attId'] or $name == $atts['data'][$n]['filename']) {
  248. $link = "";
  249. if (isset($bullets) && $bullets) {
  250. $link .= "<li>";
  251. }
  252. if (isset($image) and $image) {
  253. $link .= '<img src="tiki-download_wiki_attachment.php?attId=' . $atts['data'][$n]['attId'] . $url . '" class="wiki"';
  254. $link .= ' alt="';
  255. if (empty($showdesc) || empty($atts['data'][$n]['comment'])) {
  256. $link .= $atts['data'][$n]['filename'];
  257. } else {
  258. $link .= $atts['data'][$n]['comment'];
  259. }
  260. if (isset($dls)) {
  261. $link .= " " . $atts['data'][$n]['hits'];
  262. }
  263. $link .= '"/>';
  264. } else {
  265. $link .= '<a href="tiki-download_wiki_attachment.php?attId=' . $atts['data'][$n]['attId'] . $url . '&amp;download=y" class="wiki"';
  266. $link .= ' title="';
  267. if (empty($showdesc) || empty($atts['data'][$n]['comment'])) {
  268. $link .= $atts['data'][$n]['filename'];
  269. } else {
  270. $link .= $atts['data'][$n]['comment'];
  271. }
  272. if (isset($dls)) {
  273. $link .= " " . $atts['data'][$n]['hits'];
  274. }
  275. $link .= '">';
  276. if (isset($icon)) {
  277. $smarty = TikiLib::lib('smarty');
  278. $smarty->loadPlugin('smarty_modifier_iconify');
  279. $iconhtml = smarty_modifier_iconify($atts['data'][$n]['filename']);
  280. $link .= $iconhtml . '&nbsp';
  281. }
  282. if (! empty($showdesc) && ! empty($atts['data'][$n]['comment'])) {
  283. $link .= strip_tags($atts['data'][$n]['comment']);
  284. } elseif (! empty($inline) && ! empty($data)) {
  285. $link .= $data;
  286. } else {
  287. $link .= strip_tags($atts['data'][$n]['filename']);
  288. }
  289. $link .= '</a>';
  290. $pageall = strip_tags($atts['data'][$n]['page']);
  291. if (isset($all)) {
  292. $link .= " attached to " . '<a title="' . $pageall . '" href="' . $pageall . '" class="wiki">' . $pageall . '</a>';
  293. }
  294. }
  295. if (isset($bullets) && $bullets) {
  296. $link .= "</li>";
  297. }
  298. $out[] = $link;
  299. }
  300. }
  301. if (isset($bullets) && $bullets) {
  302. $separator = "\n";
  303. } else {
  304. $separator = "<br />\n";
  305. }
  306. if (! empty($inline) && ! empty($data)) {
  307. if (array_key_exists(1, $out)) {
  308. $data = $out[1];
  309. } else {
  310. $data = "";
  311. }
  312. } else {
  313. $data = implode($separator, $out);
  314. }
  315. if (isset($bullets) && $bullets) {
  316. $data = "<ul>" . $data . "</ul>";
  317. }
  318. if (strlen($data) == 0) {
  319. $data = "<strong>" . tra('No such attachment on this page') . "</strong>";
  320. }
  321. $atts = $old_atts;
  322. return '~np~' . $data . '~/np~';
  323. }