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

// For adding custom messages if using blueprint
 
// In reponses/badrequest.js
// after 
//  if (sails.config.environment === 'production') {
//    data = undefined;
//  }
// add
  if (data.invalidAttributes !== undefined) {
    var attributes = data.invalidAttributes;
    _.each(attributes, function (attribute, key) {
      _.each(attribute, function (properties) {
        var rule = properties.rule;
        var message = false;
        try {
          message = sails.__('validation.' + key + '.' + rule, key);
        }
        catch (e) {
        }
 
        try {
          message = sails.__('validation.' + rule, key);
        }
        catch (e) {
        }
 
        if (message && message != 'validation.' + rule && message != 'validation.' + key + '.' + rule) {
          properties.message = message;
        }
      });
    });
  }
 
 
 
  // In config/i18n.js 
  //add
  objectNotation: true
 
  // example :
  //In config/locales/en.json or your locale file
  //add
    "validation": {
      "isJson": "invalid JSON format in %s"
    }

Sitemap Creator v1.0

Sitemap Creator is a PHP class which creates XML sitemaps files compatible with the standard sitemaps.org protocol supported by Google and Bing.

    Features

  • Uses PHPCrawl class to crawl/spider the website and creates URLs set while all PHPCrawl methods and options are accessible through class.
  • Ability to calculate Priority, Frequency and Last-Modified date with variety of options.
  • Creates sitemaps in gzip format or uncompressed XML.
  • Pings search engines with sitemaps locations.
  • Reads from CSV files and exports entries in CSV format.

Documentations
Download
Example

Use mysqldump to create separate files and directories for databases and tables

The script creates separate directory for every database and bz2 files for every table inside that database.

#!/bin/sh
 
#edit these
USER=""
PASSWORD=""
MYSQLDIR="/path/to/backupdir"
 
MYSQLDUMP="/usr/bin/mysqldump"
MYSQL="/usr/bin/mysql"
 
echo - Dumping DB structure "$MYSQLDIR"/all.bz2 
$MYSQLDUMP --user=$USER --password=$PASSWORD -d --all-databases | bzip2 > "$MYSQLDIR"/all.bz2 
 
echo - Dumping tables for each DB
databases=`$MYSQL --user=$USER --password=$PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema)"`
for db in $databases; do
    echo - Creating "$db" DB
	mkdir $MYSQLDIR/$db
	chmod -R 777 $MYSQLDIR/$db
	for tb in `$MYSQL  --user=$USER --password=$PASSWORD -N -B -e "use $db ;show tables"`
		do 
			echo -- Creating table $tb
			$MYSQLDUMP --opt  --delayed-insert --insert-ignore --user=$USER --password=$PASSWORD $db $tb | bzip2 -c > $MYSQLDIR/$db/$tb.sql.bz2
	done
	echo
done

Nginx Error Log Reader

Nginx Error Log Reader is a php reader/parser/analyzer for Nginx error log file. the script is able to read error logs recursively then display them in a user friendly table. Script configuration includes the number of bytes to read per page and allow pagination through the error log . Additionally, table columns are sortable and full description of every error is displayed using MonnaTip.

For banning Ips, please refer to this post Using iptables to block ips that spam or attack your server

Continue reading

Add full previews to your twitter feed on WordPress using Ozh’ Tweet Archiver

Ozh’ Tweet Archiver is a WordPress plugin that can archive your tweets on your own blog; it simply creates a new post for each tweet. Additionally, it offers converting hashtags to WordPress tags, and several other features. Nevertheless, one feature, I thought might be interesting, is to process the links on tweets and show them as a Facebook feed. Recently, I modified the code and it worked very well on my blog: Gadelkareem Tweets.

- All we need is to modify the “wp-content/plugins/ozh-tweet-archiver/inc/import.php” file as following: Continue reading

Limit requests per IP on Nginx using HttpLimitZoneModule and HttpLimitReqModule except whitelist

- Make sure to check Nginx, PHP posts for information on Nginx and PHP setup and configuration.

Nginx offers two modules, HttpLimitReqModule and HttpLimitZoneModule, to limit simultaneous connections for the assigned session and the number of requests for a given session from one IP address. Basically these modules are built to protect the web server from possible DDos attacks; For example, this configuration limits remote clients to no more than 20 concurrently “open” connections per remote ip address:

http{
    limit_zone limit_per_ip $binary_remote_addr 5m;
    limit_conn  limit_per_ip  20;

Continue reading

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().
Continue reading

Watch log files on Linux server with Swatch 3.2.3

- Make sure to check CentOS, PHP-FPM, memcached and MYSQL posts.

- get a fresh package from Swatch project

wget "http://downloads.sourceforge.net/project/swatch/swatch/3.2.3/swatch-3.2.3.tar.gz?r=http%3A%2F%2Fsourceforge.net%2Fprojects%2Fswatch%2F&ts=1330806322&use_mirror=garr"
tar xfz swatch-3.2.3.tar.gz 
cd swatch-3.2.3
cpan -i Date::Format File::Tail
cpan -i Date::Manip Date::Calc
cpan -i Test::Inter Test::Pod Test::Pod::Coverage Module::Build
cp /root/.cpan/sources/authors/id/S/SB/SBECK/Date-Manip-* .
tar xfz Date-Manip-*.tar.gz
cd Date-Manip-*
perl Build.PL 
./Build install
cd ..
perl Makefile.PL
make install

- configure swatch
Continue reading

Install Nginx 1.0.12 for PHP-FPM on CentOS 6.2

- Make sure to check PHP-FPM, memcached and MYSQL posts.
- get the latest package from wiki.nginx.org

mkdir nginx
cd nginx
wget http://nginx.org/download/nginx-1.0.12.tar.gz
tar xfz nginx-1.0.12.tar.gz 
cd nginx-1.0.12
 
#Change server string
vi src/http/ngx_http_header_filter_module.c
#change
static char ngx_http_server_string[] = "Server: example.com" CRLF;
static char ngx_http_server_full_string[] = "Server: example.com" CRLF;
 
#change
vi src/http/ngx_http_special_response.c
# replace '"<hr /><center>nginx</center>" CRLF' and '"<hr /><center>" NGINX_VER "</center>" CRLF'
"<hr /><center>example.com</center>" CRLF
 
#create tmp dirs
mkdir /server/tmp /server/tmp/nginx /server/tmp/nginx/client /server/tmp/nginx/fcgi /server/tmp/nginx/proxy
chmod -R 777 /server/tmp

-install Nginx
Continue reading