Merge branch 'testing' into 0.9.x

This commit is contained in:
Brion Vibber 2010-03-17 12:15:40 -07:00
commit 67f2f01c5e
19 changed files with 780 additions and 177 deletions

141
actions/apimediaupload.php Normal file
View File

@ -0,0 +1,141 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Upload an image via the API
*
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category API
* @author Zach Copley <zach@status.net>
* @copyright 2010 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
if (!defined('STATUSNET')) {
exit(1);
}
require_once INSTALLDIR . '/lib/apiauth.php';
require_once INSTALLDIR . '/lib/mediafile.php';
/**
* Upload an image via the API. Returns a shortened URL for the image
* to the user.
*
* @category API
* @package StatusNet
* @author Zach Copley <zach@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
class ApiMediaUploadAction extends ApiAuthAction
{
/**
* Handle the request
*
* Grab the file from the 'media' param, then store, and shorten
*
* @todo Upload throttle!
*
* @param array $args $_REQUEST data (unused)
*
* @return void
*/
function handle($args)
{
parent::handle($args);
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$this->clientError(
_('This method requires a POST.'),
400, $this->format
);
return;
}
// Workaround for PHP returning empty $_POST and $_FILES when POST
// length > post_max_size in php.ini
if (empty($_FILES)
&& empty($_POST)
&& ($_SERVER['CONTENT_LENGTH'] > 0)
) {
$msg = _('The server was unable to handle that much POST ' .
'data (%s bytes) due to its current configuration.');
$this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
return;
}
$upload = null;
try {
$upload = MediaFile::fromUpload('media', $this->auth_user);
} catch (ClientException $ce) {
$this->clientError($ce->getMessage());
return;
}
if (isset($upload)) {
$this->showResponse($upload);
} else {
$this->clientError('Upload failed.');
return;
}
}
/**
* Show a Twitpic-like response with the ID of the media file
* and a (hopefully) shortened URL for it.
*
* @param File $upload the uploaded file
*
* @return void
*/
function showResponse($upload)
{
$this->initDocument();
$this->elementStart('rsp', array('stat' => 'ok'));
$this->element('mediaid', null, $upload->fileRecord->id);
$this->element('mediaurl', null, $upload->shortUrl());
$this->elementEnd('rsp');
$this->endDocument();
}
/**
* Overrided clientError to show a more Twitpic-like error
*
* @param String $msg an error message
*
*/
function clientError($msg)
{
$this->initDocument();
$this->elementStart('rsp', array('stat' => 'fail'));
// @todo add in error code
$errAttr = array('msg' => $msg);
$this->element('err', $errAttr, null);
$this->elementEnd('rsp');
$this->endDocument();
}
}

View File

@ -97,8 +97,6 @@ class ApiStatusnetConfigAction extends ApiAction
// XXX: check that all sections and settings are legal XML elements
common_debug(var_export($this->keys, true));
foreach ($this->keys as $section => $settings) {
$this->elementStart($section);
foreach ($settings as $setting) {
@ -110,6 +108,14 @@ class ApiStatusnetConfigAction extends ApiAction
} else if ($value === true) {
$value = 'true';
}
// return theme logo if there's no site specific one
if (empty($value)) {
if ($section == 'site' && $setting == 'logo') {
$value = Theme::path('logo.png');
}
}
$this->element($setting, null, $value);
}
$this->elementEnd($section);

View File

@ -162,7 +162,15 @@ class DeleteuserAction extends ProfileFormAction
function handlePost()
{
if (Event::handle('StartDeleteUser', array($this, $this->user))) {
$this->user->delete();
// Mark the account as deleted and shove low-level deletion tasks
// to background queues. Removing a lot of posts can take a while...
if (!$this->user->hasRole(Profile_role::DELETED)) {
$this->user->grantRole(Profile_role::DELETED);
}
$qm = QueueManager::get();
$qm->enqueue($this->user, 'deluser');
Event::handle('EndDeleteUser', array($this, $this->user));
}
}

