Exemple #1
0
function smarty_function_linkup_js($params, Smarty_Internal_Template &$smarty)
{
    $params = ArrayAdapter::inst($params);
    $dir = $params->str('dir');
    $name = $params->str('name');
    $di = DirManager::resources('scripts')->getDirItem($dir, $name, 'js');
    echo $di->isFile() ? PsHtml::linkJs($di) : '';
}
Exemple #2
0
 private function buildJsDefs(PageParams $params, PageContext $ctxt)
 {
     $JS_CLASS_CONSTS = PsUtil::getClassConsts('PsConstJs');
     $JS_CONSTS = $this->jsConsts();
     $JS_COMMON = $this->jsCommon($ctxt);
     $JS_PAGE = $params->getJsParams();
     $defs = json_encode(array_merge($JS_CONSTS, $JS_COMMON, $JS_PAGE));
     $const = json_encode($JS_CLASS_CONSTS);
     $defs = "var defs={$defs}; var CONST={$const};";
     $defs = PsHtml::linkJs(null, $defs);
     $this->LOGGER->infoBox('JS DEFS', $defs);
     return $defs;
 }
 private function finaliseImpl(simple_html_dom &$html)
 {
     // Удаляем комментарии
     foreach ($html->find('comment') as $e) {
         $e->outertext = '';
     }
     $head = $html->find('head', 0);
     // Если нет тега <head> - пропускаем обработку
     if ($head) {
         $tmp;
         $resources = '';
         $linked = array();
         //$jsToObfuscate = array();
         foreach ($html->find('link, script') as $e) {
             switch ($e->tag) {
                 case 'link':
                     // <link ...>
                     $tmp = $e->outertext;
                     if (!array_key_exists($tmp, $linked)) {
                         $linked[$tmp] = true;
                         $resources .= $tmp;
                         $this->LOGGER->info($tmp);
                     }
                     break;
                 case 'script':
                     // <script ...></script>
                     $tmp = PsHtml::linkJs(null, $e->innertext, $e->attr);
                     if (!array_key_exists($tmp, $linked)) {
                         $linked[$tmp] = true;
                         $this->LOGGER->info($tmp);
                     }
                     break;
             }
             $e->outertext = '';
         }
         $head->innertext .= $resources;
     }
 }
