CachedTemplate::invalidate_cache($cache_id);
     $location = "{$base_url}/content.php?cid={$cid}";
     header("location:{$location}");
     exit;
 }
 //.. end of edit
 // If we have come this far it means it is not edit and we have to create post
 //save post normally
 if ($_POST['route_to_pa_home'] == 1) {
     $display_on_homepage = DISPLAY_ON_HOMEPAGE;
     //its zero
 } else {
     $display_on_homepage = NO_DISPLAY_ON_HOMEPAGE;
     //This will not show up on homepage - flag has opposite values
 }
 $post_saved = BlogPost::save_blogpost(0, $login_uid, $_POST["blog_title"], $_POST["description"], NULL, $terms, -1, 1, $display_on_homepage);
 $permalink_cid = $post_saved['cid'];
 if ($extra['network_content_moderation'] == NET_YES && $network_info->owner_id != $user->user_id) {
     Network::moderate_network_content(-1, $permalink_cid);
     // -1 for contents; not a part of any collection
     $error_msg = "&err=" . urlencode(MessagesHandler::get_message(1004));
 }
 $content_author_image = uihelper_resize_mk_user_img($user->picture, 80, 80, 'alt="' . $user->first_name . '" align="left" style="padding: 0px 12px 12px 0px;"');
 $network_owner_name = User::map_ids_to_logins($network_info->owner_id);
 $params['cid'] = $permalink_cid;
 $params['first_name'] = $user->first_name;
 $params['user_id'] = $user->user_id;
 $params['user_image'] = $content_author_image;
 $params['content_title'] = $_POST["blog_title"];
 $params['network_name'] = $network_info->name;
 $params['content_url'] = PA::$url . '/' . FILE_CONTENT . '?cid=' . $permalink_cid;
         foreach ($tags as $term) {
             $tr = trim($term);
             if ($tr) {
                 $terms[] = $tr;
             }
         }
     }
     try {
         $post_subject = "Network's owner bulletin - " . $_POST['title'];
         $post_message = $_POST['bulletin_body'];
         switch ($type) {
             case 'Suggestion':
                 $res = Suggestion::save_suggestion(0, $from, $post_subject, $post_message, '', $terms, 0, $is_active = ACTIVE, $user->email);
                 break;
             case 'BlogPost':
                 $res = BlogPost::save_blogpost(0, $from, $post_subject, $post_message, '', $terms, 0, $is_active = ACTIVE, $user->email);
                 break;
         }
     } catch (PAException $e) {
         $error_msg .= $e->message;
     }
     if (!empty($res['cid'])) {
         $content_obj = Content::load_content((int) $res['cid']);
         PANotify::send("content_posted_to_comm_blog", PA::$network_info, $user, $content_obj);
     }
 }
 if ($no_reg_user == TRUE) {
     $error_msg .= "No registered member in this network";
 } else {
     $error_msg .= " Bulletin has been sent ";
 }