View File

@ -732,6 +732,9 @@ class Profile extends Memcached_DataObject
function hasRight($right)
{
$result = false;
if ($this->hasRole(Profile_role::DELETED)) {
return false;
}
if (Event::handle('UserRightsCheck', array($this, $right, &$result))) {
switch ($right)
{

View File

@ -53,6 +53,7 @@ class Profile_role extends Memcached_DataObject
const ADMINISTRATOR = 'administrator';
const SANDBOXED = 'sandboxed';
const SILENCED = 'silenced';
const DELETED = 'deleted'; // Pending final deletion of notices...
public static function isValid($role)
{

View File

@ -42,6 +42,25 @@ class Safe_DataObject extends DB_DataObject
}
}
/**
* Magic function called at clone() time.
*
* We use this to drop connection with some global resources.
* This supports the fairly common pattern where individual
* items being read in a loop via a single object are cloned
* for individual processing, then fall out of scope when the
* loop comes around again.
*
* As that triggers the destructor, we want to make sure that
* the original object doesn't have its database result killed.
* It will still be freed properly when the original object
* gets destroyed.
*/
function __clone()
{
$this->_DB_resultid = false;
}
/**
* Magic function called at serialize() time.
*

View File

@ -105,8 +105,8 @@ class Subscription extends Memcached_DataObject
$auto = new Subscription();
$auto->subscriber = $subscriber->id;
$auto->subscribed = $other->id;
$auto->subscriber = $other->id;
$auto->subscribed = $subscriber->id;
$auto->created = common_sql_now();
$result = $auto->insert();

View File

@ -1083,15 +1083,11 @@ class Activity
$this->entry = $entry;
// @fixme Don't send in a DOMDocument
// Insist on a feed's root DOMElement; don't allow a DOMDocument
if ($feed instanceof DOMDocument) {
common_log(
LOG_WARNING,
'Activity::__construct() - '
. 'DOMDocument passed in for feed by mistake. '
. "Expecting a 'feed' DOMElement."
throw new ClientException(
_("Expecting a root feed element but got a whole XML document.")
);
$feed = $feed->getElementsByTagName('feed')->item(0);
}
$this->feed = $feed;

View File

@ -0,0 +1,95 @@
<?php
/*
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2010, StatusNet, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Background job to delete prolific users without disrupting front-end too much.
*
* Up to 50 messages are deleted on each run through; when all messages are gone,
* the actual account is deleted.
*
* @package QueueHandler
* @maintainer Brion Vibber <brion@status.net>
*/
class DelUserQueueHandler extends QueueHandler
{
const DELETION_WINDOW = 50;
public function transport()
{
return 'deluser';
}
public function handle($user)
{
if (!($user instanceof User)) {
common_log(LOG_ERR, "Got a bogus user, not deleting");
return true;
}
$user = User::staticGet('id', $user->id);
if (!$user) {
common_log(LOG_INFO, "User {$user->nickname} was deleted before we got here.");
return true;
}
if (!$user->hasRole(Profile_role::DELETED)) {
common_log(LOG_INFO, "User {$user->nickname} is not pending deletion; aborting.");
return true;
}
$notice = $this->getNextBatch($user);
if ($notice->N) {
common_log(LOG_INFO, "Deleting next {$notice->N} notices by {$user->nickname}");
while ($notice->fetch()) {
$del = clone($notice);
$del->delete();
}
// @todo improve reliability in case we died during the above deletions
// with a fatal error. If the job is lost, we should perform some kind
// of garbage collection later.
// Queue up the next batch.
$qm = QueueManager::get();
$qm->enqueue($user, 'deluser');
} else {
// Out of notices? Let's finish deleting this guy!
$user->delete();
common_log(LOG_INFO, "User $user->id $user->nickname deleted.");
return true;
}
return true;
}
/**
* Fetch the next self::DELETION_WINDOW messages for this user.
* @return Notice
*/
protected function getNextBatch(User $user)
{
$notice = new Notice();
$notice->profile_id = $user->id;
$notice->limit(self::DELETION_WINDOW);
$notice->find();
return $notice;
}
}

View File

@ -264,6 +264,9 @@ abstract class QueueManager extends IoManager
$this->connect('sms', 'SmsQueueHandler');
}
// Background user management tasks...
$this->connect('deluser', 'DelUserQueueHandler');
// Broadcasting profile updates to OMB remote subscribers
$this->connect('profile', 'ProfileQueueHandler');

View File

@ -628,6 +628,12 @@ class Router
array('action' => 'ApiTimelineTag',
'format' => '(xmljson|rss|atom)'));
// media related
$m->connect(
'api/statusnet/media/upload',
array('action' => 'ApiMediaUpload')
);
// search
$m->connect('api/search.atom', array('action' => 'twitapisearchatom'));
$m->connect('api/search.json', array('action' => 'twitapisearchjson'));

