Example #1
0
 /**
  * Write a configuration value to the database
  * @param helper\Database $db
  * @param string $item
  * @param mixed $value
  * @param int $editable
  * @return bool|string
  */
 public function setDBConfig(Database $db, $item, $value, $editable = 1)
 {
     ## in case DB hasn't been initialised
     if (!$this->get('has_db_config')) {
         return false;
     }
     if (!isset($this->default_config[$item])) {
         $configInfo = array('type' => 'unknown', 'allowempty' => true, 'value' => '');
     } else {
         $configInfo = $this->default_config[$item];
     }
     ## to validate we need the actual values
     $value = str_ireplace('[domain]', $this->get('domain'), $value);
     $value = str_ireplace('[website]', $this->get('website'), $value);
     switch ($configInfo['type']) {
         case 'boolean':
             if ($value == "false" || $value == "no") {
                 $value = 0;
             } elseif ($value == "true" || $value == "yes") {
                 $value = 1;
             }
             break;
         case 'integer':
             $value = sprintf('%d', $value);
             if ($value < $configInfo['min']) {
                 $value = $configInfo['min'];
             }
             if ($value > $configInfo['max']) {
                 $value = $configInfo['max'];
             }
             break;
         case 'email':
             if (!Validation::validateEmail($value, $this->get('EMAIL_ADDRESS_VALIDATION_LEVEL'), $this->get('internet_tlds'))) {
                 return $configInfo['description'] . ': ' . 'Invalid value for email address';
             }
             break;
         case 'emaillist':
             $valid = array();
             $emails = explode(',', $value);
             foreach ($emails as $email) {
                 if (Validation::validateEmail($email, $this->get('EMAIL_ADDRESS_VALIDATION_LEVEL'), $this->get('internet_tlds'))) {
                     $valid[] = $email;
                 } else {
                     return $configInfo['description'] . ': ' . 'Invalid value for email address';
                 }
             }
             $value = join(',', $valid);
             break;
     }
     ## reset to default if not set, and required
     if (empty($configInfo['allowempty']) && empty($value)) {
         $value = $configInfo['value'];
     }
     if (!empty($configInfo['hidden'])) {
         $editable = false;
     }
     $db->replaceQuery($this->getTableName('config'), array('item' => $item, 'value' => $value, 'editable' => $editable), 'item');
     //add to running config
     $this->setRunningConfig($item, $value);
     return true;
 }