<?php

    lt_include( PLOG_CLASS_PATH."class/action/action.class.php" );
    lt_include( PLOG_CLASS_PATH."class/config/config.class.php" );
    lt_include( MOBILE_PLOG_CLASS_PATH."class/net/mobilerequestgenerator.class.php" );
    lt_include( PLOG_CLASS_PATH."class/data/validator/integervalidator.class.php" );

    class MobileAction extends Action
    {
    
        var $_blogInfo;
        var $_locale;
        var $_tInfo;
    
        function MobileAction( $actionInfo, $httpRequest )
        {
            $this->Action( $actionInfo, $httpRequest );
            
            $this->_blogInfo = $this->_getBlogInfo();
            $this->_locale =& $this->_blogInfo->getLocale();
            
            // detect the terminal that made the request...
            $ti = new TerminalIdentifier();
            $this->_tInfo = $ti->identify( $_SERVER );
            
            // ...and quit the whole thing if there is no 'default' terminal
            if( !$this->_tInfo ) {
                die( "No terminal matched the request! Make sure that at least there is
                      one matching terminal definition." );
            }            
        }
        
        /**
         * @private
         */
        function _getBlogInfo()
        {
			lt_include( PLOG_CLASS_PATH."class/dao/blogs.class.php" );

			// see if we're using subdomains
			$config =& Config::getConfig();
			if( $config->getValue( "subdomains_enabled" )) {
				lt_include( PLOG_CLASS_PATH."class/net/http/subdomains.class.php" );

				$subdomainInfo = Subdomains::getSubdomainInfoFromRequest();

				if( !empty($subdomainInfo["blogdomain"]) && $this->_request->getValue( 'blogDomain' ) == "" ) {
					$this->_request->setValue( 'blogDomain', $subdomainInfo["blogdomain"] );
				}
				if( !empty($subdomainInfo["username"]) && $this->_request->getValue( 'blogUserName' ) == "" ) {
					$this->_request->setValue( 'blogUserName', $subdomainInfo["username"] );
				}
				if( !empty($subdomainInfo["blogname"]) && $this->_request->getValue( 'blogName' ) == "" ) {
					$this->_request->setValue( 'blogName', $subdomainInfo["blogname"] );
				}
			}

            $blogId = $this->_request->getValue( "blogId" );
    		$blogName = $this->_request->getValue( 'blogName' );
    		$userId = $this->_request->getValue( 'userId' );
    		$userName = $this->_request->getValue( 'blogUserName' );
            $blogDomain = $this->_request->getValue( 'blogDomain' );

            // if there is a "blogId" parameter, it takes precedence over the
            // "user" parameter.
            if( !$blogId && !$blogName && !$blogDomain) {
            	// check if there was a user parameter
                if( !empty($userName) ) {
                    lt_include( PLOG_CLASS_PATH."class/dao/users.class.php" );
                	// if so, check to which blogs the user belongs
                	$users = new Users();
                 	$userInfo = $users->getUserInfoFromUsername( $userName );
                    // if the user exists and is valid...
                	if( $userInfo ) {
                    	$userBlogs = $users->getUsersBlogs( $userInfo->getId(), BLOG_STATUS_ACTIVE );
						// check all the blogs and pick the first one that is owned. If none is owned, then pick a random
						// one (the last one that was processed)
                    	if( !empty($userBlogs)) {
							$i = 0;
							$found = false;
							while( $i < count($userBlogs) && !$found ) {
								$blog = $userBlogs[$i];
								if( $blog->getOwnerId() == $userInfo->getId()) $found = true;
								$i++;
							}
							$blogId = $blog->getId();
                        } 
						else {
                        	$blogId = $this->_config->getValue('default_blog_id');
                        }
                    } 
					else {
                    	$blogId = $this->_config->getValue('default_blog_id');
                    }
                }
                else {
                    // if there is no user parameter, we take the blogId from the session
                    if( $this->_session->getValue('blogId') != '' ) {
                    	$blogId = $this->_session->getValue('blogId');
                    }
                    else {
                        // get the default blog id from the database
                        $blogId = $this->_config->getValue('default_blog_id');                        
                    }
                }
            }			

            $val = new IntegerValidator();
            if( !$val->validate( $blogId )) {
                $config =& Config::getConfig();
                $blogId = $config->getValue( "default_blog_id" );
            }
			         
            // try to load the blog
            $blogs = new Blogs();
            $blogInfo = $blogs->getBlogInfo( $blogId );
            
            if( !$blogInfo ) {
                die( "ERROR: The blog does not exist" );
            }
            
            if( $blogInfo->getStatus() != BLOG_STATUS_ACTIVE ) {
                die( "ERROR: The blog does not exist" );            
            }
            
            return( $blogInfo );
        }
        
        function setCommonData()
        {            
            $this->_view->setValue( "blog", $this->_blogInfo );
            $this->_view->setValue( "murl", new MobileRequestGenerator( $this->_blogInfo ));
            $this->_view->setValue( "terminall", $this->_tInfo );
        }
    }
?>