| @@ -1,308 +1,0 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -use AcyMailing\Core\AcymPlugin; | |
| 4 | -use AcyMailing\Classes\UserClass; | |
| 5 | -use Joomla\Registry\Registry; | |
| 6 | -use Joomla\CMS\Access\Access; | |
| 7 | -use Joomla\CMS\Plugin\PluginHelper; | |
| 8 | -use Joomla\CMS\User\User; | |
| 9 | -use Joomla\CMS\User\UserHelper; | |
| 10 | -use Joomla\CMS\Factory; | |
| 11 | -use Joomla\CMS\Component\ComponentHelper; | |
| 12 | -use Joomla\CMS\Application\ApplicationHelper; | |
| 13 | - | |
| 14 | -class plgAcymcreateuser extends AcymPlugin | |
| 15 | -{ | |
| 16 | - public function __construct() | |
| 17 | - { | |
| 18 | - parent::__construct(); | |
| 19 | - $this->cms = 'all'; | |
| 20 | - | |
| 21 | - $usergroups = acym_getGroups(); | |
| 22 | - if ('joomla' === ACYM_CMS) { | |
| 23 | - $joomlaUsersparams = ComponentHelper::getParams('com_users'); | |
| 24 | - $defaultUsergroup = $joomlaUsersparams->get('new_usertype'); | |
| 25 | - } else { | |
| 26 | - $defaultUsergroup = get_option('default_role'); | |
| 27 | - } | |
| 28 | - | |
| 29 | - $onlyFrontOptions = [ | |
| 30 | - 'front' => acym_translation('ACYM_FRONT'), | |
| 31 | - 'both' => acym_translation('ACYM_FRONT_BACK'), | |
| 32 | - ]; | |
| 33 | - | |
| 34 | - $this->settings = [ | |
| 35 | - 'enableCreate' => [ | |
| 36 | - 'type' => 'switch', | |
| 37 | - 'label' => 'ACYM_ALLOW_CMS_USER_CREATION', | |
| 38 | - 'value' => 0, | |
| 39 | - ], | |
| 40 | - 'onlyFront' => [ | |
| 41 | - 'type' => 'select', | |
| 42 | - 'label' => 'ACYM_CREATE_FROM_SUB_ON', | |
| 43 | - 'value' => 1, | |
| 44 | - 'data' => $onlyFrontOptions, | |
| 45 | - ], | |
| 46 | - 'onModif' => [ | |
| 47 | - 'type' => 'switch', | |
| 48 | - 'label' => 'ACYM_CREATE_ON_USER_MODIFICATION', | |
| 49 | - 'value' => 0, | |
| 50 | - ], | |
| 51 | - 'group' => [ | |
| 52 | - 'type' => 'select', | |
| 53 | - 'label' => 'ACYM_USER_GROUP', | |
| 54 | - 'value' => $defaultUsergroup, | |
| 55 | - 'data' => $usergroups, | |
| 56 | - ], | |
| 57 | - ]; | |
| 58 | - } | |
| 59 | - | |
| 60 | - public function onAcymAfterUserCreate(&$user) | |
| 61 | - { | |
| 62 | - $this->createCmsUser($user); | |
| 63 | - } | |
| 64 | - | |
| 65 | - public function onAcymAfterUserModify(&$user, &$oldUser) | |
| 66 | - { | |
| 67 | - if ($this->getParam('onModif', '0') === '0') return; | |
| 68 | - | |
| 69 | - $this->createCmsUser($user); | |
| 70 | - } | |
| 71 | - | |
| 72 | - public function createCmsUser($user) | |
| 73 | - { | |
| 74 | - $enableCreate = $this->getParam('enableCreate', '0'); | |
| 75 | - if (empty($user->email) || !empty($user->cms_id) || empty($enableCreate)) return; | |
| 76 | - | |
| 77 | - if ($this->getParam('onlyFront', 'front') === 'front' && acym_isAdmin()) return; | |
| 78 | - | |
| 79 | - if ('wordpress' === ACYM_CMS) { | |
| 80 | - $this->createWordpressUser($user); | |
| 81 | - } else { | |
| 82 | - $this->createJoomlaUser($user); | |
| 83 | - } | |
| 84 | - } | |
| 85 | - | |
| 86 | - protected function createJoomlaUser($user) | |
| 87 | - { | |
| 88 | - $joomlaUsersparams = ComponentHelper::getParams('com_users'); | |
| 89 | - $joomlaConfig = Factory::getConfig(); | |
| 90 | - | |
| 91 | - $useractivation = $joomlaUsersparams->get('useractivation'); | |
| 92 | - $allowUserRegistration = $joomlaUsersparams->get('allowUserRegistration'); | |
| 93 | - if ($allowUserRegistration == 0) return; | |
| 94 | - | |
| 95 | - acym_loadLanguageFile('com_users', JPATH_SITE); | |
| 96 | - | |
| 97 | - $minimum_integers = $joomlaUsersparams->get('minimum_integers'); | |
| 98 | - $minimum_symbols = $joomlaUsersparams->get('minimum_symbols'); | |
| 99 | - $minimum_uppercase = $joomlaUsersparams->get('minimum_uppercase'); | |
| 100 | - $length = 8; | |
| 101 | - if ($joomlaUsersparams->get('minimum_length') > 8) $length = $joomlaUsersparams->get('minimum_length'); | |
| 102 | - $tryCount = 0; | |
| 103 | - do { | |
| 104 | - $password = UserHelper::genrandompassword($length); | |
| 105 | - $tryCount++; | |
| 106 | - if ($tryCount > 50) break; | |
| 107 | - } while ($this->getConditionPassword($password, $minimum_integers, $minimum_symbols, $minimum_uppercase)); | |
| 108 | - | |
| 109 | - $defaultUsergroup = $joomlaUsersparams->get('new_usertype'); | |
| 110 | - $configUserGroup = $this->getParam('group', $defaultUsergroup); | |
| 111 | - | |
| 112 | - if (empty($user->name)) { | |
| 113 | - $user->name = ucwords(trim(str_replace(['.', '_', ')', ',', '(', '-', 1, 2, 3, 4, 5, 6, 7, 8, 9, 0], ' ', substr($user->email, 0, strpos($user->email, '@'))))); | |
| 114 | - } | |
| 115 | - $userData = [ | |
| 116 | - 'name' => $user->name, | |
| 117 | - 'username' => $user->email, | |
| 118 | - 'password' => $password, | |
| 119 | - 'password2' => $password, | |
| 120 | - 'email' => $user->email, | |
| 121 | - 'block' => 0, | |
| 122 | - 'groups' => [$configUserGroup], | |
| 123 | - ]; | |
| 124 | - | |
| 125 | - $joomlaUser = new User(); | |
| 126 | - | |
| 127 | - if (in_array($useractivation, [1, 2])) { | |
| 128 | - $userData['activation'] = ApplicationHelper::getHash(UserHelper::genrandompassword()); | |
| 129 | - $userData['block'] = 1; | |
| 130 | - } | |
| 131 | - | |
| 132 | - if (!$joomlaUser->bind($userData)) return false; | |
| 133 | - if (!$this->save($joomlaUser)) return false; | |
| 134 | - | |
| 135 | - $user->cms_id = $joomlaUser->id; | |
| 136 | - $userClass = new UserClass(); | |
| 137 | - $userClass->sendConf = false; | |
| 138 | - $userClass->save($user); | |
| 139 | - | |
| 140 | - $data = $joomlaUser->getProperties(); | |
| 141 | - $data['fromname'] = $joomlaConfig->get('fromname'); | |
| 142 | - $data['mailfrom'] = $joomlaConfig->get('mailfrom'); | |
| 143 | - $data['sitename'] = $joomlaConfig->get('sitename'); | |
| 144 | - $data['siteurl'] = str_replace('/administrator', '', acym_baseURI()); | |
| 145 | - | |
| 146 | - $emailSubject = acym_translationSprintf('COM_USERS_EMAIL_ACCOUNT_DETAILS', $data['name'], $data['sitename']); | |
| 147 | - if (in_array($useractivation, [1, 2])) { | |
| 148 | - $base = acym_currentURL(); | |
| 149 | - $activationLink = 'index.php?option=com_users&task=registration.activate&token='; | |
| 150 | - $data['activate'] = $data['siteurl'].$activationLink.$data['activation']; | |
| 151 | - | |
| 152 | - if ($useractivation == 2) { | |
| 153 | - $emailBody = acym_translationSprintf( | |
| 154 | - 'COM_USERS_EMAIL_REGISTERED_WITH_ADMIN_ACTIVATION_BODY', | |
| 155 | - $data['name'], | |
| 156 | - $data['sitename'], | |
| 157 | - $data['activate'], | |
| 158 | - $data['siteurl'], | |
| 159 | - $data['username'], | |
| 160 | - $data['password_clear'] | |
| 161 | - ); | |
| 162 | - } else { | |
| 163 | - $activationMessage = 'COM_USERS_EMAIL_REGISTERED_WITH_ACTIVATION_BODY'; | |
| 164 | - $emailBody = acym_translationSprintf( | |
| 165 | - $activationMessage, | |
| 166 | - $data['name'], | |
| 167 | - $data['sitename'], | |
| 168 | - $data['activate'], | |
| 169 | - $data['siteurl'], | |
| 170 | - $data['username'], | |
| 171 | - $data['password_clear'] | |
| 172 | - ); | |
| 173 | - } | |
| 174 | - } else { | |
| 175 | - $emailBody = acym_translationSprintf( | |
| 176 | - 'COM_USERS_EMAIL_REGISTERED_BODY', | |
| 177 | - $data['name'], | |
| 178 | - $data['sitename'], | |
| 179 | - $data['siteurl'], | |
| 180 | - $data['username'], | |
| 181 | - $data['password_clear'] | |
| 182 | - ); | |
| 183 | - } | |
| 184 | - | |
| 185 | - if (ACYM_J40) { | |
| 186 | - $keys = []; | |
| 187 | - $replaceData = []; | |
| 188 | - foreach ($data as $key => $oneKey) { | |
| 189 | - if (is_array($oneKey)) continue; | |
| 190 | - | |
| 191 | - $keys[] = '{'.strtoupper($key).'}'; | |
| 192 | - $replaceData[] = $oneKey; | |
| 193 | - } | |
| 194 | - $emailSubject = str_replace($keys, $replaceData, $emailSubject); | |
| 195 | - $emailBody = str_replace($keys, $replaceData, $emailBody); | |
| 196 | - } | |
| 197 | - | |
| 198 | - $mailer = Factory::getMailer(); | |
| 199 | - $sender = [$joomlaConfig->get('mailfrom'), $joomlaConfig->get('fromname')]; | |
| 200 | - $mailer->setSender($sender); | |
| 201 | - $mailer->addRecipient($user->email); | |
| 202 | - $mailer->setSubject($emailSubject); | |
| 203 | - $mailer->setBody($emailBody); | |
| 204 | - $send = $mailer->Send(); | |
| 205 | - if ($send !== true) return false; | |
| 206 | - } | |
| 207 | - | |
| 208 | - public function save($joomlaUser) | |
| 209 | - { | |
| 210 | - $table = $joomlaUser->getTable(); | |
| 211 | - $joomlaUser->params = '{}'; | |
| 212 | - $table->bind($joomlaUser->getProperties()); | |
| 213 | - | |
| 214 | - try { | |
| 215 | - if (!$table->check()) { | |
| 216 | - $joomlaUser->setError($table->getError()); | |
| 217 | - | |
| 218 | - return false; | |
| 219 | - } | |
| 220 | - | |
| 221 | - $isNew = empty($joomlaUser->id); | |
| 222 | - | |
| 223 | - $my = Factory::getUser(); | |
| 224 | - | |
| 225 | - $oldUser = new User($joomlaUser->id); | |
| 226 | - | |
| 227 | - $iAmRehashingSuperadmin = false; | |
| 228 | - if (($my->id == 0 && !$isNew) && $joomlaUser->id == $oldUser->id && $oldUser->authorise('core.admin') && $oldUser->password != $joomlaUser->password) { | |
| 229 | - $iAmRehashingSuperadmin = true; | |
| 230 | - } | |
| 231 | - | |
| 232 | - $iAmSuperAdmin = $my->authorise('core.admin'); | |
| 233 | - if ($iAmSuperAdmin != true && $iAmRehashingSuperadmin != true) { | |
| 234 | - if (!$isNew && Access::check($joomlaUser->id, 'core.admin')) { | |
| 235 | - throw new \RuntimeException('User not Super Administrator'); | |
| 236 | - } | |
| 237 | - | |
| 238 | - if ($joomlaUser->groups != null) { | |
| 239 | - foreach ($joomlaUser->groups as $groupId) { | |
| 240 | - if (Access::checkGroup($groupId, 'core.admin')) { | |
| 241 | - throw new \RuntimeException('User not Super Administrator'); | |
| 242 | - } | |
| 243 | - } | |
| 244 | - } | |
| 245 | - } | |
| 246 | - | |
| 247 | - PluginHelper::importPlugin('user'); | |
| 248 | - if (ACYM_J40) { | |
| 249 | - $result = Factory::getApplication()->triggerEvent('onUserBeforeSave', [$oldUser->getProperties(), $isNew, $joomlaUser->getProperties()]); | |
| 250 | - } else { | |
| 251 | - $dispatcher = \JEventDispatcher::getInstance(); | |
| 252 | - $result = $dispatcher->trigger('onUserBeforeSave', [$oldUser->getProperties(), $isNew, $joomlaUser->getProperties()]); | |
| 253 | - } | |
| 254 | - if (in_array(false, $result, true)) return false; | |
| 255 | - | |
| 256 | - $result = $table->store(); | |
| 257 | - | |
| 258 | - if (empty($joomlaUser->id)) { | |
| 259 | - $joomlaUser->id = $table->get('id'); | |
| 260 | - } | |
| 261 | - | |
| 262 | - if ($my->id == $table->id) { | |
| 263 | - $registry = new Registry($table->params); | |
| 264 | - $my->setParameters($registry); | |
| 265 | - } | |
| 266 | - } catch (\Exception $e) { | |
| 267 | - $joomlaUser->setError($e->getMessage()); | |
| 268 | - | |
| 269 | - return false; | |
| 270 | - } | |
| 271 | - | |
| 272 | - return $result; | |
| 273 | - } | |
| 274 | - | |
| 275 | - protected function createWordpressUser($user) | |
| 276 | - { | |
| 277 | - if (!get_option('users_can_register')) return; | |
| 278 | - | |
| 279 | - if (false !== username_exists($user->email)) return false; | |
| 280 | - | |
| 281 | - $defaultUsergroup = get_option('default_role'); | |
| 282 | - $configUserGroup = $this->getParam('group', $defaultUsergroup); | |
| 283 | - | |
| 284 | - $pwdTemp = wp_generate_password(12, true); | |
| 285 | - $result = wp_create_user($user->email, $pwdTemp, $user->email); | |
| 286 | - | |
| 287 | - if (is_wp_error($result)) { | |
| 288 | - return false; | |
| 289 | - } else { | |
| 290 | - $user->cms_id = $result; | |
| 291 | - $userClass = new UserClass(); | |
| 292 | - $userClass->sendConf = false; | |
| 293 | - $userClass->save($user); | |
| 294 | - $wpUser = new WP_User($result); | |
| 295 | - $wpUser->set_role($configUserGroup); | |
| 296 | - wp_new_user_notification($result, null, 'user'); | |
| 297 | - } | |
| 298 | - } | |
| 299 | - | |
| 300 | - protected function getConditionPassword($password, $minimum_integers, $minimum_symbols, $minimum_uppercase) | |
| 301 | - { | |
| 302 | - $notEnoughInt = !empty($minimum_integers) && preg_match_all("/[0-9]/", $password, $out) < $minimum_integers; | |
| 303 | - $notEnoughSymbols = !empty($minimum_symbols) && preg_match_all("/[a-z]/", $password, $out) < $minimum_symbols; | |
| 304 | - $notEnoughUpperCase = !empty($minimum_uppercase) && preg_match_all("/[A-Z]/", $password, $out) < $minimum_uppercase; | |
| 305 | - | |
| 306 | - return $notEnoughInt || $notEnoughSymbols || $notEnoughUpperCase; | |
| 307 | - } | |
| 308 | -} | |