Ejemplo n.º 1
0
 /**
  * Mass set configuration options: Receives an associative array,
  * treats array keys as configuration option names and associated
  * array values as their configuration option values.
  *
  * @param array $map        Map from configuration names to values
  *
  * @return null
  */
 public function set_array(array $map)
 {
     $this->db->sql_transaction('begin');
     foreach ($map as $key => $value) {
         $sql = 'UPDATE ' . $this->table . "\n\t\t\t\tSET config_value = '" . $this->db->sql_escape($value) . "'\n\t\t\t\tWHERE config_name = '" . $this->db->sql_escape($key) . "'";
         $result = $this->db->sql_query($sql);
         if (!$this->db->sql_affectedrows($result)) {
             $sql = 'INSERT INTO ' . $this->table . ' ' . $this->db->sql_build_array('INSERT', array('config_name' => (string) $key, 'config_value' => (string) $value));
             $this->db->sql_query($sql);
         }
     }
     $this->db->sql_transaction('commit');
 }
Ejemplo n.º 2
0
    /**
     * Get the notification type id from the name
     *
     * @param string $notification_type_name The name
     * @return int the notification_type_id
     * @throws \src\notification\exception
     */
    public function get_notification_type_id($notification_type_name)
    {
        $notification_type_ids = $this->cache->get('notification_type_ids');
        if ($notification_type_ids === false) {
            $notification_type_ids = array();
            $sql = 'SELECT notification_type_id, notification_type_name
				FROM ' . $this->notification_types_table;
            $result = $this->db->sql_query($sql);
            while ($row = $this->db->sql_fetchrow($result)) {
                $notification_type_ids[$row['notification_type_name']] = (int) $row['notification_type_id'];
            }
            $this->db->sql_freeresult($result);
            $this->cache->put('notification_type_ids', $notification_type_ids);
        }
        if (!isset($notification_type_ids[$notification_type_name])) {
            if (!isset($this->notification_types[$notification_type_name]) && !isset($this->notification_types['notification.type.' . $notification_type_name])) {
                throw new \src\notification\exception($this->user->lang('NOTIFICATION_TYPE_NOT_EXIST', $notification_type_name));
            }
            $sql = 'INSERT INTO ' . $this->notification_types_table . ' ' . $this->db->sql_build_array('INSERT', array('notification_type_name' => $notification_type_name, 'notification_type_enabled' => 1));
            $this->db->sql_query($sql);
            $notification_type_ids[$notification_type_name] = (int) $this->db->sql_nextid();
            $this->cache->put('notification_type_ids', $notification_type_ids);
        }
        return $notification_type_ids[$notification_type_name];
    }
Ejemplo n.º 3
0
    /**
     * {@inheritdoc}
     */
    public function get_auth_link_data($user_id = 0)
    {
        $block_vars = array();
        // Get all external accounts tied to the current user
        $data = array('user_id' => $user_id <= 0 ? (int) $this->user->data['user_id'] : (int) $user_id);
        $sql = 'SELECT oauth_provider_id, provider FROM ' . $this->auth_provider_oauth_token_account_assoc . '
			WHERE ' . $this->db->sql_build_array('SELECT', $data);
        $result = $this->db->sql_query($sql);
        $rows = $this->db->sql_fetchrowset($result);
        $this->db->sql_freeresult($result);
        $oauth_user_ids = array();
        if ($rows !== false && sizeof($rows)) {
            foreach ($rows as $row) {
                $oauth_user_ids[$row['provider']] = $row['oauth_provider_id'];
            }
        }
        unset($rows);
        foreach ($this->service_providers as $service_name => $service_provider) {
            // Only include data if the credentials are set
            $credentials = $service_provider->get_service_credentials();
            if ($credentials['key'] && $credentials['secret']) {
                $actual_name = str_replace('auth.provider.oauth.service.', '', $service_name);
                $block_vars[$service_name] = array('HIDDEN_FIELDS' => array('link' => !isset($oauth_user_ids[$actual_name]), 'oauth_service' => $actual_name), 'SERVICE_NAME' => $this->user->lang['AUTH_PROVIDER_OAUTH_SERVICE_' . strtoupper($actual_name)], 'UNIQUE_ID' => isset($oauth_user_ids[$actual_name]) ? $oauth_user_ids[$actual_name] : null);
            }
        }
        return array('BLOCK_VAR_NAME' => 'oauth', 'BLOCK_VARS' => $block_vars, 'TEMPLATE_FILE' => 'ucp_auth_link_oauth.html');
    }
