Beispiel #1
0
 /**
  * Prepare and output the Javascript for the current page
  * @static
  */
 public static function GetHead_JS($scripts)
 {
     global $page, $config;
     $combine = $config['combinejs'] && !\gp\tool::loggedIn() && $page->pagetype !== 'admin_display';
     $scripts = self::GetHead_CDN('js', $scripts);
     //just local jquery
     if (!count($page->head_js) && count($scripts) === 1 && isset($scripts['jquery'])) {
         echo '<!-- jquery_placeholder ' . gp_random . ' -->';
         return;
     }
     if (!$combine || $page->head_force_inline) {
         echo "\n<script type=\"text/javascript\">\n";
         \gp\tool::jsStart();
         echo "\n</script>";
     }
     if (is_array($page->head_js)) {
         $scripts += $page->head_js;
         //other js files
     } else {
         trigger_error('$page->head_js is not an array');
     }
     self::CombineFiles($scripts, 'js', $combine);
 }
Beispiel #2
0
 /**
  * Generate a file with all of the combined content
  *
  */
 public static function GenerateFile($files, $type)
 {
     global $dataDir;
     //get etag
     $modified = $content_length = 0;
     $full_paths = array();
     foreach ($files as $file) {
         $full_path = self::CheckFile($file);
         if ($full_path === false) {
             continue;
         }
         self::FileStat_Static($full_path, $modified, $content_length);
         $full_paths[$file] = $full_path;
     }
     //check css imports
     if ($type == 'css') {
         $had_imported = false;
         $import_data = array();
         $imported_file = $dataDir . '/data/_cache/import_info.php';
         if (file_exists($imported_file)) {
             include_once $imported_file;
         }
         foreach ($full_paths as $file => $full_path) {
             if (!isset($import_data[$full_path])) {
                 continue;
             }
             $had_imported = true;
             foreach ($import_data[$full_path] as $imported_full) {
                 self::FileStat_Static($imported_full, $modified, $content_length);
             }
             unset($import_data[$full_path]);
         }
     }
     //check to see if file exists
     $etag = \gp\tool::GenEtag(json_encode($files), $modified, $content_length);
     $cache_relative = '/data/_cache/combined_' . $etag . '.' . $type;
     $cache_file = $dataDir . $cache_relative;
     if (file_exists($cache_file)) {
         // change modified time to extend cache
         if (time() - filemtime($cache_file) > 604800) {
             touch($cache_file);
         }
         return $cache_relative;
     }
     //create file
     if ($type == 'js') {
         ob_start();
         \gp\tool::jsStart();
         foreach ($full_paths as $full_path) {
             readfile($full_path);
             echo ";\n";
         }
         $combined_content = ob_get_clean();
     } else {
         $imports = $combined_content = '';
         $new_imported = array();
         foreach ($full_paths as $file => $full_path) {
             $temp = new \gp\tool\Output\CombineCss($file);
             $combined_content .= "\n/* " . $file . " */\n";
             $combined_content .= $temp->content;
             $imports .= $temp->imports;
             if (count($temp->imported)) {
                 $new_imported[$full_path] = $temp->imported;
             }
         }
         $combined_content = $imports . $combined_content;
         //save imported data
         if (count($new_imported) || $had_imported) {
             if (count($new_imported)) {
                 $import_data = $new_imported + $import_data;
             }
             \gp\tool\Files::SaveData($imported_file, 'import_data', $import_data);
         }
     }
     if (!\gp\tool\Files::Save($cache_file, $combined_content)) {
         return false;
     }
     \gp\admin\Tools::CleanCache();
     return $cache_relative;
 }