set() public static method

Stores a value in a cookie, by default the cookie will expire in one day.
public static set ( string $key, mixed $value, integer $time = 2592000, string $path = '/', string $domain = null, boolean $secure = null, boolean $httpOnly = true ) : boolean
$key string A name for the cookie.
$value mixed The value to be stored. Keep in mind that they will be serialized.
$time integer The number of seconds that this cookie will be available, 30 days is the default.
$path string The path on the server in which the cookie will be available. Use / for the entire domain, /foo if you just want it to be available in /foo.
$domain string The domain that the cookie is available on. Use .example.com to make it available on all subdomains of example.com.
$secure boolean Should the cookie be transmitted over a HTTPS-connection? If true, make sure you use a secure connection, otherwise the cookie won't be set.
$httpOnly boolean Should the cookie only be available through HTTP-protocol? If true, the cookie can't be accessed by Javascript, ...
return boolean If set with success, returns true otherwise false.
示例#1
0
 /**
  * Execute the order save
  */
 public function execute()
 {
     parent::execute();
     // get order values
     $this->orderValues['product_id'] = \SpoonFilter::getPostValue('productId', null, '');
     $this->orderValues['amount'] = \SpoonFilter::getPostValue('productAmount', null, '');
     $action = \SpoonFilter::getPostValue('action', null, '');
     // get cookie
     $cookieOrderId = Cookie::get('order_id');
     // check if cookies are enabled
     $cookiesEnabled = Cookie::set('enabled', 'true');
     $cookieExists = Cookie::exists('enabled');
     // check if cookies are set, when true update the order
     if (isset($cookieOrderId) && FrontendCatalogModel::existsOrder($cookieOrderId) == true) {
         $this->orderValues['order_id'] = $cookieOrderId;
         // action add or update
         if ($action == 'add-update') {
             if (FrontendCatalogModel::existsOrderValue($this->orderValues['product_id'], $this->orderValues['order_id']) == true) {
                 // update the order values
                 FrontendCatalogModel::updateOrderValue($this->orderValues, $this->orderValues['order_id'], $this->orderValues['product_id']);
                 $this->output(self::OK, null, 'Order values updated.');
             } else {
                 // insert order values
                 FrontendCatalogModel::insertOrderValue($this->orderValues);
                 $this->output(self::OK, null, 'Order values inserted.');
             }
         } elseif ($action == 'delete') {
             if (FrontendCatalogModel::existsOrderValue($this->orderValues['product_id'], $this->orderValues['order_id']) == true) {
                 // delete the order values
                 FrontendCatalogModel::deleteOrderValue($this->orderValues['order_id'], $this->orderValues['product_id']);
                 $this->output(self::OK, null, 'Order values deleted.');
             }
         }
     } else {
         // when no cookies are set, create new cookie and insert order
         $orderId = FrontendCatalogModel::insertOrder();
         if ($orderId != '') {
             // set order id
             $this->orderValues['order_id'] = $orderId;
             // set cookie
             Cookie::set('order_id', $orderId);
             // insert order values
             FrontendCatalogModel::insertOrderValue($this->orderValues);
             $this->output(self::OK, null, 'Order imported.');
         }
     }
 }
