HOME


sh-3ll 1.0
DIR:/usr/local/softaculous/iwx/enduser/
Upload File :
Current File : //usr/local/softaculous/iwx/enduser/Softaculous.php
<?php
/**
 * InterWorx Hosting Control Panel
 *
 * <pre>
 * +----------------------------------------------------------------------+
 * | Copyright (c) 2000-2010 InterWorx L.L.C., All Rights Reserved.       |
 * +----------------------------------------------------------------------+
 * | Redistribution and use in source form, with or without modification  |
 * | is NOT permitted without consent from the copyright holder.          |
 * |                                                                      |
 * | THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND |
 * | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,    |
 * | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A          |
 * | PARTICULAR PURPOSE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,    |
 * | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  |
 * | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR   |
 * | PROFITS; OF BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY  |
 * | OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT         |
 * | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE    |
 * | USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH     |
 * | DAMAGE.                                                              |
 * +----------------------------------------------------------------------+
 * </pre>
 *
 * @package InterWorx
 * @author  Paul Oehler <poehler@interworx.com>
 * @date    Oct 10, 2009
 */

/**
 * Softaculous plugin.
 *
 * @package    InterWorx
 * @subpackage Plugin
 */
class Plugin_Softaculous extends Plugin_SiteworxFeature
{
    protected $_sw_menu_text = 'Softaculous';

    protected $_sw_menu_url = '/siteworx/softaculous';

    protected $_sw_menu_parent_big = 'iw-menu-features-misc';

    //  protected $_sw_menu_style = '';

    //protected $_sw_menu_location_after = 'iw-menu-features-ssl';

    protected $_sw_menu_id = 'menu-softaculous';

    protected $_sw_menu_description = 'Softaculous is the leading auto-installer for managing software packages in SiteWorx';

    protected $_sw_account_group_name = 'softaculous';

    protected $_sw_account_group_label = 'Softaculous Plugin Options';

    protected $_sw_account_option_name = 'softaculous';

    //protected $_sw_account_option_default = 1;

    protected $_sw_account_option_label = 'Access to Softaculous';

    protected $_sw_user_perm_name = 'softaculous';

    protected $_sw_user_perm_label = 'Softaculous';

    const ERROR_INSTALL = 40;


    /**
     * Plugin init.
     */
    protected function _init()
    {
        if (!is_dir('/var/softaculous')) {
            $err = 'Can not find /var/softaculous';
            if (IWorxFileSys::isFile("{$this->getPluginDir()}/install.log")) {
                $err .= ' Installer may still be running, check install.log';
            }
            $this->_disable($err);
            return;
        }

        if (!is_dir('/usr/local/softaculous')) {
            $err = 'Can not find /usr/local/softaculous. ';
            if (IWorxFileSys::isFile("{$this->getPluginDir()}/install.log")) {
                $err .= ' Installer may still be running, check install.log';
            }
            $this->_disable($err);
            return;
        }

        if (!is_link('/usr/local/interworx/html/softaculous')) {
            symlink(
                '/usr/local/softaculous',
                '/usr/local/interworx/html/softaculous'
            );
        }

        chmod('/usr/local/interworx/plugins/softaculous', 0711);
        chmod('/usr/local/interworx/plugins/softaculous/lib', 0711);
        chmod('/usr/local/interworx/plugins/softaculous/lib/universal.php', 0644);
        chmod('/usr/local/interworx/plugins/softaculous/lib/softpanel.php', 0644);
    }

    /**
     * Get category.
     *
     * @return string
     */
    public function getCategory()
    {
        return Plugin_Category::SCRIPT_INSTALLERS;
    }

    /**
     * Get priority.
     *
     * @return integer
     */
    public function getPriority()
    {
        return 40;
    }

    /**
     * Launches the installer script into the background.
     */
    public function runInstaller()
    {
        $installer = "{$this->getPluginDir()}/lib/install.sh";
        if (IWorxFileSys::fileExists($installer)) {
            IWorxFileSys::rm($installer);
        }

        $cmd = "wget --timeout=30 --tries=1 -O {$installer} http://files.softaculous.com/install.sh";
        IWorxExec::exec($cmd, $res, $ret, IWorxExec::NO_OUTPUT);
        if ($ret !== 0) {
            $Softaculous = Plugin_Manager::getInstance()->getPlugin('softaculous');
            Ini::set(Ini::PLUGINS, $Softaculous->getName(), '0');
            Ini::write();
            return self::ERROR_INSTALL;
        }
        IWorxFileSys::chmod(0755, $installer);

        $cmd  = Ini::get(Ini::IWORX_BIN, 'runasuser');
        $cmd .= " root custom '{$installer} > {$this->getPluginDir()}/install.log 2>&1'";
        IWorxExec::exec(
            $cmd,
            $result,
            $retval,
            IWorxExec::IN_BACKGROUND |
                     IWorxExec::ALLOW_REDIR_OUT |
            IWorxExec::ALLOW_AMPERSAND
        );
        sleep(10);
    }

    /**
     * Launches the un installer script into the background.
     */
    public function runUnInstaller()
    {
        $cmd  = Ini::get(Ini::IWORX_BIN, 'runasuser');
        $cmd .= " root custom 'rm -rf /etc/cron.d/softaculous /etc/cron.d/softaculous2 /var/softaculous > {$this->getPluginDir()}/uninstall.log 2>&1'";
        IWorxExec::exec(
            $cmd,
            $result,
            $retval,
            IWorxExec::IN_BACKGROUND |
                     IWorxExec::ALLOW_REDIR_OUT |
            IWorxExec::ALLOW_AMPERSAND
        );
        sleep(5);
    }

