Merge branch '0.9.x' into userflag

This commit is contained in:
Evan Prodromou 2009-11-07 13:03:52 -05:00
commit d9cde0ef80
168 changed files with 110975 additions and 26613 deletions

View File

@ -474,3 +474,18 @@ StartPublicXRDS: Start XRDS output (right after the opening XRDS tag)
EndPublicXRDS: End XRDS output (right before the closing XRDS tag) EndPublicXRDS: End XRDS output (right before the closing XRDS tag)
- $action: the current action - $action: the current action
- &$xrdsoutputter - XRDSOutputter object to write to - &$xrdsoutputter - XRDSOutputter object to write to
CheckPassword: Check a username/password
- $nickname: The nickname to check
- $password: The password to check
- &$authenticated: set to true to indicate authentication succeeded.
AutoRegister: Register a new user with the given nickname. Should insert a new User and Profile into the database.
- $nickname: The nickname to register
ChangePassword: Handle a password change request
- $nickname: user's nickname
- $oldpassword: the user's old password
- $newpassword: the desired new password
- &$errormsg: set this to an error message if the password could not be changed. If the password was changed, leave this as false

2
README
View File

@ -151,6 +151,8 @@ released Aug 26 2009. Notable changes this version:
- Check for site servername config'd. - Check for site servername config'd.
- Compatibility fix for empty status updates with Twitter API. - Compatibility fix for empty status updates with Twitter API.
- Option to show files privately (EXPERIMENTAL! Use with caution.) - Option to show files privately (EXPERIMENTAL! Use with caution.)
- a script to register a new user.
- a script to make a user admin of a group.
Prerequisites Prerequisites
============= =============

View File

@ -0,0 +1,151 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Update the authenticating user's profile image
*
* 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
* @package StatusNet
* @author Zach Copley <zach@status.net>
* @copyright 2009 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';
/**
* Updates the authenticating user's profile image. Note that this API method
* expects raw multipart data, not a URL to an image.
*
* @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 ApiAccountUpdateProfileImageAction extends ApiAuthAction
{
/**
* Take arguments for running
*
* @param array $args $_REQUEST args
*
* @return boolean success flag
*
*/
function prepare($args)
{
parent::prepare($args);
$this->user = $this->auth_user;
return true;
}
/**
* Handle the request
*
* Check whether the credentials are valid and output the result
*
* @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;
}
if (empty($this->user)) {
$this->clientError(_('No such user!'), 404, $this->format);
return;
}
try {
$imagefile = ImageFile::fromUpload('image');
} catch (Exception $e) {
$this->clientError($e->getMessage(), 400, $this->format);
return;
}
$filename = Avatar::filename(
$user->id,
image_type_to_extension($imagefile->type),
null,
'tmp'.common_timestamp()
);
$filepath = Avatar::path($filename);
move_uploaded_file($imagefile->filepath, $filepath);
$profile = $this->user->getProfile();
if (empty($profile)) {
$this->clientError(_('User has no profile.'));
return;
}
$profile->setOriginal($filename);
common_broadcast_profile($profile);
$twitter_user = $this->twitterUserArray($this->user->getProfile(), true);
if ($this->format == 'xml') {
$this->initDocument('xml');
$this->showTwitterXmlUser($twitter_user);
$this->endDocument('xml');
} elseif ($this->format == 'json') {
$this->initDocument('json');
$this->showJsonObjects($twitter_user);
$this->endDocument('json');
}
}
}

View File

@ -33,7 +33,7 @@ if (!defined('STATUSNET')) {
exit(1); exit(1);
} }
require_once INSTALLDIR . '/lib/api.php'; require_once INSTALLDIR . '/lib/apiprivateauth.php';
/** /**
* Tests for the existence of friendship between two users. Will return true if * Tests for the existence of friendship between two users. Will return true if
@ -48,7 +48,7 @@ require_once INSTALLDIR . '/lib/api.php';
* @link http://status.net/ * @link http://status.net/
*/ */
class ApiFriendshipsExistsAction extends ApiAction class ApiFriendshipsExistsAction extends ApiPrivateAuthAction
{ {
var $user_a = null; var $user_a = null;
var $user_b = null; var $user_b = null;
@ -69,16 +69,7 @@ class ApiFriendshipsExistsAction extends ApiAction
$user_a_id = $this->trimmed('user_a'); $user_a_id = $this->trimmed('user_a');
$user_b_id = $this->trimmed('user_b'); $user_b_id = $this->trimmed('user_b');
common_debug("user_a = " . $user_a_id);
common_debug("user_b = " . $user_b_id);
$this->user_a = $this->getTargetUser($user_a_id); $this->user_a = $this->getTargetUser($user_a_id);
if (empty($this->user_a)) {
common_debug('gargargra');
}
$this->user_b = $this->getTargetUser($user_b_id); $this->user_b = $this->getTargetUser($user_b_id);
return true; return true;

View File

@ -34,7 +34,7 @@ if (!defined('STATUSNET')) {
exit(1); exit(1);
} }
require_once INSTALLDIR . '/lib/api.php'; require_once INSTALLDIR . '/lib/apiprivateauth.php';
/** /**
* Returns of the lastest 20 groups for the site * Returns of the lastest 20 groups for the site
@ -49,7 +49,7 @@ require_once INSTALLDIR . '/lib/api.php';
* @link http://status.net/ * @link http://status.net/
*/ */
class ApiGroupListAllAction extends ApiAction class ApiGroupListAllAction extends ApiPrivateAuthAction
{ {
var $groups = null; var $groups = null;

View File

@ -34,7 +34,7 @@ if (!defined('STATUSNET')) {
exit(1); exit(1);
} }
require_once INSTALLDIR . '/lib/api.php'; require_once INSTALLDIR . '/lib/apiprivateauth.php';
/** /**
* List 20 newest members of the group specified by name or ID. * List 20 newest members of the group specified by name or ID.
@ -49,7 +49,7 @@ require_once INSTALLDIR . '/lib/api.php';
* @link http://status.net/ * @link http://status.net/
*/ */
class ApiGroupMembershipAction extends ApiAction class ApiGroupMembershipAction extends ApiPrivateAuthAction
{ {
var $group = null; var $group = null;
var $profiles = null; var $profiles = null;

View File

@ -34,7 +34,7 @@ if (!defined('STATUSNET')) {
exit(1); exit(1);
} }
require_once INSTALLDIR . '/lib/api.php'; require_once INSTALLDIR . '/lib/apiprivateauth.php';
/** /**
* Outputs detailed information about the group specified by ID * Outputs detailed information about the group specified by ID
@ -49,7 +49,7 @@ require_once INSTALLDIR . '/lib/api.php';
* @link http://status.net/ * @link http://status.net/
*/ */
class ApiGroupShowAction extends ApiAction class ApiGroupShowAction extends ApiPrivateAuthAction
{ {
var $group = null; var $group = null;

View File

@ -32,7 +32,7 @@ if (!defined('STATUSNET')) {
exit(1); exit(1);
} }
require_once INSTALLDIR . '/lib/api.php'; require_once INSTALLDIR . '/lib/apiprivateauth.php';
/** /**
* Returns the string "ok" in the requested format with a 200 OK HTTP status code. * Returns the string "ok" in the requested format with a 200 OK HTTP status code.
@ -45,7 +45,7 @@ require_once INSTALLDIR . '/lib/api.php';
* @link http://status.net/ * @link http://status.net/
*/ */
class ApiHelpTestAction extends ApiAction class ApiHelpTestAction extends ApiPrivateAuthAction
{ {
/** /**

View File

@ -37,7 +37,7 @@ if (!defined('STATUSNET')) {
exit(1); exit(1);
} }
require_once INSTALLDIR . '/lib/api.php'; require_once INSTALLDIR . '/lib/apiprivateauth.php';
/** /**
* Returns the notice specified by id as a Twitter-style status and inline user * Returns the notice specified by id as a Twitter-style status and inline user
@ -55,7 +55,7 @@ require_once INSTALLDIR . '/lib/api.php';
* @link http://status.net/ * @link http://status.net/
*/ */
class ApiStatusesShowAction extends ApiAction class ApiStatusesShowAction extends ApiPrivateAuthAction
{ {
var $notice_id = null; var $notice_id = null;

View File

@ -80,7 +80,7 @@ class ApiStatusesUpdateAction extends ApiAuthAction
$this->status = $this->trimmed('status'); $this->status = $this->trimmed('status');
$this->source = $this->trimmed('source'); $this->source = $this->trimmed('source');
if (empty($this->source) || in_array($source, $this->reserved_sources)) { if (empty($this->source) || in_array($source, self::$reserved_sources)) {
$this->source = 'api'; $this->source = 'api';
} }
@ -112,6 +112,20 @@ class ApiStatusesUpdateAction extends ApiAuthAction
return; 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;
}
if (empty($this->status)) { if (empty($this->status)) {
$this->clientError( $this->clientError(
'Client must provide a \'status\' parameter with a value.', 'Client must provide a \'status\' parameter with a value.',
@ -126,13 +140,6 @@ class ApiStatusesUpdateAction extends ApiAuthAction
return; return;
} }
// Workaround for PHP returning empty $_FILES when POST length > PHP settings
if (empty($_POST) && ($_SERVER['CONTENT_LENGTH'] > 0)) {
$this->clientError(_('Unable to handle that much POST data!'));
return;
}
$status_shortened = common_shorten_links($this->status); $status_shortened = common_shorten_links($this->status);
if (Notice::contentTooLong($status_shortened)) { if (Notice::contentTooLong($status_shortened)) {

View File

@ -32,7 +32,7 @@ if (!defined('STATUSNET')) {
exit(1); exit(1);
} }
require_once INSTALLDIR . '/lib/api.php'; require_once INSTALLDIR . '/lib/apiprivateauth.php';
/** /**
* Returns a version number for this version of StatusNet, which * Returns a version number for this version of StatusNet, which
@ -48,7 +48,7 @@ require_once INSTALLDIR . '/lib/api.php';
* @link http://status.net/ * @link http://status.net/
*/ */
class ApiStatusnetVersionAction extends ApiAction class ApiStatusnetVersionAction extends ApiPrivateAuthAction
{ {
/** /**
* Take arguments for running * Take arguments for running

View File

@ -72,7 +72,7 @@ class ApiTimelineFriendsAction extends ApiBareAuthAction
function prepare($args) function prepare($args)
{ {
parent::prepare($args); parent::prepare($args);
common_debug("api friends_timeline");
$this->user = $this->getTargetUser($this->arg('id')); $this->user = $this->getTargetUser($this->arg('id'));
if (empty($this->user)) { if (empty($this->user)) {

View File

@ -34,7 +34,7 @@ if (!defined('STATUSNET')) {
exit(1); exit(1);
} }
require_once INSTALLDIR . '/lib/api.php'; require_once INSTALLDIR . '/lib/apiprivateauth.php';
/** /**
* Returns the most recent notices (default 20) posted to the group specified by ID * Returns the most recent notices (default 20) posted to the group specified by ID
@ -49,7 +49,7 @@ require_once INSTALLDIR . '/lib/api.php';
* @link http://status.net/ * @link http://status.net/
*/ */
class ApiTimelineGroupAction extends ApiAction class ApiTimelineGroupAction extends ApiPrivateAuthAction
{ {
var $group = null; var $group = null;

View File

@ -37,7 +37,7 @@ if (!defined('STATUSNET')) {
exit(1); exit(1);
} }
require_once INSTALLDIR . '/lib/api.php'; require_once INSTALLDIR . '/lib/apiprivateauth.php';
/** /**
* Returns the most recent notices (default 20) posted by everybody * Returns the most recent notices (default 20) posted by everybody
@ -55,7 +55,7 @@ require_once INSTALLDIR . '/lib/api.php';
* @link http://status.net/ * @link http://status.net/
*/ */
class ApiTimelinePublicAction extends ApiAction class ApiTimelinePublicAction extends ApiPrivateAuthAction
{ {
var $notices = null; var $notices = null;

View File

@ -34,7 +34,7 @@ if (!defined('STATUSNET')) {
exit(1); exit(1);
} }
require_once INSTALLDIR . '/lib/api.php'; require_once INSTALLDIR . '/lib/apiprivateauth.php';
/** /**
* Returns the 20 most recent notices tagged by a given tag * Returns the 20 most recent notices tagged by a given tag
@ -49,7 +49,7 @@ require_once INSTALLDIR . '/lib/api.php';
* @link http://status.net/ * @link http://status.net/
*/ */
class ApiTimelineTagAction extends ApiAction class ApiTimelineTagAction extends ApiPrivateAuthAction
{ {
var $notices = null; var $notices = null;

View File

@ -34,7 +34,7 @@ if (!defined('STATUSNET')) {
exit(1); exit(1);
} }
require_once INSTALLDIR . '/lib/api.php'; require_once INSTALLDIR . '/lib/apiprivateauth.php';
/** /**
* Ouputs information for a user, specified by ID or screen name. * Ouputs information for a user, specified by ID or screen name.
@ -50,7 +50,7 @@ require_once INSTALLDIR . '/lib/api.php';
* @link http://status.net/ * @link http://status.net/
*/ */
class ApiUserShowAction extends ApiAction class ApiUserShowAction extends ApiPrivateAuthAction
{ {
/** /**
* Take arguments for running * Take arguments for running

View File

@ -244,11 +244,25 @@ class AvatarsettingsAction extends AccountSettingsAction
function handlePost() function handlePost()
{ {
// 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->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
return;
}
// CSRF protection // CSRF protection
$token = $this->trimmed('token'); $token = $this->trimmed('token');
if (!$token || $token != common_session_token()) { if (!$token || $token != common_session_token()) {
$this->show_form(_('There was a problem with your session token. '. $this->showForm(_('There was a problem with your session token. '.
'Try again, please.')); 'Try again, please.'));
return; return;
} }

View File

@ -79,6 +79,8 @@ class LoginAction extends Action
$this->clientError(_('Already logged in.')); $this->clientError(_('Already logged in.'));
} else if ($_SERVER['REQUEST_METHOD'] == 'POST') { } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$this->checkLogin(); $this->checkLogin();
} else if (isset($args['user_id']) && isset($args['token'])){
$this->checkLogin($args['user_id'],$args['token']);
} else { } else {
common_ensure_session(); common_ensure_session();
$this->showForm(); $this->showForm();
@ -95,23 +97,48 @@ class LoginAction extends Action
* @return void * @return void
*/ */
function checkLogin() function checkLogin($user_id=null, $token=null)
{ {
// XXX: login throttle if(isset($token) && isset($user_id)){
//Token based login (from the LoginCommand)
$login_token = Login_token::staticGet('user_id',$user_id);
if($login_token && $login_token->token == $token){
if($login_token->modified > time()+2*60){
//token has expired
//delete the token as it is useless
$login_token->delete();
$this->showForm(_('Invalid or expired token.'));
return;
}else{
//delete the token so it cannot be reused
$login_token->delete();
//it's a valid token - let them log in
$user = User::staticGet('id', $user_id);
//$user = User::staticGet('nickname', "candrews");
}
}else{
$this->showForm(_('Invalid or expired token.'));
return;
}
}else{
// Regular form submission login
// CSRF protection - token set in NoticeForm // XXX: login throttle
$token = $this->trimmed('token');
if (!$token || $token != common_session_token()) { // CSRF protection - token set in NoticeForm
$this->clientError(_('There was a problem with your session token. '. $token = $this->trimmed('token');
'Try again, please.')); if (!$token || $token != common_session_token()) {
return; $this->clientError(_('There was a problem with your session token. '.
'Try again, please.'));
return;
}
$nickname = common_canonical_nickname($this->trimmed('nickname'));
$password = $this->arg('password');
$user = common_check_user($nickname, $password);
} }
$nickname = common_canonical_nickname($this->trimmed('nickname'));
$password = $this->arg('password');
$user = common_check_user($nickname, $password);
if (!$user) { if (!$user) {
$this->showForm(_('Incorrect username or password.')); $this->showForm(_('Incorrect username or password.'));
return; return;

View File

@ -224,15 +224,14 @@ class NewmessageAction extends Action
$this->msg = $msg; $this->msg = $msg;
if ($this->trimmed('ajax')) { if ($this->trimmed('ajax')) {
$this->startHTML('text/xml;charset=UTF-8'); header('Content-Type: text/xml;charset=utf-8');
$this->xw->startDocument('1.0', 'UTF-8');
$this->elementStart('html');
$this->elementStart('head'); $this->elementStart('head');
$this->element('title', null, _('New message')); $this->element('title', null, _('New message'));
$this->elementEnd('head'); $this->elementEnd('head');
$this->elementStart('body'); $this->elementStart('body');
if (common_logged_in()) { $this->showNoticeForm();
$this->showNoticeForm();
}
$this->elementEnd('div');
$this->elementEnd('body'); $this->elementEnd('body');
$this->endHTML(); $this->endHTML();
} }

View File

@ -164,23 +164,32 @@ class PasswordsettingsAction extends AccountSettingsAction
$this->showForm(_('Incorrect old password')); $this->showForm(_('Incorrect old password'));
return; return;
} }
}else{
$oldpassword = null;
} }
$original = clone($user); $errormsg = false;
if(! Event::handle('ChangePassword', array($user->nickname, $oldpassword, $newpassword, &$errormsg))){
//no handler changed the password, so change the password internally
$original = clone($user);
$user->password = common_munge_password($newpassword, $user->id); $user->password = common_munge_password($newpassword, $user->id);
$val = $user->validate(); $val = $user->validate();
if ($val !== true) { if ($val !== true) {
$this->showForm(_('Error saving user; invalid.')); $this->showForm(_('Error saving user; invalid.'));
return; return;
}
if (!$user->update($original)) {
$this->serverError(_('Can\'t save new password.'));
return;
}
} }
if (!$user->update($original)) { if($errormsg === false)
$this->serverError(_('Can\'t save new password.')); $this->showForm(_('Password saved.'), true);
return; else
} $this->showForm($errormsg);
$this->showForm(_('Password saved.'), true);
} }
} }

View File

@ -55,6 +55,12 @@ class RegisterAction extends Action
var $registered = false; var $registered = false;
/**
* Are we processing an invite?
*/
var $invite = null;
/** /**
* Prepare page to run * Prepare page to run
* *

View File

@ -33,6 +33,7 @@ if (!defined('STATUSNET') && !defined('LACONICA')) {
} }
require_once INSTALLDIR.'/lib/personalgroupnav.php'; require_once INSTALLDIR.'/lib/personalgroupnav.php';
require_once INSTALLDIR.'/lib/userprofile.php';
require_once INSTALLDIR.'/lib/noticelist.php'; require_once INSTALLDIR.'/lib/noticelist.php';
require_once INSTALLDIR.'/lib/profileminilist.php'; require_once INSTALLDIR.'/lib/profileminilist.php';
require_once INSTALLDIR.'/lib/groupminilist.php'; require_once INSTALLDIR.'/lib/groupminilist.php';
@ -181,262 +182,8 @@ class ShowstreamAction extends ProfileAction
function showProfile() function showProfile()
{ {
$this->showProfileData(); $profile = new UserProfile($this, $this->user, $this->profile);
$this->showEntityActions(); $profile->show();
}
function showProfileData()
{
if (Event::handle('StartProfilePageProfileSection', array(&$this, $this->profile))) {
$this->elementStart('div', 'entity_profile vcard author');
$this->element('h2', null, _('User profile'));
if (Event::handle('StartProfilePageProfileElements', array(&$this, $this->profile))) {
$this->showAvatar();
$this->showNickname();
$this->showFullName();
$this->showLocation();
$this->showHomepage();
$this->showBio();
$this->showProfileTags();
Event::handle('EndProfilePageProfileElements', array(&$this, $this->profile));
}
$this->elementEnd('div');
Event::handle('EndProfilePageProfileSection', array(&$this, $this->profile));
}
}
function showAvatar()
{
if (Event::handle('StartProfilePageAvatar', array($this, $this->profile))) {
$avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE);
$this->elementStart('dl', 'entity_depiction');
$this->element('dt', null, _('Photo'));
$this->elementStart('dd');
$this->element('img', array('src' => ($avatar) ? $avatar->displayUrl() : Avatar::defaultImage(AVATAR_PROFILE_SIZE),
'class' => 'photo avatar',
'width' => AVATAR_PROFILE_SIZE,
'height' => AVATAR_PROFILE_SIZE,
'alt' => $this->profile->nickname));
$this->elementEnd('dd');
$user = User::staticGet('id', $this->profile->id);
$cur = common_current_user();
if ($cur && $cur->id == $user->id) {
$this->elementStart('dd');
$this->element('a', array('href' => common_local_url('avatarsettings')), _('Edit Avatar'));
$this->elementEnd('dd');
}
$this->elementEnd('dl');
Event::handle('EndProfilePageAvatar', array($this, $this->profile));
}
}
function showNickname()
{
if (Event::handle('StartProfilePageNickname', array($this, $this->profile))) {
$this->elementStart('dl', 'entity_nickname');
$this->element('dt', null, _('Nickname'));
$this->elementStart('dd');
$hasFN = ($this->profile->fullname) ? 'nickname url uid' : 'fn nickname url uid';
$this->element('a', array('href' => $this->profile->profileurl,
'rel' => 'me', 'class' => $hasFN),
$this->profile->nickname);
$this->elementEnd('dd');
$this->elementEnd('dl');
Event::handle('EndProfilePageNickname', array($this, $this->profile));
}
}
function showFullName()
{
if (Event::handle('StartProfilePageFullName', array($this, $this->profile))) {
if ($this->profile->fullname) {
$this->elementStart('dl', 'entity_fn');
$this->element('dt', null, _('Full name'));
$this->elementStart('dd');
$this->element('span', 'fn', $this->profile->fullname);
$this->elementEnd('dd');
$this->elementEnd('dl');
}
Event::handle('EndProfilePageFullName', array($this, $this->profile));
}
}
function showLocation()
{
if (Event::handle('StartProfilePageLocation', array($this, $this->profile))) {
if ($this->profile->location) {
$this->elementStart('dl', 'entity_location');
$this->element('dt', null, _('Location'));
$this->element('dd', 'label', $this->profile->location);
$this->elementEnd('dl');
}
Event::handle('EndProfilePageLocation', array($this, $this->profile));
}
}
function showHomepage()
{
if (Event::handle('StartProfilePageHomepage', array($this, $this->profile))) {
if ($this->profile->homepage) {
$this->elementStart('dl', 'entity_url');
$this->element('dt', null, _('URL'));
$this->elementStart('dd');
$this->element('a', array('href' => $this->profile->homepage,
'rel' => 'me', 'class' => 'url'),
$this->profile->homepage);
$this->elementEnd('dd');
$this->elementEnd('dl');
}
Event::handle('EndProfilePageHomepage', array($this, $this->profile));
}
}
function showBio()
{
if (Event::handle('StartProfilePageBio', array($this, $this->profile))) {
if ($this->profile->bio) {
$this->elementStart('dl', 'entity_note');
$this->element('dt', null, _('Note'));
$this->element('dd', 'note', $this->profile->bio);
$this->elementEnd('dl');
}
Event::handle('EndProfilePageBio', array($this, $this->profile));
}
}
function showProfileTags()
{
if (Event::handle('StartProfilePageProfileTags', array($this, $this->profile))) {
$tags = Profile_tag::getTags($this->profile->id, $this->profile->id);
if (count($tags) > 0) {
$this->elementStart('dl', 'entity_tags');
$this->element('dt', null, _('Tags'));
$this->elementStart('dd');
$this->elementStart('ul', 'tags xoxo');
foreach ($tags as $tag) {
$this->elementStart('li');
// Avoid space by using raw output.
$pt = '<span class="mark_hash">#</span><a rel="tag" href="' .
common_local_url('peopletag', array('tag' => $tag)) .
'">' . $tag . '</a>';
$this->raw($pt);
$this->elementEnd('li');
}
$this->elementEnd('ul');
$this->elementEnd('dd');
$this->elementEnd('dl');
}
Event::handle('EndProfilePageProfileTags', array($this, $this->profile));
}
}
function showEntityActions()
{
if (Event::handle('StartProfilePageActionsSection', array(&$this, $this->profile))) {
$cur = common_current_user();
$this->elementStart('div', 'entity_actions');
$this->element('h2', null, _('User actions'));
$this->elementStart('ul');
if (Event::handle('StartProfilePageActionsElements', array(&$this, $this->profile))) {
if (empty($cur)) { // not logged in
$this->elementStart('li', 'entity_subscribe');
$this->showRemoteSubscribeLink();
$this->elementEnd('li');
} else {
if ($cur->id == $this->profile->id) { // your own page
$this->elementStart('li', 'entity_edit');
$this->element('a', array('href' => common_local_url('profilesettings'),
'title' => _('Edit profile settings')),
_('Edit'));
$this->elementEnd('li');
} else { // someone else's page
// subscribe/unsubscribe button
$this->elementStart('li', 'entity_subscribe');
if ($cur->isSubscribed($this->profile)) {
$usf = new UnsubscribeForm($this, $this->profile);
$usf->show();
} else {
$sf = new SubscribeForm($this, $this->profile);
$sf->show();
}
$this->elementEnd('li');
if ($cur->mutuallySubscribed($this->user)) {
// message
$this->elementStart('li', 'entity_send-a-message');
$this->element('a', array('href' => common_local_url('newmessage', array('to' => $this->user->id)),
'title' => _('Send a direct message to this user')),
_('Message'));
$this->elementEnd('li');
// nudge
if ($this->user->email && $this->user->emailnotifynudge) {
$this->elementStart('li', 'entity_nudge');
$nf = new NudgeForm($this, $this->user);
$nf->show();
$this->elementEnd('li');
}
}
// block/unblock
$blocked = $cur->hasBlocked($this->profile);
$this->elementStart('li', 'entity_block');
if ($blocked) {
$ubf = new UnblockForm($this, $this->profile,
array('action' => 'showstream',
'nickname' => $this->profile->nickname));
$ubf->show();
} else {
$bf = new BlockForm($this, $this->profile,
array('action' => 'showstream',
'nickname' => $this->profile->nickname));
$bf->show();
}
$this->elementEnd('li');
}
}
Event::handle('EndProfilePageActionsElements', array(&$this, $this->profile));
}
$this->elementEnd('ul');
$this->elementEnd('div');
Event::handle('EndProfilePageActionsSection', array(&$this, $this->profile));
}
}
function showRemoteSubscribeLink()
{
$url = common_local_url('remotesubscribe',
array('nickname' => $this->profile->nickname));
$this->element('a', array('href' => $url,
'class' => 'entity_remote_subscribe'),
_('Subscribe'));
} }
function showEmptyListMessage() function showEmptyListMessage()

View File

@ -47,18 +47,15 @@ class File_redirection extends Memcached_DataObject
/* the code above is auto generated do not remove the tag below */ /* the code above is auto generated do not remove the tag below */
###END_AUTOCODE ###END_AUTOCODE
function _commonCurl($url, $redirs) { static function _commonHttp($url, $redirs) {
$curlh = curl_init(); $request = new HTTPClient($url);
curl_setopt($curlh, CURLOPT_URL, $url); $request->setConfig(array(
curl_setopt($curlh, CURLOPT_AUTOREFERER, true); // # setup referer header when folowing redirects 'connect_timeout' => 10, // # seconds to wait
curl_setopt($curlh, CURLOPT_CONNECTTIMEOUT, 10); // # seconds to wait 'max_redirs' => $redirs, // # max number of http redirections to follow
curl_setopt($curlh, CURLOPT_MAXREDIRS, $redirs); // # max number of http redirections to follow 'follow_redirects' => true, // Follow redirects
curl_setopt($curlh, CURLOPT_USERAGENT, USER_AGENT); 'store_body' => false, // We won't need body content here.
curl_setopt($curlh, CURLOPT_FOLLOWLOCATION, true); // Follow redirects ));
curl_setopt($curlh, CURLOPT_RETURNTRANSFER, true); return $request;
curl_setopt($curlh, CURLOPT_FILETIME, true);
curl_setopt($curlh, CURLOPT_HEADER, true); // Include header in output
return $curlh;
} }
function _redirectWhere_imp($short_url, $redirs = 10, $protected = false) { function _redirectWhere_imp($short_url, $redirs = 10, $protected = false) {
@ -82,32 +79,39 @@ class File_redirection extends Memcached_DataObject
if(strpos($short_url,'://') === false){ if(strpos($short_url,'://') === false){
return $short_url; return $short_url;
} }
$curlh = File_redirection::_commonCurl($short_url, $redirs); try {
// Don't include body in output $request = self::_commonHttp($short_url, $redirs);
curl_setopt($curlh, CURLOPT_NOBODY, true); // Don't include body in output
curl_exec($curlh); $request->setMethod(HTTP_Request2::METHOD_HEAD);
$info = curl_getinfo($curlh); $response = $request->send();
curl_close($curlh);
if (405 == $info['http_code']) { if (405 == $response->getStatus()) {
$curlh = File_redirection::_commonCurl($short_url, $redirs); // Server doesn't support HEAD method? Can this really happen?
curl_exec($curlh); // We'll try again as a GET and ignore the response data.
$info = curl_getinfo($curlh); $request = self::_commonHttp($short_url, $redirs);
curl_close($curlh); $response = $request->send();
}
} catch (Exception $e) {
// Invalid URL or failure to reach server
return $short_url;
} }
if (!empty($info['redirect_count']) && File::isProtected($info['url'])) { if ($response->getRedirectCount() && File::isProtected($response->getUrl())) {
return File_redirection::_redirectWhere_imp($short_url, $info['redirect_count'] - 1, true); // Bump back up the redirect chain until we find a non-protected URL
return self::_redirectWhere_imp($short_url, $response->getRedirectCount() - 1, true);
} }
$ret = array('code' => $info['http_code'] $ret = array('code' => $response->getStatus()
, 'redirects' => $info['redirect_count'] , 'redirects' => $response->getRedirectCount()
, 'url' => $info['url']); , 'url' => $response->getUrl());
if (!empty($info['content_type'])) $ret['type'] = $info['content_type']; $type = $response->getHeader('Content-Type');
if ($type) $ret['type'] = $type;
if ($protected) $ret['protected'] = true; if ($protected) $ret['protected'] = true;
if (!empty($info['download_content_length'])) $ret['size'] = $info['download_content_length']; $size = $response->getHeader('Content-Length'); // @fixme bytes?
if (isset($info['filetime']) && ($info['filetime'] > 0)) $ret['time'] = $info['filetime']; if ($size) $ret['size'] = $size;
$time = $response->getHeader('Last-Modified');
if ($time) $ret['time'] = strtotime($time);
return $ret; return $ret;
} }

42
classes/Login_token.php Normal file
View File

@ -0,0 +1,42 @@
<?php
/**
* Table Definition for group_alias
*
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2009, 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/>.
*/
if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
class Login_token extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
public $__table = 'login_token'; // table name
public $user_id; // int(4) primary_key not_null
public $token; // char(32) not_null
public $created; // datetime() not_null
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Login_token',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
}

View File

@ -930,7 +930,10 @@ class Notice extends Memcached_DataObject
$users = $group->getUserMembers(); $users = $group->getUserMembers();
foreach ($users as $id) { foreach ($users as $id) {
if (!array_key_exists($id, $ni)) { if (!array_key_exists($id, $ni)) {
$ni[$id] = NOTICE_INBOX_SOURCE_GROUP; $user = User::staticGet('id', $id);
if (!$user->hasBlocked($notice->profile_id)) {
$ni[$id] = NOTICE_INBOX_SOURCE_GROUP;
}
} }
} }
} }

View File

@ -117,8 +117,7 @@ class User extends Memcached_DataObject
function allowed_nickname($nickname) function allowed_nickname($nickname)
{ {
// XXX: should already be validated for size, content, etc. // XXX: should already be validated for size, content, etc.
$blacklist = common_config('nickname', 'blacklist');
$blacklist = array();
//all directory and file names should be blacklisted //all directory and file names should be blacklisted
$d = dir(INSTALLDIR); $d = dir(INSTALLDIR);
@ -126,8 +125,15 @@ class User extends Memcached_DataObject
$blacklist[]=$entry; $blacklist[]=$entry;
} }
$d->close(); $d->close();
$merged = array_merge($blacklist, common_config('nickname', 'blacklist'));
return !in_array($nickname, $merged); //all top level names in the router should be blacklisted
$router = Router::get();
foreach(array_keys($router->m->getPaths()) as $path){
if(preg_match('/^\/(.*?)[\/\?]/',$path,$matches)){
$blacklist[]=$matches[1];
}
}
return !in_array($nickname, $blacklist);
} }
function getCurrentNotice($dt=null) function getCurrentNotice($dt=null)

View File

@ -555,3 +555,14 @@ created = 142
[user_role__keys] [user_role__keys]
user_id = K user_id = K
role = K role = K
[login_token]
user_id = 129
token = 130
created = 142
modified = 384
[login_token__keys]
user_id = K
token = K

View File

@ -32,3 +32,13 @@ create table user_role (
constraint primary key (user_id, role) constraint primary key (user_id, role)
) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin; ) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;
create table login_token (
user_id integer not null comment 'user owning this token' references user (id),
token char(32) not null comment 'token useable for logging in',
created datetime not null comment 'date this record was created',
modified timestamp comment 'date this record was modified',
constraint primary key (user_id)
) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;

View File

@ -38,3 +38,13 @@ create table user_role (
primary key (user_id, role) primary key (user_id, role)
); );
create table login_token (
user_id integer not null /* comment 'user owning this token'*/ references user (id),
token char(32) not null /* comment 'token useable for logging in'*/,
created timestamp not null DEFAULT CURRENT_TIMESTAMP /* comment 'date this record was created'*/,
modified timestamp /* comment 'date this record was modified'*/,
constraint primary key (user_id)
);

View File

@ -7,6 +7,7 @@ VALUES
('anyio', 'Any.IO', 'http://any.io/', now()), ('anyio', 'Any.IO', 'http://any.io/', now()),
('betwittered','BeTwittered','http://www.32hours.com/betwitteredinfo/', now()), ('betwittered','BeTwittered','http://www.32hours.com/betwitteredinfo/', now()),
('bti','bti','http://gregkh.github.com/bti/', now()), ('bti','bti','http://gregkh.github.com/bti/', now()),
('choqok', 'Choqok', 'http://choqok.gnufolks.org/', now()),
('cliqset', 'Cliqset', 'http://www.cliqset.com/', now()), ('cliqset', 'Cliqset', 'http://www.cliqset.com/', now()),
('deskbar','Deskbar-Applet','http://www.gnome.org/projects/deskbar-applet/', now()), ('deskbar','Deskbar-Applet','http://www.gnome.org/projects/deskbar-applet/', now()),
('Do','Gnome Do','http://do.davebsd.com/wiki/index.php?title=Microblog_Plugin', now()), ('Do','Gnome Do','http://do.davebsd.com/wiki/index.php?title=Microblog_Plugin', now()),
@ -43,6 +44,7 @@ VALUES
('rygh.no','rygh.no','http://rygh.no/', now()), ('rygh.no','rygh.no','http://rygh.no/', now()),
('ryghsms','ryghsms','http://sms.rygh.no/', now()), ('ryghsms','ryghsms','http://sms.rygh.no/', now()),
('smob','SMOB','http://smob.sioc-project.org/', now()), ('smob','SMOB','http://smob.sioc-project.org/', now()),
('socialoomphBfD4pMqz31', 'SocialOomph', 'http://www.socialoomph.com/', now()),
('spaz','Spaz','http://funkatron.com/spaz', now()), ('spaz','Spaz','http://funkatron.com/spaz', now()),
('tarpipe','tarpipe','http://tarpipe.com/', now()), ('tarpipe','tarpipe','http://tarpipe.com/', now()),
('tjunar','Tjunar','http://nederflash.nl/boek/titels/tjunar-air', now()), ('tjunar','Tjunar','http://nederflash.nl/boek/titels/tjunar-air', now()),

View File

@ -575,3 +575,13 @@ create table location_namespace (
modified timestamp comment 'date this record was modified' modified timestamp comment 'date this record was modified'
) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin; ) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;
create table login_token (
user_id integer not null comment 'user owning this token' references user (id),
token char(32) not null comment 'token useable for logging in',
created datetime not null comment 'date this record was created',
modified timestamp comment 'date this record was modified',
constraint primary key (user_id)
) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;

View File

@ -569,3 +569,13 @@ create table user_role (
primary key (user_id, role) primary key (user_id, role)
); );
create table login_token (
user_id integer not null /* comment 'user owning this token'*/ references user (id),
token char(32) not null /* comment 'token useable for logging in'*/,
created timestamp not null DEFAULT CURRENT_TIMESTAMP /* comment 'date this record was created'*/,
modified timestamp /* comment 'date this record was modified'*/,
constraint primary key (user_id)
);

View File

@ -2,4 +2,4 @@ A bookmarklet is a small piece of javascript code used as a bookmark. This one w
Drag-and-drop the following link to your bookmarks bar or right-click it and add it to your browser favorites to keep it handy. Drag-and-drop the following link to your bookmarks bar or right-click it and add it to your browser favorites to keep it handy.
<a href="javascript:(function(){var%20d=document,w=window,e=w.getSelection,k=d.getSelection,x=d.selection,s=(e?e():(k)?k():(x?x.createRange().text:0)),f='http://%%site.server%%/%%site.path%%/index.php?action=bookmarklet',l=d.location,e=encodeURIComponent,g=f+'&status_textarea=%22'+((e(s))?e(s):e(document.title))+'%22%20%E2%80%94%20'+l.href;function%20a(){if(!w.open(g,'t','toolbar=0,resizable=0,scrollbars=1,status=1,width=450,height=200')){l.href=g;}}a();})()">Post to %%site.name%%</a> <a href="javascript:(function(){var%20d=document,w=window,e=w.getSelection,k=d.getSelection,x=d.selection,s=(e?e():(k)?k():(x?x.createRange().text:0)),f='http://%%site.server%%/%%site.path%%/index.php?action=bookmarklet',l=d.location,e=encodeURIComponent,g=f+'&status_textarea=%E2%80%9C'+((e(s))?e(s):e(document.title))+'%E2%80%9D%20%E2%80%94%20'+l.href;function%20a(){if(!w.open(g,'t','toolbar=0,resizable=0,scrollbars=1,status=1,width=450,height=200')){l.href=g;}}a();})()">Post to %%site.name%%</a>

844
extlib/HTTP/Request2.php Normal file
View File

@ -0,0 +1,844 @@
<?php
/**
* Class representing a HTTP request
*
* PHP version 5
*
* LICENSE:
*
* Copyright (c) 2008, 2009, Alexey Borzov <avb@php.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The names of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @category HTTP
* @package HTTP_Request2
* @author Alexey Borzov <avb@php.net>
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id: Request2.php 278226 2009-04-03 21:32:48Z avb $
* @link http://pear.php.net/package/HTTP_Request2
*/
/**
* A class representing an URL as per RFC 3986.
*/
require_once 'Net/URL2.php';
/**
* Exception class for HTTP_Request2 package
*/
require_once 'HTTP/Request2/Exception.php';
/**
* Class representing a HTTP request
*
* @category HTTP
* @package HTTP_Request2
* @author Alexey Borzov <avb@php.net>
* @version Release: 0.4.1
* @link http://tools.ietf.org/html/rfc2616#section-5
*/
class HTTP_Request2 implements SplSubject
{
/**#@+
* Constants for HTTP request methods
*
* @link http://tools.ietf.org/html/rfc2616#section-5.1.1
*/
const METHOD_OPTIONS = 'OPTIONS';
const METHOD_GET = 'GET';
const METHOD_HEAD = 'HEAD';
const METHOD_POST = 'POST';
const METHOD_PUT = 'PUT';
const METHOD_DELETE = 'DELETE';
const METHOD_TRACE = 'TRACE';
const METHOD_CONNECT = 'CONNECT';
/**#@-*/
/**#@+
* Constants for HTTP authentication schemes
*
* @link http://tools.ietf.org/html/rfc2617
*/
const AUTH_BASIC = 'basic';
const AUTH_DIGEST = 'digest';
/**#@-*/
/**
* Regular expression used to check for invalid symbols in RFC 2616 tokens
* @link http://pear.php.net/bugs/bug.php?id=15630
*/
const REGEXP_INVALID_TOKEN = '![\x00-\x1f\x7f-\xff()<>@,;:\\\\"/\[\]?={}\s]!';
/**
* Regular expression used to check for invalid symbols in cookie strings
* @link http://pear.php.net/bugs/bug.php?id=15630
* @link http://cgi.netscape.com/newsref/std/cookie_spec.html
*/
const REGEXP_INVALID_COOKIE = '/[\s,;]/';
/**
* Fileinfo magic database resource
* @var resource
* @see detectMimeType()
*/
private static $_fileinfoDb;
/**
* Observers attached to the request (instances of SplObserver)
* @var array
*/
protected $observers = array();
/**
* Request URL
* @var Net_URL2
*/
protected $url;
/**
* Request method
* @var string
*/
protected $method = self::METHOD_GET;
/**
* Authentication data
* @var array
* @see getAuth()
*/
protected $auth;
/**
* Request headers
* @var array
*/
protected $headers = array();
/**
* Configuration parameters
* @var array
* @see setConfig()
*/
protected $config = array(
'adapter' => 'HTTP_Request2_Adapter_Socket',
'connect_timeout' => 10,
'timeout' => 0,
'use_brackets' => true,
'protocol_version' => '1.1',
'buffer_size' => 16384,
'store_body' => true,
'proxy_host' => '',
'proxy_port' => '',
'proxy_user' => '',
'proxy_password' => '',
'proxy_auth_scheme' => self::AUTH_BASIC,
'ssl_verify_peer' => true,
'ssl_verify_host' => true,
'ssl_cafile' => null,
'ssl_capath' => null,
'ssl_local_cert' => null,
'ssl_passphrase' => null,
'digest_compat_ie' => false
);
/**
* Last event in request / response handling, intended for observers
* @var array
* @see getLastEvent()
*/
protected $lastEvent = array(
'name' => 'start',
'data' => null
);
/**
* Request body
* @var string|resource
* @see setBody()
*/
protected $body = '';
/**
* Array of POST parameters
* @var array
*/
protected $postParams = array();
/**
* Array of file uploads (for multipart/form-data POST requests)
* @var array
*/
protected $uploads = array();
/**
* Adapter used to perform actual HTTP request
* @var HTTP_Request2_Adapter
*/
protected $adapter;
/**
* Constructor. Can set request URL, method and configuration array.
*
* Also sets a default value for User-Agent header.
*
* @param string|Net_Url2 Request URL
* @param string Request method
* @param array Configuration for this Request instance
*/
public function __construct($url = null, $method = self::METHOD_GET, array $config = array())
{
if (!empty($url)) {
$this->setUrl($url);
}
if (!empty($method)) {
$this->setMethod($method);
}
$this->setConfig($config);
$this->setHeader('user-agent', 'HTTP_Request2/0.4.1 ' .
'(http://pear.php.net/package/http_request2) ' .
'PHP/' . phpversion());
}
/**
* Sets the URL for this request
*
* If the URL has userinfo part (username & password) these will be removed
* and converted to auth data. If the URL does not have a path component,
* that will be set to '/'.
*
* @param string|Net_URL2 Request URL
* @return HTTP_Request2
* @throws HTTP_Request2_Exception
*/
public function setUrl($url)
{
if (is_string($url)) {
$url = new Net_URL2($url);
}
if (!$url instanceof Net_URL2) {
throw new HTTP_Request2_Exception('Parameter is not a valid HTTP URL');
}
// URL contains username / password?
if ($url->getUserinfo()) {
$username = $url->getUser();
$password = $url->getPassword();
$this->setAuth(rawurldecode($username), $password? rawurldecode($password): '');
$url->setUserinfo('');
}
if ('' == $url->getPath()) {
$url->setPath('/');
}
$this->url = $url;
return $this;
}
/**
* Returns the request URL
*
* @return Net_URL2
*/
public function getUrl()
{
return $this->url;
}
/**
* Sets the request method
*
* @param string
* @return HTTP_Request2
* @throws HTTP_Request2_Exception if the method name is invalid
*/
public function setMethod($method)
{
// Method name should be a token: http://tools.ietf.org/html/rfc2616#section-5.1.1
if (preg_match(self::REGEXP_INVALID_TOKEN, $method)) {
throw new HTTP_Request2_Exception("Invalid request method '{$method}'");
}
$this->method = $method;
return $this;
}
/**
* Returns the request method
*
* @return string
*/
public function getMethod()
{
return $this->method;
}
/**
* Sets the configuration parameter(s)
*
* The following parameters are available:
* <ul>
* <li> 'adapter' - adapter to use (string)</li>
* <li> 'connect_timeout' - Connection timeout in seconds (integer)</li>
* <li> 'timeout' - Total number of seconds a request can take.
* Use 0 for no limit, should be greater than
* 'connect_timeout' if set (integer)</li>
* <li> 'use_brackets' - Whether to append [] to array variable names (bool)</li>
* <li> 'protocol_version' - HTTP Version to use, '1.0' or '1.1' (string)</li>
* <li> 'buffer_size' - Buffer size to use for reading and writing (int)</li>
* <li> 'store_body' - Whether to store response body in response object.
* Set to false if receiving a huge response and
* using an Observer to save it (boolean)</li>
* <li> 'proxy_host' - Proxy server host (string)</li>
* <li> 'proxy_port' - Proxy server port (integer)</li>
* <li> 'proxy_user' - Proxy auth username (string)</li>
* <li> 'proxy_password' - Proxy auth password (string)</li>
* <li> 'proxy_auth_scheme' - Proxy auth scheme, one of HTTP_Request2::AUTH_* constants (string)</li>
* <li> 'ssl_verify_peer' - Whether to verify peer's SSL certificate (bool)</li>
* <li> 'ssl_verify_host' - Whether to check that Common Name in SSL
* certificate matches host name (bool)</li>
* <li> 'ssl_cafile' - Cerificate Authority file to verify the peer
* with (use with 'ssl_verify_peer') (string)</li>
* <li> 'ssl_capath' - Directory holding multiple Certificate
* Authority files (string)</li>
* <li> 'ssl_local_cert' - Name of a file containing local cerificate (string)</li>
* <li> 'ssl_passphrase' - Passphrase with which local certificate
* was encoded (string)</li>
* <li> 'digest_compat_ie' - Whether to imitate behaviour of MSIE 5 and 6
* in using URL without query string in digest
* authentication (boolean)</li>
* </ul>
*
* @param string|array configuration parameter name or array
* ('parameter name' => 'parameter value')
* @param mixed parameter value if $nameOrConfig is not an array
* @return HTTP_Request2
* @throws HTTP_Request2_Exception If the parameter is unknown
*/
public function setConfig($nameOrConfig, $value = null)
{
if (is_array($nameOrConfig)) {
foreach ($nameOrConfig as $name => $value) {
$this->setConfig($name, $value);
}
} else {
if (!array_key_exists($nameOrConfig, $this->config)) {
throw new HTTP_Request2_Exception(
"Unknown configuration parameter '{$nameOrConfig}'"
);
}
$this->config[$nameOrConfig] = $value;
}
return $this;
}
/**
* Returns the value(s) of the configuration parameter(s)
*
* @param string parameter name
* @return mixed value of $name parameter, array of all configuration
* parameters if $name is not given
* @throws HTTP_Request2_Exception If the parameter is unknown
*/
public function getConfig($name = null)
{
if (null === $name) {
return $this->config;
} elseif (!array_key_exists($name, $this->config)) {
throw new HTTP_Request2_Exception(
"Unknown configuration parameter '{$name}'"
);
}
return $this->config[$name];
}
/**
* Sets the autentification data
*
* @param string user name
* @param string password
* @param string authentication scheme
* @return HTTP_Request2
*/
public function setAuth($user, $password = '', $scheme = self::AUTH_BASIC)
{
if (empty($user)) {
$this->auth = null;
} else {
$this->auth = array(
'user' => (string)$user,
'password' => (string)$password,
'scheme' => $scheme
);
}
return $this;
}
/**
* Returns the authentication data
*
* The array has the keys 'user', 'password' and 'scheme', where 'scheme'
* is one of the HTTP_Request2::AUTH_* constants.
*
* @return array
*/
public function getAuth()
{
return $this->auth;
}
/**
* Sets request header(s)
*
* The first parameter may be either a full header string 'header: value' or
* header name. In the former case $value parameter is ignored, in the latter
* the header's value will either be set to $value or the header will be
* removed if $value is null. The first parameter can also be an array of
* headers, in that case method will be called recursively.
*
* Note that headers are treated case insensitively as per RFC 2616.
*
* <code>
* $req->setHeader('Foo: Bar'); // sets the value of 'Foo' header to 'Bar'
* $req->setHeader('FoO', 'Baz'); // sets the value of 'Foo' header to 'Baz'
* $req->setHeader(array('foo' => 'Quux')); // sets the value of 'Foo' header to 'Quux'
* $req->setHeader('FOO'); // removes 'Foo' header from request
* </code>
*
* @param string|array header name, header string ('Header: value')
* or an array of headers
* @param string|null header value, header will be removed if null
* @return HTTP_Request2
* @throws HTTP_Request2_Exception
*/
public function setHeader($name, $value = null)
{
if (is_array($name)) {
foreach ($name as $k => $v) {
if (is_string($k)) {
$this->setHeader($k, $v);
} else {
$this->setHeader($v);
}
}
} else {
if (null === $value && strpos($name, ':')) {
list($name, $value) = array_map('trim', explode(':', $name, 2));
}
// Header name should be a token: http://tools.ietf.org/html/rfc2616#section-4.2
if (preg_match(self::REGEXP_INVALID_TOKEN, $name)) {
throw new HTTP_Request2_Exception("Invalid header name '{$name}'");
}
// Header names are case insensitive anyway
$name = strtolower($name);
if (null === $value) {
unset($this->headers[$name]);
} else {
$this->headers[$name] = $value;
}
}
return $this;
}
/**
* Returns the request headers
*
* The array is of the form ('header name' => 'header value'), header names
* are lowercased
*
* @return array
*/
public function getHeaders()
{
return $this->headers;
}
/**
* Appends a cookie to "Cookie:" header
*
* @param string cookie name
* @param string cookie value
* @return HTTP_Request2
* @throws HTTP_Request2_Exception
*/
public function addCookie($name, $value)
{
$cookie = $name . '=' . $value;
if (preg_match(self::REGEXP_INVALID_COOKIE, $cookie)) {
throw new HTTP_Request2_Exception("Invalid cookie: '{$cookie}'");
}
$cookies = empty($this->headers['cookie'])? '': $this->headers['cookie'] . '; ';
$this->setHeader('cookie', $cookies . $cookie);
return $this;
}
/**
* Sets the request body
*
* @param string Either a string with the body or filename containing body
* @param bool Whether first parameter is a filename
* @return HTTP_Request2
* @throws HTTP_Request2_Exception
*/
public function setBody($body, $isFilename = false)
{
if (!$isFilename) {
$this->body = (string)$body;
} else {
if (!($fp = @fopen($body, 'rb'))) {
throw new HTTP_Request2_Exception("Cannot open file {$body}");
}
$this->body = $fp;
if (empty($this->headers['content-type'])) {
$this->setHeader('content-type', self::detectMimeType($body));
}
}
return $this;
}
/**
* Returns the request body
*
* @return string|resource|HTTP_Request2_MultipartBody
*/
public function getBody()
{
if (self::METHOD_POST == $this->method &&
(!empty($this->postParams) || !empty($this->uploads))
) {
if ('application/x-www-form-urlencoded' == $this->headers['content-type']) {
$body = http_build_query($this->postParams, '', '&');
if (!$this->getConfig('use_brackets')) {
$body = preg_replace('/%5B\d+%5D=/', '=', $body);
}
// support RFC 3986 by not encoding '~' symbol (request #15368)
return str_replace('%7E', '~', $body);
} elseif ('multipart/form-data' == $this->headers['content-type']) {
require_once 'HTTP/Request2/MultipartBody.php';
return new HTTP_Request2_MultipartBody(
$this->postParams, $this->uploads, $this->getConfig('use_brackets')
);
}
}
return $this->body;
}
/**
* Adds a file to form-based file upload
*
* Used to emulate file upload via a HTML form. The method also sets
* Content-Type of HTTP request to 'multipart/form-data'.
*
* If you just want to send the contents of a file as the body of HTTP
* request you should use setBody() method.
*
* @param string name of file-upload field
* @param mixed full name of local file
* @param string filename to send in the request
* @param string content-type of file being uploaded
* @return HTTP_Request2
* @throws HTTP_Request2_Exception
*/
public function addUpload($fieldName, $filename, $sendFilename = null,
$contentType = null)
{
if (!is_array($filename)) {
if (!($fp = @fopen($filename, 'rb'))) {
throw new HTTP_Request2_Exception("Cannot open file {$filename}");
}
$this->uploads[$fieldName] = array(
'fp' => $fp,
'filename' => empty($sendFilename)? basename($filename): $sendFilename,
'size' => filesize($filename),
'type' => empty($contentType)? self::detectMimeType($filename): $contentType
);
} else {
$fps = $names = $sizes = $types = array();
foreach ($filename as $f) {
if (!is_array($f)) {
$f = array($f);
}
if (!($fp = @fopen($f[0], 'rb'))) {
throw new HTTP_Request2_Exception("Cannot open file {$f[0]}");
}
$fps[] = $fp;
$names[] = empty($f[1])? basename($f[0]): $f[1];
$sizes[] = filesize($f[0]);
$types[] = empty($f[2])? self::detectMimeType($f[0]): $f[2];
}
$this->uploads[$fieldName] = array(
'fp' => $fps, 'filename' => $names, 'size' => $sizes, 'type' => $types
);
}
if (empty($this->headers['content-type']) ||
'application/x-www-form-urlencoded' == $this->headers['content-type']
) {
$this->setHeader('content-type', 'multipart/form-data');
}
return $this;
}
/**
* Adds POST parameter(s) to the request.
*
* @param string|array parameter name or array ('name' => 'value')
* @param mixed parameter value (can be an array)
* @return HTTP_Request2
*/
public function addPostParameter($name, $value = null)
{
if (!is_array($name)) {
$this->postParams[$name] = $value;
} else {
foreach ($name as $k => $v) {
$this->addPostParameter($k, $v);
}
}
if (empty($this->headers['content-type'])) {
$this->setHeader('content-type', 'application/x-www-form-urlencoded');
}
return $this;
}
/**
* Attaches a new observer
*
* @param SplObserver
*/
public function attach(SplObserver $observer)
{
foreach ($this->observers as $attached) {
if ($attached === $observer) {
return;
}
}
$this->observers[] = $observer;
}
/**
* Detaches an existing observer
*
* @param SplObserver
*/
public function detach(SplObserver $observer)
{
foreach ($this->observers as $key => $attached) {
if ($attached === $observer) {
unset($this->observers[$key]);
return;
}
}
}
/**
* Notifies all observers
*/
public function notify()
{
foreach ($this->observers as $observer) {
$observer->update($this);
}
}
/**
* Sets the last event
*
* Adapters should use this method to set the current state of the request
* and notify the observers.
*
* @param string event name
* @param mixed event data
*/
public function setLastEvent($name, $data = null)
{
$this->lastEvent = array(
'name' => $name,
'data' => $data
);
$this->notify();
}
/**
* Returns the last event
*
* Observers should use this method to access the last change in request.
* The following event names are possible:
* <ul>
* <li>'connect' - after connection to remote server,
* data is the destination (string)</li>
* <li>'disconnect' - after disconnection from server</li>
* <li>'sentHeaders' - after sending the request headers,
* data is the headers sent (string)</li>
* <li>'sentBodyPart' - after sending a part of the request body,
* data is the length of that part (int)</li>
* <li>'receivedHeaders' - after receiving the response headers,
* data is HTTP_Request2_Response object</li>
* <li>'receivedBodyPart' - after receiving a part of the response
* body, data is that part (string)</li>
* <li>'receivedEncodedBodyPart' - as 'receivedBodyPart', but data is still
* encoded by Content-Encoding</li>
* <li>'receivedBody' - after receiving the complete response
* body, data is HTTP_Request2_Response object</li>
* </ul>
* Different adapters may not send all the event types. Mock adapter does
* not send any events to the observers.
*
* @return array The array has two keys: 'name' and 'data'
*/
public function getLastEvent()
{
return $this->lastEvent;
}
/**
* Sets the adapter used to actually perform the request
*
* You can pass either an instance of a class implementing HTTP_Request2_Adapter
* or a class name. The method will only try to include a file if the class
* name starts with HTTP_Request2_Adapter_, it will also try to prepend this
* prefix to the class name if it doesn't contain any underscores, so that
* <code>
* $request->setAdapter('curl');
* </code>
* will work.
*
* @param string|HTTP_Request2_Adapter
* @return HTTP_Request2
* @throws HTTP_Request2_Exception
*/
public function setAdapter($adapter)
{
if (is_string($adapter)) {
if (!class_exists($adapter, false)) {
if (false === strpos($adapter, '_')) {
$adapter = 'HTTP_Request2_Adapter_' . ucfirst($adapter);
}
if (preg_match('/^HTTP_Request2_Adapter_([a-zA-Z0-9]+)$/', $adapter)) {
include_once str_replace('_', DIRECTORY_SEPARATOR, $adapter) . '.php';
}
if (!class_exists($adapter, false)) {
throw new HTTP_Request2_Exception("Class {$adapter} not found");
}
}
$adapter = new $adapter;
}
if (!$adapter instanceof HTTP_Request2_Adapter) {
throw new HTTP_Request2_Exception('Parameter is not a HTTP request adapter');
}
$this->adapter = $adapter;
return $this;
}
/**
* Sends the request and returns the response
*
* @throws HTTP_Request2_Exception
* @return HTTP_Request2_Response
*/
public function send()
{
// Sanity check for URL
if (!$this->url instanceof Net_URL2) {
throw new HTTP_Request2_Exception('No URL given');
} elseif (!$this->url->isAbsolute()) {
throw new HTTP_Request2_Exception('Absolute URL required');
} elseif (!in_array(strtolower($this->url->getScheme()), array('https', 'http'))) {
throw new HTTP_Request2_Exception('Not a HTTP URL');
}
if (empty($this->adapter)) {
$this->setAdapter($this->getConfig('adapter'));
}
// magic_quotes_runtime may break file uploads and chunked response
// processing; see bug #4543
if ($magicQuotes = ini_get('magic_quotes_runtime')) {
ini_set('magic_quotes_runtime', false);
}
// force using single byte encoding if mbstring extension overloads
// strlen() and substr(); see bug #1781, bug #10605
if (extension_loaded('mbstring') && (2 & ini_get('mbstring.func_overload'))) {
$oldEncoding = mb_internal_encoding();
mb_internal_encoding('iso-8859-1');
}
try {
$response = $this->adapter->sendRequest($this);
} catch (Exception $e) {
}
// cleanup in either case (poor man's "finally" clause)
if ($magicQuotes) {
ini_set('magic_quotes_runtime', true);
}
if (!empty($oldEncoding)) {
mb_internal_encoding($oldEncoding);
}
// rethrow the exception
if (!empty($e)) {
throw $e;
}
return $response;
}
/**
* Tries to detect MIME type of a file
*
* The method will try to use fileinfo extension if it is available,
* deprecated mime_content_type() function in the other case. If neither
* works, default 'application/octet-stream' MIME type is returned
*
* @param string filename
* @return string file MIME type
*/
protected static function detectMimeType($filename)
{
// finfo extension from PECL available
if (function_exists('finfo_open')) {
if (!isset(self::$_fileinfoDb)) {
self::$_fileinfoDb = @finfo_open(FILEINFO_MIME);
}
if (self::$_fileinfoDb) {
$info = finfo_file(self::$_fileinfoDb, $filename);
}
}
// (deprecated) mime_content_type function available
if (empty($info) && function_exists('mime_content_type')) {
return mime_content_type($filename);
}
return empty($info)? 'application/octet-stream': $info;
}
}
?>

View File

@ -0,0 +1,152 @@
<?php
/**
* Base class for HTTP_Request2 adapters
*
* PHP version 5
*
* LICENSE:
*
* Copyright (c) 2008, 2009, Alexey Borzov <avb@php.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The names of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @category HTTP
* @package HTTP_Request2
* @author Alexey Borzov <avb@php.net>
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id: Adapter.php 274684 2009-01-26 23:07:27Z avb $
* @link http://pear.php.net/package/HTTP_Request2
*/
/**
* Class representing a HTTP response
*/
require_once 'HTTP/Request2/Response.php';
/**
* Base class for HTTP_Request2 adapters
*
* HTTP_Request2 class itself only defines methods for aggregating the request
* data, all actual work of sending the request to the remote server and
* receiving its response is performed by adapters.
*
* @category HTTP
* @package HTTP_Request2
* @author Alexey Borzov <avb@php.net>
* @version Release: 0.4.1
*/
abstract class HTTP_Request2_Adapter
{
/**
* A list of methods that MUST NOT have a request body, per RFC 2616
* @var array
*/
protected static $bodyDisallowed = array('TRACE');
/**
* Methods having defined semantics for request body
*
* Content-Length header (indicating that the body follows, section 4.3 of
* RFC 2616) will be sent for these methods even if no body was added
*
* @var array
* @link http://pear.php.net/bugs/bug.php?id=12900
* @link http://pear.php.net/bugs/bug.php?id=14740
*/
protected static $bodyRequired = array('POST', 'PUT');
/**
* Request being sent
* @var HTTP_Request2
*/
protected $request;
/**
* Request body
* @var string|resource|HTTP_Request2_MultipartBody
* @see HTTP_Request2::getBody()
*/
protected $requestBody;
/**
* Length of the request body
* @var integer
*/
protected $contentLength;
/**
* Sends request to the remote server and returns its response
*
* @param HTTP_Request2
* @return HTTP_Request2_Response
* @throws HTTP_Request2_Exception
*/
abstract public function sendRequest(HTTP_Request2 $request);
/**
* Calculates length of the request body, adds proper headers
*
* @param array associative array of request headers, this method will
* add proper 'Content-Length' and 'Content-Type' headers
* to this array (or remove them if not needed)
*/
protected function calculateRequestLength(&$headers)
{
$this->requestBody = $this->request->getBody();
if (is_string($this->requestBody)) {
$this->contentLength = strlen($this->requestBody);
} elseif (is_resource($this->requestBody)) {
$stat = fstat($this->requestBody);
$this->contentLength = $stat['size'];
rewind($this->requestBody);
} else {
$this->contentLength = $this->requestBody->getLength();
$headers['content-type'] = 'multipart/form-data; boundary=' .
$this->requestBody->getBoundary();
$this->requestBody->rewind();
}
if (in_array($this->request->getMethod(), self::$bodyDisallowed) ||
0 == $this->contentLength
) {
unset($headers['content-type']);
// No body: send a Content-Length header nonetheless (request #12900),
// but do that only for methods that require a body (bug #14740)
if (in_array($this->request->getMethod(), self::$bodyRequired)) {
$headers['content-length'] = 0;
} else {
unset($headers['content-length']);
}
} else {
if (empty($headers['content-type'])) {
$headers['content-type'] = 'application/x-www-form-urlencoded';
}
$headers['content-length'] = $this->contentLength;
}
}
}
?>

View File

@ -0,0 +1,383 @@
<?php
/**
* Adapter for HTTP_Request2 wrapping around cURL extension
*
* PHP version 5
*
* LICENSE:
*
* Copyright (c) 2008, 2009, Alexey Borzov <avb@php.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The names of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @category HTTP
* @package HTTP_Request2
* @author Alexey Borzov <avb@php.net>
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id: Curl.php 278226 2009-04-03 21:32:48Z avb $
* @link http://pear.php.net/package/HTTP_Request2
*/
/**
* Base class for HTTP_Request2 adapters
*/
require_once 'HTTP/Request2/Adapter.php';
/**
* Adapter for HTTP_Request2 wrapping around cURL extension
*
* @category HTTP
* @package HTTP_Request2
* @author Alexey Borzov <avb@php.net>
* @version Release: 0.4.1
*/
class HTTP_Request2_Adapter_Curl extends HTTP_Request2_Adapter
{
/**
* Mapping of header names to cURL options
* @var array
*/
protected static $headerMap = array(
'accept-encoding' => CURLOPT_ENCODING,
'cookie' => CURLOPT_COOKIE,
'referer' => CURLOPT_REFERER,
'user-agent' => CURLOPT_USERAGENT
);
/**
* Mapping of SSL context options to cURL options
* @var array
*/
protected static $sslContextMap = array(
'ssl_verify_peer' => CURLOPT_SSL_VERIFYPEER,
'ssl_cafile' => CURLOPT_CAINFO,
'ssl_capath' => CURLOPT_CAPATH,
'ssl_local_cert' => CURLOPT_SSLCERT,
'ssl_passphrase' => CURLOPT_SSLCERTPASSWD
);
/**
* Response being received
* @var HTTP_Request2_Response
*/
protected $response;
/**
* Whether 'sentHeaders' event was sent to observers
* @var boolean
*/
protected $eventSentHeaders = false;
/**
* Whether 'receivedHeaders' event was sent to observers
* @var boolean
*/
protected $eventReceivedHeaders = false;
/**
* Position within request body
* @var integer
* @see callbackReadBody()
*/
protected $position = 0;
/**
* Information about last transfer, as returned by curl_getinfo()
* @var array
*/
protected $lastInfo;
/**
* Sends request to the remote server and returns its response
*
* @param HTTP_Request2
* @return HTTP_Request2_Response
* @throws HTTP_Request2_Exception
*/
public function sendRequest(HTTP_Request2 $request)
{
if (!extension_loaded('curl')) {
throw new HTTP_Request2_Exception('cURL extension not available');
}
$this->request = $request;
$this->response = null;
$this->position = 0;
$this->eventSentHeaders = false;
$this->eventReceivedHeaders = false;
try {
if (false === curl_exec($ch = $this->createCurlHandle())) {
$errorMessage = 'Error sending request: #' . curl_errno($ch) .
' ' . curl_error($ch);
}
} catch (Exception $e) {
}
$this->lastInfo = curl_getinfo($ch);
curl_close($ch);
if (!empty($e)) {
throw $e;
} elseif (!empty($errorMessage)) {
throw new HTTP_Request2_Exception($errorMessage);
}
if (0 < $this->lastInfo['size_download']) {
$this->request->setLastEvent('receivedBody', $this->response);
}
return $this->response;
}
/**
* Returns information about last transfer
*
* @return array associative array as returned by curl_getinfo()
*/
public function getInfo()
{
return $this->lastInfo;
}
/**
* Creates a new cURL handle and populates it with data from the request
*
* @return resource a cURL handle, as created by curl_init()
* @throws HTTP_Request2_Exception
*/
protected function createCurlHandle()
{
$ch = curl_init();
curl_setopt_array($ch, array(
// setup callbacks
CURLOPT_READFUNCTION => array($this, 'callbackReadBody'),
CURLOPT_HEADERFUNCTION => array($this, 'callbackWriteHeader'),
CURLOPT_WRITEFUNCTION => array($this, 'callbackWriteBody'),
// disallow redirects
CURLOPT_FOLLOWLOCATION => false,
// buffer size
CURLOPT_BUFFERSIZE => $this->request->getConfig('buffer_size'),
// connection timeout
CURLOPT_CONNECTTIMEOUT => $this->request->getConfig('connect_timeout'),
// save full outgoing headers, in case someone is interested
CURLINFO_HEADER_OUT => true,
// request url
CURLOPT_URL => $this->request->getUrl()->getUrl()
));
// request timeout
if ($timeout = $this->request->getConfig('timeout')) {
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
}
// set HTTP version
switch ($this->request->getConfig('protocol_version')) {
case '1.0':
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
break;
case '1.1':
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
}
// set request method
switch ($this->request->getMethod()) {
case HTTP_Request2::METHOD_GET:
curl_setopt($ch, CURLOPT_HTTPGET, true);
break;
case HTTP_Request2::METHOD_POST:
curl_setopt($ch, CURLOPT_POST, true);
break;
default:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->request->getMethod());
}
// set proxy, if needed
if ($host = $this->request->getConfig('proxy_host')) {
if (!($port = $this->request->getConfig('proxy_port'))) {
throw new HTTP_Request2_Exception('Proxy port not provided');
}
curl_setopt($ch, CURLOPT_PROXY, $host . ':' . $port);
if ($user = $this->request->getConfig('proxy_user')) {
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $user . ':' .
$this->request->getConfig('proxy_password'));
switch ($this->request->getConfig('proxy_auth_scheme')) {
case HTTP_Request2::AUTH_BASIC:
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
break;
case HTTP_Request2::AUTH_DIGEST:
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_DIGEST);
}
}
}
// set authentication data
if ($auth = $this->request->getAuth()) {
curl_setopt($ch, CURLOPT_USERPWD, $auth['user'] . ':' . $auth['password']);
switch ($auth['scheme']) {
case HTTP_Request2::AUTH_BASIC:
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
break;
case HTTP_Request2::AUTH_DIGEST:
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
}
}
// set SSL options
if (0 == strcasecmp($this->request->getUrl()->getScheme(), 'https')) {
foreach ($this->request->getConfig() as $name => $value) {
if ('ssl_verify_host' == $name && null !== $value) {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $value? 2: 0);
} elseif (isset(self::$sslContextMap[$name]) && null !== $value) {
curl_setopt($ch, self::$sslContextMap[$name], $value);
}
}
}
$headers = $this->request->getHeaders();
// make cURL automagically send proper header
if (!isset($headers['accept-encoding'])) {
$headers['accept-encoding'] = '';
}
// set headers having special cURL keys
foreach (self::$headerMap as $name => $option) {
if (isset($headers[$name])) {
curl_setopt($ch, $option, $headers[$name]);
unset($headers[$name]);
}
}
$this->calculateRequestLength($headers);
// set headers not having special keys
$headersFmt = array();
foreach ($headers as $name => $value) {
$canonicalName = implode('-', array_map('ucfirst', explode('-', $name)));
$headersFmt[] = $canonicalName . ': ' . $value;
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headersFmt);
return $ch;
}
/**
* Callback function called by cURL for reading the request body
*
* @param resource cURL handle
* @param resource file descriptor (not used)
* @param integer maximum length of data to return
* @return string part of the request body, up to $length bytes
*/
protected function callbackReadBody($ch, $fd, $length)
{
if (!$this->eventSentHeaders) {
$this->request->setLastEvent(
'sentHeaders', curl_getinfo($ch, CURLINFO_HEADER_OUT)
);
$this->eventSentHeaders = true;
}
if (in_array($this->request->getMethod(), self::$bodyDisallowed) ||
0 == $this->contentLength || $this->position >= $this->contentLength
) {
return '';
}
if (is_string($this->requestBody)) {
$string = substr($this->requestBody, $this->position, $length);
} elseif (is_resource($this->requestBody)) {
$string = fread($this->requestBody, $length);
} else {
$string = $this->requestBody->read($length);
}
$this->request->setLastEvent('sentBodyPart', strlen($string));
$this->position += strlen($string);
return $string;
}
/**
* Callback function called by cURL for saving the response headers
*
* @param resource cURL handle
* @param string response header (with trailing CRLF)
* @return integer number of bytes saved
* @see HTTP_Request2_Response::parseHeaderLine()
*/
protected function callbackWriteHeader($ch, $string)
{
// we may receive a second set of headers if doing e.g. digest auth
if ($this->eventReceivedHeaders || !$this->eventSentHeaders) {
// don't bother with 100-Continue responses (bug #15785)
if (!$this->eventSentHeaders ||
$this->response->getStatus() >= 200
) {
$this->request->setLastEvent(
'sentHeaders', curl_getinfo($ch, CURLINFO_HEADER_OUT)
);
}
$this->eventSentHeaders = true;
// we'll need a new response object
if ($this->eventReceivedHeaders) {
$this->eventReceivedHeaders = false;
$this->response = null;
}
}
if (empty($this->response)) {
$this->response = new HTTP_Request2_Response($string, false);
} else {
$this->response->parseHeaderLine($string);
if ('' == trim($string)) {
// don't bother with 100-Continue responses (bug #15785)
if (200 <= $this->response->getStatus()) {
$this->request->setLastEvent('receivedHeaders', $this->response);
}
$this->eventReceivedHeaders = true;
}
}
return strlen($string);
}
/**
* Callback function called by cURL for saving the response body
*
* @param resource cURL handle (not used)
* @param string part of the response body
* @return integer number of bytes saved
* @see HTTP_Request2_Response::appendBody()
*/
protected function callbackWriteBody($ch, $string)
{
// cURL calls WRITEFUNCTION callback without calling HEADERFUNCTION if
// response doesn't start with proper HTTP status line (see bug #15716)
if (empty($this->response)) {
throw new HTTP_Request2_Exception("Malformed response: {$string}");
}
if ($this->request->getConfig('store_body')) {
$this->response->appendBody($string);
}
$this->request->setLastEvent('receivedBodyPart', $string);
return strlen($string);
}
}
?>

View File

@ -0,0 +1,171 @@
<?php
/**
* Mock adapter intended for testing
*
* PHP version 5
*
* LICENSE:
*
* Copyright (c) 2008, 2009, Alexey Borzov <avb@php.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The names of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @category HTTP
* @package HTTP_Request2
* @author Alexey Borzov <avb@php.net>
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id: Mock.php 274406 2009-01-23 18:01:57Z avb $
* @link http://pear.php.net/package/HTTP_Request2
*/
/**
* Base class for HTTP_Request2 adapters
*/
require_once 'HTTP/Request2/Adapter.php';
/**
* Mock adapter intended for testing
*
* Can be used to test applications depending on HTTP_Request2 package without
* actually performing any HTTP requests. This adapter will return responses
* previously added via addResponse()
* <code>
* $mock = new HTTP_Request2_Adapter_Mock();
* $mock->addResponse("HTTP/1.1 ... ");
*
* $request = new HTTP_Request2();
* $request->setAdapter($mock);
*
* // This will return the response set above
* $response = $req->send();
* </code>
*
* @category HTTP
* @package HTTP_Request2
* @author Alexey Borzov <avb@php.net>
* @version Release: 0.4.1
*/
class HTTP_Request2_Adapter_Mock extends HTTP_Request2_Adapter
{
/**
* A queue of responses to be returned by sendRequest()
* @var array
*/
protected $responses = array();
/**
* Returns the next response from the queue built by addResponse()
*
* If the queue is empty will return default empty response with status 400,
* if an Exception object was added to the queue it will be thrown.
*
* @param HTTP_Request2
* @return HTTP_Request2_Response
* @throws Exception
*/
public function sendRequest(HTTP_Request2 $request)
{
if (count($this->responses) > 0) {
$response = array_shift($this->responses);
if ($response instanceof HTTP_Request2_Response) {
return $response;
} else {
// rethrow the exception,
$class = get_class($response);
$message = $response->getMessage();
$code = $response->getCode();
throw new $class($message, $code);
}
} else {
return self::createResponseFromString("HTTP/1.1 400 Bad Request\r\n\r\n");
}
}
/**
* Adds response to the queue
*
* @param mixed either a string, a pointer to an open file,
* a HTTP_Request2_Response or Exception object
* @throws HTTP_Request2_Exception
*/
public function addResponse($response)
{
if (is_string($response)) {
$response = self::createResponseFromString($response);
} elseif (is_resource($response)) {
$response = self::createResponseFromFile($response);
} elseif (!$response instanceof HTTP_Request2_Response &&
!$response instanceof Exception
) {
throw new HTTP_Request2_Exception('Parameter is not a valid response');
}
$this->responses[] = $response;
}
/**
* Creates a new HTTP_Request2_Response object from a string
*
* @param string
* @return HTTP_Request2_Response
* @throws HTTP_Request2_Exception
*/
public static function createResponseFromString($str)
{
$parts = preg_split('!(\r?\n){2}!m', $str, 2);
$headerLines = explode("\n", $parts[0]);
$response = new HTTP_Request2_Response(array_shift($headerLines));
foreach ($headerLines as $headerLine) {
$response->parseHeaderLine($headerLine);
}
$response->parseHeaderLine('');
if (isset($parts[1])) {
$response->appendBody($parts[1]);
}
return $response;
}
/**
* Creates a new HTTP_Request2_Response object from a file
*
* @param resource file pointer returned by fopen()
* @return HTTP_Request2_Response
* @throws HTTP_Request2_Exception
*/
public static function createResponseFromFile($fp)
{
$response = new HTTP_Request2_Response(fgets($fp));
do {
$headerLine = fgets($fp);
$response->parseHeaderLine($headerLine);
} while ('' != trim($headerLine));
while (!feof($fp)) {
$response->appendBody(fread($fp, 8192));
}
return $response;
}
}
?>

View File

@ -0,0 +1,971 @@
<?php
/**
* Socket-based adapter for HTTP_Request2
*
* PHP version 5
*
* LICENSE:
*
* Copyright (c) 2008, 2009, Alexey Borzov <avb@php.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The names of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @category HTTP
* @package HTTP_Request2
* @author Alexey Borzov <avb@php.net>
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id: Socket.php 279760 2009-05-03 10:46:42Z avb $
* @link http://pear.php.net/package/HTTP_Request2
*/
/**
* Base class for HTTP_Request2 adapters
*/
require_once 'HTTP/Request2/Adapter.php';
/**
* Socket-based adapter for HTTP_Request2
*
* This adapter uses only PHP sockets and will work on almost any PHP
* environment. Code is based on original HTTP_Request PEAR package.
*
* @category HTTP
* @package HTTP_Request2
* @author Alexey Borzov <avb@php.net>
* @version Release: 0.4.1
*/
class HTTP_Request2_Adapter_Socket extends HTTP_Request2_Adapter
{
/**
* Regular expression for 'token' rule from RFC 2616
*/
const REGEXP_TOKEN = '[^\x00-\x1f\x7f-\xff()<>@,;:\\\\"/\[\]?={}\s]+';
/**
* Regular expression for 'quoted-string' rule from RFC 2616
*/
const REGEXP_QUOTED_STRING = '"(?:\\\\.|[^\\\\"])*"';
/**
* Connected sockets, needed for Keep-Alive support
* @var array
* @see connect()
*/
protected static $sockets = array();
/**
* Data for digest authentication scheme
*
* The keys for the array are URL prefixes.
*
* The values are associative arrays with data (realm, nonce, nonce-count,
* opaque...) needed for digest authentication. Stored here to prevent making
* duplicate requests to digest-protected resources after we have already
* received the challenge.
*
* @var array
*/
protected static $challenges = array();
/**
* Connected socket
* @var resource
* @see connect()
*/
protected $socket;
/**
* Challenge used for server digest authentication
* @var array
*/
protected $serverChallenge;
/**
* Challenge used for proxy digest authentication
* @var array
*/
protected $proxyChallenge;
/**
* Global timeout, exception will be raised if request continues past this time
* @var integer
*/
protected $timeout = null;
/**
* Remaining length of the current chunk, when reading chunked response
* @var integer
* @see readChunked()
*/
protected $chunkLength = 0;
/**
* Sends request to the remote server and returns its response
*
* @param HTTP_Request2
* @return HTTP_Request2_Response
* @throws HTTP_Request2_Exception
*/
public function sendRequest(HTTP_Request2 $request)
{
$this->request = $request;
$keepAlive = $this->connect();
$headers = $this->prepareHeaders();
// Use global request timeout if given, see feature requests #5735, #8964
if ($timeout = $request->getConfig('timeout')) {
$this->timeout = time() + $timeout;
} else {
$this->timeout = null;
}
try {
if (false === @fwrite($this->socket, $headers, strlen($headers))) {
throw new HTTP_Request2_Exception('Error writing request');
}
// provide request headers to the observer, see request #7633
$this->request->setLastEvent('sentHeaders', $headers);
$this->writeBody();
if ($this->timeout && time() > $this->timeout) {
throw new HTTP_Request2_Exception(
'Request timed out after ' .
$request->getConfig('timeout') . ' second(s)'
);
}
$response = $this->readResponse();
if (!$this->canKeepAlive($keepAlive, $response)) {
$this->disconnect();
}
if ($this->shouldUseProxyDigestAuth($response)) {
return $this->sendRequest($request);
}
if ($this->shouldUseServerDigestAuth($response)) {
return $this->sendRequest($request);
}
if ($authInfo = $response->getHeader('authentication-info')) {
$this->updateChallenge($this->serverChallenge, $authInfo);
}
if ($proxyInfo = $response->getHeader('proxy-authentication-info')) {
$this->updateChallenge($this->proxyChallenge, $proxyInfo);
}
} catch (Exception $e) {
$this->disconnect();
throw $e;
}
return $response;
}
/**
* Connects to the remote server
*
* @return bool whether the connection can be persistent
* @throws HTTP_Request2_Exception
*/
protected function connect()
{
$secure = 0 == strcasecmp($this->request->getUrl()->getScheme(), 'https');
$tunnel = HTTP_Request2::METHOD_CONNECT == $this->request->getMethod();
$headers = $this->request->getHeaders();
$reqHost = $this->request->getUrl()->getHost();
if (!($reqPort = $this->request->getUrl()->getPort())) {
$reqPort = $secure? 443: 80;
}
if ($host = $this->request->getConfig('proxy_host')) {
if (!($port = $this->request->getConfig('proxy_port'))) {
throw new HTTP_Request2_Exception('Proxy port not provided');
}
$proxy = true;
} else {
$host = $reqHost;
$port = $reqPort;
$proxy = false;
}
if ($tunnel && !$proxy) {
throw new HTTP_Request2_Exception(
"Trying to perform CONNECT request without proxy"
);
}
if ($secure && !in_array('ssl', stream_get_transports())) {
throw new HTTP_Request2_Exception(
'Need OpenSSL support for https:// requests'
);
}
// RFC 2068, section 19.7.1: A client MUST NOT send the Keep-Alive
// connection token to a proxy server...
if ($proxy && !$secure &&
!empty($headers['connection']) && 'Keep-Alive' == $headers['connection']
) {
$this->request->setHeader('connection');
}
$keepAlive = ('1.1' == $this->request->getConfig('protocol_version') &&
empty($headers['connection'])) ||
(!empty($headers['connection']) &&
'Keep-Alive' == $headers['connection']);
$host = ((!$secure || $proxy)? 'tcp://': 'ssl://') . $host;
$options = array();
if ($secure || $tunnel) {
foreach ($this->request->getConfig() as $name => $value) {
if ('ssl_' == substr($name, 0, 4) && null !== $value) {
if ('ssl_verify_host' == $name) {
if ($value) {
$options['CN_match'] = $reqHost;
}
} else {
$options[substr($name, 4)] = $value;
}
}
}
ksort($options);
}
// Changing SSL context options after connection is established does *not*
// work, we need a new connection if options change
$remote = $host . ':' . $port;
$socketKey = $remote . (($secure && $proxy)? "->{$reqHost}:{$reqPort}": '') .
(empty($options)? '': ':' . serialize($options));
unset($this->socket);
// We use persistent connections and have a connected socket?
// Ensure that the socket is still connected, see bug #16149
if ($keepAlive && !empty(self::$sockets[$socketKey]) &&
!feof(self::$sockets[$socketKey])
) {
$this->socket =& self::$sockets[$socketKey];
} elseif ($secure && $proxy && !$tunnel) {
$this->establishTunnel();
$this->request->setLastEvent(
'connect', "ssl://{$reqHost}:{$reqPort} via {$host}:{$port}"
);
self::$sockets[$socketKey] =& $this->socket;
} else {
// Set SSL context options if doing HTTPS request or creating a tunnel
$context = stream_context_create();
foreach ($options as $name => $value) {
if (!stream_context_set_option($context, 'ssl', $name, $value)) {
throw new HTTP_Request2_Exception(
"Error setting SSL context option '{$name}'"
);
}
}
$this->socket = @stream_socket_client(
$remote, $errno, $errstr,
$this->request->getConfig('connect_timeout'),
STREAM_CLIENT_CONNECT, $context
);
if (!$this->socket) {
throw new HTTP_Request2_Exception(
"Unable to connect to {$remote}. Error #{$errno}: {$errstr}"
);
}
$this->request->setLastEvent('connect', $remote);
self::$sockets[$socketKey] =& $this->socket;
}
return $keepAlive;
}
/**
* Establishes a tunnel to a secure remote server via HTTP CONNECT request
*
* This method will fail if 'ssl_verify_peer' is enabled. Probably because PHP
* sees that we are connected to a proxy server (duh!) rather than the server
* that presents its certificate.
*
* @link http://tools.ietf.org/html/rfc2817#section-5.2
* @throws HTTP_Request2_Exception
*/
protected function establishTunnel()
{
$donor = new self;
$connect = new HTTP_Request2(
$this->request->getUrl(), HTTP_Request2::METHOD_CONNECT,
array_merge($this->request->getConfig(),
array('adapter' => $donor))
);
$response = $connect->send();
// Need any successful (2XX) response
if (200 > $response->getStatus() || 300 <= $response->getStatus()) {
throw new HTTP_Request2_Exception(
'Failed to connect via HTTPS proxy. Proxy response: ' .
$response->getStatus() . ' ' . $response->getReasonPhrase()
);
}
$this->socket = $donor->socket;
$modes = array(
STREAM_CRYPTO_METHOD_TLS_CLIENT,
STREAM_CRYPTO_METHOD_SSLv3_CLIENT,
STREAM_CRYPTO_METHOD_SSLv23_CLIENT,
STREAM_CRYPTO_METHOD_SSLv2_CLIENT
);
foreach ($modes as $mode) {
if (stream_socket_enable_crypto($this->socket, true, $mode)) {
return;
}
}
throw new HTTP_Request2_Exception(
'Failed to enable secure connection when connecting through proxy'
);
}
/**
* Checks whether current connection may be reused or should be closed
*
* @param boolean whether connection could be persistent
* in the first place
* @param HTTP_Request2_Response response object to check
* @return boolean
*/
protected function canKeepAlive($requestKeepAlive, HTTP_Request2_Response $response)
{
// Do not close socket on successful CONNECT request
if (HTTP_Request2::METHOD_CONNECT == $this->request->getMethod() &&
200 <= $response->getStatus() && 300 > $response->getStatus()
) {
return true;
}
$lengthKnown = 'chunked' == strtolower($response->getHeader('transfer-encoding')) ||
null !== $response->getHeader('content-length');
$persistent = 'keep-alive' == strtolower($response->getHeader('connection')) ||
(null === $response->getHeader('connection') &&
'1.1' == $response->getVersion());
return $requestKeepAlive && $lengthKnown && $persistent;
}
/**
* Disconnects from the remote server
*/
protected function disconnect()
{
if (is_resource($this->socket)) {
fclose($this->socket);
$this->socket = null;
$this->request->setLastEvent('disconnect');
}
}
/**
* Checks whether another request should be performed with server digest auth
*
* Several conditions should be satisfied for it to return true:
* - response status should be 401
* - auth credentials should be set in the request object
* - response should contain WWW-Authenticate header with digest challenge
* - there is either no challenge stored for this URL or new challenge
* contains stale=true parameter (in other case we probably just failed
* due to invalid username / password)
*
* The method stores challenge values in $challenges static property
*
* @param HTTP_Request2_Response response to check
* @return boolean whether another request should be performed
* @throws HTTP_Request2_Exception in case of unsupported challenge parameters
*/
protected function shouldUseServerDigestAuth(HTTP_Request2_Response $response)
{
// no sense repeating a request if we don't have credentials
if (401 != $response->getStatus() || !$this->request->getAuth()) {
return false;
}
if (!$challenge = $this->parseDigestChallenge($response->getHeader('www-authenticate'))) {
return false;
}
$url = $this->request->getUrl();
$scheme = $url->getScheme();
$host = $scheme . '://' . $url->getHost();
if ($port = $url->getPort()) {
if ((0 == strcasecmp($scheme, 'http') && 80 != $port) ||
(0 == strcasecmp($scheme, 'https') && 443 != $port)
) {
$host .= ':' . $port;
}
}
if (!empty($challenge['domain'])) {
$prefixes = array();
foreach (preg_split('/\\s+/', $challenge['domain']) as $prefix) {
// don't bother with different servers
if ('/' == substr($prefix, 0, 1)) {
$prefixes[] = $host . $prefix;
}
}
}
if (empty($prefixes)) {
$prefixes = array($host . '/');
}
$ret = true;
foreach ($prefixes as $prefix) {
if (!empty(self::$challenges[$prefix]) &&
(empty($challenge['stale']) || strcasecmp('true', $challenge['stale']))
) {
// probably credentials are invalid
$ret = false;
}
self::$challenges[$prefix] =& $challenge;
}
return $ret;
}
/**
* Checks whether another request should be performed with proxy digest auth
*
* Several conditions should be satisfied for it to return true:
* - response status should be 407
* - proxy auth credentials should be set in the request object
* - response should contain Proxy-Authenticate header with digest challenge
* - there is either no challenge stored for this proxy or new challenge
* contains stale=true parameter (in other case we probably just failed
* due to invalid username / password)
*
* The method stores challenge values in $challenges static property
*
* @param HTTP_Request2_Response response to check
* @return boolean whether another request should be performed
* @throws HTTP_Request2_Exception in case of unsupported challenge parameters
*/
protected function shouldUseProxyDigestAuth(HTTP_Request2_Response $response)
{
if (407 != $response->getStatus() || !$this->request->getConfig('proxy_user')) {
return false;
}
if (!($challenge = $this->parseDigestChallenge($response->getHeader('proxy-authenticate')))) {
return false;
}
$key = 'proxy://' . $this->request->getConfig('proxy_host') .
':' . $this->request->getConfig('proxy_port');
if (!empty(self::$challenges[$key]) &&
(empty($challenge['stale']) || strcasecmp('true', $challenge['stale']))
) {
$ret = false;
} else {
$ret = true;
}
self::$challenges[$key] = $challenge;
return $ret;
}
/**
* Extracts digest method challenge from (WWW|Proxy)-Authenticate header value
*
* There is a problem with implementation of RFC 2617: several of the parameters
* here are defined as quoted-string and thus may contain backslash escaped
* double quotes (RFC 2616, section 2.2). However, RFC 2617 defines unq(X) as
* just value of quoted-string X without surrounding quotes, it doesn't speak
* about removing backslash escaping.
*
* Now realm parameter is user-defined and human-readable, strange things
* happen when it contains quotes:
* - Apache allows quotes in realm, but apparently uses realm value without
* backslashes for digest computation
* - Squid allows (manually escaped) quotes there, but it is impossible to
* authorize with either escaped or unescaped quotes used in digest,
* probably it can't parse the response (?)
* - Both IE and Firefox display realm value with backslashes in
* the password popup and apparently use the same value for digest
*
* HTTP_Request2 follows IE and Firefox (and hopefully RFC 2617) in
* quoted-string handling, unfortunately that means failure to authorize
* sometimes
*
* @param string value of WWW-Authenticate or Proxy-Authenticate header
* @return mixed associative array with challenge parameters, false if
* no challenge is present in header value
* @throws HTTP_Request2_Exception in case of unsupported challenge parameters
*/
protected function parseDigestChallenge($headerValue)
{
$authParam = '(' . self::REGEXP_TOKEN . ')\\s*=\\s*(' .
self::REGEXP_TOKEN . '|' . self::REGEXP_QUOTED_STRING . ')';
$challenge = "!(?<=^|\\s|,)Digest ({$authParam}\\s*(,\\s*|$))+!";
if (!preg_match($challenge, $headerValue, $matches)) {
return false;
}
preg_match_all('!' . $authParam . '!', $matches[0], $params);
$paramsAry = array();
$knownParams = array('realm', 'domain', 'nonce', 'opaque', 'stale',
'algorithm', 'qop');
for ($i = 0; $i < count($params[0]); $i++) {
// section 3.2.1: Any unrecognized directive MUST be ignored.
if (in_array($params[1][$i], $knownParams)) {
if ('"' == substr($params[2][$i], 0, 1)) {
$paramsAry[$params[1][$i]] = substr($params[2][$i], 1, -1);
} else {
$paramsAry[$params[1][$i]] = $params[2][$i];
}
}
}
// we only support qop=auth
if (!empty($paramsAry['qop']) &&
!in_array('auth', array_map('trim', explode(',', $paramsAry['qop'])))
) {
throw new HTTP_Request2_Exception(
"Only 'auth' qop is currently supported in digest authentication, " .
"server requested '{$paramsAry['qop']}'"
);
}
// we only support algorithm=MD5
if (!empty($paramsAry['algorithm']) && 'MD5' != $paramsAry['algorithm']) {
throw new HTTP_Request2_Exception(
"Only 'MD5' algorithm is currently supported in digest authentication, " .
"server requested '{$paramsAry['algorithm']}'"
);
}
return $paramsAry;
}
/**
* Parses [Proxy-]Authentication-Info header value and updates challenge
*
* @param array challenge to update
* @param string value of [Proxy-]Authentication-Info header
* @todo validate server rspauth response
*/
protected function updateChallenge(&$challenge, $headerValue)
{
$authParam = '!(' . self::REGEXP_TOKEN . ')\\s*=\\s*(' .
self::REGEXP_TOKEN . '|' . self::REGEXP_QUOTED_STRING . ')!';
$paramsAry = array();
preg_match_all($authParam, $headerValue, $params);
for ($i = 0; $i < count($params[0]); $i++) {
if ('"' == substr($params[2][$i], 0, 1)) {
$paramsAry[$params[1][$i]] = substr($params[2][$i], 1, -1);
} else {
$paramsAry[$params[1][$i]] = $params[2][$i];
}
}
// for now, just update the nonce value
if (!empty($paramsAry['nextnonce'])) {
$challenge['nonce'] = $paramsAry['nextnonce'];
$challenge['nc'] = 1;
}
}
/**
* Creates a value for [Proxy-]Authorization header when using digest authentication
*
* @param string user name
* @param string password
* @param string request URL
* @param array digest challenge parameters
* @return string value of [Proxy-]Authorization request header
* @link http://tools.ietf.org/html/rfc2617#section-3.2.2
*/
protected function createDigestResponse($user, $password, $url, &$challenge)
{
if (false !== ($q = strpos($url, '?')) &&
$this->request->getConfig('digest_compat_ie')
) {
$url = substr($url, 0, $q);
}
$a1 = md5($user . ':' . $challenge['realm'] . ':' . $password);
$a2 = md5($this->request->getMethod() . ':' . $url);
if (empty($challenge['qop'])) {
$digest = md5($a1 . ':' . $challenge['nonce'] . ':' . $a2);
} else {
$challenge['cnonce'] = 'Req2.' . rand();
if (empty($challenge['nc'])) {
$challenge['nc'] = 1;
}
$nc = sprintf('%08x', $challenge['nc']++);
$digest = md5($a1 . ':' . $challenge['nonce'] . ':' . $nc . ':' .
$challenge['cnonce'] . ':auth:' . $a2);
}
return 'Digest username="' . str_replace(array('\\', '"'), array('\\\\', '\\"'), $user) . '", ' .
'realm="' . $challenge['realm'] . '", ' .
'nonce="' . $challenge['nonce'] . '", ' .
'uri="' . $url . '", ' .
'response="' . $digest . '"' .
(!empty($challenge['opaque'])?
', opaque="' . $challenge['opaque'] . '"':
'') .
(!empty($challenge['qop'])?
', qop="auth", nc=' . $nc . ', cnonce="' . $challenge['cnonce'] . '"':
'');
}
/**
* Adds 'Authorization' header (if needed) to request headers array
*
* @param array request headers
* @param string request host (needed for digest authentication)
* @param string request URL (needed for digest authentication)
* @throws HTTP_Request2_Exception
*/
protected function addAuthorizationHeader(&$headers, $requestHost, $requestUrl)
{
if (!($auth = $this->request->getAuth())) {
return;
}
switch ($auth['scheme']) {
case HTTP_Request2::AUTH_BASIC:
$headers['authorization'] =
'Basic ' . base64_encode($auth['user'] . ':' . $auth['password']);
break;
case HTTP_Request2::AUTH_DIGEST:
unset($this->serverChallenge);
$fullUrl = ('/' == $requestUrl[0])?
$this->request->getUrl()->getScheme() . '://' .
$requestHost . $requestUrl:
$requestUrl;
foreach (array_keys(self::$challenges) as $key) {
if ($key == substr($fullUrl, 0, strlen($key))) {
$headers['authorization'] = $this->createDigestResponse(
$auth['user'], $auth['password'],
$requestUrl, self::$challenges[$key]
);
$this->serverChallenge =& self::$challenges[$key];
break;
}
}
break;
default:
throw new HTTP_Request2_Exception(
"Unknown HTTP authentication scheme '{$auth['scheme']}'"
);
}
}
/**
* Adds 'Proxy-Authorization' header (if needed) to request headers array
*
* @param array request headers
* @param string request URL (needed for digest authentication)
* @throws HTTP_Request2_Exception
*/
protected function addProxyAuthorizationHeader(&$headers, $requestUrl)
{
if (!$this->request->getConfig('proxy_host') ||
!($user = $this->request->getConfig('proxy_user')) ||
(0 == strcasecmp('https', $this->request->getUrl()->getScheme()) &&
HTTP_Request2::METHOD_CONNECT != $this->request->getMethod())
) {
return;
}
$password = $this->request->getConfig('proxy_password');
switch ($this->request->getConfig('proxy_auth_scheme')) {
case HTTP_Request2::AUTH_BASIC:
$headers['proxy-authorization'] =
'Basic ' . base64_encode($user . ':' . $password);
break;
case HTTP_Request2::AUTH_DIGEST:
unset($this->proxyChallenge);
$proxyUrl = 'proxy://' . $this->request->getConfig('proxy_host') .
':' . $this->request->getConfig('proxy_port');
if (!empty(self::$challenges[$proxyUrl])) {
$headers['proxy-authorization'] = $this->createDigestResponse(
$user, $password,
$requestUrl, self::$challenges[$proxyUrl]
);
$this->proxyChallenge =& self::$challenges[$proxyUrl];
}
break;
default:
throw new HTTP_Request2_Exception(
"Unknown HTTP authentication scheme '" .
$this->request->getConfig('proxy_auth_scheme') . "'"
);
}
}
/**
* Creates the string with the Request-Line and request headers
*
* @return string
* @throws HTTP_Request2_Exception
*/
protected function prepareHeaders()
{
$headers = $this->request->getHeaders();
$url = $this->request->getUrl();
$connect = HTTP_Request2::METHOD_CONNECT == $this->request->getMethod();
$host = $url->getHost();
$defaultPort = 0 == strcasecmp($url->getScheme(), 'https')? 443: 80;
if (($port = $url->getPort()) && $port != $defaultPort || $connect) {
$host .= ':' . (empty($port)? $defaultPort: $port);
}
// Do not overwrite explicitly set 'Host' header, see bug #16146
if (!isset($headers['host'])) {
$headers['host'] = $host;
}
if ($connect) {
$requestUrl = $host;
} else {
if (!$this->request->getConfig('proxy_host') ||
0 == strcasecmp($url->getScheme(), 'https')
) {
$requestUrl = '';
} else {
$requestUrl = $url->getScheme() . '://' . $host;
}
$path = $url->getPath();
$query = $url->getQuery();
$requestUrl .= (empty($path)? '/': $path) . (empty($query)? '': '?' . $query);
}
if ('1.1' == $this->request->getConfig('protocol_version') &&
extension_loaded('zlib') && !isset($headers['accept-encoding'])
) {
$headers['accept-encoding'] = 'gzip, deflate';
}
$this->addAuthorizationHeader($headers, $host, $requestUrl);
$this->addProxyAuthorizationHeader($headers, $requestUrl);
$this->calculateRequestLength($headers);
$headersStr = $this->request->getMethod() . ' ' . $requestUrl . ' HTTP/' .
$this->request->getConfig('protocol_version') . "\r\n";
foreach ($headers as $name => $value) {
$canonicalName = implode('-', array_map('ucfirst', explode('-', $name)));
$headersStr .= $canonicalName . ': ' . $value . "\r\n";
}
return $headersStr . "\r\n";
}
/**
* Sends the request body
*
* @throws HTTP_Request2_Exception
*/
protected function writeBody()
{
if (in_array($this->request->getMethod(), self::$bodyDisallowed) ||
0 == $this->contentLength
) {
return;
}
$position = 0;
$bufferSize = $this->request->getConfig('buffer_size');
while ($position < $this->contentLength) {
if (is_string($this->requestBody)) {
$str = substr($this->requestBody, $position, $bufferSize);
} elseif (is_resource($this->requestBody)) {
$str = fread($this->requestBody, $bufferSize);
} else {
$str = $this->requestBody->read($bufferSize);
}
if (false === @fwrite($this->socket, $str, strlen($str))) {
throw new HTTP_Request2_Exception('Error writing request');
}
// Provide the length of written string to the observer, request #7630
$this->request->setLastEvent('sentBodyPart', strlen($str));
$position += strlen($str);
}
}
/**
* Reads the remote server's response
*
* @return HTTP_Request2_Response
* @throws HTTP_Request2_Exception
*/
protected function readResponse()
{
$bufferSize = $this->request->getConfig('buffer_size');
do {
$response = new HTTP_Request2_Response($this->readLine($bufferSize), true);
do {
$headerLine = $this->readLine($bufferSize);
$response->parseHeaderLine($headerLine);
} while ('' != $headerLine);
} while (in_array($response->getStatus(), array(100, 101)));
$this->request->setLastEvent('receivedHeaders', $response);
// No body possible in such responses
if (HTTP_Request2::METHOD_HEAD == $this->request->getMethod() ||
(HTTP_Request2::METHOD_CONNECT == $this->request->getMethod() &&
200 <= $response->getStatus() && 300 > $response->getStatus()) ||
in_array($response->getStatus(), array(204, 304))
) {
return $response;
}
$chunked = 'chunked' == $response->getHeader('transfer-encoding');
$length = $response->getHeader('content-length');
$hasBody = false;
if ($chunked || null === $length || 0 < intval($length)) {
// RFC 2616, section 4.4:
// 3. ... If a message is received with both a
// Transfer-Encoding header field and a Content-Length header field,
// the latter MUST be ignored.
$toRead = ($chunked || null === $length)? null: $length;
$this->chunkLength = 0;
while (!feof($this->socket) && (is_null($toRead) || 0 < $toRead)) {
if ($chunked) {
$data = $this->readChunked($bufferSize);
} elseif (is_null($toRead)) {
$data = $this->fread($bufferSize);
} else {
$data = $this->fread(min($toRead, $bufferSize));
$toRead -= strlen($data);
}
if ('' == $data && (!$this->chunkLength || feof($this->socket))) {
break;
}
$hasBody = true;
if ($this->request->getConfig('store_body')) {
$response->appendBody($data);
}
if (!in_array($response->getHeader('content-encoding'), array('identity', null))) {
$this->request->setLastEvent('receivedEncodedBodyPart', $data);
} else {
$this->request->setLastEvent('receivedBodyPart', $data);
}
}
}
if ($hasBody) {
$this->request->setLastEvent('receivedBody', $response);
}
return $response;
}
/**
* Reads until either the end of the socket or a newline, whichever comes first
*
* Strips the trailing newline from the returned data, handles global
* request timeout. Method idea borrowed from Net_Socket PEAR package.
*
* @param int buffer size to use for reading
* @return Available data up to the newline (not including newline)
* @throws HTTP_Request2_Exception In case of timeout
*/
protected function readLine($bufferSize)
{
$line = '';
while (!feof($this->socket)) {
if ($this->timeout) {
stream_set_timeout($this->socket, max($this->timeout - time(), 1));
}
$line .= @fgets($this->socket, $bufferSize);
$info = stream_get_meta_data($this->socket);
if ($info['timed_out'] || $this->timeout && time() > $this->timeout) {
throw new HTTP_Request2_Exception(
'Request timed out after ' .
$this->request->getConfig('timeout') . ' second(s)'
);
}
if (substr($line, -1) == "\n") {
return rtrim($line, "\r\n");
}
}
return $line;
}
/**
* Wrapper around fread(), handles global request timeout
*
* @param int Reads up to this number of bytes
* @return Data read from socket
* @throws HTTP_Request2_Exception In case of timeout
*/
protected function fread($length)
{
if ($this->timeout) {
stream_set_timeout($this->socket, max($this->timeout - time(), 1));
}
$data = fread($this->socket, $length);
$info = stream_get_meta_data($this->socket);
if ($info['timed_out'] || $this->timeout && time() > $this->timeout) {
throw new HTTP_Request2_Exception(
'Request timed out after ' .
$this->request->getConfig('timeout') . ' second(s)'
);
}
return $data;
}
/**
* Reads a part of response body encoded with chunked Transfer-Encoding
*
* @param int buffer size to use for reading
* @return string
* @throws HTTP_Request2_Exception
*/
protected function readChunked($bufferSize)
{
// at start of the next chunk?
if (0 == $this->chunkLength) {
$line = $this->readLine($bufferSize);
if (!preg_match('/^([0-9a-f]+)/i', $line, $matches)) {
throw new HTTP_Request2_Exception(
"Cannot decode chunked response, invalid chunk length '{$line}'"
);
} else {
$this->chunkLength = hexdec($matches[1]);
// Chunk with zero length indicates the end
if (0 == $this->chunkLength) {
$this->readLine($bufferSize);
return '';
}
}
}
$data = $this->fread(min($this->chunkLength, $bufferSize));
$this->chunkLength -= strlen($data);
if (0 == $this->chunkLength) {
$this->readLine($bufferSize); // Trailing CRLF
}
return $data;
}
}
?>

View File

@ -0,0 +1,62 @@
<?php
/**
* Exception class for HTTP_Request2 package
*
* PHP version 5
*
* LICENSE:
*
* Copyright (c) 2008, 2009, Alexey Borzov <avb@php.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The names of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @category HTTP
* @package HTTP_Request2
* @author Alexey Borzov <avb@php.net>
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id: Exception.php 273003 2009-01-07 19:28:22Z avb $
* @link http://pear.php.net/package/HTTP_Request2
*/
/**
* Base class for exceptions in PEAR
*/
require_once 'PEAR/Exception.php';
/**
* Exception class for HTTP_Request2 package
*
* Such a class is required by the Exception RFC:
* http://pear.php.net/pepr/pepr-proposal-show.php?id=132
*
* @category HTTP
* @package HTTP_Request2
* @version Release: 0.4.1
*/
class HTTP_Request2_Exception extends PEAR_Exception
{
}
?>

View File

@ -0,0 +1,274 @@
<?php
/**
* Helper class for building multipart/form-data request body
*
* PHP version 5
*
* LICENSE:
*
* Copyright (c) 2008, 2009, Alexey Borzov <avb@php.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The names of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @category HTTP
* @package HTTP_Request2
* @author Alexey Borzov <avb@php.net>
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id: MultipartBody.php 287306 2009-08-14 15:22:52Z avb $
* @link http://pear.php.net/package/HTTP_Request2
*/
/**
* Class for building multipart/form-data request body
*
* The class helps to reduce memory consumption by streaming large file uploads
* from disk, it also allows monitoring of upload progress (see request #7630)
*
* @category HTTP
* @package HTTP_Request2
* @author Alexey Borzov <avb@php.net>
* @version Release: 0.4.1
* @link http://tools.ietf.org/html/rfc1867
*/
class HTTP_Request2_MultipartBody
{
/**
* MIME boundary
* @var string
*/
private $_boundary;
/**
* Form parameters added via {@link HTTP_Request2::addPostParameter()}
* @var array
*/
private $_params = array();
/**
* File uploads added via {@link HTTP_Request2::addUpload()}
* @var array
*/
private $_uploads = array();
/**
* Header for parts with parameters
* @var string
*/
private $_headerParam = "--%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n";
/**
* Header for parts with uploads
* @var string
*/
private $_headerUpload = "--%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\nContent-Type: %s\r\n\r\n";
/**
* Current position in parameter and upload arrays
*
* First number is index of "current" part, second number is position within
* "current" part
*
* @var array
*/
private $_pos = array(0, 0);
/**
* Constructor. Sets the arrays with POST data.
*
* @param array values of form fields set via {@link HTTP_Request2::addPostParameter()}
* @param array file uploads set via {@link HTTP_Request2::addUpload()}
* @param bool whether to append brackets to array variable names
*/
public function __construct(array $params, array $uploads, $useBrackets = true)
{
$this->_params = self::_flattenArray('', $params, $useBrackets);
foreach ($uploads as $fieldName => $f) {
if (!is_array($f['fp'])) {
$this->_uploads[] = $f + array('name' => $fieldName);
} else {
for ($i = 0; $i < count($f['fp']); $i++) {
$upload = array(
'name' => ($useBrackets? $fieldName . '[' . $i . ']': $fieldName)
);
foreach (array('fp', 'filename', 'size', 'type') as $key) {
$upload[$key] = $f[$key][$i];
}
$this->_uploads[] = $upload;
}
}
}
}
/**
* Returns the length of the body to use in Content-Length header
*
* @return integer
*/
public function getLength()
{
$boundaryLength = strlen($this->getBoundary());
$headerParamLength = strlen($this->_headerParam) - 4 + $boundaryLength;
$headerUploadLength = strlen($this->_headerUpload) - 8 + $boundaryLength;
$length = $boundaryLength + 6;
foreach ($this->_params as $p) {
$length += $headerParamLength + strlen($p[0]) + strlen($p[1]) + 2;
}
foreach ($this->_uploads as $u) {
$length += $headerUploadLength + strlen($u['name']) + strlen($u['type']) +
strlen($u['filename']) + $u['size'] + 2;
}
return $length;
}
/**
* Returns the boundary to use in Content-Type header
*
* @return string
*/
public function getBoundary()
{
if (empty($this->_boundary)) {
$this->_boundary = '--' . md5('PEAR-HTTP_Request2-' . microtime());
}
return $this->_boundary;
}
/**
* Returns next chunk of request body
*
* @param integer Amount of bytes to read
* @return string Up to $length bytes of data, empty string if at end
*/
public function read($length)
{
$ret = '';
$boundary = $this->getBoundary();
$paramCount = count($this->_params);
$uploadCount = count($this->_uploads);
while ($length > 0 && $this->_pos[0] <= $paramCount + $uploadCount) {
$oldLength = $length;
if ($this->_pos[0] < $paramCount) {
$param = sprintf($this->_headerParam, $boundary,
$this->_params[$this->_pos[0]][0]) .
$this->_params[$this->_pos[0]][1] . "\r\n";
$ret .= substr($param, $this->_pos[1], $length);
$length -= min(strlen($param) - $this->_pos[1], $length);
} elseif ($this->_pos[0] < $paramCount + $uploadCount) {
$pos = $this->_pos[0] - $paramCount;
$header = sprintf($this->_headerUpload, $boundary,
$this->_uploads[$pos]['name'],
$this->_uploads[$pos]['filename'],
$this->_uploads[$pos]['type']);
if ($this->_pos[1] < strlen($header)) {
$ret .= substr($header, $this->_pos[1], $length);
$length -= min(strlen($header) - $this->_pos[1], $length);
}
$filePos = max(0, $this->_pos[1] - strlen($header));
if ($length > 0 && $filePos < $this->_uploads[$pos]['size']) {
$ret .= fread($this->_uploads[$pos]['fp'], $length);
$length -= min($length, $this->_uploads[$pos]['size'] - $filePos);
}
if ($length > 0) {
$start = $this->_pos[1] + ($oldLength - $length) -
strlen($header) - $this->_uploads[$pos]['size'];
$ret .= substr("\r\n", $start, $length);
$length -= min(2 - $start, $length);
}
} else {
$closing = '--' . $boundary . "--\r\n";
$ret .= substr($closing, $this->_pos[1], $length);
$length -= min(strlen($closing) - $this->_pos[1], $length);
}
if ($length > 0) {
$this->_pos = array($this->_pos[0] + 1, 0);
} else {
$this->_pos[1] += $oldLength;
}
}
return $ret;
}
/**
* Sets the current position to the start of the body
*
* This allows reusing the same body in another request
*/
public function rewind()
{
$this->_pos = array(0, 0);
foreach ($this->_uploads as $u) {
rewind($u['fp']);
}
}
/**
* Returns the body as string
*
* Note that it reads all file uploads into memory so it is a good idea not
* to use this method with large file uploads and rely on read() instead.
*
* @return string
*/
public function __toString()
{
$this->rewind();
return $this->read($this->getLength());
}
/**
* Helper function to change the (probably multidimensional) associative array
* into the simple one.
*
* @param string name for item
* @param mixed item's values
* @param bool whether to append [] to array variables' names
* @return array array with the following items: array('item name', 'item value');
*/
private static function _flattenArray($name, $values, $useBrackets)
{
if (!is_array($values)) {
return array(array($name, $values));
} else {
$ret = array();
foreach ($values as $k => $v) {
if (empty($name)) {
$newName = $k;
} elseif ($useBrackets) {
$newName = $name . '[' . $k . ']';
} else {
$newName = $name;
}
$ret = array_merge($ret, self::_flattenArray($newName, $v, $useBrackets));
}
return $ret;
}
}
}
?>

View File

@ -0,0 +1,215 @@
<?php
/**
* An observer useful for debugging / testing.
*
* PHP version 5
*
* LICENSE:
*
* Copyright (c) 2008, 2009, Alexey Borzov <avb@php.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The names of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @category HTTP
* @package HTTP_Request2
* @author David Jean Louis <izi@php.net>
* @author Alexey Borzov <avb@php.net>
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id: Log.php 272593 2009-01-02 16:27:14Z avb $
* @link http://pear.php.net/package/HTTP_Request2
*/
/**
* Exception class for HTTP_Request2 package
*/
require_once 'HTTP/Request2/Exception.php';
/**
* A debug observer useful for debugging / testing.
*
* This observer logs to a log target data corresponding to the various request
* and response events, it logs by default to php://output but can be configured
* to log to a file or via the PEAR Log package.
*
* A simple example:
* <code>
* require_once 'HTTP/Request2.php';
* require_once 'HTTP/Request2/Observer/Log.php';
*
* $request = new HTTP_Request2('http://www.example.com');
* $observer = new HTTP_Request2_Observer_Log();
* $request->attach($observer);
* $request->send();
* </code>
*
* A more complex example with PEAR Log:
* <code>
* require_once 'HTTP/Request2.php';
* require_once 'HTTP/Request2/Observer/Log.php';
* require_once 'Log.php';
*
* $request = new HTTP_Request2('http://www.example.com');
* // we want to log with PEAR log
* $observer = new HTTP_Request2_Observer_Log(Log::factory('console'));
*
* // we only want to log received headers
* $observer->events = array('receivedHeaders');
*
* $request->attach($observer);
* $request->send();
* </code>
*
* @category HTTP
* @package HTTP_Request2
* @author David Jean Louis <izi@php.net>
* @author Alexey Borzov <avb@php.net>
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version Release: 0.4.1
* @link http://pear.php.net/package/HTTP_Request2
*/
class HTTP_Request2_Observer_Log implements SplObserver
{
// properties {{{
/**
* The log target, it can be a a resource or a PEAR Log instance.
*
* @var resource|Log $target
*/
protected $target = null;
/**
* The events to log.
*
* @var array $events
*/
public $events = array(
'connect',
'sentHeaders',
'sentBodyPart',
'receivedHeaders',
'receivedBody',
'disconnect',
);
// }}}
// __construct() {{{
/**
* Constructor.
*
* @param mixed $target Can be a file path (default: php://output), a resource,
* or an instance of the PEAR Log class.
* @param array $events Array of events to listen to (default: all events)
*
* @return void
*/
public function __construct($target = 'php://output', array $events = array())
{
if (!empty($events)) {
$this->events = $events;
}
if (is_resource($target) || $target instanceof Log) {
$this->target = $target;
} elseif (false === ($this->target = @fopen($target, 'w'))) {
throw new HTTP_Request2_Exception("Unable to open '{$target}'");
}
}
// }}}
// update() {{{
/**
* Called when the request notify us of an event.
*
* @param HTTP_Request2 $subject The HTTP_Request2 instance
*
* @return void
*/
public function update(SplSubject $subject)
{
$event = $subject->getLastEvent();
if (!in_array($event['name'], $this->events)) {
return;
}
switch ($event['name']) {
case 'connect':
$this->log('* Connected to ' . $event['data']);
break;
case 'sentHeaders':
$headers = explode("\r\n", $event['data']);
array_pop($headers);
foreach ($headers as $header) {
$this->log('> ' . $header);
}
break;
case 'sentBodyPart':
$this->log('> ' . $event['data']);
break;
case 'receivedHeaders':
$this->log(sprintf('< HTTP/%s %s %s',
$event['data']->getVersion(),
$event['data']->getStatus(),
$event['data']->getReasonPhrase()));
$headers = $event['data']->getHeader();
foreach ($headers as $key => $val) {
$this->log('< ' . $key . ': ' . $val);
}
$this->log('< ');
break;
case 'receivedBody':
$this->log($event['data']->getBody());
break;
case 'disconnect':
$this->log('* Disconnected');
break;
}
}
// }}}
// log() {{{
/**
* Log the given message to the configured target.
*
* @param string $message Message to display
*
* @return void
*/
protected function log($message)
{
if ($this->target instanceof Log) {
$this->target->debug($message);
} elseif (is_resource($this->target)) {
fwrite($this->target, $message . "\r\n");
}
}
// }}}
}
?>

View File

@ -0,0 +1,549 @@
<?php
/**
* Class representing a HTTP response
*
* PHP version 5
*
* LICENSE:
*
* Copyright (c) 2008, 2009, Alexey Borzov <avb@php.net>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The names of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @category HTTP
* @package HTTP_Request2
* @author Alexey Borzov <avb@php.net>
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id: Response.php 287948 2009-09-01 17:12:18Z avb $
* @link http://pear.php.net/package/HTTP_Request2
*/
/**
* Exception class for HTTP_Request2 package
*/
require_once 'HTTP/Request2/Exception.php';
/**
* Class representing a HTTP response
*
* The class is designed to be used in "streaming" scenario, building the
* response as it is being received:
* <code>
* $statusLine = read_status_line();
* $response = new HTTP_Request2_Response($statusLine);
* do {
* $headerLine = read_header_line();
* $response->parseHeaderLine($headerLine);
* } while ($headerLine != '');
*
* while ($chunk = read_body()) {
* $response->appendBody($chunk);
* }
*
* var_dump($response->getHeader(), $response->getCookies(), $response->getBody());
* </code>
*
*
* @category HTTP
* @package HTTP_Request2
* @author Alexey Borzov <avb@php.net>
* @version Release: 0.4.1
* @link http://tools.ietf.org/html/rfc2616#section-6
*/
class HTTP_Request2_Response
{
/**
* HTTP protocol version (e.g. 1.0, 1.1)
* @var string
*/
protected $version;
/**
* Status code
* @var integer
* @link http://tools.ietf.org/html/rfc2616#section-6.1.1
*/
protected $code;
/**
* Reason phrase
* @var string
* @link http://tools.ietf.org/html/rfc2616#section-6.1.1
*/
protected $reasonPhrase;
/**
* Associative array of response headers
* @var array
*/
protected $headers = array();
/**
* Cookies set in the response
* @var array
*/
protected $cookies = array();
/**
* Name of last header processed by parseHederLine()
*
* Used to handle the headers that span multiple lines
*
* @var string
*/
protected $lastHeader = null;
/**
* Response body
* @var string
*/
protected $body = '';
/**
* Whether the body is still encoded by Content-Encoding
*
* cURL provides the decoded body to the callback; if we are reading from
* socket the body is still gzipped / deflated
*
* @var bool
*/
protected $bodyEncoded;
/**
* Associative array of HTTP status code / reason phrase.
*
* @var array
* @link http://tools.ietf.org/html/rfc2616#section-10
*/
protected static $phrases = array(
// 1xx: Informational - Request received, continuing process
100 => 'Continue',
101 => 'Switching Protocols',
// 2xx: Success - The action was successfully received, understood and
// accepted
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
// 3xx: Redirection - Further action must be taken in order to complete
// the request
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found', // 1.1
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
307 => 'Temporary Redirect',
// 4xx: Client Error - The request contains bad syntax or cannot be
// fulfilled
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
// 5xx: Server Error - The server failed to fulfill an apparently
// valid request
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
509 => 'Bandwidth Limit Exceeded',
);
/**
* Constructor, parses the response status line
*
* @param string Response status line (e.g. "HTTP/1.1 200 OK")
* @param bool Whether body is still encoded by Content-Encoding
* @throws HTTP_Request2_Exception if status line is invalid according to spec
*/
public function __construct($statusLine, $bodyEncoded = true)
{
if (!preg_match('!^HTTP/(\d\.\d) (\d{3})(?: (.+))?!', $statusLine, $m)) {
throw new HTTP_Request2_Exception("Malformed response: {$statusLine}");
}
$this->version = $m[1];
$this->code = intval($m[2]);
if (!empty($m[3])) {
$this->reasonPhrase = trim($m[3]);
} elseif (!empty(self::$phrases[$this->code])) {
$this->reasonPhrase = self::$phrases[$this->code];
}
$this->bodyEncoded = (bool)$bodyEncoded;
}
/**
* Parses the line from HTTP response filling $headers array
*
* The method should be called after reading the line from socket or receiving
* it into cURL callback. Passing an empty string here indicates the end of
* response headers and triggers additional processing, so be sure to pass an
* empty string in the end.
*
* @param string Line from HTTP response
*/
public function parseHeaderLine($headerLine)
{
$headerLine = trim($headerLine, "\r\n");
// empty string signals the end of headers, process the received ones
if ('' == $headerLine) {
if (!empty($this->headers['set-cookie'])) {
$cookies = is_array($this->headers['set-cookie'])?
$this->headers['set-cookie']:
array($this->headers['set-cookie']);
foreach ($cookies as $cookieString) {
$this->parseCookie($cookieString);
}
unset($this->headers['set-cookie']);
}
foreach (array_keys($this->headers) as $k) {
if (is_array($this->headers[$k])) {
$this->headers[$k] = implode(', ', $this->headers[$k]);
}
}
// string of the form header-name: header value
} elseif (preg_match('!^([^\x00-\x1f\x7f-\xff()<>@,;:\\\\"/\[\]?={}\s]+):(.+)$!', $headerLine, $m)) {
$name = strtolower($m[1]);
$value = trim($m[2]);
if (empty($this->headers[$name])) {
$this->headers[$name] = $value;
} else {
if (!is_array($this->headers[$name])) {
$this->headers[$name] = array($this->headers[$name]);
}
$this->headers[$name][] = $value;
}
$this->lastHeader = $name;
// string
} elseif (preg_match('!^\s+(.+)$!', $headerLine, $m) && $this->lastHeader) {
if (!is_array($this->headers[$this->lastHeader])) {
$this->headers[$this->lastHeader] .= ' ' . trim($m[1]);
} else {
$key = count($this->headers[$this->lastHeader]) - 1;
$this->headers[$this->lastHeader][$key] .= ' ' . trim($m[1]);
}
}
}
/**
* Parses a Set-Cookie header to fill $cookies array
*
* @param string value of Set-Cookie header
* @link http://cgi.netscape.com/newsref/std/cookie_spec.html
*/
protected function parseCookie($cookieString)
{
$cookie = array(
'expires' => null,
'domain' => null,
'path' => null,
'secure' => false
);
// Only a name=value pair
if (!strpos($cookieString, ';')) {
$pos = strpos($cookieString, '=');
$cookie['name'] = trim(substr($cookieString, 0, $pos));
$cookie['value'] = trim(substr($cookieString, $pos + 1));
// Some optional parameters are supplied
} else {
$elements = explode(';', $cookieString);
$pos = strpos($elements[0], '=');
$cookie['name'] = trim(substr($elements[0], 0, $pos));
$cookie['value'] = trim(substr($elements[0], $pos + 1));
for ($i = 1; $i < count($elements); $i++) {
if (false === strpos($elements[$i], '=')) {
$elName = trim($elements[$i]);
$elValue = null;
} else {
list ($elName, $elValue) = array_map('trim', explode('=', $elements[$i]));
}
$elName = strtolower($elName);
if ('secure' == $elName) {
$cookie['secure'] = true;
} elseif ('expires' == $elName) {
$cookie['expires'] = str_replace('"', '', $elValue);
} elseif ('path' == $elName || 'domain' == $elName) {
$cookie[$elName] = urldecode($elValue);
} else {
$cookie[$elName] = $elValue;
}
}
}
$this->cookies[] = $cookie;
}
/**
* Appends a string to the response body
* @param string
*/
public function appendBody($bodyChunk)
{
$this->body .= $bodyChunk;
}
/**
* Returns the status code
* @return integer
*/
public function getStatus()
{
return $this->code;
}
/**
* Returns the reason phrase
* @return string
*/
public function getReasonPhrase()
{
return $this->reasonPhrase;
}
/**
* Returns either the named header or all response headers
*
* @param string Name of header to return
* @return string|array Value of $headerName header (null if header is
* not present), array of all response headers if
* $headerName is null
*/
public function getHeader($headerName = null)
{
if (null === $headerName) {
return $this->headers;
} else {
$headerName = strtolower($headerName);
return isset($this->headers[$headerName])? $this->headers[$headerName]: null;
}
}
/**
* Returns cookies set in response
*
* @return array
*/
public function getCookies()
{
return $this->cookies;
}
/**
* Returns the body of the response
*
* @return string
* @throws HTTP_Request2_Exception if body cannot be decoded
*/
public function getBody()
{
if (!$this->bodyEncoded ||
!in_array(strtolower($this->getHeader('content-encoding')), array('gzip', 'deflate'))
) {
return $this->body;
} else {
if (extension_loaded('mbstring') && (2 & ini_get('mbstring.func_overload'))) {
$oldEncoding = mb_internal_encoding();
mb_internal_encoding('iso-8859-1');
}
try {
switch (strtolower($this->getHeader('content-encoding'))) {
case 'gzip':
$decoded = self::decodeGzip($this->body);
break;
case 'deflate':
$decoded = self::decodeDeflate($this->body);
}
} catch (Exception $e) {
}
if (!empty($oldEncoding)) {
mb_internal_encoding($oldEncoding);
}
if (!empty($e)) {
throw $e;
}
return $decoded;
}
}
/**
* Get the HTTP version of the response
*
* @return string
*/
public function getVersion()
{
return $this->version;
}
/**
* Decodes the message-body encoded by gzip
*
* The real decoding work is done by gzinflate() built-in function, this
* method only parses the header and checks data for compliance with
* RFC 1952
*
* @param string gzip-encoded data
* @return string decoded data
* @throws HTTP_Request2_Exception
* @link http://tools.ietf.org/html/rfc1952
*/
public static function decodeGzip($data)
{
$length = strlen($data);
// If it doesn't look like gzip-encoded data, don't bother
if (18 > $length || strcmp(substr($data, 0, 2), "\x1f\x8b")) {
return $data;
}
if (!function_exists('gzinflate')) {
throw new HTTP_Request2_Exception('Unable to decode body: gzip extension not available');
}
$method = ord(substr($data, 2, 1));
if (8 != $method) {
throw new HTTP_Request2_Exception('Error parsing gzip header: unknown compression method');
}
$flags = ord(substr($data, 3, 1));
if ($flags & 224) {
throw new HTTP_Request2_Exception('Error parsing gzip header: reserved bits are set');
}
// header is 10 bytes minimum. may be longer, though.
$headerLength = 10;
// extra fields, need to skip 'em
if ($flags & 4) {
if ($length - $headerLength - 2 < 8) {
throw new HTTP_Request2_Exception('Error parsing gzip header: data too short');
}
$extraLength = unpack('v', substr($data, 10, 2));
if ($length - $headerLength - 2 - $extraLength[1] < 8) {
throw new HTTP_Request2_Exception('Error parsing gzip header: data too short');
}
$headerLength += $extraLength[1] + 2;
}
// file name, need to skip that
if ($flags & 8) {
if ($length - $headerLength - 1 < 8) {
throw new HTTP_Request2_Exception('Error parsing gzip header: data too short');
}
$filenameLength = strpos(substr($data, $headerLength), chr(0));
if (false === $filenameLength || $length - $headerLength - $filenameLength - 1 < 8) {
throw new HTTP_Request2_Exception('Error parsing gzip header: data too short');
}
$headerLength += $filenameLength + 1;
}
// comment, need to skip that also
if ($flags & 16) {
if ($length - $headerLength - 1 < 8) {
throw new HTTP_Request2_Exception('Error parsing gzip header: data too short');
}
$commentLength = strpos(substr($data, $headerLength), chr(0));
if (false === $commentLength || $length - $headerLength - $commentLength - 1 < 8) {
throw new HTTP_Request2_Exception('Error parsing gzip header: data too short');
}
$headerLength += $commentLength + 1;
}
// have a CRC for header. let's check
if ($flags & 2) {
if ($length - $headerLength - 2 < 8) {
throw new HTTP_Request2_Exception('Error parsing gzip header: data too short');
}
$crcReal = 0xffff & crc32(substr($data, 0, $headerLength));
$crcStored = unpack('v', substr($data, $headerLength, 2));
if ($crcReal != $crcStored[1]) {
throw new HTTP_Request2_Exception('Header CRC check failed');
}
$headerLength += 2;
}
// unpacked data CRC and size at the end of encoded data
$tmp = unpack('V2', substr($data, -8));
$dataCrc = $tmp[1];
$dataSize = $tmp[2];
// finally, call the gzinflate() function
// don't pass $dataSize to gzinflate, see bugs #13135, #14370
$unpacked = gzinflate(substr($data, $headerLength, -8));
if (false === $unpacked) {
throw new HTTP_Request2_Exception('gzinflate() call failed');
} elseif ($dataSize != strlen($unpacked)) {
throw new HTTP_Request2_Exception('Data size check failed');
} elseif ((0xffffffff & $dataCrc) != (0xffffffff & crc32($unpacked))) {
throw new HTTP_Request2_Exception('Data CRC check failed');
}
return $unpacked;
}
/**
* Decodes the message-body encoded by deflate
*
* @param string deflate-encoded data
* @return string decoded data
* @throws HTTP_Request2_Exception
*/
public static function decodeDeflate($data)
{
if (!function_exists('gzuncompress')) {
throw new HTTP_Request2_Exception('Unable to decode body: gzip extension not available');
}
// RFC 2616 defines 'deflate' encoding as zlib format from RFC 1950,
// while many applications send raw deflate stream from RFC 1951.
// We should check for presence of zlib header and use gzuncompress() or
// gzinflate() as needed. See bug #15305
$header = unpack('n', substr($data, 0, 2));
return (0 == $header[1] % 31)? gzuncompress($data): gzinflate($data);
}
}
?>

1791
extlib/Net/LDAP2.php Normal file

File diff suppressed because it is too large Load Diff

1055
extlib/Net/LDAP2/Entry.php Normal file

File diff suppressed because it is too large Load Diff

514
extlib/Net/LDAP2/Filter.php Normal file
View File

@ -0,0 +1,514 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
/**
* File containing the Net_LDAP2_Filter interface class.
*
* PHP version 5
*
* @category Net
* @package Net_LDAP2
* @author Benedikt Hallinger <beni@php.net>
* @copyright 2009 Benedikt Hallinger
* @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPLv3
* @version SVN: $Id: Filter.php 289978 2009-10-27 09:56:41Z beni $
* @link http://pear.php.net/package/Net_LDAP2/
*/
/**
* Includes
*/
require_once 'PEAR.php';
require_once 'Util.php';
/**
* Object representation of a part of a LDAP filter.
*
* This Class is not completely compatible to the PERL interface!
*
* The purpose of this class is, that users can easily build LDAP filters
* without having to worry about right escaping etc.
* A Filter is built using several independent filter objects
* which are combined afterwards. This object works in two
* modes, depending how the object is created.
* If the object is created using the {@link create()} method, then this is a leaf-object.
* If the object is created using the {@link combine()} method, then this is a container object.
*
* LDAP filters are defined in RFC-2254 and can be found under
* {@link http://www.ietf.org/rfc/rfc2254.txt}
*
* Here a quick copy&paste example:
* <code>
* $filter0 = Net_LDAP2_Filter::create('stars', 'equals', '***');
* $filter_not0 = Net_LDAP2_Filter::combine('not', $filter0);
*
* $filter1 = Net_LDAP2_Filter::create('gn', 'begins', 'bar');
* $filter2 = Net_LDAP2_Filter::create('gn', 'ends', 'baz');
* $filter_comp = Net_LDAP2_Filter::combine('or',array($filter_not0, $filter1, $filter2));
*
* echo $filter_comp->asString();
* // This will output: (|(!(stars=\0x5c0x2a\0x5c0x2a\0x5c0x2a))(gn=bar*)(gn=*baz))
* // The stars in $filter0 are treaten as real stars unless you disable escaping.
* </code>
*
* @category Net
* @package Net_LDAP2
* @author Benedikt Hallinger <beni@php.net>
* @license http://www.gnu.org/copyleft/lesser.html LGPL
* @link http://pear.php.net/package/Net_LDAP2/
*/
class Net_LDAP2_Filter extends PEAR
{
/**
* Storage for combination of filters
*
* This variable holds a array of filter objects
* that should be combined by this filter object.
*
* @access protected
* @var array
*/
protected $_subfilters = array();
/**
* Match of this filter
*
* If this is a leaf filter, then a matching rule is stored,
* if it is a container, then it is a logical operator
*
* @access protected
* @var string
*/
protected $_match;
/**
* Single filter
*
* If we operate in leaf filter mode,
* then the constructing method stores
* the filter representation here
*
* @acces private
* @var string
*/
protected $_filter;
/**
* Create a new Net_LDAP2_Filter object and parse $filter.
*
* This is for PERL Net::LDAP interface.
* Construction of Net_LDAP2_Filter objects should happen through either
* {@link create()} or {@link combine()} which give you more control.
* However, you may use the perl iterface if you already have generated filters.
*
* @param string $filter LDAP filter string
*
* @see parse()
*/
public function __construct($filter = false)
{
// The optional parameter must remain here, because otherwise create() crashes
if (false !== $filter) {
$filter_o = self::parse($filter);
if (PEAR::isError($filter_o)) {
$this->_filter = $filter_o; // assign error, so asString() can report it
} else {
$this->_filter = $filter_o->asString();
}
}
}
/**
* Constructor of a new part of a LDAP filter.
*
* The following matching rules exists:
* - equals: One of the attributes values is exactly $value
* Please note that case sensitiviness is depends on the
* attributes syntax configured in the server.
* - begins: One of the attributes values must begin with $value
* - ends: One of the attributes values must end with $value
* - contains: One of the attributes values must contain $value
* - present | any: The attribute can contain any value but must be existent
* - greater: The attributes value is greater than $value
* - less: The attributes value is less than $value
* - greaterOrEqual: The attributes value is greater or equal than $value
* - lessOrEqual: The attributes value is less or equal than $value
* - approx: One of the attributes values is similar to $value
*
* If $escape is set to true (default) then $value will be escaped
* properly. If it is set to false then $value will be treaten as raw filter value string.
* You should escape yourself using {@link Net_LDAP2_Util::escape_filter_value()}!
*
* Examples:
* <code>
* // This will find entries that contain an attribute "sn" that ends with "foobar":
* $filter = new Net_LDAP2_Filter('sn', 'ends', 'foobar');
*
* // This will find entries that contain an attribute "sn" that has any value set:
* $filter = new Net_LDAP2_Filter('sn', 'any');
* </code>
*
* @param string $attr_name Name of the attribute the filter should apply to
* @param string $match Matching rule (equals, begins, ends, contains, greater, less, greaterOrEqual, lessOrEqual, approx, any)
* @param string $value (optional) if given, then this is used as a filter
* @param boolean $escape Should $value be escaped? (default: yes, see {@link Net_LDAP2_Util::escape_filter_value()} for detailed information)
*
* @return Net_LDAP2_Filter|Net_LDAP2_Error
*/
public static function &create($attr_name, $match, $value = '', $escape = true)
{
$leaf_filter = new Net_LDAP2_Filter();
if ($escape) {
$array = Net_LDAP2_Util::escape_filter_value(array($value));
$value = $array[0];
}
switch (strtolower($match)) {
case 'equals':
$leaf_filter->_filter = '(' . $attr_name . '=' . $value . ')';
break;
case 'begins':
$leaf_filter->_filter = '(' . $attr_name . '=' . $value . '*)';
break;
case 'ends':
$leaf_filter->_filter = '(' . $attr_name . '=*' . $value . ')';
break;
case 'contains':
$leaf_filter->_filter = '(' . $attr_name . '=*' . $value . '*)';
break;
case 'greater':
$leaf_filter->_filter = '(' . $attr_name . '>' . $value . ')';
break;
case 'less':
$leaf_filter->_filter = '(' . $attr_name . '<' . $value . ')';
break;
case 'greaterorequal':
case '>=':
$leaf_filter->_filter = '(' . $attr_name . '>=' . $value . ')';
break;
case 'lessorequal':
case '<=':
$leaf_filter->_filter = '(' . $attr_name . '<=' . $value . ')';
break;
case 'approx':
case '~=':
$leaf_filter->_filter = '(' . $attr_name . '~=' . $value . ')';
break;
case 'any':
case 'present': // alias that may improve user code readability
$leaf_filter->_filter = '(' . $attr_name . '=*)';
break;
default:
return PEAR::raiseError('Net_LDAP2_Filter create error: matching rule "' . $match . '" not known!');
}
return $leaf_filter;
}
/**
* Combine two or more filter objects using a logical operator
*
* This static method combines two or more filter objects and returns one single
* filter object that contains all the others.
* Call this method statically: $filter = Net_LDAP2_Filter('or', array($filter1, $filter2))
* If the array contains filter strings instead of filter objects, we will try to parse them.
*
* @param string $log_op The locicall operator. May be "and", "or", "not" or the subsequent logical equivalents "&", "|", "!"
* @param array|Net_LDAP2_Filter $filters array with Net_LDAP2_Filter objects
*
* @return Net_LDAP2_Filter|Net_LDAP2_Error
* @static
*/
public static function &combine($log_op, $filters)
{
if (PEAR::isError($filters)) {
return $filters;
}
// substitude named operators to logical operators
if ($log_op == 'and') $log_op = '&';
if ($log_op == 'or') $log_op = '|';
if ($log_op == 'not') $log_op = '!';
// tests for sane operation
if ($log_op == '!') {
// Not-combination, here we only accept one filter object or filter string
if ($filters instanceof Net_LDAP2_Filter) {
$filters = array($filters); // force array
} elseif (is_string($filters)) {
$filter_o = self::parse($filters);
if (PEAR::isError($filter_o)) {
$err = PEAR::raiseError('Net_LDAP2_Filter combine error: '.$filter_o->getMessage());
return $err;
} else {
$filters = array($filter_o);
}
} elseif (is_array($filters)) {
$err = PEAR::raiseError('Net_LDAP2_Filter combine error: operator is "not" but $filter is an array!');
return $err;
} else {
$err = PEAR::raiseError('Net_LDAP2_Filter combine error: operator is "not" but $filter is not a valid Net_LDAP2_Filter nor a filter string!');
return $err;
}
} elseif ($log_op == '&' || $log_op == '|') {
if (!is_array($filters) || count($filters) < 2) {
$err = PEAR::raiseError('Net_LDAP2_Filter combine error: parameter $filters is not an array or contains less than two Net_LDAP2_Filter objects!');
return $err;
}
} else {
$err = PEAR::raiseError('Net_LDAP2_Filter combine error: logical operator is not known!');
return $err;
}
$combined_filter = new Net_LDAP2_Filter();
foreach ($filters as $key => $testfilter) { // check for errors
if (PEAR::isError($testfilter)) {
return $testfilter;
} elseif (is_string($testfilter)) {
// string found, try to parse into an filter object
$filter_o = self::parse($testfilter);
if (PEAR::isError($filter_o)) {
return $filter_o;
} else {
$filters[$key] = $filter_o;
}
} elseif (!$testfilter instanceof Net_LDAP2_Filter) {
$err = PEAR::raiseError('Net_LDAP2_Filter combine error: invalid object passed in array $filters!');
return $err;
}
}
$combined_filter->_subfilters = $filters;
$combined_filter->_match = $log_op;
return $combined_filter;
}
/**
* Parse FILTER into a Net_LDAP2_Filter object
*
* This parses an filter string into Net_LDAP2_Filter objects.
*
* @param string $FILTER The filter string
*
* @access static
* @return Net_LDAP2_Filter|Net_LDAP2_Error
* @todo Leaf-mode: Do we need to escape at all? what about *-chars?check for the need of encoding values, tackle problems (see code comments)
*/
public static function parse($FILTER)
{
if (preg_match('/^\((.+?)\)$/', $FILTER, $matches)) {
if (in_array(substr($matches[1], 0, 1), array('!', '|', '&'))) {
// Subfilter processing: pass subfilters to parse() and combine
// the objects using the logical operator detected
// we have now something like "&(...)(...)(...)" but at least one part ("!(...)").
// Each subfilter could be an arbitary complex subfilter.
// extract logical operator and filter arguments
$log_op = substr($matches[1], 0, 1);
$remaining_component = substr($matches[1], 1);
// split $remaining_component into individual subfilters
// we cannot use split() for this, because we do not know the
// complexiness of the subfilter. Thus, we look trough the filter
// string and just recognize ending filters at the first level.
// We record the index number of the char and use that information
// later to split the string.
$sub_index_pos = array();
$prev_char = ''; // previous character looked at
$level = 0; // denotes the current bracket level we are,
// >1 is too deep, 1 is ok, 0 is outside any
// subcomponent
for ($curpos = 0; $curpos < strlen($remaining_component); $curpos++) {
$cur_char = substr($remaining_component, $curpos, 1);
// rise/lower bracket level
if ($cur_char == '(' && $prev_char != '\\') {
$level++;
} elseif ($cur_char == ')' && $prev_char != '\\') {
$level--;
}
if ($cur_char == '(' && $prev_char == ')' && $level == 1) {
array_push($sub_index_pos, $curpos); // mark the position for splitting
}
$prev_char = $cur_char;
}
// now perform the splits. To get also the last part, we
// need to add the "END" index to the split array
array_push($sub_index_pos, strlen($remaining_component));
$subfilters = array();
$oldpos = 0;
foreach ($sub_index_pos as $s_pos) {
$str_part = substr($remaining_component, $oldpos, $s_pos - $oldpos);
array_push($subfilters, $str_part);
$oldpos = $s_pos;
}
// some error checking...
if (count($subfilters) == 1) {
// only one subfilter found
} elseif (count($subfilters) > 1) {
// several subfilters found
if ($log_op == "!") {
return PEAR::raiseError("Filter parsing error: invalid filter syntax - NOT operator detected but several arguments given!");
}
} else {
// this should not happen unless the user specified a wrong filter
return PEAR::raiseError("Filter parsing error: invalid filter syntax - got operator '$log_op' but no argument!");
}
// Now parse the subfilters into objects and combine them using the operator
$subfilters_o = array();
foreach ($subfilters as $s_s) {
$o = self::parse($s_s);
if (PEAR::isError($o)) {
return $o;
} else {
array_push($subfilters_o, self::parse($s_s));
}
}
$filter_o = self::combine($log_op, $subfilters_o);
return $filter_o;
} else {
// This is one leaf filter component, do some syntax checks, then escape and build filter_o
// $matches[1] should be now something like "foo=bar"
// detect multiple leaf components
// [TODO] Maybe this will make problems with filters containing brackets inside the value
if (stristr($matches[1], ')(')) {
return PEAR::raiseError("Filter parsing error: invalid filter syntax - multiple leaf components detected!");
} else {
$filter_parts = preg_split('/(?<!\\\\)(=|=~|>|<|>=|<=)/', $matches[1], 2, PREG_SPLIT_DELIM_CAPTURE);
if (count($filter_parts) != 3) {
return PEAR::raiseError("Filter parsing error: invalid filter syntax - unknown matching rule used");
} else {
$filter_o = new Net_LDAP2_Filter();
// [TODO]: Do we need to escape at all? what about *-chars user provide and that should remain special?
// I think, those prevent escaping! We need to check against PERL Net::LDAP!
// $value_arr = Net_LDAP2_Util::escape_filter_value(array($filter_parts[2]));
// $value = $value_arr[0];
$value = $filter_parts[2];
$filter_o->_filter = '('.$filter_parts[0].$filter_parts[1].$value.')';
return $filter_o;
}
}
}
} else {
// ERROR: Filter components must be enclosed in round brackets
return PEAR::raiseError("Filter parsing error: invalid filter syntax - filter components must be enclosed in round brackets");
}
}
/**
* Get the string representation of this filter
*
* This method runs through all filter objects and creates
* the string representation of the filter. If this
* filter object is a leaf filter, then it will return
* the string representation of this filter.
*
* @return string|Net_LDAP2_Error
*/
public function asString()
{
if ($this->isLeaf()) {
$return = $this->_filter;
} else {
$return = '';
foreach ($this->_subfilters as $filter) {
$return = $return.$filter->asString();
}
$return = '(' . $this->_match . $return . ')';
}
return $return;
}
/**
* Alias for perl interface as_string()
*
* @see asString()
* @return string|Net_LDAP2_Error
*/
public function as_string()
{
return $this->asString();
}
/**
* Print the text representation of the filter to FH, or the currently selected output handle if FH is not given
*
* This method is only for compatibility to the perl interface.
* However, the original method was called "print" but due to PHP language restrictions,
* we can't have a print() method.
*
* @param resource $FH (optional) A filehandle resource
*
* @return true|Net_LDAP2_Error
*/
public function printMe($FH = false)
{
if (!is_resource($FH)) {
if (PEAR::isError($FH)) {
return $FH;
}
$filter_str = $this->asString();
if (PEAR::isError($filter_str)) {
return $filter_str;
} else {
print($filter_str);
}
} else {
$filter_str = $this->asString();
if (PEAR::isError($filter_str)) {
return $filter_str;
} else {
$res = @fwrite($FH, $this->asString());
if ($res == false) {
return PEAR::raiseError("Unable to write filter string to filehandle \$FH!");
}
}
}
return true;
}
/**
* This can be used to escape a string to provide a valid LDAP-Filter.
*
* LDAP will only recognise certain characters as the
* character istself if they are properly escaped. This is
* what this method does.
* The method can be called statically, so you can use it outside
* for your own purposes (eg for escaping only parts of strings)
*
* In fact, this is just a shorthand to {@link Net_LDAP2_Util::escape_filter_value()}.
* For upward compatibiliy reasons you are strongly encouraged to use the escape
* methods provided by the Net_LDAP2_Util class.
*
* @param string $value Any string who should be escaped
*
* @static
* @return string The string $string, but escaped
* @deprecated Do not use this method anymore, instead use Net_LDAP2_Util::escape_filter_value() directly
*/
public static function escape($value)
{
$return = Net_LDAP2_Util::escape_filter_value(array($value));
return $return[0];
}
/**
* Is this a container or a leaf filter object?
*
* @access protected
* @return boolean
*/
protected function isLeaf()
{
if (count($this->_subfilters) > 0) {
return false; // Container!
} else {
return true; // Leaf!
}
}
}
?>

922
extlib/Net/LDAP2/LDIF.php Normal file
View File

@ -0,0 +1,922 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
/**
* File containing the Net_LDAP2_LDIF interface class.
*
* PHP version 5
*
* @category Net
* @package Net_LDAP2
* @author Benedikt Hallinger <beni@php.net>
* @copyright 2009 Benedikt Hallinger
* @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPLv3
* @version SVN: $Id: LDIF.php 286718 2009-08-03 07:30:49Z beni $
* @link http://pear.php.net/package/Net_LDAP2/
*/
/**
* Includes
*/
require_once 'PEAR.php';
require_once 'Net/LDAP2.php';
require_once 'Net/LDAP2/Entry.php';
require_once 'Net/LDAP2/Util.php';
/**
* LDIF capabilitys for Net_LDAP2, closely taken from PERLs Net::LDAP
*
* It provides a means to convert between Net_LDAP2_Entry objects and LDAP entries
* represented in LDIF format files. Reading and writing are supported and may
* manipulate single entries or lists of entries.
*
* Usage example:
* <code>
* // Read and parse an ldif-file into Net_LDAP2_Entry objects
* // and print out the DNs. Store the entries for later use.
* require 'Net/LDAP2/LDIF.php';
* $options = array(
* 'onerror' => 'die'
* );
* $entries = array();
* $ldif = new Net_LDAP2_LDIF('test.ldif', 'r', $options);
* do {
* $entry = $ldif->read_entry();
* $dn = $entry->dn();
* echo " done building entry: $dn\n";
* array_push($entries, $entry);
* } while (!$ldif->eof());
* $ldif->done();
*
*
* // write those entries to another file
* $ldif = new Net_LDAP2_LDIF('test.out.ldif', 'w', $options);
* $ldif->write_entry($entries);
* $ldif->done();
* </code>
*
* @category Net
* @package Net_LDAP2
* @author Benedikt Hallinger <beni@php.net>
* @license http://www.gnu.org/copyleft/lesser.html LGPL
* @link http://pear.php.net/package/Net_LDAP22/
* @see http://www.ietf.org/rfc/rfc2849.txt
* @todo Error handling should be PEARified
* @todo LDAPv3 controls are not implemented yet
*/
class Net_LDAP2_LDIF extends PEAR
{
/**
* Options
*
* @access protected
* @var array
*/
protected $_options = array('encode' => 'base64',
'onerror' => null,
'change' => 0,
'lowercase' => 0,
'sort' => 0,
'version' => null,
'wrap' => 78,
'raw' => ''
);
/**
* Errorcache
*
* @access protected
* @var array
*/
protected $_error = array('error' => null,
'line' => 0
);
/**
* Filehandle for read/write
*
* @access protected
* @var array
*/
protected $_FH = null;
/**
* Says, if we opened the filehandle ourselves
*
* @access protected
* @var array
*/
protected $_FH_opened = false;
/**
* Linecounter for input file handle
*
* @access protected
* @var array
*/
protected $_input_line = 0;
/**
* counter for processed entries
*
* @access protected
* @var int
*/
protected $_entrynum = 0;
/**
* Mode we are working in
*
* Either 'r', 'a' or 'w'
*
* @access protected
* @var string
*/
protected $_mode = false;
/**
* Tells, if the LDIF version string was already written
*
* @access protected
* @var boolean
*/
protected $_version_written = false;
/**
* Cache for lines that have build the current entry
*
* @access protected
* @var boolean
*/
protected $_lines_cur = array();
/**
* Cache for lines that will build the next entry
*
* @access protected
* @var boolean
*/
protected $_lines_next = array();
/**
* Open LDIF file for reading or for writing
*
* new (FILE):
* Open the file read-only. FILE may be the name of a file
* or an already open filehandle.
* If the file doesn't exist, it will be created if in write mode.
*
* new (FILE, MODE, OPTIONS):
* Open the file with the given MODE (see PHPs fopen()), eg "w" or "a".
* FILE may be the name of a file or an already open filehandle.
* PERLs Net_LDAP2 "FILE|" mode does not work curently.
*
* OPTIONS is an associative array and may contain:
* encode => 'none' | 'canonical' | 'base64'
* Some DN values in LDIF cannot be written verbatim and have to be encoded in some way:
* 'none' No encoding.
* 'canonical' See "canonical_dn()" in Net::LDAP::Util.
* 'base64' Use base64. (default, this differs from the Perl interface.
* The perl default is "none"!)
*
* onerror => 'die' | 'warn' | NULL
* Specify what happens when an error is detected.
* 'die' Net_LDAP2_LDIF will croak with an appropriate message.
* 'warn' Net_LDAP2_LDIF will warn (echo) with an appropriate message.
* NULL Net_LDAP2_LDIF will not warn (default), use error().
*
* change => 1
* Write entry changes to the LDIF file instead of the entries itself. I.e. write LDAP
* operations acting on the entries to the file instead of the entries contents.
* This writes the changes usually carried out by an update() to the LDIF file.
*
* lowercase => 1
* Convert attribute names to lowercase when writing.
*
* sort => 1
* Sort attribute names when writing entries according to the rule:
* objectclass first then all other attributes alphabetically sorted by attribute name
*
* version => '1'
* Set the LDIF version to write to the resulting LDIF file.
* According to RFC 2849 currently the only legal value for this option is 1.
* When this option is set Net_LDAP2_LDIF tries to adhere more strictly to
* the LDIF specification in RFC2489 in a few places.
* The default is NULL meaning no version information is written to the LDIF file.
*
* wrap => 78
* Number of columns where output line wrapping shall occur.
* Default is 78. Setting it to 40 or lower inhibits wrapping.
*
* raw => REGEX
* Use REGEX to denote the names of attributes that are to be
* considered binary in search results if writing entries.
* Example: raw => "/(?i:^jpegPhoto|;binary)/i"
*
* @param string|ressource $file Filename or filehandle
* @param string $mode Mode to open filename
* @param array $options Options like described above
*/
public function __construct($file, $mode = 'r', $options = array())
{
$this->PEAR('Net_LDAP2_Error'); // default error class
// First, parse options
// todo: maybe implement further checks on possible values
foreach ($options as $option => $value) {
if (!array_key_exists($option, $this->_options)) {
$this->dropError('Net_LDAP2_LDIF error: option '.$option.' not known!');
return;
} else {
$this->_options[$option] = strtolower($value);
}
}
// setup LDIF class
$this->version($this->_options['version']);
// setup file mode
if (!preg_match('/^[rwa]\+?$/', $mode)) {
$this->dropError('Net_LDAP2_LDIF error: file mode '.$mode.' not supported!');
} else {
$this->_mode = $mode;
// setup filehandle
if (is_resource($file)) {
// TODO: checks on mode possible?
$this->_FH =& $file;
} else {
$imode = substr($this->_mode, 0, 1);
if ($imode == 'r') {
if (!file_exists($file)) {
$this->dropError('Unable to open '.$file.' for read: file not found');
$this->_mode = false;
}
if (!is_readable($file)) {
$this->dropError('Unable to open '.$file.' for read: permission denied');
$this->_mode = false;
}
}
if (($imode == 'w' || $imode == 'a')) {
if (file_exists($file)) {
if (!is_writable($file)) {
$this->dropError('Unable to open '.$file.' for write: permission denied');
$this->_mode = false;
}
} else {
if (!@touch($file)) {
$this->dropError('Unable to create '.$file.' for write: permission denied');
$this->_mode = false;
}
}
}
if ($this->_mode) {
$this->_FH = @fopen($file, $this->_mode);
if (false === $this->_FH) {
// Fallback; should never be reached if tests above are good enough!
$this->dropError('Net_LDAP2_LDIF error: Could not open file '.$file);
} else {
$this->_FH_opened = true;
}
}
}
}
}
/**
* Read one entry from the file and return it as a Net::LDAP::Entry object.
*
* @return Net_LDAP2_Entry
*/
public function read_entry()
{
// read fresh lines, set them as current lines and create the entry
$attrs = $this->next_lines(true);
if (count($attrs) > 0) {
$this->_lines_cur = $attrs;
}
return $this->current_entry();
}
/**
* Returns true when the end of the file is reached.
*
* @return boolean
*/
public function eof()
{
return feof($this->_FH);
}
/**
* Write the entry or entries to the LDIF file.
*
* If you want to build an LDIF file containing several entries AND
* you want to call write_entry() several times, you must open the filehandle
* in append mode ("a"), otherwise you will always get the last entry only.
*
* @param Net_LDAP2_Entry|array $entries Entry or array of entries
*
* @return void
* @todo implement operations on whole entries (adding a whole entry)
*/
public function write_entry($entries)
{
if (!is_array($entries)) {
$entries = array($entries);
}
foreach ($entries as $entry) {
$this->_entrynum++;
if (!$entry instanceof Net_LDAP2_Entry) {
$this->dropError('Net_LDAP2_LDIF error: entry '.$this->_entrynum.' is not an Net_LDAP2_Entry object');
} else {
if ($this->_options['change']) {
// LDIF change mode
// fetch change information from entry
$entry_attrs_changes = $entry->getChanges();
$num_of_changes = count($entry_attrs_changes['add'])
+ count($entry_attrs_changes['replace'])
+ count($entry_attrs_changes['delete']);
$is_changed = ($num_of_changes > 0 || $entry->willBeDeleted() || $entry->willBeMoved());
// write version if not done yet
// also write DN of entry
if ($is_changed) {
if (!$this->_version_written) {
$this->write_version();
}
$this->writeDN($entry->currentDN());
}
// process changes
// TODO: consider DN add!
if ($entry->willBeDeleted()) {
$this->writeLine("changetype: delete".PHP_EOL);
} elseif ($entry->willBeMoved()) {
$this->writeLine("changetype: modrdn".PHP_EOL);
$olddn = Net_LDAP2_Util::ldap_explode_dn($entry->currentDN(), array('casefold' => 'none')); // maybe gives a bug if using multivalued RDNs
$oldrdn = array_shift($olddn);
$oldparent = implode(',', $olddn);
$newdn = Net_LDAP2_Util::ldap_explode_dn($entry->dn(), array('casefold' => 'none')); // maybe gives a bug if using multivalued RDNs
$rdn = array_shift($newdn);
$parent = implode(',', $newdn);
$this->writeLine("newrdn: ".$rdn.PHP_EOL);
$this->writeLine("deleteoldrdn: 1".PHP_EOL);
if ($parent !== $oldparent) {
$this->writeLine("newsuperior: ".$parent.PHP_EOL);
}
// TODO: What if the entry has attribute changes as well?
// I think we should check for that and make a dummy
// entry with the changes that is written to the LDIF file
} elseif ($num_of_changes > 0) {
// write attribute change data
$this->writeLine("changetype: modify".PHP_EOL);
foreach ($entry_attrs_changes as $changetype => $entry_attrs) {
foreach ($entry_attrs as $attr_name => $attr_values) {
$this->writeLine("$changetype: $attr_name".PHP_EOL);
if ($attr_values !== null) $this->writeAttribute($attr_name, $attr_values, $changetype);
$this->writeLine("-".PHP_EOL);
}
}
}
// finish this entrys data if we had changes
if ($is_changed) {
$this->finishEntry();
}
} else {
// LDIF-content mode
// fetch attributes for further processing
$entry_attrs = $entry->getValues();
// sort and put objectclass-attrs to first position
if ($this->_options['sort']) {
ksort($entry_attrs);
if (array_key_exists('objectclass', $entry_attrs)) {
$oc = $entry_attrs['objectclass'];
unset($entry_attrs['objectclass']);
$entry_attrs = array_merge(array('objectclass' => $oc), $entry_attrs);
}
}
// write data
if (!$this->_version_written) {
$this->write_version();
}
$this->writeDN($entry->dn());
foreach ($entry_attrs as $attr_name => $attr_values) {
$this->writeAttribute($attr_name, $attr_values);
}
$this->finishEntry();
}
}
}
}
/**
* Write version to LDIF
*
* If the object's version is defined, this method allows to explicitely write the version before an entry is written.
* If not called explicitely, it gets called automatically when writing the first entry.
*
* @return void
*/
public function write_version()
{
$this->_version_written = true;
if (!is_null($this->version())) {
return $this->writeLine('version: '.$this->version().PHP_EOL, 'Net_LDAP2_LDIF error: unable to write version');
}
}
/**
* Get or set LDIF version
*
* If called without arguments it returns the version of the LDIF file or NULL if no version has been set.
* If called with an argument it sets the LDIF version to VERSION.
* According to RFC 2849 currently the only legal value for VERSION is 1.
*
* @param int $version (optional) LDIF version to set
*
* @return int
*/
public function version($version = null)
{
if ($version !== null) {
if ($version != 1) {
$this->dropError('Net_LDAP2_LDIF error: illegal LDIF version set');
} else {
$this->_options['version'] = $version;
}
}
return $this->_options['version'];
}
/**
* Returns the file handle the Net_LDAP2_LDIF object reads from or writes to.
*
* You can, for example, use this to fetch the content of the LDIF file yourself
*
* @return null|resource
*/
public function &handle()
{
if (!is_resource($this->_FH)) {
$this->dropError('Net_LDAP2_LDIF error: invalid file resource');
$null = null;
return $null;
} else {
return $this->_FH;
}
}
/**
* Clean up
*
* This method signals that the LDIF object is no longer needed.
* You can use this to free up some memory and close the file handle.
* The file handle is only closed, if it was opened from Net_LDAP2_LDIF.
*
* @return void
*/
public function done()
{
// close FH if we opened it
if ($this->_FH_opened) {
fclose($this->handle());
}
// free variables
foreach (get_object_vars($this) as $name => $value) {
unset($this->$name);
}
}
/**
* Returns last error message if error was found.
*
* Example:
* <code>
* $ldif->someAction();
* if ($ldif->error()) {
* echo "Error: ".$ldif->error()." at input line: ".$ldif->error_lines();
* }
* </code>
*
* @param boolean $as_string If set to true, only the message is returned
*
* @return false|Net_LDAP2_Error
*/
public function error($as_string = false)
{
if (Net_LDAP2::isError($this->_error['error'])) {
return ($as_string)? $this->_error['error']->getMessage() : $this->_error['error'];
} else {
return false;
}
}
/**
* Returns lines that resulted in error.
*
* Perl returns an array of faulty lines in list context,
* but we always just return an int because of PHPs language.
*
* @return int
*/
public function error_lines()
{
return $this->_error['line'];
}
/**
* Returns the current Net::LDAP::Entry object.
*
* @return Net_LDAP2_Entry|false
*/
public function current_entry()
{
return $this->parseLines($this->current_lines());
}
/**
* Parse LDIF lines of one entry into an Net_LDAP2_Entry object
*
* @param array $lines LDIF lines for one entry
*
* @return Net_LDAP2_Entry|false Net_LDAP2_Entry object for those lines
* @todo what about file inclusions and urls? "jpegphoto:< file:///usr/local/directory/photos/fiona.jpg"
*/
public function parseLines($lines)
{
// parse lines into an array of attributes and build the entry
$attributes = array();
$dn = false;
foreach ($lines as $line) {
if (preg_match('/^(\w+)(:|::|:<)\s(.+)$/', $line, $matches)) {
$attr =& $matches[1];
$delim =& $matches[2];
$data =& $matches[3];
if ($delim == ':') {
// normal data
$attributes[$attr][] = $data;
} elseif ($delim == '::') {
// base64 data
$attributes[$attr][] = base64_decode($data);
} elseif ($delim == ':<') {
// file inclusion
// TODO: Is this the job of the LDAP-client or the server?
$this->dropError('File inclusions are currently not supported');
//$attributes[$attr][] = ...;
} else {
// since the pattern above, the delimeter cannot be something else.
$this->dropError('Net_LDAP2_LDIF parsing error: invalid syntax at parsing entry line: '.$line);
continue;
}
if (strtolower($attr) == 'dn') {
// DN line detected
$dn = $attributes[$attr][0]; // save possibly decoded DN
unset($attributes[$attr]); // remove wrongly added "dn: " attribute
}
} else {
// line not in "attr: value" format -> ignore
// maybe we should rise an error here, but this should be covered by
// next_lines() already. A problem arises, if users try to feed data of
// several entries to this method - the resulting entry will
// get wrong attributes. However, this is already mentioned in the
// methods documentation above.
}
}
if (false === $dn) {
$this->dropError('Net_LDAP2_LDIF parsing error: unable to detect DN for entry');
return false;
} else {
$newentry = Net_LDAP2_Entry::createFresh($dn, $attributes);
return $newentry;
}
}
/**
* Returns the lines that generated the current Net::LDAP::Entry object.
*
* Note that this returns an empty array if no lines have been read so far.
*
* @return array Array of lines
*/
public function current_lines()
{
return $this->_lines_cur;
}
/**
* Returns the lines that will generate the next Net::LDAP::Entry object.
*
* If you set $force to TRUE then you can iterate over the lines that build
* up entries manually. Otherwise, iterating is done using {@link read_entry()}.
* Force will move the file pointer forward, thus returning the next entries lines.
*
* Wrapped lines will be unwrapped. Comments are stripped.
*
* @param boolean $force Set this to true if you want to iterate over the lines manually
*
* @return array
*/
public function next_lines($force = false)
{
// if we already have those lines, just return them, otherwise read
if (count($this->_lines_next) == 0 || $force) {
$this->_lines_next = array(); // empty in case something was left (if used $force)
$entry_done = false;
$fh = &$this->handle();
$commentmode = false; // if we are in an comment, for wrapping purposes
$datalines_read = 0; // how many lines with data we have read
while (!$entry_done && !$this->eof()) {
$this->_input_line++;
// Read line. Remove line endings, we want only data;
// this is okay since ending spaces should be encoded
$data = rtrim(fgets($fh));
if ($data === false) {
// error only, if EOF not reached after fgets() call
if (!$this->eof()) {
$this->dropError('Net_LDAP2_LDIF error: error reading from file at input line '.$this->_input_line, $this->_input_line);
}
break;
} else {
if (count($this->_lines_next) > 0 && preg_match('/^$/', $data)) {
// Entry is finished if we have an empty line after we had data
$entry_done = true;
// Look ahead if the next EOF is nearby. Comments and empty
// lines at the file end may cause problems otherwise
$current_pos = ftell($fh);
$data = fgets($fh);
while (!feof($fh)) {
if (preg_match('/^\s*$/', $data) || preg_match('/^#/', $data)) {
// only empty lines or comments, continue to seek
// TODO: Known bug: Wrappings for comments are okay but are treaten as
// error, since we do not honor comment mode here.
// This should be a very theoretically case, however
// i am willing to fix this if really necessary.
$this->_input_line++;
$current_pos = ftell($fh);
$data = fgets($fh);
} else {
// Data found if non emtpy line and not a comment!!
// Rewind to position prior last read and stop lookahead
fseek($fh, $current_pos);
break;
}
}
// now we have either the file pointer at the beginning of
// a new data position or at the end of file causing feof() to return true
} else {
// build lines
if (preg_match('/^version:\s(.+)$/', $data, $match)) {
// version statement, set version
$this->version($match[1]);
} elseif (preg_match('/^\w+::?\s.+$/', $data)) {
// normal attribute: add line
$commentmode = false;
$this->_lines_next[] = trim($data);
$datalines_read++;
} elseif (preg_match('/^\s(.+)$/', $data, $matches)) {
// wrapped data: unwrap if not in comment mode
if (!$commentmode) {
if ($datalines_read == 0) {
// first line of entry: wrapped data is illegal
$this->dropError('Net_LDAP2_LDIF error: illegal wrapping at input line '.$this->_input_line, $this->_input_line);
} else {
$last = array_pop($this->_lines_next);
$last = $last.trim($matches[1]);
$this->_lines_next[] = $last;
$datalines_read++;
}
}
} elseif (preg_match('/^#/', $data)) {
// LDIF comments
$commentmode = true;
} elseif (preg_match('/^\s*$/', $data)) {
// empty line but we had no data for this
// entry, so just ignore this line
$commentmode = false;
} else {
$this->dropError('Net_LDAP2_LDIF error: invalid syntax at input line '.$this->_input_line, $this->_input_line);
continue;
}
}
}
}
}
return $this->_lines_next;
}
/**
* Convert an attribute and value to LDIF string representation
*
* It honors correct encoding of values according to RFC 2849.
* Line wrapping will occur at the configured maximum but only if
* the value is greater than 40 chars.
*
* @param string $attr_name Name of the attribute
* @param string $attr_value Value of the attribute
*
* @access protected
* @return string LDIF string for that attribute and value
*/
protected function convertAttribute($attr_name, $attr_value)
{
// Handle empty attribute or process
if (strlen($attr_value) == 0) {
$attr_value = " ";
} else {
$base64 = false;
// ASCII-chars that are NOT safe for the
// start and for being inside the value.
// These are the int values of those chars.
$unsafe_init = array(0, 10, 13, 32, 58, 60);
$unsafe = array(0, 10, 13);
// Test for illegal init char
$init_ord = ord(substr($attr_value, 0, 1));
if ($init_ord > 127 || in_array($init_ord, $unsafe_init)) {
$base64 = true;
}
// Test for illegal content char
for ($i = 0; $i < strlen($attr_value); $i++) {
$char_ord = ord(substr($attr_value, $i, 1));
if ($char_ord > 127 || in_array($char_ord, $unsafe)) {
$base64 = true;
}
}
// Test for ending space
if (substr($attr_value, -1) == ' ') {
$base64 = true;
}
// If converting is needed, do it
// Either we have some special chars or a matching "raw" regex
if ($base64 || ($this->_options['raw'] && preg_match($this->_options['raw'], $attr_name))) {
$attr_name .= ':';
$attr_value = base64_encode($attr_value);
}
// Lowercase attr names if requested
if ($this->_options['lowercase']) $attr_name = strtolower($attr_name);
// Handle line wrapping
if ($this->_options['wrap'] > 40 && strlen($attr_value) > $this->_options['wrap']) {
$attr_value = wordwrap($attr_value, $this->_options['wrap'], PHP_EOL." ", true);
}
}
return $attr_name.': '.$attr_value;
}
/**
* Convert an entries DN to LDIF string representation
*
* It honors correct encoding of values according to RFC 2849.
*
* @param string $dn UTF8-Encoded DN
*
* @access protected
* @return string LDIF string for that DN
* @todo I am not sure, if the UTF8 stuff is correctly handled right now
*/
protected function convertDN($dn)
{
$base64 = false;
// ASCII-chars that are NOT safe for the
// start and for being inside the dn.
// These are the int values of those chars.
$unsafe_init = array(0, 10, 13, 32, 58, 60);
$unsafe = array(0, 10, 13);
// Test for illegal init char
$init_ord = ord(substr($dn, 0, 1));
if ($init_ord >= 127 || in_array($init_ord, $unsafe_init)) {
$base64 = true;
}
// Test for illegal content char
for ($i = 0; $i < strlen($dn); $i++) {
$char = substr($dn, $i, 1);
if (ord($char) >= 127 || in_array($init_ord, $unsafe)) {
$base64 = true;
}
}
// Test for ending space
if (substr($dn, -1) == ' ') {
$base64 = true;
}
// if converting is needed, do it
return ($base64)? 'dn:: '.base64_encode($dn) : 'dn: '.$dn;
}
/**
* Writes an attribute to the filehandle
*
* @param string $attr_name Name of the attribute
* @param string|array $attr_values Single attribute value or array with attribute values
*
* @access protected
* @return void
*/
protected function writeAttribute($attr_name, $attr_values)
{
// write out attribute content
if (!is_array($attr_values)) {
$attr_values = array($attr_values);
}
foreach ($attr_values as $attr_val) {
$line = $this->convertAttribute($attr_name, $attr_val).PHP_EOL;
$this->writeLine($line, 'Net_LDAP2_LDIF error: unable to write attribute '.$attr_name.' of entry '.$this->_entrynum);
}
}
/**
* Writes a DN to the filehandle
*
* @param string $dn DN to write
*
* @access protected
* @return void
*/
protected function writeDN($dn)
{
// prepare DN
if ($this->_options['encode'] == 'base64') {
$dn = $this->convertDN($dn).PHP_EOL;
} elseif ($this->_options['encode'] == 'canonical') {
$dn = Net_LDAP2_Util::canonical_dn($dn, array('casefold' => 'none')).PHP_EOL;
} else {
$dn = $dn.PHP_EOL;
}
$this->writeLine($dn, 'Net_LDAP2_LDIF error: unable to write DN of entry '.$this->_entrynum);
}
/**
* Finishes an LDIF entry
*
* @access protected
* @return void
*/
protected function finishEntry()
{
$this->writeLine(PHP_EOL, 'Net_LDAP2_LDIF error: unable to close entry '.$this->_entrynum);
}
/**
* Just write an arbitary line to the filehandle
*
* @param string $line Content to write
* @param string $error If error occurs, drop this message
*
* @access protected
* @return true|false
*/
protected function writeLine($line, $error = 'Net_LDAP2_LDIF error: unable to write to filehandle')
{
if (is_resource($this->handle()) && fwrite($this->handle(), $line, strlen($line)) === false) {
$this->dropError($error);
return false;
} else {
return true;
}
}
/**
* Optionally raises an error and pushes the error on the error cache
*
* @param string $msg Errortext
* @param int $line Line in the LDIF that caused the error
*
* @access protected
* @return void
*/
protected function dropError($msg, $line = null)
{
$this->_error['error'] = new Net_LDAP2_Error($msg);
if ($line !== null) $this->_error['line'] = $line;
if ($this->_options['onerror'] == 'die') {
die($msg.PHP_EOL);
} elseif ($this->_options['onerror'] == 'warn') {
echo $msg.PHP_EOL;
}
}
}
?>

View File

@ -0,0 +1,240 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
/**
* File containing the Net_LDAP2_RootDSE interface class.
*
* PHP version 5
*
* @category Net
* @package Net_LDAP2
* @author Jan Wagner <wagner@netsols.de>
* @copyright 2009 Jan Wagner
* @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPLv3
* @version SVN: $Id: RootDSE.php 286718 2009-08-03 07:30:49Z beni $
* @link http://pear.php.net/package/Net_LDAP2/
*/
/**
* Includes
*/
require_once 'PEAR.php';
/**
* Getting the rootDSE entry of a LDAP server
*
* @category Net
* @package Net_LDAP2
* @author Jan Wagner <wagner@netsols.de>
* @license http://www.gnu.org/copyleft/lesser.html LGPL
* @link http://pear.php.net/package/Net_LDAP22/
*/
class Net_LDAP2_RootDSE extends PEAR
{
/**
* @access protected
* @var object Net_LDAP2_Entry
**/
protected $_entry;
/**
* Class constructor
*
* @param Net_LDAP2_Entry &$entry Net_LDAP2_Entry object of the RootDSE
*/
protected function __construct(&$entry)
{
$this->_entry = $entry;
}
/**
* Fetches a RootDSE object from an LDAP connection
*
* @param Net_LDAP2 $ldap Directory from which the RootDSE should be fetched
* @param array $attrs Array of attributes to search for
*
* @access static
* @return Net_LDAP2_RootDSE|Net_LDAP2_Error
*/
public static function fetch($ldap, $attrs = null)
{
if (!$ldap instanceof Net_LDAP2) {
return PEAR::raiseError("Unable to fetch Schema: Parameter \$ldap must be a Net_LDAP2 object!");
}
if (is_array($attrs) && count($attrs) > 0 ) {
$attributes = $attrs;
} else {
$attributes = array('vendorName',
'vendorVersion',
'namingContexts',
'altServer',
'supportedExtension',
'supportedControl',
'supportedSASLMechanisms',
'supportedLDAPVersion',
'subschemaSubentry' );
}
$result = $ldap->search('', '(objectClass=*)', array('attributes' => $attributes, 'scope' => 'base'));
if (self::isError($result)) {
return $result;
}
$entry = $result->shiftEntry();
if (false === $entry) {
return PEAR::raiseError('Could not fetch RootDSE entry');
}
$ret = new Net_LDAP2_RootDSE($entry);
return $ret;
}
/**
* Gets the requested attribute value
*
* Same usuage as {@link Net_LDAP2_Entry::getValue()}
*
* @param string $attr Attribute name
* @param array $options Array of options
*
* @access public
* @return mixed Net_LDAP2_Error object or attribute values
* @see Net_LDAP2_Entry::get_value()
*/
public function getValue($attr = '', $options = '')
{
return $this->_entry->get_value($attr, $options);
}
/**
* Alias function of getValue() for perl-ldap interface
*
* @see getValue()
* @return mixed
*/
public function get_value()
{
$args = func_get_args();
return call_user_func_array(array( &$this, 'getValue' ), $args);
}
/**
* Determines if the extension is supported
*
* @param array $oids Array of oids to check
*
* @access public
* @return boolean
*/
public function supportedExtension($oids)
{
return $this->checkAttr($oids, 'supportedExtension');
}
/**
* Alias function of supportedExtension() for perl-ldap interface
*
* @see supportedExtension()
* @return boolean
*/
public function supported_extension()
{
$args = func_get_args();
return call_user_func_array(array( &$this, 'supportedExtension'), $args);
}
/**
* Determines if the version is supported
*
* @param array $versions Versions to check
*
* @access public
* @return boolean
*/
public function supportedVersion($versions)
{
return $this->checkAttr($versions, 'supportedLDAPVersion');
}
/**
* Alias function of supportedVersion() for perl-ldap interface
*
* @see supportedVersion()
* @return boolean
*/
public function supported_version()
{
$args = func_get_args();
return call_user_func_array(array(&$this, 'supportedVersion'), $args);
}
/**
* Determines if the control is supported
*
* @param array $oids Control oids to check
*
* @access public
* @return boolean
*/
public function supportedControl($oids)
{
return $this->checkAttr($oids, 'supportedControl');
}
/**
* Alias function of supportedControl() for perl-ldap interface
*
* @see supportedControl()
* @return boolean
*/
public function supported_control()
{
$args = func_get_args();
return call_user_func_array(array(&$this, 'supportedControl' ), $args);
}
/**
* Determines if the sasl mechanism is supported
*
* @param array $mechlist SASL mechanisms to check
*
* @access public
* @return boolean
*/
public function supportedSASLMechanism($mechlist)
{
return $this->checkAttr($mechlist, 'supportedSASLMechanisms');
}
/**
* Alias function of supportedSASLMechanism() for perl-ldap interface
*
* @see supportedSASLMechanism()
* @return boolean
*/
public function supported_sasl_mechanism()
{
$args = func_get_args();
return call_user_func_array(array(&$this, 'supportedSASLMechanism'), $args);
}
/**
* Checks for existance of value in attribute
*
* @param array $values values to check
* @param string $attr attribute name
*
* @access protected
* @return boolean
*/
protected function checkAttr($values, $attr)
{
if (!is_array($values)) $values = array($values);
foreach ($values as $value) {
if (!@in_array($value, $this->get_value($attr, 'all'))) {
return false;
}
}
return true;
}
}
?>

516
extlib/Net/LDAP2/Schema.php Normal file
View File

@ -0,0 +1,516 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
/**
* File containing the Net_LDAP2_Schema interface class.
*
* PHP version 5
*
* @category Net
* @package Net_LDAP2
* @author Jan Wagner <wagner@netsols.de>
* @author Benedikt Hallinger <beni@php.net>
* @copyright 2009 Jan Wagner, Benedikt Hallinger
* @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPLv3
* @version SVN: $Id: Schema.php 286718 2009-08-03 07:30:49Z beni $
* @link http://pear.php.net/package/Net_LDAP2/
* @todo see the comment at the end of the file
*/
/**
* Includes
*/
require_once 'PEAR.php';
/**
* Syntax definitions
*
* Please don't forget to add binary attributes to isBinary() below
* to support proper value fetching from Net_LDAP2_Entry
*/
define('NET_LDAP2_SYNTAX_BOOLEAN', '1.3.6.1.4.1.1466.115.121.1.7');
define('NET_LDAP2_SYNTAX_DIRECTORY_STRING', '1.3.6.1.4.1.1466.115.121.1.15');
define('NET_LDAP2_SYNTAX_DISTINGUISHED_NAME', '1.3.6.1.4.1.1466.115.121.1.12');
define('NET_LDAP2_SYNTAX_INTEGER', '1.3.6.1.4.1.1466.115.121.1.27');
define('NET_LDAP2_SYNTAX_JPEG', '1.3.6.1.4.1.1466.115.121.1.28');
define('NET_LDAP2_SYNTAX_NUMERIC_STRING', '1.3.6.1.4.1.1466.115.121.1.36');
define('NET_LDAP2_SYNTAX_OID', '1.3.6.1.4.1.1466.115.121.1.38');
define('NET_LDAP2_SYNTAX_OCTET_STRING', '1.3.6.1.4.1.1466.115.121.1.40');
/**
* Load an LDAP Schema and provide information
*
* This class takes a Subschema entry, parses this information
* and makes it available in an array. Most of the code has been
* inspired by perl-ldap( http://perl-ldap.sourceforge.net).
* You will find portions of their implementation in here.
*
* @category Net
* @package Net_LDAP2
* @author Jan Wagner <wagner@netsols.de>
* @author Benedikt Hallinger <beni@php.net>
* @license http://www.gnu.org/copyleft/lesser.html LGPL
* @link http://pear.php.net/package/Net_LDAP22/
*/
class Net_LDAP2_Schema extends PEAR
{
/**
* Map of entry types to ldap attributes of subschema entry
*
* @access public
* @var array
*/
public $types = array(
'attribute' => 'attributeTypes',
'ditcontentrule' => 'dITContentRules',
'ditstructurerule' => 'dITStructureRules',
'matchingrule' => 'matchingRules',
'matchingruleuse' => 'matchingRuleUse',
'nameform' => 'nameForms',
'objectclass' => 'objectClasses',
'syntax' => 'ldapSyntaxes'
);
/**
* Array of entries belonging to this type
*
* @access protected
* @var array
*/
protected $_attributeTypes = array();
protected $_matchingRules = array();
protected $_matchingRuleUse = array();
protected $_ldapSyntaxes = array();
protected $_objectClasses = array();
protected $_dITContentRules = array();
protected $_dITStructureRules = array();
protected $_nameForms = array();
/**
* hash of all fetched oids
*
* @access protected
* @var array
*/
protected $_oids = array();
/**
* Tells if the schema is initialized
*
* @access protected
* @var boolean
* @see parse(), get()
*/
protected $_initialized = false;
/**
* Constructor of the class
*
* @access protected
*/
protected function __construct()
{
$this->PEAR('Net_LDAP2_Error'); // default error class
}
/**
* Fetch the Schema from an LDAP connection
*
* @param Net_LDAP2 $ldap LDAP connection
* @param string $dn (optional) Subschema entry dn
*
* @access public
* @return Net_LDAP2_Schema|NET_LDAP2_Error
*/
public function fetch($ldap, $dn = null)
{
if (!$ldap instanceof Net_LDAP2) {
return PEAR::raiseError("Unable to fetch Schema: Parameter \$ldap must be a Net_LDAP2 object!");
}
$schema_o = new Net_LDAP2_Schema();
if (is_null($dn)) {
// get the subschema entry via root dse
$dse = $ldap->rootDSE(array('subschemaSubentry'));
if (false == Net_LDAP2::isError($dse)) {
$base = $dse->getValue('subschemaSubentry', 'single');
if (!Net_LDAP2::isError($base)) {
$dn = $base;
}
}
}
// Support for buggy LDAP servers (e.g. Siemens DirX 6.x) that incorrectly
// call this entry subSchemaSubentry instead of subschemaSubentry.
// Note the correct case/spelling as per RFC 2251.
if (is_null($dn)) {
// get the subschema entry via root dse
$dse = $ldap->rootDSE(array('subSchemaSubentry'));
if (false == Net_LDAP2::isError($dse)) {
$base = $dse->getValue('subSchemaSubentry', 'single');
if (!Net_LDAP2::isError($base)) {
$dn = $base;
}
}
}
// Final fallback case where there is no subschemaSubentry attribute
// in the root DSE (this is a bug for an LDAP v3 server so report this
// to your LDAP vendor if you get this far).
if (is_null($dn)) {
$dn = 'cn=Subschema';
}
// fetch the subschema entry
$result = $ldap->search($dn, '(objectClass=*)',
array('attributes' => array_values($schema_o->types),
'scope' => 'base'));
if (Net_LDAP2::isError($result)) {
return $result;
}
$entry = $result->shiftEntry();
if (!$entry instanceof Net_LDAP2_Entry) {
return PEAR::raiseError('Could not fetch Subschema entry');
}
$schema_o->parse($entry);
return $schema_o;
}
/**
* Return a hash of entries for the given type
*
* Returns a hash of entry for th givene type. Types may be:
* objectclasses, attributes, ditcontentrules, ditstructurerules, matchingrules,
* matchingruleuses, nameforms, syntaxes
*
* @param string $type Type to fetch
*
* @access public
* @return array|Net_LDAP2_Error Array or Net_LDAP2_Error
*/
public function &getAll($type)
{
$map = array('objectclasses' => &$this->_objectClasses,
'attributes' => &$this->_attributeTypes,
'ditcontentrules' => &$this->_dITContentRules,
'ditstructurerules' => &$this->_dITStructureRules,
'matchingrules' => &$this->_matchingRules,
'matchingruleuses' => &$this->_matchingRuleUse,
'nameforms' => &$this->_nameForms,
'syntaxes' => &$this->_ldapSyntaxes );
$key = strtolower($type);
$ret = ((key_exists($key, $map)) ? $map[$key] : PEAR::raiseError("Unknown type $type"));
return $ret;
}
/**
* Return a specific entry
*
* @param string $type Type of name
* @param string $name Name or OID to fetch
*
* @access public
* @return mixed Entry or Net_LDAP2_Error
*/
public function &get($type, $name)
{
if ($this->_initialized) {
$type = strtolower($type);
if (false == key_exists($type, $this->types)) {
return PEAR::raiseError("No such type $type");
}
$name = strtolower($name);
$type_var = &$this->{'_' . $this->types[$type]};
if (key_exists($name, $type_var)) {
return $type_var[$name];
} elseif (key_exists($name, $this->_oids) && $this->_oids[$name]['type'] == $type) {
return $this->_oids[$name];
} else {
return PEAR::raiseError("Could not find $type $name");
}
} else {
$return = null;
return $return;
}
}
/**
* Fetches attributes that MAY be present in the given objectclass
*
* @param string $oc Name or OID of objectclass
*
* @access public
* @return array|Net_LDAP2_Error Array with attributes or Net_LDAP2_Error
*/
public function may($oc)
{
return $this->_getAttr($oc, 'may');
}
/**
* Fetches attributes that MUST be present in the given objectclass
*
* @param string $oc Name or OID of objectclass
*
* @access public
* @return array|Net_LDAP2_Error Array with attributes or Net_LDAP2_Error
*/
public function must($oc)
{
return $this->_getAttr($oc, 'must');
}
/**
* Fetches the given attribute from the given objectclass
*
* @param string $oc Name or OID of objectclass
* @param string $attr Name of attribute to fetch
*
* @access protected
* @return array|Net_LDAP2_Error The attribute or Net_LDAP2_Error
*/
protected function _getAttr($oc, $attr)
{
$oc = strtolower($oc);
if (key_exists($oc, $this->_objectClasses) && key_exists($attr, $this->_objectClasses[$oc])) {
return $this->_objectClasses[$oc][$attr];
} elseif (key_exists($oc, $this->_oids) &&
$this->_oids[$oc]['type'] == 'objectclass' &&
key_exists($attr, $this->_oids[$oc])) {
return $this->_oids[$oc][$attr];
} else {
return PEAR::raiseError("Could not find $attr attributes for $oc ");
}
}
/**
* Returns the name(s) of the immediate superclass(es)
*
* @param string $oc Name or OID of objectclass
*
* @access public
* @return array|Net_LDAP2_Error Array of names or Net_LDAP2_Error
*/
public function superclass($oc)
{
$o = $this->get('objectclass', $oc);
if (Net_LDAP2::isError($o)) {
return $o;
}
return (key_exists('sup', $o) ? $o['sup'] : array());
}
/**
* Parses the schema of the given Subschema entry
*
* @param Net_LDAP2_Entry &$entry Subschema entry
*
* @access public
* @return void
*/
public function parse(&$entry)
{
foreach ($this->types as $type => $attr) {
// initialize map type to entry
$type_var = '_' . $attr;
$this->{$type_var} = array();
// get values for this type
if ($entry->exists($attr)) {
$values = $entry->getValue($attr);
if (is_array($values)) {
foreach ($values as $value) {
unset($schema_entry); // this was a real mess without it
// get the schema entry
$schema_entry = $this->_parse_entry($value);
// set the type
$schema_entry['type'] = $type;
// save a ref in $_oids
$this->_oids[$schema_entry['oid']] = &$schema_entry;
// save refs for all names in type map
$names = $schema_entry['aliases'];
array_push($names, $schema_entry['name']);
foreach ($names as $name) {
$this->{$type_var}[strtolower($name)] = &$schema_entry;
}
}
}
}
}
$this->_initialized = true;
}
/**
* Parses an attribute value into a schema entry
*
* @param string $value Attribute value
*
* @access protected
* @return array|false Schema entry array or false
*/
protected function &_parse_entry($value)
{
// tokens that have no value associated
$noValue = array('single-value',
'obsolete',
'collective',
'no-user-modification',
'abstract',
'structural',
'auxiliary');
// tokens that can have multiple values
$multiValue = array('must', 'may', 'sup');
$schema_entry = array('aliases' => array()); // initilization
$tokens = $this->_tokenize($value); // get an array of tokens
// remove surrounding brackets
if ($tokens[0] == '(') array_shift($tokens);
if ($tokens[count($tokens) - 1] == ')') array_pop($tokens); // -1 doesnt work on arrays :-(
$schema_entry['oid'] = array_shift($tokens); // first token is the oid
// cycle over the tokens until none are left
while (count($tokens) > 0) {
$token = strtolower(array_shift($tokens));
if (in_array($token, $noValue)) {
$schema_entry[$token] = 1; // single value token
} else {
// this one follows a string or a list if it is multivalued
if (($schema_entry[$token] = array_shift($tokens)) == '(') {
// this creates the list of values and cycles through the tokens
// until the end of the list is reached ')'
$schema_entry[$token] = array();
while ($tmp = array_shift($tokens)) {
if ($tmp == ')') break;
if ($tmp != '$') array_push($schema_entry[$token], $tmp);
}
}
// create a array if the value should be multivalued but was not
if (in_array($token, $multiValue) && !is_array($schema_entry[$token])) {
$schema_entry[$token] = array($schema_entry[$token]);
}
}
}
// get max length from syntax
if (key_exists('syntax', $schema_entry)) {
if (preg_match('/{(\d+)}/', $schema_entry['syntax'], $matches)) {
$schema_entry['max_length'] = $matches[1];
}
}
// force a name
if (empty($schema_entry['name'])) {
$schema_entry['name'] = $schema_entry['oid'];
}
// make one name the default and put the other ones into aliases
if (is_array($schema_entry['name'])) {
$aliases = $schema_entry['name'];
$schema_entry['name'] = array_shift($aliases);
$schema_entry['aliases'] = $aliases;
}
return $schema_entry;
}
/**
* Tokenizes the given value into an array of tokens
*
* @param string $value String to parse
*
* @access protected
* @return array Array of tokens
*/
protected function _tokenize($value)
{
$tokens = array(); // array of tokens
$matches = array(); // matches[0] full pattern match, [1,2,3] subpatterns
// this one is taken from perl-ldap, modified for php
$pattern = "/\s* (?:([()]) | ([^'\s()]+) | '((?:[^']+|'[^\s)])*)') \s*/x";
/**
* This one matches one big pattern wherin only one of the three subpatterns matched
* We are interested in the subpatterns that matched. If it matched its value will be
* non-empty and so it is a token. Tokens may be round brackets, a string, or a string
* enclosed by '
*/
preg_match_all($pattern, $value, $matches);
for ($i = 0; $i < count($matches[0]); $i++) { // number of tokens (full pattern match)
for ($j = 1; $j < 4; $j++) { // each subpattern
if (null != trim($matches[$j][$i])) { // pattern match in this subpattern
$tokens[$i] = trim($matches[$j][$i]); // this is the token
}
}
}
return $tokens;
}
/**
* Returns wether a attribute syntax is binary or not
*
* This method gets used by Net_LDAP2_Entry to decide which
* PHP function needs to be used to fetch the value in the
* proper format (e.g. binary or string)
*
* @param string $attribute The name of the attribute (eg.: 'sn')
*
* @access public
* @return boolean
*/
public function isBinary($attribute)
{
$return = false; // default to false
// This list contains all syntax that should be treaten as
// containing binary values
// The Syntax Definitons go into constants at the top of this page
$syntax_binary = array(
NET_LDAP2_SYNTAX_OCTET_STRING,
NET_LDAP2_SYNTAX_JPEG
);
// Check Syntax
$attr_s = $this->get('attribute', $attribute);
if (Net_LDAP2::isError($attr_s)) {
// Attribute not found in schema
$return = false; // consider attr not binary
} elseif (isset($attr_s['syntax']) && in_array($attr_s['syntax'], $syntax_binary)) {
// Syntax is defined as binary in schema
$return = true;
} else {
// Syntax not defined as binary, or not found
// if attribute is a subtype, check superior attribute syntaxes
if (isset($attr_s['sup'])) {
foreach ($attr_s['sup'] as $superattr) {
$return = $this->isBinary($superattr);
if ($return) {
break; // stop checking parents since we are binary
}
}
}
}
return $return;
}
// [TODO] add method that allows us to see to which objectclasses a certain attribute belongs to
// it should return the result structured, e.g. sorted in "may" and "must". Optionally it should
// be able to return it just "flat", e.g. array_merge()d.
// We could use get_all() to achieve this easily, i think
}
?>

View File

@ -0,0 +1,59 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
/**
* File containing the Net_LDAP2_SchemaCache interface class.
*
* PHP version 5
*
* @category Net
* @package Net_LDAP2
* @author Benedikt Hallinger <beni@php.net>
* @copyright 2009 Benedikt Hallinger
* @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPLv3
* @version SVN: $Id: SchemaCache.interface.php 286718 2009-08-03 07:30:49Z beni $
* @link http://pear.php.net/package/Net_LDAP2/
*/
/**
* Interface describing a custom schema cache object
*
* To implement a custom schema cache, one must implement this interface and
* pass the instanciated object to Net_LDAP2s registerSchemaCache() method.
*/
interface Net_LDAP2_SchemaCache
{
/**
* Return the schema object from the cache
*
* Net_LDAP2 will consider anything returned invalid, except
* a valid Net_LDAP2_Schema object.
* In case you return a Net_LDAP2_Error, this error will be routed
* to the return of the $ldap->schema() call.
* If you return something else, Net_LDAP2 will
* fetch a fresh Schema object from the LDAP server.
*
* You may want to implement a cache aging mechanism here too.
*
* @return Net_LDAP2_Schema|Net_LDAP2_Error|false
*/
public function loadSchema();
/**
* Store a schema object in the cache
*
* This method will be called, if Net_LDAP2 has fetched a fresh
* schema object from LDAP and wants to init or refresh the cache.
*
* In case of errors you may return a Net_LDAP2_Error which will
* be routet to the client.
* Note that doing this prevents, that the schema object fetched from LDAP
* will be given back to the client, so only return errors if storing
* of the cache is something crucial (e.g. for doing something else with it).
* Normaly you dont want to give back errors in which case Net_LDAP2 needs to
* fetch the schema once per script run and instead use the error
* returned from loadSchema().
*
* @return true|Net_LDAP2_Error
*/
public function storeSchema($schema);
}

614
extlib/Net/LDAP2/Search.php Normal file
View File

@ -0,0 +1,614 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
/**
* File containing the Net_LDAP2_Search interface class.
*
* PHP version 5
*
* @category Net
* @package Net_LDAP2
* @author Tarjej Huse <tarjei@bergfald.no>
* @author Benedikt Hallinger <beni@php.net>
* @copyright 2009 Tarjej Huse, Benedikt Hallinger
* @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPLv3
* @version SVN: $Id: Search.php 286718 2009-08-03 07:30:49Z beni $
* @link http://pear.php.net/package/Net_LDAP2/
*/
/**
* Includes
*/
require_once 'PEAR.php';
/**
* Result set of an LDAP search
*
* @category Net
* @package Net_LDAP2
* @author Tarjej Huse <tarjei@bergfald.no>
* @author Benedikt Hallinger <beni@php.net>
* @license http://www.gnu.org/copyleft/lesser.html LGPL
* @link http://pear.php.net/package/Net_LDAP22/
*/
class Net_LDAP2_Search extends PEAR implements Iterator
{
/**
* Search result identifier
*
* @access protected
* @var resource
*/
protected $_search;
/**
* LDAP resource link
*
* @access protected
* @var resource
*/
protected $_link;
/**
* Net_LDAP2 object
*
* A reference of the Net_LDAP2 object for passing to Net_LDAP2_Entry
*
* @access protected
* @var object Net_LDAP2
*/
protected $_ldap;
/**
* Result entry identifier
*
* @access protected
* @var resource
*/
protected $_entry = null;
/**
* The errorcode the search got
*
* Some errorcodes might be of interest, but might not be best handled as errors.
* examples: 4 - LDAP_SIZELIMIT_EXCEEDED - indicates a huge search.
* Incomplete results are returned. If you just want to check if there's anything in the search.
* than this is a point to handle.
* 32 - no such object - search here returns a count of 0.
*
* @access protected
* @var int
*/
protected $_errorCode = 0; // if not set - sucess!
/**
* Cache for all entries already fetched from iterator interface
*
* @access protected
* @var array
*/
protected $_iteratorCache = array();
/**
* What attributes we searched for
*
* The $attributes array contains the names of the searched attributes and gets
* passed from $Net_LDAP2->search() so the Net_LDAP2_Search object can tell
* what attributes was searched for ({@link searchedAttrs())
*
* This variable gets set from the constructor and returned
* from {@link searchedAttrs()}
*
* @access protected
* @var array
*/
protected $_searchedAttrs = array();
/**
* Cache variable for storing entries fetched internally
*
* This currently is only used by {@link pop_entry()}
*
* @access protected
* @var array
*/
protected $_entry_cache = false;
/**
* Constructor
*
* @param resource &$search Search result identifier
* @param Net_LDAP2|resource &$ldap Net_LDAP2 object or just a LDAP-Link resource
* @param array $attributes (optional) Array with searched attribute names. (see {@link $_searchedAttrs})
*
* @access public
*/
public function __construct(&$search, &$ldap, $attributes = array())
{
$this->PEAR('Net_LDAP2_Error');
$this->setSearch($search);
if ($ldap instanceof Net_LDAP2) {
$this->_ldap =& $ldap;
$this->setLink($this->_ldap->getLink());
} else {
$this->setLink($ldap);
}
$this->_errorCode = @ldap_errno($this->_link);
if (is_array($attributes) && !empty($attributes)) {
$this->_searchedAttrs = $attributes;
}
}
/**
* Returns an array of entry objects
*
* @return array Array of entry objects.
*/
public function entries()
{
$entries = array();
while ($entry = $this->shiftEntry()) {
$entries[] = $entry;
}
return $entries;
}
/**
* Get the next entry in the searchresult.
*
* This will return a valid Net_LDAP2_Entry object or false, so
* you can use this method to easily iterate over the entries inside
* a while loop.
*
* @return Net_LDAP2_Entry|false Reference to Net_LDAP2_Entry object or false
*/
public function &shiftEntry()
{
if ($this->count() == 0 ) {
$false = false;
return $false;
}
if (is_null($this->_entry)) {
$this->_entry = @ldap_first_entry($this->_link, $this->_search);
$entry = Net_LDAP2_Entry::createConnected($this->_ldap, $this->_entry);
if ($entry instanceof Net_LDAP2_Error) $entry = false;
} else {
if (!$this->_entry = @ldap_next_entry($this->_link, $this->_entry)) {
$false = false;
return $false;
}
$entry = Net_LDAP2_Entry::createConnected($this->_ldap, $this->_entry);
if ($entry instanceof Net_LDAP2_Error) $entry = false;
}
return $entry;
}
/**
* Alias function of shiftEntry() for perl-ldap interface
*
* @see shiftEntry()
* @return Net_LDAP2_Entry|false
*/
public function shift_entry()
{
$args = func_get_args();
return call_user_func_array(array( &$this, 'shiftEntry' ), $args);
}
/**
* Retrieve the next entry in the searchresult, but starting from last entry
*
* This is the opposite to {@link shiftEntry()} and is also very useful
* to be used inside a while loop.
*
* @return Net_LDAP2_Entry|false
*/
public function popEntry()
{
if (false === $this->_entry_cache) {
// fetch entries into cache if not done so far
$this->_entry_cache = $this->entries();
}
$return = array_pop($this->_entry_cache);
return (null === $return)? false : $return;
}
/**
* Alias function of popEntry() for perl-ldap interface
*
* @see popEntry()
* @return Net_LDAP2_Entry|false
*/
public function pop_entry()
{
$args = func_get_args();
return call_user_func_array(array( &$this, 'popEntry' ), $args);
}
/**
* Return entries sorted as array
*
* This returns a array with sorted entries and the values.
* Sorting is done with PHPs {@link array_multisort()}.
* This method relies on {@link as_struct()} to fetch the raw data of the entries.
*
* Please note that attribute names are case sensitive!
*
* Usage example:
* <code>
* // to sort entries first by location, then by surename, but descending:
* $entries = $search->sorted_as_struct(array('locality','sn'), SORT_DESC);
* </code>
*
* @param array $attrs Array of attribute names to sort; order from left to right.
* @param int $order Ordering direction, either constant SORT_ASC or SORT_DESC
*
* @return array|Net_LDAP2_Error Array with sorted entries or error
* @todo what about server side sorting as specified in http://www.ietf.org/rfc/rfc2891.txt?
*/
public function sorted_as_struct($attrs = array('cn'), $order = SORT_ASC)
{
/*
* Old Code, suitable and fast for single valued sorting
* This code should be used if we know that single valued sorting is desired,
* but we need some method to get that knowledge...
*/
/*
$attrs = array_reverse($attrs);
foreach ($attrs as $attribute) {
if (!ldap_sort($this->_link, $this->_search, $attribute)){
$this->raiseError("Sorting failed for Attribute " . $attribute);
}
}
$results = ldap_get_entries($this->_link, $this->_search);
unset($results['count']); //for tidier output
if ($order) {
return array_reverse($results);
} else {
return $results;
}*/
/*
* New code: complete "client side" sorting
*/
// first some parameterchecks
if (!is_array($attrs)) {
return PEAR::raiseError("Sorting failed: Parameterlist must be an array!");
}
if ($order != SORT_ASC && $order != SORT_DESC) {
return PEAR::raiseError("Sorting failed: sorting direction not understood! (neither constant SORT_ASC nor SORT_DESC)");
}
// fetch the entries data
$entries = $this->as_struct();
// now sort each entries attribute values
// this is neccessary because later we can only sort by one value,
// so we need the highest or lowest attribute now, depending on the
// selected ordering for that specific attribute
foreach ($entries as $dn => $entry) {
foreach ($entry as $attr_name => $attr_values) {
sort($entries[$dn][$attr_name]);
if ($order == SORT_DESC) {
array_reverse($entries[$dn][$attr_name]);
}
}
}
// reformat entrys array for later use with array_multisort()
$to_sort = array(); // <- will be a numeric array similar to ldap_get_entries
foreach ($entries as $dn => $entry_attr) {
$row = array();
$row['dn'] = $dn;
foreach ($entry_attr as $attr_name => $attr_values) {
$row[$attr_name] = $attr_values;
}
$to_sort[] = $row;
}
// Build columns for array_multisort()
// each requested attribute is one row
$columns = array();
foreach ($attrs as $attr_name) {
foreach ($to_sort as $key => $row) {
$columns[$attr_name][$key] =& $to_sort[$key][$attr_name][0];
}
}
// sort the colums with array_multisort, if there is something
// to sort and if we have requested sort columns
if (!empty($to_sort) && !empty($columns)) {
$sort_params = '';
foreach ($attrs as $attr_name) {
$sort_params .= '$columns[\''.$attr_name.'\'], '.$order.', ';
}
eval("array_multisort($sort_params \$to_sort);"); // perform sorting
}
return $to_sort;
}
/**
* Return entries sorted as objects
*
* This returns a array with sorted Net_LDAP2_Entry objects.
* The sorting is actually done with {@link sorted_as_struct()}.
*
* Please note that attribute names are case sensitive!
* Also note, that it is (depending on server capabilitys) possible to let
* the server sort your results. This happens through search controls
* and is described in detail at {@link http://www.ietf.org/rfc/rfc2891.txt}
*
* Usage example:
* <code>
* // to sort entries first by location, then by surename, but descending:
* $entries = $search->sorted(array('locality','sn'), SORT_DESC);
* </code>
*
* @param array $attrs Array of sort attributes to sort; order from left to right.
* @param int $order Ordering direction, either constant SORT_ASC or SORT_DESC
*
* @return array|Net_LDAP2_Error Array with sorted Net_LDAP2_Entries or error
* @todo Entry object construction could be faster. Maybe we could use one of the factorys instead of fetching the entry again
*/
public function sorted($attrs = array('cn'), $order = SORT_ASC)
{
$return = array();
$sorted = $this->sorted_as_struct($attrs, $order);
if (PEAR::isError($sorted)) {
return $sorted;
}
foreach ($sorted as $key => $row) {
$entry = $this->_ldap->getEntry($row['dn'], $this->searchedAttrs());
if (!PEAR::isError($entry)) {
array_push($return, $entry);
} else {
return $entry;
}
}
return $return;
}
/**
* Return entries as array
*
* This method returns the entries and the selected attributes values as
* array.
* The first array level contains all found entries where the keys are the
* DNs of the entries. The second level arrays contian the entries attributes
* such that the keys is the lowercased name of the attribute and the values
* are stored in another indexed array. Note that the attribute values are stored
* in an array even if there is no or just one value.
*
* The array has the following structure:
* <code>
* $return = array(
* 'cn=foo,dc=example,dc=com' => array(
* 'sn' => array('foo'),
* 'multival' => array('val1', 'val2', 'valN')
* )
* 'cn=bar,dc=example,dc=com' => array(
* 'sn' => array('bar'),
* 'multival' => array('val1', 'valN')
* )
* )
* </code>
*
* @return array associative result array as described above
*/
public function as_struct()
{
$return = array();
$entries = $this->entries();
foreach ($entries as $entry) {
$attrs = array();
$entry_attributes = $entry->attributes();
foreach ($entry_attributes as $attr_name) {
$attr_values = $entry->getValue($attr_name, 'all');
if (!is_array($attr_values)) {
$attr_values = array($attr_values);
}
$attrs[$attr_name] = $attr_values;
}
$return[$entry->dn()] = $attrs;
}
return $return;
}
/**
* Set the search objects resource link
*
* @param resource &$search Search result identifier
*
* @access public
* @return void
*/
public function setSearch(&$search)
{
$this->_search = $search;
}
/**
* Set the ldap ressource link
*
* @param resource &$link Link identifier
*
* @access public
* @return void
*/
public function setLink(&$link)
{
$this->_link = $link;
}
/**
* Returns the number of entries in the searchresult
*
* @return int Number of entries in search.
*/
public function count()
{
// this catches the situation where OL returned errno 32 = no such object!
if (!$this->_search) {
return 0;
}
return @ldap_count_entries($this->_link, $this->_search);
}
/**
* Get the errorcode the object got in its search.
*
* @return int The ldap error number.
*/
public function getErrorCode()
{
return $this->_errorCode;
}
/**
* Destructor
*
* @access protected
*/
public function _Net_LDAP2_Search()
{
@ldap_free_result($this->_search);
}
/**
* Closes search result
*
* @return void
*/
public function done()
{
$this->_Net_LDAP2_Search();
}
/**
* Return the attribute names this search selected
*
* @return array
* @see $_searchedAttrs
* @access protected
*/
protected function searchedAttrs()
{
return $this->_searchedAttrs;
}
/**
* Tells if this search exceeds a sizelimit
*
* @return boolean
*/
public function sizeLimitExceeded()
{
return ($this->getErrorCode() == 4);
}
/*
* SPL Iterator interface methods.
* This interface allows to use Net_LDAP2_Search
* objects directly inside a foreach loop!
*/
/**
* SPL Iterator interface: Return the current element.
*
* The SPL Iterator interface allows you to fetch entries inside
* a foreach() loop: <code>foreach ($search as $dn => $entry) { ...</code>
*
* Of course, you may call {@link current()}, {@link key()}, {@link next()},
* {@link rewind()} and {@link valid()} yourself.
*
* If the search throwed an error, it returns false.
* False is also returned, if the end is reached
* In case no call to next() was made, we will issue one,
* thus returning the first entry.
*
* @return Net_LDAP2_Entry|false
*/
public function current()
{
if (count($this->_iteratorCache) == 0) {
$this->next();
reset($this->_iteratorCache);
}
$entry = current($this->_iteratorCache);
return ($entry instanceof Net_LDAP2_Entry)? $entry : false;
}
/**
* SPL Iterator interface: Return the identifying key (DN) of the current entry.
*
* @see current()
* @return string|false DN of the current entry; false in case no entry is returned by current()
*/
public function key()
{
$entry = $this->current();
return ($entry instanceof Net_LDAP2_Entry)? $entry->dn() :false;
}
/**
* SPL Iterator interface: Move forward to next entry.
*
* After a call to {@link next()}, {@link current()} will return
* the next entry in the result set.
*
* @see current()
* @return void
*/
public function next()
{
// fetch next entry.
// if we have no entrys anymore, we add false (which is
// returned by shiftEntry()) so current() will complain.
if (count($this->_iteratorCache) - 1 <= $this->count()) {
$this->_iteratorCache[] = $this->shiftEntry();
}
// move on array pointer to current element.
// even if we have added all entries, this will
// ensure proper operation in case we rewind()
next($this->_iteratorCache);
}
/**
* SPL Iterator interface: Check if there is a current element after calls to {@link rewind()} or {@link next()}.
*
* Used to check if we've iterated to the end of the collection.
*
* @see current()
* @return boolean FALSE if there's nothing more to iterate over
*/
public function valid()
{
return ($this->current() instanceof Net_LDAP2_Entry);
}
/**
* SPL Iterator interface: Rewind the Iterator to the first element.
*
* After rewinding, {@link current()} will return the first entry in the result set.
*
* @see current()
* @return void
*/
public function rewind()
{
reset($this->_iteratorCache);
}
}
?>

View File

@ -0,0 +1,97 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
/**
* File containing the example simple file based Schema Caching class.
*
* PHP version 5
*
* @category Net
* @package Net_LDAP2
* @author Benedikt Hallinger <beni@php.net>
* @copyright 2009 Benedikt Hallinger
* @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPLv3
* @version SVN: $Id: SimpleFileSchemaCache.php 286718 2009-08-03 07:30:49Z beni $
* @link http://pear.php.net/package/Net_LDAP2/
*/
/**
* A simple file based schema cacher with cache aging.
*
* Once the cache is too old, the loadSchema() method will return false, so
* Net_LDAP2 will fetch a fresh object from the LDAP server that will
* overwrite the current (outdated) old cache.
*/
class Net_LDAP2_SimpleFileSchemaCache implements Net_LDAP2_SchemaCache
{
/**
* Internal config of this cache
*
* @see Net_LDAP2_SimpleFileSchemaCache()
* @var array
*/
protected $config = array(
'path' => '/tmp/Net_LDAP_Schema.cache',
'max_age' => 1200
);
/**
* Initialize the simple cache
*
* Config is as following:
* path Complete path to the cache file.
* max_age Maximum age of cache in seconds, 0 means "endlessly".
*
* @param array $cfg Config array
*/
public function Net_LDAP2_SimpleFileSchemaCache($cfg)
{
foreach ($cfg as $key => $value) {
if (array_key_exists($key, $this->config)) {
if (gettype($this->config[$key]) != gettype($value)) {
$this->getCore()->dropFatalError(__CLASS__.": Could not set config! Key $key does not match type ".gettype($this->config[$key])."!");
}
$this->config[$key] = $value;
} else {
$this->getCore()->dropFatalError(__CLASS__.": Could not set config! Key $key is not defined!");
}
}
}
/**
* Return the schema object from the cache
*
* If file is existent and cache has not expired yet,
* then the cache is deserialized and returned.
*
* @return Net_LDAP2_Schema|Net_LDAP2_Error|false
*/
public function loadSchema()
{
$return = false; // Net_LDAP2 will load schema from LDAP
if (file_exists($this->config['path'])) {
$cache_maxage = filemtime($this->config['path']) + $this->config['max_age'];
if (time() <= $cache_maxage || $this->config['max_age'] == 0) {
$return = unserialize(file_get_contents($this->config['path']));
}
}
return $return;
}
/**
* Store a schema object in the cache
*
* This method will be called, if Net_LDAP2 has fetched a fresh
* schema object from LDAP and wants to init or refresh the cache.
*
* To invalidate the cache and cause Net_LDAP2 to refresh the cache,
* you can call this method with null or false as value.
* The next call to $ldap->schema() will then refresh the caches object.
*
* @param mixed $schema The object that should be cached
* @return true|Net_LDAP2_Error|false
*/
public function storeSchema($schema) {
file_put_contents($this->config['path'], serialize($schema));
return true;
}
}

572
extlib/Net/LDAP2/Util.php Normal file
View File

@ -0,0 +1,572 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
/**
* File containing the Net_LDAP2_Util interface class.
*
* PHP version 5
*
* @category Net
* @package Net_LDAP2
* @author Benedikt Hallinger <beni@php.net>
* @copyright 2009 Benedikt Hallinger
* @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPLv3
* @version SVN: $Id: Util.php 286718 2009-08-03 07:30:49Z beni $
* @link http://pear.php.net/package/Net_LDAP2/
*/
/**
* Includes
*/
require_once 'PEAR.php';
/**
* Utility Class for Net_LDAP2
*
* This class servers some functionality to the other classes of Net_LDAP2 but most of
* the methods can be used separately as well.
*
* @category Net
* @package Net_LDAP2
* @author Benedikt Hallinger <beni@php.net>
* @license http://www.gnu.org/copyleft/lesser.html LGPL
* @link http://pear.php.net/package/Net_LDAP22/
*/
class Net_LDAP2_Util extends PEAR
{
/**
* Constructor
*
* @access public
*/
public function __construct()
{
// We do nothing here, since all methods can be called statically.
// In Net_LDAP <= 0.7, we needed a instance of Util, because
// it was possible to do utf8 encoding and decoding, but this
// has been moved to the LDAP class. The constructor remains only
// here to document the downward compatibility of creating an instance.
}
/**
* Explodes the given DN into its elements
*
* {@link http://www.ietf.org/rfc/rfc2253.txt RFC 2253} says, a Distinguished Name is a sequence
* of Relative Distinguished Names (RDNs), which themselves
* are sets of Attributes. For each RDN a array is constructed where the RDN part is stored.
*
* For example, the DN 'OU=Sales+CN=J. Smith,DC=example,DC=net' is exploded to:
* <kbd>array( [0] => array([0] => 'OU=Sales', [1] => 'CN=J. Smith'), [2] => 'DC=example', [3] => 'DC=net' )</kbd>
*
* [NOT IMPLEMENTED] DNs might also contain values, which are the bytes of the BER encoding of
* the X.500 AttributeValue rather than some LDAP string syntax. These values are hex-encoded
* and prefixed with a #. To distinguish such BER values, ldap_explode_dn uses references to
* the actual values, e.g. '1.3.6.1.4.1.1466.0=#04024869,DC=example,DC=com' is exploded to:
* [ { '1.3.6.1.4.1.1466.0' => "\004\002Hi" }, { 'DC' => 'example' }, { 'DC' => 'com' } ];
* See {@link http://www.vijaymukhi.com/vmis/berldap.htm} for more information on BER.
*
* It also performs the following operations on the given DN:
* - Unescape "\" followed by ",", "+", """, "\", "<", ">", ";", "#", "=", " ", or a hexpair
* and strings beginning with "#".
* - Removes the leading 'OID.' characters if the type is an OID instead of a name.
* - If an RDN contains multiple parts, the parts are re-ordered so that the attribute type names are in alphabetical order.
*
* OPTIONS is a list of name/value pairs, valid options are:
* casefold Controls case folding of attribute types names.
* Attribute values are not affected by this option.
* The default is to uppercase. Valid values are:
* lower Lowercase attribute types names.
* upper Uppercase attribute type names. This is the default.
* none Do not change attribute type names.
* reverse If TRUE, the RDN sequence is reversed.
* onlyvalues If TRUE, then only attributes values are returned ('foo' instead of 'cn=foo')
*
* @param string $dn The DN that should be exploded
* @param array $options Options to use
*
* @static
* @return array Parts of the exploded DN
* @todo implement BER
*/
public static function ldap_explode_dn($dn, $options = array('casefold' => 'upper'))
{
if (!isset($options['onlyvalues'])) $options['onlyvalues'] = false;
if (!isset($options['reverse'])) $options['reverse'] = false;
if (!isset($options['casefold'])) $options['casefold'] = 'upper';
// Escaping of DN and stripping of "OID."
$dn = self::canonical_dn($dn, array('casefold' => $options['casefold']));
// splitting the DN
$dn_array = preg_split('/(?<=[^\\\\]),/', $dn);
// clear wrong splitting (possibly we have split too much)
// /!\ Not clear, if this is neccessary here
//$dn_array = self::correct_dn_splitting($dn_array, ',');
// construct subarrays for multivalued RDNs and unescape DN value
// also convert to output format and apply casefolding
foreach ($dn_array as $key => $value) {
$value_u = self::unescape_dn_value($value);
$rdns = self::split_rdn_multival($value_u[0]);
if (count($rdns) > 1) {
// MV RDN!
foreach ($rdns as $subrdn_k => $subrdn_v) {
// Casefolding
if ($options['casefold'] == 'upper') $subrdn_v = preg_replace("/^(\w+=)/e", "''.strtoupper('\\1').''", $subrdn_v);
if ($options['casefold'] == 'lower') $subrdn_v = preg_replace("/^(\w+=)/e", "''.strtolower('\\1').''", $subrdn_v);
if ($options['onlyvalues']) {
preg_match('/(.+?)(?<!\\\\)=(.+)/', $subrdn_v, $matches);
$rdn_ocl = $matches[1];
$rdn_val = $matches[2];
$unescaped = self::unescape_dn_value($rdn_val);
$rdns[$subrdn_k] = $unescaped[0];
} else {
$unescaped = self::unescape_dn_value($subrdn_v);
$rdns[$subrdn_k] = $unescaped[0];
}
}
$dn_array[$key] = $rdns;
} else {
// normal RDN
// Casefolding
if ($options['casefold'] == 'upper') $value = preg_replace("/^(\w+=)/e", "''.strtoupper('\\1').''", $value);
if ($options['casefold'] == 'lower') $value = preg_replace("/^(\w+=)/e", "''.strtolower('\\1').''", $value);
if ($options['onlyvalues']) {
preg_match('/(.+?)(?<!\\\\)=(.+)/', $value, $matches);
$dn_ocl = $matches[1];
$dn_val = $matches[2];
$unescaped = self::unescape_dn_value($dn_val);
$dn_array[$key] = $unescaped[0];
} else {
$unescaped = self::unescape_dn_value($value);
$dn_array[$key] = $unescaped[0];
}
}
}
if ($options['reverse']) {
return array_reverse($dn_array);
} else {
return $dn_array;
}
}
/**
* Escapes a DN value according to RFC 2253
*
* Escapes the given VALUES according to RFC 2253 so that they can be safely used in LDAP DNs.
* The characters ",", "+", """, "\", "<", ">", ";", "#", "=" with a special meaning in RFC 2252
* are preceeded by ba backslash. Control characters with an ASCII code < 32 are represented as \hexpair.
* Finally all leading and trailing spaces are converted to sequences of \20.
*
* @param array $values An array containing the DN values that should be escaped
*
* @static
* @return array The array $values, but escaped
*/
public static function escape_dn_value($values = array())
{
// Parameter validation
if (!is_array($values)) {
$values = array($values);
}
foreach ($values as $key => $val) {
// Escaping of filter meta characters
$val = str_replace('\\', '\\\\', $val);
$val = str_replace(',', '\,', $val);
$val = str_replace('+', '\+', $val);
$val = str_replace('"', '\"', $val);
$val = str_replace('<', '\<', $val);
$val = str_replace('>', '\>', $val);
$val = str_replace(';', '\;', $val);
$val = str_replace('#', '\#', $val);
$val = str_replace('=', '\=', $val);
// ASCII < 32 escaping
$val = self::asc2hex32($val);
// Convert all leading and trailing spaces to sequences of \20.
if (preg_match('/^(\s*)(.+?)(\s*)$/', $val, $matches)) {
$val = $matches[2];
for ($i = 0; $i < strlen($matches[1]); $i++) {
$val = '\20'.$val;
}
for ($i = 0; $i < strlen($matches[3]); $i++) {
$val = $val.'\20';
}
}
if (null === $val) $val = '\0'; // apply escaped "null" if string is empty
$values[$key] = $val;
}
return $values;
}
/**
* Undoes the conversion done by escape_dn_value().
*
* Any escape sequence starting with a baskslash - hexpair or special character -
* will be transformed back to the corresponding character.
*
* @param array $values Array of DN Values
*
* @return array Same as $values, but unescaped
* @static
*/
public static function unescape_dn_value($values = array())
{
// Parameter validation
if (!is_array($values)) {
$values = array($values);
}
foreach ($values as $key => $val) {
// strip slashes from special chars
$val = str_replace('\\\\', '\\', $val);
$val = str_replace('\,', ',', $val);
$val = str_replace('\+', '+', $val);
$val = str_replace('\"', '"', $val);
$val = str_replace('\<', '<', $val);
$val = str_replace('\>', '>', $val);
$val = str_replace('\;', ';', $val);
$val = str_replace('\#', '#', $val);
$val = str_replace('\=', '=', $val);
// Translate hex code into ascii
$values[$key] = self::hex2asc($val);
}
return $values;
}
/**
* Returns the given DN in a canonical form
*
* Returns false if DN is not a valid Distinguished Name.
* DN can either be a string or an array
* as returned by ldap_explode_dn, which is useful when constructing a DN.
* The DN array may have be indexed (each array value is a OCL=VALUE pair)
* or associative (array key is OCL and value is VALUE).
*
* It performs the following operations on the given DN:
* - Removes the leading 'OID.' characters if the type is an OID instead of a name.
* - Escapes all RFC 2253 special characters (",", "+", """, "\", "<", ">", ";", "#", "="), slashes ("/"), and any other character where the ASCII code is < 32 as \hexpair.
* - Converts all leading and trailing spaces in values to be \20.
* - If an RDN contains multiple parts, the parts are re-ordered so that the attribute type names are in alphabetical order.
*
* OPTIONS is a list of name/value pairs, valid options are:
* casefold Controls case folding of attribute type names.
* Attribute values are not affected by this option. The default is to uppercase.
* Valid values are:
* lower Lowercase attribute type names.
* upper Uppercase attribute type names. This is the default.
* none Do not change attribute type names.
* [NOT IMPLEMENTED] mbcescape If TRUE, characters that are encoded as a multi-octet UTF-8 sequence will be escaped as \(hexpair){2,*}.
* reverse If TRUE, the RDN sequence is reversed.
* separator Separator to use between RDNs. Defaults to comma (',').
*
* Note: The empty string "" is a valid DN, so be sure not to do a "$can_dn == false" test,
* because an empty string evaluates to false. Use the "===" operator instead.
*
* @param array|string $dn The DN
* @param array $options Options to use
*
* @static
* @return false|string The canonical DN or FALSE
* @todo implement option mbcescape
*/
public static function canonical_dn($dn, $options = array('casefold' => 'upper', 'separator' => ','))
{
if ($dn === '') return $dn; // empty DN is valid!
// options check
if (!isset($options['reverse'])) {
$options['reverse'] = false;
} else {
$options['reverse'] = true;
}
if (!isset($options['casefold'])) $options['casefold'] = 'upper';
if (!isset($options['separator'])) $options['separator'] = ',';
if (!is_array($dn)) {
// It is not clear to me if the perl implementation splits by the user defined
// separator or if it just uses this separator to construct the new DN
$dn = preg_split('/(?<=[^\\\\])'.$options['separator'].'/', $dn);
// clear wrong splitting (possibly we have split too much)
$dn = self::correct_dn_splitting($dn, $options['separator']);
} else {
// Is array, check, if the array is indexed or associative
$assoc = false;
foreach ($dn as $dn_key => $dn_part) {
if (!is_int($dn_key)) {
$assoc = true;
}
}
// convert to indexed, if associative array detected
if ($assoc) {
$newdn = array();
foreach ($dn as $dn_key => $dn_part) {
if (is_array($dn_part)) {
ksort($dn_part, SORT_STRING); // we assume here, that the rdn parts are also associative
$newdn[] = $dn_part; // copy array as-is, so we can resolve it later
} else {
$newdn[] = $dn_key.'='.$dn_part;
}
}
$dn =& $newdn;
}
}
// Escaping and casefolding
foreach ($dn as $pos => $dnval) {
if (is_array($dnval)) {
// subarray detected, this means very surely, that we had
// a multivalued dn part, which must be resolved
$dnval_new = '';
foreach ($dnval as $subkey => $subval) {
// build RDN part
if (!is_int($subkey)) {
$subval = $subkey.'='.$subval;
}
$subval_processed = self::canonical_dn($subval);
if (false === $subval_processed) return false;
$dnval_new .= $subval_processed.'+';
}
$dn[$pos] = substr($dnval_new, 0, -1); // store RDN part, strip last plus
} else {
// try to split multivalued RDNS into array
$rdns = self::split_rdn_multival($dnval);
if (count($rdns) > 1) {
// Multivalued RDN was detected!
// The RDN value is expected to be correctly split by split_rdn_multival().
// It's time to sort the RDN and build the DN!
$rdn_string = '';
sort($rdns, SORT_STRING); // Sort RDN keys alphabetically
foreach ($rdns as $rdn) {
$subval_processed = self::canonical_dn($rdn);
if (false === $subval_processed) return false;
$rdn_string .= $subval_processed.'+';
}
$dn[$pos] = substr($rdn_string, 0, -1); // store RDN part, strip last plus
} else {
// no multivalued RDN!
// split at first unescaped "="
$dn_comp = preg_split('/(?<=[^\\\\])=/', $rdns[0], 2);
$ocl = ltrim($dn_comp[0]); // trim left whitespaces 'cause of "cn=foo, l=bar" syntax (whitespace after comma)
$val = $dn_comp[1];
// strip 'OID.', otherwise apply casefolding and escaping
if (substr(strtolower($ocl), 0, 4) == 'oid.') {
$ocl = substr($ocl, 4);
} else {
if ($options['casefold'] == 'upper') $ocl = strtoupper($ocl);
if ($options['casefold'] == 'lower') $ocl = strtolower($ocl);
$ocl = self::escape_dn_value(array($ocl));
$ocl = $ocl[0];
}
// escaping of dn-value
$val = self::escape_dn_value(array($val));
$val = str_replace('/', '\/', $val[0]);
$dn[$pos] = $ocl.'='.$val;
}
}
}
if ($options['reverse']) $dn = array_reverse($dn);
return implode($options['separator'], $dn);
}
/**
* Escapes the given VALUES according to RFC 2254 so that they can be safely used in LDAP filters.
*
* Any control characters with an ACII code < 32 as well as the characters with special meaning in
* LDAP filters "*", "(", ")", and "\" (the backslash) are converted into the representation of a
* backslash followed by two hex digits representing the hexadecimal value of the character.
*
* @param array $values Array of values to escape
*
* @static
* @return array Array $values, but escaped
*/
public static function escape_filter_value($values = array())
{
// Parameter validation
if (!is_array($values)) {
$values = array($values);
}
foreach ($values as $key => $val) {
// Escaping of filter meta characters
$val = str_replace('\\', '\5c', $val);
$val = str_replace('*', '\2a', $val);
$val = str_replace('(', '\28', $val);
$val = str_replace(')', '\29', $val);
// ASCII < 32 escaping
$val = self::asc2hex32($val);
if (null === $val) $val = '\0'; // apply escaped "null" if string is empty
$values[$key] = $val;
}
return $values;
}
/**
* Undoes the conversion done by {@link escape_filter_value()}.
*
* Converts any sequences of a backslash followed by two hex digits into the corresponding character.
*
* @param array $values Array of values to escape
*
* @static
* @return array Array $values, but unescaped
*/
public static function unescape_filter_value($values = array())
{
// Parameter validation
if (!is_array($values)) {
$values = array($values);
}
foreach ($values as $key => $value) {
// Translate hex code into ascii
$values[$key] = self::hex2asc($value);
}
return $values;
}
/**
* Converts all ASCII chars < 32 to "\HEX"
*
* @param string $string String to convert
*
* @static
* @return string
*/
public static function asc2hex32($string)
{
for ($i = 0; $i < strlen($string); $i++) {
$char = substr($string, $i, 1);
if (ord($char) < 32) {
$hex = dechex(ord($char));
if (strlen($hex) == 1) $hex = '0'.$hex;
$string = str_replace($char, '\\'.$hex, $string);
}
}
return $string;
}
/**
* Converts all Hex expressions ("\HEX") to their original ASCII characters
*
* @param string $string String to convert
*
* @static
* @author beni@php.net, heavily based on work from DavidSmith@byu.net
* @return string
*/
public static function hex2asc($string)
{
$string = preg_replace("/\\\([0-9A-Fa-f]{2})/e", "''.chr(hexdec('\\1')).''", $string);
return $string;
}
/**
* Split an multivalued RDN value into an Array
*
* A RDN can contain multiple values, spearated by a plus sign.
* This function returns each separate ocl=value pair of the RDN part.
*
* If no multivalued RDN is detected, an array containing only
* the original rdn part is returned.
*
* For example, the multivalued RDN 'OU=Sales+CN=J. Smith' is exploded to:
* <kbd>array([0] => 'OU=Sales', [1] => 'CN=J. Smith')</kbd>
*
* The method trys to be smart if it encounters unescaped "+" characters, but may fail,
* so ensure escaped "+"es in attr names and attr values.
*
* [BUG] If you have a multivalued RDN with unescaped plus characters
* and there is a unescaped plus sign at the end of an value followed by an
* attribute name containing an unescaped plus, then you will get wrong splitting:
* $rdn = 'OU=Sales+C+N=J. Smith';
* returns:
* array('OU=Sales+C', 'N=J. Smith');
* The "C+" is treaten as value of the first pair instead as attr name of the second pair.
* To prevent this, escape correctly.
*
* @param string $rdn Part of an (multivalued) escaped RDN (eg. ou=foo OR ou=foo+cn=bar)
*
* @static
* @return array Array with the components of the multivalued RDN or Error
*/
public static function split_rdn_multival($rdn)
{
$rdns = preg_split('/(?<!\\\\)\+/', $rdn);
$rdns = self::correct_dn_splitting($rdns, '+');
return array_values($rdns);
}
/**
* Splits a attribute=value syntax into an array
*
* The split will occur at the first unescaped '=' character.
*
* @param string $attr Attribute and Value Syntax
*
* @return array Indexed array: 0=attribute name, 1=attribute value
*/
public static function split_attribute_string($attr)
{
return preg_split('/(?<!\\\\)=/', $attr, 2);
}
/**
* Corrects splitting of dn parts
*
* @param array $dn Raw DN array
* @param array $separator Separator that was used when splitting
*
* @return array Corrected array
* @access protected
*/
protected static function correct_dn_splitting($dn = array(), $separator = ',')
{
foreach ($dn as $key => $dn_value) {
$dn_value = $dn[$key]; // refresh value (foreach caches!)
// if the dn_value is not in attr=value format, then we had an
// unescaped separator character inside the attr name or the value.
// We assume, that it was the attribute value.
// [TODO] To solve this, we might ask the schema. Keep in mind, that UTIL class
// must remain independent from the other classes or connections.
if (!preg_match('/.+(?<!\\\\)=.+/', $dn_value)) {
unset($dn[$key]);
if (array_key_exists($key-1, $dn)) {
$dn[$key-1] = $dn[$key-1].$separator.$dn_value; // append to previous attr value
} else {
$dn[$key+1] = $dn_value.$separator.$dn[$key+1]; // first element: prepend to next attr name
}
}
}
return array_values($dn);
}
}
?>

View File

@ -1,44 +1,58 @@
<?php <?php
// +-----------------------------------------------------------------------+
// | Copyright (c) 2007-2008, Christian Schmidt, Peytz & Co. A/S |
// | All rights reserved. |
// | |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | are met: |
// | |
// | o Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | o Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution.|
// | o The names of the authors may not be used to endorse or promote |
// | products derived from this software without specific prior written |
// | permission. |
// | |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
// | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
// | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
// | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
// | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
// | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
// | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
// | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
// | |
// +-----------------------------------------------------------------------+
// | Author: Christian Schmidt <schmidt at php dot net> |
// +-----------------------------------------------------------------------+
//
// $Id: URL2.php,v 1.10 2008/04/26 21:57:08 schmidt Exp $
//
// Net_URL2 Class (PHP5 Only)
// This code is released under the BSD License - http://www.opensource.org/licenses/bsd-license.php
/** /**
* @license BSD License * Net_URL2, a class representing a URL as per RFC 3986.
*
* PHP version 5
*
* LICENSE:
*
* Copyright (c) 2007-2009, Peytz & Co. A/S
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the distribution.
* * Neither the name of the PHP_LexerGenerator nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @category Networking
* @package Net_URL2
* @author Christian Schmidt <chsc@peytz.dk>
* @copyright 2007-2008 Peytz & Co. A/S
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id: URL2.php 286661 2009-08-02 12:50:54Z schmidt $
* @link http://www.rfc-editor.org/rfc/rfc3986.txt
*/
/**
* Represents a URL as per RFC 3986.
*
* @category Networking
* @package Net_URL2
* @author Christian Schmidt <chsc@peytz.dk>
* @copyright 2007-2008 Peytz & Co. ApS
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
* @version Release: @package_version@
* @link http://pear.php.net/package/Net_URL2
*/ */
class Net_URL2 class Net_URL2
{ {
@ -46,24 +60,24 @@ class Net_URL2
* Do strict parsing in resolve() (see RFC 3986, section 5.2.2). Default * Do strict parsing in resolve() (see RFC 3986, section 5.2.2). Default
* is true. * is true.
*/ */
const OPTION_STRICT = 'strict'; const OPTION_STRICT = 'strict';
/** /**
* Represent arrays in query using PHP's [] notation. Default is true. * Represent arrays in query using PHP's [] notation. Default is true.
*/ */
const OPTION_USE_BRACKETS = 'use_brackets'; const OPTION_USE_BRACKETS = 'use_brackets';
/** /**
* URL-encode query variable keys. Default is true. * URL-encode query variable keys. Default is true.
*/ */
const OPTION_ENCODE_KEYS = 'encode_keys'; const OPTION_ENCODE_KEYS = 'encode_keys';
/** /**
* Query variable separators when parsing the query string. Every character * Query variable separators when parsing the query string. Every character
* is considered a separator. Default is specified by the * is considered a separator. Default is specified by the
* arg_separator.input php.ini setting (this defaults to "&"). * arg_separator.input php.ini setting (this defaults to "&").
*/ */
const OPTION_SEPARATOR_INPUT = 'input_separator'; const OPTION_SEPARATOR_INPUT = 'input_separator';
/** /**
* Query variable separator used when generating the query string. Default * Query variable separator used when generating the query string. Default
@ -75,7 +89,7 @@ class Net_URL2
/** /**
* Default options corresponds to how PHP handles $_GET. * Default options corresponds to how PHP handles $_GET.
*/ */
private $options = array( private $_options = array(
self::OPTION_STRICT => true, self::OPTION_STRICT => true,
self::OPTION_USE_BRACKETS => true, self::OPTION_USE_BRACKETS => true,
self::OPTION_ENCODE_KEYS => true, self::OPTION_ENCODE_KEYS => true,
@ -86,41 +100,43 @@ class Net_URL2
/** /**
* @var string|bool * @var string|bool
*/ */
private $scheme = false; private $_scheme = false;
/** /**
* @var string|bool * @var string|bool
*/ */
private $userinfo = false; private $_userinfo = false;
/** /**
* @var string|bool * @var string|bool
*/ */
private $host = false; private $_host = false;
/** /**
* @var int|bool * @var int|bool
*/ */
private $port = false; private $_port = false;
/** /**
* @var string * @var string
*/ */
private $path = ''; private $_path = '';
/** /**
* @var string|bool * @var string|bool
*/ */
private $query = false; private $_query = false;
/** /**
* @var string|bool * @var string|bool
*/ */
private $fragment = false; private $_fragment = false;
/** /**
* Constructor.
*
* @param string $url an absolute or relative URL * @param string $url an absolute or relative URL
* @param array $options * @param array $options an array of OPTION_xxx constants
*/ */
public function __construct($url, $options = null) public function __construct($url, $options = null)
{ {
@ -130,12 +146,12 @@ class Net_URL2
ini_get('arg_separator.output')); ini_get('arg_separator.output'));
if (is_array($options)) { if (is_array($options)) {
foreach ($options as $optionName => $value) { foreach ($options as $optionName => $value) {
$this->setOption($optionName); $this->setOption($optionName, $value);
} }
} }
if (preg_match('@^([a-z][a-z0-9.+-]*):@i', $url, $reg)) { if (preg_match('@^([a-z][a-z0-9.+-]*):@i', $url, $reg)) {
$this->scheme = $reg[1]; $this->_scheme = $reg[1];
$url = substr($url, strlen($reg[0])); $url = substr($url, strlen($reg[0]));
} }
@ -145,19 +161,58 @@ class Net_URL2
} }
$i = strcspn($url, '?#'); $i = strcspn($url, '?#');
$this->path = substr($url, 0, $i); $this->_path = substr($url, 0, $i);
$url = substr($url, $i); $url = substr($url, $i);
if (preg_match('@^\?([^#]*)@', $url, $reg)) { if (preg_match('@^\?([^#]*)@', $url, $reg)) {
$this->query = $reg[1]; $this->_query = $reg[1];
$url = substr($url, strlen($reg[0])); $url = substr($url, strlen($reg[0]));
} }
if ($url) { if ($url) {
$this->fragment = substr($url, 1); $this->_fragment = substr($url, 1);
} }
} }
/**
* Magic Setter.
*
* This method will magically set the value of a private variable ($var)
* with the value passed as the args
*
* @param string $var The private variable to set.
* @param mixed $arg An argument of any type.
* @return void
*/
public function __set($var, $arg)
{
$method = 'set' . $var;
if (method_exists($this, $method)) {
$this->$method($arg);
}
}
/**
* Magic Getter.
*
* This is the magic get method to retrieve the private variable
* that was set by either __set() or it's setter...
*
* @param string $var The property name to retrieve.
* @return mixed $this->$var Either a boolean false if the
* property is not set or the value
* of the private property.
*/
public function __get($var)
{
$method = 'get' . $var;
if (method_exists($this, $method)) {
return $this->$method();
}
return false;
}
/** /**
* Returns the scheme, e.g. "http" or "urn", or false if there is no * Returns the scheme, e.g. "http" or "urn", or false if there is no
* scheme specified, i.e. if this is a relative URL. * scheme specified, i.e. if this is a relative URL.
@ -166,18 +221,23 @@ class Net_URL2
*/ */
public function getScheme() public function getScheme()
{ {
return $this->scheme; return $this->_scheme;
} }
/** /**
* @param string|bool $scheme * Sets the scheme, e.g. "http" or "urn". Specify false if there is no
* scheme specified, i.e. if this is a relative URL.
*
* @param string|bool $scheme e.g. "http" or "urn", or false if there is no
* scheme specified, i.e. if this is a relative
* URL
* *
* @return void * @return void
* @see getScheme() * @see getScheme()
*/ */
public function setScheme($scheme) public function setScheme($scheme)
{ {
$this->scheme = $scheme; $this->_scheme = $scheme;
} }
/** /**
@ -188,7 +248,9 @@ class Net_URL2
*/ */
public function getUser() public function getUser()
{ {
return $this->userinfo !== false ? preg_replace('@:.*$@', '', $this->userinfo) : false; return $this->_userinfo !== false
? preg_replace('@:.*$@', '', $this->_userinfo)
: false;
} }
/** /**
@ -201,7 +263,9 @@ class Net_URL2
*/ */
public function getPassword() public function getPassword()
{ {
return $this->userinfo !== false ? substr(strstr($this->userinfo, ':'), 1) : false; return $this->_userinfo !== false
? substr(strstr($this->_userinfo, ':'), 1)
: false;
} }
/** /**
@ -212,7 +276,7 @@ class Net_URL2
*/ */
public function getUserinfo() public function getUserinfo()
{ {
return $this->userinfo; return $this->_userinfo;
} }
/** /**
@ -220,15 +284,15 @@ class Net_URL2
* in the userinfo part as username ":" password. * in the userinfo part as username ":" password.
* *
* @param string|bool $userinfo userinfo or username * @param string|bool $userinfo userinfo or username
* @param string|bool $password * @param string|bool $password optional password, or false
* *
* @return void * @return void
*/ */
public function setUserinfo($userinfo, $password = false) public function setUserinfo($userinfo, $password = false)
{ {
$this->userinfo = $userinfo; $this->_userinfo = $userinfo;
if ($password !== false) { if ($password !== false) {
$this->userinfo .= ':' . $password; $this->_userinfo .= ':' . $password;
} }
} }
@ -236,21 +300,24 @@ class Net_URL2
* Returns the host part, or false if there is no authority part, e.g. * Returns the host part, or false if there is no authority part, e.g.
* relative URLs. * relative URLs.
* *
* @return string|bool * @return string|bool a hostname, an IP address, or false
*/ */
public function getHost() public function getHost()
{ {
return $this->host; return $this->_host;
} }
/** /**
* @param string|bool $host * Sets the host part. Specify false if there is no authority part, e.g.
* relative URLs.
*
* @param string|bool $host a hostname, an IP address, or false
* *
* @return void * @return void
*/ */
public function setHost($host) public function setHost($host)
{ {
$this->host = $host; $this->_host = $host;
} }
/** /**
@ -261,65 +328,72 @@ class Net_URL2
*/ */
public function getPort() public function getPort()
{ {
return $this->port; return $this->_port;
} }
/** /**
* @param int|bool $port * Sets the port number. Specify false if there is no port number specified,
* i.e. if the default port is to be used.
*
* @param int|bool $port a port number, or false
* *
* @return void * @return void
*/ */
public function setPort($port) public function setPort($port)
{ {
$this->port = intval($port); $this->_port = intval($port);
} }
/** /**
* Returns the authority part, i.e. [ userinfo "@" ] host [ ":" port ], or * Returns the authority part, i.e. [ userinfo "@" ] host [ ":" port ], or
* false if there is no authority none. * false if there is no authority.
* *
* @return string|bool * @return string|bool
*/ */
public function getAuthority() public function getAuthority()
{ {
if (!$this->host) { if (!$this->_host) {
return false; return false;
} }
$authority = ''; $authority = '';
if ($this->userinfo !== false) { if ($this->_userinfo !== false) {
$authority .= $this->userinfo . '@'; $authority .= $this->_userinfo . '@';
} }
$authority .= $this->host; $authority .= $this->_host;
if ($this->port !== false) { if ($this->_port !== false) {
$authority .= ':' . $this->port; $authority .= ':' . $this->_port;
} }
return $authority; return $authority;
} }
/** /**
* @param string|false $authority * Sets the authority part, i.e. [ userinfo "@" ] host [ ":" port ]. Specify
* false if there is no authority.
*
* @param string|false $authority a hostname or an IP addresse, possibly
* with userinfo prefixed and port number
* appended, e.g. "foo:bar@example.org:81".
* *
* @return void * @return void
*/ */
public function setAuthority($authority) public function setAuthority($authority)
{ {
$this->user = false; $this->_userinfo = false;
$this->pass = false; $this->_host = false;
$this->host = false; $this->_port = false;
$this->port = false; if (preg_match('@^(([^\@]*)\@)?([^:]+)(:(\d*))?$@', $authority, $reg)) {
if (preg_match('@^(([^\@]+)\@)?([^:]+)(:(\d*))?$@', $authority, $reg)) {
if ($reg[1]) { if ($reg[1]) {
$this->userinfo = $reg[2]; $this->_userinfo = $reg[2];
} }
$this->host = $reg[3]; $this->_host = $reg[3];
if (isset($reg[5])) { if (isset($reg[5])) {
$this->port = intval($reg[5]); $this->_port = intval($reg[5]);
} }
} }
} }
@ -331,65 +405,74 @@ class Net_URL2
*/ */
public function getPath() public function getPath()
{ {
return $this->path; return $this->_path;
} }
/** /**
* @param string $path * Sets the path part (possibly an empty string).
*
* @param string $path a path
* *
* @return void * @return void
*/ */
public function setPath($path) public function setPath($path)
{ {
$this->path = $path; $this->_path = $path;
} }
/** /**
* Returns the query string (excluding the leading "?"), or false if "?" * Returns the query string (excluding the leading "?"), or false if "?"
* isn't present in the URL. * is not present in the URL.
* *
* @return string|bool * @return string|bool
* @see self::getQueryVariables() * @see self::getQueryVariables()
*/ */
public function getQuery() public function getQuery()
{ {
return $this->query; return $this->_query;
} }
/** /**
* @param string|bool $query * Sets the query string (excluding the leading "?"). Specify false if "?"
* is not present in the URL.
*
* @param string|bool $query a query string, e.g. "foo=1&bar=2"
* *
* @return void * @return void
* @see self::setQueryVariables() * @see self::setQueryVariables()
*/ */
public function setQuery($query) public function setQuery($query)
{ {
$this->query = $query; $this->_query = $query;
} }
/** /**
* Returns the fragment name, or false if "#" isn't present in the URL. * Returns the fragment name, or false if "#" is not present in the URL.
* *
* @return string|bool * @return string|bool
*/ */
public function getFragment() public function getFragment()
{ {
return $this->fragment; return $this->_fragment;
} }
/** /**
* @param string|bool $fragment * Sets the fragment name. Specify false if "#" is not present in the URL.
*
* @param string|bool $fragment a fragment excluding the leading "#", or
* false
* *
* @return void * @return void
*/ */
public function setFragment($fragment) public function setFragment($fragment)
{ {
$this->fragment = $fragment; $this->_fragment = $fragment;
} }
/** /**
* Returns the query string like an array as the variables would appear in * Returns the query string like an array as the variables would appear in
* $_GET in a PHP script. * $_GET in a PHP script. If the URL does not contain a "?", an empty array
* is returned.
* *
* @return array * @return array
*/ */
@ -398,7 +481,7 @@ class Net_URL2
$pattern = '/[' . $pattern = '/[' .
preg_quote($this->getOption(self::OPTION_SEPARATOR_INPUT), '/') . preg_quote($this->getOption(self::OPTION_SEPARATOR_INPUT), '/') .
']/'; ']/';
$parts = preg_split($pattern, $this->query, -1, PREG_SPLIT_NO_EMPTY); $parts = preg_split($pattern, $this->_query, -1, PREG_SPLIT_NO_EMPTY);
$return = array(); $return = array();
foreach ($parts as $part) { foreach ($parts as $part) {
@ -445,6 +528,8 @@ class Net_URL2
} }
/** /**
* Sets the query string to the specified variable in the query string.
*
* @param array $array (name => value) array * @param array $array (name => value) array
* *
* @return void * @return void
@ -452,11 +537,11 @@ class Net_URL2
public function setQueryVariables(array $array) public function setQueryVariables(array $array)
{ {
if (!$array) { if (!$array) {
$this->query = false; $this->_query = false;
} else { } else {
foreach ($array as $name => $value) { foreach ($array as $name => $value) {
if ($this->getOption(self::OPTION_ENCODE_KEYS)) { if ($this->getOption(self::OPTION_ENCODE_KEYS)) {
$name = rawurlencode($name); $name = self::urlencode($name);
} }
if (is_array($value)) { if (is_array($value)) {
@ -466,19 +551,21 @@ class Net_URL2
: ($name . '=' . $v); : ($name . '=' . $v);
} }
} elseif (!is_null($value)) { } elseif (!is_null($value)) {
$parts[] = $name . '=' . $value; $parts[] = $name . '=' . self::urlencode($value);
} else { } else {
$parts[] = $name; $parts[] = $name;
} }
} }
$this->query = implode($this->getOption(self::OPTION_SEPARATOR_OUTPUT), $this->_query = implode($this->getOption(self::OPTION_SEPARATOR_OUTPUT),
$parts); $parts);
} }
} }
/** /**
* @param string $name * Sets the specified variable in the query string.
* @param mixed $value *
* @param string $name variable name
* @param mixed $value variable value
* *
* @return array * @return array
*/ */
@ -490,7 +577,9 @@ class Net_URL2
} }
/** /**
* @param string $name * Removes the specifed variable from the query string.
*
* @param string $name a query string variable, e.g. "foo" in "?foo=1"
* *
* @return void * @return void
*/ */
@ -511,27 +600,38 @@ class Net_URL2
// See RFC 3986, section 5.3 // See RFC 3986, section 5.3
$url = ""; $url = "";
if ($this->scheme !== false) { if ($this->_scheme !== false) {
$url .= $this->scheme . ':'; $url .= $this->_scheme . ':';
} }
$authority = $this->getAuthority(); $authority = $this->getAuthority();
if ($authority !== false) { if ($authority !== false) {
$url .= '//' . $authority; $url .= '//' . $authority;
} }
$url .= $this->path; $url .= $this->_path;
if ($this->query !== false) { if ($this->_query !== false) {
$url .= '?' . $this->query; $url .= '?' . $this->_query;
} }
if ($this->fragment !== false) { if ($this->_fragment !== false) {
$url .= '#' . $this->fragment; $url .= '#' . $this->_fragment;
} }
return $url; return $url;
} }
/**
* Returns a string representation of this URL.
*
* @return string
* @see toString()
*/
public function __toString()
{
return $this->getURL();
}
/** /**
* Returns a normalized string representation of this URL. This is useful * Returns a normalized string representation of this URL. This is useful
* for comparison of URLs. * for comparison of URLs.
@ -555,36 +655,38 @@ class Net_URL2
// See RFC 3886, section 6 // See RFC 3886, section 6
// Schemes are case-insensitive // Schemes are case-insensitive
if ($this->scheme) { if ($this->_scheme) {
$this->scheme = strtolower($this->scheme); $this->_scheme = strtolower($this->_scheme);
} }
// Hostnames are case-insensitive // Hostnames are case-insensitive
if ($this->host) { if ($this->_host) {
$this->host = strtolower($this->host); $this->_host = strtolower($this->_host);
} }
// Remove default port number for known schemes (RFC 3986, section 6.2.3) // Remove default port number for known schemes (RFC 3986, section 6.2.3)
if ($this->port && if ($this->_port &&
$this->scheme && $this->_scheme &&
$this->port == getservbyname($this->scheme, 'tcp')) { $this->_port == getservbyname($this->_scheme, 'tcp')) {
$this->port = false; $this->_port = false;
} }
// Normalize case of %XX percentage-encodings (RFC 3986, section 6.2.2.1) // Normalize case of %XX percentage-encodings (RFC 3986, section 6.2.2.1)
foreach (array('userinfo', 'host', 'path') as $part) { foreach (array('_userinfo', '_host', '_path') as $part) {
if ($this->$part) { if ($this->$part) {
$this->$part = preg_replace('/%[0-9a-f]{2}/ie', 'strtoupper("\0")', $this->$part); $this->$part = preg_replace('/%[0-9a-f]{2}/ie',
'strtoupper("\0")',
$this->$part);
} }
} }
// Path segment normalization (RFC 3986, section 6.2.2.3) // Path segment normalization (RFC 3986, section 6.2.2.3)
$this->path = self::removeDotSegments($this->path); $this->_path = self::removeDotSegments($this->_path);
// Scheme based normalization (RFC 3986, section 6.2.3) // Scheme based normalization (RFC 3986, section 6.2.3)
if ($this->host && !$this->path) { if ($this->_host && !$this->_path) {
$this->path = '/'; $this->_path = '/';
} }
} }
@ -595,7 +697,7 @@ class Net_URL2
*/ */
public function isAbsolute() public function isAbsolute()
{ {
return (bool) $this->scheme; return (bool) $this->_scheme;
} }
/** /**
@ -608,7 +710,7 @@ class Net_URL2
*/ */
public function resolve($reference) public function resolve($reference)
{ {
if (is_string($reference)) { if (!$reference instanceof Net_URL2) {
$reference = new self($reference); $reference = new self($reference);
} }
if (!$this->isAbsolute()) { if (!$this->isAbsolute()) {
@ -617,54 +719,54 @@ class Net_URL2
// A non-strict parser may ignore a scheme in the reference if it is // A non-strict parser may ignore a scheme in the reference if it is
// identical to the base URI's scheme. // identical to the base URI's scheme.
if (!$this->getOption(self::OPTION_STRICT) && $reference->scheme == $this->scheme) { if (!$this->getOption(self::OPTION_STRICT) && $reference->_scheme == $this->_scheme) {
$reference->scheme = false; $reference->_scheme = false;
} }
$target = new self(''); $target = new self('');
if ($reference->scheme !== false) { if ($reference->_scheme !== false) {
$target->scheme = $reference->scheme; $target->_scheme = $reference->_scheme;
$target->setAuthority($reference->getAuthority()); $target->setAuthority($reference->getAuthority());
$target->path = self::removeDotSegments($reference->path); $target->_path = self::removeDotSegments($reference->_path);
$target->query = $reference->query; $target->_query = $reference->_query;
} else { } else {
$authority = $reference->getAuthority(); $authority = $reference->getAuthority();
if ($authority !== false) { if ($authority !== false) {
$target->setAuthority($authority); $target->setAuthority($authority);
$target->path = self::removeDotSegments($reference->path); $target->_path = self::removeDotSegments($reference->_path);
$target->query = $reference->query; $target->_query = $reference->_query;
} else { } else {
if ($reference->path == '') { if ($reference->_path == '') {
$target->path = $this->path; $target->_path = $this->_path;
if ($reference->query !== false) { if ($reference->_query !== false) {
$target->query = $reference->query; $target->_query = $reference->_query;
} else { } else {
$target->query = $this->query; $target->_query = $this->_query;
} }
} else { } else {
if (substr($reference->path, 0, 1) == '/') { if (substr($reference->_path, 0, 1) == '/') {
$target->path = self::removeDotSegments($reference->path); $target->_path = self::removeDotSegments($reference->_path);
} else { } else {
// Merge paths (RFC 3986, section 5.2.3) // Merge paths (RFC 3986, section 5.2.3)
if ($this->host !== false && $this->path == '') { if ($this->_host !== false && $this->_path == '') {
$target->path = '/' . $this->path; $target->_path = '/' . $this->_path;
} else { } else {
$i = strrpos($this->path, '/'); $i = strrpos($this->_path, '/');
if ($i !== false) { if ($i !== false) {
$target->path = substr($this->path, 0, $i + 1); $target->_path = substr($this->_path, 0, $i + 1);
} }
$target->path .= $reference->path; $target->_path .= $reference->_path;
} }
$target->path = self::removeDotSegments($target->path); $target->_path = self::removeDotSegments($target->_path);
} }
$target->query = $reference->query; $target->_query = $reference->_query;
} }
$target->setAuthority($this->getAuthority()); $target->setAuthority($this->getAuthority());
} }
$target->scheme = $this->scheme; $target->_scheme = $this->_scheme;
} }
$target->fragment = $reference->fragment; $target->_fragment = $reference->_fragment;
return $target; return $target;
} }
@ -677,7 +779,7 @@ class Net_URL2
* *
* @return string a path * @return string a path
*/ */
private static function removeDotSegments($path) public static function removeDotSegments($path)
{ {
$output = ''; $output = '';
@ -685,28 +787,25 @@ class Net_URL2
// method // method
$j = 0; $j = 0;
while ($path && $j++ < 100) { while ($path && $j++ < 100) {
// Step A
if (substr($path, 0, 2) == './') { if (substr($path, 0, 2) == './') {
// Step 2.A
$path = substr($path, 2); $path = substr($path, 2);
} elseif (substr($path, 0, 3) == '../') { } elseif (substr($path, 0, 3) == '../') {
// Step 2.A
$path = substr($path, 3); $path = substr($path, 3);
// Step B
} elseif (substr($path, 0, 3) == '/./' || $path == '/.') { } elseif (substr($path, 0, 3) == '/./' || $path == '/.') {
// Step 2.B
$path = '/' . substr($path, 3); $path = '/' . substr($path, 3);
// Step C
} elseif (substr($path, 0, 4) == '/../' || $path == '/..') { } elseif (substr($path, 0, 4) == '/../' || $path == '/..') {
$path = '/' . substr($path, 4); // Step 2.C
$i = strrpos($output, '/'); $path = '/' . substr($path, 4);
$i = strrpos($output, '/');
$output = $i === false ? '' : substr($output, 0, $i); $output = $i === false ? '' : substr($output, 0, $i);
// Step D
} elseif ($path == '.' || $path == '..') { } elseif ($path == '.' || $path == '..') {
// Step 2.D
$path = ''; $path = '';
// Step E
} else { } else {
// Step 2.E
$i = strpos($path, '/'); $i = strpos($path, '/');
if ($i === 0) { if ($i === 0) {
$i = strpos($path, '/', 1); $i = strpos($path, '/', 1);
@ -722,6 +821,22 @@ class Net_URL2
return $output; return $output;
} }
/**
* Percent-encodes all non-alphanumeric characters except these: _ . - ~
* Similar to PHP's rawurlencode(), except that it also encodes ~ in PHP
* 5.2.x and earlier.
*
* @param $raw the string to encode
* @return string
*/
public static function urlencode($string)
{
$encoded = rawurlencode($string);
// This is only necessary in PHP < 5.3.
$encoded = str_replace('%7E', '~', $encoded);
return $encoded;
}
/** /**
* Returns a Net_URL2 instance representing the canonical URL of the * Returns a Net_URL2 instance representing the canonical URL of the
* currently executing PHP script. * currently executing PHP script.
@ -737,13 +852,13 @@ class Net_URL2
// Begin with a relative URL // Begin with a relative URL
$url = new self($_SERVER['PHP_SELF']); $url = new self($_SERVER['PHP_SELF']);
$url->scheme = isset($_SERVER['HTTPS']) ? 'https' : 'http'; $url->_scheme = isset($_SERVER['HTTPS']) ? 'https' : 'http';
$url->host = $_SERVER['SERVER_NAME']; $url->_host = $_SERVER['SERVER_NAME'];
$port = intval($_SERVER['SERVER_PORT']); $port = intval($_SERVER['SERVER_PORT']);
if ($url->scheme == 'http' && $port != 80 || if ($url->_scheme == 'http' && $port != 80 ||
$url->scheme == 'https' && $port != 443) { $url->_scheme == 'https' && $port != 443) {
$url->port = $port; $url->_port = $port;
} }
return $url; return $url;
} }
@ -773,7 +888,7 @@ class Net_URL2
// Begin with a relative URL // Begin with a relative URL
$url = new self($_SERVER['REQUEST_URI']); $url = new self($_SERVER['REQUEST_URI']);
$url->scheme = isset($_SERVER['HTTPS']) ? 'https' : 'http'; $url->_scheme = isset($_SERVER['HTTPS']) ? 'https' : 'http';
// Set host and possibly port // Set host and possibly port
$url->setAuthority($_SERVER['HTTP_HOST']); $url->setAuthority($_SERVER['HTTP_HOST']);
return $url; return $url;
@ -792,10 +907,10 @@ class Net_URL2
*/ */
function setOption($optionName, $value) function setOption($optionName, $value)
{ {
if (!array_key_exists($optionName, $this->options)) { if (!array_key_exists($optionName, $this->_options)) {
return false; return false;
} }
$this->options[$optionName] = $value; $this->_options[$optionName] = $value;
} }
/** /**
@ -807,7 +922,7 @@ class Net_URL2
*/ */
function getOption($optionName) function getOption($optionName)
{ {
return isset($this->options[$optionName]) return isset($this->_options[$optionName])
? $this->options[$optionName] : false; ? $this->_options[$optionName] : false;
} }
} }

View File

@ -68,6 +68,7 @@ function getPath($req)
*/ */
function handleError($error) function handleError($error)
{ {
//error_log(print_r($error,1));
if ($error->getCode() == DB_DATAOBJECT_ERROR_NODATA) { if ($error->getCode() == DB_DATAOBJECT_ERROR_NODATA) {
return; return;
} }
@ -239,6 +240,7 @@ function main()
if (!$user && common_config('site', 'private') if (!$user && common_config('site', 'private')
&& !isLoginAction($action) && !isLoginAction($action)
&& !preg_match('/rss$/', $action) && !preg_match('/rss$/', $action)
&& !preg_match('/^Api/', $action)
) { ) {
common_redirect(common_local_url('login')); common_redirect(common_local_url('login'));
return; return;

View File

@ -93,6 +93,13 @@ $external_libraries=array(
'include'=>'HTTP/Request.php', 'include'=>'HTTP/Request.php',
'check_class'=>'HTTP_Request' 'check_class'=>'HTTP_Request'
), ),
array(
'name'=>'HTTP_Request2',
'pear'=>'HTTP_Request2',
'url'=>'http://pear.php.net/package/HTTP_Request2',
'include'=>'HTTP/Request2.php',
'check_class'=>'HTTP_Request2'
),
array( array(
'name'=>'Mail', 'name'=>'Mail',
'pear'=>'Mail', 'pear'=>'Mail',
@ -123,6 +130,14 @@ $external_libraries=array(
'include'=>'Net/URL/Mapper.php', 'include'=>'Net/URL/Mapper.php',
'check_class'=>'Net_URL_Mapper' 'check_class'=>'Net_URL_Mapper'
), ),
array(
'name'=>'Net_LDAP2',
'pear'=>'Net_LDAP2',
'url'=>'http://pear.php.net/package/Net_LDAP2',
'deb'=>'php-net-ldap2',
'include'=>'Net/LDAP2.php',
'check_class'=>'Net_LDAP2'
),
array( array(
'name'=>'Net_Socket', 'name'=>'Net_Socket',
'pear'=>'Net_Socket', 'pear'=>'Net_Socket',

View File

@ -1,8 +1,71 @@
// identica badge -- updated to work with the native API, 12-4-2008 // identica badge -- updated to work with the native API, 12-4-2008
// Modified to point to Identi.ca, 2-20-2009 by Zach // Modified to point to Identi.ca, 2-20-2009 by Zach
// Modified for XHTML, 27-9-2009 by Will Daniels
// (see http://willdaniels.co.uk/blog/tech-stuff/26-identica-badge-xhtml)
// copyright Kent Brewster 2008 // copyright Kent Brewster 2008
// see http://kentbrewster.com/identica-badge for info // see http://kentbrewster.com/identica-badge for info
( function() {
function createHTMLElement(tagName) {
if(document.createElementNS)
var elem = document.createElementNS("http://www.w3.org/1999/xhtml", tagName);
else
var elem = document.createElement(tagName);
return elem;
}
function isNumeric(value) {
if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/)) return false;
return true;
}
function markupPost(raw, server) {
var start = 0; var p = createHTMLElement('p');
raw.replace(/((http|https):\/\/|\!|@|#)(([\w_]+)?[^\s]*)/g,
function(sub, type, scheme, url, word, offset, full)
{
if(!scheme && !word) return; // just punctuation
var label = ''; var href = '';
var pretext = full.substr(start, offset - start);
moniker = word.split('_'); // behaviour with underscores differs
if(type == '#') moniker = moniker.join('');
else word = moniker = moniker[0].toLowerCase();
switch(type) {
case 'http://': case 'https://': // html links
href = scheme + '://' + url; break;
case '@': // link users
href = 'http://' + server + '/' + moniker; break;
case '!': // link groups
href = 'http://' + server + '/group/' + moniker; break;
case '#': // link tags
href = 'http://' + server + '/tag/' + moniker; break;
default: // bad call (just reset position for text)
start = offset;
}
if(scheme) { // only urls will have scheme
label = sub; start = offset + sub.length;
} else {
label = word; pretext += type;
start = offset + word.length + type.length;
}
p.appendChild(document.createTextNode(pretext));
var link = createHTMLElement('a');
link.appendChild(document.createTextNode(label));
link.href = href; link.target = '_statusnet';
p.appendChild(link);
});
if(start != raw.length) {
endtext = raw.substr(start);
p.appendChild(document.createTextNode(endtext));
}
return p;
}
(function() {
var trueName = ''; var trueName = '';
for (var i = 0; i < 16; i++) { for (var i = 0; i < 16; i++) {
trueName += String.fromCharCode(Math.floor(Math.random() * 26) + 97); trueName += String.fromCharCode(Math.floor(Math.random() * 26) + 97);
@ -13,7 +76,7 @@
return { return {
runFunction : [], runFunction : [],
init : function(target) { init : function(target) {
var theScripts = document.getElementsByTagName('SCRIPT'); var theScripts = document.getElementsByTagName('script');
for (var i = 0; i < theScripts.length; i++) { for (var i = 0; i < theScripts.length; i++) {
if (theScripts[i].src.match(target)) { if (theScripts[i].src.match(target)) {
$.a = {}; $.a = {};
@ -66,9 +129,17 @@
"server" : "identi.ca" "server" : "identi.ca"
}; };
for (var k in $.d) { if ($.a[k] === undefined) { $.a[k] = $.d[k]; } } for (var k in $.d) { if ($.a[k] === undefined) { $.a[k] = $.d[k]; } }
// fix inout units
if(isNumeric($.a.width)) {
$.a.innerWidth = ($.a.width - 22) + 'px'; $.a.width += 'px';
} else {
$.a.innerWidth = 'auto';
}
if(isNumeric($.a.height)) $.a.height += 'px';
}, },
buildPresentation : function () { buildPresentation : function () {
var ns = document.createElement('style'); var setZoom = ''; if(navigator.appName == 'Microsoft Internet Explorer') setZoom = 'zoom:1;';
var ns = createHTMLElement('style');
document.getElementsByTagName('head')[0].appendChild(ns); document.getElementsByTagName('head')[0].appendChild(ns);
if (!window.createPopup) { if (!window.createPopup) {
ns.appendChild(document.createTextNode('')); ns.appendChild(document.createTextNode(''));
@ -76,36 +147,37 @@
} }
var s = document.styleSheets[document.styleSheets.length - 1]; var s = document.styleSheets[document.styleSheets.length - 1];
var rules = { var rules = {
"" : "{zoom:1;margin:0;padding:0;width:" + $.a.width + "px;background:" + $.a.background + ";border:" + $.a.border + ";font:13px/1.2em tahoma, veranda, arial, helvetica, clean, sans-serif;*font-size:small;*font:x-small;}", "" : "{margin:0px;padding:0px;width:" + $.a.width + ";background:" + $.a.background + ";border:" + $.a.border + ";font:87%/1.2em tahoma, veranda, arial, helvetica, clean, sans-serif;}",
"a" : "{cursor:pointer;text-decoration:none;}", "a" : "{cursor:pointer;text-decoration:none;}",
"a:hover" : "{text-decoration:underline;}", "a:hover" : "{text-decoration:underline;}",
"cite" : "{font-weight:bold;margin:0 0 0 4px;padding:0;display:block;font-style:normal;line-height:" + ($.a.thumbnailSize/2) + "px;}", ".cite" : "{" + setZoom + "font-weight:bold;margin:0px 0px 0px 4px;padding:0px;display:block;font-style:normal;line-height:" + ($.a.thumbnailSize/2) + "px;vertical-align:middle;}",
"cite a" : "{color:#C15D42;}", ".cite a" : "{color:#C15D42;}",
"date":"{font-size:87%;margin:0 0 0 4px;padding:0;display:block;font-style:normal;line-height:" + ($.a.thumbnailSize/2) + "px;}", ".date":"{margin:0px 0px 0px 4px;padding:0px;display:block;font-style:normal;line-height:" + ($.a.thumbnailSize/2) + "px;vertical-align:middle;}",
"date:after" : "{clear:both; content:\".\"; display:block; height:0; visibility:hidden; }", ".date:after" : "{clear:both;content:\".\"; display:block;height:0px;visibility:hidden;}",
"date a" : "{color:#676;}", ".date a" : "{color:#676;}",
"h3" : "{margin:0;padding:" + $.a.padding + "px;font-weight:bold;background:" + $.a.headerBackground + " url('http://" + $.a.server + "/favicon.ico') " + $.a.padding + "px 50% no-repeat;text-indent:" + ($.a.padding + 16) + "px;}", "h3" : "{margin:0px;padding:" + $.a.padding + "px;font-weight:bold;background:" + $.a.headerBackground + " url('http://" + $.a.server + "/favicon.ico') " + $.a.padding + "px 50% no-repeat;padding-left:" + ($.a.padding + 20) + "px;}",
"h3.loading" : "{background-image:url('http://l.yimg.com/us.yimg.com/i/us/my/mw/anim_loading_sm.gif');}", "h3.loading" : "{background-image:url('http://l.yimg.com/us.yimg.com/i/us/my/mw/anim_loading_sm.gif');}",
"h3 a" : "{font-size:92%; color:" + $.a.headerColor + ";}", "h3 a" : "{font-size:92%; color:" + $.a.headerColor + ";}",
"h4" : "{font-weight:normal; background:" + $.a.headerBackground + ";text-align:right;margin:0;padding:" + $.a.padding + "px;}", "h4" : "{font-weight:normal;background:" + $.a.headerBackground + ";text-align:right;margin:0px;padding:" + $.a.padding + "px;}",
"h4 a" : "{font-size:92%; color:" + $.a.headerColor + ";}", "h4 a" : "{font-size:92%; color:" + $.a.headerColor + ";}",
"img":"{float:left; height:" + $.a.thumbnailSize + "px;width:" + $.a.thumbnailSize + "px;border:" + $.a.thumbnailBorder + ";margin-right:" + $.a.padding + "px;}", "img":"{float:left;height:" + $.a.thumbnailSize + "px;width:" + $.a.thumbnailSize + "px;border:" + $.a.thumbnailBorder + ";margin-right:" + $.a.padding + "px;}",
"p" : "{margin:0; padding:0;width:" + ($.a.width - 22) + "px;overflow:hidden;font-size:87%;}", "p" : "{margin:2px 0px 0px 0px;padding:0px;width:" + $.a.innerWidth + ";overflow:hidden;line-height:normal;}",
"p a" : "{color:#C15D42;}", "p a" : "{color:#C15D42;}",
"ul":"{margin:0; padding:0; height:" + $.a.height + "px;width:" + $.a.width + "px;overflow:auto;}", "ul":"{margin:0px; padding:0px; height:" + $.a.height + ";width:" + $.a.innerWidth + ";overflow:auto;}",
"ul li":"{background:" + $.a.evenBackground + ";margin:0;padding:" + $.a.padding + "px;list-style:none;width:" + ($.a.width - 22) + "px;overflow:hidden;border-bottom:1px solid #D8E2D7;}", "ul li":"{background:" + $.a.evenBackground + ";margin:0px;padding:" + $.a.padding + "px;list-style:none;width:auto;overflow:hidden;border-bottom:1px solid #D8E2D7;}",
"ul li:hover":"{background:#f3f8ea;}" "ul li:hover":"{background:#f3f8ea;}"
}; };
var ieRules = ""; var ieRules = "";
// brute-force each and every style rule here to !important // brute-force each and every style rule here to !important
// sometimes you have to take off and nuke the site from orbit; it's the only way to be sure // sometimes you have to take off and nuke the site from orbit; it's the only way to be sure
for (var z in rules) { for (var z in rules) {
var selector = '.' + trueName + ' ' + z; if(z.charAt(0)=='.') var selector = '.' + trueName + '-' + z.substring(1);
else var selector = '.' + trueName + ' ' + z;
var rule = rules[z]; var rule = rules[z];
if (typeof rule === 'string') { if (typeof rule === 'string') {
var important = rule.replace(/;/gi, '!important;'); var important = rule.replace(/;/gi, '!important;');
if (!window.createPopup) { if (!window.createPopup) {
var theRule = document.createTextNode(selector + important); var theRule = document.createTextNode(selector + important + '\n');
ns.appendChild(theRule); ns.appendChild(theRule);
} else { } else {
ieRules += selector + important; ieRules += selector + important;
@ -115,17 +187,17 @@
if (window.createPopup) { s.cssText = ieRules; } if (window.createPopup) { s.cssText = ieRules; }
}, },
buildStructure : function() { buildStructure : function() {
$.s = document.createElement('DIV'); $.s = createHTMLElement('div');
$.s.className = trueName; $.s.className = trueName;
$.s.h = document.createElement('H3'); $.s.h = createHTMLElement('h3');
$.s.h.a = document.createElement('A'); $.s.h.a = createHTMLElement('a');
$.s.h.a.target = '_statusnet'; $.s.h.a.target = '_statusnet';
$.s.h.appendChild($.s.h.a); $.s.h.appendChild($.s.h.a);
$.s.appendChild($.s.h); $.s.appendChild($.s.h);
$.s.r = document.createElement('UL'); $.s.r = createHTMLElement('ul');
$.s.appendChild($.s.r); $.s.appendChild($.s.r);
$.s.f = document.createElement('H4'); $.s.f = createHTMLElement('h4');
var a = document.createElement('A'); var a = createHTMLElement('a');
a.innerHTML = 'get this'; a.innerHTML = 'get this';
a.target = '_blank'; a.target = '_blank';
a.href = 'http://identi.ca/doc/badge'; a.href = 'http://identi.ca/doc/badge';
@ -139,7 +211,7 @@
var id = trueName + '.f.runFunction[' + n + ']'; var id = trueName + '.f.runFunction[' + n + ']';
$.f.runFunction[n] = function(r) { $.f.runFunction[n] = function(r) {
delete($.f.runFunction[n]); delete($.f.runFunction[n]);
var a = document.createElement('A'); var a = createHTMLElement('a');
a.rel = $.a.user; a.rel = $.a.user;
a.rev = r.name; a.rev = r.name;
a.id = r.screen_name; a.id = r.screen_name;
@ -178,10 +250,11 @@
} }
} }
r = $.f.sortArray(r, "status_id", true); r = $.f.sortArray(r, "status_id", true);
$.s.h.className = ''; $.s.h.className = ''; // for IE6
$.s.h.removeAttribute('class');
for (var i = 0; i < r.length; i++) { for (var i = 0; i < r.length; i++) {
var li = document.createElement('LI'); var li = createHTMLElement('li');
var icon = document.createElement('A'); var icon = createHTMLElement('a');
if (r[i] && r[i].url) { if (r[i] && r[i].url) {
icon.href = r[i].url; icon.href = r[i].url;
icon.target = '_statusnet'; icon.target = '_statusnet';
@ -192,17 +265,19 @@
icon.title = 'Visit ' + r[i].screen_name + ' at http://' + $.a.server + '/' + r[i].screen_name; icon.title = 'Visit ' + r[i].screen_name + ' at http://' + $.a.server + '/' + r[i].screen_name;
} }
var img = document.createElement('IMG'); var img = createHTMLElement('img');
img.alt = 'profile image for ' + r[i].screen_name;
img.src = r[i].profile_image_url; img.src = r[i].profile_image_url;
icon.appendChild(img); icon.appendChild(img);
li.appendChild(icon); li.appendChild(icon);
var user = document.createElement('CITE'); var user = createHTMLElement('span');
var a = document.createElement('A'); user.className = trueName + '-cite';
var a = createHTMLElement('a');
a.rel = r[i].id; a.rel = r[i].id;
a.rev = r[i].name; a.rev = r[i].name;
a.id = r[i].screen_name; a.id = r[i].screen_name;
a.innerHTML = r[i].name; a.innerHTML = r[i].name;
a.href = 'http://' + $.a.server + '/' + r[i].screen_name; a.href = 'http://' + $.a.server + '/' + r[i].screen_name;
a.onclick = function() { a.onclick = function() {
$.f.changeUserTo(this); $.f.changeUserTo(this);
@ -210,16 +285,17 @@
}; };
user.appendChild(a); user.appendChild(a);
li.appendChild(user); li.appendChild(user);
var updated = document.createElement('DATE'); var updated = createHTMLElement('span');
updated.className = trueName + '-date';
if (r[i].status && r[i].status.created_at) { if (r[i].status && r[i].status.created_at) {
var date_link = document.createElement('A'); var date_link = createHTMLElement('a');
date_link.innerHTML = r[i].status.created_at.split(/\+/)[0]; date_link.innerHTML = r[i].status.created_at.split(/\+/)[0];
date_link.href = 'http://' + $.a.server + '/notice/' + r[i].status.id; date_link.href = 'http://' + $.a.server + '/notice/' + r[i].status.id;
date_link.target = '_statusnet'; date_link.target = '_statusnet';
updated.appendChild(date_link); updated.appendChild(date_link);
if (r[i].status.in_reply_to_status_id) { if (r[i].status.in_reply_to_status_id) {
updated.appendChild(document.createTextNode(' in reply to ')); updated.appendChild(document.createTextNode(' in reply to '));
var in_reply_to = document.createElement('A'); var in_reply_to = createHTMLElement('a');
in_reply_to.innerHTML = r[i].status.in_reply_to_status_id; in_reply_to.innerHTML = r[i].status.in_reply_to_status_id;
in_reply_to.href = 'http://' + $.a.server + '/notice/' + r[i].status.in_reply_to_status_id; in_reply_to.href = 'http://' + $.a.server + '/notice/' + r[i].status.in_reply_to_status_id;
in_reply_to.target = '_statusnet'; in_reply_to.target = '_statusnet';
@ -229,20 +305,16 @@
updated.innerHTML = 'has not updated yet'; updated.innerHTML = 'has not updated yet';
} }
li.appendChild(updated); li.appendChild(updated);
var p = document.createElement('P'); var p = createHTMLElement('p');
if (r[i].status && r[i].status.text) { if (r[i].status && r[i].status.text) {
var raw = r[i].status.text; var raw = r[i].status.text;
var cooked = raw; p = markupPost(raw, $.a.server);
cooked = cooked.replace(/http:\/\/([^ ]+)/g, "<a href=\"http://$1\" target=\"_statusnet\">http://$1</a>");
cooked = cooked.replace(/@([\w*]+)/g, '@<a href="http://' + $.a.server + '/$1" target=\"_statusnet\">$1</a>');
cooked = cooked.replace(/#([\w*]+)/g, '#<a href="http://' + $.a.server + '/tag/$1" target="_statusnet">$1</a>');
p.innerHTML = cooked;
} }
li.appendChild(p); li.appendChild(p);
var a = p.getElementsByTagName('A'); var a = p.getElementsByTagName('a');
for (var j = 0; j < a.length; j++) { for (var j = 0; j < a.length; j++) {
if (a[j].className == 'changeUserTo') { if (a[j].className == 'changeUserTo') {
a[j].className = ''; a[j].removeAttribute('class');
a[j].href = 'http://' + $.a.server + '/' + a[j].innerHTML; a[j].href = 'http://' + $.a.server + '/' + a[j].innerHTML;
a[j].rel = a[j].innerHTML; a[j].rel = a[j].innerHTML;
a[j].onclick = function() { a[j].onclick = function() {
@ -269,7 +341,7 @@
return r; return r;
}, },
runScript : function(url, id) { runScript : function(url, id) {
var s = document.createElement('script'); var s = createHTMLElement('script');
s.id = id; s.id = id;
s.type ='text/javascript'; s.type ='text/javascript';
s.src = url; s.src = url;
@ -292,3 +364,4 @@
} }
} )(); } )();

View File

@ -24,29 +24,6 @@
* @link http://status.net/ * @link http://status.net/
*/ */
$(document).ready(function(){
if ($('body.user_in').length > 0) {
$('.'+SN.C.S.FormNotice).each(function() { SN.U.FormNoticeEnhancements($(this)); });
$('.form_user_subscribe').each(function() { SN.U.FormXHR($(this)); });
$('.form_user_unsubscribe').each(function() { SN.U.FormXHR($(this)); });
$('.form_favor').each(function() { SN.U.FormXHR($(this)); });
$('.form_disfavor').each(function() { SN.U.FormXHR($(this)); });
$('.form_group_join').each(function() { SN.U.FormXHR($(this)); });
$('.form_group_leave').each(function() { SN.U.FormXHR($(this)); });
$('.form_user_nudge').each(function() { SN.U.FormXHR($(this)); });
SN.U.NoticeReply();
SN.U.NoticeDataAttach();
SN.U.NewDirectMessage();
}
SN.U.NoticeAttachments();
});
var SN = { // StatusNet var SN = { // StatusNet
C: { // Config C: { // Config
I: { // Init I: { // Init
@ -76,6 +53,8 @@ var SN = { // StatusNet
U: { // Utils U: { // Utils
FormNoticeEnhancements: function(form) { FormNoticeEnhancements: function(form) {
form_id = form.attr('id'); form_id = form.attr('id');
$('#'+form_id+' #'+SN.C.S.NoticeDataText).unbind('keyup');
$('#'+form_id+' #'+SN.C.S.NoticeDataText).unbind('keydown');
if (maxLength > 0) { if (maxLength > 0) {
$('#'+form_id+' #'+SN.C.S.NoticeDataText).bind('keyup', function(e) { $('#'+form_id+' #'+SN.C.S.NoticeDataText).bind('keyup', function(e) {
SN.U.Counter(form); SN.U.Counter(form);
@ -91,8 +70,6 @@ var SN = { // StatusNet
if($('body')[0].id != 'conversation') { if($('body')[0].id != 'conversation') {
$('#'+form_id+' textarea').focus(); $('#'+form_id+' textarea').focus();
} }
SN.U.FormNoticeXHR(form);
}, },
SubmitOnReturn: function(event, el) { SubmitOnReturn: function(event, el) {
@ -100,7 +77,7 @@ var SN = { // StatusNet
el.submit(); el.submit();
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
$('#'+el[0].id+' #'+SN.U.NoticeDataText).blur(); $('#'+el[0].id+' #'+SN.C.S.NoticeDataText).blur();
$('body').focus(); $('body').focus();
return false; return false;
} }
@ -122,7 +99,7 @@ var SN = { // StatusNet
var counter = $('#'+form_id+' #'+SN.C.S.NoticeTextCount); var counter = $('#'+form_id+' #'+SN.C.S.NoticeTextCount);
if (remaining.toString() != counter.text()) { if (remaining.toString() != counter.text()) {
if (!SN.C.I.CounterBlackout || remaining == 0) { if (!SN.C.I.CounterBlackout || remaining === 0) {
if (counter.text() != String(remaining)) { if (counter.text() != String(remaining)) {
counter.text(remaining); counter.text(remaining);
} }
@ -213,14 +190,15 @@ var SN = { // StatusNet
} }
else { else {
$('#'+form_id+' #'+SN.C.S.NoticeDataText).val(''); $('#'+form_id+' #'+SN.C.S.NoticeDataText).val('');
SN.U.Counter($('#'+SN.C.S.FormNotice)); SN.U.FormNoticeEnhancements($('#'+form_id));
} }
} }
} }
}, },
success: function(data, textStatus) { success: function(data, textStatus) {
var result;
if ($('#'+SN.C.S.Error, data).length > 0) { if ($('#'+SN.C.S.Error, data).length > 0) {
var result = document._importNode($('p', data)[0], true); result = document._importNode($('p', data)[0], true);
alert(result.textContent || result.innerHTML); alert(result.textContent || result.innerHTML);
} }
else { else {
@ -229,12 +207,12 @@ var SN = { // StatusNet
} }
if ($('#'+SN.C.S.CommandResult, data).length > 0) { if ($('#'+SN.C.S.CommandResult, data).length > 0) {
var result = document._importNode($('p', data)[0], true); result = document._importNode($('p', data)[0], true);
alert(result.textContent || result.innerHTML); alert(result.textContent || result.innerHTML);
} }
else { else {
notice = document._importNode($('li', data)[0], true); notice = document._importNode($('li', data)[0], true);
if ($('#'+notice.id).length == 0) { if ($('#'+notice.id).length === 0) {
var notice_irt_value = $('#'+SN.C.S.NoticeInReplyTo).val(); var notice_irt_value = $('#'+SN.C.S.NoticeInReplyTo).val();
var notice_irt = '#notices_primary #notice-'+notice_irt_value; var notice_irt = '#notices_primary #notice-'+notice_irt_value;
if($('body')[0].id == 'conversation') { if($('body')[0].id == 'conversation') {
@ -256,7 +234,7 @@ var SN = { // StatusNet
$('#'+form_id+' #'+SN.C.S.NoticeDataAttach).val(''); $('#'+form_id+' #'+SN.C.S.NoticeDataAttach).val('');
$('#'+form_id+' #'+SN.C.S.NoticeInReplyTo).val(''); $('#'+form_id+' #'+SN.C.S.NoticeInReplyTo).val('');
$('#'+form_id+' #'+SN.C.S.NoticeDataAttachSelected).remove(); $('#'+form_id+' #'+SN.C.S.NoticeDataAttachSelected).remove();
SN.U.Counter($('#'+SN.C.S.FormNotice)); SN.U.FormNoticeEnhancements($('#'+form_id));
} }
}, },
complete: function(xhr, textStatus) { complete: function(xhr, textStatus) {
@ -287,10 +265,10 @@ var SN = { // StatusNet
replyto = '@' + nick + ' '; replyto = '@' + nick + ' ';
text.val(replyto + text.val().replace(RegExp(replyto, 'i'), '')); text.val(replyto + text.val().replace(RegExp(replyto, 'i'), ''));
$('#'+SN.C.S.FormNotice+' input#'+SN.C.S.NoticeInReplyTo).val(id); $('#'+SN.C.S.FormNotice+' input#'+SN.C.S.NoticeInReplyTo).val(id);
if (text.get(0).setSelectionRange) { if (text[0].setSelectionRange) {
var len = text.val().length; var len = text.val().length;
text.get(0).setSelectionRange(len,len); text[0].setSelectionRange(len,len);
text.get(0).focus(); text[0].focus();
} }
return false; return false;
} }
@ -310,7 +288,7 @@ var SN = { // StatusNet
imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif', imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif',
bgClickToClose : true, bgClickToClose : true,
success : function() { success : function() {
$('#jOverlayContent').append('<button>&#215;</button>'); $('#jOverlayContent').append('<button class="close">&#215;</button>');
$('#jOverlayContent button').click($.closeOverlay); $('#jOverlayContent button').click($.closeOverlay);
}, },
timeout : 0, timeout : 0,
@ -330,7 +308,7 @@ var SN = { // StatusNet
$("a.thumbnail").children('img').hide(); $("a.thumbnail").children('img').hide();
anchor.closest(".entry-title").addClass('ov'); anchor.closest(".entry-title").addClass('ov');
if (anchor.children('img').length == 0) { if (anchor.children('img').length === 0) {
t = setTimeout(function() { t = setTimeout(function() {
$.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) { $.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) {
anchor.append(data); anchor.append(data);
@ -352,9 +330,14 @@ var SN = { // StatusNet
NoticeDataAttach: function() { NoticeDataAttach: function() {
NDA = $('#'+SN.C.S.NoticeDataAttach); NDA = $('#'+SN.C.S.NoticeDataAttach);
NDA.change(function() { NDA.change(function() {
S = '<div id="'+SN.C.S.NoticeDataAttachSelected+'" class="'+SN.C.S.Success+'"><code>'+$(this).val()+'</code> <button>&#215;</button></div>'; S = '<div id="'+SN.C.S.NoticeDataAttachSelected+'" class="'+SN.C.S.Success+'"><code>'+$(this).val()+'</code> <button class="close">&#215;</button></div>';
NDAS = $('#'+SN.C.S.NoticeDataAttachSelected); NDAS = $('#'+SN.C.S.NoticeDataAttachSelected);
(NDAS.length > 0) ? NDAS.replaceWith(S) : $('#'+SN.C.S.FormNotice).append(S); if (NDAS.length > 0) {
NDAS.replaceWith(S);
}
else {
$('#'+SN.C.S.FormNotice).append(S);
}
$('#'+SN.C.S.NoticeDataAttachSelected+' button').click(function(){ $('#'+SN.C.S.NoticeDataAttachSelected+' button').click(function(){
$('#'+SN.C.S.NoticeDataAttachSelected).remove(); $('#'+SN.C.S.NoticeDataAttachSelected).remove();
NDA.val(''); NDA.val('');
@ -367,12 +350,13 @@ var SN = { // StatusNet
NDM.attr({'href':NDM.attr('href')+'&ajax=1'}); NDM.attr({'href':NDM.attr('href')+'&ajax=1'});
NDM.click(function() { NDM.click(function() {
var NDMF = $('.entity_send-a-message form'); var NDMF = $('.entity_send-a-message form');
if (NDMF.length == 0) { if (NDMF.length === 0) {
$.get(NDM.attr('href'), null, function(data) { $.get(NDM.attr('href'), null, function(data) {
$('.entity_send-a-message').append(document._importNode($('form', data).get(0), true)); $('.entity_send-a-message').append(document._importNode($('form', data)[0], true));
NDMF = $('.entity_send-a-message .form_notice'); NDMF = $('.entity_send-a-message .form_notice');
SN.U.FormNoticeXHR(NDMF);
SN.U.FormNoticeEnhancements(NDMF); SN.U.FormNoticeEnhancements(NDMF);
NDMF.append('<button>&#215;</button>'); NDMF.append('<button class="close">&#215;</button>');
$('.entity_send-a-message button').click(function(){ $('.entity_send-a-message button').click(function(){
NDMF.hide(); NDMF.hide();
return false; return false;
@ -387,4 +371,30 @@ var SN = { // StatusNet
}); });
} }
} }
} };
$(document).ready(function(){
if ($('body.user_in').length > 0) {
$('.'+SN.C.S.FormNotice).each(function() {
SN.U.FormNoticeXHR($(this));
SN.U.FormNoticeEnhancements($(this));
});
$('.form_user_subscribe').each(function() { SN.U.FormXHR($(this)); });
$('.form_user_unsubscribe').each(function() { SN.U.FormXHR($(this)); });
$('.form_favor').each(function() { SN.U.FormXHR($(this)); });
$('.form_disfavor').each(function() { SN.U.FormXHR($(this)); });
$('.form_group_join').each(function() { SN.U.FormXHR($(this)); });
$('.form_group_leave').each(function() { SN.U.FormXHR($(this)); });
$('.form_user_nudge').each(function() { SN.U.FormXHR($(this)); });
SN.U.NoticeReply();
SN.U.NoticeDataAttach();
SN.U.NewDirectMessage();
}
SN.U.NoticeAttachments();
});

View File

@ -41,22 +41,18 @@ abstract class ShortUrlApi
return strlen($url) >= common_config('site', 'shorturllength'); return strlen($url) >= common_config('site', 'shorturllength');
} }
protected function http_post($data) { protected function http_post($data)
$ch = curl_init(); {
curl_setopt($ch, CURLOPT_URL, $this->service_url); $request = HTTPClient::start();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = $request->post($this->service_url, null, $data);
curl_setopt($ch, CURLOPT_POST, 1); return $response->getBody();
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if (($code < 200) || ($code >= 400)) return false;
return $response;
} }
protected function http_get($url) { protected function http_get($url)
$encoded_url = urlencode($url); {
return file_get_contents("{$this->service_url}$encoded_url"); $request = HTTPClient::start();
$response = $request->get($this->service_url . urlencode($url));
return $response->getBody();
} }
protected function tidy($response) { protected function tidy($response) {

View File

@ -134,11 +134,20 @@ class ApiAction extends Action
$twitter_user['protected'] = false; # not supported by StatusNet yet $twitter_user['protected'] = false; # not supported by StatusNet yet
$twitter_user['followers_count'] = $profile->subscriberCount(); $twitter_user['followers_count'] = $profile->subscriberCount();
// Need to pull up the user for some of this
$user = $profile->getUser();
$design = $user->getDesign();
$defaultDesign = Design::siteDesign(); $defaultDesign = Design::siteDesign();
if (!$design) $design = $defaultDesign; $design = null;
$user = $profile->getUser();
// Note: some profiles don't have an associated user
if (!empty($user)) {
$design = $user->getDesign();
}
if (empty($design)) {
$design = $defaultDesign;
}
$color = Design::toWebColor(empty($design->backgroundcolor) ? $defaultDesign->backgroundcolor : $design->backgroundcolor); $color = Design::toWebColor(empty($design->backgroundcolor) ? $defaultDesign->backgroundcolor : $design->backgroundcolor);
$twitter_user['profile_background_color'] = ($color == null) ? '' : '#'.$color->hexValue(); $twitter_user['profile_background_color'] = ($color == null) ? '' : '#'.$color->hexValue();
$color = Design::toWebColor(empty($design->textcolor) ? $defaultDesign->textcolor : $design->textcolor); $color = Design::toWebColor(empty($design->textcolor) ? $defaultDesign->textcolor : $design->textcolor);

82
lib/apiprivateauth.php Normal file
View File

@ -0,0 +1,82 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Base class for API actions that only require auth when a site
* is configured to be private
*
* 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
* @package StatusNet
* @author Adrian Lang <mail@adrianlang.de>
* @author Brenda Wallace <shiny@cpan.org>
* @author Craig Andrews <candrews@integralblue.com>
* @author Dan Moore <dan@moore.cx>
* @author Evan Prodromou <evan@status.net>
* @author mEDI <medi@milaro.net>
* @author Sarven Capadisli <csarven@status.net>
* @author Zach Copley <zach@status.net>
* @copyright 2009 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';
/**
* Actions extending this class will require auth only if a site is private
*
* @category API
* @package StatusNet
* @author Adrian Lang <mail@adrianlang.de>
* @author Brenda Wallace <shiny@cpan.org>
* @author Craig Andrews <candrews@integralblue.com>
* @author Dan Moore <dan@moore.cx>
* @author Evan Prodromou <evan@status.net>
* @author mEDI <medi@milaro.net>
* @author Sarven Capadisli <csarven@status.net>
* @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 ApiPrivateAuthAction extends ApiAuthAction
{
/**
* Does this API resource require authentication?
*
* @return boolean true or false
*/
function requiresAuth()
{
// If the site is "private", all API methods except statusnet/config
// need authentication
if (common_config('site', 'private')) {
return true;
}
return false;
}
}

View File

@ -579,6 +579,32 @@ class OnCommand extends Command
} }
} }
class LoginCommand extends Command
{
function execute($channel)
{
$login_token = Login_token::staticGet('user_id',$this->user->id);
if($login_token){
$login_token->delete();
}
$login_token = new Login_token();
$login_token->user_id = $this->user->id;
$login_token->token = common_good_rand(16);
$login_token->created = common_sql_now();
$result = $login_token->insert();
if (!$result) {
common_log_db_error($login_token, 'INSERT', __FILE__);
$channel->error($this->user, sprintf(_('Could not create login token for %s'),
$this->user->nickname));
return;
}
$channel->output($this->user,
sprintf(_('This link is useable only once, and is good for only 2 minutes: %s'),
common_local_url('login',
array('user_id'=>$login_token->user_id, 'token'=>$login_token->token))));
}
}
class HelpCommand extends Command class HelpCommand extends Command
{ {
function execute($channel) function execute($channel)
@ -598,6 +624,7 @@ class HelpCommand extends Command
"reply #<notice_id> - reply to notice with a given id\n". "reply #<notice_id> - reply to notice with a given id\n".
"reply <nickname> - reply to the last notice from user\n". "reply <nickname> - reply to the last notice from user\n".
"join <group> - join group\n". "join <group> - join group\n".
"login - Get a link to login to the web interface\n".
"drop <group> - leave group\n". "drop <group> - leave group\n".
"stats - get your stats\n". "stats - get your stats\n".
"stop - same as 'off'\n". "stop - same as 'off'\n".

View File

@ -41,6 +41,12 @@ class CommandInterpreter
return null; return null;
} }
return new HelpCommand($user); return new HelpCommand($user);
case 'login':
if ($arg) {
return null;
} else {
return new LoginCommand($user);
}
case 'on': case 'on':
if ($arg) { if ($arg) {
list($other, $extra) = $this->split_arg($arg); list($other, $extra) = $this->split_arg($arg);

View File

@ -169,6 +169,7 @@ if (isset($conffile)) {
$_config_files[] = INSTALLDIR.'/config.php'; $_config_files[] = INSTALLDIR.'/config.php';
} }
global $_have_a_config;
$_have_a_config = false; $_have_a_config = false;
foreach ($_config_files as $_config_file) { foreach ($_config_files as $_config_file) {
@ -187,7 +188,7 @@ function _have_config()
// XXX: Throw a conniption if database not installed // XXX: Throw a conniption if database not installed
// XXX: Find a way to use htmlwriter for this instead of handcoded markup // XXX: Find a way to use htmlwriter for this instead of handcoded markup
if (!_have_config()) { if (!_have_config()) {
echo '<p>'. _('No configuation file found. ') .'</p>'; echo '<p>'. _('No configuration file found. ') .'</p>';
echo '<p>'. _('I looked for configuration files in the following places: ') .'<br/> '. implode($_config_files, '<br/>'); echo '<p>'. _('I looked for configuration files in the following places: ') .'<br/> '. implode($_config_files, '<br/>');
echo '<p>'. _('You may wish to run the installer to fix this.') .'</p>'; echo '<p>'. _('You may wish to run the installer to fix this.') .'</p>';
echo '<a href="install.php">'. _('Go to the installer.') .'</a>'; echo '<a href="install.php">'. _('Go to the installer.') .'</a>';

View File

@ -1,179 +0,0 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Utility class for wrapping Curl
*
* 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 HTTP
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @copyright 2009 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);
}
define(CURLCLIENT_VERSION, "0.1");
/**
* Wrapper for Curl
*
* Makes Curl HTTP client calls within our HTTPClient framework
*
* @category HTTP
* @package StatusNet
* @author Evan Prodromou <evan@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 CurlClient extends HTTPClient
{
function __construct()
{
}
function head($url, $headers=null)
{
$ch = curl_init($url);
$this->setup($ch);
curl_setopt_array($ch,
array(CURLOPT_NOBODY => true));
if (!is_null($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
$result = curl_exec($ch);
curl_close($ch);
return $this->parseResults($result);
}
function get($url, $headers=null)
{
$ch = curl_init($url);
$this->setup($ch);
if (!is_null($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
$result = curl_exec($ch);
curl_close($ch);
return $this->parseResults($result);
}
function post($url, $headers=null, $body=null)
{
$ch = curl_init($url);
$this->setup($ch);
curl_setopt($ch, CURLOPT_POST, true);
if (!is_null($body)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
}
if (!is_null($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
$result = curl_exec($ch);
curl_close($ch);
return $this->parseResults($result);
}
function setup($ch)
{
curl_setopt_array($ch,
array(CURLOPT_USERAGENT => $this->userAgent(),
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true));
}
function userAgent()
{
$version = curl_version();
return parent::userAgent() . " CurlClient/".CURLCLIENT_VERSION . " cURL/" . $version['version'];
}
function parseResults($results)
{
$resp = new HTTPResponse();
$lines = explode("\r\n", $results);
if (preg_match("#^HTTP/1.[01] (\d\d\d) .+$#", $lines[0], $match)) {
$resp->code = $match[1];
} else {
throw Exception("Bad format: initial line is not HTTP status line");
}
$lastk = null;
for ($i = 1; $i < count($lines); $i++) {
$l =& $lines[$i];
if (mb_strlen($l) == 0) {
$resp->body = implode("\r\n", array_slice($lines, $i + 1));
break;
}
if (preg_match("#^(\S+):\s+(.*)$#", $l, $match)) {
$k = $match[1];
$v = $match[2];
if (array_key_exists($k, $resp->headers)) {
if (is_array($resp->headers[$k])) {
$resp->headers[$k][] = $v;
} else {
$resp->headers[$k] = array($resp->headers[$k], $v);
}
} else {
$resp->headers[$k] = $v;
}
$lastk = $k;
} else if (preg_match("#^\s+(.*)$#", $l, $match)) {
// continuation line
if (is_null($lastk)) {
throw Exception("Bad format: initial whitespace in headers");
}
$h =& $resp->headers[$lastk];
if (is_array($h)) {
$n = count($h);
$h[$n-1] .= $match[1];
} else {
$h .= $match[1];
}
}
}
return $resp;
}
}

View File

@ -228,8 +228,6 @@ $default =
array('contentlimit' => null), array('contentlimit' => null),
'message' => 'message' =>
array('contentlimit' => null), array('contentlimit' => null),
'http' =>
array('client' => 'curl'), // XXX: should this be the default?
'location' => 'location' =>
array('namespace' => 1), // 1 = geonames, 2 = Yahoo Where on Earth array('namespace' => 1), // 1 = geonames, 2 = Yahoo Where on Earth
); );

View File

@ -271,17 +271,20 @@ class DesignSettingsAction extends AccountSettingsAction
function handlePost() function handlePost()
{ {
// XXX: Robin's workaround for a bug in PHP where $_POST
// and $_FILE are empty in the case that the uploaded
// file is bigger than PHP is configured to handle.
if ($_SERVER['REQUEST_METHOD'] == 'POST') { if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (empty($_POST) && $_SERVER['CONTENT_LENGTH']) {
// 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 ' . $msg = _('The server was unable to handle that much POST ' .
'data (%s bytes) due to its current configuration.'); 'data (%s bytes) due to its current configuration.');
$this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH'])); $this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
return;
} }
} }

View File

@ -70,7 +70,7 @@ class ErrorAction extends Action
*/ */
function extraHeaders() function extraHeaders()
{ {
$status_string = $this->status[$this->code]; $status_string = @self::$status[$this->code];
header('HTTP/1.1 '.$this->code.' '.$status_string); header('HTTP/1.1 '.$this->code.' '.$status_string);
} }
@ -92,7 +92,7 @@ class ErrorAction extends Action
function title() function title()
{ {
return self::$status[$this->code]; return @self::$status[$this->code];
} }
function isReadOnly($args) function isReadOnly($args)

View File

@ -97,7 +97,7 @@ class GroupList extends Widget
$this->out->elementStart('a', array('href' => $this->group->homeUrl(), $this->out->elementStart('a', array('href' => $this->group->homeUrl(),
'class' => 'url', 'class' => 'url',
'rel' => 'group')); 'rel' => 'contact group'));
$this->out->element('img', array('src' => $logo, $this->out->element('img', array('src' => $logo,
'class' => 'photo avatar', 'class' => 'photo avatar',
'width' => AVATAR_STREAM_SIZE, 'width' => AVATAR_STREAM_SIZE,
@ -105,48 +105,32 @@ class GroupList extends Widget
'alt' => 'alt' =>
($this->group->fullname) ? $this->group->fullname : ($this->group->fullname) ? $this->group->fullname :
$this->group->nickname)); $this->group->nickname));
$hasFN = ($this->group->fullname) ? 'nickname url uid' : 'fn org nickname url uid'; $hasFN = ($this->group->fullname) ? 'nickname' : 'fn org nickname';
$this->out->elementStart('span', $hasFN); $this->out->elementStart('span', $hasFN);
$this->out->raw($this->highlight($this->group->nickname)); $this->out->raw($this->highlight($this->group->nickname));
$this->out->elementEnd('span'); $this->out->elementEnd('span');
$this->out->elementEnd('a'); $this->out->elementEnd('a');
if ($this->group->fullname) { if ($this->group->fullname) {
$this->out->elementStart('dl', 'entity_fn');
$this->out->element('dt', null, 'Full name');
$this->out->elementStart('dd');
$this->out->elementStart('span', 'fn org'); $this->out->elementStart('span', 'fn org');
$this->out->raw($this->highlight($this->group->fullname)); $this->out->raw($this->highlight($this->group->fullname));
$this->out->elementEnd('span'); $this->out->elementEnd('span');
$this->out->elementEnd('dd');
$this->out->elementEnd('dl');
} }
if ($this->group->location) { if ($this->group->location) {
$this->out->elementStart('dl', 'entity_location'); $this->out->elementStart('span', 'label');
$this->out->element('dt', null, _('Location'));
$this->out->elementStart('dd', 'label');
$this->out->raw($this->highlight($this->group->location)); $this->out->raw($this->highlight($this->group->location));
$this->out->elementEnd('dd'); $this->out->elementEnd('span');
$this->out->elementEnd('dl');
} }
if ($this->group->homepage) { if ($this->group->homepage) {
$this->out->elementStart('dl', 'entity_url');
$this->out->element('dt', null, _('URL'));
$this->out->elementStart('dd');
$this->out->elementStart('a', array('href' => $this->group->homepage, $this->out->elementStart('a', array('href' => $this->group->homepage,
'class' => 'url')); 'class' => 'url'));
$this->out->raw($this->highlight($this->group->homepage)); $this->out->raw($this->highlight($this->group->homepage));
$this->out->elementEnd('a'); $this->out->elementEnd('a');
$this->out->elementEnd('dd');
$this->out->elementEnd('dl');
} }
if ($this->group->description) { if ($this->group->description) {
$this->out->elementStart('dl', 'entity_note'); $this->out->elementStart('p', 'note');
$this->out->element('dt', null, _('Note'));
$this->out->elementStart('dd', 'note');
$this->out->raw($this->highlight($this->group->description)); $this->out->raw($this->highlight($this->group->description));
$this->out->elementEnd('dd'); $this->out->elementEnd('p');
$this->out->elementEnd('dl');
} }
# If we're on a list with an owner (subscriptions or subscribers)... # If we're on a list with an owner (subscriptions or subscribers)...

View File

@ -31,6 +31,9 @@ if (!defined('STATUSNET')) {
exit(1); exit(1);
} }
require_once 'HTTP/Request2.php';
require_once 'HTTP/Request2/Response.php';
/** /**
* Useful structure for HTTP responses * Useful structure for HTTP responses
* *
@ -38,18 +41,53 @@ if (!defined('STATUSNET')) {
* ways of doing them. This class hides the specifics of what underlying * ways of doing them. This class hides the specifics of what underlying
* library (curl or PHP-HTTP or whatever) that's used. * library (curl or PHP-HTTP or whatever) that's used.
* *
* This extends the HTTP_Request2_Response class with methods to get info
* about any followed redirects.
*
* @category HTTP * @category HTTP
* @package StatusNet * @package StatusNet
* @author Evan Prodromou <evan@status.net> * @author Evan Prodromou <evan@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @author Brion Vibber <brion@status.net>
* @link http://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 HTTPResponse extends HTTP_Request2_Response
class HTTPResponse
{ {
public $code = null; function __construct(HTTP_Request2_Response $response, $url, $redirects=0)
public $headers = array(); {
public $body = null; foreach (get_object_vars($response) as $key => $val) {
$this->$key = $val;
}
$this->url = strval($url);
$this->redirectCount = intval($redirects);
}
/**
* Get the count of redirects that have been followed, if any.
* @return int
*/
function getRedirectCount()
{
return $this->redirectCount;
}
/**
* Gets the final target URL, after any redirects have been followed.
* @return string URL
*/
function getUrl()
{
return $this->url;
}
/**
* Check if the response is OK, generally a 200 status code.
* @return bool
*/
function isOk()
{
return ($this->getStatus() == 200);
}
} }
/** /**
@ -59,64 +97,163 @@ class HTTPResponse
* ways of doing them. This class hides the specifics of what underlying * ways of doing them. This class hides the specifics of what underlying
* library (curl or PHP-HTTP or whatever) that's used. * library (curl or PHP-HTTP or whatever) that's used.
* *
* This extends the PEAR HTTP_Request2 package:
* - sends StatusNet-specific User-Agent header
* - 'follow_redirects' config option, defaulting off
* - 'max_redirs' config option, defaulting to 10
* - extended response class adds getRedirectCount() and getUrl() methods
* - get() and post() convenience functions return body content directly
*
* @category HTTP * @category HTTP
* @package StatusNet * @package StatusNet
* @author Evan Prodromou <evan@status.net> * @author Evan Prodromou <evan@status.net>
* @author Brion Vibber <brion@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/ * @link http://status.net/
*/ */
class HTTPClient class HTTPClient extends HTTP_Request2
{ {
static $_client = null;
static function start() function __construct($url=null, $method=self::METHOD_GET, $config=array())
{ {
if (!is_null(self::$_client)) { $this->config['max_redirs'] = 10;
return self::$_client; $this->config['follow_redirects'] = true;
parent::__construct($url, $method, $config);
$this->setHeader('User-Agent', $this->userAgent());
}
/**
* Convenience/back-compat instantiator
* @return HTTPClient
*/
public static function start()
{
return new HTTPClient();
}
/**
* Convenience function to run a GET request.
*
* @return HTTPResponse
* @throws HTTP_Request2_Exception
*/
public function get($url, $headers=array())
{
return $this->doRequest($url, self::METHOD_GET, $headers);
}
/**
* Convenience function to run a HEAD request.
*
* @return HTTPResponse
* @throws HTTP_Request2_Exception
*/
public function head($url, $headers=array())
{
return $this->doRequest($url, self::METHOD_HEAD, $headers);
}
/**
* Convenience function to POST form data.
*
* @param string $url
* @param array $headers optional associative array of HTTP headers
* @param array $data optional associative array or blob of form data to submit
* @return HTTPResponse
* @throws HTTP_Request2_Exception
*/
public function post($url, $headers=array(), $data=array())
{
if ($data) {
$this->addPostParameter($data);
} }
return $this->doRequest($url, self::METHOD_POST, $headers);
}
$type = common_config('http', 'client'); /**
* @return HTTPResponse
switch ($type) { * @throws HTTP_Request2_Exception
case 'curl': */
self::$_client = new CurlClient(); protected function doRequest($url, $method, $headers)
break; {
default: $this->setUrl($url);
throw new Exception("Unknown HTTP client type '$type'"); $this->setMethod($method);
break; if ($headers) {
foreach ($headers as $header) {
$this->setHeader($header);
}
} }
$response = $this->send();
return self::$_client; return $response;
} }
function head($url, $headers) protected function log($level, $detail) {
{ $method = $this->getMethod();
throw new Exception("HEAD method unimplemented"); $url = $this->getUrl();
} common_log($level, __CLASS__ . ": HTTP $method $url - $detail");
function get($url, $headers)
{
throw new Exception("GET method unimplemented");
}
function post($url, $headers, $body)
{
throw new Exception("POST method unimplemented");
}
function put($url, $headers, $body)
{
throw new Exception("PUT method unimplemented");
}
function delete($url, $headers)
{
throw new Exception("DELETE method unimplemented");
} }
/**
* Pulls up StatusNet's customized user-agent string, so services
* we hit can track down the responsible software.
*
* @return string
*/
function userAgent() function userAgent()
{ {
return "StatusNet/".STATUSNET_VERSION." (".STATUSNET_CODENAME.")"; return "StatusNet/".STATUSNET_VERSION." (".STATUSNET_CODENAME.")";
} }
/**
* Actually performs the HTTP request and returns an HTTPResponse object
* with response body and header info.
*
* Wraps around parent send() to add logging and redirection processing.
*
* @return HTTPResponse
* @throw HTTP_Request2_Exception
*/
public function send()
{
$maxRedirs = intval($this->config['max_redirs']);
if (empty($this->config['follow_redirects'])) {
$maxRedirs = 0;
}
$redirs = 0;
do {
try {
$response = parent::send();
} catch (HTTP_Request2_Exception $e) {
$this->log(LOG_ERR, $e->getMessage());
throw $e;
}
$code = $response->getStatus();
if ($code >= 200 && $code < 300) {
$reason = $response->getReasonPhrase();
$this->log(LOG_INFO, "$code $reason");
} elseif ($code >= 300 && $code < 400) {
$url = $this->getUrl();
$target = $response->getHeader('Location');
if (++$redirs >= $maxRedirs) {
common_log(LOG_ERR, __CLASS__ . ": Too many redirects: skipping $code redirect from $url to $target");
break;
}
try {
$this->setUrl($target);
$this->setHeader('Referer', $url);
common_log(LOG_INFO, __CLASS__ . ": Following $code redirect from $url to $target");
continue;
} catch (HTTP_Request2_Exception $e) {
common_log(LOG_ERR, __CLASS__ . ": Invalid $code redirect from $url to $target");
}
} else {
$reason = $response->getReasonPhrase();
$this->log(LOG_ERR, "$code $reason");
}
break;
} while ($maxRedirs);
return new HTTPResponse($response, $this->getUrl(), $redirs);
}
} }

View File

@ -72,14 +72,19 @@ class ImageFile
break; break;
case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE: case UPLOAD_ERR_FORM_SIZE:
throw new Exception(sprintf(_('That file is too big. The maximum file size is %d.'), throw new Exception(sprintf(_('That file is too big. The maximum file size is %s.'),
ImageFile::maxFileSize())); ImageFile::maxFileSize()));
return; return;
case UPLOAD_ERR_PARTIAL: case UPLOAD_ERR_PARTIAL:
@unlink($_FILES[$param]['tmp_name']); @unlink($_FILES[$param]['tmp_name']);
throw new Exception(_('Partial upload.')); throw new Exception(_('Partial upload.'));
return; return;
case UPLOAD_ERR_NO_FILE:
// No file; probably just a non-AJAX submission.
return;
default: default:
common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " .
$_FILES[$param]['error']);
throw new Exception(_('System error uploading file.')); throw new Exception(_('System error uploading file.'));
return; return;
} }

View File

@ -100,38 +100,39 @@ function get_nice_language_list()
* @return array mapping of language codes to language info * @return array mapping of language codes to language info
*/ */
function get_all_languages() { function get_all_languages() {
return array( return array(
'bg' => array('q' => 0.8, 'lang' => 'bg', 'name' => 'Bulgarian', 'direction' => 'ltr'), 'bg' => array('q' => 0.8, 'lang' => 'bg', 'name' => 'Bulgarian', 'direction' => 'ltr'),
'ca' => array('q' => 0.5, 'lang' => 'ca', 'name' => 'Catalan', 'direction' => 'ltr'), 'ca' => array('q' => 0.5, 'lang' => 'ca', 'name' => 'Catalan', 'direction' => 'ltr'),
'cs' => array('q' => 0.5, 'lang' => 'cs', 'name' => 'Czech', 'direction' => 'ltr'), 'cs' => array('q' => 0.5, 'lang' => 'cs', 'name' => 'Czech', 'direction' => 'ltr'),
'de' => array('q' => 0.8, 'lang' => 'de', 'name' => 'German', 'direction' => 'ltr'), 'de' => array('q' => 0.8, 'lang' => 'de', 'name' => 'German', 'direction' => 'ltr'),
'el' => array('q' => 0.1, 'lang' => 'el', 'name' => 'Greek', 'direction' => 'ltr'), 'el' => array('q' => 0.1, 'lang' => 'el', 'name' => 'Greek', 'direction' => 'ltr'),
'en-us' => array('q' => 1, 'lang' => 'en', 'name' => 'English (US)', 'direction' => 'ltr'), 'en-us' => array('q' => 1, 'lang' => 'en', 'name' => 'English (US)', 'direction' => 'ltr'),
'en-gb' => array('q' => 1, 'lang' => 'en_GB', 'name' => 'English (British)', 'direction' => 'ltr'), 'en-gb' => array('q' => 1, 'lang' => 'en_GB', 'name' => 'English (British)', 'direction' => 'ltr'),
'en' => array('q' => 1, 'lang' => 'en', 'name' => 'English (US)', 'direction' => 'ltr'), 'en' => array('q' => 1, 'lang' => 'en', 'name' => 'English (US)', 'direction' => 'ltr'),
'es' => array('q' => 1, 'lang' => 'es', 'name' => 'Spanish', 'direction' => 'ltr'), 'es' => array('q' => 1, 'lang' => 'es', 'name' => 'Spanish', 'direction' => 'ltr'),
'fi' => array('q' => 1, 'lang' => 'fi', 'name' => 'Finnish', 'direction' => 'ltr'), 'fi' => array('q' => 1, 'lang' => 'fi', 'name' => 'Finnish', 'direction' => 'ltr'),
'fr-fr' => array('q' => 1, 'lang' => 'fr', 'name' => 'French', 'direction' => 'ltr'), 'fr-fr' => array('q' => 1, 'lang' => 'fr', 'name' => 'French', 'direction' => 'ltr'),
'ga' => array('q' => 0.5, 'lang' => 'ga', 'name' => 'Galician', 'direction' => 'ltr'), 'ga' => array('q' => 0.5, 'lang' => 'ga', 'name' => 'Galician', 'direction' => 'ltr'),
'he' => array('q' => 0.5, 'lang' => 'he', 'name' => 'Hebrew', 'direction' => 'rtl'), 'he' => array('q' => 0.5, 'lang' => 'he', 'name' => 'Hebrew', 'direction' => 'rtl'),
'it' => array('q' => 1, 'lang' => 'it', 'name' => 'Italian', 'direction' => 'ltr'), 'is' => array('q' => 0.1, 'lang' => 'is', 'name' => 'Icelandic', 'direction' => 'ltr'),
'jp' => array('q' => 0.5, 'lang' => 'ja', 'name' => 'Japanese', 'direction' => 'ltr'), 'it' => array('q' => 1, 'lang' => 'it', 'name' => 'Italian', 'direction' => 'ltr'),
'ko' => array('q' => 0.9, 'lang' => 'ko', 'name' => 'Korean', 'direction' => 'ltr'), 'jp' => array('q' => 0.5, 'lang' => 'ja', 'name' => 'Japanese', 'direction' => 'ltr'),
'mk' => array('q' => 0.5, 'lang' => 'mk', 'name' => 'Macedonian', 'direction' => 'ltr'), 'ko' => array('q' => 0.9, 'lang' => 'ko', 'name' => 'Korean', 'direction' => 'ltr'),
'nb' => array('q' => 0.1, 'lang' => 'nb', 'name' => 'Norwegian (Bokmål)', 'direction' => 'ltr'), 'mk' => array('q' => 0.5, 'lang' => 'mk', 'name' => 'Macedonian', 'direction' => 'ltr'),
'no' => array('q' => 0.1, 'lang' => 'nb', 'name' => 'Norwegian (Bokmål)', 'direction' => 'ltr'), 'nb' => array('q' => 0.1, 'lang' => 'nb', 'name' => 'Norwegian (Bokmål)', 'direction' => 'ltr'),
'nn' => array('q' => 1, 'lang' => 'nn', 'name' => 'Norwegian (Nynorsk)', 'direction' => 'ltr'), 'no' => array('q' => 0.1, 'lang' => 'nb', 'name' => 'Norwegian (Bokmål)', 'direction' => 'ltr'),
'nl' => array('q' => 0.5, 'lang' => 'nl', 'name' => 'Dutch', 'direction' => 'ltr'), 'nn' => array('q' => 1, 'lang' => 'nn', 'name' => 'Norwegian (Nynorsk)', 'direction' => 'ltr'),
'pl' => array('q' => 0.5, 'lang' => 'pl', 'name' => 'Polish', 'direction' => 'ltr'), 'nl' => array('q' => 0.5, 'lang' => 'nl', 'name' => 'Dutch', 'direction' => 'ltr'),
'pt' => array('q' => 0.1, 'lang' => 'pt', 'name' => 'Portuguese', 'direction' => 'ltr'), 'pl' => array('q' => 0.5, 'lang' => 'pl', 'name' => 'Polish', 'direction' => 'ltr'),
'pt-br' => array('q' => 0.9, 'lang' => 'pt_BR', 'name' => 'Portuguese Brazil', 'direction' => 'ltr'), 'pt' => array('q' => 0.1, 'lang' => 'pt', 'name' => 'Portuguese', 'direction' => 'ltr'),
'ru' => array('q' => 0.9, 'lang' => 'ru', 'name' => 'Russian', 'direction' => 'ltr'), 'pt-br' => array('q' => 0.9, 'lang' => 'pt_BR', 'name' => 'Portuguese Brazil', 'direction' => 'ltr'),
'sv' => array('q' => 0.8, 'lang' => 'sv', 'name' => 'Swedish', 'direction' => 'ltr'), 'ru' => array('q' => 0.9, 'lang' => 'ru', 'name' => 'Russian', 'direction' => 'ltr'),
'te' => array('q' => 0.3, 'lang' => 'te', 'name' => 'Telugu', 'direction' => 'ltr'), 'sv' => array('q' => 0.8, 'lang' => 'sv', 'name' => 'Swedish', 'direction' => 'ltr'),
'tr' => array('q' => 0.5, 'lang' => 'tr', 'name' => 'Turkish', 'direction' => 'ltr'), 'te' => array('q' => 0.3, 'lang' => 'te', 'name' => 'Telugu', 'direction' => 'ltr'),
'uk' => array('q' => 1, 'lang' => 'uk', 'name' => 'Ukrainian', 'direction' => 'ltr'), 'tr' => array('q' => 0.5, 'lang' => 'tr', 'name' => 'Turkish', 'direction' => 'ltr'),
'vi' => array('q' => 0.8, 'lang' => 'vi', 'name' => 'Vietnamese', 'direction' => 'ltr'), 'uk' => array('q' => 1, 'lang' => 'uk', 'name' => 'Ukrainian', 'direction' => 'ltr'),
'zh-cn' => array('q' => 0.9, 'lang' => 'zh_CN', 'name' => 'Chinese (Simplified)', 'direction' => 'ltr'), 'vi' => array('q' => 0.8, 'lang' => 'vi', 'name' => 'Vietnamese', 'direction' => 'ltr'),
'zh-hant' => array('q' => 0.2, 'lang' => 'zh_TW', 'name' => 'Chinese (Taiwanese)', 'direction' => 'ltr'), 'zh-cn' => array('q' => 0.9, 'lang' => 'zh_CN', 'name' => 'Chinese (Simplified)', 'direction' => 'ltr'),
); 'zh-hant' => array('q' => 0.2, 'lang' => 'zh_TW', 'name' => 'Chinese (Taiwanese)', 'direction' => 'ltr'),
);
} }

View File

@ -152,6 +152,9 @@ class MediaFile
throw new ClientException(_('The uploaded file was only' . throw new ClientException(_('The uploaded file was only' .
' partially uploaded.')); ' partially uploaded.'));
return; return;
case UPLOAD_ERR_NO_FILE:
// No file; probably just a non-AJAX submission.
return;
case UPLOAD_ERR_NO_TMP_DIR: case UPLOAD_ERR_NO_TMP_DIR:
throw new ClientException(_('Missing a temporary folder.')); throw new ClientException(_('Missing a temporary folder.'));
return; return;
@ -162,6 +165,8 @@ class MediaFile
throw new ClientException(_('File upload stopped by extension.')); throw new ClientException(_('File upload stopped by extension.'));
return; return;
default: default:
common_log(LOG_ERR, __METHOD__ . ": Unknown upload error " .
$_FILES[$param]['error']);
throw new ClientException(_('System error uploading file.')); throw new ClientException(_('System error uploading file.'));
return; return;
} }

View File

@ -43,7 +43,7 @@ require_once 'OAuth.php';
* @link http://status.net/ * @link http://status.net/
* *
*/ */
class OAuthClientCurlException extends Exception class OAuthClientException extends Exception
{ {
} }
@ -97,9 +97,14 @@ class OAuthClient
function getRequestToken($url) function getRequestToken($url)
{ {
$response = $this->oAuthGet($url); $response = $this->oAuthGet($url);
parse_str($response); $arr = array();
$token = new OAuthToken($oauth_token, $oauth_token_secret); parse_str($response, $arr);
return $token; if (isset($arr['oauth_token']) && isset($arr['oauth_token_secret'])) {
$token = new OAuthToken($arr['oauth_token'], @$arr['oauth_token_secret']);
return $token;
} else {
throw new OAuthClientException();
}
} }
/** /**
@ -177,7 +182,7 @@ class OAuthClient
} }
/** /**
* Make a HTTP request using cURL. * Make a HTTP request.
* *
* @param string $url Where to make the * @param string $url Where to make the
* @param array $params post parameters * @param array $params post parameters
@ -186,40 +191,32 @@ class OAuthClient
*/ */
function httpRequest($url, $params = null) function httpRequest($url, $params = null)
{ {
$options = array( $request = new HTTPClient($url);
CURLOPT_RETURNTRANSFER => true, $request->setConfig(array(
CURLOPT_FAILONERROR => true, 'connect_timeout' => 120,
CURLOPT_HEADER => false, 'timeout' => 120,
CURLOPT_FOLLOWLOCATION => true, 'follow_redirects' => true,
CURLOPT_USERAGENT => 'StatusNet', 'ssl_verify_peer' => false,
CURLOPT_CONNECTTIMEOUT => 120, ));
CURLOPT_TIMEOUT => 120,
CURLOPT_HTTPAUTH => CURLAUTH_ANY,
CURLOPT_SSL_VERIFYPEER => false,
// Twitter is strict about accepting invalid "Expect" headers // Twitter is strict about accepting invalid "Expect" headers
$request->setHeader('Expect', '');
CURLOPT_HTTPHEADER => array('Expect:')
);
if (isset($params)) { if (isset($params)) {
$options[CURLOPT_POST] = true; $request->setMethod(HTTP_Request2::METHOD_POST);
$options[CURLOPT_POSTFIELDS] = $params; $request->setBody($params);
} }
$ch = curl_init($url); try {
curl_setopt_array($ch, $options); $response = $request->send();
$response = curl_exec($ch); $code = $response->getStatus();
if ($code < 200 || $code >= 400) {
if ($response === false) { throw new OAuthClientException($response->getBody(), $code);
$msg = curl_error($ch); }
$code = curl_errno($ch); return $response->getBody();
throw new OAuthClientCurlException($msg, $code); } catch (Exception $e) {
throw new OAuthClientException($e->getMessage(), $e->getCode());
} }
curl_close($ch);
return $response;
} }
} }

View File

@ -44,20 +44,16 @@ function ping_broadcast_notice($notice) {
array('nickname' => $profile->nickname)), array('nickname' => $profile->nickname)),
$tags)); $tags));
$context = stream_context_create(array('http' => array('method' => "POST", $request = HTTPClient::start();
'header' => $httpResponse = $request->post($notify_url, array('Content-Type: text/xml'), $req);
"Content-Type: text/xml\r\n".
"User-Agent: StatusNet/".STATUSNET_VERSION."\r\n",
'content' => $req)));
$file = file_get_contents($notify_url, false, $context);
if ($file === false || mb_strlen($file) == 0) { if (!$httpResponse || mb_strlen($httpResponse->getBody()) == 0) {
common_log(LOG_WARNING, common_log(LOG_WARNING,
"XML-RPC empty results for ping ($notify_url, $notice->id) "); "XML-RPC empty results for ping ($notify_url, $notice->id) ");
continue; continue;
} }
$response = xmlrpc_decode($file); $response = xmlrpc_decode($httpResponse->getBody());
if (is_array($response) && xmlrpc_is_fault($response)) { if (is_array($response) && xmlrpc_is_fault($response)) {
common_log(LOG_WARNING, common_log(LOG_WARNING,

View File

@ -182,7 +182,8 @@ class ProfileListItem extends Widget
{ {
$avatar = $this->profile->getAvatar(AVATAR_STREAM_SIZE); $avatar = $this->profile->getAvatar(AVATAR_STREAM_SIZE);
$this->out->elementStart('a', array('href' => $this->profile->profileurl, $this->out->elementStart('a', array('href' => $this->profile->profileurl,
'class' => 'url')); 'class' => 'url',
'rel' => 'contact'));
$this->out->element('img', array('src' => ($avatar) ? $avatar->displayUrl() : Avatar::defaultImage(AVATAR_STREAM_SIZE), $this->out->element('img', array('src' => ($avatar) ? $avatar->displayUrl() : Avatar::defaultImage(AVATAR_STREAM_SIZE),
'class' => 'photo avatar', 'class' => 'photo avatar',
'width' => AVATAR_STREAM_SIZE, 'width' => AVATAR_STREAM_SIZE,
@ -190,7 +191,7 @@ class ProfileListItem extends Widget
'alt' => 'alt' =>
($this->profile->fullname) ? $this->profile->fullname : ($this->profile->fullname) ? $this->profile->fullname :
$this->profile->nickname)); $this->profile->nickname));
$hasFN = ($this->profile->fullname !== '') ? 'nickname' : 'fn nickname'; $hasFN = (!empty($this->profile->fullname)) ? 'nickname' : 'fn nickname';
$this->out->elementStart('span', $hasFN); $this->out->elementStart('span', $hasFN);
$this->out->raw($this->highlight($this->profile->nickname)); $this->out->raw($this->highlight($this->profile->nickname));
$this->out->elementEnd('span'); $this->out->elementEnd('span');
@ -200,53 +201,37 @@ class ProfileListItem extends Widget
function showFullName() function showFullName()
{ {
if (!empty($this->profile->fullname)) { if (!empty($this->profile->fullname)) {
$this->out->elementStart('dl', 'entity_fn');
$this->out->element('dt', null, 'Full name');
$this->out->elementStart('dd');
$this->out->elementStart('span', 'fn'); $this->out->elementStart('span', 'fn');
$this->out->raw($this->highlight($this->profile->fullname)); $this->out->raw($this->highlight($this->profile->fullname));
$this->out->elementEnd('span'); $this->out->elementEnd('span');
$this->out->elementEnd('dd');
$this->out->elementEnd('dl');
} }
} }
function showLocation() function showLocation()
{ {
if (!empty($this->profile->location)) { if (!empty($this->profile->location)) {
$this->out->elementStart('dl', 'entity_location'); $this->out->elementStart('span', 'location');
$this->out->element('dt', null, _('Location'));
$this->out->elementStart('dd', 'label');
$this->out->raw($this->highlight($this->profile->location)); $this->out->raw($this->highlight($this->profile->location));
$this->out->elementEnd('dd'); $this->out->elementEnd('span');
$this->out->elementEnd('dl');
} }
} }
function showHomepage() function showHomepage()
{ {
if (!empty($this->profile->homepage)) { if (!empty($this->profile->homepage)) {
$this->out->elementStart('dl', 'entity_url');
$this->out->element('dt', null, _('URL'));
$this->out->elementStart('dd');
$this->out->elementStart('a', array('href' => $this->profile->homepage, $this->out->elementStart('a', array('href' => $this->profile->homepage,
'class' => 'url')); 'class' => 'url'));
$this->out->raw($this->highlight($this->profile->homepage)); $this->out->raw($this->highlight($this->profile->homepage));
$this->out->elementEnd('a'); $this->out->elementEnd('a');
$this->out->elementEnd('dd');
$this->out->elementEnd('dl');
} }
} }
function showBio() function showBio()
{ {
if (!empty($this->profile->bio)) { if (!empty($this->profile->bio)) {
$this->out->elementStart('dl', 'entity_note'); $this->out->elementStart('p', 'note');
$this->out->element('dt', null, _('Note'));
$this->out->elementStart('dd', 'note');
$this->out->raw($this->highlight($this->profile->bio)); $this->out->raw($this->highlight($this->profile->bio));
$this->out->elementEnd('dd'); $this->out->elementEnd('p');
$this->out->elementEnd('dl');
} }
} }

View File

@ -88,6 +88,8 @@ class Router
$m->connect('doc/:title', array('action' => 'doc')); $m->connect('doc/:title', array('action' => 'doc'));
$m->connect('main/login?user_id=:user_id&token=:token', array('action'=>'login'), array('user_id'=> '[0-9]+', 'token'=>'.+'));
// main stuff is repetitive // main stuff is repetitive
$main = array('login', 'logout', 'register', 'subscribe', $main = array('login', 'logout', 'register', 'subscribe',
@ -120,7 +122,7 @@ class Router
// exceptional // exceptional
$m->connect('main/remote', array('action' => 'remotesubscribe')); $m->connect('main/remote', array('action' => 'remotesubscribe'));
$m->connect('main/remote?nickname=:nickname', array('action' => 'remotesubscribe'), array('nickname' => '[A-Za-z0-9_-]+')); $m->connect('main/remote?nickname=:nickname', array('action' => 'remotesubscribe'), array('nickname' => '['.NICKNAME_FMT.']+'));
foreach (Router::$bare as $action) { foreach (Router::$bare as $action) {
$m->connect('index.php?action=' . $action, array('action' => $action)); $m->connect('index.php?action=' . $action, array('action' => $action));
@ -164,10 +166,10 @@ class Router
$m->connect('notice/new', array('action' => 'newnotice')); $m->connect('notice/new', array('action' => 'newnotice'));
$m->connect('notice/new?replyto=:replyto', $m->connect('notice/new?replyto=:replyto',
array('action' => 'newnotice'), array('action' => 'newnotice'),
array('replyto' => '[A-Za-z0-9_-]+')); array('replyto' => '['.NICKNAME_FMT.']+'));
$m->connect('notice/new?replyto=:replyto&inreplyto=:inreplyto', $m->connect('notice/new?replyto=:replyto&inreplyto=:inreplyto',
array('action' => 'newnotice'), array('action' => 'newnotice'),
array('replyto' => '[A-Za-z0-9_-]+'), array('replyto' => '['.NICKNAME_FMT.']+'),
array('inreplyto' => '[0-9]+')); array('inreplyto' => '[0-9]+'));
$m->connect('notice/:notice/file', $m->connect('notice/:notice/file',
@ -191,7 +193,7 @@ class Router
array('id' => '[0-9]+')); array('id' => '[0-9]+'));
$m->connect('message/new', array('action' => 'newmessage')); $m->connect('message/new', array('action' => 'newmessage'));
$m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '[A-Za-z0-9_-]+')); $m->connect('message/new?to=:to', array('action' => 'newmessage'), array('to' => '['.NICKNAME_FMT.']+'));
$m->connect('message/:message', $m->connect('message/:message',
array('action' => 'showmessage'), array('action' => 'showmessage'),
array('message' => '[0-9]+')); array('message' => '[0-9]+'));
@ -275,7 +277,7 @@ class Router
$m->connect('api/statuses/friends_timeline/:id.:format', $m->connect('api/statuses/friends_timeline/:id.:format',
array('action' => 'ApiTimelineFriends', array('action' => 'ApiTimelineFriends',
'id' => '[a-zA-Z0-9]+', 'id' => '['.NICKNAME_FMT.']+',
'format' => '(xml|json|rss|atom)')); 'format' => '(xml|json|rss|atom)'));
$m->connect('api/statuses/home_timeline.:format', $m->connect('api/statuses/home_timeline.:format',
array('action' => 'ApiTimelineFriends', array('action' => 'ApiTimelineFriends',
@ -283,7 +285,7 @@ class Router
$m->connect('api/statuses/home_timeline/:id.:format', $m->connect('api/statuses/home_timeline/:id.:format',
array('action' => 'ApiTimelineFriends', array('action' => 'ApiTimelineFriends',
'id' => '[a-zA-Z0-9]+', 'id' => '['.NICKNAME_FMT.']+',
'format' => '(xml|json|rss|atom)')); 'format' => '(xml|json|rss|atom)'));
$m->connect('api/statuses/user_timeline.:format', $m->connect('api/statuses/user_timeline.:format',
@ -292,7 +294,7 @@ class Router
$m->connect('api/statuses/user_timeline/:id.:format', $m->connect('api/statuses/user_timeline/:id.:format',
array('action' => 'ApiTimelineUser', array('action' => 'ApiTimelineUser',
'id' => '[a-zA-Z0-9]+', 'id' => '['.NICKNAME_FMT.']+',
'format' => '(xml|json|rss|atom)')); 'format' => '(xml|json|rss|atom)'));
$m->connect('api/statuses/mentions.:format', $m->connect('api/statuses/mentions.:format',
@ -301,7 +303,7 @@ class Router
$m->connect('api/statuses/mentions/:id.:format', $m->connect('api/statuses/mentions/:id.:format',
array('action' => 'ApiTimelineMentions', array('action' => 'ApiTimelineMentions',
'id' => '[a-zA-Z0-9]+', 'id' => '['.NICKNAME_FMT.']+',
'format' => '(xml|json|rss|atom)')); 'format' => '(xml|json|rss|atom)'));
$m->connect('api/statuses/replies.:format', $m->connect('api/statuses/replies.:format',
@ -310,7 +312,7 @@ class Router
$m->connect('api/statuses/replies/:id.:format', $m->connect('api/statuses/replies/:id.:format',
array('action' => 'ApiTimelineMentions', array('action' => 'ApiTimelineMentions',
'id' => '[a-zA-Z0-9]+', 'id' => '['.NICKNAME_FMT.']+',
'format' => '(xml|json|rss|atom)')); 'format' => '(xml|json|rss|atom)'));
$m->connect('api/statuses/friends.:format', $m->connect('api/statuses/friends.:format',
@ -319,7 +321,7 @@ class Router
$m->connect('api/statuses/friends/:id.:format', $m->connect('api/statuses/friends/:id.:format',
array('action' => 'ApiUserFriends', array('action' => 'ApiUserFriends',
'id' => '[a-zA-Z0-9]+', 'id' => '['.NICKNAME_FMT.']+',
'format' => '(xml|json)')); 'format' => '(xml|json)'));
$m->connect('api/statuses/followers.:format', $m->connect('api/statuses/followers.:format',
@ -328,7 +330,7 @@ class Router
$m->connect('api/statuses/followers/:id.:format', $m->connect('api/statuses/followers/:id.:format',
array('action' => 'ApiUserFollowers', array('action' => 'ApiUserFollowers',
'id' => '[a-zA-Z0-9]+', 'id' => '['.NICKNAME_FMT.']+',
'format' => '(xml|json)')); 'format' => '(xml|json)'));
$m->connect('api/statuses/show.:format', $m->connect('api/statuses/show.:format',
@ -357,14 +359,9 @@ class Router
$m->connect('api/users/show/:id.:format', $m->connect('api/users/show/:id.:format',
array('action' => 'ApiUserShow', array('action' => 'ApiUserShow',
'id' => '[a-zA-Z0-9]+', 'id' => '['.NICKNAME_FMT.']+',
'format' => '(xml|json)')); 'format' => '(xml|json)'));
$m->connect('api/users/:method',
array('action' => 'api',
'apiaction' => 'users'),
array('method' => 'show(\.(xml|json))?'));
// direct messages // direct messages
$m->connect('api/direct_messages.:format', $m->connect('api/direct_messages.:format',
@ -400,12 +397,12 @@ class Router
$m->connect('api/friendships/create/:id.:format', $m->connect('api/friendships/create/:id.:format',
array('action' => 'ApiFriendshipsCreate', array('action' => 'ApiFriendshipsCreate',
'id' => '[a-zA-Z0-9]+', 'id' => '['.NICKNAME_FMT.']+',
'format' => '(xml|json)')); 'format' => '(xml|json)'));
$m->connect('api/friendships/destroy/:id.:format', $m->connect('api/friendships/destroy/:id.:format',
array('action' => 'ApiFriendshipsDestroy', array('action' => 'ApiFriendshipsDestroy',
'id' => '[a-zA-Z0-9]+', 'id' => '['.NICKNAME_FMT.']+',
'format' => '(xml|json)')); 'format' => '(xml|json)'));
// Social graph // Social graph
@ -431,6 +428,9 @@ class Router
$m->connect('api/account/verify_credentials.:format', $m->connect('api/account/verify_credentials.:format',
array('action' => 'ApiAccountVerifyCredentials')); array('action' => 'ApiAccountVerifyCredentials'));
$m->connect('api/account/update_profile_image.:format',
array('action' => 'ApiAccountUpdateProfileImage'));
// special case where verify_credentials is called w/out a format // special case where verify_credentials is called w/out a format
$m->connect('api/account/verify_credentials', $m->connect('api/account/verify_credentials',
@ -447,35 +447,28 @@ class Router
$m->connect('api/favorites/:id.:format', $m->connect('api/favorites/:id.:format',
array('action' => 'ApiTimelineFavorites', array('action' => 'ApiTimelineFavorites',
'id' => '[a-zA-Z0-9]+', 'id' => '['.NICKNAME_FMT.']+',
'format' => '(xmljson|rss|atom)')); 'format' => '(xmljson|rss|atom)'));
$m->connect('api/favorites/create/:id.:format', $m->connect('api/favorites/create/:id.:format',
array('action' => 'ApiFavoriteCreate', array('action' => 'ApiFavoriteCreate',
'id' => '[a-zA-Z0-9]+', 'id' => '['.NICKNAME_FMT.']+',
'format' => '(xml|json)')); 'format' => '(xml|json)'));
$m->connect('api/favorites/destroy/:id.:format', $m->connect('api/favorites/destroy/:id.:format',
array('action' => 'ApiFavoriteDestroy', array('action' => 'ApiFavoriteDestroy',
'id' => '[a-zA-Z0-9]+', 'id' => '['.NICKNAME_FMT.']+',
'format' => '(xml|json)')); 'format' => '(xml|json)'));
// notifications
$m->connect('api/notifications/:method/:argument',
array('action' => 'api',
'apiaction' => 'favorites'));
// blocks // blocks
$m->connect('api/blocks/create/:id.:format', $m->connect('api/blocks/create/:id.:format',
array('action' => 'ApiBlockCreate', array('action' => 'ApiBlockCreate',
'id' => '[a-zA-Z0-9]+', 'id' => '['.NICKNAME_FMT.']+',
'format' => '(xml|json)')); 'format' => '(xml|json)'));
$m->connect('api/blocks/destroy/:id.:format', $m->connect('api/blocks/destroy/:id.:format',
array('action' => 'ApiBlockDestroy', array('action' => 'ApiBlockDestroy',
'id' => '[a-zA-Z0-9]+', 'id' => '['.NICKNAME_FMT.']+',
'format' => '(xml|json)')); 'format' => '(xml|json)'));
// help // help
@ -591,14 +584,14 @@ class Router
'replies', 'inbox', 'outbox', 'microsummary') as $a) { 'replies', 'inbox', 'outbox', 'microsummary') as $a) {
$m->connect(':nickname/'.$a, $m->connect(':nickname/'.$a,
array('action' => $a), array('action' => $a),
array('nickname' => '[a-zA-Z0-9]{1,64}')); array('nickname' => '['.NICKNAME_FMT.']{1,64}'));
} }
foreach (array('subscriptions', 'subscribers') as $a) { foreach (array('subscriptions', 'subscribers') as $a) {
$m->connect(':nickname/'.$a.'/:tag', $m->connect(':nickname/'.$a.'/:tag',
array('action' => $a), array('action' => $a),
array('tag' => '[a-zA-Z0-9]+', array('tag' => '[a-zA-Z0-9]+',
'nickname' => '[a-zA-Z0-9]{1,64}')); 'nickname' => '['.NICKNAME_FMT.']{1,64}'));
} }
foreach (array('rss', 'groups') as $a) { foreach (array('rss', 'groups') as $a) {
@ -610,31 +603,31 @@ class Router
foreach (array('all', 'replies', 'favorites') as $a) { foreach (array('all', 'replies', 'favorites') as $a) {
$m->connect(':nickname/'.$a.'/rss', $m->connect(':nickname/'.$a.'/rss',
array('action' => $a.'rss'), array('action' => $a.'rss'),
array('nickname' => '[a-zA-Z0-9]{1,64}')); array('nickname' => '['.NICKNAME_FMT.']{1,64}'));
} }
$m->connect(':nickname/favorites', $m->connect(':nickname/favorites',
array('action' => 'showfavorites'), array('action' => 'showfavorites'),
array('nickname' => '[a-zA-Z0-9]{1,64}')); array('nickname' => '['.NICKNAME_FMT.']{1,64}'));
$m->connect(':nickname/avatar/:size', $m->connect(':nickname/avatar/:size',
array('action' => 'avatarbynickname'), array('action' => 'avatarbynickname'),
array('size' => '(original|96|48|24)', array('size' => '(original|96|48|24)',
'nickname' => '[a-zA-Z0-9]{1,64}')); 'nickname' => '['.NICKNAME_FMT.']{1,64}'));
$m->connect(':nickname/tag/:tag/rss', $m->connect(':nickname/tag/:tag/rss',
array('action' => 'userrss'), array('action' => 'userrss'),
array('nickname' => '[a-zA-Z0-9]{1,64}'), array('nickname' => '['.NICKNAME_FMT.']{1,64}'),
array('tag' => '[a-zA-Z0-9]+')); array('tag' => '[a-zA-Z0-9]+'));
$m->connect(':nickname/tag/:tag', $m->connect(':nickname/tag/:tag',
array('action' => 'showstream'), array('action' => 'showstream'),
array('nickname' => '[a-zA-Z0-9]{1,64}'), array('nickname' => '['.NICKNAME_FMT.']{1,64}'),
array('tag' => '[a-zA-Z0-9]+')); array('tag' => '[a-zA-Z0-9]+'));
$m->connect(':nickname', $m->connect(':nickname',
array('action' => 'showstream'), array('action' => 'showstream'),
array('nickname' => '[a-zA-Z0-9]{1,64}')); array('nickname' => '['.NICKNAME_FMT.']{1,64}'));
Event::handle('RouterInitialized', array($m)); Event::handle('RouterInitialized', array($m));
} }

View File

@ -172,26 +172,9 @@ class Snapshot
{ {
// XXX: Use OICU2 and OAuth to make authorized requests // XXX: Use OICU2 and OAuth to make authorized requests
$postdata = http_build_query($this->stats);
$opts =
array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: '.
'application/x-www-form-urlencoded',
'content' => $postdata,
'user_agent' => 'StatusNet/'.STATUSNET_VERSION
)
);
$context = stream_context_create($opts);
$reporturl = common_config('snapshot', 'reporturl'); $reporturl = common_config('snapshot', 'reporturl');
$request = HTTPClient::start();
$result = @file_get_contents($reporturl, false, $context); $request->post($reporturl, null, $this->stats);
return $result;
} }
/** /**

323
lib/userprofile.php Normal file
View File

@ -0,0 +1,323 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Profile for a particular user
*
* 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 Action
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @author Sarven Capadisli <csarven@status.net>
* @copyright 2008 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') && !defined('LACONICA')) {
exit(1);
}
require_once INSTALLDIR.'/lib/widget.php';
/**
* Profile of a user
*
* Shows profile information about a particular user
*
* @category Output
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @author Sarven Capadisli <csarven@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/
*
* @see HTMLOutputter
*/
class UserProfile extends Widget
{
var $user = null;
var $profile = null;
function __construct($action=null, $user=null, $profile=null)
{
parent::__construct($action);
$this->user = $user;
$this->profile = $profile;
}
function show()
{
$this->showProfileData();
$this->showEntityActions();
}
function showProfileData()
{
if (Event::handle('StartProfilePageProfileSection', array(&$this, $this->profile))) {
$this->out->elementStart('div', 'entity_profile vcard author');
$this->out->element('h2', null, _('User profile'));
if (Event::handle('StartProfilePageProfileElements', array(&$this, $this->profile))) {
$this->showAvatar();
$this->showNickname();
$this->showFullName();
$this->showLocation();
$this->showHomepage();
$this->showBio();
$this->showProfileTags();
Event::handle('EndProfilePageProfileElements', array(&$this, $this->profile));
}
$this->out->elementEnd('div');
Event::handle('EndProfilePageProfileSection', array(&$this, $this->profile));
}
}
function showAvatar()
{
if (Event::handle('StartProfilePageAvatar', array($this, $this->profile))) {
$avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE);
$this->out->elementStart('dl', 'entity_depiction');
$this->out->element('dt', null, _('Photo'));
$this->out->elementStart('dd');
$this->out->element('img', array('src' => ($avatar) ? $avatar->displayUrl() : Avatar::defaultImage(AVATAR_PROFILE_SIZE),
'class' => 'photo avatar',
'width' => AVATAR_PROFILE_SIZE,
'height' => AVATAR_PROFILE_SIZE,
'alt' => $this->profile->nickname));
$this->out->elementEnd('dd');
$user = User::staticGet('id', $this->profile->id);
$cur = common_current_user();
if ($cur && $cur->id == $user->id) {
$this->out->elementStart('dd');
$this->out->element('a', array('href' => common_local_url('avatarsettings')), _('Edit Avatar'));
$this->out->elementEnd('dd');
}
$this->out->elementEnd('dl');
Event::handle('EndProfilePageAvatar', array($this, $this->profile));
}
}
function showNickname()
{
if (Event::handle('StartProfilePageNickname', array($this, $this->profile))) {
$this->out->elementStart('dl', 'entity_nickname');
$this->out->element('dt', null, _('Nickname'));
$this->out->elementStart('dd');
$hasFN = ($this->profile->fullname) ? 'nickname url uid' : 'fn nickname url uid';
$this->out->element('a', array('href' => $this->profile->profileurl,
'rel' => 'me', 'class' => $hasFN),
$this->profile->nickname);
$this->out->elementEnd('dd');
$this->out->elementEnd('dl');
Event::handle('EndProfilePageNickname', array($this, $this->profile));
}
}
function showFullName()
{
if (Event::handle('StartProfilePageFullName', array($this, $this->profile))) {
if ($this->profile->fullname) {
$this->out->elementStart('dl', 'entity_fn');
$this->out->element('dt', null, _('Full name'));
$this->out->elementStart('dd');
$this->out->element('span', 'fn', $this->profile->fullname);
$this->out->elementEnd('dd');
$this->out->elementEnd('dl');
}
Event::handle('EndProfilePageFullName', array($this, $this->profile));
}
}
function showLocation()
{
if (Event::handle('StartProfilePageLocation', array($this, $this->profile))) {
if ($this->profile->location) {
$this->out->elementStart('dl', 'entity_location');
$this->out->element('dt', null, _('Location'));
$this->out->element('dd', 'label', $this->profile->location);
$this->out->elementEnd('dl');
}
Event::handle('EndProfilePageLocation', array($this, $this->profile));
}
}
function showHomepage()
{
if (Event::handle('StartProfilePageHomepage', array($this, $this->profile))) {
if ($this->profile->homepage) {
$this->out->elementStart('dl', 'entity_url');
$this->out->element('dt', null, _('URL'));
$this->out->elementStart('dd');
$this->out->element('a', array('href' => $this->profile->homepage,
'rel' => 'me', 'class' => 'url'),
$this->profile->homepage);
$this->out->elementEnd('dd');
$this->out->elementEnd('dl');
}
Event::handle('EndProfilePageHomepage', array($this, $this->profile));
}
}
function showBio()
{
if (Event::handle('StartProfilePageBio', array($this, $this->profile))) {
if ($this->profile->bio) {
$this->out->elementStart('dl', 'entity_note');
$this->out->element('dt', null, _('Note'));
$this->out->element('dd', 'note', $this->profile->bio);
$this->out->elementEnd('dl');
}
Event::handle('EndProfilePageBio', array($this, $this->profile));
}
}
function showProfileTags()
{
if (Event::handle('StartProfilePageProfileTags', array($this, $this->profile))) {
$tags = Profile_tag::getTags($this->profile->id, $this->profile->id);
if (count($tags) > 0) {
$this->out->elementStart('dl', 'entity_tags');
$this->out->element('dt', null, _('Tags'));
$this->out->elementStart('dd');
$this->out->elementStart('ul', 'tags xoxo');
foreach ($tags as $tag) {
$this->out->elementStart('li');
// Avoid space by using raw output.
$pt = '<span class="mark_hash">#</span><a rel="tag" href="' .
common_local_url('peopletag', array('tag' => $tag)) .
'">' . $tag . '</a>';
$this->out->raw($pt);
$this->out->elementEnd('li');
}
$this->out->elementEnd('ul');
$this->out->elementEnd('dd');
$this->out->elementEnd('dl');
}
Event::handle('EndProfilePageProfileTags', array($this, $this->profile));
}
}
function showEntityActions()
{
if (Event::handle('StartProfilePageActionsSection', array(&$this, $this->profile))) {
$cur = common_current_user();
$this->out->elementStart('div', 'entity_actions');
$this->out->element('h2', null, _('User actions'));
$this->out->elementStart('ul');
if (Event::handle('StartProfilePageActionsElements', array(&$this, $this->profile))) {
if (empty($cur)) { // not logged in
$this->out->elementStart('li', 'entity_subscribe');
$this->showRemoteSubscribeLink();
$this->out->elementEnd('li');
} else {
if ($cur->id == $this->profile->id) { // your own page
$this->out->elementStart('li', 'entity_edit');
$this->out->element('a', array('href' => common_local_url('profilesettings'),
'title' => _('Edit profile settings')),
_('Edit'));
$this->out->elementEnd('li');
} else { // someone else's page
// subscribe/unsubscribe button
$this->out->elementStart('li', 'entity_subscribe');
if ($cur->isSubscribed($this->profile)) {
$usf = new UnsubscribeForm($this->out, $this->profile);
$usf->show();
} else {
$sf = new SubscribeForm($this->out, $this->profile);
$sf->show();
}
$this->out->elementEnd('li');
if ($cur->mutuallySubscribed($this->user)) {
// message
$this->out->elementStart('li', 'entity_send-a-message');
$this->out->element('a', array('href' => common_local_url('newmessage', array('to' => $this->user->id)),
'title' => _('Send a direct message to this user')),
_('Message'));
$this->out->elementEnd('li');
// nudge
if ($this->user->email && $this->user->emailnotifynudge) {
$this->out->elementStart('li', 'entity_nudge');
$nf = new NudgeForm($this->out, $this->user);
$nf->show();
$this->out->elementEnd('li');
}
}
// block/unblock
$blocked = $cur->hasBlocked($this->profile);
$this->out->elementStart('li', 'entity_block');
if ($blocked) {
$ubf = new UnblockForm($this->out, $this->profile,
array('action' => 'showstream',
'nickname' => $this->profile->nickname));
$ubf->show();
} else {
$bf = new BlockForm($this->out, $this->profile,
array('action' => 'showstream',
'nickname' => $this->profile->nickname));
$bf->show();
}
$this->out->elementEnd('li');
}
}
Event::handle('EndProfilePageActionsElements', array(&$this, $this->profile));
}
$this->out->elementEnd('ul');
$this->out->elementEnd('div');
Event::handle('EndProfilePageActionsSection', array(&$this, $this->profile));
}
}
function showRemoteSubscribeLink()
{
$url = common_local_url('remotesubscribe',
array('nickname' => $this->profile->nickname));
$this->out->element('a', array('href' => $url,
'class' => 'entity_remote_subscribe'),
_('Subscribe'));
}
}

View File

@ -119,16 +119,44 @@ function common_munge_password($password, $id)
// check if a username exists and has matching password // check if a username exists and has matching password
function common_check_user($nickname, $password) function common_check_user($nickname, $password)
{ {
// NEVER allow blank passwords, even if they match the DB $authenticated = false;
if (mb_strlen($password) == 0) { $eventResult = Event::handle('CheckPassword', array($nickname, $password, &$authenticated));
return false;
}
$user = User::staticGet('nickname', $nickname); $user = User::staticGet('nickname', $nickname);
if (is_null($user) || $user === false) { if (is_null($user) || $user === false) {
return false; //user does not exist
if($authenticated){
//a handler said these are valid credentials, so see if a plugin wants to auto register the user
if(Event::handle('AutoRegister', array($nickname))){
//no handler registered the user
return false;
}else{
$user = User::staticGet('nickname', $nickname);
if (is_null($user) || $user === false) {
common_log(LOG_WARNING, "A plugin handled the AutoRegister event, but did not actually register the user, nickname: $nickname");
return false;
}else{
return $user;
}
}
}else{
//no handler indicated the credentials were valid, and we know their not valid because the user isn't in the database
return false;
}
} else { } else {
if (0 == strcmp(common_munge_password($password, $user->id), if($eventResult && ! $authenticated){
$user->password)) { //no handler was authoritative
if (mb_strlen($password) == 0) {
// NEVER allow blank passwords, even if they match the DB
return false;
}else{
if (0 == strcmp(common_munge_password($password, $user->id),
$user->password)) {
//internal checking passed
$authenticated = true;
}
}
}
if($authenticated){
return $user; return $user;
} else { } else {
return false; return false;
@ -422,7 +450,7 @@ function common_render_text($text)
function common_replace_urls_callback($text, $callback, $notice_id = null) { function common_replace_urls_callback($text, $callback, $notice_id = null) {
// Start off with a regex // Start off with a regex
$regex = '#'. $regex = '#'.
'(?:^|[\s\(\)\[\]\{\}\\\'\\\";]+)(?![\@\!\#])'. '(?:^|[\s\<\>\(\)\[\]\{\}\\\'\\\";]+)(?![\@\!\#])'.
'('. '('.
'(?:'. '(?:'.
'(?:'. //Known protocols '(?:'. //Known protocols
@ -452,9 +480,9 @@ function common_replace_urls_callback($text, $callback, $notice_id = null) {
')'. ')'.
'(?:'. '(?:'.
'(?:\:\d+)?'. //:port '(?:\:\d+)?'. //:port
'(?:/[\pN\pL$\[\]\,\!\(\)\.\:\-\_\+\/\=\&\;\%\~\*\$\+\'\"@]*)?'. // /path '(?:/[\pN\pL$\,\!\(\)\.\:\-\_\+\/\=\&\;\%\~\*\$\+\'@]*)?'. // /path
'(?:\?[\pN\pL\$\[\]\,\!\(\)\.\:\-\_\+\/\=\&\;\%\~\*\$\+\'\"@\/]*)?'. // ?query string '(?:\?[\pN\pL\$\,\!\(\)\.\:\-\_\+\/\=\&\;\%\~\*\$\+\'@\/]*)?'. // ?query string
'(?:\#[\pN\pL$\[\]\,\!\(\)\.\:\-\_\+\/\=\&\;\%\~\*\$\+\'\"\@/\?\#]*)?'. // #fragment '(?:\#[\pN\pL$\,\!\(\)\.\:\-\_\+\/\=\&\;\%\~\*\$\+\'\@/\?\#]*)?'. // #fragment
')(?<![\?\.\,\#\,])'. ')(?<![\?\.\,\#\,])'.
')'. ')'.
'#ixu'; '#ixu';
@ -480,6 +508,10 @@ function callback_helper($matches, $callback, $notice_id) {
array( array(
'left'=>'{', 'left'=>'{',
'right'=>'}' 'right'=>'}'
),
array(
'left'=>'<',
'right'=>'>'
) )
); );
$cannotEndWith=array('.','?',',','#'); $cannotEndWith=array('.','?',',','#');
@ -1366,9 +1398,28 @@ function common_memcache()
} }
} }
function common_license_terms($uri)
{
if(preg_match('/creativecommons.org\/licenses\/([^\/]+)/', $uri, $matches)) {
return explode('-',$matches[1]);
}
return array($uri);
}
function common_compatible_license($from, $to) function common_compatible_license($from, $to)
{ {
$from_terms = common_license_terms($from);
// public domain and cc-by are compatible with everything
if(count($from_terms) == 1 && ($from_terms[0] == 'publicdomain' || $from_terms[0] == 'by')) {
return true;
}
$to_terms = common_license_terms($to);
// sa is compatible across versions. IANAL
if(in_array('sa',$from_terms) || in_array('sa',$to_terms)) {
return count(array_diff($from_terms, $to_terms)) == 0;
}
// XXX: better compatibility check needed here! // XXX: better compatibility check needed here!
// Should at least normalise URIs
return ($from == $to); return ($from == $to);
} }

Binary file not shown.

File diff suppressed because it is too large Load Diff

Binary file not shown.

File diff suppressed because it is too large Load Diff

Binary file not shown.

File diff suppressed because it is too large Load Diff

Binary file not shown.

File diff suppressed because it is too large Load Diff

Binary file not shown.

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

Binary file not shown.

File diff suppressed because it is too large Load Diff

Binary file not shown.

File diff suppressed because it is too large Load Diff

Binary file not shown.

File diff suppressed because it is too large Load Diff

Binary file not shown.

File diff suppressed because it is too large Load Diff

Binary file not shown.

Some files were not shown because too many files have changed in this diff Show More