Advertisement
Guest User

Untitled

a guest
Jun 10th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 18.30 KB | None | 0 0
  1. <?php
  2.  
  3.   // Use company profile module
  4.   use_controller('company_profile', SYSTEM_MODULE);
  5.  
  6.   /**
  7.    * User profile controller
  8.    *
  9.    * @package activeCollab.modules.system
  10.    * @subpackage controllers
  11.    */
  12.   class UserProfileController extends CompanyProfileController {
  13.    
  14.     /**
  15.      * Name of this controller
  16.      *
  17.      * @var string
  18.      */
  19.     var $controller_name = 'user_profile';
  20.    
  21.     /**
  22.      * Name of the parent module
  23.      *
  24.      * @var mixed
  25.      */
  26.     var $active_module = SYSTEM_MODULE;
  27.      
  28.     /**
  29.      * Selected use
  30.      *
  31.      * @var User
  32.      */
  33.     var $active_user;
  34.    
  35.     /**
  36.      * Array of controller actions that can be accessed through API
  37.      *
  38.      * @var array
  39.      */
  40.     var $api_actions = array('index', 'add', 'edit', 'delete');
  41.        
  42.     /**
  43.      * Construct Profile Controller
  44.      *
  45.      * @param void
  46.      * @return null
  47.      */
  48.     function __construct($request){
  49.         parent::__construct($request);
  50.        
  51.         $user_id = $this->request->get('user_id');
  52.         if($user_id) {
  53.             $this->active_user = Users::findById($user_id);
  54.         } // if
  55.        
  56.         if(instance_of($this->active_user,'User')) {
  57.           if(!in_array($this->active_user->getId(), $this->logged_user->visibleUserIds())) {
  58.           $this->httpError(HTTP_ERR_NOT_FOUND);
  59.         } // if
  60.         $this->wireframe->addBreadCrumb($this->active_user->getName(), $this->active_user->getViewUrl());
  61.         } else {
  62.             $this->active_user = new User();
  63.         } // if
  64.        
  65.         $this->smarty->assign('active_user', $this->active_user);
  66.     } // __construct
  67.    
  68.     /**
  69.      * Show user profile page
  70.      *
  71.      * @param void
  72.      * @return null
  73.      */
  74.     function index() {
  75.       if($this->active_user->isNew()) {
  76.         $this->httpError(HTTP_ERR_NOT_FOUND, null, true, $this->request->isApiCall());
  77.       } // if
  78.      
  79.       if($this->request->isApiCall()) {
  80.         $this->serveData($this->active_user, 'user', array(
  81.           'describe_company' => true,
  82.           'describe_avatar' => true,
  83.         ));
  84.       } else {
  85.         $this->redirectToUrl($this->active_company->getViewUrl() . '#user' . $this->active_user->getId());
  86.       } // if
  87.     } // index
  88.    
  89.     /**
  90.      * Create new user
  91.      *
  92.      * @param void
  93.      * @return null
  94.      */
  95.     function add() {
  96.       $this->wireframe->print_button = false;
  97.      
  98.       if($this->request->isApiCall() && !$this->request->isSubmitted()) {
  99.         $this->httpError(HTTP_ERR_BAD_REQUEST);
  100.       } // if
  101.      
  102.       if(!User::canAdd($this->logged_user, $this->active_company)) {
  103.         $this->httpError(HTTP_ERR_FORBIDDEN);
  104.       } // if
  105.      
  106.       $user_data = $this->request->post('user');
  107.       if(!is_array($user_data)) {
  108.         $user_data = array(
  109.           'role_id' => ConfigOptions::getValue('default_role'),
  110.           'auto_assign' => false,
  111.         );
  112.       } // if
  113.      
  114.       $this->smarty->assign(array(
  115.         'user_data' => $user_data,
  116.       ));
  117.      
  118.       if($this->request->isSubmitted()) {
  119.         db_begin_work();
  120.        
  121.         // Validate password
  122.         if(array_var($user_data, 'password_generator') == 'generate') {
  123.           $password = make_password(11);
  124.         } else {
  125.           $errors = new ValidationErrors();
  126.          
  127.           $password = array_var($user_data, 'password');
  128.           $password_a = array_var($user_data, 'password_a');
  129.          
  130.           if(strlen(trim($password)) < 3) {
  131.             $errors->addError(lang('Minimal password length is 3 characters'), 'password');
  132.           } else {
  133.             if($password != $password_a) {
  134.               $errors->addError(lang('Passwords do not match'), 'passwords');
  135.             } // if
  136.           } // if
  137.          
  138.           if($errors->hasErrors()) {
  139.             if($this->request->getFormat() == FORMAT_HTML) {
  140.               $this->smarty->assign('errors', $errors);
  141.               $this->render();
  142.             } else {
  143.               $this->serveData($errors);
  144.             } // if
  145.           } // if
  146.         } // if
  147.        
  148.         $this->active_user = new User();
  149.         $this->active_user->setAttributes($user_data);
  150.         $this->active_user->setPassword($password);
  151.         $this->active_user->setCompanyId($this->active_company->getId());
  152.        
  153.         if($this->logged_user->isPeopleManager()) {
  154.           $this->active_user->setAutoAssignData(
  155.             (boolean) array_var($user_data, 'auto_assign'),
  156.             (integer) array_var($user_data, 'auto_assign_role_id'),
  157.             array_var($user_data, 'auto_assign_permissions')
  158.           );
  159.         } else {
  160.           $this->active_user->setRoleId(ConfigOptions::getValue('default_role'));
  161.         } // if
  162.        
  163.         $save = $this->active_user->save();
  164.         if($save && !is_error($save)) {
  165.           if(array_key_exists('send_welcome_message', $user_data) && $user_data['send_welcome_message']) {
  166.             ApplicationMailer::send(array($this->active_user), 'system/new_user', array(
  167.               'created_by_id'   => $this->logged_user->getId(),
  168.               'created_by_name' => $this->logged_user->getDisplayName(),
  169.               'created_by_url'  => $this->logged_user->getViewUrl(),
  170.               'email'           => $this->active_user->getEmail(),
  171.               'password'        => $password,
  172.               'login_url'       => assemble_url('login'),
  173.               'welcome_body'    => nl2br(clean(array_var($user_data, 'welcome_message'))),
  174.             ));
  175.           } // if
  176.          
  177.             db_commit();
  178.            
  179.             if($this->request->isApiCall()) {
  180.               $this->serveData($this->active_user);
  181.             } else {
  182.               flash_success('New user account has been created');
  183.                 $this->redirectToUrl($this->active_user->getViewUrl());
  184.             } // if
  185.         } else {
  186.             db_rollback();
  187.            
  188.             if($this->request->isApiCall()) {
  189.               $this->serveData($save);
  190.             } else {
  191.               $this->smarty->assign('errors', $save);
  192.             } // if
  193.         } // if
  194.       } // if
  195.     } // add
  196.    
  197.     /**
  198.      * Edit Profile
  199.      *
  200.      * @param void
  201.      * @return null
  202.      */
  203.     function edit() {
  204.       $this->wireframe->print_button = false;
  205.      
  206.       if($this->active_user->isNew()) {
  207.         $this->httpError(HTTP_ERR_NOT_FOUND);
  208.       } // if
  209.      
  210.       if(!$this->active_user->canEdit($this->logged_user)) {
  211.         $this->httpError(HTTP_ERR_FORBIDDEN);
  212.       } // if
  213.      
  214.       $config_options = array('title', 'phone_work', 'phone_mobile', 'format_date', 'format_time', 'time_timezone', 'time_dst', 'time_first_week_day', 'visual_editor', 'im_type', 'im_value', 'theme');
  215.       if(LOCALIZATION_ENABLED) {
  216.         $config_options[] = 'language';
  217.       } // if
  218.      
  219.       $user_data = $this->request->post('user');
  220.       if(!is_array($user_data)) {
  221.         $user_data = array_merge(array(
  222.           'company_id'              => $this->active_user->getCompanyId(),
  223.           'first_name'              => $this->active_user->getFirstName(),
  224.           'last_name'               => $this->active_user->getLastName(),
  225.           'email'                   => $this->active_user->getEmail(),
  226.           'role_id'                 => $this->active_user->getRoleId(),
  227.           'auto_assign'             => $this->active_user->getAutoAssign(),
  228.           'auto_assign_role_id'     => $this->active_user->getAutoAssignRoleId(),
  229.           'auto_assign_permissions' => $this->active_user->getAutoAssignPermissions(),
  230.         ), UserConfigOptions::getValues($config_options, $this->active_user));
  231.        
  232.         if(LOCALIZATION_ENABLED) {
  233.             if(!UserConfigOptions::hasValue('language', $this->active_user)) {
  234.               $user_data['language'] = null;
  235.             } // if
  236.         } // if
  237.        
  238.         if(!UserConfigOptions::hasValue('format_date', $this->active_user)) {
  239.           $user_data['format_date'] = null;
  240.         } // if
  241.        
  242.         if(!UserConfigOptions::hasValue('format_time', $this->active_user)) {
  243.           $user_data['format_time'] = null;
  244.         } // if
  245.       } // if
  246.      
  247.       $this->smarty->assign(array(
  248.         'user_data' => $user_data,
  249.         'only_administrator' => $this->active_user->isOnlyAdministrator(),
  250.       ));
  251.  
  252.       if($this->request->isSubmitted()) {
  253.         db_begin_work();
  254.        
  255.         $display = $this->active_user->getDisplayName();
  256.         $old_role_id = $this->active_user->getRoleId();
  257.        
  258.         if($this->active_user->isOnlyAdministrator()) {
  259.           $user_data['role_id'] = $old_role_id; // cannot change role
  260.         } // if
  261.        
  262.         $this->active_user->setAttributes($user_data);
  263.        
  264.         if($this->active_user->canChangeRole($this->logged_user)) {
  265.           $this->active_user->setAutoAssignData(
  266.             (boolean) array_var($user_data, 'auto_assign'),
  267.             (integer) array_var($user_data, 'auto_assign_role_id'),
  268.             array_var($user_data, 'auto_assign_permissions')
  269.           );
  270.         } else {
  271.           $this->active_user->setRoleId($old_role_id);
  272.         } // if
  273.        
  274.         $save = $this->active_user->save();
  275.         if($save && !is_error($save)) {
  276.           foreach($config_options as $config_option) {
  277.             if($config_option == 'time_dst' || $config_option == 'visual_editor') {
  278.               $value = (boolean) array_var($user_data, $config_option);
  279.             } elseif($config_option == 'time_timezone' || $config_option == 'time_first_week_day ') {
  280.               $value = (integer) array_var($user_data, $config_option);
  281.             } else {
  282.               $value = trim(array_var($user_data, $config_option));
  283.             } // if
  284.            
  285.             if($value === '') {
  286.               UserConfigOptions::removeValue($config_option, $this->active_user);
  287.             } else {
  288.               UserConfigOptions::setValue($config_option, $value, $this->active_user);
  289.             } // if
  290.           } // foreach
  291.             db_commit();
  292.            
  293.             if($this->request->isApiCall()) {
  294.               $this->serveData($this->active_user, 'user');
  295.             } else {
  296.               flash_success(":display's profile has been updated", array('display' => $display));
  297.                 $this->redirectToUrl($this->active_user->getViewUrl());
  298.             } // if
  299.         } else {
  300.             db_rollback();
  301.            
  302.             if($this->request->isApiCall()) {
  303.               $this->serveData($save);
  304.             } else {
  305.               $this->smarty->assign('errors', $save);
  306.             } // if
  307.         } // if
  308.       } else {
  309.         if($this->request->isApiCall()) {
  310.           $this->httpError(HTTP_ERR_BAD_REQUEST, null, true, true);
  311.         } // if
  312.       } // if
  313.     } // edit
  314.    
  315.     /**
  316.      * Edit Profile Password
  317.      *
  318.      * @param void
  319.      * @return null
  320.      */
  321.     function edit_password() {
  322.       $this->wireframe->print_button = false;
  323.      
  324.       if($this->active_user->isNew()) {
  325.         $this->httpError(HTTP_ERR_NOT_FOUND);
  326.       } // if
  327.      
  328.       if(!$this->active_user->canEdit($this->logged_user)) {
  329.         $this->httpError(HTTP_ERR_FORBIDDEN);
  330.       } // if
  331.      
  332.         $user_data = $this->request->post('user');
  333.       $this->smarty->assign('user_data', $user_data);
  334.      
  335.         if($this->request->isSubmitted()) {
  336.         $errors = new ValidationErrors();
  337.        
  338.         $password = array_var($user_data, 'password');
  339.         $repeat_password = array_var($user_data, 'repeat_password');
  340.        
  341.         if(empty($password)) {
  342.             $errors->addError(lang('Password value is required'), 'password');
  343.         } // if
  344.        
  345.         if(strlen($password) < 8) {
  346.             $errors->addError(lang('Passwords must be at least 8 characters'), 'password');
  347.         } elseif(!preg_match("/(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/", $password)) {
  348.             $errors->addError(lang('Please use at least one upper case and lower case character, along with a digit or special character'), 'password');
  349.         }
  350.        
  351.         if(empty($repeat_password)) {
  352.             $errors->addError(lang('Repeat Password value is required'), 'repeat_password');
  353.         } // if
  354.        
  355.         if(!$errors->hasErrors() && ($password !== $repeat_password)) {
  356.           $errors->addError(lang('Inserted values does not match'));
  357.         } // if
  358.        
  359.         if($errors->hasErrors()) {
  360.           $this->smarty->assign('errors', $errors);
  361.           $this->render();
  362.         } // if
  363.        
  364.           db_begin_work();
  365.          
  366.             $this->active_user->setPassword($user_data['password']);
  367.             $save = $this->active_user->save();
  368.            
  369.             if($save && !is_error($save)) {
  370.                 db_commit();
  371.                 if($this->active_user->getNewAcc() == '0') {
  372.                     $this->active_user->setNewAcc('1');
  373.                     $this->active_user->save();
  374.                 }
  375.                 if($this->request->getFormat() == FORMAT_HTML) {
  376.                   flash_success('Password has been updated');
  377.                   $this->redirectToUrl($this->active_user->getViewUrl());  
  378.                 } else {
  379.                     $this->serveData($this->active_user, 'user');
  380.                 } // if
  381.             } else {
  382.               db_rollback();
  383.              
  384.             if($this->request->getFormat() == FORMAT_HTML) {
  385.                 $this->smarty->assign('errors', $errors);
  386.             } else {
  387.                 $this->serveData($errors);
  388.             } // if
  389.             } // if
  390.         } // if
  391.     } // edit_password
  392.    
  393.     /**
  394.      * Edit Profile Avatar
  395.      *
  396.      * @param void
  397.      * @return null
  398.      */
  399.     function edit_avatar() {
  400.       $this->wireframe->print_button = false;
  401.      
  402.       if($this->active_user->isNew()) {
  403.         $this->httpError(HTTP_ERR_NOT_FOUND);
  404.       } // if
  405.      
  406.       if(!$this->active_user->canEdit($this->logged_user)) {
  407.         $this->httpError(HTTP_ERR_FORBIDDEN);
  408.       } // if
  409.      
  410.       if(!extension_loaded('gd')) {
  411.         $this->wireframe->addPageMessage(lang('<b>GD not Installed</b> - GD extension is not installed on your system. You will not be able to upload project icons, company logos and avatars!'), PAGE_MESSAGE_ERROR);
  412.       } // if
  413.      
  414.         if($this->request->isSubmitted()) {
  415.           if(!isset($_FILES['avatar']) || !is_file($_FILES['avatar']['tmp_name'])) {
  416.             flash_error("Please select an image");
  417.             $this->redirectToUrl($this->active_user->getEditAvatarUrl());
  418.           } // if
  419.          
  420.             if(can_resize_images()) {
  421.               $errors = new ValidationErrors();
  422.               do {
  423.                 $from = WORK_PATH.'/'.make_password(10).'_'.$_FILES['avatar']['name'];
  424.               } while (is_file($from));
  425.              
  426.               if(move_uploaded_file($_FILES['avatar']['tmp_name'], $from)) {
  427.             $to = $this->active_user->getAvatarPath();
  428.               $small = scale_image($from, $to, 16, 16, IMAGETYPE_JPEG,100);
  429.              
  430.               $to = $this->active_user->getAvatarPath(true);
  431.               $large = scale_image($from, $to, 40, 40, IMAGETYPE_JPEG,100);
  432.              
  433.             @unlink($from);
  434.               } else {
  435.             $errors->addError('Can\'t copy image to work path', 'icon');
  436.               } // if
  437.              
  438.               if(empty($from)) {
  439.                 $errors->addError('Select avatar', 'avatar');
  440.               } // if
  441.              
  442.               if($errors->hasErrors()) {
  443.             $this->smarty->assign('errors', $errors);
  444.             $this->render();
  445.           } // if
  446.             } // if
  447.         } // if
  448.     } // edit_avatar
  449.    
  450.     /**
  451.      * Delete user
  452.      *
  453.      * @param void
  454.      * @return null
  455.      */
  456.     function delete() {
  457.       if($this->active_user->isNew()) {
  458.         $this->httpError(HTTP_ERR_NOT_FOUND, null, true, $this->request->isApiCall());
  459.       } // if
  460.      
  461.       if(!$this->active_user->canDelete($this->logged_user)) {
  462.         $this->httpError(HTTP_ERR_FORBIDDEN, null, true, $this->request->isApiCall());
  463.       } // if
  464.      
  465.       if($this->request->isSubmitted()) {
  466.         $delete = $this->active_user->delete();
  467.         if($delete && !is_error($delete)) {
  468.           if($this->request->isApiCall()) {
  469.             $this->httpOk();
  470.           } else {
  471.             flash_success('User ":name" has been deleted', array('name' => $this->active_user->getDisplayName()));
  472.             $this->redirectToUrl($this->active_company->getViewUrl());
  473.           } // if
  474.         } else {
  475.           if($this->request->isApiCall()) {
  476.             $this->serveData($delete);
  477.           } else {
  478.             flash_error('Failed to delete ":name"', array('name' => $this->active_user->getDisplayName()));
  479.             $this->redirectToUrl($this->active_company->getViewUrl());
  480.           } // if
  481.         } // if
  482.         } else {
  483.           $this->httpError(HTTP_ERR_BAD_REQUEST, null, true, $this->request->isApiCall());
  484.         } // if
  485.     } // delete
  486.    
  487.     /**
  488.      * Delete Profile Avatar
  489.      *
  490.      * @param void
  491.      * @return null
  492.      */
  493.     function delete_avatar() {
  494.       if($this->active_user->isNew()) {
  495.         $this->httpError(HTTP_ERR_NOT_FOUND);
  496.       } // if
  497.      
  498.       if(!$this->active_user->canEdit($this->logged_user)) {
  499.         $this->httpError(HTTP_ERR_FORBIDDEN);
  500.       } // if
  501.      
  502.         if($this->request->isSubmitted()) {
  503.           unlink($this->active_user->getAvatarPath());
  504.           unlink($this->active_user->getAvatarPath(true));
  505.          
  506.           $this->redirectToUrl($this->active_user->getEditAvatarUrl());
  507.         } else {
  508.           $this->httpError(HTTP_ERR_BAD_REQUEST);
  509.         } // if
  510.     } // delete_avatar
  511.    
  512.     /**
  513.      * Show API settings URL
  514.      *
  515.      * @param void
  516.      * @return null
  517.      */
  518.     function api() {
  519.       if(!$this->active_user->canEdit($this->logged_user)) {
  520.         $this->httpError(HTTP_ERR_FORBIDDEN);
  521.       } // if
  522.      
  523.       $this->wireframe->print_button = false;
  524.      
  525.       $this->smarty->assign('api_url', ROOT_URL);
  526.     } // api
  527.    
  528.     /**
  529.      * Reset API key
  530.      *
  531.      * @param void
  532.      * @return null
  533.      */
  534.     function api_reset_key() {
  535.       if($this->active_user->isNew()) {
  536.         $this->httpError(HTTP_ERR_NOT_FOUND);
  537.       } // if
  538.      
  539.       if(!$this->active_user->canEdit($this->logged_user)) {
  540.         $this->httpError(HTTP_ERR_FORBIDDEN);
  541.       } // if
  542.      
  543.       if($this->request->isSubmitted()) {
  544.         $this->active_user->setToken(make_string(40));
  545.         $save = $this->active_user->save();
  546.         if($save && !is_error($save)) {
  547.           flash_success('API key updated');
  548.         } else {
  549.           flash_error('Failed to update API key. Try again in a few minutes');
  550.         } // if
  551.        
  552.         $this->redirectToUrl($this->active_user->getApiSettingsUrl());
  553.       } else {
  554.         $this->httpError(HTTP_ERR_BAD_REQUEST);
  555.       } // if
  556.     } // api_reset_key
  557.    
  558.   }
  559.  
  560. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement