public function handleMatch($segment) { $params_list = explode(' ', $segment); $controller = array_shift($params_list); $params = array(); $substitutions = array(); foreach ($params_list as $param) { list($key, $value) = explode('=', $param); $key = trim($key); $value = trim($value); preg_match_all('/\\$\\{([a-zA-Z0-9_\\-\\/]+?)\\}\\s*/', $value, $matches); if ($matches[1]) { foreach ($matches[1] as $match) { $value = str_replace('${' . $match . '}', '___CTX' . count($substitutions) . '___', $value); array_push($substitutions, $match); } } $params[$key] = $value; } // generate the URL $url = chip('#Chippino/Router/GenerateURL')->with(array('alias' => $controller, 'params' => $params)); $url = '"' . $url . '"'; // sub back in all runtime resolutions preg_match_all('/(___CTX.*?___)/', $url, $matches); if (isset($matches[1])) { foreach ($matches[1] as $match) { $idx = trim($match, '_'); $idx = str_replace('CTX', '', $idx); $url = str_replace($match, '".urlencode(PXHTMLTemplate::ctx()->resolve("' . $substitutions[$idx] . '"))."', $url); } } return $url; }
public function handleMatch($node) { $PXHTMLTemplate = new PXHTMLTemplate(); $xtn = $node->getAttribute('xtn') ? '.' . $node->getAttribute('xtn') : '.php'; $cdata = $node->getAttribute('cdata') ? $node->getAttribute('cdata') : FALSE; $dom = $PXHTMLTemplate->parse(chip($node->getAttribute('file'))->getFilePath() . $xtn, TRUE, $cdata); // collect all things under the PXHTML root in new_dom, and // attach them as an appendChild statement $xpath = new DOMXPath($dom); $results = $xpath->query('/pxhtml:root'); $results = $results->item(0)->firstChild; // transfer all of the nodes from the include into the document at the include point while ($results) { $new_node = $this->importNode($results); $node->parentNode->insertBefore($new_node, $node); $results = $results->nextSibling; } $node->parentNode->removeChild($node); }
function chippino($settings) { // set defaults if not set $settings = array_merge(array('base_path' => dirname(__FILE__) . DIRECTORY_SEPARATOR . 'apps/chippino', 'chippino_class' => 'Chippino_Factory'), $settings); if (!isset($settings['config_path'])) { $settings['config_path'] = $settings['base_path'] . DIRECTORY_SEPARATOR . 'config'; } // do any special formatting per setting foreach ($settings as $key => $value) { switch ($key) { case 'base_path': case 'config_path': $value = substr($value, strrpos($value, DIRECTORY_SEPARATOR)) === DIRECTORY_SEPARATOR ? $value : $value . DIRECTORY_SEPARATOR; $value = substr($value, 0, -1); $settings[$key] = $value; break; } // assign to framework config chippino_config()->{$key} = $value; } // all done, execute chip('#Chippino/Core/Init')->with(); }
if (!class_exists('PXHTML')) { chip('#Chippino/Template/PXHTML/Package')->with(); chip('#Chippino/Template/Chip/Node/Component')->with(); chip('#Chippino/Template/Chip/Node/Filter')->with(); chip('#Chippino/Template/Chip/Node/Include')->with(); chip('#Chippino/Template/Chip/Node/Inspect')->with(); chip('#Chippino/Template/Chip/Node/Header')->with(); chip('#Chippino/Template/Chip/Node/Redirect')->with(); chip('#Chippino/Template/Chip/Expr/Url')->with(); } $core = chipi('#Chippino/Config')->with()->core; $cache = isset($core['template_cache']) ? $core['template_cache'] : NULL; $file = chip($this->route)->getFilePath() . '.php'; $chk = md5(file_get_contents($file)); if ($cache) { $_chip_pxhtml_cache_file = chip($cache . '/' . $chk)->getFilePath() . '.php'; } else { $_chip_pxhtml_cache_file = tempnam(sys_get_temp_dir(), 'pxhtml') . '.php'; } define('PXHTML_SIGNATURE_HASH', $chk); header('X-Chippino-File-Checksum: ' . PXHTML_SIGNATURE_HASH . "\n\n"); // begin output buffering // check for zlib.output_compression // open proper output bufer if (ini_get('zlib.output_compression')) { ob_start(); } else { ob_start('ob_gzhandler'); } if (file_exists($_chip_pxhtml_cache_file)) { include $_chip_pxhtml_cache_file;
* for performance, this should be called asSingleton() **/ // if asSingleton is enabled, this will get set if (isset($this->config_results)) { return $this->config_results; } $path = chippino_config()->config_path; $config = array(); $handle = opendir($path); while (FALSE !== ($file = readdir($handle))) { // skip files starting with . if (substr($file, 0, 1) == '.') { continue; } // skip directories if (is_dir($file)) { continue; } // skip things not ending in .php if (substr($file, -4) != '.php') { continue; } // strip ending .php, add to configs $file = substr($file, 0, -4); $default = file_exists(chip('#Chippino/Config/' . $file)->getFilePath() . '.php') ? chip('#Chippino/Config/' . $file)->with() : array(); $loaded = chip('!' . $path . '/' . $file)->with(); $config[$file] = array_merge($default, $loaded); } // wrap into a Chippino_Container $this->config_results = new Grok_Container($config); return $this->config_results;
<?php /** * gets the parameters sanitized based on the provided options * @param ... a list of params passed in with their explicit type * this can be optionally an array containing: * "as" => What to Sanitize As * "default" => a default value * Valid Sanitize Options are INT, FLOAT, STRING, RAW * @return a stdclass containing valid request items sanitized **/ $output = new stdClass(); foreach ($this as $key => $params) { if (!is_array($params)) { $params = array('as' => $params); } // magic quotes GPC protection if (isset($_REQUEST[$key])) { $output->{$key} = trim(chip('#Chippino/Request/Sanitize')->with(array('value' => chip('#Chippino/Request/_MagicQuotes')->with(array('str' => $_REQUEST[$key])), 'as' => $params['as']))); continue; } if (isset($params['default'])) { $output->{$key} = trim(chip('#Chippino/Request/Sanitize')->with(array('value' => $params['default'], 'as' => $params['as']))); continue; } $output->{$key} = trim(chip('#Chippino/Request/Sanitize')->with(array('value' => NULL, 'as' => $params['as']))); } // cast as auto object return $output;
$value = preg_replace('~&#([0-9]{2,4})~e', 'chr(\\1)', $value); } // alright, now we can clean up our blacklists since all entities are sane $value = str_replace(array_keys($blacklist_strings), array_values($blacklist_strings), $value); // strip all possible opening php tags // this catches XML tags as well $value = str_replace(array('<?php', '<?PHP', '<?', '?' . '>'), array('<?php', '<?PHP', '<?', '?>'), $value); // collapse our expandable whitespace items foreach ($blacklist_whitespace as $word) { $regex = '#(' . implode('\\s*', str_split($word)) . ')(\\W)#is'; $value = preg_replace($regex, $word . "\\2", $value); } // load into a DOM document // add a paragraph element we can extract from if (!function_exists('xss_loadhtml_error_handler')) { chip('#Chippino/Request/_XSSErrorHandler')->with(); } set_error_handler('xss_loadhtml_error_handler'); $dom = new DOMDocument(); $dom->recover = TRUE; $dom->strictErrorChecking = FALSE; $dom->loadHTML('<html><body><div class="' . $hashes['wrapper'] . '">' . $value . '</div></body></html>'); $xpath = new DOMXPath($dom); restore_error_handler(); // strip all nodes that are not allowed // keep going until done $nodes_exist = TRUE; while ($nodes_exist) { $nodes = $xpath->query('//' . implode(' | //', $xpath_remove_nodes)); if (!$nodes->length) { $nodes_exist = FALSE;
// standardize newlines if needed if (strpos($value, "\r") !== FALSE) { $value = str_replace(array("\r\n", "\r"), "\n", $value); } // standardize spaces if needed if (strpos($value, "\t") !== FALSE) { $value = str_replace(array("\t"), " ", $value); } switch ($as) { case 'raw': return $value; case 'string': $value = str_replace(array("\r\n", "\r"), "\n", $value); $value = preg_replace('/\\0+/', '', $value); $value = preg_replace('/(\\\\0)+/', '', $value); return chip('#Chippino/Request/_XSS')->with(array('value' => $value)); case 'float': $is_negative = strpos($value, '-') === 0 ? TRUE : FALSE; $value = trim($value, '-'); // get the fractional part of the number and convert it to a floatval // friendly usable part if (strpos($value, '.') !== FALSE) { $f_part = preg_replace('/(.*?)[^0-9]([0-9]+).*$/iu', '\\2', $value); $f_part = preg_replace('/[^0-9]/iu', '', $f_part); } else { $f_part = 0; } // capture the interger part // see getAsInt preg $value = preg_replace('/([0-9]+)(.*)$/iu', '\\1', $value); $value = preg_replace('/[^0-9]/iu', '', $value);
<?php // load the routing table $xml = @file_get_contents(chip('Config/Handlers')->getFilePath() . '.xml'); if (!$xml) { return FALSE; } // make an XML document to hold this // create a function on the fly if we need to for squelching XML errors $function_name = 'f' . md5(__FILE__); $function_exception = 'fe' . $function_name; if (!function_exists($function_name)) { eval(implode('', array("function {$function_name}(\$errno, \$errstr, \$errfile, \$errline) {}", "function {$function_exception}(\$str) { throw new Exception(\$str); }"))); } set_error_handler($function_name); $dom = new DOMDocument(); $dom->loadXML($xml); $xpath = new DOMXPath($dom); restore_error_handler(); $handlers = array(); $results = $xpath->query('//handler'); for ($i = 0; $i < $results->length; $i++) { $node = $results->item($i); $data = array(); $data['name'] = $node->hasAttribute('name') ? $node->getAttribute('name') : $function_exception('handlers must be defined by a name'); $data['path'] = $node->hasAttribute('target') ? $node->getAttribute('target') : $function_exception('"' . $data['name'] . '" must have a target defined'); $data['redirect'] = $node->hasAttribute('redirect') ? $node->getAttribute('redirect') : null; $handlers[$data['name']] = $data; } return $handlers;
/** * variant of chip() returns a singleton instance as a convience method * @see chip() **/ function chipi($file = NULL, $namespace = 'chip') { $chip = chip($file); $chip->asSingleton($namespace); return $chip; }
public function testDispatchGoesThroughMock() { $foo = chip('Chips/Default')->with(); return $this->assertEqual($foo, 'alternate_default_called'); }
<?php // inverts things $request = chip('#Chippino/Request/GetRequest')->with(array('foo' => 'string', 'bar' => 'int', 'baz' => 'float')); $results = array(); foreach ($request as $key => $value) { if (is_int($value) || is_float($value)) { $results[$key] = chip('Components/_NumberInverter')->with(array('value' => $value)); continue; } $results[$key] = chip('Components/_StringInverter')->with(array('value' => $value)); } return $results;
<?php // load the routing table $xml = @file_get_contents(chip('#Chippino/Config')->with()->core['routes']); if (!$xml) { return FALSE; } // make an XML document to hold this // create a function on the fly if we need to for squelching XML errors $function_name = 'f' . md5(__FILE__); $function_exception = 'fe' . $function_name; if (!function_exists($function_name)) { eval(implode('', array("function {$function_name}(\$errno, \$errstr, \$errfile, \$errline) {}", "function {$function_exception}(\$str) { throw new Exception(\$str); }"))); } set_error_handler($function_name); $dom = new DOMDocument(); $dom->loadXML($xml); $xpath = new DOMXPath($dom); restore_error_handler(); $routing_data = array('routes' => array(), 'generators' => array()); // grab all URL elements in the XML struct $results = $xpath->query('//url'); for ($i = 0; $i < $results->length; $i++) { $node = $results->item($i); $generator = array(); $alias = $node->hasAttribute('alias') ? $node->getAttribute('alias') : $function_exception('urls must have an alias'); $canonical = $node->hasAttribute('canonical') ? $node->getAttribute('canonical') : $function_exception('"' . $alias . '" must have an attribute "canonical"'); $target = $node->hasAttribute('target') ? $node->getAttribute('target') : $function_exception('"' . $alias . '" must have an attribute "target"'); $type = $node->hasAttribute('type') ? $node->getAttribute('type') : 'pxhtml'; $route_replacements = array(); $params = array();
public function loadRoute() { $urlpath = chip('#Chippino/Router/GetPath')->with(array('mode' => chipi('#Chippino/Config')->with()->core['url_mode'])); $route = chip('#Chippino/Router/GetRoute')->with(array('path' => $urlpath)); $this->route = $route; }
<?php if (!isset($this->override_route)) { session_start(); $urlpath = chip('#Chippino/Router/GetPath')->with(array('mode' => chipi('#Chippino/Config')->with()->core['url_mode'])); $route = chip('#Chippino/Router/GetRoute')->with(array('path' => $urlpath)); } if ($route['type'] == 'pxhtml') { // post Mod: Look for post data. If we have post data, then we do the post handler $handler = chip('#Chippino/Router/GetPostHandler')->with(array('handler' => isset($_POST['action']) ? $_POST['action'] : null)); // var_dump($handler); if ($handler) { // each handler will redirect if required $res = chip($handler['path'])->with(array('POST' => TRUE, 'redirect' => $handler['redirect'] && isset($_POST[$handler['redirect']]) ? $_POST[$handler['redirect']] : null)); } // to arrive here, either the handler had errors (and are set) // or there was no posting to begin with echo chip('#Chippino/Template/Init')->with(array('route' => $route['path'])); } else { $res = chip($route['path'])->with(array('segments' => explode('/', trim($urlpath, '/')), 'POST' => count($_POST) ? TRUE : FALSE)); }
<?php $path = chip('#Chippino/Router/GetPath')->with(array('mode' => chipi('#Chippino/Config')->with()->core['url_mode'])); $route = chip('#Chippino/Router/GetRoute')->with(array('path' => $path)); return $route;