<?php

/*
 * Copyright (C) 2017 KAINOTOMO PH LTD <info@kainotomo.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// No direct access.
defined('_JEXEC') or die;

/**
 * SPStgSrv helper.
 *
 * @package		Joomla.Administrator
 * @subpackage	com_spstgsrv
 * @since		1.6
 */
class SpStgSrvHelper {

        /**
         * Configure the Linkbar.
         *
         * @param	string	The name of the active view.
         */
        public static function addSubmenu($vName = 'log') {
                JSubMenuHelper::addEntry(
                        JText::_('COM_SPSTGSRV_SUBMENU_LOG'), 'index.php?option=com_spstgsrv', $vName == 'log'
                );
        }

        /**
         * Gets a list of the actions that can be performed.
         *
         * @return	JObject
         * @since	1.6
         */
        public static function getActions() {
                $user = JFactory::getUser();
                $result = new JObject;

                $assetName = 'com_spstgsrv';

                $actions = array(
                    'core.admin', 'core.manage', 'core.install', 'core.delete'
                );

                foreach ($actions as $action) {
                        $result->set($action, $user->authorise($action, $assetName));
                }

                return $result;
        }

        public static function getVersion() {
                $version_arr = explode('.', JVERSION);
                $major_version = $version_arr[0];

                if (version_compare($major_version, '4', 'ge')) {
                        return 4;
                }
                if (version_compare($major_version, '3', 'ge')) {
                        return 3;
                }
                if (version_compare($major_version, '1', 'ge')) {
                        return 2;
                }
                return -1;
        }

        public static function getCurlAgent() {


                $jversion = SpStgSrvHelper::getVersion();

                switch ($jversion) {
                        case 2:
                                $parser = JFactory::getXMLParser('Simple');
                                $xml = JPATH_ADMINISTRATOR . '/components/com_spstgsrv/com_spstgsrv.xml';
                                $parser->loadFile($xml);
                                $doc = $parser->document;
                                $element = $doc->getElementByPath('version');
                                $version = $element->data();

                                break;
                        case 3:
                                $version = JFactory::getXML(JPATH_ADMINISTRATOR . '/components/com_spstgsrv/com_spstgsrv.xml')->version;
                                break;

                        default:
                                $version = '1.0';
                                $domain = 'localhost';
                }

                $agent = 'Mozilla/5.0 (compatible; SPStaging/' . $version . '; +' . JUri::root() . ')';

                return $agent;
        }

        /**
         * Encrypt text based on a password using openssl
         * 
         * @param string $decrypted Decrypted text
         * @param string $password
         * @return string The encrypted string
         */
        public static function encrypt($decrypted, $password) {
                $cipher = 'AES-256-CBC';
                $encrypted = @base64_encode(openssl_encrypt($decrypted, $cipher, $password));
                return $encrypted;
        }

        /**
         * Encrypt text based on a password using mcrypt
         * 
         * @param string $decrypted Decrypted text
         * @param string $password
         * @return string The encrypted string
         */
        public static function encrypt_mcrypt($decrypted, $password) {
                $salt = $password;
                // Build a 256-bit $key which is a SHA256 hash of $salt and $password.
                $key = hash('SHA256', $salt . $password, true);
                // Build $iv and $iv_base64.  We use a block size of 128 bits (AES compliant) and CBC mode.  (Note: ECB mode is inadequate as IV is not used.)
                srand();
                $iv = @mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
                if (strlen($iv_base64 = rtrim(base64_encode($iv), '=')) != 22) {
                        return false;
                }
                // Encrypt $decrypted and an MD5 of $decrypted using $key.  MD5 is fine to use here because it's just to verify successful decryption.
                $encrypted = @base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $decrypted . md5($decrypted), MCRYPT_MODE_CBC, $iv));
                // We're done!
                return $iv_base64 . $encrypted;
        }
                
        /**
         * Decrypt an encrypted text based on password using openssl.
         * 
         * @param string $encrypted Encrypted string
         * @param string $password The hash to sue
         * @return string The decrypted string
         */
        public static function decrypt($encrypted, $password) {
                $cipher = 'AES-256-CBC';
                $decrypted = @openssl_decrypt(base64_decode($encrypted), $cipher, $password);
                return $decrypted;
        }

        /**
         * Decrypt an encrypted text based on password using mcrypt.
         * 
         * @param string $encrypted Encrypted string
         * @param string $password The hash to sue
         * @return string The decrypted string
         */
        public static function decrypt_mcrypt($encrypted, $password) {
                $salt = $password;
                // Build a 256-bit $key which is a SHA256 hash of $salt and $password.
                $key = hash('SHA256', $salt . $password, true);
                // Retrieve $iv which is the first 22 characters plus ==, base64_decoded.
                $iv = base64_decode(substr($encrypted, 0, 22) . '==');
                // Remove $iv from $encrypted.
                $encrypted = substr($encrypted, 22);
                // Decrypt the data.  rtrim won't corrupt the data because the last 32 characters are the md5 hash; thus any \0 character has to be padding.
                if (!($decrypted = @rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($encrypted), MCRYPT_MODE_CBC, $iv), "\0\4"))) {
                        return false;
                }
                // Retrieve $hash which is the last 32 characters of $decrypted.
                $hash = substr($decrypted, -32);
                // Remove the last 32 characters from $decrypted.
                $decrypted = substr($decrypted, 0, -32);
                // Integrity check.  If this fails, either the data is corrupted, or the password/salt was incorrect.
                if (md5($decrypted) != $hash)
                        return false;
                // Yay!
                return $decrypted;
        }

}