Ejemplo n.º 4
0
    /**
     * A helper function that performs the query for retrieving an access token
     *
     * @param	array	$data
     * @return	mixed
     */
    protected function get_access_token_row($data)
    {
        $sql = 'SELECT oauth_token FROM ' . $this->auth_provider_oauth_table . '
			WHERE ' . $this->db->sql_build_array('SELECT', $data);
        $result = $this->db->sql_query($sql);
        $row = $this->db->sql_fetchrow($result);
        $this->db->sql_freeresult($result);
        return $row;
    }
Ejemplo n.º 5
0
 /**
  * Adds a new category
  *
  * @param	string	$category_name	Name of the category to be added
  * @return	bool		True if the category was added successfully
  */
 public function add_category_teampage($category_name)
 {
     if ($category_name === '') {
         return false;
     }
     $num_entries = $this->get_group_count();
     $sql_ary = array('group_id' => 0, 'teampage_position' => $num_entries + 1, 'teampage_parent' => 0, 'teampage_name' => truncate_string($category_name, 255, 255));
     $sql = 'INSERT INTO ' . TEAMPAGE_TABLE . ' ' . $this->db->sql_build_array('INSERT', $sql_ary);
     $this->db->sql_query($sql);
     $this->cache->destroy('sql', TEAMPAGE_TABLE);
     return true;
 }
Ejemplo n.º 6
0
 /**
  * Create sphinx table
  *
  * @return string|bool error string is returned incase of errors otherwise false
  */
 public function create_index($acp_module, $u_action)
 {
     if (!$this->index_created()) {
         $table_data = array('COLUMNS' => array('counter_id' => array('UINT', 0), 'max_doc_id' => array('UINT', 0)), 'PRIMARY_KEY' => 'counter_id');
         $this->db_tools->sql_create_table(SPHINX_TABLE, $table_data);
         $sql = 'TRUNCATE TABLE ' . SPHINX_TABLE;
         $this->db->sql_query($sql);
         $data = array('counter_id' => '1', 'max_doc_id' => '0');
         $sql = 'INSERT INTO ' . SPHINX_TABLE . ' ' . $this->db->sql_build_array('INSERT', $data);
         $this->db->sql_query($sql);
     }
     return false;
 }
Ejemplo n.º 7
0
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $sql = 'SELECT user_id, user_email, user_email_hash
			FROM ' . USERS_TABLE . '
			WHERE user_type <> ' . USER_IGNORE . "\n\t\t\t\tAND user_email <> ''";
        $result = $this->db->sql_query($sql);
        while ($row = $this->db->sql_fetchrow($result)) {
            $user_email_hash = src_email_hash($row['user_email']);
            if ($user_email_hash !== $row['user_email_hash']) {
                $sql_ary = array('user_email_hash' => $user_email_hash);
                $sql = 'UPDATE ' . USERS_TABLE . '
					SET ' . $this->db->sql_build_array('UPDATE', $sql_ary) . '
					WHERE user_id = ' . (int) $row['user_id'];
                $this->db->sql_query($sql);
                if ($output->getVerbosity() >= OutputInterface::VERBOSITY_DEBUG) {
                    $output->writeln(sprintf('user_id %d, email %s => %s', $row['user_id'], $row['user_email'], $user_email_hash));
                }
            }
        }
        $this->db->sql_freeresult($result);
        $output->writeln('<info>' . $this->user->lang('CLI_FIXUP_RECALCULATE_EMAIL_HASH_SUCCESS') . '</info>');
    }
