Contao\System::setCookie PHP Method

setCookie() public static method

Set a cookie
public static setCookie ( string $strName, mixed $varValue, integer $intExpires, string $strPath = null, string $strDomain = null, boolean $blnSecure = false, boolean $blnHttpOnly = false )
$strName string The cookie name
$varValue mixed The cookie value
$intExpires integer The expiration date
$strPath string An optional path
$strDomain string An optional domain name
$blnSecure boolean If true, the secure flag will be set
$blnHttpOnly boolean If true, the http-only flag will be set
    public static function setCookie($strName, $varValue, $intExpires, $strPath = null, $strDomain = null, $blnSecure = false, $blnHttpOnly = false)
    {
        if ($strPath == '') {
            $strPath = \Environment::get('path') ?: '/';
            // see #4390
        }
        $objCookie = new \stdClass();
        $objCookie->strName = $strName;
        $objCookie->varValue = $varValue;
        $objCookie->intExpires = $intExpires;
        $objCookie->strPath = $strPath;
        $objCookie->strDomain = $strDomain;
        $objCookie->blnSecure = $blnSecure;
        $objCookie->blnHttpOnly = $blnHttpOnly;
        // HOOK: allow to add custom logic
        if (isset($GLOBALS['TL_HOOKS']['setCookie']) && is_array($GLOBALS['TL_HOOKS']['setCookie'])) {
            foreach ($GLOBALS['TL_HOOKS']['setCookie'] as $callback) {
                $objCookie = static::importStatic($callback[0])->{$callback[1]}($objCookie);
            }
        }
        setcookie($objCookie->strName, $objCookie->varValue, $objCookie->intExpires, $objCookie->strPath, $objCookie->strDomain, $objCookie->blnSecure, $objCookie->blnHttpOnly);
    }

Usage Example

