/** * 记录日志 * * @param string $msg 记录的信息 * @param int $level * @param string $sourceName * @param string $actorName * @param string $actorId * @param mix $exts * @return boolean */ public function log($msg, $level = 0, $sourceName = "", $actorName = "", $actorId = "", $exts = null, $extsInt = null) { $now = $this->_microtime_float(); $used = $now - $this->_lastLogTime; $totalUsed = $now - $this->_startTime; $this->_lastLogTime = $now; $datetime = date("Y-m-d H:i:s"); $timestamp = time(); $ip = $_SERVER['REMOTE_ADDR']; $session_id = Watt_Session::getSession()->getUserId(); $session_name = Watt_Session::getSession()->getUserName(); /** * Log Post To test * @author terry * @version 0.1.0 * Thu Jul 05 09:38:47 CST 2007 */ // $exts .= 'Post:'.$this->_formatInfo( var_export( $_POST, true ) ); // $exts .= 'Cookie:'.$this->_formatInfo( var_export( $_COOKIE, true ) ); $logLine = $datetime . " | " . $timestamp . " | {$ip} | " . "{$used}ms/{$totalUsed}ms" . " | " . $sourceName . " | " . $this->_formatInfo($msg) . " | " . $level . " | " . 'uid:' . $session_id . " | uname:" . $session_name . " | " . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . " | " . $actorName . " | " . $actorId . " | " . $exts . " | " . $extsInt . " | " . $this->logerSn . "\n"; $rev = false; if ($this->_hLogFile) { $rev = fwrite($this->_hLogFile, $logLine); } /** * 增加系统级别的日志 * @author terry * @version 0.1.0 * Thu Sep 20 19:06:00 CST 2007 */ exec("logger -t tpm \"" . addslashes($logLine) . "\""); return $rev; }
/** * 获得一个Request的变量 * 如果没有在request中定义,则返回一个null * 不推荐直接使用,尽量使用in * * @param string $varname * @return mix */ protected function getRequestVar($varname) { if (isset($_REQUEST[$varname])) { /** * 容不得考虑仔细了,先满足了再说 * 系统耦合度已经越来越高了..要坏掉了 * @author terry * @version 0.1.0 * Thu Feb 14 10:26:10 CST 2008 */ if (Watt_Session::getSession()->getUserId()) { return $_REQUEST[$varname]; } else { //仅过滤未登录用户输入的信息 $filterList = Tpm_Config::getUserConfig(Watt_Config::getDefaultZuId(), 'filter_words'); return Watt_Util_String::filterString($_REQUEST[$varname], $filterList); } //return $_REQUEST[$varname]; } else { return null; } }
/** * 获得权限相关的sql条件。 * 外边需要用AND来做。 * * @param string $tablename * @return string */ public static function getCond($tablename = '') { // if( $tablename ){ // return ( " $tablename.ZU_ID = 'xxxxxx'" ); // }else{ // return ( " ZU_ID = 'xxxxxx'" ); // } //return ( $tablename.ZU_ID = 'xxxxxx' ) if (!defined('ADMIN') || !ADMIN) { $zu_id = Watt_Session::getSession()->getGroupId(); if ($tablename) { $tablename = $tablename . '.'; } return " ({$tablename}shifoushanchu='n' and ( {$tablename}ZU_ID='{$zu_id}' or {$tablename}ZU_ID is null or {$tablename}ZU_ID = '') ) "; } else { return " (1=1) "; } }
/** * 记录日志 * * @param string $msg 记录的信息 * @param int $level * @param string $sourceName * @param string $actorName * @param string $actorId * @param mix $exts * @return boolean */ public function log($msg, $level = 0, $sourceName = "", $actorName = "", $actorId = "", $exts = null, $extsInt = null) { $rev = false; $datetime = date("Y-m-d H:i:s"); $timestamp = time(); $ip = $_SERVER['REMOTE_ADDR']; if (Watt_Session::getSession()) { //如果是岗位用户 那么还用原来的用户ID 2007-10-24 john if (@$_SESSION["shanggang"]) { $yh_id = $_SESSION["old_user_id"]; $yhs = TpmYonghuPeer::retrieveByPK($yh_id); $user_id = $yhs->getYhId(); $user_name = $yhs->getYhZhanghu(); $c = new Criteria(); $c->add(TpmYonghu2juesePeer::YH_ID, $yh_id); $jsids = TpmYonghu2juesePeer::doSelectOne($c); if ($jsids) { $js_id = $jsids->getJsId(); $c = new Criteria(); $c->add(TpmJuesePeer::JS_ID, $js_id); $jueses = TpmJuesePeer::doSelectOne($c); if ($jueses) { $user_js_id = $jueses->getJsId(); $user_js_mingcheng = $jueses->getJsMingcheng(); } } } else { $user_id = Watt_Session::getSession()->getUserId(); $user_name = Watt_Session::getSession()->getUserName(); $user_js_id = Watt_Session::getSession()->getRoleId(); $user_js_mingcheng = Watt_Session::getSession()->getRoleName(); } /*$session_id = Watt_Session::getSession()->getUserId(); $session_name = Watt_Session::getSession()->getUserName(); $js_id = Watt_Session::getSession()->getRoleId(); $js_mingcheng = Watt_Session::getSession()->getRoleName(); */ $session_id = $user_id; $session_name = $user_name ? $user_name : $actorName; $js_id = $user_js_id; $js_mingcheng = $user_js_mingcheng; } else { $session_id = ''; $session_name = $actorName; $js_id = ''; $js_mingcheng = ''; } $app = App::getApp(); $app->_add($this->_logName, array('yh_id' => $session_id, 'yh_zhanghu' => chks($session_name), 'js_id' => $js_id, 'js_mingcheng' => chks($js_mingcheng), 'rz_level' => $level, 'rz_ip' => $ip, 'rz_type' => chks($sourceName), 'rz_ruanjian' => chks($_SERVER["HTTP_USER_AGENT"]), 'rz_laiyuan' => isset($_SERVER["HTTP_REFERER"]) ? chks($_SERVER["HTTP_REFERER"]) : null, 'rz_neirong' => chks($msg), 'rz_dizhi' => chks($_SERVER['REQUEST_URI']), 'rz_qita_vchar' => chks($exts), 'rz_qita_int' => chks($extsInt), 'created_at' => $timestamp)); /* $log = new TpmRizhi(); $log->setYhId( $session_id ); //用户名 $log->setYhZhanghu( $session_name ); $log->setRzLevel( $level ); $log->setRzIp( $ip ); $log->setRzRuanjian( $_SERVER["HTTP_USER_AGENT"] ); $log->setRzType( $sourceName ); //即日志的逻辑标示 if( isset( $_SERVER["HTTP_REFERER"] ) ){ $log->setRzLaiyuan( $_SERVER["HTTP_REFERER"] ); } $log->setRzNeirong( $msg ); $log->setRzDizhi($_SERVER['REQUEST_URI']); $log->setRzQitaVchar( $exts ); $log->setCreatedAt( $timestamp ); $rev = $log->save();*/ /* rz_id yh_id yh_zhanghu rz_level rz_ip rz_type rz_ruanjian rz_laiyuan rz_neirong rz_dizhi rz_qita_vchar rz_qita_int created_at */ return $rev; }
/** * 载入语言串 * * @todo I18n优化 速度 设置语言的位置 */ private static function _loadStrings() { if (self::$isStringsLoaded) { return; } Watt_Debug::addInfoToDefault('Before load language string.'); self::setLang(Watt_Session::getSession()->getLanguage()); self::_connectMemcache(); if (self::$_memcache) { if (self::$_memcache->get(self::$_memcacheKeyPrefix . self::$lang . '_LOADED')) { self::$isStringsLoaded = true; Watt_Debug::addInfoToDefault('After load language string.'); return; } Watt_Debug::addInfoToDefault('Before load language string from memcache.'); } //include后 1.8 1.9 ms左右 //include前 1.5 1.6 ms左右 include 还挺费时间... //考虑放到各个模块里..用到那个载入哪个? //这个更像php风格 //还是放到一起一并载入? //先每个人用到的分开,最后合并 $langPath = Watt_Config::getLangPath() . self::$lang . DIRECTORY_SEPARATOR; self::_loadLangFilesInDir($langPath); //载入备用目录的语言文件 // $langPath = Watt_Config::getLangPath(1) . self::$lang . DIRECTORY_SEPARATOR; self::_loadLangFilesInDir($langPath); /* 合并语言文件以后用下面这段代码,合并以前用上面那段 $str2s = include_once( Watt_Config::getLangPath() .self::$lang .DIRECTORY_SEPARATOR."lang.php" ); self::$strings = array_merge( self::$strings, $str2s); */ if (self::$_memcache) { self::$_memcache->set(self::$_memcacheKeyPrefix . self::$lang . '_LOADED', true); Watt_Debug::addInfoToDefault('After load language string from memcache.'); } self::$isStringsLoaded = true; Watt_Debug::addInfoToDefault('After load language string.'); }
/** * @todo 解决和dialog重复的问题 * * @param boolean $show * @return string */ public function render($show = true) { $haveHeaderInfo = $this->_header && is_array($this->_header); /** * 是否输出主体,默认输出 */ $outputBody = true; //ob_start(); //echo "Html Header"; //echo "Body Header"; if ($haveHeaderInfo) { //这里展开了 sys_title 和 tpm_css extract($this->_header); } if (!isset($sys_title) || $sys_title == "") { $sys_title = "TPM"; } $sys_title .= "[" . i18ntrans(Watt_Session::getSession()->getRoleName()) . "." . Watt_Session::getSession()->getUserName() . "]"; include $this->_getAbsViewPathFilename("inc/header.html.php"); /** * 这里显示 Tq消息 * Tq消息要在 tip 之前 */ echo Tpm_Message_Sender_Tq::getMsgHtml(); if ($haveHeaderInfo) { /** * 处理在 ctrl 里设置的tip提示信息 */ if (key_exists(Watt_Controller_Action::HEADER_TIP, $this->_header)) { $tip = $this->_header[Watt_Controller_Action::HEADER_TIP]; echo "<script>"; $msg = $tip[Watt_Controller_Action::HEADER_TIP_MSG]; if ($nextUrl = $tip[Watt_Controller_Action::HEADER_TIP_URL]) { $matchs = null; if (preg_match("/^javascript:(.*)/", $nextUrl, $matchs)) { // echo "function onloadTip(){Ext.Msg.alert('TPM', '".addslashes($tip[Watt_Controller_Action::HEADER_TIP_MSG])."', function(){{$matchs[1]};return false;});}"; echo "function onloadTip() {" . ($msg ? "alert('" . addslashes($msg) . "');" : '') . "{$matchs[1]}}"; } else { // echo "function onloadTip(){Ext.Msg.alert('TPM', '".addslashes($tip[Watt_Controller_Action::HEADER_TIP_MSG])."', function(){location.href='".$nextUrl."';return false;});}"; echo "function onloadTip() {" . ($msg ? "alert('" . addslashes($msg) . "');" : '') . "location.href='" . $nextUrl . "'}"; } //如果有转向,则不输出主体 $outputBody = false; } else { // echo "function onloadTip(){Ext.Msg.alert('TPM', '".addslashes($tip[Watt_Controller_Action::HEADER_TIP_MSG])."');}"; echo "function onloadTip() {" . ($msg ? "alert('" . addslashes($msg) . "');" : '') . "}"; } echo "window.onload = onloadTip;"; echo "</script>"; } } if ($outputBody) { //没有设置不显示主体 //这里显示菜单 有true 是因为 菜单数据目前是在Watt_View_Helper_Menu里的 if (isset($this->_header["menu"])) { $menu = new Watt_View_Helper_Menu(); $menu->buildMenu($this->_header["menu"]); } /** * 这里显示主体部分 */ parent::render(true); } /** * 这里显示底部 */ include $this->_getAbsViewPathFilename("inc/footer.html.php"); // $out = ob_get_clean(); // if( $show ) // { // echo $out; // } // return $out; }
/** * 是否是外部用户 * @return boolean * @author terry * Tue Feb 17 14:07:05 CST 2009 */ public function isOutterUser() { if (Watt_Config::isFlowMode()) { $crSessionRoleId = array('6b32ff50-df19-4e07-d50c-45b6b62bc171' => 'CR', '4ade1c61-fac6-8f11-4200-466fa0a2c627' => 'CR', '8fdee018-5bd1-1a17-61c4-491a8b139cf9' => 'CRCPM', '2798de2b-30bf-9dcb-22cd-45b6b68b315e' => 'TR', '61c705eb-0cde-4867-3211-45b6b6753d4d' => 'PR', '84f3fb25-f8f2-0f43-e33f-4b8c751b7280' => 'EDIT'); //说明这个是客户的角色ID $crSessionRoleShortName = array('CR' => 'CR', 'TR' => 'TR', 'PR' => 'PR', 'EDIT' => 'EDIT'); } else { $crSessionRoleId = array('6b32ff50-df19-4e07-d50c-45b6b62bc171' => 'CR', '2798de2b-30bf-9dcb-22cd-45b6b68b315e' => 'TR', '61c705eb-0cde-4867-3211-45b6b6753d4d' => 'PR', '4ade1c61-fac6-8f11-4200-466fa0a2c627' => 'CR', '8fdee018-5bd1-1a17-61c4-491a8b139cf9' => 'CRCPM', '84f3fb25-f8f2-0f43-e33f-4b8c751b7280' => 'EDIT'); //说明这个是客户的角色ID $crSessionRoleShortName = array('CR' => 'CR', 'TR' => 'TR', 'PR' => 'PR', 'EDIT' => 'EDIT'); } $sessionRoleId = Watt_Session::getSession() ? Watt_Session::getSession()->getRoleId() : ''; $sessionRoleShortName = Watt_Session::getSession() ? Watt_Session::getSession()->getRoleShortName() : ''; return key_exists($sessionRoleId, $crSessionRoleId) || key_exists($sessionRoleShortName, $crSessionRoleShortName); }
/** * 检查 某个 会话是否具有访问某个 controller 的 某个 action 的权限 * 如果没有权限,则抛出一个异常 * * @param WATT_SESSION $session * @param Watt_Controller_Action $ctrlObj * @param string $actionName * @return boolean|TpmQuanxian */ public static function checkActionPrivilege(WATT_SESSION $session, Watt_Controller_Action $ctrlObj, $actionName) { /** * 危险的东西 * 免登陆 * @author terry */ /** * 注释掉了 * @author terry * @version 0.1.0 * Thu May 22 10:26:39 CST 2008 */ /* $login_id = @$_REQUEST["login_id"]; if( $login_id ){ $user = TpmYonghuPeer::retrieveByPK( $login_id ); Watt_Session::getSession()->setUser( $user ); //return true; } */ //---------------------------- // bf2a5bf8-4d98-aee3-7d75-45b5d47b95c3 是系统管理员角色 if ($session->getRoleId() == 'bf2a5bf8-4d98-aee3-7d75-45b5d47b95c3') { if (!defined('ADMIN')) { define('ADMIN', true); } } //如果return true,则拥有所有权限 //return true; /** * 暂时取消权限验证 2007-1-16 */ $rev = false; if ($ctrlObj->isPublic()) { $rev = true; } elseif ($ctrlObj->isActionPublic($actionName)) { $rev = true; } elseif ($session->getUserId()) { //echo $ctrlObj->getActionLevel( $actionName); if (self::LEVEL_LOGIN == $ctrlObj->getActionLevel($actionName)) { $rev = true; } else { //这里进行针对 action 的权限校验 //$privilege_do = $ctrlObj->getControllerName()."_".$actionName; $privilege_do = $ctrlObj->getMappingedPrivilegeByAction($actionName); //$rev = TpmQuanxianPeer::jianchaYonghuQuanxian($session->getUserId(), $privilege_do); $rev = TpmQuanxianPeer::jianchaJueseQuanxian($session->getRoleId(), $privilege_do); /** * 全注释是不检验action权限,也就是最高登录权限 * @todo 外部配置检验级别 * @author terry * @version 0.1.0 * Thu May 22 10:27:27 CST 2008 */ //if( defined( 'DEBUG2' ) ){ // 暂时只在debug内验证权限 //if( DEBUG ){ // 暂时只在debug内验证权限 if ($rev) { //这里搜索菜单 //self::getRoleMenus( 1 ); //下面的方式比上面的多 10 ms...研究 //TpmMenuPeer::getRoleMenus( 1 ); } else { $rev = false; throw new Watt_Exception(Watt_I18n::trans("EXCEPTION_NO_PRIVILEGE"), Watt_Exception::EXCEPTION_NO_PRIVILEGE); } //} } } else { Watt_Session::getSession()->recordCurrentVisitPage(); $rev = false; throw new Watt_Exception(Watt_I18n::trans("EXCEPTION_NEED_LOGIN"), Watt_Exception::EXCEPTION_NEED_LOGIN); } return $rev; }
/** * 处理 do * 有可能会返回 goToDo * * 只有一个单词的do,对应的action是 index * * * @param string $do * @return string $goToDo */ protected function processDo($do, $defaultView = "Html") { if ($do == "") { $e = new Watt_Exception(Watt_I18n::trans("ERR_DISPATCH_NODO")); throw $e; } $goToDo = ""; $arrCtrlAndAction = $this->_analyzeDoToControllerAndAction($do); $controller = $arrCtrlAndAction[0]; $action = $arrCtrlAndAction[1]; $doFile = $arrCtrlAndAction[2]; $doAction = $arrCtrlAndAction[3]; //exit( $doFile . "|" . $doAction ); //使用 ob_start 是为了Controller里的 redirect 可以正常使用 if (defined('ENABLE_CTRL_BUFFER') && ENABLE_CTRL_BUFFER) { /** * 为了不让服务器过长等待时间,改为不启用Ctrl Buffer * @author terry * @version 0.1.0 * Mon Jan 14 14:41:39 CST 2008 */ ob_start(); } /** * 增加了对页面缓存的支持 */ $cache = null; Watt_Debug::addInfoToDefault('Begin create action [' . $controller . '] [' . $action . ']'); $theCtrl = Watt_Controller_Action::factory($controller, $action); Watt_Debug::addInfoToDefault('After create action'); $viewMenu = isset($_REQUEST["view_menu"]) ? trim($_REQUEST["view_menu"]) == '0' ? false : true : true; $theCtrl->setNeedMenu($viewMenu); $actionCacheTime = $theCtrl->getActionCacheTime($action); if ($actionCacheTime > 0) { $cache = new Watt_Cache($actionCacheTime); if ($cache->cacheCheck()) { //如果符合缓存条件,则会读取缓存文件,并 exit. /** * 改为退出处理,为了记录页面执行时间。 * 这里一定不能 return true. * @author terry * @version 0.1.0 * Mon Jan 14 14:30:43 CST 2008 */ return ''; } } /** * 检查 会话的权限。 * 如果没有权限,抛出一个异常 * 此处别扭 */ $rbac = new Watt_Rbac(); //$rbac->checkSession(Watt_Session::getSession(), $do); $privilege = $rbac->checkActionPrivilege(Watt_Session::getSession(), $theCtrl, $action); if (is_object($privilege) && $privilege instanceof TpmYonghuzhaoquanxian) { if (!$theCtrl->getTitle()) { $theCtrl->setTitle(Watt_I18n::trans($privilege->getQxMingcheng())); } } Watt_Debug::addInfoToDefault('', 'Pre do action..'); if (method_exists($theCtrl, $doAction)) { //执行controller中的action $theCtrl->{$doAction}(); } else { throw new Exception(Watt_I18n::trans("ERR_APP_LOST_ACTION")); } Watt_Debug::addInfoToDefault('', 'After do action..'); $goToDo = $theCtrl->getGoToDo(); $data = $theCtrl->getData(); /** * 改为对 Ctrl 不进行 Buffer 的处理 * @author terry * @version 0.1.0 * Mon Jan 14 15:05:28 CST 2008 */ if (defined('ENABLE_CTRL_BUFFER') && ENABLE_CTRL_BUFFER) { if (defined("DEBUG") && DEBUG) { //调试阶段才显示Controller里输出的信息 echo ob_get_clean(); } else { //用户使用阶段不允许 action 里输出显示数据 ob_clean(); } } if ($theCtrl->isNeedView()) { Watt_Debug::addInfoToDefault('', 'Pre load view..'); if ($theCtrl->getViewType()) { $defaultView = $theCtrl->getViewType(); } /** * 创建一个View。将来可以用不同的View代替此View */ //$view = Watt_View::factory( "Html", Watt_Config::getViewPath() ); $view = Watt_View::factory($defaultView, Watt_Config::getViewPath()); $view->setHeader($theCtrl->getHeader()); Watt_Debug::addInfoToDefault('', 'After view factory..'); /** * 读取菜单应该由View来判断 * @author terry * Thu Jul 22 10:46:07 CST 2010 */ if ($theCtrl->isNeedMenu() && strtolower($defaultView) == 'html') { //$theCtrl->isNeedCaidan(); /** * 如果用户已登录,读取菜单信息 * @todo 未登录可能也可以有菜单 */ if ($user_id = Watt_Session::getSession()->getUserId()) { $tpmCaidans = TpmCaidanPeer::getJueseCaidan(Watt_Session::getSession()->getRoleId()); if (count($tpmCaidans)) { $view->setHeader($tpmCaidans, "menu"); } } //读取菜单完 } //$view->renderModel($theCtrl); $view->renderView($data, $this->_getDefaultViewFileOfAction($controller, $action), true); } /** * 与开始的Cache对应 */ if ($actionCacheTime > 0 && $cache instanceof Watt_Cache) { $cache->caching(); } return $goToDo; }
public static function buildMenuEx($data, $show = true) { //没调好 先藏掉 //Terry //return ""; $xhtml = ""; if (is_array($data)) { $menu_arr = self::sortMenu($data); $roleId = Watt_Session::getSession()->getRoleId(); $crSessionRoleId = array('6b32ff50-df19-4e07-d50c-45b6b62bc171' => 'CR', '2798de2b-30bf-9dcb-22cd-45b6b68b315e' => 'TR', '4ade1c61-fac6-8f11-4200-466fa0a2c627' => 'CR', '61c705eb-0cde-4867-3211-45b6b6753d4d' => 'PR'); // if( key_exists( $roleId, $crSessionRoleId ) ){ // $xhtml .= "<link rel=\"stylesheet\" href=\"{$this->_menu_lib_path}xqtrmenu.css\">"; // }else{ // $xhtml .= "<link rel=\"stylesheet\" href=\"{$this->_menu_lib_path}xqmenu.css\">"; // } $xhtml = '<div id="mainmenu" style="" onmouseout="this.style.left=\'-118px\'" onmouseover="$(\'mainmenu\').style.left=\'0px\'">' . "\n"; // $xhtml .= '<ul>'; // foreach ( $data as $key=>$val) // { // $xhtml .= '<li><a href="'.$val[1].'">'.$val[0].'</a></li>'."\n"; // } // $xhtml .= '</ul>'; // 结束 if (Watt_Session::getSession()->getSession()->getRoleShortname() == 'CR') { $xhtml .= "<div class='quick_order'>"; if (Watt_Session::getSession()->isTq()) { $xhtml .= "<a TQCmd='CmdType=CallOrder' id='TQCmdTag_CallOrder' style='cursor:hand;' TQFileType=\"" . Watt_I18n::trans("EC_PUBLIC_FILETYPES_LIST") . "\">" . Watt_I18n::trans('快速下单') . "</a>"; } else { //$xhtml .= "<a id='TQCmdTag_CallOrder' style='cursor:hand;' href='?do=ec_dingdan_add'\">".Watt_I18n::trans('快速下单')."</a>"; $xhtml .= "<a id='TQCmdTag_CallOrder' style='cursor:hand;' href='?do=twftpm_start_startNewFlowAndExecute&lclx_id=16'\">" . Watt_I18n::trans('快速下单') . "</a>"; } $xhtml .= '</div>'; } $xhtml .= '<div class="cls"></div>' . "\n"; $xhtml .= '<div style="float:left;width:117px;background-color:#FFF;">' . "\n"; $xhtml .= self::_buildMenuArr($menu_arr, 'menulist'); $xhtml .= '</div>' . "\n"; $xhtml .= '<div class="cls"></div>' . "\n"; $xhtml .= '</div>'; // echo "<pre>Terry at [".__FILE__."(line:".__LINE__.")]\nWhen [Thu Jul 17 11:48:15 CST 2008] :\n "; // var_dump( $menu_arr ); // echo "</pre>"; // exit(); } if ($show) { echo $xhtml; } return $xhtml; }