/** * Parses the html for this filefield. * * @param \SpoonTemplate $template The template to parse the element in. * @return string */ public function parse($template = null) { // get upload_max_filesize $uploadMaxFilesize = ini_get('upload_max_filesize'); if ($uploadMaxFilesize === false) { $uploadMaxFilesize = 0; } // reformat if defined as an integer if (\SpoonFilter::isInteger($uploadMaxFilesize)) { $uploadMaxFilesize = $uploadMaxFilesize / 1024 . 'MB'; } // reformat if specified in kB if (strtoupper(substr($uploadMaxFilesize, -1, 1)) == 'K') { $uploadMaxFilesize = substr($uploadMaxFilesize, 0, -1) . 'kB'; } // reformat if specified in MB if (strtoupper(substr($uploadMaxFilesize, -1, 1)) == 'M') { $uploadMaxFilesize .= 'B'; } // reformat if specified in GB if (strtoupper(substr($uploadMaxFilesize, -1, 1)) == 'G') { $uploadMaxFilesize .= 'B'; } // name is required if ($this->attributes['name'] == '') { throw new \SpoonFormException('A name is required for a file field. Please provide a name.'); } // start html generation $output = '<input type="file"'; // add attributes $output .= $this->getAttributesHTML(array('[id]' => $this->attributes['id'], '[name]' => $this->attributes['name'])) . ' />'; // add help txt if needed if (!$this->hideHelpTxt) { if (isset($this->attributes['extension'])) { $output .= '<span class="helpTxt">' . sprintf(Language::getMessage('HelpFileFieldWithMaxFileSize', 'core'), $this->attributes['extension'], $uploadMaxFilesize) . '</span>'; } else { $output .= '<span class="helpTxt">' . sprintf(Language::getMessage('HelpMaxFileSize'), $uploadMaxFilesize) . '</span>'; } } // parse to template if ($template !== null) { $template->assign('file' . \SpoonFilter::toCamelCase($this->attributes['name']), $output); $template->assign('file' . \SpoonFilter::toCamelCase($this->attributes['name']) . 'Error', $this->errors != '' ? '<span class="formError">' . $this->errors . '</span>' : ''); } return $output; }
/** * Create form action and strip the identifier parameter. * * We use this function to create the action for the form. * This action cannot contain an identifier since these are used for * statistics and failed form submits cannot be tracked. * * @return string */ private function createAction() { // pages $action = implode('/', $this->URL->getPages()); // init parameters $parameters = $this->URL->getParameters(); $moduleParameters = array(); $getParameters = array(); // sort by key (important for action order) ksort($parameters); // loop and filter parameters foreach ($parameters as $key => $value) { // skip identifier if ($key === 'identifier') { continue; } // normal parameter if (\SpoonFilter::isInteger($key)) { $moduleParameters[] = $value; } else { // get parameter $getParameters[$key] = $value; } } // single language if ($this->getContainer()->getParameter('site.multilanguage')) { $action = LANGUAGE . '/' . $action; } // add to action if (count($moduleParameters) > 0) { $action .= '/' . implode('/', $moduleParameters); } if (count($getParameters) > 0) { $action .= '?' . http_build_query($getParameters, null, '&', PHP_QUERY_RFC3986); } // remove trailing slash $action = rtrim($action, '/'); // cough up action return '/' . $action; }
/** * Create form action and strip the identifier parameter. * * We use this function to create the action for the form. * This action cannot contain an identifier since these are used for statistics and failed form submits cannot be tracked. * * @return string */ private function createAction() { // pages $action = implode('/', $this->URL->getPages()); // init parameters $parameters = $this->URL->getParameters(); $moduleParameters = array(); $getParameters = array(); // sort by key (important for action order) ksort($parameters); // loop and filter parameters foreach ($parameters as $key => $value) { // skip identifier if ($key === 'identifier') { continue; } // normal parameter if (SpoonFilter::isInteger($key)) { $moduleParameters[] = $value; } else { $getParameters[$key] = $value; } } // single language if (SITE_MULTILANGUAGE) { $action = FRONTEND_LANGUAGE . '/' . $action; } // add to action if (count($moduleParameters) > 0) { $action .= '/' . implode('/', $moduleParameters); } if (count($getParameters) > 0) { $action .= '?' . http_build_query($getParameters); } // remove trailing slash $action = rtrim($action, '/'); // cough up action return SITE_URL . '/' . $action; }
public function testIsInteger() { $this->assertTrue(SpoonFilter::isInteger(0)); $this->assertTrue(SpoonFilter::isInteger(1)); $this->assertTrue(SpoonFilter::isInteger(1234567890)); $this->assertTrue(SpoonFilter::isInteger(-1234567890)); $this->assertFalse(SpoonFilter::isInteger(1.337)); $this->assertFalse(SpoonFilter::isInteger(-1.337)); $this->assertFalse(SpoonFilter::isInteger(array())); }
/** * Checks this field for numbers 0-9 and an optional - (minus) sign (in the beginning only). * * @return bool * @param string[optional] $error The error message to set. */ public function isInteger($error = null) { // filled if ($this->isFilled()) { // post/get data $data = $this->getMethod(true); // validate if (!isset($data[$this->attributes['name']]) || !SpoonFilter::isInteger($data[$this->attributes['name']])) { if ($error !== null) { $this->setError($error); } return false; } return true; } // not submitted if ($error !== null) { $this->setError($error); } return false; }
/** * Fetch a specific day of the week for a specific language. * * @return string * @param mixed $day The name/number of the day. * @param string[optional] $language The language to use, possible values can be found by calling SpoonLocale::getAvailableLanguages(). * @param bool[optional] $abbreviated Should the abbreviated value be used? */ public static function getWeekDay($day, $language = 'en', $abbreviated = false) { // init vars $dayIndexes = array('sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'); $dayNames = array('sunday' => 'sun', 'monday' => 'mon', 'tuesday' => 'tue', 'wednesday' => 'wed', 'thursday' => 'thu', 'friday' => 'fri', 'saturday' => 'sat'); $language = SpoonFilter::getValue($language, self::$languages, 'en'); $locale = array(); // which day? if (SpoonFilter::isInteger($day)) { $day = $dayIndexes[SpoonFilter::getValue(strtolower($day), range(0, 6), 0)]; } else { $day = $dayNames[SpoonFilter::getValue(strtolower($day), array_keys($dayNames), 'sunday')]; } // fetch file require 'data/' . $language . '.php'; // abbreviated? return $abbreviated ? $locale['date']['days']['abbreviated'][$day] : $locale['date']['days']['full'][$day]; }
/** * Checks if this field is correctly submitted. * * @return bool * @param string[optional] $error The errormessage to set. */ public function isValid($error = null) { // field has been filled in if ($this->isFilled()) { // post/get data $data = $this->getMethod(true); if (!is_scalar($data[$this->getName()])) { if ($error !== null) { $this->setError($error); } return false; } // maxlength checks out (needs to be equal) if (strlen((string) $data[$this->attributes['name']]) == $this->attributes['maxlength']) { // define long mask $longMask = str_replace(array('d', 'm', 'y', 'Y'), array('dd', 'mm', 'y', 'yy'), $this->mask); // init vars $year = (int) date('Y'); $month = (int) date('m'); $day = (int) date('d'); // validate year (yyyy) if (strpos($longMask, 'yy') !== false) { // redefine year $year = substr($data[$this->attributes['name']], strpos($longMask, 'yy'), 4); // not an int if (!SpoonFilter::isInteger($year)) { if ($error !== null) { $this->setError($error); } return false; } // invalid year if (!checkdate(1, 1, $year)) { if ($error !== null) { $this->setError($error); } return false; } } // validate year (yy) if (strpos($longMask, 'y') !== false && strpos($longMask, 'yy') === false) { // redefine year $year = substr($data[$this->attributes['name']], strpos($longMask, 'y'), 2); // not an int if (!SpoonFilter::isInteger($year)) { if ($error !== null) { $this->setError($error); } return false; } // invalid year if (!checkdate(1, 1, '19' . $year)) { if ($error !== null) { $this->setError($error); } return false; } } // validate month (mm) if (strpos($longMask, 'mm') !== false) { // redefine month $month = substr($data[$this->attributes['name']], strpos($longMask, 'mm'), 2); // not an int if (!SpoonFilter::isInteger($month)) { if ($error !== null) { $this->setError($error); } return false; } // invalid month if (!checkdate($month, 1, $year)) { if ($error !== null) { $this->setError($error); } return false; } } // validate day (dd) if (strpos($longMask, 'dd') !== false) { // redefine day $day = substr($data[$this->attributes['name']], strpos($longMask, 'dd'), 2); // not an int if (!SpoonFilter::isInteger($day)) { if ($error !== null) { $this->setError($error); } return false; } // invalid day if (!checkdate($month, $day, $year)) { if ($error !== null) { $this->setError($error); } return false; } } } else { if ($error !== null) { $this->setError($error); } return false; } } else { if ($error !== null) { $this->setError($error); } return false; } /** * When the code reaches the point, it means no errors have occured * and truth will out! */ return true; }
/** * Get templates * * @param string[optional] $theme The theme we want to fetch the templates from. * @return array */ public static function getTemplates($theme = null) { // get db $db = BackendModel::getDB(); // validate input $theme = SpoonFilter::getValue((string) $theme, null, BackendModel::getModuleSetting('core', 'theme', 'core')); // get templates $templates = (array) $db->getRecords('SELECT i.id, i.label, i.path, i.data FROM themes_templates AS i WHERE i.theme = ? AND i.active = ? ORDER BY i.label ASC', array($theme, 'Y'), 'id'); // get extras $extras = (array) self::getExtras(); // init var $half = (int) ceil(count($templates) / 2); $i = 0; // loop templates to unserialize the data foreach ($templates as $key => &$row) { // unserialize $row['data'] = unserialize($row['data']); $row['has_block'] = false; // reset if (isset($row['data']['default_extras_' . BL::getWorkingLanguage()])) { $row['data']['default_extras'] = $row['data']['default_extras_' . BL::getWorkingLanguage()]; } // any extras? if (isset($row['data']['default_extras'])) { // loop extras foreach ($row['data']['default_extras'] as $value) { // store if the module has blocks if (SpoonFilter::isInteger($value) && isset($extras[$value]) && $extras[$value]['type'] == 'block') { $row['has_block'] = true; } } } // validate if (!isset($row['data']['format'])) { throw new BackendException('Invalid template-format.'); } // build template HTML $row['html'] = self::buildTemplateHTML($row['data']['format']); $row['htmlLarge'] = self::buildTemplateHTML($row['data']['format'], true); // add all data as json $row['json'] = json_encode($row); // add the break-element so the templates can be split in 2 columns in the templatechooser if ($i == $half) { $row['break'] = true; } // increment $i++; } return (array) $templates; }
/** * Get templates * * @param string $theme The theme we want to fetch the templates from. * * @return array * @throws Exception */ public static function getTemplates($theme = null) { $db = BackendModel::getContainer()->get('database'); $theme = \SpoonFilter::getValue((string) $theme, null, BackendModel::get('fork.settings')->get('Core', 'theme', 'Core')); $templates = (array) $db->getRecords('SELECT i.id, i.label, i.path, i.data FROM themes_templates AS i WHERE i.theme = ? AND i.active = ? ORDER BY i.label ASC', array($theme, 'Y'), 'id'); $extras = (array) self::getExtras(); $half = (int) ceil(count($templates) / 2); $i = 0; foreach ($templates as &$row) { $row['data'] = unserialize($row['data']); $row['has_block'] = false; // reset if (isset($row['data']['default_extras_' . BL::getWorkingLanguage()])) { $row['data']['default_extras'] = $row['data']['default_extras_' . BL::getWorkingLanguage()]; } // any extras? if (isset($row['data']['default_extras'])) { foreach ($row['data']['default_extras'] as $value) { if (\SpoonFilter::isInteger($value) && isset($extras[$value]) && $extras[$value]['type'] == 'block') { $row['has_block'] = true; } } } // validate if (!isset($row['data']['format'])) { throw new Exception('Invalid template-format.'); } $row['html'] = self::buildTemplateHTML($row['data']['format']); $row['htmlLarge'] = self::buildTemplateHTML($row['data']['format'], true); $row['json'] = json_encode($row); if ($i == $half) { $row['break'] = true; } ++$i; } return (array) $templates; }
/** * Sets the email priority level. * * @param int[optional] $level The e-mail's priority level (1-5, where 1 is not urgent). */ public function setPriority($level = 3) { // check input if (!SpoonFilter::isInteger($level) || !SpoonFilter::getValue($level, range(1, 5, 1), 3, 'int')) { throw new SpoonEmailException('No valid priority level given, integer from 1 to 5 required.'); } // store priority level $this->priority = $level; }
/** * Check the string for syntax errors * * @return bool * @param string $string * @param string $type */ private function isCorrectSyntax($string, $type) { // init vars $string = (string) $string; $type = SpoonFilter::getValue($type, array('cycle', 'iteration', 'option', 'variable'), 'variable', 'string'); // types switch ($type) { // cycle string case 'cycle': // the number of single qoutes should always be an even number if (!SpoonFilter::isEven(substr_count($string, "'"))) { return false; } break; // iteration string // iteration string case 'iteration': // the number of square opening/closing brackets should be equal if (substr_count($string, '[') != substr_count($string, ']')) { return false; } // the number of single qoutes should always be an even number if (!SpoonFilter::isEven(substr_count($string, "'"))) { return false; } // first charachter should not be a number if (SpoonFilter::isInteger(substr($string, 2, 1))) { return false; } // square bracket followed by a dot is NOT allowed eg {option:variable[0].var} if (substr_count($string, '].') != 0) { return false; } // dot followed by a square bracket is NOT allowed eg {option:variable.['test']} if (substr_count($string, '.[') != 0) { return false; } // empty brackets are NOT allowed if (substr_count($string, '[]') != 0) { return false; } break; // option string // option string case 'option': // the number of square opening/closing brackets should be equal if (substr_count($string, '[') != substr_count($string, ']')) { return false; } // the number of single qoutes should always be an even number if (!SpoonFilter::isEven(substr_count($string, "'"))) { return false; } // square bracket followed by a dot is NOT allowed eg {option:variable[0].var} if (substr_count($string, '].') != 0) { return false; } // dot followed by a square bracket is NOT allowed eg {option:variable.['test']} if (substr_count($string, '.[') != 0) { return false; } // empty brackets are NOT allowed if (substr_count($string, '[]') != 0) { return false; } break; // variable string // variable string case 'variable': // the number of square opening/closing brackets should be equal if (substr_count($string, '[') != substr_count($string, ']')) { return false; } // the number of single qoutes should always be an even number if (!SpoonFilter::isEven(substr_count($string, "'"))) { return false; } // first charachter should not be a number if (SpoonFilter::isInteger(substr($string, 2, 1))) { return false; } // square bracket followed by a dot is NOT allowed eg {$variable[0].var} if (substr_count($string, '].') != 0) { return false; } // dot followed by a square bracket is NOT allowed eg {$variable.['test']} if (substr_count($string, '.[') != 0) { return false; } // empty brackets are NOT allowed if (substr_count($string, '[]') != 0) { return false; } break; } return true; }