substr() public static method

The function preserves words, so the result might be a bit shorter or longer than the number of characters given. It strips all tags.
public static substr ( string $strString, integer $intNumberOfChars, string $strEllipsis = ' …' ) : string
$strString string The string to shorten
$intNumberOfChars integer The target number of characters
$strEllipsis string An optional ellipsis to append to the shortened string
return string The shortened string
 /**
  * Shorten a string to a given number of characters.
  *
  * The function preserves words, so the result might be a bit shorter or
  * longer than the number of characters given. It strips all tags.
  *
  * @param string  $strString        The string to shorten.
  * @param integer $intNumberOfChars The target number of characters.
  * @param string  $strEllipsis      An optional ellipsis to append to the shortened string.
  *
  * @return string The shortened string
  */
 public static function substr($strString, $intNumberOfChars, $strEllipsis = ' …')
 {
     if (self::isStringUtilAvailable()) {
         return StringUtil::substr($strString, $intNumberOfChars, $strEllipsis);
     }
     return \Contao\String::substr($strString, $intNumberOfChars, $strEllipsis);
 }
Exemplo n.º 2
0
 /**
  * Add a list of versions to a template
  *
  * @param BackendTemplate|object $objTemplate
  */
 public static function addToTemplate(BackendTemplate $objTemplate)
 {
     $arrVersions = array();
     $objUser = \BackendUser::getInstance();
     $objDatabase = \Database::getInstance();
     // Get the total number of versions
     $objTotal = $objDatabase->prepare("SELECT COUNT(*) AS count FROM tl_version WHERE version>1" . (!$objUser->isAdmin ? " AND userid=?" : ""))->execute($objUser->id);
     $intLast = ceil($objTotal->count / 30);
     $intPage = \Input::get('vp') !== null ? \Input::get('vp') : 1;
     $intOffset = ($intPage - 1) * 30;
     // Validate the page number
     if ($intPage < 1 || $intLast > 0 && $intPage > $intLast) {
         header('HTTP/1.1 404 Not Found');
     }
     // Create the pagination menu
     $objPagination = new \Pagination($objTotal->count, 30, 7, 'vp', new \BackendTemplate('be_pagination'));
     $objTemplate->pagination = $objPagination->generate();
     // Get the versions
     $objVersions = $objDatabase->prepare("SELECT pid, tstamp, version, fromTable, username, userid, description, editUrl, active FROM tl_version" . (!$objUser->isAdmin ? " WHERE userid=?" : "") . " ORDER BY tstamp DESC, pid, version DESC")->limit(30, $intOffset)->execute($objUser->id);
     while ($objVersions->next()) {
         $arrRow = $objVersions->row();
         // Add some parameters
         $arrRow['from'] = max($objVersions->version - 1, 1);
         // see #4828
         $arrRow['to'] = $objVersions->version;
         $arrRow['date'] = date(\Config::get('datimFormat'), $objVersions->tstamp);
         $arrRow['description'] = \StringUtil::substr($arrRow['description'], 32);
         $arrRow['shortTable'] = \StringUtil::substr($arrRow['fromTable'], 18);
         // see #5769
         if ($arrRow['editUrl'] != '') {
             $arrRow['editUrl'] = preg_replace('/&(amp;)?rt=[^&]+/', '&amp;rt=' . REQUEST_TOKEN, ampersand($arrRow['editUrl']));
         }
         $arrVersions[] = $arrRow;
     }
     $intCount = -1;
     $arrVersions = array_values($arrVersions);
     // Add the "even" and "odd" classes
     foreach ($arrVersions as $k => $v) {
         $arrVersions[$k]['class'] = ++$intCount % 2 == 0 ? 'even' : 'odd';
         try {
             // Mark deleted versions (see #4336)
             $objDeleted = $objDatabase->prepare("SELECT COUNT(*) AS count FROM " . $v['fromTable'] . " WHERE id=?")->execute($v['pid']);
             $arrVersions[$k]['deleted'] = $objDeleted->count < 1;
         } catch (\Exception $e) {
             // Probably a disabled module
             --$intCount;
             unset($arrVersions[$k]);
         }
     }
     $objTemplate->versions = $arrVersions;
 }
Exemplo n.º 3
0
 /**
  * Prepare a text to be used in the meta description tag
  *
  * @param string $strText
  *
  * @return string
  */
 protected function prepareMetaDescription($strText)
 {
     $strText = $this->replaceInsertTags($strText, false);
     $strText = strip_tags($strText);
     $strText = str_replace("\n", ' ', $strText);
     $strText = \StringUtil::substr($strText, 180);
     return trim($strText);
 }