function route2groups()
{
    global $user, $is_edit;
    $extra = unserialize(PA::$network_info->extra);
    $tags = preg_split('/\\s*,\\s*/', strtolower($_POST['tags']));
    $tags = array_unique($tags);
    $net_owner = new User();
    $net_owner->load((int) PA::$network_info->owner_id);
    $valid_post_types = array('BlogPost', 'Contribution', 'Suggestion');
    $type = isset($_POST) && isset($_POST['blog_type']) && in_array($_POST['blog_type'], $valid_post_types) ? $_POST['blog_type'] : 'BlogPost';
    //find tag entry
    $terms = array();
    foreach ($tags as $term) {
        $tr = trim($term);
        if ($tr) {
            $terms[] = $tr;
        }
    }
    if (!empty($_POST['route_to_pa_home']) && $_POST['route_to_pa_home'] == 1) {
        $display_on_homepage = DISPLAY_ON_HOMEPAGE;
        //its zero
    } else {
        $display_on_homepage = NO_DISPLAY_ON_HOMEPAGE;
        //This will not show up on homepage - flag has opposite values
    }
    if (is_array($_POST['route_targets_group'])) {
        if (in_array(-2, $_POST['route_targets_group'])) {
            //-2 means Select none of group
            // no need to post in any group
        } elseif (in_array(-1, $_POST['route_targets_group'])) {
            //-1 means select all the groups
            // post in all the groups
            $group_array = explode(',', $_POST['Allgroups']);
            foreach ($group_array as $gid) {
                // post to all the groups
                $_group = Group::load_group_by_id((int) $gid);
                $login_required_str = null;
                if ($_group->access_type == ACCESS_PRIVATE) {
                    $login_required_str = '&login_required=true';
                }
                switch ($type) {
                    case 'BlogPost':
                    default:
                        $res = BlogPost::save_blogpost(0, PA::$login_uid, $_POST['blog_title'], $_POST['description'], NULL, $terms, $gid, $is_active = 1, $display_on_homepage);
                        break;
                    case 'Contribution':
                        $res = Contribution::save_contribution(0, PA::$login_uid, $_POST['blog_title'], $_POST['description'], NULL, $terms, $gid, $is_active = 1, $display_on_homepage);
                        break;
                    case 'Suggestion':
                        $res = Suggetion::save_suggestion(0, PA::$login_uid, $_POST['blog_title'], $_POST['description'], NULL, $terms, $gid, $is_active = 1, $display_on_homepage);
                        break;
                }
                $permalink_cid = $res['cid'];
                // NOTE: would this notification message be sent for each group ???
                $content_obj = Content::load_content((int) $permalink_cid);
                PANotify::send("content_posted", PA::$network_info, $user, $content_obj);
                // notify network owner (maybe group owner would be better?)
                if ($display_on_homepage == DISPLAY_ON_HOMEPAGE) {
                    PANotify::send("content_posted_to_comm_blog", PA::$network_info, $user, $content_obj);
                }
                //-------
                //for rivers of people
                $activity = 'group_post_a_blog';
                $activity_extra['info'] = $user->first_name . 'posted a new blog';
                $activity_extra['blog_name'] = $_POST["blog_title"];
                $activity_extra['blog_id'] = $permalink_cid;
                $activity_extra['blog_url'] = PA::$url . PA_ROUTE_CONTENT . '/cid=' . $permalink_cid . $login_required_str;
                $extra = serialize($activity_extra);
                $object = $gid;
                // update status to unverified
                $group = ContentCollection::load_collection((int) $gid, PA::$login_uid);
                if ($group->reg_type == REG_MODERATED) {
                    Network::moderate_network_content((int) $gid, $permalink_cid);
                } else {
                    if ($extra['network_content_moderation'] == NET_YES && $is_edit == 0 && PA::$network_info->owner_id != $user->user_id) {
                        Network::moderate_network_content($gid, $permalink_cid);
                    }
                }
                if (!PA::is_moderated_content() && $group->reg_type != REG_MODERATED) {
                    //Write to activity log only when moderation is off
                    Activities::save($user->user_id, $activity, $object, $extra);
                }
            }
        } else {
            // post in selected groups
            foreach ($_POST['route_targets_group'] as $gid) {
                //only send to selected groups
                $_group = Group::load_group_by_id((int) $gid);
                $login_required_str = null;
                if ($_group->access_type == ACCESS_PRIVATE) {
                    $login_required_str = '&login_required=true';
                }
                switch ($type) {
                    case 'BlogPost':
                    default:
                        $res = BlogPost::save_blogpost(0, PA::$login_uid, $_POST['blog_title'], $_POST['description'], NULL, $terms, $gid, $is_active = 1, $display_on_homepage);
                        break;
                    case 'Contribution':
                        $res = Contribution::save_contribution(0, PA::$login_uid, $_POST['blog_title'], $_POST['description'], NULL, $terms, $gid, $is_active = 1, $display_on_homepage);
                        break;
                    case 'Suggestion':
                        $res = Suggestion::save_suggestion(0, PA::$login_uid, $_POST['blog_title'], $_POST['description'], NULL, $terms, $gid, $is_active = 1, $display_on_homepage);
                        break;
                }
                $permalink_cid = $res['cid'];
                $content_obj = Content::load_content((int) $permalink_cid);
                PANotify::send("content_posted", PA::$network_info, $user, $content_obj);
                // notify network owner (maybe group owner would be better?)
                if ($display_on_homepage == DISPLAY_ON_HOMEPAGE) {
                    PANotify::send("content_posted_to_comm_blog", PA::$network_info, $user, $content_obj);
                }
                //for rivers of people
                $activity = 'group_post_a_blog';
                $activity_extra['info'] = $user->first_name . 'posted a new blog';
                $activity_extra['blog_name'] = $_POST["blog_title"];
                $activity_extra['blog_id'] = $permalink_cid;
                $activity_extra['blog_url'] = PA::$url . PA_ROUTE_CONTENT . '/cid=' . $permalink_cid . $login_required_str;
                $extra = serialize($activity_extra);
                $object = $gid;
                // update status to unverified
                $group = ContentCollection::load_collection((int) $gid, PA::$login_uid);
                if ($group->reg_type == REG_MODERATED) {
                    Network::moderate_network_content((int) $gid, $permalink_cid);
                } else {
                    if ($extra['network_content_moderation'] == NET_YES && $is_edit == 0 && PA::$network_info->owner_id != $user->user_id) {
                        Network::moderate_network_content($gid, $permalink_cid);
                    }
                }
                if (!PA::is_moderated_content() && $group->reg_type != REG_MODERATED) {
                    //Write to activity log only when moderation is off
                    Activities::save($user->user_id, $activity, $object, $extra);
                }
            }
        }
    }
    return TRUE;
}
 public static function add_default_blog($user_id)
 {
     require_once "api/BlogPost/BlogPost.php";
     $extra = unserialize(PA::$network_info->extra);
     if ($extra['user_defaults']['default_blog'] != NET_NO) {
         // if network operator has set a default blog
         $net_extra_blog_id = (int) $extra['user_defaults']['default_blog'];
         $condition = 'content_id = ' . $net_extra_blog_id;
         $admin_content = Content::get(NULL, $condition);
         $no_display_on_home_page = !DISPLAY_ON_HOMEPAGE;
         if (!empty($admin_content[0])) {
             try {
                 $post_saved = BlogPost::save_blogpost(0, $user_id, $admin_content[0]['title'], $admin_content[0]['body'], NULL, NULL, -1, ACTIVE, $no_display_on_home_page, TRUE);
             } catch (PAException $e) {
                 throw $e;
             }
         }
     }
     // end of if
 }
