/** * Accept a request to upload an image either via POST data (user upload) * or via flickr or google / wikimedia.org search. * * @param $src string with value 'upload', 'flickr' or 'wiki' * @return html outputs image details page */ private function uploadImage($src, $fromIIA = false) { global $wgRequest, $wgUser, $IP, $wgOut; $error = ''; $debugInfo = array(); if ($src == 'upload') { $tempname = TempLocalFile::createTempFilename(); $file = new TempLocalFile(Title::newFromText($tempname, NS_IMAGE), RepoGroup::singleton()->getLocalRepo()); $name = $wgRequest->getFileName('wpUploadFile'); $file->upload($wgRequest->getFileTempName('wpUploadFile'), '', ''); $comment = ''; $filesize = $file->getSize(); if (!$filesize) { $error = wfMsg('eiu-upload-error'); } } elseif ($fromIIA || $src == 'flickr' || $src == 'wiki') { $sourceName = $fromIIA || $src == 'flickr' ? 'Flickr' : 'Mediawiki Commons'; $tempname = TempLocalFile::createTempFilename(); $file = new TempLocalFile(Title::newFromText($tempname, NS_IMAGE), RepoGroup::singleton()->getLocalRepo()); $details = (array) json_decode($wgRequest->getVal('img-details')); $name = $details['name']; // scrape the file using curl $filename = '/tmp/tmp-curl-' . mt_rand(0, 100000000) . '.jpg'; $remoteFile = strlen($details['url_l']) ? $details['url_l'] : $details['url']; $ch = curl_init($remoteFile); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $fp = fopen($filename, 'w'); curl_setopt($ch, CURLOPT_FILE, $fp); $ret = curl_exec($ch); $err = curl_error($ch); curl_close($ch); fclose($fp); if ($err) { $debugInfo['curl'] = $err; } $filesize = @filesize($filename); if ($filesize) { if ($fromIIA || $src == 'flickr' || preg_match('@^http://[^/]*flickr@', $details['url'])) { require_once $IP . '/extensions/3rdparty/phpFlickr-2.3.1/phpFlickr.php'; $flickr = new phpFlickr(WH_FLICKR_API_KEY); $photo = $flickr->photos_getInfo($details['photoid']); $err = $flickr->getErrorMsg(); if ($err) { $debugInfo['flickrAPI'] = $err; } $license = $photo['license']; $username = $photo['owner']['username']; $comment = '{{flickr' . intval($license) . '|' . wfEscapeWikiText($details['photoid']) . '|' . wfEscapeWikiText($details['ownerid']) . '|' . wfEscapeWikiText($username) . '}}'; } else { $comment = self::getWPLicenseTag($details['url']); } // finish initializing the $file obj $status = $file->upload($filename, '', ''); if (!$status->ok) { $error = wfMsg('eiu-upload-error'); } } else { $error = wfMsg('eiu-download-error', $sourceName); } } if ($error) { $html = EasyTemplate::html('eiu_file_error.tmpl.php', array('error' => $error)); $wgOut->addHTML($html); error_log("file from {$src} error msgs: " . print_r($debugInfo, true)); } else { $mwname = $tempname; if (!$fromIIA) { $props = array('src' => $src, 'name' => $name, 'mwname' => $mwname, 'is_image' => $file->media_type == 'BITMAP' || $file->media_type == 'DRAWING', 'width' => $file->width, 'height' => $file->height, 'upload_file' => $file, 'image_comment' => $comment, 'license' => $wgUser->getOption('image_license')); $html = EasyTemplate::html('eiu_image_details.tmpl.php', $props); $wgOut->addHTML($html); } else { $this->insertImage('', $name, $mwname, true, $comment); } } }
<form id="ConflictImageForm" method="post" name="ConflictImageForm"> <input id="eiu-image-details" type="hidden" name="image-details" value="" /> <input id="ImageUploadUseExisting" type="hidden" name="ImageUploadUseExisting" value="" /> <input id="ImageUploadRename" type="hidden" name="ImageUploadRename" value="" /> <input type="hidden" name="type" value="<?php echo isset($file) ? htmlspecialchars($file->media_type) : ''; ?> " /> <input type="hidden" name="ImageIsConflict" value="1" /> <input id="ImageAttribution" type="hidden" name="ImageAttribution" value="<?php echo htmlspecialchars($image_comment); ?> " /> <?php $file_temp = new TempLocalFile(Title::newFromText($mwname, NS_IMAGE), RepoGroup::singleton()->getLocalRepo()); $existing_title = Title::newFromText($name, NS_IMAGE); $file_existing = new LocalFile($existing_title, RepoGroup::singleton()->getLocalRepo()); print wfMsg('eiu-conflict-inf', $file_existing->getName()); ?> <input name="mwname" type="hidden" value="<?php echo htmlspecialchars($mwname); ?> " /> <br/><br/> <table cellspacing="0" class="ImageUploadConflictTable wh_block" width='100%'> <tr> <td> <h2><?php echo wfMsg('eiu-rename'); ?>