示例#2
0
文件: Url.php 项目: forkcms/forkcms
 /**
  * Process the query string
  */
 private function processQueryString()
 {
     // store the query string local, so we don't alter it.
     $queryString = trim($this->request->getPathInfo(), '/');
     // split into chunks
     $chunks = (array) explode('/', $queryString);
     $hasMultiLanguages = $this->getContainer()->getParameter('site.multilanguage');
     // single language
     if (!$hasMultiLanguages) {
         // set language id
         $language = $this->get('fork.settings')->get('Core', 'default_language', SITE_DEFAULT_LANGUAGE);
     } else {
         // multiple languages
         // default value
         $mustRedirect = false;
         // get possible languages
         $possibleLanguages = (array) Language::getActiveLanguages();
         $redirectLanguages = (array) Language::getRedirectLanguages();
         // the language is present in the URL
         if (isset($chunks[0]) && in_array($chunks[0], $possibleLanguages)) {
             // define language
             $language = (string) $chunks[0];
             // try to set a cookie with the language
             try {
                 // set cookie
                 CommonCookie::set('frontend_language', $language);
             } catch (\SpoonCookieException $e) {
                 // settings cookies isn't allowed, because this isn't a real problem we ignore the exception
             }
             // set sessions
             \SpoonSession::set('frontend_language', $language);
             // remove the language part
             array_shift($chunks);
         } elseif (CommonCookie::exists('frontend_language') && in_array(CommonCookie::get('frontend_language'), $redirectLanguages)) {
             // set languageId
             $language = (string) CommonCookie::get('frontend_language');
             // redirect is needed
             $mustRedirect = true;
         } else {
             // default browser language
             // set languageId & abbreviation
             $language = Language::getBrowserLanguage();
             // try to set a cookie with the language
             try {
                 // set cookie
                 CommonCookie::set('frontend_language', $language);
             } catch (\SpoonCookieException $e) {
                 // settings cookies isn't allowed, because this isn't a real problem we ignore the exception
             }
             // redirect is needed
             $mustRedirect = true;
         }
         // redirect is required
         if ($mustRedirect) {
             // build URL
             // trim the first / from the query string to prevent double slashes
             $url = rtrim('/' . $language . '/' . trim($this->getQueryString(), '/'), '/');
             // when we are just adding the language to the domain, it's a temporary redirect because
             // Safari keeps the 301 in cache, so the cookie to switch language doesn't work any more
             $redirectCode = $url == '/' . $language ? 302 : 301;
             // set header & redirect
             throw new RedirectException('Redirect', new RedirectResponse($url, $redirectCode));
         }
     }
     // define the language
     defined('FRONTEND_LANGUAGE') || define('FRONTEND_LANGUAGE', $language);
     defined('LANGUAGE') || define('LANGUAGE', $language);
     // sets the locale file
     Language::setLocale($language);
     // list of pageIds & their full URL
     $keys = Navigation::getKeys();
     // rebuild our URL, but without the language parameter. (it's tripped earlier)
     $url = implode('/', $chunks);
     $startURL = $url;
     // loop until we find the URL in the list of pages
     while (!in_array($url, $keys)) {
         // remove the last chunk
         array_pop($chunks);
         // redefine the URL
         $url = implode('/', $chunks);
     }
     // remove language from query string
     if ($hasMultiLanguages) {
         $queryString = trim(mb_substr($queryString, mb_strlen($language)), '/');
     }
     // if it's the homepage AND parameters were given (not allowed!)
     if ($url == '' && $queryString != '') {
         // get 404 URL
         $url = Navigation::getURL(404);
         // remove language
         if ($hasMultiLanguages) {
             $url = str_replace('/' . $language, '', $url);
         }
     }
     // set pages
     $url = trim($url, '/');
     // currently not in the homepage
     if ($url != '') {
         // explode in pages
         $pages = explode('/', $url);
         // reset pages
         $this->setPages($pages);
         // reset parameters
         $this->setParameters(array());
     }
     // set parameters
     $parameters = trim(mb_substr($startURL, mb_strlen($url)), '/');
     // has at least one parameter
     if ($parameters != '') {
         // parameters will be separated by /
         $parameters = explode('/', $parameters);
         // set parameters
         $this->setParameters($parameters);
     }
     // pageId, parentId & depth
     $pageId = Navigation::getPageId(implode('/', $this->getPages()));
     $pageInfo = Navigation::getPageInfo($pageId);
     // invalid page, or parameters but no extra
     if ($pageInfo === false || !empty($parameters) && !$pageInfo['has_extra']) {
         // get 404 URL
         $url = Navigation::getURL(404);
         // remove language
         if ($hasMultiLanguages) {
             $url = str_replace('/' . $language, '', $url);
         }
         // remove the first slash
         $url = trim($url, '/');
         // currently not in the homepage
         if ($url != '') {
             // explode in pages
             $pages = explode('/', $url);
             // reset pages
             $this->setPages($pages);
             // reset parameters
             $this->setParameters(array());
         }
     }
     // is this an internal redirect?
     if (isset($pageInfo['redirect_page_id']) && $pageInfo['redirect_page_id'] != '') {
         // get url for item
         $newPageURL = Navigation::getURL((int) $pageInfo['redirect_page_id']);
         $errorURL = Navigation::getURL(404);
         // not an error?
         if ($newPageURL != $errorURL) {
             // redirect
             throw new RedirectException('Redirect', new RedirectResponse($newPageURL, $pageInfo['redirect_code']));
         }
     }
     // is this an external redirect?
     if (isset($pageInfo['redirect_url']) && $pageInfo['redirect_url'] != '') {
         // redirect
         throw new RedirectException('Redirect', new RedirectResponse($pageInfo['redirect_url'], $pageInfo['redirect_code']));
     }
 }
