public function browse($dir = "") { if (!is_dir(Setting::get('repo.path'))) { mkdir(Setting::get('repo.path')); } $originaldir = $dir; $isRelativePath = false; if ($dir == "" || $dir == DIRECTORY_SEPARATOR) { $dir = $this->repoPath; $parent = ""; } else { if (strpos($this->repoPath, $dir) === 0) { $isRelativePath = true; } $dir = $this->repoPath . DIRECTORY_SEPARATOR . trim($dir, DIRECTORY_SEPARATOR); $parent = dirname($dir); } if (!realpath($dir) && $isRelativePath) { $dir = getcwd() . DIRECTORY_SEPARATOR . $dir; } $list = []; if (!is_dir($dir)) { if (RepoManager::getModuleDir() == $originaldir) { mkdir($dir); } else { return false; } } ## listing dir $list = []; if ($handle = opendir($dir)) { $count = 0; while (false !== ($entry = readdir($handle))) { if ($entry != "." && $entry != "..") { $path = RepoManager::resolve($dir . DIRECTORY_SEPARATOR . $entry); $perm = RepoManager::getPerms($path); $size = filesize($path); $path = $this->relativePath($dir . DIRECTORY_SEPARATOR . $entry); $path = substr($path, strlen($this->repoPath)); $list[] = ['name' => $entry, 'type' => $perm[0] == 'd' ? "dir" : "." . substr($entry, strrpos($entry, '.') + 1), 'size' => $size, 'downloadPath' => base64_encode($path), 'path' => $path]; $count++; } } closedir($handle); } usort($list, ['RepoManager', 'sortItem']); $count = count($list); if ($originaldir != "" && $originaldir != RepoManager::getModuleDir()) { $parent = $this->relativePath($parent); $parent = substr($parent, strlen($this->repoPath)); } else { $parent = ""; } $detail = ['parent' => $parent, 'path' => $this->relativePath($dir), 'type' => 'dir', 'item' => $list, 'count' => $count]; return $detail; }
public function actionRemove() { $postdata = file_get_contents("php://input"); $post = CJSON::decode($postdata); $file = base64_decode($post['file']); $file = RepoManager::resolve($file); @unlink($file); // unlink($file . '.json'); }
public function save() { $path = $this->uploadPath; $path = trim(RepoManager::createDir($path)); $valid = $this->validate(); if ($valid) { ## remove delimeter character from attributes $attr = $this->attributes; foreach ($attr as $k => $i) { $attr[$k] = str_replace($this->repoDef['delimeter'], "-", $i); } ## modify attributes, so it can be safely stored as filename foreach ($attr as $k => $a) { $attr[$k] = preg_replace("([^\\w\\s\\d\\-_~,;:\\[\\]\\(\\).]|[\\.]{2,})", '', $a); } extract($attr); ## get dir $pattern = $this->repoDef['pattern']; preg_match_all("/\\{(.*?)\\}/", $pattern, $blocks); foreach ($blocks[1] as $b) { $varname = '$' . explode(":", $b)[0]; $pattern = str_replace('{' . $b . '}', '{' . $varname . '}', $pattern); } eval('$dir = "' . $pattern . '";'); ## create dir $newdir = $path . "/" . $dir; $dirRenamed = false; if (!is_dir($newdir)) { if ($this->dirPath == "") { mkdir($newdir, 0777, true); } else { rename($path . "/" . $this->dirPath, $newdir); $dirRenamed = true; } } $oldDirPath = $path . "/" . $this->dirPath; $this->_dirpath = $dir; ## move uploaded file $fb = FormBuilder::load(get_class($this)); $fb->model = $this; $model = $this; foreach ($this->repoDef['fileFields'] as $fieldName) { if ($this->{$fieldName} != '') { ## find the FormField to get its file pattern $field = $fb->findField(['name' => $fieldName]); ## make sure file path is in the right path $this->{$fieldName} = RepoManager::resolve($this->{$fieldName}); ## get file extension $ext = pathinfo($this->{$fieldName}, PATHINFO_EXTENSION); ## file is uploaded if (is_file($this->{$fieldName})) { ## move the file to correct location eval('@rename($this->{$fieldName}, $newdir . "/' . $field['filePattern'] . '");'); ## assign new location to its var eval('$this->{$fieldName} = $newdir . "/' . $field['filePattern'] . '";'); if (isset($this->oldAttr[$fieldName])) { $old = RepoManager::resolve($this->oldAttr[$fieldName]); if ($old != $this->{$fieldName}) { @unlink($old); } } } else { if ($dirRenamed) { $this->{$fieldName} = $newdir . substr($this->{$fieldName}, strlen($oldDirPath)); } } ## change the path to relative path $this->{$fieldName} = RepoManager::getRelativePath($this->{$fieldName}); } } } return $valid; }