/** * Fetches and parses an RSS or Atom feed, and returns its items. * * Each element in the returned array will have the following keys: * * - **authors** – An array of the item’s authors, where each sub-element has the following keys: * - **name** – The author’s name * - **url** – The author’s URL * - **email** – The author’s email * - **categories** – An array of the item’s categories, where each sub-element has the following keys: * - **term** – The category’s term * - **scheme** – The category’s scheme * - **label** – The category’s label * - **content** – The item’s main content. * - **contributors** – An array of the item’s contributors, where each sub-element has the following keys: * - **name** – The contributor’s name * - **url** – The contributor’s URL * - **email** – The contributor’s email * - **date** – A {@link DateTime} object representing the item’s date. * - **dateUpdated** – A {@link DateTime} object representing the item’s last updated date. * - **permalink** – The item’s URL. * - **summary** – The item’s summary content. * - **title** – The item’s title. * * @param string $url The feed’s URL. * @param int $limit The maximum number of items to return. Default is 0 (no limit). * @param int $offset The number of items to skip. Defaults to 0. * @param string $cacheDuration Any valid [PHP time format](http://www.php.net/manual/en/datetime.formats.time.php). * * @return array|string The list of feed items. */ public function getFeedItems($url, $limit = 0, $offset = 0, $cacheDuration = null) { $items = array(); if (!extension_loaded('dom')) { Craft::log('Craft needs the PHP DOM extension (http://www.php.net/manual/en/book.dom.php) enabled to parse feeds.', LogLevel::Warning); return $items; } if (!$cacheDuration) { $cacheDuration = craft()->config->getCacheDuration(); } else { $cacheDuration = DateTimeHelper::timeFormatToSeconds($cacheDuration); } $feed = new \SimplePie(); $feed->set_feed_url($url); $feed->set_cache_location(craft()->path->getCachePath()); $feed->set_cache_duration($cacheDuration); $feed->init(); // Something went wrong. if ($feed->error()) { Craft: log('There was a problem parsing the feed: ' . $feed->error(), LogLevel::Warning); return array(); } foreach ($feed->get_items($offset, $limit) as $item) { $date = $item->get_date('U'); $dateUpdated = $item->get_updated_date('U'); $items[] = array('authors' => $this->_getItemAuthors($item->get_authors()), 'categories' => $this->_getItemCategories($item->get_categories()), 'content' => $item->get_content(true), 'contributors' => $this->_getItemAuthors($item->get_contributors()), 'date' => $date ? new DateTime('@' . $date) : null, 'dateUpdated' => $dateUpdated ? new DateTime('@' . $dateUpdated) : null, 'permalink' => $item->get_permalink(), 'summary' => $item->get_description(true), 'title' => $item->get_title(), 'enclosures' => $this->_getEnclosures($item->get_enclosures())); } return $items; }
/** * Fetches and parses an RSS or Atom feed, and returns its items. * * Each element in the returned array will have the following keys: * * - **authors** – An array of the item’s authors, where each sub-element has the following keys: * - **name** – The author’s name * - **url** – The author’s URL * - **email** – The author’s email * - **categories** – An array of the item’s categories, where each sub-element has the following keys: * - **term** – The category’s term * - **scheme** – The category’s scheme * - **label** – The category’s label * - **content** – The item’s main content. * - **contributors** – An array of the item’s contributors, where each sub-element has the following keys: * - **name** – The contributor’s name * - **url** – The contributor’s URL * - **email** – The contributor’s email * - **date** – A {@link DateTime} object representing the item’s date. * - **dateUpdated** – A {@link DateTime} object representing the item’s last updated date. * - **permalink** – The item’s URL. * - **summary** – The item’s summary content. * - **title** – The item’s title. * * @param string $url The feed’s URL. * @param int $limit The maximum number of items to return. Default is 0 (no limit). * @param int $offset The number of items to skip. Defaults to 0. * @param string $cacheDuration Any valid [PHP time format](http://www.php.net/manual/en/datetime.formats.time.php). * * @return array|string The list of feed items. */ public function getFeedItems($url, $limit = 0, $offset = 0, $cacheDuration = null) { $items = array(); if (!extension_loaded('dom')) { Craft::log('Craft needs the PHP DOM extension (http://www.php.net/manual/en/book.dom.php) enabled to parse feeds.', LogLevel::Warning); return $items; } if (!$cacheDuration) { $cacheDuration = craft()->config->getCacheDuration(); } else { $cacheDuration = DateTimeHelper::timeFormatToSeconds($cacheDuration); } // Potentially long-running request, so close session to prevent session blocking on subsequent requests. craft()->session->close(); $feed = new \SimplePie(); $feed->set_feed_url($url); $feed->set_cache_location(craft()->path->getCachePath()); $feed->set_cache_duration($cacheDuration); $feed->init(); // Something went wrong. if ($feed->error()) { Craft::log('There was a problem parsing the feed: ' . $feed->error(), LogLevel::Warning); return array(); } foreach ($feed->get_items($offset, $limit) as $item) { // Validate the permalink $permalink = $item->get_permalink(); if ($permalink) { $urlModel = new UrlModel(); $urlModel->url = $item->get_permalink(); if (!$urlModel->validate()) { Craft::log('An item was omitted from the feed (' . $url . ') because its permalink was an invalid URL: ' . $permalink); continue; } } $date = $item->get_date('U'); $dateUpdated = $item->get_updated_date('U'); $items[] = array('authors' => $this->_getItemAuthors($item->get_authors()), 'categories' => $this->_getItemCategories($item->get_categories()), 'content' => $item->get_content(true), 'contributors' => $this->_getItemAuthors($item->get_contributors()), 'date' => $date ? new DateTime('@' . $date) : null, 'dateUpdated' => $dateUpdated ? new DateTime('@' . $dateUpdated) : null, 'permalink' => $item->get_permalink(), 'summary' => $item->get_description(true), 'title' => $item->get_title(), 'enclosures' => $this->_getEnclosures($item->get_enclosures())); } return $items; }
/** * If the 'rememberUsernameDuration' config setting is set, will save a cookie with the given username for that * duration. Otherwise, will delete any existing username cookie. * * @param string $username The username to save in the cookie. * * @return null */ public function processUsernameCookie($username) { // See if the 'rememberUsernameDuration' config item is set. If so, save the name to a cookie. $rememberUsernameDuration = craft()->config->get('rememberUsernameDuration'); if ($rememberUsernameDuration) { $this->saveCookie('username', $username, DateTimeHelper::timeFormatToSeconds($rememberUsernameDuration)); } else { // Just in case... $this->deleteStateCookie('username'); } }
/** * Returns the current user identity cookie’s value, if there is one. * * @param HttpCookie|null The user identity cookie, or `null` if you don’t have it on hand. * * @return array|null The user identity cookie’s data, or `null` if it didn’t exist. */ public function getIdentityCookieValue(HttpCookie $cookie = null) { if (!$cookie) { $cookie = $this->getIdentityCookie(); } if ($cookie && ($data = $this->getStateCookieValue($cookie)) && is_array($data) && isset($data[0], $data[1], $data[2], $data[3], $data[4], $data[5])) { // TODO: remove this code after a while // If $data[3] is something besides 0 or 1, it was created before Craft 2.2, and represents the auth timeout // rather than whether Remember Me was checked. Let's fix that. if ($data[3] != 0 && $data[3] != 1) { // Delete the old rememberMe cookie(s) craft()->request->deleteCookie('rememberMe'); $this->deleteStateCookie('rememberMe'); // Replace $data[3]'s value with a 0 or 1 $duration = craft()->config->get('rememberedUserSessionDuration'); if (is_numeric($data[3]) && $data[3] >= DateTimeHelper::timeFormatToSeconds($duration)) { $data[3] = 1; } else { $data[3] = 0; } } return $data; } }
/** * Returns the configured elevated session duration in seconds. * * @return int The elevated session duration in seconds. */ public function getElevatedSessionDuration() { $duration = craft()->config->get('elevatedSessionDuration'); if ($duration) { return DateTimeHelper::timeFormatToSeconds($duration); } // Default to 5 minutes return 300; }
/** * Returns the configured user session duration in seconds, or `null` if there is none because user sessions should * expire when the HTTP session expires. * * You can choose whether the * [rememberedUserSessionDuration](http://craftcms.com/docs/config-settings#rememberedUserSessionDuration) * or [userSessionDuration](http://craftcms.com/docs/config-settings#userSessionDuration) config setting * should be used with the $remembered param. If rememberedUserSessionDuration’s value is empty (disabling the * feature) then userSessionDuration will be used regardless of $remembered. * * @param bool $remembered Whether the rememberedUserSessionDuration config setting should be used if it’s set. * Default is `false`. * * @return int|null The user session duration in seconds, or `null` if user sessions should expire along with the * HTTP session. */ public function getUserSessionDuration($remembered = false) { if ($remembered) { $duration = craft()->config->get('rememberedUserSessionDuration'); } // Even if $remembered = true, it's possible that they've disabled long-term user sessions // by setting rememberedUserSessionDuration = 0 if (empty($duration)) { $duration = craft()->config->get('userSessionDuration'); } if ($duration) { return DateTimeHelper::timeFormatToSeconds($duration); } }
/** * Returns the configured elevated session duration in seconds. * * @return int|boolean The elevated session duration in seconds or false if it has been disabled. */ public function getElevatedSessionDuration() { $duration = craft()->config->get('elevatedSessionDuration'); // See if it has been disabled. if ($duration === false) { return false; } if ($duration) { return DateTimeHelper::timeFormatToSeconds($duration); } // Default to 5 minutes return 300; }