Ejemplo n.º 8
0
    /**
     * Regenerate left/right ids from parent/child relationship
     *
     * This method regenerates the left/right ids for the tree based on
     * the parent/child relations. This function executes three queries per
     * item, so it should only be called, when the set has one of the following
     * problems:
     *	- The set has a duplicated value inside the left/right id chain
     *	- The set has a missing value inside the left/right id chain
     *	- The set has items that do not have a left/right id set
     *
     * When regenerating the items, the items are sorted by parent id and their
     * current left id, so the current child/parent relationships are kept
     * and running the function on a working set will not change the order.
     *
     * @param int	$new_id		First left_id to be used (should start with 1)
     * @param int	$parent_id	parent_id of the current set (default = 0)
     * @param bool	$reset_ids	Should we reset all left_id/right_id on the first call?
     * @return	int		$new_id		The next left_id/right_id that should be used
     */
    public function regenerate_left_right_ids($new_id, $parent_id = 0, $reset_ids = false)
    {
        if ($acquired_new_lock = $this->acquire_lock()) {
            $this->db->sql_transaction('begin');
            if (!$reset_ids) {
                $sql = 'UPDATE ' . $this->table_name . '
					SET ' . $this->column_item_parents . " = ''\n\t\t\t\t\t" . $this->get_sql_where('WHERE');
                $this->db->sql_query($sql);
            }
        }
        if ($reset_ids) {
            $sql = 'UPDATE ' . $this->table_name . '
				SET ' . $this->db->sql_build_array('UPDATE', array($this->column_left_id => 0, $this->column_right_id => 0, $this->column_item_parents => '')) . '
				' . $this->get_sql_where('WHERE');
            $this->db->sql_query($sql);
        }
        $sql = 'SELECT *
			FROM ' . $this->table_name . '
			WHERE ' . $this->column_parent_id . ' = ' . (int) $parent_id . '
				' . $this->get_sql_where('AND') . '
			ORDER BY ' . $this->column_left_id . ', ' . $this->column_item_id . ' ASC';
        $result = $this->db->sql_query($sql);
        while ($row = $this->db->sql_fetchrow($result)) {
            // First we update the left_id for this module
            if ($row[$this->column_left_id] != $new_id) {
                $sql = 'UPDATE ' . $this->table_name . '
					SET ' . $this->db->sql_build_array('UPDATE', array($this->column_left_id => $new_id)) . '
					WHERE ' . $this->column_item_id . ' = ' . (int) $row[$this->column_item_id];
                $this->db->sql_query($sql);
            }
            $new_id++;
            // Then we go through any children and update their left/right id's
            $new_id = $this->regenerate_left_right_ids($new_id, $row[$this->column_item_id]);
            // Then we come back and update the right_id for this module
            if ($row[$this->column_right_id] != $new_id) {
                $sql = 'UPDATE ' . $this->table_name . '
					SET ' . $this->db->sql_build_array('UPDATE', array($this->column_right_id => $new_id)) . '
					WHERE ' . $this->column_item_id . ' = ' . (int) $row[$this->column_item_id];
                $this->db->sql_query($sql);
            }
            $new_id++;
        }
        $this->db->sql_freeresult($result);
        if ($acquired_new_lock) {
            $this->db->sql_transaction('commit');
            $this->lock->release();
        }
        return $new_id;
    }
Ejemplo n.º 9
0
    /**
     * Update profile field data directly
     */
    public function update_profile_field_data($user_id, $cp_data)
    {
        if (!sizeof($cp_data)) {
            return;
        }
        $sql = 'UPDATE ' . $this->fields_data_table . '
			SET ' . $this->db->sql_build_array('UPDATE', $cp_data) . '
			WHERE user_id = ' . (int) $user_id;
        $this->db->sql_query($sql);
        if (!$this->db->sql_affectedrows()) {
            $cp_data = $this->build_insert_sql_array($cp_data);
            $cp_data['user_id'] = (int) $user_id;
            $sql = 'INSERT INTO ' . $this->fields_data_table . ' ' . $this->db->sql_build_array('INSERT', $cp_data);
            $this->db->sql_query($sql);
        }
    }
Ejemplo n.º 10
0
    /**
     * Insert/Update migration row into the database
     *
     * @param string $name Name of the migration
     * @param array $state
     * @return null
     */
    protected function set_migration_state($name, $state)
    {
        $migration_row = $state;
        $migration_row['migration_depends_on'] = serialize($state['migration_depends_on']);
        if (isset($this->migration_state[$name])) {
            $sql = 'UPDATE ' . $this->migrations_table . '
				SET ' . $this->db->sql_build_array('UPDATE', $migration_row) . "\n\t\t\t\tWHERE migration_name = '" . $this->db->sql_escape($name) . "'";
            $this->db->sql_query($sql);
        } else {
            $migration_row['migration_name'] = $name;
            $sql = 'INSERT INTO ' . $this->migrations_table . '
				' . $this->db->sql_build_array('INSERT', $migration_row);
            $this->db->sql_query($sql);
        }
        $this->migration_state[$name] = $state;
        $this->last_run_migration['state'] = $state;
    }
