/** * Send a file to the user agent * * @param bool $forcedownload Set to true to force download instead of just sending the file. * * @throws \Exception * * @return void */ public function sendToUserAgent($forcedownload = false) { $view = \Core\view(); $request = \Core\page_request(); $view->mode = \View::MODE_NOOUTPUT; $view->contenttype = $this->getMimetype(); $view->updated = $this->getMTime(); if($forcedownload){ $view->headers['Content-Disposition'] = 'attachment; filename="' . $this->getBasename() . '"'; $view->headers['Cache-Control'] = 'no-cache, must-revalidate'; $view->headers['Content-Transfer-Encoding'] = 'binary'; } $view->headers['Content-Length'] = $this->getFilesize(); // Send all the view headers $view->render(); // And now the actual content if it's not a HEAD request. if($request->method != \PageRequest::METHOD_HEAD){ echo $this->getContents(); } }
/** * Load the underlying Filters from a given request, (optionally). * * @param \PageRequest|null $request */ public function loadFiltersFromRequest($request = null){ if($request === null){ $request = \Core\page_request(); } $this->getFilters()->load($request); }
/** * Function that attaches the core javascript to the page. * * This should be called automatically from the hook /core/page/preexecute. */ public static function _AttachCoreJavascript() { if(Core::IsComponentAvailable('User')){ $userid = (\Core\user()->get('id') ? \Core\user()->get('id') : 0); $userauth = \Core\user()->exists() ? 'true' : 'false'; } else{ $userid = 0; $userauth = 'false'; } $ua = \Core\UserAgent::Construct(); $uastring = ''; foreach($ua->asArray() as $k => $v){ if($v === true){ $uastring .= "\t\t\t$k: true,\n"; } elseif($v === false){ $uastring .= "\t\t\t$k: false,\n"; } else{ $uastring .= "\t\t\t$k: \"$v\",\n"; } } $uastring .= "\t\t\tis_mobile: " . ($ua->isMobile() ? 'true' : 'false') . "\n"; $url = htmlentities(\Core\page_request()->uriresolved); if(ConfigHandler::Get('/core/page/url_remove_stop_words')){ $stopwords = json_encode(\Core\get_stop_words()); $removeStopWords = 'true'; } else{ $stopwords = '""'; $removeStopWords = 'false'; } $version = DEVELOPMENT_MODE ? self::GetComponent()->getVersion() : ''; $rootWDIR = ROOT_WDIR; $rootURL = ROOT_URL; $rootURLSSL = ROOT_URL_SSL; $rootURLnoSSL = ROOT_URL_NOSSL; $ssl = SSL ? 'true' : 'false'; $sslMode = SSL_MODE; $script = <<<EOD <script type="text/javascript"> var Core = { Version: "$version", ROOT_WDIR: "$rootWDIR", ROOT_URL: "$rootURL", ROOT_URL_SSL: "$rootURLSSL", ROOT_URL_NOSSL: "$rootURLnoSSL", SSL: $ssl, SSL_MODE: "$sslMode", User: { id: "$userid", authenticated: $userauth }, Url: "$url", Browser: { $uastring }, URLRemoveStopWords: $removeStopWords, StopWords: $stopwords }; </script> EOD; $minified = \ConfigHandler::Get('/core/javascript/minified'); if($minified){ $script = str_replace(["\t", "\n"], ['', ''], $script); } \Core\view()->addScript($script, 'head'); // And the static functions. \Core\view()->addScript('js/core.js', 'foot'); //\Core\view()->addScript('js/core-foot.js', 'foot'); }
/** * Return a valid resolved "A" tag, with whatever inner content preserved. * * This is the recommended way to handle <a/> tags in Core templates. * The href attribute is automatically resolved to the primary rewrite URL, * and all additional links, (mostly), are passed through to the template as-is. * * #### Smarty Parameters * * * assign * * Assign the result to a variable instead of printing it to stdout. * * confirm * * Set to a string to prompt the user with the string before submitting the link via a POST request. * * history * * Set to a number, (1, 2, etc), to set the href to that user's last nth page from history. * * #### Standard Example * * <pre> * // The smarty line * {a href="/content/view/1"}Something Blah{/a} * // Resolves to * <a href="/homepage">Something Blah</a> * </pre> * * #### External Link * * Also works with already-resolved or external links * * <pre> * {a href="http://corepl.us" target="_blank"}Link Somewhere!{/a} * // Resolves to * <a href="http://corepl.use" target="_blank">Link Somewhere!</a> * </pre> * * #### Multisite Links * * When multi-site is installed and activated, cross-site links can also be used that resolve based on that site's criteria. * * <pre> * {a href="site:12/content/view/5"}Child #12 About Page{/a} * // Resolves to * <a href="http://child-12-url.example.com/about-us">Child #12 About Page</a> * </pre> * * @param array $params Associative (and/or indexed) array of smarty parameters passed in from the template * @param string|null $content Null on opening pass, rendered source of the contents inside the block on closing pass * @param Smarty $smarty Parent Smarty template object * @param boolean $repeat True at the first call of the block-function (the opening tag) and * false on all subsequent calls to the block function (the block's closing tag). * Each time the function implementation returns with $repeat being TRUE, * the contents between {func}...{/func} are evaluated and the function implementation * is called again with the new block contents in the parameter $content. * * @return string */ function smarty_block_a($params, $content, $smarty, &$repeat){ // This only needs to be called once. if($repeat) return ''; $assign= false; // Start the A tag $newcontent = '<a'; // Allow "confirm" text to override the href and onClick functions. // This has the cool ability of not requiring jquery to run, since it is all handled with PHP logic. if(isset($params['confirm'])){ $params['onclick'] = 'return Core.ConfirmEvent(this);'; $params['data-href'] = \Core\resolve_link($params['href']); $params['data-confirm'] = $params['confirm']; $params['href'] = '#false'; } // Add in any attributes. foreach($params as $k => $v){ $k = strtolower($k); switch($k){ case 'href': $newcontent .= ' href="' . \Core\resolve_link($v) . '"'; break; case 'history': case 'back': $newcontent .= ' href="' . \Core\page_request()->getReferrer() . '"'; break; case 'assign': $assign = $v; break; case 'alt': case 'title': case 'data-confirm': // These all support translation via the "t:" prefix! if(strpos($v, 't:') === 0){ $newcontent .= " $k=\"" . str_replace('"', '"', t(substr($v, 2))) . "\""; } else{ $newcontent .= " $k=\"" . str_replace('"', '"', $v) . "\""; } break; default: // Standard non-fun values :p $newcontent .= " $k=\"" . str_replace('"', '"', $v) . "\""; } } // Close the starting tag. $newcontent .= '>'; // Add any content inside. $newcontent .= $content; // Close the set. $newcontent .= '</a>'; if($assign){ $smarty->assign($assign, $newcontent); return ''; } else{ return $newcontent; } }
/** * Render this form and all inside elements to valid HTML. * * This will also save the form to the session data for post-submission validation. * (if called with null or "foot") * * @param mixed $part "body|head|foot| or null * Render just a specific part of the form. Useful for advanced usage. * null: Render all of the form and its element. * "head": Render just the beginning of the form, including the <form> opening tag. * "body": Render just the body of the form, specifically the elements. * "foot": Render just the end of the form, including the </form> closing tag. * * @return string (valid HTML) */ public function render($part = null) { // Check and see if there are any elements in this form that require a fileupload. foreach ($this->getElements() as $e) { if ($e->requiresupload) { $this->set('enctype', 'multipart/form-data'); break; } } // Will be used to know if the errors in elements should be removed prior to rendering. $ignoreerrors = false; // Slip in the formid tracker to remember this submission. if (($part === null || $part == 'body') && $this->get('callsmethod')) { /*$e = new FormHiddenInput(array('name' => '___formid', 'value' => $this->get('uniqueid'))); $this->_elements = array_merge(array($e), $this->_elements); */ /* // I need to ensure a repeatable but unique id for this form. // Essentially when this form is submitted, I need to be able to know that it's the same form upon re-rendering. if (!$this->get('uniqueid')) { $hash = $this->generateUniqueHash(); $this->set('uniqueid', $hash); $this->getElementByName('___formid')->set('value', $hash); } */ // Was this form already submitted, (and thus saved in the session? // If so, render that form instead! This way the values get transported seamlessly. // I need the hash at present, regardless if all elements have been rendered to the screen or not. $hash = ($this->get('uniqueid') ? $this->get('uniqueid') : $this->generateUniqueHash()); if (($savedform = \Core\Session::Get('FormData/' . $hash)) !== null) { if (($savedform = unserialize($savedform))) { /** @var Form $savedform */ // If this form is not set as persistent, then don't restore the values! if($savedform->persistent){ foreach($this->_elements as $k => $element){ /** @var FormElement $element */ if($element->persistent){ $this->_elements[$k] = $savedform->_elements[$k]; } } } } else { $ignoreerrors = true; } } else { $ignoreerrors = true; } } if(($part == null || $part == 'foot') && $this->get('callsmethod')){ // I need to ensure a repeatable but unique id for this form. // Essentially when this form is submitted, I need to be able to know that it's the same form upon re-rendering. if (!$this->get('uniqueid')) { $hash = $this->generateUniqueHash(); $this->set('uniqueid', $hash); } } if ($ignoreerrors) { foreach ($this->getElements(true) as $el) { $el->setError(false); } } $tpl = \Core\Templates\Template::Factory('forms/form.tpl'); $tpl->assign('group', $this); if ($part === null || $part == 'body') { $els = ''; // Fill in the elements foreach ($this->_elements as $e) { $els .= $e->render(); } $tpl->assign('elements', $els); } switch ($part) { case null: $out = $tpl->fetch('forms/form.tpl'); break; case 'head': $out = $tpl->fetch('forms/form.head.tpl'); break; case 'body': $out = $tpl->fetch('forms/form.body.tpl'); break; case 'foot': $out = $tpl->fetch('forms/form.foot.tpl'); break; default: if(($el = $this->getElement($part)) !== false){ $out = $el->render(); } } // Save it $this->referrer = \Core\page_request()->referrer; $this->originalurl = CUR_CALL; $this->persistent = false; if (($part === null || $part == 'foot') && $this->get('callsmethod')) { $this->saveToSession(); } return $out; }
/** * Hook into /core/page/rendering to add the control link for this page if necessary and the user has the appropriate permissions. */ public static function HookPageRender(){ $viewer = \Core\user()->checkAccess('p:/core/widgets/manage'); $manager = \Core\user()->checkAccess('p:/core/widgets/manage'); if(!($viewer || $manager)){ // User does not have access to view nor to edit widgets, simply return out of here. return true; } $request = \Core\page_request(); $view = \Core\view(); $page = $request->getPageModel(); $tmplName = $page->get('last_template') ? $page->get('last_template') : $view->templatename; if(!$tmplName){ // This page has no templates, ergo no widget areas. return true; } $template = \Core\Templates\Template::Factory($tmplName); $areas = $template->getWidgetAreas(); if(!sizeof($areas)){ // Selected template does not have any widget areas defined, no need to display the option then! return true; } // Otherwise... $view->addControl('Page Widgets', '/admin/widgets?baseurl=' . $page->get('baseurl'), 'cog'); return true; }