示例#3
0
 /**
  * Validate the form
  */
 private function validateForm()
 {
     // get settings
     $subscriptionsAllowed = isset($this->settings['allow_subscriptions']) && $this->settings['allow_subscriptions'];
     // subscriptions aren't allowed so we don't have to validate
     if (!$subscriptionsAllowed) {
         return false;
     }
     // is the form submitted
     if ($this->frm->isSubmitted()) {
         // cleanup the submitted fields, ignore fields that were added by hackers
         $this->frm->cleanupFields();
         // does the key exists?
         if (\SpoonSession::exists('agenda_subscription_' . $this->record['id'])) {
             // calculate difference
             $diff = time() - (int) \SpoonSession::get('agenda_subscription_' . $this->record['id']);
             // calculate difference, it it isn't 10 seconds the we tell the user to slow down
             if ($diff < 10 && $diff != 0) {
                 $this->frm->getField('message')->addError(FL::err('CommentTimeout'));
             }
         }
         // validate required fields
         $this->frm->getField('name')->isFilled(FL::err('NameIsRequired'));
         $this->frm->getField('email')->isEmail(FL::err('EmailIsRequired'));
         // no errors?
         if ($this->frm->isCorrect()) {
             // get module setting
             $moderationEnabled = isset($this->settings['moderation']) && $this->settings['moderation'];
             // reformat data
             $name = $this->frm->getField('name')->getValue();
             $email = $this->frm->getField('email')->getValue();
             // build array
             $subscription['agenda_id'] = $this->record['id'];
             $subscription['language'] = FRONTEND_LANGUAGE;
             $subscription['created_on'] = FrontendModel::getUTCDate();
             $subscription['name'] = $name;
             $subscription['email'] = $email;
             $subscription['status'] = 'subscribed';
             // get URL for article
             $permaLink = $this->record['full_url'];
             $redirectLink = $permaLink;
             // is moderation enabled
             if ($moderationEnabled) {
                 // if the commenter isn't moderated before alter the subscription status so it will appear in the moderation queue
                 if (!FrontendAgendaModel::isModerated($name, $email)) {
                     $subscription['status'] = 'moderation';
                 }
             }
             // insert comment
             $subscription['id'] = FrontendAgendaModel::insertSubscription($subscription);
             // trigger event
             FrontendModel::triggerEvent('agenda', 'after_add_subscription', array('subscription' => $subscription));
             // append a parameter to the URL so we can show moderation
             if (strpos($redirectLink, '?') === false) {
                 if ($subscription['status'] == 'moderation') {
                     $redirectLink .= '?subscription=moderation#' . FL::act('Subscribe');
                 }
                 if ($subscription['status'] == 'subscribed') {
                     $redirectLink .= '?subscription=true#subscription-' . $subscription['id'];
                 }
             } else {
                 if ($subscription['status'] == 'moderation') {
                     $redirectLink .= '&subscription=moderation#' . FL::act('Subscribe');
                 }
                 if ($subscription['status'] == 'subscribed') {
                     $redirectLink .= '&subscription=true#comment-' . $subscription['id'];
                 }
             }
             // set title
             $subscription['agenda_title'] = $this->record['title'];
             $subscription['agenda_url'] = $this->record['url'];
             // notify the admin
             FrontendAgendaModel::notifyAdmin($subscription);
             // store timestamp in session so we can block excessive usage
             \SpoonSession::set('agenda_subscription_' . $this->record['id'], time());
             // store author-data in cookies
             try {
                 Cookie::set('subscription_author', $name);
                 Cookie::set('subscription_email', $email);
             } catch (Exception $e) {
                 // settings cookies isn't allowed, but because this isn't a real problem we ignore the exception
             }
             // redirect
             $this->redirect($redirectLink);
         }
     }
 }
