public function dump_export_data() { foreach ($this->exporter->get('artefacts') as $artefact) { if (in_array($artefact->get('artefacttype'), PluginArtefactFile::get_artefact_types())) { $this->artefactdata[$artefact->get('id')] = $artefact; } } // Grab all parent folders of all artefacts selected so we can export // the files in their correct folder location if ($this->exporter->get('artefactexportmode') != PluginExport::EXPORT_ALL_ARTEFACTS && $this->artefactdata) { $folderids = array(); foreach (array_keys($this->artefactdata) as $artefactid) { $folderids = array_merge($folderids, array_keys(artefact_get_parents_for_cache($artefactid))); } $folderids = array_unique($folderids); foreach ($folderids as $folderid) { if (!isset($this->artefactdata[$folderid])) { $artefact = artefact_instance_from_id($folderid); // We grabbed all parents of the artefacts in the export, // but not all parents are folders if ($artefact->get('artefacttype') == 'folder') { $this->artefactdata[$folderid] = $artefact; } } } } $this->populate_profileicons(); $this->create_index_for_directory($this->fileroot, 0, null); $this->populate_filedir($this->fileroot, 0, null); }
public function dump_export_data() { global $SESSION; $this->owner = $this->exporter->get('user')->get('id'); foreach ($this->exporter->get('artefacts') as $artefact) { if (in_array($artefact->get('artefacttype'), PluginArtefactFile::get_artefact_types())) { $id = $artefact->get('id'); $this->artefactdata[$id] = $artefact; if ($artefact->get('owner') != $this->owner) { $this->otherfiles[$id] = $id; } } } // Grab all parent folders of all artefacts owned by the exporting user, so we can export // the files in their correct folder location if ($this->exporter->get('artefactexportmode') != PluginExport::EXPORT_ALL_ARTEFACTS && $this->artefactdata) { $folderids = array(); foreach (array_keys($this->artefactdata) as $artefactid) { if (!isset($this->otherfiles[$artefactid])) { $folderids = array_merge($folderids, array_keys(artefact_get_parents_for_cache($artefactid))); } } $folderids = array_unique($folderids); foreach ($folderids as $folderid) { if (!isset($this->artefactdata[$folderid])) { $artefact = artefact_instance_from_id($folderid); // We grabbed all parents of the artefacts in the export, // but not all parents are folders if ($artefact->get('artefacttype') == 'folder') { $this->artefactdata[$folderid] = $artefact; } } } } $this->populate_profileicons(); $this->create_index_for_directory($this->fileroot, 0, null); $this->populate_filedir($this->fileroot, 0, null); // Copy other users' files into the extrafileroot directory foreach ($this->otherfiles as $id) { if (!$this->artefactdata[$id] instanceof ArtefactTypeFile) { continue; } $dest = $this->extrafileroot . $id . '-' . PluginExportHtml::sanitise_path($this->artefactdata[$id]->get('title')); if (!copy($this->artefactdata[$id]->get_path(), $dest)) { $SESSION->add_error_msg(get_string('unabletocopyartefact', 'export', $this->artefactdata[$id]->get('title'))); } } }
function pieform_element_filebrowser_move(Pieform $form, $element, $data) { global $USER; $artefactid = $data['artefact']; // Artefact being moved $newparentid = $data['newparent']; // Folder to move it to try { $artefact = artefact_instance_from_id($artefactid); } catch (ArtefactNotFoundException $e) { $result = array('error' => true, 'message' => get_string('movingfailed', 'artefact.file'), 'newlist' => pieform_element_filebrowser_build_filelist($form, $element, $data['folder'])); return $result; } if (!$USER->can_edit_artefact($artefact)) { return array('error' => true, 'message' => get_string('movefailednotowner', 'artefact.file')); } if (!in_array($artefact->get('artefacttype'), PluginArtefactFile::get_artefact_types())) { return array('error' => true, 'message' => get_string('movefailednotfileartefact', 'artefact.file')); } if ($newparentid > 0) { if ($newparentid == $artefactid) { return array('error' => true, 'message' => get_string('movefaileddestinationinartefact', 'artefact.file')); } if ($newparentid == $artefact->get('parent')) { return array('error' => false, 'message' => get_string('filealreadyindestination', 'artefact.file')); } try { $newparent = artefact_instance_from_id($newparentid); } catch (ArtefactNotFoundException $e) { $parentfolder = $element['folder'] ? $element['folder'] : null; $result = array('error' => true, 'message' => get_string('movingfailed', 'artefact.file'), 'newlist' => pieform_element_filebrowser_build_filelist($form, $element, $data['folder'])); return $result; } if (!$USER->can_edit_artefact($newparent)) { return array('error' => true, 'message' => get_string('movefailednotowner', 'artefact.file')); } else { if ($newparent->get('locked')) { return array('error' => true, 'message' => get_string('cannoteditfoldersubmitted', 'artefact.file')); } } $group = $artefact->get('group'); if ($group && $group !== $newparent->get('group')) { return array('error' => true, 'message' => get_string('movefailednotowner', 'artefact.file')); } if ($newparent->get('artefacttype') != 'folder') { return array('error' => true, 'message' => get_string('movefaileddestinationnotfolder', 'artefact.file')); } $nextparentid = $newparent->get('parent'); while (!empty($nextparentid)) { if ($nextparentid != $artefactid) { $ancestor = artefact_instance_from_id($nextparentid); $nextparentid = $ancestor->get('parent'); } else { return array('error' => true, 'message' => get_string('movefaileddestinationinartefact', 'artefact.file')); } } } else { // $newparentid === 0 if ($artefact->get('parent') == null) { return array('error' => false, 'message' => get_string('filealreadyindestination', 'artefact.file')); } $group = $artefact->get('group'); if ($group) { if (!pieform_element_filebrowser_edit_group_folder($group, 0)) { return array('error' => true, 'message' => get_string('movefailednotowner', 'artefact.file')); } } $newparentid = null; } if ($oldparentid = $artefact->get('parent')) { $oldparent = artefact_instance_from_id($oldparentid); if ($oldparent->get('locked')) { return array('error' => true, 'message' => get_string('cannotremovefromsubmittedfolder', 'artefact.file')); } } if ($artefact->move($newparentid)) { return array('error' => false, 'newlist' => pieform_element_filebrowser_build_filelist($form, $element, $data['folder'])); } return array('error' => true, 'message' => get_string('movefailed', 'artefact.file')); }
/** * Retrieve file list in a folder * @global object $REMOTEWWWROOT * @param string $username * @param integer $folderid folder to browse * @return array The complete folder path + list of files for a specific Mahara folder */ function get_folder_files($username, $folderid) { global $REMOTEWWWROOT; //check that the user exists list($user, $authinstance) = find_remote_user($username, $REMOTEWWWROOT); if (!$user) { throw new ExportException("Could not find user {$username} for {$REMOTEWWWROOT}"); } $list = array(); safe_require('artefact', 'file'); $filetypes = array_diff(PluginArtefactFile::get_artefact_types(), array('profileicon')); foreach ($filetypes as $k => $v) { if ($v == 'folder') { unset($filetypes[$k]); } } $filetypesql = "('" . join("','", $filetypes) . "')"; $ownersql = artefact_owner_sql($user->id); $folderpath = array(); //the complete folder path (some client could need it) if (!empty($folderid)) { $pathsql = " AND parent = {$folderid}"; //build the path $parentids = artefact_get_parents_for_cache($folderid); //the closest parent is on the first key //the further parent is on the last key foreach ($parentids as $id => $dump) { $artefact = get_record('artefact', 'id', $id); array_unshift($folderpath, array('path' => $artefact->id, 'name' => $artefact->title)); } } else { $pathsql = "AND parent IS NULL"; } array_unshift($folderpath, array('path' => null, 'name' => 'Root')); //retrieve folders and files of a specific Mahara folder $list = array('files' => get_records_select_array('artefact', "artefacttype IN {$filetypesql} AND {$ownersql} {$pathsql}", array(), 'title'), 'folders' => get_records_select_array('artefact', "artefacttype = 'folder' AND {$ownersql} {$pathsql}", array(), 'title')); return array($folderpath, $list); }
/** * Return a unique artefact title for a given owner & parent. * * Try to add digits before the filename extension: If the desired * title contains a ".", add "." plus digits before the final ".", * otherwise append "." and digits. * * @param string $desired * @param integer $parent * @param integer $owner * @param integer $group * @param string $institution */ public static function get_new_file_title($desired, $parent, $owner = null, $group = null, $institution = null) { $bits = split('\\.', $desired); if (count($bits) > 1 && preg_match('/[^0-9]/', end($bits))) { $start = join('.', array_slice($bits, 0, count($bits) - 1)); $end = '.' . end($bits); } else { $start = $desired; $end = ''; } $where = $parent ? "parent = {$parent}" : 'parent IS NULL'; $where .= ' AND ' . artefact_owner_sql($owner, $group, $institution); $taken = get_column_sql("\n SELECT title FROM {artefact}\n WHERE artefacttype IN ('" . join("','", array_diff(PluginArtefactFile::get_artefact_types(), array('profileicon'))) . "')\n AND title LIKE ? || '%' || ? AND " . $where, array($start, $end)); $taken = array_flip($taken); $i = 0; $newname = $start . $end; while (isset($taken[$newname])) { $i++; $newname = $start . '.' . $i . $end; } return $newname; }
public static function save_config_options($form, $values) { global $USER; $updatingquota = false; $oldquotalimit = get_config_plugin('artefact', 'file', 'quotanotifylimit'); if ($values['updateuserquotas'] && $values['defaultquota']) { set_field('usr', 'quota', $values['defaultquota'], 'deleted', 0); $updatingquota = true; } if ($values['updategroupquotas'] && $values['defaultgroupquota']) { set_field('group', 'quota', $values['defaultgroupquota'], 'deleted', 0); // We need to alert group admins that the group may now be over the threshold that wasn't before $sqlwhere = " ((g.quotaused / g.quota) * 100) "; if (is_postgres()) { $sqlwhere = " ((CAST(g.quotaused AS float) / CAST(g.quota AS float)) * 100) "; } if ($groups = get_records_sql_assoc("SELECT g.id, g.name, g.quota, " . $sqlwhere . " AS quotausedpercent FROM {group} g WHERE " . $sqlwhere . " >= ?", array($values['quotanotifylimit']))) { ArtefactTypeFile::notify_groups_threshold_exceeded($groups); } } set_config_plugin('artefact', 'file', 'defaultquota', $values['defaultquota']); set_config_plugin('artefact', 'file', 'defaultgroupquota', $values['defaultgroupquota']); set_config_plugin('artefact', 'file', 'institutionaloverride', $values['institutionaloverride']); set_config_plugin('artefact', 'file', 'maxquota', $values['maxquota']); set_config_plugin('artefact', 'file', 'maxquotaenabled', $values['maxquotaenabled']); set_config_plugin('artefact', 'file', 'profileiconwidth', $values['profileiconwidth']); set_config_plugin('artefact', 'file', 'profileiconheight', $values['profileiconheight']); set_config_plugin('artefact', 'file', 'uploadagreement', $values['uploadagreement']); set_config_plugin('artefact', 'file', 'usecustomagreement', $values['usecustomagreement']); set_config_plugin('artefact', 'file', 'resizeonuploadenable', $values['resizeonuploadenable']); set_config_plugin('artefact', 'file', 'resizeonuploaduseroption', $values['resizeonuploaduseroption']); set_config_plugin('artefact', 'file', 'resizeonuploadmaxwidth', $values['resizeonuploadmaxwidth']); set_config_plugin('artefact', 'file', 'resizeonuploadmaxheight', $values['resizeonuploadmaxheight']); set_config_plugin('artefact', 'file', 'folderdownloadkeepzipfor', $values['folderdownloadkeepzipfor']); set_config_plugin('artefact', 'file', 'quotanotifylimit', $values['quotanotifylimit']); set_config_plugin('artefact', 'file', 'quotanotifyadmin', $values['quotanotifyadmin']); if ($oldquotalimit != $values['quotanotifylimit'] || $updatingquota) { // We need to alert anyone that may now be over the threshold that wasn't before $sqlwhere = " ((u.quotaused / u.quota) * 100) "; if (is_postgres()) { $sqlwhere = " ((CAST(u.quotaused AS float) / CAST(u.quota AS float)) * 100) "; } if ($users = get_records_sql_assoc("SELECT u.id, u.quota, " . $sqlwhere . " AS quotausedpercent FROM {usr} u WHERE " . $sqlwhere . " >= ?", array($values['quotanotifylimit']))) { $notifyadmin = get_config_plugin('artefact', 'file', 'quotanotifyadmin'); ArtefactTypeFile::notify_users_threshold_exceeded($users, $notifyadmin); } else { if ($users = get_records_sql_assoc("SELECT * FROM {usr} u, {usr_account_preference} uap WHERE " . $sqlwhere . " < ? AND uap.usr = u.id AND uap.field = ? AND uap.value = ?", array($values['quotanotifylimit'], 'quota_exceeded_notified', '1'))) { foreach ($users as $user) { set_account_preference($user->id, 'quota_exceeded_notified', false); } } } } $data = new StdClass(); $data->name = 'uploadcopyright'; $data->content = $values['customagreement']; $data->mtime = db_format_timestamp(time()); $data->mauthor = $USER->get('id'); $data->institution = 'mahara'; if (record_exists('site_content', 'name', $data->name, 'institution', $data->institution)) { update_record('site_content', $data, array('name', 'institution')); } else { $data->ctime = db_format_timestamp(time()); insert_record('site_content', $data); } foreach (PluginArtefactFile::get_artefact_types() as $at) { set_config_plugin('artefact', 'file', 'commentsallowed' . $at, (int) in_array($at, $values['commentdefault'])); } }
$options = array(); if (empty($embedded)) { $options['forcedownload'] = true; } // Clean up the temp directory before creating anymore zip files. zip_clean_temp_dir(); // Home folder if ($folderid === 0) { if (function_exists('zip_open')) { global $USER; $userid = $USER->get('id'); $select = ' SELECT a.id, a.artefacttype, a.title'; $from = ' FROM {artefact} a'; $in = "('" . join("','", PluginArtefactFile::get_artefact_types()) . "')"; $where = "\n WHERE artefacttype IN {$in}"; $phvals = array(); if ($institution) { if ($institution == 'mahara' && !$USER->get('admin')) { // If non-admins are browsing site files, only let them see the public folder & its contents $publicfolder = ArtefactTypeFolder::admin_public_folder_id(); $where .= ' AND (a.path = ? OR a.path LIKE ?)'; $phvals = array("/{$publicfolder}", db_like_escape("/{$publicfolder}/") . '%'); } $where .= ' AND a.institution = ? AND a.owner IS NULL'; $phvals[] = $institution; } else { if ($groupid) {
function pieform_element_filebrowser_move(Pieform $form, $element, $data) { global $USER; $artefactid = $data['artefact']; // Artefact being moved $newparentid = $data['newparent']; // Folder to move it to $artefact = artefact_instance_from_id($artefactid); if (!$USER->can_edit_artefact($artefact)) { return array('error' => true, 'message' => get_string('movefailednotowner', 'artefact.file')); } if (!in_array($artefact->get('artefacttype'), PluginArtefactFile::get_artefact_types())) { return array('error' => true, 'message' => get_string('movefailednotfileartefact', 'artefact.file')); } if ($newparentid > 0) { if ($newparentid == $artefactid) { return array('error' => true, 'message' => get_string('movefaileddestinationinartefact', 'artefact.file')); } if ($newparentid == $artefact->get('parent')) { return array('error' => false, 'message' => get_string('filealreadyindestination', 'artefact.file')); } $newparent = artefact_instance_from_id($newparentid); if (!$USER->can_edit_artefact($newparent)) { return array('error' => true, 'message' => get_string('movefailednotowner', 'artefact.file')); } $group = $artefact->get('group'); if ($group && $group !== $newparent->get('group')) { return array('error' => true, 'message' => get_string('movefailednotowner', 'artefact.file')); } if ($newparent->get('artefacttype') != 'folder') { return array('error' => true, 'message' => get_string('movefaileddestinationnotfolder', 'artefact.file')); } $nextparentid = $newparent->get('parent'); while (!empty($nextparentid)) { if ($nextparentid != $artefactid) { $ancestor = artefact_instance_from_id($nextparentid); $nextparentid = $ancestor->get('parent'); } else { return array('error' => true, 'message' => get_string('movefaileddestinationinartefact', 'artefact.file')); } } } else { // $newparentid === 0 if ($artefact->get('parent') == null) { return array('error' => false, 'message' => get_string('filealreadyindestination', 'artefact.file')); } $group = $artefact->get('group'); if ($group) { // Use default grouptype artefact permissions to check if the // user can move a file to the group's root directory require_once get_config('libroot') . 'group.php'; $permissions = group_get_default_artefact_permissions($group); if (!$permissions[group_user_access($group)]->edit) { return array('error' => true, 'message' => get_string('movefailednotowner', 'artefact.file')); } } $newparentid = null; } if ($artefact->move($newparentid)) { return array('error' => false, 'newlist' => pieform_element_filebrowser_build_filelist($form, $element, $data['folder'])); } return array('error' => true, 'message' => get_string('movefailed', 'artefact.file')); }