/**
  * 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)
 {
     global $wgRequest, $wgUser, $IP, $wgOut;
     $error = '';
     $debugInfo = array();
     if ($src == 'upload') {
         $tempname = self::createTempFilename();
         $tempUser = self::getTempFileUser();
         $file = new LocalFile(Title::newFromText($tempname, NS_IMAGE), RepoGroup::singleton()->getLocalRepo());
         $name = $wgRequest->getFileName('wpUploadFile');
         $comment = '';
         $file->upload($wgRequest->getFileTempName('wpUploadFile'), $comment, '', 0, false, false, $tempUser);
         $filesize = $file->getSize();
         if (!$filesize) {
             $error = wfMsg('eiu-upload-error');
         }
     } elseif ($src == 'flickr' || $src == 'wiki') {
         $sourceName = $src == 'flickr' ? 'Flickr' : 'Mediawiki Commons';
         $tempname = self::createTempFilename();
         $file = new LocalFile(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 ($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
             $tempUser = self::getTempFileUser();
             $status = $file->upload($filename, '', '', 0, false, false, $tempUser);
             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;
         $props = array('src' => $src, 'name' => $name, 'mwname' => $mwname, 'is_image' => $file->getMediaType() == 'BITMAP' || $file->getMediaType() == 'DRAWING', 'width' => $file->getWidth(), 'height' => $file->getHeight(), 'upload_file' => $file, 'image_comment' => $comment, 'license' => $wgUser->getOption('image_license'), 'file' => $file);
         $html = EasyTemplate::html('eiu_image_details.tmpl.php', $props);
         $wgOut->addHTML($html);
     }
 }