static function ProcessShortCode($args, $content = null, $tag = null) { $id = empty($args['id']) ? -1 : intval($args['id']); if ($id <= 0 && !empty($args['path'])) { // path indentification wpfb_loadclass('File', 'Category'); $args['id'] = $id = is_null($item = WPFB_Item::GetByPath($args['path'])) ? 0 : $item->GetId(); } switch ($args['tag']) { case 'list': return do_shortcode(self::FileList($args)); case 'file': wpfb_loadclass('File', 'Category'); if ($id > 0 && ($file = WPFB_File::GetFile($id)) != null && $file->CurUserCanAccess(true)) { return do_shortcode($file->GenTpl2($args['tpl'])); } else { break; } case 'fileurl': if ($id > 0 && ($file = wpfb_call('File', 'GetFile', $id)) != null) { if (empty($args['linktext'])) { return $file->GetUrl(); } if ($new_tab = $args['linktext'][0] == '*') { $args['linktext'] = substr($args['linktext'], 1); } return '<a href="' . $file->GetUrl() . '" ' . ($new_tab ? 'target="_blank"' : '') . '>' . $args['linktext'] . '</a>'; } else { break; } case 'attachments': return do_shortcode(self::PostAttachments(false, $args['tpl'])); case 'browser': $content = ''; self::FileBrowser($content, $id, 0); // by ref return $content; } return ''; }
static function ParseQuery(&$query) { // conditional loading of the search hooks global $wp_query; if (!empty($wp_query->query_vars['s'])) { wpfb_loadclass('Search'); } if (!empty($_GET['wpfb_s']) || !empty($_GET['s'])) { WPFB_Core::$file_browser_search = true; add_filter('the_excerpt', array(__CLASS__, 'SearchExcerptFilter'), 100); // must be lower than 11 (before do_shortcode) and after wpautop (>9) } // check if current post is file browser if (($id = self::GetPostId($query)) == WPFB_Core::$settings->file_browser_post_id) { wpfb_loadclass('File', 'Category'); if (!empty($_GET['wpfb_file'])) { self::$file_browser_item = WPFB_File::GetFile($_GET['wpfb_file']); } elseif (!empty($_GET['wpfb_cat'])) { self::$file_browser_item = WPFB_Category::GetCat($_GET['wpfb_cat']); } else { $url = (is_ssl() ? 'https' : 'http') . '://' . $_SERVER["HTTP_HOST"] . stripslashes($_SERVER['REQUEST_URI']); if (($qs = strpos($url, '?')) !== false) { $url = substr($url, 0, $qs); } // remove query string $path = trim(substr($url, strlen(WPFB_Core::GetPostUrl($id))), '/'); if (!empty($path)) { self::$file_browser_item = WPFB_Item::GetByPath(urldecode($path)); if (is_null(self::$file_browser_item)) { self::$file_browser_item = WPFB_Item::GetByPath($path); } } } } }
static function AddNewFiles($sync_data, $progress_bar = null, $max_batch_size = 0) { self::PrintDebugTrace(); $keys = array_keys($sync_data->new_files); $upload_dir = self::cleanPath(WPFB_Core::UploadDir()); $upload_dir_len = strlen($upload_dir); $batch_size = 0; $start_time = $cur_time = time(); foreach ($keys as $i) { $fn = $sync_data->new_files[$i]; $rel_path = substr($fn, $upload_dir_len); unset($sync_data->new_files[$i]); if (empty($fn)) { continue; } // skip files that where already added, for some reason if (is_null($ex_file = WPFB_Item::GetByPath($rel_path))) { self::PrintDebugTrace("add_existing_file:{$fn}"); $res = WPFB_Admin::AddExistingFile($fn, empty($sync_data->thumbnails[$fn]) ? null : $sync_data->thumbnails[$fn]); self::PrintDebugTrace("added_existing_file"); if (empty($res['error'])) { $sync_data->log['added'][] = empty($res['file']) ? substr($fn, $upload_dir_len) : $res['file']; $sync_data->known_filenames[] = $rel_path; if (!empty($res['file']) && $res['file']->GetThumbPath()) { $sync_data->known_filenames[] = substr(self::cleanPath($res['file']->GetThumbPath()), $upload_dir_len); } } else { $sync_data->log['error'][] = $res['error'] . " (file {$fn})"; } } else { //$res = array('file' => $ex_file); $sync_data->log['added'][] = $ex_file; $sync_data->known_filenames[] = $rel_path; } $sync_data->num_files_processed++; if (!empty($progress_bar)) { $progress_bar->step(); } if (!empty($res['file'])) { $batch_size += $res['file']->file_size; if ($max_batch_size > 0 && $batch_size > $max_batch_size) { return false; } } } if (!empty($progress_bar)) { $progress_bar->complete(); } return true; }
function ChangeCategoryOrName($new_cat_id, $new_name = null, $add_existing = false, $overwrite = false) { // 1. apply new values (inherit permissions if nothing (Everyone) set!) // 2. check for name collision and rename // 3. move stuff // 4. notify parents // 5. update child paths if (empty($new_name)) { $new_name = $this->GetName(); } elseif (!$add_existing) { $new_name = sanitize_file_name($new_name); } // also removes ()! $this->Lock(true); $new_cat_id = intval($new_cat_id); $old_cat_id = $this->GetParentId(); $old_path_rel = $this->GetLocalPathRel(true); $old_path = $this->GetLocalPath(); $old_name = $this->GetName(); if ($this->is_file) { $old_thumb_path = $this->GetThumbPath(); } $old_cat = $this->GetParent(); $new_cat = WPFB_Category::GetCat($new_cat_id); if (!$new_cat) { $new_cat_id = 0; } $cat_changed = $new_cat_id != $old_cat_id; if ($cat_changed && $new_cat_id > 0 && $this->IsAncestorOf($new_cat)) { return array('error' => __('Cannot move category into a sub-category of itself.', 'wp-filebase')); } // strip accents/umlauts if ($new_name != $old_name) { if ($this->is_file && $add_existing) { $this->file_name_original = rawurldecode($new_name); // expect utf8 chars to be urlencoded on disk, so decode them } else { $prev_new_name = $new_name; $new_name = remove_accents($new_name); // sanitize, but make sure not to strip too much $sani = sanitize_file_name($new_name); if (strlen($sani) >= 6) { $new_name = $sani; } if (wpfb_call('Misc', 'IsUtf8', $new_name)) { $new_name = rawurlencode($new_name); } if ($this->is_file) { $this->file_name_original = $prev_new_name; } } } // unset original name if equal to actual if ($this->is_file && $new_name === $this->file_name_original) { $this->file_name_original = ''; } if ($this->is_file) { $this->file_category = $new_cat_id; $this->file_name = $new_name; $this->file_category_name = $new_cat_id == 0 ? '' : $new_cat->GetTitle(); } else { $this->cat_parent = $new_cat_id; $this->cat_folder = $new_name; } // inherit user roles if (count($this->GetReadPermissions()) == 0) { $this->SetReadPermissions($new_cat_id != 0 ? $new_cat->GetReadPermissions() : WPFB_Core::$settings->default_roles); } // flush cache $this->last_parent_id = -1; $new_path_rel = $this->GetLocalPathRel(true); $new_path = $this->GetLocalPath(); if ($new_path_rel != $old_path_rel) { $i = 1; if (!$add_existing) { $name = $this->GetName(); if ($overwrite) { if (@file_exists($new_path)) { $ex_file = WPFB_File::GetByPath($new_path_rel); if (!is_null($ex_file)) { $ex_file->Remove(); } else { @unlink($new_path); } } } else { // rename item if filename collision (ignore if coliding with $this and ignore existing folders that does not belong to categories) while (@file_exists($new_path) && ($this->is_file || !is_dir($new_path) || !is_null(WPFB_Item::GetByPath($new_path_rel))) || !is_null($ex_file = WPFB_Item::GetByPath($new_path_rel)) && !$this->Equals($ex_file)) { $i++; if ($this->is_file) { $p = strrpos($name, '.'); $this->file_name = $p <= 0 ? "{$name}_{$i}" : substr($name, 0, $p) . "_{$i}" . substr($name, $p); } else { $this->cat_folder = "{$name}_{$i}"; } $new_path_rel = $this->GetLocalPathRel(true); $new_path = $this->GetLocalPath(); } } } // finally move it! if (!empty($old_name) && @file_exists($old_path)) { //echo "MOVING! $old_path -> $new_path"; if ($this->is_file) { if (!@rename($old_path, $new_path)) { return array('error' => sprintf('Unable to move file %s!', $old_path)); } @chmod($new_path, octdec(WPFB_PERM_FILE)); } else { if (!@is_dir($new_path)) { wp_mkdir_p($new_path); } wpfb_loadclass('FileUtils'); if (!@WPFB_FileUtils::MoveDir($old_path, $new_path)) { return array('error' => sprintf('Could not move folder %s to %s', $old_path, $new_path)); } } } else { if ($this->is_category) { if (!@is_dir($new_path) && !wp_mkdir_p($new_path)) { return array('error' => sprintf(__('Unable to create directory %s. Is it\'s parent directory writable?'), $new_path)); } } } // move thumb if ($this->is_file && !empty($old_thumb_path) && @is_file($old_thumb_path)) { $thumb_path = $this->GetThumbPath(); if ($i > 1) { $p = strrpos($thumb_path, '-'); if ($p <= 0) { $p = strrpos($thumb_path, '.'); } $thumb_path = substr($thumb_path, 0, $p) . "_{$i}" . substr($thumb_path, $p); $this->file_thumbnail = basename($thumb_path); } if (!is_dir(dirname($thumb_path))) { WPFB_Admin::Mkdir(dirname($thumb_path)); } if (!@rename($old_thumb_path, $thumb_path)) { return array('error' => 'Unable to move thumbnail! ' . $thumb_path); } @chmod($thumb_path, octdec(WPFB_PERM_FILE)); } $all_files = $this->is_file || $this->GetId() > 0 ? $this->GetChildFiles(true) : array(); // all children files (recursively) if (!empty($all_files)) { foreach ($all_files as $file) { if ($cat_changed) { if ($old_cat) { $old_cat->NotifyFileRemoved($file); } // notify parent cat to remove files if ($new_cat) { $new_cat->NotifyFileAdded($file); } } $file->GetLocalPathRel(true); // update file's path } } unset($all_files); if ($this->is_category && $this->GetId() > 0) { $cats = $this->GetChildCats(true); if (!empty($cats)) { foreach ($cats as $cat) { $cat->GetLocalPathRel(true); // update cats's path } } unset($cats); } } $this->Lock(false); if (!$this->locked) { $this->DBSave(); } return array('error' => false); /* * // create the directory if it doesnt exist // move file if($this->IsLocal() && !empty($old_file_path) && @is_file($old_file_path) && $new_file_path != $old_file_path) { if(!@rename($old_file_path, $new_file_path)) return array( 'error' => sprintf('Unable to move file %s!', $this->GetLocalPath())); @chmod($new_file_path, octdec(WPFB_PERM_FILE)); } */ }