PluginProbe
UpStream: a Project Management Plugin for WordPress / 2.1.0
UpStream: a Project Management Plugin for WordPress v2.1.0
trunk 1.39.0 1.39.1 1.39.2 1.39.3 2.0.7 2.1.0
upstream / includes / up-filesystem.php

up-filesystem.php in UpStream: a Project Management Plugin for WordPress 2.1.0, at includes/up-filesystem.php

137 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handle filesystem functions.
4 *
5 * @package UpStream
6 */
7
8 /**
9 * Load WordPress upgrade API.
10 */
11 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
12
13 /**
14 * Upstream_upfs_get_file_url
15 *
16 * @param string|mixed $value Download file (upfsid) query string.
17 */
18 function upstream_upfs_get_file_url( $value ) {
19 if ( ! is_string( $value ) ) {
20 return $value;
21 }
22
23 if ( substr( $value, 0, 7 ) === '_upfs__' ) {
24 // upfs file.
25 return add_query_arg( 'download', $value, get_post_type_archive_link( 'project' ) );
26 } elseif ( filter_var( $value, FILTER_VALIDATE_INT ) ) {
27 // an fid.
28 return wp_get_attachment_url( $value );
29 } else {
30 return $value;
31 }
32 }
33
34 /**
35 * Upstream_upfs_info
36 *
37 * @param string|mixed $value Download file (upfsid) query string.
38 */
39 function upstream_upfs_info( $value ) {
40 global $wpdb;
41 $res = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix . 'upfs_files WHERE upfsid=%s', $value ) );
42
43 if ( ! empty( $res ) ) {
44 foreach ( $res as $row ) {
45 return $row;
46 }
47 }
48
49 return null;
50 }
51
52 /**
53 * Upstream_upfs_download
54 *
55 * @param string|mixed $value Download file (upfsid) query string.
56 */
57 function upstream_upfs_download( $value ) {
58 global $wpdb;
59 $res = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix . 'upfs_files WHERE upfsid=%s', $value ) );
60
61 if ( ! empty( $res ) ) {
62 foreach ( $res as $row ) {
63 $path = upstream_filesystem_path() . '/' . $row->saved_filename;
64
65 if ( file_exists( $path ) ) {
66 header( 'Content-Description: File Transfer' );
67 header( 'Content-Disposition: attachment; filename=' . $row->orig_filename );
68 header( 'Expires: 0' );
69 header( 'Cache-Control: must-revalidate' );
70 header( 'Pragma: public' );
71 header( 'Content-Length: ' . filesize( $path ) );
72 header( 'Content-Type: ' . $row->mime_type );
73 readfile( $path );
74 }
75
76 exit();
77 }
78 }
79
80 exit();
81 }
82
83 /**
84 * Upstream_upfs_upload
85 *
86 * @param array|mixed $file File data.
87 */
88 function upstream_upfs_upload( $file ) {
89 $folder = upstream_filesystem_path();
90 $info = array();
91
92 if ( ! empty( $file ) ) {
93 if ( ! empty( $file['tmp_name'] ) ) {
94 // Checks the size of the the image.
95 if ( filesize( $file['tmp_name'] ) < upstream_filesystem_max_size() ) {
96
97 if ( is_dir( $folder ) ) {
98 $upfsid = '_upfs__' . upstream_generate_random_string( 50 );
99
100 // Moves the image to the created file.
101 if ( move_uploaded_file( $file['tmp_name'], $folder . '/' . $upfsid ) ) {
102 global $wpdb;
103
104 $sql = 'CREATE TABLE ' . $wpdb->prefix . 'upfs_files ( upfsid VARCHAR(60), orig_filename VARCHAR(255), saved_filename VARCHAR(255), mime_type VARCHAR(255), file_size INT, access_rules TEXT, PRIMARY KEY (upfsid) )';
105 $r = maybe_create_table( $wpdb->prefix . 'upfs_files', $sql );
106 $r = $wpdb->insert(
107 $wpdb->prefix . 'upfs_files',
108 array(
109 'upfsid' => $upfsid,
110 'orig_filename' => $file['name'],
111 'saved_filename' => $upfsid,
112 'mime_type' => $file['type'],
113 'file_size' => $file['size'],
114 'access_rules' => '',
115 )
116 );
117
118 return $upfsid;
119 } else {
120 unlink( $file['tmp_name'] );
121 array_push( $info, __( 'Unable to move file to target folder', 'upstream' ) );
122 }
123 } else {
124 unlink( $file['tmp_name'] );
125 array_push( $info, __( 'Unable to move file. The target folder does not exist.', 'upstream' ) );
126 }
127 } else {
128 array_push( $info, __( 'File exceeds the maximum file size', 'upstream' ) );
129 }
130 } else {
131 array_push( $info, __( 'File exceeds the maximum file size', 'upstream' ) );
132 }
133 }
134
135 return $info;
136 }
137