GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Xcloner_File_Transfer   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 6
Bugs 2 Features 0
Metric Value
eloc 48
c 6
b 2
f 0
dl 0
loc 133
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A curl_file_create() 0 11 4
A transfer_file() 0 71 5
A get_target() 0 3 1
A set_target() 0 3 1
1
<?php
2
/**
3
 * XCloner - Backup and Restore backup plugin for Wordpress
4
 *
5
 * class-xcloner-file-transfer.php
6
 * @author Liuta Ovidiu <[email protected]>
7
 *
8
 *        This program is free software; you can redistribute it and/or modify
9
 *        it under the terms of the GNU General Public License as published by
10
 *        the Free Software Foundation; either version 2 of the License, or
11
 *        (at your option) any later version.
12
 *
13
 *        This program is distributed in the hope that it will be useful,
14
 *        but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *        GNU General Public License for more details.
17
 *
18
 *        You should have received a copy of the GNU General Public License
19
 *        along with this program; if not, write to the Free Software
20
 *        Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21
 *        MA 02110-1301, USA.
22
 *
23
 * @link https://github.com/ovidiul/XCloner-Wordpress
24
 *
25
 * @modified 7/25/18 1:46 PM
26
 *
27
 */
28
29
class Xcloner_File_Transfer extends Xcloner_File_System
30
{
31
32
	/**
33
	 * Target url web address of the restore script
34
	 * @var string
35
	 */
36
	private $target_url;
37
	/**
38
	 * Transfer data limit in bytes
39
	 * @var int
40
	 */
41
	private $transfer_limit = 1048576; //bytes 1MB= 1048576 300KB = 358400
42
43
44
	/**
45
	 * @param $target_url
46
	 *
47
	 * @return mixed
48
	 */
49
	public function set_target($target_url)
50
	{
51
		return $this->target_url = $target_url;
52
	}
53
54
	/**
55
	 * @return string
56
	 */
57
	public function get_target()
58
	{
59
		return $this->target_url;
60
	}
61
62
63
	/**
64
	 * @param $file
65
	 * @param int $start
66
	 * @param string $hash
67
	 *
68
	 * @return bool|int
69
	 * @throws Exception
70
	 */
71
	public function transfer_file($file, $start = 0, $hash = "")
72
	{
73
		if (!$this->target_url) {
74
			throw new Exception("Please setup a target url for upload");
75
		}
76
77
78
		$fp = $this->get_storage_filesystem()->readStream($file);
79
80
		fseek($fp, $start, SEEK_SET);
81
82
		$binary_data = fread($fp, $this->transfer_limit);
83
84
		$tmp_filename = "xcloner_upload_".substr(md5(time()), 0, 5);
85
86
		$this->get_tmp_filesystem()->write($tmp_filename, $binary_data);
87
88
		$tmp_file_path = $this->get_tmp_filesystem_adapter()->applyPathPrefix($tmp_filename);
89
90
		$send_array = array();
91
92
		$send_array['file'] = $file;
93
		$send_array['start'] = $start;
94
		$send_array['xcloner_action'] = "write_file";
95
		$send_array['hash'] = $hash;
96
		#$send_array['blob'] 	= $binary_data;
97
		$send_array['blob'] = $this->curl_file_create($tmp_file_path, 'application/x-binary', $tmp_filename);
98
99
		//$data = http_build_query($send_array);
100
101
		$this->get_logger()->info(sprintf("Sending curl request to %s with %s data of file %s starting position %s using temporary file %s",
102
			$this->target_url, $this->transfer_limit, $file, $start, $tmp_filename));
103
104
105
		$ch = curl_init();
106
		curl_setopt($ch, CURLOPT_URL, $this->target_url);
107
108
		curl_setopt($ch, CURLOPT_POST, 1);
109
		//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
110
		//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
111
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
112
		curl_setopt($ch, CURLOPT_TIMEOUT, 1200);
113
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
114
115
		curl_setopt($ch, CURLOPT_POSTFIELDS, $send_array);
116
		curl_setopt($ch, CURLOPT_VERBOSE, true);
117
118
		$original_result = curl_exec($ch);
119
120
121
		$this->get_tmp_filesystem()->delete($tmp_filename);
122
123
		$result = json_decode($original_result);
124
125
		if (!$result) {
126
			throw new Exception("We have received no valid response from the remote host, original message: ".$original_result);
127
		}
128
129
		if ($result->status != 200) {
130
			throw new Exception($result->response);
131
		}
132
133
		if (ftell($fp) >= $this->get_storage_filesystem()->getSize($file)) {
134
			$this->get_logger()->info(sprintf("Upload done for file %s to target url %s, transferred a total of %s bytes",
135
				$file, $this->target_url, ftell($fp)));
136
			$this->remove_tmp_filesystem();
137
138
			return false;
139
		}
140
141
		return ftell($fp);
142
	}
143
144
	/**
145
	 * @param string $filename
146
	 * @param string $mimetype
147
	 * @param string $postname
148
	 *
149
	 * @return CURLFile|string
150
	 */
151
	private function curl_file_create($filename, $mimetype = '', $postname = '')
152
	{
153
		if (!function_exists('curl_file_create')) {
154
155
			return "@$filename;filename="
156
				. ($postname ?: basename($filename))
157
				. ($mimetype ? ";type=$mimetype" : '');
158
159
		} else {
160
161
			return curl_file_create($filename, $mimetype, $postname);
162
163
		}
164
	}
165
}
166