public function action_create() { if (is_post()) { $parameters = get_previous_parameters(); $object = new CommentObj(); $object = $object->fromRequest(); $object['foreign_id'] = empty($object['foreign_id']) ? reset($parameters) : $object['foreign_id']; $object['foreign_table'] = empty($object['foreign_table']) ? table_name(get_previous_area()) : $object['foreign_table']; //If we don't have a logged in user, create a dummy account if (!BackendUser::check()) { $query = new SelectQuery('BackendUser'); $query->filter('`email` = :email'); if ($old_user = Controller::getVar('user')) { $existing_user = $query->fetchAssoc(array(':email' => $old_user['email'])); } switch (true) { case $existing_user && $existing_user['confirmed'] && $existing_user['active']: //Attribute quote to user? Seems risque, actually, if I know a user's email address, I can just attribute to him. Auth first Backend::addError('Comment not added. Please login first'); return false; break; case $existing_user && !$existing_user['confirmed'] && $existing_user['active']: //Unregistered user commented before $object['user_id'] = $existing_user['id']; break; default: case !$existing_user: $user_data = array('name' => $old_user['name'], 'surname' => '', 'email' => $old_user['email'], 'website' => $old_user['website'], 'username' => $old_user['email'], 'password' => get_random(), 'confirmed' => 0, 'active' => 1); $user = self::getObject('BackendUser'); if ($user->create($user_data)) { $object['user_id'] = $user->array['id']; $url = SITE_LINK . '/?q=backend_user/confirm/' . $user->array['salt']; $app_name = ConfigValue::get('Title'); $message = <<<END Hi {$user->array['name']}! Thank you for your comment on {$app_name}. An account has automatically been created for you. To activate it, please click on the following link: {$url} Please note that you don't need to do this for your comments to show, but this account will be deleted if it isn't confirmed in a weeks time. Regards END; send_email($user->array['email'], 'Thank you for your comment.', $message); } else { Backend::addError('Could not create user to add Comment'); return false; } break; } } $object = array_filter($object, create_function('$var', 'return !is_null($var);')); Controller::setVar('obj', $object); } return parent::action_create(); }
/** * Check if a tag exists for a specified Area, add it if it doesn't */ public static function check($name, $area) { if ($area instanceof DBObject) { $area = $area->getMeta('table'); } //Check if tag exists $query = new SelectQuery('Tag'); $query->filter('`foreign_table` = :table')->filter('`name` = :tag'); if ($tag = $query->fetchAssoc(array(':tag' => $name, ':table' => $area))) { return $tag; } //Tag doesn't already exist $data = array('name' => $name, 'foreign_table' => $area, 'active' => 1); $tag = new TagObj(); if ($tag->create($data)) { return $tag->array; } return false; }
/** * We check if there's any content of the name ?q=:name */ public static function hook_init() { $query = Controller::getVar('q'); if (empty($query)) { return; } if (substr($query, -1) == '/') { $query = substr($query, 0, strlen($query) - 1); } $select = new SelectQuery('Content'); $select->filter('`name` = :query'); $row = $select->fetchAssoc(array(':query' => $query)); if ($row) { Controller::setVar('q', 'content/' . $row['id']); } }
public static function userStats() { $msg = array(); $query = new SelectQuery('BackendUser'); $query->field('COUNT(*) AS `Total`, SUM(IF(TO_DAYS(NOW()) - TO_DAYS(`added`) < 7, 1, 0)) AS `New`')->filter('`active` = 1')->filter('`confirmed` = 1'); if ($stats = $query->fetchAssoc()) { $msg[] = 'There are a total of ' . $stats['Total'] . ' **active** users, of which ' . $stats['New'] . ' signed up in the last 7 days'; } $query = new SelectQuery('BackendUser'); $query->field('COUNT(*) AS `Total`, SUM(IF(TO_DAYS(NOW()) - TO_DAYS(`added`) < 7, 1, 0)) AS `New`')->filter('`active` = 1')->filter('`confirmed` = 1'); if ($stats = $query->fetchAssoc()) { $msg[] = 'There are a total of ' . $stats['Total'] . ' **unconfirmed** users, of which ' . $stats['New'] . ' signed up in the last 7 days'; } $msg = implode(PHP_EOL . PHP_EOL, $msg); send_email(ConfigValue::get('author.Email', ConfigValue::get('application.Email', 'info@' . SITE_DOMAIN)), 'User stats for ' . Backend::get('Title'), $msg); return true; }