Thứ Sáu, 30 tháng 8, 2013

download image from url and save to file java


    public static void downloadImage(String imageUrl, String savePath) throws   MalformedURLException, IOException {
        Connection conn = Jsoup.connect(imageUrl);
        conn = conn.timeout(10000).ignoreContentType(true);
        Connection.Response resultImageResponse = conn.execute();
        FileOutputStream out = (new FileOutputStream(new java.io.File(savePath)));
        out.write(resultImageResponse.bodyAsBytes());          
        out.close();
    }

Thứ Tư, 28 tháng 8, 2013

tinyMCE init /paste event /set content event/ get content event

tinyMCE.init({
        selector: "textarea#content",
        language: "vi",
        theme: "advanced",
        skin: "ugceditor",
        skin_variant: "m3_1.02",
        plugins: "media,paste",
        width: 715,
        height: 650,
        theme_advanced_buttons1: "bold,italic,underline,fontselect,fontsizeselect,forecolor,backcolor,|,bullist,numlist,outdent,indent,|,justifyleft,justifycenter,justifyright,justifyfull,|,link,unlink, zcode",
        theme_advanced_buttons2: "",
        theme_advanced_buttons3: "",
        theme_advanced_toolbar_location: "top",
        theme_advanced_toolbar_align: "left",
        theme_advanced_statusbar_location: "",
        theme_advanced_path: false,
        theme_advanced_resize_horizontal: false,
        theme_advanced_resizing_max_width: 720,
        theme_advanced_resizing_min_width: 720,
        theme_advanced_resizing_min_height: 650,
        paste_auto_cleanup_on_paste: true,
        paste_remove_spans: true,
        paste_remove_styles: true,
        relative_urls : false,
        tab_focus: ":prev,:next",
        entity_encoding: "raw",
        media_strict: false,
        forced_root_block: 'div',        
        content_css: a + "/skins/ugceditor/content-1.04.css",
        debug: false,
        insertPhotos: {},
        force_br_newlines: true,
        force_p_newlines: false,
        setup: function (b) {
            //            b.addButton("zemotions", {
            //                title: "Chèn biểu tượng",
            //                "class": "mce_emotions",
            //                onclick: blog.Emotion
            //            });
            //            b.addButton("zimage", {
            //                title: "Chèn hình ảnh",
            //                "class": "mce_image",
            //                onclick: blog.UploadImageNormal2
            //            });
            //            b.addButton("zmusic", {
            //                title: "Chèn nhạc",
            //                "class": "mce_music",
            //                onclick: blog.SearchMusic2
            //            });
            b.addButton("zcode", {
                title: "Sửa HTML Source",
                "class": "mce_code",
                onclick: function () {
                    this.windowManager.open({
                        url: a + "/source_editor.htm",
                        width: parseInt(b.getParam("theme_advanced_source_editor_width", 720)),
                        height: parseInt(b.getParam("theme_advanced_source_editor_height", 580)),
                        inline: true,
                        resizable: true,
                        maximizable: true
                    }, {
                        theme_url: a
                    })
                }
            });
            b.onGetContent.add(function (c, d) {
                if (d.source_view) {
                    d.content = d.content.replace(/<img([^>]*)id="imgContent_(\d+)"([^>]*)class="imgContent"([^>]*)src="([^">]+)"([^>]*)\/>/gi, function (f, j, k, h, g, i, e) {
                        tinyMCE.settings.insertPhotos[k] = i;
                        return '<span id="imgContent_' + k + '" class="imgContent"' + j + h + g + e + "></span>"
                    })
                }
            });
            b.onBeforeSetContent.add(function (c, d) {
                if (d.source_view) {
                    d.content = d.content.replace(/<span\sid="imgContent_(\d+)"\sclass="imgContent"([^>]*)>\s*<\/span>/gi, function (e, h, g) {
                        var f = tinyMCE.settings.insertPhotos[h];
                        if (f) {
                            return '<img id="imgContent_' + h + '" class="imgContent" src="' + f + '"' + g + " />"
                        }
                        return e
                    })
                }
            });
            b.onSaveContent.add(function (c, d) {
                d.content = d.content.replace(/<img([^>]*)id="imgContent_(\d+)"([^>]*)class="imgContent"([^>]*)\/>/gi, '<span class="imgContent" rel="$2"$1$3$4></span>')
            });
            b.onPostRender.add(function (c) {
                c.dom.bind(c.getBody(), "focus", blog.showToolbar)
            })
        },
        paste_preprocess : function(pl, o) {
            // Content string containing the HTML from the clipboard
            var htmlObject = document.createElement('div');
            htmlObject.innerHTML = o.content;
            var a = htmlObject.getElementsByTagName("p");
            for(var i=0;i<a.length;i++){
                console.log(a[i].innerHTML);
            }
        //            o.content = "-: CLEANED :-\n" + o.content;
        },
        paste_postprocess : function(pl, o) {
        // Content DOM node containing the DOM structure of the clipboard
        //            alert(o.node.innerHTML);
        //            o.node.innerHTML = o.node.innerHTML + "\n-: CLEANED :-";
        }
    })
});

