Skip to content

Commit

Permalink
get file now use wp ajax
Browse files Browse the repository at this point in the history
  • Loading branch information
hunk committed Jun 4, 2016
1 parent 2087a42 commit b5db4a6
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 21 deletions.
127 changes: 127 additions & 0 deletions MF_GetFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

class MF_GetFile {

/**
* Constructor
*/
public function __construct() {
add_action( 'wp_ajax_mf_get_file', array( $this, 'getFile' ) );
}

function getFile() {

global $mf_domain;

check_ajax_referer( 'nonce_url_file', 'nonce_url_file');

if( !( is_user_logged_in() && current_user_can('upload_files') ) ) {
echo json_encode(
array(
'success' => false,
'error' => "You don't have permission to upload files, contact to the administrator for more information!",$mf_domain
)
);
wp_die();
}

if ( !isset($_POST['upload_url']) && empty($_POST['upload_url']) ) {
echo json_encode(
array(
'success' => false,
'error' => __("Url missing or empty",$mf_domain)
)
);
wp_die();
}

if (!$this->isValidUrl($_POST['upload_url'])) {
echo json_encode(
array(
'success' => false,
'error' => __("not a valid url format",$mf_domain)
)
);
wp_die();
}

if ( ( isset($_SERVER['HTTPS']) && 'on' == strtolower($_SERVER['HTTPS']) ) && empty($_COOKIE[SECURE_AUTH_COOKIE]) && !empty($_REQUEST['auth_cookie']) )
$_COOKIE[SECURE_AUTH_COOKIE] = $_REQUEST['auth_cookie'];
elseif ( empty($_COOKIE[AUTH_COOKIE]) && !empty($_REQUEST['auth_cookie']) )
$_COOKIE[AUTH_COOKIE] = $_REQUEST['auth_cookie'];

// file was send from browser
$_POST['upload_url'] = esc_url($_POST['upload_url']);
$filename = $this->downloadFile();

if ($filename == false) {
$result_msg = '<span class="mf-upload-error">'.__("Upload Unsuccessful",$mf_domain).'!</span>';
} else {
$result_msg = '<span class="mf-upload-success">'.__("Successful upload",$mf_domain).'!</span>' ;
}

if($filename){
echo json_encode(array('success'=>true, 'msg' => $result_msg."*".$filename));
}else{
echo json_encode(array('success'=>true, 'msg' => $result_msg."*"."None"));
}

wp_die();
}

function isValidUrl($url) {

$check = preg_replace(
'#((https?|ftp)://(\S*?\.\S*?))([\s)\[\]{},;"\':<]|\.\s|$)#i',
"'<a href=\"$1\" target=\"_blank\">$3</a>$4'",
$url
);

return $check;
}

function downloadFile(){
global $mf_domain, $wpdb;
$url = $_POST['upload_url'];

$allowedExtensions = array("pdf", "doc", "xls", "ppt", "txt", "jpeg", "psd", "jpg", "gif", "png", "docx", "pptx", "xslx", "pps", "zip", "gz", "gzip", "mp3", "aac", "mp4", "wav", "wma", "aif", "aiff", "ogg", "flv", "f4v", "mov", "avi", "mkv", "xvid", "divx","gpx");
$path = pathinfo($url);
$ext = $path['extension'];

if(!in_array(strtolower($ext), $allowedExtensions)){
echo json_encode(
array(
'success'=>false,
'error' => _("Invalid file extension",$mf_domain)
)
);
wp_die();
}

//Retrieve file
if ($fp_source = @fopen($url, 'rb')) {
//Get target filename
$exploded_url = explode('.', $url);
$ext = array_pop( $exploded_url );
$filename = time() . '_' . str_replace( 'rc_cwp_meta_', '', $_POST["input_name"]) . '.' . $ext;

$directory = MF_FILES_PATH;

$fp_dest = @fopen($directory . $filename,"wb");
if ($fp_dest == false) return false;
while(!feof($fp_source)) {
set_time_limit(30);
$readData = fread($fp_source, 1024*2);
fwrite($fp_dest,$readData);
}
fclose($fp_source);
fclose($fp_dest);

return $filename;
}
return false;
}
}

$mf_get_file = new MF_GetFile();

15 changes: 4 additions & 11 deletions Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
//Inflection class
require_once 'tools/inflect.php';

require_once 'MF_GetFile.php';

/**
* function for languages
*/
Expand Down Expand Up @@ -179,19 +181,10 @@ function tmce_not_remove_p_and_br(){
add_action( 'admin_print_footer_scripts', 'tmce_not_remove_p_and_br', 50 );
}
}

//Ajax
add_action( 'wp_ajax_mf1_call', 'mf_callback_ajax_call' );
if ( !function_exists('mf_callback_ajax_call') ) {
function mf_callback_ajax_call(){
echo "hello";
wp_die();
}
}


}



require_once ('RCCWP_Options.php');
require_once ('RCCWP_Query.php');
add_action('pre_get_posts', array('RCCWP_Query', 'FilterPrepare'));
Expand Down
2 changes: 1 addition & 1 deletion RCCWP_SWFUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static function Body($inputName, $fileType, $isCanvas = 0, $urlInputSize
type="text"
size="<?php echo $urlInputSize ?>"
class="mf-upload-url" />
<input type="button" onclick="uploadurl('<?php echo $idField ?>','<?php echo $fileType ?>','<?php echo wp_create_nonce("nonce_url_file") ?>')" value="Upload" class="button" style="width:70px"/>
<input type="button" onclick="uploadurl('<?php echo $idField ?>','<?php echo $fileType ?>','<?php echo wp_create_nonce("nonce_url_file") ?>')" value="Upload" class="button" style="width:70px"/>
</td>
</tr>
</table>
Expand Down
31 changes: 22 additions & 9 deletions js/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,25 @@ uploadurl = function(input_name,file_type,nonce){
progr.css('visibility','visible');
progr.css('height','auto');
progr.html("<img src="+mf_path+"images/spinner.gif /> Downlading File ...");


var data = {
'action': 'mf_get_file',
'upload_url': url,
'input_name': input_name,
'type': file_type,
'nonce_url_file': nonce
};

jQuery.ajax({
type: "POST",
url: ajaxurl,
type: 'POST',
async: false,
dataType: 'json',
data: "upload_url="+url+"&input_name="+input_name+"&type="+file_type+"&nonce="+nonce,
url: mf_path+'RCCWP_GetFile.php',
success: function(result){
data: data,
success: function(response){

if (result.success == true) {
h = result.msg.split("*");
if (response.success == true) {
h = response.msg.split("*");
progr.html(h[0]);
progr.show();
if(h[1] == "None"){
Expand Down Expand Up @@ -53,8 +62,12 @@ uploadurl = function(input_name,file_type,nonce){
}
} else {
progr.hide();
alert("Error: " + result.error);
alert("Error: " + response.error);
}
}

},
error: function(xhr,status,error){
alert(error);
}
});
}
1 change: 1 addition & 0 deletions tools/debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Debug
function log($msg,$path = "") {
if(empty($path)){
$path = dirname(__FILE__)."/../tmp/debug/";
$path = MF_FILES_PATH;
}

if(!is_string($msg)){
Expand Down

0 comments on commit b5db4a6

Please sign in to comment.