* @package common * @version $Id: model.php 4150 2016-10-17 08:06:43Z liugang $ * @link https://xuanim.com */ class commonModel extends model { /** * Do some init functions. * * @access public * @return void */ public function __construct($appName = '') { parent::__construct($appName); $this->startSession(); $this->setUser(); $this->loadConfigFromDB(); if(!$this->checkIP()) die($this->lang->ipLimited); $this->loadLangFromDB(); } /** * Load configs from database and save it to config->system and config->personal. * * @access public * @return void */ public function loadConfigFromDB() { /* Upgrade to 2.5.1 rename table. */ $version = $this->config->version; $prefix = $this->config->db->prefix; /* Get configs of system and current user. */ $account = isset($this->app->user->account) ? $this->app->user->account : ''; if(!empty($this->config->db->name)) $config = $this->loadModel('setting')->getSysAndPersonalConfig($account); $this->config->system = isset($config['system']) ? $config['system'] : new stdclass(); $this->config->personal = isset($config[$account]) ? $config[$account] : new stdclass(); foreach($this->config->system as $module => $records) { /* Overide the items defined in config/config.php and config/my.php. */ if(!isset($this->config->$module)) $this->config->$module = new stdclass(); if(isset($this->config->system->$module)) helper::mergeConfig($this->config->system->$module, $module); } foreach($this->config->personal as $module => $records) { /* Overide the items defined in config/config.php and config/my.php. */ if(!isset($this->config->$module)) $this->config->$module = new stdclass(); if(isset($this->config->personal->$module)) helper::mergeConfig($this->config->personal->$module, $module); } /* Overide the items defined in config/config.php and config/my.php. */ if(isset($this->config->system->common)) helper::mergeConfig($this->config->system->common, 'common'); if(isset($this->config->personal->common)) helper::mergeConfig($this->config->personal->common, 'common'); } /** * Load custom lang from DB. * * @access public * @return void */ public function loadLangFromDB() { if(!$this->config->db->name) return; $records = $this->loadModel('setting')->getAllLang(); if(!$records) return; $this->lang->db = new stdclass(); $this->lang->db->custom = $records; } /** * Start the session. * * @access public * @return void */ public function startSession() { if(!defined('SESSION_STARTED')) { $sessionName = $this->config->sessionVar; if(isset($_GET[$sessionName])) session_id($_GET[$sessionName]); session_name($sessionName); session_start(); define('SESSION_STARTED', true); } } /** * Check the Privilege. * * @access public * @return void */ public function checkPriv() { if(!empty($this->config->group->unUpdatedAccounts) and strpos($this->config->group->unUpdatedAccounts, $this->app->user->account) !== false) { $user = $this->app->user; $user->rights = $this->loadModel('user')->authorize($user); $this->session->set('user', $user); $this->app->user = $this->session->user; $unUpdatedAccounts = str_replace($this->app->user->account, '', $this->config->group->unUpdatedAccounts); if(str_replace(',', '', $unUpdatedAccounts) == '') $unUpdatedAccounts = ''; $this->loadModel('setting')->setItem("system.sys.group.unUpdatedAccounts", $unUpdatedAccounts); } $module = $this->app->getModuleName(); $method = $this->app->getMethodName(); if($this->isOpenMethod($module, $method)) return true; /* Try to identify by cookie if not login. */ if(!$this->loadModel('user')->isLogon() and $this->cookie->keepLogin == 'on') $this->user->identifyByCookie(); /* If no $app->user yet, go to the login pae. */ if($this->app->user->account == 'guest') { $referer = helper::safe64Encode($this->app->getURI(true)); die(js::locate(helper::createLink('user', 'login', "referer=$referer"))); } /* Check the Privilege. */ if(!commonModel::hasPriv($module, $method)) $this->deny($module, $method); } /** * Check current user has Privilege to the module's method or not. * * @param mixed $module the module * @param mixed $method the method * @static * @access public * @return bool */ public static function hasPriv($module, $method) { global $app; if(!commonModel::isLicensedMethod($module, $method)) return false; if($app->user->admin == 'super') return true; if(RUN_MODE == 'admin') return false; $rights = $app->user->rights; if(isset($rights[strtolower($module)][strtolower($method)])) return true; return false; } /** * Check whether IP in white list. * * @access public * @return bool */ public function checkIP() { $ip = $this->server->remote_addr; $ipWhiteList = $this->config->ipWhiteList; /* If the ip white list is '*'. */ if($ipWhiteList == '*') return true; /* The ip is same as ip in white list. */ if($ip == $ipWhiteList) return true; /* If the ip in white list is like 192.168.1.1-192.168.1.10. */ if(strpos($ipWhiteList, '-') !== false) { list($min, $max) = explode('-', $ipWhiteList); $min = ip2long(trim($min)); $max = ip2long(trim($max)); $ip = ip2long(trim($ip)); return $ip >= $min and $ip <= $max; } /* If the ip in white list is in IP/CIDR format eg 127.0.0.1/24. Thanks to zcat. */ if(strpos($ipWhiteList, '/') == false) $ipWhiteList .= '/32'; list($ipWhiteList, $netmask) = explode('/', $ipWhiteList, 2); $ip = ip2long($ip); $ipWhiteList = ip2long($ipWhiteList); $wildcard = pow(2, (32 - $netmask)) - 1; $netmask = ~ $wildcard; return (($ip & $netmask) == ($ipWhiteList & $netmask)); } /** * Show the deny info. * * @param mixed $module the module * @param mixed $method the method * @access public * @return void */ public function deny($module, $method) { if(helper::isAjaxRequest()) { $this->app->loadLang($module); $this->app->loadLang('user'); $moduleName = isset($this->lang->$module->common) ? $this->lang->$module->common: $module; $methodName = isset($this->lang->$module->$method) ? $this->lang->$module->$method: $method; $data = sprintf($this->lang->error->deny, $moduleName, $methodName); print(json_encode($data)) and die(helper::removeUTF8Bom(ob_get_clean())); } /* Get authorize again. */ $user = $this->app->user; $user->rights = $this->loadModel('user')->authorize($user); $this->session->set('user', $user); $this->app->user = $this->session->user; if(commonModel::hasPriv($module, $method)) return true; $vars = "module=$module&method=$method"; if(isset($_SERVER['HTTP_REFERER'])) { $referer = helper::safe64Encode($_SERVER['HTTP_REFERER']); $vars .= "&referer=$referer"; } $denyLink = helper::createLink('user', 'deny', $vars); die(js::locate($denyLink)); } /** * Judge a method of one module is open or not? * * @param string $module * @param string $method * @access public * @return bool */ public function isOpenMethod($module, $method) { if($module == 'user' and strpos(',login|logout|deny|control', $method)) return true; if($module == 'api' and $method == 'getsessionid') return true; if($module == 'misc' and $method == 'ping') return true; if($module == 'misc' and $method == 'ignorenotice') return true; if($module == 'action' and $method == 'read') return true; if($module == 'sso' and strpos(',auth|check|gettodolist|leaveusers', $method)) return true; if($module == 'file' and $method == 'read') return true; if($module == 'file' and $method == 'download') return true; if($module == 'im' and $method == 'authorize') return true; if($module == 'integration' and $method == 'wopi') return true; if($module == 'block') return true; if($module == 'notice') return true; if($this->loadModel('user')->isLogon() and stripos($method, 'ajax') !== false) return true; return false; } /** * Check if the method is licensed. * * @param string $module * @param string $method * @param string $extra can be message type, etc. * @static * @access public * @return boolean */ public static function isLicensedMethod($module, $method, $extra = '') { $permissions = commonModel::getLicensePropertyValue('permissions'); if($module == 'integration' && strpos('office|wopi', $method) !== false) { return strpos($permissions, 'integration/office') !== false; } if($module == 'restriction' && $method == 'index') { return strpos($permissions, 'restriction/index') !== false; } if($module == 'ldap' && $method == 'index') { return strpos($permissions, 'ldap/index') !== false; } if($module == 'sso' && $method == 'check') { return strpos($permissions, 'sso/check') !== false; } if($module == 'im') { if($method == 'roadrunner') return strpos($permissions, 'im/roadrunner') !== false; if($method == 'fileEncrypt') return strpos($permissions, 'im/fileEncrypt') !== false; if($method == 'messageEncrypt') return strpos($permissions, 'im/messageEncrypt') !== false; } if($module == 'conference') { if($method == 'detachedConference') return strpos($permissions, 'conference/detachedConference') !== false; } return true; } /** * Get license properties * * @static * @access public * @return array|bool */ public static function getLicenseProperties() { if(!function_exists('ioncube_file_is_encoded') || !ioncube_file_is_encoded()) { global $config; return isset($config->licenseData) ? $config->licenseData : false; } if(!function_exists('ioncube_license_properties')) return false; return ioncube_license_properties(); } /** * Get license property * * @param string $propertyName * @static * @access public * @return array|bool */ public static function getLicenseProperty($propertyName) { $properties = commonModel::getLicenseProperties(); if($properties !== false && !empty($properties[$propertyName])) { return $properties[$propertyName]; } return false; } /** * Get license property value * * @param string $propertyName * @static * @access public * @return string|bool */ public static function getLicensePropertyValue($propertyName) { $property = commonModel::getLicenseProperty($propertyName); if($property !== false) { return $property['value']; } return false; } /** * Create the main menu. * * @param string $currentModule * @static * @access public * @return string */ public static function createMainMenu($currentModule = '', $currentMethod = '') { global $app, $lang, $config; /* Set current module. */ if(isset($lang->menuGroups->$currentModule)) $currentModule = $lang->menuGroups->$currentModule; $isMobile = $app->viewType === 'mhtml'; $string = !$isMobile ? "\n" : ''; return $string; } /** * Create the module menu. * * @param string $currentModule * @static * @access public * @return string */ public static function createModuleMenu($currentModule) { global $lang, $app, $config; /* Get current method. */ $currentMethod = $app->getMethodName(); if(!isset($lang->$currentModule->menu)) return false; $isMobile = $app->viewType === 'mhtml'; $string = !$isMobile ? "\n" : ''; return $string; } /** * Get Link From Submenu. * * @param string $menuGroup * @access public * @return string */ public static function getLinkFromSubmenu($menuGroup) { global $lang, $config; if(!isset($lang->$menuGroup->menu)) return ''; foreach($lang->$menuGroup->menu as $code => $menu) { if(is_array($menu)) $menu = $menu['link']; list($label, $moduleName, $methodName, $vars) = explode('|', $menu); if(commonModel::hasPriv($moduleName, $methodName)) return helper::createLink($moduleName, $methodName, $vars); } return ''; } /** * Print position bar * * @param object $module * @param object $object * @param mixed $misc other params. * @access public * @return void */ public function printPositionBar($module = '', $object = '', $misc = '', $root = '') { echo ''; } /** * Print the link contains orderBy field. * * This method will auto set the orderby param according the params. For example, if the order by is desc, * will be changed to asc. * * @param string $fieldName the field name to sort by * @param string $orderBy the order by string * @param string $vars the vars to be passed * @param string $label the label of the link * @param string $module the module name * @param string $method the method name * @static * @access public * @return void */ public static function printOrderLink($fieldName, $orderBy, $vars, $label, $module = '', $method = '', $print = true) { global $lang, $app; if(empty($module)) $module = $app->getModuleName(); if(empty($method)) $method = $app->getMethodName(); $className = ''; $isMobile = $app->viewType === 'mhtml'; $fieldOrderByDesc = strtolower($fieldName . '_desc'); $fieldOrderByAsc = strtolower($fieldName . '_asc'); $orderByLower = strtolower($orderBy); if($fieldOrderByDesc == $orderByLower) { $orderBy = str_ireplace('_desc', '_asc', $orderBy); $className = 'SortDown'; } elseif($fieldOrderByAsc == $orderByLower) { $orderBy = str_ireplace('asc', 'desc', $orderBy); $className = 'SortUp'; } else $orderBy = $fieldName . '_' . 'asc'; $link = helper::createLink($module, $method, sprintf($vars, $orderBy)); $html = ''; if(!$isMobile) $html = "
" . html::a($link, $label) . '
'; else $html = html::a($link, $label, "class='$className'"); if(!$print) return $html; echo$html; } /** * Set the user info. * * @access public * @return void */ public function setUser() { if($this->session->user) return $this->app->user = $this->session->user; /* Create a guest account. */ $user = new stdclass(); $user->id = 0; $user->dept = 0; $user->account = 'guest'; $user->realname = 'guest'; $user->admin = RUN_MODE == 'cli' ? 'super' : 'no'; $user->rights = array(); $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; } /** * Print top bar. * * @static * @access public * @return void */ public static function printTopBar() { global $lang, $app; printf($lang->todayIs, date(DT_DATE4)); if(isset($app->user)) echo $app->user->realname . ' '; if(isset($app->user) and $app->user->account != 'guest') { echo html::a(helper::createLink('user', 'logout'), $lang->logout); } else { echo html::a(helper::createLink('user', 'login'), $lang->login); } echo ' |  '; echo html::a(helper::createLink('misc', 'about'), $lang->about, '', "class='about'"); echo ' | '; echo html::select('', $app->config->langs, $app->cookie->lang, 'onchange="selectLang(this.value)"'); } /** * Print the main menu. * * @param string $moduleName * @static * @access public * @return void */ public static function printMainmenu($moduleName, $methodName = '') { global $app, $lang; echo "