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
    /**
     * {@inheritDoc}
     */
    public function move($group_id, $delta)
    {
        $delta = (int) $delta;
        if (!$delta) {
            return false;
        }
        $move_up = $delta > 0 ? true : false;
        $current_value = $this->get_group_value($group_id);
        if ($current_value != self::GROUP_DISABLED) {
            $this->db->sql_transaction('begin');
            // First we move all groups between our current value and the target value up/down 1,
            // so we have a gap for our group to move.
            $sql = 'UPDATE ' . GROUPS_TABLE . '
				SET group_legend = group_legend' . ($move_up ? ' + 1' : ' - 1') . '
				WHERE group_legend > ' . self::GROUP_DISABLED . '
					AND group_legend' . ($move_up ? ' >= ' : ' <= ') . ($current_value - $delta) . '
					AND group_legend' . ($move_up ? ' < ' : ' > ') . $current_value;
            $this->db->sql_query($sql);
            // Because there might be fewer groups above/below the group than we wanted to move,
            // we use the number of changed groups, to update the group.
            $delta = (int) $this->db->sql_affectedrows();
            if ($delta) {
                // And now finally, when we moved some other groups and built a gap,
                // we can move the desired group to it.
                $sql = 'UPDATE ' . GROUPS_TABLE . '
					SET group_legend = group_legend ' . ($move_up ? ' - ' : ' + ') . $delta . '
					WHERE group_id = ' . (int) $group_id;
                $this->db->sql_query($sql);
                $this->db->sql_transaction('commit');
                return true;
            }
            $this->db->sql_transaction('commit');
        }
        return false;
    }
Ejemplo n.º 3
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.º 4
0
    /**
     * Add an item which already has a database row at the end of the tree
     *
     * @param int	$item_id	The item to be added
     * @return array		Array with updated data, if the item was added successfully
     *					Empty array otherwise
     */
    protected function add_item_to_nestedset($item_id)
    {
        $sql = 'SELECT MAX(' . $this->column_right_id . ') AS ' . $this->column_right_id . '
			FROM ' . $this->table_name . '
			' . $this->get_sql_where('WHERE');
        $result = $this->db->sql_query($sql);
        $current_max_right_id = (int) $this->db->sql_fetchfield($this->column_right_id);
        $this->db->sql_freeresult($result);
        $update_item_data = array($this->column_parent_id => 0, $this->column_left_id => $current_max_right_id + 1, $this->column_right_id => $current_max_right_id + 2, $this->column_item_parents => '');
        $sql = 'UPDATE ' . $this->table_name . '
			SET ' . $this->db->sql_build_array('UPDATE', $update_item_data) . '
			WHERE ' . $this->column_item_id . ' = ' . (int) $item_id . '
				AND ' . $this->column_parent_id . ' = 0
				AND ' . $this->column_left_id . ' = 0
				AND ' . $this->column_right_id . ' = 0';
        $this->db->sql_query($sql);
        return $this->db->sql_affectedrows() == 1 ? $update_item_data : array();
    }
Ejemplo n.º 5
0
    /**
     * Deletes an item from the list and closes the gap in the position list.
     *
     * @param	int		$teampage_id	teampage_id of the item to be deleted
     * @param	bool	$skip_group		Skip setting the group to GROUP_DISABLED, to save the query, when you need to update it anyway.
     * @return	bool		True if the item was deleted successfully
     */
    public function delete_teampage($teampage_id, $skip_group = false)
    {
        $current_value = $this->get_teampage_value($teampage_id);
        if ($current_value != self::GROUP_DISABLED) {
            $sql = 'DELETE FROM ' . TEAMPAGE_TABLE . '
				WHERE teampage_id = ' . $teampage_id . '
					OR teampage_parent = ' . $teampage_id;
            $this->db->sql_query($sql);
            $delta = (int) $this->db->sql_affectedrows();
            $sql = 'UPDATE ' . TEAMPAGE_TABLE . '
				SET teampage_position = teampage_position - ' . $delta . '
				WHERE teampage_position > ' . $current_value;
            $this->db->sql_query($sql);
            $this->cache->destroy('sql', TEAMPAGE_TABLE);
            return true;
        }
        $this->cache->destroy('sql', TEAMPAGE_TABLE);
        return false;
    }
Ejemplo n.º 6
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.º 7
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.º 8
0
    /**
     * Delete a subscription
     *
     * @param string $item_type Type identifier of the subscription
     * @param int $item_id The id of the item
     * @param string $method The method of the notification e.g. '', 'email', or 'jabber'
     * @param bool|int $user_id The user_id to add the subscription for (bool false for current user)
     */
    public function delete_subscription($item_type, $item_id = 0, $method = '', $user_id = false)
    {
        $user_id = $user_id === false ? $this->user->data['user_id'] : $user_id;
        // If no method, make sure that no other notification methods for this item are selected before deleting
        if ($method === '') {
            $sql = 'SELECT COUNT(*) as num_notifications
				FROM ' . $this->user_notifications_table . "\n\t\t\t\tWHERE item_type = '" . $this->db->sql_escape($item_type) . "'\n\t\t\t\t\tAND item_id = " . (int) $item_id . '
					AND user_id = ' . (int) $user_id . "\n\t\t\t\t\tAND method <> ''\n\t\t\t\t\tAND notify = 1";
            $this->db->sql_query($sql);
            $num_notifications = $this->db->sql_fetchfield('num_notifications');
            $this->db->sql_freeresult();
            if ($num_notifications) {
                return;
            }
        }
        $sql = 'UPDATE ' . $this->user_notifications_table . "\n\t\t\tSET notify = 0\n\t\t\tWHERE item_type = '" . $this->db->sql_escape($item_type) . "'\n\t\t\t\tAND item_id = " . (int) $item_id . '
				AND user_id = ' . (int) $user_id . "\n\t\t\t\tAND method = '" . $this->db->sql_escape($method) . "'";
        $this->db->sql_query($sql);
        if (!$this->db->sql_affectedrows()) {
            $sql = 'INSERT INTO ' . $this->user_notifications_table . ' ' . $this->db->sql_build_array('INSERT', array('item_type' => $item_type, 'item_id' => (int) $item_id, 'user_id' => (int) $user_id, 'method' => $method, 'notify' => 0));
            $this->db->sql_query($sql);
        }
    }