Exemple #1
0
 /**
  * upload a file as a image attach to a given anchor
  * to be used in custom "edit_as" script
  * 
  * @global string $context
  * @param object $anchor
  * @param array $file (from $_FILES)
  * @param bool $set_as_thumb
  * @param bool $put
  */
 public static function upload_to($anchor, $file, $set_as_thumb = false, $put = false)
 {
     global $context;
     // attach some image
     $path = Files::get_path($anchor->get_reference(), 'images');
     // $_REQUEST['action'] = 'set_as_icon'; // instruction for image::upload
     if (isset($file) && ($uploaded = Files::upload($file, $path, array('Image', 'upload')))) {
         // prepare image informations
         $image = array();
         $image['image_name'] = $uploaded;
         $image['image_size'] = $file['size'];
         $image['thumbnail_name'] = 'thumbs/' . $uploaded;
         $image['anchor'] = $anchor->get_reference();
         //$combined = array_merge($image, $_FILES);
         // post the image which was uploaded
         if ($image['id'] = Images::post($image)) {
             // successfull post
             $context['text'] .= '<p>' . i18n::s('Following image has been added:') . '</p>' . Codes::render_object('image', $image['id']) . '<br style="clear:left;" />' . "\n";
             // set image as icon and thumbnail
             if ($set_as_thumb) {
                 // delete former icon if any
                 /*if(isset($anchor->item['icon_url'])
                                                         && $anchor->item['icon_url']
                                                         && $match = Images::get_by_anchor_and_name($anchor->get_reference(), pathinfo($anchor->item['icon_url'],PATHINFO_BASENAME))) {
                 
                 
                                                     if($match['id'] != $image['id'])
                                                         Images::delete($match['id']);
                                                 }*/
                 $fields = array('thumbnail_url' => Images::get_thumbnail_href($image), 'icon_url' => Images::get_icon_href($image));
                 if ($put) {
                     $fields['id'] = $_REQUEST['id'];
                     $class = $anchor->get_static_group_class();
                     $class::put_attributes($fields);
                 } else {
                     $_REQUEST = array_merge($_REQUEST, $fields);
                 }
             }
         }
     }
 }
