public function redactor__upload() { if (!Auth::getCurrentMember()) { exit("Invalid Request"); } $path = Request::get('path'); $is_image = Request::get('is_image'); if (isset($path)) { $dir = Path::tidy(ltrim($path, '/') . '/'); if (isset($_POST['subfolder'])) { $dir .= $_POST['subfolder'] . '/'; } Folder::make($dir); $file_type = strtolower($_FILES['file']['type']); $file_info = pathinfo($_FILES['file']['name']); // pull out the filename bits $filename = $file_info['filename']; $ext = $file_info['extension']; // build filename $file = $dir . $filename . '.' . $ext; // check for dupes if (File::exists($file)) { $file = BASE_PATH . '/' . $dir . $filename . '-' . date('YmdHis') . '.' . $ext; } if (!Folder::isWritable($dir)) { Log::error('Upload failed. Directory "' . $dir . '" is not writable.', 'redactor'); echo json_encode(array('error' => "Redactor: Upload directory not writable.")); die; } if ($is_image && ($_FILES['file']['type'] == 'image/png' || $_FILES['file']['type'] == 'image/jpg' || $_FILES['file']['type'] == 'image/gif' || $_FILES['file']['type'] == 'image/jpeg')) { if (Request::get('resize', false)) { $image = Image::make($_FILES['file']['tmp_name']); $width = Request::get('width', null); $height = Request::get('height', null); $ratio = Request::get('ratio', true); $upsize = Request::get('upsize', false); $quality = Request::get('quality', '75'); $image->resize($width, $height, $ratio, $upsize)->save($file, $quality); } else { move_uploaded_file($_FILES['file']['tmp_name'], $file); } } else { move_uploaded_file($_FILES['file']['tmp_name'], $file); } $return = array('filelink' => Path::toAsset($file)); echo stripslashes(json_encode($return)); } else { echo json_encode(array('error' => "Redactor: Upload directory not set.")); } }
/** * Loads the content cache into the local cache variable if not done yet * * @param boolean $force Force this to load? * @return void * @throws Exception */ public static function loadCache($force = false) { if (!$force && self::$cache_loaded) { return; } self::$cache_loaded = true; self::$cache = unserialize(File::get(Path::tidy(BASE_PATH . "/_cache/_app/content/content.php"))); if (!is_array(self::$cache)) { // Attempt to make the cache folder. It doesn't hurt to try. Folder::make(BASE_PATH . '/_cache/'); // something has gone wrong, log a message and set to an empty array self::$cache = array(); Log::fatal('Could not find or access your cache. Try checking your file permissions.', 'core', 'ContentService'); throw new Exception('Could not find or access your cache. Try checking your file permissions.'); } }
public function redactor__upload() { if (!Statamic_Auth::get_current_user()) { exit("Invalid Request"); } $path = Request::get('path'); if (isset($path)) { $dir = Path::tidy(ltrim($path, '/') . '/'); if (isset($_POST['subfolder'])) { $dir .= $_POST['subfolder'] . '/'; } Folder::make($dir); $_FILES['file']['type'] = strtolower($_FILES['file']['type']); if ($_FILES['file']['type'] == 'image/png' || $_FILES['file']['type'] == 'image/jpg' || $_FILES['file']['type'] == 'image/gif' || $_FILES['file']['type'] == 'image/jpeg') { $file_info = pathinfo($_FILES['file']['name']); // pull out the filename bits $filename = $file_info['filename']; $ext = $file_info['extension']; // build filename $file = $dir . $filename . '.' . $ext; // check for dupes if (File::exists($file)) { $file = $dir . $filename . '-' . date('YmdHis') . '.' . $ext; } if (!Folder::isWritable($dir)) { Log::error('Upload failed. Directory "' . $dir . '" is not writable.', 'redactor'); echo json_encode(array('error' => "Redactor: Upload directory not writable.")); die; } copy($_FILES['file']['tmp_name'], $file); // display file $array = array('filelink' => Config::getSiteRoot() . $file); echo stripslashes(json_encode($array)); } else { echo json_encode(array('error' => "Redactor: Could not find directory: '{$dir}'")); } } else { echo json_encode(array('error' => "Redactor: Upload directory not set.")); } }
public function index() { /* |-------------------------------------------------------------------------- | Check for image |-------------------------------------------------------------------------- | | Transform just needs the path to an image to get started. If it exists, | the fun begins. | */ $image_src = $this->fetchParam('src', null, false, false, false); // Set full system path $image_path = Path::tidy(BASE_PATH . '/' . $image_src); // Check if image exists before doing anything. if (!File::isImage($image_path)) { Log::error("Could not find requested image to transform: " . $image_path, "core", "Transform"); return; } /* |-------------------------------------------------------------------------- | Resizing and cropping options |-------------------------------------------------------------------------- | | The first transformations we want to run is for size to reduce the | memory usage for future effects. | */ $width = $this->fetchParam('width', null, 'is_numeric'); $height = $this->fetchParam('height', null, 'is_numeric'); // resize specific $ratio = $this->fetchParam('ratio', true, false, true); $upsize = $this->fetchParam('upsize', true, false, true); // crop specific $pos_x = $this->fetchParam('pos_x', 0, 'is_numeric'); $pos_y = $this->fetchParam('pos_y', 0, 'is_numeric'); $quality = $this->fetchParam('quality', '75', 'is_numeric'); /* |-------------------------------------------------------------------------- | Action |-------------------------------------------------------------------------- | | Available actions: resize, crop, and guess. | | "Guess" will find the best fitting aspect ratio of your given width and | height on the current image automatically, cut it out and resize it to | the given dimension. | */ $action = $this->fetchParam('action', 'resize'); /* |-------------------------------------------------------------------------- | Extra bits |-------------------------------------------------------------------------- | | Delicious and probably rarely used options. | */ $angle = $this->fetchParam('rotate', false); $flip_side = $this->fetchParam('flip', false); $blur = $this->fetchParam('blur', false, 'is_numeric'); $pixelate = $this->fetchParam('pixelate', false, 'is_numeric'); $grayscale = $this->fetchParam('grayscale', false, false, true); // Silly brits $greyscale = $this->fetchParam('greyscale', $grayscale, false, true); /* |-------------------------------------------------------------------------- | Assemble filename and check for duplicate |-------------------------------------------------------------------------- | | We need to make sure we don't already have this image created, so we | defer any action until we've processed the parameters, which create | a unique filename. | */ // Find .jpg, .png, etc $extension = File::getExtension($image_path); // Filename with the extension removed so we can append our unique filename flags $stripped_image_path = str_replace('.' . $extension, '', $image_path); // The possible filename flags $parameter_flags = array('width' => $width, 'height' => $height, 'quality' => $quality, 'rotate' => $angle, 'flip' => $flip_side, 'pos_x' => $pos_x, 'pos_y' => $pos_y, 'blur' => $blur, 'pixelate' => $pixelate, 'greyscale' => $greyscale); // Start with a 1 character action flag $file_breadcrumbs = '-' . $action[0]; foreach ($parameter_flags as $param => $value) { if ($value) { $flag = is_bool($value) ? '' : $value; // don't show boolean flags $file_breadcrumbs .= '-' . $param[0] . $flag; } } // Allow converting filetypes (jpg, png, gif) $extension = $this->fetchParam('type', $extension); // Allow saving in a different directory $destination = $this->fetchParam('destination', Config::get('transform_destination', false), false, false, false); if ($destination) { $destination = Path::tidy(BASE_PATH . '/' . $destination); // Method checks to see if folder exists before creating it Folder::make($destination); $stripped_image_path = Path::tidy($destination . '/' . basename($stripped_image_path)); } // Reassembled filename with all flags filtered and delimited $new_image_path = $stripped_image_path . $file_breadcrumbs . '.' . $extension; // Check if we've already built this image before if (File::exists($new_image_path)) { return File::cleanURL($new_image_path); } /* |-------------------------------------------------------------------------- | Create Image |-------------------------------------------------------------------------- | | Transform just needs the path to an image to get started. The image is | created in memory so we can start manipulating it. | */ $image = Image::make($image_path); /* |-------------------------------------------------------------------------- | Perform Actions |-------------------------------------------------------------------------- | | This is fresh transformation. Time to work the magic! | */ if ($action === 'resize' && ($width || $height)) { $image->resize($width, $height, $ratio, $upsize); } if ($action === 'crop' && $width && $height) { $image->crop($width, $height, $pos_x, $pos_y); } if ($action === 'smart') { $image->grab($width, $height); } if ($angle) { $image->rotate($angle); } if ($flip_side === 'h' || $flip_side === 'v') { $image->flip($flip_side); } if ($greyscale) { $image->greyscale(); } if ($blur) { $image->blur($blur); } if ($pixelate) { $image->pixelate($pixelate); } /* |-------------------------------------------------------------------------- | Save |-------------------------------------------------------------------------- | | Get out of dodge! | */ $image->save($new_image_path, $quality); return File::cleanURL($new_image_path); }
/** * Upload a file. * * @param string $file Name of file * @param string $target target of file * @param string $filename Name of new file * @return bool **/ public static function upload($file, $destination, $add_root_variable = false, $renamed_file = false) { Folder::make($destination); $info = pathinfo($file['name']); $extension = $info['extension']; $filename = $renamed_file ?: $info['filename']; // build filename $new_filename = Path::assemble(BASE_PATH, $destination, $filename . '.' . $extension); // check for dupes if (File::exists($new_filename)) { $new_filename = Path::assemble(BASE_PATH, $destination, $filename . '-' . date('YmdHis') . '.' . $extension); } // Check if destination is writable if (!Folder::isWritable($destination)) { Log::error('Upload failed. Directory "' . $destination . '" is not writable.', 'core'); return null; } // write file move_uploaded_file($file['tmp_name'], $new_filename); return Path::toAsset($new_filename, $add_root_variable); }
public function markAsSpam($file) { $info = new SplFileInfo($file); $filename = $info->getFilename(); $directory = str_replace($filename, '', $file) . 'spam/'; Folder::make($directory); File::move($file, Path::assemble($directory, $filename)); }
/** * Upload file(s) * * @param string $destination Where the file is going * @param string $id The field took look at in the files array * @return array */ public static function uploadBatch($destination = null, $id = null) { $destination = $destination ?: Request::get('destination'); $id = $id ?: Request::get('id'); $files = self::standardizeFileUploads($_FILES); $results = array(); // Resizing configuration if ($resize = Request::get('resize')) { $width = Request::get('width', null); $height = Request::get('height', null); $ratio = Request::get('ratio', true); $upsize = Request::get('upsize', false); $quality = Request::get('quality', '75'); } // If $files[$id][0] exists, it means there's an array of images. // If there's not, there's just one. We want to change this to an array. if ( ! isset($files[$id][0])) { $tmp = $files[$id]; unset($files[$id]); $files[$id][] = $tmp; } // Process each image foreach ($files[$id] as $file) { // Image data $path = File::upload($file, $destination); $name = basename($path); // Resize if ($resize) { $image = \Intervention\Image\Image::make(Path::assemble(BASE_PATH, $path)); $resize_folder = Path::assemble($image->dirname, 'resized'); if ( ! Folder::exists($resize_folder)) { Folder::make($resize_folder); } $resize_path = Path::assemble($resize_folder, $image->basename); $path = Path::toAsset($resize_path); $name = basename($path); $image->resize($width, $height, $ratio, $upsize)->save($resize_path, $quality); } $results[] = compact('path', 'name'); } return $results; }
/** * Save submission to file * * @param array $data Array of values to store * @param array $config Array of configuration values * @param string $location Path to folder where submissions should be saved * @param string $prefix Filename prefix to use for submission file * @param string $suffix Filename suffix to use for submission file * @return void */ private function save($data, $config, $location, $is_spam = false) { if (array_get($this->config, 'master_killswitch')) { return; } $EXT = array_get($config, 'submission_save_extension', 'yaml'); // Clean up whitespace array_walk_recursive($data, function (&$value, $key) { $value = trim($value); }); if (!File::exists($location)) { Folder::make($location); } if ($format = array_get($config, 'filename_format')) { $now = time(); $time_variables = array('year' => date('Y', $now), 'month' => date('m', $now), 'day' => date('d', $now), 'hour' => date('G', $now), 'minute' => date('i', $now), 'minutes' => date('i', $now), 'second' => date('s', $now), 'seconds' => date('s', $now)); $available_variables = $time_variables + $data; $filename = Parse::template($format, $available_variables); } else { $filename = date('Y-m-d-Gi-s', time()); } if ($is_spam) { $location = $location . 'spam/'; Folder::make($location); } // Put it in the right folder $filename = $location . $filename; // Ensure a unique filename in the event two forms are submitted in the same second if (File::exists($filename . '.' . $EXT)) { for ($i = 1; $i < 60; $i++) { if (!file_exists($filename . '-' . $i . '.' . $EXT)) { $filename = $filename . '-' . $i; break; } } } $filename .= '.' . $EXT; if ($EXT === 'json') { File::put($filename, json_encode($data)); } else { File::put($filename, Yaml::dump($data) . '---'); } }
if (isset($form_data['new'])) { if ($form_data['type'] == 'date') { $date_or_datetime = Config::getEntryTimestamps() ? $datestamp . "-" . $timestamp : $datestamp; $file = $content_root . "/" . $path . "/" . $status_prefix . $date_or_datetime . "-" . $slug . "." . $content_type; } elseif ($form_data['type'] == 'number') { $slug = $numeric . "." . $slug; $file = $content_root . "/" . $path . "/" . $status_prefix . $slug . "." . $content_type; } elseif ($form_data['type'] == 'none') { if (!preg_match(Pattern::NUMERIC, $slug, $matches)) { $numeric = Statamic::get_next_numeric_folder($path); $slug = $numeric . "-" . $slug; } $file = $content_root . "/" . $path . "/" . $status_prefix . $slug . "/page." . $content_type; $file = Path::tidy($file); if (!File::exists(dirname($file))) { Folder::make(dirname($file)); } } else { $file = $content_root . "/" . $path . "/" . $form_data['original_slug'] . "." . $content_type; } $folder = $path; } else { $file = Path::assemble(Config::getContentRoot(), $path) . '.' . $content_type; } // load the original yaml if (isset($form_data['new'])) { $file_data = array(); } else { $page = basename($path); $folder = dirname($path); $file_data = Statamic::get_content_meta($page, $folder, true);
public function index() { /* |-------------------------------------------------------------------------- | Check for image |-------------------------------------------------------------------------- | | Transform just needs the path to an image to get started. If it exists, | the fun begins. | | The way to do this changes depending on whether its an internal or | external file. | */ $image_src = $this->fetchParam('src', null, false, false, false); // External URL if ($is_external = URL::isExternalUrl($image_src)) { $image_path = $image_src; // Check if file is an image before doing anything. // @TODO: Maybe check that the file exists. $img_info = pathinfo($image_src); $is_image = in_array($img_info['extension'], array('jpg', 'jpeg', 'png', 'gif')); if ( ! $is_image) { Log::error("Requested file is not an image: " . $image_path, "core", "Transform"); return; } } // Internal URL else { // Set full system path $image_path = Path::standardize(Path::fromAsset($image_src)); // Check if image exists before doing anything. if ( ! File::isImage($image_path)) { Log::error("Could not find requested image to transform: " . $image_path, "core", "Transform"); return; } } /* |-------------------------------------------------------------------------- | Resizing and cropping options |-------------------------------------------------------------------------- | | The first transformations we want to run is for size to reduce the | memory usage for future effects. | */ $width = $this->fetchParam('width', null, 'is_numeric'); $height = $this->fetchParam('height', null, 'is_numeric'); // resize specific $ratio = $this->fetchParam('ratio', true, false, true); $upsize = $this->fetchParam('upsize', true, false, true); // crop specific $pos_x = $this->fetchParam('pos_x', 0, 'is_numeric'); $pos_y = $this->fetchParam('pos_y', 0, 'is_numeric'); $quality = $this->fetchParam('quality', '75', 'is_numeric'); /* |-------------------------------------------------------------------------- | Action |-------------------------------------------------------------------------- | | Available actions: resize, crop, and guess. | | "Guess" will find the best fitting aspect ratio of your given width and | height on the current image automatically, cut it out and resize it to | the given dimension. | */ $action = $this->fetchParam('action', 'resize'); /* |-------------------------------------------------------------------------- | Extra bits |-------------------------------------------------------------------------- | | Delicious and probably rarely used options. | */ $angle = $this->fetchParam('rotate', false); $flip_side = $this->fetchParam('flip' , false); $blur = $this->fetchParam('blur', false, 'is_numeric'); $pixelate = $this->fetchParam('pixelate', false, 'is_numeric'); $greyscale = $this->fetchParam(array('greyscale', 'grayscale'), false, false, true); $watermark = $this->fetchParam('watermark', false, false, false, false); /* |-------------------------------------------------------------------------- | Assemble filename and check for duplicate |-------------------------------------------------------------------------- | | We need to make sure we don't already have this image created, so we | defer any action until we've processed the parameters, which create | a unique filename. | */ // Late modified time of original image $last_modified = ($is_external) ? false : File::getLastModified($image_path); // Find .jpg, .png, etc $extension = File::getExtension($image_path); // Filename with the extension removed so we can append our unique filename flags $stripped_image_path = str_replace('.' . $extension, '', $image_path); // The possible filename flags $parameter_flags = array( 'width' => $width, 'height' => $height, 'quality' => $quality, 'rotate' => $angle, 'flip' => $flip_side, 'pos_x' => $pos_x, 'pos_y' => $pos_y, 'blur' => $blur, 'pixelate' => $pixelate, 'greyscale' => $greyscale, 'modified' => $last_modified ); // Start with a 1 character action flag $file_breadcrumbs = '-'.$action[0]; foreach ($parameter_flags as $param => $value) { if ($value) { $flag = is_bool($value) ? '' : $value; // don't show boolean flags $file_breadcrumbs .= '-' . $param[0] . $flag; } } // Allow converting filetypes (jpg, png, gif) $extension = $this->fetchParam('type', $extension); // Allow saving in a different directory $destination = $this->fetchParam('destination', Config::get('transform_destination', false), false, false, false); if ($destination) { $destination = Path::tidy(BASE_PATH . '/' . $destination); // Method checks to see if folder exists before creating it Folder::make($destination); $stripped_image_path = Path::tidy($destination . '/' . basename($stripped_image_path)); } // Reassembled filename with all flags filtered and delimited $new_image_path = $stripped_image_path . $file_breadcrumbs . '.' . $extension; // Check if we've already built this image before if (File::exists($new_image_path)) { return Path::toAsset($new_image_path); } /* |-------------------------------------------------------------------------- | Create Image |-------------------------------------------------------------------------- | | Transform just needs the path to an image to get started. The image is | created in memory so we can start manipulating it. | */ $image = Image::make($image_path); /* |-------------------------------------------------------------------------- | Perform Actions |-------------------------------------------------------------------------- | | This is fresh transformation. Time to work the magic! | */ if ($action === 'resize' && ($width || $height) ) { $image->resize($width, $height, $ratio, $upsize); } if ($action === 'crop' && $width && $height) { $image->crop($width, $height, $pos_x, $pos_y); } if ($action === 'smart') { $image->grab($width, $height); } $resize = $this->fetchParam('resize', null); if ($resize) { $resize_options = Helper::explodeOptions($resize, true); $image->resize( array_get($resize_options, 'width'), array_get($resize_options, 'height'), array_get($resize_options, 'ratio', true), array_get($resize_options, 'upsize', true) ); } $crop = $this->fetchParam('crop', null); if ($crop) { $crop_options = Helper::explodeOptions($crop, true); $image->crop( array_get($crop_options, 'width'), array_get($crop_options, 'height'), array_get($crop_options, 'x'), array_get($crop_options, 'y') ); } if ($angle) { $image->rotate($angle); } if ($flip_side === 'h' || $flip_side === 'v') { $image->flip($flip_side); } if ($greyscale) { $image->greyscale(); } if ($blur) { $image->blur($blur); } if ($pixelate) { $image->pixelate($pixelate); } // Positioning options via ordered pipe settings: // source|position|x offset|y offset if ($watermark) { $watermark_options = Helper::explodeOptions($watermark); $source = Path::tidy(BASE_PATH . '/' . array_get($watermark_options, 0, null)); $anchor = array_get($watermark_options, 1, null); $pos_x = array_get($watermark_options, 2, 0); $pos_y = array_get($watermark_options, 3, 0); $image->insert($source, $pos_x, $pos_y, $anchor); } /* |-------------------------------------------------------------------------- | Save |-------------------------------------------------------------------------- | | Get out of dodge! | */ try { $image->save($new_image_path, $quality); } catch(Exception $e) { Log::fatal('Could not write new images. Try checking your file permissions.', 'core', 'Transform'); throw new Exception('Could not write new images. Try checking your file permissions.'); } return File::cleanURL($new_image_path); }
/** * Upload a file. * * @param string $file Name of file * @param string $destination Destination of file * @param string $filename Name of new file * @return bool **/ public static function upload($file, $destination, $filename = null) { Folder::make($destination); return move_uploaded_file($file, $destination . '/' . $filename); }