Ejemplo n.º 11
0
    /**
     * Add a new permission role
     *
     * @param string $role_name The new role name
     * @param string $role_type The type (u_, m_, a_)
     * @param string $role_description Description of the new role
     *
     * @return null
     */
    public function role_add($role_name, $role_type, $role_description = '')
    {
        $sql = 'SELECT role_id
			FROM ' . ACL_ROLES_TABLE . "\n\t\t\tWHERE role_name = '" . $this->db->sql_escape($role_name) . "'";
        $this->db->sql_query($sql);
        $role_id = (int) $this->db->sql_fetchfield('role_id');
        if ($role_id) {
            return;
        }
        $sql = 'SELECT MAX(role_order) AS max_role_order
			FROM ' . ACL_ROLES_TABLE . "\n\t\t\tWHERE role_type = '" . $this->db->sql_escape($role_type) . "'";
        $this->db->sql_query($sql);
        $role_order = (int) $this->db->sql_fetchfield('max_role_order');
        $role_order = !$role_order ? 1 : $role_order + 1;
        $sql_ary = array('role_name' => $role_name, 'role_description' => $role_description, 'role_type' => $role_type, 'role_order' => $role_order);
        $sql = 'INSERT INTO ' . ACL_ROLES_TABLE . ' ' . $this->db->sql_build_array('INSERT', $sql_ary);
        $this->db->sql_query($sql);
    }
Ejemplo n.º 12
0
    /**
     * Install style
     *
     * @param array $style style data
     * @return int Style id
     */
    protected function install_style($style)
    {
        // Generate row
        $sql_ary = array();
        foreach ($style as $key => $value) {
            if ($key != 'style_id' && substr($key, 0, 1) != '_') {
                $sql_ary[$key] = $value;
            }
        }
        // Add to database
        $this->db->sql_transaction('begin');
        $sql = 'INSERT INTO ' . STYLES_TABLE . '
			' . $this->db->sql_build_array('INSERT', $sql_ary);
        $this->db->sql_query($sql);
        $id = $this->db->sql_nextid();
        $this->db->sql_transaction('commit');
        add_log('admin', 'LOG_STYLE_ADD', $sql_ary['style_name']);
        return $id;
    }
Ejemplo n.º 13
0
 /**
  * Sets a configuration option's value only if the old_value matches the
  * current configuration value or the configuration value does not exist yet.
  *
  * @param  string $key       The configuration option's name
  * @param  mixed  $old_value Current configuration value or false to ignore
  *                           the old value
  * @param  string $new_value New configuration value
  * @param  bool   $use_cache Whether this variable should be cached or if it
  *                           changes too frequently to be efficiently cached
  * @return bool              True if the value was changed, false otherwise
  */
 public function set_atomic($key, $old_value, $new_value, $use_cache = true)
 {
     $sql = 'UPDATE ' . $this->table . "\n\t\t\tSET config_value = '" . $this->db->sql_escape($new_value) . "'\n\t\t\tWHERE config_name = '" . $this->db->sql_escape($key) . "'";
     if ($old_value !== false) {
         $sql .= " AND config_value = '" . $this->db->sql_escape($old_value) . "'";
     }
     $this->db->sql_query($sql);
     if (!$this->db->sql_affectedrows() && isset($this->config[$key])) {
         return false;
     }
     if (!isset($this->config[$key])) {
         $sql = 'INSERT INTO ' . $this->table . ' ' . $this->db->sql_build_array('INSERT', array('config_name' => $key, 'config_value' => $new_value, 'is_dynamic' => $use_cache ? 0 : 1));
         $this->db->sql_query($sql);
     }
     if ($use_cache) {
         $this->cache->destroy('config');
     }
     $this->config[$key] = $new_value;
     return true;
 }
