<?php

/**
 * @file
 *   Form with PHP Settings
 *
 * @version
 *   $Id: drupal_tweaks.admin.php.inc,v 1.1.2.3 2009/09/24 23:11:25 kenorb Exp $
 *
 * @developers
 *   Rafal Wieczorek <kenorb@gmail.com>
 */


/**
 * Menu callback for the settings form.
 */
function drupal_tweaks_php_settings_form() {
  module_load_include('inc', 'drupal_tweaks'); // load additional functions from included file
  drupal_tweaks_include_shared_code();

  $php_conf = drupal_tweaks_get_php_configuration(); // get actual settings
  if ((bool)$php_conf['memory_limit']['php'] !== (bool)$php_conf['memory_limit']['conf']) {
      drupal_set_message(t("Actual %variable is differ than it was set, probably you do not have proper privileges.", array('%variable' => 'memory_limit')),'error');
  }
  if ((int)$php_conf['max_execution_time']['php'] !== (int)$php_conf['max_execution_time']['conf']) {
      drupal_set_message(t("Actual %variable is differ than it was set, probably you do not have proper privileges.", array('%variable' => 'max_execution_time')),'error');
  }

  $form['php'] = array(
      '#type' => 'fieldset',
      '#title' => t('PHP settings'),
      '#description' => t('You may check your actual PHP configuration on <a href="!url">Status Report page</a> or on <a href="!url_php">PHP Info page</a>.', array('!url' => url('admin/reports/status'), '!url_php' => url('admin/reports/status/php'))),
      '#collapsible' => TRUE,
  ); 
  $form['php']['drupal_tweaks_php_activated'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable PHP tweaks.'),
    '#description' => t('Select if you want to enable above changes.'),
    '#default_value' => variable_get('drupal_tweaks_php_activated', FALSE),
  );
  $form['php']['drupal_tweaks_php_memory_limit'] = array(
      '#type' => 'textfield',
      '#title' => t('Default PHP memory limit.'),
      '#default_value' => $php_conf['memory_limit']['php'],
      '#size' => 20,
      '#maxlength' => 20,
      '#description' => t('Maximum memory limit. Some hosting providers could not allow you to change that. Examples: 20M, 128M'),
  ); 
  $form['php']['drupal_tweaks_php_max_execution_time'] = array(
      '#type' => 'textfield',
      '#title' => t('Default maximum execution time for php scripts.'),
      '#default_value' => $php_conf['max_execution_time']['php'],
      '#size' => 10,
      '#maxlength' => 8,
      '#description' => t('For how long PHP script should be executed, before it will be terminated.'),
      '#field_suffix' => t('sec'),
  ); 
  $form['php']['drupal_tweaks_php_upload_max_filesize'] = array(
      '#type' => 'textfield',
      '#title' => t('The maximum size of an uploaded file (upload_max_filesize).'),
      '#default_value' => $php_conf['upload_max_filesize']['php'],
      '#size' => 10,
      '#maxlength' => 8,
      '#field_suffix' => t('MB'),
  ); 

  if (!variable_get('drupal_tweaks_php_activated', FALSE)) {
    drupal_set_message(t('Please enable PHP tweaks to activate following settings.'), 'warning');
  }

  $form['#validate'] = array('drupal_tweaks_php_settings_form_validate');

  return system_settings_form($form); 
}
 
/**
 * Form API callback to validate the upload settings form.
 */
function drupal_tweaks_php_settings_form_validate($form, &$form_state) {
  module_load_include('inc', 'drupal_tweaks');
  $php_conf = drupal_tweaks_get_php_configuration(TRUE); // get db configuration

  // get actual variables from configuration
  $max_execution_time = $php_conf['max_execution_time']['conf'] = $form_state['values']['drupal_tweaks_php_max_execution_time'];
  $memory_limit = $php_conf['memory_limit']['conf'] = $form_state['values']['drupal_tweaks_php_memory_limit'];
  $upload_max_filesize = $php_conf['upload_max_filesize']['conf'] = $form_state['values']['drupal_tweaks_php_upload_max_filesize'];

  // validate PHP memory_limit
  if (!is_numeric(parse_size($memory_limit)) && parse_size($memory_limit) <= 0) {
      form_set_error('drupal_tweaks_php_memory_limit', t('The PHP memory_limit limit must be a number and greater than 0 (current limit is: %size).', array('%size' => $php_conf['memory_limit']['php'])));
  } else if (parse_size($memory_limit) < parse_size(DRUPAL_MINIMUM_PHP_MEMORY_LIMIT)) {
      form_set_error('drupal_tweaks_php_memory_limit', t('To prevent breaking your website, it is strictly recommended that PHP memory_limit should be greater than %min_memory_limit (current limit is: %size).', array('%size' => $php_conf['memory_limit']['php'], '%min_memory_limit' => DRUPAL_MINIMUM_PHP_MEMORY_LIMIT)));
  }

  // validate PHP upload_max_filesize
  if (!is_numeric(parse_size($upload_max_filesize)) && parse_size($upload_max_filesize) <= 1) {
      form_set_error('drupal_tweaks_php_upload_max_filesize', t('The PHP upload_max_filesize limit must be a number and greater than 1 (current limit is: %size).', array('%size' => $php_conf['upload_max_filesize']['php'])));
  }

  // validate PHP max_execution_time
  if (!is_numeric($max_execution_time) || ($max_execution_time <= 0)) {
      form_set_error('drupal_tweaks_php_max_execution_time', t('The max_execution_time must be a number and greater than 0 (current timeout is: %size).', array('%size' => $php_conf['max_execution_time']['php'])));
  } else if ((int)$max_execution_time < (int)DRUPAL_MINIMUM_MAX_EXECUTION_TIME) {
      form_set_error('drupal_tweaks_php_max_execution_time', t('To prevent breaking your website, it is strictly recommended that PHP max_execution_time should be greater than %min_max_execution_time (current limit is: %size).', array('%size' => $php_conf['memory_limit']['php'], '%min_max_execution_time' => DRUPAL_MINIMUM_PHP_MEMORY_LIMIT)));
  }
  if (!form_get_errors()) {
    /* validate PHP */
    foreach ($php_conf as $var_name => $var_values) {
        // update php settings if necessary
        if ($var_values['conf'] <> $var_values['php']) {
            if (!ini_set($var_name, $var_values['conf'])) {
              drupal_set_message(t('Cannot set variable `%variable` to `%value` in your PHP configuration!', array('%variable' => $var_name, '%value' => $var_values['conf'])). t('<br>') . t('Probably you do not have proper privileges.'), 'error');
              variable_set($var_name, $var_values['php']); // reverting changes in settings (to prevent showing error message)
            }
        }
    }
  }
}

