Ejemplo n.º 1
0
 /**
  * Calculate the sum of integer values (values can be any length)
  */
 function Summation($A)
 {
     $Arguments = func_get_args();
     if (is_array($A)) {
         $Arguments = Flatten($Arguments);
     }
     $MaxLengthArray = array_map('strlen', $Arguments);
     $MaxLength = max($MaxLengthArray);
     foreach ($Arguments as $Index => $Value) {
         settype($Value, 'string');
         $Arguments[$Index] = str_pad($Value, $MaxLength, '0', STR_PAD_LEFT);
     }
     //$Result = array_fill(0, $MaxLength, 0);
     for ($i = $MaxLength - 1; $i >= 0; $i--) {
         if (!isset($Result[$i])) {
             $Result[$i] = 0;
         }
         foreach ($Arguments as $Value) {
             $Result[$i] += (int) $Value[$i];
         }
         $Sum = strval($Result[$i]);
         $Length = strlen($Sum);
         for ($n = $Length - 1; $n >= 0; $n--) {
             $k = $i - ($Length - $n - 1);
             $Result[$k] = $Sum[$n];
         }
     }
     ksort($Result);
     $Result = implode('', $Result);
     return $Result;
 }
Ejemplo n.º 2
0
 function CombineArrays()
 {
     $Arrays = func_get_args();
     $Result = Flatten($Arrays);
     $Result = array_values(array_unique($Result));
     return $Result;
 }
