/** * Find a controller based on the parsed request path */ protected function findController() { $parts = $this->path; $className = '\\Controllers\\'; array_pop($parts); $dir = APPLICATION_PATH . '/controllers/'; foreach ($parts as $part) { if (file_exists($dir . $part . '.php')) { $this->controller = $dir . $part . '.php'; $this->controllerClassName = $className . Utilities::formatClassName($part); return; } if (!is_dir($dir . $part . '/')) { break; } $dir .= $part . '/'; if (file_exists($dir . 'Index.php')) { $this->controller = $dir . 'Index.php'; $this->controllerClassName = $className . 'Index'; return; } } if (file_exists($dir . 'Index.php')) { $this->controller = $dir . 'Index.php'; $this->controllerClassName = $className . 'Index'; } }
/** * StringsHandler constructor. */ public function __construct() { $config = Config::getInstance(); $adapterClass = __NAMESPACE__ . '\\ConfigAdapter\\' . Utilities::formatClassName($config->get('strings_adapter', 'ini')); $this->adapter = new $adapterClass('strings'); $this->loadStrings(); }
/** * Get singleton instance * * @return Cache */ public static function getInstance() { if (null === static::$instance) { $config = Config::getInstance(); $adapterClass = __NAMESPACE__ . '\\CacheAdapter\\' . Utilities::formatClassName($config->get('cache_adapter', 'file')); static::$instance = new static(); static::$instance->adapter = new $adapterClass(); } return static::$instance; }
/** * FormValidator constructor. * @param array $post * @param View $view */ public function __construct(array $post, View $view) { $this->view = $view; $errors = array(); $fields = $this->view->getFormFields(); $formData = array(); foreach ($fields as $name => $field) { // filter values $value = !empty($post[$name]) ? trim($post[$name]) : ''; $formattedName = Utilities::formatClassName($name); // validate data if (isset($field['required']) && $field['required'] != 'false' && empty($value)) { $errors[$name] = $formattedName . ' is a required field.'; } if (!empty($value)) { if (isset($field['type']) && $field['type'] == 'email' && !$this->isValidEmail($value)) { $errors[$name] = $formattedName . ' must be a valid email address.'; } if (isset($field['type']) && $field['type'] == 'url' && !preg_match('@^https?://@', $value)) { $errors[$name] = $formattedName . ' must be a valid URL.'; } if (isset($field['maxlength']) && $field['maxlength'] < trim(strlen($value))) { $errors[$name] = $formattedName . ' can not be more than ' . $field['maxlength'] . ' characters.'; } if (isset($field['minlength']) && $field['minlength'] > trim(strlen($value))) { $errors[$name] = $formattedName . ' must be more than ' . $field['minlength'] . ' characters.'; } if (isset($field['type']) && $field['type'] == 'number') { $value = (double) $value; if (isset($field['max']) && $field['max'] < $value) { $errors[$name] = $formattedName . ' must be a number less than ' . $field['max'] . '.'; } if (isset($field['min']) && $field['min'] > $value) { $errors[$name] = $formattedName . ' must be a number greater than ' . $field['min'] . '.'; } } if (isset($field['pattern']) && !preg_match('/' . str_replace('/', '\\/', $field['pattern']) . '/', $value)) { $errors[$name] = $formattedName . ' is not in a valid format.'; } } $formData[$name] = $value; } // validate recaptcha $config = Config::getInstance(); if ($config->recaptcha_on && !$this->isValidCaptcha()) { $errors['recaptcha'] = 'An incorrect CAPTCHA code was entered.'; } $this->errors = $errors; $this->data = $formData; }
/** * Render fields for a specific section * * @param array $items * @param array $groups * @param string $rowTemplate * @param string $fieldGroup * @return array */ protected function renderFields(array $items, array $groups, $rowTemplate, $fieldGroup = '', $isSeries = false) { $rows = array(); foreach ($items as $key => $value) { // detect special suffixes $suffix = $fieldGroup == '' ? 'textarea' : 'text'; $suffixOffset = strpos($key, '/'); if ($suffixOffset !== false) { $suffix = substr($key, $suffixOffset + 1); } // format values $name = $fieldGroup == '' ? sprintf('data[%s][%s]', implode('][', $groups), $key) : "{$fieldGroup}[{$key}]"; $label = ucwords(preg_replace('/\\/.+/', '', str_replace(array('-', '_'), ' ', $key))); if ($isSeries) { $name .= '[]'; } // determine helper class to use for rendering form field $helperClass = __NAMESPACE__ . '\\TokenHelpers\\' . Utilities::formatClassName($suffix); $helper = new $helperClass($value, $name, $label); // add field to row if (!is_array($value)) { $rows[] = str_replace('[[__content]]', $helper->getField(), $rowTemplate); } else { // iterate through and process tokens each item $subFieldGroups = array(); // restructure array $subValues = array(); foreach ($value as $subStringKey => $subStringValues) { $i = 0; foreach ($subStringValues as $subStringValue) { if (!isset($subValues[$i])) { $subValues[$i] = array(); } $subValues[$i][$subStringKey] = $subStringValue; ++$i; } } // build sub-field groups foreach ($subValues as $subValue) { $subFields = $this->renderFields($subValue, array_merge($groups, array($key)), $rowTemplate, $fieldGroup, true); $subFieldGroups[] = $helper->processFields($subFields); } $rows[] = $helper->processFieldGroup($subFieldGroups); } } return $rows; }
/** * Process tokens found within templates * * @param string $tokenGroup * @param array $strings * @param string $html * @return string */ protected function processTokens($tokenGroup, $strings, $html) { // process any global tokens if (!empty($strings['_global'])) { foreach ($strings['_global'] as $token => $value) { $html = str_replace('[[' . $token . ']]', $value, $html); } } // process any action specific tokens if (isset($strings[$tokenGroup])) { foreach ($strings[$tokenGroup] as $token => $value) { // detect special suffixes $suffix = 'textarea'; $suffixOffset = strpos($token, '/'); if ($suffixOffset !== false) { $suffix = substr($token, $suffixOffset + 1); } // determine helper class to use for rendering token's string $helperClass = __NAMESPACE__ . '\\TokenHelpers\\' . Utilities::formatClassName($suffix); $helper = new $helperClass($value, $token); // replace token with string in HTML if (!is_array($value)) { $html = str_replace('[[' . $token . ']]', $helper->getString(), $html); } else { // get html slice within repeater region if (!preg_match('@\\[\\[' . $token . '\\]\\](.*?)\\[\\[/' . $token . '\\]\\]@mis', $html, $match)) { continue; } $htmlSlice = $match[1]; // restructure array $subValues = array(); foreach ($value as $subStringKey => $subStringValues) { $i = 0; foreach ($subStringValues as $subStringValue) { if (!isset($subValues[$i])) { $subValues[$i] = array(); } $subValues[$i][$subStringKey] = $subStringValue; ++$i; } } // iterate through and process tokens each item $output = array(); foreach ($subValues as $subStrings) { $output[] = $this->processTokens(0, array($subStrings), $htmlSlice); } // process item results with repeater token helper $htmlSlice = $helper->processRegion($output); // replace original html region slice with new content $html = preg_replace('@\\[\\[' . $token . '\\]\\].*?\\[\\[/' . $token . '\\]\\]@mis', $htmlSlice, $html); } } } return $html; }