Style fixes on UploadFromUrl.php
[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 = null, $ignoreWarn = null, $async = 'async' ) {
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
55 if ( $async == 'async' ) {
56 $job = new UploadFromUrlJob( $title, $params );
57 return $job->insert();
58 } else {
59 $this->mUrl = trim( $url );
60 $this->comment = $comment;
61 $this->watchList = $watchList;
62 $this->ignoreWarnings = $ignoreWarn;
63 $this->mDesiredDestName = $title;
64 $this->getTitle();
65
66 return true;
67 }
68 }
69
70 /**
71 * Initialize a queued download
72 * @param $job Job
73 */
74 public function initializeFromJob( $job ) {
75 global $wgTmpDirectory;
76
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->ignoreWarnings = $job->params['ignorewarnings'];
83 $this->getTitle();
84 }
85
86 /**
87 * Entry point for SpecialUpload
88 * @param $request Object: WebRequest object
89 */
90 public function initializeFromRequest( &$request ) {
91 $desiredDestName = $request->getText( 'wpDestFile' );
92 if ( !$desiredDestName )
93 $desiredDestName = $request->getText( 'wpUploadFileURL' );
94 return $this->initialize(
95 $desiredDestName,
96 $request->getVal( 'wpUploadFileURL' ),
97 $request->getVal( 'wpUploadDescription' ),
98 $request->getVal( 'wpWatchThis' ),
99 $request->getVal( 'wpIgnoreWarnings' ),
100 'async'
101 );
102 }
103
104 /**
105 * @param $request Object: WebRequest object
106 */
107 public static function isValidRequest( $request ) {
108 global $wgUser;
109
110 $url = $request->getVal( 'wpUploadFileURL' );
111 return !empty( $url )
112 && Http::isValidURI( $url )
113 && $wgUser->isAllowed( 'upload_by_url' );
114 }
115
116 private function saveTempFile( $req ) {
117 $filename = tempnam( wfTempDir(), 'URL' );
118 if ( $filename === false ) {
119 return Status::newFatal( 'tmp-create-error' );
120 }
121 if ( file_put_contents( $filename, $req->getContent() ) === false ) {
122 return Status::newFatal( 'tmp-write-error' );
123 }
124
125 $this->mTempPath = $filename;
126 $this->mFileSize = filesize( $filename );
127
128 return Status::newGood();
129 }
130
131 public function retrieveFileFromUrl() {
132 $req = HttpRequest::factory( $this->mUrl );
133 $status = $req->execute();
134
135 if ( !$status->isOk() ) {
136 return $status;
137 }
138
139 $status = $this->saveTempFile( $req );
140 if ( !$status->isGood() ) {
141 return $status;
142 }
143 $this->mRemoveTempFile = true;
144
145 return $status;
146 }
147
148 public function doUpload() {
149 global $wgUser;
150
151 $status = $this->retrieveFileFromUrl();
152
153 if ( $status->isGood() ) {
154
155 $v = $this->verifyUpload();
156 if ( $v['status'] !== UploadBase::OK ) {
157 return $this->convertVerifyErrorToStatus( $v['status'], $v['details'] );
158 }
159
160 $status = $this->getLocalFile()->upload( $this->mTempPath, $this->comment,
161 $this->comment, File::DELETE_SOURCE, $this->mFileProps, false, $wgUser );
162 }
163
164 if ( $status->isGood() ) {
165 $file = $this->getLocalFile();
166
167 $wgUser->leaveUserMessage( wfMsg( 'successfulupload' ),
168 wfMsg( 'upload-success-msg', $file->getDescriptionUrl() ) );
169 } else {
170 $wgUser->leaveUserMessage( wfMsg( 'upload-failure-subj' ),
171 wfMsg( 'upload-failure-msg', $status->getWikiText() ) );
172 }
173
174 return $status;
175 }
176 }