From 8f2901925c8afa822810287497e51d4981b9da86 Mon Sep 17 00:00:00 2001 From: Richard Bateman Date: Sat, 17 Dec 2011 16:02:33 -0700 Subject: [PATCH 1/4] Added support for overriding the class include path in a module --- framework/ZincModule.php | 48 ++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/framework/ZincModule.php b/framework/ZincModule.php index 9ec3ca9..a1a22c9 100644 --- a/framework/ZincModule.php +++ b/framework/ZincModule.php @@ -5,23 +5,23 @@ abstract class ZincModule private $depends = array(), $includes = array(), $classes = array(); protected $hasConfig = false; public $path; - + final function __construct($path, $lib) { // assign in the paramamters $this->path = $path; $this->lib = $lib; $this->name = strtolower(str_replace('Module', '', get_class($this))); - + // second stage (module specific) construction $this->init(); - + $classname = get_class($this); // load any dependant modules if($this->getDepends()) foreach($this->getDepends() as $thisDepends) $this->lib->loadMod($thisDepends); - + // include any normal files that need to be included if($this->getIncludes()) { @@ -33,22 +33,22 @@ final function __construct($path, $lib) require($this->path . '/' . $thisInclude); } } - + // register any class files if($classes = $this->getClasses()) - foreach($classes as $thisClass) - ZoopLoader::addClass($thisClass, $this->path . '/' . $thisClass . '.php'); - + foreach($classes as $className => $classPath) + ZoopLoader::addClass($className, $classPath); + if($this->hasConfig) $this->loadConfig(); - + // handle configuration - $this->configure(); + $this->configure(); } - + protected function init() {} protected function configure() {} - + /** * Figures out the name of the module by removing the word "Module" from * the class name and returning the result @@ -59,17 +59,17 @@ function createName() { return strtolower(str_replace('Module', '', get_class($this))); } - + function getConfigPath() { return $this->name; } - + private function loadConfig() { Config::suggest($this->path . '/' . 'config.yaml', 'zinc.' . $this->getConfigPath()); } - + /** * Returns the configuration options using the Config class. * Returns config options from "zinc.." @@ -83,37 +83,41 @@ function getConfig($path = '') $config = Config::get('zinc.' . $this->getConfigPath() . $path); return $config; } - + /** * stuff about this function * * @return array(list of files to include) or false; */ - protected function addClass($className) + protected function addClass($className, $classPath = null) { $this->classes[] = $className; + if (!$classPath) { + $classPath = $this->path . "/" . $className . ".php"; + } + $this->classes[$className] = $classPath; } - + protected function getClasses() { return $this->classes; } - + protected function addInclude($include) { $this->includes[] = $include; } - + protected function getIncludes() { return $this->includes; } - + protected function depend($module) { $this->depends[] = $module; } - + private function getDepends() { return $this->depends; From 288ccf7ac5c340084becd1958a98c12d90b8e9b0 Mon Sep 17 00:00:00 2001 From: Richard Bateman Date: Sat, 17 Dec 2011 16:20:34 -0700 Subject: [PATCH 2/4] Added support for put and delete page handlers --- framework/core/utils/Utils.php | 62 ++++++++++++++++++--------- framework/core/zone/Zone.php | 78 ++++++++++++++++++---------------- 2 files changed, 83 insertions(+), 57 deletions(-) diff --git a/framework/core/utils/Utils.php b/framework/core/utils/Utils.php index 78efe91..e34ca5d 100755 --- a/framework/core/utils/Utils.php +++ b/framework/core/utils/Utils.php @@ -19,13 +19,33 @@ function RequestIsPost() return $_SERVER['REQUEST_METHOD'] == 'POST' ? 1 : 0; } +/** + * Returns true if the current page was requested with the POST method + * + * @return boolean + */ +function RequestIsPut() +{ + return $_SERVER['REQUEST_METHOD'] == 'PUT' ? 1 : 0; +} + +/** + * Returns true if the current page was requested with the POST method + * + * @return boolean + */ +function RequestIsDelete() +{ + return $_SERVER['REQUEST_METHOD'] == 'DELETE' ? 1 : 0; +} + /** * Evaluates the POST variables and creates a standard "year-month-day Hour24:minute:second -7:00" date from a POSTed form * The fields in the form should be as follows: * Month, Day, Year * Hour, Minute, Second * Meridian (<-- "am" or "pm") - * + * * @param $name Prefix of the POST variables to evaluate * @return string Date string */ @@ -34,7 +54,7 @@ function GetFormDate($name, $src = null) { if(!$src) $src = $_POST; - + if(is_array($src[$name])) { $year = $src[$name]['Date_Year']; @@ -48,7 +68,7 @@ function GetFormDate($name, $src = null) $day = $src[$name . 'Day']; $year = $src[$name . 'Year']; } - + return "$year-$month-$day"; } @@ -65,9 +85,9 @@ function GetPostDate($name) $minute = $_POST[$name . 'Minute']; $second = $_POST[$name . 'Second']; $meridian = $_POST[$name . 'Meridian']; - + $hour = $meridian == 'pm' ? ($hour + 12) : $hour; - + return "$year-$month-$day $hour:$minute:$second -7:00"; } */ @@ -83,10 +103,10 @@ function echo_r($var, $showBacktrace = NULL) { if(is_null($showBacktrace)) $showBacktrace = Config::get('zinc.debug.echorShowBacktrace'); - + if($showBacktrace) EchoBacktrace(); - + echo '
';
 	print_r($var);
 	echo '
'; @@ -142,7 +162,7 @@ function FormatBacktraceHtml($backtraceInfo) FileLineFunction - @@ -173,7 +193,7 @@ function FormatBacktraceFunctionCellHtml($lineInfo) $call .= isset($lineInfo['class']) ? ($lineInfo['class'] . $lineInfo['type']) : ''; $call .= $lineInfo['function'] . '('; $argStrings = array(); - + if(isset($lineInfo['args'])) foreach($lineInfo['args'] as $thisArg) { @@ -207,14 +227,14 @@ function FormatBacktraceFunctionCellHtml($lineInfo) die('unhandled type ' . gettype($thisArg)); break; } - + // echo 'call = ' . $call . '
'; } - + $call .= implode(', ', $argStrings); - + $call .= ')'; - + return $call; } @@ -228,11 +248,11 @@ function FormatBacktraceFunctionCellHtml($lineInfo) function EchoStaticFile($filename) { $fp = fopen($filename, 'rb'); - + // send the headers //header("Content-Type: image/png"); // figure out what should really be done here header("Content-Length: " . filesize($filename)); // also we want to be able to properly set the cache headers here - + fpassthru($fp); } @@ -255,17 +275,17 @@ function ListDir($path, $params) { $keep = 0; $extention = GetFileExtention($entry); - + if(in_array($extention, $params['extentions'])) //echo $extention . "\n"; $keep = 1; } - + if($keep) $entries[] = $entry; } $d->close(); - + return $entries; } @@ -277,7 +297,7 @@ function WalkDir($dir, $action) { if($entry == '.' || $entry == '..') continue; - + echo $entry."\n"; $action($entry); } @@ -358,7 +378,7 @@ function GetRandomBytes($count, $allowFallback = false) { if(!$allowFallback) trigger_error('system could not generate enough random data'); - + $output = ''; for ($i = 0; $i < $count; $i += 16) { $this->random_state = @@ -382,7 +402,7 @@ function GetNonEmptyLines($text) if($line) $lines[] = $line; } - + return $lines; } diff --git a/framework/core/zone/Zone.php b/framework/core/zone/Zone.php index 120446f..30f5a90 100755 --- a/framework/core/zone/Zone.php +++ b/framework/core/zone/Zone.php @@ -3,7 +3,7 @@ abstract class Zone extends Object { private $requestInfo; private $params; - + function init($requestInfo = NULL, $params = array()) { if(!$requestInfo) @@ -16,46 +16,52 @@ function init($requestInfo = NULL, $params = array()) $this->requestInfo = $requestInfo; $this->requestInfo[] = array('zone' => strtolower(substr(get_class($this), 4))); } - + $this->params = array(); } - + public function initZone($p, $z) {} - + // // methods to override // - + function getParamNames() { return array(); } - + // the main functionality of zone function handleRequest($pathParts) { $originalPart = array_shift($pathParts); $thisPart = ucfirst($originalPart); - + $zoneName = "Zone$thisPart"; $pageName = "page$thisPart"; $postName = "post$thisPart"; + $putName = "put$thisPart"; + $deleteName = "delete$thisPart"; $defaultName = "pageDefault"; $defaultPostName = "postDefault"; - + // first try to find a matching page if( RequestIsPost() && $this->_methodExists($postName) ) $foundPage = $postName; + elseif( RequestIsPut() && $this->_methodExists($putName) ) + $foundPage = $putName; + elseif( RequestIsDelete() && $this->_methodExists($deleteName) ) + $foundPage = $deleteName; else { if( $this->_methodExists($pageName) ) $foundPage = $pageName; } - + // next look for an existing zone that is not this class if( !isset($foundPage) ) { - + if( $thisPart && class_exists($zoneName) ) { $newZone = new $zoneName(); @@ -70,7 +76,7 @@ function handleRequest($pathParts) $newZone->requestInfo[count($newZone->requestInfo) - 1]['params'][] = $thisParamName; $newZone->params[$thisParamName] = $paramValue; } - + $this->initZone($pathParts, $this->params); $newZone->handleRequest($pathParts); } @@ -95,46 +101,46 @@ function handleRequest($pathParts) } } } - + // if we found a page then run it if( isset($foundPage) ) { $postfix = $foundPage == 'pageDefault' || $foundPage == 'postDefault' ? array(0 => 'default', 1 => $originalPart) : array(0 => $originalPart); $pageParams = array_merge($postfix, $pathParts); - + $this->initZone($pageParams, $this->params); - + if( $this->_methodExists('initPages') ) $this->initPages($pageParams, $this->params); - + $this->$foundPage($pageParams, $this->params); - + if( RequestIsGet() && $this->_methodExists('closePages') ) $this->closePages($pageParams, $this->params); - + if( RequestIsPost() && $this->_methodExists('closePosts') ) $this->closePosts($pageParams, $this->params); } } - - + + // // path manipulation functions // - + function setParam($name, $value) { $this->params[$name] = $value; } - + function getRequestPath($numLevels = 0) { $path = ''; - + $maxLevel = count($this->requestInfo) - 1; if($numLevels < 0) $maxLevel += $numLevels; - + foreach($this->requestInfo as $index => $thisZoneInfo) { $path .= $thisZoneInfo['zone']; @@ -143,55 +149,55 @@ function getRequestPath($numLevels = 0) foreach($thisZoneInfo['params'] as $paramName) $path .= '/' . $this->params[$paramName]; } - + if($maxLevel == $index) break; } - + return $path; } - + function getUrl($extra = NULL, $numLevels = 0) { $url = script_url; if($this->getRequestPath($numLevels)) $url .= '/' . $this->getRequestPath($numLevels); - + if($extra) $url .= '/' . $extra; - + return $url; } - + function getBackUrl($extra = NULL, $numLevels = 0) { $url = back_script_url; if($this->getRequestPath($numLevels)) $url .= '/' . $this->getRequestPath($numLevels); - + if($extra) $url .= '/' . $extra; - + return $url; } - + function redirect($extra) { $url = $this->getUrl($extra); Redirect($url); } - + function pushZone($zonePlusExtra) { $url = $this->getUrl($zonePlusExtra); - Redirect($url); + Redirect($url); } - + function popZone() { trigger_error('not done yet'); } - + function switchZone($extra) { $url = $this->getUrl('', -1) . '/' . $extra; From 3e9bbaee66220c8cf20992f994a497640692e694 Mon Sep 17 00:00:00 2001 From: Richard Bateman Date: Sun, 18 Dec 2011 00:15:30 -0700 Subject: [PATCH 3/4] Added function to get Json data from POST or PUT request --- framework/core/utils/Utils.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/framework/core/utils/Utils.php b/framework/core/utils/Utils.php index e34ca5d..542341f 100755 --- a/framework/core/utils/Utils.php +++ b/framework/core/utils/Utils.php @@ -39,6 +39,13 @@ function RequestIsDelete() return $_SERVER['REQUEST_METHOD'] == 'DELETE' ? 1 : 0; } +function GetJsonData() +{ + $input = file_get_contents("php://input", 1000000); + $value = json_decode($input, true); + return $value; +} + /** * Evaluates the POST variables and creates a standard "year-month-day Hour24:minute:second -7:00" date from a POSTed form * The fields in the form should be as follows: From e38c78efe9bcee6fdffa8b3f631888e7b4e16033 Mon Sep 17 00:00:00 2001 From: Richard Bateman Date: Mon, 16 Jan 2012 23:17:14 -0700 Subject: [PATCH 4/4] Fixed https when using nginx proxy on front of apache --- framework/core/app/Globals.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/framework/core/app/Globals.php b/framework/core/app/Globals.php index 46310da..ee039ea 100755 --- a/framework/core/app/Globals.php +++ b/framework/core/app/Globals.php @@ -22,6 +22,8 @@ { if( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ) $protocol = 'https://'; + elseif ( isset($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on') + $protocol = 'https://'; else $protocol = 'http://'; $sslProtocol = 'https://'; @@ -34,7 +36,7 @@ // what other situations besides mod_rewrite set this to 200? if(isset($_SERVER['REDIRECT_STATUS']) && $_SERVER['REDIRECT_STATUS'] == 200) { - $uri = $_SERVER['REQUEST_URI']; + $uri = urldecode($_SERVER['REQUEST_URI']); if($pos = strpos($uri, '?')) $uri = substr($uri, 0, $pos);