示例#4
0
 /**
  * Get the visitor's id (using a tracking cookie)
  *
  * @return string
  */
 public static function getVisitorId()
 {
     // check if tracking id is fetched already
     if (self::$visitorId !== null) {
         return self::$visitorId;
     }
     // get/init tracking identifier
     self::$visitorId = CommonCookie::exists('track') && !empty($_COOKIE['track']) ? (string) CommonCookie::get('track') : md5(uniqid() . \SpoonSession::getSessionId());
     if (!self::get('fork.settings')->get('Core', 'show_cookie_bar', false) || CommonCookie::hasAllowedCookies()) {
         CommonCookie::set('track', self::$visitorId, 86400 * 365);
     }
     return self::getVisitorId();
 }
示例#5
0
 /**
  * Validate the form
  */
 private function validateForm()
 {
     // get settings
     $commentsAllowed = isset($this->settings['allow_comments']) && $this->settings['allow_comments'];
     // comments aren't allowed so we don't have to validate
     if (!$commentsAllowed) {
         return false;
     }
     // is the form submitted
     if ($this->frm->isSubmitted()) {
         // cleanup the submitted fields, ignore fields that were added by hackers
         $this->frm->cleanupFields();
         // does the key exists?
         if (\SpoonSession::exists('blog_comment_' . $this->record['id'])) {
             // calculate difference
             $diff = time() - (int) \SpoonSession::get('blog_comment_' . $this->record['id']);
             // calculate difference, it it isn't 10 seconds the we tell the user to slow down
             if ($diff < 10 && $diff != 0) {
                 $this->frm->getField('message')->addError(FL::err('CommentTimeout'));
             }
         }
         // validate required fields
         $this->frm->getField('author')->isFilled(FL::err('AuthorIsRequired'));
         $this->frm->getField('email')->isEmail(FL::err('EmailIsRequired'));
         $this->frm->getField('message')->isFilled(FL::err('MessageIsRequired'));
         // validate optional fields
         if ($this->frm->getField('website')->isFilled() && $this->frm->getField('website')->getValue() != 'http://') {
             $this->frm->getField('website')->isURL(FL::err('InvalidURL'));
         }
         // no errors?
         if ($this->frm->isCorrect()) {
             // get module setting
             $spamFilterEnabled = isset($this->settings['spamfilter']) && $this->settings['spamfilter'];
             $moderationEnabled = isset($this->settings['moderation']) && $this->settings['moderation'];
             // reformat data
             $author = $this->frm->getField('author')->getValue();
             $email = $this->frm->getField('email')->getValue();
             $website = $this->frm->getField('website')->getValue();
             if (trim($website) == '' || $website == 'http://') {
                 $website = null;
             }
             $text = $this->frm->getField('message')->getValue();
             // build array
             $comment['post_id'] = $this->record['id'];
             $comment['language'] = FRONTEND_LANGUAGE;
             $comment['created_on'] = FrontendModel::getUTCDate();
             $comment['author'] = $author;
             $comment['email'] = $email;
             $comment['website'] = $website;
             $comment['text'] = $text;
             $comment['status'] = 'published';
             $comment['data'] = serialize(array('server' => $_SERVER));
             // get URL for article
             $permaLink = $this->record['full_url'];
             $redirectLink = $permaLink;
             // is moderation enabled
             if ($moderationEnabled) {
                 // if the commenter isn't moderated before alter the
                 // comment status so it will appear in the moderation queue
                 if (!FrontendBlogModel::isModerated($author, $email)) {
                     $comment['status'] = 'moderation';
                 }
             }
             // should we check if the item is spam
             if ($spamFilterEnabled) {
                 // check for spam
                 $result = FrontendModel::isSpam($text, SITE_URL . $permaLink, $author, $email, $website);
                 // if the comment is spam alter the comment status so it will appear in the spam queue
                 if ($result) {
                     $comment['status'] = 'spam';
                 } elseif ($result == 'unknown') {
                     // if the status is unknown then we should moderate it manually
                     $comment['status'] = 'moderation';
                 }
             }
             // insert comment
             $comment['id'] = FrontendBlogModel::insertComment($comment);
             // trigger event
             FrontendModel::triggerEvent('Blog', 'after_add_comment', array('comment' => $comment));
             // append a parameter to the URL so we can show moderation
             if (strpos($redirectLink, '?') === false) {
                 if ($comment['status'] == 'moderation') {
                     $redirectLink .= '?comment=moderation#' . FL::act('Comment');
                 }
                 if ($comment['status'] == 'spam') {
                     $redirectLink .= '?comment=spam#' . FL::act('Comment');
                 }
                 if ($comment['status'] == 'published') {
                     $redirectLink .= '?comment=true#comment-' . $comment['id'];
                 }
             } else {
                 if ($comment['status'] == 'moderation') {
                     $redirectLink .= '&comment=moderation#' . FL::act('Comment');
                 }
                 if ($comment['status'] == 'spam') {
                     $redirectLink .= '&comment=spam#' . FL::act('Comment');
                 }
                 if ($comment['status'] == 'published') {
                     $redirectLink .= '&comment=true#comment-' . $comment['id'];
                 }
             }
             // set title
             $comment['post_title'] = $this->record['title'];
             $comment['post_url'] = $this->record['url'];
             // notify the admin
             FrontendBlogModel::notifyAdmin($comment);
             // store timestamp in session so we can block excessive usage
             \SpoonSession::set('blog_comment_' . $this->record['id'], time());
             // store author-data in cookies
             try {
                 CommonCookie::set('comment_author', $author);
                 CommonCookie::set('comment_email', $email);
                 CommonCookie::set('comment_website', $website);
             } catch (\Exception $e) {
                 // settings cookies isn't allowed, but because this isn't a real problem we ignore the exception
             }
             // redirect
             $this->redirect($redirectLink);
         }
     }
 }
