From: Sarven Capadisli Date: Fri, 29 May 2009 00:10:23 +0000 (+0000) Subject: Merge branch '0.7.x' into 0.8.x X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=a456ceb47c5019acd8d8766be274eda955a58125;hp=daf845dbe67a909e2bc9e039d3c2ccc560345b4d;p=quix0rs-gnu-social.git Merge branch '0.7.x' into 0.8.x --- diff --git a/EVENTS.txt b/EVENTS.txt index e0ce116667..8e917f11de 100644 --- a/EVENTS.txt +++ b/EVENTS.txt @@ -109,5 +109,11 @@ EndSubGroupNav: At the end of the subscriptions group nav menu RouterInitialized: After the router instance has been initialized - $m: the Net_URL_Mapper that has just been set up +StartLogout: Before logging out +- $action: the logout action + +EndLogout: After logging out +- $action: the logout action + ArgsInitialized: After the argument array has been initialized - $args: associative array of arguments, can be modified diff --git a/README b/README index 4649eb79ce..db912f2016 100644 --- a/README +++ b/README @@ -176,6 +176,10 @@ and the URLs are listed here for your convenience. version may render your Laconica site unable to send or receive XMPP messages. - Facebook library. Used for the Facebook application. +- PEAR Services_oEmbed. Used for some multimedia integration. +- PEAR HTTP_Request is an oEmbed dependency. +- PEAR Validat is an oEmbed dependency.e +- PEAR Net_URL is an oEmbed dependency.2 A design goal of Laconica is that the basic Web functionality should work on even the most restrictive commercial hosting services. @@ -1166,6 +1170,32 @@ welcome: nickname of a user account that sends welcome messages to new If either of these special user accounts are specified, the users should be created before the configuration is updated. +snapshot +-------- + +The software will, by default, send statistical snapshots about the +local installation to a stats server on the laconi.ca Web site. This +data is used by the developers to prioritize development decisions. No +identifying data about users or organizations is collected. The data +is available to the public for review. Participating in this survey +helps Laconica developers take your needs into account when updating +the software. + +run: string indicating when to run the statistics. Values can be 'web' + (run occasionally at Web time), 'cron' (run from a cron script), + or 'never' (don't ever run). If you set it to 'cron', remember to + schedule the script to run on a regular basis. +frequency: if run value is 'web', how often to report statistics. + Measured in Web hits; depends on how active your site is. + Default is 10000 -- that is, one report every 10000 Web hits, + on average. +reporturl: URL to post statistics to. Defaults to Laconica developers' + report system, but if they go evil or disappear you may + need to update this to another value. Note: if you + don't want to report stats, it's much better to + set 'run' to 'never' than to set this value to something + nonsensical. + Troubleshooting =============== diff --git a/actions/api.php b/actions/api.php index 8762b4bcd3..b25ba99f30 100644 --- a/actions/api.php +++ b/actions/api.php @@ -67,6 +67,7 @@ class ApiAction extends Action $this->process_command(); } else { # basic authentication failed + common_log(LOG_WARNING, "Failed API auth attempt, nickname: $nickname."); $this->show_basic_auth_error(); } } diff --git a/actions/attachment.php b/actions/attachment.php new file mode 100644 index 0000000000..16ee723d96 --- /dev/null +++ b/actions/attachment.php @@ -0,0 +1,205 @@ +. + * + * @category Personal + * @package Laconica + * @author Evan Prodromou + * @copyright 2008-2009 Control Yourself, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + */ + +if (!defined('LACONICA')) { + exit(1); +} + +require_once INSTALLDIR.'/lib/attachmentlist.php'; + +/** + * Show notice attachments + * + * @category Personal + * @package Laconica + * @author Evan Prodromou + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + */ + +class AttachmentAction extends Action +{ + /** + * Attachment object to show + */ + + var $attachment = null; + + /** + * Load attributes based on database arguments + * + * Loads all the DB stuff + * + * @param array $args $_REQUEST array + * + * @return success flag + */ + + function prepare($args) + { + parent::prepare($args); + + if ($id = $this->trimmed('attachment')) { + $this->attachment = File::staticGet($id); + } + + if (empty($this->attachment)) { + $this->clientError(_('No such attachment.'), 404); + return false; + } + return true; + } + + /** + * Is this action read-only? + * + * @return boolean true + */ + + function isReadOnly($args) + { + return true; + } + + /** + * Title of the page + * + * @return string title of the page + */ + function title() + { + $a = new Attachment($this->attachment); + return $a->title(); + } + + /** + * Last-modified date for page + * + * When was the content of this page last modified? Based on notice, + * profile, avatar. + * + * @return int last-modified date as unix timestamp + */ +/* + function lastModified() + { + return max(strtotime($this->notice->created), + strtotime($this->profile->modified), + ($this->avatar) ? strtotime($this->avatar->modified) : 0); + } +*/ + + /** + * An entity tag for this page + * + * Shows the ETag for the page, based on the notice ID and timestamps + * for the notice, profile, and avatar. It's weak, since we change + * the date text "one hour ago", etc. + * + * @return string etag + */ +/* + function etag() + { + $avtime = ($this->avatar) ? + strtotime($this->avatar->modified) : 0; + + return 'W/"' . implode(':', array($this->arg('action'), + common_language(), + $this->notice->id, + strtotime($this->notice->created), + strtotime($this->profile->modified), + $avtime)) . '"'; + } +*/ + + + /** + * Handle input + * + * Only handles get, so just show the page. + * + * @param array $args $_REQUEST data (unused) + * + * @return void + */ + + function handle($args) + { + parent::handle($args); + $this->showPage(); + } + + /** + * Don't show local navigation + * + * @return void + */ + + function showLocalNavBlock() + { + } + + /** + * Fill the content area of the page + * + * Shows a single notice list item. + * + * @return void + */ + + function showContent() + { + $ali = new Attachment($this->attachment, $this); + $cnt = $ali->show(); + } + + /** + * Don't show page notice + * + * @return void + */ + + function showPageNoticeBlock() + { + } + + /** + * Show aside: this attachments appears in what notices + * + * @return void + */ + function showSections() { + $ns = new AttachmentNoticeSection($this); + $ns->show(); + $atcs = new AttachmentTagCloudSection($this); + $atcs->show(); + } +} + diff --git a/actions/attachment_ajax.php b/actions/attachment_ajax.php new file mode 100644 index 0000000000..3d83393c51 --- /dev/null +++ b/actions/attachment_ajax.php @@ -0,0 +1,119 @@ +. + * + * @category Personal + * @package Laconica + * @author Evan Prodromou + * @copyright 2008-2009 Control Yourself, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + */ + +if (!defined('LACONICA')) { + exit(1); +} + +require_once INSTALLDIR.'/actions/attachment.php'; + +/** + * Show notice attachments + * + * @category Personal + * @package Laconica + * @author Evan Prodromou + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + */ + +class Attachment_ajaxAction extends AttachmentAction +{ + /** + * Show page, a template method. + * + * @return nothing + */ + function showPage() + { + if (Event::handle('StartShowBody', array($this))) { + $this->showCore(); + Event::handle('EndShowBody', array($this)); + } + } + + /** + * Show core. + * + * Shows local navigation, content block and aside. + * + * @return nothing + */ + function showCore() + { + $this->elementStart('div', array('id' => 'core')); + if (Event::handle('StartShowContentBlock', array($this))) { + $this->showContentBlock(); + Event::handle('EndShowContentBlock', array($this)); + } + $this->elementEnd('div'); + } + + /** + * Last-modified date for page + * + * When was the content of this page last modified? Based on notice, + * profile, avatar. + * + * @return int last-modified date as unix timestamp + */ +/* + function lastModified() + { + return max(strtotime($this->notice->created), + strtotime($this->profile->modified), + ($this->avatar) ? strtotime($this->avatar->modified) : 0); + } +*/ + + /** + * An entity tag for this page + * + * Shows the ETag for the page, based on the notice ID and timestamps + * for the notice, profile, and avatar. It's weak, since we change + * the date text "one hour ago", etc. + * + * @return string etag + */ +/* + function etag() + { + $avtime = ($this->avatar) ? + strtotime($this->avatar->modified) : 0; + + return 'W/"' . implode(':', array($this->arg('action'), + common_language(), + $this->notice->id, + strtotime($this->notice->created), + strtotime($this->profile->modified), + $avtime)) . '"'; + } +*/ +} + diff --git a/actions/attachment_thumbnail.php b/actions/attachment_thumbnail.php new file mode 100644 index 0000000000..b4070e747b --- /dev/null +++ b/actions/attachment_thumbnail.php @@ -0,0 +1,118 @@ +. + * + * @category Personal + * @package Laconica + * @author Evan Prodromou + * @copyright 2008-2009 Control Yourself, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + */ + +if (!defined('LACONICA')) { + exit(1); +} + +require_once INSTALLDIR.'/actions/attachment.php'; + +/** + * Show notice attachments + * + * @category Personal + * @package Laconica + * @author Evan Prodromou + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + */ + +class Attachment_thumbnailAction extends AttachmentAction +{ + /** + * Show page, a template method. + * + * @return nothing + */ + function showPage() + { + if (Event::handle('StartShowBody', array($this))) { + $this->showCore(); + Event::handle('EndShowBody', array($this)); + } + } + + /** + * Show core. + * + * Shows local navigation, content block and aside. + * + * @return nothing + */ + function showCore() + { + $file_thumbnail = File_thumbnail::staticGet('file_id', $this->attachment->id); + if (empty($file_thumbnail->url)) { + return; + } + $this->element('img', array('src' => $file_thumbnail->url, 'alt' => 'Thumbnail')); + } + + /** + * Last-modified date for page + * + * When was the content of this page last modified? Based on notice, + * profile, avatar. + * + * @return int last-modified date as unix timestamp + */ +/* + function lastModified() + { + return max(strtotime($this->notice->created), + strtotime($this->profile->modified), + ($this->avatar) ? strtotime($this->avatar->modified) : 0); + } +*/ + + /** + * An entity tag for this page + * + * Shows the ETag for the page, based on the notice ID and timestamps + * for the notice, profile, and avatar. It's weak, since we change + * the date text "one hour ago", etc. + * + * @return string etag + */ +/* + function etag() + { + $avtime = ($this->avatar) ? + strtotime($this->avatar->modified) : 0; + + return 'W/"' . implode(':', array($this->arg('action'), + common_language(), + $this->notice->id, + strtotime($this->notice->created), + strtotime($this->profile->modified), + $avtime)) . '"'; + } +*/ +} + diff --git a/actions/conversation.php b/actions/conversation.php new file mode 100644 index 0000000000..ef189016aa --- /dev/null +++ b/actions/conversation.php @@ -0,0 +1,298 @@ + + * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3 + * @link http://laconi.ca/ + * + * Laconica - a distributed open-source microblogging tool + * Copyright (C) 2009, Control Yourself, 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 . + */ + +if (!defined('LACONICA')) { + exit(1); +} + +require_once INSTALLDIR.'/lib/noticelist.php'; + +/** + * Conversation tree in the browser + * + * @category Action + * @package Laconica + * @author Evan Prodromou + * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3 + * @link http://laconi.ca/ + */ + +class ConversationAction extends Action +{ + var $id = null; + var $page = null; + + /** + * Initialization. + * + * @param array $args Web and URL arguments + * + * @return boolean false if id not passed in + */ + + function prepare($args) + { + parent::prepare($args); + $this->id = $this->trimmed('id'); + if (empty($this->id)) { + return false; + } + $this->page = $this->trimmed('page'); + if (empty($this->page)) { + $this->page = 1; + } + return true; + } + + /** + * Handle the action + * + * @param array $args Web and URL arguments + * + * @return void + */ + + function handle($args) + { + parent::handle($args); + $this->showPage(); + } + + /** + * Returns the page title + * + * @return string page title + */ + + function title() + { + return _("Conversation"); + } + + /** + * Show content. + * + * Display a hierarchical unordered list in the content area. + * Uses ConversationTree to do most of the heavy lifting. + * + * @return void + */ + + function showContent() + { + // FIXME this needs to be a tree, not a list + + $qry = 'SELECT * FROM notice WHERE conversation = %s '; + + $offset = ($this->page-1) * NOTICES_PER_PAGE; + $limit = NOTICES_PER_PAGE + 1; + + $txt = sprintf($qry, $this->id); + + $notices = Notice::getStream($txt, + 'notice:conversation:'.$this->id, + $offset, $limit); + + $ct = new ConversationTree($notices, $this); + + $cnt = $ct->show(); + + $this->pagination($this->page > 1, $cnt > NOTICES_PER_PAGE, + $this->page, 'conversation', array('id' => $this->id)); + } + +} + +/** + * Conversation tree + * + * The widget class for displaying a hierarchical list of notices. + * + * @category Widget + * @package Laconica + * @author Evan Prodromou + * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3 + * @link http://laconi.ca/ + */ + +class ConversationTree extends NoticeList +{ + var $tree = null; + var $table = null; + + /** + * Show the tree of notices + * + * @return void + */ + + function show() + { + $cnt = 0; + + $this->tree = array(); + $this->table = array(); + + while ($this->notice->fetch()) { + + $cnt++; + + $id = $this->notice->id; + $notice = clone($this->notice); + + $this->table[$id] = $notice; + + if (is_null($notice->reply_to)) { + $this->tree['root'] = array($notice->id); + } else if (array_key_exists($notice->reply_to, $this->tree)) { + $this->tree[$notice->reply_to][] = $notice->id; + } else { + $this->tree[$notice->reply_to] = array($notice->id); + } + } + + $this->out->elementStart('div', array('id' =>'notices_primary')); + $this->out->element('h2', null, _('Notices')); + $this->out->elementStart('ul', array('class' => 'notices')); + + if (array_key_exists('root', $this->tree)) { + $rootid = $this->tree['root'][0]; + $this->showNoticePlus($rootid); + } + + $this->out->elementEnd('ul'); + $this->out->elementEnd('div'); + + return $cnt; + } + + /** + * Shows a notice plus its list of children. + * + * @param integer $id ID of the notice to show + * + * @return void + */ + + function showNoticePlus($id) + { + $notice = $this->table[$id]; + + // We take responsibility for doing the li + + $this->out->elementStart('li', array('class' => 'hentry notice', + 'id' => 'notice-' . $this->notice->id)); + + $item = $this->newListItem($notice); + $item->show(); + + if (array_key_exists($id, $this->tree)) { + $children = $this->tree[$id]; + + $this->out->elementStart('ul', array('class' => 'notices')); + + foreach ($children as $child) { + $this->showNoticePlus($child); + } + + $this->out->elementEnd('ul'); + } + + $this->out->elementEnd('li'); + } + + /** + * Override parent class to return our preferred item. + * + * @param Notice $notice Notice to display + * + * @return NoticeListItem a list item to show + */ + + function newListItem($notice) + { + return new ConversationTreeItem($notice, $this->out); + } +} + +/** + * Conversation tree list item + * + * Special class of NoticeListItem for use inside conversation trees. + * + * @category Widget + * @package Laconica + * @author Evan Prodromou + * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3 + * @link http://laconi.ca/ + */ + +class ConversationTreeItem extends NoticeListItem +{ + /** + * start a single notice. + * + * The default creates the
  • ; we skip, since the ConversationTree + * takes care of that. + * + * @return void + */ + + function showStart() + { + return; + } + + /** + * finish the notice + * + * The default closes the
  • ; we skip, since the ConversationTree + * takes care of that. + * + * @return void + */ + + function showEnd() + { + return; + } + + /** + * show link to notice conversation page + * + * Since we're only used on the conversation page, we skip this + * + * @return void + */ + + function showContext() + { + return; + } +} \ No newline at end of file diff --git a/actions/deletenotice.php b/actions/deletenotice.php index 6c350b33ab..e733f9650a 100644 --- a/actions/deletenotice.php +++ b/actions/deletenotice.php @@ -112,8 +112,8 @@ class DeletenoticeAction extends DeleteAction $this->hidden('token', common_session_token()); $this->hidden('notice', $this->trimmed('notice')); $this->element('p', null, _('Are you sure you want to delete this notice?')); - $this->submit('form_action-yes', _('Yes'), 'submit form_action-primary', 'yes'); - $this->submit('form_action-no', _('No'), 'submit form_action-secondary', 'no'); + $this->submit('form_action-no', _('No'), 'submit form_action-primary', 'no', _("Do not delete this notice")); + $this->submit('form_action-yes', _('Yes'), 'submit form_action-secondary', 'yes', _('Delete this notice')); $this->elementEnd('fieldset'); $this->elementEnd('form'); } diff --git a/actions/designsettings.php b/actions/designsettings.php new file mode 100644 index 0000000000..315e5a199c --- /dev/null +++ b/actions/designsettings.php @@ -0,0 +1,264 @@ +. + * + * @category Settings + * @package Laconica + * @author Sarven Capadisli + * @copyright 2008-2009 Control Yourself, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + */ + +if (!defined('LACONICA')) { + exit(1); +} + +require_once INSTALLDIR.'/lib/accountsettingsaction.php'; + + + +class DesignsettingsAction extends AccountSettingsAction +{ + /** + * Title of the page + * + * @return string Title of the page + */ + + function title() + { + return _('Profile design'); + } + + /** + * Instructions for use + * + * @return instructions for use + */ + + function getInstructions() + { + return _('Customize the way your profile looks with a background image and a colour palette of your choice.'); + } + + /** + * Content area of the page + * + * Shows a form for changing the password + * + * @return void + */ + + function showContent() + { + $user = common_current_user(); + $this->elementStart('form', array('method' => 'POST', + 'id' => 'form_settings_design', + 'class' => 'form_settings', + 'action' => + common_local_url('designsettings'))); + $this->elementStart('fieldset'); + $this->hidden('token', common_session_token()); + + $this->elementStart('fieldset', array('id' => 'settings_design_background-image')); + $this->element('legend', null, _('Change background image')); + $this->elementStart('ul', 'form_data'); + $this->elementStart('li'); + $this->element('label', array('for' => 'design_ background-image_file'), + _('Upload file')); + $this->element('input', array('name' => 'design_background-image_file', + 'type' => 'file', + 'id' => 'design_background-image_file')); + $this->element('p', 'form_guide', _('You can upload your personal background image. The maximum file size is 2Mb.')); + $this->element('input', array('name' => 'MAX_FILE_SIZE', + 'type' => 'hidden', + 'id' => 'MAX_FILE_SIZE', + 'value' => ImageFile::maxFileSizeInt())); + $this->elementEnd('li'); + $this->elementEnd('ul'); + $this->elementEnd('fieldset'); + + $this->elementStart('fieldset', array('id' => 'settings_design_color')); + $this->element('legend', null, _('Change colours')); + $this->elementStart('ul', 'form_data'); + + //This is a JSON object in the DB field. Here for testing. Remove later. + $userSwatch = '{"body":{"background-color":"#F0F2F5"}, + "#content":{"background-color":"#FFFFFF"}, + "#aside_primary":{"background-color":"#CEE1E9"}, + "html body":{"color":"#000000"}, + "a":{"color":"#002E6E"}}'; + + //Default theme swatch -- Where should this be stored? + $defaultSwatch = array('body' => array('background-color' => '#F0F2F5'), + '#content' => array('background-color' => '#FFFFFF'), + '#aside_primary' => array('background-color' => '#CEE1E9'), + 'html body' => array('color' => '#000000'), + 'a' => array('color' => '#002E6E')); + + $userSwatch = ($userSwatch) ? json_decode($userSwatch, true) : $defaultSwatch; + + $s = 0; + $labelSwatch = array('Background', + 'Content', + 'Sidebar', + 'Text', + 'Links'); + foreach($userSwatch as $propertyvalue => $value) { + $foo = array_values($value); + $this->elementStart('li'); + $this->element('label', array('for' => 'swatch-'.$s), _($labelSwatch[$s])); + $this->element('input', array('name' => 'swatch-'.$s, //prefer swatch[$s] ? + 'type' => 'text', + 'id' => 'swatch-'.$s, + 'class' => 'swatch', + 'maxlength' => '7', + 'size' => '7', + 'value' => $foo[0])); + $this->elementEnd('li'); + $s++; + } + + $this->elementEnd('ul'); + $this->elementEnd('fieldset'); + + $this->element('input', array('id' => 'settings_design_reset', + 'type' => 'reset', + 'value' => 'Reset', + 'class' => 'submit form_action-primary', + 'title' => _('Reset back to default'))); + $this->submit('save', _('Save'), 'submit form_action-secondary', 'save', _('Save design')); + +/*TODO: Check submitted form values: +json_encode(form values) +if submitted Swatch == DefaultSwatch, don't store in DB. +else store in BD +*/ + $this->elementEnd('fieldset'); + $this->elementEnd('form'); + + } + + /** + * Handle a post + * + * Validate input and save changes. Reload the form with a success + * or error message. + * + * @return void + */ + + function handlePost() + { + /* + // CSRF protection + + $token = $this->trimmed('token'); + if (!$token || $token != common_session_token()) { + $this->showForm(_('There was a problem with your session token. '. + 'Try again, please.')); + return; + } + + $user = common_current_user(); + assert(!is_null($user)); // should already be checked + + // FIXME: scrub input + + $newpassword = $this->arg('newpassword'); + $confirm = $this->arg('confirm'); + + # Some validation + + if (strlen($newpassword) < 6) { + $this->showForm(_('Password must be 6 or more characters.')); + return; + } else if (0 != strcmp($newpassword, $confirm)) { + $this->showForm(_('Passwords don\'t match.')); + return; + } + + if ($user->password) { + $oldpassword = $this->arg('oldpassword'); + + if (!common_check_user($user->nickname, $oldpassword)) { + $this->showForm(_('Incorrect old password')); + return; + } + } + + $original = clone($user); + + $user->password = common_munge_password($newpassword, $user->id); + + $val = $user->validate(); + if ($val !== true) { + $this->showForm(_('Error saving user; invalid.')); + return; + } + + if (!$user->update($original)) { + $this->serverError(_('Can\'t save new password.')); + return; + } + + $this->showForm(_('Password saved.'), true); + */ + } + + + /** + * Add the Farbtastic stylesheet + * + * @return void + */ + + function showStylesheets() + { + parent::showStylesheets(); + $farbtasticStyle = + common_path('theme/base/css/farbtastic.css?version='.LACONICA_VERSION); + + $this->element('link', array('rel' => 'stylesheet', + 'type' => 'text/css', + 'href' => $farbtasticStyle, + 'media' => 'screen, projection, tv')); + } + + /** + * Add the Farbtastic scripts + * + * @return void + */ + + function showScripts() + { + parent::showScripts(); + + $farbtasticPack = common_path('js/farbtastic/farbtastic.js'); + $farbtasticGo = common_path('js/farbtastic/farbtastic.go.js'); + + $this->element('script', array('type' => 'text/javascript', + 'src' => $farbtasticPack)); + $this->element('script', array('type' => 'text/javascript', + 'src' => $farbtasticGo)); + } +} diff --git a/actions/facebookhome.php b/actions/facebookhome.php index 4c2b26355c..00b35ef686 100644 --- a/actions/facebookhome.php +++ b/actions/facebookhome.php @@ -115,7 +115,7 @@ class FacebookhomeAction extends FacebookAction $flink->foreign_id = $this->fbuid; $flink->service = FACEBOOK_SERVICE; $flink->created = common_sql_now(); - $flink->set_flags(true, false, false); + $flink->set_flags(true, false, false, false); $flink_id = $flink->insert(); diff --git a/actions/facebooksettings.php b/actions/facebooksettings.php index 236460c1c9..227e123160 100644 --- a/actions/facebooksettings.php +++ b/actions/facebooksettings.php @@ -55,7 +55,7 @@ class FacebooksettingsAction extends FacebookAction $prefix = $this->trimmed('prefix'); $original = clone($this->flink); - $this->flink->set_flags($noticesync, $replysync, false); + $this->flink->set_flags($noticesync, $replysync, false, false); $result = $this->flink->update($original); $this->facebook->api_client->data_setUserPreference(FACEBOOK_NOTICE_PREFIX, diff --git a/actions/logout.php b/actions/logout.php index 9f3bfe2470..c34b10987a 100644 --- a/actions/logout.php +++ b/actions/logout.php @@ -70,10 +70,20 @@ class LogoutAction extends Action if (!common_logged_in()) { $this->clientError(_('Not logged in.')); } else { - common_set_user(null); - common_real_login(false); // not logged in - common_forgetme(); // don't log back in! + if (Event::handle('StartLogout', array($this))) { + $this->logout(); + } + Event::handle('EndLogout', array($this)); + common_redirect(common_local_url('public'), 303); } } + + function logout() + { + common_set_user(null); + common_real_login(false); // not logged in + common_forgetme(); // don't log back in! + } + } diff --git a/actions/newnotice.php b/actions/newnotice.php index cbd04c58b2..ae0ff96363 100644 --- a/actions/newnotice.php +++ b/actions/newnotice.php @@ -158,7 +158,8 @@ class NewnoticeAction extends Action $replyto = 'false'; } - $notice = Notice::saveNew($user->id, $content, 'web', 1, +// $notice = Notice::saveNew($user->id, $content_shortened, 'web', 1, + $notice = Notice::saveNew($user->id, $content_shortened, 'web', 1, ($replyto == 'false') ? null : $replyto); if (is_string($notice)) { @@ -166,6 +167,8 @@ class NewnoticeAction extends Action return; } + $this->saveUrls($notice); + common_broadcast_notice($notice); if ($this->boolean('ajax')) { @@ -191,6 +194,24 @@ class NewnoticeAction extends Action } } + /** save all urls in the notice to the db + * + * follow redirects and save all available file information + * (mimetype, date, size, oembed, etc.) + * + * @param class $notice Notice to pull URLs from + * + * @return void + */ + function saveUrls($notice) { + common_replace_urls_callback($notice->content, array($this, 'saveUrl'), $notice->id); + } + + function saveUrl($data) { + list($url, $notice_id) = $data; + $zzz = File::processNew($url, $notice_id); + } + /** * Show an Ajax-y error message * diff --git a/actions/register.php b/actions/register.php index 033cf557f8..dcbbbdb6a6 100644 --- a/actions/register.php +++ b/actions/register.php @@ -382,6 +382,19 @@ class RegisterAction extends Action function showFormContent() { + $code = $this->trimmed('code'); + + $invite = null; + + if ($code) { + $invite = Invitation::staticGet($code); + } + + if (common_config('site', 'inviteonly') && !($code && $invite)) { + $this->clientError(_('Sorry, only invited people can register.')); + return; + } + $this->elementStart('form', array('method' => 'post', 'id' => 'form_register', 'class' => 'form_settings', diff --git a/actions/showstream.php b/actions/showstream.php index 82665e5b8b..678a3174c1 100644 --- a/actions/showstream.php +++ b/actions/showstream.php @@ -68,6 +68,9 @@ class ShowstreamAction extends ProfileAction } else { $base = $this->user->nickname; } + if (!empty($this->tag)) { + $base .= sprintf(_(' tagged %s'), $this->tag); + } if ($this->page == 1) { return $base; @@ -110,6 +113,15 @@ class ShowstreamAction extends ProfileAction function getFeeds() { + if (!empty($this->tag)) { + return array(new Feed(Feed::RSS1, + common_local_url('userrss', + array('nickname' => $this->user->nickname, + 'tag' => $this->tag)), + sprintf(_('Notice feed for %s tagged %s (RSS 1.0)'), + $this->user->nickname, $this->tag))); + } + return array(new Feed(Feed::RSS1, common_local_url('userrss', array('nickname' => $this->user->nickname)), @@ -363,7 +375,9 @@ class ShowstreamAction extends ProfileAction function showNotices() { - $notice = $this->user->getNotices(($this->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1); + $notice = empty($this->tag) + ? $this->user->getNotices(($this->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1) + : $this->user->getTaggedNotices(($this->page-1)*NOTICES_PER_PAGE, NOTICES_PER_PAGE + 1, 0, 0, null, $this->tag); $pnl = new ProfileNoticeList($notice, $this); $cnt = $pnl->show(); diff --git a/actions/tag.php b/actions/tag.php index 02f3e35225..e9351d2419 100644 --- a/actions/tag.php +++ b/actions/tag.php @@ -51,7 +51,6 @@ class TagAction extends Action $pop->show(); } - function title() { if ($this->page == 1) { diff --git a/actions/twittersettings.php b/actions/twittersettings.php index 0b98eef591..2b742788ee 100644 --- a/actions/twittersettings.php +++ b/actions/twittersettings.php @@ -138,7 +138,7 @@ class TwittersettingsAction extends ConnectSettingsAction $this->elementStart('ul', 'form_data'); $this->elementStart('li'); - $this->checkbox('noticesync', + $this->checkbox('noticesend', _('Automatically send my notices to Twitter.'), ($flink) ? ($flink->noticesync & FOREIGN_NOTICE_SEND) : @@ -158,6 +158,22 @@ class TwittersettingsAction extends ConnectSettingsAction ($flink->friendsync & FOREIGN_FRIEND_RECV) : false); $this->elementEnd('li'); + + if (common_config('twitterbridge','enabled')) { + $this->elementStart('li'); + $this->checkbox('noticerecv', + _('Import my Friends Timeline.'), + ($flink) ? + ($flink->noticesync & FOREIGN_NOTICE_RECV) : + false); + $this->elementEnd('li'); + } else { + // preserve setting even if bidrection bridge toggled off + if ($flink && ($flink->noticesync & FOREIGN_NOTICE_RECV)) { + $this->hidden('noticerecv', true, 'noticerecv'); + } + } + $this->elementEnd('ul'); if ($flink) { @@ -320,7 +336,8 @@ class TwittersettingsAction extends ConnectSettingsAction { $screen_name = $this->trimmed('twitter_username'); $password = $this->trimmed('twitter_password'); - $noticesync = $this->boolean('noticesync'); + $noticesend = $this->boolean('noticesend'); + $noticerecv = $this->boolean('noticerecv'); $replysync = $this->boolean('replysync'); $friendsync = $this->boolean('friendsync'); @@ -363,7 +380,7 @@ class TwittersettingsAction extends ConnectSettingsAction $flink->credentials = $password; $flink->created = common_sql_now(); - $flink->set_flags($noticesync, $replysync, $friendsync); + $flink->set_flags($noticesend, $noticerecv, $replysync, $friendsync); $flink_id = $flink->insert(); @@ -421,7 +438,8 @@ class TwittersettingsAction extends ConnectSettingsAction function savePreferences() { - $noticesync = $this->boolean('noticesync'); + $noticesend = $this->boolean('noticesend'); + $noticerecv = $this->boolean('noticerecv'); $friendsync = $this->boolean('friendsync'); $replysync = $this->boolean('replysync'); @@ -450,7 +468,7 @@ class TwittersettingsAction extends ConnectSettingsAction $original = clone($flink); - $flink->set_flags($noticesync, $replysync, $friendsync); + $flink->set_flags($noticesend, $noticerecv, $replysync, $friendsync); $result = $flink->update($original); diff --git a/actions/userrss.php b/actions/userrss.php index 5861d9ee36..2280509b22 100644 --- a/actions/userrss.php +++ b/actions/userrss.php @@ -25,14 +25,15 @@ require_once(INSTALLDIR.'/lib/rssaction.php'); class UserrssAction extends Rss10Action { - var $user = null; + var $tag = null; function prepare($args) { parent::prepare($args); - $nickname = $this->trimmed('nickname'); + $nickname = $this->trimmed('nickname'); $this->user = User::staticGet('nickname', $nickname); + $this->tag = $this->trimmed('tag'); if (!$this->user) { $this->clientError(_('No such user.')); @@ -42,6 +43,25 @@ class UserrssAction extends Rss10Action } } + function getTaggedNotices($tag = null, $limit=0) + { + $user = $this->user; + + if (is_null($user)) { + return null; + } + + $notice = $user->getTaggedNotices(0, ($limit == 0) ? NOTICES_PER_PAGE : $limit, 0, 0, null, $tag); + + $notices = array(); + while ($notice->fetch()) { + $notices[] = clone($notice); + } + + return $notices; + } + + function getNotices($limit=0) { diff --git a/classes/File.php b/classes/File.php new file mode 100644 index 0000000000..e5913115b7 --- /dev/null +++ b/classes/File.php @@ -0,0 +1,123 @@ +. + */ + +if (!defined('LACONICA')) { exit(1); } + +require_once INSTALLDIR.'/classes/Memcached_DataObject.php'; +require_once INSTALLDIR.'/classes/File_redirection.php'; +require_once INSTALLDIR.'/classes/File_oembed.php'; +require_once INSTALLDIR.'/classes/File_thumbnail.php'; +require_once INSTALLDIR.'/classes/File_to_post.php'; +//require_once INSTALLDIR.'/classes/File_redirection.php'; + +/** + * Table Definition for file + */ + +class File extends Memcached_DataObject +{ + ###START_AUTOCODE + /* the code below is auto generated do not remove the above tag */ + + public $__table = 'file'; // table name + public $id; // int(11) not_null primary_key group_by + public $url; // varchar(255) unique_key + public $mimetype; // varchar(50) + public $size; // int(11) group_by + public $title; // varchar(255) + public $date; // int(11) group_by + public $protected; // int(1) group_by + + /* Static get */ + function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('File',$k,$v); } + + /* the code above is auto generated do not remove the tag below */ + ###END_AUTOCODE + + function isProtected($url) { + return 'http://www.facebook.com/login.php' === $url; + } + + function getAttachments($post_id) { + $query = "select file.* from file join file_to_post on (file_id = file.id) join notice on (post_id = notice.id) where post_id = " . $this->escape($post_id); + $this->query($query); + $att = array(); + while ($this->fetch()) { + $att[] = clone($this); + } + $this->free(); + return $att; + } + + function saveNew($redir_data, $given_url) { + $x = new File; + $x->url = $given_url; + if (!empty($redir_data['protected'])) $x->protected = $redir_data['protected']; + if (!empty($redir_data['title'])) $x->title = $redir_data['title']; + if (!empty($redir_data['type'])) $x->mimetype = $redir_data['type']; + if (!empty($redir_data['size'])) $x->size = intval($redir_data['size']); + if (isset($redir_data['time']) && $redir_data['time'] > 0) $x->date = intval($redir_data['time']); + $file_id = $x->insert(); + + if (isset($redir_data['type']) + && ('text/html' === substr($redir_data['type'], 0, 9)) + && ($oembed_data = File_oembed::_getOembed($given_url)) + && isset($oembed_data['json'])) { + + File_oembed::saveNew($oembed_data['json'], $file_id); + } + return $x; + } + + function processNew($given_url, $notice_id) { + if (empty($given_url)) return -1; // error, no url to process + $given_url = File_redirection::_canonUrl($given_url); + if (empty($given_url)) return -1; // error, no url to process + $file = File::staticGet('url', $given_url); + if (empty($file->id)) { + $file_redir = File_redirection::staticGet('url', $given_url); + if (empty($file_redir->id)) { + $redir_data = File_redirection::where($given_url); + $redir_url = $redir_data['url']; + if ($redir_url === $given_url) { + $x = File::saveNew($redir_data, $given_url); + $file_id = $x->id; + + } else { + $x = File::processNew($redir_url, $notice_id); + $file_id = $x->id; + File_redirection::saveNew($redir_data, $file_id, $given_url); + } + } else { + $file_id = $file_redir->file_id; + } + } else { + $file_id = $file->id; + $x = $file; + } + + if (empty($x)) { + $x = File::staticGet($file_id); + if (empty($x)) die('Impossible!'); + } + + File_to_post::processNew($file_id, $notice_id); + return $x; + } +} diff --git a/classes/File_oembed.php b/classes/File_oembed.php new file mode 100644 index 0000000000..f1b2cb13c0 --- /dev/null +++ b/classes/File_oembed.php @@ -0,0 +1,87 @@ +. + */ + +if (!defined('LACONICA')) { exit(1); } + +require_once INSTALLDIR.'/classes/Memcached_DataObject.php'; + +/** + * Table Definition for file_oembed + */ + +class File_oembed extends Memcached_DataObject +{ + ###START_AUTOCODE + /* the code below is auto generated do not remove the above tag */ + + public $__table = 'file_oembed'; // table name + public $id; // int(11) not_null primary_key group_by + public $file_id; // int(11) unique_key group_by + public $version; // varchar(20) + public $type; // varchar(20) + public $provider; // varchar(50) + public $provider_url; // varchar(255) + public $width; // int(11) group_by + public $height; // int(11) group_by + public $html; // blob(65535) blob + public $title; // varchar(255) + public $author_name; // varchar(50) + public $author_url; // varchar(255) + public $url; // varchar(255) + + /* Static get */ + function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('File_oembed',$k,$v); } + + /* the code above is auto generated do not remove the tag below */ + ###END_AUTOCODE + + + function _getOembed($url, $maxwidth = 500, $maxheight = 400, $format = 'json') { + $cmd = 'http://oohembed.com/oohembed/?url=' . urlencode($url); + if (is_int($maxwidth)) $cmd .= "&maxwidth=$maxwidth"; + if (is_int($maxheight)) $cmd .= "&maxheight=$maxheight"; + if (is_string($format)) $cmd .= "&format=$format"; + $oe = @file_get_contents($cmd); + if (false === $oe) return false; + return array($format => (('json' === $format) ? json_decode($oe, true) : $oe)); + } + + function saveNew($data, $file_id) { + $file_oembed = new File_oembed; + $file_oembed->file_id = $file_id; + $file_oembed->version = $data['version']; + $file_oembed->type = $data['type']; + if (!empty($data['provider_name'])) $file_oembed->provider = $data['provider_name']; + if (!isset($file_oembed->provider) && !empty($data['provide'])) $file_oembed->provider = $data['provider']; + if (!empty($data['provide_url'])) $file_oembed->provider_url = $data['provider_url']; + if (!empty($data['width'])) $file_oembed->width = intval($data['width']); + if (!empty($data['height'])) $file_oembed->height = intval($data['height']); + if (!empty($data['html'])) $file_oembed->html = $data['html']; + if (!empty($data['title'])) $file_oembed->title = $data['title']; + if (!empty($data['author_name'])) $file_oembed->author_name = $data['author_name']; + if (!empty($data['author_url'])) $file_oembed->author_url = $data['author_url']; + if (!empty($data['url'])) $file_oembed->url = $data['url']; + $file_oembed->insert(); + if (!empty($data['thumbnail_url'])) { + File_thumbnail::saveNew($data, $file_id); + } + } +} + + diff --git a/classes/File_redirection.php b/classes/File_redirection.php new file mode 100644 index 0000000000..0eae681783 --- /dev/null +++ b/classes/File_redirection.php @@ -0,0 +1,274 @@ +. + */ + +if (!defined('LACONICA')) { exit(1); } + +require_once INSTALLDIR.'/classes/Memcached_DataObject.php'; +require_once INSTALLDIR.'/classes/File.php'; +require_once INSTALLDIR.'/classes/File_oembed.php'; + +define('USER_AGENT', 'Laconica user agent / file probe'); + + +/** + * Table Definition for file_redirection + */ + +class File_redirection extends Memcached_DataObject +{ + ###START_AUTOCODE + /* the code below is auto generated do not remove the above tag */ + + public $__table = 'file_redirection'; // table name + public $id; // int(11) not_null primary_key group_by + public $url; // varchar(255) unique_key + public $file_id; // int(11) group_by + public $redirections; // int(11) group_by + public $httpcode; // int(11) group_by + + /* Static get */ + function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('File_redirection',$k,$v); } + + /* the code above is auto generated do not remove the tag below */ + ###END_AUTOCODE + + + + function _commonCurl($url, $redirs) { + $curlh = curl_init(); + curl_setopt($curlh, CURLOPT_URL, $url); + curl_setopt($curlh, CURLOPT_AUTOREFERER, true); // # setup referer header when folowing redirects + curl_setopt($curlh, CURLOPT_CONNECTTIMEOUT, 10); // # seconds to wait + curl_setopt($curlh, CURLOPT_MAXREDIRS, $redirs); // # max number of http redirections to follow + curl_setopt($curlh, CURLOPT_USERAGENT, USER_AGENT); + curl_setopt($curlh, CURLOPT_FOLLOWLOCATION, true); // Follow redirects + curl_setopt($curlh, CURLOPT_RETURNTRANSFER, true); + 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) { + if ($redirs < 0) return false; + + // let's see if we know this... + $a = File::staticGet('url', $short_url); + if (empty($a->id)) { + $b = File_redirection::staticGet('url', $short_url); + if (empty($b->id)) { + // we'll have to figure it out + } else { + // this is a redirect to $b->file_id + $a = File::staticGet($b->file_id); + $url = $a->url; + } + } else { + // this is a direct link to $a->url + $url = $a->url; + } + if (isset($url)) { + return $url; + } + + + + $curlh = File_redirection::_commonCurl($short_url, $redirs); + // Don't include body in output + curl_setopt($curlh, CURLOPT_NOBODY, true); + curl_exec($curlh); + $info = curl_getinfo($curlh); + curl_close($curlh); + + if (405 == $info['http_code']) { + $curlh = File_redirection::_commonCurl($short_url, $redirs); + curl_exec($curlh); + $info = curl_getinfo($curlh); + curl_close($curlh); + } + + if (!empty($info['redirect_count']) && File::isProtected($info['url'])) { + return File_redirection::_redirectWhere_imp($short_url, $info['redirect_count'] - 1, true); + } + + $ret = array('code' => $info['http_code'] + , 'redirects' => $info['redirect_count'] + , 'url' => $info['url']); + + if (!empty($info['content_type'])) $ret['type'] = $info['content_type']; + if ($protected) $ret['protected'] = true; + if (!empty($info['download_content_length'])) $ret['size'] = $info['download_content_length']; + if (isset($info['filetime']) && ($info['filetime'] > 0)) $ret['time'] = $info['filetime']; + return $ret; + } + + function where($in_url) { + $ret = File_redirection::_redirectWhere_imp($in_url); + return $ret; + } + + function makeShort($long_url) { + $long_url = File_redirection::_canonUrl($long_url); + // do we already know this long_url and have a short redirection for it? + $file = new File; + $file_redir = new File_redirection; + $file->url = $long_url; + $file->joinAdd($file_redir); + $file->selectAdd('length(file_redirection.url) as len'); + $file->limit(1); + $file->orderBy('len'); + $file->find(true); + if (!empty($file->id)) { + return $file->url; + } + + // if yet unknown, we must find a short url according to user settings + $short_url = File_redirection::_userMakeShort($long_url, common_current_user()); + return $short_url; + } + + function _userMakeShort($long_url, $user) { + if (empty($user)) { + // common current user does not find a user when called from the XMPP daemon + // therefore we'll set one here fix, so that XMPP given URLs may be shortened + $user->urlshorteningservice = 'ur1.ca'; + } + $curlh = curl_init(); + curl_setopt($curlh, CURLOPT_CONNECTTIMEOUT, 20); // # seconds to wait + curl_setopt($curlh, CURLOPT_USERAGENT, 'Laconica'); + curl_setopt($curlh, CURLOPT_RETURNTRANSFER, true); + + switch($user->urlshorteningservice) { + case 'ur1.ca': + require_once INSTALLDIR.'/lib/Shorturl_api.php'; + $short_url_service = new LilUrl; + $short_url = $short_url_service->shorten($long_url); + break; + + case '2tu.us': + $short_url_service = new TightUrl; + require_once INSTALLDIR.'/lib/Shorturl_api.php'; + $short_url = $short_url_service->shorten($long_url); + break; + + case 'ptiturl.com': + require_once INSTALLDIR.'/lib/Shorturl_api.php'; + $short_url_service = new PtitUrl; + $short_url = $short_url_service->shorten($long_url); + break; + + case 'bit.ly': + curl_setopt($curlh, CURLOPT_URL, 'http://bit.ly/api?method=shorten&long_url='.urlencode($long_url)); + $short_url = current(json_decode(curl_exec($curlh))->results)->hashUrl; + break; + + case 'is.gd': + curl_setopt($curlh, CURLOPT_URL, 'http://is.gd/api.php?longurl='.urlencode($long_url)); + $short_url = curl_exec($curlh); + break; + case 'snipr.com': + curl_setopt($curlh, CURLOPT_URL, 'http://snipr.com/site/snip?r=simple&link='.urlencode($long_url)); + $short_url = curl_exec($curlh); + break; + case 'metamark.net': + curl_setopt($curlh, CURLOPT_URL, 'http://metamark.net/api/rest/simple?long_url='.urlencode($long_url)); + $short_url = curl_exec($curlh); + break; + case 'tinyurl.com': + curl_setopt($curlh, CURLOPT_URL, 'http://tinyurl.com/api-create.php?url='.urlencode($long_url)); + $short_url = curl_exec($curlh); + break; + default: + $short_url = false; + } + + curl_close($curlh); + + if ($short_url) { + $short_url = (string)$short_url; + // store it + $file = File::staticGet('url', $long_url); + if (empty($file)) { + $redir_data = File_redirection::where($long_url); + $file = File::saveNew($redir_data, $long_url); + $file_id = $file->id; + if (!empty($redir_data['oembed']['json'])) { + File_oembed::saveNew($redir_data['oembed']['json'], $file_id); + } + } else { + $file_id = $file->id; + } + $file_redir = File_redirection::staticGet('url', $short_url); + if (empty($file_redir)) { + $file_redir = new File_redirection; + $file_redir->url = $short_url; + $file_redir->file_id = $file_id; + $file_redir->insert(); + } + return $short_url; + } + return $long_url; + } + + function _canonUrl($in_url, $default_scheme = 'http://') { + if (empty($in_url)) return false; + $out_url = $in_url; + $p = parse_url($out_url); + if (empty($p['host']) || empty($p['scheme'])) { + list($scheme) = explode(':', $in_url, 2); + switch ($scheme) { + case 'fax': + case 'tel': + $out_url = str_replace('.-()', '', $out_url); + break; + + case 'mailto': + case 'aim': + case 'jabber': + case 'xmpp': + // don't touch anything + break; + + default: + $out_url = $default_scheme . ltrim($out_url, '/'); + $p = parse_url($out_url); + if (empty($p['scheme'])) return false; + break; + } + } + + if (('ftp' == $p['scheme']) || ('http' == $p['scheme']) || ('https' == $p['scheme'])) { + if (empty($p['host'])) return false; + if (empty($p['path'])) { + $out_url .= '/'; + } + } + + return $out_url; + } + + function saveNew($data, $file_id, $url) { + $file_redir = new File_redirection; + $file_redir->url = $url; + $file_redir->file_id = $file_id; + $file_redir->redirections = intval($data['redirects']); + $file_redir->httpcode = intval($data['code']); + $file_redir->insert(); + } +} + diff --git a/classes/File_thumbnail.php b/classes/File_thumbnail.php new file mode 100644 index 0000000000..1a65b92c90 --- /dev/null +++ b/classes/File_thumbnail.php @@ -0,0 +1,55 @@ +. + */ + +if (!defined('LACONICA')) { exit(1); } + +require_once INSTALLDIR.'/classes/Memcached_DataObject.php'; + +/** + * Table Definition for file_thumbnail + */ + +class File_thumbnail extends Memcached_DataObject +{ + ###START_AUTOCODE + /* the code below is auto generated do not remove the above tag */ + + public $__table = 'file_thumbnail'; // table name + public $id; // int(11) not_null primary_key group_by + public $file_id; // int(11) unique_key group_by + public $url; // varchar(255) unique_key + public $width; // int(11) group_by + public $height; // int(11) group_by + + /* Static get */ + function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('File_thumbnail',$k,$v); } + + /* the code above is auto generated do not remove the tag below */ + ###END_AUTOCODE + + function saveNew($data, $file_id) { + $tn = new File_thumbnail; + $tn->file_id = $file_id; + $tn->url = $data['thumbnail_url']; + $tn->width = intval($data['thumbnail_width']); + $tn->height = intval($data['thumbnail_height']); + $tn->insert(); + } +} + diff --git a/classes/File_to_post.php b/classes/File_to_post.php new file mode 100644 index 0000000000..00ddebe6b8 --- /dev/null +++ b/classes/File_to_post.php @@ -0,0 +1,60 @@ +. + */ + +if (!defined('LACONICA')) { exit(1); } + +require_once INSTALLDIR.'/classes/Memcached_DataObject.php'; + +/** + * Table Definition for file_to_post + */ + +class File_to_post extends Memcached_DataObject +{ + ###START_AUTOCODE + /* the code below is auto generated do not remove the above tag */ + + public $__table = 'file_to_post'; // table name + public $id; // int(11) not_null primary_key group_by + public $file_id; // int(11) multiple_key group_by + public $post_id; // int(11) group_by + + /* Static get */ + function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('File_to_post',$k,$v); } + + /* the code above is auto generated do not remove the tag below */ + ###END_AUTOCODE + + function processNew($file_id, $notice_id) { + static $seen = array(); + if (empty($seen[$notice_id]) || !in_array($file_id, $seen[$notice_id])) { + $f2p = new File_to_post; + $f2p->file_id = $file_id; + $f2p->post_id = $notice_id; + $f2p->insert(); + if (empty($seen[$notice_id])) { + $seen[$notice_id] = array($file_id); + } else { + $seen[$notice_id][] = $file_id; + } + } + + } +} + diff --git a/classes/Foreign_link.php b/classes/Foreign_link.php index af2b3f189d..c0b356eced 100644 --- a/classes/Foreign_link.php +++ b/classes/Foreign_link.php @@ -11,7 +11,7 @@ class Foreign_link extends Memcached_DataObject public $__table = 'foreign_link'; // table name public $user_id; // int(4) primary_key not_null - public $foreign_id; // int(4) primary_key not_null + public $foreign_id; // bigint(8) primary_key not_null unsigned public $service; // int(4) primary_key not_null public $credentials; // varchar(255) public $noticesync; // tinyint(1) not_null default_1 @@ -59,13 +59,19 @@ class Foreign_link extends Memcached_DataObject return null; } - function set_flags($noticesync, $replysync, $friendsync) + function set_flags($noticesend, $noticerecv, $replysync, $friendsync) { - if ($noticesync) { + if ($noticesend) { $this->noticesync |= FOREIGN_NOTICE_SEND; } else { $this->noticesync &= ~FOREIGN_NOTICE_SEND; } + + if ($noticerecv) { + $this->noticesync |= FOREIGN_NOTICE_RECV; + } else { + $this->noticesync &= ~FOREIGN_NOTICE_RECV; + } if ($replysync) { $this->noticesync |= FOREIGN_NOTICE_SEND_REPLY; diff --git a/classes/Group_inbox.php b/classes/Group_inbox.php old mode 100755 new mode 100644 diff --git a/classes/Group_member.php b/classes/Group_member.php old mode 100755 new mode 100644 diff --git a/classes/Notice.php b/classes/Notice.php index 771a4e715f..1b5c0ab0a5 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -46,6 +46,7 @@ class Notice extends Memcached_DataObject public $reply_to; // int(4) public $is_local; // tinyint(1) public $source; // varchar(32) + public $conversation; // int(4) /* Static get */ function staticGet($k,$v=NULL) { @@ -123,8 +124,6 @@ class Notice extends Memcached_DataObject $profile = Profile::staticGet($profile_id); - $final = common_shorten_links($content); - if (!$profile) { common_log(LOG_ERR, 'Problem saving notice. Unknown user.'); return _('Problem saving notice. Unknown user.'); @@ -135,7 +134,7 @@ class Notice extends Memcached_DataObject return _('Too many notices too fast; take a breather and post again in a few minutes.'); } - if (common_config('site', 'dupelimit') > 0 && !Notice::checkDupes($profile_id, $final)) { + if (common_config('site', 'dupelimit') > 0 && !Notice::checkDupes($profile_id, $content)) { common_log(LOG_WARNING, 'Dupe posting by profile #' . $profile_id . '; throttled.'); return _('Too many duplicate messages too quickly; take a breather and post again in a few minutes.'); } @@ -166,11 +165,19 @@ class Notice extends Memcached_DataObject $notice->reply_to = $reply_to; $notice->created = common_sql_now(); - $notice->content = $final; - $notice->rendered = common_render_content($final, $notice); + $notice->content = $content; + $notice->rendered = common_render_content($content, $notice); $notice->source = $source; $notice->uri = $uri; + if (!empty($reply_to)) { + $reply_notice = Notice::staticGet('id', $reply_to); + if (!empty($reply_notice)) { + $notice->reply_to = $reply_to; + $notice->conversation = $reply_notice->conversation; + } + } + if (Event::handle('StartNoticeSave', array(&$notice))) { $id = $notice->insert(); @@ -270,6 +277,16 @@ class Notice extends Memcached_DataObject return true; } + function hasAttachments() { + $post = clone $this; + $query = "select count(file_id) as n_attachments from file join file_to_post on (file_id = file.id) join notice on (post_id = notice.id) where post_id = " . $post->escape($post->id); + $post->query($query); + $post->fetch(); + $n_attachments = intval($post->n_attachments); + $post->free(); + return $n_attachments; + } + function blowCaches($blowLast=false) { $this->blowSubsCache($blowLast); @@ -847,6 +864,7 @@ class Notice extends Memcached_DataObject if ($recipient_notice) { $orig = clone($this); $this->reply_to = $recipient_notice->id; + $this->conversation = $recipient_notice->conversation; $this->update($orig); } } @@ -896,6 +914,14 @@ class Notice extends Memcached_DataObject } } + // If it's not a reply, make it the root of a new conversation + + if (empty($this->conversation)) { + $orig = clone($this); + $this->conversation = $this->id; + $this->update($orig); + } + foreach (array_keys($replied) as $recipient) { $user = User::staticGet('id', $recipient); if ($user) { @@ -998,7 +1024,7 @@ class Notice extends Memcached_DataObject } } - function stream($fn, $args, $cachekey, $offset=0, $limit=20, $since_id=0, $before_id=0, $since=null) + function stream($fn, $args, $cachekey, $offset=0, $limit=20, $since_id=0, $before_id=0, $since=null, $tag=null) { $cache = common_memcache(); @@ -1006,7 +1032,7 @@ class Notice extends Memcached_DataObject $since_id != 0 || $before_id != 0 || !is_null($since) || ($offset + $limit) > NOTICE_CACHE_WINDOW) { return call_user_func_array($fn, array_merge($args, array($offset, $limit, $since_id, - $before_id, $since))); + $before_id, $since, $tag))); } $idkey = common_cache_key($cachekey); @@ -1026,7 +1052,7 @@ class Notice extends Memcached_DataObject $window = explode(',', $laststr); $last_id = $window[0]; $new_ids = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW, - $last_id, 0, null))); + $last_id, 0, null, $tag))); $new_window = array_merge($new_ids, $window); @@ -1041,7 +1067,7 @@ class Notice extends Memcached_DataObject } $window = call_user_func_array($fn, array_merge($args, array(0, NOTICE_CACHE_WINDOW, - 0, 0, null))); + 0, 0, null, $tag))); $windowstr = implode(',', $window); diff --git a/classes/Profile.php b/classes/Profile.php index ae5641d79d..afc0ea4f74 100644 --- a/classes/Profile.php +++ b/classes/Profile.php @@ -153,18 +153,66 @@ class Profile extends Memcached_DataObject return null; } - function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) + function getTaggedNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null, $tag=null) + { + // XXX: I'm not sure this is going to be any faster. It probably isn't. + $ids = Notice::stream(array($this, '_streamTaggedDirect'), + array(), + 'profile:notice_ids:' . $this->id, + $offset, $limit, $since_id, $before_id, $since, $tag); + common_debug(print_r($ids, true)); + return Notice::getStreamByIds($ids); + } + + function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null) { // XXX: I'm not sure this is going to be any faster. It probably isn't. $ids = Notice::stream(array($this, '_streamDirect'), array(), 'profile:notice_ids:' . $this->id, - $offset, $limit, $since_id, $before_id); + $offset, $limit, $since_id, $before_id, $since); return Notice::getStreamByIds($ids); } - function _streamDirect($offset, $limit, $since_id, $before_id, $since) + function _streamTaggedDirect($offset, $limit, $since_id, $before_id, $since=null, $tag=null) + { + common_debug('_streamTaggedDirect()'); + $notice = new Notice(); + $notice->profile_id = $this->id; + $query = "select id from notice join notice_tag on id=notice_id where tag='" . $notice->escape($tag) . "' and profile_id=" . $notice->escape($notice->profile_id); + if ($since_id != 0) { + $query .= " and id > $since_id"; + } + + if ($before_id != 0) { + $query .= " and id < $before_id"; + } + + if (!is_null($since)) { + $query .= " and created > '" . date('Y-m-d H:i:s', $since) . "'"; + } + + $query .= ' order by id DESC'; + + if (!is_null($offset)) { + $query .= " limit $offset, $limit"; + } + $notice->query($query); + $ids = array(); + + while ($notice->fetch()) { + common_debug(print_r($notice, true)); + $ids[] = $notice->id; + } + + return $ids; + } + + + + + function _streamDirect($offset, $limit, $since_id, $before_id, $since = null) { $notice = new Notice(); diff --git a/classes/Related_group.php b/classes/Related_group.php old mode 100755 new mode 100644 diff --git a/classes/Status_network.php b/classes/Status_network.php new file mode 100644 index 0000000000..f7747f71d7 --- /dev/null +++ b/classes/Status_network.php @@ -0,0 +1,61 @@ +dbhost)) ? 'localhost' : $sn->dbhost; + $dbuser = (empty($sn->dbuser)) ? $sn->nickname : $sn->dbuser; + $dbpass = $sn->dbpass; + $dbname = (empty($sn->dbname)) ? $sn->nickname : $sn->dbname; + + $config['db']['database'] = "mysqli://$dbuser:$dbpass@$dbhost/$dbname"; + $config['site']['name'] = $sn->sitename; + return true; + } else { + return false; + } + } +} diff --git a/classes/User.php b/classes/User.php index b5ac7b2206..ea8ba40817 100644 --- a/classes/User.php +++ b/classes/User.php @@ -407,13 +407,22 @@ class User extends Memcached_DataObject return Notice::getStreamByIds($ids); } + function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null) { + $profile = $this->getProfile(); + if (!$profile) { + return null; + } else { + return $profile->getTaggedNotices($tag, $offset, $limit, $since_id, $before_id, $since); + } + } + function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null) { $profile = $this->getProfile(); if (!$profile) { return null; } else { - return $profile->getNotices($offset, $limit, $since_id, $before_id); + return $profile->getNotices($offset, $limit, $since_id, $before_id, $since); } } diff --git a/classes/User_group.php b/classes/User_group.php old mode 100755 new mode 100644 diff --git a/classes/laconica.ini b/classes/laconica.ini old mode 100755 new mode 100644 index c054195884..92bbb35d4c --- a/classes/laconica.ini +++ b/classes/laconica.ini @@ -1,4 +1,3 @@ - [avatar] profile_id = 129 original = 17 @@ -47,6 +46,64 @@ modified = 384 notice_id = K user_id = K +[file] +id = 129 +url = 2 +mimetype = 2 +size = 1 +title = 2 +date = 1 +protected = 1 + +[file__keys] +id = N + +[file_oembed] +id = 129 +file_id = 1 +version = 2 +type = 2 +provider = 2 +provider_url = 2 +width = 1 +height = 1 +html = 34 +title = 2 +author_name = 2 +author_url = 2 +url = 2 + +[file_oembed__keys] +id = N + +[file_redirection] +id = 129 +url = 2 +file_id = 1 +redirections = 1 +httpcode = 1 + +[file_redirection__keys] +id = N + +[file_thumbnail] +id = 129 +file_id = 1 +url = 2 +width = 1 +height = 1 + +[file_thumbnail__keys] +id = N + +[file_to_post] +id = 129 +file_id = 1 +post_id = 1 + +[file_to_post__keys] +id = N + [foreign_link] user_id = 129 foreign_id = 129 @@ -170,6 +227,7 @@ modified = 384 reply_to = 1 is_local = 17 source = 2 +conversation = 1 [notice__keys] id = N diff --git a/classes/laconica.links.ini b/classes/laconica.links.ini index 173b187267..95c63f3c09 100644 --- a/classes/laconica.links.ini +++ b/classes/laconica.links.ini @@ -41,3 +41,17 @@ subscribed = profile:id [fave] notice_id = notice:id user_id = user:id + +[file_oembed] +file_id = file:id + +[file_redirection] +file_id = file:id + +[file_thumbnail] +file_id = file:id + +[file_to_post] +file_id = file:id +post_id = notice:id + diff --git a/classes/statusnet.ini b/classes/statusnet.ini new file mode 100755 index 0000000000..a70cd41228 --- /dev/null +++ b/classes/statusnet.ini @@ -0,0 +1,17 @@ + +[status_network] +nickname = 130 +hostname = 2 +pathname = 2 +sitename = 2 +dbhost = 2 +dbuser = 2 +dbpass = 2 +dbname = 2 +created = 142 +modified = 384 + +[status_network__keys] +nickname = K +hostname = U +pathname = U diff --git a/config.php.sample b/config.php.sample index e70d9ab46e..282826a7fb 100644 --- a/config.php.sample +++ b/config.php.sample @@ -3,190 +3,215 @@ if (!defined('LACONICA')) { exit(1); } -#If you have downloaded libraries in random little places, you -#can add the paths here +// If you have downloaded libraries in random little places, you +// can add the paths here -#$extra_path = array("/opt/php-openid-2.0.1", "/usr/local/share/php"); -#set_include_path(implode(PATH_SEPARATOR, $extra_path) . PATH_SEPARATOR . get_include_path()); +// $extra_path = array("/opt/php-openid-2.0.1", "/usr/local/share/php"); +// set_include_path(implode(PATH_SEPARATOR, $extra_path) . PATH_SEPARATOR . get_include_path()); -# We get called by common.php, $config is a tree with lots of config -# options -# These are for configuring your URLs +// We get called by common.php, $config is a tree with lots of config +// options +// These are for configuring your URLs $config['site']['name'] = 'Just another Laconica microblog'; $config['site']['server'] = 'localhost'; $config['site']['path'] = 'laconica'; -#$config['site']['fancy'] = false; -#$config['site']['theme'] = 'default'; -#To enable the built-in mobile style sheet, defaults to false. -#$config['site']['mobile'] = true; -#For contact email, defaults to $_SERVER["SERVER_ADMIN"] -#$config['site']['email'] = 'admin@example.net'; -#Brought by... -#$config['site']['broughtby'] = 'Individual or Company'; -#$config['site']['broughtbyurl'] = 'http://example.net/'; -#If you don't want to let users register (say, for a one-person install) -#Crude but effective -- register everybody, then lock down -#$config['site']['closed'] = true; -#Only allow registration for people invited by another user -#$config['site']['inviteonly'] = true; -#Make the site invisible to non-logged-in users -#$config['site']['private'] = true; - -# If you want logging sent to a file instead of syslog -#$config['site']['logfile'] = '/tmp/laconica.log'; - -# Enables extra log information, for example full details of PEAR DB errors -#$config['site']['logdebug'] = true; - -#To set your own logo, overriding the one in the theme -#$config['site']['logo'] = '/mylogo.png'; - -# This is a PEAR DB DSN, see http://pear.php.net/manual/en/package.database.db.intro-dsn.php -# Set it to match your actual database +// $config['site']['fancy'] = false; +// $config['site']['theme'] = 'default'; +// To enable the built-in mobile style sheet, defaults to false. +// $config['site']['mobile'] = true; +// For contact email, defaults to $_SERVER["SERVER_ADMIN"] +// $config['site']['email'] = 'admin@example.net'; +// Brought by... +// $config['site']['broughtby'] = 'Individual or Company'; +// $config['site']['broughtbyurl'] = 'http://example.net/'; +// If you don't want to let users register (say, for a one-person install) +// Crude but effective -- register everybody, then lock down +// $config['site']['closed'] = true; +// Only allow registration for people invited by another user +// $config['site']['inviteonly'] = true; +// Make the site invisible to non-logged-in users +// $config['site']['private'] = true; + +// If you want logging sent to a file instead of syslog +// $config['site']['logfile'] = '/tmp/laconica.log'; + +// Enables extra log information, for example full details of PEAR DB errors +// $config['site']['logdebug'] = true; + +// To set your own logo, overriding the one in the theme +// $config['site']['logo'] = '/mylogo.png'; + +// This is a PEAR DB DSN, see http://pear.php.net/manual/en/package.database.db.intro-dsn.php +// Set it to match your actual database $config['db']['database'] = 'mysql://laconica:microblog@localhost/laconica'; -#$config['db']['ini_your_db_name'] = $config['db']['schema_location'].'/laconica.ini'; -# *** WARNING *** WARNING *** WARNING *** WARNING *** -# Setting debug to a non-zero value will expose your DATABASE PASSWORD to Web users. -# !!!!!! DO NOT SET THIS ON PRODUCTION SERVERS !!!!!! DB_DataObject's bug, btw, not -# ours. -# *** WARNING *** WARNING *** WARNING *** WARNING *** -#$config['db']['debug'] = 0; -#$config['db']['db_driver'] = 'MDB2'; - -#Database type. For mysql, these defaults are fine. For postgresql, set -#'quote_identifiers' to true and 'type' to 'pgsql': -#$config['db']['quote_identifiers'] = false; -#$config['db']['type'] = 'mysql'; - -#session_set_cookie_params(0, '/'. $config['site']['path'] .'/'); - -#Standard fancy-url clashes prevented by not allowing nicknames on a blacklist -#Add your own here. Note: empty array by default -#$config['nickname']['blacklist'][] = 'scobleizer'; - -# sphinx search +// $config['db']['ini_your_db_name'] = $config['db']['schema_location'].'/laconica.ini'; +// *** WARNING *** WARNING *** WARNING *** WARNING *** +// Setting debug to a non-zero value will expose your DATABASE PASSWORD to Web users. +// !!!!!! DO NOT SET THIS ON PRODUCTION SERVERS !!!!!! DB_DataObject's bug, btw, not +// ours. +// *** WARNING *** WARNING *** WARNING *** WARNING *** +// $config['db']['debug'] = 0; +// $config['db']['db_driver'] = 'MDB2'; + +// Database type. For mysql, these defaults are fine. For postgresql, set +// 'quote_identifiers' to true and 'type' to 'pgsql': +// $config['db']['quote_identifiers'] = false; +// $config['db']['type'] = 'mysql'; + +// session_set_cookie_params(0, '/'. $config['site']['path'] .'/'); + +// Standard fancy-url clashes prevented by not allowing nicknames on a blacklist +// Add your own here. Note: empty array by default +// $config['nickname']['blacklist'][] = 'scobleizer'; + +// sphinx search $config['sphinx']['enabled'] = false; $config['sphinx']['server'] = 'localhost'; $config['sphinx']['port'] = 3312; -# Users to populate the 'Featured' tab -#$config['nickname']['featured'][] = 'scobleizer'; - -# xmpp -#$config['xmpp']['enabled'] = false; -#$config['xmpp']['server'] = 'server.example.net'; -#$config['xmpp']['host'] = NULL; # Only set if different from server -#$config['xmpp']['port'] = 5222; -#$config['xmpp']['user'] = 'update'; -#$config['xmpp']['encryption'] = false; -#$config['xmpp']['resource'] = 'uniquename'; -#$config['xmpp']['password'] = 'blahblahblah'; -#$config['xmpp']['public'][] = 'someindexer@example.net'; -#$config['xmpp']['debug'] = false; - -#Default locale info -#$config['site']['timezone'] = 'Pacific/Auckland'; -#$config['site']['language'] = 'en_NZ'; - -#Email info, used for all outbound email -#$config['mail']['notifyfrom'] = 'microblog@example.net'; -#$config['mail']['domain'] = 'microblog.example.net'; -# See http://pear.php.net/manual/en/package.mail.mail.factory.php for options -#$config['mail']['backend'] = 'smtp'; -#$config['mail']['params'] = array( -# 'host' => 'localhost', -# 'port' => 25, -# ); -#For incoming email, if enabled. Defaults to site server name. -#$config['mail']['domain'] = 'incoming.example.net'; - -#exponential decay factor for tags, default 10 days -#raise this if traffic is slow, lower it if it's fast -#$config['tag']['dropoff'] = 86400.0 * 10; - -#exponential decay factor for popular (most favorited notices) -#default 10 days -- similar to tag dropoff -#$config['popular']['dropoff'] = 86400.0 * 10; - -#optionally show non-local messages in public timeline -#$config['public']['localonly'] = false; - -#hide certain users from public pages, by ID -#$config['public']['blacklist'][] = 123; -#$config['public']['blacklist'][] = 2307; - -#Mark certain notice sources as automatic and thus not -#appropriate for public feed -#$config['public]['autosource'][] = 'twitterfeed'; -#$config['public]['autosource'][] = 'rssdent'; -#$config['public]['autosource'][] = 'Ping.Fm'; -#$config['public]['autosource'][] = 'HelloTxt'; -#$config['public]['autosource'][] = 'Updating.Me'; - -#Do notice broadcasts offline -#If you use this, you must run the six offline daemons in the -#background. See the README for details. -#$config['queue']['enabled'] = true; - -#The following customise the behaviour of the various daemons: -#$config['daemon']['piddir'] = '/var/run'; -#$config['daemon']['user'] = false; -#$config['daemon']['group'] = false; - -#For installations with high traffic, laconica can use MemCached to cache -#frequently requested information. Only enable the following if you have -#MemCached up and running: -#$config['memcached']['enabled'] = false; -#$config['memcached']['server'] = 'localhost'; -#$config['memcached']['port'] = 11211; - -#Twitter integration source attribute. Note: default is Laconica -#$config['integration']['source'] = 'Laconica'; - -# Edit throttling. Off by default. If turned on, you can only post 20 notices -# every 10 minutes. Admins may want to play with the settings to minimize inconvenience for -# real users without getting uncontrollable floods from spammers or runaway bots. - -#$config['throttle']['enabled'] = true; -#$config['throttle']['count'] = 100; -#$config['throttle']['timespan'] = 3600; - -# List of users banned from posting (nicknames and/or IDs) -#$config['profile']['banned'][] = 'hacker'; -#$config['profile']['banned'][] = 12345; - -# Config section for the built-in Facebook application -#$config['facebook']['apikey'] = 'APIKEY'; -#$config['facebook']['secret'] = 'SECRET'; - -# Add Google Analytics -# require_once('plugins/GoogleAnalyticsPlugin.php'); -# $ga = new GoogleAnalyticsPlugin('your secret code'); - -# Use Templating (template: /tpl/index.php) -# require_once('plugins/TemplatePlugin.php'); -# $tpl = new TemplatePlugin(); - -#Don't allow saying the same thing more than once per hour -#$config['site']['dupelimit'] = 3600; -#Don't enforce the dupe limit -#$config['site']['dupelimit'] = -1; - -#Base string for minting Tag URIs in Atom feeds. Defaults to -#"yourserver,2009". This needs to be configured properly for your Atom -#feeds to validate. See: http://www.faqs.org/rfcs/rfc4151.html and -#http://taguri.org/ Examples: -#$config['integration']['taguri'] = 'example.net,2008'; -#$config['integration']['taguri'] = 'admin@example.net,2009-03-09' - -#Don't use SSL -#$config['site']['ssl'] = 'never'; -#Use SSL only for sensitive pages (like login, password change) -#$config['site']['ssl'] = 'sometimes'; -#Use SSL for all pages -#$config['site']['ssl'] = 'always'; - -#Use a different hostname for SSL-encrypted pages -#$config['site']['sslserver'] = 'secure.example.org'; +// Users to populate the 'Featured' tab +// $config['nickname']['featured'][] = 'scobleizer'; + +// xmpp +// $config['xmpp']['enabled'] = false; +// $config['xmpp']['server'] = 'server.example.net'; +// $config['xmpp']['host'] = NULL; // Only set if different from server +// $config['xmpp']['port'] = 5222; +// $config['xmpp']['user'] = 'update'; +// $config['xmpp']['encryption'] = false; +// $config['xmpp']['resource'] = 'uniquename'; +// $config['xmpp']['password'] = 'blahblahblah'; +// $config['xmpp']['public'][] = 'someindexer@example.net'; +// $config['xmpp']['debug'] = false; + +// Default locale info +// $config['site']['timezone'] = 'Pacific/Auckland'; +// $config['site']['language'] = 'en_NZ'; + +// Email info, used for all outbound email +// $config['mail']['notifyfrom'] = 'microblog@example.net'; +// $config['mail']['domain'] = 'microblog.example.net'; +// See http://pear.php.net/manual/en/package.mail.mail.factory.php for options +// $config['mail']['backend'] = 'smtp'; +// $config['mail']['params'] = array( +// 'host' => 'localhost', +// 'port' => 25, +// ); +// For incoming email, if enabled. Defaults to site server name. +// $config['mail']['domain'] = 'incoming.example.net'; + +// exponential decay factor for tags, default 10 days +// raise this if traffic is slow, lower it if it's fast +// $config['tag']['dropoff'] = 86400.0 * 10; + +// exponential decay factor for popular (most favorited notices) +// default 10 days -- similar to tag dropoff +// $config['popular']['dropoff'] = 86400.0 * 10; + +// optionally show non-local messages in public timeline +// $config['public']['localonly'] = false; + +// hide certain users from public pages, by ID +// $config['public']['blacklist'][] = 123; +// $config['public']['blacklist'][] = 2307; + +// Mark certain notice sources as automatic and thus not +// appropriate for public feed +// $config['public]['autosource'][] = 'twitterfeed'; +// $config['public]['autosource'][] = 'rssdent'; +// $config['public]['autosource'][] = 'Ping.Fm'; +// $config['public]['autosource'][] = 'HelloTxt'; +// $config['public]['autosource'][] = 'Updating.Me'; + +// Do notice broadcasts offline +// If you use this, you must run the six offline daemons in the +// background. See the README for details. +// $config['queue']['enabled'] = true; + +// Queue subsystem +// subsystems: internal (default) or stomp +// using stomp requires an external message queue server +// $config['queue']['subsystem'] = 'stomp'; +// $config['queue']['stomp_server'] = 'tcp://localhost:61613'; +// use different queue_basename for each laconica instance managed by the server +// $config['queue']['queue_basename'] = 'laconica'; + +// The following customise the behaviour of the various daemons: +// $config['daemon']['piddir'] = '/var/run'; +// $config['daemon']['user'] = false; +// $config['daemon']['group'] = false; + +// For installations with high traffic, laconica can use MemCached to cache +// frequently requested information. Only enable the following if you have +// MemCached up and running: +// $config['memcached']['enabled'] = false; +// $config['memcached']['server'] = 'localhost'; +// $config['memcached']['port'] = 11211; + +// Twitter integration source attribute. Note: default is Laconica +// $config['integration']['source'] = 'Laconica'; + +// Edit throttling. Off by default. If turned on, you can only post 20 notices +// every 10 minutes. Admins may want to play with the settings to minimize inconvenience for +// real users without getting uncontrollable floods from spammers or runaway bots. + +// $config['throttle']['enabled'] = true; +// $config['throttle']['count'] = 100; +// $config['throttle']['timespan'] = 3600; + +// List of users banned from posting (nicknames and/or IDs) +// $config['profile']['banned'][] = 'hacker'; +// $config['profile']['banned'][] = 12345; + +// Config section for the built-in Facebook application +// $config['facebook']['apikey'] = 'APIKEY'; +// $config['facebook']['secret'] = 'SECRET'; + +// Add Google Analytics +// require_once('plugins/GoogleAnalyticsPlugin.php'); +// $ga = new GoogleAnalyticsPlugin('your secret code'); + +// Use Templating (template: /tpl/index.php) +// require_once('plugins/TemplatePlugin.php'); +// $tpl = new TemplatePlugin(); + +// Don't allow saying the same thing more than once per hour +// $config['site']['dupelimit'] = 3600; +// Don't enforce the dupe limit +// $config['site']['dupelimit'] = -1; + +// Base string for minting Tag URIs in Atom feeds. Defaults to +// "yourserver,2009". This needs to be configured properly for your Atom +// feeds to validate. See: http://www.faqs.org/rfcs/rfc4151.html and +// http://taguri.org/ Examples: +// $config['integration']['taguri'] = 'example.net,2008'; +// $config['integration']['taguri'] = 'admin@example.net,2009-03-09' + +// Don't use SSL +// $config['site']['ssl'] = 'never'; +// Use SSL only for sensitive pages (like login, password change) +// $config['site']['ssl'] = 'sometimes'; +// Use SSL for all pages +// $config['site']['ssl'] = 'always'; + +// Use a different hostname for SSL-encrypted pages +// $config['site']['sslserver'] = 'secure.example.org'; + +// If you have a lot of status networks on the same server, you can +// store the site data in a database and switch as follows +// Status_network::setupDB('localhost', 'statusnet', 'statuspass', 'statusnet'); +// if (!Status_network::setupSite($_server, $_path)) { +// print "Error\n"; +// exit(1); +// } + +// How often to send snapshots; in # of web hits. Ideally, +// try to do this once per month (that is, make this equal to number +// of hits per month) +// $config['snapshot']['frequency'] = 10000; +// If you don't want to report statistics to the central server, uncomment. +// $config['snapshot']['run'] = 'never'; +// If you want to report statistics in a cron job instead. +// $config['snapshot']['run'] = 'cron'; diff --git a/db/foreign_services.sql b/db/foreign_services.sql index 557ede0246..79c04cee56 100644 --- a/db/foreign_services.sql +++ b/db/foreign_services.sql @@ -2,4 +2,5 @@ insert into foreign_service (id, name, description, created) values ('1','Twitter', 'Twitter Micro-blogging service', now()), - ('2','Facebook', 'Facebook', now()); + ('2','Facebook', 'Facebook', now()), + ('3','FacebookConnect', 'Facebook Connect', now()); diff --git a/db/laconica.sql b/db/laconica.sql index c9730098e3..a11e316925 100644 --- a/db/laconica.sql +++ b/db/laconica.sql @@ -114,8 +114,10 @@ create table notice ( reply_to integer comment 'notice replied to (usually a guess)' references notice (id), is_local tinyint default 0 comment 'notice was generated by a user', source varchar(32) comment 'source of comment, like "web", "im", or "clientname"', + conversation integer comment 'id of root notice in this conversation' references notice (id), index notice_profile_id_idx (profile_id), + index notice_conversation_idx (conversation), index notice_created_idx (created), index notice_replyto_idx (reply_to), FULLTEXT(content) @@ -283,7 +285,7 @@ create table foreign_user ( create table foreign_link ( user_id int comment 'link to user on this system, if exists' references user (id), - foreign_id int comment 'link ' references foreign_user(id), + foreign_id bigint unsigned comment 'link to user on foreign service, if exists' references foreign_user(id), service int not null comment 'foreign key to service' references foreign_service(id), credentials varchar(255) comment 'authc credentials, typically a password', noticesync tinyint not null default 1 comment 'notice synchronization, bit 1 = sync outgoing, bit 2 = sync incoming, bit 3 = filter local replies', @@ -423,3 +425,60 @@ create table group_inbox ( ) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin; +create table file ( + id integer primary key auto_increment, + url varchar(255), mimetype varchar(50), + size integer, + title varchar(255), + date integer(11), + protected integer(1), + + unique(url) +) ENGINE=MyISAM CHARACTER SET utf8 COLLATE utf8_general_ci; + +create table file_oembed ( + id integer primary key auto_increment, + file_id integer, + version varchar(20), + type varchar(20), + provider varchar(50), + provider_url varchar(255), + width integer, + height integer, + html text, + title varchar(255), + author_name varchar(50), + author_url varchar(255), + url varchar(255), + + unique(file_id) +) ENGINE=MyISAM CHARACTER SET utf8 COLLATE utf8_general_ci; + +create table file_redirection ( + id integer primary key auto_increment, + url varchar(255), + file_id integer, + redirections integer, + httpcode integer, + + unique(url) +) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin; + +create table file_thumbnail ( + id integer primary key auto_increment, + file_id integer, + url varchar(255), + width integer, + height integer, + + unique(file_id), + unique(url) +) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin; + +create table file_to_post ( + id integer primary key auto_increment, + file_id integer, + post_id integer, + + unique(file_id, post_id) +) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin; diff --git a/db/laconica_pg.sql b/db/laconica_pg.sql index a27a616f24..b213bbd502 100644 --- a/db/laconica_pg.sql +++ b/db/laconica_pg.sql @@ -427,6 +427,64 @@ create table group_inbox ( ); create index group_inbox_created_idx on group_inbox using btree(created); + +/*attachments and URLs stuff */ +create sequence file_seq; +create table file ( + id bigint default nextval('file_seq') primary key /* comment 'unique identifier' */, + url varchar(255) unique, + mimetype varchar(50), + size integer, + title varchar(255), + date integer(11), + protected integer(1) +); + +create sequence file_oembed_seq; +create table file_oembed ( + id bigint default nextval('file_oembed_seq') primary key /* comment 'unique identifier' */, + file_id bigint unique, + version varchar(20), + type varchar(20), + provider varchar(50), + provider_url varchar(255), + width integer, + height integer, + html text, + title varchar(255), + author_name varchar(50), + author_url varchar(255), + url varchar(255), +); + +create sequence file_redirection_seq; +create table file_redirection ( + id bigint default nextval('file_redirection_seq') primary key /* comment 'unique identifier' */, + url varchar(255) unique, + file_id bigint, + redirections integer, + httpcode integer +); + +create sequence file_thumbnail_seq; +create table file_thumbnail ( + id bigint default nextval('file_thumbnail_seq') primary key /* comment 'unique identifier' */, + file_id bigint unique, + url varchar(255) unique, + width integer, + height integer +); + +create sequence file_to_post_seq; +create table file_to_post ( + id bigint default nextval('file_to_post_seq') primary key /* comment 'unique identifier' */, + file_id bigint, + post_id bigint, + + unique(file_id, post_id) +); + + /* Textsearch stuff */ create index textsearch_idx on profile using gist(textsearch); diff --git a/db/notice_source.sql b/db/notice_source.sql index ac73d3d13b..d5a280b82c 100644 --- a/db/notice_source.sql +++ b/db/notice_source.sql @@ -48,6 +48,7 @@ VALUES ('twidge','Twidge','http://software.complete.org/twidge', now()), ('twidroid','twidroid','http://www.twidroid.com/', now()), ('twittelator','Twittelator','http://www.stone.com/iPhone/Twittelator/', now()), + ('twitter','Twitter','http://twitter.com/', now()), ('twitterfeed','twitterfeed','http://twitterfeed.com/', now()), ('twitterphoto','TwitterPhoto','http://richfish.org/twitterphoto/', now()), ('twitterpm','Net::Twitter','http://search.cpan.org/dist/Net-Twitter/', now()), diff --git a/db/site.sql b/db/site.sql new file mode 100644 index 0000000000..660ba475bb --- /dev/null +++ b/db/site.sql @@ -0,0 +1,17 @@ +/* For managing multiple sites */ + +create table status_network ( + + nickname varchar(64) primary key comment 'nickname', + hostname varchar(255) unique key comment 'alternate hostname if any', + pathname varchar(255) unique key comment 'alternate pathname if any', + sitename varchar(255) comment 'display name', + dbhost varchar(255) comment 'database host', + dbuser varchar(255) comment 'database username', + dbpass varchar(255) comment 'database password', + dbname varchar(255) comment 'database name', + + created datetime not null comment 'date this record was created', + modified timestamp comment 'date this record was modified' + +) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci; diff --git a/extlib/DB/DataObject.php b/extlib/DB/DataObject.php index b1a1a4e218..0c6a13dc28 100644 --- a/extlib/DB/DataObject.php +++ b/extlib/DB/DataObject.php @@ -2357,6 +2357,8 @@ class DB_DataObject extends DB_DataObject_Overload $t= explode(' ',microtime()); $_DB_DATAOBJECT['QUERYENDTIME'] = $time = $t[0]+$t[1]; + + do { if ($_DB_driver == 'DB') { $result = $DB->query($string); @@ -2374,8 +2376,19 @@ class DB_DataObject extends DB_DataObject_Overload break; } } - - + + // try to reconnect, at most 3 times + $again = false; + if (is_a($result, 'PEAR_Error') + AND $result->getCode() == DB_ERROR_NODBSELECTED + AND $cpt++<3) { + $DB->disconnect(); + sleep(1); + $DB->connect($DB->dsn); + $again = true; + } + + } while ($again); if (is_a($result,'PEAR_Error')) { if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) { diff --git a/extlib/HTTP/Request.php b/extlib/HTTP/Request.php new file mode 100644 index 0000000000..42eac3b141 --- /dev/null +++ b/extlib/HTTP/Request.php @@ -0,0 +1,1521 @@ + + * @author Alexey Borzov + * @copyright 2002-2007 Richard Heyes + * @license http://opensource.org/licenses/bsd-license.php New BSD License + * @version CVS: $Id: Request.php,v 1.63 2008/10/11 11:07:10 avb Exp $ + * @link http://pear.php.net/package/HTTP_Request/ + */ + +/** + * PEAR and PEAR_Error classes (for error handling) + */ +require_once 'PEAR.php'; +/** + * Socket class + */ +require_once 'Net/Socket.php'; +/** + * URL handling class + */ +require_once 'Net/URL.php'; + +/**#@+ + * Constants for HTTP request methods + */ +define('HTTP_REQUEST_METHOD_GET', 'GET', true); +define('HTTP_REQUEST_METHOD_HEAD', 'HEAD', true); +define('HTTP_REQUEST_METHOD_POST', 'POST', true); +define('HTTP_REQUEST_METHOD_PUT', 'PUT', true); +define('HTTP_REQUEST_METHOD_DELETE', 'DELETE', true); +define('HTTP_REQUEST_METHOD_OPTIONS', 'OPTIONS', true); +define('HTTP_REQUEST_METHOD_TRACE', 'TRACE', true); +/**#@-*/ + +/**#@+ + * Constants for HTTP request error codes + */ +define('HTTP_REQUEST_ERROR_FILE', 1); +define('HTTP_REQUEST_ERROR_URL', 2); +define('HTTP_REQUEST_ERROR_PROXY', 4); +define('HTTP_REQUEST_ERROR_REDIRECTS', 8); +define('HTTP_REQUEST_ERROR_RESPONSE', 16); +define('HTTP_REQUEST_ERROR_GZIP_METHOD', 32); +define('HTTP_REQUEST_ERROR_GZIP_READ', 64); +define('HTTP_REQUEST_ERROR_GZIP_DATA', 128); +define('HTTP_REQUEST_ERROR_GZIP_CRC', 256); +/**#@-*/ + +/**#@+ + * Constants for HTTP protocol versions + */ +define('HTTP_REQUEST_HTTP_VER_1_0', '1.0', true); +define('HTTP_REQUEST_HTTP_VER_1_1', '1.1', true); +/**#@-*/ + +if (extension_loaded('mbstring') && (2 & ini_get('mbstring.func_overload'))) { + /** + * Whether string functions are overloaded by their mbstring equivalents + */ + define('HTTP_REQUEST_MBSTRING', true); +} else { + /** + * @ignore + */ + define('HTTP_REQUEST_MBSTRING', false); +} + +/** + * Class for performing HTTP requests + * + * Simple example (fetches yahoo.com and displays it): + * + * $a = &new HTTP_Request('http://www.yahoo.com/'); + * $a->sendRequest(); + * echo $a->getResponseBody(); + * + * + * @category HTTP + * @package HTTP_Request + * @author Richard Heyes + * @author Alexey Borzov + * @version Release: 1.4.4 + */ +class HTTP_Request +{ + /**#@+ + * @access private + */ + /** + * Instance of Net_URL + * @var Net_URL + */ + var $_url; + + /** + * Type of request + * @var string + */ + var $_method; + + /** + * HTTP Version + * @var string + */ + var $_http; + + /** + * Request headers + * @var array + */ + var $_requestHeaders; + + /** + * Basic Auth Username + * @var string + */ + var $_user; + + /** + * Basic Auth Password + * @var string + */ + var $_pass; + + /** + * Socket object + * @var Net_Socket + */ + var $_sock; + + /** + * Proxy server + * @var string + */ + var $_proxy_host; + + /** + * Proxy port + * @var integer + */ + var $_proxy_port; + + /** + * Proxy username + * @var string + */ + var $_proxy_user; + + /** + * Proxy password + * @var string + */ + var $_proxy_pass; + + /** + * Post data + * @var array + */ + var $_postData; + + /** + * Request body + * @var string + */ + var $_body; + + /** + * A list of methods that MUST NOT have a request body, per RFC 2616 + * @var array + */ + var $_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 + */ + var $_bodyRequired = array('POST', 'PUT'); + + /** + * Files to post + * @var array + */ + var $_postFiles = array(); + + /** + * Connection timeout. + * @var float + */ + var $_timeout; + + /** + * HTTP_Response object + * @var HTTP_Response + */ + var $_response; + + /** + * Whether to allow redirects + * @var boolean + */ + var $_allowRedirects; + + /** + * Maximum redirects allowed + * @var integer + */ + var $_maxRedirects; + + /** + * Current number of redirects + * @var integer + */ + var $_redirects; + + /** + * Whether to append brackets [] to array variables + * @var bool + */ + var $_useBrackets = true; + + /** + * Attached listeners + * @var array + */ + var $_listeners = array(); + + /** + * Whether to save response body in response object property + * @var bool + */ + var $_saveBody = true; + + /** + * Timeout for reading from socket (array(seconds, microseconds)) + * @var array + */ + var $_readTimeout = null; + + /** + * Options to pass to Net_Socket::connect. See stream_context_create + * @var array + */ + var $_socketOptions = null; + /**#@-*/ + + /** + * Constructor + * + * Sets up the object + * @param string The url to fetch/access + * @param array Associative array of parameters which can have the following keys: + *
      + *
    • method - Method to use, GET, POST etc (string)
    • + *
    • http - HTTP Version to use, 1.0 or 1.1 (string)
    • + *
    • user - Basic Auth username (string)
    • + *
    • pass - Basic Auth password (string)
    • + *
    • proxy_host - Proxy server host (string)
    • + *
    • proxy_port - Proxy server port (integer)
    • + *
    • proxy_user - Proxy auth username (string)
    • + *
    • proxy_pass - Proxy auth password (string)
    • + *
    • timeout - Connection timeout in seconds (float)
    • + *
    • allowRedirects - Whether to follow redirects or not (bool)
    • + *
    • maxRedirects - Max number of redirects to follow (integer)
    • + *
    • useBrackets - Whether to append [] to array variable names (bool)
    • + *
    • saveBody - Whether to save response body in response object property (bool)
    • + *
    • readTimeout - Timeout for reading / writing data over the socket (array (seconds, microseconds))
    • + *
    • socketOptions - Options to pass to Net_Socket object (array)
    • + *
    + * @access public + */ + function HTTP_Request($url = '', $params = array()) + { + $this->_method = HTTP_REQUEST_METHOD_GET; + $this->_http = HTTP_REQUEST_HTTP_VER_1_1; + $this->_requestHeaders = array(); + $this->_postData = array(); + $this->_body = null; + + $this->_user = null; + $this->_pass = null; + + $this->_proxy_host = null; + $this->_proxy_port = null; + $this->_proxy_user = null; + $this->_proxy_pass = null; + + $this->_allowRedirects = false; + $this->_maxRedirects = 3; + $this->_redirects = 0; + + $this->_timeout = null; + $this->_response = null; + + foreach ($params as $key => $value) { + $this->{'_' . $key} = $value; + } + + if (!empty($url)) { + $this->setURL($url); + } + + // Default useragent + $this->addHeader('User-Agent', 'PEAR HTTP_Request class ( http://pear.php.net/ )'); + + // We don't do keep-alives by default + $this->addHeader('Connection', 'close'); + + // Basic authentication + if (!empty($this->_user)) { + $this->addHeader('Authorization', 'Basic ' . base64_encode($this->_user . ':' . $this->_pass)); + } + + // Proxy authentication (see bug #5913) + if (!empty($this->_proxy_user)) { + $this->addHeader('Proxy-Authorization', 'Basic ' . base64_encode($this->_proxy_user . ':' . $this->_proxy_pass)); + } + + // Use gzip encoding if possible + if (HTTP_REQUEST_HTTP_VER_1_1 == $this->_http && extension_loaded('zlib')) { + $this->addHeader('Accept-Encoding', 'gzip'); + } + } + + /** + * Generates a Host header for HTTP/1.1 requests + * + * @access private + * @return string + */ + function _generateHostHeader() + { + if ($this->_url->port != 80 AND strcasecmp($this->_url->protocol, 'http') == 0) { + $host = $this->_url->host . ':' . $this->_url->port; + + } elseif ($this->_url->port != 443 AND strcasecmp($this->_url->protocol, 'https') == 0) { + $host = $this->_url->host . ':' . $this->_url->port; + + } elseif ($this->_url->port == 443 AND strcasecmp($this->_url->protocol, 'https') == 0 AND strpos($this->_url->url, ':443') !== false) { + $host = $this->_url->host . ':' . $this->_url->port; + + } else { + $host = $this->_url->host; + } + + return $host; + } + + /** + * Resets the object to its initial state (DEPRECATED). + * Takes the same parameters as the constructor. + * + * @param string $url The url to be requested + * @param array $params Associative array of parameters + * (see constructor for details) + * @access public + * @deprecated deprecated since 1.2, call the constructor if this is necessary + */ + function reset($url, $params = array()) + { + $this->HTTP_Request($url, $params); + } + + /** + * Sets the URL to be requested + * + * @param string The url to be requested + * @access public + */ + function setURL($url) + { + $this->_url = &new Net_URL($url, $this->_useBrackets); + + if (!empty($this->_url->user) || !empty($this->_url->pass)) { + $this->setBasicAuth($this->_url->user, $this->_url->pass); + } + + if (HTTP_REQUEST_HTTP_VER_1_1 == $this->_http) { + $this->addHeader('Host', $this->_generateHostHeader()); + } + + // set '/' instead of empty path rather than check later (see bug #8662) + if (empty($this->_url->path)) { + $this->_url->path = '/'; + } + } + + /** + * Returns the current request URL + * + * @return string Current request URL + * @access public + */ + function getUrl() + { + return empty($this->_url)? '': $this->_url->getUrl(); + } + + /** + * Sets a proxy to be used + * + * @param string Proxy host + * @param int Proxy port + * @param string Proxy username + * @param string Proxy password + * @access public + */ + function setProxy($host, $port = 8080, $user = null, $pass = null) + { + $this->_proxy_host = $host; + $this->_proxy_port = $port; + $this->_proxy_user = $user; + $this->_proxy_pass = $pass; + + if (!empty($user)) { + $this->addHeader('Proxy-Authorization', 'Basic ' . base64_encode($user . ':' . $pass)); + } + } + + /** + * Sets basic authentication parameters + * + * @param string Username + * @param string Password + */ + function setBasicAuth($user, $pass) + { + $this->_user = $user; + $this->_pass = $pass; + + $this->addHeader('Authorization', 'Basic ' . base64_encode($user . ':' . $pass)); + } + + /** + * Sets the method to be used, GET, POST etc. + * + * @param string Method to use. Use the defined constants for this + * @access public + */ + function setMethod($method) + { + $this->_method = $method; + } + + /** + * Sets the HTTP version to use, 1.0 or 1.1 + * + * @param string Version to use. Use the defined constants for this + * @access public + */ + function setHttpVer($http) + { + $this->_http = $http; + } + + /** + * Adds a request header + * + * @param string Header name + * @param string Header value + * @access public + */ + function addHeader($name, $value) + { + $this->_requestHeaders[strtolower($name)] = $value; + } + + /** + * Removes a request header + * + * @param string Header name to remove + * @access public + */ + function removeHeader($name) + { + if (isset($this->_requestHeaders[strtolower($name)])) { + unset($this->_requestHeaders[strtolower($name)]); + } + } + + /** + * Adds a querystring parameter + * + * @param string Querystring parameter name + * @param string Querystring parameter value + * @param bool Whether the value is already urlencoded or not, default = not + * @access public + */ + function addQueryString($name, $value, $preencoded = false) + { + $this->_url->addQueryString($name, $value, $preencoded); + } + + /** + * Sets the querystring to literally what you supply + * + * @param string The querystring data. Should be of the format foo=bar&x=y etc + * @param bool Whether data is already urlencoded or not, default = already encoded + * @access public + */ + function addRawQueryString($querystring, $preencoded = true) + { + $this->_url->addRawQueryString($querystring, $preencoded); + } + + /** + * Adds postdata items + * + * @param string Post data name + * @param string Post data value + * @param bool Whether data is already urlencoded or not, default = not + * @access public + */ + function addPostData($name, $value, $preencoded = false) + { + if ($preencoded) { + $this->_postData[$name] = $value; + } else { + $this->_postData[$name] = $this->_arrayMapRecursive('urlencode', $value); + } + } + + /** + * Recursively applies the callback function to the value + * + * @param mixed Callback function + * @param mixed Value to process + * @access private + * @return mixed Processed value + */ + function _arrayMapRecursive($callback, $value) + { + if (!is_array($value)) { + return call_user_func($callback, $value); + } else { + $map = array(); + foreach ($value as $k => $v) { + $map[$k] = $this->_arrayMapRecursive($callback, $v); + } + return $map; + } + } + + /** + * 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. + * + * @access public + * @param string name of file-upload field + * @param mixed file name(s) + * @param mixed content-type(s) of file(s) being uploaded + * @return bool true on success + * @throws PEAR_Error + */ + function addFile($inputName, $fileName, $contentType = 'application/octet-stream') + { + if (!is_array($fileName) && !is_readable($fileName)) { + return PEAR::raiseError("File '{$fileName}' is not readable", HTTP_REQUEST_ERROR_FILE); + } elseif (is_array($fileName)) { + foreach ($fileName as $name) { + if (!is_readable($name)) { + return PEAR::raiseError("File '{$name}' is not readable", HTTP_REQUEST_ERROR_FILE); + } + } + } + $this->addHeader('Content-Type', 'multipart/form-data'); + $this->_postFiles[$inputName] = array( + 'name' => $fileName, + 'type' => $contentType + ); + return true; + } + + /** + * Adds raw postdata (DEPRECATED) + * + * @param string The data + * @param bool Whether data is preencoded or not, default = already encoded + * @access public + * @deprecated deprecated since 1.3.0, method setBody() should be used instead + */ + function addRawPostData($postdata, $preencoded = true) + { + $this->_body = $preencoded ? $postdata : urlencode($postdata); + } + + /** + * Sets the request body (for POST, PUT and similar requests) + * + * @param string Request body + * @access public + */ + function setBody($body) + { + $this->_body = $body; + } + + /** + * Clears any postdata that has been added (DEPRECATED). + * + * Useful for multiple request scenarios. + * + * @access public + * @deprecated deprecated since 1.2 + */ + function clearPostData() + { + $this->_postData = null; + } + + /** + * Appends a cookie to "Cookie:" header + * + * @param string $name cookie name + * @param string $value cookie value + * @access public + */ + function addCookie($name, $value) + { + $cookies = isset($this->_requestHeaders['cookie']) ? $this->_requestHeaders['cookie']. '; ' : ''; + $this->addHeader('Cookie', $cookies . $name . '=' . $value); + } + + /** + * Clears any cookies that have been added (DEPRECATED). + * + * Useful for multiple request scenarios + * + * @access public + * @deprecated deprecated since 1.2 + */ + function clearCookies() + { + $this->removeHeader('Cookie'); + } + + /** + * Sends the request + * + * @access public + * @param bool Whether to store response body in Response object property, + * set this to false if downloading a LARGE file and using a Listener + * @return mixed PEAR error on error, true otherwise + */ + function sendRequest($saveBody = true) + { + if (!is_a($this->_url, 'Net_URL')) { + return PEAR::raiseError('No URL given', HTTP_REQUEST_ERROR_URL); + } + + $host = isset($this->_proxy_host) ? $this->_proxy_host : $this->_url->host; + $port = isset($this->_proxy_port) ? $this->_proxy_port : $this->_url->port; + + if (strcasecmp($this->_url->protocol, 'https') == 0) { + // Bug #14127, don't try connecting to HTTPS sites without OpenSSL + if (version_compare(PHP_VERSION, '4.3.0', '<') || !extension_loaded('openssl')) { + return PEAR::raiseError('Need PHP 4.3.0 or later with OpenSSL support for https:// requests', + HTTP_REQUEST_ERROR_URL); + } elseif (isset($this->_proxy_host)) { + return PEAR::raiseError('HTTPS proxies are not supported', HTTP_REQUEST_ERROR_PROXY); + } + $host = 'ssl://' . $host; + } + + // magic quotes may fuck up file uploads and chunked response processing + $magicQuotes = ini_get('magic_quotes_runtime'); + ini_set('magic_quotes_runtime', false); + + // RFC 2068, section 19.7.1: A client MUST NOT send the Keep-Alive + // connection token to a proxy server... + if (isset($this->_proxy_host) && !empty($this->_requestHeaders['connection']) && + 'Keep-Alive' == $this->_requestHeaders['connection']) + { + $this->removeHeader('connection'); + } + + $keepAlive = (HTTP_REQUEST_HTTP_VER_1_1 == $this->_http && empty($this->_requestHeaders['connection'])) || + (!empty($this->_requestHeaders['connection']) && 'Keep-Alive' == $this->_requestHeaders['connection']); + $sockets = &PEAR::getStaticProperty('HTTP_Request', 'sockets'); + $sockKey = $host . ':' . $port; + unset($this->_sock); + + // There is a connected socket in the "static" property? + if ($keepAlive && !empty($sockets[$sockKey]) && + !empty($sockets[$sockKey]->fp)) + { + $this->_sock =& $sockets[$sockKey]; + $err = null; + } else { + $this->_notify('connect'); + $this->_sock =& new Net_Socket(); + $err = $this->_sock->connect($host, $port, null, $this->_timeout, $this->_socketOptions); + } + PEAR::isError($err) or $err = $this->_sock->write($this->_buildRequest()); + + if (!PEAR::isError($err)) { + if (!empty($this->_readTimeout)) { + $this->_sock->setTimeout($this->_readTimeout[0], $this->_readTimeout[1]); + } + + $this->_notify('sentRequest'); + + // Read the response + $this->_response = &new HTTP_Response($this->_sock, $this->_listeners); + $err = $this->_response->process( + $this->_saveBody && $saveBody, + HTTP_REQUEST_METHOD_HEAD != $this->_method + ); + + if ($keepAlive) { + $keepAlive = (isset($this->_response->_headers['content-length']) + || (isset($this->_response->_headers['transfer-encoding']) + && strtolower($this->_response->_headers['transfer-encoding']) == 'chunked')); + if ($keepAlive) { + if (isset($this->_response->_headers['connection'])) { + $keepAlive = strtolower($this->_response->_headers['connection']) == 'keep-alive'; + } else { + $keepAlive = 'HTTP/'.HTTP_REQUEST_HTTP_VER_1_1 == $this->_response->_protocol; + } + } + } + } + + ini_set('magic_quotes_runtime', $magicQuotes); + + if (PEAR::isError($err)) { + return $err; + } + + if (!$keepAlive) { + $this->disconnect(); + // Store the connected socket in "static" property + } elseif (empty($sockets[$sockKey]) || empty($sockets[$sockKey]->fp)) { + $sockets[$sockKey] =& $this->_sock; + } + + // Check for redirection + if ( $this->_allowRedirects + AND $this->_redirects <= $this->_maxRedirects + AND $this->getResponseCode() > 300 + AND $this->getResponseCode() < 399 + AND !empty($this->_response->_headers['location'])) { + + + $redirect = $this->_response->_headers['location']; + + // Absolute URL + if (preg_match('/^https?:\/\//i', $redirect)) { + $this->_url = &new Net_URL($redirect); + $this->addHeader('Host', $this->_generateHostHeader()); + // Absolute path + } elseif ($redirect{0} == '/') { + $this->_url->path = $redirect; + + // Relative path + } elseif (substr($redirect, 0, 3) == '../' OR substr($redirect, 0, 2) == './') { + if (substr($this->_url->path, -1) == '/') { + $redirect = $this->_url->path . $redirect; + } else { + $redirect = dirname($this->_url->path) . '/' . $redirect; + } + $redirect = Net_URL::resolvePath($redirect); + $this->_url->path = $redirect; + + // Filename, no path + } else { + if (substr($this->_url->path, -1) == '/') { + $redirect = $this->_url->path . $redirect; + } else { + $redirect = dirname($this->_url->path) . '/' . $redirect; + } + $this->_url->path = $redirect; + } + + $this->_redirects++; + return $this->sendRequest($saveBody); + + // Too many redirects + } elseif ($this->_allowRedirects AND $this->_redirects > $this->_maxRedirects) { + return PEAR::raiseError('Too many redirects', HTTP_REQUEST_ERROR_REDIRECTS); + } + + return true; + } + + /** + * Disconnect the socket, if connected. Only useful if using Keep-Alive. + * + * @access public + */ + function disconnect() + { + if (!empty($this->_sock) && !empty($this->_sock->fp)) { + $this->_notify('disconnect'); + $this->_sock->disconnect(); + } + } + + /** + * Returns the response code + * + * @access public + * @return mixed Response code, false if not set + */ + function getResponseCode() + { + return isset($this->_response->_code) ? $this->_response->_code : false; + } + + /** + * Returns the response reason phrase + * + * @access public + * @return mixed Response reason phrase, false if not set + */ + function getResponseReason() + { + return isset($this->_response->_reason) ? $this->_response->_reason : false; + } + + /** + * Returns either the named header or all if no name given + * + * @access public + * @param string The header name to return, do not set to get all headers + * @return mixed either the value of $headername (false if header is not present) + * or an array of all headers + */ + function getResponseHeader($headername = null) + { + if (!isset($headername)) { + return isset($this->_response->_headers)? $this->_response->_headers: array(); + } else { + $headername = strtolower($headername); + return isset($this->_response->_headers[$headername]) ? $this->_response->_headers[$headername] : false; + } + } + + /** + * Returns the body of the response + * + * @access public + * @return mixed response body, false if not set + */ + function getResponseBody() + { + return isset($this->_response->_body) ? $this->_response->_body : false; + } + + /** + * Returns cookies set in response + * + * @access public + * @return mixed array of response cookies, false if none are present + */ + function getResponseCookies() + { + return isset($this->_response->_cookies) ? $this->_response->_cookies : false; + } + + /** + * Builds the request string + * + * @access private + * @return string The request string + */ + function _buildRequest() + { + $separator = ini_get('arg_separator.output'); + ini_set('arg_separator.output', '&'); + $querystring = ($querystring = $this->_url->getQueryString()) ? '?' . $querystring : ''; + ini_set('arg_separator.output', $separator); + + $host = isset($this->_proxy_host) ? $this->_url->protocol . '://' . $this->_url->host : ''; + $port = (isset($this->_proxy_host) AND $this->_url->port != 80) ? ':' . $this->_url->port : ''; + $path = $this->_url->path . $querystring; + $url = $host . $port . $path; + + if (!strlen($url)) { + $url = '/'; + } + + $request = $this->_method . ' ' . $url . ' HTTP/' . $this->_http . "\r\n"; + + if (in_array($this->_method, $this->_bodyDisallowed) || + (0 == strlen($this->_body) && (HTTP_REQUEST_METHOD_POST != $this->_method || + (empty($this->_postData) && empty($this->_postFiles))))) + { + $this->removeHeader('Content-Type'); + } else { + if (empty($this->_requestHeaders['content-type'])) { + // Add default content-type + $this->addHeader('Content-Type', 'application/x-www-form-urlencoded'); + } elseif ('multipart/form-data' == $this->_requestHeaders['content-type']) { + $boundary = 'HTTP_Request_' . md5(uniqid('request') . microtime()); + $this->addHeader('Content-Type', 'multipart/form-data; boundary=' . $boundary); + } + } + + // Request Headers + if (!empty($this->_requestHeaders)) { + foreach ($this->_requestHeaders as $name => $value) { + $canonicalName = implode('-', array_map('ucfirst', explode('-', $name))); + $request .= $canonicalName . ': ' . $value . "\r\n"; + } + } + + // Method does not allow a body, simply add a final CRLF + if (in_array($this->_method, $this->_bodyDisallowed)) { + + $request .= "\r\n"; + + // Post data if it's an array + } elseif (HTTP_REQUEST_METHOD_POST == $this->_method && + (!empty($this->_postData) || !empty($this->_postFiles))) { + + // "normal" POST request + if (!isset($boundary)) { + $postdata = implode('&', array_map( + create_function('$a', 'return $a[0] . \'=\' . $a[1];'), + $this->_flattenArray('', $this->_postData) + )); + + // multipart request, probably with file uploads + } else { + $postdata = ''; + if (!empty($this->_postData)) { + $flatData = $this->_flattenArray('', $this->_postData); + foreach ($flatData as $item) { + $postdata .= '--' . $boundary . "\r\n"; + $postdata .= 'Content-Disposition: form-data; name="' . $item[0] . '"'; + $postdata .= "\r\n\r\n" . urldecode($item[1]) . "\r\n"; + } + } + foreach ($this->_postFiles as $name => $value) { + if (is_array($value['name'])) { + $varname = $name . ($this->_useBrackets? '[]': ''); + } else { + $varname = $name; + $value['name'] = array($value['name']); + } + foreach ($value['name'] as $key => $filename) { + $fp = fopen($filename, 'r'); + $basename = basename($filename); + $type = is_array($value['type'])? @$value['type'][$key]: $value['type']; + + $postdata .= '--' . $boundary . "\r\n"; + $postdata .= 'Content-Disposition: form-data; name="' . $varname . '"; filename="' . $basename . '"'; + $postdata .= "\r\nContent-Type: " . $type; + $postdata .= "\r\n\r\n" . fread($fp, filesize($filename)) . "\r\n"; + fclose($fp); + } + } + $postdata .= '--' . $boundary . "--\r\n"; + } + $request .= 'Content-Length: ' . + (HTTP_REQUEST_MBSTRING? mb_strlen($postdata, 'iso-8859-1'): strlen($postdata)) . + "\r\n\r\n"; + $request .= $postdata; + + // Explicitly set request body + } elseif (0 < strlen($this->_body)) { + + $request .= 'Content-Length: ' . + (HTTP_REQUEST_MBSTRING? mb_strlen($this->_body, 'iso-8859-1'): strlen($this->_body)) . + "\r\n\r\n"; + $request .= $this->_body; + + // No body: send a Content-Length header nonetheless (request #12900), + // but do that only for methods that require a body (bug #14740) + } else { + + if (in_array($this->_method, $this->_bodyRequired)) { + $request .= "Content-Length: 0\r\n"; + } + $request .= "\r\n"; + } + + return $request; + } + + /** + * Helper function to change the (probably multidimensional) associative array + * into the simple one. + * + * @param string name for item + * @param mixed item's values + * @return array array with the following items: array('item name', 'item value'); + * @access private + */ + function _flattenArray($name, $values) + { + if (!is_array($values)) { + return array(array($name, $values)); + } else { + $ret = array(); + foreach ($values as $k => $v) { + if (empty($name)) { + $newName = $k; + } elseif ($this->_useBrackets) { + $newName = $name . '[' . $k . ']'; + } else { + $newName = $name; + } + $ret = array_merge($ret, $this->_flattenArray($newName, $v)); + } + return $ret; + } + } + + + /** + * Adds a Listener to the list of listeners that are notified of + * the object's events + * + * Events sent by HTTP_Request object + * - 'connect': on connection to server + * - 'sentRequest': after the request was sent + * - 'disconnect': on disconnection from server + * + * Events sent by HTTP_Response object + * - 'gotHeaders': after receiving response headers (headers are passed in $data) + * - 'tick': on receiving a part of response body (the part is passed in $data) + * - 'gzTick': on receiving a gzip-encoded part of response body (ditto) + * - 'gotBody': after receiving the response body (passes the decoded body in $data if it was gzipped) + * + * @param HTTP_Request_Listener listener to attach + * @return boolean whether the listener was successfully attached + * @access public + */ + function attach(&$listener) + { + if (!is_a($listener, 'HTTP_Request_Listener')) { + return false; + } + $this->_listeners[$listener->getId()] =& $listener; + return true; + } + + + /** + * Removes a Listener from the list of listeners + * + * @param HTTP_Request_Listener listener to detach + * @return boolean whether the listener was successfully detached + * @access public + */ + function detach(&$listener) + { + if (!is_a($listener, 'HTTP_Request_Listener') || + !isset($this->_listeners[$listener->getId()])) { + return false; + } + unset($this->_listeners[$listener->getId()]); + return true; + } + + + /** + * Notifies all registered listeners of an event. + * + * @param string Event name + * @param mixed Additional data + * @access private + * @see HTTP_Request::attach() + */ + function _notify($event, $data = null) + { + foreach (array_keys($this->_listeners) as $id) { + $this->_listeners[$id]->update($this, $event, $data); + } + } +} + + +/** + * Response class to complement the Request class + * + * @category HTTP + * @package HTTP_Request + * @author Richard Heyes + * @author Alexey Borzov + * @version Release: 1.4.4 + */ +class HTTP_Response +{ + /** + * Socket object + * @var Net_Socket + */ + var $_sock; + + /** + * Protocol + * @var string + */ + var $_protocol; + + /** + * Return code + * @var string + */ + var $_code; + + /** + * Response reason phrase + * @var string + */ + var $_reason; + + /** + * Response headers + * @var array + */ + var $_headers; + + /** + * Cookies set in response + * @var array + */ + var $_cookies; + + /** + * Response body + * @var string + */ + var $_body = ''; + + /** + * Used by _readChunked(): remaining length of the current chunk + * @var string + */ + var $_chunkLength = 0; + + /** + * Attached listeners + * @var array + */ + var $_listeners = array(); + + /** + * Bytes left to read from message-body + * @var null|int + */ + var $_toRead; + + /** + * Constructor + * + * @param Net_Socket socket to read the response from + * @param array listeners attached to request + */ + function HTTP_Response(&$sock, &$listeners) + { + $this->_sock =& $sock; + $this->_listeners =& $listeners; + } + + + /** + * Processes a HTTP response + * + * This extracts response code, headers, cookies and decodes body if it + * was encoded in some way + * + * @access public + * @param bool Whether to store response body in object property, set + * this to false if downloading a LARGE file and using a Listener. + * This is assumed to be true if body is gzip-encoded. + * @param bool Whether the response can actually have a message-body. + * Will be set to false for HEAD requests. + * @throws PEAR_Error + * @return mixed true on success, PEAR_Error in case of malformed response + */ + function process($saveBody = true, $canHaveBody = true) + { + do { + $line = $this->_sock->readLine(); + if (!preg_match('!^(HTTP/\d\.\d) (\d{3})(?: (.+))?!', $line, $s)) { + return PEAR::raiseError('Malformed response', HTTP_REQUEST_ERROR_RESPONSE); + } else { + $this->_protocol = $s[1]; + $this->_code = intval($s[2]); + $this->_reason = empty($s[3])? null: $s[3]; + } + while ('' !== ($header = $this->_sock->readLine())) { + $this->_processHeader($header); + } + } while (100 == $this->_code); + + $this->_notify('gotHeaders', $this->_headers); + + // RFC 2616, section 4.4: + // 1. Any response message which "MUST NOT" include a message-body ... + // is always terminated by the first empty line after the header fields + // 3. ... If a message is received with both a + // Transfer-Encoding header field and a Content-Length header field, + // the latter MUST be ignored. + $canHaveBody = $canHaveBody && $this->_code >= 200 && + $this->_code != 204 && $this->_code != 304; + + // If response body is present, read it and decode + $chunked = isset($this->_headers['transfer-encoding']) && ('chunked' == $this->_headers['transfer-encoding']); + $gzipped = isset($this->_headers['content-encoding']) && ('gzip' == $this->_headers['content-encoding']); + $hasBody = false; + if ($canHaveBody && ($chunked || !isset($this->_headers['content-length']) || + 0 != $this->_headers['content-length'])) + { + if ($chunked || !isset($this->_headers['content-length'])) { + $this->_toRead = null; + } else { + $this->_toRead = $this->_headers['content-length']; + } + while (!$this->_sock->eof() && (is_null($this->_toRead) || 0 < $this->_toRead)) { + if ($chunked) { + $data = $this->_readChunked(); + } elseif (is_null($this->_toRead)) { + $data = $this->_sock->read(4096); + } else { + $data = $this->_sock->read(min(4096, $this->_toRead)); + $this->_toRead -= HTTP_REQUEST_MBSTRING? mb_strlen($data, 'iso-8859-1'): strlen($data); + } + if ('' == $data && (!$this->_chunkLength || $this->_sock->eof())) { + break; + } else { + $hasBody = true; + if ($saveBody || $gzipped) { + $this->_body .= $data; + } + $this->_notify($gzipped? 'gzTick': 'tick', $data); + } + } + } + + if ($hasBody) { + // Uncompress the body if needed + if ($gzipped) { + $body = $this->_decodeGzip($this->_body); + if (PEAR::isError($body)) { + return $body; + } + $this->_body = $body; + $this->_notify('gotBody', $this->_body); + } else { + $this->_notify('gotBody'); + } + } + return true; + } + + + /** + * Processes the response header + * + * @access private + * @param string HTTP header + */ + function _processHeader($header) + { + if (false === strpos($header, ':')) { + return; + } + list($headername, $headervalue) = explode(':', $header, 2); + $headername = strtolower($headername); + $headervalue = ltrim($headervalue); + + if ('set-cookie' != $headername) { + if (isset($this->_headers[$headername])) { + $this->_headers[$headername] .= ',' . $headervalue; + } else { + $this->_headers[$headername] = $headervalue; + } + } else { + $this->_parseCookie($headervalue); + } + } + + + /** + * Parse a Set-Cookie header to fill $_cookies array + * + * @access private + * @param string value of Set-Cookie header + */ + function _parseCookie($headervalue) + { + $cookie = array( + 'expires' => null, + 'domain' => null, + 'path' => null, + 'secure' => false + ); + + // Only a name=value pair + if (!strpos($headervalue, ';')) { + $pos = strpos($headervalue, '='); + $cookie['name'] = trim(substr($headervalue, 0, $pos)); + $cookie['value'] = trim(substr($headervalue, $pos + 1)); + + // Some optional parameters are supplied + } else { + $elements = explode(';', $headervalue); + $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; + } + + + /** + * Read a part of response body encoded with chunked Transfer-Encoding + * + * @access private + * @return string + */ + function _readChunked() + { + // at start of the next chunk? + if (0 == $this->_chunkLength) { + $line = $this->_sock->readLine(); + if (preg_match('/^([0-9a-f]+)/i', $line, $matches)) { + $this->_chunkLength = hexdec($matches[1]); + // Chunk with zero length indicates the end + if (0 == $this->_chunkLength) { + $this->_sock->readLine(); // make this an eof() + return ''; + } + } else { + return ''; + } + } + $data = $this->_sock->read($this->_chunkLength); + $this->_chunkLength -= HTTP_REQUEST_MBSTRING? mb_strlen($data, 'iso-8859-1'): strlen($data); + if (0 == $this->_chunkLength) { + $this->_sock->readLine(); // Trailing CRLF + } + return $data; + } + + + /** + * Notifies all registered listeners of an event. + * + * @param string Event name + * @param mixed Additional data + * @access private + * @see HTTP_Request::_notify() + */ + function _notify($event, $data = null) + { + foreach (array_keys($this->_listeners) as $id) { + $this->_listeners[$id]->update($this, $event, $data); + } + } + + + /** + * 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 + * + * @access private + * @param string gzip-encoded data + * @return string decoded data + */ + function _decodeGzip($data) + { + if (HTTP_REQUEST_MBSTRING) { + $oldEncoding = mb_internal_encoding(); + mb_internal_encoding('iso-8859-1'); + } + $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; + } + $method = ord(substr($data, 2, 1)); + if (8 != $method) { + return PEAR::raiseError('_decodeGzip(): unknown compression method', HTTP_REQUEST_ERROR_GZIP_METHOD); + } + $flags = ord(substr($data, 3, 1)); + if ($flags & 224) { + return PEAR::raiseError('_decodeGzip(): reserved bits are set', HTTP_REQUEST_ERROR_GZIP_DATA); + } + + // 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) { + return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA); + } + $extraLength = unpack('v', substr($data, 10, 2)); + if ($length - $headerLength - 2 - $extraLength[1] < 8) { + return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA); + } + $headerLength += $extraLength[1] + 2; + } + // file name, need to skip that + if ($flags & 8) { + if ($length - $headerLength - 1 < 8) { + return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA); + } + $filenameLength = strpos(substr($data, $headerLength), chr(0)); + if (false === $filenameLength || $length - $headerLength - $filenameLength - 1 < 8) { + return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA); + } + $headerLength += $filenameLength + 1; + } + // comment, need to skip that also + if ($flags & 16) { + if ($length - $headerLength - 1 < 8) { + return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA); + } + $commentLength = strpos(substr($data, $headerLength), chr(0)); + if (false === $commentLength || $length - $headerLength - $commentLength - 1 < 8) { + return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA); + } + $headerLength += $commentLength + 1; + } + // have a CRC for header. let's check + if ($flags & 1) { + if ($length - $headerLength - 2 < 8) { + return PEAR::raiseError('_decodeGzip(): data too short', HTTP_REQUEST_ERROR_GZIP_DATA); + } + $crcReal = 0xffff & crc32(substr($data, 0, $headerLength)); + $crcStored = unpack('v', substr($data, $headerLength, 2)); + if ($crcReal != $crcStored[1]) { + return PEAR::raiseError('_decodeGzip(): header CRC check failed', HTTP_REQUEST_ERROR_GZIP_CRC); + } + $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) { + return PEAR::raiseError('_decodeGzip(): gzinflate() call failed', HTTP_REQUEST_ERROR_GZIP_READ); + } elseif ($dataSize != strlen($unpacked)) { + return PEAR::raiseError('_decodeGzip(): data size check failed', HTTP_REQUEST_ERROR_GZIP_READ); + } elseif ((0xffffffff & $dataCrc) != (0xffffffff & crc32($unpacked))) { + return PEAR::raiseError('_decodeGzip(): data CRC check failed', HTTP_REQUEST_ERROR_GZIP_CRC); + } + if (HTTP_REQUEST_MBSTRING) { + mb_internal_encoding($oldEncoding); + } + return $unpacked; + } +} // End class HTTP_Response +?> diff --git a/extlib/HTTP/Request/Listener.php b/extlib/HTTP/Request/Listener.php new file mode 100644 index 0000000000..b4fe444b35 --- /dev/null +++ b/extlib/HTTP/Request/Listener.php @@ -0,0 +1,106 @@ + + * @copyright 2002-2007 Richard Heyes + * @license http://opensource.org/licenses/bsd-license.php New BSD License + * @version CVS: $Id: Listener.php,v 1.3 2007/05/18 10:33:31 avb Exp $ + * @link http://pear.php.net/package/HTTP_Request/ + */ + +/** + * Listener for HTTP_Request and HTTP_Response objects + * + * This class implements the Observer part of a Subject-Observer + * design pattern. + * + * @category HTTP + * @package HTTP_Request + * @author Alexey Borzov + * @version Release: 1.4.4 + */ +class HTTP_Request_Listener +{ + /** + * A listener's identifier + * @var string + */ + var $_id; + + /** + * Constructor, sets the object's identifier + * + * @access public + */ + function HTTP_Request_Listener() + { + $this->_id = md5(uniqid('http_request_', 1)); + } + + + /** + * Returns the listener's identifier + * + * @access public + * @return string + */ + function getId() + { + return $this->_id; + } + + + /** + * This method is called when Listener is notified of an event + * + * @access public + * @param object an object the listener is attached to + * @param string Event name + * @param mixed Additional data + * @abstract + */ + function update(&$subject, $event, $data = null) + { + echo "Notified of event: '$event'\n"; + if (null !== $data) { + echo "Additional data: "; + var_dump($data); + } + } +} +?> diff --git a/extlib/Net/URL2.php b/extlib/Net/URL2.php new file mode 100644 index 0000000000..7a654aed8f --- /dev/null +++ b/extlib/Net/URL2.php @@ -0,0 +1,813 @@ + | +// +-----------------------------------------------------------------------+ +// +// $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 + */ +class Net_URL2 +{ + /** + * Do strict parsing in resolve() (see RFC 3986, section 5.2.2). Default + * is true. + */ + const OPTION_STRICT = 'strict'; + + /** + * Represent arrays in query using PHP's [] notation. Default is true. + */ + const OPTION_USE_BRACKETS = 'use_brackets'; + + /** + * URL-encode query variable keys. Default is true. + */ + const OPTION_ENCODE_KEYS = 'encode_keys'; + + /** + * Query variable separators when parsing the query string. Every character + * is considered a separator. Default is specified by the + * arg_separator.input php.ini setting (this defaults to "&"). + */ + const OPTION_SEPARATOR_INPUT = 'input_separator'; + + /** + * Query variable separator used when generating the query string. Default + * is specified by the arg_separator.output php.ini setting (this defaults + * to "&"). + */ + const OPTION_SEPARATOR_OUTPUT = 'output_separator'; + + /** + * Default options corresponds to how PHP handles $_GET. + */ + private $options = array( + self::OPTION_STRICT => true, + self::OPTION_USE_BRACKETS => true, + self::OPTION_ENCODE_KEYS => true, + self::OPTION_SEPARATOR_INPUT => 'x&', + self::OPTION_SEPARATOR_OUTPUT => 'x&', + ); + + /** + * @var string|bool + */ + private $scheme = false; + + /** + * @var string|bool + */ + private $userinfo = false; + + /** + * @var string|bool + */ + private $host = false; + + /** + * @var int|bool + */ + private $port = false; + + /** + * @var string + */ + private $path = ''; + + /** + * @var string|bool + */ + private $query = false; + + /** + * @var string|bool + */ + private $fragment = false; + + /** + * @param string $url an absolute or relative URL + * @param array $options + */ + public function __construct($url, $options = null) + { + $this->setOption(self::OPTION_SEPARATOR_INPUT, + ini_get('arg_separator.input')); + $this->setOption(self::OPTION_SEPARATOR_OUTPUT, + ini_get('arg_separator.output')); + if (is_array($options)) { + foreach ($options as $optionName => $value) { + $this->setOption($optionName); + } + } + + if (preg_match('@^([a-z][a-z0-9.+-]*):@i', $url, $reg)) { + $this->scheme = $reg[1]; + $url = substr($url, strlen($reg[0])); + } + + if (preg_match('@^//([^/#?]+)@', $url, $reg)) { + $this->setAuthority($reg[1]); + $url = substr($url, strlen($reg[0])); + } + + $i = strcspn($url, '?#'); + $this->path = substr($url, 0, $i); + $url = substr($url, $i); + + if (preg_match('@^\?([^#]*)@', $url, $reg)) { + $this->query = $reg[1]; + $url = substr($url, strlen($reg[0])); + } + + if ($url) { + $this->fragment = substr($url, 1); + } + } + + /** + * Returns the scheme, e.g. "http" or "urn", or false if there is no + * scheme specified, i.e. if this is a relative URL. + * + * @return string|bool + */ + public function getScheme() + { + return $this->scheme; + } + + /** + * @param string|bool $scheme + * + * @return void + * @see getScheme() + */ + public function setScheme($scheme) + { + $this->scheme = $scheme; + } + + /** + * Returns the user part of the userinfo part (the part preceding the first + * ":"), or false if there is no userinfo part. + * + * @return string|bool + */ + public function getUser() + { + return $this->userinfo !== false ? preg_replace('@:.*$@', '', $this->userinfo) : false; + } + + /** + * Returns the password part of the userinfo part (the part after the first + * ":"), or false if there is no userinfo part (i.e. the URL does not + * contain "@" in front of the hostname) or the userinfo part does not + * contain ":". + * + * @return string|bool + */ + public function getPassword() + { + return $this->userinfo !== false ? substr(strstr($this->userinfo, ':'), 1) : false; + } + + /** + * Returns the userinfo part, or false if there is none, i.e. if the + * authority part does not contain "@". + * + * @return string|bool + */ + public function getUserinfo() + { + return $this->userinfo; + } + + /** + * Sets the userinfo part. If two arguments are passed, they are combined + * in the userinfo part as username ":" password. + * + * @param string|bool $userinfo userinfo or username + * @param string|bool $password + * + * @return void + */ + public function setUserinfo($userinfo, $password = false) + { + $this->userinfo = $userinfo; + if ($password !== false) { + $this->userinfo .= ':' . $password; + } + } + + /** + * Returns the host part, or false if there is no authority part, e.g. + * relative URLs. + * + * @return string|bool + */ + public function getHost() + { + return $this->host; + } + + /** + * @param string|bool $host + * + * @return void + */ + public function setHost($host) + { + $this->host = $host; + } + + /** + * Returns the port number, or false if there is no port number specified, + * i.e. if the default port is to be used. + * + * @return int|bool + */ + public function getPort() + { + return $this->port; + } + + /** + * @param int|bool $port + * + * @return void + */ + public function setPort($port) + { + $this->port = intval($port); + } + + /** + * Returns the authority part, i.e. [ userinfo "@" ] host [ ":" port ], or + * false if there is no authority none. + * + * @return string|bool + */ + public function getAuthority() + { + if (!$this->host) { + return false; + } + + $authority = ''; + + if ($this->userinfo !== false) { + $authority .= $this->userinfo . '@'; + } + + $authority .= $this->host; + + if ($this->port !== false) { + $authority .= ':' . $this->port; + } + + return $authority; + } + + /** + * @param string|false $authority + * + * @return void + */ + public function setAuthority($authority) + { + $this->user = false; + $this->pass = false; + $this->host = false; + $this->port = false; + if (preg_match('@^(([^\@]+)\@)?([^:]+)(:(\d*))?$@', $authority, $reg)) { + if ($reg[1]) { + $this->userinfo = $reg[2]; + } + + $this->host = $reg[3]; + if (isset($reg[5])) { + $this->port = intval($reg[5]); + } + } + } + + /** + * Returns the path part (possibly an empty string). + * + * @return string + */ + public function getPath() + { + return $this->path; + } + + /** + * @param string $path + * + * @return void + */ + public function setPath($path) + { + $this->path = $path; + } + + /** + * Returns the query string (excluding the leading "?"), or false if "?" + * isn't present in the URL. + * + * @return string|bool + * @see self::getQueryVariables() + */ + public function getQuery() + { + return $this->query; + } + + /** + * @param string|bool $query + * + * @return void + * @see self::setQueryVariables() + */ + public function setQuery($query) + { + $this->query = $query; + } + + /** + * Returns the fragment name, or false if "#" isn't present in the URL. + * + * @return string|bool + */ + public function getFragment() + { + return $this->fragment; + } + + /** + * @param string|bool $fragment + * + * @return void + */ + public function setFragment($fragment) + { + $this->fragment = $fragment; + } + + /** + * Returns the query string like an array as the variables would appear in + * $_GET in a PHP script. + * + * @return array + */ + public function getQueryVariables() + { + $pattern = '/[' . + preg_quote($this->getOption(self::OPTION_SEPARATOR_INPUT), '/') . + ']/'; + $parts = preg_split($pattern, $this->query, -1, PREG_SPLIT_NO_EMPTY); + $return = array(); + + foreach ($parts as $part) { + if (strpos($part, '=') !== false) { + list($key, $value) = explode('=', $part, 2); + } else { + $key = $part; + $value = null; + } + + if ($this->getOption(self::OPTION_ENCODE_KEYS)) { + $key = rawurldecode($key); + } + $value = rawurldecode($value); + + if ($this->getOption(self::OPTION_USE_BRACKETS) && + preg_match('#^(.*)\[([0-9a-z_-]*)\]#i', $key, $matches)) { + + $key = $matches[1]; + $idx = $matches[2]; + + // Ensure is an array + if (empty($return[$key]) || !is_array($return[$key])) { + $return[$key] = array(); + } + + // Add data + if ($idx === '') { + $return[$key][] = $value; + } else { + $return[$key][$idx] = $value; + } + } elseif (!$this->getOption(self::OPTION_USE_BRACKETS) + && !empty($return[$key]) + ) { + $return[$key] = (array) $return[$key]; + $return[$key][] = $value; + } else { + $return[$key] = $value; + } + } + + return $return; + } + + /** + * @param array $array (name => value) array + * + * @return void + */ + public function setQueryVariables(array $array) + { + if (!$array) { + $this->query = false; + } else { + foreach ($array as $name => $value) { + if ($this->getOption(self::OPTION_ENCODE_KEYS)) { + $name = rawurlencode($name); + } + + if (is_array($value)) { + foreach ($value as $k => $v) { + $parts[] = $this->getOption(self::OPTION_USE_BRACKETS) + ? sprintf('%s[%s]=%s', $name, $k, $v) + : ($name . '=' . $v); + } + } elseif (!is_null($value)) { + $parts[] = $name . '=' . $value; + } else { + $parts[] = $name; + } + } + $this->query = implode($this->getOption(self::OPTION_SEPARATOR_OUTPUT), + $parts); + } + } + + /** + * @param string $name + * @param mixed $value + * + * @return array + */ + public function setQueryVariable($name, $value) + { + $array = $this->getQueryVariables(); + $array[$name] = $value; + $this->setQueryVariables($array); + } + + /** + * @param string $name + * + * @return void + */ + public function unsetQueryVariable($name) + { + $array = $this->getQueryVariables(); + unset($array[$name]); + $this->setQueryVariables($array); + } + + /** + * Returns a string representation of this URL. + * + * @return string + */ + public function getURL() + { + // See RFC 3986, section 5.3 + $url = ""; + + if ($this->scheme !== false) { + $url .= $this->scheme . ':'; + } + + $authority = $this->getAuthority(); + if ($authority !== false) { + $url .= '//' . $authority; + } + $url .= $this->path; + + if ($this->query !== false) { + $url .= '?' . $this->query; + } + + if ($this->fragment !== false) { + $url .= '#' . $this->fragment; + } + + return $url; + } + + /** + * Returns a normalized string representation of this URL. This is useful + * for comparison of URLs. + * + * @return string + */ + public function getNormalizedURL() + { + $url = clone $this; + $url->normalize(); + return $url->getUrl(); + } + + /** + * Returns a normalized Net_URL2 instance. + * + * @return Net_URL2 + */ + public function normalize() + { + // See RFC 3886, section 6 + + // Schemes are case-insensitive + if ($this->scheme) { + $this->scheme = strtolower($this->scheme); + } + + // Hostnames are case-insensitive + if ($this->host) { + $this->host = strtolower($this->host); + } + + // Remove default port number for known schemes (RFC 3986, section 6.2.3) + if ($this->port && + $this->scheme && + $this->port == getservbyname($this->scheme, 'tcp')) { + + $this->port = false; + } + + // Normalize case of %XX percentage-encodings (RFC 3986, section 6.2.2.1) + foreach (array('userinfo', 'host', 'path') as $part) { + if ($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) + $this->path = self::removeDotSegments($this->path); + + // Scheme based normalization (RFC 3986, section 6.2.3) + if ($this->host && !$this->path) { + $this->path = '/'; + } + } + + /** + * Returns whether this instance represents an absolute URL. + * + * @return bool + */ + public function isAbsolute() + { + return (bool) $this->scheme; + } + + /** + * Returns an Net_URL2 instance representing an absolute URL relative to + * this URL. + * + * @param Net_URL2|string $reference relative URL + * + * @return Net_URL2 + */ + public function resolve($reference) + { + if (is_string($reference)) { + $reference = new self($reference); + } + if (!$this->isAbsolute()) { + throw new Exception('Base-URL must be absolute'); + } + + // A non-strict parser may ignore a scheme in the reference if it is + // identical to the base URI's scheme. + if (!$this->getOption(self::OPTION_STRICT) && $reference->scheme == $this->scheme) { + $reference->scheme = false; + } + + $target = new self(''); + if ($reference->scheme !== false) { + $target->scheme = $reference->scheme; + $target->setAuthority($reference->getAuthority()); + $target->path = self::removeDotSegments($reference->path); + $target->query = $reference->query; + } else { + $authority = $reference->getAuthority(); + if ($authority !== false) { + $target->setAuthority($authority); + $target->path = self::removeDotSegments($reference->path); + $target->query = $reference->query; + } else { + if ($reference->path == '') { + $target->path = $this->path; + if ($reference->query !== false) { + $target->query = $reference->query; + } else { + $target->query = $this->query; + } + } else { + if (substr($reference->path, 0, 1) == '/') { + $target->path = self::removeDotSegments($reference->path); + } else { + // Merge paths (RFC 3986, section 5.2.3) + if ($this->host !== false && $this->path == '') { + $target->path = '/' . $this->path; + } else { + $i = strrpos($this->path, '/'); + if ($i !== false) { + $target->path = substr($this->path, 0, $i + 1); + } + $target->path .= $reference->path; + } + $target->path = self::removeDotSegments($target->path); + } + $target->query = $reference->query; + } + $target->setAuthority($this->getAuthority()); + } + $target->scheme = $this->scheme; + } + + $target->fragment = $reference->fragment; + + return $target; + } + + /** + * Removes dots as described in RFC 3986, section 5.2.4, e.g. + * "/foo/../bar/baz" => "/bar/baz" + * + * @param string $path a path + * + * @return string a path + */ + private static function removeDotSegments($path) + { + $output = ''; + + // Make sure not to be trapped in an infinite loop due to a bug in this + // method + $j = 0; + while ($path && $j++ < 100) { + // Step A + if (substr($path, 0, 2) == './') { + $path = substr($path, 2); + } elseif (substr($path, 0, 3) == '../') { + $path = substr($path, 3); + + // Step B + } elseif (substr($path, 0, 3) == '/./' || $path == '/.') { + $path = '/' . substr($path, 3); + + // Step C + } elseif (substr($path, 0, 4) == '/../' || $path == '/..') { + $path = '/' . substr($path, 4); + $i = strrpos($output, '/'); + $output = $i === false ? '' : substr($output, 0, $i); + + // Step D + } elseif ($path == '.' || $path == '..') { + $path = ''; + + // Step E + } else { + $i = strpos($path, '/'); + if ($i === 0) { + $i = strpos($path, '/', 1); + } + if ($i === false) { + $i = strlen($path); + } + $output .= substr($path, 0, $i); + $path = substr($path, $i); + } + } + + return $output; + } + + /** + * Returns a Net_URL2 instance representing the canonical URL of the + * currently executing PHP script. + * + * @return string + */ + public static function getCanonical() + { + if (!isset($_SERVER['REQUEST_METHOD'])) { + // ALERT - no current URL + throw new Exception('Script was not called through a webserver'); + } + + // Begin with a relative URL + $url = new self($_SERVER['PHP_SELF']); + $url->scheme = isset($_SERVER['HTTPS']) ? 'https' : 'http'; + $url->host = $_SERVER['SERVER_NAME']; + $port = intval($_SERVER['SERVER_PORT']); + if ($url->scheme == 'http' && $port != 80 || + $url->scheme == 'https' && $port != 443) { + + $url->port = $port; + } + return $url; + } + + /** + * Returns the URL used to retrieve the current request. + * + * @return string + */ + public static function getRequestedURL() + { + return self::getRequested()->getUrl(); + } + + /** + * Returns a Net_URL2 instance representing the URL used to retrieve the + * current request. + * + * @return Net_URL2 + */ + public static function getRequested() + { + if (!isset($_SERVER['REQUEST_METHOD'])) { + // ALERT - no current URL + throw new Exception('Script was not called through a webserver'); + } + + // Begin with a relative URL + $url = new self($_SERVER['REQUEST_URI']); + $url->scheme = isset($_SERVER['HTTPS']) ? 'https' : 'http'; + // Set host and possibly port + $url->setAuthority($_SERVER['HTTP_HOST']); + return $url; + } + + /** + * Sets the specified option. + * + * @param string $optionName a self::OPTION_ constant + * @param mixed $value option value + * + * @return void + * @see self::OPTION_STRICT + * @see self::OPTION_USE_BRACKETS + * @see self::OPTION_ENCODE_KEYS + */ + function setOption($optionName, $value) + { + if (!array_key_exists($optionName, $this->options)) { + return false; + } + $this->options[$optionName] = $value; + } + + /** + * Returns the value of the specified option. + * + * @param string $optionName The name of the option to retrieve + * + * @return mixed + */ + function getOption($optionName) + { + return isset($this->options[$optionName]) + ? $this->options[$optionName] : false; + } +} diff --git a/extlib/Services/oEmbed.php b/extlib/Services/oEmbed.php new file mode 100644 index 0000000000..5d38ed883d --- /dev/null +++ b/extlib/Services/oEmbed.php @@ -0,0 +1,357 @@ + + * @copyright 2008 Digg.com, Inc. + * @license http://tinyurl.com/42zef New BSD License + * @version SVN: @version@ + * @link http://code.google.com/p/digg + * @link http://oembed.com + */ + +require_once 'Validate.php'; +require_once 'Net/URL2.php'; +require_once 'HTTP/Request.php'; +require_once 'Services/oEmbed/Exception.php'; +require_once 'Services/oEmbed/Exception/NoSupport.php'; +require_once 'Services/oEmbed/Object.php'; + +/** + * Base class for consuming oEmbed objects + * + * + * 'http://www.flickr.com/services/oembed/' + * )); + * $object = $oEmbed->getObject(); + * + * // All of the objects have somewhat sane __toString() methods that allow + * // you to output them directly. + * echo (string)$object; + * + * ?> + * + * + * @category Services + * @package Services_oEmbed + * @author Joe Stump + * @copyright 2008 Digg.com, Inc. + * @license http://tinyurl.com/42zef New BSD License + * @version Release: @version@ + * @link http://code.google.com/p/digg + * @link http://oembed.com + */ +class Services_oEmbed +{ + /** + * HTTP timeout in seconds + * + * All HTTP requests made by Services_oEmbed will respect this timeout. + * This can be passed to {@link Services_oEmbed::setOption()} or to the + * options parameter in {@link Services_oEmbed::__construct()}. + * + * @var string OPTION_TIMEOUT Timeout in seconds + */ + const OPTION_TIMEOUT = 'http_timeout'; + + /** + * HTTP User-Agent + * + * All HTTP requests made by Services_oEmbed will be sent with the + * string set by this option. + * + * @var string OPTION_USER_AGENT The HTTP User-Agent string + */ + const OPTION_USER_AGENT = 'http_user_agent'; + + /** + * The API's URI + * + * If the API is known ahead of time this option can be used to explicitly + * set it. If not present then the API is attempted to be discovered + * through the auto-discovery mechanism. + * + * @var string OPTION_API + */ + const OPTION_API = 'oembed_api'; + + /** + * Options for oEmbed requests + * + * @var array $options The options for making requests + */ + protected $options = array( + self::OPTION_TIMEOUT => 3, + self::OPTION_API => null, + self::OPTION_USER_AGENT => 'Services_oEmbed 0.1.0' + ); + + /** + * URL of object to get embed information for + * + * @var object $url {@link Net_URL2} instance of URL of object + */ + protected $url = null; + + /** + * Constructor + * + * @param string $url The URL to fetch an oEmbed for + * @param array $options A list of options for the oEmbed lookup + * + * @throws {@link Services_oEmbed_Exception} if the $url is invalid + * @throws {@link Services_oEmbed_Exception} when no valid API is found + * @return void + */ + public function __construct($url, array $options = array()) + { + if (Validate::uri($url)) { + $this->url = new Net_URL2($url); + } else { + throw new Services_oEmbed_Exception('URL is invalid'); + } + + if (count($options)) { + foreach ($options as $key => $val) { + $this->setOption($key, $val); + } + } + + if ($this->options[self::OPTION_API] === null) { + $this->options[self::OPTION_API] = $this->discover(); + } + } + + /** + * Set an option for the oEmbed request + * + * @param mixed $option The option name + * @param mixed $value The option value + * + * @see Services_oEmbed::OPTION_API, Services_oEmbed::OPTION_TIMEOUT + * @throws {@link Services_oEmbed_Exception} on invalid option + * @access public + * @return void + */ + public function setOption($option, $value) + { + switch ($option) { + case self::OPTION_API: + case self::OPTION_TIMEOUT: + break; + default: + throw new Services_oEmbed_Exception( + 'Invalid option "' . $option . '"' + ); + } + + $func = '_set_' . $option; + if (method_exists($this, $func)) { + $this->options[$option] = $this->$func($value); + } else { + $this->options[$option] = $value; + } + } + + /** + * Set the API option + * + * @param string $value The API's URI + * + * @throws {@link Services_oEmbed_Exception} on invalid API URI + * @see Validate::uri() + * @return string + */ + protected function _set_oembed_api($value) + { + if (!Validate::uri($value)) { + throw new Services_oEmbed_Exception( + 'API URI provided is invalid' + ); + } + + return $value; + } + + /** + * Get the oEmbed response + * + * @param array $params Optional parameters for + * + * @throws {@link Services_oEmbed_Exception} on cURL errors + * @throws {@link Services_oEmbed_Exception} on HTTP errors + * @throws {@link Services_oEmbed_Exception} when result is not parsable + * @return object The oEmbed response as an object + */ + public function getObject(array $params = array()) + { + $params['url'] = $this->url->getURL(); + if (!isset($params['format'])) { + $params['format'] = 'json'; + } + + $sets = array(); + foreach ($params as $var => $val) { + $sets[] = $var . '=' . urlencode($val); + } + + $url = $this->options[self::OPTION_API] . '?' . implode('&', $sets); + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_HEADER, false); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->options[self::OPTION_TIMEOUT]); + $result = curl_exec($ch); + + if (curl_errno($ch)) { + throw new Services_oEmbed_Exception( + curl_error($ch), curl_errno($ch) + ); + } + + $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + if (substr($code, 0, 1) != '2') { + throw new Services_oEmbed_Exception('Non-200 code returned'); + } + + curl_close($ch); + + switch ($params['format']) { + case 'json': + $res = json_decode($result); + if (!is_object($res)) { + throw new Services_oEmbed_Exception( + 'Could not parse JSON response' + ); + } + break; + case 'xml': + libxml_use_internal_errors(true); + $res = simplexml_load_string($result); + if (!$res instanceof SimpleXMLElement) { + $errors = libxml_get_errors(); + $err = array_shift($errors); + libxml_clear_errors(); + libxml_use_internal_errors(false); + throw new Services_oEmbed_Exception( + $err->message, $error->code + ); + } + break; + } + + return Services_oEmbed_Object::factory($res); + } + + /** + * Discover an oEmbed API + * + * @param string $url The URL to attempt to discover oEmbed for + * + * @throws {@link Services_oEmbed_Exception} if the $url is invalid + * @return string The oEmbed API endpoint discovered + */ + protected function discover($url) + { + $body = $this->sendRequest($url); + + // Find all tags that have a valid oembed type set. We then + // extract the href attribute for each type. + $regexp = '#]*)type="' . + '(application/json|text/xml)\+oembed"([^>]*)>#i'; + + $m = $ret = array(); + if (!preg_match_all($regexp, $body, $m)) { + throw new Services_oEmbed_Exception_NoSupport( + 'No valid oEmbed links found on page' + ); + } + + foreach ($m[0] as $i => $link) { + $h = array(); + if (preg_match('/href="([^"]+)"/i', $link, $h)) { + $ret[$m[2][$i]] = $h[1]; + } + } + + return (isset($ret['json']) ? $ret['json'] : array_pop($ret)); + } + + /** + * Send a GET request to the provider + * + * @param mixed $url The URL to send the request to + * + * @throws {@link Services_oEmbed_Exception} on HTTP errors + * @return string The contents of the response + */ + private function sendRequest($url) + { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_HEADER, false); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->options[self::OPTION_TIMEOUT]); + curl_setopt($ch, CURLOPT_USERAGENT, $this->options[self::OPTION_USER_AGENT]); + $result = curl_exec($ch); + if (curl_errno($ch)) { + throw new Services_oEmbed_Exception( + curl_error($ch), curl_errno($ch) + ); + } + + $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); + if (substr($code, 0, 1) != '2') { + throw new Services_oEmbed_Exception('Non-200 code returned'); + } + + return $result; + } +} + +?> diff --git a/extlib/Services/oEmbed/Exception.php b/extlib/Services/oEmbed/Exception.php new file mode 100644 index 0000000000..446ac2a706 --- /dev/null +++ b/extlib/Services/oEmbed/Exception.php @@ -0,0 +1,65 @@ + + * @copyright 2008 Digg.com, Inc. + * @license http://tinyurl.com/42zef New BSD License + * @version SVN: @version@ + * @link http://code.google.com/p/digg + * @link http://oembed.com + */ + +require_once 'PEAR/Exception.php'; + +/** + * Base exception class for {@link Services_oEmbed} + * + * @category Services + * @package Services_oEmbed + * @author Joe Stump + * @copyright 2008 Digg.com, Inc. + * @license http://tinyurl.com/42zef New BSD License + * @version Release: @version@ + * @link http://code.google.com/p/digg + * @link http://oembed.com + */ +class Services_oEmbed_Exception extends PEAR_Exception +{ + +} + +?> diff --git a/extlib/Services/oEmbed/Exception/NoSupport.php b/extlib/Services/oEmbed/Exception/NoSupport.php new file mode 100644 index 0000000000..384c7191f2 --- /dev/null +++ b/extlib/Services/oEmbed/Exception/NoSupport.php @@ -0,0 +1,63 @@ + + * @copyright 2008 Digg.com, Inc. + * @license http://tinyurl.com/42zef New BSD License + * @version SVN: @version@ + * @link http://code.google.com/p/digg + * @link http://oembed.com + */ + +/** + * Exception class when no oEmbed support is discovered + * + * @category Services + * @package Services_oEmbed + * @author Joe Stump + * @copyright 2008 Digg.com, Inc. + * @license http://tinyurl.com/42zef New BSD License + * @version Release: @version@ + * @link http://code.google.com/p/digg + * @link http://oembed.com + */ +class Services_oEmbed_Exception_NoSupport extends Services_oEmbed_Exception +{ + +} + +?> diff --git a/extlib/Services/oEmbed/Object.php b/extlib/Services/oEmbed/Object.php new file mode 100644 index 0000000000..9eedd7efb6 --- /dev/null +++ b/extlib/Services/oEmbed/Object.php @@ -0,0 +1,126 @@ + + * @copyright 2008 Digg.com, Inc. + * @license http://tinyurl.com/42zef New BSD License + * @version SVN: @version@ + * @link http://code.google.com/p/digg + * @link http://oembed.com + */ + +require_once 'Services/oEmbed/Object/Exception.php'; + +/** + * Base class for consuming oEmbed objects + * + * @category Services + * @package Services_oEmbed + * @author Joe Stump + * @copyright 2008 Digg.com, Inc. + * @license http://tinyurl.com/42zef New BSD License + * @version Release: @version@ + * @link http://code.google.com/p/digg + * @link http://oembed.com + */ +abstract class Services_oEmbed_Object +{ + + /** + * Valid oEmbed object types + * + * @var array $types Array of valid object types + * @see Services_oEmbed_Object::factory() + */ + static protected $types = array( + 'photo' => 'Photo', + 'video' => 'Video', + 'link' => 'Link', + 'rich' => 'Rich' + ); + + /** + * Create an oEmbed object from result + * + * @param object $object Raw object returned from API + * + * @throws {@link Services_oEmbed_Object_Exception} on object error + * @return object Instance of object driver + * @see Services_oEmbed_Object_Link, Services_oEmbed_Object_Photo + * @see Services_oEmbed_Object_Rich, Services_oEmbed_Object_Video + */ + static public function factory($object) + { + if (!isset($object->type)) { + throw new Services_oEmbed_Object_Exception( + 'Object has no type' + ); + } + + $type = (string)$object->type; + if (!isset(self::$types[$type])) { + throw new Services_oEmbed_Object_Exception( + 'Object type is unknown or invalid: ' . $type + ); + } + + $file = 'Services/oEmbed/Object/' . self::$types[$type] . '.php'; + include_once $file; + + $class = 'Services_oEmbed_Object_' . self::$types[$type]; + if (!class_exists($class)) { + throw new Services_oEmbed_Object_Exception( + 'Object class is invalid or not present' + ); + } + + $instance = new $class($object); + return $instance; + } + + /** + * Instantiation is not allowed + * + * @return void + */ + private function __construct() + { + + } +} + +?> diff --git a/extlib/Services/oEmbed/Object/Common.php b/extlib/Services/oEmbed/Object/Common.php new file mode 100644 index 0000000000..f568ec89f5 --- /dev/null +++ b/extlib/Services/oEmbed/Object/Common.php @@ -0,0 +1,139 @@ + + * @copyright 2008 Digg.com, Inc. + * @license http://tinyurl.com/42zef New BSD License + * @version SVN: @version@ + * @link http://code.google.com/p/digg + * @link http://oembed.com + */ + +/** + * Base class for oEmbed objects + * + * @category Services + * @package Services_oEmbed + * @author Joe Stump + * @copyright 2008 Digg.com, Inc. + * @license http://tinyurl.com/42zef New BSD License + * @version Release: @version@ + * @link http://code.google.com/p/digg + * @link http://oembed.com + */ +abstract class Services_oEmbed_Object_Common +{ + /** + * Raw object returned from API + * + * @var object $object The raw object from the API + */ + protected $object = null; + + /** + * Required fields per the specification + * + * @var array $required Array of required fields + * @link http://oembed.com + */ + protected $required = array(); + + /** + * Constructor + * + * @param object $object Raw object returned from the API + * + * @throws {@link Services_oEmbed_Object_Exception} on missing fields + * @return void + */ + public function __construct($object) + { + $this->object = $object; + + $this->required[] = 'version'; + foreach ($this->required as $field) { + if (!isset($this->$field)) { + throw new Services_oEmbed_Object_Exception( + 'Object is missing required ' . $field . ' attribute' + ); + } + } + } + + /** + * Get object variable + * + * @param string $var Variable to get + * + * @see Services_oEmbed_Object_Common::$object + * @return mixed Attribute's value or null if it's not set/exists + */ + public function __get($var) + { + if (property_exists($this->object, $var)) { + return $this->object->$var; + } + + return null; + } + + /** + * Is variable set? + * + * @param string $var Variable name to check + * + * @return boolean True if set, false if not + * @see Services_oEmbed_Object_Common::$object + */ + public function __isset($var) + { + if (property_exists($this->object, $var)) { + return (isset($this->object->$var)); + } + + return false; + } + + /** + * Require a sane __toString for all objects + * + * @return string + */ + abstract public function __toString(); +} + +?> diff --git a/extlib/Services/oEmbed/Object/Exception.php b/extlib/Services/oEmbed/Object/Exception.php new file mode 100644 index 0000000000..6025ffd494 --- /dev/null +++ b/extlib/Services/oEmbed/Object/Exception.php @@ -0,0 +1,65 @@ + + * @copyright 2008 Digg.com, Inc. + * @license http://tinyurl.com/42zef New BSD License + * @version SVN: @version@ + * @link http://code.google.com/p/digg + * @link http://oembed.com + */ + +require_once 'Services/oEmbed/Exception.php'; + +/** + * Exception for {@link Services_oEmbed_Object} + * + * @category Services + * @package Services_oEmbed + * @author Joe Stump + * @copyright 2008 Digg.com, Inc. + * @license http://tinyurl.com/42zef New BSD License + * @version Release: @version@ + * @link http://code.google.com/p/digg + * @link http://oembed.com + */ +class Services_oEmbed_Object_Exception extends Services_oEmbed_Exception +{ + +} + +?> diff --git a/extlib/Services/oEmbed/Object/Link.php b/extlib/Services/oEmbed/Object/Link.php new file mode 100644 index 0000000000..9b627a89ac --- /dev/null +++ b/extlib/Services/oEmbed/Object/Link.php @@ -0,0 +1,73 @@ + + * @copyright 2008 Digg.com, Inc. + * @license http://tinyurl.com/42zef New BSD License + * @version SVN: @version@ + * @link http://code.google.com/p/digg + * @link http://oembed.com + */ + +require_once 'Services/oEmbed/Object/Common.php'; + +/** + * Link object for {@link Services_oEmbed} + * + * @category Services + * @package Services_oEmbed + * @author Joe Stump + * @copyright 2008 Digg.com, Inc. + * @license http://tinyurl.com/42zef New BSD License + * @version Release: @version@ + * @link http://code.google.com/p/digg + * @link http://oembed.com + */ +class Services_oEmbed_Object_Link extends Services_oEmbed_Object_Common +{ + /** + * Output a sane link + * + * @return string An HTML link of the object + */ + public function __toString() + { + return '' . $this->title . ''; + } +} + +?> diff --git a/extlib/Services/oEmbed/Object/Photo.php b/extlib/Services/oEmbed/Object/Photo.php new file mode 100644 index 0000000000..5fbf4292fa --- /dev/null +++ b/extlib/Services/oEmbed/Object/Photo.php @@ -0,0 +1,89 @@ + + * @copyright 2008 Digg.com, Inc. + * @license http://tinyurl.com/42zef New BSD License + * @version SVN: @version@ + * @link http://code.google.com/p/digg + * @link http://oembed.com + */ + +require_once 'Services/oEmbed/Object/Common.php'; + +/** + * Photo object for {@link Services_oEmbed} + * + * @category Services + * @package Services_oEmbed + * @author Joe Stump + * @copyright 2008 Digg.com, Inc. + * @license http://tinyurl.com/42zef New BSD License + * @version Release: @version@ + * @link http://code.google.com/p/digg + * @link http://oembed.com + */ +class Services_oEmbed_Object_Photo extends Services_oEmbed_Object_Common +{ + /** + * Required fields for photo objects + * + * @var array $required Required fields + */ + protected $required = array( + 'url', 'width', 'height' + ); + + /** + * Output a valid HTML tag for image + * + * @return string HTML tag for Photo + */ + public function __toString() + { + $img = 'title)) { + $img .= ' alt="' . $this->title . '"'; + } + + return $img . ' />'; + } +} + +?> diff --git a/extlib/Services/oEmbed/Object/Rich.php b/extlib/Services/oEmbed/Object/Rich.php new file mode 100644 index 0000000000..dbf6933ac7 --- /dev/null +++ b/extlib/Services/oEmbed/Object/Rich.php @@ -0,0 +1,82 @@ + + * @copyright 2008 Digg.com, Inc. + * @license http://tinyurl.com/42zef New BSD License + * @version SVN: @version@ + * @link http://code.google.com/p/digg + * @link http://oembed.com + */ + +require_once 'Services/oEmbed/Object/Common.php'; + +/** + * Photo object for {@link Services_oEmbed} + * + * @category Services + * @package Services_oEmbed + * @author Joe Stump + * @copyright 2008 Digg.com, Inc. + * @license http://tinyurl.com/42zef New BSD License + * @version Release: @version@ + * @link http://code.google.com/p/digg + * @link http://oembed.com + */ +class Services_oEmbed_Object_Rich extends Services_oEmbed_Object_Common +{ + /** + * Required fields for rich objects + * + * @var array $required Required fields + */ + protected $required = array( + 'html', 'width', 'height' + ); + + /** + * Output a the HTML tag for rich object + * + * @return string HTML for rich object + */ + public function __toString() + { + return $this->html; + } +} + +?> diff --git a/extlib/Services/oEmbed/Object/Video.php b/extlib/Services/oEmbed/Object/Video.php new file mode 100644 index 0000000000..7461081151 --- /dev/null +++ b/extlib/Services/oEmbed/Object/Video.php @@ -0,0 +1,82 @@ + + * @copyright 2008 Digg.com, Inc. + * @license http://tinyurl.com/42zef New BSD License + * @version SVN: @version@ + * @link http://code.google.com/p/digg + * @link http://oembed.com + */ + +require_once 'Services/oEmbed/Object/Common.php'; + +/** + * Photo object for {@link Services_oEmbed} + * + * @category Services + * @package Services_oEmbed + * @author Joe Stump + * @copyright 2008 Digg.com, Inc. + * @license http://tinyurl.com/42zef New BSD License + * @version Release: @version@ + * @link http://code.google.com/p/digg + * @link http://oembed.com + */ +class Services_oEmbed_Object_Video extends Services_oEmbed_Object_Common +{ + /** + * Required fields for video objects + * + * @var array $required Required fields + */ + protected $required = array( + 'html', 'width', 'height' + ); + + /** + * Output a valid embed tag for video + * + * @return string HTML for video + */ + public function __toString() + { + return $this->html; + } +} + +?> diff --git a/extlib/Stomp.php b/extlib/Stomp.php new file mode 100644 index 0000000000..9e1c97b3b3 --- /dev/null +++ b/extlib/Stomp.php @@ -0,0 +1,594 @@ + + * @author Dejan Bosanac + * @author Michael Caplan + * @version $Revision: 43 $ + */ +class Stomp +{ + /** + * Perform request synchronously + * + * @var boolean + */ + public $sync = false; + + /** + * Default prefetch size + * + * @var int + */ + public $prefetchSize = 1; + + /** + * Client id used for durable subscriptions + * + * @var string + */ + public $clientId = null; + + protected $_brokerUri = null; + protected $_socket = null; + protected $_hosts = array(); + protected $_params = array(); + protected $_subscriptions = array(); + protected $_defaultPort = 61613; + protected $_currentHost = - 1; + protected $_attempts = 10; + protected $_username = ''; + protected $_password = ''; + protected $_sessionId; + protected $_read_timeout_seconds = 60; + protected $_read_timeout_milliseconds = 0; + + /** + * Constructor + * + * @param string $brokerUri Broker URL + * @throws Stomp_Exception + */ + public function __construct ($brokerUri) + { + $this->_brokerUri = $brokerUri; + $this->_init(); + } + /** + * Initialize connection + * + * @throws Stomp_Exception + */ + protected function _init () + { + $pattern = "|^(([a-zA-Z]+)://)+\(*([a-zA-Z0-9\.:/i,-]+)\)*\??([a-zA-Z0-9=]*)$|i"; + if (preg_match($pattern, $this->_brokerUri, $regs)) { + $scheme = $regs[2]; + $hosts = $regs[3]; + $params = $regs[4]; + if ($scheme != "failover") { + $this->_processUrl($this->_brokerUri); + } else { + $urls = explode(",", $hosts); + foreach ($urls as $url) { + $this->_processUrl($url); + } + } + if ($params != null) { + parse_str($params, $this->_params); + } + } else { + require_once 'Stomp/Exception.php'; + throw new Stomp_Exception("Bad Broker URL {$this->_brokerUri}"); + } + } + /** + * Process broker URL + * + * @param string $url Broker URL + * @throws Stomp_Exception + * @return boolean + */ + protected function _processUrl ($url) + { + $parsed = parse_url($url); + if ($parsed) { + array_push($this->_hosts, array($parsed['host'] , $parsed['port'] , $parsed['scheme'])); + } else { + require_once 'Stomp/Exception.php'; + throw new Stomp_Exception("Bad Broker URL $url"); + } + } + /** + * Make socket connection to the server + * + * @throws Stomp_Exception + */ + protected function _makeConnection () + { + if (count($this->_hosts) == 0) { + require_once 'Stomp/Exception.php'; + throw new Stomp_Exception("No broker defined"); + } + + // force disconnect, if previous established connection exists + $this->disconnect(); + + $i = $this->_currentHost; + $att = 0; + $connected = false; + while (! $connected && $att ++ < $this->_attempts) { + if (isset($this->_params['randomize']) && $this->_params['randomize'] == 'true') { + $i = rand(0, count($this->_hosts) - 1); + } else { + $i = ($i + 1) % count($this->_hosts); + } + $broker = $this->_hosts[$i]; + $host = $broker[0]; + $port = $broker[1]; + $scheme = $broker[2]; + if ($port == null) { + $port = $this->_defaultPort; + } + if ($this->_socket != null) { + fclose($this->_socket); + $this->_socket = null; + } + $this->_socket = @fsockopen($scheme . '://' . $host, $port); + if (!is_resource($this->_socket) && $att >= $this->_attempts && !array_key_exists($i + 1, $this->_hosts)) { + require_once 'Stomp/Exception.php'; + throw new Stomp_Exception("Could not connect to $host:$port ($att/{$this->_attempts})"); + } else if (is_resource($this->_socket)) { + $connected = true; + $this->_currentHost = $i; + break; + } + } + if (! $connected) { + require_once 'Stomp/Exception.php'; + throw new Stomp_Exception("Could not connect to a broker"); + } + } + /** + * Connect to server + * + * @param string $username + * @param string $password + * @return boolean + * @throws Stomp_Exception + */ + public function connect ($username = '', $password = '') + { + $this->_makeConnection(); + if ($username != '') { + $this->_username = $username; + } + if ($password != '') { + $this->_password = $password; + } + $headers = array('login' => $this->_username , 'passcode' => $this->_password); + if ($this->clientId != null) { + $headers["client-id"] = $this->clientId; + } + $frame = new Stomp_Frame("CONNECT", $headers); + $this->_writeFrame($frame); + $frame = $this->readFrame(); + if ($frame instanceof Stomp_Frame && $frame->command == 'CONNECTED') { + $this->_sessionId = $frame->headers["session"]; + return true; + } else { + require_once 'Stomp/Exception.php'; + if ($frame instanceof Stomp_Frame) { + throw new Stomp_Exception("Unexpected command: {$frame->command}", 0, $frame->body); + } else { + throw new Stomp_Exception("Connection not acknowledged"); + } + } + } + + /** + * Check if client session has ben established + * + * @return boolean + */ + public function isConnected () + { + return !empty($this->_sessionId) && is_resource($this->_socket); + } + /** + * Current stomp session ID + * + * @return string + */ + public function getSessionId() + { + return $this->_sessionId; + } + /** + * Send a message to a destination in the messaging system + * + * @param string $destination Destination queue + * @param string|Stomp_Frame $msg Message + * @param array $properties + * @param boolean $sync Perform request synchronously + * @return boolean + */ + public function send ($destination, $msg, $properties = null, $sync = null) + { + if ($msg instanceof Stomp_Frame) { + $msg->headers['destination'] = $destination; + $msg->headers = array_merge($msg->headers, $properties); + $frame = $msg; + } else { + $headers = $properties; + $headers['destination'] = $destination; + $frame = new Stomp_Frame('SEND', $headers, $msg); + } + $this->_prepareReceipt($frame, $sync); + $this->_writeFrame($frame); + return $this->_waitForReceipt($frame, $sync); + } + /** + * Prepair frame receipt + * + * @param Stomp_Frame $frame + * @param boolean $sync + */ + protected function _prepareReceipt (Stomp_Frame $frame, $sync) + { + $receive = $this->sync; + if ($sync !== null) { + $receive = $sync; + } + if ($receive == true) { + $frame->headers['receipt'] = md5(microtime()); + } + } + /** + * Wait for receipt + * + * @param Stomp_Frame $frame + * @param boolean $sync + * @return boolean + * @throws Stomp_Exception + */ + protected function _waitForReceipt (Stomp_Frame $frame, $sync) + { + + $receive = $this->sync; + if ($sync !== null) { + $receive = $sync; + } + if ($receive == true) { + $id = (isset($frame->headers['receipt'])) ? $frame->headers['receipt'] : null; + if ($id == null) { + return true; + } + $frame = $this->readFrame(); + if ($frame instanceof Stomp_Frame && $frame->command == 'RECEIPT') { + if ($frame->headers['receipt-id'] == $id) { + return true; + } else { + require_once 'Stomp/Exception.php'; + throw new Stomp_Exception("Unexpected receipt id {$frame->headers['receipt-id']}", 0, $frame->body); + } + } else { + require_once 'Stomp/Exception.php'; + if ($frame instanceof Stomp_Frame) { + throw new Stomp_Exception("Unexpected command {$frame->command}", 0, $frame->body); + } else { + throw new Stomp_Exception("Receipt not received"); + } + } + } + return true; + } + /** + * Register to listen to a given destination + * + * @param string $destination Destination queue + * @param array $properties + * @param boolean $sync Perform request synchronously + * @return boolean + * @throws Stomp_Exception + */ + public function subscribe ($destination, $properties = null, $sync = null) + { + $headers = array('ack' => 'client'); + $headers['activemq.prefetchSize'] = $this->prefetchSize; + if ($this->clientId != null) { + $headers["activemq.subcriptionName"] = $this->clientId; + } + if (isset($properties)) { + foreach ($properties as $name => $value) { + $headers[$name] = $value; + } + } + $headers['destination'] = $destination; + $frame = new Stomp_Frame('SUBSCRIBE', $headers); + $this->_prepareReceipt($frame, $sync); + $this->_writeFrame($frame); + if ($this->_waitForReceipt($frame, $sync) == true) { + $this->_subscriptions[$destination] = $properties; + return true; + } else { + return false; + } + } + /** + * Remove an existing subscription + * + * @param string $destination + * @param array $properties + * @param boolean $sync Perform request synchronously + * @return boolean + * @throws Stomp_Exception + */ + public function unsubscribe ($destination, $properties = null, $sync = null) + { + $headers = array(); + if (isset($properties)) { + foreach ($properties as $name => $value) { + $headers[$name] = $value; + } + } + $headers['destination'] = $destination; + $frame = new Stomp_Frame('UNSUBSCRIBE', $headers); + $this->_prepareReceipt($frame, $sync); + $this->_writeFrame($frame); + if ($this->_waitForReceipt($frame, $sync) == true) { + unset($this->_subscriptions[$destination]); + return true; + } else { + return false; + } + } + /** + * Start a transaction + * + * @param string $transactionId + * @param boolean $sync Perform request synchronously + * @return boolean + * @throws Stomp_Exception + */ + public function begin ($transactionId = null, $sync = null) + { + $headers = array(); + if (isset($transactionId)) { + $headers['transaction'] = $transactionId; + } + $frame = new Stomp_Frame('BEGIN', $headers); + $this->_prepareReceipt($frame, $sync); + $this->_writeFrame($frame); + return $this->_waitForReceipt($frame, $sync); + } + /** + * Commit a transaction in progress + * + * @param string $transactionId + * @param boolean $sync Perform request synchronously + * @return boolean + * @throws Stomp_Exception + */ + public function commit ($transactionId = null, $sync = null) + { + $headers = array(); + if (isset($transactionId)) { + $headers['transaction'] = $transactionId; + } + $frame = new Stomp_Frame('COMMIT', $headers); + $this->_prepareReceipt($frame, $sync); + $this->_writeFrame($frame); + return $this->_waitForReceipt($frame, $sync); + } + /** + * Roll back a transaction in progress + * + * @param string $transactionId + * @param boolean $sync Perform request synchronously + */ + public function abort ($transactionId = null, $sync = null) + { + $headers = array(); + if (isset($transactionId)) { + $headers['transaction'] = $transactionId; + } + $frame = new Stomp_Frame('ABORT', $headers); + $this->_prepareReceipt($frame, $sync); + $this->_writeFrame($frame); + return $this->_waitForReceipt($frame, $sync); + } + /** + * Acknowledge consumption of a message from a subscription + * Note: This operation is always asynchronous + * + * @param string|Stomp_Frame $messageMessage ID + * @param string $transactionId + * @return boolean + * @throws Stomp_Exception + */ + public function ack ($message, $transactionId = null) + { + if ($message instanceof Stomp_Frame) { + $frame = new Stomp_Frame('ACK', $message->headers); + $this->_writeFrame($frame); + return true; + } else { + $headers = array(); + if (isset($transactionId)) { + $headers['transaction'] = $transactionId; + } + $headers['message-id'] = $message; + $frame = new Stomp_Frame('ACK', $headers); + $this->_writeFrame($frame); + return true; + } + } + /** + * Graceful disconnect from the server + * + */ + public function disconnect () + { + $header = array(); + + if ($this->clientId != null) { + $headers["client-id"] = $this->clientId; + } + + if (is_resource($this->_socket)) { + $this->_writeFrame(new Stomp_Frame('DISCONNECT', $headers)); + fclose($this->_socket); + } + $this->_socket = null; + $this->_sessionId = null; + $this->_currentHost = -1; + $this->_subscriptions = array(); + $this->_username = ''; + $this->_password = ''; + } + /** + * Write frame to server + * + * @param Stomp_Frame $stompFrame + */ + protected function _writeFrame (Stomp_Frame $stompFrame) + { + if (!is_resource($this->_socket)) { + require_once 'Stomp/Exception.php'; + throw new Stomp_Exception('Socket connection hasn\'t been established'); + } + + $data = $stompFrame->__toString(); + $r = fwrite($this->_socket, $data, strlen($data)); + if ($r === false || $r == 0) { + $this->_reconnect(); + $this->_writeFrame($stompFrame); + } + } + + /** + * Set timeout to wait for content to read + * + * @param int $seconds_to_wait Seconds to wait for a frame + * @param int $milliseconds Milliseconds to wait for a frame + */ + public function setReadTimeout($seconds, $milliseconds = 0) + { + $this->_read_timeout_seconds = $seconds; + $this->_read_timeout_milliseconds = $milliseconds; + } + + /** + * Read responce frame from server + * + * @return Stomp_Frame|Stomp_Message_Map|boolean False when no frame to read + */ + public function readFrame () + { + if (!$this->hasFrameToRead()) { + return false; + } + + $rb = 1024; + $data = ''; + do { + $read = fgets($this->_socket, $rb); + if ($read === false) { + $this->_reconnect(); + return $this->readFrame(); + } + $data .= $read; + $len = strlen($data); + } while (($len < 2 || ! ($data[$len - 2] == "\x00" && $data[$len - 1] == "\n"))); + + list ($header, $body) = explode("\n\n", $data, 2); + $header = explode("\n", $header); + $headers = array(); + $command = null; + foreach ($header as $v) { + if (isset($command)) { + list ($name, $value) = explode(':', $v, 2); + $headers[$name] = $value; + } else { + $command = $v; + } + } + $frame = new Stomp_Frame($command, $headers, trim($body)); + if (isset($frame->headers['amq-msg-type']) && $frame->headers['amq-msg-type'] == 'MapMessage') { + require_once 'Stomp/Message/Map.php'; + return new Stomp_Message_Map($frame); + } else { + return $frame; + } + } + + /** + * Check if there is a frame to read + * + * @return boolean + */ + public function hasFrameToRead() + { + $read = array($this->_socket); + $write = null; + $except = null; + + $has_frame_to_read = stream_select($read, $write, $except, $this->_read_timeout_seconds, $this->_read_timeout_milliseconds); + + if ($has_frame_to_read === false) { + throw new Stomp_Exception('Check failed to determin if the socket is readable'); + } else if ($has_frame_to_read > 0) { + return true; + } else { + return false; + } + } + + /** + * Reconnects and renews subscriptions (if there were any) + * Call this method when you detect connection problems + */ + protected function _reconnect () + { + $subscriptions = $this->_subscriptions; + + $this->connect($this->_username, $this->_password); + foreach ($subscriptions as $dest => $properties) { + $this->subscribe($dest, $properties); + } + } + /** + * Graceful object desruction + * + */ + public function __destruct() + { + $this->disconnect(); + } +} +?> diff --git a/extlib/Stomp/Exception.php b/extlib/Stomp/Exception.php new file mode 100644 index 0000000000..e6870bc15d --- /dev/null +++ b/extlib/Stomp/Exception.php @@ -0,0 +1,57 @@ + + * @version $Revision: 23 $ + */ +class Stomp_Exception extends Exception +{ + protected $_details; + + /** + * Constructor + * + * @param string $message Error message + * @param int $code Error code + * @param string $details Stomp server error details + */ + public function __construct($message = null, $code = 0, $details = '') + { + $this->_details = $details; + + parent::__construct($message, $code); + } + + /** + * Stomp server error details + * + * @return string + */ + public function getDetails() + { + return $this->_details; + } +} +?> \ No newline at end of file diff --git a/extlib/Stomp/Frame.php b/extlib/Stomp/Frame.php new file mode 100644 index 0000000000..dc59c1cb7f --- /dev/null +++ b/extlib/Stomp/Frame.php @@ -0,0 +1,80 @@ + + * @author Dejan Bosanac + * @author Michael Caplan + * @version $Revision: 36 $ + */ +class Stomp_Frame +{ + public $command; + public $headers = array(); + public $body; + + /** + * Constructor + * + * @param string $command + * @param array $headers + * @param string $body + */ + public function __construct ($command = null, $headers = null, $body = null) + { + $this->_init($command, $headers, $body); + } + + protected function _init ($command = null, $headers = null, $body = null) + { + $this->command = $command; + if ($headers != null) { + $this->headers = $headers; + } + $this->body = $body; + + if ($this->command == 'ERROR') { + require_once 'Stomp/Exception.php'; + throw new Stomp_Exception($this->headers['message'], 0, $this->body); + } + } + + /** + * Convert frame to transportable string + * + * @return string + */ + public function __toString() + { + $data = $this->command . "\n"; + + foreach ($this->headers as $name => $value) { + $data .= $name . ": " . $value . "\n"; + } + + $data .= "\n"; + $data .= $this->body; + return $data .= "\x00\n"; + } +} +?> \ No newline at end of file diff --git a/extlib/Stomp/Message.php b/extlib/Stomp/Message.php new file mode 100644 index 0000000000..6bcad3efd9 --- /dev/null +++ b/extlib/Stomp/Message.php @@ -0,0 +1,37 @@ + + * @version $Revision: 23 $ + */ +class Stomp_Message extends Stomp_Frame +{ + public function __construct ($body, $headers = null) + { + $this->_init("SEND", $headers, $body); + } +} +?> \ No newline at end of file diff --git a/extlib/Stomp/Message/Bytes.php b/extlib/Stomp/Message/Bytes.php new file mode 100644 index 0000000000..c75f23e43a --- /dev/null +++ b/extlib/Stomp/Message/Bytes.php @@ -0,0 +1,47 @@ + + * @version $Revision: 23 $ + */ +class Stomp_Message_Bytes extends Stomp_Message +{ + /** + * Constructor + * + * @param string $body + * @param array $headers + */ + function __construct ($body, $headers = null) + { + $this->_init("SEND", $headers, $body); + if ($this->headers == null) { + $this->headers = array(); + } + $this->headers['content-length'] = count($body); + } +} +?> \ No newline at end of file diff --git a/extlib/Stomp/Message/Map.php b/extlib/Stomp/Message/Map.php new file mode 100644 index 0000000000..288456a849 --- /dev/null +++ b/extlib/Stomp/Message/Map.php @@ -0,0 +1,55 @@ + + * @version $Revision: 23 $ + */ +class Stomp_Message_Map extends Stomp_Message +{ + public $map; + + /** + * Constructor + * + * @param Stomp_Frame|string $msg + * @param array $headers + */ + function __construct ($msg, $headers = null) + { + if ($msg instanceof Stomp_Frame) { + $this->_init($msg->command, $msg->headers, $msg->body); + $this->map = json_decode($msg->body); + } else { + $this->_init("SEND", $headers, $msg); + if ($this->headers == null) { + $this->headers = array(); + } + $this->headers['amq-msg-type'] = 'MapMessage'; + $this->body = json_encode($msg); + } + } +} +?> \ No newline at end of file diff --git a/extlib/Validate.php b/extlib/Validate.php index 4c05506b3d..3d8bc23f21 100644 --- a/extlib/Validate.php +++ b/extlib/Validate.php @@ -1,1051 +1,1116 @@ - | -// | Pierre-Alain Joye | -// | Amir Mohammad Saied | -// +----------------------------------------------------------------------+ -// -/** - * Validation class - * - * Package to validate various datas. It includes : - * - numbers (min/max, decimal or not) - * - email (syntax, domain check) - * - string (predifined type alpha upper and/or lowercase, numeric,...) - * - date (min, max, rfc822 compliant) - * - uri (RFC2396) - * - possibility valid multiple data with a single method call (::multiple) - * - * @category Validate - * @package Validate - * @author Tomas V.V.Cox - * @author Pierre-Alain Joye - * @author Amir Mohammad Saied - * @copyright 1997-2006 Pierre-Alain Joye,Tomas V.V.Cox,Amir Mohammad Saied - * @license http://www.opensource.org/licenses/bsd-license.php New BSD License - * @version CVS: $Id: Validate.php,v 1.123 2007/12/12 16:45:51 davidc Exp $ - * @link http://pear.php.net/package/Validate - */ - -/** - * Methods for common data validations - */ -define('VALIDATE_NUM', '0-9'); -define('VALIDATE_SPACE', '\s'); -define('VALIDATE_ALPHA_LOWER', 'a-z'); -define('VALIDATE_ALPHA_UPPER', 'A-Z'); -define('VALIDATE_ALPHA', VALIDATE_ALPHA_LOWER . VALIDATE_ALPHA_UPPER); -define('VALIDATE_EALPHA_LOWER', VALIDATE_ALPHA_LOWER . 'áéíóúýàèìòùäëïöüÿâêîôûãñõ¨åæç½ðøþß'); -define('VALIDATE_EALPHA_UPPER', VALIDATE_ALPHA_UPPER . 'ÁÉÍÓÚÝÀÈÌÒÙÄËÏÖܾÂÊÎÔÛÃÑÕ¦ÅÆǼÐØÞ'); -define('VALIDATE_EALPHA', VALIDATE_EALPHA_LOWER . VALIDATE_EALPHA_UPPER); -define('VALIDATE_PUNCTUATION', VALIDATE_SPACE . '\.,;\:&"\'\?\!\(\)'); -define('VALIDATE_NAME', VALIDATE_EALPHA . VALIDATE_SPACE . "'" . "-"); -define('VALIDATE_STREET', VALIDATE_NUM . VALIDATE_NAME . "/\\ºª\."); - -define('VALIDATE_ITLD_EMAILS', 1); -define('VALIDATE_GTLD_EMAILS', 2); -define('VALIDATE_CCTLD_EMAILS', 4); -define('VALIDATE_ALL_EMAILS', 8); - -/** - * Validation class - * - * Package to validate various datas. It includes : - * - numbers (min/max, decimal or not) - * - email (syntax, domain check) - * - string (predifined type alpha upper and/or lowercase, numeric,...) - * - date (min, max) - * - uri (RFC2396) - * - possibility valid multiple data with a single method call (::multiple) - * - * @category Validate - * @package Validate - * @author Tomas V.V.Cox - * @author Pierre-Alain Joye - * @author Amir Mohammad Saied - * @copyright 1997-2006 Pierre-Alain Joye,Tomas V.V.Cox,Amir Mohammad Saied - * @license http://www.opensource.org/licenses/bsd-license.php New BSD License - * @version Release: @package_version@ - * @link http://pear.php.net/package/Validate - */ -class Validate -{ - /** - * International Top-Level Domain - * - * This is an array of the known international - * top-level domain names. - * - * @access protected - * @var array $_iTld (International top-level domains) - */ - var $_itld = array( - 'arpa', - 'root', - ); - - /** - * Generic top-level domain - * - * This is an array of the official - * generic top-level domains. - * - * @access protected - * @var array $_gTld (Generic top-level domains) - */ - var $_gtld = array( - 'aero', - 'biz', - 'cat', - 'com', - 'coop', - 'edu', - 'gov', - 'info', - 'int', - 'jobs', - 'mil', - 'mobi', - 'museum', - 'name', - 'net', - 'org', - 'pro', - 'travel', - 'asia', - 'post', - 'tel', - 'geo', - ); - - /** - * Country code top-level domains - * - * This is an array of the official country - * codes top-level domains - * - * @access protected - * @var array $_ccTld (Country Code Top-Level Domain) - */ - var $_cctld = array( - 'ac', - 'ad','ae','af','ag', - 'ai','al','am','an', - 'ao','aq','ar','as', - 'at','au','aw','ax', - 'az','ba','bb','bd', - 'be','bf','bg','bh', - 'bi','bj','bm','bn', - 'bo','br','bs','bt', - 'bu','bv','bw','by', - 'bz','ca','cc','cd', - 'cf','cg','ch','ci', - 'ck','cl','cm','cn', - 'co','cr','cs','cu', - 'cv','cx','cy','cz', - 'de','dj','dk','dm', - 'do','dz','ec','ee', - 'eg','eh','er','es', - 'et','eu','fi','fj', - 'fk','fm','fo','fr', - 'ga','gb','gd','ge', - 'gf','gg','gh','gi', - 'gl','gm','gn','gp', - 'gq','gr','gs','gt', - 'gu','gw','gy','hk', - 'hm','hn','hr','ht', - 'hu','id','ie','il', - 'im','in','io','iq', - 'ir','is','it','je', - 'jm','jo','jp','ke', - 'kg','kh','ki','km', - 'kn','kp','kr','kw', - 'ky','kz','la','lb', - 'lc','li','lk','lr', - 'ls','lt','lu','lv', - 'ly','ma','mc','md', - 'me','mg','mh','mk', - 'ml','mm','mn','mo', - 'mp','mq','mr','ms', - 'mt','mu','mv','mw', - 'mx','my','mz','na', - 'nc','ne','nf','ng', - 'ni','nl','no','np', - 'nr','nu','nz','om', - 'pa','pe','pf','pg', - 'ph','pk','pl','pm', - 'pn','pr','ps','pt', - 'pw','py','qa','re', - 'ro','rs','ru','rw', - 'sa','sb','sc','sd', - 'se','sg','sh','si', - 'sj','sk','sl','sm', - 'sn','so','sr','st', - 'su','sv','sy','sz', - 'tc','td','tf','tg', - 'th','tj','tk','tl', - 'tm','tn','to','tp', - 'tr','tt','tv','tw', - 'tz','ua','ug','uk', - 'us','uy','uz','va', - 'vc','ve','vg','vi', - 'vn','vu','wf','ws', - 'ye','yt','yu','za', - 'zm','zw', - ); - - - /** - * Validate a number - * - * @param string $number Number to validate - * @param array $options array where: - * 'decimal' is the decimal char or false when decimal not allowed - * i.e. ',.' to allow both ',' and '.' - * 'dec_prec' Number of allowed decimals - * 'min' minimum value - * 'max' maximum value - * - * @return boolean true if valid number, false if not - * - * @access public - */ - function number($number, $options = array()) - { - $decimal = $dec_prec = $min = $max = null; - if (is_array($options)) { - extract($options); - } - - $dec_prec = $dec_prec ? "{1,$dec_prec}" : '+'; - $dec_regex = $decimal ? "[$decimal][0-9]$dec_prec" : ''; - - if (!preg_match("|^[-+]?\s*[0-9]+($dec_regex)?\$|", $number)) { - return false; - } - - if ($decimal != '.') { - $number = strtr($number, $decimal, '.'); - } - - $number = (float)str_replace(' ', '', $number); - if ($min !== null && $min > $number) { - return false; - } - - if ($max !== null && $max < $number) { - return false; - } - return true; - } - - /** - * Converting a string to UTF-7 (RFC 2152) - * - * @param $string string to be converted - * - * @return string converted string - * - * @access private - */ - function __stringToUtf7($string) { - $return = ''; - $utf7 = array( - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', - 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', - 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', - 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', - 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', - '3', '4', '5', '6', '7', '8', '9', '+', ',' - ); - - $state = 0; - if (!empty($string)) { - $i = 0; - while ($i <= strlen($string)) { - $char = substr($string, $i, 1); - if ($state == 0) { - if ((ord($char) >= 0x7F) || (ord($char) <= 0x1F)) { - if ($char) { - $return .= '&'; - } - $state = 1; - } elseif ($char == '&') { - $return .= '&-'; - } else { - $return .= $char; - } - } elseif (($i == strlen($string) || - !((ord($char) >= 0x7F)) || (ord($char) <= 0x1F))) { - if ($state != 1) { - if (ord($char) > 64) { - $return .= ''; - } else { - $return .= $utf7[ord($char)]; - } - } - $return .= '-'; - $state = 0; - } else { - switch($state) { - case 1: - $return .= $utf7[ord($char) >> 2]; - $residue = (ord($char) & 0x03) << 4; - $state = 2; - break; - case 2: - $return .= $utf7[$residue | (ord($char) >> 4)]; - $residue = (ord($char) & 0x0F) << 2; - $state = 3; - break; - case 3: - $return .= $utf7[$residue | (ord($char) >> 6)]; - $return .= $utf7[ord($char) & 0x3F]; - $state = 1; - break; - } - } - $i++; - } - return $return; - } - return ''; - } - - /** - * Validate an email according to full RFC822 (inclusive human readable part) - * - * @param string $email email to validate, - * will return the address for optional dns validation - * @param array $options email() options - * - * @return boolean true if valid email, false if not - * - * @access private - */ - function __emailRFC822(&$email, &$options) - { - if (Validate::__stringToUtf7($email) != $email) { - return false; - } - static $address = null; - static $uncomment = null; - if (!$address) { - // atom = 1* - $atom = '[^][()<>@,;:\\".\s\000-\037\177-\377]+\s*'; - // qtext = , ; => may be folded - // "\" & CR, and including linear-white-space> - $qtext = '[^"\\\\\r]'; - // quoted-pair = "\" CHAR ; may quote any char - $quoted_pair = '\\\\.'; - // quoted-string = <"> *(qtext/quoted-pair) <">; Regular qtext or - // ; quoted chars. - $quoted_string = '"(?:' . $qtext . '|' . $quoted_pair . ')*"\s*'; - // word = atom / quoted-string - $word = '(?:' . $atom . '|' . $quoted_string . ')'; - // local-part = word *("." word) ; uninterpreted - // ; case-preserved - $local_part = $word . '(?:\.\s*' . $word . ')*'; - // dtext = may be folded - // "]", "\" & CR, & including linear-white-space> - $dtext = '[^][\\\\\r]'; - // domain-literal = "[" *(dtext / quoted-pair) "]" - $domain_literal = '\[(?:' . $dtext . '|' . $quoted_pair . ')*\]\s*'; - // sub-domain = domain-ref / domain-literal - // domain-ref = atom ; symbolic reference - $sub_domain = '(?:' . $atom . '|' . $domain_literal . ')'; - // domain = sub-domain *("." sub-domain) - $domain = $sub_domain . '(?:\.\s*' . $sub_domain . ')*'; - // addr-spec = local-part "@" domain ; global address - $addr_spec = $local_part . '@\s*' . $domain; - // route = 1#("@" domain) ":" ; path-relative - $route = '@' . $domain . '(?:,@\s*' . $domain . ')*:\s*'; - // route-addr = "<" [route] addr-spec ">" - $route_addr = '<\s*(?:' . $route . ')?' . $addr_spec . '>\s*'; - // phrase = 1*word ; Sequence of words - $phrase = $word . '+'; - // mailbox = addr-spec ; simple address - // / phrase route-addr ; name & addr-spec - $mailbox = '(?:' . $addr_spec . '|' . $phrase . $route_addr . ')'; - // group = phrase ":" [#mailbox] ";" - $group = $phrase . ':\s*(?:' . $mailbox . '(?:,\s*' . $mailbox . ')*)?;\s*'; - // address = mailbox ; one addressee - // / group ; named list - $address = '/^\s*(?:' . $mailbox . '|' . $group . ')$/'; - $uncomment = - '/((?:(?:\\\\"|[^("])*(?:' . $quoted_string . - ')?)*)((?{$tmpVar}; - } - - $e = $self->executeFullEmailValidation($email, $toValidate); - - return $e; - } - // {{{ protected function executeFullEmailValidation - /** - * Execute the validation - * - * This function will execute the full email vs tld - * validation using an array of tlds passed to it. - * - * @access public - * @param string $email The email to validate. - * @param array $arrayOfTLDs The array of the TLDs to validate - * @return true or false (Depending on if it validates or if it does not) - */ - function executeFullEmailValidation($email, $arrayOfTLDs) - { - $emailEnding = explode('.', $email); - $emailEnding = $emailEnding[count($emailEnding)-1]; - - foreach ($arrayOfTLDs as $validator => $keys) { - if (in_array($emailEnding, $keys)) { - return true; - } - } - return false; - } - // }}} - - /** - * Validate an email - * - * @param string $email email to validate - * @param mixed boolean (BC) $check_domain Check or not if the domain exists - * array $options associative array of options - * 'check_domain' boolean Check or not if the domain exists - * 'use_rfc822' boolean Apply the full RFC822 grammar - * - * @return boolean true if valid email, false if not - * - * @access public - */ - function email($email, $options = null) - { - $check_domain = false; - $use_rfc822 = false; - if (is_bool($options)) { - $check_domain = $options; - } elseif (is_array($options)) { - extract($options); - } - - /** - * @todo Fix bug here.. even if it passes this, it won't be passing - * The regular expression below - */ - if (isset($fullTLDValidation)) { - $valid = Validate::_fullTLDValidation($email, $fullTLDValidation); - - if (!$valid) { - return false; - } - } - - // the base regexp for address - $regex = '&^(?: # recipient: - ("\s*(?:[^"\f\n\r\t\v\b\s]+\s*)+")| #1 quoted name - ([-\w!\#\$%\&\'*+~/^`|{}]+(?:\.[-\w!\#\$%\&\'*+~/^`|{}]+)*)) #2 OR dot-atom - @(((\[)? #3 domain, 4 as IPv4, 5 optionally bracketed - (?:(?:(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:[0-1]?[0-9]?[0-9]))\.){3} - (?:(?:25[0-5])|(?:2[0-4][0-9])|(?:[0-1]?[0-9]?[0-9]))))(?(5)\])| - ((?:[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\.)*[a-z0-9](?:[-a-z0-9]*[a-z0-9])?) #6 domain as hostname - \.((?:([^- ])[-a-z]*[-a-z]))) #7 TLD - $&xi'; - - if ($use_rfc822? Validate::__emailRFC822($email, $options) : - preg_match($regex, $email)) { - if ($check_domain && function_exists('checkdnsrr')) { - list (, $domain) = explode('@', $email); - if (checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A')) { - return true; - } - return false; - } - return true; - } - return false; - } - - /** - * Validate a string using the given format 'format' - * - * @param string $string String to validate - * @param array $options Options array where: - * 'format' is the format of the string - * Ex: VALIDATE_NUM . VALIDATE_ALPHA (see constants) - * 'min_length' minimum length - * 'max_length' maximum length - * - * @return boolean true if valid string, false if not - * - * @access public - */ - function string($string, $options) - { - $format = null; - $min_length = $max_length = 0; - if (is_array($options)) { - extract($options); - } - if ($format && !preg_match("|^[$format]*\$|s", $string)) { - return false; - } - if ($min_length && strlen($string) < $min_length) { - return false; - } - if ($max_length && strlen($string) > $max_length) { - return false; - } - return true; - } - - /** - * Validate an URI (RFC2396) - * This function will validate 'foobarstring' by default, to get it to validate - * only http, https, ftp and such you have to pass it in the allowed_schemes - * option, like this: - * - * $options = array('allowed_schemes' => array('http', 'https', 'ftp')) - * var_dump(Validate::uri('http://www.example.org', $options)); - * - * - * NOTE 1: The rfc2396 normally allows middle '-' in the top domain - * e.g. http://example.co-m should be valid - * However, as '-' is not used in any known TLD, it is invalid - * NOTE 2: As double shlashes // are allowed in the path part, only full URIs - * including an authority can be valid, no relative URIs - * the // are mandatory (optionally preceeded by the 'sheme:' ) - * NOTE 3: the full complience to rfc2396 is not achieved by default - * the characters ';/?:@$,' will not be accepted in the query part - * if not urlencoded, refer to the option "strict'" - * - * @param string $url URI to validate - * @param array $options Options used by the validation method. - * key => type - * 'domain_check' => boolean - * Whether to check the DNS entry or not - * 'allowed_schemes' => array, list of protocols - * List of allowed schemes ('http', - * 'ssh+svn', 'mms') - * 'strict' => string the refused chars - * in query and fragment parts - * default: ';/?:@$,' - * empty: accept all rfc2396 foreseen chars - * - * @return boolean true if valid uri, false if not - * - * @access public - */ - function uri($url, $options = null) - { - $strict = ';/?:@$,'; - $domain_check = false; - $allowed_schemes = null; - if (is_array($options)) { - extract($options); - } - if (preg_match( - '&^(?:([a-z][-+.a-z0-9]*):)? # 1. scheme - (?:// # authority start - (?:((?:%[0-9a-f]{2}|[-a-z0-9_.!~*\'();:\&=+$,])*)@)? # 2. authority-userinfo - (?:((?:[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\.)*[a-z](?:[a-z0-9]+)?\.?) # 3. authority-hostname OR - |([0-9]{1,3}(?:\.[0-9]{1,3}){3})) # 4. authority-ipv4 - (?::([0-9]*))?) # 5. authority-port - ((?:/(?:%[0-9a-f]{2}|[-a-z0-9_.!~*\'():@\&=+$,;])*)*/?)? # 6. path - (?:\?([^#]*))? # 7. query - (?:\#((?:%[0-9a-f]{2}|[-a-z0-9_.!~*\'();/?:@\&=+$,])*))? # 8. fragment - $&xi', $url, $matches)) { - $scheme = isset($matches[1]) ? $matches[1] : ''; - $authority = isset($matches[3]) ? $matches[3] : '' ; - if (is_array($allowed_schemes) && - !in_array($scheme,$allowed_schemes) - ) { - return false; - } - if (!empty($matches[4])) { - $parts = explode('.', $matches[4]); - foreach ($parts as $part) { - if ($part > 255) { - return false; - } - } - } elseif ($domain_check && function_exists('checkdnsrr')) { - if (!checkdnsrr($authority, 'A')) { - return false; - } - } - if ($strict) { - $strict = '#[' . preg_quote($strict, '#') . ']#'; - if ((!empty($matches[7]) && preg_match($strict, $matches[7])) - || (!empty($matches[8]) && preg_match($strict, $matches[8]))) { - return false; - } - } - return true; - } - return false; - } - - /** - * Validate date and times. Note that this method need the Date_Calc class - * - * @param string $date Date to validate - * @param array $options array options where : - * 'format' The format of the date (%d-%m-%Y) - * or rfc822_compliant - * 'min' The date has to be greater - * than this array($day, $month, $year) - * or PEAR::Date object - * 'max' The date has to be smaller than - * this array($day, $month, $year) - * or PEAR::Date object - * - * @return boolean true if valid date/time, false if not - * - * @access public - */ - function date($date, $options) - { - $max = $min = false; - $format = ''; - if (is_array($options)) { - extract($options); - } - - if (strtolower($format) == 'rfc822_compliant') { - $preg = '&^(?:(Mon|Tue|Wed|Thu|Fri|Sat|Sun),) \s+ - (?:(\d{2})?) \s+ - (?:(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)?) \s+ - (?:(\d{2}(\d{2})?)?) \s+ - (?:(\d{2}?)):(?:(\d{2}?))(:(?:(\d{2}?)))? \s+ - (?:[+-]\d{4}|UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|[A-IK-Za-ik-z])$&xi'; - - if (!preg_match($preg, $date, $matches)) { - return false; - } - - $year = (int)$matches[4]; - $months = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', - 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'); - $month = array_keys($months, $matches[3]); - $month = (int)$month[0]+1; - $day = (int)$matches[2]; - $weekday= $matches[1]; - $hour = (int)$matches[6]; - $minute = (int)$matches[7]; - isset($matches[9]) ? $second = (int)$matches[9] : $second = 0; - - if ((strlen($year) != 4) || - ($day > 31 || $day < 1)|| - ($hour > 23) || - ($minute > 59) || - ($second > 59)) { - return false; - } - } else { - $date_len = strlen($format); - for ($i = 0; $i < $date_len; $i++) { - $c = $format{$i}; - if ($c == '%') { - $next = $format{$i + 1}; - switch ($next) { - case 'j': - case 'd': - if ($next == 'j') { - $day = (int)Validate::_substr($date, 1, 2); - } else { - $day = (int)Validate::_substr($date, 0, 2); - } - if ($day < 1 || $day > 31) { - return false; - } - break; - case 'm': - case 'n': - if ($next == 'm') { - $month = (int)Validate::_substr($date, 0, 2); - } else { - $month = (int)Validate::_substr($date, 1, 2); - } - if ($month < 1 || $month > 12) { - return false; - } - break; - case 'Y': - case 'y': - if ($next == 'Y') { - $year = Validate::_substr($date, 4); - $year = (int)$year?$year:''; - } else { - $year = (int)(substr(date('Y'), 0, 2) . - Validate::_substr($date, 2)); - } - if (strlen($year) != 4 || $year < 0 || $year > 9999) { - return false; - } - break; - case 'g': - case 'h': - if ($next == 'g') { - $hour = Validate::_substr($date, 1, 2); - } else { - $hour = Validate::_substr($date, 2); - } - if (!preg_match('/^\d+$/', $hour) || $hour < 0 || $hour > 12) { - return false; - } - break; - case 'G': - case 'H': - if ($next == 'G') { - $hour = Validate::_substr($date, 1, 2); - } else { - $hour = Validate::_substr($date, 2); - } - if (!preg_match('/^\d+$/', $hour) || $hour < 0 || $hour > 24) { - return false; - } - break; - case 's': - case 'i': - $t = Validate::_substr($date, 2); - if (!preg_match('/^\d+$/', $t) || $t < 0 || $t > 59) { - return false; - } - break; - default: - trigger_error("Not supported char `$next' after % in offset " . ($i+2), E_USER_WARNING); - } - $i++; - } else { - //literal - if (Validate::_substr($date, 1) != $c) { - return false; - } - } - } - } - // there is remaing data, we don't want it - if (strlen($date) && (strtolower($format) != 'rfc822_compliant')) { - return false; - } - - if (isset($day) && isset($month) && isset($year)) { - if (!checkdate($month, $day, $year)) { - return false; - } - - if (strtolower($format) == 'rfc822_compliant') { - if ($weekday != date("D", mktime(0, 0, 0, $month, $day, $year))) { - return false; - } - } - - if ($min) { - include_once 'Date/Calc.php'; - if (is_a($min, 'Date') && - (Date_Calc::compareDates($day, $month, $year, - $min->getDay(), $min->getMonth(), $min->getYear()) < 0)) - { - return false; - } elseif (is_array($min) && - (Date_Calc::compareDates($day, $month, $year, - $min[0], $min[1], $min[2]) < 0)) - { - return false; - } - } - - if ($max) { - include_once 'Date/Calc.php'; - if (is_a($max, 'Date') && - (Date_Calc::compareDates($day, $month, $year, - $max->getDay(), $max->getMonth(), $max->getYear()) > 0)) - { - return false; - } elseif (is_array($max) && - (Date_Calc::compareDates($day, $month, $year, - $max[0], $max[1], $max[2]) > 0)) - { - return false; - } - } - } - - return true; - } - - function _substr(&$date, $num, $opt = false) - { - if ($opt && strlen($date) >= $opt && preg_match('/^[0-9]{'.$opt.'}/', $date, $m)) { - $ret = $m[0]; - } else { - $ret = substr($date, 0, $num); - } - $date = substr($date, strlen($ret)); - return $ret; - } - - function _modf($val, $div) { - if (function_exists('bcmod')) { - return bcmod($val, $div); - } elseif (function_exists('fmod')) { - return fmod($val, $div); - } - $r = $val / $div; - $i = intval($r); - return intval($val - $i * $div + .1); - } - - /** - * Calculates sum of product of number digits with weights - * - * @param string $number number string - * @param array $weights reference to array of weights - * - * @returns int returns product of number digits with weights - * - * @access protected - */ - function _multWeights($number, &$weights) { - if (!is_array($weights)) { - return -1; - } - $sum = 0; - - $count = min(count($weights), strlen($number)); - if ($count == 0) { // empty string or weights array - return -1; - } - for ($i = 0; $i < $count; ++$i) { - $sum += intval(substr($number, $i, 1)) * $weights[$i]; - } - - return $sum; - } - - /** - * Calculates control digit for a given number - * - * @param string $number number string - * @param array $weights reference to array of weights - * @param int $modulo (optionsl) number - * @param int $subtract (optional) number - * @param bool $allow_high (optional) true if function can return number higher than 10 - * - * @returns int -1 calculated control number is returned - * - * @access protected - */ - function _getControlNumber($number, &$weights, $modulo = 10, $subtract = 0, $allow_high = false) { - // calc sum - $sum = Validate::_multWeights($number, $weights); - if ($sum == -1) { - return -1; - } - $mod = Validate::_modf($sum, $modulo); // calculate control digit - - if ($subtract > $mod && $mod > 0) { - $mod = $subtract - $mod; - } - if ($allow_high === false) { - $mod %= 10; // change 10 to zero - } - return $mod; - } - - /** - * Validates a number - * - * @param string $number number to validate - * @param array $weights reference to array of weights - * @param int $modulo (optionsl) number - * @param int $subtract (optional) numbier - * - * @returns bool true if valid, false if not - * - * @access protected - */ - function _checkControlNumber($number, &$weights, $modulo = 10, $subtract = 0) { - if (strlen($number) < count($weights)) { - return false; - } - $target_digit = substr($number, count($weights), 1); - $control_digit = Validate::_getControlNumber($number, $weights, $modulo, $subtract, $modulo > 10); - - if ($control_digit == -1) { - return false; - } - if ($target_digit === 'X' && $control_digit == 10) { - return true; - } - if ($control_digit != $target_digit) { - return false; - } - return true; - } - - /** - * Bulk data validation for data introduced in the form of an - * assoc array in the form $var_name => $value. - * Can be used on any of Validate subpackages - * - * @param array $data Ex: array('name' => 'toto', 'email' => 'toto@thing.info'); - * @param array $val_type Contains the validation type and all parameters used in. - * 'val_type' is not optional - * others validations properties must have the same name as the function - * parameters. - * Ex: array('toto'=>array('type'=>'string','format'='toto@thing.info','min_length'=>5)); - * @param boolean $remove if set, the elements not listed in data will be removed - * - * @return array value name => true|false the value name comes from the data key - * - * @access public - */ - function multiple(&$data, &$val_type, $remove = false) - { - $keys = array_keys($data); - $valid = array(); - foreach ($keys as $var_name) { - if (!isset($val_type[$var_name])) { - if ($remove) { - unset($data[$var_name]); - } - continue; - } - $opt = $val_type[$var_name]; - $methods = get_class_methods('Validate'); - $val2check = $data[$var_name]; - // core validation method - if (in_array(strtolower($opt['type']), $methods)) { - //$opt[$opt['type']] = $data[$var_name]; - $method = $opt['type']; - unset($opt['type']); - - if (sizeof($opt) == 1 && is_array(reset($opt))) { - $opt = array_pop($opt); - } - $valid[$var_name] = call_user_func(array('Validate', $method), $val2check, $opt); - - /** - * external validation method in the form: - * "" - * Ex: us_ssn will include class Validate/US.php and call method ssn() - */ - } elseif (strpos($opt['type'], '_') !== false) { - $validateType = explode('_', $opt['type']); - $method = array_pop($validateType); - $class = implode('_', $validateType); - $classPath = str_replace('_', DIRECTORY_SEPARATOR, $class); - $class = 'Validate_' . $class; - if (!@include_once "Validate/$classPath.php") { - trigger_error("$class isn't installed or you may have some permissoin issues", E_USER_ERROR); - } - - $ce = substr(phpversion(), 0, 1) > 4 ? class_exists($class, false) : class_exists($class); - if (!$ce || - !in_array($method, get_class_methods($class))) - { - trigger_error("Invalid validation type $class::$method", E_USER_WARNING); - continue; - } - unset($opt['type']); - if (sizeof($opt) == 1) { - $opt = array_pop($opt); - } - $valid[$var_name] = call_user_func(array($class, $method), $data[$var_name], $opt); - } else { - trigger_error("Invalid validation type {$opt['type']}", E_USER_WARNING); - } - } - return $valid; - } -} - + + * Pierre-Alain Joye + * Amir Mohammad Saied + * + * + * Package to validate various datas. It includes : + * - numbers (min/max, decimal or not) + * - email (syntax, domain check) + * - string (predifined type alpha upper and/or lowercase, numeric,...) + * - date (min, max, rfc822 compliant) + * - uri (RFC2396) + * - possibility valid multiple data with a single method call (::multiple) + * + * @category Validate + * @package Validate + * @author Tomas V.V.Cox + * @author Pierre-Alain Joye + * @author Amir Mohammad Saied + * @copyright 1997-2006 Pierre-Alain Joye,Tomas V.V.Cox,Amir Mohammad Saied + * @license http://www.opensource.org/licenses/bsd-license.php New BSD License + * @version CVS: $Id: Validate.php,v 1.134 2009/01/28 12:27:33 davidc Exp $ + * @link http://pear.php.net/package/Validate + */ + +/** + * Methods for common data validations + */ +define('VALIDATE_NUM', '0-9'); +define('VALIDATE_SPACE', '\s'); +define('VALIDATE_ALPHA_LOWER', 'a-z'); +define('VALIDATE_ALPHA_UPPER', 'A-Z'); +define('VALIDATE_ALPHA', VALIDATE_ALPHA_LOWER . VALIDATE_ALPHA_UPPER); +define('VALIDATE_EALPHA_LOWER', VALIDATE_ALPHA_LOWER . 'áéíóúýàèìòùäëïöüÿâêîôûãñõ¨åæç½ðøþß'); +define('VALIDATE_EALPHA_UPPER', VALIDATE_ALPHA_UPPER . 'ÁÉÍÓÚÝÀÈÌÒÙÄËÏÖܾÂÊÎÔÛÃÑÕ¦ÅÆǼÐØÞ'); +define('VALIDATE_EALPHA', VALIDATE_EALPHA_LOWER . VALIDATE_EALPHA_UPPER); +define('VALIDATE_PUNCTUATION', VALIDATE_SPACE . '\.,;\:&"\'\?\!\(\)'); +define('VALIDATE_NAME', VALIDATE_EALPHA . VALIDATE_SPACE . "'" . "-"); +define('VALIDATE_STREET', VALIDATE_NUM . VALIDATE_NAME . "/\\ºª\."); + +define('VALIDATE_ITLD_EMAILS', 1); +define('VALIDATE_GTLD_EMAILS', 2); +define('VALIDATE_CCTLD_EMAILS', 4); +define('VALIDATE_ALL_EMAILS', 8); + +/** + * Validation class + * + * Package to validate various datas. It includes : + * - numbers (min/max, decimal or not) + * - email (syntax, domain check) + * - string (predifined type alpha upper and/or lowercase, numeric,...) + * - date (min, max) + * - uri (RFC2396) + * - possibility valid multiple data with a single method call (::multiple) + * + * @category Validate + * @package Validate + * @author Tomas V.V.Cox + * @author Pierre-Alain Joye + * @author Amir Mohammad Saied + * @copyright 1997-2006 Pierre-Alain Joye,Tomas V.V.Cox,Amir Mohammad Saied + * @license http://www.opensource.org/licenses/bsd-license.php New BSD License + * @version Release: @package_version@ + * @link http://pear.php.net/package/Validate + */ +class Validate +{ + /** + * International Top-Level Domain + * + * This is an array of the known international + * top-level domain names. + * + * @access protected + * @var array $_iTld (International top-level domains) + */ + var $_itld = array( + 'arpa', + 'root', + ); + + /** + * Generic top-level domain + * + * This is an array of the official + * generic top-level domains. + * + * @access protected + * @var array $_gTld (Generic top-level domains) + */ + var $_gtld = array( + 'aero', + 'biz', + 'cat', + 'com', + 'coop', + 'edu', + 'gov', + 'info', + 'int', + 'jobs', + 'mil', + 'mobi', + 'museum', + 'name', + 'net', + 'org', + 'pro', + 'travel', + 'asia', + 'post', + 'tel', + 'geo', + ); + + /** + * Country code top-level domains + * + * This is an array of the official country + * codes top-level domains + * + * @access protected + * @var array $_ccTld (Country Code Top-Level Domain) + */ + var $_cctld = array( + 'ac', + 'ad','ae','af','ag', + 'ai','al','am','an', + 'ao','aq','ar','as', + 'at','au','aw','ax', + 'az','ba','bb','bd', + 'be','bf','bg','bh', + 'bi','bj','bm','bn', + 'bo','br','bs','bt', + 'bu','bv','bw','by', + 'bz','ca','cc','cd', + 'cf','cg','ch','ci', + 'ck','cl','cm','cn', + 'co','cr','cs','cu', + 'cv','cx','cy','cz', + 'de','dj','dk','dm', + 'do','dz','ec','ee', + 'eg','eh','er','es', + 'et','eu','fi','fj', + 'fk','fm','fo','fr', + 'ga','gb','gd','ge', + 'gf','gg','gh','gi', + 'gl','gm','gn','gp', + 'gq','gr','gs','gt', + 'gu','gw','gy','hk', + 'hm','hn','hr','ht', + 'hu','id','ie','il', + 'im','in','io','iq', + 'ir','is','it','je', + 'jm','jo','jp','ke', + 'kg','kh','ki','km', + 'kn','kp','kr','kw', + 'ky','kz','la','lb', + 'lc','li','lk','lr', + 'ls','lt','lu','lv', + 'ly','ma','mc','md', + 'me','mg','mh','mk', + 'ml','mm','mn','mo', + 'mp','mq','mr','ms', + 'mt','mu','mv','mw', + 'mx','my','mz','na', + 'nc','ne','nf','ng', + 'ni','nl','no','np', + 'nr','nu','nz','om', + 'pa','pe','pf','pg', + 'ph','pk','pl','pm', + 'pn','pr','ps','pt', + 'pw','py','qa','re', + 'ro','rs','ru','rw', + 'sa','sb','sc','sd', + 'se','sg','sh','si', + 'sj','sk','sl','sm', + 'sn','so','sr','st', + 'su','sv','sy','sz', + 'tc','td','tf','tg', + 'th','tj','tk','tl', + 'tm','tn','to','tp', + 'tr','tt','tv','tw', + 'tz','ua','ug','uk', + 'us','uy','uz','va', + 'vc','ve','vg','vi', + 'vn','vu','wf','ws', + 'ye','yt','yu','za', + 'zm','zw', + ); + + /** + * Validate a tag URI (RFC4151) + * + * @param string $uri tag URI to validate + * + * @return boolean true if valid tag URI, false if not + * + * @access private + */ + function __uriRFC4151($uri) + { + $datevalid = false; + if (preg_match( + '/^tag:(?.*),(?\d{4}-?\d{0,2}-?\d{0,2}):(?.*)(.*:)*$/', $uri, $matches)) { + $date = $matches['date']; + $date6 = strtotime($date); + if ((strlen($date) == 4) && $date <= date('Y')) { + $datevalid = true; + } elseif ((strlen($date) == 7) && ($date6 < strtotime("now"))) { + $datevalid = true; + } elseif ((strlen($date) == 10) && ($date6 < strtotime("now"))) { + $datevalid = true; + } + if (self::email($matches['name'])) { + $namevalid = true; + } else { + $namevalid = self::email('info@' . $matches['name']); + } + return $datevalid && $namevalid; + } else { + return false; + } + } + + /** + * Validate a number + * + * @param string $number Number to validate + * @param array $options array where: + * 'decimal' is the decimal char or false when decimal + * not allowed. + * i.e. ',.' to allow both ',' and '.' + * 'dec_prec' Number of allowed decimals + * 'min' minimum value + * 'max' maximum value + * + * @return boolean true if valid number, false if not + * + * @access public + */ + function number($number, $options = array()) + { + $decimal = $dec_prec = $min = $max = null; + if (is_array($options)) { + extract($options); + } + + $dec_prec = $dec_prec ? "{1,$dec_prec}" : '+'; + $dec_regex = $decimal ? "[$decimal][0-9]$dec_prec" : ''; + + if (!preg_match("|^[-+]?\s*[0-9]+($dec_regex)?\$|", $number)) { + return false; + } + + if ($decimal != '.') { + $number = strtr($number, $decimal, '.'); + } + + $number = (float)str_replace(' ', '', $number); + if ($min !== null && $min > $number) { + return false; + } + + if ($max !== null && $max < $number) { + return false; + } + return true; + } + + /** + * Converting a string to UTF-7 (RFC 2152) + * + * @param string $string string to be converted + * + * @return string converted string + * + * @access private + */ + function __stringToUtf7($string) + { + $return = ''; + $utf7 = array( + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', + 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', + 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', + 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', + 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', + '3', '4', '5', '6', '7', '8', '9', '+', ',' + ); + + + $state = 0; + + if (!empty($string)) { + $i = 0; + while ($i <= strlen($string)) { + $char = substr($string, $i, 1); + if ($state == 0) { + if ((ord($char) >= 0x7F) || (ord($char) <= 0x1F)) { + if ($char) { + $return .= '&'; + } + $state = 1; + } elseif ($char == '&') { + $return .= '&-'; + } else { + $return .= $char; + } + } elseif (($i == strlen($string) || + !((ord($char) >= 0x7F)) || (ord($char) <= 0x1F))) { + if ($state != 1) { + if (ord($char) > 64) { + $return .= ''; + } else { + $return .= $utf7[ord($char)]; + } + } + $return .= '-'; + $state = 0; + } else { + switch($state) { + case 1: + $return .= $utf7[ord($char) >> 2]; + $residue = (ord($char) & 0x03) << 4; + $state = 2; + break; + case 2: + $return .= $utf7[$residue | (ord($char) >> 4)]; + $residue = (ord($char) & 0x0F) << 2; + $state = 3; + break; + case 3: + $return .= $utf7[$residue | (ord($char) >> 6)]; + $return .= $utf7[ord($char) & 0x3F]; + $state = 1; + break; + } + } + $i++; + } + return $return; + } + return ''; + } + + /** + * Validate an email according to full RFC822 (inclusive human readable part) + * + * @param string $email email to validate, + * will return the address for optional dns validation + * @param array $options email() options + * + * @return boolean true if valid email, false if not + * + * @access private + */ + function __emailRFC822(&$email, &$options) + { + static $address = null; + static $uncomment = null; + if (!$address) { + // atom = 1* + $atom = '[^][()<>@,;:\\".\s\000-\037\177-\377]+\s*'; + // qtext = , ; => may be folded + // "\" & CR, and including linear-white-space> + $qtext = '[^"\\\\\r]'; + // quoted-pair = "\" CHAR ; may quote any char + $quoted_pair = '\\\\.'; + // quoted-string = <"> *(qtext/quoted-pair) <">; Regular qtext or + // ; quoted chars. + $quoted_string = '"(?:' . $qtext . '|' . $quoted_pair . ')*"\s*'; + // word = atom / quoted-string + $word = '(?:' . $atom . '|' . $quoted_string . ')'; + // local-part = word *("." word) ; uninterpreted + // ; case-preserved + $local_part = $word . '(?:\.\s*' . $word . ')*'; + // dtext = may be folded + // "]", "\" & CR, & including linear-white-space> + $dtext = '[^][\\\\\r]'; + // domain-literal = "[" *(dtext / quoted-pair) "]" + $domain_literal = '\[(?:' . $dtext . '|' . $quoted_pair . ')*\]\s*'; + // sub-domain = domain-ref / domain-literal + // domain-ref = atom ; symbolic reference + $sub_domain = '(?:' . $atom . '|' . $domain_literal . ')'; + // domain = sub-domain *("." sub-domain) + $domain = $sub_domain . '(?:\.\s*' . $sub_domain . ')*'; + // addr-spec = local-part "@" domain ; global address + $addr_spec = $local_part . '@\s*' . $domain; + // route = 1#("@" domain) ":" ; path-relative + $route = '@' . $domain . '(?:,@\s*' . $domain . ')*:\s*'; + // route-addr = "<" [route] addr-spec ">" + $route_addr = '<\s*(?:' . $route . ')?' . $addr_spec . '>\s*'; + // phrase = 1*word ; Sequence of words + $phrase = $word . '+'; + // mailbox = addr-spec ; simple address + // / phrase route-addr ; name & addr-spec + $mailbox = '(?:' . $addr_spec . '|' . $phrase . $route_addr . ')'; + // group = phrase ":" [#mailbox] ";" + $group = $phrase . ':\s*(?:' . $mailbox . '(?:,\s*' . $mailbox . ')*)?;\s*'; + // address = mailbox ; one addressee + // / group ; named list + $address = '/^\s*(?:' . $mailbox . '|' . $group . ')$/'; + + $uncomment = + '/((?:(?:\\\\"|[^("])*(?:' . $quoted_string . + ')?)*)((?{$tmpVar}; + } + + $e = $self->executeFullEmailValidation($email, $toValidate); + + return $e; + } + + /** + * Execute the validation + * + * This function will execute the full email vs tld + * validation using an array of tlds passed to it. + * + * @param string $email The email to validate. + * @param array $arrayOfTLDs The array of the TLDs to validate + * + * @access public + * + * @return true or false (Depending on if it validates or if it does not) + */ + function executeFullEmailValidation($email, $arrayOfTLDs) + { + $emailEnding = explode('.', $email); + $emailEnding = $emailEnding[count($emailEnding)-1]; + foreach ($arrayOfTLDs as $validator => $keys) { + if (in_array($emailEnding, $keys)) { + return true; + } + } + return false; + } + + /** + * Validate an email + * + * @param string $email email to validate + * @param mixed boolean (BC) $check_domain Check or not if the domain exists + * array $options associative array of options + * 'check_domain' boolean Check or not if the domain exists + * 'use_rfc822' boolean Apply the full RFC822 grammar + * + * Ex. + * $options = array( + * 'check_domain' => 'true', + * 'fullTLDValidation' => 'true', + * 'use_rfc822' => 'true', + * 'VALIDATE_GTLD_EMAILS' => 'true', + * 'VALIDATE_CCTLD_EMAILS' => 'true', + * 'VALIDATE_ITLD_EMAILS' => 'true', + * ); + * + * @return boolean true if valid email, false if not + * + * @access public + */ + function email($email, $options = null) + { + $check_domain = false; + $use_rfc822 = false; + if (is_bool($options)) { + $check_domain = $options; + } elseif (is_array($options)) { + extract($options); + } + + /** + * Check for IDN usage so we can encode the domain as Punycode + * before continuing. + */ + $hasIDNA = false; + + if (@include_once('Net/IDNA.php')) { + $hasIDNA = true; + } + + if ($hasIDNA === true) { + if (strpos($email, '@') !== false) { + list($name, $domain) = explode('@', $email, 2); + + // Check if the domain contains characters > 127 which means + // it's an idn domain name. + $chars = count_chars($domain, 1); + if (!empty($chars) && max(array_keys($chars)) > 127) { + $idna =& Net_IDNA::singleton(); + $domain = $idna->encode($domain); + } + + $email = "$name@$domain"; + } + } + + /** + * @todo Fix bug here.. even if it passes this, it won't be passing + * The regular expression below + */ + if (isset($fullTLDValidation)) { + //$valid = Validate::_fullTLDValidation($email, $fullTLDValidation); + $valid = Validate::_fullTLDValidation($email, $options); + + if (!$valid) { + return false; + } + } + + // the base regexp for address + $regex = '&^(?: # recipient: + ("\s*(?:[^"\f\n\r\t\v\b\s]+\s*)+")| #1 quoted name + ([-\w!\#\$%\&\'*+~/^`|{}]+(?:\.[-\w!\#\$%\&\'*+~/^`|{}]+)*)) #2 OR dot-atom + @(((\[)? #3 domain, 4 as IPv4, 5 optionally bracketed + (?:(?:(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:[0-1]?[0-9]?[0-9]))\.){3} + (?:(?:25[0-5])|(?:2[0-4][0-9])|(?:[0-1]?[0-9]?[0-9]))))(?(5)\])| + ((?:[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\.)*[a-z0-9](?:[-a-z0-9]*[a-z0-9])?) #6 domain as hostname + \.((?:([^- ])[-a-z]*[-a-z]))) #7 TLD + $&xi'; + + //checks if exists the domain (MX or A) + if ($use_rfc822? Validate::__emailRFC822($email, $options) : + preg_match($regex, $email)) { + if ($check_domain && function_exists('checkdnsrr')) { + list ($account, $domain) = explode('@', $email); + if (checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A')) { + return true; + } + return false; + } + return true; + } + return false; + } + + /** + * Validate a string using the given format 'format' + * + * @param string $string String to validate + * @param array $options Options array where: + * 'format' is the format of the string + * Ex:VALIDATE_NUM . VALIDATE_ALPHA (see constants) + * 'min_length' minimum length + * 'max_length' maximum length + * + * @return boolean true if valid string, false if not + * + * @access public + */ + function string($string, $options) + { + $format = null; + $min_length = 0; + $max_length = 0; + + if (is_array($options)) { + extract($options); + } + + if ($format && !preg_match("|^[$format]*\$|s", $string)) { + return false; + } + + if ($min_length && strlen($string) < $min_length) { + return false; + } + + if ($max_length && strlen($string) > $max_length) { + return false; + } + + return true; + } + + /** + * Validate an URI (RFC2396) + * This function will validate 'foobarstring' by default, to get it to validate + * only http, https, ftp and such you have to pass it in the allowed_schemes + * option, like this: + * + * $options = array('allowed_schemes' => array('http', 'https', 'ftp')) + * var_dump(Validate::uri('http://www.example.org', $options)); + * + * + * NOTE 1: The rfc2396 normally allows middle '-' in the top domain + * e.g. http://example.co-m should be valid + * However, as '-' is not used in any known TLD, it is invalid + * NOTE 2: As double shlashes // are allowed in the path part, only full URIs + * including an authority can be valid, no relative URIs + * the // are mandatory (optionally preceeded by the 'sheme:' ) + * NOTE 3: the full complience to rfc2396 is not achieved by default + * the characters ';/?:@$,' will not be accepted in the query part + * if not urlencoded, refer to the option "strict'" + * + * @param string $url URI to validate + * @param array $options Options used by the validation method. + * key => type + * 'domain_check' => boolean + * Whether to check the DNS entry or not + * 'allowed_schemes' => array, list of protocols + * List of allowed schemes ('http', + * 'ssh+svn', 'mms') + * 'strict' => string the refused chars + * in query and fragment parts + * default: ';/?:@$,' + * empty: accept all rfc2396 foreseen chars + * + * @return boolean true if valid uri, false if not + * + * @access public + */ + function uri($url, $options = null) + { + $strict = ';/?:@$,'; + $domain_check = false; + $allowed_schemes = null; + if (is_array($options)) { + extract($options); + } + if (is_array($allowed_schemes) && + in_array("tag", $allowed_schemes) + ) { + if (strpos($url, "tag:") === 0) { + return self::__uriRFC4151($url); + } + } + + if (preg_match( + '&^(?:([a-z][-+.a-z0-9]*):)? # 1. scheme + (?:// # authority start + (?:((?:%[0-9a-f]{2}|[-a-z0-9_.!~*\'();:\&=+$,])*)@)? # 2. authority-userinfo + (?:((?:[a-z0-9](?:[-a-z0-9]*[a-z0-9])?\.)*[a-z](?:[a-z0-9]+)?\.?) # 3. authority-hostname OR + |([0-9]{1,3}(?:\.[0-9]{1,3}){3})) # 4. authority-ipv4 + (?::([0-9]*))?) # 5. authority-port + ((?:/(?:%[0-9a-f]{2}|[-a-z0-9_.!~*\'():@\&=+$,;])*)*/?)? # 6. path + (?:\?([^#]*))? # 7. query + (?:\#((?:%[0-9a-f]{2}|[-a-z0-9_.!~*\'();/?:@\&=+$,])*))? # 8. fragment + $&xi', $url, $matches)) { + $scheme = isset($matches[1]) ? $matches[1] : ''; + $authority = isset($matches[3]) ? $matches[3] : '' ; + if (is_array($allowed_schemes) && + !in_array($scheme, $allowed_schemes) + ) { + return false; + } + if (!empty($matches[4])) { + $parts = explode('.', $matches[4]); + foreach ($parts as $part) { + if ($part > 255) { + return false; + } + } + } elseif ($domain_check && function_exists('checkdnsrr')) { + if (!checkdnsrr($authority, 'A')) { + return false; + } + } + if ($strict) { + $strict = '#[' . preg_quote($strict, '#') . ']#'; + if ((!empty($matches[7]) && preg_match($strict, $matches[7])) + || (!empty($matches[8]) && preg_match($strict, $matches[8]))) { + return false; + } + } + return true; + } + return false; + } + + /** + * Validate date and times. Note that this method need the Date_Calc class + * + * @param string $date Date to validate + * @param array $options array options where : + * 'format' The format of the date (%d-%m-%Y) + * or rfc822_compliant + * 'min' The date has to be greater + * than this array($day, $month, $year) + * or PEAR::Date object + * 'max' The date has to be smaller than + * this array($day, $month, $year) + * or PEAR::Date object + * + * @return boolean true if valid date/time, false if not + * + * @access public + */ + function date($date, $options) + { + $max = false; + $min = false; + $format = ''; + + if (is_array($options)) { + extract($options); + } + + if (strtolower($format) == 'rfc822_compliant') { + $preg = '&^(?:(Mon|Tue|Wed|Thu|Fri|Sat|Sun),) \s+ + (?:(\d{2})?) \s+ + (?:(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)?) \s+ + (?:(\d{2}(\d{2})?)?) \s+ + (?:(\d{2}?)):(?:(\d{2}?))(:(?:(\d{2}?)))? \s+ + (?:[+-]\d{4}|UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|[A-IK-Za-ik-z])$&xi'; + + if (!preg_match($preg, $date, $matches)) { + return false; + } + + $year = (int)$matches[4]; + $months = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', + 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'); + $month = array_keys($months, $matches[3]); + $month = (int)$month[0]+1; + $day = (int)$matches[2]; + $weekday = $matches[1]; + $hour = (int)$matches[6]; + $minute = (int)$matches[7]; + isset($matches[9]) ? $second = (int)$matches[9] : $second = 0; + + if ((strlen($year) != 4) || + ($day > 31 || $day < 1)|| + ($hour > 23) || + ($minute > 59) || + ($second > 59)) { + return false; + } + } else { + $date_len = strlen($format); + for ($i = 0; $i < $date_len; $i++) { + $c = $format{$i}; + if ($c == '%') { + $next = $format{$i + 1}; + switch ($next) { + case 'j': + case 'd': + if ($next == 'j') { + $day = (int)Validate::_substr($date, 1, 2); + } else { + $day = (int)Validate::_substr($date, 0, 2); + } + if ($day < 1 || $day > 31) { + return false; + } + break; + case 'm': + case 'n': + if ($next == 'm') { + $month = (int)Validate::_substr($date, 0, 2); + } else { + $month = (int)Validate::_substr($date, 1, 2); + } + if ($month < 1 || $month > 12) { + return false; + } + break; + case 'Y': + case 'y': + if ($next == 'Y') { + $year = Validate::_substr($date, 4); + $year = (int)$year?$year:''; + } else { + $year = (int)(substr(date('Y'), 0, 2) . + Validate::_substr($date, 2)); + } + if (strlen($year) != 4 || $year < 0 || $year > 9999) { + return false; + } + break; + case 'g': + case 'h': + if ($next == 'g') { + $hour = Validate::_substr($date, 1, 2); + } else { + $hour = Validate::_substr($date, 2); + } + if (!preg_match('/^\d+$/', $hour) || $hour < 0 || $hour > 12) { + return false; + } + break; + case 'G': + case 'H': + if ($next == 'G') { + $hour = Validate::_substr($date, 1, 2); + } else { + $hour = Validate::_substr($date, 2); + } + if (!preg_match('/^\d+$/', $hour) || $hour < 0 || $hour > 24) { + return false; + } + break; + case 's': + case 'i': + $t = Validate::_substr($date, 2); + if (!preg_match('/^\d+$/', $t) || $t < 0 || $t > 59) { + return false; + } + break; + default: + trigger_error("Not supported char `$next' after % in offset " . ($i+2), E_USER_WARNING); + } + $i++; + } else { + //literal + if (Validate::_substr($date, 1) != $c) { + return false; + } + } + } + } + // there is remaing data, we don't want it + if (strlen($date) && (strtolower($format) != 'rfc822_compliant')) { + return false; + } + + if (isset($day) && isset($month) && isset($year)) { + if (!checkdate($month, $day, $year)) { + return false; + } + + if (strtolower($format) == 'rfc822_compliant') { + if ($weekday != date("D", mktime(0, 0, 0, $month, $day, $year))) { + return false; + } + } + + if ($min) { + include_once 'Date/Calc.php'; + if (is_a($min, 'Date') && + (Date_Calc::compareDates($day, $month, $year, + $min->getDay(), $min->getMonth(), $min->getYear()) < 0) + ) { + return false; + } elseif (is_array($min) && + (Date_Calc::compareDates($day, $month, $year, + $min[0], $min[1], $min[2]) < 0) + ) { + return false; + } + } + + if ($max) { + include_once 'Date/Calc.php'; + if (is_a($max, 'Date') && + (Date_Calc::compareDates($day, $month, $year, + $max->getDay(), $max->getMonth(), $max->getYear()) > 0) + ) { + return false; + } elseif (is_array($max) && + (Date_Calc::compareDates($day, $month, $year, + $max[0], $max[1], $max[2]) > 0) + ) { + return false; + } + } + } + + return true; + } + + /** + * Substr + * + * @param string &$date Date + * @param string $num Length + * @param string $opt Unknown + * + * @access private + * @return string + */ + function _substr(&$date, $num, $opt = false) + { + if ($opt && strlen($date) >= $opt && preg_match('/^[0-9]{'.$opt.'}/', $date, $m)) { + $ret = $m[0]; + } else { + $ret = substr($date, 0, $num); + } + $date = substr($date, strlen($ret)); + return $ret; + } + + function _modf($val, $div) + { + if (function_exists('bcmod')) { + return bcmod($val, $div); + } elseif (function_exists('fmod')) { + return fmod($val, $div); + } + $r = $val / $div; + $i = intval($r); + return intval($val - $i * $div + .1); + } + + /** + * Calculates sum of product of number digits with weights + * + * @param string $number number string + * @param array $weights reference to array of weights + * + * @access protected + * + * @return int returns product of number digits with weights + */ + function _multWeights($number, &$weights) + { + if (!is_array($weights)) { + return -1; + } + $sum = 0; + + $count = min(count($weights), strlen($number)); + if ($count == 0) { // empty string or weights array + return -1; + } + for ($i = 0; $i < $count; ++$i) { + $sum += intval(substr($number, $i, 1)) * $weights[$i]; + } + + return $sum; + } + + /** + * Calculates control digit for a given number + * + * @param string $number number string + * @param array $weights reference to array of weights + * @param int $modulo (optionsl) number + * @param int $subtract (optional) number + * @param bool $allow_high (optional) true if function can return number higher than 10 + * + * @access protected + * + * @return int -1 calculated control number is returned + */ + function _getControlNumber($number, &$weights, $modulo = 10, $subtract = 0, $allow_high = false) + { + // calc sum + $sum = Validate::_multWeights($number, $weights); + if ($sum == -1) { + return -1; + } + $mod = Validate::_modf($sum, $modulo); // calculate control digit + + if ($subtract > $mod && $mod > 0) { + $mod = $subtract - $mod; + } + if ($allow_high === false) { + $mod %= 10; // change 10 to zero + } + return $mod; + } + + /** + * Validates a number + * + * @param string $number number to validate + * @param array $weights reference to array of weights + * @param int $modulo (optional) number + * @param int $subtract (optional) number + * + * @access protected + * + * @return bool true if valid, false if not + */ + function _checkControlNumber($number, &$weights, $modulo = 10, $subtract = 0) + { + if (strlen($number) < count($weights)) { + return false; + } + $target_digit = substr($number, count($weights), 1); + $control_digit = Validate::_getControlNumber($number, $weights, $modulo, $subtract, $modulo > 10); + + if ($control_digit == -1) { + return false; + } + if ($target_digit === 'X' && $control_digit == 10) { + return true; + } + if ($control_digit != $target_digit) { + return false; + } + return true; + } + + /** + * Bulk data validation for data introduced in the form of an + * assoc array in the form $var_name => $value. + * Can be used on any of Validate subpackages + * + * @param array $data Ex: array('name' => 'toto', 'email' => 'toto@thing.info'); + * @param array $val_type Contains the validation type and all parameters used in. + * 'val_type' is not optional + * others validations properties must have the same name as the function + * parameters. + * Ex: array('toto'=>array('type'=>'string','format'='toto@thing.info','min_length'=>5)); + * @param boolean $remove if set, the elements not listed in data will be removed + * + * @return array value name => true|false the value name comes from the data key + * + * @access public + */ + function multiple(&$data, &$val_type, $remove = false) + { + $keys = array_keys($data); + $valid = array(); + + foreach ($keys as $var_name) { + if (!isset($val_type[$var_name])) { + if ($remove) { + unset($data[$var_name]); + } + continue; + } + $opt = $val_type[$var_name]; + $methods = get_class_methods('Validate'); + $val2check = $data[$var_name]; + // core validation method + if (in_array(strtolower($opt['type']), $methods)) { + //$opt[$opt['type']] = $data[$var_name]; + $method = $opt['type']; + unset($opt['type']); + + if (sizeof($opt) == 1 && is_array(reset($opt))) { + $opt = array_pop($opt); + } + $valid[$var_name] = call_user_func(array('Validate', $method), $val2check, $opt); + + /** + * external validation method in the form: + * "" + * Ex: us_ssn will include class Validate/US.php and call method ssn() + */ + } elseif (strpos($opt['type'], '_') !== false) { + $validateType = explode('_', $opt['type']); + $method = array_pop($validateType); + $class = implode('_', $validateType); + $classPath = str_replace('_', DIRECTORY_SEPARATOR, $class); + $class = 'Validate_' . $class; + if (!@include_once "Validate/$classPath.php") { + trigger_error("$class isn't installed or you may have some permissoin issues", E_USER_ERROR); + } + + $ce = substr(phpversion(), 0, 1) > 4 ? + class_exists($class, false) : class_exists($class); + if (!$ce || + !in_array($method, get_class_methods($class)) + ) { + trigger_error("Invalid validation type $class::$method", + E_USER_WARNING); + continue; + } + unset($opt['type']); + if (sizeof($opt) == 1) { + $opt = array_pop($opt); + } + $valid[$var_name] = call_user_func(array($class, $method), + $data[$var_name], $opt); + } else { + trigger_error("Invalid validation type {$opt['type']}", + E_USER_WARNING); + } + } + return $valid; + } +} + diff --git a/extlib/facebook/facebook.php b/extlib/facebook/facebook.php index 35de6be509..fee1dd086a 100644 --- a/extlib/facebook/facebook.php +++ b/extlib/facebook/facebook.php @@ -1,5 +1,5 @@ api_client->auth_expireSession()) { - if (!$this->in_fb_canvas() && isset($_COOKIE[$this->api_key . '_user'])) { - $cookies = array('user', 'session_key', 'expires', 'ss'); - foreach ($cookies as $name) { - setcookie($this->api_key . '_' . $name, false, time() - 3600); - unset($_COOKIE[$this->api_key . '_' . $name]); - } - setcookie($this->api_key, false, time() - 3600); - unset($_COOKIE[$this->api_key]); - } - - // now, clear the rest of the stored state - $this->user = 0; - $this->api_client->session_key = 0; + $this->clear_cookie_state(); return true; } else { return false; } } + /** Logs the user out of all temporary application sessions as well as their + * Facebook session. Note this will only work if the user has a valid current + * session with the application. + * + * @param string $next URL to redirect to upon logging out + * + */ + public function logout($next) { + $logout_url = $this->get_logout_url($next); + + // Clear any stored state + $this->clear_cookie_state(); + + $this->redirect($logout_url); + } + + /** + * Clears any persistent state stored about the user, including + * cookies and information related to the current session in the + * client. + * + */ + public function clear_cookie_state() { + if (!$this->in_fb_canvas() && isset($_COOKIE[$this->api_key . '_user'])) { + $cookies = array('user', 'session_key', 'expires', 'ss'); + foreach ($cookies as $name) { + setcookie($this->api_key . '_' . $name, false, time() - 3600); + unset($_COOKIE[$this->api_key . '_' . $name]); + } + setcookie($this->api_key, false, time() - 3600); + unset($_COOKIE[$this->api_key]); + } + + // now, clear the rest of the stored state + $this->user = 0; + $this->api_client->session_key = 0; + } + public function redirect($url) { if ($this->in_fb_canvas()) { echo ''; @@ -249,7 +275,8 @@ class Facebook { } public function in_frame() { - return isset($this->fb_params['in_canvas']) || isset($this->fb_params['in_iframe']); + return isset($this->fb_params['in_canvas']) + || isset($this->fb_params['in_iframe']); } public function in_fb_canvas() { return isset($this->fb_params['in_canvas']); @@ -296,14 +323,42 @@ class Facebook { } public function get_add_url($next=null) { - return self::get_facebook_url().'/add.php?api_key='.$this->api_key . - ($next ? '&next=' . urlencode($next) : ''); + $page = self::get_facebook_url().'/add.php'; + $params = array('api_key' => $this->api_key); + + if ($next) { + $params['next'] = $next; + } + + return $page . '?' . http_build_query($params); } public function get_login_url($next, $canvas) { - return self::get_facebook_url().'/login.php?v=1.0&api_key=' . $this->api_key . - ($next ? '&next=' . urlencode($next) : '') . - ($canvas ? '&canvas' : ''); + $page = self::get_facebook_url().'/login.php'; + $params = array('api_key' => $this->api_key, + 'v' => '1.0'); + + if ($next) { + $params['next'] = $next; + } + if ($canvas) { + $params['canvas'] = '1'; + } + + return $page . '?' . http_build_query($params); + } + + public function get_logout_url($next) { + $page = self::get_facebook_url().'/logout.php'; + $params = array('app_key' => $this->api_key, + 'session_key' => $this->api_client->session_key); + + if ($next) { + $params['connect_next'] = 1; + $params['next'] = $next; + } + + return $page . '?' . http_build_query($params); } public function set_user($user, $session_key, $expires=null, $session_secret=null) { @@ -410,7 +465,20 @@ class Facebook { return $fb_params; } - /* + /** + * Validates the account that a user was trying to set up an + * independent account through Facebook Connect. + * + * @param user The user attempting to set up an independent account. + * @param hash The hash passed to the reclamation URL used. + * @return bool True if the user is the one that selected the + * reclamation link. + */ + public function verify_account_reclamation($user, $hash) { + return $hash == md5($user . $this->secret); + } + + /** * Validates that a given set of parameters match their signature. * Parameters all match a given input prefix, such as "fb_sig". * @@ -422,6 +490,37 @@ class Facebook { return self::generate_sig($fb_params, $this->secret) == $expected_sig; } + /** + * Validate the given signed public session data structure with + * public key of the app that + * the session proof belongs to. + * + * @param $signed_data the session info that is passed by another app + * @param string $public_key Optional public key of the app. If this + * is not passed, function will make an API call to get it. + * return true if the session proof passed verification. + */ + public function verify_signed_public_session_data($signed_data, + $public_key = null) { + + // If public key is not already provided, we need to get it through API + if (!$public_key) { + $public_key = $this->api_client->auth_getAppPublicKey( + $signed_data['api_key']); + } + + // Create data to verify + $data_to_serialize = $signed_data; + unset($data_to_serialize['sig']); + $serialized_data = implode('_', $data_to_serialize); + + // Decode signature + $signature = base64_decode($signed_data['sig']); + $result = openssl_verify($serialized_data, $signature, $public_key, + OPENSSL_ALGO_SHA1); + return $result == 1; + } + /* * Generate a signature using the application secret key. * diff --git a/extlib/facebook/facebook_desktop.php b/extlib/facebook/facebook_desktop.php index 90cdf66bd0..e79a2ca343 100644 --- a/extlib/facebook/facebook_desktop.php +++ b/extlib/facebook/facebook_desktop.php @@ -1,5 +1,5 @@ batch_mode = FacebookRestClient::BATCH_MODE_DEFAULT; $this->last_call_id = 0; $this->call_as_apikey = ''; - $this->server_addr = Facebook::get_facebook_url('api') . '/restserver.php'; + $this->use_curl_if_available = true; + $this->server_addr = Facebook::get_facebook_url('api') . '/restserver.php'; if (!empty($GLOBALS['facebook_config']['debug'])) { $this->cur_id = 0; @@ -122,40 +127,62 @@ function toggleDisplay(id, type) { $this->user = $uid; } + /** + * Normally, if the cURL library/PHP extension is available, it is used for + * HTTP transactions. This allows that behavior to be overridden, falling + * back to a vanilla-PHP implementation even if cURL is installed. + * + * @param $use_curl_if_available bool whether or not to use cURL if available + */ + public function set_use_curl_if_available($use_curl_if_available) { + $this->use_curl_if_available = $use_curl_if_available; + } + /** * Start a batch operation. */ public function begin_batch() { - if($this->batch_queue !== null) { + if ($this->pending_batch()) { $code = FacebookAPIErrorCodes::API_EC_BATCH_ALREADY_STARTED; - throw new FacebookRestClientException($code, - FacebookAPIErrorCodes::$api_error_descriptions[$code]); + $description = FacebookAPIErrorCodes::$api_error_descriptions[$code]; + throw new FacebookRestClientException($description, $code); } $this->batch_queue = array(); + $this->pending_batch = true; } /* * End current batch operation */ public function end_batch() { - if($this->batch_queue === null) { + if (!$this->pending_batch()) { $code = FacebookAPIErrorCodes::API_EC_BATCH_NOT_STARTED; - throw new FacebookRestClientException($code, - FacebookAPIErrorCodes::$api_error_descriptions[$code]); + $description = FacebookAPIErrorCodes::$api_error_descriptions[$code]; + throw new FacebookRestClientException($description, $code); } - $this->execute_server_side_batch(); + $this->pending_batch = false; + $this->execute_server_side_batch(); $this->batch_queue = null; } + /** + * are we currently queueing up calls for a batch? + */ + public function pending_batch() { + return $this->pending_batch; + } + private function execute_server_side_batch() { $item_count = count($this->batch_queue); $method_feed = array(); foreach($this->batch_queue as $batch_item) { - $method_feed[] = $this->create_post_string($batch_item['m'], - $batch_item['p']); + $method = $batch_item['m']; + $params = $batch_item['p']; + $this->finalize_params($method, $params); + $method_feed[] = $this->create_post_string($method, $params); } $method_feed_json = json_encode($method_feed); @@ -202,6 +229,18 @@ function toggleDisplay(id, type) { $this->call_as_apikey = ''; } + + /* + * If a page is loaded via HTTPS, then all images and static + * resources need to be printed with HTTPS urls to avoid + * mixed content warnings. If your page loads with an HTTPS + * url, then call set_use_ssl_resources to retrieve the correct + * urls. + */ + public function set_use_ssl_resources($is_ssl = true) { + $this->use_ssl_resources = $is_ssl; + } + /** * Returns public information for an application (as shown in the application * directory) by either application ID, API key, or canvas page name. @@ -231,7 +270,7 @@ function toggleDisplay(id, type) { * @return string An authentication token. */ public function auth_createToken() { - return $this->call_method('facebook.auth.createToken', array()); + return $this->call_method('facebook.auth.createToken'); } /** @@ -246,8 +285,7 @@ function toggleDisplay(id, type) { * @return array An assoc array containing session_key, uid */ public function auth_getSession($auth_token, $generate_session_secret=false) { - //Check if we are in batch mode - if($this->batch_queue === null) { + if (!$this->pending_batch()) { $result = $this->call_method('facebook.auth.getSession', array('auth_token' => $auth_token, 'generate_session_secret' => $generate_session_secret)); @@ -271,7 +309,7 @@ function toggleDisplay(id, type) { * API_EC_PARAM_UNKNOWN */ public function auth_promoteSession() { - return $this->call_method('facebook.auth.promoteSession', array()); + return $this->call_method('facebook.auth.promoteSession'); } /** @@ -282,7 +320,20 @@ function toggleDisplay(id, type) { * @return bool true if session expiration was successful, false otherwise */ public function auth_expireSession() { - return $this->call_method('facebook.auth.expireSession', array()); + return $this->call_method('facebook.auth.expireSession'); + } + + /** + * Revokes the given extended permission that the user granted at some + * prior time (for instance, offline_access or email). If no user is + * provided, it will be revoked for the user of the current session. + * + * @param string $perm The permission to revoke + * @param int $uid The user for whom to revoke the permission. + */ + public function auth_revokeExtendedPermission($perm, $uid=null) { + return $this->call_method('facebook.auth.revokeExtendedPermission', + array('perm' => $perm, 'uid' => $uid)); } /** @@ -302,6 +353,30 @@ function toggleDisplay(id, type) { array('uid' => $uid)); } + /** + * Get public key that is needed to verify digital signature + * an app may pass to other apps. The public key is only used by + * other apps for verification purposes. + * @param string API key of an app + * @return string The public key for the app. + */ + public function auth_getAppPublicKey($target_app_key) { + return $this->call_method('facebook.auth.getAppPublicKey', + array('target_app_key' => $target_app_key)); + } + + /** + * Get a structure that can be passed to another app + * as proof of session. The other app can verify it using public + * key of this app. + * + * @return signed public session data structure. + */ + public function auth_getSignedPublicSessionData() { + return $this->call_method('facebook.auth.getSignedPublicSessionData', + array()); + } + /** * Returns the number of unconnected friends that exist in this application. * This number is determined based on the accounts registered through @@ -363,8 +438,9 @@ function toggleDisplay(id, type) { * * @param int $uid (Optional) User associated with events. A null * parameter will default to the session user. - * @param array $eids (Optional) Filter by these event ids. A null - * parameter will get all events for the user. + * @param array/string $eids (Optional) Filter by these event + * ids. A null parameter will get all events for + * the user. (A csv list will work but is deprecated) * @param int $start_time (Optional) Filter with this unix time as lower * bound. A null or zero parameter indicates no * lower bound. @@ -718,12 +794,15 @@ function toggleDisplay(id, type) { * @param string $body_general (Optional) Additional markup that extends * the body of a short story. * @param int $story_size (Optional) A story size (see above) + * @param string $user_message (Optional) A user message for a short + * story. * * @return bool true on success */ public function &feed_publishUserAction( $template_bundle_id, $template_data, $target_ids='', $body_general='', - $story_size=FacebookRestClient::STORY_SIZE_ONE_LINE) { + $story_size=FacebookRestClient::STORY_SIZE_ONE_LINE, + $user_message='') { if (is_array($template_data)) { $template_data = json_encode($template_data); @@ -739,7 +818,107 @@ function toggleDisplay(id, type) { 'template_data' => $template_data, 'target_ids' => $target_ids, 'body_general' => $body_general, - 'story_size' => $story_size)); + 'story_size' => $story_size, + 'user_message' => $user_message)); + } + + + /** + * Publish a post to the user's stream. + * + * @param $message the user's message + * @param $attachment the post's attachment (optional) + * @param $action links the post's action links (optional) + * @param $target_id the user on whose wall the post will be posted + * (optional) + * @param $uid the actor (defaults to session user) + * @return string the post id + */ + public function stream_publish( + $message, $attachment = null, $action_links = null, $target_id = null, + $uid = null) { + + return $this->call_method( + 'facebook.stream.publish', + array('message' => $message, + 'attachment' => $attachment, + 'action_links' => $action_links, + 'target_id' => $target_id, + 'uid' => $this->get_uid($uid))); + } + + /** + * Remove a post from the user's stream. + * Currently, you may only remove stories you application created. + * + * @param $post_id the post id + * @param $uid the actor (defaults to session user) + * @return bool + */ + public function stream_remove($post_id, $uid = null) { + return $this->call_method( + 'facebook.stream.remove', + array('post_id' => $post_id, + 'uid' => $this->get_uid($uid))); + } + + /** + * Add a comment to a stream post + * + * @param $post_id the post id + * @param $comment the comment text + * @param $uid the actor (defaults to session user) + * @return string the id of the created comment + */ + public function stream_addComment($post_id, $comment, $uid = null) { + return $this->call_method( + 'facebook.stream.addComment', + array('post_id' => $post_id, + 'comment' => $comment, + 'uid' => $this->get_uid($uid))); + } + + + /** + * Remove a comment from a stream post + * + * @param $comment_id the comment id + * @param $uid the actor (defaults to session user) + * @return bool + */ + public function stream_removeComment($comment_id, $uid = null) { + return $this->call_method( + 'facebook.stream.removeComment', + array('comment_id' => $comment_id, + 'uid' => $this->get_uid($uid))); + } + + /** + * Add a like to a stream post + * + * @param $post_id the post id + * @param $uid the actor (defaults to session user) + * @return bool + */ + public function stream_addLike($post_id, $uid = null) { + return $this->call_method( + 'facebook.stream.addLike', + array('post_id' => $post_id, + 'uid' => $this->get_uid($uid))); + } + + /** + * Remove a like from a stream post + * + * @param $post_id the post id + * @param $uid the actor (defaults to session user) + * @return bool + */ + public function stream_removeLike($post_id, $uid = null) { + return $this->call_method( + 'facebook.stream.removeLike', + array('post_id' => $post_id, + 'uid' => $this->get_uid($uid))); } /** @@ -750,7 +929,7 @@ function toggleDisplay(id, type) { * @return array An array of feed story objects. */ public function &feed_getAppFriendStories() { - return $this->call_method('facebook.feed.getAppFriendStories', array()); + return $this->call_method('facebook.feed.getAppFriendStories'); } /** @@ -771,33 +950,42 @@ function toggleDisplay(id, type) { * Returns whether or not pairs of users are friends. * Note that the Facebook friend relationship is symmetric. * - * @param array $uids1 array of ids (id_1, id_2,...) of some length X - * @param array $uids2 array of ids (id_A, id_B,...) of SAME length X + * @param array/string $uids1 list of ids (id_1, id_2,...) + * of some length X (csv is deprecated) + * @param array/string $uids2 list of ids (id_A, id_B,...) + * of SAME length X (csv is deprecated) * * @return array An array with uid1, uid2, and bool if friends, e.g.: * array(0 => array('uid1' => id_1, 'uid2' => id_A, 'are_friends' => 1), * 1 => array('uid1' => id_2, 'uid2' => id_B, 'are_friends' => 0) * ...) + * @error + * API_EC_PARAM_USER_ID_LIST */ public function &friends_areFriends($uids1, $uids2) { return $this->call_method('facebook.friends.areFriends', - array('uids1' => $uids1, 'uids2' => $uids2)); + array('uids1' => $uids1, + 'uids2' => $uids2)); } /** * Returns the friends of the current session user. * * @param int $flid (Optional) Only return friends on this friend list. + * @param int $uid (Optional) Return friends for this user. * * @return array An array of friends */ - public function &friends_get($flid=null) { + public function &friends_get($flid=null, $uid = null) { if (isset($this->friends_list)) { return $this->friends_list; } $params = array(); - if (isset($this->canvas_user)) { - $params['uid'] = $this->canvas_user; + if (!$uid && isset($this->canvas_user)) { + $uid = $this->canvas_user; + } + if ($uid) { + $params['uid'] = $uid; } if ($flid) { $params['flid'] = $flid; @@ -812,7 +1000,7 @@ function toggleDisplay(id, type) { * @return array An array of friend list objects */ public function &friends_getLists() { - return $this->call_method('facebook.friends.getLists', array()); + return $this->call_method('facebook.friends.getLists'); } /** @@ -822,7 +1010,7 @@ function toggleDisplay(id, type) { * @return array An array of friends also using the app */ public function &friends_getAppUsers() { - return $this->call_method('facebook.friends.getAppUsers', array()); + return $this->call_method('facebook.friends.getAppUsers'); } /** @@ -830,8 +1018,9 @@ function toggleDisplay(id, type) { * * @param int $uid (Optional) User associated with groups. A null * parameter will default to the session user. - * @param array $gids (Optional) Group ids to query. A null parameter will - * get all groups for the user. + * @param array/string $gids (Optional) Array of group ids to query. A null + * parameter will get all groups for the user. + * (csv is deprecated) * * @return array An array of group objects */ @@ -889,6 +1078,40 @@ function toggleDisplay(id, type) { 'path' => $path)); } + /** + * Retrieves links posted by the given user. + * + * @param int $uid The user whose links you wish to retrieve + * @param int $limit The maximimum number of links to retrieve + * @param array $link_ids (Optional) Array of specific link + * IDs to retrieve by this user + * + * @return array An array of links. + */ + public function &links_get($uid, $limit, $link_ids = null) { + return $this->call_method('links.get', + array('uid' => $uid, + 'limit' => $limit, + 'link_ids' => $link_ids)); + } + + /** + * Posts a link on Facebook. + * + * @param string $url URL/link you wish to post + * @param string $comment (Optional) A comment about this link + * @param int $uid (Optional) User ID that is posting this link; + * defaults to current session user + * + * @return bool + */ + public function &links_post($url, $comment='', $uid = null) { + return $this->call_method('links.post', + array('uid' => $uid, + 'url' => $url, + 'comment' => $comment)); + } + /** * Permissions API */ @@ -945,6 +1168,78 @@ function toggleDisplay(id, type) { array('permissions_apikey' => $permissions_apikey)); } + /** + * Creates a note with the specified title and content. + * + * @param string $title Title of the note. + * @param string $content Content of the note. + * @param int $uid (Optional) The user for whom you are creating a + * note; defaults to current session user + * + * @return int The ID of the note that was just created. + */ + public function ¬es_create($title, $content, $uid = null) { + return $this->call_method('notes.create', + array('uid' => $uid, + 'title' => $title, + 'content' => $content)); + } + + /** + * Deletes the specified note. + * + * @param int $note_id ID of the note you wish to delete + * @param int $uid (Optional) Owner of the note you wish to delete; + * defaults to current session user + * + * @return bool + */ + public function ¬es_delete($note_id, $uid = null) { + return $this->call_method('notes.delete', + array('uid' => $uid, + 'note_id' => $note_id)); + } + + /** + * Edits a note, replacing its title and contents with the title + * and contents specified. + * + * @param int $note_id ID of the note you wish to edit + * @param string $title Replacement title for the note + * @param string $content Replacement content for the note + * @param int $uid (Optional) Owner of the note you wish to edit; + * defaults to current session user + * + * @return bool + */ + public function ¬es_edit($note_id, $title, $content, $uid = null) { + return $this->call_method('notes.edit', + array('uid' => $uid, + 'note_id' => $note_id, + 'title' => $title, + 'content' => $content)); + } + + /** + * Retrieves all notes by a user. If note_ids are specified, + * retrieves only those specific notes by that user. + * + * @param int $uid User whose notes you wish to retrieve + * @param array $note_ids (Optional) List of specific note + * IDs by this user to retrieve + * + * @return array A list of all of the given user's notes, or an empty list + * if the viewer lacks permissions or if there are no visible + * notes. + */ + public function ¬es_get($uid, $note_ids = null) { + + return $this->call_method('notes.get', + array('uid' => $uid, + 'note_ids' => $note_ids)); + } + + /** * Returns the outstanding notifications for the session user. * @@ -954,13 +1249,15 @@ function toggleDisplay(id, type) { * and an eid list of 'event_invites' */ public function ¬ifications_get() { - return $this->call_method('facebook.notifications.get', array()); + return $this->call_method('facebook.notifications.get'); } /** * Sends a notification to the specified users. * * @return A comma separated list of successful recipients + * @error + * API_EC_PARAM_USER_ID_LIST */ public function ¬ifications_send($to_ids, $notification, $type) { return $this->call_method('facebook.notifications.send', @@ -972,12 +1269,14 @@ function toggleDisplay(id, type) { /** * Sends an email to the specified user of the application. * - * @param array $recipients id of the recipients + * @param array/string $recipients array of ids of the recipients (csv is deprecated) * @param string $subject subject of the email * @param string $text (plain text) body of the email * @param string $fbml fbml markup for an html version of the email * * @return string A comma separated list of successful recipients + * @error + * API_EC_PARAM_USER_ID_LIST */ public function ¬ifications_sendEmail($recipients, $subject, @@ -993,9 +1292,9 @@ function toggleDisplay(id, type) { /** * Returns the requested info fields for the requested set of pages. * - * @param array $page_ids an array of page ids - * @param array $fields an array of strings describing the info fields - * desired + * @param array/string $page_ids an array of page ids (csv is deprecated) + * @param array/string $fields an array of strings describing the + * info fields desired (csv is deprecated) * @param int $uid (Optional) limit results to pages of which this * user is a fan. * @param string type limits results to a particular type of page. @@ -1090,7 +1389,7 @@ function toggleDisplay(id, type) { 'tag_text' => $tag_text, 'x' => $x, 'y' => $y, - 'tags' => json_encode($tags), + 'tags' => (is_array($tags)) ? json_encode($tags) : null, 'owner_uid' => $this->get_uid($owner_uid))); } @@ -1128,7 +1427,8 @@ function toggleDisplay(id, type) { * @param int $subj_id (Optional) Filter by uid of user tagged in the photos. * @param int $aid (Optional) Filter by an album, as returned by * photos_getAlbums. - * @param array $pids (Optional) Restrict to a list of pids + * @param array/string $pids (Optional) Restrict to an array of pids + * (csv is deprecated) * * Note that at least one of these parameters needs to be specified, or an * error is returned. @@ -1143,9 +1443,10 @@ function toggleDisplay(id, type) { /** * Returns the albums created by the given user. * - * @param int $uid (Optional) The uid of the user whose albums you want. - * A null will return the albums of the session user. - * @param array $aids (Optional) A list of aids to restrict the query. + * @param int $uid (Optional) The uid of the user whose albums you want. + * A null will return the albums of the session user. + * @param string $aids (Optional) An array of aids to restrict + * the query. (csv is deprecated) * * Note that at least one of the (uid, aids) parameters must be specified. * @@ -1171,17 +1472,67 @@ function toggleDisplay(id, type) { array('pids' => $pids)); } + /** + * Uploads a photo. + * + * @param string $file The location of the photo on the local filesystem. + * @param int $aid (Optional) The album into which to upload the + * photo. + * @param string $caption (Optional) A caption for the photo. + * @param int uid (Optional) The user ID of the user whose photo you + * are uploading + * + * @return array An array of user objects + */ + public function photos_upload($file, $aid=null, $caption=null, $uid=null) { + return $this->call_upload_method('facebook.photos.upload', + array('aid' => $aid, + 'caption' => $caption, + 'uid' => $uid), + $file); + } + + + /** + * Uploads a video. + * + * @param string $file The location of the video on the local filesystem. + * @param string $title (Optional) A title for the video. Titles over 65 characters in length will be truncated. + * @param string $description (Optional) A description for the video. + * + * @return array An array with the video's ID, title, description, and a link to view it on Facebook. + */ + public function video_upload($file, $title=null, $description=null) { + return $this->call_upload_method('facebook.video.upload', + array('title' => $title, + 'description' => $description), + $file, + Facebook::get_facebook_url('api-video') . '/restserver.php'); + } + + /** + * Returns an array with the video limitations imposed on the current session's + * associated user. Maximum length is measured in seconds; maximum size is + * measured in bytes. + * + * @return array Array with "length" and "size" keys + */ + public function &video_getUploadLimits() { + return $this->call_method('facebook.video.getUploadLimits'); + } + /** * Returns the requested info fields for the requested set of users. * - * @param array $uids An array of user ids - * @param array $fields An array of info field names desired + * @param array/string $uids An array of user ids (csv is deprecated) + * @param array/string $fields An array of info field names desired (csv is deprecated) * * @return array An array of user objects */ public function &users_getInfo($uids, $fields) { return $this->call_method('facebook.users.getInfo', - array('uids' => $uids, 'fields' => $fields)); + array('uids' => $uids, + 'fields' => $fields)); } /** @@ -1194,14 +1545,15 @@ function toggleDisplay(id, type) { * users, use users.getInfo instead, so that proper privacy rules will be * applied. * - * @param array $uids An array of user ids - * @param array $fields An array of info field names desired + * @param array/string $uids An array of user ids (csv is deprecated) + * @param array/string $fields An array of info field names desired (csv is deprecated) * * @return array An array of user objects */ public function &users_getStandardInfo($uids, $fields) { return $this->call_method('facebook.users.getStandardInfo', - array('uids' => $uids, 'fields' => $fields)); + array('uids' => $uids, + 'fields' => $fields)); } /** @@ -1210,7 +1562,7 @@ function toggleDisplay(id, type) { * @return integer User id */ public function &users_getLoggedInUser() { - return $this->call_method('facebook.users.getLoggedInUser', array()); + return $this->call_method('facebook.users.getLoggedInUser'); } /** @@ -1238,6 +1590,17 @@ function toggleDisplay(id, type) { return $this->call_method('facebook.users.isAppUser', array('uid' => $uid)); } + /** + * Returns whether or not the user corresponding to the current + * session object is verified by Facebook. See the documentation + * for Users.isVerified for details. + * + * @return boolean true if the user is verified + */ + public function &users_isVerified() { + return $this->call_method('facebook.users.isVerified'); + } + /** * Sets the users' current status message. Message does NOT contain the * word "is" , so make sure to include a verb. @@ -1268,6 +1631,69 @@ function toggleDisplay(id, type) { return $this->call_method('facebook.users.setStatus', $args); } + /** + * Gets the stream on behalf of a user using a set of users. This + * call will return the latest $limit queries between $start_time + * and $end_time. + * + * @param int $viewer_id user making the call (def: session) + * @param array $source_ids users/pages to look at (def: all connections) + * @param int $start_time start time to look for stories (def: 1 day ago) + * @param int $end_time end time to look for stories (def: now) + * @param int $limit number of stories to attempt to fetch (def: 30) + * @param string $filter_key key returned by stream.getFilters to fetch + * + * @return array( + * 'posts' => array of posts, + * 'profiles' => array of profile metadata of users/pages in posts + * 'albums' => array of album metadata in posts + * ) + */ + public function &stream_get($viewer_id = null, + $source_ids = null, + $start_time = 0, + $end_time = 0, + $limit = 30, + $filter_key = '') { + $args = array( + 'viewer_id' => $viewer_id, + 'source_ids' => $source_ids, + 'start_time' => $start_time, + 'end_time' => $end_time, + 'limit' => $limit, + 'filter_key' => $filter_key); + return $this->call_method('facebook.stream.get', $args); + } + + /** + * Gets the filters (with relevant filter keys for stream.get) for a + * particular user. These filters are typical things like news feed, + * friend lists, networks. They can be used to filter the stream + * without complex queries to determine which ids belong in which groups. + * + * @param int $uid user to get filters for + * + * @return array of stream filter objects + */ + public function &stream_getFilters($uid = null) { + $args = array('uid' => $uid); + return $this->call_method('facebook.stream.getFilters', $args); + } + + /** + * Gets the full comments given a post_id from stream.get or the + * stream FQL table. Initially, only a set of preview comments are + * returned because some posts can have many comments. + * + * @param string $post_id id of the post to get comments for + * + * @return array of comment objects + */ + public function &stream_getComments($post_id) { + $args = array('post_id' => $post_id); + return $this->call_method('facebook.stream.getComments', $args); + } + /** * Sets the FBML for the profile of the user attached to this session. * @@ -1690,7 +2116,7 @@ function toggleDisplay(id, type) { * API_EC_DATA_UNKNOWN_ERROR */ public function &data_getObjectTypes() { - return $this->call_method('facebook.data.getObjectTypes', array()); + return $this->call_method('facebook.data.getObjectTypes'); } /** @@ -2315,12 +2741,14 @@ function toggleDisplay(id, type) { * * @param string $integration_point_name Name of an integration point * (see developer wiki for list). + * @param int $uid Specific user to check the limit. * * @return int Integration point allocation value */ - public function &admin_getAllocation($integration_point_name) { + public function &admin_getAllocation($integration_point_name, $uid=null) { return $this->call_method('facebook.admin.getAllocation', - array('integration_point_name' => $integration_point_name)); + array('integration_point_name' => $integration_point_name, + 'uid' => $uid)); } /** @@ -2376,28 +2804,75 @@ function toggleDisplay(id, type) { */ public function admin_getRestrictionInfo() { return json_decode( - $this->call_method('admin.getRestrictionInfo', array()), + $this->call_method('admin.getRestrictionInfo'), true); } + + /** + * Bans a list of users from the app. Banned users can't + * access the app's canvas page and forums. + * + * @param array $uids an array of user ids + * @return bool true on success + */ + public function admin_banUsers($uids) { + return $this->call_method( + 'admin.banUsers', array('uids' => json_encode($uids))); + } + + /** + * Unban users that have been previously banned with + * admin_banUsers(). + * + * @param array $uids an array of user ids + * @return bool true on success + */ + public function admin_unbanUsers($uids) { + return $this->call_method( + 'admin.unbanUsers', array('uids' => json_encode($uids))); + } + + /** + * Gets the list of users that have been banned from the application. + * $uids is an optional parameter that filters the result with the list + * of provided user ids. If $uids is provided, + * only banned user ids that are contained in $uids are returned. + * + * @param array $uids an array of user ids to filter by + * @return bool true on success + */ + + public function admin_getBannedUsers($uids = null) { + return $this->call_method( + 'admin.getBannedUsers', + array('uids' => $uids ? json_encode($uids) : null)); + } + /* UTILITY FUNCTIONS */ /** - * Calls the specified method with the specified parameters. + * Calls the specified normal POST method with the specified parameters. * * @param string $method Name of the Facebook method to invoke * @param array $params A map of param names => param values * - * @return mixed Result of method call + * @return mixed Result of method call; this returns a reference to support + * 'delayed returns' when in a batch context. + * See: http://wiki.developers.facebook.com/index.php/Using_batching_API */ - public function & call_method($method, $params) { - //Check if we are in batch mode - if($this->batch_queue === null) { + public function &call_method($method, $params = array()) { + if (!$this->pending_batch()) { if ($this->call_as_apikey) { $params['call_as_apikey'] = $this->call_as_apikey; } - $xml = $this->post_request($method, $params); - $result = $this->convert_xml_to_result($xml, $method, $params); + $data = $this->post_request($method, $params); + if (empty($params['format']) || strtolower($params['format']) != 'json') { + $result = $this->convert_xml_to_result($data, $method, $params); + } + else { + $result = json_decode($data, true); + } if (is_array($result) && isset($result['error_code'])) { throw new FacebookRestClientException($result['error_msg'], @@ -2413,11 +2888,46 @@ function toggleDisplay(id, type) { return $result; } - private function convert_xml_to_result($xml, $method, $params) { + /** + * Calls the specified file-upload POST method with the specified parameters + * + * @param string $method Name of the Facebook method to invoke + * @param array $params A map of param names => param values + * @param string $file A path to the file to upload (required) + * + * @return array A dictionary representing the response. + */ + public function call_upload_method($method, $params, $file, $server_addr = null) { + if (!$this->pending_batch()) { + if (!file_exists($file)) { + $code = + FacebookAPIErrorCodes::API_EC_PARAM; + $description = FacebookAPIErrorCodes::$api_error_descriptions[$code]; + throw new FacebookRestClientException($description, $code); + } + + $xml = $this->post_upload_request($method, $params, $file, $server_addr); + $result = $this->convert_xml_to_result($xml, $method, $params); + + if (is_array($result) && isset($result['error_code'])) { + throw new FacebookRestClientException($result['error_msg'], + $result['error_code']); + } + } + else { + $code = + FacebookAPIErrorCodes::API_EC_BATCH_METHOD_NOT_ALLOWED_IN_BATCH_MODE; + $description = FacebookAPIErrorCodes::$api_error_descriptions[$code]; + throw new FacebookRestClientException($description, $code); + } + + return $result; + } + + protected function convert_xml_to_result($xml, $method, $params) { $sxml = simplexml_load_string($xml); $result = self::convert_simplexml_to_array($sxml); - if (!empty($GLOBALS['facebook_config']['debug'])) { // output the raw xml and its corresponding php object, for debugging: print '
    '; @@ -2436,7 +2946,25 @@ function toggleDisplay(id, type) { return $result; } - private function create_post_string($method, $params) { + private function finalize_params($method, &$params) { + $this->add_standard_params($method, $params); + // we need to do this before signing the params + $this->convert_array_values_to_json($params); + $params['sig'] = Facebook::generate_sig($params, $this->secret); + } + + private function convert_array_values_to_json(&$params) { + foreach ($params as $key => &$val) { + if (is_array($val)) { + $val = json_encode($val); + } + } + } + + private function add_standard_params($method, &$params) { + if ($this->call_as_apikey) { + $params['call_as_apikey'] = $this->call_as_apikey; + } $params['method'] = $method; $params['session_key'] = $this->session_key; $params['api_key'] = $this->api_key; @@ -2448,50 +2976,118 @@ function toggleDisplay(id, type) { if (!isset($params['v'])) { $params['v'] = '1.0'; } + if (isset($this->use_ssl_resources) && + $this->use_ssl_resources) { + $params['return_ssl_resources'] = true; + } + } + + private function create_post_string($method, $params) { $post_params = array(); foreach ($params as $key => &$val) { - if (is_array($val)) $val = implode(',', $val); $post_params[] = $key.'='.urlencode($val); } - $secret = $this->secret; - $post_params[] = 'sig='.Facebook::generate_sig($params, $secret); return implode('&', $post_params); } - public function post_request($method, $params) { + private function run_multipart_http_transaction($method, $params, $file, $server_addr) { - $post_string = $this->create_post_string($method, $params); + // the format of this message is specified in RFC1867/RFC1341. + // we add twenty pseudo-random digits to the end of the boundary string. + $boundary = '--------------------------FbMuLtIpArT' . + sprintf("%010d", mt_rand()) . + sprintf("%010d", mt_rand()); + $content_type = 'multipart/form-data; boundary=' . $boundary; + // within the message, we prepend two extra hyphens. + $delimiter = '--' . $boundary; + $close_delimiter = $delimiter . '--'; + $content_lines = array(); + foreach ($params as $key => &$val) { + $content_lines[] = $delimiter; + $content_lines[] = 'Content-Disposition: form-data; name="' . $key . '"'; + $content_lines[] = ''; + $content_lines[] = $val; + } + // now add the file data + $content_lines[] = $delimiter; + $content_lines[] = + 'Content-Disposition: form-data; filename="' . $file . '"'; + $content_lines[] = 'Content-Type: application/octet-stream'; + $content_lines[] = ''; + $content_lines[] = file_get_contents($file); + $content_lines[] = $close_delimiter; + $content_lines[] = ''; + $content = implode("\r\n", $content_lines); + return $this->run_http_post_transaction($content_type, $content, $server_addr); + } - if (function_exists('curl_init')) { - // Use CURL if installed... + public function post_request($method, $params) { + $this->finalize_params($method, $params); + $post_string = $this->create_post_string($method, $params); + if ($this->use_curl_if_available && function_exists('curl_init')) { $useragent = 'Facebook API PHP5 Client 1.1 (curl) ' . phpversion(); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->server_addr); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, $useragent); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); + curl_setopt($ch, CURLOPT_TIMEOUT, 30); $result = curl_exec($ch); curl_close($ch); } else { - // Non-CURL based version... $content_type = 'application/x-www-form-urlencoded'; - $user_agent = 'Facebook API PHP5 Client 1.1 (non-curl) '.phpversion(); - $context = - array('http' => + $content = $post_string; + $result = $this->run_http_post_transaction($content_type, + $content, + $this->server_addr); + } + return $result; + } + + private function post_upload_request($method, $params, $file, $server_addr = null) { + $server_addr = $server_addr ? $server_addr : $this->server_addr; + $this->finalize_params($method, $params); + if ($this->use_curl_if_available && function_exists('curl_init')) { + // prepending '@' causes cURL to upload the file; the key is ignored. + $params['_file'] = '@' . $file; + $useragent = 'Facebook API PHP5 Client 1.1 (curl) ' . phpversion(); + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $server_addr); + // this has to come before the POSTFIELDS set! + curl_setopt($ch, CURLOPT_POST, 1 ); + // passing an array gets curl to use the multipart/form-data content type + curl_setopt($ch, CURLOPT_POSTFIELDS, $params); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_USERAGENT, $useragent); + $result = curl_exec($ch); + curl_close($ch); + } else { + $result = $this->run_multipart_http_transaction($method, $params, $file, $server_addr); + } + return $result; + } + + private function run_http_post_transaction($content_type, $content, $server_addr) { + + $user_agent = 'Facebook API PHP5 Client 1.1 (non-curl) ' . phpversion(); + $content_length = strlen($content); + $context = + array('http' => array('method' => 'POST', - 'header' => 'Content-type: '.$content_type."\r\n". - 'User-Agent: '.$user_agent."\r\n". - 'Content-length: ' . strlen($post_string), - 'content' => $post_string)); - $contextid=stream_context_create($context); - $sock=fopen($this->server_addr, 'r', false, $contextid); - if ($sock) { - $result=''; - while (!feof($sock)) - $result.=fgets($sock, 4096); - - fclose($sock); + 'user_agent' => $user_agent, + 'header' => 'Content-Type: ' . $content_type . "\r\n" . + 'Content-Length: ' . $content_length, + 'content' => $content)); + $context_id = stream_context_create($context); + $sock = fopen($server_addr, 'r', false, $context_id); + + $result = ''; + if ($sock) { + while (!feof($sock)) { + $result .= fgets($sock, 4096); } + fclose($sock); } return $result; } @@ -2541,6 +3137,14 @@ class FacebookAPIErrorCodes { const API_EC_METHOD = 3; const API_EC_TOO_MANY_CALLS = 4; const API_EC_BAD_IP = 5; + const API_EC_HOST_API = 6; + const API_EC_HOST_UP = 7; + const API_EC_SECURE = 8; + const API_EC_RATE = 9; + const API_EC_PERMISSION_DENIED = 10; + const API_EC_DEPRECATED = 11; + const API_EC_VERSION = 12; + const API_EC_INTERNAL_FQL_ERROR = 13; /* * PARAMETER ERRORS @@ -2550,27 +3154,121 @@ class FacebookAPIErrorCodes { const API_EC_PARAM_SESSION_KEY = 102; const API_EC_PARAM_CALL_ID = 103; const API_EC_PARAM_SIGNATURE = 104; + const API_EC_PARAM_TOO_MANY = 105; const API_EC_PARAM_USER_ID = 110; const API_EC_PARAM_USER_FIELD = 111; const API_EC_PARAM_SOCIAL_FIELD = 112; + const API_EC_PARAM_EMAIL = 113; + const API_EC_PARAM_USER_ID_LIST = 114; + const API_EC_PARAM_FIELD_LIST = 115; const API_EC_PARAM_ALBUM_ID = 120; + const API_EC_PARAM_PHOTO_ID = 121; + const API_EC_PARAM_FEED_PRIORITY = 130; + const API_EC_PARAM_CATEGORY = 140; + const API_EC_PARAM_SUBCATEGORY = 141; + const API_EC_PARAM_TITLE = 142; + const API_EC_PARAM_DESCRIPTION = 143; + const API_EC_PARAM_BAD_JSON = 144; const API_EC_PARAM_BAD_EID = 150; const API_EC_PARAM_UNKNOWN_CITY = 151; + const API_EC_PARAM_BAD_PAGE_TYPE = 152; /* * USER PERMISSIONS ERRORS */ const API_EC_PERMISSION = 200; const API_EC_PERMISSION_USER = 210; + const API_EC_PERMISSION_NO_DEVELOPERS = 211; const API_EC_PERMISSION_ALBUM = 220; const API_EC_PERMISSION_PHOTO = 221; + const API_EC_PERMISSION_MESSAGE = 230; + const API_EC_PERMISSION_OTHER_USER = 240; + const API_EC_PERMISSION_STATUS_UPDATE = 250; + const API_EC_PERMISSION_PHOTO_UPLOAD = 260; + const API_EC_PERMISSION_VIDEO_UPLOAD = 261; + const API_EC_PERMISSION_SMS = 270; + const API_EC_PERMISSION_CREATE_LISTING = 280; + const API_EC_PERMISSION_CREATE_NOTE = 281; + const API_EC_PERMISSION_SHARE_ITEM = 282; const API_EC_PERMISSION_EVENT = 290; + const API_EC_PERMISSION_LARGE_FBML_TEMPLATE = 291; + const API_EC_PERMISSION_LIVEMESSAGE = 292; const API_EC_PERMISSION_RSVP_EVENT = 299; - const FQL_EC_PARSER = 601; + /* + * DATA EDIT ERRORS + */ + const API_EC_EDIT = 300; + const API_EC_EDIT_USER_DATA = 310; + const API_EC_EDIT_PHOTO = 320; + const API_EC_EDIT_ALBUM_SIZE = 321; + const API_EC_EDIT_PHOTO_TAG_SUBJECT = 322; + const API_EC_EDIT_PHOTO_TAG_PHOTO = 323; + const API_EC_EDIT_PHOTO_FILE = 324; + const API_EC_EDIT_PHOTO_PENDING_LIMIT = 325; + const API_EC_EDIT_PHOTO_TAG_LIMIT = 326; + const API_EC_EDIT_ALBUM_REORDER_PHOTO_NOT_IN_ALBUM = 327; + const API_EC_EDIT_ALBUM_REORDER_TOO_FEW_PHOTOS = 328; + + const API_EC_MALFORMED_MARKUP = 329; + const API_EC_EDIT_MARKUP = 330; + + const API_EC_EDIT_FEED_TOO_MANY_USER_CALLS = 340; + const API_EC_EDIT_FEED_TOO_MANY_USER_ACTION_CALLS = 341; + const API_EC_EDIT_FEED_TITLE_LINK = 342; + const API_EC_EDIT_FEED_TITLE_LENGTH = 343; + const API_EC_EDIT_FEED_TITLE_NAME = 344; + const API_EC_EDIT_FEED_TITLE_BLANK = 345; + const API_EC_EDIT_FEED_BODY_LENGTH = 346; + const API_EC_EDIT_FEED_PHOTO_SRC = 347; + const API_EC_EDIT_FEED_PHOTO_LINK = 348; + + const API_EC_EDIT_VIDEO_SIZE = 350; + const API_EC_EDIT_VIDEO_INVALID_FILE = 351; + const API_EC_EDIT_VIDEO_INVALID_TYPE = 352; + const API_EC_EDIT_VIDEO_FILE = 353; + + const API_EC_EDIT_FEED_TITLE_ARRAY = 360; + const API_EC_EDIT_FEED_TITLE_PARAMS = 361; + const API_EC_EDIT_FEED_BODY_ARRAY = 362; + const API_EC_EDIT_FEED_BODY_PARAMS = 363; + const API_EC_EDIT_FEED_PHOTO = 364; + const API_EC_EDIT_FEED_TEMPLATE = 365; + const API_EC_EDIT_FEED_TARGET = 366; + const API_EC_EDIT_FEED_MARKUP = 367; + + /** + * SESSION ERRORS + */ + const API_EC_SESSION_TIMED_OUT = 450; + const API_EC_SESSION_METHOD = 451; + const API_EC_SESSION_INVALID = 452; + const API_EC_SESSION_REQUIRED = 453; + const API_EC_SESSION_REQUIRED_FOR_SECRET = 454; + const API_EC_SESSION_CANNOT_USE_SESSION_SECRET = 455; + + + /** + * FQL ERRORS + */ + const FQL_EC_UNKNOWN_ERROR = 600; + const FQL_EC_PARSER = 601; // backwards compatibility + const FQL_EC_PARSER_ERROR = 601; const FQL_EC_UNKNOWN_FIELD = 602; const FQL_EC_UNKNOWN_TABLE = 603; - const FQL_EC_NOT_INDEXABLE = 604; + const FQL_EC_NOT_INDEXABLE = 604; // backwards compatibility + const FQL_EC_NO_INDEX = 604; + const FQL_EC_UNKNOWN_FUNCTION = 605; + const FQL_EC_INVALID_PARAM = 606; + const FQL_EC_INVALID_FIELD = 607; + const FQL_EC_INVALID_SESSION = 608; + const FQL_EC_UNSUPPORTED_APP_TYPE = 609; + const FQL_EC_SESSION_SECRET_NOT_ALLOWED = 610; + const FQL_EC_DEPRECATED_TABLE = 611; + const FQL_EC_EXTENDED_PERMISSION = 612; + const FQL_EC_RATE_LIMIT_EXCEEDED = 613; + + const API_EC_REF_SET_FAILED = 700; /** * DATA STORE API ERRORS @@ -2581,52 +3279,122 @@ class FacebookAPIErrorCodes { const API_EC_DATA_OBJECT_NOT_FOUND = 803; const API_EC_DATA_OBJECT_ALREADY_EXISTS = 804; const API_EC_DATA_DATABASE_ERROR = 805; + const API_EC_DATA_CREATE_TEMPLATE_ERROR = 806; + const API_EC_DATA_TEMPLATE_EXISTS_ERROR = 807; + const API_EC_DATA_TEMPLATE_HANDLE_TOO_LONG = 808; + const API_EC_DATA_TEMPLATE_HANDLE_ALREADY_IN_USE = 809; + const API_EC_DATA_TOO_MANY_TEMPLATE_BUNDLES = 810; + const API_EC_DATA_MALFORMED_ACTION_LINK = 811; + const API_EC_DATA_TEMPLATE_USES_RESERVED_TOKEN = 812; /* - * Batch ERROR + * APPLICATION INFO ERRORS */ - const API_EC_BATCH_ALREADY_STARTED = 900; - const API_EC_BATCH_NOT_STARTED = 901; - const API_EC_BATCH_METHOD_NOT_ALLOWED_IN_BATCH_MODE = 902; + const API_EC_NO_SUCH_APP = 900; + /* + * BATCH ERRORS + */ + const API_EC_BATCH_TOO_MANY_ITEMS = 950; + const API_EC_BATCH_ALREADY_STARTED = 951; + const API_EC_BATCH_NOT_STARTED = 952; + const API_EC_BATCH_METHOD_NOT_ALLOWED_IN_BATCH_MODE = 953; + + /* + * EVENT API ERRORS + */ + const API_EC_EVENT_INVALID_TIME = 1000; + + /* + * INFO BOX ERRORS + */ + const API_EC_INFO_NO_INFORMATION = 1050; + const API_EC_INFO_SET_FAILED = 1051; + + /* + * LIVEMESSAGE API ERRORS + */ + const API_EC_LIVEMESSAGE_SEND_FAILED = 1100; + const API_EC_LIVEMESSAGE_EVENT_NAME_TOO_LONG = 1101; + const API_EC_LIVEMESSAGE_MESSAGE_TOO_LONG = 1102; + + /* + * CONNECT SESSION ERRORS + */ + const API_EC_CONNECT_FEED_DISABLED = 1300; + + /* + * Platform tag bundles errors + */ + const API_EC_TAG_BUNDLE_QUOTA = 1400; + + /* + * SHARE + */ + const API_EC_SHARE_BAD_URL = 1500; + + /* + * NOTES + */ + const API_EC_NOTE_CANNOT_MODIFY = 1600; + + /* + * COMMENTS + */ + const API_EC_COMMENTS_UNKNOWN = 1700; + const API_EC_COMMENTS_POST_TOO_LONG = 1701; + const API_EC_COMMENTS_DB_DOWN = 1702; + const API_EC_COMMENTS_INVALID_XID = 1703; + const API_EC_COMMENTS_INVALID_UID = 1704; + const API_EC_COMMENTS_INVALID_POST = 1705; + + /** + * This array is no longer maintained; to view the description of an error + * code, please look at the message element of the API response or visit + * the developer wiki at http://wiki.developers.facebook.com/. + */ public static $api_error_descriptions = array( - API_EC_SUCCESS => 'Success', - API_EC_UNKNOWN => 'An unknown error occurred', - API_EC_SERVICE => 'Service temporarily unavailable', - API_EC_METHOD => 'Unknown method', - API_EC_TOO_MANY_CALLS => 'Application request limit reached', - API_EC_BAD_IP => 'Unauthorized source IP address', - API_EC_PARAM => 'Invalid parameter', - API_EC_PARAM_API_KEY => 'Invalid API key', - API_EC_PARAM_SESSION_KEY => 'Session key invalid or no longer valid', - API_EC_PARAM_CALL_ID => 'Call_id must be greater than previous', - API_EC_PARAM_SIGNATURE => 'Incorrect signature', - API_EC_PARAM_USER_ID => 'Invalid user id', - API_EC_PARAM_USER_FIELD => 'Invalid user info field', - API_EC_PARAM_SOCIAL_FIELD => 'Invalid user field', - API_EC_PARAM_ALBUM_ID => 'Invalid album id', - API_EC_PARAM_BAD_EID => 'Invalid eid', - API_EC_PARAM_UNKNOWN_CITY => 'Unknown city', - API_EC_PERMISSION => 'Permissions error', - API_EC_PERMISSION_USER => 'User not visible', - API_EC_PERMISSION_ALBUM => 'Album not visible', - API_EC_PERMISSION_PHOTO => 'Photo not visible', - API_EC_PERMISSION_EVENT => 'Creating and modifying events required the extended permission create_event', - API_EC_PERMISSION_RSVP_EVENT => 'RSVPing to events required the extended permission rsvp_event', - FQL_EC_PARSER => 'FQL: Parser Error', - FQL_EC_UNKNOWN_FIELD => 'FQL: Unknown Field', - FQL_EC_UNKNOWN_TABLE => 'FQL: Unknown Table', - FQL_EC_NOT_INDEXABLE => 'FQL: Statement not indexable', - FQL_EC_UNKNOWN_FUNCTION => 'FQL: Attempted to call unknown function', - FQL_EC_INVALID_PARAM => 'FQL: Invalid parameter passed in', - API_EC_DATA_UNKNOWN_ERROR => 'Unknown data store API error', - API_EC_DATA_INVALID_OPERATION => 'Invalid operation', - API_EC_DATA_QUOTA_EXCEEDED => 'Data store allowable quota was exceeded', - API_EC_DATA_OBJECT_NOT_FOUND => 'Specified object cannot be found', - API_EC_DATA_OBJECT_ALREADY_EXISTS => 'Specified object already exists', - API_EC_DATA_DATABASE_ERROR => 'A database error occurred. Please try again', - API_EC_BATCH_ALREADY_STARTED => 'begin_batch already called, please make sure to call end_batch first', - API_EC_BATCH_NOT_STARTED => 'end_batch called before start_batch', - API_EC_BATCH_METHOD_NOT_ALLOWED_IN_BATCH_MODE => 'This method is not allowed in batch mode', + self::API_EC_SUCCESS => 'Success', + self::API_EC_UNKNOWN => 'An unknown error occurred', + self::API_EC_SERVICE => 'Service temporarily unavailable', + self::API_EC_METHOD => 'Unknown method', + self::API_EC_TOO_MANY_CALLS => 'Application request limit reached', + self::API_EC_BAD_IP => 'Unauthorized source IP address', + self::API_EC_PARAM => 'Invalid parameter', + self::API_EC_PARAM_API_KEY => 'Invalid API key', + self::API_EC_PARAM_SESSION_KEY => 'Session key invalid or no longer valid', + self::API_EC_PARAM_CALL_ID => 'Call_id must be greater than previous', + self::API_EC_PARAM_SIGNATURE => 'Incorrect signature', + self::API_EC_PARAM_USER_ID => 'Invalid user id', + self::API_EC_PARAM_USER_FIELD => 'Invalid user info field', + self::API_EC_PARAM_SOCIAL_FIELD => 'Invalid user field', + self::API_EC_PARAM_USER_ID_LIST => 'Invalid user id list', + self::API_EC_PARAM_FIELD_LIST => 'Invalid field list', + self::API_EC_PARAM_ALBUM_ID => 'Invalid album id', + self::API_EC_PARAM_BAD_EID => 'Invalid eid', + self::API_EC_PARAM_UNKNOWN_CITY => 'Unknown city', + self::API_EC_PERMISSION => 'Permissions error', + self::API_EC_PERMISSION_USER => 'User not visible', + self::API_EC_PERMISSION_NO_DEVELOPERS => 'Application has no developers', + self::API_EC_PERMISSION_ALBUM => 'Album not visible', + self::API_EC_PERMISSION_PHOTO => 'Photo not visible', + self::API_EC_PERMISSION_EVENT => 'Creating and modifying events required the extended permission create_event', + self::API_EC_PERMISSION_RSVP_EVENT => 'RSVPing to events required the extended permission rsvp_event', + self::API_EC_EDIT_ALBUM_SIZE => 'Album is full', + self::FQL_EC_PARSER => 'FQL: Parser Error', + self::FQL_EC_UNKNOWN_FIELD => 'FQL: Unknown Field', + self::FQL_EC_UNKNOWN_TABLE => 'FQL: Unknown Table', + self::FQL_EC_NOT_INDEXABLE => 'FQL: Statement not indexable', + self::FQL_EC_UNKNOWN_FUNCTION => 'FQL: Attempted to call unknown function', + self::FQL_EC_INVALID_PARAM => 'FQL: Invalid parameter passed in', + self::API_EC_DATA_UNKNOWN_ERROR => 'Unknown data store API error', + self::API_EC_DATA_INVALID_OPERATION => 'Invalid operation', + self::API_EC_DATA_QUOTA_EXCEEDED => 'Data store allowable quota was exceeded', + self::API_EC_DATA_OBJECT_NOT_FOUND => 'Specified object cannot be found', + self::API_EC_DATA_OBJECT_ALREADY_EXISTS => 'Specified object already exists', + self::API_EC_DATA_DATABASE_ERROR => 'A database error occurred. Please try again', + self::API_EC_BATCH_ALREADY_STARTED => 'begin_batch already called, please make sure to call end_batch first', + self::API_EC_BATCH_NOT_STARTED => 'end_batch called before begin_batch', + self::API_EC_BATCH_METHOD_NOT_ALLOWED_IN_BATCH_MODE => 'This method is not allowed in batch mode' ); } diff --git a/index.php b/index.php index 0c69e226f7..4eff99dff5 100644 --- a/index.php +++ b/index.php @@ -63,8 +63,14 @@ function handleError($error) function main() { + // quick check for fancy URL auto-detection support in installer. + if (isset($_SERVER['REDIRECT_URL']) && ((dirname($_SERVER['REQUEST_URI']) . '/check-fancy') === $_SERVER['REDIRECT_URL'])) { + die("Fancy URL support detection succeeded. We suggest you enable this to get fancy (pretty) URLs."); + } global $user, $action, $config; + Snapshot::check(); + if (!_have_config()) { $msg = sprintf(_("No configuration file found. Try running ". "the installation program first.")); diff --git a/install.php b/install.php index 87a99a6508..133f2b30f6 100644 --- a/install.php +++ b/install.php @@ -35,15 +35,17 @@ function main() function checkPrereqs() { + $pass = true; + if (file_exists(INSTALLDIR.'/config.php')) { ?>

    Config file "config.php" already exists.

    Require PHP version 5 or greater.

    Cannot load required extension "".

    Cannot load required extension:

    Cannot write config file to "".

    -

    On your server, try this command:

    -
    chmod a+w
    + ?>

    Cannot write config file to:

    +

    On your server, try this command: chmod a+w

    Cannot write avatar directory "/avatar/".

    -

    On your server, try this command:

    -
    chmod a+w /avatar/
    + ?>

    Cannot write avatar directory: /avatar/

    +

    On your server, try this command: chmod a+w /avatar/

    -

    Enter your database connection information below to initialize the database.

    -
    -
    -
      -
    • - - -

      The name of your site

      -
    • -
    • -
    • - - -

      Database hostname

      -
    • -
    • - - -

      Database name

      -
    • -
    • - - -

      Database username

      -
    • -
    • - - -

      Database password

      -
    • -
    - -
    + $config_path = htmlentities(trim(dirname($_SERVER['REQUEST_URI']), '/')); + echo<< + + +
    +
    Page notice
    +
    +
    +

    Enter your database connection information below to initialize the database.

    +
    +
    +
    + +
    + Connection settings +
      +
    • + + +

      The name of your site

      +
    • +
    • + + enable
      + disable
      +

      Enable fancy (pretty) URLs. Auto-detection failed, it depends on Javascript.

      +
    • +
    • + + +

      Site path, following the "/" after the domain name in the URL. Empty is fine. Field should be filled automatically.

      +
    • +
    • + + +

      Database hostname

      +
    • +
    • + + +

      Database name

      +
    • +
    • + + +

      Database username

      +
    • +
    • + + +

      Database password

      +
    • +
    + +
    - -
  • - -
  • -> + + -
      - +
      +
      Page notice
      +
      +
        +new Laconica site."); ?> -
      -"); return $res; } @@ -253,21 +288,37 @@ function runDbScript($filename, $conn) } ?> - - - Install Laconica - - - - - -
      -
      -
      -

      Install Laconica

      + xml version="1.0" encoding="UTF-8" "; ?> + + + + Install Laconica + + + + + + + + + +
      + +
      +
      +

      Install Laconica

      -
      -
      -
      - +
      +
      +
      + diff --git a/js/farbtastic/LICENSE.txt b/js/farbtastic/LICENSE.txt new file mode 100644 index 0000000000..5a3cc209ad --- /dev/null +++ b/js/farbtastic/LICENSE.txt @@ -0,0 +1,341 @@ + + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/js/farbtastic/farbtastic.go.js b/js/farbtastic/farbtastic.go.js new file mode 100644 index 0000000000..0149eca7d9 --- /dev/null +++ b/js/farbtastic/farbtastic.go.js @@ -0,0 +1,85 @@ +/** Init for Farbtastic library and page setup + * + * @package Laconica + * @author Sarven Capadisli + * @copyright 2009 Control Yourself, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + */ +$(document).ready(function() { + function UpdateColors(S) { + C = $(S).val(); + switch (parseInt(S.id.slice(-1))) { + case 0: default: + $('body').css({'background-color':C}); + break; + case 1: + $('#content').css({'background-color':C}); + break; + case 2: + $('#aside_primary').css({'background-color':C}); + break; + case 3: + $('body').css({'color':C}); + break; + case 4: + $('a').css({'color':C}); + break; + } + } + + function UpdateFarbtastic(e) { + f.linked = e; + f.setColor(e.value); + } + + function UpdateSwatch(e) { + $(e).css({"background-color": e.value, + "color": f.hsl[2] > 0.5 ? "#000": "#fff"}); + } + + function SynchColors(e) { + var S = f.linked; + var C = f.color; + + if (S && S.value && S.value != C) { + S.value = C; + UpdateSwatch(S); + UpdateColors(S); + } + } + + function Init() { + $('#settings_design_color').append('
      '); + $('#color-picker').hide(); + + f = $.farbtastic('#color-picker', SynchColors); + swatches = $('#settings_design_color .swatch'); + + swatches + .each(SynchColors) + .blur(function() { + $(this).val($(this).val().toUpperCase()); + }) + .focus(function() { + $('#color-picker').show(); + UpdateFarbtastic(this); + }) + .change(function() { + UpdateFarbtastic(this); + UpdateSwatch(this); + UpdateColors(this); + }).change(); + } + + var f, swatches; + Init(); + $('#form_settings_design').bind('reset', function(){ + setTimeout(function(){ + swatches.each(function(){UpdateColors(this);}); + $('#color-picker').remove(); + swatches.unbind(); + Init(); + },10); + }); +}); diff --git a/js/farbtastic/farbtastic.js b/js/farbtastic/farbtastic.js new file mode 100644 index 0000000000..24a377803c --- /dev/null +++ b/js/farbtastic/farbtastic.js @@ -0,0 +1,329 @@ +// $Id: farbtastic.js,v 1.2 2007/01/08 22:53:01 unconed Exp $ +// Farbtastic 1.2 + +jQuery.fn.farbtastic = function (callback) { + $.farbtastic(this, callback); + return this; +}; + +jQuery.farbtastic = function (container, callback) { + var container = $(container).get(0); + return container.farbtastic || (container.farbtastic = new jQuery._farbtastic(container, callback)); +} + +jQuery._farbtastic = function (container, callback) { + // Store farbtastic object + var fb = this; + + // Insert markup + $(container).html('
      '); + var e = $('.farbtastic', container); + fb.wheel = $('.wheel', container).get(0); + // Dimensions + fb.radius = 84; + fb.square = 100; + fb.width = 194; + + // Fix background PNGs in IE6 + if (navigator.appVersion.match(/MSIE [0-6]\./)) { + $('*', e).each(function () { + if (this.currentStyle.backgroundImage != 'none') { + var image = this.currentStyle.backgroundImage; + image = this.currentStyle.backgroundImage.substring(5, image.length - 2); + $(this).css({ + 'backgroundImage': 'none', + 'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='" + image + "')" + }); + } + }); + } + + /** + * Link to the given element(s) or callback. + */ + fb.linkTo = function (callback) { + // Unbind previous nodes + if (typeof fb.callback == 'object') { + $(fb.callback).unbind('keyup', fb.updateValue); + } + + // Reset color + fb.color = null; + + // Bind callback or elements + if (typeof callback == 'function') { + fb.callback = callback; + } + else if (typeof callback == 'object' || typeof callback == 'string') { + fb.callback = $(callback); + fb.callback.bind('keyup', fb.updateValue); + if (fb.callback.get(0).value) { + fb.setColor(fb.callback.get(0).value); + } + } + return this; + } + fb.updateValue = function (event) { + if (this.value && this.value != fb.color) { + fb.setColor(this.value); + } + } + + /** + * Change color with HTML syntax #123456 + */ + fb.setColor = function (color) { + var unpack = fb.unpack(color); + if (fb.color != color && unpack) { + fb.color = color; + fb.rgb = unpack; + fb.hsl = fb.RGBToHSL(fb.rgb); + fb.updateDisplay(); + } + return this; + } + + /** + * Change color with HSL triplet [0..1, 0..1, 0..1] + */ + fb.setHSL = function (hsl) { + fb.hsl = hsl; + fb.rgb = fb.HSLToRGB(hsl); + fb.color = fb.pack(fb.rgb); + fb.updateDisplay(); + return this; + } + + ///////////////////////////////////////////////////// + + /** + * Retrieve the coordinates of the given event relative to the center + * of the widget. + */ + fb.widgetCoords = function (event) { + var x, y; + var el = event.target || event.srcElement; + var reference = fb.wheel; + + if (typeof event.offsetX != 'undefined') { + // Use offset coordinates and find common offsetParent + var pos = { x: event.offsetX, y: event.offsetY }; + + // Send the coordinates upwards through the offsetParent chain. + var e = el; + while (e) { + e.mouseX = pos.x; + e.mouseY = pos.y; + pos.x += e.offsetLeft; + pos.y += e.offsetTop; + e = e.offsetParent; + } + + // Look for the coordinates starting from the wheel widget. + var e = reference; + var offset = { x: 0, y: 0 } + while (e) { + if (typeof e.mouseX != 'undefined') { + x = e.mouseX - offset.x; + y = e.mouseY - offset.y; + break; + } + offset.x += e.offsetLeft; + offset.y += e.offsetTop; + e = e.offsetParent; + } + + // Reset stored coordinates + e = el; + while (e) { + e.mouseX = undefined; + e.mouseY = undefined; + e = e.offsetParent; + } + } + else { + // Use absolute coordinates + var pos = fb.absolutePosition(reference); + x = (event.pageX || 0*(event.clientX + $('html').get(0).scrollLeft)) - pos.x; + y = (event.pageY || 0*(event.clientY + $('html').get(0).scrollTop)) - pos.y; + } + // Subtract distance to middle + return { x: x - fb.width / 2, y: y - fb.width / 2 }; + } + + /** + * Mousedown handler + */ + fb.mousedown = function (event) { + // Capture mouse + if (!document.dragging) { + $(document).bind('mousemove', fb.mousemove).bind('mouseup', fb.mouseup); + document.dragging = true; + } + + // Check which area is being dragged + var pos = fb.widgetCoords(event); + fb.circleDrag = Math.max(Math.abs(pos.x), Math.abs(pos.y)) * 2 > fb.square; + + // Process + fb.mousemove(event); + return false; + } + + /** + * Mousemove handler + */ + fb.mousemove = function (event) { + // Get coordinates relative to color picker center + var pos = fb.widgetCoords(event); + + // Set new HSL parameters + if (fb.circleDrag) { + var hue = Math.atan2(pos.x, -pos.y) / 6.28; + if (hue < 0) hue += 1; + fb.setHSL([hue, fb.hsl[1], fb.hsl[2]]); + } + else { + var sat = Math.max(0, Math.min(1, -(pos.x / fb.square) + .5)); + var lum = Math.max(0, Math.min(1, -(pos.y / fb.square) + .5)); + fb.setHSL([fb.hsl[0], sat, lum]); + } + return false; + } + + /** + * Mouseup handler + */ + fb.mouseup = function () { + // Uncapture mouse + $(document).unbind('mousemove', fb.mousemove); + $(document).unbind('mouseup', fb.mouseup); + document.dragging = false; + } + + /** + * Update the markers and styles + */ + fb.updateDisplay = function () { + // Markers + var angle = fb.hsl[0] * 6.28; + $('.h-marker', e).css({ + left: Math.round(Math.sin(angle) * fb.radius + fb.width / 2) + 'px', + top: Math.round(-Math.cos(angle) * fb.radius + fb.width / 2) + 'px' + }); + + $('.sl-marker', e).css({ + left: Math.round(fb.square * (.5 - fb.hsl[1]) + fb.width / 2) + 'px', + top: Math.round(fb.square * (.5 - fb.hsl[2]) + fb.width / 2) + 'px' + }); + + // Saturation/Luminance gradient + $('.color', e).css('backgroundColor', fb.pack(fb.HSLToRGB([fb.hsl[0], 1, 0.5]))); + + // Linked elements or callback + if (typeof fb.callback == 'object') { + // Set background/foreground color + $(fb.callback).css({ + backgroundColor: fb.color, + color: fb.hsl[2] > 0.5 ? '#000' : '#fff' + }); + + // Change linked value + $(fb.callback).each(function() { + if (this.value && this.value != fb.color) { + this.value = fb.color; + } + }); + } + else if (typeof fb.callback == 'function') { + fb.callback.call(fb, fb.color); + } + } + + /** + * Get absolute position of element + */ + fb.absolutePosition = function (el) { + var r = { x: el.offsetLeft, y: el.offsetTop }; + // Resolve relative to offsetParent + if (el.offsetParent) { + var tmp = fb.absolutePosition(el.offsetParent); + r.x += tmp.x; + r.y += tmp.y; + } + return r; + }; + + /* Various color utility functions */ + fb.pack = function (rgb) { + var r = Math.round(rgb[0] * 255); + var g = Math.round(rgb[1] * 255); + var b = Math.round(rgb[2] * 255); + return '#' + (r < 16 ? '0' : '') + r.toString(16) + + (g < 16 ? '0' : '') + g.toString(16) + + (b < 16 ? '0' : '') + b.toString(16); + } + + fb.unpack = function (color) { + if (color.length == 7) { + return [parseInt('0x' + color.substring(1, 3)) / 255, + parseInt('0x' + color.substring(3, 5)) / 255, + parseInt('0x' + color.substring(5, 7)) / 255]; + } + else if (color.length == 4) { + return [parseInt('0x' + color.substring(1, 2)) / 15, + parseInt('0x' + color.substring(2, 3)) / 15, + parseInt('0x' + color.substring(3, 4)) / 15]; + } + } + + fb.HSLToRGB = function (hsl) { + var m1, m2, r, g, b; + var h = hsl[0], s = hsl[1], l = hsl[2]; + m2 = (l <= 0.5) ? l * (s + 1) : l + s - l*s; + m1 = l * 2 - m2; + return [this.hueToRGB(m1, m2, h+0.33333), + this.hueToRGB(m1, m2, h), + this.hueToRGB(m1, m2, h-0.33333)]; + } + + fb.hueToRGB = function (m1, m2, h) { + h = (h < 0) ? h + 1 : ((h > 1) ? h - 1 : h); + if (h * 6 < 1) return m1 + (m2 - m1) * h * 6; + if (h * 2 < 1) return m2; + if (h * 3 < 2) return m1 + (m2 - m1) * (0.66666 - h) * 6; + return m1; + } + + fb.RGBToHSL = function (rgb) { + var min, max, delta, h, s, l; + var r = rgb[0], g = rgb[1], b = rgb[2]; + min = Math.min(r, Math.min(g, b)); + max = Math.max(r, Math.max(g, b)); + delta = max - min; + l = (min + max) / 2; + s = 0; + if (l > 0 && l < 1) { + s = delta / (l < 0.5 ? (2 * l) : (2 - 2 * l)); + } + h = 0; + if (delta > 0) { + if (max == r && max != g) h += (g - b) / delta; + if (max == g && max != b) h += (2 + (b - r) / delta); + if (max == b && max != r) h += (4 + (r - g) / delta); + h /= 6; + } + return [h, s, l]; + } + + // Install mousedown handler (the others are set on the document on-demand) + $('*', e).mousedown(fb.mousedown); + + // Init color + fb.setColor('#000000'); + + // Set linked elements/callback + if (callback) { + fb.linkTo(callback); + } +} \ No newline at end of file diff --git a/js/farbtastic/marker.png b/js/farbtastic/marker.png new file mode 100755 index 0000000000..3929bbb51d Binary files /dev/null and b/js/farbtastic/marker.png differ diff --git a/js/farbtastic/mask.png b/js/farbtastic/mask.png new file mode 100644 index 0000000000..b0a4d406fb Binary files /dev/null and b/js/farbtastic/mask.png differ diff --git a/js/farbtastic/wheel.png b/js/farbtastic/wheel.png new file mode 100644 index 0000000000..97b343d98c Binary files /dev/null and b/js/farbtastic/wheel.png differ diff --git a/js/install.js b/js/install.js new file mode 100644 index 0000000000..32a54111e2 --- /dev/null +++ b/js/install.js @@ -0,0 +1,18 @@ +$(document).ready(function(){ + $.ajax({url:'check-fancy', + type:'GET', + success:function(data, textStatus) { + $('#fancy-enable').attr('checked', true); + $('#fancy-disable').attr('checked', false); + $('#fancy-form_guide').text(data); + }, + error:function(XMLHttpRequest, textStatus, errorThrown) { + $('#fancy-enable').attr('checked', false); + $('#fancy-disable').attr('checked', true); + $('#fancy-enable').attr('disabled', true); + $('#fancy-disable').attr('disabled', true); + $('#fancy-form_guide').text("Fancy URL support detection failed, disabling this option. Make sure you renamed htaccess.sample to .htaccess."); + } + }); +}); + diff --git a/js/jcrop/jquery.Jcrop.go.js b/js/jcrop/jquery.Jcrop.go.js index a0399d5405..4e1cbfd1e7 100644 --- a/js/jcrop/jquery.Jcrop.go.js +++ b/js/jcrop/jquery.Jcrop.go.js @@ -1,39 +1,48 @@ - $(function(){ - var x = ($('#avatar_crop_x').val()) ? $('#avatar_crop_x').val() : 0; - var y = ($('#avatar_crop_y').val()) ? $('#avatar_crop_y').val() : 0; - var w = ($('#avatar_crop_w').val()) ? $('#avatar_crop_w').val() : $("#avatar_original img").attr("width"); - var h = ($('#avatar_crop_h').val()) ? $('#avatar_crop_h').val() : $("#avatar_original img").attr("height"); +/** Init for Jcrop library and page setup + * + * @package Laconica + * @author Sarven Capadisli + * @copyright 2009 Control Yourself, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link http://laconi.ca/ + */ - jQuery("#avatar_original img").Jcrop({ - onChange: showPreview, - setSelect: [ x, y, w, h ], - onSelect: updateCoords, - aspectRatio: 1, - boxWidth: 480, - boxHeight: 480, - bgColor: '#000', - bgOpacity: .4 - }); - }); +$(function(){ + var x = ($('#avatar_crop_x').val()) ? $('#avatar_crop_x').val() : 0; + var y = ($('#avatar_crop_y').val()) ? $('#avatar_crop_y').val() : 0; + var w = ($('#avatar_crop_w').val()) ? $('#avatar_crop_w').val() : $("#avatar_original img").attr("width"); + var h = ($('#avatar_crop_h').val()) ? $('#avatar_crop_h').val() : $("#avatar_original img").attr("height"); - function showPreview(coords) { - var rx = 96 / coords.w; - var ry = 96 / coords.h; + jQuery("#avatar_original img").Jcrop({ + onChange: showPreview, + setSelect: [ x, y, w, h ], + onSelect: updateCoords, + aspectRatio: 1, + boxWidth: 480, + boxHeight: 480, + bgColor: '#000', + bgOpacity: .4 + }); +}); - var img_width = $("#avatar_original img").attr("width"); - var img_height = $("#avatar_original img").attr("height"); +function showPreview(coords) { + var rx = 96 / coords.w; + var ry = 96 / coords.h; - $('#avatar_preview img').css({ - width: Math.round(rx *img_width) + 'px', - height: Math.round(ry * img_height) + 'px', - marginLeft: '-' + Math.round(rx * coords.x) + 'px', - marginTop: '-' + Math.round(ry * coords.y) + 'px' - }); - }; + var img_width = $("#avatar_original img").attr("width"); + var img_height = $("#avatar_original img").attr("height"); - function updateCoords(c) { - $('#avatar_crop_x').val(c.x); - $('#avatar_crop_y').val(c.y); - $('#avatar_crop_w').val(c.w); - $('#avatar_crop_h').val(c.h); - }; + $('#avatar_preview img').css({ + width: Math.round(rx *img_width) + 'px', + height: Math.round(ry * img_height) + 'px', + marginLeft: '-' + Math.round(rx * coords.x) + 'px', + marginTop: '-' + Math.round(ry * coords.y) + 'px' + }); +}; + +function updateCoords(c) { + $('#avatar_crop_x').val(c.x); + $('#avatar_crop_y').val(c.y); + $('#avatar_crop_w').val(c.w); + $('#avatar_crop_h').val(c.h); +}; diff --git a/js/jquery.joverlay.min.js b/js/jquery.joverlay.min.js new file mode 100644 index 0000000000..c9168506a5 --- /dev/null +++ b/js/jquery.joverlay.min.js @@ -0,0 +1,6 @@ +/* Copyright (c) 2009 Alvaro A. Lima Jr http://alvarojunior.com/jquery/joverlay.html + * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) + * Version: 0.6 (Abr 23, 2009) + * Requires: jQuery 1.3+ + */ +(function($){var f=$.browser.msie&&$.browser.version==6.0;var g=null;$.fn.jOverlay=function(b){var b=$.extend({},$.fn.jOverlay.options,b);if(g!=null){clearTimeout(g)}var c=this.is('*')?this:'#jOverlayContent';var d=f?'absolute':'fixed';var e=b.imgLoading?"":'';$('body').prepend(e+"
      "+"