]> _ Git - cubeextranet.git/commitdiff
wait #2116 @0.5
authorvincent@cubedesigners.com <vincent@cubedesigners.com@f5622870-0f3c-0410-866d-9cb505b7a8ef>
Fri, 20 Jul 2018 13:08:55 +0000 (13:08 +0000)
committervincent@cubedesigners.com <vincent@cubedesigners.com@f5622870-0f3c-0410-866d-9cb505b7a8ef>
Fri, 20 Jul 2018 13:08:55 +0000 (13:08 +0000)
inc/ws/Util/html5/advancedseo/class.ws.html5.compiler.php
inc/ws/Util/html5/master/class.ws.html5.compiler.php
inc/ws/Util/html5/master/class.ws.html5.seo.php [new file with mode: 0644]

index 3be2a1f4915a77ad2813fe0cbfcade52f1946473..5bf08d9d2a18ea4ece55e23636a43b50ec0bb924 100644 (file)
@@ -1157,6 +1157,7 @@ class wsHTML5Compiler
                        switch ($this->config->basketManager) {
                                case 'Remarkable':
                                        $this->addJs('js/libs/parsley.min.js');
+                                       $this->addJs('js/libs/jquery/jquery.cookie.js');
                                        $this->addJs('js/libs/fluidbook/cart/fluidbook.cart.remarkable.js');
                                        break;
                                default:
@@ -1818,7 +1819,7 @@ class wsHTML5Compiler
                $res = array('width' => 0, 'height' => 0);
                if (file_exists($d . '/index.html')) {
                        $doc = new DOMDocument();
-                       $doc->loadHTMLFile($d . '/index.html');
+                       @$doc->loadHTMLFile($d . '/index.html');
                        $xpath = new DOMXPath($doc);
                        $c = $xpath->query("//canvas");
                        foreach ($c as $canvas) {
index c2206d3106f72b42d47dfd5bcedb1ea85e366740..ed57b637d0cddea6b1d0a78c036c0223dc33c8fb 100644 (file)
@@ -2,7 +2,7 @@
 
 class wsHTML5Compiler
 {
-       protected static $resolutions = array(150, 300);
+       public static $resolutions = array(150, 300);
        public $maxRes = 300;
 
        public $jsFiles = array(
@@ -98,17 +98,17 @@ class wsHTML5Compiler
        public $pluginCSS = array();
        public $pluginJs = array();
        public $htmlmultimedia = array();
-       protected $cssX = array();
-       protected $cssY = array();
-       protected $cssWidths = array();
-       protected $pdf2htmlRatio;
-       protected $scale;
-       protected $multiply;
-       protected $div = array();
-       protected $numerotation;
-       protected $fontDocs = array();
-       protected $dir;
-       protected $z = 3;
+       public $cssX = array();
+       public $cssY = array();
+       public $cssWidths = array();
+       public $pdf2htmlRatio;
+       public $scale;
+       public $multiply;
+       public $div = array();
+       public $numerotation;
+       public $fontDocs = array();
+       public $dir;
+       public $z = 3;
        public $vdir;
        public $wdir;
 
@@ -117,51 +117,55 @@ class wsHTML5Compiler
         * @var wsBook
         */
        public $book;
-       protected $pages;
-       protected $theme;
+       public $pages;
+       public $theme;
        public $version;
        public $book_id;
-       protected $themeRoot;
+       public $themeRoot;
 
        /**
         *
         * @var wsDAOBook
         */
-       protected $daoBook;
-       protected $needToRecompileContents = true;
-       protected $needToRecompileSettings = true;
+       public $daoBook;
+       public $needToRecompileContents = true;
+       public $needToRecompileSettings = true;
        public $width;
        public $height;
-       protected $cssWidth;
-       protected $cssHeight;
-       protected $cssOneWidth;
-       protected $cssOneHeight;
-       protected $cssScale;
-       protected $cssSVGScale;
-       protected $optimalWidth = 567;
-       protected $optimalHeight = 709;
-       protected $additionalConfig = array();
-       protected $fontScale = 1;
-       protected $cache = array();
-       protected $backgroundsPrefix = array();
-       protected $svg = true;
-       protected $config = array();
-       protected $assets = '';
-       protected $phonegap = false;
-       protected $phonegapVersion;
-       protected $standalone = false;
-       protected $hiddenContents = array();
-       protected $appcache;
-       protected $home;
-       protected $widget = true;
-       protected $multiApp = false;
-       protected $pageLabels = array();
-       protected $stylesheets = array();
-       protected $logfp = null;
-       protected $logtime = null;
-       protected $beginBody = array();
-       protected $seoArticles = [];
-       protected $securityPolicyWhitelist = ['*.google-analytics.com', '*.youtube.com', '*.ytimg.com'];
+       public $cssWidth;
+       public $cssHeight;
+       public $cssOneWidth;
+       public $cssOneHeight;
+       public $cssScale;
+       public $cssSVGScale;
+       public $optimalWidth = 567;
+       public $optimalHeight = 709;
+       public $additionalConfig = array();
+       public $fontScale = 1;
+       public $cache = array();
+       public $backgroundsPrefix = array();
+       public $svg = true;
+       public $config = array();
+       public $assets = '';
+       public $phonegap = false;
+       public $phonegapVersion;
+       public $standalone = false;
+       public $hiddenContents = array();
+       public $appcache;
+       public $home;
+       public $widget = true;
+       public $multiApp = false;
+       public $pageLabels = array();
+       public $stylesheets = array();
+       public $logfp = null;
+       public $logtime = null;
+       public $beginBody = array();
+       public $seoArticles = [];
+       public $securityPolicyWhitelist = ['*.google-analytics.com', '*.youtube.com', '*.ytimg.com'];
+       /**
+        * @var wsHTML5Seo
+        */
+       public $seo = null;
 
 
        function __construct($book_id, $version = 'stable', $phonegap = false, $phonegapVersion = 'latest', $dir = null, $standalone = false, $appcache = false, $home = false)
@@ -357,7 +361,9 @@ class wsHTML5Compiler
                $this->log('CSS written');
                $this->writeLangs();
                $this->log('Langs written');
+               $this->writeSEO();
                $this->writeIndex();
+               $this->writeWidget();
                $this->log('Index written');
                $this->writeSounds();
                $this->log('Sound written');
@@ -367,7 +373,6 @@ class wsHTML5Compiler
                $this->log('Extras written');
                $this->writeJs();
                $this->log('Js written');
-               $this->writeSEO();
                $this->vdir->sync($delete);
                $this->log('Files Synced');
        }
@@ -473,12 +478,12 @@ class wsHTML5Compiler
                $this->vdir->copyDirectory(WS_SOUNDS . '/' . $this->book->parametres->soundTheme, 'data/sounds');
        }
 
-       protected function writeIndex()
+       protected function _writeIndex($page)
        {
                global $core;
 
-               $html = file_get_contents($this->assets . '/_index.html');
-               $uhtml = $html;
+               $seo = $this->seo->pages[$page];
+               $html = $seo->getHTML();
 
                $titre = $this->book->parametres->title;
 
@@ -501,19 +506,6 @@ class wsHTML5Compiler
 
                $bgcolor = $this->theme->parametres->loadingBackColor;
 
-               // Google analytics
-               $ga = '';
-               if ($this->book->parametres->googleAnalytics != '') {
-                       $ga = cubePage::googleAnalytics($this->book->parametres->googleAnalytics);
-               }
-               if ($this->book->parametres->googleAnalyticsCustom != '') {
-                       $ga .= $this->book->parametres->googleAnalyticsCustom;
-               }
-
-               $statsfooter = '';
-               if ($this->book->parametres->statsCustom != '') {
-                       $statsfooter = $this->book->parametres->statsCustom;
-               }
                // Feuilles de style
                $sheets = array_merge($this->stylesheets, $this->specialCSS);
 
@@ -536,7 +528,7 @@ class wsHTML5Compiler
                        $iscript .= '</script>' . "\n";
                }
 
-               $script = '';
+               $script = '<script type="text/javascript">var FLUIDBOOK_START_PAGE="' . $page . '";</script>' . "\n";
                $script .= '<script type="text/javascript" charset="utf-8" src="data/datas.js"></script>' . "\n";
                $script .= '<script type="text/javascript" charset="utf-8" src="data/fluidbook.js"></script>' . "\n";
                if ($this->book->parametres->scorm_enable) {
@@ -550,7 +542,6 @@ class wsHTML5Compiler
                        $script .= '<script type="text/javascript" charset="utf-8" src="' . $p . '"></script>' . "\n";
                }
                $script .= $iscript;
-               $description = '<meta name="description" content="' . html::escapeHTML(isset($this->book->parametres->seoDescription) && $this->book->parametres->seoDescription ? $this->book->parametres->seoDescription : $titre . ' - Powered by Fluidbook') . '">';
 
                $socialTitle = $this->book->parametres->facebook_title ? $this->book->parametres->facebook_title : $titre;
                $socialDescription = $this->book->parametres->seoDescription ? $this->book->parametres->seoDescription : $this->book->parametres->seoDescription;
@@ -629,7 +620,7 @@ class wsHTML5Compiler
                if ($this->phonegap) {
                        $csp = "<meta http-equiv=\"Content-Security-Policy\" content=\"default-src 'self' data: gap: 'unsafe-inline' *; style-src 'self' 'unsafe-inline'; font-src 'self' data:; script-src 'self' 'unsafe-inline' 'unsafe-eval' " . implode(' ', array_unique($this->securityPolicyWhitelist)) . "; img-src * data:\">";
                }
-               $vars = array('titre', 'credits', 'statsfooter', 'ga', 'style', 'script', 'pagesContents', 'description', 'print', 'hiddenContents', 'splash', 'cache', 'bgcolor', 'message', 'favicon', 'svg', 'beginbody', 'csp', 'opengraph', 'twittercard');
+               $vars = array('credits', 'style', 'script', 'pagesContents', 'print', 'hiddenContents', 'splash', 'cache', 'bgcolor', 'message', 'favicon', 'svg', 'beginbody', 'csp', 'opengraph', 'twittercard');
                foreach ($vars as $v) {
                        if (isset($$v)) {
                                $html = str_replace('<!-- $' . $v . ' -->', $$v, $html);
@@ -638,45 +629,21 @@ class wsHTML5Compiler
                        }
                }
 
-               $scripts = array();
-               foreach ($this->debugJsFiles as $js) {
-                       $scripts[] = '<script type="text/javascript" charset="utf-8" src="' . $js . '"></script>';
-               }
-               foreach ($this->jsFiles as $js) {
-                       $scripts[] = '<script type="text/javascript" charset="utf-8" src="' . $js . '"></script>';
-               }
-               foreach ($this->pluginJs as $js) {
-                       $scripts[] = '<script type="text/javascript" charset="utf-8" src="' . $js . '"></script>';
+               $seo->writePage($html, $this->vdir);
+               if ($page == 1) {
+                       $seo->writePage($html, $this->vdir, 'index.html');
                }
-
-               $scripts[] = '<script type="text/javascript" charset="utf-8" src="data/datas.js"></script>';
-               $scripts[] = $iscript;
-               $script = implode("\n\t\t", $scripts);
-
-               $scripts = array();
-               foreach ($this->testJsFiles as $js) {
-                       $scripts[] = '<script type="text/javascript" charset="utf-8" src="' . $js . '"></script>';
                }
-               $scripts[] = '<script type="text/javascript" charset="utf-8" src="data/datas.js"></script>';
-               $script_test = implode("\n\t\t", $scripts);
-
-               $thtml = $uhtml;
 
-               $vars = array('titre', 'credits', 'statsfooter', 'ga', 'style', 'script', 'pagesContents', 'print', 'hiddenContents', 'splash', 'cache', 'bgcolor', 'message', 'favicon');
-               foreach ($vars as $v) {
-                       $uhtml = str_replace('<!-- $' . $v . ' -->', $$v, $uhtml);
+       protected function writeIndex()
+       {
+               foreach ($this->pages as $page => $infos) {
+                       $this->_writeIndex($page);
                }
-
-               $script .= "\n\t\t" . '<script type="text/javascript" charset="utf-8">window.tester = true;</script>';
-               $vars = array('titre', 'credits', 'statsfooter', 'ga', 'style', 'script', 'print', 'hiddenContents', 'splash', 'message');
-               foreach ($vars as $v) {
-                       $thtml = str_replace('<!-- $' . $v . ' -->', $$v, $thtml);
                }
 
-               $this->vdir->file_put_contents('index.html', $html);
-               $this->vdir->file_put_contents('indexu.html', $uhtml);
-               $this->vdir->file_put_contents('indext.html', $thtml);
-
+       protected function writeWidget()
+       {
                // Write widget html
                if ($this->widget) {
                        $whtml = file_get_contents($this->assets . '/widget.html');
@@ -710,6 +677,7 @@ class wsHTML5Compiler
                        }
                        $this->vdir->file_put_contents('p/' . $seoArticle['url'], $html);
                }
+               $this->seo = new wsHTML5Seo($this);
        }
 
        protected function writeScorm()
diff --git a/inc/ws/Util/html5/master/class.ws.html5.seo.php b/inc/ws/Util/html5/master/class.ws.html5.seo.php
new file mode 100644 (file)
index 0000000..4890796
--- /dev/null
@@ -0,0 +1,218 @@
+<?php\r
+\r
+class wsHTML5Seo\r
+{\r
+       /**\r
+        * @var wsHTML5Compiler\r
+        */\r
+       public $compiler;\r
+       public $pages;\r
+       protected $_nav = null;\r
+\r
+\r
+       public function __construct($compiler)\r
+       {\r
+               $this->compiler = $compiler;\r
+               // Define default seo contents\r
+               $this->pages = [];\r
+\r
+               foreach ($compiler->pages as $page => $infos) {\r
+                       $p = new wsHTML5SeoPage($this);\r
+                       $p->page = $page;\r
+                       $p->title = $this->_getPageLabel($page);\r
+                       $p->text = $this->getTextContent($infos);\r
+                       $p->description = $this->compiler->book->parametres->seoDescription ? $this->compiler->book->parametres->seoDescription : $this->compiler->book->parametres->title . ' - Powered by Fluidbook';\r
+                       $p->keywords = $this->compiler->book->parametres->seoKeywords;\r
+                       $p->robots = $this->compiler->book->parametres->seoRobots ? 'index,follow' : 'noindex,nofollow';\r
+\r
+                       // Google analytics\r
+                       $p->ua = '';\r
+                       if ($this->compiler->book->parametres->googleAnalytics != '') {\r
+                               $p->ua .= cubePage::googleAnalytics($this->compiler->book->parametres->googleAnalytics);\r
+                       }\r
+                       if ($this->compiler->book->parametres->googleAnalyticsCustom != '') {\r
+                               $p->ua .= $this->compiler->book->parametres->googleAnalyticsCustom;\r
+                       }\r
+\r
+                       if ($this->compiler->book->parametres->statsCustom != '') {\r
+                               $p->footer = $this->compiler->book->parametres->statsCustom;\r
+                       }\r
+\r
+                       $this->pages[$page] = $p;\r
+               }\r
+\r
+\r
+               if ($compiler->book->parametres->seoAdvanced) {\r
+                       $sheets = wsUtil::excelToArray($compiler->wdir . '/' . $compiler->book->parametres->seoAdvanced, true);\r
+                       foreach ($sheets as $sheet) {\r
+                               $a = $sheet;\r
+                               break;\r
+                       }\r
+                       $minPage = 100000000;\r
+                       foreach ($a as $item) {\r
+                               $minPage = min($minPage, $item['page']);\r
+                       }\r
+                       $offsetPage = -($minPage - 1);\r
+\r
+                       foreach ($a as $item) {\r
+                               $page = $item['page'] + $offsetPage;\r
+                               foreach ($item as $k => $v) {\r
+                                       if ($k == 'page') {\r
+                                               continue;\r
+                                       }\r
+                                       $this->pages[$page]->$k = $v;\r
+                               }\r
+                       }\r
+               }\r
+       }\r
+\r
+       public function getTextContent($infos)\r
+       {\r
+               switch ($this->compiler->book->parametres->textExtraction) {\r
+                       case 'poppler':\r
+                               $prefix = 'p';\r
+                               break;\r
+                       case 'fluidbook':\r
+                               $prefix = 'f';\r
+                               break;\r
+                       default:\r
+                               $prefix = '';\r
+                               break;\r
+               }\r
+\r
+               return file_get_contents(wsDocument::getDir($infos['document_id']) . $prefix . 'h' . $infos['document_page'] . '.txt');\r
+       }\r
+\r
+       public function _getPageLabel($page)\r
+       {\r
+               $res = $this->compiler->book->parametres->title;\r
+               foreach ($this->compiler->book->chapters as $chapter) {\r
+                       $p = $this->compiler->virtualToPhysical($chapter->page);\r
+                       if ($page < $p) {\r
+                               continue;\r
+                       }\r
+                       if ($page >= $p) {\r
+                               $res = $chapter->label;\r
+                       }\r
+               }\r
+               return $res;\r
+       }\r
+\r
+       public function getNav()\r
+       {\r
+               if (null == $this->_nav) {\r
+                       $this->_nav = '<nav>';\r
+                       foreach ($this->pages as $page => $p) {\r
+                               $this->_nav .= '<a href="' . $p->getUrl() . '">' . $p->title . '</a>';\r
+                       }\r
+                       $this->_nav .= '</nav>';\r
+               }\r
+               return $this->_nav;\r
+       }\r
+}\r
+\r
+class wsHTML5SeoPage\r
+{\r
+       public $page;\r
+       public $text;\r
+       public $title;\r
+       public $description;\r
+       public $url = null;\r
+       public $h1;\r
+       public $ua;\r
+       public $canonical;\r
+       public $prev;\r
+       public $next;\r
+       public $keywords;\r
+       public $robots;\r
+       public $footer;\r
+\r
+       /**\r
+        * @var wsHTML5Seo\r
+        */\r
+       public $_container;\r
+\r
+\r
+       public function __construct($container)\r
+       {\r
+               $this->_container = $container;\r
+       }\r
+\r
+       public function getHTML()\r
+       {\r
+               $compiler = $this->_container->compiler;\r
+               $html = file_get_contents($this->_container->compiler->assets . '/_index.html');\r
+\r
+               $vars = ['description' => $this->description ? '<meta name="description" content="' . $this->description . '">' : '',\r
+                       'keywords' => $this->keywords ? '<meta name="keywords" content="' . $this->keywords . '">' : '',\r
+                       'titre' => $this->title,\r
+                       'canonical' => $this->canonical ? '<link rel="canonical" href="' . $this->canonical . '">' : '',\r
+                       'prev' => $this->prev ? '<meta name="prev" href="' . $this->prev . '">' : '',\r
+                       'next' => $this->next ? '<meta name="next" href="' . $this->next . '">' : '',\r
+                       'robots' => $this->robots ? '<meta name="robots" content="' . $this->robots . '">' : '',\r
+                       'statsfooter' => $this->footer,\r
+                       'ga' => $this->ua,\r
+                       'seoContent' => $this->getSEOContent()];\r
+\r
+               foreach ($vars as $k => $var) {\r
+                       $html = str_replace('<!-- $' . $k . ' -->', $var, $html);\r
+               }\r
+\r
+               return $html;\r
+       }\r
+\r
+       public function getSEOContent()\r
+       {\r
+               $res = '';\r
+               if (null !== $this->h1) {\r
+                       $res .= '<h1>' . htmlentities($this->h1) . '</h1>';\r
+               }\r
+               $res .= $this->text;\r
+               $res .= $this->_container->getNav();\r
+               return $res;\r
+       }\r
+\r
+       public function getURL()\r
+       {\r
+               if (null === $this->url) {\r
+                       return $this->page . '-' . CubeIT_Text::str2URL($this->title) . '.html';\r
+               }\r
+               return $this->url;\r
+       }\r
+\r
+       public function getHTMLRelativePath()\r
+       {\r
+               $url = $this->getURL();\r
+               if (null == $this->url) {\r
+                       $res = 'p/' . $url;\r
+               }\r
+\r
+               if ($this->_container->compiler->book->parametres->seoBaseURL == '') {\r
+                       $res = $url;\r
+               } else {\r
+                       $res = str_replace($this->_container->compiler->book->parametres->seoBaseURL, '', $url);\r
+               }\r
+\r
+               return ltrim($res, '/');\r
+       }\r
+\r
+       /**\r
+        * @param string $html\r
+        * @param CubeIT_Files_VirtualDirectory $vdir\r
+        * @param null|string $path\r
+        */\r
+       public function writePage($html, $vdir, $path = null)\r
+       {\r
+               if ($path == null) {\r
+                       $path = $this->getHTMLRelativePath();\r
+               }\r
+               $relativeLevel = count(explode('/', rtrim($path, "/"))) - 1;\r
+               $base = '';\r
+               if ($relativeLevel > 0) {\r
+                       $base = '<base href="' . str_repeat('../', $relativeLevel) . '" >';\r
+               }\r
+               $html = str_replace('<!-- $base -->', $base, $html);\r
+\r
+               $vdir->file_put_contents($path, $html);\r
+       }\r
+}
\ No newline at end of file