示例#6
0
 /**
  * Set locale
  * It will require the correct file and init the needed vars
  *
  * @param string $language The language to load.
  */
 public static function setLocale($language)
 {
     $language = (string) $language;
     // validate file, generate it if needed
     if (!is_file(BACKEND_CACHE_PATH . '/Locale/en.json')) {
         BackendLocaleModel::buildCache('en', APPLICATION);
     }
     if (!is_file(BACKEND_CACHE_PATH . '/Locale/' . $language . '.json')) {
         // if you use the language in the console act like it is in the backend
         BackendLocaleModel::buildCache($language, defined('APPLICATION') && APPLICATION === 'Console' ? 'Backend' : APPLICATION);
     }
     // store
     self::$currentInterfaceLanguage = $language;
     // attempt to set a cookie
     try {
         // Needed to make it possible to use the backend language in the console.
         if (defined('APPLICATION') && APPLICATION !== 'Console') {
             CommonCookie::set('interface_language', $language);
         }
     } catch (\SpoonCookieException $e) {
         // settings cookies isn't allowed, because this isn't a real problem we ignore the exception
     }
     // set English translations, they'll be the fallback
     $translations = json_decode(file_get_contents(BACKEND_CACHE_PATH . '/Locale/en.json'), true);
     self::$err = (array) $translations['err'];
     self::$lbl = (array) $translations['lbl'];
     self::$msg = (array) $translations['msg'];
     // overwrite with the requested language's translations
     $translations = json_decode(file_get_contents(BACKEND_CACHE_PATH . '/Locale/' . $language . '.json'), true);
     $err = (array) $translations['err'];
     $lbl = (array) $translations['lbl'];
     $msg = (array) $translations['msg'];
     foreach ($err as $module => $translations) {
         if (!isset(self::$err[$module])) {
             self::$err[$module] = array();
         }
         self::$err[$module] = array_merge(self::$err[$module], $translations);
     }
     foreach ($lbl as $module => $translations) {
         if (!isset(self::$lbl[$module])) {
             self::$lbl[$module] = array();
         }
         self::$lbl[$module] = array_merge(self::$lbl[$module], $translations);
     }
     foreach ($msg as $module => $translations) {
         if (!isset(self::$msg[$module])) {
             self::$msg[$module] = array();
         }
         self::$msg[$module] = array_merge(self::$msg[$module], $translations);
     }
 }
