package Dojo::Model::Users; use strict; use warnings; use Mojo::File 'path'; use DBI; use Mojo::Util 'secure_compare'; use Dojo::Conf; my $data_path = path('lib/Dojo/Model/Data')->make_path; sub new { bless {}, shift } sub createu{ my ($c,$user,$uname,$tel,$mail,$pass,$passchk)=@_; my $ttel= $tel =~m/^$/ ? "0000000000" : $tel; $ttel =~ s/[^\+0-9]//g; if( $ttel =~m/\+?[0-9]{10,}/ && Email::Valid->address($mail) && $user =~m/[a-zA-Z]{3,25}/ && $user !~m/\s/ && #mejora este $uname =~m/[a-zA-Z]{3,50}/ && ($pass eq $passchk) ){ if( $user =~m/\s|sex|http|webpage/ && $uname =~m/sex|http|webpage/ ){ return 2} #possible spam # $ttel = $tel =~m/^$/? "null"; if ( _write( "insert into usuario (usuario,nombre,telefono,correo,pass,permiso_id,ultima) values (?,?,?,?,?,2,now())", $user,$uname,$ttel,$mail,$pass) == 0){return 0;} return 1; } return 0; } sub grulla_pass{ return _read( "select pass as gpass from usuario where usuario like 'grullas'")->[0] }; sub ugrulla_pass{ my($c,$p)=@_; _write("update usuario set pass= ? where usuario like 'grullas'",$p); } sub check { my ($self, $user, $pass) = @_; my $q="select permiso_id as pid from usuario where usuario = ? and pass=? "; # | 0 | bloqueado | # | 1 | invitado | # | 2 | usuario | # | 3 | pago | # | 4 | master | my $r= _read($q,$user,$pass)->[0]; return $r->{pid} // 0; } sub store{ return _read ($data_path->child("/store/qStore.q")->slurp); } sub store_id{ my ($self,$id)=@_; return _read ($data_path->child("/store/qIdStore.q")->slurp,$id)->[0]; } sub _read{ my ($q,@bind)=@_; my (@empty,$arr); my $dbh = DBI->connect("DBI:mysql:".Dojo::Conf::GRULLADB.":".Dojo::Conf::GRULLADB_H,Dojo::Conf::GRULLADB_UR,Dojo::Conf::GRULLADB_URP,{mysql_enable_utf8 => 1}); return \@empty unless($dbh); #$dbh->do(qq{SET NAMES 'utf8'}); my $h=$dbh->selectall_arrayref($q,{ Slice => {} },@bind)//"error"; #((col1=>d1,col2=>d1),(col1=>d2,col2=>d2)) $dbh->disconnect(); return $h; } sub _write{ my ($q,@bind)=@_; my (@empty); my $dbh = DBI->connect("DBI:mysql:".Dojo::Conf::GRULLADB.":".Dojo::Conf::GRULLADB_H,Dojo::Conf::GRULLADB_UW,Dojo::Conf::GRULLADB_UWP,{mysql_enable_utf8 => 1}); return 0 unless($dbh); my $h=$dbh->do($q,{ Slice => {} },@bind)//"error"; $dbh->disconnect(); # log("db write:". $h ); return $h; } 1;