PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/tiki-upload_file.php

https://gitlab.com/ElvisAns/tiki
PHP | 317 lines | 264 code | 34 blank | 19 comment | 104 complexity | fea32ac285c7fef99d0f41872541fee8 MD5 | raw file
  1. <?php
  2. /**
  3. * @package tikiwiki
  4. */
  5. // (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
  6. //
  7. // All Rights Reserved. See copyright.txt for details and a complete list of authors.
  8. // Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
  9. // $Id$
  10. use Tiki\TikiInit;
  11. $section = 'file_galleries';
  12. $isUpload = false;
  13. if (isset($_POST['upload'])) {
  14. $isUpload = true;
  15. unset($_POST['upload']);
  16. unset($_GET['upload']);
  17. unset($_REQUEST['upload']);
  18. }
  19. if (isset($_POST['PHPSESSID']) && $_POST['PHPSESSID'] != '') {
  20. session_id($_POST['PHPSESSID']);
  21. }
  22. require_once('tiki-setup.php');
  23. if ($prefs['feature_categories'] == 'y') {
  24. $categlib = TikiLib::lib('categ');
  25. }
  26. $access->check_feature('feature_file_galleries');
  27. $filegallib = TikiLib::lib('filegal');
  28. if ($prefs['feature_groupalert'] == 'y') {
  29. $groupalertlib = TikiLib::lib('groupalert');
  30. }
  31. @ini_set('max_execution_time', 0);
  32. $auto_query_args = ['galleryId', 'fileId', 'filegals_manager', 'view', 'simpleMode', 'insertion_syntax'];
  33. if ($prefs['auth_token_access'] == 'y' && ! empty($token)) {
  34. $smarty->assign('token_id', $token);
  35. }
  36. $requestGalleryId = null;
  37. if (isset($_REQUEST['galleryId']) && ! is_array($_REQUEST['galleryId'])) {
  38. $requestGalleryId = $_REQUEST['galleryId'];
  39. $_REQUEST['galleryId'] = [ $requestGalleryId ];
  40. }
  41. $fileInfo = null;
  42. $fileId = null;
  43. if (! empty($_REQUEST['fileId'])) {
  44. $fileId = $_REQUEST['fileId'];
  45. if (! ($fileInfo = $filegallib->get_file_info($fileId))) {
  46. $smarty->assign('msg', tra("Incorrect param"));
  47. $smarty->display('error.tpl');
  48. die;
  49. }
  50. if (empty($_REQUEST['galleryId'][0])) {
  51. $_REQUEST['galleryId'][0] = $fileInfo['galleryId'];
  52. } elseif ($_REQUEST['galleryId'][0] != $fileInfo['galleryId']) {
  53. $smarty->assign('msg', tra("Could not find the file requested"));
  54. $smarty->display('error.tpl');
  55. die;
  56. }
  57. include_once('lib/mime/mimetypes.php');
  58. global $mimetypes;
  59. asort($mimetypes);
  60. $smarty->assign_by_ref('mimetypes', $mimetypes);
  61. if (! empty($prefs['ocr_enable']) && $prefs['ocr_enable'] === 'y') {
  62. if (! empty($prefs['ocr_file_level']) && $prefs['ocr_file_level'] === 'y') {
  63. if (empty($prefs['ocr_limit_languages'])) {
  64. $ocr = TikiLib::lib('ocr');
  65. $langs = $ocr->getTesseractLangs();
  66. } else {
  67. $langs = $prefs['ocr_limit_languages'];
  68. }
  69. $selectedLangs = json_decode($fileInfo['ocr_lang']);
  70. // lets remove the language codes from the unselected list if they are already selected
  71. foreach ($selectedLangs as $lang) {
  72. unset($langs[array_search($lang, $langs)]);
  73. }
  74. $langLib = TikiLib::lib('language');
  75. if (! empty($selectedLangs)) {
  76. $smarty->assign('selectedLanguages', $langLib->findLanguageNames($selectedLangs));
  77. }
  78. $smarty->assign('languages', $langLib->findLanguageNames($langs));
  79. }
  80. if ($fileInfo['ocr_state']) {
  81. $smarty->assign('ocr_state', true);
  82. }
  83. }
  84. $subGalleries = $filegallib->getSubGalleries();
  85. $gals = [];
  86. foreach ($subGalleries['data'] as $gal) {
  87. $gals[] = [
  88. 'label' => $gal['parentName'] . ' > ' . $gal['name'],
  89. 'id' => $gal['id'],
  90. 'perms' => $gal['perms'],
  91. 'public' => $gal['public'],
  92. 'user' => $gal['user'],
  93. ];
  94. }
  95. sort($gals);
  96. $smarty->assign_by_ref('all_galleries', $gals);
  97. }
  98. if (isset($_REQUEST['galleryId'][0])) {
  99. $gal_info = $filegallib->get_file_gallery((int)$_REQUEST['galleryId'][0]);
  100. if (empty($gal_info)) {
  101. $smarty->assign('msg', tra('Incorrect file gallery'));
  102. $smarty->display('error.tpl');
  103. die;
  104. }
  105. $tikilib->get_perm_object($_REQUEST['galleryId'][0], 'file gallery', $gal_info, true);
  106. $smarty->assign_by_ref('gal_info', $gal_info);
  107. }
  108. if (empty($fileId) && $tiki_p_upload_files != 'y' && $tiki_p_admin_file_galleries != 'y') {
  109. $smarty->assign('errortype', 401);
  110. $smarty->assign('msg', tra("Permission denied"));
  111. $smarty->display('error.tpl');
  112. die;
  113. }
  114. if (isset($_REQUEST['galleryId'][1])) {
  115. foreach ($_REQUEST['galleryId'] as $i => $gal) {
  116. if (! $i) {
  117. continue;
  118. }
  119. // TODO get the good gal_info
  120. $perms = $tikilib->get_perm_object($_REQUEST['galleryId'][$i], 'file gallery', isset($gal_info) ? $gal_info : '', false);
  121. $access->check_permission('tiki_p_upload_files');
  122. }
  123. }
  124. if (! empty($fileId)) {
  125. if (! empty($fileInfo['lockedby']) && $fileInfo['lockedby'] != $user && $tiki_p_admin_file_galleries != 'y') { // if locked must be the locker
  126. $smarty->assign('msg', tra(sprintf('The file has been locked by %s', $fileInfo['lockedby'])));
  127. $smarty->display('error.tpl');
  128. die;
  129. }
  130. if (! ((! empty($user) && ($user == $fileInfo['user'] || $user == $fileInfo['lockedby'])) || $tiki_p_edit_gallery_file == 'y')) { // must be the owner or the locker or have the perms
  131. $smarty->assign('errortype', 401);
  132. $smarty->assign('msg', tra("You do not have permission to edit this file"));
  133. $smarty->display('error.tpl');
  134. die;
  135. }
  136. if ($gal_info['backlinkPerms'] == 'y' && $filegallib->hasOnlyPrivateBacklinks($fileId)) {
  137. $smarty->assign('errortype', 401);
  138. $smarty->assign('msg', tra("You do not have permission to edit this file"));
  139. $smarty->display('error.tpl');
  140. die;
  141. }
  142. if (isset($_REQUEST['lockedby']) && $fileInfo['lockedby'] != $_REQUEST['lockedby']) {
  143. if (empty($fileInfo['lockedby'])) {
  144. $smarty->assign('msg', tra(sprintf('The file has been unlocked meanwhile')));
  145. } else {
  146. $smarty->assign('msg', tra(sprintf('The file has been locked by %s', $fileInfo['lockedby'])));
  147. }
  148. $smarty->display('error.tpl');
  149. die;
  150. }
  151. if ($gal_info['lockable'] == 'y' && empty($fileInfo['lockedby']) && $tiki_p_admin_file_galleries != 'y') {
  152. $smarty->assign('msg', tra('You must lock the file before editing it'));
  153. $smarty->display('error.tpl');
  154. die;
  155. }
  156. }
  157. $smarty->assign('show', 'n');
  158. if (! empty($_REQUEST['galleryId'][0]) && $prefs['feature_groupalert'] == 'y') {
  159. $groupforalert = $groupalertlib->GetGroup('file gallery', (int)$_REQUEST['galleryId'][0]);
  160. if ($groupforalert != '') {
  161. $showeachuser = $groupalertlib->GetShowEachUser('file gallery', (int)$_REQUEST['galleryId'][0], $groupforalert);
  162. $listusertoalert = $userlib->get_users(0, -1, 'login_asc', '', '', false, $groupforalert, '');
  163. $smarty->assign_by_ref('listusertoalert', $listusertoalert['data']);
  164. }
  165. $smarty->assign_by_ref('groupforalert', $groupforalert);
  166. $smarty->assign_by_ref('showeachuser', $showeachuser);
  167. }
  168. if (empty($_REQUEST['returnUrl'])) {
  169. include('lib/filegals/max_upload_size.php');
  170. }
  171. // Process an upload here
  172. if ($isUpload) {
  173. $shortLivedTokens = ($prefs['site_short_lived_csrf_tokens'] ?? 'n') === 'y';
  174. // multiple form submissions are possible but the same ticket is in each form if JS is enabled,
  175. // so save ticket info from first submission
  176. if ($shortLivedTokens && (int) $_POST['submission'] === 1 && ! empty($_POST['totalSubmissions']) && (int) $_POST['totalSubmissions'] > 1) {
  177. $_SESSION['tickets']['repeatTicket']['ticket'] = $_POST['ticket'];
  178. $_SESSION['tickets']['repeatTicket']['time'] = $_SESSION['tickets'][$_POST['ticket']];
  179. }
  180. if (
  181. ((! $shortLivedTokens || (int) $_POST['submission'] === 1) && $access->checkCsrf())
  182. // for subsequent submissions check ticket against saved ticket info from first submission
  183. || ((int) $_POST['submission'] > 1
  184. && (int) $_POST['submission'] <= (int) $_POST['totalSubmissions']
  185. // check that posted ticket matches saved ticket from first submission
  186. && $_POST['ticket'] === $_SESSION['tickets']['repeatTicket']['ticket']
  187. // check that the ticket from the first submission hasn't expired
  188. && ! empty($_SESSION['tickets']['repeatTicket']['time'])
  189. && $_SESSION['tickets']['repeatTicket']['time'] < time()
  190. && $_SESSION['tickets']['repeatTicket']['time'] > time()
  191. - $prefs['site_security_timeout']
  192. )
  193. ) {
  194. if ($shortLivedTokens && ! empty($_POST['totalSubmissions']) && (int) $_POST['submission'] === (int) $_POST['totalSubmissions']) {
  195. unset($_SESSION['tickets']['repeatTicket']);
  196. }
  197. $optionalRequestParams = [
  198. 'fileId',
  199. 'parentGalleryId',
  200. 'name',
  201. 'user',
  202. 'description',
  203. 'author',
  204. 'comment',
  205. 'returnUrl',
  206. 'isbatch',
  207. 'deleteAfter',
  208. 'deleteAfter_unit',
  209. 'hit_limit',
  210. 'listtoalert',
  211. 'insertion_syntax',
  212. 'filetype',
  213. 'imagesize',
  214. 'image_max_size_x',
  215. 'image_max_size_y',
  216. 'ocr_state',
  217. 'ocr_lang'
  218. ];
  219. $uploadParams = [
  220. 'fileInfo' => $fileInfo,
  221. 'galleryId' => $_REQUEST['galleryId'],
  222. ];
  223. foreach ($optionalRequestParams as $p) {
  224. if (isset($_REQUEST[ $p ])) {
  225. if ($p === 'parentGalleryId') {
  226. $uploadParams[ 'galleryId' ] = [$_REQUEST[ $p ]]; // new parent gallery for file from edit properties form
  227. } else {
  228. $uploadParams[ $p ] = $_REQUEST[ $p ];
  229. }
  230. }
  231. }
  232. if (! empty($prefs['ocr_enable']) && $prefs['ocr_enable'] === 'y' && empty($_POST['ocr_state'][0])) {
  233. $uploadParams['ocr_state'][0] = null;
  234. }
  235. if ($fileInfo = $filegallib->actionHandler('uploadFile', $uploadParams)) {
  236. $fileId = $fileInfo['fileId'];
  237. }
  238. }
  239. }
  240. $fileparts = pathinfo($fileInfo['filename']);
  241. $fileInfo['extension'] = isset($fileparts['extension']) ? $fileparts['extension'] : '';
  242. $smarty->assign_by_ref('fileInfo', $fileInfo);
  243. $smarty->assign('editFileId', (int) $fileId);
  244. // Get the list of galleries to display the select box in the template
  245. $smarty->assign('galleryId', empty($_REQUEST['galleryId'][0]) ? '' : $_REQUEST['galleryId'][0]);
  246. if (empty($fileId)) {
  247. if (isset($gal_info['type']) && $gal_info['type'] == 'user') {
  248. $galleries = $filegallib->getSubGalleries($requestGalleryId, true, 'userfiles');
  249. } else {
  250. $galleries = $filegallib->getSubGalleries($requestGalleryId, true, 'upload_files');
  251. }
  252. $smarty->assign_by_ref('galleries', $galleries["data"]);
  253. $smarty->assign('treeRootId', $galleries['parentId']);
  254. }
  255. if ($prefs['fgal_limit_hits_per_file'] == 'y') {
  256. $smarty->assign('hit_limit', $filegallib->get_download_limit($fileId));
  257. }
  258. if (! empty($fileInfo['fileId'])) {
  259. $smarty->assign('metarray', $filegallib->metadataAction($fileInfo['fileId']), 'get_array');
  260. }
  261. $is_iis = TikiInit::isIIS();
  262. $smarty->assign('is_iis', $is_iis);
  263. $cat_type = 'file';
  264. $cat_objid = (int) $fileId;
  265. include_once('categorize_list.php');
  266. include_once('tiki-section_options.php');
  267. // disallow robots to index page:
  268. $smarty->assign('metatag_robots', 'NOINDEX, NOFOLLOW');
  269. $smarty->assign('category_jail', TikiLib::lib('tiki')->get_jail(false));
  270. // Display the template
  271. if ($prefs['javascript_enabled'] != 'y' or ! $isUpload || ! empty($_REQUEST['fileId'])) {
  272. if ($prefs['file_galleries_use_jquery_upload'] !== 'y') {
  273. $headerlib->add_jsfile('vendor_bundled/vendor/jquery-form/form/jquery.form.js');
  274. }
  275. $smarty->assign('mid', 'tiki-upload_file.tpl');
  276. if (! empty($_REQUEST['filegals_manager'])) {
  277. $smarty->assign('filegals_manager', $_REQUEST['filegals_manager']);
  278. $smarty->assign('insertion_syntax', $jitRequest->insertion_syntax->text());
  279. $smarty->display("tiki_full.tpl");
  280. } else {
  281. $smarty->display("tiki.tpl");
  282. }
  283. }