Exemplo n.º 1
0
 /**
  * Store plugins info and config as json data instead of serialized data
  *
  * @return array SQL statements to be executed
  */
 protected function r145()
 {
     $sqlUdp = array();
     $stmt = execute_query('SELECT plugin_id, plugin_info, plugin_config FROM plugin');
     if ($stmt->rowCount()) {
         $db = iMSCP_Database::getRawInstance();
         while ($row = $stmt->fetchRow(PDO::FETCH_ASSOC)) {
             if (!isJson($row['plugin_info'])) {
                 $pluginInfo = $db->quote(json_encode(unserialize($row['plugin_info'])));
             } else {
                 $pluginInfo = $db->quote($row['plugin_info']);
             }
             if (!isJson($row['plugin_config'])) {
                 $pluginConfig = $db->quote(json_encode(unserialize($row['plugin_config'])));
             } else {
                 $pluginConfig = $db->quote($row['plugin_config']);
             }
             $sqlUdp[] = "\n\t\t\t\t\tUPDATE\n\t\t\t\t\t\tplugin\n\t\t\t\t\tSET\n\t\t\t\t\t\tplugin_info = {$pluginInfo}, plugin_config = {$pluginConfig}\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tplugin_id = {$row['plugin_id']}\n\t\t\t\t";
         }
     }
     return $sqlUdp;
 }
Exemplo n.º 2
0
/**
 * Generates user table.
 *
 * @param iMSCP_pTemplate $tpl Template engine instance
 */
