more tidying shimmie api and user page
This commit is contained in:
parent
793bc3614b
commit
78c44c7067
@ -48,52 +48,30 @@ class _SafeImage {
|
||||
|
||||
class ShimmieApi extends Extension {
|
||||
public function onPageRequest(PageRequestEvent $event) {
|
||||
global $database, $page, $user;
|
||||
global $page, $user;
|
||||
|
||||
if($event->page_matches("api/shimmie")) {
|
||||
$page->set_mode("data");
|
||||
$page->set_type("text/plain");
|
||||
if(!$event->page_matches("api/shimmie/get_tags") && !$event->page_matches("api/shimmie/get_image") && !$event->page_matches("api/shimmie/find_images") && !$event->page_matches("api/shimmie/get_user")){
|
||||
$page->set_mode("redirect");
|
||||
$page->set_redirect(make_link("ext_doc/shimmie_api"));
|
||||
}
|
||||
|
||||
if($event->page_matches("api/shimmie/get_tags")){
|
||||
$arg = $event->get_arg(0);
|
||||
|
||||
if(!empty($arg)){
|
||||
$all = $database->get_all(
|
||||
"SELECT tag FROM tags WHERE tag LIKE ?",
|
||||
array($arg."%"));
|
||||
}
|
||||
elseif(isset($_GET['tag'])){
|
||||
$all = $database->get_all(
|
||||
"SELECT tag FROM tags WHERE tag LIKE ?",
|
||||
array($_GET['tag']."%"));
|
||||
}
|
||||
else {
|
||||
$all = $database->get_all("SELECT tag FROM tags");
|
||||
}
|
||||
$res = array();
|
||||
foreach($all as $row) {$res[] = $row["tag"];}
|
||||
$tag = $event->get_arg(0);
|
||||
if(empty($tag) && isset($_GET['tag'])) $tag = $_GET['tag'];
|
||||
$res = $this->api_get_tags($tag);
|
||||
$page->set_data(json_encode($res));
|
||||
}
|
||||
|
||||
if($event->page_matches("api/shimmie/get_image")) {
|
||||
elseif($event->page_matches("api/shimmie/get_image")) {
|
||||
$arg = $event->get_arg(0);
|
||||
if(!empty($arg)){
|
||||
$image = Image::by_id(int_escape($event->get_arg(0)));
|
||||
}
|
||||
elseif(isset($_GET['id'])){
|
||||
$image = Image::by_id(int_escape($_GET['id']));
|
||||
}
|
||||
if(empty($arg) && isset($_GET['id'])) $arg = $_GET['id'];
|
||||
$image = Image::by_id(int_escape($arg));
|
||||
// FIXME: handle null image
|
||||
$image->get_tag_array(); // tag data isn't loaded into the object until necessary
|
||||
$safe_image = new _SafeImage($image);
|
||||
$page->set_data(json_encode($safe_image));
|
||||
}
|
||||
|
||||
if($event->page_matches("api/shimmie/find_images")) {
|
||||
elseif($event->page_matches("api/shimmie/find_images")) {
|
||||
$search_terms = $event->get_search_terms();
|
||||
$page_number = $event->get_page_number();
|
||||
$page_size = $event->get_page_size();
|
||||
@ -106,7 +84,7 @@ class ShimmieApi extends Extension {
|
||||
$page->set_data(json_encode($safe_images));
|
||||
}
|
||||
|
||||
if($event->page_matches("api/shimmie/get_user")) {
|
||||
elseif($event->page_matches("api/shimmie/get_user")) {
|
||||
$query = $user->id;
|
||||
$type = "id";
|
||||
if($event->count_args() == 1) {
|
||||
@ -121,41 +99,77 @@ class ShimmieApi extends Extension {
|
||||
$type = "name";
|
||||
}
|
||||
|
||||
$all = $database->get_row(
|
||||
"SELECT id,name,joindate,class FROM users WHERE $type=?",
|
||||
array($query));
|
||||
|
||||
if(!empty($all)){
|
||||
//FIXME?: For some weird reason, get_all seems to return twice. Unsetting second value to make things look nice..
|
||||
// - it returns data as eg array(0=>1234, 'id'=>1234, 1=>'bob', 'name'=>bob, ...);
|
||||
for($i=0; $i<4; $i++) unset($all[$i]);
|
||||
$all['uploadcount'] = Image::count_images(array("user_id=".$all['id']));
|
||||
$all['commentcount'] = $database->get_one(
|
||||
"SELECT COUNT(*) AS count FROM comments WHERE owner_id=:owner_id",
|
||||
array("owner_id"=>$all['id']));
|
||||
|
||||
if(isset($_GET['recent'])){
|
||||
$recent = $database->get_all(
|
||||
"SELECT * FROM images WHERE owner_id=? ORDER BY id DESC LIMIT 0, 5",
|
||||
array($all['id']));
|
||||
|
||||
$i = 0;
|
||||
foreach($recent as $all['recentposts'][$i]){
|
||||
unset($all['recentposts'][$i]['owner_id']); //We already know the owners id..
|
||||
unset($all['recentposts'][$i]['owner_ip']);
|
||||
|
||||
for($x=0; $x<14; $x++) unset($all['recentposts'][$i][$x]);
|
||||
if(empty($all['recentposts'][$i]['author'])) unset($all['recentposts'][$i]['author']);
|
||||
if($all['recentposts'][$i]['notes'] > 0) $all['recentposts'][$i]['has_notes'] = "Y";
|
||||
else $all['recentposts'][$i]['has_notes'] = "N";
|
||||
unset($all['recentposts'][$i]['notes']);
|
||||
$i += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
$all = $this->api_get_user($type, $query);
|
||||
$page->set_data(json_encode($all));
|
||||
}
|
||||
|
||||
else {
|
||||
$page->set_mode("redirect");
|
||||
$page->set_redirect(make_link("ext_doc/shimmie_api"));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $arg
|
||||
* @return array
|
||||
*/
|
||||
private function api_get_tags($arg) {
|
||||
global $database;
|
||||
if (!empty($arg)) {
|
||||
$all = $database->get_all("SELECT tag FROM tags WHERE tag LIKE ?", array($arg . "%"));
|
||||
} else {
|
||||
$all = $database->get_all("SELECT tag FROM tags");
|
||||
}
|
||||
$res = array();
|
||||
foreach ($all as $row) {
|
||||
$res[] = $row["tag"];
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $type
|
||||
* @param $query
|
||||
* @return array
|
||||
*/
|
||||
private function api_get_user($type, $query) {
|
||||
global $database;
|
||||
$all = $database->get_row(
|
||||
"SELECT id, name, joindate, class FROM users WHERE $type=?",
|
||||
array($query)
|
||||
);
|
||||
|
||||
if (!empty($all)) {
|
||||
//FIXME?: For some weird reason, get_all seems to return twice. Unsetting second value to make things look nice..
|
||||
// - it returns data as eg array(0=>1234, 'id'=>1234, 1=>'bob', 'name'=>bob, ...);
|
||||
for ($i = 0; $i < 4; $i++) unset($all[$i]);
|
||||
$all['uploadcount'] = Image::count_images(array("user_id=" . $all['id']));
|
||||
$all['commentcount'] = $database->get_one(
|
||||
"SELECT COUNT(*) AS count FROM comments WHERE owner_id=:owner_id",
|
||||
array("owner_id" => $all['id']));
|
||||
|
||||
if (isset($_GET['recent'])) {
|
||||
$recent = $database->get_all(
|
||||
"SELECT * FROM images WHERE owner_id=? ORDER BY id DESC LIMIT 0, 5",
|
||||
array($all['id']));
|
||||
|
||||
$i = 0;
|
||||
foreach ($recent as $all['recentposts'][$i]) {
|
||||
unset($all['recentposts'][$i]['owner_id']); //We already know the owners id..
|
||||
unset($all['recentposts'][$i]['owner_ip']);
|
||||
|
||||
for ($x = 0; $x < 14; $x++) unset($all['recentposts'][$i][$x]);
|
||||
if (empty($all['recentposts'][$i]['author'])) unset($all['recentposts'][$i]['author']);
|
||||
if ($all['recentposts'][$i]['notes'] > 0) $all['recentposts'][$i]['has_notes'] = "Y";
|
||||
else $all['recentposts'][$i]['has_notes'] = "N";
|
||||
unset($all['recentposts'][$i]['notes']);
|
||||
$i += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $all;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -80,6 +80,9 @@ class UserCreationException extends SCoreException {}
|
||||
class NullUserException extends SCoreException {}
|
||||
|
||||
class UserPage extends Extension {
|
||||
/** @var UserPageTheme $theme */
|
||||
var $theme;
|
||||
|
||||
public function onInitExt(InitExtEvent $event) {
|
||||
global $config;
|
||||
$config->set_default_bool("login_signup_enabled", true);
|
||||
@ -94,64 +97,22 @@ class UserPage extends Extension {
|
||||
public function onPageRequest(PageRequestEvent $event) {
|
||||
global $config, $page, $user;
|
||||
|
||||
// user info is shown on all pages
|
||||
if($user->is_anonymous()) {
|
||||
$this->theme->display_login_block($page);
|
||||
}
|
||||
else {
|
||||
$ubbe = new UserBlockBuildingEvent();
|
||||
send_event($ubbe);
|
||||
ksort($ubbe->parts);
|
||||
$this->theme->display_user_block($page, $user, $ubbe->parts);
|
||||
}
|
||||
$this->show_user_info();
|
||||
|
||||
if($event->page_matches("user_admin")) {
|
||||
if($event->get_arg(0) == "login") {
|
||||
if(isset($_POST['user']) && isset($_POST['pass'])) {
|
||||
$this->login($page);
|
||||
$this->page_login($_POST['user'], $_POST['pass']);
|
||||
}
|
||||
else {
|
||||
$this->theme->display_login_page($page);
|
||||
}
|
||||
}
|
||||
else if($event->get_arg(0) == "recover") {
|
||||
$user = User::by_name($_POST['username']);
|
||||
if(is_null($user)) {
|
||||
$this->theme->display_error(404, "Error", "There's no user with that name");
|
||||
}
|
||||
else if(is_null($user->email)) {
|
||||
$this->theme->display_error(400, "Error", "That user has no registered email address");
|
||||
}
|
||||
else {
|
||||
// send email
|
||||
}
|
||||
$this->page_recover($_POST['username']);
|
||||
}
|
||||
else if($event->get_arg(0) == "create") {
|
||||
if(!$config->get_bool("login_signup_enabled")) {
|
||||
$this->theme->display_signups_disabled($page);
|
||||
}
|
||||
else if(!isset($_POST['name'])) {
|
||||
$this->theme->display_signup_page($page);
|
||||
}
|
||||
else if($_POST['pass1'] != $_POST['pass2']) {
|
||||
$this->theme->display_error(400, "Password Mismatch", "Passwords don't match");
|
||||
}
|
||||
else {
|
||||
try {
|
||||
if(!captcha_check()) {
|
||||
throw new UserCreationException("Error in captcha");
|
||||
}
|
||||
|
||||
$uce = new UserCreationEvent($_POST['name'], $_POST['pass1'], $_POST['email']);
|
||||
send_event($uce);
|
||||
$this->set_login_cookie($uce->username, $uce->password);
|
||||
$page->set_mode("redirect");
|
||||
$page->set_redirect(make_link("user"));
|
||||
}
|
||||
catch(UserCreationException $ex) {
|
||||
$this->theme->display_error(400, "User Creation Error", $ex->getMessage());
|
||||
}
|
||||
}
|
||||
$this->page_create();
|
||||
}
|
||||
else if($event->get_arg(0) == "list") {
|
||||
// select users.id,name,joindate,admin,
|
||||
@ -165,24 +126,7 @@ class UserPage extends Extension {
|
||||
$this->theme->display_user_list($page, User::by_list(0), $user);
|
||||
}
|
||||
else if($event->get_arg(0) == "logout") {
|
||||
$page->add_cookie("session", "", time()+60*60*24*$config->get_int('login_memory'), "/");
|
||||
if(CACHE_HTTP || SPEED_HAX) {
|
||||
# to keep as few versions of content as possible,
|
||||
# make cookies all-or-nothing
|
||||
$page->add_cookie("user", "", time()+60*60*24*$config->get_int('login_memory'), "/");
|
||||
}
|
||||
log_info("user", "Logged out");
|
||||
$page->set_mode("redirect");
|
||||
|
||||
// Try forwarding to same page on logout unless user comes from registration page
|
||||
if ($config->get_int("user_loginshowprofile",0) == 0 &&
|
||||
isset($_SERVER['HTTP_REFERER']) &&
|
||||
strstr($_SERVER['HTTP_REFERER'], "post/"))
|
||||
{
|
||||
$page->set_redirect ($_SERVER['HTTP_REFERER']);
|
||||
} else {
|
||||
$page->set_redirect(make_link());
|
||||
}
|
||||
$this->page_logout();
|
||||
}
|
||||
|
||||
if(!$user->check_auth_token()) {
|
||||
@ -388,16 +332,24 @@ class UserPage extends Extension {
|
||||
$event->add_querylet(new Querylet("images.owner_ip = '$user_ip'"));
|
||||
}
|
||||
}
|
||||
|
||||
private function show_user_info() {
|
||||
global $user, $page;
|
||||
// user info is shown on all pages
|
||||
if ($user->is_anonymous()) {
|
||||
$this->theme->display_login_block($page);
|
||||
} else {
|
||||
$ubbe = new UserBlockBuildingEvent();
|
||||
send_event($ubbe);
|
||||
ksort($ubbe->parts);
|
||||
$this->theme->display_user_block($page, $user, $ubbe->parts);
|
||||
}
|
||||
}
|
||||
// }}}
|
||||
// Things done *with* the user {{{
|
||||
/**
|
||||
* @param Page $page
|
||||
*/
|
||||
private function login(Page $page) {
|
||||
global $config, $user;
|
||||
private function page_login($name, $pass) {
|
||||
global $config, $user, $page;
|
||||
|
||||
$name = $_POST['user'];
|
||||
$pass = $_POST['pass'];
|
||||
|
||||
if(empty($name) || empty($pass)) {
|
||||
$this->theme->display_error(400, "Error", "Username or password left blank");
|
||||
@ -427,12 +379,72 @@ class UserPage extends Extension {
|
||||
}
|
||||
}
|
||||
|
||||
private function page_logout() {
|
||||
global $page, $config;
|
||||
$page->add_cookie("session", "", time() + 60 * 60 * 24 * $config->get_int('login_memory'), "/");
|
||||
if (CACHE_HTTP || SPEED_HAX) {
|
||||
# to keep as few versions of content as possible,
|
||||
# make cookies all-or-nothing
|
||||
$page->add_cookie("user", "", time() + 60 * 60 * 24 * $config->get_int('login_memory'), "/");
|
||||
}
|
||||
log_info("user", "Logged out");
|
||||
$page->set_mode("redirect");
|
||||
|
||||
// Try forwarding to same page on logout unless user comes from registration page
|
||||
if ($config->get_int("user_loginshowprofile", 0) == 0 &&
|
||||
isset($_SERVER['HTTP_REFERER']) &&
|
||||
strstr($_SERVER['HTTP_REFERER'], "post/")
|
||||
) {
|
||||
$page->set_redirect($_SERVER['HTTP_REFERER']);
|
||||
} else {
|
||||
$page->set_redirect(make_link());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $username
|
||||
*/
|
||||
private function page_recover($username) {
|
||||
$user = User::by_name($username);
|
||||
if (is_null($user)) {
|
||||
$this->theme->display_error(404, "Error", "There's no user with that name");
|
||||
} else if (is_null($user->email)) {
|
||||
$this->theme->display_error(400, "Error", "That user has no registered email address");
|
||||
} else {
|
||||
// send email
|
||||
}
|
||||
}
|
||||
|
||||
private function page_create() {
|
||||
global $config, $page;
|
||||
if (!$config->get_bool("login_signup_enabled")) {
|
||||
$this->theme->display_signups_disabled($page);
|
||||
} else if (!isset($_POST['name'])) {
|
||||
$this->theme->display_signup_page($page);
|
||||
} else if ($_POST['pass1'] != $_POST['pass2']) {
|
||||
$this->theme->display_error(400, "Password Mismatch", "Passwords don't match");
|
||||
} else {
|
||||
try {
|
||||
if (!captcha_check()) {
|
||||
throw new UserCreationException("Error in captcha");
|
||||
}
|
||||
|
||||
$uce = new UserCreationEvent($_POST['name'], $_POST['pass1'], $_POST['email']);
|
||||
send_event($uce);
|
||||
$this->set_login_cookie($uce->username, $uce->password);
|
||||
$page->set_mode("redirect");
|
||||
$page->set_redirect(make_link("user"));
|
||||
} catch (UserCreationException $ex) {
|
||||
$this->theme->display_error(400, "User Creation Error", $ex->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param UserCreationEvent $event
|
||||
* @throws UserCreationException
|
||||
*/
|
||||
private function check_user_creation(UserCreationEvent $event)
|
||||
{
|
||||
private function check_user_creation(UserCreationEvent $event) {
|
||||
$name = $event->username;
|
||||
//$pass = $event->password;
|
||||
//$email = $event->email;
|
||||
@ -450,8 +462,7 @@ class UserPage extends Extension {
|
||||
}
|
||||
}
|
||||
|
||||
private function create_user(UserCreationEvent $event)
|
||||
{
|
||||
private function create_user(UserCreationEvent $event) {
|
||||
global $database, $user;
|
||||
|
||||
$email = (!empty($event->email)) ? $event->email : null;
|
||||
|
Loading…
x
Reference in New Issue
Block a user