Skip to content

Commit

Permalink
v0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasott committed Jun 7, 2016
1 parent 7d24438 commit d1fc337
Show file tree
Hide file tree
Showing 8 changed files with 419 additions and 53 deletions.
23 changes: 16 additions & 7 deletions webmention/controllers/Webmention_WebmentionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class Webmention_WebmentionController extends BaseController
* @param string $src
* @return string
*/
public function checkResponseType(&$result, $src) {
public function checkResponseType(&$result, $entry, $src, $useBridgy) {

/* Check for brid.gy first */
if(!empty($src) and (preg_match('!http(.*?)://brid-gy.appspot.com!', $src) or preg_match('!http(.*?)://brid.gy!', $src))) {
if(!empty($src) and ($useBridgy == true) and (preg_match('!http(.*?)://brid-gy.appspot.com!', $src) or preg_match('!http(.*?)://brid.gy!', $src))) {

/* Is it Twitter? */
if(!empty($result['url']) and preg_match('!http(.*?)://twitter.com/(.*?)/status!', $result['url'])) {
Expand Down Expand Up @@ -59,8 +59,14 @@ public function checkResponseType(&$result, $src) {
if(preg_match('/rsvp/', $src)) {
$result['type'] = 'rsvp';
}
} else {
if (isset($entry[properties][like-of]) || isset($entry[properties][like])) {
$result['type'] = 'like';
}
if (isset($entry[properties][repost-of]) || isset($entry[properties][repost])) {
$result['type'] = 'repost';
}
}

}

/**
Expand Down Expand Up @@ -204,6 +210,10 @@ public function actionHandleWebmention()
$html = preg_replace('~(?!.*;$)&#x([0-9a-fA-F]+)~i', "&#x\\1;", $html);
$html = html_entity_decode($html, ENT_QUOTES, "utf-8");

/* HTMLPurifier doesn't know HTML5 tags, so we'll replace the structural tags (http://developers.whatwg.org/sections.html) with div tags
This is a working workaround :) */
$html = preg_replace('/(<|\/)(section|article|nav|aside|hgroup|header|footer|address)(\s|>)/i', '$1div$3', $html);

/* Purify HTML with Yii's HTMLPurifier wrapper */
$purifier = new \CHtmlPurifier();
$purifier->options = array('URI.AllowedSchemes'=>array(
Expand All @@ -217,7 +227,6 @@ public function actionHandleWebmention()

/* Let's look up where the h-entry is and use this array */
foreach($parsed['items'] as $item){

if ( in_array('h-entry', $item['type']) || in_array('p-entry', $item['type'])) {
$entry = $item;
}
Expand All @@ -231,9 +240,9 @@ public function actionHandleWebmention()
throw new Exception('Probably spam');
}

if ($settings->useBridgy){
$this->checkResponseType($result, $src);
}
/* Determine the type of the repsonse */
$this->checkResponseType($result, $entry, $src, $settings->useBridgy);

if (function_exists('http_response_code')) {
http_response_code(202);
}
Expand Down
27 changes: 27 additions & 0 deletions webmention/elementactions/Webmention_DeleteElementAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace Craft;

/**
* Webmention Delete Element Action
*
*/
class Webmention_DeleteElementAction extends DeleteElementAction
{

/**
* @inheritDoc IElementAction::performAction()
*
* @param ElementCriteriaModel $criteria
*
* @return bool
*/
public function performAction(ElementCriteriaModel $criteria)
{
craft()->webmention->deleteWebmentions($criteria->ids());

$this->setMessage(Craft::t('Wementions deleted.'));

return true;
}

}
268 changes: 268 additions & 0 deletions webmention/elementtypes/Webmention_WebmentionElementType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
<?php
namespace Craft;

/**
* Webmention - Webmention element type
*/
class Webmention_WebmentionElementType extends BaseElementType
{
/**
* Returns the element type name.
*
* @return string
*/
public function getName()
{
return Craft::t('Webmention');
}

/**
* Returns whether this element type has content.
*
* @return bool
*/
public function hasContent()
{
return false;
}

/**
* Returns whether this element type has titles.
*
* @return bool
*/
public function hasTitles()
{
return false;
}

/**
* Returns this element type's sources.
*
* @param string|null $context
* @return array|false
*/
public function getSources($context = null)
{
$sources = array(
'*' => array(
'label' => Craft::t('All Webmentions'),
)
);

return $sources;
}

/**
* Returns the attributes that can be shown/sorted by in table views.
*
* @param string|null $source
* @return array
*/
public function defineAvailableTableAttributes($source = null)
{
return array(
'id' => Craft::t('ID'),
'author_name' => Craft::t('Author'),
'text' => Craft::t('Text'),
'target' => Craft::t('Target'),
'type' => Craft::t('Type'),
'dateUpdated' => Craft::t('Date'),
);
}

public function defineSortableAttributes()
{
return array(
'id' => Craft::t('ID'),
'author_name' => Craft::t('Author'),
'text' => Craft::t('Text'),
'target' => Craft::t('Target'),
'type' => Craft::t('Type'),
'dateUpdated' => Craft::t('Date'),
);
}

/**
* Returns the table view HTML for a given attribute.
*
* @param BaseElementModel $element
* @param string $attribute
* @return string
*/
public function getTableAttributeHtml(BaseElementModel $element, $attribute)
{
switch ($attribute)
{
case 'author_name':
{
return '<strong>'.$element->$attribute.'</strong>';
}
case 'text':
{
return $element->$attribute;
}
case 'dateUpdated':
{
$date = $element->$attribute;
if ($date)
{
return $date->localeDate();
}
else
{
return '';
}
}
case 'target':
{
return '<a href="' . $element->$attribute . '">'. $element->$attribute .'</a>';
}
default:
{
return parent::getTableAttributeHtml($element, $attribute);
}
}
}

/**
* Defines any custom element criteria attributes for this element type.
*
* @return array
*/
public function defineCriteriaAttributes()
{
return array(
'author_photo' => AttributeType::Url,
'author_name' => AttributeType::Mixed,
'author_url' => AttributeType::Url,
'published' => AttributeType::DateTime,
'name' => AttributeType::Mixed,
'text' => AttributeType::Mixed,
'target' => AttributeType::Url,
'source' => AttributeType::Url,
'url' => AttributeType::Url,
'site' => AttributeType::Mixed,
'type' => AttributeType::Mixed,
'rsvp' => AttributeType::Mixed,
'order' => array(AttributeType::String, 'default' => 'webmention_webmention.dateUpdated desc'),
);
}

/**
* Modifies an element query targeting elements of this type.
*
* @param DbCommand $query
* @param ElementCriteriaModel $criteria
* @return mixed
*/
public function modifyElementsQuery(DbCommand $query, ElementCriteriaModel $criteria)
{
$query
->addSelect(' webmention_webmention.id,
webmention_webmention.author_name,
webmention_webmention.author_photo,
webmention_webmention.author_url,
webmention_webmention.published,
webmention_webmention.name,
webmention_webmention.text,
webmention_webmention.target,
webmention_webmention.source,
webmention_webmention.url,
webmention_webmention.site,
webmention_webmention.type,
webmention_webmention.rsvp')
->join('webmention_webmention webmention_webmention', 'webmention_webmention.id = elements.id');

if ($criteria->author_name)
{
$query->andWhere(DbHelper::parseParam('webmention_webmention.author_name', $criteria->author_name, $query->params));
}

if ($criteria->author_photo)
{
$query->andWhere(DbHelper::parseParam('webmention_webmention.author_photo', $criteria->author_photo, $query->params));
}

if ($criteria->published)
{
$query->andWhere(DbHelper::parseParam('webmention_webmention.published', $criteria->published, $query->params));
}

if ($criteria->name)
{
$query->andWhere(DbHelper::parseParam('webmention_webmention.name', $criteria->name, $query->params));
}

if ($criteria->text)
{
$query->andWhere(DbHelper::parseParam('webmention_webmention.text', $criteria->text, $query->params));
}

if ($criteria->target)
{
$query->andWhere(DbHelper::parseParam('webmention_webmention.target', $criteria->target, $query->params));
}

if ($criteria->source)
{
$query->andWhere(DbHelper::parseParam('webmention_webmention.source', $criteria->source, $query->params));
}

if ($criteria->url)
{
$query->andWhere(DbHelper::parseParam('webmention_webmention.url', $criteria->url, $query->params));
}

if ($criteria->site)
{
$query->andWhere(DbHelper::parseParam('webmention_webmention.site', $criteria->site, $query->params));
}

if ($criteria->type)
{
$query->andWhere(DbHelper::parseParam('webmention_webmention.type', $criteria->type, $query->params));
}

if ($criteria->rsvp)
{
$query->andWhere(DbHelper::parseParam('webmention_webmention.rsvp', $criteria->rsvp, $query->params));
}

}

/**
* Populates an element model based on a query result.
*
* @param array $row
* @return array
*/
public function populateElementModel($row)
{
return Webmention_WebmentionModel::populateModel($row);
}

/**
* Returns the HTML for an editor HUD for the given element.
*
* @param BaseElementModel $element
* @return string
*/
public function getEditorHtml(BaseElementModel $element)
{
// Start/End Dates
// $html = craft()->templates->render('events/_edit', array(
// 'element' => $element,
// ));

// Everything else
$html .= parent::getEditorHtml($element);

return $html;
}

public function getAvailableActions($source = null)
{
return array('Webmention_Delete');
}
}
Loading

0 comments on commit d1fc337

Please sign in to comment.