function smarty_function_linkup_js($params, Smarty_Internal_Template &$smarty)
{
    $di = DirItem::inst(array_get_value('dir', $params), array_get_value('name', $params), PsConst::EXT_JS);
    echo $di->isFile() ? PsHtml::linkJs($di) : '';
}
 protected function doFinalize($html)
 {
     /*
      * ИНИЦИАЛИЗАЦИЯ
      */
     $this->OBFUSCATABLE[] = DirManager::resources()->relDirPath('folded');
     $this->OBFUSCATABLE[] = DirManager::resources()->relDirPath('scripts/ps');
     //Расширим для предотвращения PREG_BACKTRACK_LIMIT_ERROR
     ini_set('pcre.backtrack_limit', 10 * 1000 * 1000);
     /*
      * НАЧАЛО РЫБОТЫ
      */
     /* Удалим комментарии */
     $pattern = "/<!--(.*?)-->/si";
     $html = preg_replace($pattern, '', $html);
     $resources = '';
     /*
      * JAVASCRIPT
      * <script ...>...</script>
      */
     $linked = array();
     $matches = array();
     $pattern = "/<script ([^>]*)>(.*?)<\\/script>/si";
     $cnt = preg_match_all($pattern, $html, $matches, PREG_PATTERN_ORDER);
     $attributes = $matches[1];
     $contents = $matches[2];
     for ($index = 0; $index < $cnt; $index++) {
         $attrs = $this->parseAttributesString($attributes[$index]);
         $content = trim($contents[$index]);
         if ($content) {
             //Есть содержимое, этот тег включаем сразу
             $tmp = PsHtml::linkJs(null, $content, $attrs);
             $resources .= $tmp . "\n";
             $this->LOGGER->info($tmp);
             continue;
         }
         $src = array_get_value('src', $attrs);
         if (!$src || in_array($src, $linked)) {
             //Нет пути или путь уже включён - пропускаем
             continue;
         }
         $linked[] = $src;
         $newSrc = $this->tryReplaceResource($src);
         if (!$newSrc) {
             if ($this->LOGGER->isEnabled()) {
                 $tmp = PsHtml::linkJs(null, null, $attrs);
                 $this->LOGGER->info($tmp . '  [EXCLUDED]');
             }
             continue;
         }
         $replaced = $src != $newSrc;
         if ($replaced) {
             $linked[] = $newSrc;
             $attrs['src'] = $newSrc;
         }
         $tmp = PsHtml::linkJs(null, null, $attrs);
         $resources .= $tmp . "\n";
         if ($this->LOGGER->isEnabled()) {
             $attrs['src'] = $src;
             $this->LOGGER->info(($replaced ? PsHtml::linkJs(null, null, $attrs) . '  [REPLACED]  ' : '') . $tmp);
         }
     }
     $html = preg_replace($pattern, '', $html);
     /*
      * FAVICON, CSS
      * <link .../>
      */
     /* Вырежем css и другие "линки" */
     $matches = array();
     $pattern = "/<link ([^>]*)\\/>/si";
     $cnt = preg_match_all($pattern, $html, $matches, PREG_PATTERN_ORDER);
     $attributes = $matches[1];
     for ($index = 0; $index < $cnt; $index++) {
         $attrs = $this->parseAttributesString($attributes[$index]);
         $src = array_get_value('href', $attrs);
         if (!$src || in_array($src, $linked)) {
             //Нет пути или путь уже включён - пропускаем
             continue;
         }
         $linked[] = $src;
         $newSrc = $this->tryReplaceResource($src);
         if (!$newSrc) {
             if ($this->LOGGER->isEnabled()) {
                 $tmp = PsHtml::linkLink($attrs);
                 $this->LOGGER->info($tmp . '  [EXCLUDED]');
             }
             continue;
         }
         $replaced = $src != $newSrc;
         if ($replaced) {
             $linked[] = $newSrc;
             $attrs['href'] = $newSrc;
         }
         $tmp = PsHtml::linkLink($attrs);
         $resources .= $tmp . "\n";
         if ($this->LOGGER->isEnabled()) {
             $attrs['href'] = $src;
             $this->LOGGER->info(($replaced ? PsHtml::linkLink($attrs) . '  [REPLACED]  ' : '') . $tmp);
         }
     }
     $html = preg_replace($pattern, '', $html);
     $resources = "\n" . trim($resources) . "\n";
     /*
      * Удалим пробелы
      */
     $matches = array();
     $cnt = preg_match("/<head>(.*?)<\\/head>/si", $html, $matches);
     if ($cnt == 1) {
         /**
          * $headOld - ресурсы страницы, находящиеся в блоке <head ...>...</head> и 
          * оставшиеся после вырезания .js и .css. Обычно там остаётся два блока:
          * <meta...>...</meta> и <title>...</title>
          */
         $headOld = $matches[1];
         $headNew = normalize_string($headOld);
         $headNew = "{$headNew} {$resources}";
         $html = str_replace_first($headOld, $headNew, $html);
     } else {
         //Вставляем ресурсы в <head>
         $html = str_replace_first('</head>', $resources . '</head>', $html);
     }
     $this->LOGGER->infoBox('PAGE FINALISED', $html);
     return $html;
 }
Exemple #6
0
 /**
  * Метод подключает ресурсы к сущности фолдинга.
  * Всегда подключаем все ресурсы, ненужные будут выкинуты в процессе финализации страницы.
  */
 public function getResourcesLinks($ident, $content = null)
 {
     $this->assertHasAccess($ident);
     $this->LOGGER->info("Getting resource links for entity [{$ident}].");
     $tokens = array();
     foreach ($this->RESOURCE_TYPES_LINKED as $type) {
         $di = $this->getResourceDi($ident, $type);
         switch ($type) {
             case self::RTYPE_JS:
                 $tokens[] = PsHtml::linkJs($di);
                 break;
             case self::RTYPE_CSS:
                 $tokens[] = PsHtml::linkCss($di);
                 break;
             case self::RTYPE_PCSS:
                 $tokens[] = PsHtml::linkCss($di, 'print');
                 break;
         }
     }
     //Приаттачим спрайты
     $sprite = $this->getSprite($ident);
     $tokens[] = $sprite ? PsHtml::linkCss($sprite->getCssDi()) : '';
     //Контент - после ресурсов
     $tokens[] = $content;
     return concat($tokens);
 }