function PageMain()
{
    global $template, $templatesdir, $includesdir, $config;
    //Do any page logic here.
    //If $using_db is set in standard_page_logic.inc the global $db
    //will be set. Most db queries should be done in a class library, though.
    //This simple example does stuff internally. For complicated json, you
    //might be better off making a json template in templates/js
    $template['callback'] = getVar('callback');
    $template['echo'] = getVar('echo');
    if ($template['callback'] != "" && preg_match('/^\\w+$/', $template['callback'])) {
        //use jsonp
        header('Content-type: text/javascript');
    } else {
        //standard json
        header('Content-type: application/json');
    }
    $pid = getVar('plusid');
    if ($pid == "") {
        $pid = '109463793347920104139';
    }
    $plookup = array();
    $linklookup = array();
    $people = array();
    $links = array();
    //This person
    $person = getCachedPerson($pid);
    $pdata = extractPersonData($person, 0);
    $plookup[$pdata['id']] = 1;
    //Followees
    $followees = PlusPerson::FetchVisiblePlusPeople($person->googleplus_id);
    foreach ($followees as $followee) {
        $pdata = extractPersonData($followee, 1);
        if (!isset($plookup[$pdata['id']])) {
            $people[] = $pdata;
            $plookup[$pdata['id']] = 1;
        }
    }
    $data = array('person' => extractPersonData($person, 0), 'relationships' => $people);
    $responsedata = json_encode($data);
    //wrap jsonp if necessary
    if ($template['callback'] != "" && preg_match('/^\\w+$/', $template['callback'])) {
        $responsedata = $template['callback'] . '(' . $responsedata . ');';
    }
    echo $responsedata;
}
function PageMain()
{
    global $template, $templatesdir, $includesdir, $config;
    //Do any page logic here.
    //If $using_db is set in standard_page_logic.inc the global $db
    //will be set. Most db queries should be done in a class library, though.
    //This simple example does stuff internally. For complicated json, you
    //might be better off making a json template in templates/js
    $template['callback'] = getVar('callback');
    $template['echo'] = getVar('echo');
    if ($template['callback'] != "" && preg_match('/^\\w+$/', $template['callback'])) {
        //use jsonp
        header('Content-type: text/javascript');
    } else {
        //standard json
        header('Content-type: application/json');
    }
    $pid = getVar('plusid');
    if ($pid == "") {
        $pid = '109463793347920104139';
    }
    $plookup = array();
    $linklookup = array();
    $people = array();
    $links = array();
    //This person
    $person = getCachedPerson($pid);
    $pdata = extractPersonData($person, 0);
    $people[] = $pdata;
    $plookup[$pdata['id']] = 1;
    //First degree
    $circledpeople = getCachedCircled($person);
    foreach ($circledpeople as $cp) {
        $pdata = extractPersonData($cp, 1);
        if (!isset($plookup[$pdata['id']])) {
            $people[] = $pdata;
            $plookup[$pdata['id']] = 1;
        }
        $link = array();
        $link['from'] = $pid;
        $link['to'] = $pdata['id'];
        $link['weight'] = 1;
        $linklookup[$link['from'] . "-" . $link['to']] = count($links);
        $links[] = $link;
    }
    //Only matching relationships for third degree
    foreach ($circledpeople as $cp) {
        $rels = array();
        if ($cp->fetched_relationships != 1) {
            $rels = PlusPerson::FetchVisiblePlusPeople($cp->googleplus_id);
        } else {
            $or = PlusRelationship::FetchRelationshipsByOwner($cp->googleplus_id);
            foreach ($or as $r) {
                $p = new PlusPerson();
                $p->googleplus_id = $r->hasincircle_id;
                $rels[] = $p;
            }
        }
        foreach ($rels as $rel) {
            $link = array();
            $link['from'] = $cp->googleplus_id;
            $link['to'] = $rel->googleplus_id;
            $link['weight'] = 1;
            //only add the links if they link back to someone we know
            //and only add them once per connection pair
            if (isset($plookup[$rel->googleplus_id])) {
                /*                if ( !isset( $linklookup[ $link['from'] . "-" . $link['to']  ] ) && 
                                    !isset( $linklookup[ $link['to'] . "-" . $link['from']  ] ) ) {
                            
                                    $linklookup[ $link['from'] . "-" . $link['to']  ] = count( $links );
                                    $links[] = $link;
                                } else {
                                    if ( isset( $linklookup[ $link['from'] . "-" . $link['to']  ] )) {
                                        $links[ $linklookup[ $link['from'] . "-" . $link['to']  ] ]['weight'] += 1;
                                    } else {
                                        $links[ $linklookup[ $link['to'] . "-" . $link['from']] ]['weight'] += 1;
                                    }
                                }
                */
                if (!isset($linklookup[$link['from'] . "-" . $link['to']])) {
                    $linklookup[$link['from'] . "-" . $link['to']] = count($links);
                    $links[] = $link;
                }
                if (isset($linklookup[$link['to'] . "-" . $link['from']])) {
                    $links[$linklookup[$link['from'] . "-" . $link['to']]]['weight'] += 1;
                    $links[$linklookup[$link['to'] . "-" . $link['from']]]['weight'] += 1;
                }
            }
        }
    }
    //lets filter out only the bidirectional links
    $bilinks = array();
    foreach ($links as $link) {
        if ($link['weight'] > 1) {
            $bilinks[] = $link;
        }
    }
    $data = array('people' => $people, 'relationships' => $links);
    $responsedata = json_encode($data);
    //wrap jsonp if necessary
    if ($template['callback'] != "" && preg_match('/^\\w+$/', $template['callback'])) {
        $responsedata = $template['callback'] . '(' . $responsedata . ');';
    }
    echo $responsedata;
}