/** * Gets all of the values from a configuration object * All configuration keys are converted to lowercase * * @param object $source * @return bool */ public function load($source) { if (!$source instanceof Config_base) { throw new Config_InvalidObject('unable to load config object, class does not extend "Config_base"'); } $values = $source->getAll(); if (!is_array($values)) { throw new Config_InvalidValue('unable to load config, value given is not an array'); } // Load the values return $this->setConfigValues(zula_merge_recursive($this->getAll(), $values)); }
/** * Get all of the configuration values from the provided table * * @param string $table * @return bool */ public function load($table) { $this->setTable($table); $configValues = array(); foreach ($this->_sql->query('SELECT * FROM {PREFIX}' . $table, PDO::FETCH_ASSOC) as $row) { /** * Create the configuration array into the correct* format that is needed */ $configSplit = preg_split('#(?<!\\\\)/#', trim($row['name'], '/')); foreach ($configSplit as &$val) { $val = str_replace('\\/', '/', $val); } $splitCount = count($configSplit); $confKey = array($configSplit[$splitCount - 1] => $row['value']); for ($i = $splitCount - 2; $i >= 0; $i--) { $confKey = array($configSplit[$i] => $confKey); } $configValues = zula_merge_recursive($configValues, $confKey); } return $this->setConfigValues($configValues); }
/** * Builds the view form to add/edit a user * * @param array $details * @return object */ protected function buildUserForm(array $details = array()) { $op = empty($details['id']) ? 'add' : 'edit'; $details = zula_merge_recursive(array('id' => null, 'username' => null, 'status' => 'active', 'group' => null, 'first_name' => null, 'last_name' => null, 'email' => null, 'hide_email' => true), $details); // Build form and validation $form = new View_Form('config/user_form.html', 'users', empty($details['id'])); $form->addElement('users/username', $details['username'], t('Username'), array(new Validator_Alphanumeric('_()!:@.^-'), new Validator_Length(2, 32), array($this, 'validateUsername'))); $form->addElement('users/status', $details['status'], t('Status'), new Validator_InArray(array('active', 'locked'))); $form->addElement('users/group', $details['group'], t('Group'), new Validator_Int(), false); $form->addElement('users/first_name', $details['first_name'], t('First name'), new Validator_Length(0, 255)); $form->addElement('users/last_name', $details['last_name'], t('Last name'), new Validator_Length(0, 255)); $form->addElement('users/password', null, t('Password'), array(new Validator_Length(4, 32), new Validator_Confirm('users_password_confirm', Validator_Confirm::_POST)), empty($details['id'])); $form->addElement('users/hide_email', $details['hide_email'], t('Hide email'), new Validator_Bool()); // Email validation, we still want to display email when editing remember $emailValidation = array(new Validator_Email()); if ($op == 'add' || $this->_input->has('post', 'users_email_confirm') && $this->_input->post('users_email_confirm')) { $emailValidation[] = new Validator_Confirm('users_email_confirm', Validator_Confirm::_POST); } $form->addElement('users/email', $details['email'], t('Email'), $emailValidation); $form->assign(array('OP' => $op, 'ID' => $details['id'])); return $form; }
/** * Assigns a new tag to use that will get replaced, all array keys * will get converted to lower case! * * @param array $tags * @param bool $overwrite If a tag already exists, should it be overwriten or appended? * @param bool $allowHtml should HTML be allowed in the tag value? * @param bool $prepend Prepend content to the tag, instead of append * @return object */ public function assign(array $tags, $overwrite = true, $allowHtml = false, $prepend = false) { if ($this->caseSensitive === false) { zula_array_key_case($tags, CASE_LOWER); } foreach ($tags as $tag => $val) { if ($allowHtml == false) { $val = $this->cleanTagValue($val); } unset($tags[$tag]); $tags[$this->cleanTag($tag)] = $val; } if (empty($this->assignedTags)) { $this->assignedTags = $tags; } else { if ($overwrite == false) { /** * Allows tags that have been assigned more than once to be * appended onto the end, instead of overwriting the old tag */ foreach ($tags as $key => $val) { if (isset($this->assignedTags[$key])) { if (is_array($val)) { if ($prepend) { $this->assignedTags[$key] = zula_merge_recursive($val, $this->assignedTags[$key]); } else { $this->assignedTags[$key] = zula_merge_recursive($this->assignedTags[$key], $val); } } else { if ($prepend) { $this->assignedTags[$key] = $val . $this->assignedTags[$key]; } else { $this->assignedTags[$key] .= $val; } } } else { $this->assignedTags[$key] = $val; } } } else { $this->assignedTags = zula_merge_recursive($this->assignedTags, $tags); } } return $this; }
/** * Merges 2 arrays recursively, similar to array_merge_recursive * however it will replace the values if there are any conflicts * instead of creating an array of the valuess * * @param array $array1 * @param array $array2 * @return array */ function zula_merge_recursive($array1, $array2) { if (!is_array($array1) && !is_array($array2)) { return $array2; } if (empty($array1)) { return $array2; } else { if (empty($array2)) { return $array1; } } foreach ($array2 as $key => $val) { if (isset($array1[$key])) { $array1[$key] = zula_merge_recursive($array1[$key], $val); } else { $array1[$key] = $val; } } return $array1; }
/** * Fetchs input from the correct superglobal and empties * the array so they can not be used directly in the script. * * @param string $type * @return bool */ protected function fetchInput($type) { if (!array_key_exists($type, $this->inputs)) { throw new Input_Exception('unknown input type "' . $type . '"'); } if ($this->inputs[$type] === null) { switch ($type) { case 'POST': $inputData = $_POST; if (isset($_SESSION['post-restore'])) { $inputData = zula_merge_recursive($inputData, $_SESSION['post-restore']); unset($_SESSION['post-restore']); } $_POST = array(); break; case 'GET': $inputData = $_GET; $_GET = array(); break; case 'COOKIE': $inputData = $_COOKIE; $_COOKIE = array(); break; case 'CLI': $inputData = array(); if (!isset($_SERVER['argv'])) { throw new Input_Exception("'register_argc_argv' appears to be disabled"); } $cliArgs = array_slice($_SERVER['argv'], 1); while (($arg = array_shift($cliArgs)) !== null) { if ($arg[0] === '-') { if (preg_match('#^--([A-Za-z_\\-]+)=(.*?)$#', $arg, $matches)) { // Support for --flag=parameter $flag = $matches[1]; $param = $matches[2]; } else { $flag = ltrim($arg, '-'); $param = array_shift($cliArgs); } if (isset($inputData[$flag])) { $inputData[$flag] = (array) $inputData[$flag]; $inputData[$flag][] = $param; } else { $inputData[$flag] = $param; } } } break; } } foreach ($inputData as $key => $val) { if (($newKey = $this->cleanInputKey($key)) != $key) { unset($inputData[$key]); } $inputData[$newKey] = $this->cleanInputData($val); } $this->inputs[$type] = $inputData; return true; }
/** * Runs all of the validation checks on the elements using the * validatiors that are stored * * @return bool */ public function isValid() { if ($this->csrfToken === true && !$this->_input->checkToken()) { // CSRF protection failed! if ($this->storeErrors === true) { $this->_event->error(Input::csrfMsg()); } return false; } foreach ($this->elements as $element) { try { $value = $this->_input->get($element['input_name'], $element['source']); } catch (Input_KeyNoExist $e) { if ($element['required'] === true) { throw $e; } else { continue; } } // Store the input names value correclty as a multi-dimensional array $tmpVal = $value; foreach (array_reverse(preg_split('#(?<!\\\\)/#', trim($element['input_name'], '/'))) as $v) { $tmpVal = array($v => $tmpVal); } $this->values = zula_merge_recursive($this->values, $tmpVal); $count = is_array($value) ? count($value) : strlen($value); if ($element['required'] === false && $count == 0) { continue; } // Check if it is valid $validator = new Validator($value, $element['title']); foreach (array_filter($element['validators']) as $tmpValidator) { $validator->add($tmpValidator); } if ($validator->validate() === false) { $this->valid = false; if ($this->storeErrors === true) { // Store all errors (if any) foreach ($validator->getErrors() as $error) { $this->_event->error($error); } } } } // Check if the antispam was successful, if enabled if ($this->valid && $this->antispam === true) { $antispam = new Antispam(); if (!$antispam->check()) { $this->valid = false; if ($this->storeErrors === true) { $this->_event->error(t('Sorry, incorrect answer to the captcha', I18n::_DTD)); } } } return $this->valid; }