GetConfig(); //Handle a current login if one is in queue in the SESSION if (isset($_SESSION['login_user_id'])) { debug_buffer("Found login_user_id. Going to generate the user object."); generate_user_object($_SESSION['login_user_id']); unset($_SESSION['login_user_id']); } if (isset($_SESSION['login_cms_language'])) { debug_buffer('Setting language to: ' . $_SESSION['login_cms_language']); cms_cookies::set('cms_language', $_SESSION['login_cms_language']); unset($_SESSION['login_cms_language']); } if (!isset($_SESSION["cms_admin_user_id"])) { debug_buffer('No session found. Now check for cookies'); if (isset($_COOKIE["cms_admin_user_id"]) && isset($_COOKIE["cms_passhash"])) { debug_buffer('Cookies found, do a passhash check'); if (check_passhash($_COOKIE["cms_admin_user_id"], $_COOKIE["cms_passhash"])) { debug_buffer('passhash check succeeded... creating session object'); generate_user_object($_COOKIE["cms_admin_user_id"]); } else { debug_buffer('passhash check failed... redirect to login'); $_SESSION["redirect_url"] = $_SERVER["REQUEST_URI"]; if (false == $no_redirect) { redirect($config['admin_url']."/login.php"); } return false; } } else { debug_buffer('No cookies found. Redirect to login.'); $_SESSION["redirect_url"] = $_SERVER["REQUEST_URI"]; if (false == $no_redirect) { redirect($config['admin_url']."/login.php"); } return false; } } global $CMS_ADMIN_PAGE; if( ($config['debug'] === false) && isset($CMS_ADMIN_PAGE) ) { if( !isset($_SESSION[CMS_USER_KEY]) ) { // it's not in the session, try to grab something from cookies if( isset($_COOKIE[CMS_SECURE_PARAM_NAME]) ) { $_SESSION[CMS_USER_KEY] = $_COOKIE[CMS_SECURE_PARAM_NAME]; } } // now we've got to check the request // and make sure it matches the session key if( !isset($_SESSION[CMS_USER_KEY]) || !isset($_GET[CMS_SECURE_PARAM_NAME]) || !isset($_POST[CMS_SECURE_PARAM_NAME]) ) { $v = ''; if( isset($_GET[CMS_SECURE_PARAM_NAME]) ) { $v = $_GET[CMS_SECURE_PARAM_NAME]; } else if( isset($_POST[CMS_SECURE_PARAM_NAME]) ) { $v = $_POST[CMS_SECURE_PARAM_NAME]; } if( $v != $_SESSION[CMS_USER_KEY] && !isset($config['stupidly_ignore_xss_vulnerability']) ) { debug_buffer('Session key mismatch problem... redirect to login'); if (false == $no_redirect) { redirect($config['admin_url'].'/login.php'); } return false; } } } return true; } /** * Gets the userid of the currently logged in user. * * @since 0.1 * @param boolean Redirect to the admin login page if the user is not logged in. * @return integer The UID of the logged in administrator, otherwise FALSE */ function get_userid($check = true) { if ($check) { check_login(); //It'll redirect out to login if it fails } if (isset($_SESSION["cms_admin_user_id"])) { return $_SESSION["cms_admin_user_id"]; } else { return false; } } /** * A function to check if the checksum provided can be used to validate the user to this site * * @internal * @access private * @param int The admin userid. * @param string The checksum variable * @return boolean */ function check_passhash($userid, $checksum) { $check = false; $gCms = cmsms(); $config = $gCms->GetConfig(); $userops = $gCms->GetUserOperations(); $oneuser = $userops->LoadUserByID($userid); $tmp = array(md5(__FILE__),$oneuser->password,cms_utils::get_real_ip(),$_SERVER['HTTP_USER_AGENT']); $tmp = md5(serialize($tmp)); if ($oneuser && (string)$checksum != '' && $checksum == $tmp ) { $check = true; } return $check; } /** * Regenerates the user session information from a userid. This is basically used * so that if the session expires, but the cookie still remains (site is left along * for 20+ minutes with no interaction), the user won't have to relogin to regenerate * the details. * * @internal * @access private * @since 0.5 * @param integer The admin user id * @return void */ function generate_user_object($userid) { $gCms = cmsms(); $config = $gCms->GetConfig(); $userops = $gCms->GetUserOperations(); $oneuser = $userops->LoadUserByID($userid); if ($oneuser) { $_SESSION['cms_admin_user_id'] = $userid; $_SESSION['cms_admin_username'] = $oneuser->username; cms_cookies::set('cms_admin_user_id', $oneuser->id); $tmp = array(md5(__FILE__),$oneuser->password,cms_utils::get_real_ip(),$_SERVER['HTTP_USER_AGENT']); $tmp = md5(serialize($tmp)); cms_cookies::set('cms_passhash', $tmp); } } /** * Loads all permissions for a particular user into a global variable so we don't hit the db for every one. * * @internal * @access private * @since 0.8 * @param int The user id * @return void */ function load_all_permissions($userid) { $gCms = cmsms(); $db = $gCms->GetDb(); $variables =& $gCms->variables; $perms = array(); $query = "SELECT DISTINCT permission_name FROM ".cms_db_prefix()."user_groups ug INNER JOIN ".cms_db_prefix()."group_perms gp ON gp.group_id = ug.group_id INNER JOIN ".cms_db_prefix()."permissions p ON p.permission_id = gp.permission_id INNER JOIN ".cms_db_prefix()."groups gr ON gr.group_id = ug.group_id WHERE ug.user_id = ? AND gr.active = 1"; $result = &$db->Execute($query, array($userid)); while ($result && !$result->EOF) { $perms[] =& $result->fields['permission_name']; $result->MoveNext(); } if ($result) $result->Close(); $variables['userperms'] = $perms; } /** * Checks to see that the given userid has access to the given permission. * Members of the admin group have all permissions. * * @since 0.1 * @param int The user id * @param string The permission name * @return boolean */ function check_permission($userid, $permname) { $check = false; $gCms = cmsms(); $userops = $gCms->GetUserOperations(); $adminuser = $userops->UserInGroup($userid,1); if (!isset($gCms->variables['userperms'])) { load_all_permissions($userid); } if (isset($gCms->variables['userperms'])) { if (in_array($permname, $gCms->variables['userperms']) || $adminuser || ($userid == 1) ) { $check = true; } } return $check; } /** * Checks that the given userid is the owner of the given contentid. * (members of the admin group have all permission) * * @internal * @since 0.1 * @param integer The User ID * @param integer The content id * @param boolean use strict checking (ignored) * @return boolean */ function check_ownership($userid, $contentid = '', $strict = false) { $check = false; $gCms = cmsms(); $userops = $gCms->GetUserOperations(); $adminuser = $userops->UserInGroup($userid,1); if( $adminuser ) return true; if (!isset($gCms->variables['ownerpages'])) { $db = $gCms->GetDb(); $variables = &$gCms->variables; $tmpa = array(); $query = "SELECT content_id FROM ".cms_db_prefix()."content WHERE owner_id = ?"; $result = &$db->Execute($query, array($userid)); while ($result && !$result->EOF) { $tmpa[] = $result->fields['content_id']; $result->MoveNext(); } $gCms->variables['ownerpages'] = $tmpa; if ($result) $result->Close(); } if (isset($gCms->variables['ownerpages'])) { if (in_array($contentid, $gCms->variables['ownerpages'])) { $check = true; } } return $check; } /** * Checks that the given userid has access to modify the given * pageid. This would mean that they were set as additional * authors/editors by the owner. * * @internal * @since 0.2 * @param integer The admin user id * @param integer A valid content id. * @return boolean */ function check_authorship($userid, $contentid = '') { $check = false; $gCms = cmsms(); if (!isset($gCms->variables['authorpages'])) { author_pages($userid); } if (isset($gCms->variables['authorpages'])) { if (in_array($contentid, $gCms->variables['authorpages'])) { $check = true; } } return $check; } /** * Prepares an array with the list of the pages $userid is an author of * * @internal * @since 0.11 * @param integer The user id. * @return array An array of pages this user is an author of. */ function author_pages($userid) { $gCms = cmsms(); $db = $gCms->GetDb(); $userops = $gCms->GetUserOperations(); $variables = &$gCms->variables; if (!isset($variables['authorpages'])) { // Get all of the pages this user owns $query = "SELECT content_id FROM ".cms_db_prefix()."content WHERE owner_id = ?"; $data = $db->GetCol($query, array($userid)); // Get all of the pages this user has access to. $query = "SELECT user_id,content_id FROM ".cms_db_prefix()."additional_users"; $result = $db->GetArray($query); if( is_array($result) && count($result) ) { foreach( $result as $row ) { $uid = $row['user_id']; $content_id = $row['content_id']; if( $uid == $userid ) { $data[] = $content_id; } else if( $uid < 0 ) { $gid = $uid * -1; if( $userops->UserInGroup($userid,$gid) ) { $data[] = $content_id; } } } } $variables['authorpages'] = $data; } return $variables['authorpages']; } /** * Quickly checks that the given userid has access to modify the given * pageid. This would mean that they were set as additional * authors/editors by the owner. * * @since 0.11 * @internal * @param integer The content id to test with * @param array A list of the authors pages. * @return boolean */ function quick_check_authorship($contentid, $hispages) { $check = false; if (in_array($contentid, $hispages)) { $check = true; } return $check; } /** * Put an event into the audit (admin) log. This should be * done on most admin events for consistency. * * @since 0.3 * @param integer The item id (perhaps a content id, or a record id from a module) * @param string The item name (perhaps Content, or the module name) * @param string The action that needs to be audited * @return void */ function audit($itemid, $itemname, $action) { if( !isset($action) ) $action = '-- unset --'; $db = cmsms()->GetDb(); $userid = 0; $username = ''; $ip_addr = ''; if( $itemid == '' ) $itemid = -1; if (isset($_SESSION["cms_admin_user_id"])) { $userid = $_SESSION["cms_admin_user_id"]; $ip_addr = cms_utils::get_real_ip(); } else { if (isset($_SESSION['login_user_id'])) { $userid = $_SESSION['login_user_id']; $username = $_SESSION['login_user_username']; } } if (isset($_SESSION["cms_admin_username"])) { $username = $_SESSION["cms_admin_username"]; } if (!isset($userid) || $userid == "") { $userid = 0; } $query = "INSERT INTO ".cms_db_prefix()."adminlog (timestamp, user_id, username, item_id, item_name, action, ip_addr) VALUES (?,?,?,?,?,?,?)"; $db->Execute($query,array(time(),$userid,$username,$itemid,$itemname,$action,$ip_addr)); } /** * Gets the given site prefernce * * @since 0.6 * @param string The preference name * @param mixed The default value if the preference does not exist * @return mixed */ function get_site_preference($prefname, $defaultvalue = '') { return cms_siteprefs::get($prefname,$defaultvalue); } /** * Removes the given site preference * * @param string Preference name to remove * @param boolean Wether or not to remove all preferences that are LIKE the supplied name * @return void */ function remove_site_preference($prefname,$uselike=false) { return cms_siteprefs::remove($prefname,$uselike); } /** * Sets the given site perference with the given value. * * @since 0.6 * @param string The preference name * @param mixed The preference value (will be stored as a string) * @return void */ function set_site_preference($prefname, $value) { return cms_siteprefs::set($prefname,$value); } /** * Gets the given preference for the given userid. * * @since 0.3 * @param integer The user id * @param string The preference name * @param mixed The default value if the preference is not set for the given user id. * @return mixed. */ function get_preference($userid, $prefname, $default='') { return cms_userprefs::get_for_user($userid,$prefname,$default); } /** * Sets the given perference for the given userid with the given value. * * @since 0.3 * @param integer The user id * @param string The preference name * @param mixed The preference value (will be stored as a string) * @return void */ function set_preference($userid, $prefname, $value) { return cms_userprefs::set_for_user($userid, $prefname,$value); } /** * Strips slashes from an array of values. * * @internal * @param array A reference to an array of strings * @return reference to the cleaned values */ function & stripslashes_deep(&$value) { if (is_array($value)) { $value = array_map('stripslashes_deep', $value); } elseif (!empty($value) && is_string($value)) { $value = stripslashes($value); } return $value; } /** * A method to create a text area control * * @internal * @access private * @param boolean Wether or not we are enabling a wysiwyg. If false, and forcewysiwyg is not empty then a syntax area is used. * @param string The contents of the text area * @param string The name of the text area * @param string An optional class name * @param string An optional ID (HTML ID) value * @param string The optional encoding * @param string Optional style information * @param integer Width (the number of columns) (CSS can and will override this) * @param integer Hieght (the number of rows) (CSS can and will override this) * @param string Optional name of the syntax hilighter or wysiwyg to use. If empty, preferences indicate which a syntax editor or wysiwyg should be used. * @param string Optional name of the language used. If non empty it indicates that a syntax highlihter will be used. * @param string Optional additional text to include in the textarea tag * @return string */ function create_textarea($enablewysiwyg, $text, $name, $classname = '', $id = '', $encoding = '', $stylesheet = '', $width = '80', $height = '15', $forcewysiwyg = '', $wantedsyntax = '', $addtext = '') { // todo: rewrite me with var args... to accept a numeric array of arguments, or a hash. $gCms = cmsms(); $result = ''; $uid = get_userid(false); if ($enablewysiwyg == true) { $module = cms_utils::get_wysiwyg_module($forcewysiwyg); if( $module ) { $result = $module->WYSIWYGTextArea($name,$width,$height,$encoding,$text,$stylesheet,$addtext); } } if( !$result && $wantedsyntax ) { // here we should get a list of installed/available modules. $module = cmsms()->GetModuleOperations()->GetSyntaxHighlighter($forcewysiwyg); if( $module ) { $result = $module->SyntaxTextArea($name,$wantedsyntax,$width,$height,$encoding,$text,$addtext); } } if ($result == '') { $result = '