DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Remote Install Of A Joomla Module

Snippets Manager user avatar by
Snippets Manager
·
Sep. 03, 09 · · Code Snippet
Like (0)
Save
Tweet
442 Views

Join the DZone community and get the full member experience.

Join For Free
Install a module in your joomla application without using your web browser.
You need the curb gem.


require 'net/http'
require 'uri'
require 'rubygems'
require 'curb'


JoomlaSession = Struct.new(:key, :value)

class JoomlaModuleInstaller
  attr_accessor :file_path, :url, :login, :password
  def initialize(login, password, file_path = "", url = "")
    @login = login
    @password = password
    @file_path = file_path
    @url = URI.parse(url)
  end

  def install
    raise "Get session failed" unless joomla_session
    raise "Login Failed" unless login
    raise "Upload token not found" unless upload_form_token
    install_module
    successful?
  end

  def message
    @message.gsub!(/<\/?\w{1,4}( class="[\w\s]*")?>/, '').gsub!(/\n\t/im, "  ")
  end

  def failed?
    @failed
  end

  def successful?
    !failed?
  end

  private
  def joomla_session
    unless @joomla_session
      http = Net::HTTP.start(@url.host)
      res = http.request_get("#{url_path}/administrator/index.php")
      key, value = res['set-cookie'].split(/=/)
      @joomla_session = JoomlaSession.new(key, value.sub(/;.+/, ''))
      res.body =~ //
      @login_token = $1
    end
    return @joomla_session
  end

  def login
    req = Net::HTTP::Post.new(@url.path)
    req.set_form_data(
      {
        'username' => @login,
        'passwd' => @password,
        'lang' => '',
        'option' => 'com_login',
        'task' => 'login',
        @login_token => '1'
      }
    )
    req['Cookie'] = "#{joomla_session.key}=#{joomla_session.value}"

    res = Net::HTTP.new(@url.host, @url.port).start {|http| http.request(req) }
    res.is_a?(Net::HTTPMovedPermanently)
  end

  def upload_form_token
    unless @upload_form_token
      req = Net::HTTP::Get.new("#{url_path}/administrator/index.php?option=com_installer")
      req['Cookie'] = "#{joomla_session.key}=#{joomla_session.value}"
      res = Net::HTTP.new(@url.host, @url.port).start {|http| http.request(req) }
      @upload_form_token = $1 if res.body =~ /<\/form>/
    end
    return @upload_form_token
  end

  def install_module
    raise "File not found" unless File.exists?(@file_path)
    
    params = {
      'option' => 'com_installer',
      'task' => 'doInstall',
      'installtype' =>
      'upload', 'type' => '',
      upload_form_token => '1'
    }

    post_data = params.map {|k, v| Curl::PostField.content(k, v)}
    post_data << Curl::PostField.file('install_package', @file_path)

    c = Curl::Easy.new(@url.to_s)
    c.multipart_form_post = true
    c.headers['Cookie'] = "#{joomla_session.key}=#{joomla_session.value}"
    c.http_post(*post_data)
    
    @message = $1 if c.body_str =~ /
(.+)<\/dl>/im @failed = @message.include?('error') end def url_path $1 if @url.to_s =~ /(\/[\w]*)?\/administrator\/index\.php/i end end Example : @file = "com_acajoom.zip" @url = 'http://www.example.com/jos/administrator/index.php' @login = "admin" @password = "password" installer = JoomlaModuleInstaller.new(@login, @password, @file, @url) if installer.install puts "#{@file}: Module installation succeeded" else puts installer.message end
remote Joomla

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Toying With Kotlin’s Context Receivers
  • Data Visualization of Healthcare Expenses by Country Using Web Scraping in Python
  • Chopping the Monolith
  • Troubleshooting Memory Leaks With Heap Profilers

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo