/** * This function handles filebrowser actions, such as uploading files, deleting files, creating folders, etc. * It piggybacks on the surrounding pieform but bypasses the normal Pieforms validation process. * * @param Pieform $form * @param array $element * @return mixed */ function pieform_element_filebrowser_doupdate(Pieform $form, $element) { require_once 'license.php'; $result = null; $prefix = $form->get_name() . '_' . $element['name']; // Since this is executed before normal pieforms validation, we'll redundantly call the validation here try { $sesskey = param_variable('sesskey', null); pieform_validate($form, array('sesskey' => $sesskey)); } catch (Exception $e) { return array('error' => true, 'message' => $e->getMessage()); } $delete = param_variable($prefix . '_delete', null); if (is_array($delete)) { $keys = array_keys($delete); return pieform_element_filebrowser_delete($form, $element, (int) $keys[0]); } $resizeonuploaduserenable = param_variable($prefix . '_resizeonuploaduserenable', null); if (!empty($resizeonuploaduserenable)) { $resizeimage = 1; } else { $resizeimage = 0; } $update = param_variable($prefix . '_update', null); if (is_array($update)) { $keys = array_keys($update); $artefactid = (int) $keys[0]; $edit_title = param_variable($prefix . '_edit_title'); $namelength = strlen($edit_title); if (!$namelength) { return array('edit' => $artefactid, 'error' => true, 'message' => get_string('filenamefieldisrequired1', 'artefact.file')); } else { if ($namelength > 1024) { return array('edit' => $artefactid, 'error' => true, 'message' => get_string('nametoolong', 'artefact.file')); } } $data = array('artefact' => $artefactid, 'title' => $edit_title, 'description' => param_variable($prefix . '_edit_description'), 'tags' => param_variable($prefix . '_edit_tags'), 'folder' => $element['folder'], 'allowcomments' => param_boolean($prefix . '_edit_allowcomments')); if (get_config('licensemetadata')) { $data = array_merge($data, array('license' => license_coalesce(null, param_variable($prefix . '_edit_license'), param_variable($prefix . '_edit_license_other', null)), 'licensor' => param_variable($prefix . '_edit_licensor'), 'licensorurl' => param_variable($prefix . '_edit_licensorurl'))); } if ($form->get_property('group')) { $data['permissions'] = array('admin' => (object) array('view' => true, 'edit' => true, 'republish' => true)); foreach ($_POST as $k => $v) { if (preg_match('/^' . $prefix . '_permission:([a-z]+):([a-z]+)$/', $k, $m)) { if (!isset($data['permissions'][$m[1]])) { $data['permissions'][$m[1]] = new stdClass(); } $data['permissions'][$m[1]]->{$m[2]} = (bool) $v; } } } return pieform_element_filebrowser_update($form, $element, $data); } $move = param_variable($prefix . '_move', null); if (!empty($move)) { return pieform_element_filebrowser_move($form, $element, array('artefact' => (int) $move, 'newparent' => param_integer($prefix . '_moveto'), 'folder' => $element['folder'])); } $createfolder = param_variable($prefix . '_createfolder', null); if (!empty($createfolder)) { $createfolder_name = param_variable($prefix . '_createfolder_name'); $namelength = strlen($createfolder_name); if (!$namelength) { return array('error' => true, 'message' => get_string('foldernamerequired', 'artefact.file')); } else { if ($namelength > 1024) { return array('error' => true, 'message' => get_string('nametoolong', 'artefact.file')); } } return pieform_element_filebrowser_createfolder($form, $element, array('title' => $createfolder_name, 'folder' => $element['folder'])); } // {$prefix}_upload is set in all browsers except safari when javascript is // on (and set in all browsers when it's not) $upload = param_variable($prefix . '_upload', null); if (!empty($upload)) { if (empty($_FILES['userfile']['name'])) { return array('error' => true, 'message' => get_string('filenamefieldisrequired', 'artefact.file'), 'browse' => 1); } else { if (is_array($_FILES['userfile']['name'])) { foreach ($_FILES['userfile']['name'] as $filename) { if (empty($filename)) { // TODO, how to specify which file is in error... return array('error' => true, 'message' => get_string('filenamefieldisrequired', 'artefact.file'), 'browse' => 1); } } } } } if (!empty($_FILES['userfile']['name'])) { if (!is_array($_FILES['userfile']['name'])) { if (!empty($_POST['_userfile']) && is_array($_POST['_userfile'])) { // renaming file for drag and drop $_FILES['userfile']['name'] = $_POST['_userfile']['name']; $_FILES['userfile']['type'] = $_POST['_userfile']['type']; } if (strlen($_FILES['userfile']['name']) > 1024) { return array('error' => true, 'message' => get_string('nametoolong', 'artefact.file')); } else { if ($element['config']['uploadagreement'] && !param_boolean($prefix . '_notice', false)) { return array('error' => true, 'message' => get_string('youmustagreetothecopyrightnotice', 'artefact.file'), 'browse' => 1); } } $data = array('userfile' => $_FILES['userfile'], 'uploadnumber' => param_integer($prefix . '_uploadnumber'), 'uploadfolder' => $element['folder'] ? $element['folder'] : null, 'uploadfoldername' => param_variable($prefix . '_foldername'), 'resizeonuploaduserenable' => $resizeimage); if (get_config('licensemetadata') && param_variable('dropzone')) { $data = array_merge($data, array('license' => license_coalesce(null, param_variable($prefix . '_license'), param_variable($prefix . '_license_other', null)), 'licensor' => param_variable($prefix . '_licensor'), 'licensorurl' => param_variable($prefix . '_licensorurl'))); } else { if (get_config('licensemetadata')) { $data = array_merge($data, array('license' => license_coalesce(null, param_variable($prefix . '_edit_license'), param_variable($prefix . '_edit_license_other', null)), 'licensor' => param_variable($prefix . '_edit_licensor'), 'licensorurl' => param_variable($prefix . '_edit_licensorurl'))); } } $result = pieform_element_filebrowser_upload($form, $element, $data); // If it's a non-js upload, automatically select the newly uploaded file. $result['browse'] = 1; if (!$form->submitted_by_js() && !$result['error'] && !empty($element['config']['select'])) { if (isset($element['selectcallback']) && is_callable($element['selectcallback'])) { $element['selectcallback']($result['highlight']); } else { $result['select'] = $result['highlight']; } } return $result; } else { if (!empty($_FILES['userfile']['name'][0])) { if ($element['config']['uploadagreement'] && !param_boolean($prefix . '_notice', false)) { return array('error' => true, 'message' => get_string('youmustagreetothecopyrightnotice', 'artefact.file'), 'browse' => 1); } $result = array('multiuploads' => array()); $size = sizeof($_FILES['userfile']['name']); for ($i = 0; $i < $size; $i++) { if (strlen($_FILES['userfile']['name'][$i]) > 1024) { return array('error' => true, 'message' => get_string('nametoolong', 'artefact.file')); } $data = array('userfile' => $_FILES['userfile'], 'userfileindex' => $i, 'uploadnumber' => param_integer($prefix . '_uploadnumber') - ($size - $i - 1), 'uploadfolder' => $element['folder'] ? $element['folder'] : null, 'uploadfoldername' => param_variable($prefix . '_foldername'), 'resizeonuploaduserenable' => $resizeimage); if (get_config('licensemetadata')) { $data = array_merge($data, array('license' => license_coalesce(null, param_variable($prefix . '_license'), param_variable($prefix . '_license_other', null)), 'licensor' => param_variable($prefix . '_licensor'), 'licensorurl' => param_variable($prefix . '_licensorurl'))); } $result['multiuploads'][$i] = pieform_element_filebrowser_upload($form, $element, $data); // TODO, what to do here... // If it's a non-js upload, automatically select the newly uploaded file. $result['multiuploads'][$i]['browse'] = 1; if (!$form->submitted_by_js() && !$result['multiuploads'][$i]['error'] && !empty($element['config']['select'])) { if (isset($element['selectcallback']) && is_callable($element['selectcallback'])) { $element['selectcallback']($result['multiuploads'][$i]['highlight']); } else { $result['multiuploads'][$i]['select'] = $result['multiuploads'][$i]['highlight']; } } $result['multiuploads'][$i]['folder'] = $element['folder']; } return $result; } } } if (!$form->submitted_by_js()) { $select = param_variable($prefix . '_select', null); if (is_array($select)) { $keys = array_keys($select); $add = (int) $keys[0]; if (isset($element['selectcallback']) && is_callable($element['selectcallback'])) { try { $element['selectcallback']($add); } catch (ArtefactNotFoundException $e) { $result = array('error' => true, 'message' => get_string('selectingfailed', 'artefact.file')); return $result; } } else { $result['select'] = $add; } $result['message'] = get_string('fileadded', 'artefact.file'); $result['browse'] = 1; return $result; } $unselect = param_variable($prefix . '_unselect', null); if (is_array($unselect)) { $keys = array_keys($unselect); $del = (int) $keys[0]; if (isset($element['unselectcallback']) && is_callable($element['unselectcallback'])) { try { $element['unselectcallback']($del); } catch (ArtefactNotFoundException $e) { $result = array('error' => true, 'message' => get_string('removingfailed', 'artefact.file')); return $result; } } else { $result['unselect'] = $del; } $result['message'] = get_string('fileremoved', 'artefact.file'); return $result; } $edit = param_variable($prefix . '_edit', null); if (is_array($edit)) { $keys = array_keys($edit); $result['edit'] = (int) $keys[0]; return $result; } if (param_variable('browse', 0) && !param_variable($prefix . '_cancelbrowse', 0)) { $result['browse'] = 1; return $result; } } $changeowner = param_variable($prefix . '_changeowner', null); if (!empty($changeowner)) { $result = pieform_element_filebrowser_changeowner($form, $element); $result['browse'] = 1; return $result; } $newfolder = param_variable($prefix . '_changefolder', null); if (!is_null($newfolder) && is_numeric($newfolder)) { $result = pieform_element_filebrowser_changefolder($form, $element, $newfolder); $result['browse'] = 1; $result['folder'] = $newfolder; return $result; } }
function pieform_element_filebrowser_doupdate(Pieform $form, $element) { $result = null; $prefix = $form->get_name() . '_' . $element['name']; $delete = param_variable($prefix . '_delete', null); if (is_array($delete)) { $keys = array_keys($delete); return pieform_element_filebrowser_delete($form, $element, (int) $keys[0]); } $update = param_variable($prefix . '_update', null); if (is_array($update)) { $edit_title = param_variable($prefix . '_edit_title'); $namelength = strlen($edit_title); if (!$namelength) { return array('error' => true, 'message' => get_string('filenamefieldisrequired', 'artefact.file')); } else { if ($namelength > 1024) { return array('error' => true, 'message' => get_string('nametoolong', 'artefact.file')); } } $keys = array_keys($update); $data = array('artefact' => (int) $keys[0], 'title' => $edit_title, 'description' => param_variable($prefix . '_edit_description'), 'tags' => param_variable($prefix . '_edit_tags'), 'folder' => $element['folder']); if ($form->get_property('group')) { $data['permissions'] = array('admin' => (object) array('view' => true, 'edit' => true, 'republish' => true)); foreach ($_POST as $k => $v) { if (preg_match('/^' . $prefix . '_permission:([a-z]+):([a-z]+)$/', $k, $m)) { $data['permissions'][$m[1]]->{$m[2]} = (bool) $v; } } } return pieform_element_filebrowser_update($form, $element, $data); } $move = param_variable($prefix . '_move', null); if (!empty($move)) { return pieform_element_filebrowser_move($form, $element, array('artefact' => (int) $move, 'newparent' => param_integer($prefix . '_moveto'), 'folder' => $element['folder'])); } $createfolder = param_variable($prefix . '_createfolder', null); if (!empty($createfolder)) { $createfolder_name = param_variable($prefix . '_createfolder_name'); $namelength = strlen($createfolder_name); if (!$namelength) { return array('error' => true, 'message' => get_string('foldernamerequired', 'artefact.file')); } else { if ($namelength > 1024) { return array('error' => true, 'message' => get_string('nametoolong', 'artefact.file')); } } return pieform_element_filebrowser_createfolder($form, $element, array('title' => $createfolder_name, 'folder' => $element['folder'])); } // {$prefix}_upload is set in all browsers except safari when javascript is // on (and set in all browsers when it's not) $upload = param_variable($prefix . '_upload', null); if (!empty($upload)) { if (empty($_FILES['userfile']['name'])) { return array('error' => true, 'message' => get_string('filenamefieldisrequired', 'artefact.file'), 'browse' => 1); } } if (isset($_FILES['userfile']['error']) && $_FILES['userfile']['error'] == 0) { if (strlen($_FILES['userfile']['name']) > 1024) { return array('error' => true, 'message' => get_string('nametoolong', 'artefact.file')); } else { if ($element['config']['uploadagreement'] && !param_boolean($prefix . '_notice', false)) { return array('error' => true, 'message' => get_string('youmustagreetothecopyrightnotice', 'artefact.file'), 'browse' => 1); } } $result = pieform_element_filebrowser_upload($form, $element, array('userfile' => $_FILES['userfile'], 'uploadnumber' => param_integer($prefix . '_uploadnumber'), 'uploadfolder' => $element['folder'] ? $element['folder'] : null, 'uploadfoldername' => param_variable($prefix . '_foldername'))); // If it's a non-js upload, automatically select the newly uploaded file. $result['browse'] = 1; if (!$form->submitted_by_js() && !$result['error'] && !empty($element['config']['select'])) { if (isset($element['selectcallback']) && is_callable($element['selectcallback'])) { $element['selectcallback']($result['highlight']); } else { $result['select'] = $result['highlight']; } } return $result; } if (!$form->submitted_by_js()) { $select = param_variable($prefix . '_select', null); if (is_array($select)) { $keys = array_keys($select); $add = (int) $keys[0]; if (isset($element['selectcallback']) && is_callable($element['selectcallback'])) { try { $element['selectcallback']($add); } catch (ArtefactNotFoundException $e) { $result = array('error' => true, 'message' => get_string('selectingfailed', 'artefact.file')); return $result; } } else { $result['select'] = $add; } $result['message'] = get_string('fileadded', 'artefact.file'); $result['browse'] = 1; return $result; } $unselect = param_variable($prefix . '_unselect', null); if (is_array($unselect)) { $keys = array_keys($unselect); $del = (int) $keys[0]; if (isset($element['unselectcallback']) && is_callable($element['unselectcallback'])) { try { $element['unselectcallback']($del); } catch (ArtefactNotFoundException $e) { $result = array('error' => true, 'message' => get_string('removingfailed', 'artefact.file')); return $result; } } else { $result['unselect'] = $del; } $result['message'] = get_string('fileremoved', 'artefact.file'); return $result; } $edit = param_variable($prefix . '_edit', null); if (is_array($edit)) { $keys = array_keys($edit); $result['edit'] = (int) $keys[0]; return $result; } if (param_variable('browse', 0) && !param_variable($prefix . '_cancelbrowse', 0)) { $result['browse'] = 1; return $result; } } $changeowner = param_variable($prefix . '_changeowner', null); if (!empty($changeowner)) { $result = pieform_element_filebrowser_changeowner($form, $element); $result['browse'] = 1; return $result; } $newfolder = param_variable($prefix . '_changefolder', null); if (!is_null($newfolder) && is_numeric($newfolder)) { $result = pieform_element_filebrowser_changefolder($form, $element, $newfolder); $result['browse'] = 1; $result['folder'] = $newfolder; return $result; } }