From: Vincent Vanwaelscappel Date: Mon, 28 Aug 2023 18:04:46 +0000 (+0200) Subject: wait #6201 @10.5 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=81065470e85d72ae3cdc25ea50297e801f8414f0;p=fluidbook-toolbox.git wait #6201 @10.5 --- diff --git a/app/Fluidbook/Compiler/Articles.php b/app/Fluidbook/Compiler/Articles.php new file mode 100644 index 000000000..8ec2c1102 --- /dev/null +++ b/app/Fluidbook/Compiler/Articles.php @@ -0,0 +1,324 @@ +fluidbookSettings->articlesFile; + + $this->lessVariables['articles-title-color'] = '#000000'; + $this->lessVariables['articles-font'] = 'Open Sans'; + + if (!$f || !file_exists($this->wdir . '/' . $f) || !is_file($this->wdir . '/' . $f)) { + return; + } + $f = $this->wdir . '/' . $f; + $mapFonts = ['OpenSans' => 'Open Sans']; + $this->addLess('articles'); + if ($this->fluidbookSettings->articlesStyle !== 'default') { + $this->lessVariables['articles-styles'] = $this->fluidbookSettings->articlesStyle; + } + $this->lessVariables['articles-font'] = $mapFonts[$this->fluidbookSettings->articlesFont] ?? $this->fluidbookSettings->articlesFont; + $fontPath = $this->addFontKit($this->fluidbookSettings->articlesFont); + + $list = $this->config->articlesList ?? []; + + $this->lessVariables['articles-title-color'] = '#565657'; + + $svg = ' + + '; + + $x = simplexml_load_string(file_get_contents($f)); + foreach ($x->xpath('/articles/article') as $k => $a) { + $dir = isset($a['dir']) ? (string)$a['dir'] : null; + $url = (string)$a['url']; + $id = (string)$a['id']; + $color = (string)$a['color']; + if (!$color) { + $color = '#000'; + } + + $specificStyles = '## h3, ## figure figcaption{background-color:' . $color . '}'; + $specificStyles .= '## .chapo, ## blockquote, ## a{color:' . $color . ';}'; + + $inner = '
'; + $inner .= '
'; + if ($this->fluidbookSettings->articlesShare && $this->fluidbookSettings->share) { + $inner .= ''; + } + $inner .= ''; + $inner .= '
'; + + $inner .= '
'; + + $title = ''; + $lead = ''; + $image = ''; + + $first = true; + + foreach ($a->children() as $child) { + if ($first) { + $first = false; + if ($child->getName() !== 'category') { + $inner .= '

 

