/** * implements snapshot_symlink * * see snapshot() in module_glue.inc.php */ function image_snapshot_symlink($args) { $obj = $args['obj']; if (!isset($obj['type']) || $obj['type'] != 'image') { return false; } // consider the following: // * an image object is on page a, which at some point got distributed to all // other pages through symlinks // * we are now creating a snapshot of any page b, come across the symlink // pointing to an object and page a // in this case we don't copy the symlink but the current content of the // object, as we by all means want to preserve the current _state_ we copy // the symlink's content (this happens in snapshot() in module_glue.inc.php) // - thus turning it into a first-class object on the new snapshot-page // because of this we need to copy any referenced files as well from the // shared directory in page a to the one on page b, this happens in this // hook $dest_dir = CONTENT_DIR . '/' . array_shift(expl('.', $obj['name'])) . '/shared'; $src_dir = CONTENT_DIR . '/' . array_shift(expl('.', $args['origin'])) . '/shared'; // we do this for image-file and image-resized-file // .. to add a bit of complexity ;) foreach (array('image-file', 'image-resized-file') as $field) { if (empty($obj[$field])) { continue; } else { $src_file = $src_dir . '/' . $obj[$field]; } if (($f = dir_has_same_file($dest_dir, $src_file)) !== false) { $obj[$field] = $f; } else { // copy file $dest_file = $dest_dir . '/' . unique_filename($dest_dir, $src_file); $m = umask(0111); if (!@copy($src_file, $dest_file)) { umask($m); log_msg('error', 'image_snapshot_symlink: error copying referenced file ' . quot($src_file) . ' to ' . quot($dest_file)); return false; } umask($m); $obj[$field] = basename($dest_file); log_msg('info', 'image_snapshot_symlink: copied referenced file to ' . quot($dest_file)); } } // save changes in the object $ret = save_object($obj); if ($ret['#error']) { log_msg('error', 'image_snapshot_symlink: error saving object ' . quot($obj['name'])); return false; } else { return true; } }
function video_snapshot_symlink($args) { $obj = $args['obj']; if (!isset($obj['type']) || $obj['type'] != 'video') { return false; } $dest_dir = CONTENT_DIR . '/' . array_shift(expl('.', $obj['name'])) . '/shared'; $src_file = CONTENT_DIR . '/' . array_shift(expl('.', $args['origin'])) . '/shared/' . $obj['video-file']; if (($f = dir_has_same_file($dest_dir, $src_file)) !== false) { $obj['video-file'] = $f; } else { // copy file $dest_file = $dest_dir . '/' . unique_filename($dest_dir, $src_file); $m = umask(0111); if (!@copy($src_file, $dest_file)) { umask($m); log_msg('error', 'video_snapshot_symlink: error copying referenced file ' . quot($src_file) . ' to ' . quot($dest_file)); return false; } umask($m); $obj['video-file'] = basename($dest_file); log_msg('info', 'video_snapshot_symlink: copied referenced file to ' . quot($dest_file)); } $ret = save_object($obj); if ($ret['#error']) { log_msg('error', 'video_snapshot_symlink: error saving object ' . quot($obj['name'])); return false; } else { return true; } }
/** * move an uploaded file to the shared directory of a page * * this function reuses existing files when possible. * @param string $fn filename of newly uploaded file (most likely in /tmp) * @param string $page page or pagename * @param string $orig_fn the original filename on the client machine (optional) * @param bool &$existed set to true if the filename returned did already exist * before * @return filename inside the shared directory or false in case of error */ function upload_file($fn, $page, $orig_fn = '', &$existed = false) { // default to the temporary filename if ($orig_fn == '') { $orig_fn = $fn; } $a = expl('.', $page); if (count($a) < 1 || !is_dir(CONTENT_DIR . '/' . $a[0])) { log_msg('error', 'common: page ' . quot($page) . ' does not exist, cannot move uploaded file'); // not sure if we ought to remove the file in /tmp here (probably not) return false; } // create shared directory if it doesn't exist yet $d = CONTENT_DIR . '/' . $a[0] . '/shared'; if (!is_dir($d)) { $m = umask(00); if (!@mkdir($d, 0777)) { umask($m); log_msg('error', 'common: cannot create shared directory ' . quot($d) . ', cannot move uploaded file'); // not sure if we ought to remove the file in /tmp here (probably not) return false; } umask($m); } // check if file is already in shared directory if (($f = dir_has_same_file($d, $fn, $orig_fn)) !== false) { log_msg('info', 'common: reusing file ' . quot($f) . ' instead of newly uploaded file as they don\'t differ'); @unlink($fn); $existed = true; return $f; } else { // at least give it a unique name $f = unique_filename($d, basename($orig_fn)); $m = umask(0111); if (!@move_uploaded_file($fn, $d . '/' . $f)) { umask($m); log_msg('error', 'common: error moving uploaded file to ' . quot($d . '/' . $f)); // not sure if we ought to remove the file in /tmp here (probably not) return false; } else { umask($m); log_msg('info', 'common: moved uploaded file to ' . quot($d . '/' . $f)); $existed = false; return $f; } } }