示例#7
0
 /**
  * Login a profile.
  *
  * @param  int  $profileId Login the profile with this id in.
  * @param  bool $remember  Should we set a cookie for later?
  * @return bool
  */
 public static function login($profileId, $remember = false)
 {
     // redefine vars
     $profileId = (int) $profileId;
     $remember = (bool) $remember;
     $secretKey = null;
     // cleanup old sessions
     self::cleanupOldSessions();
     // set profile_logged_in to true
     \SpoonSession::set('frontend_profile_logged_in', true);
     // should we remember the user?
     if ($remember) {
         // generate secret key
         $secretKey = FrontendProfilesModel::getEncryptedString(\SpoonSession::getSessionId(), FrontendProfilesModel::getRandomString());
         // set cookie
         CommonCookie::set('frontend_profile_secret_key', $secretKey);
     }
     // delete all records for this session to prevent duplicate keys (this should never happen)
     FrontendModel::getContainer()->get('database')->delete('profiles_sessions', 'session_id = ?', \SpoonSession::getSessionId());
     // insert new session record
     FrontendModel::getContainer()->get('database')->insert('profiles_sessions', array('profile_id' => $profileId, 'session_id' => \SpoonSession::getSessionId(), 'secret_key' => $secretKey, 'date' => FrontendModel::getUTCDate()));
     // update last login
     FrontendProfilesModel::update($profileId, array('last_login' => FrontendModel::getUTCDate()));
     // trigger event
     FrontendModel::triggerEvent('Profiles', 'after_logged_in', array('profile_id' => $profileId));
     // load the profile object
     self::$profile = new FrontendProfilesProfile($profileId);
 }
示例#8
0
 /**
  * Validate the form
  */
 private function validateForm()
 {
     // is the form submitted
     if ($this->frm->isSubmitted()) {
         // cleanup the submitted fields, ignore fields that were added by hackers
         $this->frm->cleanupFields();
         // validate required fields
         $this->frm->getField('email')->isEmail(FL::err('EmailIsRequired'));
         $this->frm->getField('fname')->isFilled(FL::err('MessageIsRequired'));
         $this->frm->getField('lname')->isFilled(FL::err('MessageIsRequired'));
         $this->frm->getField('address')->isFilled(FL::err('MessageIsRequired'));
         $this->frm->getField('hnumber')->isFilled(FL::err('MessageIsRequired'));
         $this->frm->getField('postal')->isFilled(FL::err('MessageIsRequired'));
         $this->frm->getField('hometown')->isFilled(FL::err('MessageIsRequired'));
         // correct?
         if ($this->frm->isCorrect()) {
             // build array
             $order['email'] = $this->frm->getField('email')->getValue();
             $order['fname'] = $this->frm->getField('fname')->getValue();
             $order['lname'] = $this->frm->getField('lname')->getValue();
             $order['address'] = $this->frm->getField('address')->getValue();
             $order['hnumber'] = $this->frm->getField('hnumber')->getValue();
             $order['postal'] = $this->frm->getField('postal')->getValue();
             $order['hometown'] = $this->frm->getField('hometown')->getValue();
             $order['status'] = 'moderation';
             // insert values in database
             FrontendCatalogModel::updateOrder($order, $this->cookieOrderId);
             // delete cookie
             $argument = 'order_id';
             unset($_COOKIE[(string) $argument]);
             setcookie((string) $argument, null, 1, '/');
             // set cookies person --> optional
             Cookie::set('email', $order['email']);
             Cookie::set('fname', $order['fname']);
             Cookie::set('lname', $order['lname']);
             Cookie::set('address', $order['address']);
             Cookie::set('hnumber', $order['hnumber']);
             Cookie::set('postal', $order['postal']);
             Cookie::set('hometown', $order['hometown']);
             Cookie::set('status', $order['status']);
             // trigger event
             FrontendModel::triggerEvent('Catalog', 'after_add_order', array('order' => $order));
             $url = FrontendNavigation::getURLForBlock('Catalog', 'OrderReceived');
             $this->redirect($url);
         }
     }
 }