tazbug rev 48

Add online signup, well tested with TinyCM
author Christophe Lincoln <pankso@slitaz.org>
date Sat Jan 04 21:46:07 2014 +0100 (2014-01-04)
parents 4bd5e2d2092b
children bb625b6de902
files web/bugs.cgi web/bugs.conf web/lib/functions.js
line diff
     1.1 --- a/web/bugs.cgi	Thu Jul 25 03:45:14 2013 +0300
     1.2 +++ b/web/bugs.cgi	Sat Jan 04 21:46:07 2014 +0100
     1.3 @@ -2,15 +2,17 @@
     1.4  #
     1.5  # TazBug Web interface
     1.6  #
     1.7 -# Copyright (C) 2012 SliTaz GNU/Linux - BSD License
     1.8 +# Copyright (C) 2012-2014 SliTaz GNU/Linux - BSD License
     1.9  #
    1.10  . /usr/lib/slitaz/httphelper
    1.11  [ -f "/etc/slitaz/bugs.conf" ] && . /etc/slitaz/bugs.conf
    1.12 +#. bugs.conf
    1.13  
    1.14  # Internal variable
    1.15  bugdir="$TAZBUG/bug"
    1.16  plugins="plugins"
    1.17  sessions="/tmp/bugs/sessions"
    1.18 +script="$SCRIPT_NAME"
    1.19  po=""
    1.20  
    1.21  # Content negotiation for Gettext
    1.22 @@ -40,11 +42,6 @@
    1.23  TEXTDOMAIN='tazbug'
    1.24  export TEXTDOMAIN
    1.25  
    1.26 -
    1.27 -
    1.28 -
    1.29 -
    1.30 -
    1.31  #
    1.32  # Functions
    1.33  #
    1.34 @@ -80,13 +77,11 @@
    1.35  	echo "<script type=\"text/javascript\"> document.location = \"$1\"; </script>"
    1.36  }
    1.37  
    1.38 -
    1.39  js_log()
    1.40  {
    1.41  	echo "<script type=\"text/javascript\">console.log('$1')</script>";
    1.42  }
    1.43  
    1.44 -
    1.45  js_set_cookie()
    1.46  {
    1.47  	name=$1
    1.48 @@ -98,7 +93,6 @@
    1.49  	echo "</script>"
    1.50  }
    1.51  
    1.52 -
    1.53  js_unset_cookie()
    1.54  {
    1.55  	name=$1
    1.56 @@ -109,7 +103,6 @@
    1.57  	echo "</script>"
    1.58  }
    1.59  
    1.60 -
    1.61  # Check if user is auth
    1.62  check_auth() {
    1.63  	auth="$(COOKIE auth)"
    1.64 @@ -137,7 +130,7 @@
    1.65  		cat << EOT
    1.66  <div id="user">
    1.67  <a href="?user=$user">$(get_gravatar $MAIL 20)</a>
    1.68 -<a href="?logout">$(gettext 'Log out')</a>
    1.69 +<a href="?logout">$(gettext 'Logout')</a>
    1.70  </div>
    1.71  EOT
    1.72  	else
    1.73 @@ -163,6 +156,35 @@
    1.74  EOT
    1.75  }
    1.76  
    1.77 +# Signup page
    1.78 +signup_page() {
    1.79 +	cat << EOT
    1.80 +
    1.81 +<div id="signup">
    1.82 +	<form method="post" name="signup" action="$SCRIPT_NAME" onsubmit="return checkSignup();">
    1.83 +		<input type="hidden" name="signup" value="new" />
    1.84 +		<input type="text" name="name" placeholder="$(gettext "Real name")" />
    1.85 +		<input type="text" name="user" placeholder="$(gettext "User name")" />
    1.86 +		<input type="text" name="mail" placeholder="$(gettext "Email")" />
    1.87 +		<input type="password" name="pass" placeholder="$(gettext "Password")" />
    1.88 +		<div>
    1.89 +			<input type="submit" value="$(gettext "Create new account")" />
    1.90 +		</div>
    1.91 +	</form>
    1.92 +</div>
    1.93 +
    1.94 +EOT
    1.95 +}
    1.96 +
    1.97 +# Link for online signup if enabled.
    1.98 +online_signup() {
    1.99 +	if [ "$ONLINE_SIGNUP" == "yes" ]; then
   1.100 +		echo -n "<p>" && gettext "Or:"; echo -n " "
   1.101 +		echo -n "<a href='$script?signup&amp;online'>"
   1.102 +		gettext "Sign Up Online"
   1.103 +		echo '</a></p>'
   1.104 +	fi
   1.105 +}
   1.106  
   1.107  # Login page
   1.108  login_page() {
   1.109 @@ -175,8 +197,9 @@
   1.110  <h2>$(gettext 'Login')</h2>
   1.111  
   1.112  <div id="account-info">
   1.113 -<p>$(gettext "No account yet? Please signup using the SliTaz Bugs reporter \
   1.114 +<p>$(gettext "No account yet? You can signup using the SliTaz Bugs reporter \
   1.115  on your SliTaz system.")</p>
   1.116 +$(online_signup)
   1.117  <p>$(gettext "Tip: to attach big files or images, you can use SliTaz Paste \
   1.118  services:") <a href="http://paste.slitaz.org/">paste.slitaz.org</a></p>
   1.119  </div>
   1.120 @@ -196,7 +219,6 @@
   1.121  EOT
   1.122  }
   1.123  
   1.124 -
   1.125  # Display user public profile.
   1.126  public_people() {
   1.127  	cat << EOT
   1.128 @@ -206,7 +228,6 @@
   1.129  EOT
   1.130  }
   1.131  
   1.132 -
   1.133  # Display authentified user profile. TODO: change password
   1.134  auth_people() {
   1.135  	cat << EOT
   1.136 @@ -218,7 +239,6 @@
   1.137  EOT
   1.138  }
   1.139  
   1.140 -
   1.141  # Usage: list_bugs STATUS
   1.142  list_bugs() {
   1.143  	bug="$1"
   1.144 @@ -469,17 +489,20 @@
   1.145  
   1.146  # Create a new user in AUTH_FILE and PEOPLE
   1.147  new_user_config() {
   1.148 -	mail="$(GET mail)"
   1.149 -	pass="$(GET pass)"
   1.150 +	if [ ! "$online" ]; then
   1.151 +		name="$(GET name)"
   1.152 +		mail="$(GET mail)"
   1.153 +		pass="$(GET pass)"
   1.154 +		echo "Creating Server Key..."
   1.155 +	fi
   1.156  	key=$(echo -n "$user:$mail:$pass" | md5sum | awk '{print $1}')
   1.157 -	echo "Server Key generated"
   1.158  	echo "$user:$pass" >> $AUTH_FILE
   1.159  	mkdir -pm0700 $PEOPLE/$user/
   1.160  	cat > $PEOPLE/$user/account.conf << EOT
   1.161  # SliTaz user configuration
   1.162  #
   1.163  
   1.164 -NAME="$(GET name)"
   1.165 +NAME="$name"
   1.166  USER="$user"
   1.167  MAIL="$mail"
   1.168  KEY="$key"
   1.169 @@ -492,15 +515,12 @@
   1.170  	chmod 0600 $PEOPLE/$user/account.conf
   1.171  	if [ ! -f $PEOPLE/$user/account.conf ]; then
   1.172  		echo "ERROR: User creation failed!"
   1.173 -		fi;
   1.174 -	}
   1.175 +	fi
   1.176 +}
   1.177  
   1.178 -
   1.179 -
   1.180 -
   1.181 -###################################################
   1.182 -# POST actions
   1.183 -###################################################
   1.184 +########################################################################
   1.185 +# POST actions                                                         #
   1.186 +########################################################################
   1.187  
   1.188  case " $(POST) " in
   1.189  	*\ auth\ *)
   1.190 @@ -513,9 +533,9 @@
   1.191  		pass="$(echo -n "$(POST pass)" | md5sum | awk '{print $1}')"
   1.192  
   1.193  		IDLOC=""
   1.194 -			if [[ "$(GET id)" ]] ;then
   1.195 -				IDLOC="&id=$(GET id)"
   1.196 -			fi
   1.197 +		if [[ "$(GET id)" ]] ;then
   1.198 +			IDLOC="&id=$(GET id)"
   1.199 +		fi
   1.200  
   1.201  		if [  ! -f $AUTH_FILE ] ; then
   1.202  			js_log "$AUTH_FILE (defined in \$AUTH_FILE) have not been found."
   1.203 @@ -537,15 +557,30 @@
   1.204  			js_log "Login authentification have been executed & refused"
   1.205  			js_redirection_to "$WEB_URL?login&error$IDLOC"
   1.206  		fi
   1.207 -
   1.208 -		html_footer
   1.209 -		;;
   1.210 +		html_footer ;;
   1.211 +	*\ signup\ *)
   1.212 +		# POST action for online signup
   1.213 +		name="$(POST name)"
   1.214 +		user="$(POST user)"
   1.215 +		mail="$(POST mail)"
   1.216 +		pass="$(md5crypt "$(POST pass)")"
   1.217 +		if ! grep "^${user}:" $AUTH_FILE; then
   1.218 +			online="yes"
   1.219 +			new_user_config
   1.220 +			header "Location: $SCRIPT_NAME?login"
   1.221 +		else
   1.222 +			header
   1.223 +			html_header
   1.224 +			user_box
   1.225 +			echo "<h2>gettext "User already exists: $user"</h2>"
   1.226 +			html_footer && exit 0
   1.227 +		fi ;;
   1.228  esac
   1.229  
   1.230  
   1.231 -###################################################
   1.232 -# GET actions
   1.233 -###################################################
   1.234 +########################################################################
   1.235 +# GET actions                                                          #
   1.236 +########################################################################
   1.237  
   1.238  case " $(GET) " in
   1.239  	*\ README\ *)
   1.240 @@ -645,15 +680,27 @@
   1.241  		html_footer ;;
   1.242  	*\ signup\ *)
   1.243  		# Signup
   1.244 -		header "Content-type: text/plain;"
   1.245 -		user="$(GET signup)"
   1.246 -		echo "Requested user login : $user"
   1.247 -		if fgrep -q "$user:" $AUTH_FILE; then
   1.248 -			echo "ERROR: User already exists" && exit 1
   1.249 +		if [ "$(GET online)" ];then
   1.250 +			header
   1.251 +			html_header
   1.252 +			user_box
   1.253 +			echo "<h2>$(gettext "Sign Up")</h2>"
   1.254 +			if [ "$ONLINE_SIGNUP" == "yes" ]; then
   1.255 +				signup_page
   1.256 +			else
   1.257 +				gettext "Online registration is disabled"
   1.258 +			fi
   1.259 +			html_footer && exit 0
   1.260  		else
   1.261 -
   1.262 -			echo "Creating account for : $(GET name)"
   1.263 -			new_user_config
   1.264 +			header "Content-type: text/plain;"
   1.265 +			user="$(GET signup)"
   1.266 +			echo "Requested user login : $user"
   1.267 +			if fgrep -q "$user:" $AUTH_FILE; then
   1.268 +				echo "ERROR: User already exists" && exit 1
   1.269 +			else
   1.270 +				echo "Creating account for : $(GET name)"
   1.271 +				new_user_config
   1.272 +			fi
   1.273  		fi ;;
   1.274  	*\ key\ *)
   1.275  		# Let user post new bug or message with crypted key (no gettext)
   1.276 @@ -762,9 +809,9 @@
   1.277  esac
   1.278  
   1.279  
   1.280 -###################################################
   1.281 -# Plugins
   1.282 -###################################################
   1.283 +########################################################################
   1.284 +# Plugins                                                              #
   1.285 +########################################################################
   1.286  
   1.287  for p in $(ls -1 $plugins)
   1.288  do
     2.1 --- a/web/bugs.conf	Thu Jul 25 03:45:14 2013 +0300
     2.2 +++ b/web/bugs.conf	Sat Jan 04 21:46:07 2014 +0100
     2.3 @@ -12,3 +12,6 @@
     2.4  
     2.5  # People config files
     2.6  PEOPLE="/var/lib/slitaz/people"
     2.7 +
     2.8 +# Online registration for user
     2.9 +ONLINE_SIGNUP="yes"
     3.1 --- a/web/lib/functions.js	Thu Jul 25 03:45:14 2013 +0300
     3.2 +++ b/web/lib/functions.js	Sat Jan 04 21:46:07 2014 +0100
     3.3 @@ -16,3 +16,27 @@
     3.4          return false;
     3.5      }
     3.6  }
     3.7 +
     3.8 +// Check form to avoid empty values and bad email.
     3.9 +function checkSignup() {
    3.10 +	if(document.forms["signup"]["name"].value == "")
    3.11 +    {
    3.12 +        alert("Please enter your real name");
    3.13 +        document.forms["signup"]["name"].focus();
    3.14 +        return false;
    3.15 +    }
    3.16 +    if(document.forms["signup"]["user"].value == "")
    3.17 +    {
    3.18 +        alert("Please fill in your user name");
    3.19 +        document.forms["signup"]["user"].focus();
    3.20 +        return false;
    3.21 +    }
    3.22 +	var x=document.forms["signup"]["mail"].value;
    3.23 +	var atpos=x.indexOf("@");
    3.24 +	var dotpos=x.lastIndexOf(".");
    3.25 +	if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
    3.26 +	{
    3.27 +		alert("Missing or not a valid email address");
    3.28 +		return false;
    3.29 +	}
    3.30 +}