diff --git a/.rvmrc b/.rvmrc new file mode 100644 index 00000000..c9b4fc50 --- /dev/null +++ b/.rvmrc @@ -0,0 +1 @@ +rvm ruby-1.9.2-p180@rails31rc4 diff --git a/:q b/:q deleted file mode 100644 index d0083d8e..00000000 --- a/:q +++ /dev/null @@ -1,45 +0,0 @@ -<%= stylesheet_link_tag "design_temp" %> -<%= javascript_include_tag "design_temp" %> -
-<%= f.label :title, t('admin.title') %> -<%= f.text_field :title, :class => 'text' %> -
- --<%= f.label :author, t('admin.author') %> -<%= f.text_field :author, :class => 'text' %> -
- --<%= f.label :intro, t('admin.intro') %> -<%= f.text_field :intro, :class => 'text' %> -
- --<%= f.label "layout", t('admin.layout') %> -<% if @design.layout.blank? %> - <%= f.file_field :layout_file %> - <% else%> - <% debugger %> - <%= File.basename (@design.layout.url) %> -<% end %> -
--<%= f.label "structure_css", t('admin.structure_css') %> -<% if @design.structure_css.blank? %> - <%= f.file_field :structure_css %> - <% else%> - <%= File.basename (Design.all.last.structure_css.url) %> -<% end %> -
--<%= render :partial => 'design_file' ,:locals => { :fieldname=>"themes",:object=>@design ,:f=>f,:rtype=>'stylesheets',:item_destroy=>true,:item_editable=>true } %> -
- --<%= render :partial => 'design_file' ,:locals => { :fieldname=>"javascript",:object=>@design ,:f=>f,:rtype=>'javascripts' ,:item_destroy=>true,:item_editable=>true} %> -
- --<%= render :partial => 'design_file' ,:locals => { :fieldname=>"image",:object=>@design ,:f=>f,:rtype=>'images' ,:item_destroy=>true,:item_snapshot=>true} %> -
diff --git a/Gemfile b/Gemfile index 052637c8..05a549e3 100644 --- a/Gemfile +++ b/Gemfile @@ -9,6 +9,7 @@ gem 'ckeditor' gem 'devise' gem 'mini_magick' gem 'rubyzip' +gem 'sinatra' gem 'mongoid' diff --git a/Gemfile.lock b/Gemfile.lock index 79f31998..00b66197 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -131,6 +131,9 @@ GEM archive-tar-minitar (>= 0.5.2) rubyzip (0.9.4) shoulda-matchers (1.0.0.beta2) + sinatra (1.2.6) + rack (~> 1.1) + tilt (>= 1.2.2, < 2.0) sprockets (2.0.0.beta.10) hike (~> 1.0) rack (~> 1.0) @@ -168,5 +171,6 @@ DEPENDENCIES ruby-debug19 rubyzip shoulda-matchers + sinatra watchr yajl-ruby diff --git a/app/controllers/admin/purchases_controller.rb b/app/controllers/admin/purchases_controller.rb new file mode 100644 index 00000000..82b6a92b --- /dev/null +++ b/app/controllers/admin/purchases_controller.rb @@ -0,0 +1,88 @@ +class Admin::PurchasesController < ApplicationController + require "net/http" + require "uri" + require 'zip/zip' + + def index + @purchases = Purchase.all.entries + end + + def download + @purchase = Purchase.first(:conditions => {:id => params[:id]}) + download_purchase(@purchase, request.env['REQUEST_URI'].split('admin')[0].chop) + @purchase.downloaded = true + @purchase.save + redirect_to admin_purchases_url + end + + protected + + def download_purchase(purchase, url) + uri = URI.parse("http://localhost:3001/download/design") + http = Net::HTTP.new(uri.host, uri.port) + request = Net::HTTP::Post.new(uri.request_uri) + + public_key = OpenSSL::PKey::RSA.new(File.read(APP_CONFIG['main_public_key'])) + encrypted_data = public_key.public_encrypt({:purchase_id => purchase.purchase_id, + :roaming_id => Site.find(session[:site]).roaming_id, + :url => url}.to_json) + + request.set_form_data({ :purchase => encrypted_data}) + response = http.request(request) + temp_file = Tempfile.new("temp_file") + temp_file.binmode + temp_file.write response.body + temp_file.rewind + zip_name = response['content-disposition'].split('filename=')[1].gsub(/[\\\"]|.zip/, '') rescue '' + debugger + unzip_design(temp_file, zip_name) + temp_file.close + temp_file.unlink + end + + def unzip_design(file, zip_name) + Zip::ZipFile.open(file) do |zip_file| + encrypted = {} + ['encrypted_data', 'encrypted_key', 'encrypted_iv'].each do |e| + encrypted.merge!(e => zip_file.read(e)) + end + orig_file = decrypt_data(encrypted['encrypted_data'], encrypted['encrypted_key'], encrypted['encrypted_iv']) + + temp_file = Tempfile.new("temp_file_zip") + temp_file.binmode + temp_file.write orig_file + temp_file.rewind + + Zip::ZipFile.open(temp_file) { |orig_zip| + design = Design.new.from_json(orig_zip.read("#{zip_name}/#{zip_name}.json")) + Dir.mktmpdir('f_path') { |dir| + + title = design.layout.file_filename + temp = File.new(dir + '/' + title, 'w+') + temp.write orig_zip.read(zip_name + '/' + title) + design.layout.file = temp + + title = design.structure_css_filename + temp = File.new(dir + '/' + title, 'w+') + temp.write orig_zip.read(zip_name + '/' + title) + design.layout.file = temp + + ['themes', 'javascripts', 'images'].each do |type| + eval("design.#{type}").each do |object| + title = object.file_filename + temp = File.new(dir + '/' + title, 'w+') + temp.write orig_zip.read(zip_name + '/' + type + '/' + title) + object.file = temp + object.to_save = true + end + end + } + temp_file.close + temp_file.unlink + design.save + } + + end + end + +end \ No newline at end of file diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 2dd99b4a..7bda19c4 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -25,6 +25,8 @@ class ApplicationController < ActionController::Base end end + protected + # Set I18n.locale def set_locale # update session if passed @@ -55,5 +57,21 @@ class ApplicationController < ActionController::Base def set_current_item session[:current_page] = params[:id] || @item.id rescue nil end + + def decrypt_data(encrypted_data, encrypted_key, encrypted_iv) + site = Site.find(session[:site]) + if encrypted_data + private_key = OpenSSL::PKey::RSA.new(site.private_key) + cipher = OpenSSL::Cipher.new('aes-256-cbc') + cipher.decrypt + cipher.key = private_key.private_decrypt(encrypted_key) + cipher.iv = private_key.private_decrypt(encrypted_iv) + + decrypted_data = cipher.update(encrypted_data) + decrypted_data << cipher.final + else + '' + end + end end diff --git a/app/controllers/gridfs_controller.rb b/app/controllers/gridfs_controller.rb new file mode 100644 index 00000000..be276673 --- /dev/null +++ b/app/controllers/gridfs_controller.rb @@ -0,0 +1,16 @@ +require 'mongo' + +class GridfsController < ActionController::Metal + def serve + gridfs_path = env["PATH_INFO"].gsub("/gridfs/", "") + begin + gridfs_file = Mongo::GridFileSystem.new(Mongoid.database).open(gridfs_path, 'r') + self.response_body = gridfs_file.read + self.content_type = gridfs_file.content_type + rescue + self.status = :file_not_found + self.content_type = 'text/plain' + self.response_body = '' + end + end +end \ No newline at end of file diff --git a/app/controllers/sites_controller.rb b/app/controllers/sites_controller.rb index cb820fbb..a8137715 100644 --- a/app/controllers/sites_controller.rb +++ b/app/controllers/sites_controller.rb @@ -4,11 +4,7 @@ class SitesController < ApplicationController def index @site = Site.first - if @site - @title = "site" - else - redirect_to :action => :new - end + redirect_to :action => :new unless @site end def new diff --git a/app/models/design.rb b/app/models/design/design.rb similarity index 100% rename from app/models/design.rb rename to app/models/design/design.rb diff --git a/app/models/design_file.rb b/app/models/design/design_file.rb similarity index 100% rename from app/models/design_file.rb rename to app/models/design/design_file.rb diff --git a/app/models/image.rb b/app/models/design/image.rb similarity index 100% rename from app/models/image.rb rename to app/models/design/image.rb diff --git a/app/models/javascript.rb b/app/models/design/javascript.rb similarity index 100% rename from app/models/javascript.rb rename to app/models/design/javascript.rb diff --git a/app/models/layout.rb b/app/models/design/layout.rb similarity index 100% rename from app/models/layout.rb rename to app/models/design/layout.rb diff --git a/app/models/layout_part.rb b/app/models/design/layout_part.rb similarity index 100% rename from app/models/layout_part.rb rename to app/models/design/layout_part.rb diff --git a/app/models/stylesheet.rb b/app/models/design/stylesheet.rb similarity index 100% rename from app/models/stylesheet.rb rename to app/models/design/stylesheet.rb diff --git a/app/models/theme.rb b/app/models/design/theme.rb similarity index 100% rename from app/models/theme.rb rename to app/models/design/theme.rb diff --git a/app/models/purchase/p_design.rb b/app/models/purchase/p_design.rb new file mode 100644 index 00000000..427fe4f5 --- /dev/null +++ b/app/models/purchase/p_design.rb @@ -0,0 +1,3 @@ +class PDesign < Purchase + +end \ No newline at end of file diff --git a/app/models/purchase/purchase.rb b/app/models/purchase/purchase.rb new file mode 100644 index 00000000..760724a2 --- /dev/null +++ b/app/models/purchase/purchase.rb @@ -0,0 +1,11 @@ +class Purchase + include Mongoid::Document + include Mongoid::Timestamps + + field :purchase_id + field :title + field :author + field :intro + field :downloaded + +end diff --git a/app/models/site.rb b/app/models/site.rb index 6cd490da..11d1c768 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -6,4 +6,21 @@ class Site field :in_use_locales, :type => Array field :valid_locales, :type => Array + field :roaming_id + field :private_key, :type => Binary + field :public_key, :type => Binary + + field :school + field :department + + def generate_keys + private_key = OpenSSL::PKey::RSA.generate(2048) + self.public_key = private_key.public_key.to_s + self.private_key = private_key.to_s + end + + def registered? + !self.roaming_id.blank? + end + end diff --git a/app/models/attribute_model.rb b/app/models/user/attribute_model.rb similarity index 100% rename from app/models/attribute_model.rb rename to app/models/user/attribute_model.rb diff --git a/app/models/attribute_value.rb b/app/models/user/attribute_value.rb similarity index 100% rename from app/models/attribute_value.rb rename to app/models/user/attribute_value.rb diff --git a/app/models/user.rb b/app/models/user/user.rb similarity index 100% rename from app/models/user.rb rename to app/models/user/user.rb diff --git a/app/models/user_attribute.rb b/app/models/user/user_attribute.rb similarity index 100% rename from app/models/user_attribute.rb rename to app/models/user/user_attribute.rb diff --git a/app/models/user_attribute_model.rb b/app/models/user/user_attribute_model.rb similarity index 100% rename from app/models/user_attribute_model.rb rename to app/models/user/user_attribute_model.rb diff --git a/app/models/user_info.rb b/app/models/user/user_info.rb similarity index 100% rename from app/models/user_info.rb rename to app/models/user/user_info.rb diff --git a/app/models/user_info_model.rb b/app/models/user/user_info_model.rb similarity index 100% rename from app/models/user_info_model.rb rename to app/models/user/user_info_model.rb diff --git a/app/models/user_role.rb b/app/models/user/user_role.rb similarity index 100% rename from app/models/user_role.rb rename to app/models/user/user_role.rb diff --git a/app/models/user_role_model.rb b/app/models/user/user_role_model.rb similarity index 100% rename from app/models/user_role_model.rb rename to app/models/user/user_role_model.rb diff --git a/app/views/admin/purchases/index.html.erb b/app/views/admin/purchases/index.html.erb new file mode 100644 index 00000000..cfeffa90 --- /dev/null +++ b/app/views/admin/purchases/index.html.erb @@ -0,0 +1,10 @@ +<% @purchases.each do |purchase| %> + <%= purchase.title %> | + <%= purchase.intro %> | + <%= purchase.author %> | + <% if purchase.downloaded %> + Downloaded + <% else %> + <%= link_to 'Download', download_admin_purchase_path(purchase) %> + <% end %> +<% end %> \ No newline at end of file diff --git a/app/views/layouts/admin.html.erb b/app/views/layouts/admin.html.erb index 461f3fb2..5add9003 100644 --- a/app/views/layouts/admin.html.erb +++ b/app/views/layouts/admin.html.erb @@ -51,12 +51,6 @@Rulingcom
- -<%= javascript_include_tag "jquery", - "jquery-ui", - "rails", - "easy", - "application", :cache => 'all' %> <%= yield :page_specific_javascript %>