View File

@ -229,6 +229,17 @@ class UserProfile extends Widget
function showEntityActions()
{
if ($this->profile->hasRole(Profile_role::DELETED)) {
$this->out->elementStart('div', 'entity_actions');
$this->out->element('h2', null, _('User actions'));
$this->out->elementStart('ul');
$this->out->elementStart('p', array('class' => 'profile_deleted'));
$this->out->text(_('User deletion in progress...'));
$this->out->elementEnd('p');
$this->out->elementEnd('ul');
$this->out->elementEnd('div');
return;
}
if (Event::handle('StartProfilePageActionsSection', array(&$this->out, $this->profile))) {
$cur = common_current_user();

View File

@ -242,7 +242,7 @@ class OStatusSubAction extends Action
if (Validate::email($this->profile_uri)) {
$this->oprofile = Ostatus_profile::ensureWebfinger($this->profile_uri);
} else if (Validate::uri($this->profile_uri)) {
$this->oprofile = Ostatus_profile::ensureProfile($this->profile_uri);
$this->oprofile = Ostatus_profile::ensureProfileURL($this->profile_uri);
} else {
$this->error = _m("Sorry, we could not reach that address. Please make sure that the OStatus address is like nickname@example.com or http://example.net/nickname");
common_debug('Invalid address format.', __FILE__);
@ -339,7 +339,6 @@ class OStatusSubAction extends Action
}
}
/**
* Handle posts to this form
*

View File

@ -708,18 +708,122 @@ class Ostatus_profile extends Memcached_DataObject
* @return Ostatus_profile
* @throws FeedSubException
*/
public static function ensureProfile($profile_uri, $hints=array())
public static function ensureProfileURL($profile_url, $hints=array())
{
// Get the canonical feed URI and check it
$discover = new FeedDiscovery();
if (isset($hints['feedurl'])) {
$feeduri = $hints['feedurl'];
$feeduri = $discover->discoverFromFeedURL($feeduri);
} else {
$feeduri = $discover->discoverFromURL($profile_uri);
$hints['feedurl'] = $feeduri;
$oprofile = self::getFromProfileURL($profile_url);
if (!empty($oprofile)) {
return $oprofile;
}
$hints['profileurl'] = $profile_url;
// Fetch the URL
// XXX: HTTP caching
$client = new HTTPClient();
$client->setHeader('Accept', 'text/html,application/xhtml+xml');
$response = $client->get($profile_url);
if (!$response->isOk()) {
return null;
}
// Check if we have a non-canonical URL
$finalUrl = $response->getUrl();
if ($finalUrl != $profile_url) {
$hints['profileurl'] = $finalUrl;
$oprofile = self::getFromProfileURL($finalUrl);
if (!empty($oprofile)) {
return $oprofile;
}
}
// Try to get some hCard data
$body = $response->getBody();
$hcardHints = DiscoveryHints::hcardHints($body, $finalUrl);
if (!empty($hcardHints)) {
$hints = array_merge($hints, $hcardHints);
}
// Check if they've got an LRDD header
$lrdd = LinkHeader::getLink($response, 'lrdd', 'application/xrd+xml');
if (!empty($lrdd)) {
$xrd = Discovery::fetchXrd($lrdd);
$xrdHints = DiscoveryHints::fromXRD($xrd);
$hints = array_merge($hints, $xrdHints);
}
// If discovery found a feedurl (probably from LRDD), use it.
if (array_key_exists('feedurl', $hints)) {
return self::ensureFeedURL($hints['feedurl'], $hints);
}
// Get the feed URL from HTML
$discover = new FeedDiscovery();
$feedurl = $discover->discoverFromHTML($finalUrl, $body);
if (!empty($feedurl)) {
$hints['feedurl'] = $feedurl;
return self::ensureFeedURL($feedurl, $hints);
}
}
static function getFromProfileURL($profile_url)
{
$profile = Profile::staticGet('profileurl', $profile_url);
if (empty($profile)) {
return null;
}
// Is it a known Ostatus profile?
$oprofile = Ostatus_profile::staticGet('profile_id', $profile->id);
if (!empty($oprofile)) {
return $oprofile;
}
// Is it a local user?
$user = User::staticGet('id', $profile->id);
if (!empty($user)) {
throw new Exception("'$profile_url' is the profile for local user '{$user->nickname}'.");
}
// Continue discovery; it's a remote profile
// for OMB or some other protocol, may also
// support OStatus
return null;
}
public static function ensureFeedURL($feed_url, $hints=array())
{
$discover = new FeedDiscovery();
$feeduri = $discover->discoverFromFeedURL($feed_url);
$hints['feedurl'] = $feeduri;
$huburi = $discover->getAtomLink('hub');
$hints['hub'] = $huburi;
$salmonuri = $discover->getAtomLink(Salmon::NS_REPLIES);
@ -1303,7 +1407,7 @@ class Ostatus_profile extends Memcached_DataObject
}
}
// First, look it up
// Try looking it up
$oprofile = Ostatus_profile::staticGet('uri', 'acct:'.$addr);
@ -1317,7 +1421,7 @@ class Ostatus_profile extends Memcached_DataObject
$disco = new Discovery();
try {
$result = $disco->lookup($addr);
$xrd = $disco->lookup($addr);
} catch (Exception $e) {
// Save negative cache entry so we don't waste time looking it up again.
// @fixme distinguish temporary failures?
@ -1327,38 +1431,26 @@ class Ostatus_profile extends Memcached_DataObject
$hints = array('webfinger' => $addr);
foreach ($result->links as $link) {
switch ($link['rel']) {
case Discovery::PROFILEPAGE:
$hints['profileurl'] = $profileUrl = $link['href'];
break;
case Salmon::NS_REPLIES:
$hints['salmon'] = $salmonEndpoint = $link['href'];
break;
case Discovery::UPDATESFROM:
$hints['feedurl'] = $feedUrl = $link['href'];
break;
case Discovery::HCARD:
$hcardUrl = $link['href'];
break;
default:
common_log(LOG_NOTICE, "Don't know what to do with rel = '{$link['rel']}'");
break;
}
}
$dhints = DiscoveryHints::fromXRD($xrd);
if (isset($hcardUrl)) {
$hcardHints = self::slurpHcard($hcardUrl);
// Note: Webfinger > hcard
$hints = array_merge($hcardHints, $hints);
$hints = array_merge($hints, $dhints);
// If there's an Hcard, let's grab its info
if (array_key_exists('hcard', $hints)) {
if (!array_key_exists('profileurl', $hints) ||
$hints['hcard'] != $hints['profileurl']) {
$hcardHints = DiscoveryHints::fromHcardUrl($hints['hcard']);
$hints = array_merge($hcardHints, $hints);
}
}
// If we got a feed URL, try that
if (isset($feedUrl)) {
if (array_key_exists('feedurl', $hints)) {
try {
common_log(LOG_INFO, "Discovery on acct:$addr with feed URL $feedUrl");
$oprofile = self::ensureProfile($feedUrl, $hints);
$oprofile = self::ensureFeedURL($hints['feedurl'], $hints);
self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri);
return $oprofile;
} catch (Exception $e) {
@ -1369,10 +1461,10 @@ class Ostatus_profile extends Memcached_DataObject
// If we got a profile page, try that!
if (isset($profileUrl)) {
if (array_key_exists('profileurl', $hints)) {
try {
common_log(LOG_INFO, "Discovery on acct:$addr with profile URL $profileUrl");
$oprofile = self::ensureProfile($profileUrl, $hints);
$oprofile = self::ensureProfile($hints['profileurl'], $hints);
self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri);
return $oprofile;
} catch (Exception $e) {
@ -1384,7 +1476,9 @@ class Ostatus_profile extends Memcached_DataObject
// XXX: try hcard
// XXX: try FOAF
if (isset($salmonEndpoint)) {
if (array_key_exists('salmon', $hints)) {
$salmonEndpoint = $hints['salmon'];
// An account URL, a salmon endpoint, and a dream? Not much to go
// on, but let's give it a try
@ -1464,67 +1558,4 @@ class Ostatus_profile extends Memcached_DataObject
return $file;
}
protected static function slurpHcard($url)
{
set_include_path(get_include_path() . PATH_SEPARATOR . INSTALLDIR . '/plugins/OStatus/extlib/hkit/');
require_once('hkit.class.php');
$h = new hKit;
// Google Buzz hcards need to be tidied. Probably others too.
$h->tidy_mode = 'proxy'; // 'proxy', 'exec', 'php' or 'none'
// Get by URL
$hcards = $h->getByURL('hcard', $url);
if (empty($hcards)) {
return array();
}
// @fixme more intelligent guess on multi-hcard pages
$hcard = $hcards[0];
$hints = array();
$hints['profileurl'] = $url;
if (array_key_exists('nickname', $hcard)) {
$hints['nickname'] = $hcard['nickname'];
}
if (array_key_exists('fn', $hcard)) {
$hints['fullname'] = $hcard['fn'];
} else if (array_key_exists('n', $hcard)) {
$hints['fullname'] = implode(' ', $hcard['n']);
}
if (array_key_exists('photo', $hcard)) {
$hints['avatar'] = $hcard['photo'];
}
if (array_key_exists('note', $hcard)) {
$hints['bio'] = $hcard['note'];
}
if (array_key_exists('adr', $hcard)) {
if (is_string($hcard['adr'])) {
$hints['location'] = $hcard['adr'];
} else if (is_array($hcard['adr'])) {
$hints['location'] = implode(' ', $hcard['adr']);
}
}
if (array_key_exists('url', $hcard)) {
if (is_string($hcard['url'])) {
$hints['homepage'] = $hcard['url'];
} else if (is_array($hcard['url'])) {
// HACK get the last one; that's how our hcards look
$hints['homepage'] = $hcard['url'][count($hcard['url'])-1];
}
}
return $hints;
}
}

View File

@ -50,7 +50,6 @@ class Discovery
$this->registerMethod('Discovery_LRDD_Link_HTML');
}
public function registerMethod($class)
{
$this->methods[] = $class;
@ -122,7 +121,6 @@ class Discovery
}
}
public static function applyTemplate($template, $id)
{
$template = str_replace('{uri}', urlencode($id), $template);
@ -130,7 +128,6 @@ class Discovery
return $template;
}
public static function fetchXrd($url)
{
try {
@ -203,45 +200,11 @@ class Discovery_LRDD_Link_Header implements Discovery_LRDD
protected static function parseHeader($header)
{
preg_match('/^<[^>]+>/', $header, $uri_reference);
//if (empty($uri_reference)) return;
$lh = new LinkHeader($header);
$links = array();
$link_uri = trim($uri_reference[0], '<>');
$link_rel = array();
$link_type = null;
// remove uri-reference from header
$header = substr($header, strlen($uri_reference[0]));
// parse link-params
$params = explode(';', $header);
foreach ($params as $param) {
if (empty($param)) continue;
list($param_name, $param_value) = explode('=', $param, 2);
$param_name = trim($param_name);
$param_value = preg_replace('(^"|"$)', '', trim($param_value));
// for now we only care about 'rel' and 'type' link params
// TODO do something with the other links-params
switch ($param_name) {
case 'rel':
$link_rel = trim($param_value);
break;
case 'type':
$link_type = trim($param_value);
}
}
$links[] = array(
'href' => $link_uri,
'rel' => $link_rel,
'type' => $link_type);
return $links;
return array('href' => $lh->href,
'rel' => $lh->rel,
'type' => $lh->type);
}
}
@ -263,7 +226,6 @@ class Discovery_LRDD_Link_HTML implements Discovery_LRDD
return Discovery_LRDD_Link_HTML::parse($response->getBody());
}
public function parse($html)
{
$links = array();

View File

@ -0,0 +1,182 @@
<?php
/*
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2010, StatusNet, Inc.
*
* Some utilities for generating hint data
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
class DiscoveryHints {
static function fromXRD($xrd)
{
$hints = array();
foreach ($xrd->links as $link) {
switch ($link['rel']) {
case Discovery::PROFILEPAGE:
$hints['profileurl'] = $link['href'];
break;
case Salmon::NS_REPLIES:
$hints['salmon'] = $link['href'];
break;
case Discovery::UPDATESFROM:
$hints['feedurl'] = $link['href'];
break;
case Discovery::HCARD:
$hints['hcardurl'] = $link['href'];
break;
default:
break;
}
}
return $hints;
}
static function fromHcardUrl($url)
{
$client = new HTTPClient();
$client->setHeader('Accept', 'text/html,application/xhtml+xml');
$response = $client->get($url);
if (!$response->isOk()) {
return null;
}
return self::hcardHints($response->getBody(),
$response->getUrl());
}
static function hcardHints($body, $url)
{
common_debug("starting tidy");
$body = self::_tidy($body);
common_debug("done with tidy");
set_include_path(get_include_path() . PATH_SEPARATOR . INSTALLDIR . '/plugins/OStatus/extlib/hkit/');
require_once('hkit.class.php');
$h = new hKit;
$hcards = $h->getByString('hcard', $body);
if (empty($hcards)) {
return array();
}
if (count($hcards) == 1) {
$hcard = $hcards[0];
} else {
foreach ($hcards as $try) {
if (array_key_exists('url', $try)) {
if (is_string($try['url']) && $try['url'] == $url) {
$hcard = $try;
break;
} else if (is_array($try['url'])) {
foreach ($try['url'] as $tryurl) {
if ($tryurl == $url) {
$hcard = $try;
break 2;
}
}
}
}
}
// last chance; grab the first one
if (empty($hcard)) {
$hcard = $hcards[0];
}
}
$hints = array();
if (array_key_exists('nickname', $hcard)) {
$hints['nickname'] = $hcard['nickname'];
}
if (array_key_exists('fn', $hcard)) {
$hints['fullname'] = $hcard['fn'];
} else if (array_key_exists('n', $hcard)) {
$hints['fullname'] = implode(' ', $hcard['n']);
}
if (array_key_exists('photo', $hcard)) {
$hints['avatar'] = $hcard['photo'];
}
if (array_key_exists('note', $hcard)) {
$hints['bio'] = $hcard['note'];
}
if (array_key_exists('adr', $hcard)) {
if (is_string($hcard['adr'])) {
$hints['location'] = $hcard['adr'];
} else if (is_array($hcard['adr'])) {
$hints['location'] = implode(' ', $hcard['adr']);
}
}
if (array_key_exists('url', $hcard)) {
if (is_string($hcard['url'])) {
$hints['homepage'] = $hcard['url'];
} else if (is_array($hcard['url'])) {
// HACK get the last one; that's how our hcards look
$hints['homepage'] = $hcard['url'][count($hcard['url'])-1];
}
}
return $hints;
}
private static function _tidy($body)
{
if (function_exists('tidy_parse_string')) {
common_debug("Tidying with extension");
$text = tidy_parse_string($body);
$text = tidy_clean_repair($text);
return $body;
} else if ($fullpath = self::_findProgram('tidy')) {
common_debug("Tidying with program $fullpath");
$tempfile = tempnam('/tmp', 'snht'); // statusnet hcard tidy
file_put_contents($tempfile, $source);
exec("$fullpath -utf8 -indent -asxhtml -numeric -bare -quiet $tempfile", $tidy);
unlink($tempfile);
return implode("\n", $tidy);
} else {
common_debug("Not tidying.");
return $body;
}
}
private static function _findProgram($name)
{
$path = $_ENV['PATH'];
$parts = explode(':', $path);
foreach ($parts as $part) {
$fullpath = $part . '/' . $name;
if (is_executable($fullpath)) {
return $fullpath;
}
}
return null;
}
}

View File

@ -0,0 +1,63 @@
<?php
class LinkHeader
{
var $href;
var $rel;
var $type;
function __construct($str)
{
preg_match('/^<[^>]+>/', $str, $uri_reference);
//if (empty($uri_reference)) return;
$this->uri = trim($uri_reference[0], '<>');
$this->rel = array();
$this->type = null;
// remove uri-reference from header
$str = substr($str, strlen($uri_reference[0]));
// parse link-params
$params = explode(';', $str);
foreach ($params as $param) {
if (empty($param)) continue;
list($param_name, $param_value) = explode('=', $param, 2);
$param_name = trim($param_name);
$param_value = preg_replace('(^"|"$)', '', trim($param_value));
// for now we only care about 'rel' and 'type' link params
// TODO do something with the other links-params
switch ($param_name) {
case 'rel':
$this->rel = trim($param_value);
break;
case 'type':
$this->type = trim($param_value);
}
}
}
static function getLink($response, $rel=null, $type=null)
{
$headers = $response->getHeader('Link');
// Can get an array or string, so try to simplify the path
if (!is_array($headers)) {
$headers = array($headers);
}
foreach ($headers as $header) {
$lh = new LinkHeader($header);
if ((is_null($rel) || $lh->rel == $rel) &&
(is_null($type) || $lh->type == $type)) {
return $lh->href;
}
}
return null;
}
}

77
scripts/fixup_files.php Executable file
View File

@ -0,0 +1,77 @@
#!/usr/bin/env php
<?php
/*
* StatusNet - a distributed open-source microblogging tool
* Copyright (C) 2010 StatusNet, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
$longoptions = array('dry-run');
$helptext = <<<END_OF_USERROLE_HELP
fixup_files.php [options]
Patches up file entries with corrupted types and titles (the "h bug").
--dry-run look but don't touch
END_OF_USERROLE_HELP;
require_once INSTALLDIR.'/scripts/commandline.inc';
$dry = have_option('dry-run');
$f = new File();
$f->title = 'h';
$f->mimetype = 'h';
$f->size = 0;
$f->protected = 0;
$f->find();
echo "Found $f->N bad items:\n";
while ($f->fetch()) {
echo "$f->id $f->url";
$data = File_redirection::lookupWhere($f->url);
if ($dry) {
if (is_array($data)) {
echo " (unchanged)\n";
} else {
echo " (unchanged, but embedding lookup failed)\n";
}
} else {
// NULL out the mime/title/size/protected fields
$sql = sprintf("UPDATE file " .
"SET mimetype=null,title=null,size=null,protected=null " .
"WHERE id=%d",
$f->id);
$f->query($sql);
$f->decache();
if (is_array($data)) {
if ($f->saveOembed($data, $f->url)) {
echo " (ok)\n";
} else {
echo " (ok, no embedding data)\n";
}
} else {
echo " (ok, but embedding lookup failed)\n";
}
}
}
echo "done.\n";