Ejemplo n.º 3
0
 public function HeadModule_BeforeToString_Handler($Head)
 {
     $Configuration =& $this->Configuration;
     $Tags = $Head->Tags();
     usort($Tags, array('HeadModule', 'TagCmp'));
     // BeforeToString fires before sort
     $CombinedJavascript = array('library' => array());
     $CombinedCss = array();
     $RemoveIndex = array();
     $AllInOne = ArrayValue('AllInOne', $Configuration);
     $DeferJavaScript = ArrayValue('DeferJavaScript', $Configuration);
     foreach ($Tags as $Index => &$Tag) {
         // JavaScript (script tag)
         if (GetValue(HeadModule::TAG_KEY, $Tag) == 'script') {
             if (!isset($JsTag)) {
                 $JsTag = $Tag;
             }
             $CachedFilePath = $this->GetCachedFilePath($Tag, 'src', $FilePath);
             if ($CachedFilePath === False) {
                 if ($DeferJavaScript) {
                     $this->DeferJavaScriptFiles[] = $Tag['src'];
                     $RemoveIndex[] = $Index;
                 }
                 continue;
             }
             if (!file_exists($CachedFilePath)) {
                 if (!isset($Snoopy)) {
                     $Snoopy = Gdn::Factory('Snoopy');
                 }
                 $Snoopy->Submit('http://marijnhaverbeke.nl/uglifyjs', array('code_url' => '', 'download' => '', 'js_code' => file_get_contents($FilePath)));
                 file_put_contents($CachedFilePath, trim($Snoopy->results));
             }
             if (!$AllInOne) {
                 $GroupName = self::GetGroupName($FilePath);
                 if ($GroupName == 'js' || $GroupName == 'themes') {
                     $GroupName = 'library';
                 } elseif (!in_array($GroupName, array('plugins', 'applications', 'library'))) {
                     // Unknown group, move it to application group.
                     $GroupName = 'applications';
                 }
                 $CombinedJavascript[$GroupName][$Index] = $CachedFilePath;
             } else {
                 $CombinedJavascript[$Index] = $CachedFilePath;
             }
         } elseif (GetValue(HeadModule::TAG_KEY, $Tag) == 'link' && GetValue('rel', $Tag) == 'stylesheet') {
             $CachedFilePath = $this->GetCachedFilePath($Tag, 'href', $FilePath);
             if ($CachedFilePath === False) {
                 continue;
             }
             if (!file_exists($CachedFilePath)) {
                 $Css = file_get_contents($FilePath);
                 $CssText = self::ProcessImportCssText($Css, $FilePath);
                 if ($CssText === False) {
                     $CssText = $Css;
                 }
                 if (GetValue('MinifyCss', $Configuration, True)) {
                     $CssText = self::MinifyCssText($CssText);
                 }
                 self::ChangeBackgroundUrl($CssText, $FilePath);
                 // TODO: COMBINE CSS (WE MUST CHECK MEDIA)
                 // style.css + custom.css, admin.css + customadmin.css
                 // TODO: MORE EFFECTIVE COMBINE colorbox.css + custom-colorbox.css
                 file_put_contents($CachedFilePath, $CssText);
             }
             if (!$AllInOne) {
                 $GroupName = self::GetGroupName($FilePath);
                 // combine in two group applications and plugins
                 // TODO: REMOVE if (!isset($CssTag)) $CssTag = $Tag;
                 if (!isset($CssTag)) {
                     $CssTag = $Tag;
                 }
                 if (!in_array($GroupName, array('plugins', 'applications'))) {
                     $GroupName = 'applications';
                 }
                 $CombinedCss[$GroupName][$Index] = $CachedFilePath;
             } else {
                 $CombinedCss[$Index] = $CachedFilePath;
             }
         }
     }
     if ($AllInOne == 2) {
         // Js
         unset($CombinedJavascript['library']);
         foreach ($CombinedJavascript as $Index => $Src) {
             $NewSrc = Asset($Src, False, False);
             if ($DeferJavaScript) {
                 $RemoveIndex[] = $Index;
                 $this->DeferJavaScriptFiles[] = $NewSrc;
             } else {
                 $Tags[$Index]['src'] = $NewSrc;
             }
         }
         // Css
         foreach ($CombinedCss as $Index => $Src) {
             $Tags[$Index]['href'] = Asset($Src, False, False);
         }
     } elseif ($AllInOne) {
         $RemoveIndex[] = array_keys($CombinedCss);
         // Css
         $CombinedCss = array_unique($CombinedCss);
         $CachedFilePath = 'cache/ps/style.' . self::HashSumFiles($CombinedCss) . '.css';
         if (!file_exists($CachedFilePath)) {
             $Combined = '';
             //$IncludeBasename = Gdn::Session()->CheckPermission('Garden.Admin.Only');
             foreach ($CombinedCss as $File) {
                 //if ($IncludeBasename) $Combined .= '/*' . basename($File) . "*/\n";
                 $Combined .= file_get_contents($File) . "\n";
             }
             file_put_contents($CachedFilePath, $Combined);
         }
         $Tags[] = array(HeadModule::TAG_KEY => 'link', 'rel' => 'stylesheet', 'type' => 'text/css', 'href' => Asset($CachedFilePath, False, False), 'media' => 'all', HeadModule::SORT_KEY => 98);
         // Js
         unset($CombinedJavascript['library']);
         $RemoveIndex[] = array_keys($CombinedJavascript);
         $CombinedJavascript = array_unique($CombinedJavascript);
         $CachedFilePath = 'cache/ps/functions.' . self::HashSumFiles($CombinedJavascript) . '.js';
         if (!file_exists($CachedFilePath)) {
             $Combined = '';
             foreach ($CombinedJavascript as $File) {
                 $Combined .= file_get_contents($File) . ";\n";
             }
             file_put_contents($CachedFilePath, $Combined);
         }
         $Src = Asset($CachedFilePath, False, False);
         // Defer loading of JavaScript
         if ($DeferJavaScript) {
             $this->DeferJavaScriptFiles[] = $Src;
         } else {
             $Tags[] = array(HeadModule::TAG_KEY => 'script', 'type' => 'text/javascript', 'src' => $Src, HeadModule::SORT_KEY => 99);
         }
         //d($RemoveIndex, Flatten($RemoveIndex), @$CombinedCss, @$CombinedJavascript);
     } else {
         if (count($CombinedCss) > 0) {
             foreach ($CombinedCss as $Group => $Files) {
                 $RemoveIndex[] = array_keys($Files);
                 $Files = array_unique($Files);
                 $Hash = self::HashSumFiles($Files);
                 $CachedFilePath = "cache/ps/{$Group}.{$Hash}.css";
                 if (!file_exists($CachedFilePath)) {
                     $Combined = '';
                     foreach ($Files as $Index => $File) {
                         $Combined .= '/*' . basename($File) . "*/\n" . file_get_contents($File) . "\n";
                     }
                     file_put_contents($CachedFilePath, $Combined);
                 }
                 $CssTag[HeadModule::SORT_KEY] += 1;
                 // This is not works...
                 // $CssTag['href'] = Asset($CachedFilePath);
                 // $Tags[] = $CssTag;
                 // This works...
                 $Tags[] = array_merge($CssTag, array('href' => Asset($CachedFilePath, False, False)));
             }
         }
         // TODO: IF ONE FILE IN GROUP NO NEED TO PARSE/COMBINE IT
         if (count($CombinedJavascript) > 1) {
             // TODO: array_unique
             foreach ($CombinedJavascript as $Group => $Files) {
                 $RemoveIndex[] = array_keys($Files);
                 $Files = array_unique($Files);
                 $Hash = self::HashSumFiles($Files);
                 $CachedFilePath = "cache/ps/{$Group}.{$Hash}.js";
                 if (!file_exists($CachedFilePath)) {
                     $Combined = '';
                     foreach ($Files as $Index => $File) {
                         $Combined .= '//' . basename($File) . "\n" . file_get_contents($File) . ";\n";
                     }
                     file_put_contents($CachedFilePath, $Combined);
                 }
                 $Src = Asset($CachedFilePath, False, False);
                 if ($DeferJavaScript) {
                     $this->DeferJavaScriptFiles[] = $Src;
                 } else {
                     $JsTag['src'] = $Src;
                     $JsTag[HeadModule::SORT_KEY] += 1;
                     $Tags[] = $JsTag;
                 }
             }
         }
     }
     if (count($RemoveIndex) > 0) {
         $Tags = self::RemoveKeyFromArray($Tags, Flatten($RemoveIndex));
     }
     $Head->Tags($Tags);
 }
Ejemplo n.º 4
0
         $PluginManager->CallEventHandlers($Handler, 'Tick', $Event);
         //K('#'.$Event, (int)K('#'.$Event) + 1);
     } catch (Exception $Exception) {
     }
     Console::Message('Tick: %s', $Event);
     $Events[] = $Event;
 }
 $Event = $Events[1] . '_' . date('l');
 try {
     $PluginManager->CallEventHandlers($Handler, 'Tick', $Event);
     //K('#'.$Event, (int)K('#'.$Event) + 1);
 } catch (Exception $Exception) {
 }
 Console::Message('Tick: %s', $Event);
 $Events[] = $Event;
 $Range = Flatten(array(range(1, 99, 1), range(100, 999, 5)));
 foreach ($Range as $i) {
     foreach ($Ticks as $Second => $Name) {
         $Suffix = $i == 1 ? '' : 's';
         if ($YearSeconds % $Second == 0 && $YearSeconds / $Second % $i == 0) {
             $Event = 'Every_' . $i . '_' . $Name . $Suffix;
             $Events[] = $Event;
             try {
                 $PluginManager->CallEventHandlers($Handler, 'Tick', $Event);
                 //K('#'.$Event, (int)K('#'.$Event) + 1);
             } catch (Exception $Exception) {
             }
             Console::Message('Tick: %s', $Event);
         }
     }
 }