Chủ Nhật, 25 tháng 8, 2013

setup nginx apache phpmyadmin virtualhost ubuntu


nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
location /phpmyadmin {
        proxy_pass      http://127.0.0.1:8000;
        include /server/conf/proxy.conf;
    }
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    server { # simple reverse-proxy
    listen       80;
    server_name  thoitrangtphcm.vn;

    # pass requests for dynamic content to rails/turbogears/zope, et al
    location / {
      proxy_pass      http://127.0.0.1:8000;
      include /server/conf/proxy.conf;
    }
    }

}

proxy.conf

proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 8m;
client_body_buffer_size 256k;
proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffer_size 4k;
proxy_buffers 32 256k;
proxy_busy_buffers_size 512k;
proxy_temp_file_write_size 256k;

ports.conf(apache2)

NameVirtualHost *:8000
Listen 8000

<IfModule mod_ssl.c>
    # If you add NameVirtualHost *:443 here, you will also have to change
    # the VirtualHost statement in /etc/apache2/sites-available/default-ssl
    # to <VirtualHost *:443>
    # Server Name Indication for SSL named virtual hosts is currently not
    # supported by MSIE on Windows XP.
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443
</IfModule>


/etc/sites-available/thoitrangtphcm.vn
<VirtualHost *:8000>
ServerAdmin webmaster@localhost
ServerName thoitrangtphcm.vn
ServerAlias www.thoitrangtphcm.vn
DocumentRoot /var/www/thoitrangtphcm.vn
# <Directory />
# Options FollowSymLinks
# AllowOverride All
#</Directory>
<Directory /var/www/thoitrangtphcm.vn>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
 Require all granted

</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

sudo vim /etc/apache2/apache2.conf

<Directory /zserver/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

/etc/hosts
127.0.0.1  thoitrangtphcm.vn 


Thứ Năm, 22 tháng 8, 2013

chuyển chuỗi in hoa thành in thường php / string to lowercase php (vietnamese)


