diff --git a/module/common/control.php b/module/common/control.php index 930b304a85..50d42de18b 100644 --- a/module/common/control.php +++ b/module/common/control.php @@ -12,7 +12,7 @@ class common extends control { /** - * 构造函数:启动会话,加载公司模块,并设置公司信息。 + * The construc method, to do some auto things. * * @access public * @return void @@ -20,13 +20,10 @@ class common extends control public function __construct() { parent::__construct(); - session_name($this->config->sessionVar); - if(isset($_GET[$this->config->sessionVar])) session_id($_GET[$this->config->sessionVar]); - session_start(); - $this->sendHeader(); - $this->loadModel('company'); - $this->setCompany(); - $this->setUser(); + $this->common->startSession(); + $this->common->sendHeader(); + $this->common->setCompany(); + $this->common->setUser(); } /** @@ -341,65 +338,6 @@ EOT; return true; } - /** - * 设置当前访问的公司信息。 - * - * 首先尝试按照当前访问的域名查找对应的公司信息, - * 如果无法查到,再按照默认的域名进行查找。 - * 如果还无法查到,则取第一个公司作为默认的公司。 - * 获取公司信息之后,将其写入到$_SESSION中。 - * - * @access public - * @return void - */ - private function setCompany() - { - $httpHost = $_SERVER['HTTP_HOST']; - if(strpos($httpHost, ":")) - { - $httpHost = explode(":", $httpHost); - $httpHost = $httpHost[0]; - } - - if(isset($_SESSION['company']) and $_SESSION['company']->pms == $httpHost) - { - $this->app->company = $_SESSION['company']; - } - else - { - $company = $this->company->getByDomain(); - if(!$company and isset($this->config->default->domain)) $company = $this->company->getByDomain($this->config->default->domain); - if(!$company) $company = $this->company->getFirst(); - if(!$company) $this->app->error(sprintf($this->lang->error->companyNotFound, $httpHost), __FILE__, __LINE__, $exit = true); - $_SESSION['company'] = $company; - $this->app->company = $company; - } - } - - /** - * 设置当前访问的用户信息。 - * - * @access public - * @return void - */ - private function setUser() - { - if(isset($_SESSION['user'])) - { - $this->app->user = $_SESSION['user']; - } - elseif($this->app->company->guest) - { - $user = new stdClass(); - $user->id = 0; - $user->account = 'guest'; - $user->realname = 'guest'; - $user->rights = $this->loadModel('user')->authorize('guest'); - $_SESSION['user'] = $user; - $this->app->user = $_SESSION['user']; - } - } - /* 保存最后浏览的产品id到session会话中。*/ public static function saveProductState($productID, $defaultProductID) { @@ -422,18 +360,14 @@ EOT; } /** - * 发送header信息到浏览器。 + * Create changes of one object. * + * @param mixed $old the old object + * @param mixed $new the new object + * @static * @access public - * @return void + * @return array */ - public function sendHeader() - { - header("Content-Type: text/html; Language={$this->config->encoding}"); - header("Cache-control: private"); - } - - /* 比较两个数组元素的不同,产生修改记录。*/ public static function createChanges($old, $new) { global $config; @@ -451,30 +385,19 @@ EOT; if($value != $old->$key) { $diff = ''; - if(substr_count($value, "\n") > 1 or substr_count($old->$key, "\n") > 1 or strpos('name,title,desc,spec,steps,content,digest', strtolower($key)) !== false) $diff = self::diff($old->$key, $value); + if(substr_count($value, "\n") > 1 or substr_count($old->$key, "\n") > 1 or strpos('name,title,desc,spec,steps,content,digest', strtolower($key)) !== false) $diff = commonModel::diff($old->$key, $value); $changes[] = array('field' => $key, 'old' => $old->$key, 'new' => $value, 'diff' => $diff); } } return $changes; } - /* 比较两个字符串的不同。摘自PHPQAT自动化测试框架。*/ - public static function diff($text1, $text2) - { - $w = explode("\n", trim($text1)); - $o = explode("\n", trim($text2)); - $w1 = array_diff_assoc($w,$o); - $o1 = array_diff_assoc($o,$w); - $w2 = array(); - $o2 = array(); - foreach($w1 as $idx => $val) $w2[sprintf("%03d<",$idx)] = sprintf("%03d- ", $idx+1) . "" . trim($val) . ""; - foreach($o1 as $idx => $val) $o2[sprintf("%03d>",$idx)] = sprintf("%03d+ ", $idx+1) . "" . trim($val) . ""; - $diff = array_merge($w2, $o2); - ksort($diff); - return implode("\n", $diff); - } - - /* 获得系统URL地址。*/ + /** + * Get the full url of the system. + * + * @access public + * @return string + */ public function getSysURL() { global $config; @@ -483,14 +406,15 @@ EOT; return "$httpType://$httpHost"; } - /* 获得系统默认的样式表。*/ - public function getDefaultCss() + /** + * Print the run info. + * + * @param mixed $startTime the start time. + * @access public + * @return void + */ + public function printRunInfo($startTime) { - global $app; - $pathFix = $app->getPathFix(); - $cssFile = $app->getAppRoot() . "www{$pathFix}theme{$pathFix}default{$pathFix}style.css"; - $cssContent = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!','', file_get_contents($cssFile)); - $cssContent = str_replace(array(" {", "} ", ' ', "\r\n", "\r", "\n", "\t"), array("{", '}', ' ', ''), $cssContent); - return $cssContent; + vprintf($this->lang->runInfo, $this->common->getRunInfo($startTime)); } } diff --git a/module/common/lang/zh-cn.php b/module/common/lang/zh-cn.php index 7a195ab517..abeea392bd 100644 --- a/module/common/lang/zh-cn.php +++ b/module/common/lang/zh-cn.php @@ -24,6 +24,7 @@ $lang->logout = '退出系统'; $lang->login = '登录'; $lang->aboutZenTao = '关于禅道'; $lang->todayIs = '今天是%s,'; +$lang->runInfo = "
时间: %s 毫秒, 内存: %s KB, 查询: %s.
"; $lang->reset = '重填'; $lang->edit = '编辑'; diff --git a/module/common/model.php b/module/common/model.php new file mode 100644 index 0000000000..2d5e358141 --- /dev/null +++ b/module/common/model.php @@ -0,0 +1,133 @@ + + * @package common + * @version $Id$ + * @link http://www.zentao.net + */ +class commonModel extends model +{ + /** + * Start the session. + * + * @access public + * @return void + */ + public function startSession() + { + session_name($this->config->sessionVar); + if(isset($_GET[$this->config->sessionVar])) session_id($_GET[$this->config->sessionVar]); + session_start(); + } + + /** + * Set the header info. + * + * @access public + * @return void + */ + public function sendHeader() + { + header("Content-Type: text/html; Language={$this->config->encoding}"); + header("Cache-control: private"); + } + + /** + * Set the commpany. + * + * First, search company by the http host. If not found, search by the default domain. Last, use the first as the default. + * After get the company, save it to session. + * @access public + * @return void + */ + public function setCompany() + { + $httpHost = $this->server->http_host; + if(strpos($httpHost, ":")) + { + $httpHost = explode(":", $httpHost); + $httpHost = $httpHost[0]; + } + + if($this->session->company and $this->session->company->pms == $httpHost) + { + $this->app->company = $this->session->company; + } + else + { + $company = $this->loadModel('company')->getByDomain(); + if(!$company and isset($this->config->default->domain)) $company = $this->company->getByDomain($this->config->default->domain); + if(!$company) $company = $this->company->getFirst(); + if(!$company) $this->app->error(sprintf($this->lang->error->companyNotFound, $httpHost), __FILE__, __LINE__, $exit = true); + $this->session->set('company', $company); + $this->app->company = $company; + } + } + + /** + * Set the user info. + * + * @access public + * @return void + */ + public function setUser() + { + if($this->session->user) + { + $this->app->user = $this->session->user; + } + elseif($this->app->company->guest) + { + $user = new stdClass(); + $user->id = 0; + $user->account = 'guest'; + $user->realname = 'guest'; + $user->rights = $this->loadModel('user')->authorize('guest'); + $this->session->set('user', $user); + $this->app->user = $this->session->user; + } + } + + /** + * Get the run info. + * + * @param mixed $startTime the start time of this execution + * @access public + * @return array the run info array. + */ + public function getRunInfo($startTime) + { + $info['timeUsed'] = round(getTime() - $startTime, 4) * 1000; + $info['memory'] = round(memory_get_peak_usage() / 1024, 1); + $info['querys'] = count(dao::$querys); + return $info; + } + + /** + * Diff two string. (see phpt) + * + * @param string $text1 + * @param string $text2 + * @static + * @access public + * @return string + */ + public static function diff($text1, $text2) + { + $w = explode("\n", trim($text1)); + $o = explode("\n", trim($text2)); + $w1 = array_diff_assoc($w,$o); + $o1 = array_diff_assoc($o,$w); + $w2 = array(); + $o2 = array(); + foreach($w1 as $idx => $val) $w2[sprintf("%03d<",$idx)] = sprintf("%03d- ", $idx+1) . "" . trim($val) . ""; + foreach($o1 as $idx => $val) $o2[sprintf("%03d>",$idx)] = sprintf("%03d+ ", $idx+1) . "" . trim($val) . ""; + $diff = array_merge($w2, $o2); + ksort($diff); + return implode("\n", $diff); + } +}