Exemple #2
0
 /**
  * create a referenced image
  *
  * @param array of entity attributes (e.g., 'Content-Disposition')
  * @param string image actual content
  * @param array poster attributes
  * @param string the target anchor (e.g., 'article:123')
  * @param string reference of the object to be extended, if any
  * @return string reference to the created object, or NULL
  */
 public static function submit_image($entity_headers, $content, $user, $anchor, $target = NULL)
 {
     global $context;
     // retrieve queue parameters
     list($server, $account, $password, $allowed, $match, $section, $options, $hooks, $prefix, $suffix) = $context['mail_queue'];
     // locate content-disposition
     foreach ($entity_headers as $header) {
         if (preg_match('/Content-Disposition/i', $header['name'])) {
             $content_disposition = $header['value'];
             break;
         }
     }
     // find file name in content-disposition
     $file_name = '';
     if ($content_disposition && preg_match('/filename="*([a-zA-Z0-9\'\\(\\)\\+_,-\\.\\/:=\\? ]+)"*\\s*/i', $content_disposition, $matches)) {
         $file_name = $matches[1];
     }
     // as an alternative, look in content-type
     if (!$file_name) {
         // locate content-type
         foreach ($entity_headers as $header) {
             if (preg_match('/Content-Type/i', $header['name'])) {
                 $content_type = $header['value'];
                 break;
             }
         }
         // find file name in content-type
         if ($content_type && preg_match('/name="*([a-zA-Z0-9\'\\(\\)\\+_,-\\.\\/:=\\? ]+)"*\\s*/i', $content_type, $matches)) {
             $file_name = $matches[1];
         }
     }
     // as an alternative, look in content-description
     if (!$file_name) {
         // locate content-description
         foreach ($entity_headers as $header) {
             if (preg_match('/Content-Description/i', $header['name'])) {
                 $content_description = $header['value'];
                 break;
             }
         }
         // find file name in content-description
         $file_name = $content_description;
     }
     // sanity check
     if (!$file_name) {
         Logger::remember('agents/messages.php: No file name to use for submitted image');
         return NULL;
     }
     // file size
     $file_size = strlen($content);
     // sanity check
     if ($file_size < 7) {
         Logger::remember('agents/messages.php: Short image skipped', $file_name);
         return NULL;
     }
     // sanity check
     if (!$anchor) {
         Logger::remember('agents/messages.php: No anchor to use for submitted image', $file_name);
         return NULL;
     }
     // get anchor data -- this is a mutable object
     $host = Anchors::get($anchor, TRUE);
     if (!is_object($host)) {
         Logger::remember('agents/messages.php: Unknown anchor ' . $anchor, $file_name);
         return NULL;
     }
     // create target folders
     $file_path = Files::get_path($anchor, 'images');
     if (!Safe::make_path($file_path)) {
         Logger::remember('agents/messages.php: Impossible to create ' . $file_path);
         return NULL;
     }
     if (!Safe::make_path($file_path . '/thumbs')) {
         Logger::remember('agents/messages.php: Impossible to create ' . $file_path . '/thumbs');
         return NULL;
     }
     $file_path = $context['path_to_root'] . $file_path . '/';
     // save the entity in the file system
     if (!($file = Safe::fopen($file_path . $file_name, 'wb'))) {
         Logger::remember('agents/messages.php: Impossible to open ' . $file_path . $file_name);
         return NULL;
     }
     if (fwrite($file, $content) === FALSE) {
         Logger::remember('agents/messages.php: Impossible to write to ' . $file_path . $file_name);
         return NULL;
     }
     fclose($file);
     // get image information
     if (!($image_information = Safe::GetImageSize($file_path . $file_name))) {
         Safe::unlink($file_path . $file_name);
         Logger::remember('agents/messages.php: No image information in ' . $file_path . $file_name);
         return NULL;
     }
     // we accept only gif, jpeg and png
     if ($image_information[2] != 1 && $image_information[2] != 2 && $image_information[2] != 3) {
         Safe::unlink($file_path . $file_name);
         Logger::remember('agents/messages.php: Rejected image type for ' . $file_path . $file_name);
         return NULL;
     }
     // build a thumbnail
     $thumbnail_name = 'thumbs/' . $file_name;
     // do not stop on error
     include_once $context['path_to_root'] . 'images/image.php';
     if (!Image::shrink($file_path . $file_name, $file_path . $thumbnail_name, FALSE, FALSE)) {
         Logger::remember('agents/messages.php: No thumbnail has been created for ' . $file_path . $file_name);
     }
     // resize the image where applicable
     if (Image::adjust($file_path . $file_name, FALSE)) {
         $file_size = Safe::filesize($file_path . $file_name);
     }
     // all details
     $details = array();
     // image size
     if ($image_information = Safe::GetImageSize($file_path . $file_name)) {
         $details[] = i18n::c('Size') . ': ' . $image_information[0] . ' x ' . $image_information[1];
     }
     // update image description
     $item = array();
     $item['anchor'] = $anchor;
     $item['image_name'] = $file_name;
     $item['thumbnail_name'] = $thumbnail_name;
     $item['image_size'] = $file_size;
     $item['description'] = '';
     if (isset($content_description) && $content_description != $file_name) {
         $item['description'] .= $content_description;
     }
     if (@count($details)) {
         $item['description'] .= "\n\n" . '<p class="details">' . implode("<br />\n", $details) . "</p>\n";
     }
     $item['edit_date'] = gmstrftime('%Y-%m-%d %H:%M:%S', time());
     $item['edit_name'] = $user['nick_name'];
     $item['edit_id'] = $user['id'];
     $item['edit_address'] = $user['email'];
     // create an image record in the database
     include_once $context['path_to_root'] . 'images/images.php';
     if (!($item['id'] = Images::post($item))) {
         Logger::remember('agents/messages.php: Impossible to save image ' . $item['image_name']);
         return NULL;
     }
     if ($context['debug_messages'] == 'Y') {
         Logger::remember('agents/messages.php: Messages::submit_image()', $item, 'debug');
     }
     // insert the image in the anchor page
     $host->touch('image:create', $item['id'], TRUE);
     return 'image:' . $item['id'];
 }
Exemple #3
0
         // an image has been found
         if (Image::upload($node, $context['path_to_root'] . $file_path . '/', TRUE)) {
             $count++;
             // resize the image where applicable
             Image::adjust($context['path_to_root'] . $file_path . '/' . $node, TRUE, 'standard');
             // if the file does not exist yet
             if (!($item =& Images::get_by_anchor_and_name($anchor->get_reference(), $node))) {
                 // create a new image record for this file
                 $item = array();
                 $item['anchor'] = $anchor->get_reference();
                 $item['image_name'] = $node;
                 $item['thumbnail_name'] = 'thumbs/' . $node;
                 $item['image_size'] = Safe::filesize($file_path . '/' . $node);
                 $item['use_thumbnail'] = 'A';
                 // ensure it is always displayed as a clickable small image
                 $item['id'] = Images::post($item);
             }
             // ensure that the image is in anchor description field
             $nodes[$node] = $item['id'];
         }
     }
     Safe::closedir($handle);
     // embed uploaded images in alphabetical order
     ksort($nodes);
     foreach ($nodes as $name => $id) {
         $anchor->touch('image:create', $id);
     }
 }
 // clear floating thumbnails
 if ($count) {
     $anchor->touch('clear');