Permalink
Browse files

Added checking of duplicate links when submitting

Tags are stripped automatically from article titles
  • Loading branch information...
sanjeevan committed Jul 15, 2010
1 parent 57f73c3 commit 75c61182e60e4576c1f21ef81e2b23a66dabb0d1
@@ -93,7 +93,7 @@
<?php endif; ?>
<?php if ($a->getFlavour() == 'code'): ?>
-<pre class="brush: <?php echo $a->getBrushAlias(); ?>"><?php echo htmlspecialchars($a->getCode()); ?></pre>
+<pre style="display: none;" class="brush: <?php echo $a->getBrushAlias(); ?>"><?php echo htmlspecialchars($a->getCode()); ?></pre>
<?php endif; ?>
<?php if ($a->getFlavour() == 'question'): ?>
@@ -30,6 +30,8 @@
<?php echo htmlspecialchars($article->getCode()); ?>
</pre>
+<p id="code-formatting">Code formatting: <?php echo $article->getCodeLanguage(); ?></p>
+
<script type="text/javascript">
$(document).ready(function(){
SyntaxHighlighter.all();
@@ -40,150 +40,6 @@
@define( 'MARKDOWN_PARSER_CLASS', 'Markdown_Parser' );
-function Markdown($text) {
-#
-# Initialize the parser and return the result of its transform method.
-#
- # Setup static parser variable.
- static $parser;
- if (!isset($parser)) {
- $parser_class = MARKDOWN_PARSER_CLASS;
- $parser = new $parser_class;
- }
-
- # Transform text using parser.
- return $parser->transform($text);
-}
-
-
-### WordPress Plugin Interface ###
-
-/*
-Plugin Name: Markdown
-Plugin URI: http://michelf.com/projects/php-markdown/
-Description: <a href="http://daringfireball.net/projects/markdown/syntax">Markdown syntax</a> allows you to write using an easy-to-read, easy-to-write plain text format. Based on the original Perl version by <a href="http://daringfireball.net/">John Gruber</a>. <a href="http://michelf.com/projects/php-markdown/">More...</a>
-Version: 1.0.1n
-Author: Michel Fortin
-Author URI: http://michelf.com/
-*/
-
-if (isset($wp_version)) {
- # More details about how it works here:
- # <http://michelf.com/weblog/2005/wordpress-text-flow-vs-markdown/>
-
- # Post content and excerpts
- # - Remove WordPress paragraph generator.
- # - Run Markdown on excerpt, then remove all tags.
- # - Add paragraph tag around the excerpt, but remove it for the excerpt rss.
- if (MARKDOWN_WP_POSTS) {
- remove_filter('the_content', 'wpautop');
- remove_filter('the_content_rss', 'wpautop');
- remove_filter('the_excerpt', 'wpautop');
- add_filter('the_content', 'Markdown', 6);
- add_filter('the_content_rss', 'Markdown', 6);
- add_filter('get_the_excerpt', 'Markdown', 6);
- add_filter('get_the_excerpt', 'trim', 7);
- add_filter('the_excerpt', 'mdwp_add_p');
- add_filter('the_excerpt_rss', 'mdwp_strip_p');
-
- remove_filter('content_save_pre', 'balanceTags', 50);
- remove_filter('excerpt_save_pre', 'balanceTags', 50);
- add_filter('the_content', 'balanceTags', 50);
- add_filter('get_the_excerpt', 'balanceTags', 9);
- }
-
- # Comments
- # - Remove WordPress paragraph generator.
- # - Remove WordPress auto-link generator.
- # - Scramble important tags before passing them to the kses filter.
- # - Run Markdown on excerpt then remove paragraph tags.
- if (MARKDOWN_WP_COMMENTS) {
- remove_filter('comment_text', 'wpautop', 30);
- remove_filter('comment_text', 'make_clickable');
- add_filter('pre_comment_content', 'Markdown', 6);
- add_filter('pre_comment_content', 'mdwp_hide_tags', 8);
- add_filter('pre_comment_content', 'mdwp_show_tags', 12);
- add_filter('get_comment_text', 'Markdown', 6);
- add_filter('get_comment_excerpt', 'Markdown', 6);
- add_filter('get_comment_excerpt', 'mdwp_strip_p', 7);
-
- global $mdwp_hidden_tags, $mdwp_placeholders;
- $mdwp_hidden_tags = explode(' ',
- '<p> </p> <pre> </pre> <ol> </ol> <ul> </ul> <li> </li>');
- $mdwp_placeholders = explode(' ', str_rot13(
- 'pEj07ZbbBZ U1kqgh4w4p pre2zmeN6K QTi31t9pre ol0MP1jzJR '.
- 'ML5IjmbRol ulANi1NsGY J7zRLJqPul liA8ctl16T K9nhooUHli'));
- }
-
- function mdwp_add_p($text) {
- if (!preg_match('{^$|^<(p|ul|ol|dl|pre|blockquote)>}i', $text)) {
- $text = '<p>'.$text.'</p>';
- $text = preg_replace('{\n{2,}}', "</p>\n\n<p>", $text);
- }
- return $text;
- }
-
- function mdwp_strip_p($t) { return preg_replace('{</?p>}i', '', $t); }
-
- function mdwp_hide_tags($text) {
- global $mdwp_hidden_tags, $mdwp_placeholders;
- return str_replace($mdwp_hidden_tags, $mdwp_placeholders, $text);
- }
- function mdwp_show_tags($text) {
- global $mdwp_hidden_tags, $mdwp_placeholders;
- return str_replace($mdwp_placeholders, $mdwp_hidden_tags, $text);
- }
-}
-
-
-### bBlog Plugin Info ###
-
-function identify_modifier_markdown() {
- return array(
- 'name' => 'markdown',
- 'type' => 'modifier',
- 'nicename' => 'Markdown',
- 'description' => 'A text-to-HTML conversion tool for web writers',
- 'authors' => 'Michel Fortin and John Gruber',
- 'licence' => 'BSD-like',
- 'version' => MARKDOWN_VERSION,
- 'help' => '<a href="http://daringfireball.net/projects/markdown/syntax">Markdown syntax</a> allows you to write using an easy-to-read, easy-to-write plain text format. Based on the original Perl version by <a href="http://daringfireball.net/">John Gruber</a>. <a href="http://michelf.com/projects/php-markdown/">More...</a>'
- );
-}
-
-
-### Smarty Modifier Interface ###
-
-function smarty_modifier_markdown($text) {
- return Markdown($text);
-}
-
-
-### Textile Compatibility Mode ###
-
-# Rename this file to "classTextile.php" and it can replace Textile everywhere.
-
-if (strcasecmp(substr(__FILE__, -16), "classTextile.php") == 0) {
- # Try to include PHP SmartyPants. Should be in the same directory.
- @include_once 'smartypants.php';
- # Fake Textile class. It calls Markdown instead.
- class Textile {
- function TextileThis($text, $lite='', $encode='') {
- if ($lite == '' && $encode == '') $text = Markdown($text);
- if (function_exists('SmartyPants')) $text = SmartyPants($text);
- return $text;
- }
- # Fake restricted version: restrictions are not supported for now.
- function TextileRestricted($text, $lite='', $noimage='') {
- return $this->TextileThis($text, $lite);
- }
- # Workaround to ensure compatibility with TextPattern 4.0.3.
- function blockLite($text) { return $text; }
- }
-}
-
-
-
#
# Markdown Parser Class
#
@@ -84,7 +84,7 @@ public function save(User $user = null, $article = null)
$article->setSummaryHtml(myUtil::markdown($this->getValue('summary')));
}
- $article->setTitle($this->getValue('title'));
+ $article->setTitle($this->getValueEscaped('title'));
$article->setCode($this->getValue('code'));
$article->setCodeLanguage($this->getValue('language'));
$article->save();
@@ -3,6 +3,20 @@
class ArticleLinkForm extends BaseForm
{
private $job = null;
+
+ /**
+ * True if we're editing an existing article
+ *
+ * @var boolean
+ */
+ private $edit_mode = false;
+
+ /**
+ * Article that this form is editing
+ *
+ * @var Article
+ */
+ private $object = null;
public function setup()
{
@@ -11,15 +25,18 @@ public function setup()
'url' => new sfWidgetFormInputText(),
'summary' => new sfWidgetFormTextarea(),
'images' => new sfWidgetFormInputCheckbox(array('value_attribute_value' => 1)),
- //'captcha' => new sfWidgetFormReCaptcha(array('public_key' => sfConfig::get('app_recaptcha_public'))),
'type' => new sfWidgetFormInputHidden()
));
+
+ $url_validator = new sfValidatorAnd(array(
+ new sfValidatorUrl(),
+ new sfValidatorCallback(array('callback' => array($this, 'uniqueUrl')))
+ ), array('required' => true));
$this->setValidators(array(
'title' => new sfValidatorString(array('required' => true, 'max_length' => 255, 'min_length' => 5)),
- 'url' => new sfValidatorUrl(array('required' => true)),
+ 'url' => $url_validator,
'summary' => new sfValidatorString(array('required' => false)),
- //'captcha' => new sfValidatorReCaptcha(array('private_key' => sfConfig::get('app_recaptcha_private'))),
'type' => new sfValidatorString(array('required' => true)),
'images' => new sfValidatorInteger(array('required' => false))
));
@@ -35,20 +52,65 @@ public function setup()
$this->widgetSchema->setFormFormatterName('list');
$this->widgetSchema->setNameFormat('article[%s]');
- /*
- if (sfContext::getInstance()->getUser()->isAdmin()){
- unset($this['captcha']);
- }
- */
+
parent::setup();
}
+ /**
+ * If set to true, we remvoe fetching of thumbnails
+ *
+ */
public function setEditMode()
{
unset($this['images']);
+ $this->edit_mode = true;
+ }
+
+ /**
+ * Set the object we're editing
+ *
+ * @param Article $article
+ */
+ public function setObject(Article $article)
+ {
+ $this->object = $article;
}
+ /**
+ * Checks if the passed value which is a URL is unique
+ *
+ * @param sfValidator $validator
+ * @param mixed $value
+ */
+ public function uniqueUrl($validator, $value)
+ {
+ $q = Doctrine_Query::create()
+ ->select('a.*')
+ ->from('Article a')
+ ->where('a.url = ?', $value)
+ ->limit(1);
+
+ if ($this->object instanceof Article){
+ $q->andWhere('a.id != ?', $this->object->getId());
+ }
+
+ $article = $q->fetchOne();
+
+ if ($article){
+ $title = htmlspecialchars($article->getTitle());
+ $link = "<a target='_blank' href='/{$article->getFlavour()}/{$article->getSlug()}'>{$title}<a/>";
+ throw new sfValidatorError($validator, "That url has already been submitted, see: $link");
+ }
+
+ return $value;
+ }
+ /**
+ * Create form, and populate values from specified models
+ *
+ * @param Article $article
+ * @return ArticleLinkForm
+ */
public static function fromModel(Article $article)
{
$defaults = array(
@@ -58,7 +120,10 @@ public static function fromModel(Article $article)
'type' => 'link'
);
- return new ArticleLinkForm($defaults);
+ $form = new ArticleLinkForm($defaults);
+ $form->setObject($article);
+
+ return $form;
}
public function save(User $user = null, $article = null)
@@ -69,7 +134,7 @@ public function save(User $user = null, $article = null)
$article->setUserId($user->getId());
$article->setUsername($user->getUsername());
- $article->setTitle($this->getValue('title'));
+ $article->setTitle($this->getValueEscaped('title'));
$article->setUrl($this->getValue('url'));
if (strlen($this->getValue('summary')) > 0){
@@ -93,6 +158,11 @@ public function save(User $user = null, $article = null)
return $article;
}
+ /**
+ * Get job information for fetching thumbnails
+ *
+ * @return array
+ */
public function getJob()
{
return $this->job;
@@ -45,7 +45,7 @@ public function save(User $user, $article = null)
$article->setUserId($user->getId());
$article->setUsername($user->getUsername());
- $article->setTitle($this->getValue('title'));
+ $article->setTitle($this->getValueEscaped('title'));
$article->setQuestion($this->getValue('question'));
$article->setQuestionHtml(myUtil::markdown($this->getValue('question')));
$article->setFlavour('question');
@@ -95,7 +95,7 @@ public function save(User $user = null, $article = null)
$article->setUserId($user->getId());
$article->setUsername($user->getUsername());
- $article->setTitle($this->getValue('title'));
+ $article->setTitle($this->getValueEscaped('title'));
$article->setFlavour('snapshot');
$article->setHasThumbnails(false);
@@ -16,4 +16,9 @@ public function setup()
parent::setup();
}
+
+ public function getValueEscaped($field)
+ {
+ return strip_tags($this->getValue($field));
+ }
}
View
@@ -654,9 +654,6 @@ img.photo {
/* end of */
-
-
-
div#add-selection {
margin: 0 0 20px 0;
color: #666;

0 comments on commit 75c6118

Please sign in to comment.