PHP北海道
脱はてダして居酒屋を開発する
2007/09/29 15:19:37
プロフィールを変更できるようにする
21:09にUserクラスを変更しました><(PNGがアップロードできない環境があるみたい)
rhaco修行シリーズ!
なんかコードばっかりでよくわからない感じになってるなぁ。とは思いつつも、どこを説明して良いのかよくわからない。。。
そもそも読んでる人が居るかどうかもわからない。ツッコミ入るまではこのまま突っ走ってやります><!
setting.phpの実装
前回の記事でsetting.phpへのリンクを用意したので、ユーザー情報の変更のアクションになる部分を実装します。
ここもrhacoを使うととても簡単に書けます。
とりあえずコードを先に。
<?php
require_once '__init__.php';
Rhaco::import('network.http.RequestLogin');
Rhaco::import('network.http.Header');
Rhaco::import('generic.Flow');
Rhaco::import('database.DbUtil');
Rhaco::import('database.model.Criteria');
Rhaco::import('database.model.Q');
Rhaco::constant("HTML_CSRF_FILTER", true);
if(!RequestLogin::isLogin()){
Header::redirect(Rhaco::url('login.php'));
}
$db = new DbUtil(User::connection());
$flow = new Flow();
$user = RequestLogin::getLoginSession();
$object = $db->get(new User(), new Criteria(Q::eq(User::columnId(), $user->getId())));
if(is_null($object)){
$flow->write('404.html');
Rhaco::end();
}
if($flow->isPost()){
$flow->setVariable('id', $user->getId());
if($db->update($flow->toObject($object))){
Header::redirect(Rhaco::url());
}
}
$flow->parseObject($object);
$flow->setVariable('object', $object);
$flow->write(Rhaco::constant("TEMPLATE") . '/user_form.html');
Rhaco::constant("HTML_CSRF_FILTER", true);というのは、CSRF攻撃を防止するためのよくあるアレです。rhacoはこれだけでチェックできちゃうんですね!
他は特別難しいことはないですが、
$flow->toObject($object)はリクエストの値を$objectにセットするものです。insertとかに使いたいときは$request->toObject(new User())とかで出来ると思います。
そして$flow->parseObject($object);はその逆で、objectの値をテンプレートに流します。<input ... reference="true" />とかしてるときに便利です。
写真をアップロードするためのコードをつくる
前回の記事で、写真をアップロードするとか書いてるのに全然コード書いて無いじゃないか!とか思った方、お待たせしました(そんなやつ居るのか?)(でも忘れない。)
で、今回ここ迷ったんですが、アップロードするコードをモデルに書いてしまいました。これって良いのかなぁ?
そのほかにも、modifiedとかの値をセットするためと、パスワードの暗号化も考えてみました。
ということで、library/model/User.phpを以下のように編集しました。
<?php
Rhaco::import("model.table.UserTable");
Rhaco::import('io.FileUtil');
Rhaco::import('network.http.Request');
class User extends UserTable{
function beforeInsert(&$db){
$this->encryptPassword();
$this->uploadPicture();
}
function beforeUpdate(&$db){
$this->encryptPassword();
$this->uploadPicture();
if(empty($this->password)){
$user = $db->get($this);
if(is_null($user)) return false;
$this->password = $user->getPassword();
}
if(empty($this->picture)){
$user = $db->get($this);
if(is_null($user)) return false;
$this->picture = $user->getPicture();
}
$this->modified = time();
}
function encryptPassword(){
if(!empty($this->password)){
$this->password = sha1($this->password . Rhaco::constant('APPLICATION_ID'));
}
}
function uploadPicture(){
$request = new Request();
$filename = '';
if($request->isFile('picture')){
if($this->getPicture() != '' && FileUtil::exist(Rhaco::resource('picture/' . $this->getPicture()))){
FileUtil::rm(Rhaco::resource('picture/' . $this->getPicture()));
}
$file = $request->getFile('picture');
$image_info = getimagesize($file->tmp);
if($image_info[0] == 0 || $image_info[1] == 0) return $this->setPicture($filename);
$width = Rhaco::constant('PICTURE_WIDTH', 50);
$height = $image_info[1] * ($width / $image_info[0]);
switch($image_info[2]){
case IMG_GIF:
$img = imagecreatefromgif($file->tmp);
$img_new = imagecreatetruecolor($width, $height);
imagecopyresampled($img_new, $img, 0, 0, 0, 0, $width, $height, $image_info[0], $image_info[1]);
imagegif($img_new, Rhaco::resource(sprintf('picture/%d.gif', $this->getId())));
$filename = $this->getId() . '.gif';
break;
case IMG_JPG:
case IMG_JPEG:
$img = imagecreatefromjpeg($file->tmp);
$img_new = imagecreatetruecolor($width, $height);
imagecopyresampled($img_new, $img, 0, 0, 0, 0, $width, $height, $image_info[0], $image_info[1]);
imagejpeg($img_new, Rhaco::resource(sprintf('picture/%d.jpg', $this->getId())), 100);
$filename = $this->getId() . '.jpg';
break;
case IMG_PNG:
case 3: // IMG_PNGがうまくいかないことがある
$img = imagecreatefrompng($file->tmp);
$img_new = imagecreatetruecolor($width, $height);
imagecopyresampled($img_new, $img, 0, 0, 0, 0, $width, $height, $image_info[0], $image_info[1]);
imagepng($img_new, Rhaco::resource(sprintf('picture/%d.png', $this->getId())));
$filename = $this->getId() . '.png';
break;
default:
// 対応していない形式
break;
}
}
$this->setPicture($filename);
}
}
ここまでくると説明のしようが…><
画像の処理はCakePHPのやつとほとんど一緒です。
PICTURE_WIDTHを設定していると、その幅に合わせるようになってます。setupでPICTURE_WIDTH設定出来るようにproject.xmlに
<input name="PICTURE_WIDTH"> <title>profile picture width</title> <data>50</data> </input>
としておくと良いとおもいます。
つぎに、パスワードを暗号化したので、LoginConditionにも変更があります。
function condition($request){
$db = new DbUtil(User::connection());
$user = $db->get(
new User(),
new Criteria(
Q::eq(User::columnEmail(), $request->getVariable('email')),
Q::eq(User::columnPassword(), sha1($request->getVariable('password') . Rhaco::constant('APPLICATION_ID')))
)
);
こんな感じになります。たぶん。
設定画面のHTMLをつくる
長くなってきました。。。
HTMLはいつものようにindex.htmlを継承します。
ファイル名はuser_form.htmlとしました。
<rt:extends href="./index.html" />
<rt:block name="pan">
<li><a href="{$rhaco.url()}">{$object.name}</a></li>
<li>settings</li>
</rt:block>
<rt:block name="menu">
<li id="MENU01"><a href="{$rhaco.url()}">index</a></li>
<li id="MENU02"><a href="{$rhaco.url()}">Diary</a></li>
<li id="MENU03"><a href="{$rhaco.url()}">Friends</a></li>
<li id="MENU04" class="menu-on"><a
href="{$rhaco.url('setting.php')}">Settings</a></li>
<li id="MENU05"><a href="{$rhaco.url()}">Logout</a></li>
</rt:block>
<rt:block name="content">
<h2>{$object.name}</h2>
<div class="text">
<h3>Settings</h3>
<rt:invalid name="exceptions" errors="errors">
<ul class="exception">
<rt:loop param="errors" var="msg">
<li>{$msg}</li>
</rt:loop>
</ul>
</rt:invalid>
<form action="{$rhaco.url('setting.php')}" method="post"
enctype="multipart/form-data"><input type="hidden"
name="MAX_FILE_SIZE" value="30000" />
<p><label for="name">name</label><input name="name" type="text"
size="30" reference="true" /></p>
<p><label for="email">email</label><input name="email" type="text"
size="30" reference="true" /></p>
<p><label for="password">password</label><input name="password"
type="password" size="30" /></p>
<p><label for="profile">profile</label><textarea name="profile"
cols="25" rows="5" reference="true"></textarea></p>
<p><label for="picture">picture</label><input type="file" size="20"
name="picture" /></p>
<p><input type="submit" value="Update" /></p>
</form>
<ul class="modori">
<li><a href="#PAGETOP">TOP</a></li>
</ul>
</div>
</rt:block>
そのうち画像削除も必要ですねー。(たぶん)
疲れた(ブログのほうが…w)ので今回はこの辺で。(でも忘れない)
Add Comment
-
4219Ig <a href="http://dvigagetqbmn.com/">dvigagetqbmn</a>, [url=http://wflcitqtxhen.com/]wflcitqtxhen[/url], [link=http://ycsycwprrxrn.com/]ycsycwprrxrn[/link], http://uvnlzitghlre.com/
posted by acmhwuhhl : 2008/09/10
-
Px49KW <a href="http://nxeovbgcivej.com/">nxeovbgcivej</a>, [url=http://khzsvcdeoquw.com/]khzsvcdeoquw[/url], [link=http://tpyxegstzagt.com/]tpyxegstzagt[/link], http://bqtanzitdoaf.com/
posted by nvzfxmcwzg : 2008/09/11
-
2EvBFN <a href="http://qapatelxbcex.com/">qapatelxbcex</a>, [url=http://yvkzfihuhosn.com/]yvkzfihuhosn[/url], [link=http://ljzqtywntjkc.com/]ljzqtywntjkc[/link], http://desopkdoatlj.com/
posted by ujmzmnkuh : 2008/10/04
-
g0rbFa <a href="http://cnhrbwwafarw.com/">cnhrbwwafarw</a>, [url=http://svwdnokxtydy.com/]svwdnokxtydy[/url], [link=http://krdxfyvccjeg.com/]krdxfyvccjeg[/link], http://fhkeknvgotkd.com/
posted by ruwcdeaqund : 2009/01/01
-
JXUe2R <a href="http://gjclridrqhot.com/">gjclridrqhot</a>, [url=http://omiqfivyexit.com/]omiqfivyexit[/url], [link=http://ktbqinvxdemt.com/]ktbqinvxdemt[/link], http://zmecbzhldgnl.com/
posted by wbhfhoh : 2009/11/11
-
YcqRlY <a href="http://abdvtduimadg.com/">abdvtduimadg</a>, [url=http://oyjnojgbxvpn.com/]oyjnojgbxvpn[/url], [link=http://lacxgauxvvzq.com/]lacxgauxvvzq[/link], http://bqurrjcqmhsp.com/
posted by rirckafhz : 2009/12/19
-
mzsRFx <a href="http://whgeokiwlkhr.com/">whgeokiwlkhr</a>, [url=http://myylshrnylff.com/]myylshrnylff[/url], [link=http://huvqdpllhicu.com/]huvqdpllhicu[/link], http://xfiwdwbnylyj.com/
posted by wvizuxutjr : 2010/02/18
