public function uploadFile($file) { $target_dir = "data/"; $filename = md5(time()); $i = 1; $cpt = md5($i); $ext = strtolower(substr(strrchr($file['name'], '.'), 1)); $target_file = $target_dir . $filename . "_" . $cpt . "." . $ext; // Si le fichier existe while (file_exists(Page::path($target_file))) { $i++; $cpt = md5($i); $target_file = $target_dir . $filename . "_" . $cpt . "." . $ext; } if (move_uploaded_file($file["tmp_name"], $target_file)) { $f = new FileObject(); $f->permission = "access content"; $f->module = ""; $f->path = "/" . $target_file; $f->content_type = $file['type']; $f->save(); return $f->id_file; } else { return false; } }
public static function page_download_content($id_file) { $theme = new Theme(); $f = new FileObject(); if ($f->load($id_file)) { $f->nb_dl++; $f->save(); $filename = Page::path($f->path); if (ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); } header('Pragma: public'); // required header("Content-Type: {$f->content_type}"); header('Content-Disposition: attachment; filename="' . $f->id_file . '.' . $f->getExtension() . '"'); header('Content-Transfer-Encoding: binary'); header('Content-Length: ' . filesize($filename)); // provide file size header('Connection: close'); readfile($filename); // push it out exit; } else { $theme->process_404(); } return; }
/** * Debug info specific to Page objects * * @param Page $page * @return array * */ protected function Page(Page $page) { $info = array('instanceID' => $page->instanceID, 'id' => $page->id, 'name' => $page->name, 'namePrevious' => '', 'path' => $page->path(), 'status' => implode(', ', $page->status(true)), 'statusPrevious' => 0, 'template' => $page->template ? $page->template->name : '', 'templatePrevious' => '', 'parent' => $page->parent ? $page->parent->path : '', 'parentPrevious' => '', 'numChildren' => $page->numChildren(), 'sort' => $page->sort, 'sortfield' => $page->sortfield, 'created' => $page->created, 'modified' => $page->modified, 'createdUser' => $page->createdUser ? $page->createdUser->name : $page->created_users_id, 'modifiedUser' => $page->modifiedUser ? $page->modifiedUser->name : $page->modified_users_id); if ($page->namePrevious) { $info['namePrevious'] = $page->namePrevious; } else { unset($info['namePrevious']); } if ($page->statusPrevious !== null) { $info['statusPrevious'] = implode(', ', $page->status(true, $page->statusPrevious)); } else { unset($info['statusPrevious']); } if ($page->templatePrevious) { $info['templatePrevious'] = $page->templatePrevious->name; } else { unset($info['templatePrevious']); } if ($page->parentPrevious) { $info['parentPrevious'] = $page->parentPrevious->path(); } else { unset($info['parentPrevious']); } if ($page->isNew) { $info['isNew'] = 1; } $info['isLoaded'] = (int) $page->isLoaded(); $info['outputFormatting'] = (int) $page->outputFormatting(); if ($page->quietMode) { $info['quietMode'] = 1; } foreach (array('created', 'modified') as $key) { $info[$key] = wireDate($this->wire('config')->dateFormat, $info[$key]) . " " . "(" . wireDate('relative', $info[$key]) . ")"; } return $info; }
/** * Render shortcodes. * * @param string $content The content to render. * @param array $options Options to be passed to the renderer. * @param null|Page $page Null or an instance of \Grav\Common\Page. * * @return string The modified contents. */ public function render($content, $options = [], $page = null) { // Build an anonymous function to pass to twig `render` method $function = function ($tag, $body, $arguments) use($options, $page) { if (isset($this->shortcodes[$tag])) { $options = isset($options[$tag]) ? $options[$tag] : []; } $event = new Event(['body' => $body, 'options' => new Data(array_replace_recursive($options, $arguments)), 'grav' => self::getGrav(), 'shortcodes' => $this, 'page' => $page, 'tag' => $tag]); return $event; }; // Wrapper for shortcodes filter function $filter_function = function ($name, $content, $context, $env) { return $this->filterShortcode($name, $content, $context, $env); }; // Process in-page shortcodes Twig $name = '@Shortcodes:' . $page->path(); $this->loader->setTemplate($name, $content); $vars = ['__shortcodes' => $function, '__shortcodes_filter' => $filter_function]; try { $page_default = $this->page; $this->page = $page; $output = $this->twig->render($name, $vars); } catch (\Twig_Error_Loader $e) { throw new \RuntimeException($e->getRawMessage(), 404, $e); } $shortcodes = isset($page->header()->shortcodes) ? $page->header()->shortcodes : []; if (isset($shortcodes['extra'])) { /** @var Cache $cache */ $cache = self::getGrav()['cache']; $cache_id = md5('shortcodes' . $page->id() . $cache->getKey()); $cache->save($cache_id, $shortcodes['extra']); } $this->page = $page_default; return $output; }
/** * Given a Selectors object or a selector string, return whether this Page matches it * * @param Page $page * @param string|Selectors $s * @return bool * */ public function matches(Page $page, $s) { if (is_string($s) || is_int($s)) { if (ctype_digit("{$s}")) { $s = (int) $s; } if (is_string($s)) { // exit early for simple path comparison if (substr($s, 0, 1) == '/' && $page->path() == rtrim($s, '/') . '/') { return true; } if (!Selectors::stringHasOperator($s)) { return false; } $selectors = new Selectors($s); } else { if (is_int($s)) { // exit early for simple ID comparison return $page->id == $s; } } } else { if ($s instanceof Selectors) { $selectors = $s; } else { return false; } } $matches = false; foreach ($selectors as $selector) { $name = $selector->field; if (in_array($name, array('limit', 'start', 'sort', 'include'))) { continue; } $matches = true; $value = $page->getUnformatted($name); if (is_object($value)) { // if the current page value resolves to an object if ($value instanceof Page) { // if it's a Page, get both the ID and path as allowed comparison values $value = array($value->id, $value->path); } else { if ($value instanceof PageArray) { // if it's a PageArray, then get the ID and path of all of them // @todo add support for @ selectors $_value = array(); foreach ($value as $v) { $_value[] = $v->id; $_value[] = $v->path; } $value = $_value; } else { if ($value instanceof Template) { $value = array($value->id, $value->name); } else { // otherwise just get the string value of the object $value = "{$value}"; } } } } else { if (is_array($value)) { // ok: selector matches will accept an array } else { // convert to a string value, whatever it may be $value = "{$value}"; } } if (!$selector->matches($value)) { $matches = false; break; } } return $matches; }