半年ぶり位のブランクを経て
久しぶりににphpとsmartyに触れてみようかと思ったら、
のっけからはまってしまった(TT)
smartyの
$securityをtrueにすると
Warning: Smarty error: (secure mode) accessing “app.conf” is not allowed in C:\xxxx\xxxx\xxxx\Smarty.class.php on line 1092
Warning: Smarty error: (secure mode) accessing “index.tpl” is not allowed in C:\xxxx\xxxx\xxxx\Smarty.class.php on line 1092
上記みたいなエラーが。。
securityをfalseにすれば出ないから
とりあえずfalseにしとこうかとも思ったけど
ついつい調べ始めたら数時間経ってしまいました。
とりあえず解決できたので、メモ。
smartyをextendsしているファイルは一部ですが
下記のような感じ
require_once(’Smarty.class.php’);
class MySmarty extends Smarty {
// コンストラクタ
public function __construct($folder_name) {
$this->Smarty();
$this->security=true;
$this->template_dir= “C:/xxxx/xxxx/xxxx/templates” . “/” . $folder_name;
$this->compile_dir= “C:/xxxx/xxxx/xxxx/templates_c” . “/” . $folder_name;
$this->config_dir= “C:/xxxx/xxxx/xxxx/config” . “/” . $folder_name;
このsmartyクラスを継承したMySmartyクラスを利用して
ファイルを作っていました。
コンストラクトの引数$folder_nameには
stringで作成するページのトップページからのディレクトリ名を入力していました。
例えば
http://www.アドレス/
がトップページで、このMySmartyを利用するページが
http://www.アドレス/test/test.php
だった場合には、
$folder_name == “test”;
みたいな感じです。
今回はトップページのファイルを作っていたので、
$folder_name == “”;
です。
そんな感じで作ってたところ先ほどのようなエラー。
エラー内容からして
accessing “app.conf” is not allowed in
accessing “index.tpl” is not allowed in
なんだか許可が無いからapp.confとindex.tplにアクセス出来ないとのエラーみたい。
でwarningを吐き出してるのはSmarty.class.phpの1092行目あたりとの事なので
smarty.class.phpを覗いてみました。
すると
1640行目あたりの
$_fullpath = $_curr_path . DIRECTORY_SEPARATOR . $params[’resource_name’];
上記行$_fullpathの中身をechoで出力すると
C:/xxxx/xxxx/xxxx/config/\app.conf
C:/xxxx/xxxx/xxxx/templates/\index.tpl
上記行のように
config/\app.conf
スラッシュと\(バックスラッシュ)が続いている為に
app.confとindex.tplにアクセスできてない事が判明
先ほどの1640行目を見ると
$_curr_pathの中身は
C:/xxxx/xxxx/xxxx/config/
$params[’resource_name’]は
app.conf
DIRECTORY_SEPARATORの部分は
PHPでは定義済みの定数で、OSによって適切に”\”か”/”を設定してくれるらしい。
つまり
“C:/xxxx/xxxx/xxxx/config/” . “\” . “app.conf”
という感じになってる。
でsmartyクラスを継承した
MySmartyを見直してみると
$this->template_dir= “C:/xxxx/xxxx/xxxx/templates” . “/” . $folder_name;
$this->compile_dir= “C:/xxxx/xxxx/xxxx/templates_c” . “/” . $folder_name;
$this->config_dir= “C:/xxxx/xxxx/xxxx/config” . “/” . $folder_name;
今回$folder_nameはトップページを作っていて”"なので、
$this->template_dir等に代入したstringの最後に”/”スラッシュが入っていて
その最後の行にスラッシュが入っている所にDIRECTORY_SEPARATORで
バックスラッシュを継ぎ足していたので、こんなエラーになっていた事がわかった。。
原因判明すっきり!。。
=============================================
その他忘れかけてたphpあれこれ
var の使いどころ
http://chaichan.web.infoseek.co.jp/qanda/qa6859.htm?06-07-22-00-49
クラスのメンバ変数に使う
変数のアンダースコア
http://jamz.jp/tech/2006/10/coding-rule-about-private-parameter.html
クラス内のプライベート変数に _ (アンダースコアー)が接頭語として使われていたので一般的なコーディング習慣
(変数の値をgetやsetで取り出すプライベート変数に慣習で使っている)