'; + } + } + $inner .= $this->_articleToHTML($child, $title, $lead, $image, $dir); + } + $inner .= '
'; + + if (!$title) { + $title = 'Article sans titre ' . $k; + } + + if (!$id) { + $id = Text::str2URL($title); + } + + if (!$url) { + $url = $id . '.html'; + } + + $inner = str_replace(array('$id', '$url'), array($id, $url), $inner); + + $article = ['id' => $id, + 'url' => $url, + 'color' => $color, + 'contents' => '', + 'type' => 'xml']; + + $article['contents'] = $inner; + $content = ''; + $content .= ''; + $content .= ''; + $content .= ''; + $content .= ''; + $content .= ''; + $content .= $svg; + $content .= $inner; + $content .= ''; + $article['print'] = $content; + $list[] = $article; + + $this->addSEOArticle('#/article/' . $article['url'], $title, $lead, $image, $article['id'], $article['url'], $inner); + } + + if (isset($list)) { + $this->config->articlesList = $list; + } + } + + public function findArticleById($id) + { + foreach ($this->config->articlesList as $item) { + if ($item['id'] === $id) { + return $item; + } + } + return null; + } + + public function updateArticleById($id, $article) + { + foreach ($this->config->articlesList as $k => $item) { + if ($item['id'] === $id) { + $this->config->articlesList[$k] = $article; + break; + } + } + } + + public function writeArticles() + { + $list = $this->config->articlesList ?? []; + + $nb = count($list); + + usort($list, function ($a, $b) { + if ($a['page'] == $b['page']) { + $ea = explode('-', $a['id']); + $eb = explode('-', $b['id']); + if (is_numeric($ea[0]) && is_numeric($eb[0])) { + return $ea[0] - $eb[0]; + } + return strcmp($a['id'], $b['id']); + } + return $a['page'] - $b['page']; + }); + + foreach ($list as $k => $item) { + $nextIndex = ($k + 1) % $nb; + $prevIndex = ($k - 1 + $nb) % $nb; + $list[$k]['prev'] = $list[$prevIndex]['url']; + $list[$k]['next'] = $list[$nextIndex]['url']; + } + + + $idlist = []; + foreach ($list as $item) { + $idlist[$item['id']] = $item; + } + + $this->config->articlesList = $idlist; + } + + /** + * @param $child SimpleXMLElement + * @param $title + * @param $lead + * @param $image + * @return string|void + */ + protected function _articleToHTML($child, &$title, &$lead, &$image, $dir = null) + { + $markupMap = ['category' => 'h3', + 'subtitle' => 'h2', + 'legend' => 'figcaption', + 'title' => 'h1', + 'lead' => 'div.chapo', + 'paragraph' => 'p', + 'note' => 'div.note', + 'quote' => 'blockquote', + 'signature' => 'div.author', + 'intertitle' => 'h2.inter', + 'bigfont' => 'h2.bigfont', + 'separator' => 'hr', + 'link' => 'a']; + + $attrsmap = ['a' => ['link' => 'href']]; + + $dirattr = ''; + if (isset($child['dir'])) { + $d = (string)$child['dir']; + if ($d !== $dir) { + $dirattr = ' dir="' . $d . '"'; + $dir = $d; + } + } + + $res = ''; + $tag = $child->getName(); + if ($tag === 'encadre') { + $res .= ''; + foreach ($child->children() as $sub) { + $res .= $this->_articleToHTML($sub, $a1, $a2, $a3, $dir); + } + $res .= ''; + } else if ($tag === 'youtube') { + $res .= '
'; + } else if ($tag === 'image') { + $srcattrs = ['href', 'src', 'file']; + $file = ''; + foreach ($srcattrs as $srcattr) { + if (isset($child[$srcattr])) { + $file = (string)$child[$srcattr]; + break; + } + } + if ($image === '') { + $image = 'articles/' . $file; + } + $filepath = $this->wdir . '/articles/' . $file; + $this->vdir->copy($filepath, 'data/articles/' . $file); + $legend = (string)$child; + $caption = $legend ? '
' . $legend . '
' : ''; + if (file_exists($filepath)) { + $dim = getimagesize($filepath); + } else { + $dim = [0 => 1024, 1 => 10]; + } + $res .= '' . $legend . '' . $caption . ''; + } else { + $c = trim($this->SimpleXMLElement_innerXML($child)); + if (!$c) { + return; + } + if ($title === '' && $tag === 'title') { + $title = $c; + } + if ($lead === '' && $tag === 'lead') { + $lead = $c; + } + $m = $markupMap[$tag] ?? $tag; + $e = explode('.', $m); + $markup = $e[0]; + $attrs = $dirattr; + if (count($e) === 2) { + $attrs .= ' class="' . $e[1] . '"'; + } + if ($m === 'a') { + $attrs .= ' target="_blank"'; + } + foreach ($child->attributes() as $name => $v) { + $n = $attrsmap[$m][$name] ?? $name; + $attrs .= ' ' . $n . '="' . htmlspecialchars($v) . '"'; + } + $res .= '<' . $markup . $attrs . '>' . $c . ''; + } + return $res; + } + + function writeSEO() + { + foreach ($this->seoArticles as $seoArticle) { + if ($this->hybrid) { + $html = file_get_contents($this->assets . '/_seohybrid.html'); + } else { + $html = file_get_contents($this->assets . '/_seo.html'); + } + $a = $seoArticle; + unset($a['image']); + $a['imageurl'] = SocialImage::socialImageURL($this->getFluidbook()->cid, $seoArticle['image']); + $dim = SocialImage::getSocialImageSize($this->getFluidbook(), $seoArticle['image']); + if (null !== $dim) { + $a['imagewidth'] = $dim[0]; + $a['imageheight'] = $dim[1]; + } + foreach ($a as $k => $v) { + $html = str_replace('$' . $k, $v, $html); + } + $this->vdir->file_put_contents('p/' . $seoArticle['url'], $html); + } + $this->seo = new Document($this); + } + + public function addSEOArticle($page, $title, $intro, $image, $id = null, $url = null, $content = '') + { + if (null === $url) { + $url = Text::str2URL($title) . '.html'; + } + if (null === $id) { + $id = $title; + } + + $this->seoArticles[$id] = ['title' => $title, 'description' => $intro, 'image' => $image, 'content' => $content, 'page' => $page, 'url' => $url, 'id' => $id]; + } + + public function SimpleXMLElement_innerXML($xml) + { + $innerXML = ''; + foreach (dom_import_simplexml($xml)->childNodes as $child) { + $innerXML .= $child->ownerDocument->saveXML($child); + } + return $innerXML; + } +} diff --git a/app/Fluidbook/Compiler/Compiler.php b/app/Fluidbook/Compiler/Compiler.php index 1e8183dcc..a0c7d3f85 100644 --- a/app/Fluidbook/Compiler/Compiler.php +++ b/app/Fluidbook/Compiler/Compiler.php @@ -51,6 +51,7 @@ class Compiler extends Base implements CompilerInterface use Secure; use Images; use Sound; + use Articles; protected static $uaPrefixes = array('-moz-', '-webkit-', '-o-', '-ms-', ''); @@ -238,7 +239,7 @@ class Compiler extends Base implements CompilerInterface public $logfp = null; public $logtime = null; public $beginBody = array(); - public $seoArticles = []; + public $securityPolicyWhitelist = ['*.google-analytics.com', '*.youtube.com', '*.ytimg.com', '*.googletagmanager.com']; public $writeLinksData = false; public $content_lock = []; @@ -416,9 +417,9 @@ class Compiler extends Base implements CompilerInterface public function pushSetting($key, $value) { - $v = $this->fluidbookSettings->get($key, []); + $v = $this->config->$key ?? []; $v[] = $value; - $this->fluidbookSettings->set($key, $v); + $this->config->$key = $v; } @@ -1236,29 +1237,6 @@ class Compiler extends Base implements CompilerInterface } } - function writeSEO() - { - foreach ($this->seoArticles as $seoArticle) { - if ($this->hybrid) { - $html = file_get_contents($this->assets . '/_seohybrid.html'); - } else { - $html = file_get_contents($this->assets . '/_seo.html'); - } - $a = $seoArticle; - unset($a['image']); - $a['imageurl'] = SocialImage::socialImageURL($this->getFluidbook()->cid, $seoArticle['image']); - $dim = SocialImage::getSocialImageSize($this->getFluidbook(), $seoArticle['image']); - if (null !== $dim) { - $a['imagewidth'] = $dim[0]; - $a['imageheight'] = $dim[1]; - } - foreach ($a as $k => $v) { - $html = str_replace('$' . $k, $v, $html); - } - $this->vdir->file_put_contents('p/' . $seoArticle['url'], $html); - } - $this->seo = new Document($this); - } public function addContentLock($page, $unlockConditions = '') { @@ -1482,17 +1460,6 @@ class Compiler extends Base implements CompilerInterface $this->addLess('slideshow/' . $l); } - public function addSEOArticle($page, $title, $intro, $image, $id = null, $url = null, $content = '') - { - if (null === $url) { - $url = Text::str2URL($title) . '.html'; - } - if (null === $id) { - $id = $title; - } - - $this->seoArticles[$id] = ['title' => $title, 'description' => $intro, 'image' => $image, 'content' => $content, 'page' => $page, 'url' => $url, 'id' => $id]; - } public function _sortLinks($a, $b) { @@ -2522,284 +2489,6 @@ class Compiler extends Base implements CompilerInterface return $path . '/font.css'; } - - public function SimpleXMLElement_innerXML($xml) - { - $innerXML = ''; - foreach (dom_import_simplexml($xml)->childNodes as $child) { - $innerXML .= $child->ownerDocument->saveXML($child); - } - return $innerXML; - } - - public function writeXMLArticles() - { - $f = $this->fluidbookSettings->articlesFile; - - $this->lessVariables['articles-title-color'] = '#000000'; - $this->lessVariables['articles-font'] = 'Open Sans'; - - if (!$f || !file_exists($this->wdir . '/' . $f) || !is_file($this->wdir . '/' . $f)) { - return; - } - $f = $this->wdir . '/' . $f; - $mapFonts = ['OpenSans' => 'Open Sans']; - $this->addLess('articles'); - if ($this->fluidbookSettings->articlesStyle !== 'default') { - $this->lessVariables['articles-styles'] = $this->fluidbookSettings->articlesStyle; - } - $this->lessVariables['articles-font'] = $mapFonts[$this->fluidbookSettings->articlesFont] ?? $this->fluidbookSettings->articlesFont; - $fontPath = $this->addFontKit($this->fluidbookSettings->articlesFont); - - $list = $this->config->articlesList ?? []; - - - $this->lessVariables['articles-title-color'] = '#565657'; - - - $svg = ' - - '; - - - $x = simplexml_load_string(file_get_contents($f)); - foreach ($x->xpath('/articles/article') as $k => $a) { - $dir = isset($a['dir']) ? (string)$a['dir'] : null; - $url = (string)$a['url']; - $id = (string)$a['id']; - $color = (string)$a['color']; - if (!$color) { - $color = '#000'; - } - - $specificStyles = '## h3, ## figure figcaption{background-color:' . $color . '}'; - $specificStyles .= '## .chapo, ## blockquote, ## a{color:' . $color . ';}'; - - $inner = '
'; - $inner .= '
'; - if ($this->fluidbookSettings->articlesShare && $this->fluidbookSettings->share) { - $inner .= ''; - } - $inner .= ''; - $inner .= '
'; - - $inner .= '
'; - - $title = ''; - $lead = ''; - $image = ''; - - $first = true; - - foreach ($a->children() as $child) { - if ($first) { - $first = false; - if ($child->getName() !== 'category') { - $inner .= '

 

