2011-12-14 12:18:36 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
if (IN_serendipity !== true) {
|
|
|
|
die ("Don't hack!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Probe for a language include with constants. Still include defines later on, if some constants were missing
|
|
|
|
$probelang = dirname(__FILE__) . '/' . $serendipity['charset'] . 'lang_' . $serendipity['lang'] . '.inc.php';
|
|
|
|
if (file_exists($probelang)) {
|
|
|
|
include $probelang;
|
|
|
|
}
|
|
|
|
|
|
|
|
include dirname(__FILE__) . '/lang_en.inc.php';
|
2011-12-15 16:48:50 +01:00
|
|
|
require_once dirname(__FILE__) . '/oembed/config.php'; // autoload oembed classes and config
|
2011-12-15 01:42:11 +01:00
|
|
|
require_once dirname(__FILE__) . '/OEmbedDatabase.php';
|
2011-12-15 16:48:50 +01:00
|
|
|
require_once dirname(__FILE__) . '/OEmbedTemplater.php';
|
|
|
|
require_once dirname(__FILE__) . '/oembed/ProviderList.php';
|
2011-12-15 01:42:11 +01:00
|
|
|
|
2011-12-18 00:44:57 +01:00
|
|
|
@define('OEMBED_USE_CURL',TRUE);
|
2011-12-14 15:29:47 +01:00
|
|
|
|
2011-12-14 12:18:36 +01:00
|
|
|
class serendipity_event_oembed extends serendipity_event
|
|
|
|
{
|
|
|
|
var $title = PLUGIN_EVENT_OEMBED_NAME;
|
|
|
|
|
|
|
|
function introspect(&$propbag)
|
|
|
|
{
|
|
|
|
global $serendipity;
|
|
|
|
|
|
|
|
$propbag->add('name', PLUGIN_EVENT_OEMBED_NAME);
|
|
|
|
$propbag->add('description', PLUGIN_EVENT_OEMBED_DESC);
|
|
|
|
$propbag->add('stackable', false);
|
|
|
|
$propbag->add('author', 'Grischa Brockhaus');
|
2011-12-21 11:46:25 +01:00
|
|
|
$propbag->add('version', '1.02');
|
2011-12-14 12:18:36 +01:00
|
|
|
$propbag->add('requirements', array(
|
|
|
|
'serendipity' => '0.8',
|
|
|
|
'smarty' => '2.6.7',
|
|
|
|
'php' => '5.1.0'
|
|
|
|
));
|
|
|
|
$propbag->add('groups', array('FRONTEND_EXTERNAL_SERVICES'));
|
|
|
|
$propbag->add('event_hooks', array(
|
2011-12-14 15:29:47 +01:00
|
|
|
'frontend_display' => true,
|
2011-12-21 11:46:25 +01:00
|
|
|
'css' => true,
|
2011-12-14 12:18:36 +01:00
|
|
|
));
|
2011-12-18 20:27:21 +01:00
|
|
|
$configuration = $configuration = array('info','maxwidth','maxheight','generic_service','embedly_apikey');
|
|
|
|
$configuration[] = 'supported'; // always last
|
2011-12-16 11:44:55 +01:00
|
|
|
$propbag->add('configuration', $configuration);
|
2011-12-14 12:18:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function introspect_config_item($name, &$propbag)
|
|
|
|
{
|
2011-12-18 20:27:21 +01:00
|
|
|
$generic_services = array (
|
|
|
|
'none' => PLUGIN_EVENT_OEMBED_SERVICE_NONE,
|
|
|
|
'oohembed' => PLUGIN_EVENT_OEMBED_SERVICE_OOHEMBED,
|
|
|
|
'embedly' => PLUGIN_EVENT_OEMBED_SERVICE_EMBEDLY,
|
|
|
|
);
|
2011-12-15 01:42:11 +01:00
|
|
|
switch($name) {
|
|
|
|
case 'info':
|
|
|
|
$propbag->add('type', 'content');
|
2011-12-15 16:48:50 +01:00
|
|
|
$propbag->add('default', sprintf(PLUGIN_EVENT_OEMBED_INFO, ProviderList::ul_providernames(true)));
|
2011-12-15 01:42:11 +01:00
|
|
|
break;
|
2011-12-16 11:44:55 +01:00
|
|
|
case 'maxwidth':
|
|
|
|
$propbag->add('type', 'string');
|
|
|
|
$propbag->add('name', PLUGIN_EVENT_OEMBED_MAXWIDTH);
|
|
|
|
$propbag->add('description', PLUGIN_EVENT_OEMBED_MAXWIDTH_DESC);
|
|
|
|
$propbag->add('default', '');
|
|
|
|
break;
|
|
|
|
case 'maxheight':
|
|
|
|
$propbag->add('type', 'string');
|
|
|
|
$propbag->add('name', PLUGIN_EVENT_OEMBED_MAXHEIGHT);
|
|
|
|
$propbag->add('description', PLUGIN_EVENT_OEMBED_MAXHEIGHT_DESC);
|
|
|
|
$propbag->add('default', '');
|
|
|
|
break;
|
2011-12-18 20:27:21 +01:00
|
|
|
case 'generic_service':
|
|
|
|
$propbag->add('type', 'select');
|
|
|
|
$propbag->add('name', PLUGIN_EVENT_OEMBED_GENERIC_SERVICE);
|
|
|
|
$propbag->add('description', PLUGIN_EVENT_OEMBED_GENERIC_SERVICE_DESC);
|
|
|
|
$propbag->add('select_values', $generic_services);
|
|
|
|
$propbag->add('default', 'oohembed');
|
|
|
|
break;
|
|
|
|
case 'embedly_apikey':
|
|
|
|
$propbag->add('type', 'string');
|
|
|
|
$propbag->add('name', PLUGIN_EVENT_OEMBED_EMBEDLY_APIKEY);
|
|
|
|
$propbag->add('description', PLUGIN_EVENT_OEMBED_EMBEDLY_APIKEY_DESC);
|
|
|
|
$propbag->add('default', '');
|
|
|
|
break;
|
|
|
|
case 'supported':
|
|
|
|
$propbag->add('type', 'content');
|
|
|
|
$propbag->add('default', sprintf(PLUGIN_EVENT_OEMBED_SUPPORTED, ProviderList::ul_providernames(true)));
|
|
|
|
break;
|
2011-12-15 01:42:11 +01:00
|
|
|
}
|
2011-12-16 11:44:55 +01:00
|
|
|
return true;
|
2011-12-14 12:18:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function event_hook($event, &$bag, &$eventData) {
|
|
|
|
global $serendipity;
|
2011-12-14 15:29:47 +01:00
|
|
|
|
|
|
|
static $simplePatterns = null;
|
|
|
|
|
|
|
|
if ($simplePatterns==null) {
|
|
|
|
$simplePatterns = array(
|
|
|
|
//'simpleTweet' => '@\(tweet\s+(\S*)\)@Usi',
|
2011-12-18 00:44:57 +01:00
|
|
|
'simpleTweet' => '@\[(?:e|embed)\s+(.*)\]@Usi',
|
2011-12-14 15:29:47 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-12-14 12:18:36 +01:00
|
|
|
$hooks = &$bag->get('event_hooks');
|
|
|
|
|
|
|
|
if (isset($hooks[$event])) {
|
|
|
|
switch($event) {
|
2011-12-14 15:29:47 +01:00
|
|
|
case 'frontend_display':
|
2011-12-16 11:44:55 +01:00
|
|
|
if (isset($eventData['body']) && isset($eventData['extended'])) {
|
|
|
|
$this->update_entry($eventData, $simplePatterns, 'body');
|
|
|
|
$this->update_entry($eventData, $simplePatterns, 'extended');
|
2011-12-14 15:29:47 +01:00
|
|
|
}
|
2011-12-14 12:18:36 +01:00
|
|
|
return true;
|
2011-12-21 11:46:25 +01:00
|
|
|
case 'css':
|
|
|
|
if (strpos($eventData, '.serendipity_oembed')) {
|
|
|
|
// class exists in CSS, so a user has customized it and we don't need default
|
|
|
|
// (doesn't work with templates like BP or 2k11 as the user css is loaded from a seperate file)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
?>
|
|
|
|
.serendipity_oembed_rich, .serendipity_oembed_video, .serendipity_oembed_photo, .serendipity_oembed_link, .serendipity_oembed {
|
|
|
|
max-width: 100%;
|
|
|
|
height: auto;
|
|
|
|
}
|
|
|
|
<?php
|
|
|
|
return true;
|
2011-12-14 12:18:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-12-16 11:44:55 +01:00
|
|
|
function update_entry(&$eventData, &$patterns, $dateType) {
|
|
|
|
if (!empty($eventData[$dateType])) {
|
|
|
|
$eventData[$dateType] = preg_replace_callback(
|
2011-12-14 15:29:47 +01:00
|
|
|
$patterns['simpleTweet'],
|
|
|
|
array( $this, "oembedRewriteCallback"),
|
2011-12-16 11:44:55 +01:00
|
|
|
$eventData[$dateType]);
|
2011-12-14 15:29:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function oembedRewriteCallback($match) {
|
|
|
|
$url = $match[1];
|
2011-12-16 11:44:55 +01:00
|
|
|
$maxwidth = $this->get_config('maxwidth','');
|
|
|
|
$maxheight = $this->get_config('maxheight','');
|
|
|
|
$obj = $this->expand($url, $maxwidth, $maxheight);
|
|
|
|
return OEmbedTemplater::fetchTemplate('oembed.tpl',$obj, $url);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method can be used by other plugins. It will expand an URL to an oembed object (or null if not supported).
|
|
|
|
* @param string $url The url to be expanded
|
|
|
|
* @param string $maxwidth Maximum width of returned object (if service supports this). May be left empty
|
|
|
|
* @param string $maxheight Maximum height of returned object (if service supports this). May be left empty
|
|
|
|
* @return OEmbed or null
|
|
|
|
*/
|
|
|
|
function expand($url, $maxwidth=null, $maxheight=null) {
|
2011-12-15 01:42:11 +01:00
|
|
|
$obj = OEmbedDatabase::load_oembed($url);
|
|
|
|
if (empty($obj)) {
|
2011-12-16 11:44:55 +01:00
|
|
|
$manager = ProviderManager::getInstance($maxwidth,$maxheight);
|
2011-12-15 01:42:11 +01:00
|
|
|
try {
|
|
|
|
$obj=$manager->provide($url,"object");
|
2011-12-18 20:27:21 +01:00
|
|
|
if (!isset($obj)) {
|
|
|
|
$obj = $this->expand_by_general_provider($url,$maxwidth,$maxheight);
|
|
|
|
}
|
2011-12-15 03:13:15 +01:00
|
|
|
if (isset($obj)) {
|
2011-12-15 16:48:50 +01:00
|
|
|
$obj = OEmbedDatabase::save_oembed($url,$obj);
|
2011-12-15 01:42:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (ErrorException $e) {
|
|
|
|
// Timeout in most cases
|
2011-12-15 16:48:50 +01:00
|
|
|
//return $e;
|
2011-12-15 01:42:11 +01:00
|
|
|
}
|
|
|
|
}
|
2011-12-16 11:44:55 +01:00
|
|
|
return $obj;
|
2011-12-15 16:48:50 +01:00
|
|
|
}
|
2011-12-16 11:44:55 +01:00
|
|
|
|
2011-12-18 20:27:21 +01:00
|
|
|
function expand_by_general_provider($url, $maxwidth=null, $maxheight=null) {
|
|
|
|
$provider = $this->get_config('generic_service', 'none');
|
|
|
|
$manager = null;
|
|
|
|
if ('oohembed' == $provider) {
|
|
|
|
require_once dirname(__FILE__) . '/oembed/OohEmbedProvider.class.php';
|
|
|
|
$manager = new OohEmbedProvider($url, $maxwidth, $maxheight);
|
|
|
|
}
|
|
|
|
elseif ('embedly' == $provider) {
|
|
|
|
$apikey = $this->get_config('embedly_apikey', '');
|
|
|
|
if (!empty($apikey)) {
|
|
|
|
require_once dirname(__FILE__) . '/oembed/EmbedlyProvider.class.php';
|
|
|
|
$manager = new EmbedlyProvider($url, $apikey, $maxwidth, $maxheight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($manager)) {
|
|
|
|
try {
|
|
|
|
return $manager->provide($url,'object');
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-15 16:48:50 +01:00
|
|
|
function cleanup_html( $str ) {
|
|
|
|
// Clear unicode stuff
|
|
|
|
$str=str_ireplace("\u003C","<",$str);
|
|
|
|
$str=str_ireplace("\u003E",">",$str);
|
|
|
|
// Clear CDATA Trash.
|
|
|
|
$str = preg_replace("@^<!\[CDATA\[(.*)]]>$@", '$1', $str);
|
|
|
|
$str = preg_replace("@^<!\[CDATA\[(.*)$@", '$1', $str);
|
|
|
|
$str = preg_replace("@(.*)]]>$@", '$1', $str);
|
|
|
|
return $str;
|
2011-12-14 12:18:36 +01:00
|
|
|
}
|
2011-12-15 01:42:11 +01:00
|
|
|
function cleanup() {
|
|
|
|
OEmbedDatabase::install($this);
|
2011-12-16 11:44:55 +01:00
|
|
|
OEmbedDatabase::clear_cache();
|
|
|
|
echo '<div class="serendipityAdminMsgSuccess">Cleared oembed cache.</div>';
|
2011-12-15 01:42:11 +01:00
|
|
|
}
|
|
|
|
function install() {
|
|
|
|
OEmbedDatabase::install($this);
|
|
|
|
}
|
|
|
|
|
2011-12-14 12:18:36 +01:00
|
|
|
}
|