439254310601ce71b34b31e5d4c6ec60316d64dd
[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, $ignoreWarnings;
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, $ignoreWarn ) {
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 "ignorewarnings" => $ignoreWarn);
52
53 $title = Title::newFromText( $name );
54 /* // Check whether the user has the appropriate permissions to upload anyway */
55 /* $permission = $this->isAllowed( $wgUser ); */
56
57 /* if ( $permission !== true ) { */
58 /* if ( !$wgUser->isLoggedIn() ) { */
59 /* return Status::newFatal( 'uploadnologintext' ); */
60 /* } else { */
61 /* return Status::newFatal( 'badaccess-groups' ); */
62 /* } */
63 /* } */
64
65 /* $permErrors = $this->verifyPermissions( $wgUser ); */
66 /* if ( $permErrors !== true ) { */
67 /* return Status::newFatal( 'badaccess-groups' ); */
68 /* } */
69
70
71 $job = new UploadFromUrlJob( $title, $params );
72 return $job->insert();
73 }
74
75 /**
76 * Initialize a queued download
77 * @param $job Job
78 */
79 public function initializeFromJob( $job ) {
80 $this->mUrl = $job->params['url'];
81 $this->mTempPath = tempnam( $wgTmpDirectory, 'COPYUPLOAD' );
82 $this->mDesiredDestName = $job->title;
83 $this->comment = $job->params['comment'];
84 $this->watchList = $job->params['watchlist'];
85 $this->ignoreWarnings = $job->params['ignorewarnings'];
86 $this->getTitle();
87 }
88
89 /**
90 * Entry point for SpecialUpload
91 * @param $request Object: WebRequest object
92 */
93 public function initializeFromRequest( &$request ) {
94 $desiredDestName = $request->getText( 'wpDestFile' );
95 if( !$desiredDestName )
96 $desiredDestName = $request->getText( 'wpUploadFileURL' );
97 return $this->initialize(
98 $desiredDestName,
99 $request->getVal( 'wpUploadFileURL' ),
100 $request->getVal( 'wpUploadDescription' ),
101 $request->getVal( 'wpWatchThis' ),
102 $request->getVal( 'wpIgnoreWarnings' )
103 );
104 }
105
106 /**
107 * @param $request Object: WebRequest object
108 */
109 public static function isValidRequest( $request ){
110 global $wgUser;
111
112 $url = $request->getVal( 'wpUploadFileURL' );
113 return !empty( $url )
114 && Http::isValidURI( $url )
115 && $wgUser->isAllowed( 'upload_by_url' );
116 }
117
118 private function saveTempFile( $req ) {
119 $filename = tempnam( wfTempDir(), 'URL' );
120 if ( $filename === false ) {
121 return Status::newFatal( 'tmp-create-error' );
122 }
123 if ( file_put_contents( $filename, $req->getContent() ) === false ) {
124 return Status::newFatal( 'tmp-write-error' );
125 }
126
127 $this->mTempPath = $filename;
128 $this->mFileSize = filesize( $filename );
129
130 return Status::newGood();
131 }
132
133 public function doUpload() {
134 global $wgUser;
135
136 $req = HttpRequest::factory($this->mUrl);
137 $status = $req->execute();
138
139 if( !$status->isOk() ) {
140 return $status;
141 }
142
143 $status = $this->saveTempFile( $req );
144 $this->mRemoveTempFile = true;
145
146 if( !$status->isOk() ) {
147 return $status;
148 }
149
150 $v = $this->verifyUpload();
151 if( $v['status'] !== UploadBase::OK ) {
152 return $this->convertVerifyErrorToStatus( $v['status'], $v['details'] );
153 }
154
155 // This has to come from API
156 /* $warnings = $this->checkForWarnings(); */
157 /* if( isset($warnings) ) return $warnings; */
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->comment, File::DELETE_SOURCE, $this->mFileProps, false, $wgUser );
169
170 return $status;
171 }
172 }