function admin_generateCustomersTable($tpl)
{
    /** @var $cfg iMSCP_Config_Handler_File */
    $cfg = iMSCP_Registry::get('config');
    $query = 'SELECT `admin_id`, `admin_name` FROM `admin` WHERE `admin_type` = ? ORDER BY `admin_name`';
    $stmt = exec_query($query, 'reseller');
    if (!$stmt->rowCount()) {
        // Should never occurs
        set_page_message(tr('Reseller list is empty.'), 'error');
        redirectTo('manage_users.php');
    }
    $resellerId = $stmt->fields['admin_id'];
    $allResellers = array();
    while (!$stmt->EOF) {
        if (isset($_POST['uaction']) && $_POST['uaction'] == 'change_src' && (isset($_POST['src_reseller']) && $_POST['src_reseller'] == $stmt->fields['admin_id'])) {
            $selected = $cfg->HTML_SELECTED;
            $resellerId = $_POST['src_reseller'];
        } elseif (isset($_POST['uaction']) && $_POST['uaction'] == 'move_user' && (isset($_POST['dst_reseller']) && $_POST['dst_reseller'] == $stmt->fields['admin_id'])) {
            $selected = $cfg->HTML_SELECTED;
            $resellerId = $_POST['dst_reseller'];
        } else {
            $selected = '';
        }
        $allResellers[] = $stmt->fields['admin_id'];
        $tpl->assign(array('SRC_RSL_OPTION' => tohtml($stmt->fields['admin_name']), 'SRC_RSL_VALUE' => $stmt->fields['admin_id'], 'SRC_RSL_SELECTED' => $selected));
        $tpl->parse('SRC_RESELLER_OPTION', '.src_reseller_option');
        $tpl->assign(array('DST_RSL_OPTION' => tohtml($stmt->fields['admin_name']), 'DST_RSL_VALUE' => $stmt->fields['admin_id'], 'DST_RSL_SELECTED' => ''));
        $tpl->parse('DST_RESELLER_OPTION', '.dst_reseller_option');
        $stmt->moveNext();
    }
    if (isset($_POST['src_reseller']) && $_POST['src_reseller'] == 0) {
        $selected = $cfg->HTML_SELECTED;
        $resellerId = 0;
    } else {
        $selected = '';
    }
    $tpl->assign(array('SRC_RSL_OPTION' => tr('N/A'), 'SRC_RSL_VALUE' => 0, 'SRC_RSL_SELECTED' => $selected));
    $tpl->parse('SRC_RESELLER_OPTION', '.src_reseller_option');
    // Must never occur in normal usage. Any user returned here are not assigned to a reseller
    if ($resellerId == 0) {
        $db = iMSCP_Database::getRawInstance();
        $query = '
			SELECT
				`admin_id`, `admin_name`
			FROM
				`admin`
			WHERE
				`admin_type` = ?
			AND
				`created_by` NOT IN (' . implode(',', array_map(array($db, 'quote'), $allResellers)) . ')
			ORDER BY
				`admin_name`
		';
        $stmt = exec_query($query, 'user');
    } else {
        $query = 'SELECT `admin_id`, `admin_name` FROM `admin` WHERE `admin_type` = ? AND `created_by` = ? ORDER BY `admin_name`';
        $stmt = exec_query($query, array('user', $resellerId));
    }
    if (!$stmt->rowCount()) {
        if ($resellerId) {
            set_page_message(tr('No users found for this reseller.'), 'static_info');
        } else {
            set_page_message(tr('No unassigned users were found in the database.'), 'static_info');
        }
        $tpl->assign('RESELLER_ITEM', '');
    } else {
        while (!$stmt->EOF) {
            $adminId = $stmt->fields['admin_id'];
            $adminIdVarname = 'admin_id_' . $adminId;
            $humanAdminName = decode_idna($stmt->fields['admin_name']);
            $tpl->assign(array('CUSTOMER_ID' => $stmt->fields['admin_id'], 'USER_NAME' => tohtml($humanAdminName), 'CKB_NAME' => $adminIdVarname));
            $tpl->parse('RESELLER_ITEM', '.reseller_item');
            $stmt->moveNext();
        }
        $tpl->parse('RESELLER_LIST', 'reseller_list');
    }
}
Exemplo n.º 3
0
/**
 * Synchronizes mailboxes quota that belong to the given domain using the given quota limit
 *
 * Algorythm:
 *
 * 1. In case the new quota limit is 0 (unlimited), equal or bigger than the sum of current quotas, we do nothing
 * 2. We have a running total, which start at zero
 * 3. We divide the quota of each mailbox by the sum of current quotas, then we multiply the result by the new quota limit
 * 4. We store the original value of the running total elsewhere, then we add the amount we have just calculated in #3
 * 5. We ensure that new quota is a least 1 MiB (each mailbox must have 1 MiB minimum quota)
 * 5. We round both old value and new value of the running total to integers, and take the difference
 * 6. We update the mailbox quota result calculated in step 5
 * 7. We repeat steps 3-6 for each quota
 *
 * This algorythm guarantees to have the total amount prorated equal to the sum of all quota after update. It also
 * ensure that each mailboxes has 1 MiB quota minimum.
 *
 * Note:  For the sum calculation of current quotas, we consider that a mailbox with a value equal to 0 (unlimited) is
 * equal to the new quota limit.
 *
 * @author Laurent Declercq <*****@*****.**>
 * @param int $domainId Customer main domain unique identifier
 * @param int $newQuota New quota limit in bytes
 * @return void
 */
