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