false, 'TableClass' => "sortable simpletable", // default table class 'ImgsExentions' => "gif,jpg,jpeg,png,svg,svgz", 'MaxImgsCountAutoPreview' => 10, // how much images will be previewed automatically 'MaxImgSizeAutoPreview' => 1024, // max image size (KB) for be previewed automatically 'IgnoredFilenamesPatterns' => array( // filenames to be ignored (regex patterns) "th[0-9]{2}---", // this is for Mini (https://www.pmwiki.org/wiki/Cookbook/Mini) ), 'TextareaInsertCommands' => array ( '*' => 'Attach:%filename%', // makes no effect, just for logic clearing // 'gif' => 'Mini:%filename%'; // example ), 'TextareaInsertTemplate' => "
%TextareaInsertCommand%
", // if redefined, keep %TextareaInsertCommand% intact )); function HandlePhAttachman($HandleActions_pagename, $auth = 0){ // TRANSLATIONS (todo: completely remake using core principles) $msg_noPreview = "Too big file, or too much images attached: click to load preview."; $msg_noUploads = "There are no uploads yet."; $msgErr_UploadsDisabled = "AttachManager Error: uploads are not enabled in config.php."; $msgErr_noPermission = "AttachManager Error: current user has no permisson to handle 'Attachman' cook."; $msgErr_noPermissionDelete = "AttachManager Error: current user has no 'upload' permisson, so 'delete' is also unaviable."; $msgErr_noFileToDelete = "AttachManager Delete Error: file was not found."; $msgErr_DeleteFailure = "AttachManager Delete Error: could't delete file, check filesystem permissions."; $TH_File = "File"; $TH_Markup = "Markup"; $TH_Size = "Size (KB)"; $TH_Modification = "Modified"; $TH_Action = "Action"; // SOME DIRTY CODE // We need to use all this throught ?action=phAttachman (ajax) AND throught (:attachman:) page command in the same time using one codebase. // I don't know how to do it correct. First $HandleActions was done, serving ?action=phAttachman. // After this (:attachman:) command was binded to the same function 'HandlePhAttachman'. // That's why in the next condition some variables are redefined manually for second case only. if (is_array ($HandleActions_pagename)){ // it looks like we are working with (:attachman:) syntax $markupCall = true; global $pagename; $HandleActions_pagename = $pagename; global $HandleAuth; $auth = $HandleAuth['phAttachman']; } // CHECKS: // check if uploads are enabled global $EnableUpload; if ( !IsEnabled($EnableUpload,0)) $stop = "$msgErr_UploadsDisabled"; // check if current user can edit pages $page = RetrieveAuthPage($HandleActions_pagename, $auth, 0, READPAGE_CURRENT); if (!$page) $stop = "$msgErr_noPermission"; // exit() for ajax, return for (:attachman:) if ($stop) {if ($markupCall) return ("$stop"); else exit("$stop"); } // PREPARE global $phAttachman; $EnableDelete = $phAttachman['EnableDelete']; $TableClass = $phAttachman['TableClass']; $ImgsExentions = $phAttachman['ImgsExentions']; $MaxImgsCountAutoPreview = $phAttachman['MaxImgsCountAutoPreview']; $MaxImgSizeAutoPreview = $phAttachman['MaxImgSizeAutoPreview']; $IgnoredFilenamesPatterns = $phAttachman['IgnoredFilenamesPatterns']; $TextareaInsertCommands = $phAttachman['TextareaInsertCommands']; $TextareaInsertTemplate = $phAttachman['TextareaInsertTemplate']; // PROCESSING: scan and generate the table, delete files global $UploadDir, $UploadPrefixFmt, $UploadUrlFmt, $EnableDirectDownload; $uploadsFolder = FmtPageName("$UploadDir$UploadPrefixFmt", $HandleActions_pagename); $uploadsUrl = FmtPageName(IsEnabled($EnableDirectDownload, 1) // that's for PmiWiki inside subfolder, with EnableDirectDownload = 0 ? "$UploadUrlFmt$UploadPrefixFmt/" : "\$PageUrl?action=download&upname=", $HandleActions_pagename); $filelist = @array_diff(scandir($uploadsFolder), array('.', '..')); if (!$filelist) {if ($markupCall) return ("$msg_noUploads"); else exit("$msg_noUploads"); } // serve 'delete' action. Works only if called throuhgt ?action=phAttachman AND user has 'upload' permissions if (!$markupCall && $_REQUEST["deletefile"]){ $deletefile = "$uploadsFolder/".$_REQUEST["deletefile"]; $page = RetrieveAuthPage($HandleActions_pagename, "upload", 0, READPAGE_CURRENT); if (!$page) exit ("$msgErr_noPermissionDelete"); if (!is_file($deletefile)) exit($msgErr_noFileToDelete); if (!unlink($deletefile)) exit($msgErr_DeleteFailure); exit("1"); } // CSS-styles and JS-scripts global $HTMLFooterFmt; // must use Footer, as Header is already generated at that point $HTMLFooterFmt['styles']['phAttachman'] = " "; $HTMLFooterFmt['scripts']['phAttachman'] = " "; // create table foreach ($filelist as $key => $value) { // IgnoredFilenamesPatterns check foreach ($IgnoredFilenamesPatterns as $k => $v) { if (preg_match("/$v/",$value)) unset($filelist["$key"]); } } $result = " ".($EnableDelete ? "" : NULL)." "; foreach ($filelist as $key => $value) { // main table generation $FileExt = strtolower(pathinfo($value, PATHINFO_EXTENSION)); // get extension if (strpos($ImgsExentions, $FileExt) !== false) $FileType = 'image'; // here support for audio and video files could be added the same way if ($FileType == 'image') { if (round(filesize("$uploadsFolder/$value")/1024) < $MaxImgSizeAutoPreview && $MaxImgsCountAutoPreview){ $File = "
$value"; $MaxImgsCountAutoPreview--; } else $File = "$value"; } else $File = "$value"; $Markup = ""; if ($TextareaInsertCommands[$FileExt]) { $ActionAdd = $TextareaInsertCommands[$FileExt]; $ActionAdd = str_replace("%filename%","$value",$ActionAdd); $Markup .= str_replace("%TextareaInsertCommand%","$ActionAdd",$TextareaInsertTemplate); unset($ActionAdd); } $Markup .= str_replace("%TextareaInsertCommand%","Attach:$value",$TextareaInsertTemplate); $Size = round(filesize("$uploadsFolder/$value")/1024); $Date = date ("Y-m-d H:i:s",filemtime("$uploadsFolder/$value")); $Date = str_replace(" ", "
",$Date); $ActionDelete = "Delete"; $result .= " ".($EnableDelete ? "" : "")." "; unset($FileExt,$FileType,$File,$Markup,$Size,$Date,$ActionDelete); } $result .= "
$TH_File $TH_Markup $TH_Size $TH_Modification$TH_Action
$File $Markup $Size $Date$ActionDelete
"; if ($markupCall) return $result; else { $result .= $HTMLFooterFmt['styles']['phAttachman']; $result .= $HTMLFooterFmt['scripts']['phAttachman']; exit($result); } }