コード例 #1
0
 /**
  * Validates settings coming from an HTML form and also for internal use.
  * This is used when saving form an HTML form to the db, and also when reading from the db
  * back into the global settings.
  * @param string $name
  * @param mixed $value
  */
 public static function validate($name, $value)
 {
     if (!is_string($name)) {
         return '';
     }
     // Type-cast to correct value for known settings
     if (($setting = CrayonGlobalSettings::get($name)) != FALSE) {
         // Booleans settings that are sent as string are allowed to have "false" == false
         if (is_string($value) && is_bool($setting->def())) {
             $value = CrayonUtil::str_to_bool($value);
         }
         // Ensure we don't cast integer settings to 0 because $value doesn't have any numbers in it
         if (is_string($value) && is_int($setting->def())) {
             // Only occurs when saving from the form ($_POST values are strings)
             if ($value == '' || ($cleaned = CrayonUtil::clean_int($value, FALSE)) == '') {
                 // The value sent has no integers, change to default
                 $value = $setting->def();
             } else {
                 // Cleaned value is int
                 $value = $cleaned;
             }
             $value = intval($value);
             // Cast all other settings as usual
         } else {
             if (!settype($value, $setting->type())) {
                 // If we can't cast, then use default value
                 if ($setting->is_array()) {
                     $value = 0;
                     // default index
                 } else {
                     $value = $setting->def();
                 }
             }
         }
     } else {
         // If setting not found, remove value
         return '';
     }
     // Validations
     if ($name == CrayonSettings::HEIGHT || $name == CrayonSettings::WIDTH) {
         if ($value < 0) {
             $value = 0;
         }
     }
     switch ($name) {
         case CrayonSettings::LOCAL_PATH:
             $path = parse_url($value, PHP_URL_PATH);
             // Remove all spaces, prefixed and trailing forward slashes
             $path = preg_replace('#^/*|/*$|\\s*#', '', $path);
             // Replace backslashes
             $path = preg_replace('#\\\\#', '/', $path);
             // Append trailing forward slash
             if (!empty($path)) {
                 $path .= '/';
             }
             return $path;
         case CrayonSettings::TAB_SIZE:
             $value = abs($value);
             break;
         case CrayonSettings::FONT_SIZE:
             if ($value < 1) {
                 $value = 1;
             }
             break;
         case CrayonSettings::THEME:
             $value = strtolower($value);
             // XXX validate settings here
     }
     // If no validation occurs, return value
     return $value;
 }