asgarosforum = $object; add_action('init', array($this, 'initialize')); } public function initialize() { $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'])) { foreach ($_FILES['forumfile']['name'] as $index => $tmpName) { if (empty($_FILES['forumfile']['error'][$index]) && !empty($_FILES['forumfile']['name'][$index])) { $file_extension = strtolower(pathinfo($_FILES['forumfile']['name'][$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'])) { foreach ($_FILES['forumfile']['name'] as $index => $tmpName) { if (!empty($_FILES['forumfile']['error'][$index]) && $_FILES['forumfile']['error'][$index] == 2) { return false; } else if (empty($_FILES['forumfile']['error'][$index]) && !empty($_FILES['forumfile']['name'][$index])) { $maximumFileSize = (1024 * (1024 * $this->asgarosforum->options['uploads_maximum_size'])); 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'])) { foreach ($_FILES['forumfile']['name'] as $index => $tmpName) { if (empty($_FILES['forumfile']['error'][$index]) && !empty($_FILES['forumfile']['name'][$index])) { $name = sanitize_file_name(stripslashes($_FILES['forumfile']['name'][$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'])) { foreach ($_POST['existingfile'] as $file) { if (is_file($path.wp_basename($file))) { $links[] = $file; } } } // Remove deleted files. if (!empty($_POST['deletefile'])) { foreach ($_POST['deletefile'] as $file) { if (is_file($path.wp_basename($file))) { unlink($path.wp_basename($file)); } } } // Upload new files. if (!empty($files)) { foreach($files as $index => $name) { move_uploaded_file($_FILES['forumfile']['tmp_name'][$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 .= '