'; - } - } - $inner .= $this->_articleToHTML($child, $title, $lead, $image, $dir); - } - $inner .= '
'; - - if (!$title) { - $title = 'Article sans titre ' . $k; - } - - if (!$id) { - $id = Text::str2URL($title); - } - - if (!$url) { - $url = $id . '.html'; - } - - $inner = str_replace(array('$id', '$url'), array($id, $url), $inner); - - $article = ['id' => $id, - 'url' => $url, - 'color' => $color, - 'contents' => '', - 'type' => 'xml']; - - $article['contents'] = $inner; - $content = ''; - $content .= ''; - $content .= ''; - $content .= ''; - $content .= ''; - $content .= ''; - $content .= $svg; - $content .= $inner; - $content .= ''; - $article['print'] = $content; - $list[] = $article; - - $this->addSEOArticle('#/article/' . $article['url'], $title, $lead, $image, $article['id'], $article['url'], $inner); - } - - if (isset($list)) { - $this->config->articlesList = $list; - } - } - - public function findArticleById($id) - { - foreach ($this->config->articlesList as $item) { - if ($item['id'] === $id) { - return $item; - } - } - return null; - } - - public function updateArticleById($id, $article) - { - foreach ($this->config->articlesList as $k => $item) { - if ($item['id'] === $id) { - $this->config->articlesList[$k] = $article; - break; - } - } - } - - public function writeArticles() - { - $list = $this->config->articlesList ?? []; - - $nb = count($list); - - usort($list, function ($a, $b) { - if ($a['page'] == $b['page']) { - $ea = explode('-', $a['id']); - $eb = explode('-', $b['id']); - if (is_numeric($ea[0]) && is_numeric($eb[0])) { - return $ea[0] - $eb[0]; - } - return strcmp($a['id'], $b['id']); - } - return $a['page'] - $b['page']; - }); - - foreach ($list as $k => $item) { - $nextIndex = ($k + 1) % $nb; - $prevIndex = ($k - 1 + $nb) % $nb; - $list[$k]['prev'] = $list[$prevIndex]['url']; - $list[$k]['next'] = $list[$nextIndex]['url']; - } - - - $idlist = []; - foreach ($list as $item) { - $idlist[$item['id']] = $item; - } - - $this->config->articlesList = $idlist; - } - - /** - * @param $child SimpleXMLElement - * @param $title - * @param $lead - * @param $image - * @return string|void - */ - protected function _articleToHTML($child, &$title, &$lead, &$image, $dir = null) - { - $markupMap = ['category' => 'h3', - 'subtitle' => 'h2', - 'legend' => 'figcaption', - 'title' => 'h1', - 'lead' => 'div.chapo', - 'paragraph' => 'p', - 'note' => 'div.note', - 'quote' => 'blockquote', - 'signature' => 'div.author', - 'intertitle' => 'h2.inter', - 'bigfont' => 'h2.bigfont', - 'separator' => 'hr', - 'link' => 'a']; - - $attrsmap = ['a' => ['link' => 'href']]; - - $dirattr = ''; - if (isset($child['dir'])) { - $d = (string)$child['dir']; - if ($d !== $dir) { - $dirattr = ' dir="' . $d . '"'; - $dir = $d; - } - } - - $res = ''; - $tag = $child->getName(); - if ($tag === 'encadre') { - $res .= ''; - foreach ($child->children() as $sub) { - $res .= $this->_articleToHTML($sub, $a1, $a2, $a3, $dir); - } - $res .= ''; - } else if ($tag === 'youtube') { - $res .= '
'; - } else if ($tag === 'image') { - $srcattrs = ['href', 'src', 'file']; - $file = ''; - foreach ($srcattrs as $srcattr) { - if (isset($child[$srcattr])) { - $file = (string)$child[$srcattr]; - break; - } - } - if ($image === '') { - $image = 'articles/' . $file; - } - $filepath = $this->wdir . '/articles/' . $file; - $this->vdir->copy($filepath, 'data/articles/' . $file); - $legend = (string)$child; - $caption = $legend ? '
' . $legend . '
' : ''; - if (file_exists($filepath)) { - $dim = getimagesize($filepath); - } else { - $dim = [0 => 1024, 1 => 10]; - } - $res .= '' . $legend . '' . $caption . ''; - } else { - $c = trim($this->SimpleXMLElement_innerXML($child)); - if (!$c) { - return; - } - if ($title === '' && $tag === 'title') { - $title = $c; - } - if ($lead === '' && $tag === 'lead') { - $lead = $c; - } - $m = $markupMap[$tag] ?? $tag; - $e = explode('.', $m); - $markup = $e[0]; - $attrs = $dirattr; - if (count($e) === 2) { - $attrs .= ' class="' . $e[1] . '"'; - } - if ($m === 'a') { - $attrs .= ' target="_blank"'; - } - foreach ($child->attributes() as $name => $v) { - $n = $attrsmap[$m][$name] ?? $name; - $attrs .= ' ' . $n . '="' . htmlspecialchars($v) . '"'; - } - $res .= '<' . $markup . $attrs . '>' . $c . ''; - } - return $res; - } - public static function getPhonegapVersion($v = 'latest') { if ($v != 'latest') {