Ejemplo n.º 14
0
    /**
     * Set topic visibility
     *
     * Allows approving (which is akin to undeleting/restore) or soft deleting an entire topic.
     * Calls set_post_visibility as needed.
     *
     * Note: By default, when a soft deleted topic is restored. Only posts that
     *		were approved at the time of soft deleting, are being restored.
     *		Same applies to soft deleting. Only approved posts will be marked
     *		as soft deleted.
     *		If you want to update all posts, use the force option.
     *
     * @param $visibility	int		Element of {ITEM_APPROVED, ITEM_DELETED, ITEM_REAPPROVE}
     * @param $topic_id		mixed	Topic ID to act on
     * @param $forum_id		int		Forum where $topic_id is found
     * @param $user_id		int		User performing the action
     * @param $time			int		Timestamp when the action is performed
     * @param $reason		string	Reason why the visibilty was changed.
     * @param $force_update_all	bool	Force to update all posts within the topic
     * @return array		Changed topic data, empty array if an error occured.
     */
    public function set_topic_visibility($visibility, $topic_id, $forum_id, $user_id, $time, $reason, $force_update_all = false)
    {
        if (!in_array($visibility, array(ITEM_APPROVED, ITEM_DELETED, ITEM_REAPPROVE))) {
            return array();
        }
        if (!$force_update_all) {
            $sql = 'SELECT topic_visibility, topic_delete_time
				FROM ' . $this->topics_table . '
				WHERE topic_id = ' . (int) $topic_id;
            $result = $this->db->sql_query($sql);
            $original_topic_data = $this->db->sql_fetchrow($result);
            $this->db->sql_freeresult($result);
            if (!$original_topic_data) {
                // The topic does not exist...
                return array();
            }
        }
        // Note, we do not set a reason for the posts, just for the topic
        $data = array('topic_visibility' => (int) $visibility, 'topic_delete_user' => (int) $user_id, 'topic_delete_time' => (int) $time ?: time(), 'topic_delete_reason' => truncate_string($reason, 255, 255, false));
        $sql = 'UPDATE ' . $this->topics_table . '
			SET ' . $this->db->sql_build_array('UPDATE', $data) . '
			WHERE topic_id = ' . (int) $topic_id;
        $this->db->sql_query($sql);
        if (!$this->db->sql_affectedrows()) {
            return array();
        }
        if (!$force_update_all && $original_topic_data['topic_delete_time'] && $original_topic_data['topic_visibility'] == ITEM_DELETED && $visibility == ITEM_APPROVED) {
            // If we're restoring a topic we only restore posts, that were soft deleted through the topic soft deletion.
            $this->set_post_visibility($visibility, false, $topic_id, $forum_id, $user_id, $time, '', true, true, $original_topic_data['topic_visibility'], $original_topic_data['topic_delete_time']);
        } else {
            if (!$force_update_all && $original_topic_data['topic_visibility'] == ITEM_APPROVED && $visibility == ITEM_DELETED) {
                // If we're soft deleting a topic we only mark approved posts as soft deleted.
                $this->set_post_visibility($visibility, false, $topic_id, $forum_id, $user_id, $time, '', true, true, $original_topic_data['topic_visibility']);
            } else {
                $this->set_post_visibility($visibility, false, $topic_id, $forum_id, $user_id, $time, '', true, true);
            }
        }
        return $data;
    }
Ejemplo n.º 15
0
    /**
     * Handle deleting avatars
     *
     * @param \src\db\driver\driver_interface $db src dbal
     * @param \src\user    $user src user object
     * @param array          $avatar_data Cleaned user data containing the user's
     *                               avatar data
     * @param string         $table Database table from which the avatar should be deleted
     * @param string         $prefix Prefix of user data columns in database
     * @return null
     */
    public function handle_avatar_delete(\src\db\driver\driver_interface $db, \src\user $user, $avatar_data, $table, $prefix)
    {
        if ($driver = $this->get_driver($avatar_data['avatar_type'])) {
            $driver->delete($avatar_data);
        }
        $result = $this->prefix_avatar_columns($prefix, self::$default_row);
        $sql = 'UPDATE ' . $table . '
			SET ' . $db->sql_build_array('UPDATE', $result) . '
			WHERE ' . $prefix . 'id = ' . (int) $avatar_data['id'];
        $db->sql_query($sql);
        // Make sure we also delete this avatar from the users
        if ($prefix === 'group_') {
            $result = $this->prefix_avatar_columns('user_', self::$default_row);
            $sql = 'UPDATE ' . USERS_TABLE . '
				SET ' . $db->sql_build_array('UPDATE', $result) . "\n\t\t\t\tWHERE user_avatar = '" . $db->sql_escape($avatar_data['avatar']) . "'";
            $db->sql_query($sql);
        }
    }