    /**
     * Executes the end user interface.
     */
    public function runEnduser()
    {
        putenv('IWORX_SESSION_ID=' . session_id());
        session_write_close();

        $uniqname = IW::SW()->getUniqname();

        $userdata = posix_getpwnam($uniqname);
        $uniqname = 'root';
        putenv('soft_euid='.$userdata['uid']);
        putenv('soft_egid='.$userdata['gid']);

        $cmd  = Ini::get(Ini::IWORX_BIN, 'runasuser');
        $cmd .= " {$uniqname} custom /usr/local/softaculous/enduser/index.php 2>&1";

        putenv('QUERY_STRING=' . http_build_query($_GET));
        putenv('REQUEST_METHOD=' . $_SERVER['REQUEST_METHOD']);

        putenv('REMOTE_ADDR=' . $_SERVER['REMOTE_ADDR']);
        putenv('HTTP_USER_AGENT=' . $_SERVER['HTTP_USER_AGENT']);
        putenv('HTTP_HOST=' . $_SERVER['HTTP_HOST']);
        putenv('SERVER_PORT=' . $_SERVER['SERVER_PORT']);
        putenv('REQUEST_URI=' . $_SERVER['REQUEST_URI']);
        putenv('HTTPS=' . $_SERVER['HTTPS']);

		// Any file posted on the form ? 
		$files = $_FILES;
		if(!empty($files)){

			putenv('FILES=' . json_encode($files));

			// Is the user uploading a file ? We need to chown it
			if(!empty($_REQUEST['act']) && $_REQUEST['act'] == 'wordpress' && !empty($_REQUEST['upload']) && !empty($_FILES['custom_file'])){

				$custom_file = $_FILES['custom_file']['tmp_name'];
				$r_custom_file = preg_replace('#/tmp/#', '', $custom_file);

				if(substr_count($r_custom_file, '/') == 0 && !is_link($custom_file)){

					$Chown = new Command_Chown();
					$Chown->setTarget('/tmp/'.$r_custom_file);
					$Chown->setOwner($userdata['name']);
					$Chown->setGroup($userdata['name']);
					$Chown->runAsRoot()->exec();
					session_write_close();

				}
			}
		}

        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            putenv('CONTENT_LENGTH=' . $_SERVER['CONTENT_LENGTH']);
            putenv('POST=' . http_build_query($_POST));
            putenv('HTTP_RAW_POST_DATA=' . http_build_query($_POST));
        }

        $this->_passthruWithHeaders($cmd);
        exit;
    }

    /**
     * Executes the admin interface.
     */
    public function runAdmin()
    {
        putenv('IWORX_SESSION_ID=' . session_id());
        session_write_close();

        $cmd = Ini::get(Ini::IWORX_BIN, 'runasuser');

        $user = 'root';
        $cmd .= " {$user} custom /usr/local/softaculous/index.php 2>&1";

        putenv('QUERY_STRING=' . http_build_query($_GET));
        putenv('REQUEST_METHOD=' . $_SERVER['REQUEST_METHOD']);

        putenv('REMOTE_ADDR=' . $_SERVER['REMOTE_ADDR']);
        putenv('HTTP_USER_AGENT=' . $_SERVER['HTTP_USER_AGENT']);

        $files = $_FILES;
        if (!empty($files)) {
            putenv('FILES=' . json_encode($files));
        }

        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            putenv('CONTENT_LENGTH=' . $_SERVER['CONTENT_LENGTH']);
            putenv('POST=' . http_build_query($_POST));
            putenv('HTTP_RAW_POST_DATA=' . http_build_query($_POST));
        }
        $this->_passthruWithHeaders($cmd);
    }

    /**
     * Do the equivalent to a "passthru" call, except treat the first lines as headers.
     *
     * @param string $cmd
     */
    private function _passthruWithHeaders($cmd)
    {
        $descriptorspec = array( 0 => array( 'pipe', 'r' ),   // stdin
                             1 => array( 'pipe', 'w' ),   // stdout
                             2 => array( 'pipe', 'w' ) ); // stderr

        $process = proc_open($cmd, $descriptorspec, $pipes);
        fclose($pipes[0]);
        if (is_resource($process)) {
            while ($s = fgets($pipes[1])) {
                header($s);
                if ($s == "\n" || $s == "\r\n") {
                    break;
                }
            }
            while ($s = fgets($pipes[1])) {
                echo $s;
            }
        }
    }

    /**
     * Integrate into the nodeworx menu.
     *
     * @param IWorxMenuManager $MenuMan
     */
    public function updateNodeworxMenu(IWorxMenuManager $MenuMan)
    {
        $new_data = array( 'text' => 'Softaculous',
                       'url' => '/nodeworx/softaculous?action=launch',
                       'parent' => 'iw-menu-sw',
                       'class' => 'mdi-power-plug',
                       'disabled_for_reseller' => '1' );

        $MenuMan->addMenuItemAfter(
            'iw-menu-sw-import',
            'menu-softaculous',
            $new_data
        );
    }

    /**
     * Process my edit form, firing installer if needed.
     *
     * @param Form_NW_Plugins_Edit $Form
     */
    public function processMyEditForm(Form_NW_Plugins_Edit $Form)
    {
        if ($Form->getValue('status') === '1') {
            if (!is_dir('/var/softaculous') || !is_dir('/usr/local/softaculous')) {
                $this->runInstaller();
            }
        }elseif($Form->getValue('status') === '0'){
            $this->runUnInstaller();
        }
    }
}