function sync_mailboxes_quota($domainId, $newQuota)
{
    if ($newQuota != 0) {
        /** @var iMSCP_Config_Handler_File $cfg */
        $cfg = iMSCP_Registry::get('config');
        $stmt = exec_query('SELECT `mail_id`, `quota` FROM `mail_users` WHERE `domain_id` = ? AND `quota` IS NOT NULL', $domainId);
        if ($stmt->rowCount()) {
            $mailboxes = $stmt->fetchAll(PDO::FETCH_ASSOC);
            $totalQuota = 0;
            foreach ($mailboxes as $mailbox) {
                $totalQuota += $mailbox['quota'] == 0 ? $newQuota : $mailbox['quota'];
            }
            $totalQuota /= 1048576;
            $newQuota /= 1048576;
            if ($newQuota < $totalQuota || isset($cfg['EMAIL_QUOTA_SYNC_MODE']) && $cfg['EMAIL_QUOTA_SYNC_MODE'] || $totalQuota == 0) {
                $db = iMSCP_Database::getRawInstance();
                $stmt = $db->prepare('UPDATE `mail_users` SET `quota` = ? WHERE `mail_id` = ?');
                $result = 0;
                foreach ($mailboxes as $mailbox) {
                    $oldResult = $result;
                    $mailboxQuota = $mailbox['quota'] ? $mailbox['quota'] / 1048576 : $newQuota;
                    $result += $newQuota * $mailboxQuota / $totalQuota;
                    if ($result < 1) {
                        $result = 1;
                    }
                    $stmt->execute(array(((int) $result - (int) $oldResult) * 1048576, $mailbox['mail_id']));
                }
            }
        }
    }
}
Exemplo n.º 4
0
 /**
  * Load configuration parameters from the database
  *
  * This function retrieves all the parameters from the database and merge them with the basis configuration object.
  *
  * Parameters that exists in the basis configuration object will be replaced by those that come from the database.
  * The basis configuration object contains parameters that come from the i-mscp.conf configuration file or any
  * parameter defined in the {@link environment.php} file.
  *
  * @throws iMSCP_Exception
  * @return void
  */
 protected function loadConfig()
 {
     /** @var $pdo PDO */
     $pdo = iMSCP_Database::getRawInstance();
     if (is_readable(DBCONFIG_CACHE_FILE_PATH)) {
         if (!$this->config['DEBUG']) {
             /** @var iMSCP_Config_Handler_Db $dbConfig */
             $dbConfig = unserialize(file_get_contents(DBCONFIG_CACHE_FILE_PATH));
             $dbConfig->setDb($pdo);
         } else {
             @unlink(DBCONFIG_CACHE_FILE_PATH);
             goto FORCE_DBCONFIG_RELOAD;
         }
     } else {
         FORCE_DBCONFIG_RELOAD:
         // Creating new Db configuration handler.
         $dbConfig = new iMSCP_Config_Handler_Db($pdo);
         if (!$this->config['DEBUG'] && PHP_SAPI != 'cli') {
             @file_put_contents(DBCONFIG_CACHE_FILE_PATH, serialize($dbConfig), LOCK_EX);
         }
     }
     // Merge main configuration object with the dbConfig object
     $this->config->merge($dbConfig);
     // Add the dbconfig object into the registry for later use
     iMSCP_Registry::set('dbConfig', $dbConfig);
 }
Exemplo n.º 5
0
// Include i-MSCP core library
include '/var/www/imscp/gui/library/imscp-lib.php';
error_reporting(0);
ini_set('display_errors', 0);
ini_set('max_execution_time', 0);
// Full path to CSV file
if (isset($argv[1])) {
    $csvFilePath = $argv[1];
} else {
    printf("USAGE: php %s <FULL_PATH_TO_CSV_FILE>\n", $argv[0]);
    exit(1);
}
// csv column delimiter
$csvDelimiter = ',';
if (($handle = fopen($csvFilePath, 'r')) !== false) {
    $db = iMSCP_Database::getRawInstance();
    $stmt = $db->prepare('
			INSERT INTO mail_users (
				mail_acc, mail_pass, mail_forward, domain_id, mail_type, sub_id, status, mail_auto_respond,
				mail_auto_respond_text, quota, mail_addr
			) VALUES (
				:mail_acc, :mail_pass, :mail_forward, :domain_id, :mail_type, :sub_id, :status, :mail_auto_respond,
				:mail_auto_respond_text, :quota, :mail_addr
			)
		');
    // Create i-MSCP mail accounts using entries from CSV file
    while (($csvEntry = fgetcsv($handle, 1024, $csvDelimiter)) !== false) {
        $mailAddr = trim($csvEntry[0]);
        $asciiMailAddr = encode_idna($mailAddr);
        $mailPassword = trim($csvEntry[1]);
        try {