Exemplo n.º 4
0
 /**
  * Generate the module
  *
  * @return string
  */
 public function run()
 {
     if (!\Config::get('enableSearch')) {
         return '';
     }
     $time = time();
     /** @var BackendTemplate|object $objTemplate */
     $objTemplate = new \BackendTemplate('be_rebuild_index');
     $objTemplate->action = ampersand(\Environment::get('request'));
     $objTemplate->indexHeadline = $GLOBALS['TL_LANG']['tl_maintenance']['searchIndex'];
     $objTemplate->isActive = $this->isActive();
     // Add the error message
     if ($_SESSION['REBUILD_INDEX_ERROR'] != '') {
         $objTemplate->indexMessage = $_SESSION['REBUILD_INDEX_ERROR'];
         $_SESSION['REBUILD_INDEX_ERROR'] = '';
     }
     // Rebuild the index
     if (\Input::get('act') == 'index') {
         // Check the request token (see #4007)
         if (!isset($_GET['rt']) || !\RequestToken::validate(\Input::get('rt'))) {
             /** @var SessionInterface $objSession */
             $objSession = \System::getContainer()->get('session');
             $objSession->set('INVALID_TOKEN_URL', \Environment::get('request'));
             $this->redirect('contao/confirm.php');
         }
         $arrPages = $this->findSearchablePages();
         // HOOK: take additional pages
         if (isset($GLOBALS['TL_HOOKS']['getSearchablePages']) && is_array($GLOBALS['TL_HOOKS']['getSearchablePages'])) {
             foreach ($GLOBALS['TL_HOOKS']['getSearchablePages'] as $callback) {
                 $this->import($callback[0]);
                 $arrPages = $this->{$callback[0]}->{$callback[1]}($arrPages);
             }
         }
         // Return if there are no pages
         if (empty($arrPages)) {
             $_SESSION['REBUILD_INDEX_ERROR'] = $GLOBALS['TL_LANG']['tl_maintenance']['noSearchable'];
             $this->redirect($this->getReferer());
         }
         // Truncate the search tables
         $this->import('Automator');
         $this->Automator->purgeSearchTables();
         // Hide unpublished elements
         $this->setCookie('FE_PREVIEW', 0, $time - 86400);
         // Calculate the hash
         $strHash = $this->getSessionHash('FE_USER_AUTH');
         // Remove old sessions
         $this->Database->prepare("DELETE FROM tl_session WHERE tstamp<? OR hash=?")->execute($time - \Config::get('sessionTimeout'), $strHash);
         // Log in the front end user
         if (is_numeric(\Input::get('user')) && \Input::get('user') > 0) {
             // Insert a new session
             $this->Database->prepare("INSERT INTO tl_session (pid, tstamp, name, sessionID, ip, hash) VALUES (?, ?, ?, ?, ?, ?)")->execute(\Input::get('user'), $time, 'FE_USER_AUTH', \System::getContainer()->get('session')->getId(), \Environment::get('ip'), $strHash);
             // Set the cookie
             $this->setCookie('FE_USER_AUTH', $strHash, $time + \Config::get('sessionTimeout'), null, null, false, true);
         } else {
             // Unset the cookies
             $this->setCookie('FE_USER_AUTH', $strHash, $time - 86400, null, null, false, true);
             $this->setCookie('FE_AUTO_LOGIN', \Input::cookie('FE_AUTO_LOGIN'), $time - 86400, null, null, false, true);
         }
         $strBuffer = '';
         $rand = rand();
         // Display the pages
         for ($i = 0, $c = count($arrPages); $i < $c; $i++) {
             $strBuffer .= '<span class="page_url" data-url="' . $arrPages[$i] . '#' . $rand . $i . '">' . \StringUtil::substr($arrPages[$i], 100) . '</span><br>';
             unset($arrPages[$i]);
             // see #5681
         }
         $objTemplate->content = $strBuffer;
         $objTemplate->note = $GLOBALS['TL_LANG']['tl_maintenance']['indexNote'];
         $objTemplate->loading = $GLOBALS['TL_LANG']['tl_maintenance']['indexLoading'];
         $objTemplate->complete = $GLOBALS['TL_LANG']['tl_maintenance']['indexComplete'];
         $objTemplate->indexContinue = $GLOBALS['TL_LANG']['MSC']['continue'];
         $objTemplate->theme = \Backend::getTheme();
         $objTemplate->isRunning = true;
         return $objTemplate->parse();
     }
     $arrUser = array('' => '-');
     // Get active front end users
     $objUser = $this->Database->execute("SELECT id, username FROM tl_member WHERE disable!='1' AND (start='' OR start<='{$time}') AND (stop='' OR stop>'" . ($time + 60) . "') ORDER BY username");
     while ($objUser->next()) {
         $arrUser[$objUser->id] = $objUser->username . ' (' . $objUser->id . ')';
     }
     // Default variables
     $objTemplate->user = $arrUser;
     $objTemplate->indexLabel = $GLOBALS['TL_LANG']['tl_maintenance']['frontendUser'][0];
     $objTemplate->indexHelp = \Config::get('showHelp') && strlen($GLOBALS['TL_LANG']['tl_maintenance']['frontendUser'][1]) ? $GLOBALS['TL_LANG']['tl_maintenance']['frontendUser'][1] : '';
     $objTemplate->indexSubmit = $GLOBALS['TL_LANG']['tl_maintenance']['indexSubmit'];
     return $objTemplate->parse();
 }