Skip to content

Commit

Permalink
Merge pull request #250 from pH7Software/change-profile-page-url-pattern
Browse files Browse the repository at this point in the history
Change profile page URL pattern
  • Loading branch information
pH-7 authored Jul 28, 2018
2 parents 2eed355 + 859508e commit c2ba048
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion _protected/app/configs/routes/en.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<!-- BEGIN SYSTEM MODULES -->

<!-- User -->
<route url="([a-zA-Z0-9_-]+)[$page_ext]" path="system/modules" module="user" controller="profile" action="index" vars="username" />
<route url="@([a-zA-Z0-9_-]+)" path="system/modules" module="user" controller="profile" action="index" vars="username" />
<route url="signup" path="system/modules" module="user" controller="signup" action="step1" />
<route url="login" path="system/modules" module="user" controller="main" action="login" />
<route url="resend-activation" path="system/modules" module="user" controller="main" action="resendactivation" />
Expand Down
7 changes: 5 additions & 2 deletions _protected/app/system/core/classes/UserCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
// Abstract Class
class UserCore
{
/** The prefix of the profile page URI path (eg https://mysite.com/@<USERNAME>) */
const PROFILE_PAGE_PREFIX = '@';

const MAX_WIDTH_AVATAR = 600;
const MAX_HEIGHT_AVATAR = 800;

Expand Down Expand Up @@ -316,9 +319,9 @@ public function deleteBackground($iProfileId, $sUsername)
public function getProfileLink($sUsername)
{
$sUsername = (new Str)->lower($sUsername);
//return (strlen($sUsername) >1) ? PH7_URL_ROOT . $sUsername . PH7_PAGE_EXT : '#';
//return (strlen($sUsername) > 1) ? PH7_URL_ROOT . '@' . $sUsername : '#';

return PH7_URL_ROOT . $sUsername . PH7_PAGE_EXT;
return PH7_URL_ROOT . self::PROFILE_PAGE_PREFIX . $sUsername;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion _protected/app/system/modules/user/forms/JoinForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static function step1()
$oForm->addElement(new \PFBC\Element\Textbox(t('Your First Name'), 'first_name', ['placeholder' => t('First Name'), 'id' => 'name_first', 'onblur' => 'CValid(this.value,this.id)', 'required' => 1, 'validation' => new \PFBC\Validation\Name]));
$oForm->addElement(new \PFBC\Element\HTMLExternal('<span class="input_error name_first"></span>'));

$oForm->addElement(new \PFBC\Element\Username(t('Your Nickname'), 'username', ['placeholder' => t('Nickname'), 'description' => PH7_URL_ROOT . '<strong><span class="your-user-name">' . t('your-user-name') . '</span><span class="username"></span></strong>' . PH7_PAGE_EXT, 'id' => 'username', 'required' => 1, 'validation' => new \PFBC\Validation\Username]));
$oForm->addElement(new \PFBC\Element\Username(t('Your Nickname'), 'username', ['placeholder' => t('Nickname'), 'description' => PH7_URL_ROOT . UserCore::PROFILE_PAGE_PREFIX . '<strong><span class="your-user-name">' . t('your-user-name') . '</span><span class="username"></span></strong>', 'id' => 'username', 'required' => 1, 'validation' => new \PFBC\Validation\Username]));

$oForm->addElement(new \PFBC\Element\Email(t('Your Email'), 'mail', ['placeholder' => t('Email'), 'id' => 'email', 'onblur' => 'CValid(this.value, this.id,\'guest\')', 'required' => 1, 'validation' => new \PFBC\Validation\CEmail('guest')]));
$oForm->addElement(new \PFBC\Element\HTMLExternal('<span class="input_error email"></span>'));
Expand Down
7 changes: 0 additions & 7 deletions _protected/framework/Mvc/Router/Uri.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,6 @@ private static function getRouteFilePath()
*/
private static function parseVariable($sContents)
{
/**
* Replace the "[$page_ext]" variable by the "PH7_PAGE_EXT" constant.
*
* @internal We add a slash for RegEx ignores the dot (e.g., '.'html), (in RegEx, the dot means "any single character").
*/
$sContents = str_replace('[$page_ext]', '\\' . PH7_PAGE_EXT, $sContents);

/**
* Replace the "[$admin_mod]" variable by the "PH7_ADMIN_MOD" constant.
*/
Expand Down
2 changes: 1 addition & 1 deletion _protected/framework/Navigation/Page.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,6 @@ private static function trailingSlash($sUrl)
*/
private static function getUrlSlug($sCurrentUrl)
{
return strpos($sCurrentUrl, '&amp;') !== false ? strrchr($sCurrentUrl, '?') : strrchr($sCurrentUrl, '?');
return strpos($sCurrentUrl, '&amp;') !== false ? strrchr($sCurrentUrl, '?') !== false : strrchr($sCurrentUrl, '?');
}
}
20 changes: 18 additions & 2 deletions _protected/framework/Parse/User.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class User
*/
public static function atUsernameToLink($sContents)
{
foreach (static::getAtUsernames($sContents) as $sUsername) {
foreach (self::getAtUsernames($sContents) as $sUsername) {
$sUsernameLink = (new UserCore)->getProfileLink($sUsername);

$sContents = str_replace(
Expand All @@ -52,7 +52,7 @@ public static function atUsernameToLink($sContents)
*/
private static function getAtUsernames($sContents)
{
if (preg_match_all('#' . static::AT . '(' . PH7_USERNAME_PATTERN . '{' . DbConfig::getSetting('minUsernameLength') . ',' . DbConfig::getSetting('maxUsernameLength') . '})#u', $sContents, $aMatches, PREG_PATTERN_ORDER)) {
if (self::areProfileFound($sContents, $aMatches)) {
$aMatches[1] = array_unique($aMatches[1]); // Delete duplicate usernames.
foreach ($aMatches[1] as $sUsername) {
if ((new ExistsCoreModel)->username($sUsername)) {
Expand All @@ -61,4 +61,20 @@ private static function getAtUsernames($sContents)
}
}
}

/**
* @param string $sContents
* @param array $aMatches
*
* @return false|int
*/
private static function areProfileFound($sContents, &$aMatches)
{
return preg_match_all(
'#' . static::AT . '(' . PH7_USERNAME_PATTERN . '{' . DbConfig::getSetting('minUsernameLength') . ',' . DbConfig::getSetting('maxUsernameLength') . '})#u',
$sContents,
$aMatches,
PREG_PATTERN_ORDER
);
}
}
4 changes: 3 additions & 1 deletion _protected/framework/Security/Validate/Validate.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use PH7\Framework\Math\Measure\Year as YearMeasure;
use PH7\Framework\Security\Ban\Ban;
use PH7\Framework\Str\Str;
use PH7\UserCore;

class Validate
{
Expand Down Expand Up @@ -224,7 +225,8 @@ public function username($sUsername, $iMin = self::DEF_MIN_USERNAME_LENGTH, $iMa

return (
preg_match('#^' . PH7_USERNAME_PATTERN . '{' . $iMin . ',' . $iMax . '}$#', $sUsername) &&
!file_exists(PH7_PATH_ROOT . $sUsername . PH7_PAGE_EXT) && !Ban::isUsername($sUsername) &&
!file_exists(PH7_PATH_ROOT . UserCore::PROFILE_PAGE_PREFIX . $sUsername) &&
!Ban::isUsername($sUsername) &&
!(new ExistsCoreModel)->username($sUsername, $sTable)
);
}
Expand Down

0 comments on commit c2ba048

Please sign in to comment.