users
[ class tree: users ] [ index: users ] [ all elements ]

Source for file preferences.php

Documentation is available at preferences.php

  1. <?php
  2. /**
  3.  * user preferences
  4.  *
  5.  * @copyright (c) 2004-15 bitweaver.org
  6.  *
  7.  * @package users
  8.  * @subpackage functions
  9.  */
  10.  
  11. /**
  12.  * required setup
  13.  */
  14. require_once( '../kernel/setup_inc.php' );
  15.  
  16. // User preferences screen
  17. $gBitSystem->verifyFeature( 'users_preferences' );
  18.  
  19. $gBitUser->verifyRegistered();
  20.  
  21. $feedback = array();
  22.  
  23. // set up the user we're editing
  24. if( !empty( $_REQUEST["view_user"] ) && $_REQUEST["view_user"] <> $gBitUser->mUserId ) {
  25.     $gBitSystem->verifyPermission( 'p_users_admin' );
  26.     $userClass = $gBitSystem->getConfig( 'user_class', (defined('ROLE_MODEL') ) ?  'RolePermUser' : 'BitPermUser' );
  27.     $editUser = new $userClass( $_REQUEST["view_user"] );
  28.     $editUser->load( TRUE );
  29.     $gBitSmarty->assign('view_user', $_REQUEST["view_user"]);
  30.     $watches = $editUser->getWatches();
  31.     $gBitSmarty->assign('watches', $watches );
  32. } else {
  33.     $gBitUser->load( TRUE );
  34.     $editUser = &$gBitUser;
  35. }
  36.  
  37. global $gQueryUserId;
  38. $gQueryUserId = &$editUser->mUserId;
  39.  
  40. $parsedUrl = parse_url( $_SERVER["REQUEST_URI"] );
  41.  
  42. // settings only applicable when the wiki package is active
  43. if( $gBitSystem->isPackageActive( 'wiki' )) {
  44.     include_once( WIKI_PKG_PATH.'BitPage.php' );
  45.     $parsedUrl1 = str_replace( USERS_PKG_URL."user_preferences", WIKI_PKG_URL."edit", $parsedUrl["path"] );
  46.     $parsedUrl2 = str_replace( USERS_PKG_URL."user_preferences", WIKI_PKG_URL."index", $parsedUrl["path"] );
  47.     $gBitSmarty->assign( 'url_edit', httpPrefix(). $parsedUrl1 );
  48.     $gBitSmarty->assign( 'url_visit', httpPrefix(). $parsedUrl2 );
  49. }
  50.  
  51. // custom user fields
  52. if( $gBitSystem->isFeatureActive( 'custom_user_fields' )) {
  53.     $customFields= explode( ',', $gBitSystem->getConfig( 'custom_user_fields' )  );
  54.     $gBitSmarty->assign('customFields', $customFields );
  55. }
  56.  
  57. // include preferences settings from other packages - these will be included as individual tabs
  58. $includeFiles = $gBitSystem->getIncludeFiles( 'user_preferences_inc.php', 'user_preferences_inc.tpl' );
  59. foreach( $includeFiles as $file ) {
  60.     require_once( $file['php'] );
  61. }
  62. $gBitSmarty->assign( 'includFiles', $includeFiles );
  63.  
  64. // fetch available languages
  65. $gBitLanguage->mLanguage = $editUser->getPreference( 'bitlanguage', $gBitLanguage->mLanguage );
  66. $gBitSmarty->assign( 'gBitLanguage', $gBitLanguage );
  67.  
  68. // allow users to set their preferred site style - this option is only available when users can set the site-wide theme
  69. if( $gBitSystem->getConfig( 'users_themes' ) == 'y' ) {
  70.     if( !empty( $_REQUEST['prefs'] )) {
  71.         if( !empty( $_REQUEST['style'] ) && $_REQUEST['style'] != $gBitSystem->getConfig( 'style' ) ) {
  72.             $editUser->storePreference( 'theme', $_REQUEST["style"] );
  73.         } else {
  74.             $editUser->storePreference( 'theme', NULL );
  75.         }
  76.         $assignStyle = $_REQUEST["style"];
  77.     }
  78.     $styles = $gBitThemes->getStyles( NULL, TRUE, TRUE );
  79.     $gBitSmarty->assign_by_ref( 'styles', $styles );
  80.  
  81.     if( !isset( $_REQUEST["style"] )) {
  82.         $assignStyle = $editUser->getPreference( 'theme' );
  83.     }
  84.     $gBitSmarty->assign( 'assignStyle', $assignStyle );
  85. }
  86.  
  87. // process the preferences form
  88. if( isset( $_REQUEST["prefs"] )) {
  89.     if ( !$editUser->store( $_REQUEST ) ) {
  90.         $feedback['error'] = $editUser->mErrors;
  91.     } else {
  92.         // preferences
  93.         $prefs = array(
  94.             'users_homepage'        => NULL,
  95.             'site_display_utc'    => 'Local',
  96.             'site_display_timezone' => 'UTC',
  97.             'users_country'         => NULL,
  98.             'users_information'     => 'public',
  99.             'users_email_display'   => 'n',
  100.         );
  101.  
  102.         if( $_REQUEST['site_display_utc'] != 'Fixed' ) {
  103.             unset( $_REQUEST['site_display_timezone'] );
  104.             $editUser->storePreference( 'site_display_timezone', NULL, USERS_PKG_NAME );
  105.         } else {
  106.             $editUser->storePreference( 'site_display_timezone', $_REQUEST['site_display_timezone'], USERS_PKG_NAME );
  107.         }
  108.  
  109.         // we don't have to store http:// in the db
  110.         if( empty( $_REQUEST['users_homepage'] ) || $_REQUEST['users_homepage'] == 'http://' ) {
  111.             unset( $_REQUEST['users_homepage'] );
  112.         } elseif( !preg_match( '/^http:\/\//', $_REQUEST['users_homepage'] )) {
  113.             $_REQUEST['users_homepage'] = 'http://'.$_REQUEST['users_homepage'];
  114.         }
  115.  
  116.         foreach( $prefs as $pref => $default ) {
  117.             if( !empty( $_REQUEST[$pref] ) && $_REQUEST[$pref] != $default ) {
  118.                 //vd( "storePreference( $pref, $_REQUEST[$pref], USERS_PKG_NAME )" );
  119.                 $editUser->storePreference( $pref, $_REQUEST[$pref], USERS_PKG_NAME );
  120.             } else {
  121.                 $editUser->storePreference( $pref, NULL, USERS_PKG_NAME );
  122.             }
  123.         }
  124.  
  125.         if( $gBitSystem->isFeatureActive( 'users_change_language' )) {
  126.             $editUser->storePreference( 'bitlanguage', ( $_REQUEST['bitlanguage'] != $gBitLanguage->mLanguage ) ? $_REQUEST['bitlanguage'] : NULL, LANGUAGES_PKG_NAME );
  127.         }
  128.  
  129.         // toggles
  130.         $toggles = array(
  131.             'users_double_click'  => USERS_PKG_NAME,
  132.         );
  133.  
  134.         foreach( $toggles as $toggle => $package ) {
  135.             if( isset( $_REQUEST[$toggle] )) {
  136.                 $editUser->storePreference( $toggle, 'y', $package );
  137.             } else {
  138.                 $editUser->storePreference( $toggle, NULL, $package );
  139.             }
  140.         }
  141.  
  142.         // process custom fields
  143.         if( isset( $customFields ) && is_array( $customFields )) {
  144.             foreach( $customFields as $f ) {
  145.                 if( isset( $_REQUEST['CUSTOM'][$f] )) {
  146.                     $editUser->storePreference( trim( $f ), trim( $_REQUEST['CUSTOM'][$f] ), USERS_PKG_NAME );
  147.                 }
  148.             }
  149.         }
  150.  
  151.         // we need to reload the page for all the included user preferences
  152.         if( isset( $_REQUEST['view_user'] )) {
  153.             bit_redirect ( USERS_PKG_URL."preferences.php?view_user=$editUser->mUserId" );
  154.         } else {
  155.             bit_redirect ( USERS_PKG_URL."preferences.php" );
  156.         }
  157.     }
  158. }
  159.  
  160. // change email address
  161. if( isset( $_REQUEST['chgemail'] )) {
  162.     // check user's password
  163.     if( !$gBitUser->hasPermission( 'p_users_admin' ) && !$editUser->validate( $editUser->mUsername, $_REQUEST['pass'], '', '' )) {
  164.         $gBitSystem->fatalError( tra("Invalid password.  Your current password is required to change your email address." ));
  165.     }
  166.  
  167.     $org_email = $editUser->getField( 'email' );
  168.     if( $editUser->changeUserEmail( $editUser->mUserId, $_REQUEST['email'] )) {
  169.         $feedback['success'] = tra( 'Your email address was updated successfully' );
  170.  
  171.         /* this file really needs a once over
  172.             we need to call services when various preferences are stored
  173.             for now my need is when the email address is changed. when the 
  174.             need expands to more we can look at cleaning up this file
  175.             into something more sane. happy to help out at that time -wjames5
  176.          */
  177.         $paramHash = $_REQUEST;
  178.         $paramHash['org_email'] = $org_email;
  179.         $editUser->invokeServices( 'content_store_function', $_REQUEST );
  180.     } else {
  181.         $feedback['error'] = $editUser->mErrors;
  182.     }
  183. }
  184.  
  185. // change user password
  186. if( isset( $_REQUEST["chgpswd"] )) {
  187.     if( $_REQUEST["pass1"] != $_REQUEST["pass2"] ) {
  188.         $gBitSystem->fatalError( tra("The passwords didn't match" ));
  189.     }
  190.     if( !$gBitUser->hasPermission( 'p_users_admin' ) && !$editUser->validate( $editUser->getField( 'email' ), $_REQUEST["old"], '', '' )) {
  191.         $gBitSystem->fatalError( tra( "Invalid old password" ));
  192.     }
  193.     //Validate password here
  194.     $users_min_pass_length = $gBitSystem->getConfig( 'users_min_pass_length', 4 );
  195.     if( strlen( $_REQUEST["pass1"] ) < $users_min_pass_length ) {
  196.         $gBitSystem->fatalError( tra( "Password should be at least" ).' '.$users_min_pass_length.' '.tra( "characters long" ));
  197.     }
  198.     // Check this code
  199.     if( $gBitSystem->isFeatureActive( 'users_pass_chr_num' )) {
  200.         if (!preg_match_all("/[0-9]+/", $_REQUEST["pass1"], $parsedUrl ) || !preg_match_all("/[A-Za-z]+/", $_REQUEST["pass1"], $parsedUrl )) {
  201.             $gBitSystem->fatalError( tra( "Password must contain both letters and numbers" ));
  202.         }
  203.     }
  204.     if( $editUser->storePassword( $_REQUEST["pass1"] )) {
  205.         $feedback['success'] = tra( 'The password was updated successfully' );
  206.     }
  207. }
  208.  
  209.  
  210. // this should go in tidbits
  211. if( isset( $_REQUEST['tasksprefs'] )) {
  212.     $editUser->storePreference( 'tasks_max_records', $_REQUEST['tasks_max_records'], 'users' );
  213.     if( isset( $_REQUEST['tasks_use_dates'] ) && $_REQUEST['tasks_use_dates'] == 'on' ) {
  214.         $editUser->storePreference( 'tasks_use_dates', 'y', 'users' );
  215.     } else {
  216.         $editUser->storePreference( 'tasks_use_dates', 'n', 'users' );
  217.     }
  218. }
  219.  
  220. // get available languages
  221. $languages = array();
  222. $languages = $gBitLanguage->listLanguages();
  223. $gBitSmarty->assign_by_ref( 'languages', $languages );
  224.  
  225. // Get flags
  226. $flags = array();
  227. $h = opendir( USERS_PKG_PATH.'icons/flags/' );
  228. while( $file = readdir( $h )) {
  229.     if( strstr( $file, ".gif" )) {
  230.         $flags[] = preg_replace( "/\.gif/", "", $file );
  231.     }
  232. }
  233. closedir( $h );
  234. sort( $flags );
  235. $gBitSmarty->assign( 'flags', $flags );
  236.  
  237. $editUser->mInfo['users_homepage'] = $editUser->getPreference( 'users_homepage', '' );
  238.  
  239. $gBitSmarty->assign( 'editUser', $editUser );
  240. $gBitSmarty->assign( 'gContent', $editUser ); // also assign to gContent to make services happy
  241. $gBitSmarty->assign( 'feedback', $feedback );
  242.  
  243. /* This should come from BitDate->get_timezone_list but that seems to rely on a global from PEAR that does not exist. */
  244. if ( version_compare( phpversion(), "5.2.0", ">=" ) ) {
  245.     $user_timezones = DateTimeZone::listIdentifiers();
  246. } else {
  247.     for($i=-12;$i<=12;$i++) {
  248.         $user_timezones[$i * 60 * 60] = $i; // Stored offset needs to be in seconds.
  249.     }
  250. }
  251. $gBitSmarty->assign( 'userTimezones', $user_timezones);
  252.  
  253. // email scrambling methods
  254. $scramblingMethods = array( "n", "strtr", "unicode", "x" );
  255. $gBitSmarty->assign_by_ref( 'scramblingMethods', $scramblingMethods );
  256. $scramblingEmails = array(
  257.     tra( "no" ),
  258.     scramble_email( $editUser->mInfo['email'], 'strtr' ),
  259.     scramble_email( $editUser->mInfo['email'], 'unicode' )."-".tra( "unicode" ),
  260.     scramble_email( $editUser->mInfo['email'], 'x' )
  261. );
  262. $gBitSmarty->assign_by_ref( 'scramblingEmails', $scramblingEmails );
  263.  
  264. // edit services
  265. $editUser->invokeServices( 'content_edit_function' );
  266. $gBitSystem->display( 'bitpackage:users/user_preferences.tpl', 'Edit User Preferences' , array( 'display_mode' => 'display' ));
  267. ?>

Documentation generated on Wed, 29 Jul 2015 13:57:16 +0000 by phpDocumentor 1.5.0-lsces