asgarosforum = $object; add_action('init', array($this, 'initialize')); } public function initialize() { $this->upload_folder = apply_filters('asgarosforum_filter_upload_folder', $this->upload_folder); $upload_dir = wp_upload_dir(); $this->upload_path = $upload_dir['basedir'].'/'.$this->upload_folder.'/'; $this->upload_url = $upload_dir['baseurl'].'/'.$this->upload_folder.'/'; $this->upload_allowed_filetypes = explode(',', $this->asgarosforum->options['allowed_filetypes']); } public function delete_post_files($post_id) { $path = $this->upload_path.$post_id.'/'; if (is_dir($path)) { $files = array_diff(scandir($path), array('.', '..')); foreach ($files as $file) { unlink($path.$file); } rmdir($path); } } // Check if its allowed to upload files with those extensions. public function check_uploads_extension() { if ($this->asgarosforum->options['allow_file_uploads'] && !empty($_FILES['forumfile'])) { if (!empty($_FILES['forumfile']['name'])) { $file_names = array_map('sanitize_file_name', $_FILES['forumfile']['name']); foreach ($file_names as $index => $tmpName) { if (empty($_FILES['forumfile']['error'][$index]) && !empty($file_names[$index])) { $file_extension = strtolower(pathinfo($file_names[$index], PATHINFO_EXTENSION)); if (!in_array($file_extension, $this->upload_allowed_filetypes)) { return false; } } } } } return true; } // Check if its allowed to upload files with those sizes. public function check_uploads_size() { if ($this->asgarosforum->options['allow_file_uploads'] && !empty($_FILES['forumfile'])) { if (!empty($_FILES['forumfile']['name'])) { $file_names = array_map('sanitize_file_name', $_FILES['forumfile']['name']); foreach ($file_names as $index => $tmpName) { if (!empty($_FILES['forumfile']['error'][$index]) && $_FILES['forumfile']['error'][$index] == 2) { return false; } else if (empty($_FILES['forumfile']['error'][$index]) && !empty($file_names[$index])) { $maximumFileSize = $this->get_maximum_size_in_bytes(); if ($maximumFileSize != 0 && $_FILES['forumfile']['size'][$index] > $maximumFileSize) { return false; } } } } } return true; } // Generates the list of new files to upload. public function get_upload_list() { $files = array(); if ($this->asgarosforum->options['allow_file_uploads'] && !empty($_FILES['forumfile'])) { if (!empty($_FILES['forumfile']['name'])) { $file_names = array_map('sanitize_file_name', $_FILES['forumfile']['name']); foreach ($file_names as $index => $tmpName) { if (empty($_FILES['forumfile']['error'][$index]) && !empty($file_names[$index])) { $name = $file_names[$index]; if (!empty($name)) { $files[$index] = $name; } } } } } return $files; } public function create_upload_folders($path) { if (!is_dir($this->upload_path)) { mkdir($this->upload_path); } if (!is_dir($path)) { mkdir($path); } } public function upload_files($post_id, $uploadList) { $path = $this->upload_path.$post_id.'/'; $links = array(); $files = $uploadList; // When there are files to upload, create the folders first. if (!empty($files)) { $this->create_upload_folders($path); } // Continue when the destination-folder exists. if (is_dir($path)) { // Register existing files. if (!empty($_POST['existingfile'])) { $existing_files = array_map('sanitize_file_name', $_POST['existingfile']); foreach ($existing_files as $file) { if (is_file($path.wp_basename($file))) { $links[] = $file; } } } // Remove deleted files. if (!empty($_POST['deletefile'])) { $deleted_files = array_map('sanitize_file_name', $_POST['deletefile']); foreach ($deleted_files as $file) { if (is_file($path.wp_basename($file))) { unlink($path.wp_basename($file)); } } } // Upload new files. if (!empty($files)) { $temporary_file_paths = array_map('sanitize_text_field', $_FILES['forumfile']['tmp_name']); foreach ($files as $index => $name) { move_uploaded_file($temporary_file_paths[$index], $path.$name); $links[] = $name; } } // Remove folder if it is empty. if (count(array_diff(scandir($path), array('.', '..'))) == 0) { rmdir($path); } } return $links; } public function show_uploaded_files($post_id, $post_uploads) { $path = $this->upload_path.$post_id.'/'; $url = $this->upload_url.$post_id.'/'; $uploads = maybe_unserialize($post_uploads); $uploadedFiles = ''; $output = ''; if (!empty($uploads) && is_dir($path)) { // Generate special message instead of file-list when hiding uploads for guests. if (!is_user_logged_in() && $this->asgarosforum->options['hide_uploads_from_guests']) { $uploadedFiles .= '