/** * Takes a CSS string, rewrites all URL's using Scaffold's built-in find_file method * * @author Anthony Short * @param $css * @return $css string */ public static function formatting_process() { # The absolute url to the directory of the current CSS file $dirPath = SCAFFOLD_DOCROOT . Scaffold::url_path(Scaffold::$css->path); $dir = rtrim(SCAFFOLD_URLPATH, '\\/') . '/' . str_replace(rtrim(SCAFFOLD_DOCROOT, '\\/') . DIRECTORY_SEPARATOR, '', Scaffold::$css->path); //$dir = str_replace('\\', '/', SCAFFOLD_URLPATH . str_replace(SCAFFOLD_DOCROOT, '', Scaffold::$css->path)); # @imports - Thanks to the guys from Minify for the regex :) if (preg_match_all('/ @import\\s+ (?:url\\(\\s*)? # maybe url( [\'"]? # maybe quote (.*?) # 1 = URI [\'"]? # maybe end quote (?:\\s*\\))? # maybe ) ([a-zA-Z,\\s]*)? # 2 = media list ; # end token /x', Scaffold::$css->string, $found)) { foreach ($found[1] as $key => $value) { # Should we skip it if (self::skip($value)) { continue; } $media = $found[2][$key] == "" ? '' : ' ' . preg_replace('/\\s+/', '', $found[2][$key]); # Absolute path $absolute = self::up_directory($dir, substr_count($url, '..' . DIRECTORY_SEPARATOR, 0)) . str_replace('..' . DIRECTORY_SEPARATOR, '', $url); $absolute = str_replace('\\', '/', $absolute); # Rewrite it # Webligo - PHP5.1 compat Scaffold::$css->string = str_replace($found[0][$key], '@import \'' . $absolute . '\'' . $media . ';', Scaffold::$css->string); } } # Convert all url()'s to absolute paths if required if (preg_match_all('/url\\(\\s*([^\\)\\s]+)\\s*\\)/', Scaffold::$css->__toString(), $found)) { foreach ($found[1] as $key => $value) { // START - Webligo Developments $original = $found[0][$key]; $url = Scaffold_Utils::unquote($value); # Absolute Path if (self::skip($url)) { continue; } # home path if ($url[0] == '~' && $url[1] == '/') { $absolute = str_replace('\\', '/', rtrim(SCAFFOLD_URLPATH, '/\\') . '/' . ltrim($url, '~/')); $absolutePath = rtrim(SCAFFOLD_DOCROOT, '/\\') . DIRECTORY_SEPARATOR . ltrim($url, '~/'); } else { $absolute = str_replace('\\', '/', self::up_directory($dir, substr_count($url, '..' . DIRECTORY_SEPARATOR, 0)) . str_replace('..' . DIRECTORY_SEPARATOR, '', $url)); $absolutePath = self::up_directory($dirPath, substr_count($url, '..' . DIRECTORY_SEPARATOR, 0)) . str_replace('..' . DIRECTORY_SEPARATOR, '', $url); } # If the file doesn't exist if (!Scaffold::find_file($absolutePath)) { Scaffold::log("Missing image - {$absolute} / {$absolutePath}", 1); } # Rewrite it Scaffold::$css->string = str_replace($original, 'url(' . $absolute . ')', Scaffold::$css->string); # Webligo - PHP5.1 compat // END - Webligo Developments } } }
/** * Imports css via @import statements * * @author Anthony Short * @param $css */ public static function server_import($css, $base) { if (preg_match_all('/\\@include\\s+(?:\'|\\")([^\'\\"]+)(?:\'|\\")\\;/', $css, $matches)) { $unique = array_unique($matches[1]); $include = str_replace("\\", "/", Scaffold_Utils::unquote($unique[0])); # If they haven't supplied an extension, we'll assume its a css file if (pathinfo($include, PATHINFO_EXTENSION) == "") { $include .= '.css'; } # Make sure it's a CSS file if (pathinfo($include, PATHINFO_EXTENSION) != 'css') { $css = str_replace($matches[0][0], '', $css); Scaffold::log('Invalid @include file - ' . $include); self::server_import($css, $base); } # Find the file if ($path = Scaffold::find_file($include, $base)) { # Make sure it hasn't already been included if (!in_array($path, self::$loaded)) { self::$loaded[] = $path; $contents = file_get_contents($path); # Check the file again for more imports $contents = self::server_import($contents, realpath(dirname($path)) . '/'); $css = str_replace($matches[0][0], $contents, $css); } else { $css = str_replace($matches[0][0], '', $css); } } else { Scaffold::error('Can\'t find the @include file - <strong>' . $unique[0] . '</strong>'); } $css = self::server_import($css, $base); } return $css; }
/** * Sets up the cache path * * @return return type */ public function setup($path, $lifetime) { if (!is_dir($path)) { Scaffold::log("Cache path does not exist. {$path}", 0); } if (!is_writable($path)) { Scaffold::log("Cache path is not writable. {$path}", 0); } self::$cache_path = $path; self::lifetime($lifetime); }
/** * Takes a CSS string, rewrites all URL's using Scaffold's built-in find_file method * * @author Anthony Short * @param $css * @return $css string */ public static function formatting_process() { # The absolute url to the directory of the current CSS file $dir = Scaffold::url_path(Scaffold::$css->path); # @imports - Thanks to the guys from Minify for the regex :) if (preg_match_all('/ @import\\s+ (?:url\\(\\s*)? # maybe url( [\'"]? # maybe quote (.*?) # 1 = URI [\'"]? # maybe end quote (?:\\s*\\))? # maybe ) ([a-zA-Z,\\s]*)? # 2 = media list ; # end token /x', Scaffold::$css, $found)) { foreach ($found[1] as $key => $value) { # Should we skip it if (self::skip($value)) { continue; } $media = $found[2][$key] == "" ? '' : ' ' . preg_replace('/\\s+/', '', $found[2][$key]); # Absolute path $absolute = self::up_directory($dir, substr_count($value, '..' . DIRECTORY_SEPARATOR, 0)) . str_replace('..' . DIRECTORY_SEPARATOR, '', $value); # Rewrite it Scaffold::$css->string = str_replace($found[0][$key], '@import \'' . $absolute . '\'' . $media . ';', Scaffold::$css); } } # Convert all url()'s to absolute paths if required if (preg_match_all('/url\\(\\s*([^\\)\\s]+)\\s*\\)/', Scaffold::$css, $found)) { foreach ($found[1] as $key => $value) { $url = Scaffold_Utils::unquote($value); # Absolute Path if (self::skip($url)) { continue; } # Absolute path $absolute = self::up_directory($dir, substr_count($url, '..' . DIRECTORY_SEPARATOR, 0)) . str_replace('..' . DIRECTORY_SEPARATOR, '', $url); # If the file doesn't exist if (!Scaffold::find_file($absolute)) { Scaffold::log("Missing image - {$absolute}", 1); } # Rewrite it Scaffold::$css->string = str_replace($found[0][$key], 'url(' . $absolute . ')', Scaffold::$css); } } }
public static function display() { if (Scaffold::option('validate')) { # Get the validator options from the config $validator_options = Scaffold::$config['Validate']['options']; # Add our options $validator_options['text'] = Scaffold::$output; $validator_options['output'] = 'soap12'; # Encode them $validator_options = http_build_query($validator_options); $url = "http://jigsaw.w3.org/css-validator/validator?{$validator_options}"; # The Curl options $options = array(CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => 1); # Start CURL $handle = curl_init(); curl_setopt_array($handle, $options); $buffer = curl_exec($handle); curl_close($handle); # If something was returned if (!empty($buffer)) { # Simplexml doesn't like colons $buffer = preg_replace("/(<\\/?)(\\w+):([^>]*>)/", "\$1\$2\$3", $buffer); # Let it be xml! $results = simplexml_load_string($buffer); $is_valid = (string) $results->envBody->mcssvalidationresponse->mvalidity; # Oh noes! Display the errors if ($is_valid == "false") { $errors = $results->envBody->mcssvalidationresponse->mresult->merrors; foreach ($errors->merrorlist->merror as $key => $error) { $line = (string) $error->mline; $message = trim((string) $error->mmessage); $near = (string) $error->mcontext; self::$errors[] = array('line' => $line, 'near' => $near, 'message' => $message); Scaffold::log("Validation Error on line {$line} near {$near} => {$message}", 1); } } } } }
/** * Replaces the mixins with their properties * * @author Anthony Short * @param $mixin_key - The bases array key corrosponding to the current mixin * @param $mixins - An array of found mixins * @return string */ public static function build_mixins($mixin_key, $mixins, $already_mixed = array()) { $bases =& self::$mixins; $mixin_name = $mixins[2][$mixin_key]; if (isset($bases[$mixin_name])) { $base_properties = $bases[$mixin_name]['properties']; # If there is no base for that mixin and we aren't in a recursion loop if (is_array($bases[$mixin_name]) and !in_array($mixin_name, $already_mixed)) { $already_mixed[] = $mixin_name; # Parse the parameters of the mixin $params = self::parse_params($mixins[0][$mixin_key], $mixins[4][$mixin_key], $bases[$mixin_name]['params']); # Set the parameters as constants foreach ($params as $key => $value) { Constants::set($key, (string) $value); } $new_properties = Constants::replace($base_properties); # Unset the parameters as constants foreach ($params as $key => $value) { Constants::remove($key); } # Parse conditionals if there are any in there $new_properties = self::parse_conditionals($new_properties); # Find nested mixins if ($inner_mixins = self::find_mixins($new_properties)) { # Loop through all the ones we found, skipping on recursion by passing # through the current mixin we're working on foreach ($inner_mixins[0] as $key => $value) { # Parse the mixin and replace it within the property string $new_properties = str_replace($value, self::build_mixins($key, $inner_mixins, $already_mixed), $new_properties); } } # Clean up memory unset($inner_mixins, $params, $mixins); return preg_replace('/^(\\s|\\n|\\r)*|(\\n|\\r|\\s)*$/', '', $new_properties); } elseif (in_array($mixin_name, $already_mixed)) { Scaffold::log('Recursion in mixin - ' . $mixin_name, 1); } } else { Scaffold::log('Missing mixin - ' . $mixin_name, 2); } }
/** * Loads constants from an XML file * * @param $param * @return return type */ private static function load_xml_constants($file) { if ($file === false) { return; } # If the xml file doesn't exist if (!file_exists($file)) { Scaffold::log("Missing constants XML file. The file ({$file}) doesn't exist.", 1); return; } # Load the xml $xml = simplexml_load_file($file); # Loop through them and set them as constants foreach ($xml->constant as $key => $value) { self::set((string) $value->name, (string) $value->value); } }