Category Archives: Blog

My own topics

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

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

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

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
Continue reading

Install PHP 5.3.10 with PHP-FPM on CentOS 6.2 64-bit

- Make sure to check Nginx, memcached and MYSQL posts.

- remove old version

#find installed packages
rpm -qa | grep php
#remove php packages
yum remove php php-devel php-cli php-pear php-common php-pdophp-xml php-gd

- download package from PHP.net

mkdir php
cd php
wget http://www.php.net/get/php-5.3.10.tar.bz2/from/us.php.net/mirror
tar jxf php-5.3.10.tar.bz2
cd php-5.3.10
 
#install PHP
./configure  \
	--with-zlib --with-curl --with-curlwrappers --enable-ftp --with-mysql --with-jpeg-dir --with-png-dir --with-gd --enable-gd-native-ttf --with-freetype-dir \
	--enable-zip --with-openssl --with-bz2 --enable-mbstring  \
	--enable-inline-optimization --disable-debug \
	--with-config-file-path=/etc \
	--enable-fpm --with-fpm-user=www --with-fpm-group=www --with-libdir=lib64;
make all install;
 
#add php-fpm service
cp  sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chmod 666 /usr/local/var/run

- Configure php-fpm
Continue reading