enqueueMessage($message); return; } //rediect to specified url and enqueue message function redirectUrl($url,$msg=''){ global $mainframe; $mainframe->redirect($url,$msg); return; } //load initial parameters function loadParameter() { // if params already loaded then simply return if($this->parameterLoaded) return; //load langauge JPlugin::loadLanguage( 'plg_xi_adminapproval', JPATH_ADMINISTRATOR ); $this->db =& JFactory::getDBO(); $plugin =& JPluginHelper::getPlugin('system', 'xi_adminapproval'); $this->params = new JParameter($plugin->params); $this->debugMode = $this->params->get('debugMode', 0); $this->mode = $this->params->get('Mode', 1); $this->activation = JRequest::getVar('activation', '', '', 'alnum' ); $this->activation = $this->db->getEscaped( $this->activation ); $usersConfig = &JComponentHelper::getParams( 'com_users' ); $this->userActivation = $usersConfig->get('useractivation'); $this->allowUserRegistration = $usersConfig->get('allowUserRegistration'); $this->activationUserID = $this->getUserIDFromActivationKey($this->activation); // set params already loaded $this->parameterLoaded = true; } // when admin is logged in backend and plugin is enabled // if joomla configuration not proper then show warning message function showAdminDebugMessage() { $this->loadParameter(); //enqueue message that registration is not working if( ! $this->allowUserRegistration){ $this->displayMessage(JText::_('MSG REGISTRATION NOT WORKING')); // and return false return false; } // enqueu Message : admin approval plugin cannot work without activation if( ! $this->userActivation){ $this->displayMessage(JText::_('MSG PLUGIN NOT WORK WITHOUT ACTIVATION')); // return false return false; } return true; } function blockActivationResend($email) { jimport('joomla.mail.helper'); jimport('joomla.user.helper'); // Make sure the e-mail address is valid if (!JMailHelper::isEmailAddress($email)) return; //find the user by email ID $db = &JFactory::getDBO(); $query = ' SELECT `id` FROM `#__users` ' . ' WHERE `email` = '.$db->Quote($email); $db->setQuery($query); $id = $db->loadResult(); // Check the results if($id === false) return; //now check if it was set to activate by admin $emailVerified = JUser::getInstance((int)$id)->getParam('emailVerified'); //if email already verified, then no need to do discard if($emailVerified == false) return; // email was already Verified, so discard user request, // and tell user to wait for admin approval $this->displayMessage(JText::_('MSG WAIT FOR ADMIN APPROVE YOUR ACCOUNT')); $this->redirectUrl('index.php'); } function onAfterRoute() { //get option & task from URL $option=JRequest::getCmd('option'); $task=JRequest::getCmd('task'); // 1. if backend then call showAdminDebugMessage and return global $mainframe; if($mainframe->isAdmin()){ $this->showAdminDebugMessage(); } // 2. I am in frontend and user came to activate // index.php?option=com_user&task=activate&activation= if(($option !='com_user' || $task !='activate') && ($option !='com_community' || $task !='activationResend') ) return; //Security Fix if($option =='com_community' && $task =='activationResend') { $email = JRequest::getVar( 'jsemail', '', 'REQUEST'); $this->blockActivationResend($email); } // 3. load all params and related information e.g. language/js/css etc. $this->loadParameter(); // 4. verify that system configuration is correct, // show admin debug message again // false -> redirect to index.php if($this->sanitizeAndVerifyInputs()==false) { //TODO: redirect to index.php return; } //5. user / Admin is here to verification if($this->isAdminDoingApproval()){ // 6. is a admin, we should enable a blocked user $user =& JUser::getInstance($this->activationUserID); // activate user and enable $user->setParam('emailVerified','0'); $user->set('block', '0'); $user->set('activation',''); if (!$user->save()){ JError::raiseWarning('', JText::_( $user->getError())); $this->redirectUrl('index.php',JText::_('DEBUG USER SAVE ERROR')); exit(); } // inform user $this->sendEmails('ACCOUNT_ACTIVATED_EMAIL_TO_USER'); // show a message to admin $this->displayMessage(JText::_('MSG USER HAS BEEN APPROVED BY ADMIN')); } else{ // 7. user came to verify his email , check, mark and block user, inform admin jimport('joomla.user.helper'); // this plugin should also work without JS Profile Types $MY_PATH_ADMIN_JSPT = JPATH_ADMINISTRATOR.DS.'components'.DS.'com_jsprofiletypes'; $this->jsptExist = JFolder::exists($MY_PATH_ADMIN_JSPT); if($this->jsptExist && $this->mode==3) { require_once (JPATH_BASE. DS.'components'.DS.'com_community'.DS.'libraries'.DS.'profiletypes.php'); // some issue here $pID = CProfiletypeLibrary::getUserProfiletypeFromUserID($this->activationUserID); $profiletypeName = CProfiletypeLibrary::getProfileTypeNameFrom($pID); // TODO : what to do for $pId =0 // if admin approval NOT required, then do nothing let the joomla handle if($pID && CProfiletypeLibrary::getProfileTypeData($pID,'approve') == false) { if($this->debugMode) $this->displayMessage('ProfileType='.$pID.' and approval not required'); return; } } $MY_PATH_ADMIN_XIPT = JPATH_ADMINISTRATOR.DS.'components'.DS.'com_xipt'; $this->xiptExist = JFolder::exists($MY_PATH_ADMIN_XIPT); if($this->xiptExist && $this->mode==3){ require_once (JPATH_BASE. DS.'components'.DS.'com_xipt'.DS.'api.xipt.php'); // some issue here $pID = XiptAPI::getUserProfiletype($this->activationUserID); $profiletypeName = XiptAPI::getUserProfiletype($this->activationUserID,'name'); // TODO : what to do for $pId =0 $allCondition = array(); $allCondition = XiptAPI::getProfiletypeInfo($pID); // if admin approval NOT required, then do nothing let the joomla handle if($allCondition){ if($pID && $allCondition[0]->approve == false){ if($this->debugMode) $this->displayMessage('ProfileType='.$pID.' and approval not required'); return; } } } // --- mark $user =& JUser::getInstance($this->activationUserID); $user->setParam('emailVerified','1'); // --- also block the user $user->set('block', '1'); $newActivationKey=JUtility::getHash( JUserHelper::genRandomPassword()); // generate new activation // save new activation key by which our admin can enable user $user->set('activation',$newActivationKey); $this->activation = $newActivationKey; if(!$user->save()){ JError::raiseWarning('', JText::_( $user->getError())); $this->redirectUrl('index.php',JText::_('USER SAVE ERROR')); exit(); } // send an email to admin with a ativation link // and profile of user. $this->sendEmails('EMAIL_TO_ADMIN_FOR_APPROVAL'); // show message to user $this->displayMessage(JText::_('MSG EMAIL VERIFIED AND ADMIN WILL APPROVE YOUR ACCOUNT')); } // 8. always redirect to index.php $this->redirectUrl('index.php'); return; } // check and verify inputs // if some issue then halt the process function sanitizeAndVerifyInputs() { // do checks and if things found not fine, then add a message and return false // else return true if (empty( $this->activation ) || $this->allowUserRegistration == '0' || $this->userActivation == '0' ) return false; // check userID if ($this->activationUserID < 62 ) { // Is it a valid user to activate? if($this->debugMode) $this->displayMessage(JText::_('MSG NOT A VALID USER TO ACTIVATE')); return false; } // no error return true; } // return user id from activation key function getUserIDFromActivationKey() { $query = 'SELECT id' . ' FROM #__users' . ' WHERE '.$this->db->nameQuote('activation').' = '.$this->db->Quote($this->activation) . ' AND block = '.$this->db->Quote('1') . ' AND lastvisitDate = '.$this->db->Quote('0000-00-00 00:00:00'); $this->db->setQuery( $query ); $id = intval( $this->db->loadResult() ); return $id; } /** * The function will check if the user is admin or not */ function isAdminDoingApproval() { // Lets get the id of the user we want to activate $id = $this->getUserIDFromActivationKey($this->activation); $this->user =& JUser::getInstance((int)$id); $this->isAdmin = $this->user->getParam('emailVerified') ? true : false; if($this->debugMode && !$this->isAdmin) $this->displayMessage(JText::_('MSG NOT AN ACTIVATION BY ADMIN')); return $this->isAdmin; } function get_jomsocial_profile_fields() { require_once (JPATH_BASE. DS.'components'.DS.'com_community'.DS.'libraries'.DS.'core.php'); $pModel = CFactory::getModel('profile'); $profile = $pModel->getViewableProfile($this->activationUserID); return $profile['fields']; } function generate_message_for_profile_fields() { require_once (JPATH_BASE. DS.'components'.DS.'com_community'.DS.'libraries'.DS.'core.php'); $fields = $this->get_jomsocial_profile_fields($this->activationUserID); $pModel = CFactory::getModel('profile'); $xiJsDetails=array(); //$message = " List of profile fileds of user :- \n "; if($this->debugMode) $this->displayMessage('userid = '.$this->activationUserID); foreach($fields as $name => $fieldGroup) { foreach($fieldGroup as $field) { if($this->debugMode) $this->displayMessage('field code = '.$field['fieldcode']); $count=1; //if($pModel->_fieldValueExists($field['fieldcode'],$this->activationUserID)) //{ $fieldId = $pModel->getFieldId($field['fieldcode']); $query = 'SELECT value FROM #__community_fields_values' . ' ' . 'WHERE `field_id`=' .$this->db->Quote( $fieldId ) . ' ' . 'AND `user_id`=' . $this->db->Quote( $this->activationUserID ); $this->db->setQuery( $query ); $result = $this->db->loadresult(); if(!empty($result)){ $xiJsDetails[$field['name']]=$result; } //} } } return $xiJsDetails; } function generalInformation() { // common infrmation $user =& JUser::getInstance((int)$this->activationUserID); $var['name'] = $user->name; $var['email'] = $user->email; $var['username']= $user->username; // activation link $link = JRoute::_(JURI::Root()."index.php?option=com_user&task=activate&activation=" .$this->activation, false); $var['actLink'] = $link; return $var; } function sendEmails($msgID) { global $mainframe; $usersConfig = &JComponentHelper::getParams( 'com_users' ); $sitename = $mainframe->getCfg( 'sitename' ); $mailfrom = $mainframe->getCfg( 'mailfrom' ); $fromname = $mainframe->getCfg( 'fromname' ); $siteURL = JURI::base(); // send approval email switch($msgID) { case 'ACCOUNT_ACTIVATED_EMAIL_TO_USER' : $data=array(); $data=$this->generalInformation(); $email=$data['email']; // $subject = "You are Approved"; $subject = sprintf(JText::_('EMAIL SUBJECT APPROVAL')); $subject = html_entity_decode($subject, ENT_QUOTES); $message = sprintf(JText::_('EMAIL ACCOUNT ACTIVATION EMAIL TO USER'),$data['name']); $message = html_entity_decode($message, ENT_QUOTES); JUtility::sendMail($mailfrom, $fromname, $email, $subject, $message); if($this->debugMode) $this->displayMessage($message); return; case 'EMAIL_TO_ADMIN_FOR_APPROVAL' : $getval = array(); $getval = $this->getMessage(); //get all super administrator $query = 'SELECT name, email, sendEmail' . ' FROM #__users' . ' WHERE LOWER( usertype ) = "super administrator"'; $this->db->setQuery( $query ); $rows = $this->db->loadObjectList(); // Send mail to all superadministrators id foreach( $rows as $row ) if ($row->sendEmail) JUtility::sendMail($mailfrom, $fromname, $row->email, $getval['subject'], $getval['message']); if($this->debugMode) $this->displayMessage($getval['message']); return; } } function getMessage() { //send request to approve to ADMIN // $val['subject'] = "Email for Approval"; $val['subject'] = sprintf(JText::_('EMAIL SUBJECT APPROVAL TO ADMIN')); $val['subject'] = html_entity_decode($val['subject'], ENT_QUOTES); require_once (JPATH_ROOT.DS.'plugins'.DS.'system'.DS .'xi_adminapproval'.DS.'tmpl'.DS.'mail-to-admin.php' ); $generaldetails=array(); $generaldetails=$this->generalInformation(); switch ($this->mode) { case XI_JOOMLA : break; case XI_JSPT : case XI_JOMSOCIAL : $msg = $this->generate_message_for_profile_fields(); if($msg == '' && $this->debugMode){ $this->displayMessage(JText::_('MSG NOT_JOMSOCIAL_AND_JSPT_USER')); } break; default: assert(0); } $mailJs = new mailtoadmin(); $val['message']=$mailJs->display($generaldetails,$msg); $val['message'] = html_entity_decode($val['message'], ENT_QUOTES); return $val; } }