Topics

function

Custom validation messages for sails js

//in api/models/User.js
function validationError(invalidAttributes, status, message) {
  var WLValidationError = require('../../node_modules/sails/node_modules/waterline/lib/waterline/error/WLValidationError.js');
  return new WLValidationError({
      invalidAttributes: invalidAttributes,
      status: status,
      message: message
    }
  );
}
var User = {
  attributes: {
    //...
  },
  ownValidate:: function (values, update, cb) {
    //example of not allowed param on update
    //if it is an update then do not allow email param
    if (update && values.email) {
      return cb(validationError({
        email: [
          {
            message: 'Email is not allowed for updates.'
          }
        ]
      }, 400 /*status*/));
    }
    sails.models['user'].findOne(values.email).exec(function (err, user) {
      if (err) return cb(err);
      if (user) {
        return cb(validationError({
          email: [
            {
              value: values.email,
              rule: 'E_UNIQUE'
              /* unique validation message is left for the default one here */
            }
          ]
        }, 409));
      }
    });
  },
  beforeCreate: function (values, cb) {
    return sails.models['user'].ownValidate(values, false, cb);
  },
  beforeUpdate: function (values, cb) {
    return sails.models['user'].ownValidate(values, true, cb);
  }
}

For blueprint custom messages validation

Disqus problem with wordpress “unable to connect to the disqus api servers”

I recently installed Disqus plugin for WordPress; however, I have been receiving this message “unable to connect to the disqus api servers.” By looking into the source code, it looks like curl is the problem! more specifically the _dsq_curl_urlopen() function, but I did not investigate this error further. As fortunately, we can easily switch to the alternative function _dsq_fsockopen_urlopen().

install memcached 1.4.13 on CentOS 6.2

– Make sure to check PHP-FPM and MYSQL posts.

– One of the dependencies of memcache is libevent, get latest version from libevent.org

mkdir libevent
cd libevent
wget https://github.com/downloads/libevent/libevent/libevent-2.0.17-stable.tar.gz
tar xfz libevent-2.0.17-stable.tar.gz
cd libevent-2.0.17-stable

– get latest version from memcached.org

Ban IP from logging in for 5 minutes after 10 failed logins


if( login_limit() )
   die( 'Your IP has been banned from logging in for the next 5 minutes' );



/*
 * Counts login times by same IP 
 * returns true if limit reached or false if not 
 */
function login_limit(){
    //get real IP if user behind proxy noted by Sebastian Enger
    $ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
    
    //user still banned from login
    if( _cache( $ip . 'banned' ) )
        return true;

    //number of seconds 
    $sec = 30;
    //find ip info array in cache saved less than $sec ago
    if( ($ip_info=_cache( $ip ))  && $ip_info[0] > time()-$sec ){
        
        //user login 10 times during last $sec 
        if(  $ip_info[1] > 10 ){
            //ban user ip for the next 5 minutes
            _cache( $ip . 'banned', 1, 0, 60*5 );
            _cache( $ip, -1 );
            return true;
        }
        //increase login retries +1 
        _cache( $ip, array(  $ip_info[0],   ++$ip_info[1] ), 0, $sec );
        return false;
        
    }
        
    //add ip info to cache
    _cache( $ip, array( time(), 1 ), 0, $sec );
    return false;
}

function _cache( $name, $val=NULL, $ttl=false ){
        //memcached
	global $mcdb;
	if(empty($mcdb) ) 
		$mcdb = memcache_connect('unix:///etc/sockets/memcached.sock', 0);
	
	if($val === -1){
		return memcache_delete($mcdb,$name);
	}elseif( $val !== NULL ){
		return memcache_set($mcdb,$name,$val, false, $ttl);
	}else{
		$retval = memcache_get($mcdb,$name);
		return  $retval ? $retval : NULL;
	}

}

jQuery plugin : monnaTip 0.1

This the simplest tooltip jQuery plugin you can find around.

    features:
  • takes advantage of live(), mouseenter and mouseleave methods
  • tracks mouse movement
  • right and bottom viewport border tracking
  • can be applied on any element

DemodownloadGoogle codeGitHub