Example #1
0
    /**
     * Auto-generate a form to override all records that are currently shown
     *
     * @return string
     *
     * @throws InternalServerErrorException
     */
    public function overrideAll()
    {
        if ($GLOBALS['TL_DCA'][$this->strTable]['config']['notEditable']) {
            throw new InternalServerErrorException('Table "' . $this->strTable . '" is not editable.');
        }
        $return = '';
        $this->import('BackendUser', 'User');
        /** @var SessionInterface $objSession */
        $objSession = \System::getContainer()->get('session');
        // Get current IDs from session
        $session = $objSession->all();
        $ids = $session['CURRENT']['IDS'];
        // Save field selection in session
        if (\Input::post('FORM_SUBMIT') == $this->strTable . '_all' && \Input::get('fields')) {
            $session['CURRENT'][$this->strTable] = \Input::post('all_fields');
            $objSession->replace($session);
        }
        // Add fields
        $fields = $session['CURRENT'][$this->strTable];
        if (!empty($fields) && is_array($fields) && \Input::get('fields')) {
            $class = 'tl_tbox';
            $formFields = array();
            // Save record
            if (\Input::post('FORM_SUBMIT') == $this->strTable) {
                foreach ($ids as $id) {
                    $this->intId = $id;
                    $this->procedure = array('id=?');
                    $this->values = array($this->intId);
                    $this->blnCreateNewVersion = false;
                    // Get the field values
                    $objRow = $this->Database->prepare("SELECT * FROM " . $this->strTable . " WHERE id=?")->limit(1)->execute($this->intId);
                    // Store the active record
                    $this->objActiveRecord = $objRow;
                    $objVersions = new \Versions($this->strTable, $this->intId);
                    $objVersions->initialize();
                    // Store all fields
                    foreach ($fields as $v) {
                        // Check whether field is excluded
                        if ($GLOBALS['TL_DCA'][$this->strTable]['fields'][$v]['exclude']) {
                            continue;
                        }
                        $this->strField = $v;
                        $this->strInputName = $v;
                        $this->varValue = '';
                        // Make sure the new value is applied
                        $GLOBALS['TL_DCA'][$this->strTable]['fields'][$v]['eval']['alwaysSave'] = true;
                        // Store value
                        $this->row();
                    }
                    // Post processing
                    if (!$this->noReload) {
                        // Call the onsubmit_callback
                        if (is_array($GLOBALS['TL_DCA'][$this->strTable]['config']['onsubmit_callback'])) {
                            foreach ($GLOBALS['TL_DCA'][$this->strTable]['config']['onsubmit_callback'] as $callback) {
                                if (is_array($callback)) {
                                    $this->import($callback[0]);
                                    $this->{$callback[0]}->{$callback[1]}($this);
                                } elseif (is_callable($callback)) {
                                    $callback($this);
                                }
                            }
                        }
                        // Create a new version
                        if ($this->blnCreateNewVersion) {
                            $objVersions->create();
                            // Call the onversion_callback
                            if (is_array($GLOBALS['TL_DCA'][$this->strTable]['config']['onversion_callback'])) {
                                foreach ($GLOBALS['TL_DCA'][$this->strTable]['config']['onversion_callback'] as $callback) {
                                    if (is_array($callback)) {
                                        $this->import($callback[0]);
                                        $this->{$callback[0]}->{$callback[1]}($this->strTable, $this->intId, $this);
                                    } elseif (is_callable($callback)) {
                                        $callback($this->strTable, $this->intId, $this);
                                    }
                                }
                            }
                            $this->log('A new version of record "' . $this->strTable . '.id=' . $this->intId . '" has been created' . $this->getParentEntries($this->strTable, $this->intId), __METHOD__, TL_GENERAL);
                        }
                        // Set the current timestamp (-> DO NOT CHANGE ORDER version - timestamp)
                        if ($GLOBALS['TL_DCA'][$this->strTable]['config']['dynamicPtable']) {
                            $this->Database->prepare("UPDATE " . $this->strTable . " SET ptable=?, tstamp=? WHERE id=?")->execute($this->ptable, time(), $this->intId);
                        } else {
                            $this->Database->prepare("UPDATE " . $this->strTable . " SET tstamp=? WHERE id=?")->execute(time(), $this->intId);
                        }
                    }
                }
            }
            $blnIsFirst = true;
            // Begin current row
            $return .= '
<div class="' . $class . '">';
            foreach ($fields as $v) {
                // Check whether field is excluded
                if ($GLOBALS['TL_DCA'][$this->strTable]['fields'][$v]['exclude']) {
                    continue;
                }
                $formFields[] = $v;
                $this->intId = 0;
                $this->procedure = array('id=?');
                $this->values = array($this->intId);
                $this->strField = $v;
                $this->strInputName = $v;
                $this->varValue = '';
                // Autofocus the first field
                if ($blnIsFirst && $GLOBALS['TL_DCA'][$this->strTable]['fields'][$this->strField]['inputType'] == 'text') {
                    $GLOBALS['TL_DCA'][$this->strTable]['fields'][$this->strField]['eval']['autofocus'] = 'autofocus';
                    $blnIsFirst = false;
                }
                // Disable auto-submit
                $GLOBALS['TL_DCA'][$this->strTable]['fields'][$this->strField]['eval']['submitOnChange'] = false;
                $return .= $this->row();
            }
            // Close box
            $return .= '
<input type="hidden" name="FORM_FIELDS[]" value="' . specialchars(implode(',', $formFields)) . '">
</div>';
            // Submit buttons
            $arrButtons = array();
            $arrButtons['save'] = '<button type="submit" name="save" id="save" class="tl_submit" accesskey="s">' . $GLOBALS['TL_LANG']['MSC']['save'] . '</button>';
            $arrButtons['saveNclose'] = '<button type="submit" name="saveNclose" id="saveNclose" class="tl_submit" accesskey="c">' . $GLOBALS['TL_LANG']['MSC']['saveNclose'] . '</button>';
            // Call the buttons_callback (see #4691)
            if (is_array($GLOBALS['TL_DCA'][$this->strTable]['edit']['buttons_callback'])) {
                foreach ($GLOBALS['TL_DCA'][$this->strTable]['edit']['buttons_callback'] as $callback) {
                    if (is_array($callback)) {
                        $this->import($callback[0]);
                        $arrButtons = $this->{$callback[0]}->{$callback[1]}($arrButtons, $this);
                    } elseif (is_callable($callback)) {
                        $arrButtons = $callback($arrButtons, $this);
                    }
                }
            }
            // Add the form
            $return = '

<form action="' . ampersand(\Environment::get('request'), true) . '" id="' . $this->strTable . '" class="tl_form" method="post" enctype="' . ($this->blnUploadable ? 'multipart/form-data' : 'application/x-www-form-urlencoded') . '">
<div class="tl_formbody_edit">
<input type="hidden" name="FORM_SUBMIT" value="' . $this->strTable . '">
<input type="hidden" name="REQUEST_TOKEN" value="' . REQUEST_TOKEN . '">' . ($this->noReload ? '

<p class="tl_error">' . $GLOBALS['TL_LANG']['ERR']['general'] . '</p>' : '') . $return . '

</div>

<div class="tl_formbody_submit">

<div class="tl_submit_container">
  ' . implode(' ', $arrButtons) . '
</div>

</div>
</form>';
            // Set the focus if there is an error
            if ($this->noReload) {
                $return .= '

<script>
  window.addEvent(\'domready\', function() {
    Backend.vScrollTo(($(\'' . $this->strTable . '\').getElement(\'label.error\').getPosition().y - 20));
  });
</script>';
            }
            // Reload the page to prevent _POST variables from being sent twice
            if (\Input::post('FORM_SUBMIT') == $this->strTable && !$this->noReload) {
                if (isset($_POST['saveNclose'])) {
                    \System::setCookie('BE_PAGE_OFFSET', 0, 0);
                    $this->redirect($this->getReferer());
                }
                $this->reload();
            }
        } else {
            $options = '';
            $fields = array();
            // Add fields of the current table
            $fields = array_merge($fields, array_keys($GLOBALS['TL_DCA'][$this->strTable]['fields']));
            // Add meta fields if the current user is an administrator
            if ($this->User->isAdmin) {
                if ($this->Database->fieldExists('sorting', $this->strTable) && !in_array('sorting', $fields)) {
                    array_unshift($fields, 'sorting');
                }
                if ($this->Database->fieldExists('pid', $this->strTable) && !in_array('pid', $fields)) {
                    array_unshift($fields, 'pid');
                }
            }
            // Show all non-excluded fields
            foreach ($fields as $field) {
                if ($field == 'pid' || $field == 'sorting' || !$GLOBALS['TL_DCA'][$this->strTable]['fields'][$field]['exclude'] && !$GLOBALS['TL_DCA'][$this->strTable]['fields'][$field]['eval']['doNotShow'] && (strlen($GLOBALS['TL_DCA'][$this->strTable]['fields'][$field]['inputType']) || is_array($GLOBALS['TL_DCA'][$this->strTable]['fields'][$field]['input_field_callback']))) {
                    $options .= '
  <input type="checkbox" name="all_fields[]" id="all_' . $field . '" class="tl_checkbox" value="' . specialchars($field) . '"> <label for="all_' . $field . '" class="tl_checkbox_label">' . (($GLOBALS['TL_DCA'][$this->strTable]['fields'][$field]['label'][0] ?: $GLOBALS['TL_LANG']['MSC'][$field][0]) . ' <span style="color:#b3b3b3;padding-left:3px">[' . $field . ']</span>') . '</label><br>';
                }
            }
            $blnIsError = $_POST && empty($_POST['all_fields']);
            // Return the select menu
            $return .= '

<form action="' . ampersand(\Environment::get('request'), true) . '&amp;fields=1" id="' . $this->strTable . '_all" class="tl_form" method="post">
<div class="tl_formbody_edit">
<input type="hidden" name="FORM_SUBMIT" value="' . $this->strTable . '_all">
<input type="hidden" name="REQUEST_TOKEN" value="' . REQUEST_TOKEN . '">' . ($blnIsError ? '

<p class="tl_error">' . $GLOBALS['TL_LANG']['ERR']['general'] . '</p>' : '') . '

<div class="tl_tbox">
<fieldset class="tl_checkbox_container">
  <legend' . ($blnIsError ? ' class="error"' : '') . '>' . $GLOBALS['TL_LANG']['MSC']['all_fields'][0] . '</legend>
  <input type="checkbox" id="check_all" class="tl_checkbox" onclick="Backend.toggleCheckboxes(this)"> <label for="check_all" style="color:#a6a6a6"><em>' . $GLOBALS['TL_LANG']['MSC']['selectAll'] . '</em></label><br>' . $options . '
</fieldset>' . ($blnIsError ? '
<p class="tl_error">' . $GLOBALS['TL_LANG']['ERR']['all_fields'] . '</p>' : (\Config::get('showHelp') && strlen($GLOBALS['TL_LANG']['MSC']['all_fields'][1]) ? '
<p class="tl_help tl_tip">' . $GLOBALS['TL_LANG']['MSC']['all_fields'][1] . '</p>' : '')) . '
</div>

</div>

<div class="tl_formbody_submit">

<div class="tl_submit_container">
  <button type="submit" name="save" id="save" class="tl_submit" accesskey="s">' . $GLOBALS['TL_LANG']['MSC']['continue'] . '</button>
</div>

</div>
</form>';
        }
        // Return
        return '
<div id="tl_buttons">
<a href="' . $this->getReferer(true) . '" class="header_back" title="' . specialchars($GLOBALS['TL_LANG']['MSC']['backBTTitle']) . '" accesskey="b" onclick="Backend.getScrollOffset()">' . $GLOBALS['TL_LANG']['MSC']['backBT'] . '</a>
</div>' . $return;
    }
All Usage Examples Of Contao\System::setCookie