2010年3月3日

画像のオリジナルサイズをJavaScriptで取得する

カテゴリー: JavaScript — admin @ 12:57 AM

getNaturalSizeにimageオブジェクトを投げてあげるとオブジェクトが返る。

var cache = [];
var getNaturalSize = (function(){
    if(Image.naturalWidth || Image.naturalHeight){
        return function(image){
            return {
                "width"  : image.naturalWidth,
                "height" : image.naturalHeight
            };
        }
    }
    else if(window.opera){
        return function(image){
            if(!cache[image.src]){
                var mem = {
                    "w": image.width,
                    "h": image.height
                };
                image.removeAttribute("width");
                image.removeAttribute("height");
                w = image.width;
                h = image.height;
                image.width = mem.w;
                image.height = mem.h;
                cache[image.src] = {
                    "width"  : w,
                    "height" : h
                };
            }
            return cache[image.src];
        };
    }
    else if(window.attachEvent){
        return function(image){
            if (image[key] && image[key].src === image.src) {
                return image[key];
            }
            run = image.runtimeStyle;
            mem = {
                "w" : run.width,
                "h" : run.height
            }; // keep runtimeStyle
            run.width  = "auto"; // override
            run.height = "auto";
            w = image.width;
            h = image.height;
            run.width  = mem.w; // restore
            run.height = mem.h;
            image[key] = {
                "width"  : w,
                "height" : h,
                "src"    : image.src
            };
            return image[key]; // bond
        }
    }
    else{
        return function(image){
            return {
                "width"  : 100,
                "height" : 100
            };
        }
    }
})();

Firefox、IE6~8、Chrome、Operaで動作する。Safari未検証。

2010年2月22日

スクロール位置を取得する

カテゴリー: JavaScript — admin @ 3:19 AM

var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;// ex) 100(px)

2010年2月22日

Zend_Controller_Router_Route_Regexで正規表現によるルーティング設定を行う

カテゴリー: ZendFramework — admin @ 1:35 AM

以下のように、Zend_Controller_Router_Route_Regexを使用する。

$front->getRouter()->addRoute(
	'page',
	new Zend_Controller_Router_Route_Regex(
		'page_(\d+)\.html',
		array(
			'controller' => 'Index',
			'action'     => 'page'
		),
		array(
			1 => 'pageid'
		)
	)
);

Actionからは以下のコードでマッチした部分を参照できる。

$this->_getParam('pageid');

2010年2月22日

ファイルの所有者を変更する

カテゴリー: Linux — admin @ 1:19 AM

/home/user/directory配下のファイルの所有者を全てftp_userにする。

chown -R ftp_user  /home/user/directory

2010年2月22日

PHPからドメインを取得する

カテゴリー: PHP — admin @ 12:18 AM

var_dump($_SERVER['SERVER_NAME']);
//string(16) "sample.org"

マルチドメイン、シングルホスティングの時には使いそうだ。

2010年2月22日

MySQLのrootパスワードを設定する

カテゴリー: MySQL — admin @ 12:03 AM

インストール直後は何もパスワードが設定されていないので注意すること!

SET PASSWORD FOR root@localhost=PASSWORD('password');

2010年2月19日

sshのrootログインを禁止する

カテゴリー: Linux — admin @ 2:10 AM

vi /etc/ssh/sshd_config

以下のオプションをnoにしてあげる。

PermitRootLogin  no

2010年2月19日

特定のユーザにmysqlリモート接続を許可する

カテゴリー: Linux, デフォルト — admin @ 2:07 AM

その前にポート3306は初期状態では閉じられているので開ける。

iptables -I INPUT -j ACCEPT -p tcp -s x.x.x.x --dport 3306

IPはクライアント側のIP。このIPからだけ接続できる。192.168.1.%のようにワイルドカードも使えるがセキュリティには注意しなくてはならない。

GRANT ALL PRIVILEGES ON *.* to user@'192.168.1.1' IDENTIFIED BY 'password';

2010年2月19日

FTP用ユーザのディレクトリを制限する

カテゴリー: Linux — admin @ 2:03 AM

■vsftpの場合

vi /etc/vsftpd/vsftpd.conf

chroot_list_enableとchroot_list_fileの記述を以下のように修正する。

chroot_list_enable=YES
chroot_list_file=/etc/vsftpdd.chroot_list
vi /etc/vsftpd/vsftpd.conf

ファイルに制限したいユーザ名を書く。

ftp_user

再起動を忘れずに。

/etc/init.d/vsftpd restart

ftp_userがホームディレクトリより上にいけなくなる。

2010年2月14日

ZendFrameworkでコマンドラインからアクションを実行する

カテゴリー: PHP, ZendFramework — admin @ 11:56 PM

コマンドラインにおけるオプションの設定・取得。|の後がエイリアス名、=の後の文字列で型を指定できる。

try {
    $options = new Zend_Console_Getopt(
        array(
            'help|h'        => 'help.',
            'zfm|m=s'       => 'module',
            'zfc|c=s'       => 'controller',
            'zfa|a=s'       => 'action'
        )
    );
    $options->parse();
}
catch(Zend_Console_Getopt_Exception $e){
    die($e->getMessage() . ' : ' . $e->getUsageMessage());
}

Zend_Controller_Request_Simpleがポイント。アクション、コントローラ、モジュールを引数に指定してリクエストオブジェクトを取得する。

if(isset($options->zfa) && isset($options->zfc) && isset($options->zfm)){
    $request = new Zend_Controller_Request_Simple(
        $options->zfa,
        $options->zfc,
        $options->zfm
    );
    $front = Zend_Controller_Front::getInstance();
    $front->setRequest($request);
    $front->setRouter(new Custom_Controller_Router_Cli());
    $front->setResponse(new Zend_Controller_Response_Cli());
    $front->throwExceptions(true);
    $front->addModuleDirectory(dirname(__FILE__) . '/application/modules');
    $front->dispatch();
}

Custom_Controller_Router_Cliはこんな感じ。

<?php
require_once 'Zend/Controller/Router/Interface.php';
require_once 'Zend/Controller/Router/Abstract.php';

class Custom_Controller_Router_Cli extends Zend_Controller_Router_Abstract implements Zend_Controller_Router_Interface
{
    public function assemble($userParams, $name = null, $reset = false, $encode = true) {}
    public function route(Zend_Controller_Request_Abstract $dispatcher) {}
}

基本的には以上で実行できるが

■アクセスコントロールの設定

アクセスコントロールを行っている場合は上述のコードよりも先にアクセスできるようにしなければならない。以下は一例。

$sesion = new Zend_Session_Namespace('global');
$sesion->userLevel = 'admin';
$acl = new Zend_Acl();
$acl->addRole(new Zend_Acl_Role('guest'));
$acl->addRole(new Zend_Acl_Role('admin'), 'guest');
$acl->add(new Zend_Acl_Resource('guestPage'));
$acl->add(new Zend_Acl_Resource('adminPage'));
$acl->allow('guest');
$acl->allow('admin');
$acl->deny('guest', 'adminPage');
$acl->allow('admin', 'adminPage');
Zend_Registry::set('acl', $acl);

基本的には一般ユーザが閲覧(実行)できる場所に、このファイルを配置するのは良くない。

次ページへ »