array( "function" => array($this, "getUsersBlogs") ), "metaWeblog.getRecentPosts" => array( 'function' => array($this, 'getRecentPosts') ), 'metaWeblog.getCategories' => array( 'function' => array($this, 'getCategories') ), 'metaWeblog.newPost' => array( 'function' => array($this, 'newPost') ) ), false); // Use nice php functions, and call the service $service->functions_parameters_type = 'phpvals'; $service->service(); // Tell SilverStripe not to try render a template return false; } /** * Authenticate the user. * * If blogid is not null, make sure that the user is the author of the blog * * @param $username * @param $password * @param $blogid * @return $member */ private function authenticate($username, $password, $blogid=null) { $member = MemberAuthenticator::authenticate(array( 'Email' => $username, 'Password' => $password, )); if (!$member){ Debug::log(Debug::text("Authentication Failed for " . $username)); return false; } Session::set("loggedInAs",$member->ID); if ($blogid && self::$RESTRICT_POST_TO_OWNER) { $blogHolder = DataObject::get_one("BlogHolder", "`BlogHolder`.`ID` = " . (int) $blogid); if ($blogHolder->OwnerID == $member->ID){ return $member; } }else { return $member; } return false; } /** * Get a list of BlogHolders the user has access to. */ function getUsersBlogs($appkey, $username, $password) { $member = $this->authenticate($username, $password); // TODO Throw approriate error. if(!$member) die(); $blogholders = DataObject::get('BlogHolder'); $response = array(); foreach($blogholders as $bh) { if(!$bh->canAddChildren($member)) continue; $bgarr = array(); $bgarr['url'] = $bh->AbsoluteLink(); $bgarr['blogid'] = (int) $bh->ID; $bgarr['blogname'] = $bh->Title; $response[] = $bgarr; } return $response; } /** * Get the most recent posts on a blog. */ function getRecentPosts($blogid, $username, $password, $numberOfPosts) { $member = $this->authenticate($username, $password); // TODO Throw approriate error. if(!$member) die(); $posts = DataObject::get('BlogEntry', '"ParentID" = ' . (int) $blogid, '"Date" DESC'); $res = array(); $postsSoFar = 0; foreach($posts as $post) { if(!$post->canEdit($member)) continue; $parr = array(); $parr['title'] = $post->Title; $parr['link'] = $post->AbsoluteLink(); $parr['description'] = $post->Content; $parr['postid'] = (int) $post->ID; $res[] = $parr; if(++$postsSoFar >= $numberOfPosts) break; } return $res; } function getCategories() { //TODO dummy function return array(); } /** * Post a new Blog entry onto a Blog * * @param $blogid * @param $username * @param $password * @param $struct * @param $publish * @return Boolean */ public function newPost($blogid, $username, $password, $struct, $publish) { $member = $this->authenticate($username, $password, $blogid); // TODO Throw approriate error. if (!$member) die(); $blogEntry = new BlogEntry(); $blogEntry->setField("Title", $struct['title']); $blogEntry->setField("Content", $struct['description']); $blogEntry->setField("ParentID", $blogid); $blogEntry->write(); if($publish && !self::$MODERATE_PUBLISHING){ $blogEntry->doPublish(); } return true; } } ?>