Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Magic 1.0. Volunteer help to support RC1. Much Appreciated!

edited May 2010 in Vanilla 2.0 - 2.8
Anyone care to help make this addon work for RC1? As it stands, this plugin only works when you preview a message. When you attempt to post it, then only the url link shows. Any help would be greatly appreciated! The plugin works when you replace the regular default.php with this default.php located in the plugins/vanillaindiscussion

Note: Code has been modified to view here without being crippled as much.

Heres the default.php code for the plugin:
<?php if(!defined('APPLICATION')) die();

$PluginInfo['Magic'] = array(
'Name' => 'Magic',
'Description' => 'Vyhledává v komentářích diskuse odkazy, smajlíky, atp. a zobrazí je graficky.',
'Version' => '1.0',
'Author' => "Michal Toman",
'AuthorEmail' => "toman.michal@gmail.com",
'RequiredApplications' => array('Vanilla' => '>=2.0'),
);

class Magic implements Gdn_IPlugin {

private $Comment = '';
private $RegularImg = '/((http|ftp)\:\/\/)?([A-Za-z0-9\-\_\.]+)([\/\-\_A-Za-z0-9]+)?(\.(jpg|JPG|jpeg|JPEG|png|PNG|gif|GIF))/';
private $RegularYoutube = '/((http|ftp)\:\/\/)?([w]{3}\.)?(youtube\.)([a-z]{2,4})(\/watch\?v=)([a-zA-Z0-9_-]+)(\&feature=)?([a-zA-Z0-9_-]+)?/';

/**
* IsValueAttribute
* @ param String $Comment
* @ param String $Url
* @ return Boolean
*/
protected function IsValueAttribute($Comment, $Url) {
if (ereg("=(\"|\')".$Url."(\"|\')", $Comment)) {
return true;
} else {
return false;
}
}

/**
* ViewImages
* @ param Array $Matches
* @ return String
*/
protected function ViewImages($Matches) {
if($this->IsValueAttribute($this->Comment, $Matches[0])) {
return $Matches[0];
} else {
if(strlen($Matches[1]) == 0) {
$Matches[0] = "http://&quot; . $Matches[0];
}

return "<</ a href=\" . $Matches[0] . "\" target=\"_blank\">
<< / img alt=\"Image\" width=\"450\" border=\"0\" src=\" . $Matches[0] . "\" />
</a>";
}
}

/**
* ViewYoutube
* @ param Array $Matches
* @ return String
*/
protected function ViewYoutube($Matches) {
if($this->IsValueAttribute($this->Comment, $Matches[0])) {
return $Matches[0];
} else {
return "<object width=\"425\" height=\"344\">
<param name=\"movie\" value=\"http://www.youtube.&quot; . $Matches[5] . "/v/" . $Matches[7] . "&fs=1&rel=0\"></param>
<param name=\"allowFullScreen\" value=\"true\">
</param><param name=\"allowscriptaccess\" value=\"always\"></param>
<embed src=\"http://www.youtube.&quot; . $Matches[5] . "/v/" . $Matches[7] . "&hl=fs=1&rel=0\"
type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\"
width=\"425\" height=\"344\"></embed>
</object>";
}
}

/**
* MakeView
* @ param String $Comment
* @ return String
*/
protected function MakeView($Comment) {
$this->Comment = $Comment;
$Comment = preg_replace_callback($this->RegularImg, array($this, 'ViewImages'), $Comment);
$this->Comment = $Comment;
$Comment = preg_replace_callback($this->RegularYoutube, array($this, 'ViewYoutube'), $Comment);

return $Comment;
}

/**
* DiscussionController_BeforeCommentBody_Handler
* @ param DiscussionController $Sender
*/
public function DiscussionController_BeforeCommentBody_Handler(&$Sender) {
$Sender->EventArguments['Comment']->Body = $this->MakeView($Sender->EventArguments['Comment']->Body);
}

/**
* PostController_BeforeCommentBody_Handler
* @ param PostController $Sender
*/
public function PostController_BeforeCommentBody_Handler(&$Sender) {
$Sender->EventArguments['Comment']->Body = $this->MakeView($Sender->EventArguments['Comment']->Body);
}

/**
* PostController_BeforeDiscussionRender_Handler
* @ param PostController $Sender
*/
public function PostController_BeforeDiscussionRender_Handler(&$Sender) {
if ($Sender->View == 'preview') {
$Sender->Comment->Body = $this->MakeView($Sender->Comment->Body);
}
}

/**
* PostController_BeforeCommentRender_Handler
* @ param PostController $Sender
*/
public function PostController_BeforeCommentRender_Handler(&$Sender) {
if ($Sender->View == 'preview') {
$Sender->Comment->Body = $this->MakeView($Sender->Comment->Body);
}
}

/**
* Setup
*/
public function Setup() {
}
}
?>

