forked from UnderpantsGnome/fckeditor_on_rails
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rewrite pretty much everything to get rid of that PHP taste :(
Make the app usable with Rails Engins and appable_plugins Clean up some junk that's not needed
- Loading branch information
1 parent
4d7730b
commit cb8931b
Showing
19 changed files
with
277 additions
and
403 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License | ||
|
||
Copyright (c) 2006,2007 Michael Moen | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
README for FCKeditor on Rails | ||
|
||
This can now be used as a plugin with both Engines and appable_plugins. | ||
|
||
Q: What is it? | ||
A: A Rails like way to use the FCKeditor in a Rails app, it basically works | ||
just like the normal helpers. | ||
|
||
It supports the "default" upload interface, I've though about mcpuk support | ||
but since I'm not even using this in any projects currently I don't have the | ||
need or the time to implement all that mcpuk does. Maybe one of these days. | ||
|
||
Sample usage: | ||
fckeditor(:object, :param, | ||
{:width => '600px', :height => '500px'}, # fck options | ||
{:class => 'fancy'} # html options | ||
) | ||
|
||
Basic install: | ||
./script/plugin install http://svn.underpantsgnome.com/fckeditor_on_rails | ||
|
||
appable_plugins info: http://wiki.pluginaweek.org/Appable_plugins | ||
|
||
Required plugins (in proper load order) for using the appable_plugins: | ||
loaded_plugins | ||
plugin_dependencies | ||
plugin_routing | ||
appable_plugins | ||
fckeditor_on_rails | ||
|
||
|
||
Rails Engines info: http://rails-engines.org/ | ||
|
||
Required plugins (in proper load order) for using Engines: | ||
engines | ||
fckeditor_on_rails | ||
|
||
|
||
Contact: | ||
Michael Moen | ||
mi-rails@underpantsgnome.com | ||
http://underpantsgnome.com/ | ||
http://trac.underpantsgnome.com/fckeditor_on_rails | ||
|
||
History: | ||
2007-01-05 (0.1.0) | ||
Clean up that ugly code a bit | ||
Make it plugin ready for Rails 1.2 | ||
Move the config to YAML | ||
|
||
2005-09-11 (0.0.1) - Initial | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,166 +1,126 @@ | ||
class FckeditorController < ApplicationController | ||
# this may not be very Rubyesque, it's my first run at Ruby code | ||
# I'm sure it will get better over time | ||
# feel free to point out better ways to do thigs in Ruby | ||
# if you can do it without being abusive ;) | ||
before_filter :setup, :except => 'index' | ||
|
||
def index | ||
if RAILS_ENV != 'development' | ||
render(:nothing => true) | ||
end | ||
end | ||
before_filter :get_options, :sanitize_directory | ||
layout false | ||
|
||
# figure out who needs to handle this request | ||
def connector | ||
if @params[:Command] == 'GetFoldersAndFiles' || @params[:Command] == 'GetFolders' | ||
get_folders_and_files(@params) | ||
elsif @params[:Command] == 'CreateFolder' | ||
create_folder(params) | ||
elsif @params[:Command] == 'FileUpload' | ||
upload_file(@params) | ||
end | ||
def index | ||
render(:nothing => true) unless RAILS_ENV == 'development' | ||
end | ||
|
||
private | ||
def setup | ||
defaultParams | ||
status = 200 | ||
|
||
# Check parameters for correctness | ||
if (@params[:Command] !~ /^(GetFolders|GetFoldersAndFiles|CreateFolder|FileUpload)$/) | ||
render :text => "Command parameter malformed: \"#{@params[:Command]}\"", status => 405 | ||
elsif (@params[:Type] !~ /^(File|Image|Flash|Media)$/) | ||
render :text => "Type parameter malformed: \"#{@params[:Type]}\"", status => 405 | ||
elsif (@params[:CurrentFolder] !~ /^\/(.+\/)*$/) | ||
render :text => "CurrentFolder parameter malformed: \"#{@params[:CurrentFolder]}\"", status => 405 | ||
else | ||
# Make sure necessary directories exist | ||
unless File.directory?(@params[:ServerPath]) | ||
Dir.mkdir(@params[:ServerPath]) | ||
end | ||
@dir = "#{params[:ServerPath]}#{params[:Type]}#{params[:CurrentFolder]}" | ||
@url = "/UserFiles/#{params[:Type]}#{params[:CurrentFolder]}" | ||
@error = 0 | ||
def connector | ||
case params[:Command] | ||
when 'GetFoldersAndFiles', 'GetFolders' | ||
get_folders_and_files | ||
when 'CreateFolder' | ||
create_folder | ||
when 'FileUpload' | ||
upload_file | ||
end | ||
end | ||
|
||
# generate a directory listing | ||
def get_folders_and_files(params) | ||
@files = Array.new | ||
@dirs = Array.new | ||
|
||
d = Dir.new(@dir) | ||
|
||
d.each { |x| | ||
# skip . and .. I'm sure there's a better way to handle this | ||
if x != '.' && x != '..' | ||
# actual file system path | ||
full_path = "#{@dir}/#{x}"; | ||
|
||
# found a directory add it to the list | ||
if File.directory?(full_path) | ||
@dirs << x | ||
#if we only want directories, skip the files | ||
elsif params[:Command] == 'GetFoldersAndFiles' && File.file?(full_path) | ||
size = File.size(full_path) | ||
|
||
if size != 0 && size < 1024 | ||
size = 1 | ||
else | ||
size = File.size(full_path) / 1024 | ||
end | ||
|
||
@files << { :name => x, :size => size }; | ||
end | ||
private | ||
|
||
def get_folders_and_files | ||
@files = [] | ||
@dirs = [] | ||
dir = Dir.new(@options[:dir]) | ||
|
||
dir.each do |x| | ||
next if x =~ /^\.\.?$/ # skip . and .. | ||
|
||
# actual file system path | ||
full_path = File.join(@options[:dir], x) | ||
|
||
# found a directory add it to the list | ||
if File.directory?(full_path) | ||
@dirs << x | ||
# if we only want directories, skip the files | ||
elsif params[:Command] == 'GetFoldersAndFiles' && File.file?(full_path) | ||
size = File.size(full_path) | ||
size = size != 0 && size < 1024 ? 1 : size / 1024 | ||
@files << { :name => x, :size => size }; | ||
end | ||
} | ||
|
||
render(:template => 'fckeditor/files_and_folders', :layout => false) | ||
|
||
rescue | ||
render(:template => 'fckeditor/files_and_folders', :layout => false) | ||
end | ||
|
||
render 'fckeditor/files_and_folders' | ||
end | ||
|
||
# create a new directory | ||
def create_folder(params) | ||
new_dir = @dir + params[:NewFolderName] | ||
d = Dir.mkdir(new_dir) | ||
|
||
render(:template => 'fckeditor/create_folder', :layout => false) | ||
|
||
rescue Errno::EEXIST | ||
# directory already exists | ||
@error = 101 | ||
render(:template => 'fckeditor/create_folder', :layout => false) | ||
rescue Errno::EACCES | ||
# permission denied | ||
@error = 103 | ||
render(:template => 'fckeditor/create_folder', :layout => false) | ||
rescue | ||
# any other error | ||
@error = 110 | ||
render(:template => 'fckeditor/create_folder', :layout => false) | ||
|
||
def create_folder | ||
begin | ||
new_dir = @options[:dir] + params[:NewFolderName] | ||
Dir.mkdir(new_dir) | ||
rescue Errno::EEXIST | ||
# directory already exists | ||
@error = 101 | ||
rescue Errno::EACCES | ||
# permission denied | ||
@error = 103 | ||
rescue | ||
# any other error | ||
@error = 110 | ||
end | ||
render 'fckeditor/create_folder' | ||
end | ||
|
||
# upload a new file | ||
# currently the FCKeditor docs only allow for 2 errors here | ||
# file renamed and invalid file name | ||
# not sure how to catch invalid file name yet | ||
# I'm thinking there should be a permission denied error as well | ||
def upload_file(params) | ||
counter = 1 | ||
@file_name = params[:NewFile].original_filename | ||
def upload_file | ||
counter = 1 | ||
@file_name = params[:NewFile].original_filename | ||
|
||
# break it up into file and extension | ||
# we need this to check the types and to build new names if necessary | ||
ext = File.extname(@file_name) | ||
path = File.basename(@file_name, ext) | ||
|
||
# check to make sure this extension isn't in deny and is in allow | ||
if ! in_array(FCKEDITOR_FILE_TYPES[params[:Type]]['deny'], ext) | ||
in_array(FCKEDITOR_FILE_TYPES[params[:Type]]['allow'], ext) | ||
while File.exist?("#{@dir}#{@file_name}") | ||
@file_name = "#{path}(#{counter})#{ext}" | ||
@error = 201 | ||
counter = counter.next | ||
filetype = params[:Type].downcase.to_sym | ||
if type_allowed(filetype, ext) | ||
while File.exist?(File.join(@options[:dir], @file_name)) | ||
@file_name = "#{path}(#{counter})#{ext}" | ||
@error = 201 | ||
counter += 1 | ||
end | ||
|
||
n = File.open("#{@dir}#{@file_name}", 'wb') { |f| f.write(params[:NewFile].read) } | ||
File.open("#{@options[:dir]}#{@file_name}", 'wb') do |f| | ||
f.write(params[:NewFile].read) | ||
end | ||
else | ||
# invalid file type | ||
@error = 202 | ||
end | ||
@error = 202 | ||
end | ||
|
||
render(:template => 'fckeditor/upload_file', :layout => false) | ||
render 'fckeditor/upload_file' | ||
end | ||
|
||
# helper to setup pathing info that is common to all methods | ||
def defaultParams | ||
if RAILS_ENV == 'production' or !@params.has_key?(:ServerPath) | ||
# Allow destination directory to be overridden, otherwise it must be that which is configured | ||
@params[:ServerPath] = "#{RAILS_ROOT}/public/UserFiles/" | ||
end | ||
def type_allowed(filetype, ext) | ||
type = Fckeditor.config[:file_types][filetype] | ||
|
||
# Set defaults | ||
@params[:Type] = "Image" unless @params.has_key?(:Type) | ||
@params[:CurrentFolder] = "/" unless @params.has_key?(:CurrentFolder) | ||
(! type[:deny] || ! type[:deny].include?(ext)) && | ||
(! type[:allow] || type[:allow].include?(ext)) | ||
end | ||
|
||
# helper to see if a value exists in an array | ||
# I'm sure there is a more Rubyesque way to do this | ||
# somebody let me know | ||
def in_array(haystack, needle) | ||
if haystack != nil && needle != nil | ||
haystack.each { |val| | ||
if val == needle | ||
return true | ||
end | ||
} | ||
def get_options | ||
@error = 0 | ||
@options = { | ||
:base_dir => File.join('public', Fckeditor.config[:uploads] || 'UserFiles') | ||
} | ||
@options[:url] = "/#{@options[:base_dir]}/#{params[:Type]}/#{params[:CurrentFolder]}".gsub(/\/+/, '/') | ||
@options[:dir] = File.join(RAILS_ROOT, @options[:url]) | ||
end | ||
|
||
def sanitize_directory | ||
if params[:CurrentFolder] | ||
clean = File.expand_path(@options[:base_dir]) | ||
dirty = File.expand_path(@options[:dir]) | ||
|
||
unless dirty.starts_with?(clean) | ||
render :nothing => true, :status => 403 | ||
false | ||
end | ||
end | ||
|
||
return false | ||
end | ||
end |
Oops, something went wrong.