Exemplo n.º 1
0
 /**
  * Returns array('success'=>true) or array('error'=>'error message')
  */
 function handleUpload($uploadDirectory, $replaceOldFile = FALSE)
 {
     elgg_load_library('elgg:shoutout');
     if (!is_writable($uploadDirectory)) {
         return array('error' => "Server error. Upload directory isn't writable.");
     }
     if (!$this->file) {
         return array('error' => 'No files were uploaded.');
     }
     $size = $this->file->getSize();
     if ($size == 0) {
         return array('error' => 'File is empty');
     }
     if ($size > $this->sizeLimit) {
         return array('error' => 'File is too large');
     }
     $pathinfo = pathinfo($this->file->getName());
     $filename = $pathinfo['filename'];
     //$filename = md5(uniqid());
     $ext = $pathinfo['extension'];
     if ($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)) {
         $these = implode(', ', $this->allowedExtensions);
         return array('error' => 'File has an invalid extension, it should be one of ' . $these . '.');
     }
     if (!$replaceOldFile) {
         /// don't overwrite previous files that were uploaded
         while (file_exists($uploadDirectory . $filename . '.' . $ext)) {
             $filename .= rand(10, 99);
         }
     }
     $full_file_path = $uploadDirectory . $filename . '.' . $ext;
     if ($this->file->save($full_file_path)) {
         $guid = elgg_get_logged_in_user_guid();
         $upload_bits = explode('/', $uploadDirectory);
         $time_bit = $upload_bits[count($upload_bits) - 2];
         if (in_array($ext, array('png', 'jpg', 'jpeg', 'gif'))) {
             $thumb = elgg_get_site_url() . 'shoutout/temporary_thumb/' . $guid . '/' . $time_bit . '/' . $filename . '.' . $ext;
         } else {
             $mime_type = shoutout_determine_mime_type($full_file_path);
             $thumb = shoutout_get_file_icon_url($mime_type);
         }
         $delete = 'action/shoutout/attach/delete?guid=' . $guid . '&time_bit=' . $time_bit . '&filename=' . $filename . '.' . $ext;
         return array('success' => true, 'uploadDirectory' => $time_bit, 'thumb' => $thumb, 'delete' => $delete);
     } else {
         return array('error' => 'Could not save uploaded file.' . 'The upload was cancelled, or server error encountered');
     }
 }
Exemplo n.º 2
0
function shoutout_attachment_listing($entity_guid, $annotation)
{
    $body = '';
    $token = $annotation->value;
    if ($token) {
        $url = elgg_get_site_url();
        list($time_bit, $ofn) = explode('|', $annotation->value);
        $pathinfo = pathinfo($ofn);
        $fn = $pathinfo['filename'];
        $ext = $pathinfo['extension'];
        $title_bit = " title = \"{$ofn}\" alt=\"{$ofn}\" ";
        $body = '<div class="shoutout-attachment-listing-item">';
        if (in_array($ext, array('png', 'jpg', 'jpeg', 'gif'))) {
            $thumb = $url . 'shoutout/show_attachment_image/' . $annotation->id;
        } else {
            $full_file_path = elgg_get_data_path() . shoutout_attach_make_file_matrix($annotation->owner_guid) . '/attachments/' . $time_bit . '/' . $ofn;
            $mime_type = shoutout_determine_mime_type($full_file_path);
            $thumb = shoutout_get_file_icon_url($mime_type);
        }
        $image = '<img class="shoutout-attachment-image" ' . $title_bit . 'src="' . $thumb . '">';
        $body .= '<a href="' . $url . 'shoutout/download_attachment/' . $annotation->id . '">' . $image . ' ' . $ofn . '</a>';
        $body .= '</div>';
    }
    return $body;
}