Comments

  • ZeFishZeFish New
    edited May 2010
    Don't know if it can help cause.... well... mine dosen't work either ;) but I modified your plugin to support oembed functionality so it support well... almost every service "flickr, youtoube, vimeo..."

    by the way... how do we write code in the comment?
    <?php if(!defined('APPLICATION')) die();

    $PluginInfo['oEmbed'] = array(
    'Name' => 'oEmbed for Vanilla',
    'Description' => 'Automaticly change media url to current video/image tag. Greatly inspired by the plugin "Magic" from Michal Toman',
    'Version' => '1.0',
    'Author' => "Francis Fontaine",
    'AuthorEmail' => "francisfontaine@gmail.com",
    'AuthorUrl' => 'http://www.francisfontaine.com'
    );

    class oEmbedPlugin extends Gdn_Plugin {

    private $Comment = '';
    private $RegularURL = "#((http|https|ftp)://(\S*?\.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|\"|'|:|\<|$|\.\s)#ie";

    public static function processURL($url) {
    $url = $url[0];
    $url_temp = str_replace("/", "", $url);
    $url_temp = expode(".", $url_temp);
    $ext = array_pop($url_temp);
    if(in_array($ext, array("jpeg","jpg","gif","png","bmp"))) {
    return "<a href=\"".$url."\" target=\"_blank\"><img alt=\"Image\" width=\"450\" border=\"0\" src=\"".$url."\" /></a>";
    } else {
    return $this->getOEmbed($url);
    }

    }

    public function curl_get($url) {
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    $return = curl_exec($curl);
    curl_close($curl);
    return $return;
    }

    public function getOEmbed($url=null) {
    if($url == null) return;
    $params = array("maxWidth" => 450);

    $oEmbedUrl = null;
    $services = array(
    "vimeo.com" => "http://www.vimeo.com/api/oembed.json?url=",
    "youtube.com" => "http://www.youtube.com/oembed?url=",
    "flickr.com/photos" => "http://www.flickr.com/services/oembed/?url=&quot;
    );
    foreach($services as $servicesName => $servicesUrl) {
    if(strpos($url, $servicesName) !== false) {
    $oEmbedUrl = $servicesUrl.$url;
    }
    }
    if($oEmbedUrl == null) {
    $data = $this->curl_get($url);
    $pattern = "/type=\"application\/json\+oembed\" href=[\\\"']?(.[^\\\"']*)[\\\"']?/i";
    preg_match_all($pattern, $data, $oembedLinks);
    if(isset($oembedLinks[1][0])) $oEmbedUrl = $oembedLinks[1][0];
    }
    if($oEmbedUrl == null) {
    $oEmbedUrl = "http://oohembed.com/oohembed/?url=".$url;
    }
    $param = "";
    foreach($params as $key => $value) $param .= "&".$key."=".$value;
    $ret = json_decode($this->curl_get($oEmbedUrl.$param."&format=json"));
    if($ret->type == "video") {
    return $ret->html;
    } else {
    return "<a href=\"".$url."\" target=\"_blank\"><img src=\"".$ret->url."\" width=\"".$ret->width."\" height=\"".$ret->height."\" /></a>";
    }

    }

    public function MakeView($Comment) {
    $this->Comment = $Comment;
    $Comment = preg_replace_callback($this->RegularURL, array(&$this, 'processURL'), $this->Comment);
    if($Comment == null) $Comment = $this->Comment;

    return $Comment;
    }

    public function DiscussionController_BeforeDiscussionRender_Handler(&$Sender) {
    $Sender->Discussion->Body = $this->MakeView($Sender->Discussion->Body);
    }

    public function PostController_BeforeCommentBody_Handler(&$Sender) {
    $Sender->Discussion->Body = $this->MakeView($Sender->Discussion->Body);
    }

    public function PostController_BeforeDiscussionRender_Handler(&$Sender) {
    if ($Sender->View == 'preview') {
    $Sender->Comment->Body = $this->MakeView($Sender->Comment->Body);
    }
    }

    public function PostController_BeforeCommentRender_Handler(&$Sender) {
    if ($Sender->View == 'preview') {
    $Sender->Comment->Body = $this->MakeView($Sender->Comment->Body);
    }
    }
    /**
    * Setup
    */
    public function Setup() {
    }
    }
    ?>
  • edited May 2010
    Hey there!

    I will apply your code and see what happens. Thanks!

    For the record, it is not I who made this plugin. All credit goes to @bas87

    However, I have tried to email him about 3 times already within the past 2 weeks and he is not anywhere to be seen. Not seen here or in email. So i wanted help from the community to see how to make this work is RC1.

    I dont think it will take so much to implement this however as again the preview works :)

    btw, you can write code in a comment by writing [/code]code right here![/code]

    replace [ ] with < >

    cheers,
    Tim
  • edited May 2010
    .
  • Great! I'm just waiting for that kind of plugin to put my brand new vanilla rc1 online :)
  • yes indeed I am too! lets just hope someone else really needs it too lol
  • StashStash
    edited May 2010
    I don't have the skill or the time to look into this, I can only say I would be interested in this working as well. And currently, on this site, this works best for code:
    <pre>&lt;?php /* code goes here */ ?&gt;</pre>
    It's best to convert the code you want to paste to entities and then stuff it in PRE tags, see...
    <?php /* code goes here */ ?>
  • thanks :)
Sign In or Register to comment.