8f851b06f2c5c03597ae0011a9a8463049323347
[lhc/web/wiklou.git] / includes / upload / UploadFromUrl.php
1 <?php
2 /**
3 * @file
4 * @ingroup upload
5 *
6 * Implements uploading from a HTTP resource.
7 *
8 * @author Bryan Tong Minh
9 * @author Michael Dale
10 */
11 class UploadFromUrl extends UploadBase {
12 protected $mTempDownloadPath;
13 protected $comment, $watchList;
14
15 /**
16 * Checks if the user is allowed to use the upload-by-URL feature. If the
17 * user is allowed, pass on permissions checking to the parent.
18 */
19 public static function isAllowed( $user ) {
20 if( !$user->isAllowed( 'upload_by_url' ) )
21 return 'upload_by_url';
22 return parent::isAllowed( $user );
23 }
24
25 /**
26 * Checks if the upload from URL feature is enabled
27 */
28 public static function isEnabled() {
29 global $wgAllowCopyUploads;
30 return $wgAllowCopyUploads && parent::isEnabled();
31 }
32
33 /**
34 * Entry point for API upload
35 */
36 public function initialize( $name, $url, $comment, $watchlist ) {
37 global $wgUser;
38
39 if( !Http::isValidURI( $url ) ) {
40 return Status::newFatal( 'http-invalid-url' );
41 }
42 $params = array(
43 "userName" => $wgUser->getName(),
44 "userID" => $wgUser->getID(),
45 "url" => trim( $url ),
46 "timestamp" => wfTimestampNow(),
47 "comment" => $comment,
48 "watchlist" => $watchlist);
49
50 $title = Title::newFromText( $name );
51 /* // Check whether the user has the appropriate permissions to upload anyway */
52 /* $permission = $this->isAllowed( $wgUser ); */
53
54 /* if ( $permission !== true ) { */
55 /* if ( !$wgUser->isLoggedIn() ) { */
56 /* return Status::newFatal( 'uploadnologintext' ); */
57 /* } else { */
58 /* return Status::newFatal( 'badaccess-groups' ); */
59 /* } */
60 /* } */
61
62 /* $permErrors = $this->verifyPermissions( $wgUser ); */
63 /* if ( $permErrors !== true ) { */
64 /* return Status::newFatal( 'badaccess-groups' ); */
65 /* } */
66
67
68 $job = new UploadFromUrlJob( $title, $params );
69 $job->insert();
70 }
71
72 /**
73 * Initialize a queued download
74 * @param $job Job
75 */
76 public function initializeFromJob( $job ) {
77 $this->mUrl = $job->params['url'];
78 $this->mTempPath = tempnam( $wgTmpDirectory, 'COPYUPLOAD' );
79 $this->mDesiredDestName = $job->title;
80 $this->comment = $job->params['comment'];
81 $this->watchList = $job->params['watchlist'];
82 $this->getTitle();
83 }
84
85 /**
86 * Entry point for SpecialUpload
87 * @param $request Object: WebRequest object
88 */
89 public function initializeFromRequest( &$request ) {
90 $desiredDestName = $request->getText( 'wpDestFile' );
91 if( !$desiredDestName )
92 $desiredDestName = $request->getText( 'wpUploadFileURL' );
93 return $this->initialize(
94 $desiredDestName,
95 $request->getVal( 'wpUploadFileURL' ),
96 false
97 );
98 }
99
100 /**
101 * @param $request Object: WebRequest object
102 */
103 public static function isValidRequest( $request ){
104 if( !$request->getVal( 'wpUploadFileURL' ) )
105 return false;
106 // check that is a valid url:
107 return Http::isValidURI( $request->getVal( 'wpUploadFileURL' ) );
108 }
109
110 private function saveTempFile( $req ) {
111 $filename = tempnam( wfTempDir(), 'URL' );
112 if ( $filename === false ) {
113 return Status::newFatal( 'tmp-create-error' );
114 }
115 if ( file_put_contents( $filename, $req->getContent() ) === false ) {
116 return Status::newFatal( 'tmp-write-error' );
117 }
118
119 $this->mTempPath = $filename;
120 $this->mFileSize = filesize( $filename );
121
122 return Status::newGood();
123 }
124
125 public function doUpload() {
126 global $wgUser;
127
128 $req = HttpRequest::factory($this->mUrl);
129 $status = $req->execute();
130
131 if( !$status->isOk() ) {
132 return $status;
133 }
134
135 $status = $this->saveTempFile( $req );
136 $this->mRemoveTempFile = true;
137
138 if( !$status->isOk() ) {
139 return $status;
140 }
141
142 $v = $this->verifyUpload();
143 if( $v['status'] !== UploadBase::OK ) {
144 return $this->convertVerifyErrorToStatus( $v['status'], $v['details'] );
145 }
146
147 // This has to come from API
148 /* $warnings = $this->checkForWarnings(); */
149 /* if( isset($warnings) ) return $warnings; */
150
151 // Use comment as initial page text by default
152 if ( is_null( $this->mParams['text'] ) ) {
153 $this->mParams['text'] = $this->mParams['comment'];
154 }
155
156 $file = $this->getLocalFile();
157 // This comes from ApiBase
158 /* $watch = $this->getWatchlistValue( $this->mParams['watchlist'], $file->getTitle() ); */
159
160 if ( !$status->isGood() ) {
161 return $status;
162 }
163
164 $status = $this->getLocalFile()->upload( $this->mTempPath, $this->comment,
165 $this->pageText, File::DELETE_SOURCE, $this->mFileProps, false, $wgUser );
166
167 return $status;
168 }
169 }