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 ''; }
function DBSave() { // validate some values before saving (fixes for mysql strict mode) if ($this->locked > 0) { return $this->TriggerLockedError(); } $this->cat_exclude_browser = (int) (!empty($this->cat_exclude_browser)); //$this->cat_required_level = intval($this->cat_required_level); $this->cat_parent = intval($this->cat_parent); $this->cat_num_files = intval($this->cat_num_files); $this->cat_num_files_total = intval($this->cat_num_files_total); return parent::DBSave(); }
function GenerateList(&$content, $categories, $list_args = null) { if (!empty($list_args)) { $this->current_list = (object) $list_args; unset($list_args); } $hia = WPFB_Core::$settings->hide_inaccessible; $sort = WPFB_Core::GetSortSql($this->current_list->file_order); if ($this->current_list->page_limit > 0) { // pagination $page = empty($_REQUEST['wpfb_list_page']) || $_REQUEST['wpfb_list_page'] < 1 ? 1 : intval($_REQUEST['wpfb_list_page']); $start = $this->current_list->page_limit * ($page - 1); } else { $start = -1; } $search_term = empty($_GET['wpfb_s']) ? null : stripslashes($_GET['wpfb_s']); if ($search_term || WPFB_Core::$file_browser_search) { // search wpfb_loadclass('Search'); $where = WPFB_Search::SearchWhereSql(WPFB_Core::$settings->search_id3, $search_term); } else { $where = '1=1'; } $num_total_files = 0; if (is_null($categories)) { // if null, just list all files! $files = WPFB_File::GetFiles2($where, $hia, $sort, $this->current_list->page_limit, $start); $num_total_files = WPFB_File::GetNumFiles2($where, $hia); foreach ($files as $file) { $content .= $file->GenTpl2($this->file_tpl_tag); } } else { if (!empty($this->current_list->cat_order)) { WPFB_Item::Sort($categories, $this->current_list->cat_order); } $cat = reset($categories); // get first category // here we check if single category and cat has at least one file (also secondary cat files!) if (count($categories) == 1 && $cat->cat_num_files > 0) { // single cat if (!$cat->CurUserCanAccess()) { return ''; } $where = "({$where}) AND " . WPFB_File::GetSqlCatWhereStr($cat->cat_id); $files = WPFB_File::GetFiles2($where, $hia, $sort, $this->current_list->page_limit, $start); $num_total_files = WPFB_File::GetNumFiles2($where, $hia); if ($this->current_list->cat_grouping && $num_total_files > 0) { $content .= $cat->GenTpl2($this->cat_tpl_tag); } foreach ($files as $file) { $content .= $file->GenTpl2($this->file_tpl_tag); } } else { // multi-cat // TODO: multi-cat list pagination does not work properly yet // special handling of categories that do not have files directly: list child cats! if (count($categories) == 1 && $cat->cat_num_files == 0) { $categories = $cat->GetChildCats(true, true); if (!empty($this->current_list->cat_order)) { WPFB_Item::Sort($categories, $this->current_list->cat_order); } } if ($this->current_list->cat_grouping) { // group by categories $n = 0; foreach ($categories as $cat) { if (!$cat->CurUserCanAccess()) { continue; } $num_total_files = max($nf = WPFB_File::GetNumFiles2("({$where}) AND " . WPFB_File::GetSqlCatWhereStr($cat->cat_id), $hia), $num_total_files); // TODO //if($n > $this->current_list->page_limit) break; // TODO!! if ($nf > 0) { $files = WPFB_File::GetFiles2("({$where}) AND " . WPFB_File::GetSqlCatWhereStr($cat->cat_id), $hia, $sort, $this->current_list->page_limit, $start); if (count($files) > 0) { $content .= $cat->GenTpl2($this->cat_tpl_tag); // check for file count again, due to pagination! foreach ($files as $file) { $content .= $file->GenTpl2($this->file_tpl_tag); } } } } } else { // this is not very efficient, because all files are loaded, no pagination! $all_files = array(); foreach ($categories as $cat) { if (!$cat->CurUserCanAccess()) { continue; } $all_files += WPFB_File::GetFiles2("({$where}) AND " . WPFB_File::GetSqlCatWhereStr($cat->cat_id), $hia, $sort); } $num_total_files = count($all_files); WPFB_Item::Sort($all_files, $sort); $keys = array_keys($all_files); if ($start == -1) { $start = 0; } $last = $this->current_list->page_limit > 0 ? min($start + $this->current_list->page_limit, $num_total_files) : $num_total_files; for ($i = $start; $i < $last; $i++) { $content .= $all_files[$keys[$i]]->GenTpl2($this->file_tpl_tag); } } } } return $num_total_files; }
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); } } } } }
function GenTpl2($tpl_tag = null, $load_js = true) { static $tpl_funcs = array('file' => array(), 'cat' => array()); if (empty($tpl_tag)) { $tpl_tag = 'default'; } if ($load_js) { WPFB_Core::$load_js = true; } $type = $this->is_file ? 'file' : 'cat'; if (empty($tpl_funcs[$type][$tpl_tag])) { $parsed_tpl = WPFB_Core::GetParsedTpl($this->is_file ? 'file' : 'cat', $tpl_tag); if (empty($parsed_tpl)) { return "Template {$type} :: {$tpl_tag} does not exist!"; } $tpl_funcs[$type][$tpl_tag] = WPFB_Core::CreateTplFunc($parsed_tpl); } self::$tpl_uid = defined('DOING_AJAX') && DOING_AJAX ? $this->GetId() . '' . round(microtime() * 1000) % 1000 : self::$tpl_uid + 1; return $tpl_funcs[$type][$tpl_tag]($this); }
} } wpfb_print_json($props); exit; case 'new-cat': if (!WPFB_Core::CurUserCanCreateCat()) { die('-1'); } wpfb_loadclass('Admin'); $result = WPFB_Admin::InsertCategory($_POST); if (isset($result['error']) && $result['error']) { wpfb_print_json(array('error' => $result['error'])); exit; } $cat = $result['cat']; $args = WPFB_Output::fileBrowserArgs($_POST['args']); $filesel = $args['type'] === 'fileselect'; $catsel = $args['type'] === 'catselect'; wpfb_print_json(array('error' => 0, 'id' => $cat->GetId(), 'name' => $cat->GetTitle(), 'id_str' => $args['idp'] . 'cat-' . $cat->cat_id, 'url' => $cat->GetUrl(), 'text' => WPFB_Output::fileBrowserCatItemText($catsel, $filesel, $cat, $args['onselect'], empty($_REQUEST['is_admin']) ? 'filebrowser' : 'filebrowser_admin'), 'classes' => $filesel || $catsel ? 'folder' : null)); exit; case 'change-category': wpfb_loadclass('File', 'Admin'); $item = WPFB_Item::GetById($_POST['id'], $_POST['type']); if ($item && $item->CurUserCanEdit()) { $res = $item->ChangeCategoryOrName($_POST['new_cat_id']); wpfb_print_json($res); } else { die('-1'); } exit; }
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; }
/** * * @global wpdb $wpdb * @staticvar boolean $init * @return type */ function TryScanLock() { global $wpdb; if (!self::$_scan_lock_init) { self::$_scan_lock_init = true; self::$_file_scan_locks = array(); self::$_cat_scan_locks = array(); register_shutdown_function(array(__CLASS__, '_removeScanLocks')); } if ($this->is_file) { $sla =& self::$_file_scan_locks; } else { $sla =& self::$_cat_scan_locks; } $table = $this->is_file ? $wpdb->wpfilebase_files : $wpdb->wpfilebase_cats; $prefix = $this->is_file ? 'file' : 'cat'; $slf = "{$prefix}_scan_lock"; $now = time(); $lock_time = $now + self::SCAN_LOCK_TIMEOUT; $id = $this->GetId(); // first check if we own the lock and update if necessary if (isset($sla[$id])) { //echo "TryLock $this @".__LINE__." isset"; return $lock_time > $sla[$id] ? $wpdb->update($table, array($slf => $lock_time), array("{$prefix}_id" => $id, $slf => $sla[$id])) && ($sla[$id] = $this->{$slf} = $lock_time) : true; } // actually try to set the lock and store it in $sla if success return $wpdb->query($wpdb->prepare("\n\t\t\tUPDATE `{$table}`\n\t\t\tSET `{$slf}` = %d\n\t\t\tWHERE (`{$prefix}_id` = %d) AND (`{$slf}` < %d)\n\t\t\t", $lock_time, $id, $now)) && ($sla[$id] = $this->{$slf} = $lock_time); }
static function CreateCatTree($file_path) { $rel_path = trim(substr($file_path, strlen(WPFB_Core::UploadDir())), '/'); $rel_dir = dirname($rel_path); if (empty($rel_dir) || $rel_dir == '.') { return 0; } $last_cat_id = 0; $dirs = explode('/', $rel_dir); foreach ($dirs as $dir) { if (empty($dir) || $dir == '.') { continue; } $cat = WPFB_Item::GetByName($dir, $last_cat_id); if ($cat != null && $cat->is_category) { $last_cat_id = $cat->cat_id; } else { $result = self::InsertCategory(array('add_existing' => true, 'cat_parent' => $last_cat_id, 'cat_folder' => $dir)); if (is_array($result) && !empty($result['error'])) { return $result; } elseif (empty($result['cat_id'])) { wp_die('Could not create category!'); } else { $last_cat_id = intval($result['cat_id']); } } } return $last_cat_id; }
function DBSave() { // validate some values before saving (fixes for mysql strict mode) if ($this->locked > 0) { return $this->TriggerLockedError(); } $ints = array('file_category', 'file_post_id', 'file_attach_order', 'file_wpattach_id', 'file_added_by', 'file_update_of', 'file_hits', 'file_ratings', 'file_rating_sum'); foreach ($ints as $i) { $this->{$i} = (int) $this->{$i}; } $this->file_offline = (int) (!empty($this->file_offline)); $this->file_direct_linking = (int) $this->file_direct_linking; $this->file_force_download = (int) (!empty($this->file_force_download)); if (empty($this->file_last_dl_time)) { $this->file_last_dl_time = '0000-00-00 00:00:00'; } $this->file_size = 0 + $this->file_size; $r = parent::DBSave(); return $r; }
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); 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)); } */ }
function CurUserCanEdit($user = null) { return parent::CurUserCanEdit($user); }
private static function actionChangeCategory($args) { wpfb_loadclass('File', 'Admin'); $item = WPFB_Item::GetById($args['id'], $args['type']); if ($item && $item->CurUserCanEdit()) { $res = $item->ChangeCategoryOrName($args['new_cat_id']); wp_send_json($res); } else { die('-1'); } }
function CurUserCanEdit($user = null) { return parent::CurUserCanEdit($user) && $this->CurUserCanAddFiles($user); }