/** * Determine which class is needed to write to $context * * @param string $context */ public static function get_filesystem_method($context) { while (!file_exists($context)) { $context = \gp\tool::DirName($context); } if (gp_is_writable($context)) { return 'gp_filesystem_direct'; } if (function_exists('ftp_connect')) { return 'gp_filesystem_ftp'; } }
/** * Get Archive Root * */ public function GetRoot($search_file = 'Addon.ini') { $archive_files = $this->ListFiles(); $archive_root = null; foreach ($archive_files as $file) { if (strpos($file['name'], $search_file) === false) { continue; } $root = \gp\tool::DirName($file['name']); if ($root == '.') { $root = ''; } if (is_null($archive_root) || strlen($root) < strlen($archive_root)) { $archive_root = $root; } } return $archive_root; }
/** * Make sure the directory exists for a file * */ public function CheckDir($file) { $dir = \gp\tool::DirName($file); if ($this->file_exists($dir)) { return true; } if (!$this->CheckDir($dir)) { return false; } return $this->mkdir($dir); }
/** * Get the contents of a revision * */ protected function GetRevision($time) { $full_path = $this->BackupFile($time); if (is_null($full_path)) { return false; } //if it's a compressed file, we need an uncompressed version if (strpos($full_path, '.gze') !== false) { ob_start(); readgzfile($full_path); $contents = ob_get_clean(); $dir = \gp\tool::DirName($full_path); $full_path = tempnam($dir, 'backup') . '.php'; \gp\tool\Files::Save($full_path, $contents); $file_sections = \gp\tool\Files::Get($full_path, 'file_sections'); unlink($full_path); } else { $file_sections = \gp\tool\Files::Get($full_path, 'file_sections'); } return $file_sections; }
/** * Return the folder path used for resized images of $img * */ static function Folder($img) { global $dataDir; $name = basename($img); return $dataDir . '/data/_resized' . \gp\tool::DirName($img) . '/' . gp_resized::EncodePath($name); }
/** * Create a resized image of the file at $src_relative * */ public static function CreateImage($src_relative, $width, $height) { global $dataDir; $src_path = $dataDir . '/data/_uploaded' . $src_relative; if (!file_exists($src_path)) { return false; } //compare to actual size $src_img = \gp\tool\Image::getSrcImg($src_path); if (!$src_img) { return false; } //Original Size $actual_w = imagesx($src_img); $actual_h = imagesy($src_img); if ($actual_w <= $width && $actual_h <= $height) { return false; } $info = \gp_resized::ImageInfo($src_relative, $width, $height); if (!$info) { return false; } $dest_index = $info['index']; if (!$dest_index) { $dest_index = \gp_resized::NewIndex(); } $dest_path = $dataDir . '/data/_resized/' . $dest_index . '/' . $info['name']; $exists_before = file_exists($dest_path); //make sure the folder exists if (!\gp\tool\Files::CheckDir(\gp\tool::DirName($dest_path))) { return false; } //create new resized image if (!\gp\tool\Image::createImg($src_img, $dest_path, 0, 0, 0, 0, $width, $height, $actual_w, $actual_h)) { return false; } //not needed if the resized image is larger than the original if (filesize($dest_path) > filesize($src_path)) { if (!$exists_before) { unlink($dest_path); } return false; } $data = array(); $data['index'] = $dest_index; $data['w'] = $width; $data['h'] = $height; $data['img'] = $src_relative; return $data; }
/** * Check recursively to see if a directory exists, if it doesn't attempt to create it * * @param string $dir The directory path * @param bool $index Whether or not to add an index.hmtl file in the directory * @return bool True on success */ public static function CheckDir($dir, $index = true) { global $config; if (!file_exists($dir)) { $parent = \gp\tool::DirName($dir); self::CheckDir($parent, $index); //ftp mkdir if (isset($config['useftp'])) { if (!self::FTP_CheckDir($dir)) { return false; } } else { if (!@mkdir($dir, gp_chmod_dir)) { return false; } @chmod($dir, gp_chmod_dir); //some systems need more than just the 0755 in the mkdir() function } // make sure there's an index.html file // only check if we just created the directory, we don't want to keep creating an index.html file if a user deletes it if ($index && gp_dir_index) { $indexFile = $dir . '/index.html'; if (!file_exists($indexFile)) { //not using \gp\tool\Files::Save() so we can avoid infinite looping (it's safe since we already know the directory exists and we're not concerned about the content) file_put_contents($indexFile, '<html></html>'); @chmod($indexFile, gp_chmod_file); } } } return true; }
/** * Create a thumbnail for the image at the path given by $original * */ public static function CreateThumbnail($original) { global $config, $dataDir; $prefix = $dataDir . '/data/_uploaded'; $thumb_prefix = $dataDir . '/data/_uploaded/image/thumbnails'; if (strpos($original, $thumb_prefix) !== false) { return; } if (strpos($original, $prefix) !== 0) { return; } $len = strlen($prefix); $thumb_path = substr($original, $len); $thumb_path = $thumb_prefix . $thumb_path; $thumb_dir = \gp\tool::DirName($thumb_path); $thumb_path = $thumb_dir . '/' . basename($thumb_path) . '.jpg'; \gp\tool\Files::CheckDir($thumb_dir); \gp\tool\Image::createSquare($original, $thumb_path, $config['maxthumbsize']); }