ManageAttachments.php 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835
  1. <?php
  2. /**
  3. * Simple Machines Forum (SMF)
  4. *
  5. * @package SMF
  6. * @author Simple Machines http://www.simplemachines.org
  7. * @copyright 2011 Simple Machines
  8. * @license http://www.simplemachines.org/about/smf/license.php BSD
  9. *
  10. * @version 2.0
  11. */
  12. if (!defined('SMF'))
  13. die('Hacking attempt...');
  14. /* /!!!
  15. void ManageAttachments()
  16. - main 'Attachments and Avatars' center function.
  17. - entry point for index.php?action=admin;area=manageattachments.
  18. - requires the manage_attachments permission.
  19. - load the ManageAttachments template.
  20. - uses the Admin language file.
  21. - uses the template layer 'manage_files' for showing the tab bar.
  22. - calls a function based on the sub-action.
  23. void ManageAttachmentSettings()
  24. - show/change attachment settings.
  25. - default sub action for the 'Attachments and Avatars' center.
  26. - uses the 'attachments' sub template.
  27. - called by index.php?action=admin;area=manageattachments;sa=attachements.
  28. void ManageAvatarSettings()
  29. - show/change avatar settings.
  30. - called by index.php?action=admin;area=manageattachments;sa=avatars.
  31. - uses the 'avatars' sub template.
  32. - show/set permissions for permissions: 'profile_server_avatar',
  33. 'profile_upload_avatar' and 'profile_remote_avatar'.
  34. void BrowseFiles()
  35. - show a list of attachment or avatar files.
  36. - called by ?action=admin;area=manageattachments;sa=browse for attachments and
  37. ?action=admin;area=manageattachments;sa=browse;avatars for avatars.
  38. - uses the 'browse' sub template
  39. - allows sorting by name, date, size and member.
  40. - paginates results.
  41. void MaintainFiles()
  42. - show several file maintenance options.
  43. - called by ?action=admin;area=manageattachments;sa=maintain.
  44. - uses the 'maintain' sub template.
  45. - calculates file statistics (total file size, number of attachments,
  46. number of avatars, attachment space available).
  47. void MoveAvatars()
  48. - move avatars from or to the attachment directory.
  49. - called from the maintenance screen by
  50. ?action=admin;area=manageattachments;sa=moveAvatars.
  51. void RemoveAttachmentByAge()
  52. - remove attachments older than a given age.
  53. - called from the maintenance screen by
  54. ?action=admin;area=manageattachments;sa=byAge.
  55. - optionally adds a certain text to the messages the attachments were
  56. removed from.
  57. void RemoveAttachmentBySize()
  58. - remove attachments larger than a given size.
  59. - called from the maintenance screen by
  60. ?action=admin;area=manageattachments;sa=bySize.
  61. - optionally adds a certain text to the messages the attachments were
  62. removed from.
  63. void RemoveAttachment()
  64. - remove a selection of attachments or avatars.
  65. - called from the browse screen as submitted form by
  66. ?action=admin;area=manageattachments;sa=remove
  67. void RemoveAllAttachments()
  68. - removes all attachments in a single click
  69. - called from the maintenance screen by
  70. ?action=admin;area=manageattachments;sa=removeall.
  71. array removeAttachments(array condition, string query_type = '', bool return_affected_messages = false, bool autoThumbRemoval = true)
  72. - removes attachments or avatars based on a given query condition.
  73. - called by several remove avatar/attachment functions in this file.
  74. - removes attachments based that match the $condition.
  75. - allows query_types 'messages' and 'members', whichever is need by the
  76. $condition parameter.
  77. void RepairAttachments()
  78. // !!!
  79. void PauseAttachmentMaintenance()
  80. // !!!
  81. void ApproveAttach()
  82. // !!!
  83. void ApproveAttachments()
  84. // !!!
  85. */
  86. // The main attachment management function.
  87. function ManageAttachments()
  88. {
  89. global $txt, $modSettings, $scripturl, $context, $options;
  90. // You have to be able to moderate the forum to do this.
  91. isAllowedTo('manage_attachments');
  92. // Setup the template stuff we'll probably need.
  93. loadTemplate('ManageAttachments');
  94. // If they want to delete attachment(s), delete them. (otherwise fall through..)
  95. $subActions = array(
  96. 'attachments' => 'ManageAttachmentSettings',
  97. 'attachpaths' => 'ManageAttachmentPaths',
  98. 'avatars' => 'ManageAvatarSettings',
  99. 'browse' => 'BrowseFiles',
  100. 'byAge' => 'RemoveAttachmentByAge',
  101. 'bySize' => 'RemoveAttachmentBySize',
  102. 'maintenance' => 'MaintainFiles',
  103. 'moveAvatars' => 'MoveAvatars',
  104. 'repair' => 'RepairAttachments',
  105. 'remove' => 'RemoveAttachment',
  106. 'removeall' => 'RemoveAllAttachments'
  107. );
  108. // Pick the correct sub-action.
  109. if (isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]))
  110. $context['sub_action'] = $_REQUEST['sa'];
  111. else
  112. $context['sub_action'] = 'browse';
  113. // Default page title is good.
  114. $context['page_title'] = $txt['attachments_avatars'];
  115. // This uses admin tabs - as it should!
  116. $context[$context['admin_menu_name']]['tab_data'] = array(
  117. 'title' => $txt['attachments_avatars'],
  118. 'help' => 'manage_files',
  119. 'description' => $txt['attachments_desc'],
  120. );
  121. // Finally fall through to what we are doing.
  122. $subActions[$context['sub_action']]();
  123. }
  124. function ManageAttachmentSettings($return_config = false)
  125. {
  126. global $txt, $modSettings, $scripturl, $context, $options, $sourcedir;
  127. $context['valid_upload_dir'] = is_dir($modSettings['attachmentUploadDir']) && is_writable($modSettings['attachmentUploadDir']);
  128. // Perform a test to see if the GD module is installed.
  129. $testGD = get_extension_funcs('gd');
  130. $config_vars = array(
  131. array('title', 'attachment_manager_settings'),
  132. // Are attachments enabled?
  133. array('select', 'attachmentEnable', array($txt['attachmentEnable_deactivate'], $txt['attachmentEnable_enable_all'], $txt['attachmentEnable_disable_new'])),
  134. '',
  135. // Extension checks etc.
  136. array('check', 'attachmentCheckExtensions'),
  137. array('text', 'attachmentExtensions', 40),
  138. array('check', 'attachmentRecodeLineEndings'),
  139. '',
  140. // Directory and size limits.
  141. empty($modSettings['currentAttachmentUploadDir']) ? array('text', 'attachmentUploadDir', 40, 'invalid' => !$context['valid_upload_dir']) : array('var_message', 'attachmentUploadDir_multiple', 'message' => 'attachmentUploadDir_multiple_configure'),
  142. array('text', 'attachmentDirSizeLimit', 6, 'postinput' => $txt['kilobyte']),
  143. array('text', 'attachmentPostLimit', 6, 'postinput' => $txt['kilobyte']),
  144. array('text', 'attachmentSizeLimit', 6, 'postinput' => $txt['kilobyte']),
  145. array('text', 'attachmentNumPerPostLimit', 6),
  146. '',
  147. // Image settings.
  148. array('warning', empty($testGD) ? 'attachment_gd_warning' : ''),
  149. array('check', 'attachment_image_reencode'),
  150. '',
  151. array('warning', 'attachment_image_paranoid_warning'),
  152. array('check', 'attachment_image_paranoid'),
  153. '',
  154. // Thumbnail settings.
  155. array('check', 'attachmentShowImages'),
  156. array('check', 'attachmentThumbnails'),
  157. array('check', 'attachment_thumb_png'),
  158. array('text', 'attachmentThumbWidth', 6),
  159. array('text', 'attachmentThumbHeight', 6),
  160. );
  161. if ($return_config)
  162. return $config_vars;
  163. // These are very likely to come in handy! (i.e. without them we're doomed!)
  164. require_once($sourcedir . '/ManagePermissions.php');
  165. require_once($sourcedir . '/ManageServer.php');
  166. // Saving settings?
  167. if (isset($_GET['save']))
  168. {
  169. checkSession();
  170. saveDBSettings($config_vars);
  171. redirectexit('action=admin;area=manageattachments;sa=attachments');
  172. }
  173. $context['post_url'] = $scripturl . '?action=admin;area=manageattachments;save;sa=attachments';
  174. prepareDBSettingContext($config_vars);
  175. $context['sub_template'] = 'show_settings';
  176. }
  177. function ManageAvatarSettings($return_config = false)
  178. {
  179. global $txt, $context, $modSettings, $sourcedir, $scripturl;
  180. // Perform a test to see if the GD module is installed.
  181. $testGD = get_extension_funcs('gd');
  182. $context['valid_avatar_dir'] = is_dir($modSettings['avatar_directory']);
  183. $context['valid_custom_avatar_dir'] = empty($modSettings['custom_avatar_enabled']) || (!empty($modSettings['custom_avatar_dir']) && is_dir($modSettings['custom_avatar_dir']) && is_writable($modSettings['custom_avatar_dir']));
  184. $config_vars = array(
  185. // Server stored avatars!
  186. array('title', 'avatar_server_stored'),
  187. array('warning', empty($testGD) ? 'avatar_gd_warning' : ''),
  188. array('permissions', 'profile_server_avatar', 0, $txt['avatar_server_stored_groups']),
  189. array('text', 'avatar_directory', 40, 'invalid' => !$context['valid_avatar_dir']),
  190. array('text', 'avatar_url', 40),
  191. // External avatars?
  192. array('title', 'avatar_external'),
  193. array('permissions', 'profile_remote_avatar', 0, $txt['avatar_external_url_groups']),
  194. array('check', 'avatar_download_external', 0, 'onchange' => 'fUpdateStatus();'),
  195. array('text', 'avatar_max_width_external', 6),
  196. array('text', 'avatar_max_height_external', 6),
  197. array('select', 'avatar_action_too_large',
  198. array(
  199. 'option_refuse' => $txt['option_refuse'],
  200. 'option_html_resize' => $txt['option_html_resize'],
  201. 'option_js_resize' => $txt['option_js_resize'],
  202. 'option_download_and_resize' => $txt['option_download_and_resize'],
  203. ),
  204. ),
  205. // Uploadable avatars?
  206. array('title', 'avatar_upload'),
  207. array('permissions', 'profile_upload_avatar', 0, $txt['avatar_upload_groups']),
  208. array('text', 'avatar_max_width_upload', 6),
  209. array('text', 'avatar_max_height_upload', 6),
  210. array('check', 'avatar_resize_upload', 'subtext' => $txt['avatar_resize_upload_note']),
  211. array('check', 'avatar_reencode'),
  212. '',
  213. array('warning', 'avatar_paranoid_warning'),
  214. array('check', 'avatar_paranoid'),
  215. '',
  216. array('check', 'avatar_download_png'),
  217. array('select', 'custom_avatar_enabled', array($txt['option_attachment_dir'], $txt['option_specified_dir']), 'onchange' => 'fUpdateStatus();'),
  218. array('text', 'custom_avatar_dir', 40, 'subtext' => $txt['custom_avatar_dir_desc'], 'invalid' => !$context['valid_custom_avatar_dir']),
  219. array('text', 'custom_avatar_url', 40),
  220. );
  221. if ($return_config)
  222. return $config_vars;
  223. // We need these files for the inline permission settings, and the settings template.
  224. require_once($sourcedir . '/ManagePermissions.php');
  225. require_once($sourcedir . '/ManageServer.php');
  226. // Saving avatar settings?
  227. if (isset($_GET['save']))
  228. {
  229. checkSession();
  230. // Just incase the admin forgot to set both custom avatar values, we disable it to prevent errors.
  231. if (isset($_POST['custom_avatar_enabled']) && $_POST['custom_avatar_enabled'] == 1 && (empty($_POST['custom_avatar_dir']) || empty($_POST['custom_avatar_url'])))
  232. $_POST['custom_avatar_enabled'] = 0;
  233. saveDBSettings($config_vars);
  234. redirectexit('action=admin;area=manageattachments;sa=avatars');
  235. }
  236. // Attempt to figure out if the admin is trying to break things.
  237. $context['settings_save_onclick'] = 'return document.getElementById(\'custom_avatar_enabled\').value == 1 && (document.getElementById(\'custom_avatar_dir\').value == \'\' || document.getElementById(\'custom_avatar_url\').value == \'\') ? confirm(\'' . $txt['custom_avatar_check_empty'] . '\') : true;';
  238. // Prepare the context.
  239. $context['post_url'] = $scripturl . '?action=admin;area=manageattachments;save;sa=avatars';
  240. prepareDBSettingContext($config_vars);
  241. // Add a layer for the javascript.
  242. $context['template_layers'][] = 'avatar_settings';
  243. $context['sub_template'] = 'show_settings';
  244. }
  245. function BrowseFiles()
  246. {
  247. global $context, $txt, $scripturl, $options, $modSettings;
  248. global $smcFunc, $sourcedir;
  249. $context['sub_template'] = 'browse';
  250. // Attachments or avatars?
  251. $context['browse_type'] = isset($_REQUEST['avatars']) ? 'avatars' : (isset($_REQUEST['thumbs']) ? 'thumbs' : 'attachments');
  252. // Set the options for the list component.
  253. $listOptions = array(
  254. 'id' => 'file_list',
  255. 'title' => $txt['attachment_manager_' . ($context['browse_type'] === 'avatars' ? 'avatars' : ( $context['browse_type'] === 'thumbs' ? 'thumbs' : 'attachments'))],
  256. 'items_per_page' => $modSettings['defaultMaxMessages'],
  257. 'base_href' => $scripturl . '?action=admin;area=manageattachments;sa=browse' . ($context['browse_type'] === 'avatars' ? ';avatars' : ($context['browse_type'] === 'thumbs' ? ';thumbs' : '')),
  258. 'default_sort_col' => 'name',
  259. 'no_items_label' => $txt['attachment_manager_' . ($context['browse_type'] === 'avatars' ? 'avatars' : ( $context['browse_type'] === 'thumbs' ? 'thumbs' : 'attachments')) . '_no_entries'],
  260. 'get_items' => array(
  261. 'function' => 'list_getFiles',
  262. 'params' => array(
  263. $context['browse_type'],
  264. ),
  265. ),
  266. 'get_count' => array(
  267. 'function' => 'list_getNumFiles',
  268. 'params' => array(
  269. $context['browse_type'],
  270. ),
  271. ),
  272. 'columns' => array(
  273. 'name' => array(
  274. 'header' => array(
  275. 'value' => $txt['attachment_name'],
  276. ),
  277. 'data' => array(
  278. 'function' => create_function('$rowData', '
  279. global $modSettings, $context, $scripturl;
  280. $link = \'<a href="\';
  281. // In case of a custom avatar URL attachments have a fixed directory.
  282. if ($rowData[\'attachment_type\'] == 1)
  283. $link .= sprintf(\'%1$s/%2$s\', $modSettings[\'custom_avatar_url\'], $rowData[\'filename\']);
  284. // By default avatars are downloaded almost as attachments.
  285. elseif ($context[\'browse_type\'] == \'avatars\')
  286. $link .= sprintf(\'%1$s?action=dlattach;type=avatar;attach=%2$d\', $scripturl, $rowData[\'id_attach\']);
  287. // Normal attachments are always linked to a topic ID.
  288. else
  289. $link .= sprintf(\'%1$s?action=dlattach;topic=%2$d.0;attach=%3$d\', $scripturl, $rowData[\'id_topic\'], $rowData[\'id_attach\']);
  290. $link .= \'"\';
  291. // Show a popup on click if it\'s a picture and we know its dimensions.
  292. if (!empty($rowData[\'width\']) && !empty($rowData[\'height\']))
  293. $link .= sprintf(\' onclick="return reqWin(this.href\' . ($rowData[\'attachment_type\'] == 1 ? \'\' : \' + \\\';image\\\'\') . \', %1$d, %2$d, true);"\', $rowData[\'width\'] + 20, $rowData[\'height\'] + 20);
  294. $link .= sprintf(\'>%1$s</a>\', preg_replace(\'~&amp;#(\\\\d{1,7}|x[0-9a-fA-F]{1,6});~\', \'&#\\\\1;\', htmlspecialchars($rowData[\'filename\'])));
  295. // Show the dimensions.
  296. if (!empty($rowData[\'width\']) && !empty($rowData[\'height\']))
  297. $link .= sprintf(\' <span class="smalltext">%1$dx%2$d</span>\', $rowData[\'width\'], $rowData[\'height\']);
  298. return $link;
  299. '),
  300. ),
  301. 'sort' => array(
  302. 'default' => 'a.filename',
  303. 'reverse' => 'a.filename DESC',
  304. ),
  305. ),
  306. 'filesize' => array(
  307. 'header' => array(
  308. 'value' => $txt['attachment_file_size'],
  309. ),
  310. 'data' => array(
  311. 'function' => create_function('$rowData','
  312. global $txt;
  313. return sprintf(\'%1$s%2$s\', round($rowData[\'size\'] / 1024, 2), $txt[\'kilobyte\']);
  314. '),
  315. 'class' => 'windowbg',
  316. ),
  317. 'sort' => array(
  318. 'default' => 'a.size',
  319. 'reverse' => 'a.size DESC',
  320. ),
  321. ),
  322. 'member' => array(
  323. 'header' => array(
  324. 'value' => $context['browse_type'] == 'avatars' ? $txt['attachment_manager_member'] : $txt['posted_by'],
  325. ),
  326. 'data' => array(
  327. 'function' => create_function('$rowData', '
  328. global $scripturl;
  329. // In case of an attachment, return the poster of the attachment.
  330. if (empty($rowData[\'id_member\']))
  331. return htmlspecialchars($rowData[\'poster_name\']);
  332. // Otherwise it must be an avatar, return the link to the owner of it.
  333. else
  334. return sprintf(\'<a href="%1$s?action=profile;u=%2$d">%3$s</a>\', $scripturl, $rowData[\'id_member\'], $rowData[\'poster_name\']);
  335. '),
  336. ),
  337. 'sort' => array(
  338. 'default' => 'mem.real_name',
  339. 'reverse' => 'mem.real_name DESC',
  340. ),
  341. ),
  342. 'date' => array(
  343. 'header' => array(
  344. 'value' => $context['browse_type'] == 'avatars' ? $txt['attachment_manager_last_active'] : $txt['date'],
  345. ),
  346. 'data' => array(
  347. 'function' => create_function('$rowData', '
  348. global $txt, $context, $scripturl;
  349. // The date the message containing the attachment was posted or the owner of the avatar was active.
  350. $date = empty($rowData[\'poster_time\']) ? $txt[\'never\'] : timeformat($rowData[\'poster_time\']);
  351. // Add a link to the topic in case of an attachment.
  352. if ($context[\'browse_type\'] !== \'avatars\')
  353. $date .= sprintf(\'<br />%1$s <a href="%2$s?topic=%3$d.0.msg%4$d#msg%4$d">%5$s</a>\', $txt[\'in\'], $scripturl, $rowData[\'id_topic\'], $rowData[\'id_msg\'], $rowData[\'subject\']);
  354. return $date;
  355. '),
  356. 'class' => 'windowbg',
  357. ),
  358. 'sort' => array(
  359. 'default' => $context['browse_type'] === 'avatars' ? 'mem.last_login' : 'm.id_msg',
  360. 'reverse' => $context['browse_type'] === 'avatars' ? 'mem.last_login DESC' : 'm.id_msg DESC',
  361. ),
  362. ),
  363. 'downloads' => array(
  364. 'header' => array(
  365. 'value' => $txt['downloads'],
  366. ),
  367. 'data' => array(
  368. 'function' => create_function('$rowData','
  369. global $txt;
  370. return comma_format($rowData[\'downloads\']);
  371. '),
  372. 'class' => 'windowbg',
  373. ),
  374. 'sort' => array(
  375. 'default' => 'a.downloads',
  376. 'reverse' => 'a.downloads DESC',
  377. ),
  378. ),
  379. 'check' => array(
  380. 'header' => array(
  381. 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
  382. ),
  383. 'data' => array(
  384. 'sprintf' => array(
  385. 'format' => '<input type="checkbox" name="remove[%1$d]" class="input_check" />',
  386. 'params' => array(
  387. 'id_attach' => false,
  388. ),
  389. ),
  390. 'style' => 'text-align: center',
  391. ),
  392. ),
  393. ),
  394. 'form' => array(
  395. 'href' => $scripturl . '?action=admin;area=manageattachments;sa=remove' . ($context['browse_type'] === 'avatars' ? ';avatars' : ($context['browse_type'] === 'thumbs' ? ';thumbs' : '')),
  396. 'include_sort' => true,
  397. 'include_start' => true,
  398. 'hidden_fields' => array(
  399. 'type' => $context['browse_type'],
  400. ),
  401. ),
  402. 'additional_rows' => array(
  403. array(
  404. 'position' => 'below_table_data',
  405. 'value' => '<input type="submit" name="remove_submit" class="button_submit" value="' . $txt['quickmod_delete_selected'] . '" onclick="return confirm(\'' . $txt['confirm_delete_attachments'] . '\');" />',
  406. 'style' => 'text-align: right;',
  407. ),
  408. ),
  409. );
  410. // Create the list.
  411. require_once($sourcedir . '/Subs-List.php');
  412. createList($listOptions);
  413. }
  414. function list_getFiles($start, $items_per_page, $sort, $browse_type)
  415. {
  416. global $smcFunc, $txt;
  417. // Choose a query depending on what we are viewing.
  418. if ($browse_type === 'avatars')
  419. $request = $smcFunc['db_query']('', '
  420. SELECT
  421. {string:blank_text} AS id_msg, IFNULL(mem.real_name, {string:not_applicable_text}) AS poster_name,
  422. mem.last_login AS poster_time, 0 AS id_topic, a.id_member, a.id_attach, a.filename, a.file_hash, a.attachment_type,
  423. a.size, a.width, a.height, a.downloads, {string:blank_text} AS subject, 0 AS id_board
  424. FROM {db_prefix}attachments AS a
  425. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = a.id_member)
  426. WHERE a.id_member != {int:guest_id}
  427. ORDER BY {raw:sort}
  428. LIMIT {int:start}, {int:per_page}',
  429. array(
  430. 'guest_id' => 0,
  431. 'blank_text' => '',
  432. 'not_applicable_text' => $txt['not_applicable'],
  433. 'sort' => $sort,
  434. 'start' => $start,
  435. 'per_page' => $items_per_page,
  436. )
  437. );
  438. else
  439. $request = $smcFunc['db_query']('', '
  440. SELECT
  441. m.id_msg, IFNULL(mem.real_name, m.poster_name) AS poster_name, m.poster_time, m.id_topic, m.id_member,
  442. a.id_attach, a.filename, a.file_hash, a.attachment_type, a.size, a.width, a.height, a.downloads, mf.subject, t.id_board
  443. FROM {db_prefix}attachments AS a
  444. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)
  445. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
  446. INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)
  447. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_member)
  448. WHERE a.attachment_type = {int:attachment_type}
  449. ORDER BY {raw:sort}
  450. LIMIT {int:start}, {int:per_page}',
  451. array(
  452. 'attachment_type' => $browse_type == 'thumbs' ? '3' : '0',
  453. 'sort' => $sort,
  454. 'start' => $start,
  455. 'per_page' => $items_per_page,
  456. )
  457. );
  458. $files = array();
  459. while ($row = $smcFunc['db_fetch_assoc']($request))
  460. $files[] = $row;
  461. $smcFunc['db_free_result']($request);
  462. return $files;
  463. }
  464. function list_getNumFiles($browse_type)
  465. {
  466. global $smcFunc;
  467. // Depending on the type of file, different queries are used.
  468. if ($browse_type === 'avatars')
  469. $request = $smcFunc['db_query']('', '
  470. SELECT COUNT(*)
  471. FROM {db_prefix}attachments
  472. WHERE id_member != {int:guest_id_member}',
  473. array(
  474. 'guest_id_member' => 0,
  475. )
  476. );
  477. else
  478. $request = $smcFunc['db_query']('', '
  479. SELECT COUNT(*) AS num_attach
  480. FROM {db_prefix}attachments AS a
  481. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)
  482. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic)
  483. INNER JOIN {db_prefix}messages AS mf ON (mf.id_msg = t.id_first_msg)
  484. WHERE a.attachment_type = {int:attachment_type}
  485. AND a.id_member = {int:guest_id_member}',
  486. array(
  487. 'attachment_type' => $browse_type === 'thumbs' ? '3' : '0',
  488. 'guest_id_member' => 0,
  489. )
  490. );
  491. list ($num_files) = $smcFunc['db_fetch_row']($request);
  492. $smcFunc['db_free_result']($request);
  493. return $num_files;
  494. }
  495. function MaintainFiles()
  496. {
  497. global $context, $modSettings, $txt, $smcFunc;
  498. $context['sub_template'] = 'maintenance';
  499. if (!empty($modSettings['currentAttachmentUploadDir']))
  500. $attach_dirs = unserialize($modSettings['attachmentUploadDir']);
  501. else
  502. $attach_dirs = array($modSettings['attachmentUploadDir']);
  503. // Get the number of attachments....
  504. $request = $smcFunc['db_query']('', '
  505. SELECT COUNT(*)
  506. FROM {db_prefix}attachments
  507. WHERE attachment_type = {int:attachment_type}
  508. AND id_member = {int:guest_id_member}',
  509. array(
  510. 'attachment_type' => 0,
  511. 'guest_id_member' => 0,
  512. )
  513. );
  514. list ($context['num_attachments']) = $smcFunc['db_fetch_row']($request);
  515. $smcFunc['db_free_result']($request);
  516. // Also get the avatar amount....
  517. $request = $smcFunc['db_query']('', '
  518. SELECT COUNT(*)
  519. FROM {db_prefix}attachments
  520. WHERE id_member != {int:guest_id_member}',
  521. array(
  522. 'guest_id_member' => 0,
  523. )
  524. );
  525. list ($context['num_avatars']) = $smcFunc['db_fetch_row']($request);
  526. $smcFunc['db_free_result']($request);
  527. // Find out how big the directory is. We have to loop through all our attachment paths in case there's an old temp file in one of them.
  528. $attachmentDirSize = 0;
  529. foreach ($attach_dirs as $id => $attach_dir)
  530. {
  531. $dir = @opendir($attach_dir) or fatal_lang_error('cant_access_upload_path', 'critical');
  532. while ($file = readdir($dir))
  533. {
  534. if ($file == '.' || $file == '..')
  535. continue;
  536. if (preg_match('~^post_tmp_\d+_\d+$~', $file) != 0)
  537. {
  538. // Temp file is more than 5 hours old!
  539. if (filemtime($attach_dir . '/' . $file) < time() - 18000)
  540. @unlink($attach_dir . '/' . $file);
  541. continue;
  542. }
  543. // We're only counting the size of the current attachment directory.
  544. if (empty($modSettings['currentAttachmentUploadDir']) || $modSettings['currentAttachmentUploadDir'] == $id)
  545. $attachmentDirSize += filesize($attach_dir . '/' . $file);
  546. }
  547. closedir($dir);
  548. }
  549. // Divide it into kilobytes.
  550. $attachmentDirSize /= 1024;
  551. // If they specified a limit only....
  552. if (!empty($modSettings['attachmentDirSizeLimit']))
  553. $context['attachment_space'] = max(round($modSettings['attachmentDirSizeLimit'] - $attachmentDirSize, 2), 0);
  554. $context['attachment_total_size'] = round($attachmentDirSize, 2);
  555. $context['attach_multiple_dirs'] = !empty($modSettings['currentAttachmentUploadDir']);
  556. }
  557. // !!! Not implemented yet.
  558. function MoveAvatars()
  559. {
  560. global $modSettings, $smcFunc;
  561. // First make sure the custom avatar dir is writable.
  562. if (!is_writable($modSettings['custom_avatar_dir']))
  563. {
  564. // Try to fix it.
  565. @chmod($modSettings['custom_avatar_dir'], 0777);
  566. // Guess that didn't work?
  567. if (!is_writable($modSettings['custom_avatar_dir']))
  568. fatal_lang_error('attachments_no_write', 'critical');
  569. }
  570. $request = $smcFunc['db_query']('', '
  571. SELECT id_attach, id_folder, id_member, filename, file_hash
  572. FROM {db_prefix}attachments
  573. WHERE attachment_type = {int:attachment_type}
  574. AND id_member > {int:guest_id_member}',
  575. array(
  576. 'attachment_type' => 0,
  577. 'guest_id_member' => 0,
  578. )
  579. );
  580. $updatedAvatars = array();
  581. while ($row = $smcFunc['db_fetch_assoc']($request))
  582. {
  583. $filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']);
  584. if (rename($filename, $modSettings['custom_avatar_dir'] . '/' . $row['filename']))
  585. $updatedAvatars[] = $row['id_attach'];
  586. }
  587. $smcFunc['db_free_result']($request);
  588. if (!empty($updatedAvatars))
  589. $smcFunc['db_query']('', '
  590. UPDATE {db_prefix}attachments
  591. SET attachment_type = {int:attachment_type}
  592. WHERE id_attach IN ({array_int:updated_avatars})',
  593. array(
  594. 'updated_avatars' => $updatedAvatars,
  595. 'attachment_type' => 1,
  596. )
  597. );
  598. redirectexit('action=admin;area=manageattachments;sa=maintenance');
  599. }
  600. function RemoveAttachmentByAge()
  601. {
  602. global $modSettings, $smcFunc;
  603. checkSession('post', 'admin');
  604. // !!! Ignore messages in topics that are stickied?
  605. // Deleting an attachment?
  606. if ($_REQUEST['type'] != 'avatars')
  607. {
  608. // Get all the old attachments.
  609. $messages = removeAttachments(array('attachment_type' => 0, 'poster_time' => (time() - 24 * 60 * 60 * $_POST['age'])), 'messages', true);
  610. // Update the messages to reflect the change.
  611. if (!empty($messages))
  612. $smcFunc['db_query']('', '
  613. UPDATE {db_prefix}messages
  614. SET body = CONCAT(body, ' . (!empty($_POST['notice']) ? '{string:notice}' : '') . ')
  615. WHERE id_msg IN ({array_int:messages})',
  616. array(
  617. 'messages' => $messages,
  618. 'notice' => empty($_POST['notice']) ? '' : '<br /><br />' . $_POST['notice'],
  619. )
  620. );
  621. }
  622. else
  623. {
  624. // Remove all the old avatars.
  625. removeAttachments(array('not_id_member' => 0, 'last_login' => (time() - 24 * 60 * 60 * $_POST['age'])), 'members');
  626. }
  627. redirectexit('action=admin;area=manageattachments' . (empty($_REQUEST['avatars']) ? ';sa=maintenance' : ';avatars'));
  628. }
  629. function RemoveAttachmentBySize()
  630. {
  631. global $modSettings, $smcFunc;
  632. checkSession('post', 'admin');
  633. // Find humungous attachments.
  634. $messages = removeAttachments(array('attachment_type' => 0, 'size' => 1024 * $_POST['size']), 'messages', true);
  635. // And make a note on the post.
  636. if (!empty($messages))
  637. $smcFunc['db_query']('', '
  638. UPDATE {db_prefix}messages
  639. SET body = CONCAT(body, ' . (!empty($_POST['notice']) ? '{string:notice}' : '') . ')
  640. WHERE id_msg IN ({array_int:messages})',
  641. array(
  642. 'messages' => $messages,
  643. 'notice' => empty($_POST['notice']) ? '' : '<br /><br />' . $_POST['notice'],
  644. )
  645. );
  646. redirectexit('action=admin;area=manageattachments;sa=maintenance');
  647. }
  648. function RemoveAttachment()
  649. {
  650. global $modSettings, $txt, $smcFunc;
  651. checkSession('post');
  652. if (!empty($_POST['remove']))
  653. {
  654. $attachments = array();
  655. // There must be a quicker way to pass this safety test??
  656. foreach ($_POST['remove'] as $removeID => $dummy)
  657. $attachments[] = (int) $removeID;
  658. if ($_REQUEST['type'] == 'avatars' && !empty($attachments))
  659. removeAttachments(array('id_attach' => $attachments));
  660. else if (!empty($attachments))
  661. {
  662. $messages = removeAttachments(array('id_attach' => $attachments), 'messages', true);
  663. // And change the message to reflect this.
  664. if (!empty($messages))
  665. $smcFunc['db_query']('', '
  666. UPDATE {db_prefix}messages
  667. SET body = CONCAT(body, {string:deleted_message})
  668. WHERE id_msg IN ({array_int:messages_affected})',
  669. array(
  670. 'messages_affected' => $messages,
  671. 'deleted_message' => '<br /><br />' . $txt['attachment_delete_admin'],
  672. )
  673. );
  674. }
  675. }
  676. $_GET['sort'] = isset($_GET['sort']) ? $_GET['sort'] : 'date';
  677. redirectexit('action=admin;area=manageattachments;sa=browse;' . $_REQUEST['type'] . ';sort=' . $_GET['sort'] . (isset($_GET['desc']) ? ';desc' : '') . ';start=' . $_REQUEST['start']);
  678. }
  679. // !!! Not implemented (yet?)
  680. function RemoveAllAttachments()
  681. {
  682. global $txt, $smcFunc;
  683. checkSession('get', 'admin');
  684. $messages = removeAttachments(array('attachment_type' => 0), '', true);
  685. if (!isset($_POST['notice']))
  686. $_POST['notice'] = $txt['attachment_delete_admin'];
  687. // Add the notice on the end of the changed messages.
  688. if (!empty($messages))
  689. $smcFunc['db_query']('', '
  690. UPDATE {db_prefix}messages
  691. SET body = CONCAT(body, {string:deleted_message})
  692. WHERE id_msg IN ({array_int:messages})',
  693. array(
  694. 'messages' => $messages,
  695. 'deleted_message' => '<br /><br />' . $_POST['notice'],
  696. )
  697. );
  698. redirectexit('action=admin;area=manageattachments;sa=maintenance');
  699. }
  700. // Removes attachments - allowed query_types: '', 'messages', 'members'
  701. function removeAttachments($condition, $query_type = '', $return_affected_messages = false, $autoThumbRemoval = true)
  702. {
  703. global $modSettings, $smcFunc;
  704. //!!! This might need more work!
  705. $new_condition = array();
  706. $query_parameter = array(
  707. 'thumb_attachment_type' => 3,
  708. );
  709. if (is_array($condition))
  710. {
  711. foreach ($condition as $real_type => $restriction)
  712. {
  713. // Doing a NOT?
  714. $is_not = substr($real_type, 0, 4) == 'not_';
  715. $type = $is_not ? substr($real_type, 4) : $real_type;
  716. if (in_array($type, array('id_member', 'id_attach', 'id_msg')))
  717. $new_condition[] = 'a.' . $type . ($is_not ? ' NOT' : '') . ' IN (' . (is_array($restriction) ? '{array_int:' . $real_type . '}' : '{int:' . $real_type . '}') . ')';
  718. elseif ($type == 'attachment_type')
  719. $new_condition[] = 'a.attachment_type = {int:' . $real_type . '}';
  720. elseif ($type == 'poster_time')
  721. $new_condition[] = 'm.poster_time < {int:' . $real_type . '}';
  722. elseif ($type == 'last_login')
  723. $new_condition[] = 'mem.last_login < {int:' . $real_type . '}';
  724. elseif ($type == 'size')
  725. $new_condition[] = 'a.size > {int:' . $real_type . '}';
  726. elseif ($type == 'id_topic')
  727. $new_condition[] = 'm.id_topic IN (' . (is_array($restriction) ? '{array_int:' . $real_type . '}' : '{int:' . $real_type . '}') . ')';
  728. // Add the parameter!
  729. $query_parameter[$real_type] = $restriction;
  730. }
  731. $condition = implode(' AND ', $new_condition);
  732. }
  733. // Delete it only if it exists...
  734. $msgs = array();
  735. $attach = array();
  736. $parents = array();
  737. // Get all the attachment names and id_msg's.
  738. $request = $smcFunc['db_query']('', '
  739. SELECT
  740. a.id_folder, a.filename, a.file_hash, a.attachment_type, a.id_attach, a.id_member' . ($query_type == 'messages' ? ', m.id_msg' : ', a.id_msg') . ',
  741. thumb.id_folder AS thumb_folder, IFNULL(thumb.id_attach, 0) AS id_thumb, thumb.filename AS thumb_filename, thumb.file_hash AS thumb_file_hash, thumb_parent.id_attach AS id_parent
  742. FROM {db_prefix}attachments AS a' .($query_type == 'members' ? '
  743. INNER JOIN {db_prefix}members AS mem ON (mem.id_member = a.id_member)' : ($query_type == 'messages' ? '
  744. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)' : '')) . '
  745. LEFT JOIN {db_prefix}attachments AS thumb ON (thumb.id_attach = a.id_thumb)
  746. LEFT JOIN {db_prefix}attachments AS thumb_parent ON (thumb.attachment_type = {int:thumb_attachment_type} AND thumb_parent.id_thumb = a.id_attach)
  747. WHERE ' . $condition,
  748. $query_parameter
  749. );
  750. while ($row = $smcFunc['db_fetch_assoc']($request))
  751. {
  752. // Figure out the "encrypted" filename and unlink it ;).
  753. if ($row['attachment_type'] == 1)
  754. @unlink($modSettings['custom_avatar_dir'] . '/' . $row['filename']);
  755. else
  756. {
  757. $filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']);
  758. @unlink($filename);
  759. // If this was a thumb, the parent attachment should know about it.
  760. if (!empty($row['id_parent']))
  761. $parents[] = $row['id_parent'];
  762. // If this attachments has a thumb, remove it as well.
  763. if (!empty($row['id_thumb']) && $autoThumbRemoval)
  764. {
  765. $thumb_filename = getAttachmentFilename($row['thumb_filename'], $row['id_thumb'], $row['thumb_folder'], false, $row['thumb_file_hash']);
  766. @unlink($thumb_filename);
  767. $attach[] = $row['id_thumb'];
  768. }
  769. }
  770. // Make a list.
  771. if ($return_affected_messages && empty($row['attachment_type']))
  772. $msgs[] = $row['id_msg'];
  773. $attach[] = $row['id_attach'];
  774. }
  775. $smcFunc['db_free_result']($request);
  776. // Removed attachments don't have to be updated anymore.
  777. $parents = array_diff($parents, $attach);
  778. if (!empty($parents))
  779. $smcFunc['db_query']('', '
  780. UPDATE {db_prefix}attachments
  781. SET id_thumb = {int:no_thumb}
  782. WHERE id_attach IN ({array_int:parent_attachments})',
  783. array(
  784. 'parent_attachments' => $parents,
  785. 'no_thumb' => 0,
  786. )
  787. );
  788. if (!empty($attach))
  789. $smcFunc['db_query']('', '
  790. DELETE FROM {db_prefix}attachments
  791. WHERE id_attach IN ({array_int:attachment_list})',
  792. array(
  793. 'attachment_list' => $attach,
  794. )
  795. );
  796. if ($return_affected_messages)
  797. return array_unique($msgs);
  798. }
  799. // This function should find attachments in the database that no longer exist and clear them, and fix filesize issues.
  800. function RepairAttachments()
  801. {
  802. global $modSettings, $context, $txt, $smcFunc;
  803. checkSession('get');
  804. // If we choose cancel, redirect right back.
  805. if (isset($_POST['cancel']))
  806. redirectexit('action=admin;area=manageattachments;sa=maintenance');
  807. // Try give us a while to sort this out...
  808. @set_time_limit(600);
  809. $_GET['step'] = empty($_GET['step']) ? 0 : (int) $_GET['step'];
  810. $_GET['substep'] = empty($_GET['substep']) ? 0 : (int) $_GET['substep'];
  811. // Don't recall the session just in case.
  812. if ($_GET['step'] == 0 && $_GET['substep'] == 0)
  813. {
  814. unset($_SESSION['attachments_to_fix'], $_SESSION['attachments_to_fix2']);
  815. // If we're actually fixing stuff - work out what.
  816. if (isset($_GET['fixErrors']))
  817. {
  818. // Nothing?
  819. if (empty($_POST['to_fix']))
  820. redirectexit('action=admin;area=manageattachments;sa=maintenance');
  821. $_SESSION['attachments_to_fix'] = array();
  822. //!!! No need to do this I think.
  823. foreach ($_POST['to_fix'] as $key => $value)
  824. $_SESSION['attachments_to_fix'][] = $value;
  825. }
  826. }
  827. // All the valid problems are here:
  828. $context['repair_errors'] = array(
  829. 'missing_thumbnail_parent' => 0,
  830. 'parent_missing_thumbnail' => 0,
  831. 'file_missing_on_disk' => 0,
  832. 'file_wrong_size' => 0,
  833. 'file_size_of_zero' => 0,
  834. 'attachment_no_msg' => 0,
  835. 'avatar_no_member' => 0,
  836. 'wrong_folder' => 0,
  837. );
  838. $to_fix = !empty($_SESSION['attachments_to_fix']) ? $_SESSION['attachments_to_fix'] : array();
  839. $context['repair_errors'] = isset($_SESSION['attachments_to_fix2']) ? $_SESSION['attachments_to_fix2'] : $context['repair_errors'];
  840. $fix_errors = isset($_GET['fixErrors']) ? true : false;
  841. // Get stranded thumbnails.
  842. if ($_GET['step'] <= 0)
  843. {
  844. $result = $smcFunc['db_query']('', '
  845. SELECT MAX(id_attach)
  846. FROM {db_prefix}attachments
  847. WHERE attachment_type = {int:thumbnail}',
  848. array(
  849. 'thumbnail' => 3,
  850. )
  851. );
  852. list ($thumbnails) = $smcFunc['db_fetch_row']($result);
  853. $smcFunc['db_free_result']($result);
  854. for (; $_GET['substep'] < $thumbnails; $_GET['substep'] += 500)
  855. {
  856. $to_remove = array();
  857. $result = $smcFunc['db_query']('', '
  858. SELECT thumb.id_attach, thumb.id_folder, thumb.filename, thumb.file_hash
  859. FROM {db_prefix}attachments AS thumb
  860. LEFT JOIN {db_prefix}attachments AS tparent ON (tparent.id_thumb = thumb.id_attach)
  861. WHERE thumb.id_attach BETWEEN {int:substep} AND {int:substep} + 499
  862. AND thumb.attachment_type = {int:thumbnail}
  863. AND tparent.id_attach IS NULL',
  864. array(
  865. 'thumbnail' => 3,
  866. 'substep' => $_GET['substep'],
  867. )
  868. );
  869. while ($row = $smcFunc['db_fetch_assoc']($result))
  870. {
  871. // Only do anything once... just in case
  872. if (!isset($to_remove[$row['id_attach']]))
  873. {
  874. $to_remove[$row['id_attach']] = $row['id_attach'];
  875. $context['repair_errors']['missing_thumbnail_parent']++;
  876. // If we are repairing remove the file from disk now.
  877. if ($fix_errors && in_array('missing_thumbnail_parent', $to_fix))
  878. {
  879. $filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']);
  880. @unlink($filename);
  881. }
  882. }
  883. }
  884. if ($smcFunc['db_num_rows']($result) != 0)
  885. $to_fix[] = 'missing_thumbnail_parent';
  886. $smcFunc['db_free_result']($result);
  887. // Do we need to delete what we have?
  888. if ($fix_errors && !empty($to_remove) && in_array('missing_thumbnail_parent', $to_fix))
  889. $smcFunc['db_query']('', '
  890. DELETE FROM {db_prefix}attachments
  891. WHERE id_attach IN ({array_int:to_remove})
  892. AND attachment_type = {int:attachment_type}',
  893. array(
  894. 'to_remove' => $to_remove,
  895. 'attachment_type' => 3,
  896. )
  897. );
  898. pauseAttachmentMaintenance($to_fix, $thumbnails);
  899. }
  900. $_GET['step'] = 1;
  901. $_GET['substep'] = 0;
  902. pauseAttachmentMaintenance($to_fix);
  903. }
  904. // Find parents which think they have thumbnails, but actually, don't.
  905. if ($_GET['step'] <= 1)
  906. {
  907. $result = $smcFunc['db_query']('', '
  908. SELECT MAX(id_attach)
  909. FROM {db_prefix}attachments
  910. WHERE id_thumb != {int:no_thumb}',
  911. array(
  912. 'no_thumb' => 0,
  913. )
  914. );
  915. list ($thumbnails) = $smcFunc['db_fetch_row']($result);
  916. $smcFunc['db_free_result']($result);
  917. for (; $_GET['substep'] < $thumbnails; $_GET['substep'] += 500)
  918. {
  919. $to_update = array();
  920. $result = $smcFunc['db_query']('', '
  921. SELECT a.id_attach
  922. FROM {db_prefix}attachments AS a
  923. LEFT JOIN {db_prefix}attachments AS thumb ON (thumb.id_attach = a.id_thumb)
  924. WHERE a.id_attach BETWEEN {int:substep} AND {int:substep} + 499
  925. AND a.id_thumb != {int:no_thumb}
  926. AND thumb.id_attach IS NULL',
  927. array(
  928. 'no_thumb' => 0,
  929. 'substep' => $_GET['substep'],
  930. )
  931. );
  932. while ($row = $smcFunc['db_fetch_assoc']($result))
  933. {
  934. $to_update[] = $row['id_attach'];
  935. $context['repair_errors']['parent_missing_thumbnail']++;
  936. }
  937. if ($smcFunc['db_num_rows']($result) != 0)
  938. $to_fix[] = 'parent_missing_thumbnail';
  939. $smcFunc['db_free_result']($result);
  940. // Do we need to delete what we have?
  941. if ($fix_errors && !empty($to_update) && in_array('parent_missing_thumbnail', $to_fix))
  942. $smcFunc['db_query']('', '
  943. UPDATE {db_prefix}attachments
  944. SET id_thumb = {int:no_thumb}
  945. WHERE id_attach IN ({array_int:to_update})',
  946. array(
  947. 'to_update' => $to_update,
  948. 'no_thumb' => 0,
  949. )
  950. );
  951. pauseAttachmentMaintenance($to_fix, $thumbnails);
  952. }
  953. $_GET['step'] = 2;
  954. $_GET['substep'] = 0;
  955. pauseAttachmentMaintenance($to_fix);
  956. }
  957. // This may take forever I'm afraid, but life sucks... recount EVERY attachments!
  958. if ($_GET['step'] <= 2)
  959. {
  960. $result = $smcFunc['db_query']('', '
  961. SELECT MAX(id_attach)
  962. FROM {db_prefix}attachments',
  963. array(
  964. )
  965. );
  966. list ($thumbnails) = $smcFunc['db_fetch_row']($result);
  967. $smcFunc['db_free_result']($result);
  968. for (; $_GET['substep'] < $thumbnails; $_GET['substep'] += 250)
  969. {
  970. $to_remove = array();
  971. $errors_found = array();
  972. $result = $smcFunc['db_query']('', '
  973. SELECT id_attach, id_folder, filename, file_hash, size, attachment_type
  974. FROM {db_prefix}attachments
  975. WHERE id_attach BETWEEN {int:substep} AND {int:substep} + 249',
  976. array(
  977. 'substep' => $_GET['substep'],
  978. )
  979. );
  980. while ($row = $smcFunc['db_fetch_assoc']($result))
  981. {
  982. // Get the filename.
  983. if ($row['attachment_type'] == 1)
  984. $filename = $modSettings['custom_avatar_dir'] . '/' . $row['filename'];
  985. else
  986. $filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']);
  987. // File doesn't exist?
  988. if (!file_exists($filename))
  989. {
  990. // If we're lucky it might just be in a different folder.
  991. if (!empty($modSettings['currentAttachmentUploadDir']))
  992. {
  993. // Get the attachment name with out the folder.
  994. $attachment_name = !empty($row['file_hash']) ? $row['id_attach'] . '_' . $row['file_hash'] : getLegacyAttachmentFilename($row['filename'], $row['id_attach'], null, true);
  995. if (!is_array($modSettings['attachmentUploadDir']))
  996. $modSettings['attachmentUploadDir'] = unserialize($modSettings['attachmentUploadDir']);
  997. // Loop through the other folders.
  998. foreach ($modSettings['attachmentUploadDir'] as $id => $dir)
  999. if (file_exists($dir . '/' . $attachment_name))
  1000. {
  1001. $context['repair_errors']['wrong_folder']++;
  1002. $errors_found[] = 'wrong_folder';
  1003. // Are we going to fix this now?
  1004. if ($fix_errors && in_array('wrong_folder', $to_fix))
  1005. $smcFunc['db_query']('', '
  1006. UPDATE {db_prefix}attachments
  1007. SET id_folder = {int:new_folder}
  1008. WHERE id_attach = {int:id_attach}',
  1009. array(
  1010. 'new_folder' => $id,
  1011. 'id_attach' => $row['id_attach'],
  1012. )
  1013. );
  1014. continue 2;
  1015. }
  1016. }
  1017. $to_remove[] = $row['id_attach'];
  1018. $context['repair_errors']['file_missing_on_disk']++;
  1019. $errors_found[] = 'file_missing_on_disk';
  1020. }
  1021. elseif (filesize($filename) == 0)
  1022. {
  1023. $context['repair_errors']['file_size_of_zero']++;
  1024. $errors_found[] = 'file_size_of_zero';
  1025. // Fixing?
  1026. if ($fix_errors && in_array('file_size_of_zero', $to_fix))
  1027. {
  1028. $to_remove[] = $row['id_attach'];
  1029. @unlink($filename);
  1030. }
  1031. }
  1032. elseif (filesize($filename) != $row['size'])
  1033. {
  1034. $context['repair_errors']['file_wrong_size']++;
  1035. $errors_found[] = 'file_wrong_size';
  1036. // Fix it here?
  1037. if ($fix_errors && in_array('file_wrong_size', $to_fix))
  1038. {
  1039. $smcFunc['db_query']('', '
  1040. UPDATE {db_prefix}attachments
  1041. SET size = {int:filesize}
  1042. WHERE id_attach = {int:id_attach}',
  1043. array(
  1044. 'filesize' => filesize($filename),
  1045. 'id_attach' => $row['id_attach'],
  1046. )
  1047. );
  1048. }
  1049. }
  1050. }
  1051. if (in_array('file_missing_on_disk', $errors_found))
  1052. $to_fix[] = 'file_missing_on_disk';
  1053. if (in_array('file_size_of_zero', $errors_found))
  1054. $to_fix[] = 'file_size_of_zero';
  1055. if (in_array('file_wrong_size', $errors_found))
  1056. $to_fix[] = 'file_wrong_size';
  1057. if (in_array('wrong_folder', $errors_found))
  1058. $to_fix[] = 'wrong_folder';
  1059. $smcFunc['db_free_result']($result);
  1060. // Do we need to delete what we have?
  1061. if ($fix_errors && !empty($to_remove))
  1062. {
  1063. $smcFunc['db_query']('', '
  1064. DELETE FROM {db_prefix}attachments
  1065. WHERE id_attach IN ({array_int:to_remove})',
  1066. array(
  1067. 'to_remove' => $to_remove,
  1068. )
  1069. );
  1070. $smcFunc['db_query']('', '
  1071. UPDATE {db_prefix}attachments
  1072. SET id_thumb = {int:no_thumb}
  1073. WHERE id_thumb IN ({array_int:to_remove})',
  1074. array(
  1075. 'to_remove' => $to_remove,
  1076. 'no_thumb' => 0,
  1077. )
  1078. );
  1079. }
  1080. pauseAttachmentMaintenance($to_fix, $thumbnails);
  1081. }
  1082. $_GET['step'] = 3;
  1083. $_GET['substep'] = 0;
  1084. pauseAttachmentMaintenance($to_fix);
  1085. }
  1086. // Get avatars with no members associated with them.
  1087. if ($_GET['step'] <= 3)
  1088. {
  1089. $result = $smcFunc['db_query']('', '
  1090. SELECT MAX(id_attach)
  1091. FROM {db_prefix}attachments',
  1092. array(
  1093. )
  1094. );
  1095. list ($thumbnails) = $smcFunc['db_fetch_row']($result);
  1096. $smcFunc['db_free_result']($result);
  1097. for (; $_GET['substep'] < $thumbnails; $_GET['substep'] += 500)
  1098. {
  1099. $to_remove = array();
  1100. $result = $smcFunc['db_query']('', '
  1101. SELECT a.id_attach, a.id_folder, a.filename, a.file_hash, a.attachment_type
  1102. FROM {db_prefix}attachments AS a
  1103. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = a.id_member)
  1104. WHERE a.id_attach BETWEEN {int:substep} AND {int:substep} + 499
  1105. AND a.id_member != {int:no_member}
  1106. AND a.id_msg = {int:no_msg}
  1107. AND mem.id_member IS NULL',
  1108. array(
  1109. 'no_member' => 0,
  1110. 'no_msg' => 0,
  1111. 'substep' => $_GET['substep'],
  1112. )
  1113. );
  1114. while ($row = $smcFunc['db_fetch_assoc']($result))
  1115. {
  1116. $to_remove[] = $row['id_attach'];
  1117. $context['repair_errors']['avatar_no_member']++;
  1118. // If we are repairing remove the file from disk now.
  1119. if ($fix_errors && in_array('avatar_no_member', $to_fix))
  1120. {
  1121. if ($row['attachment_type'] == 1)
  1122. $filename = $modSettings['custom_avatar_dir'] . '/' . $row['filename'];
  1123. else
  1124. $filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']);
  1125. @unlink($filename);
  1126. }
  1127. }
  1128. if ($smcFunc['db_num_rows']($result) != 0)
  1129. $to_fix[] = 'avatar_no_member';
  1130. $smcFunc['db_free_result']($result);
  1131. // Do we need to delete what we have?
  1132. if ($fix_errors && !empty($to_remove) && in_array('avatar_no_member', $to_fix))
  1133. $smcFunc['db_query']('', '
  1134. DELETE FROM {db_prefix}attachments
  1135. WHERE id_attach IN ({array_int:to_remove})
  1136. AND id_member != {int:no_member}
  1137. AND id_msg = {int:no_msg}',
  1138. array(
  1139. 'to_remove' => $to_remove,
  1140. 'no_member' => 0,
  1141. 'no_msg' => 0,
  1142. )
  1143. );
  1144. pauseAttachmentMaintenance($to_fix, $thumbnails);
  1145. }
  1146. $_GET['step'] = 4;
  1147. $_GET['substep'] = 0;
  1148. pauseAttachmentMaintenance($to_fix);
  1149. }
  1150. // What about attachments, who are missing a message :'(
  1151. if ($_GET['step'] <= 4)
  1152. {
  1153. $result = $smcFunc['db_query']('', '
  1154. SELECT MAX(id_attach)
  1155. FROM {db_prefix}attachments',
  1156. array(
  1157. )
  1158. );
  1159. list ($thumbnails) = $smcFunc['db_fetch_row']($result);
  1160. $smcFunc['db_free_result']($result);
  1161. for (; $_GET['substep'] < $thumbnails; $_GET['substep'] += 500)
  1162. {
  1163. $to_remove = array();
  1164. $result = $smcFunc['db_query']('', '
  1165. SELECT a.id_attach, a.id_folder, a.filename, a.file_hash
  1166. FROM {db_prefix}attachments AS a
  1167. LEFT JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)
  1168. WHERE a.id_attach BETWEEN {int:substep} AND {int:substep} + 499
  1169. AND a.id_member = {int:no_member}
  1170. AND a.id_msg != {int:no_msg}
  1171. AND m.id_msg IS NULL',
  1172. array(
  1173. 'no_member' => 0,
  1174. 'no_msg' => 0,
  1175. 'substep' => $_GET['substep'],
  1176. )
  1177. );
  1178. while ($row = $smcFunc['db_fetch_assoc']($result))
  1179. {
  1180. $to_remove[] = $row['id_attach'];
  1181. $context['repair_errors']['attachment_no_msg']++;
  1182. // If we are repairing remove the file from disk now.
  1183. if ($fix_errors && in_array('attachment_no_msg', $to_fix))
  1184. {
  1185. $filename = getAttachmentFilename($row['filename'], $row['id_attach'], $row['id_folder'], false, $row['file_hash']);
  1186. @unlink($filename);
  1187. }
  1188. }
  1189. if ($smcFunc['db_num_rows']($result) != 0)
  1190. $to_fix[] = 'attachment_no_msg';
  1191. $smcFunc['db_free_result']($result);
  1192. // Do we need to delete what we have?
  1193. if ($fix_errors && !empty($to_remove) && in_array('attachment_no_msg', $to_fix))
  1194. $smcFunc['db_query']('', '
  1195. DELETE FROM {db_prefix}attachments
  1196. WHERE id_attach IN ({array_int:to_remove})
  1197. AND id_member = {int:no_member}
  1198. AND id_msg != {int:no_msg}',
  1199. array(
  1200. 'to_remove' => $to_remove,
  1201. 'no_member' => 0,
  1202. 'no_msg' => 0,
  1203. )
  1204. );
  1205. pauseAttachmentMaintenance($to_fix, $thumbnails);
  1206. }
  1207. $_GET['step'] = 5;
  1208. $_GET['substep'] = 0;
  1209. pauseAttachmentMaintenance($to_fix);
  1210. }
  1211. // Got here we must be doing well - just the template! :D
  1212. $context['page_title'] = $txt['repair_attachments'];
  1213. $context[$context['admin_menu_name']]['current_subsection'] = 'maintenance';
  1214. $context['sub_template'] = 'attachment_repair';
  1215. // What stage are we at?
  1216. $context['completed'] = $fix_errors ? true : false;
  1217. $context['errors_found'] = !empty($to_fix) ? true : false;
  1218. }
  1219. function pauseAttachmentMaintenance($to_fix, $max_substep = 0)
  1220. {
  1221. global $context, $txt, $time_start;
  1222. // Try get more time...
  1223. @set_time_limit(600);
  1224. if (function_exists('apache_reset_timeout'))
  1225. @apache_reset_timeout();
  1226. // Have we already used our maximum time?
  1227. if (time() - array_sum(explode(' ', $time_start)) < 3)
  1228. return;
  1229. $context['continue_get_data'] = '?action=admin;area=manageattachments;sa=repair' . (isset($_GET['fixErrors']) ? ';fixErrors' : '') . ';step=' . $_GET['step'] . ';substep=' . $_GET['substep'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  1230. $context['page_title'] = $txt['not_done_title'];
  1231. $context['continue_post_data'] = '';
  1232. $context['continue_countdown'] = '2';
  1233. $context['sub_template'] = 'not_done';
  1234. // Specific stuff to not break this template!
  1235. $context[$context['admin_menu_name']]['current_subsection'] = 'maintenance';
  1236. // Change these two if more steps are added!
  1237. if (empty($max_substep))
  1238. $context['continue_percent'] = round(($_GET['step'] * 100) / 25);
  1239. else
  1240. $context['continue_percent'] = round(($_GET['step'] * 100 + ($_GET['substep'] * 100) / $max_substep) / 25);
  1241. // Never more than 100%!
  1242. $context['continue_percent'] = min($context['continue_percent'], 100);
  1243. $_SESSION['attachments_to_fix'] = $to_fix;
  1244. $_SESSION['attachments_to_fix2'] = $context['repair_errors'];
  1245. obExit();
  1246. }
  1247. // Called from a mouse click, works out what we want to do with attachments and actions it.
  1248. function ApproveAttach()
  1249. {
  1250. global $smcFunc;
  1251. // Security is our primary concern...
  1252. checkSession('get');
  1253. // If it approve or delete?
  1254. $is_approve = !isset($_GET['sa']) || $_GET['sa'] != 'reject' ? true : false;
  1255. $attachments = array();
  1256. // If we are approving all ID's in a message , get the ID's.
  1257. if ($_GET['sa'] == 'all' && !empty($_GET['mid']))
  1258. {
  1259. $id_msg = (int) $_GET['mid'];
  1260. $request = $smcFunc['db_query']('', '
  1261. SELECT id_attach
  1262. FROM {db_prefix}attachments
  1263. WHERE id_msg = {int:id_msg}
  1264. AND approved = {int:is_approved}
  1265. AND attachment_type = {int:attachment_type}',
  1266. array(
  1267. 'id_msg' => $id_msg,
  1268. 'is_approved' => 0,
  1269. 'attachment_type' => 0,
  1270. )
  1271. );
  1272. while ($row = $smcFunc['db_fetch_assoc']($request))
  1273. $attachments[] = $row['id_attach'];
  1274. $smcFunc['db_free_result']($request);
  1275. }
  1276. elseif (!empty($_GET['aid']))
  1277. $attachments[] = (int) $_GET['aid'];
  1278. if (empty($attachments))
  1279. fatal_lang_error('no_access', false);
  1280. // Now we have some ID's cleaned and ready to approve, but first - let's check we have permission!
  1281. $allowed_boards = boardsAllowedTo('approve_posts');
  1282. // Validate the attachments exist and are the right approval state.
  1283. $request = $smcFunc['db_query']('', '
  1284. SELECT a.id_attach, m.id_board, m.id_msg, m.id_topic
  1285. FROM {db_prefix}attachments AS a
  1286. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = a.id_msg)
  1287. WHERE a.id_attach IN ({array_int:attachments})
  1288. AND a.attachment_type = {int:attachment_type}
  1289. AND a.approved = {int:is_approved}',
  1290. array(
  1291. 'attachments' => $attachments,
  1292. 'attachment_type' => 0,
  1293. 'is_approved' => 0,
  1294. )
  1295. );
  1296. $attachments = array();
  1297. while ($row = $smcFunc['db_fetch_assoc']($request))
  1298. {
  1299. // We can only add it if we can approve in this board!
  1300. if ($allowed_boards = array(0) || in_array($row['id_board'], $allowed_boards))
  1301. {
  1302. $attachments[] = $row['id_attach'];
  1303. // Also come up witht he redirection URL.
  1304. $redirect = 'topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'];
  1305. }
  1306. }
  1307. $smcFunc['db_free_result']($request);
  1308. if (empty($attachments))
  1309. fatal_lang_error('no_access', false);
  1310. // Finally, we are there. Follow through!
  1311. if ($is_approve)
  1312. ApproveAttachments($attachments);
  1313. else
  1314. removeAttachments(array('id_attach' => $attachments));
  1315. // Return to the topic....
  1316. redirectexit($redirect);
  1317. }
  1318. // Approve an attachment, or maybe even more - no permission check!
  1319. function ApproveAttachments($attachments)
  1320. {
  1321. global $smcFunc;
  1322. if (empty($attachments))
  1323. return 0;
  1324. // For safety, check for thumbnails...
  1325. $request = $smcFunc['db_query']('', '
  1326. SELECT
  1327. a.id_attach, a.id_member, IFNULL(thumb.id_attach, 0) AS id_thumb
  1328. FROM {db_prefix}attachments AS a
  1329. LEFT JOIN {db_prefix}attachments AS thumb ON (thumb.id_attach = a.id_thumb)
  1330. WHERE a.id_attach IN ({array_int:attachments})
  1331. AND a.attachment_type = {int:attachment_type}',
  1332. array(
  1333. 'attachments' => $attachments,
  1334. 'attachment_type' => 0,
  1335. )
  1336. );
  1337. $attachments = array();
  1338. while ($row = $smcFunc['db_fetch_assoc']($request))
  1339. {
  1340. // Update the thumbnail too...
  1341. if (!empty($row['id_thumb']))
  1342. $attachments[] = $row['id_thumb'];
  1343. $attachments[] = $row['id_attach'];
  1344. }
  1345. $smcFunc['db_free_result']($request);
  1346. // Approving an attachment is not hard - it's easy.
  1347. $smcFunc['db_query']('', '
  1348. UPDATE {db_prefix}attachments
  1349. SET approved = {int:is_approved}
  1350. WHERE id_attach IN ({array_int:attachments})',
  1351. array(
  1352. 'attachments' => $attachments,
  1353. 'is_approved' => 1,
  1354. )
  1355. );
  1356. // Remove from the approval queue.
  1357. $smcFunc['db_query']('', '
  1358. DELETE FROM {db_prefix}approval_queue
  1359. WHERE id_attach IN ({array_int:attachments})',
  1360. array(
  1361. 'attachments' => $attachments,
  1362. )
  1363. );
  1364. }
  1365. function ManageAttachmentPaths()
  1366. {
  1367. global $modSettings, $scripturl, $context, $txt, $sourcedir, $smcFunc;
  1368. // Saving?
  1369. if (isset($_REQUEST['save']))
  1370. {
  1371. checkSession();
  1372. $new_dirs = array();
  1373. foreach ($_POST['dirs'] as $id => $path)
  1374. {
  1375. $id = (int) $id;
  1376. if ($id < 1)
  1377. continue;
  1378. if (empty($path))
  1379. {
  1380. // Let's not try to delete a path with files in it.
  1381. $request = $smcFunc['db_query']('', '
  1382. SELECT COUNT(id_attach) AS num_attach
  1383. FROM {db_prefix}attachments
  1384. WHERE id_folder = {int:id_folder}',
  1385. array(
  1386. 'id_folder' => (int) $id,
  1387. )
  1388. );
  1389. list ($num_attach) = $smcFunc['db_fetch_row']($request);
  1390. $smcFunc['db_free_result']($request);
  1391. // It's safe to delete.
  1392. if ($num_attach == 0)
  1393. continue;
  1394. }
  1395. $new_dirs[$id] = $path;
  1396. }
  1397. // We need to make sure the current directory is right.
  1398. $_POST['current_dir'] = (int) $_POST['current_dir'];
  1399. if (empty($_POST['current_dir']) || empty($new_dirs[$_POST['current_dir']]))
  1400. fatal_lang_error('attach_path_current_bad', false);
  1401. // Going back to just one path?
  1402. if (count($new_dirs) == 1)
  1403. {
  1404. // We might need to reset the paths. This loop will just loop through once.
  1405. foreach ($new_dirs as $id => $dir)
  1406. {
  1407. if ($id != 1)
  1408. $smcFunc['db_query']('', '
  1409. UPDATE {db_prefix}attachments
  1410. SET id_folder = {int:default_folder}
  1411. WHERE id_folder = {int:current_folder}',
  1412. array(
  1413. 'default_folder' => 1,
  1414. 'current_folder' => $id,
  1415. )
  1416. );
  1417. updateSettings(array(
  1418. 'currentAttachmentUploadDir' => 0,
  1419. 'attachmentUploadDir' => $dir,
  1420. ));
  1421. }
  1422. }
  1423. else
  1424. // Save it to the database.
  1425. updateSettings(array(
  1426. 'currentAttachmentUploadDir' => $_POST['current_dir'],
  1427. 'attachmentUploadDir' => serialize($new_dirs),
  1428. ));
  1429. }
  1430. // Are they here for the first time?
  1431. if (empty($modSettings['currentAttachmentUploadDir']))
  1432. {
  1433. $modSettings['attachmentUploadDir'] = array(
  1434. 1 => $modSettings['attachmentUploadDir']
  1435. );
  1436. $modSettings['currentAttachmentUploadDir'] = 1;
  1437. }
  1438. // Otherwise just load up their attachment paths.
  1439. else
  1440. $modSettings['attachmentUploadDir'] = unserialize($modSettings['attachmentUploadDir']);
  1441. $listOptions = array(
  1442. 'id' => 'attach_paths',
  1443. 'base_href' => $scripturl . '?action=admin;area=manageattachments;sa=attachpaths;' . $context['session_var'] . '=' . $context['session_id'],
  1444. 'title' => $txt['attach_paths'],
  1445. 'get_items' => array(
  1446. 'function' => 'list_getAttachDirs',
  1447. ),
  1448. 'columns' => array(
  1449. 'current_dir' => array(
  1450. 'header' => array(
  1451. 'value' => $txt['attach_current_dir'],
  1452. ),
  1453. 'data' => array(
  1454. 'function' => create_function('$rowData', '
  1455. return \'<input type="radio" name="current_dir" value="\' . $rowData[\'id\'] . \'" \' . ($rowData[\'current\'] ? \'checked="checked"\' : \'\') . \' class="input_radio" />\';
  1456. '),
  1457. 'style' => 'text-align: center; width: 15%;',
  1458. ),
  1459. ),
  1460. 'path' => array(
  1461. 'header' => array(
  1462. 'value' => $txt['attach_path'],
  1463. ),
  1464. 'data' => array(
  1465. 'function' => create_function('$rowData', '
  1466. return \'<input type="text" size="30" name="dirs[\' . $rowData[\'id\'] . \']" value="\' . $rowData[\'path\'] . \'" class="input_text" style="width: 100%" />\';
  1467. '),
  1468. 'style' => 'text-align: center; width: 30%;',
  1469. ),
  1470. ),
  1471. 'current_size' => array(
  1472. 'header' => array(
  1473. 'value' => $txt['attach_current_size'],
  1474. ),
  1475. 'data' => array(
  1476. 'db' => 'current_size',
  1477. 'style' => 'text-align: center; width: 15%;',
  1478. ),
  1479. ),
  1480. 'num_files' => array(
  1481. 'header' => array(
  1482. 'value' => $txt['attach_num_files'],
  1483. ),
  1484. 'data' => array(
  1485. 'db' => 'num_files',
  1486. 'style' => 'text-align: center; width: 15%;',
  1487. ),
  1488. ),
  1489. 'status' => array(
  1490. 'header' => array(
  1491. 'value' => $txt['attach_dir_status'],
  1492. ),
  1493. 'data' => array(
  1494. 'db' => 'status',
  1495. 'style' => 'text-align: center; width: 25%;',
  1496. ),
  1497. ),
  1498. ),
  1499. 'form' => array(
  1500. 'href' => $scripturl . '?action=admin;area=manageattachments;sa=attachpaths;' . $context['session_var'] . '=' . $context['session_id'],
  1501. ),
  1502. 'additional_rows' => array(
  1503. array(
  1504. 'position' => 'below_table_data',
  1505. 'value' => '<input type="hidden" name="' . $context['session_var'] . '" value="' . $context['session_id'] . '" /><input type="submit" name="new_path" value="' . $txt['attach_add_path'] . '" class="button_submit" />&nbsp;<input type="submit" name="save" value="' . $txt['save'] . '" class="button_submit" />',
  1506. 'style' => 'text-align: right;',
  1507. ),
  1508. ),
  1509. );
  1510. require_once($sourcedir . '/Subs-List.php');
  1511. createList($listOptions);
  1512. // Fix up our template.
  1513. $context[$context['admin_menu_name']]['current_subsection'] = 'attachments';
  1514. $context['page_title'] = $txt['attach_path_manage'];
  1515. $context['sub_template'] = 'attachment_paths';
  1516. }
  1517. // Prepare the actual attachment directories to be displayed in the list.
  1518. function list_getAttachDirs()
  1519. {
  1520. global $smcFunc, $modSettings, $context, $txt;
  1521. // The dirs should already have been unserialized but just in case...
  1522. if (!is_array($modSettings['attachmentUploadDir']))
  1523. $modSettings['attachmentUploadDir'] = unserialize($modSettings['attachmentUploadDir']);
  1524. $request = $smcFunc['db_query']('', '
  1525. SELECT id_folder, COUNT(id_attach) AS num_attach
  1526. FROM {db_prefix}attachments' . (empty($modSettings['custom_avatar_enabled']) ? '' : '
  1527. WHERE attachment_type != {int:type_avatar}') . '
  1528. GROUP BY id_folder',
  1529. array(
  1530. 'type_avatar' => 1,
  1531. )
  1532. );
  1533. $expected_files = array();
  1534. while ($row = $smcFunc['db_fetch_assoc']($request))
  1535. $expected_files[$row['id_folder']] = $row['num_attach'];
  1536. $smcFunc['db_free_result']($request);
  1537. $attachdirs = array();
  1538. foreach ($modSettings['attachmentUploadDir'] as $id => $dir)
  1539. {
  1540. // If there aren't any attachments in this directory this won't exist.
  1541. if (!isset($expected_files[$id]))
  1542. $expected_files[$id] = 0;
  1543. // Check if the directory is doing okay.
  1544. list ($status, $error, $size) = attachDirStatus($dir, $expected_files[$id]);
  1545. $attachdirs[] = array(
  1546. 'id' => $id,
  1547. 'current' => $id == $modSettings['currentAttachmentUploadDir'],
  1548. 'path' => $dir,
  1549. 'current_size' => $size,
  1550. 'num_files' => $expected_files[$id],
  1551. 'status' => ($error ? '<span class="error">' : '') . sprintf($txt['attach_dir_' . $status], $context['session_id'], $context['session_var']) . ($error ? '</span>' : ''),
  1552. );
  1553. }
  1554. // Just stick a new directory on at the bottom.
  1555. if (isset($_REQUEST['new_path']))
  1556. $attachdirs[] = array(
  1557. 'id' => max(array_merge(array_keys($expected_files), array_keys($modSettings['attachmentUploadDir']))) + 1,
  1558. 'current' => false,
  1559. 'path' => '',
  1560. 'current_size' => '',
  1561. 'num_files' => '',
  1562. 'status' => '',
  1563. );
  1564. return $attachdirs;
  1565. }
  1566. // Checks the status of an attachment directory and returns an array of the status key, if that status key signifies an error, and the folder size.
  1567. function attachDirStatus($dir, $expected_files)
  1568. {
  1569. if (!is_dir($dir))
  1570. return array('does_not_exist', true, '');
  1571. elseif (!is_writable($dir))
  1572. return array('not_writable', true, '');
  1573. // Everything is okay so far, start to scan through the directory.
  1574. $dir_size = 0;
  1575. $num_files = 0;
  1576. $dir_handle = dir($dir);
  1577. while ($file = $dir_handle->read())
  1578. {
  1579. // Now do we have a real file here?
  1580. if (in_array($file, array('.', '..', '.htaccess', 'index.php')))
  1581. continue;
  1582. $dir_size += filesize($dir . '/' . $file);
  1583. $num_files++;
  1584. }
  1585. $dir_handle->close();
  1586. $dir_size = round($dir_size / 1024, 2);
  1587. if ($num_files < $expected_files)
  1588. return array('files_missing', true, $dir_size);
  1589. // Empty?
  1590. elseif ($expected_files == 0)
  1591. return array('unused', false, $dir_size);
  1592. // All good!
  1593. else
  1594. return array('ok', false, $dir_size);
  1595. }
  1596. ?>