function tolower_vnm($str) {
    $u = array(
        'Ú', 'Ù', 'Ủ', 'Ũ', 'Ụ', 'U',
        'Ứ', 'Ừ', 'Ử', 'Ữ', 'Ự', 'Ư',
    );
    $u1 = array(
        'ú', 'ù', 'ủ', 'ũ', 'ụ', 'u',
        'ứ', 'ừ', 'ử', 'ữ', 'ự', 'ư');
    $a = array(
        'Á', 'À', 'Ả', 'Ã', 'Ạ', 'A',
        'Ắ', 'Ằ', 'Ẩ', 'Ẵ', 'Ặ', 'Ă',
        'Ấ', 'Ầ', 'Ẩ', 'Ẫ', 'Ậ', 'Â',
    );
    $a1 = array(
        'á', 'à', 'ả', 'ã', 'ạ', 'a',
        'ắ', 'ằ', 'ẳ', 'ẵ', 'ặ', 'ă',
        'ấ', 'ầ', 'ẩ', 'ẫ', 'ậ', 'â');
    $e = array(
        'É', 'È', 'Ẻ', 'Ẽ', 'Ẹ', 'E',
        'Ế', 'Ề', 'Ể', 'Ễ', 'Ệ', 'Ê',
    );
    $e1 = array(
        'é', 'è', 'ẻ', 'ẽ', 'ẹ', 'e',
        'ế', 'ề', 'ể', 'ễ', 'ệ', 'ê'
    );
    $o = array(
        'Ó', 'Ò', 'Ỏ', 'Õ', 'Ọ', 'O',
        'Ố', 'Ồ', 'Ỗ', 'Ỗ', 'Ộ', 'Ô',
        'Ớ', 'Ờ', 'Ỡ', 'Ỡ', 'Ợ', 'Ơ',
    );
    $o1 = array(
        'ó', 'ò', 'ỏ', 'õ', 'ọ', 'o',
        'ố', 'ồ', 'ổ', 'ỗ', 'ộ', 'ô',
        'ớ', 'ờ', 'ở', 'ỡ', 'ợ', 'ơ'
    );
    $ii = array(
        'Í', 'Ì', 'Ỉ', 'Ĩ', 'Ị', 'I',
    );
    $ii1 = array(
        'í', 'ì', 'ỉ', 'ĩ', 'ị', 'i',
    );
    $y = array(
        'Ý', 'Ỳ', 'Ỷ', 'Ỹ', 'Ỵ', 'Y',
    );
    $y1 = array(
        'ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ', 'y'
    );
    $d = array('Đ',
    );
    $d1 = array(
        'đ');

    $count = count($a);
    for ($i = 0; $i < $count; $i++) {
        $str = str_replace($a[$i], $a1[$i], $str);
    }

//    $str = str_replace($a, 'a', $str);
    $count = count($e);
    for ($i = 0; $i < $count; $i++) {
        $str = str_replace($e[$i], $e1[$i], $str);
    }
//    $str = str_replace($e, 'e', $str);
    $count = count($d);
    for ($i = 0; $i < $count; $i++) {
        $str = str_replace($e[$i], $e1[$i], $str);
    }
//    $str = str_replace($d, 'd', $str);
     $count = count($o);
    for ($i = 0; $i < $count; $i++) {
        $str = str_replace($o[$i], $o1[$i], $str);
    }
//    $str = str_replace($o, 'o', $str);
    $count = count($u);
    for ($i = 0; $i < $count; $i++) {
        $str = str_replace($u[$i], $u1[$i], $str);
    }
//    $str = str_replace($u, 'u', $str);
    $count = count($d);
    for ($i = 0; $i < $count; $i++) {
        $str = str_replace($d[$i], $d1[$i], $str);
    }
//    $str = str_replace($d, 'd', $str);
     $count = count($ii);
    for ($i = 0; $i < $count; $i++) {
        $str = str_replace($ii[$i], $ii1[$i], $str);
    }
//    $str = str_replace($i, 'i', $str);
    $count = count($y);
    for ($i = 0; $i < $count; $i++) {
        $str = str_replace($y[$i], $y1[$i], $str);
    }
    //$str = str_replace($y, 'y', $str);
    $count = count($d);
    for ($i = 0; $i < $count; $i++) {
        $str = str_replace($d[$i], $d1[$i], $str);
    }
    //$str = str_replace($d, 'd', $str);
    $str = strtolower($str);
    return $str;
}

tạo host ảo / virtualhost apache2 ubuntu (mysite.com)


tạo host ảo / virtualhost apache2 ubuntu (mysite.com - thay mysite.com bằng tên website của bạn)
tạo thư mục mysite.com
mở terminal (ctrl + alt + t).
sudo mkdir /var/www/mysite.com

tạo file config virtualhost
cd /etc/apache2/sites-available/
sudo cp default mysite.com
sudo gedit mysite.com

copy & paste nội dung sau vào file mysite.com
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName mysite.com
ServerAlias www.mysite.com
DocumentRoot /var/www/mysite.com
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/mysite.com>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>

lưu lại( ctrl + S)
mở terminal
cd /etc/apache2/sites-enabled/
sudo ln -s ../sites-available/mysite.com mysite.com
sudo service apache2 restart
sudo gedit /etc/hosts
thêm dòng sau
127.0.0.1 mysite.com
lưu lại (ctrl + S)
mở terminal
sudo gedit

copy & paste nội dung sau
<?php phpinfo();?>
lưu file với tên index.php vào thư mục /var/www/mysite.com
mở terminal
sudo chmod a+r+w -R /var/www/mysite.com
mở trình duyệt và gõ mysite.com để xem kết quả