public function login($welcome = null) { if ($user = panel()->site()->user()) { go(panel()->urls()->index()); } $message = l('login.error'); $error = false; $form = panel()->form('login'); $form->cancel = false; $form->save = l('login.button'); $form->centered = true; if (r::is('post') and get('_csfr') and csfr(get('_csfr'))) { $data = $form->serialize(); $user = site()->user(str::lower($data['username'])); if (!$user) { $error = true; } else { if (!$user->hasPanelAccess()) { $error = true; } else { if (!$user->login(get('password'))) { $error = true; } else { go(panel()->urls()->index()); } } } } if ($username = s::get('username')) { $form->fields->username->value = html($username, false); } return layout('login', array('meta' => new Snippet('meta'), 'welcome' => $welcome ? l('login.welcome') : '', 'form' => $form, 'error' => $error ? $message : false)); }
/** * Return the plural of the input string if quantity is larger than one. * * NOTE: This function will not handle any special cases. * * @see http://www.oxforddictionaries.com/words/plurals-of-nouns * * @param string $singular Singular noun * @param integer $quantity Quantity * @param string $plural Plural form * * @return string */ public static function plural($singular, $quantity = 2, $plural = null) { if ($quantity <= 1 || empty($singular)) { return $singular; } if (!is_null($plural)) { return $plural; } $last = str::lower($singular[str::length($singular) - 1]); $lastTwo = str::lower(substr($singular, 0, -2)); if ('y' === $last) { return substr($singular, 0, -1) . 'ies'; } else { if ('f' === $last || 'fe' === $lastTwo) { return $singular . 'ves'; } else { if (in_array($last, array('s', 'x', 'z'))) { return substr($singular, 0, -1) . 'es'; } else { if (in_array($lastTwo, array('ch', 'sh'))) { return substr($singular, 0, -2) . 'es'; } else { return $singular . 's'; } } } } }
public function optionsFromQuery($query) { // default query parameters $defaults = array('page' => $this->field->page ? $this->field->page->id() : '', 'fetch' => 'children', 'value' => '{{uid}}', 'text' => '{{title}}', 'flip' => false, 'template' => false); // sanitize the query if (!is_array($query)) { $query = array(); } // merge the default parameters with the actual query $query = array_merge($defaults, $query); // dynamic page option // ../ // ../../ etc. $page = $this->page($query['page']); $items = $this->items($page, $query['fetch']); $options = array(); if ($query['template']) { $items = $items->filter(function ($item) use($query) { return in_array(str::lower($item->intendedTemplate()), array_map('str::lower', (array) $query['template'])); }); } if ($query['flip']) { $items = $items->flip(); } foreach ($items as $item) { $value = $this->tpl($query['value'], $item); $text = $this->tpl($query['text'], $item); $options[$value] = $text; } return $options; }
function login() { s::restart(); $password = get('password'); $username = get('username'); if (empty($username) || empty($password)) { return array('status' => 'error', 'msg' => l::get('login.error')); } $account = self::load($username); if (!$account) { return array('status' => 'error', 'msg' => l::get('login.error')); } // check for matching usernames if (str::lower($account['username']) != str::lower($username)) { return array('status' => 'error', 'msg' => l::get('login.error')); } // check for a matching password if (!self::checkPassword($account, $password)) { return array('status' => 'error', 'msg' => l::get('login.error')); } // generate a random token $token = str::random(); // add the username. // It's only the key of the array so far. $account['token'] = $token; // store the token in the cookie // and the user data in the session cookie::set('auth', $token, 60 * 60 * 24); s::set($token, $account); // assign the user data to this obj $this->_ = $account; return array('status' => 'success', 'msg' => l::get('login.success')); }
public function fields($fields = null) { if (is_null($fields)) { return $this->fields; } // get the site object $site = panel()->site(); // check if untranslatable fields should be deactivated $translated = $site->multilang() && !$site->language()->default(); foreach ($fields as $name => $field) { $name = str_replace('-', '_', str::lower($name)); $field['name'] = $name; $field['default'] = a::get($field, 'default', null); $field['value'] = a::get($this->values(), $name, $field['default']); // Pass through parent field name (structureField) $field['parentField'] = $this->parentField; // Check for untranslatable fields if ($translated and isset($field['translate']) and $field['translate'] === false) { $field['readonly'] = true; $field['disabled'] = true; } $this->fields->append($name, static::field($field['type'], $field)); } return $this; }
public function __construct($username) { $this->username = str::lower($username); // check if the account file exists if (!file_exists($this->file())) { throw new Exception('The user account could not be found'); } }
public function to() { $source = $this->source(); $name = f::name($source['name']); $extension = f::extension($source['name']); $safeName = f::safeName($name); $safeExtension = str_replace('jpeg', 'jpg', str::lower($extension)); return str::template($this->options['to'], array('name' => $name, 'filename' => $source['name'], 'safeName' => $safeName, 'safeFilename' => $safeName . '.' . $safeExtension, 'extension' => $extension, 'safeExtension' => $safeExtension)); }
public function to() { if (!is_null($this->to)) { return $this->to; } $source = $this->source(); $name = f::name($source['name']); $extension = f::extension($source['name']); $safeName = f::safeName($name); $safeExtension = str_replace('jpeg', 'jpg', str::lower($extension)); if (empty($safeExtension)) { $safeExtension = f::mimeToExtension(f::mime($source['tmp_name'])); } return $this->to = str::template($this->options['to'], array('name' => $name, 'filename' => $source['name'], 'safeName' => $safeName, 'safeFilename' => $safeName . r(!empty($safeExtension), '.' . $safeExtension), 'extension' => $extension, 'safeExtension' => $safeExtension)); }
public function __construct($view, $input) { $this->view = $view; if (is_object($input) and method_exists($input, 'topbar')) { $input->topbar($this); } else { $class = is_object($input) ? str_replace('model', '', strtolower(get_class($input))) : (string) $input; $file = panel()->roots()->topbars() . DS . str::lower($class) . '.php'; if (file_exists($file)) { $callback = (require $file); $callback($this, $input); } else { throw new Exception(l('topbar.error.class.definition') . $class); } } }
public function login() { $user = app::$site->users()->find(str::lower(get('username'))); $message = l('login.error'); if (!$user) { return response::error($message); } try { if (!$user->login(get('password'))) { throw new Exception($message); } return response::success(l('login.success')); } catch (Exception $e) { return response::error($e->getMessage()); } }
public function fields($fields = null) { if (is_null($fields)) { return $this->fields; } foreach ($fields as $name => $field) { if ($name == 'title') { $field['type'] = 'title'; } $field['name'] = $name; $field['default'] = a::get($field, 'default', null); $field['value'] = a::get($this->values(), str::lower($name), $field['default']); $this->fields->append($name, static::field($field['type'], $field)); } return $this; }
public function fields($fields = null) { if (is_null($fields)) { return $this->fields; } foreach ($fields as $name => $field) { $name = str_replace('-', '_', str::lower($name)); $field['name'] = $name; $field['default'] = a::get($field, 'default', null); $field['value'] = a::get($this->values(), $name, $field['default']); // Pass through parent field name (structureField) $field['parentField'] = $this->parentField; $this->fields->append($name, static::field($field['type'], $field)); } return $this; }
public function __construct($fields = array(), $model) { if (empty($fields) or !is_array($fields)) { $fields = array(); } foreach ($fields as $name => $field) { // sanitize the name $name = str_replace('-', '_', str::lower($name)); // import a field by name if (is_string($field)) { $field = array('name' => $name, 'extends' => $field); } // add the name to the field $field['name'] = $name; // create the field object $field = new Field($field, $model); // append it to the collection $this->append($name, $field); } }
/** * Run an attempt to login * * @param string $username * @param string $password */ public function attempt($username, $password) { $this->username = str::lower($username); $this->password = $password; try { if ($this->isInvalidUsername() || $this->isInvalidPassword()) { throw new Exception(l('login.error')); } $user = $this->user(); if (!$user->login($this->password)) { throw new Exception(l('login.error')); } $this->clearLog($this->visitorId()); return true; } catch (Exception $e) { $this->log(); $this->pause(); throw $e; } }
static function fetch($file) { if (!file_exists($file)) { return array('raw' => false, 'data' => array()); } $content = f::read($file); $content = str_replace("", '', $content); $sections = preg_split('![\\r\\n]+[-]{4,}!i', $content); $data = array(); foreach ($sections as $s) { $parts = explode(':', $s); if (count($parts) == 1 && count($sections) == 1) { return $content; } $key = str::lower(preg_replace('![^a-z0-9]+!i', '_', trim($parts[0]))); if (empty($key)) { continue; } $value = trim(implode(':', array_slice($parts, 1))); $data[$key] = $value; } return array('raw' => $content, 'data' => $data); }
/** * Parses the field value as markdown * @param Field $field The calling Kirby Field instance * @return Field */ field::$methods['markdown'] = field::$methods['md'] = function ($field) { $field->value = markdown($field->value); return $field; }; /** * Converts the field value to lower case * @param Field $field The calling Kirby Field instance * @return Field */ field::$methods['lower'] = function ($field) { $field->value = str::lower($field->value); return $field; }; /** * Converts the field value to upper case * @param Field $field The calling Kirby Field instance * @return Field */ field::$methods['upper'] = function ($field) { $field->value = str::upper($field->value); return $field; }; /** * Applies the widont rule to avoid single * words on the last line * @param Field $field The calling Kirby Field instance
public function testUcfirst() { $string = str::lower($this->sample); $this->assertEquals('Super äwesøme string', str::ucfirst($string)); }
/** * Groups the collection by a given field * * @param string $field * @return object A new collection with an item for each group and a subcollection in each group */ public function groupBy($field, $i = true) { $groups = array(); foreach ($this->data as $key => $item) { // get the value to group by $value = $this->extractValue($item, $field); // make sure that there's always a proper value to group by if (!$value) { throw new Exception('Invalid grouping value for key: ' . $key); } // make sure we have a proper key for each group if (is_object($value) or is_array($value)) { throw new Exception('You cannot group by arrays or objects'); } // ignore upper/lowercase for group names if ($i) { $value = str::lower($value); } if (!isset($groups[$value])) { // create a new entry for the group if it does not exist yet $groups[$value] = new static(array($key => $item)); } else { // add the item to an existing group $groups[$value]->set($key, $item); } } return new static($groups); }
/** * Groups the collection by a given field * * @param string $field * @return object A new collection with an item for each group and a subcollection in each group */ public function groupBy($field, $i = true) { if (!is_string($field)) { throw new Exception('Cannot group by non-string values. Did you mean to call group()?'); } return $this->group(function ($item) use($field, $i) { $value = $this->extractValue($item, $field); // ignore upper/lowercase for group names return $i == true ? str::lower($value) : $value; }); }
/** * Creates an XML string from an array * * @param array $array The source array * @param string $tag The name of the root element * @param boolean $head Include the xml declaration head or not * @param string $charset The charset, which should be used for the header * @param int $level The indendation level * @return string The XML string */ static function create($array, $tag = 'root', $head = true, $charset = 'utf-8', $tab = ' ', $level = 0) { $result = ($level == 0 and $head) ? '<?xml version="1.0" encoding="' . $charset . '"?>' . PHP_EOL : ''; $nlevel = $level + 1; $result .= str_repeat($tab, $level) . '<' . $tag . '>' . PHP_EOL; foreach ($array as $key => $value) { $key = str::lower($key); if (is_array($value)) { $mtags = false; foreach ($value as $key2 => $value2) { if (is_array($value2)) { $result .= static::create($value2, $key, $head, $charset, $tab, $nlevel); } elseif (trim($value2) != '') { $value2 = htmlspecialchars($value2) != $value2 ? '<![CDATA[' . $value2 . ']]>' : $value2; $result .= str_repeat($tab, $nlevel) . '<' . $key . '>' . $value2 . '</' . $key . '>' . PHP_EOL; } $mtags = true; } if (!$mtags and count($value) > 0) { $result .= static::create($value, $key, $head, $charset, $tab, $nlevel); } } elseif (trim($value) != '') { $value = htmlspecialchars($value) != $value ? '<![CDATA[' . $value . ']]>' : $value; $result .= str_repeat($tab, $nlevel) . '<' . $key . '>' . $value . '</' . $key . '>' . PHP_EOL; } } return $result . str_repeat($tab, $level) . '</' . $tag . '>' . PHP_EOL; }
public function isLastTest($currentTest) { //return first element of yaml Tests field return str::lower($currentTest) == str::lower(end($this->sessionTests)['Test']); }
</main><!--/@main--> <nav role="navigation" id="nav"> <h1 class="hidden">Explore <?php echo smartypants($site->shorttitle); ?> Handbook</h1> <ul> <?php foreach ($pages->visible() as $page) { ?> <li><a href="<?php echo $page->url(); ?> " id="nav-<?php echo str::lower($page->title); ?> "<?php echo $page->isOpen() ? ' class="is-active"' : ''; ?> ><?php echo html($page->title()); ?> </a></li> <?php } ?> </ul> <a href="#top" class="return">▴ Return to top</a> </nav><!--/@navigation-->
static function date($params) { $format = @$params['date']; return str::lower($format) == 'year' ? date('Y') : date($format); }
$totalTestingTime += (int) $testSpecs['minutes']; //if the currently looping test equals the test stored into user's status field if (str::lower($t->uid()) == str::lower($user->status())) { //update test's button CSS classes and label. $testButtonCSSClasses = c::get('test.button.taking'); $testButtonLabel = l('test.taking'); } //add test's button CSS classes, label & test page's url to $testSpecs, //then store them into $tests array $tests[] = array_merge($testSpecs, array('btnClasses' => $testButtonCSSClasses, 'btnLabel' => $testButtonLabel, 'url' => $t->url())); //if the currently looping test equals the test stored into user's status field if (str::lower($t->uid()) == str::lower($user->status())) { //update test button CSS classes and label for upcoming looping tests $testButtonCSSClasses = c::get('test.button.to.take'); $testButtonLabel = l('test.to.take'); } //if test button CSS classes has a 'completed' status, add its time to $completedTestingTime if (!strrpos($testButtonCSSClasses, 'completed') === false) { $completedTestingTime += (int) $testSpecs['minutes']; } } } } //if user's session status equals c::get('session.status.completed') if (str::lower($user->status()) == c::get('session.status.completed')) { $sessionComplete = true; } } //return vars to view return array('tests' => $tests, 'sessionName' => $sessionName, 'totalTestingTime' => $totalTestingTime, 'totalRemainingTime' => $totalTestingTime - $completedTestingTime, 'numberOfTests' => $numberOfTests, 'sessionComplete' => $sessionComplete); };
/** * A set of sanitizer methods * * @param string $string The string to sanitize * @param string $type The method * @param string $default The default value if the string will be empty afterwards * @return string The sanitized string */ static function sanitize($string, $type = 'str', $default = null) { $string = stripslashes((string) $string); $string = urldecode($string); $string = str::utf8($string); switch ($type) { case 'int': $string = (int) $string; break; case 'str': $string = (string) $string; break; case 'array': $string = (array) $string; break; case 'nohtml': $string = self::unhtml($string); break; case 'noxml': $string = self::unxml($string); break; case 'enum': $string = in_array($string, array('y', 'n')) ? $string : $default; $string = in_array($string, array('y', 'n')) ? $string : 'n'; break; case 'checkbox': $string = $string == 'on' ? 'y' : 'n'; break; case 'url': $string = v::url($string) ? $string : ''; break; case 'email': $string = v::email($string) ? $string : ''; break; case 'plain': $string = str::unxml($string); $string = str::unhtml($string); $string = str::trim($string); break; case 'lower': $string = str::lower($string); break; case 'upper': $string = str::upper($string); break; case 'words': $string = str::sanitize($string, 'plain'); $string = preg_replace('/[^\\pL]/u', ' ', $string); case 'tags': $string = str::sanitize($string, 'plain'); $string = preg_replace('/[^\\pL\\pN]/u', ' ', $string); $string = str::trim($string); case 'nobreaks': $string = str_replace('\\n', '', $string); $string = str_replace('\\r', '', $string); $string = str_replace('\\t', '', $string); break; case 'url': $string = self::urlify($string); break; case 'filename': $string = f::safe_name($string); break; } return trim($string); }
public function __construct($field) { $this->field = $field; if (is_array($field->options)) { $this->options = $field->options; } else { if (v::url($field->options)) { $response = remote::get($field->options); $options = @json_decode($response->content(), true); if (is_array($options)) { $this->options = $options; } else { $this->options = array(); } } else { if (!$field->page) { $this->options = array(); } else { if ($field->options == 'query') { $defaults = array('page' => $field->page->id(), 'fetch' => 'children', 'value' => '{{uid}}', 'text' => '{{title}}', 'flip' => false, 'template' => false); $query = array_merge($defaults, $field->query); // dynamic page option // ../ // ../../ etc. if (str::startsWith($query['page'], '../')) { $currentPage = $field->page; $path = $query['page']; while (str::startsWith($path, '../')) { if ($parent = $currentPage->parent()) { $currentPage = $parent; } else { break; } $path = str::substr($path, 3); } $page = $currentPage; } else { $page = page($query['page']); } $items = $this->items($page, $query['fetch']); if ($query['template']) { $items = $items->filter(function ($item) use($query) { return in_array(str::lower($item->intendedTemplate()), array_map('str::lower', (array) $query['template'])); }); } if ($query['flip']) { $items = $items->flip(); } foreach ($items as $item) { $value = $this->tpl($query['value'], $item); $text = $this->tpl($query['text'], $item); $this->options[$value] = $text; } } else { if ($items = $this->items($field->page, $field->options)) { foreach ($items as $item) { if (is_a($item, 'Page')) { $this->options[$item->uid()] = (string) $item->title(); } else { if (is_a($item, 'File')) { $this->options[$item->filename()] = (string) $item->filename(); } } } } else { $this->options = array(); } } } } } // sorting if (!empty($this->field->sort)) { switch (strtolower($this->field->sort)) { case 'asc': asort($this->options); break; case 'desc': arsort($this->options); break; } } }
?> "> <img src="http://maps.google.com/maps/api/staticmap?center=<?php echo urlencode($address); ?> &zoom=<?php echo $zoom; ?> &size=<?php echo $width; ?> x<?php echo $height; ?> &maptype=<?php echo str::lower($type); ?> &markers=color:red|color:red|<?php echo urlencode($address); ?> &sensor=false" width="<?php echo $width; ?> " height="<?php echo $height; ?> " class="<?php echo $class; ?> " alt="<?php echo html($address);
/** * Enables magic queries like findByUsername or findByEmail * * @param string $method * @param array $arguments * @return mixed */ public function __call($method, $arguments) { if (preg_match('!^findBy([a-z]+)!i', $method, $match)) { $column = str::lower($match[1]); return $this->findBy($column, $arguments[0]); } else { throw new Error('Invalid query method: ' . $method, static::ERROR_INVALID_QUERY_METHOD); } }
static function date($params) { $format = @$params['date']; if (str::lower($format) == 'year') { return date('Y'); } else { return date($format); } }
protected static function load($username) { $username = str::lower($username); $dir = c::get('root.site') . '/accounts'; $file = $dir . '/' . $username . '.php'; if (!is_dir($dir) || !file_exists($file)) { return false; } $content = file_get_contents($file); $yaml = yaml($content); // remove the php direct access protection line unset($yaml[0]); return new AuthUser($yaml); }