function metaWeblog_editPost($args)
{
    $postid = $args['postid'];
    $login = $args['login'];
    $password = $args['password'];
    $post = $args['post'];
    $publish = $args['publish'];
    // ignored
    $user = api_load_user($login, $password);
    list($ccid, $context, $group, $cid, $content) = api_parse_postid($user, $postid);
    // assert access
    if ($content->author_id != $user->user_id) {
        throw new PAException(USER_ACCESS_DENIED, "Only the author can edit a post");
    }
    // save changes to post
    BlogPost::save_blogpost($cid, $user->user_id, $post['title'], $post['description'], array(), array(), $ccid);
    // invalidate caches
    $cache_id = 'content_' . $cid;
    if (PA::$network_info) {
        $cache_id .= '_network_' . PA::$network_info->network_id;
    }
    CachedTemplate::invalidate_cache($cache_id);
    Logger::log("invalidating cache for {$cache_id}");
    return true;
}
 function render_for_ajax()
 {
     $op = $this->params["op"];
     $this->gid = @$this->params['blog_id'];
     if ($op != 'paging' && empty(PA::$login_user)) {
         return __("Login required");
     }
     switch ($op) {
         case "save_post":
             // $this->note = "Save piost goes here.";
             // validation
             // return "<pre>".print_r($this->params,1)."</pre>";
             $post = $this->params;
             $this->cid = @$post['cid'];
             $errmsg = '';
             $err = FALSE;
             if (empty($post['title'])) {
                 $errmsg .= __("Please add a title.");
                 $err = TRUE;
             } else {
                 $post['title'] = $this->html($post['title']);
             }
             if (empty($post['body'])) {
                 $errmsg .= __("Please add some text.");
                 $err = TRUE;
             } else {
                 $post['body'] = $this->html($post['body']);
             }
             if ($err) {
                 $this->err = $errmsg;
                 foreach ($post as $k => $v) {
                     $this->content->{$k} = $v;
                 }
                 $this->inner_template = 'newpost.tpl';
             } else {
                 $tags = array();
                 if (!empty($post['tags'])) {
                     foreach (explode(',', $post['tags']) as $term) {
                         $tr = trim($term);
                         if ($tr) {
                             $tags[] = $tr;
                         }
                     }
                 }
                 $post_saved = BlogPost::save_blogpost($this->cid, PA::$login_user->user_id, $post["title"], $post["body"], NULL, $tags, $this->gid);
                 if (empty($post_saved['cid'])) {
                     $this->note = "<pre>" . print_r($this, 1) . "</pre>";
                     $this->err = "<pre>" . print_r($post_saved['errors'], 1) . "</pre>";
                     foreach ($post as $k => $v) {
                         $this->content->{$k} = $v;
                     }
                     $this->inner_template = 'newpost.tpl';
                 }
             }
             break;
         case "new_post":
             $this->inner_template = 'newpost.tpl';
             break;
         case "edit_post":
             $this->inner_template = 'newpost.tpl';
             $this->cid = @$this->params['cid'];
             $this->content = NULL;
             if ($this->cid) {
                 $this->content = Content::load_content((int) $this->cid, (int) PA::$login_uid);
             }
             break;
         case "delete_post":
             $post = $this->params;
             // owner check would go here
             try {
                 Content::delete_by_id($post['cid']);
                 $this->note = __("Post was deleted successfully.");
                 unset($this->params['cid']);
                 // or we'd have a permalink to a post we no longer have
             } catch (PAException $e) {
                 $this->err = __("There was an error deleting this post: ") . $e->getMessage();
             }
             break;
         case "remove_author":
             // the group is not loaded at this point soo we do it here
             $g = ContentCollection::load_collection($this->gid, PA::$login_user->user_id);
             // unjoin user to group
             if ($g->leave($this->params['pa_id'])) {
                 $this->note = "Successfully removed author.";
             } else {
                 $this->err = "Couldn't remove author.";
             }
             break;
         case "add_author":
             // the group is not loaded at this point soo we do it here
             $g = ContentCollection::load_collection($this->gid, PA::$login_user->user_id);
             // find real PA user_id
             $su = new ShadowUser($this->skin);
             try {
                 $su->load($this->params['remote_id']);
                 if ($su->user_id) {
                     // join user to group
                     if ($g->join($su->user_id)) {
                         $this->note = "Successfully removed author.";
                         unset($this->params['remote_id']);
                     } else {
                         $this->err = "Couldn't add author.";
                     }
                 } else {
                     $this->err = "Couldn't add author with UserID " . $this->params['remote_id'] . " no such user.";
                 }
             } catch (PAException $e) {
                 $this->err = "There was an error adding author: " . $e->getMessage();
             }
             break;
         default:
             break;
     }
     return $this->render();
 }
function route2groups()
{
    global $login_uid, $user, $network_info;
    $extra = unserialize($network_info->extra);
    $tags = explode(',', $_POST['tags']);
    //find tag entry
    foreach ($tags as $term) {
        $tr = trim($term);
        if ($tr) {
            $terms[] = $tr;
        }
    }
    if ($_POST['route_to_pa_home'] == 1) {
        $display_on_homepage = DISPLAY_ON_HOMEPAGE;
        //its zero
    } else {
        $display_on_homepage = NO_DISPLAY_ON_HOMEPAGE;
        //This will not show up on homepage - flag has opposite values
    }
    if (is_array($_POST['route_targets_group'])) {
        if (in_array(-2, $_POST['route_targets_group'])) {
            //-2 means Select none of group
            // no need to post in any group
        } elseif (in_array(-1, $_POST['route_targets_group'])) {
            //-1 means select all the groups
            // post in all the groups
            $group_array = explode(',', $_POST['Allgroups']);
            foreach ($group_array as $gid) {
                // post to all the groups
                $res = BlogPost::save_blogpost(0, $login_uid, $_POST['blog_title'], $_POST['description'], NULL, $terms, $gid, $is_active = 1, $display_on_homepage);
                $permalink_cid = $res['cid'];
                $content_author_image = uihelper_resize_mk_user_img($user->picture, 80, 80, 'alt="' . $user->first_name . '" align="left" style="padding: 0px 12px 12px 0px;"');
                $params['cid'] = $permalink_cid;
                $params['first_name'] = $user->first_name;
                $params['user_id'] = $user->user_id;
                $params['user_image'] = $content_author_image;
                $params['content_title'] = $_POST["blog_title"];
                $params['network_name'] = $network_info->name;
                auto_email_notification('content_posted', $params);
                if ($_POST['route_to_pa_home'] == 1) {
                    auto_email_notification('content_posted_to_comm_blog', $params);
                }
            }
            // update status to unverified
            if ($extra['network_content_moderation'] == NET_YES && $is_edit == 0 && $network_info->owner_id != $user->user_id) {
                Network::moderate_network_content($gid, $permalink_cid);
            }
        } else {
            // post in selected groups
            foreach ($_POST['route_targets_group'] as $gid) {
                //only send to selected groups
                $res = BlogPost::save_blogpost(0, $login_uid, $_POST['blog_title'], $_POST['description'], NULL, $terms, $gid, $is_active = 1, $display_on_homepage);
                $permalink_cid = $res['cid'];
                $content_author_image = uihelper_resize_mk_user_img($user->picture, 80, 80, 'alt="' . $user->first_name . '" align="left" style="padding: 0px 12px 12px 0px;"');
                $params['cid'] = $permalink_cid;
                $params['first_name'] = $user->first_name;
                $params['user_id'] = $user->user_id;
                $params['user_image'] = $content_author_image;
                $params['content_title'] = $_POST["blog_title"];
                $params['network_name'] = $network_info->name;
                auto_email_notification('content_posted', $params);
                if ($_POST['route_to_pa_home'] == 1) {
                    auto_email_notification('content_posted_to_comm_blog', $params);
                }
                // update status to unverified
                if ($extra['network_content_moderation'] == NET_YES && $is_edit == 0 && $network_info->owner_id != $user->user_id) {
                    Network::moderate_network_content($gid, $permalink_cid);
                }
            }
        }
    }
    return TRUE;
}