OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / comment / include / container / commentTopWidgetContainer.php
1 <?php
2 /**
3  * index.php用コンテナクラス
4  *
5  * PHP versions 5
6  *
7  * LICENSE: This source file is licensed under the terms of the GNU General Public License.
8  *
9  * @package    Magic3 Framework
10  * @author     平田直毅(Naoki Hirata) <naoki@aplo.co.jp>
11  * @copyright  Copyright 2006-2013 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: commentTopWidgetContainer.php 6155 2013-07-02 00:02:49Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once($gEnvManager->getWidgetContainerPath('comment') . '/commentBaseWidgetContainer.php');
17
18 class commentTopWidgetContainer extends commentBaseWidgetContainer
19 {
20         private $message;                       // ユーザ向けメッセージ
21         private $pageNo;                                // ページ番号
22         private $cmd;                           // 実行コマンド
23         private $commentSerialNo;               // コメントシリアル番号
24         private $isReadImageCheck;              // 画像読み込みチェックかどうか
25         private $isErrorInReadImage;    // 画像読み込み中にエラーがあるかどうか
26         private $currentPageUrl;                        // 現在のページURL
27         private $currentPageRootUrl;
28         private $widgetTitle;                   // ウィジェットタイトル
29         private $userLimited;           // ユーザ制限あり
30         private $commentVisible;                // コメントを表示する
31         private $commentAccept;         // コメントを受け付ける
32         private $isExistsComment;       // 表示するコメントがあるかどうか
33         private $useTitle;              // コメント入力項目(タイトルあり)
34         private $useAuthor;             // コメント入力項目(投稿者名あり)
35         private $useEmail;              // コメント入力項目(Eメールあり)
36         private $useUrl;                // コメント入力項目(URLあり)
37         private $useAvatar;             // コメント入力項目(アバターあり)
38         private $permitHtml;            // HTMLあり
39         private $permitImage;           // 画像あり
40         private $autolink;                      // 自動リンク
41         private $addLib = array();              // 追加スクリプト
42         private $avatarSize;            // アバター画像サイズ
43         private $maxImageSize;          // 画像最大サイズ
44         private $imageDir;                      // 画像格納ディレクトリ
45         private $imageFileInfoArray;    // 画像ファイル情報
46         const COMMENT_ID_SEPARATOR      = ':';                  // コメントID作成用セパレータ
47         const DEFAULT_TITLE = 'コメント';           // デフォルトのウィジェットタイトル名
48         const LINK_PAGE_COUNT           = 5;                    // リンクページ数
49         
50         const MESSAGE_NOT_PERMITTED_REFER               = '閲覧は許可されていません';                               // コメント閲覧権限がない場合
51         const MESSAGE_NOT_PERMITTED_POST                = '投稿は許可されていません';                               // コメント投稿権限がない場合
52         const MESSAGE_NO_COMMENT                = 'コメントは投稿されていません';                         // コメントが投稿されていないメッセージ
53         const NO_COMMENT_TITLE = 'タイトルなし';                          // 未設定時のコメントタイトル
54         const NO_COMMENT_AUTHOR = '投稿者名なし';                         // 未設定時のコメント投稿者名
55         const COMMENT_TITLE_FORMAT              = '「%s」についてのコメント';        // コメント用タイトルフォーマット
56         const AVATAR_TITLE_TAIL = 'のアバター';
57         const COOKIE_LIB = 'jquery.cookie';             // 名前保存用クッキーライブラリ
58         const PERMALINK_ICON_FILE = '/images/system/permalink.png';             // 「パーマリンク」アイコン
59         const PERMA_BUTTON_ICON_SIZE = 16;                              // ボタン用アイコンサイズ
60         
61         /**
62          * コンストラクタ
63          */
64         function __construct()
65         {
66                 // 親クラスを呼び出す
67                 parent::__construct();
68                 
69                 $this->imageDir = $this->gEnv->getIncludePath() . commentCommonDef::UPLOAD_IMAGE_DIR;                   // 画像格納ディレクトリ
70                 if (!is_dir($this->imageDir)) @mkdir($this->imageDir, M3_SYSTEM_DIR_PERMISSION);
71         }
72         /**
73          * テンプレートファイルを設定
74          *
75          * _assign()でデータを埋め込むテンプレートファイルのファイル名を返す。
76          * 読み込むディレクトリは、「自ウィジェットディレクトリ/include/template」に固定。
77          *
78          * @param RequestManager $request               HTTPリクエスト処理クラス
79          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
80          * @return string                                               テンプレートファイル名。テンプレートライブラリを使用しない場合は空文字列「''」を返す。
81          */
82         function _setTemplate($request, &$param)
83         {
84                 return 'main.tmpl.html';
85         }
86         /**
87          * テンプレートにデータ埋め込む
88          *
89          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
90          *
91          * @param RequestManager $request               HTTPリクエスト処理クラス
92          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
93          * @param                                                               なし
94          */
95         function _assign($request, &$param)
96         {
97                 $contentsId = '';                       // 共通コンテンツID
98                 $this->cmd = $request->trimValueOf(M3_REQUEST_PARAM_OPERATION_COMMAND);
99                 if ($this->cmd == M3_REQUEST_CMD_DO_WIDGET){    // ウィジェット単体実行
100                         // 画像IDからコメントシリアル番号取得
101                         // コメントに添付されている場合はコメントの表示条件をチェック
102                         $imageId = $request->trimValueOf(commentCommonDef::REQUEST_PARAM_IMAGE_ID);     // 画像ID
103                         $ret = $this->gInstance->getFileManager()->getAttachFileInfoByFileId(commentCommonDef::$_viewContentType, $imageId, $attachFileRow, false/*登録状態区別せず*/);
104                         if ($ret){
105                                 // コメントのアクセス可能状況をチェック
106                                 $this->commentSerialNo = $attachFileRow[af_content_id];         // コメントシリアル番号
107                                 if (empty($this->commentSerialNo)){                     // コメントに未添付の画像の場合
108                                         // クライアントIDが同じであればダウンロード可能
109                                         $clientId = $this->gAccess->getClientId();
110                                         if (!empty($clientId) && $clientId == $attachFileRow[af_client_id]){
111                                                 $this->downloadImage($request);                 // 画像取得
112                                         }
113                                 } else {
114                                         $ret = self::$_mainDb->getCommentItem($this->commentSerialNo, $row);
115                                         if ($ret && !$row['cm_deleted'] && $row['cm_status'] == 2/*コメント公開中*/){            
116                                                 $this->contentType = $row['cm_content_type'];           // コンテンツタイプ
117                                                 $contentsId = $row['cm_contents_id'];           // 共通コンテンツID
118                                         }
119                                 }
120                         }
121                 } else {
122                         $this->contentType = $this->gPage->getContentType();
123
124                         switch ($this->contentType){
125                                 case M3_VIEW_TYPE_CONTENT:                              // 汎用コンテンツ
126                                         $contentsId = $request->trimValueOf(M3_REQUEST_PARAM_CONTENT_ID);
127                                         if (empty($contentsId)) $contentsId = $request->trimValueOf(M3_REQUEST_PARAM_CONTENT_ID_SHORT);
128                                         break;
129                                 case M3_VIEW_TYPE_PRODUCT:                              // 商品情報(Eコマース)
130                                         $contentsId = $request->trimValueOf(M3_REQUEST_PARAM_PRODUCT_ID);
131                                         if (empty($contentsId)) $contentsId = $request->trimValueOf(M3_REQUEST_PARAM_PRODUCT_ID_SHORT);
132                                         break;
133                                 case M3_VIEW_TYPE_BBS:                                  // BBS
134                                         $contentsId = $request->trimValueOf(M3_REQUEST_PARAM_BBS_THREAD_ID);
135                                         if (empty($contentsId)) $contentsId = $request->trimValueOf(M3_REQUEST_PARAM_BBS_THREAD_ID_SHORT);
136                                         break;
137                                 case M3_VIEW_TYPE_BLOG:                         // ブログ
138                                         $contentsId = $request->trimValueOf(M3_REQUEST_PARAM_BLOG_ENTRY_ID);
139                                         if (empty($contentsId)) $contentsId = $request->trimValueOf(M3_REQUEST_PARAM_BLOG_ENTRY_ID_SHORT);
140                                         break;
141                                 case M3_VIEW_TYPE_WIKI:                         // wiki
142                                         $contentsId = $request->getWikiPageFromQuery();         // 「=」なしのパラメータはwikiパラメータとする
143                                         break;
144                                 case M3_VIEW_TYPE_USER:                         // ユーザ作成コンテンツ
145                                         $contentsId = $request->trimValueOf(M3_REQUEST_PARAM_ROOM_ID);
146                                         if (empty($contentsId)) $contentsId = $request->trimValueOf(M3_REQUEST_PARAM_ROOM_ID_SHORT);
147                                         break;
148                                 case M3_VIEW_TYPE_EVENT:                                // イベント情報
149                                         $contentsId = $request->trimValueOf(M3_REQUEST_PARAM_EVENT_ID);
150                                         if (empty($contentsId)) $contentsId = $request->trimValueOf(M3_REQUEST_PARAM_EVENT_ID_SHORT);
151                                         break;
152                                 case M3_VIEW_TYPE_PHOTO:                                // フォトギャラリー
153                                         $contentsId = $request->trimValueOf(M3_REQUEST_PARAM_PHOTO_ID);
154                                         if (empty($contentsId)) $contentsId = $request->trimValueOf(M3_REQUEST_PARAM_PHOTO_ID_SHORT);
155                                         break;
156                         }
157                 }
158                 
159                 // 共通コンテンツIDがない場合は非表示にする
160                 if (empty($contentsId)){
161                         //$this->cancelParse();         // テンプレート変換処理中断
162                         $this->exitWidget();            // ウィジェット終了処理
163                         return;
164                 }
165                 
166                 // コメント定義取得
167                 $ret = self::$_mainDb->getConfig($this->contentType, ''/*全体の定義*/, $row);
168                 if ($ret){
169                         $viewType                               = $row[commentCommonDef::FD_VIEW_TYPE];                         // コメントタイプ(0=フラット、1=ツリー)
170                         $viewCount                              = $row[commentCommonDef::FD_MAX_COUNT];                 // 表示項目数
171                         $viewDirection                  = $row[commentCommonDef::FD_VIEW_DIRECTION];                            // 表示順
172                         $this->commentVisible   = $row[commentCommonDef::FD_VISIBLE];           // コメントを表示する
173                         $commentVisibleDefault  = $row[commentCommonDef::FD_VISIBLE_D];         // コメントを表示する(個別デフォルト)
174                         $this->commentAccept    = $row[commentCommonDef::FD_ACCEPT_POST];               // コメントを受け付ける
175                         $commentAcceptDefault   = $row[commentCommonDef::FD_ACCEPT_POST_D];             // コメントを受け付ける(個別デフォルト)
176                         $this->userLimited              = $row[commentCommonDef::FD_USER_LIMITED];              // ユーザ制限あり
177                         $needAuthorize                  = $row[commentCommonDef::FD_NEED_AUTHORIZE];            // 認証が必要かどうか
178                         $this->permitHtml               = $row[commentCommonDef::FD_PERMIT_HTML];               // HTMLあり
179                         $this->permitImage              = $row[commentCommonDef::FD_PERMIT_IMAGE];              // 画像あり
180                         $this->autolink                 = $row[commentCommonDef::FD_AUTOLINK];                  // 自動リンク
181                         $maxLength                              = $row[commentCommonDef::FD_MAX_LENGTH];                        // 文字数
182                         $this->maxImageSize             = $row[commentCommonDef::FD_MAX_IMAGE_SIZE];            // 画像最大サイズ
183                         $this->useTitle                 = $row[commentCommonDef::FD_USE_TITLE];         // タイトルあり
184                         $this->useAuthor                = $row[commentCommonDef::FD_USE_AUTHOR];                // 投稿者名あり
185                         $this->useEmail                 = $row[commentCommonDef::FD_USE_EMAIL];         // Eメールあり
186                         $this->useUrl                   = $row[commentCommonDef::FD_USE_URL];           // URLあり
187                         $this->useAvatar                = $row[commentCommonDef::FD_USE_AVATAR];                // アバターあり
188                 } else {                // 定義が取得できないとき
189                         //$this->cancelParse();         // テンプレート変換処理中断
190                         $this->exitWidget();            // ウィジェット終了処理
191                         return;
192                 }
193                 // コンテンツ個別のコメント定義取得
194                 // 全体の定義の否定形を最優先する
195                 $ret = self::$_mainDb->getConfig($this->contentType, $contentsId, $row);
196                 if ($ret){
197                         if ($this->commentVisible) $this->commentVisible        = $row[commentCommonDef::FD_VISIBLE];           // コメントを表示する
198                         if ($this->commentAccept) $this->commentAccept          = $row[commentCommonDef::FD_ACCEPT_POST];               // コメントを受け付ける
199                 } else {                // 個別の定義がない場合はデフォルトを取得
200                         if ($this->commentVisible) $this->commentVisible        = $commentVisibleDefault;               // コメントを表示する
201                         if ($this->commentAccept) $this->commentAccept          = $commentAcceptDefault;                // コメントを受け付ける
202                 }
203                 // コメント非表示の場合は終了
204                 if (!$this->commentVisible){
205                         //$this->cancelParse();         // テンプレート変換処理中断
206                         $this->exitWidget();            // ウィジェット終了処理
207                         return;
208                 }
209                 // 初期設定値
210                 $this->currentPageUrl = $this->gEnv->createCurrentPageUrl();// 現在のページURL
211                 $this->currentPageRootUrl = $this->gEnv->getRootUrlByCurrentPage();
212                 $sendButtonLabel = '投稿';            // 送信ボタンラベル
213                 $sendStatus = 0;                // 送信状況
214                 $avatarFormat = $this->gInstance->getImageManager()->getDefaultAvatarFormat();          // 画像フォーマット取得
215                 $this->gInstance->getImageManager()->parseImageFormat($avatarFormat, $imageType, $imageAttr, $this->avatarSize);                // 画像情報取得
216                 
217                 // 入力値取得
218                 $act = $request->trimValueOf('act');
219                 $pageNo = $request->trimIntValueOf('page', '1');                                // ページ番号
220                 if ($this->useTitle){
221                         $title = $request->trimValueOf('title');
222                 } else {
223                         $title = '';
224                 }
225                 if ($this->useAuthor){
226                         $author = $request->trimValueOf('author');                      // 投稿者名
227                                         
228                         // ログイン中はログインユーザ名を取得
229                         $userName = $this->gEnv->getCurrentUserName();
230                         if (!empty($userName)) $author = $userName;
231                 } else {
232                         $author = '';
233                 }
234                 if ($this->useEmail){
235                         $email = $request->trimValueOf('email');
236                         
237                         // ログイン中はログインユーザのEメールを取得
238                         $userEmail = $this->gEnv->getCurrentUserEmail();
239                         if (!empty($userEmail)) $email = $userEmail;
240                 } else {
241                         $email = '';
242                 }
243                 if ($this->useUrl){
244                         $url = $request->trimValueOf('url');
245                 } else {
246                         $url = '';
247                 }
248                 $comment = $request->trimValueOf('comment');
249                 if ($this->permitHtml){                 // HTML送信の場合はBBCodeをHTMLに変換
250                         $commentHtml = $this->parseComment($comment);
251                 }
252                 $sendStatus = intval($request->trimValueOf('sendstatus'));                      // 送信ステータス
253                 if ($sendStatus < 0 || 1 < $sendStatus) $sendStatus = 0;
254                 $postTicket = $request->trimValueOf('ticket');          // POST確認用
255                 
256                 // コメントIDの確認
257                 $isCommentValid = false;
258                 $commentId = $request->trimValueOf('commentid');                        // コメントID
259                 if (!empty($commentId) && $commentId = md5($this->contentType . self::COMMENT_ID_SEPARATOR . $contentsId)) $isCommentValid = true;
260                 
261                 $isInit = false;        // 初期表示かどうか
262                 if ($this->cmd == M3_REQUEST_CMD_DO_WIDGET){    // ウィジェット単体実行
263                         if (!$this->userLimited || ($this->userLimited && $this->gEnv->isCurrentUserLogined())){                // ユーザ制限なし、または、ユーザ制限ありでログイン済みの場合
264                                 $this->downloadImage($request);                 // 画像取得
265                         } else {
266                                 $this->exitWidget();            // ウィジェット終了処理
267                         }
268                 } else if ($act == 'checkcomment' && $sendStatus == 0){         // コメント確認のとき
269                         if (!empty($postTicket) && $postTicket == $request->getSessionValue(M3_SESSION_POST_TICKET) && $isCommentValid){                // 正常なPOST値のとき
270                                 // 入力チェック
271                                 if ($this->useAuthor){
272                                         $this->checkInput($author, '投稿者');
273                                 }
274                                 if (empty($maxLength)){         // 空のときは長さのチェックなし
275                                         $this->checkInput($comment, 'コメント内容');
276                                 } else {
277                                         $this->checkLength($comment, 'コメント内容', $maxLength);
278                                 }
279                                 if ($this->useEmail) $this->checkMailAddress($email, 'Eメール', true);
280
281                                 // ##### 画像ありの場合は画像を取り込む #####
282                                 if ($this->permitHtml && $this->permitImage){
283                                         // 画像URL変換
284                                         $commentHtml = $this->convertImageUrl($commentHtml);
285                                         if ($this->isErrorInReadImage){
286                                                 $this->setUserErrorMsg('画像読み込みに失敗しました');
287                                                 $commentHtml = '';
288                                         }
289                                 }
290                                         
291                                 // エラーなしの場合は確認画面表示
292                                 if ($this->getMsgCount() == 0){
293                                         $this->setGuidanceMsg('この内容でコメントを投稿しますか?');
294                                         
295                                         // 入力の変更不可
296                                         $sendButtonLabel = '投稿';            // 送信ボタンラベル
297                                         $sendStatus = 1;// 送信状況を「確定」に変更
298                                 }
299                                 // ハッシュキー作成
300                                 $postTicket = md5(time() . $this->gAccess->getAccessLogSerialNo());
301                                 $request->setSessionValue(M3_SESSION_POST_TICKET, $postTicket);         // セッションに保存
302                         } else {                // ハッシュキーが異常のとき
303                                 // 送信ステータスを初期化
304                                 $sendStatus = 0;
305                                 
306                                 $request->unsetSessionValue(M3_SESSION_POST_TICKET);            // セッション値をクリア
307                         }
308                 } else if ($act == 'sendcomment' && $sendStatus == 1){  // コメント受信のとき
309                         if (!empty($postTicket) && $postTicket == $request->getSessionValue(M3_SESSION_POST_TICKET) && $isCommentValid){                // 正常なPOST値のとき
310                                 // 入力チェック
311                                 if ($this->useAuthor){
312                                         $this->checkInput($author, '投稿者');
313                                 }
314                                 if (empty($maxLength)){         // 空のときは長さのチェックなし
315                                         $this->checkInput($comment, 'コメント内容');
316                                 } else {
317                                         $this->checkLength($comment, 'コメント内容', $maxLength);
318                                 }
319                                 if ($this->useEmail) $this->checkMailAddress($email, 'Eメール', true);
320                                 
321                                 // ##### 画像ありの場合は画像を取り込む #####
322                                 if ($this->permitHtml && $this->permitImage){
323                                         // 仮登録画像を取得
324                                         $this->imageFileInfoArray = array();
325                                         $clientId = $this->gAccess->getClientId();
326                                         if (!empty($clientId)){
327                                                 $ret = $this->gInstance->getFileManager()->getAttachFileInfoByClientId(commentCommonDef::$_viewContentType, $clientId, $imageFileRows);
328                                                 if ($ret){
329                                                         for ($i = 0; $i < count($imageFileRows); $i++){
330                                                                 $fileRow = $imageFileRows[$i];
331                                                                 $newInfoObj = new stdClass;
332                                                                 $newInfoObj->title              = '';
333                                                                 $newInfoObj->filename   = '';
334                                                                 $newInfoObj->fileId             = $fileRow['af_file_id'];
335                                                                 $newInfoObj->originalUrl        = $fileRow['af_original_url'];          // 取得先URL
336                                                                 $this->imageFileInfoArray[] = $newInfoObj;
337                                                         }
338                                                 }
339                                         }
340                                         
341                                         // 画像URL変換
342                                         $this->isReadImageCheck = true;         // 画像読み込みチェック
343                                         $commentHtml = $this->convertImageUrl($commentHtml);
344                                         if ($this->isErrorInReadImage){
345                                                 $this->setUserErrorMsg('画像読み込みに失敗しました');
346                                                 $commentHtml = '';
347                                         }
348                                 }
349                                                 
350                                 if ($this->getMsgCount() == 0){
351                                         $ret = false;
352                                         if (!empty($comment) &&
353                                                 !empty($postTicket) && $postTicket == $request->getSessionValue(M3_SESSION_POST_TICKET)){               // 正常なPOST値のとき
354                                                 // コメント登録
355                                                 $commentStatus = 0;             // 未設定
356                                                 if (!$needAuthorize) $commentStatus = 2;                // 認証が必要でない場合は自動的に公開
357                                         
358                                                 // コメント追加タイプ
359                                                 $addType = 0;   // フラット(最後に追加)
360                                                 if ($this->permitHtml){                 // HTML送信の場合はBBCodeをHTMLに変換
361                                                         $ret = self::$_mainDb->addComment($addType, $this->contentType, $this->_langId, $contentsId, commentCommonDef::$_deviceType, 0/*親コメントシリアル*/, 
362                                                                                                         $title, $commentHtml, $url, $author, $email, $commentStatus, $newSerial);
363                                                 } else {
364                                                         $ret = self::$_mainDb->addComment($addType, $this->contentType, $this->_langId, $contentsId, commentCommonDef::$_deviceType, 0/*親コメントシリアル*/, 
365                                                                                                         $title, $comment, $url, $author, $email, $commentStatus, $newSerial);
366                                                 }
367                                                 
368                                                 // ##### 添付ファイル情報を更新 #####
369                                                 if ($ret){
370                                                         $ret = $this->gInstance->getFileManager()->updateAttachFileInfo(commentCommonDef::$_viewContentType, $newSerial, 0, $newSerial,
371                                                                                                                                                                                                                                                                 $this->imageFileInfoArray, $this->imageDir);
372                                                 }
373                                         }
374                                         if ($ret){
375                                                 $this->setGuidanceMsg('コメントを投稿しました');
376                                                 
377                                                 // 入力値を初期化
378                                                 $title = '';
379                                                 $comment = '';
380                                                 $commentHtml = '';
381                                                 $url = '';
382 //                                              $author = '';
383 //                                              $email = '';
384                                                 
385                                                 // 送信ステータスを更新
386                                                 $sendStatus = 0;
387                                         
388                                                 // ハッシュキー作成
389                                                 $postTicket = md5(time() . $this->gAccess->getAccessLogSerialNo());
390                                                 $request->setSessionValue(M3_SESSION_POST_TICKET, $postTicket);         // セッションに保存
391                                         } else {
392                                                 $this->setUserErrorMsg('コメントの投稿に失敗しました');
393
394                                                 // 送信ステータスを更新
395                                                 $sendStatus = 0;
396                                         
397                                                 $request->unsetSessionValue(M3_SESSION_POST_TICKET);            // セッション値をクリア
398                                         }
399                                 } else {                // 送信時入力エラーの場合は初期画面に戻す
400                                         // 送信ステータスを更新
401                                         $sendStatus = 0;
402                                         
403                                         // ハッシュキー作成
404                                         $postTicket = md5(time() . $this->gAccess->getAccessLogSerialNo());
405                                         $request->setSessionValue(M3_SESSION_POST_TICKET, $postTicket);         // セッションに保存
406                                 }
407                         } else {                // ハッシュキーが異常のとき
408                                 // 送信ステータスを初期化
409                                 $sendStatus = 0;
410                                         
411                                 $request->unsetSessionValue(M3_SESSION_POST_TICKET);            // セッション値をクリア
412                         }
413                 } else if ($act == 'sendcancel' && $sendStatus == 1){   // コメントキャンセルのとき
414                         if (!empty($postTicket) && $postTicket == $request->getSessionValue(M3_SESSION_POST_TICKET) && $isCommentValid){                // 正常なPOST値のとき
415                                 $this->setGuidanceMsg('コメントをキャンセルしました');
416                                 
417                                 // 送信ステータスを更新
418                                 $sendStatus = 0;
419
420                                 // ハッシュキー作成
421                                 $postTicket = md5(time() . $this->gAccess->getAccessLogSerialNo());
422                                 $request->setSessionValue(M3_SESSION_POST_TICKET, $postTicket);         // セッションに保存
423                         } else {                // ハッシュキーが異常のとき
424                                 // 送信ステータスを初期化
425                                 $sendStatus = 0;
426                                 
427                                 $request->unsetSessionValue(M3_SESSION_POST_TICKET);            // セッション値をクリア
428                         }
429                 } else {
430                         $isInit = true; // 初期表示かどうか
431                         
432                         // 送信ステータスを初期化
433                         $sendStatus = 0;
434                                 
435                         // ハッシュキー作成
436                         $postTicket = md5(time() . $this->gAccess->getAccessLogSerialNo());
437                         $request->setSessionValue(M3_SESSION_POST_TICKET, $postTicket);         // セッションに保存
438                         
439                         // ##### 初期表示時は仮登録の添付ファイルを削除 #####
440                         $this->gInstance->getFileManager()->cleanAttachFileInfo(commentCommonDef::$_viewContentType, $this->imageDir);
441                 }
442                 
443                 // タイトル作成
444                 $this->widgetTitle = $this->createTitle($this->contentType, $contentsId);
445                 
446                 // ##### コメントを表示 #####
447                 $showComment = false;           // コメント表示するかどうか
448                 if (!$this->userLimited || ($this->userLimited && $this->gEnv->isCurrentUserLogined())){                // ユーザ制限なし、または、ユーザ制限ありでログイン済みの場合
449                         if ($sendStatus == 0){          // 初期表示の場合はすべてのコメントを表示
450                                 //$this->tmpl->setAttribute('show_comment_list', 'visibility', 'visible');              // 既存コメントを表示
451                                 // コメント表示項目設定
452                                 if ($viewType == 1) $this->tmpl->setAttribute('show_reply', 'visibility', 'visible');           // 「返信」ボタン表示
453                                 if ($this->useTitle) $this->tmpl->setAttribute('show_title', 'visibility', 'visible');  // タイトル
454                                 if ($this->useAvatar) $this->tmpl->setAttribute('show_avatar', 'visibility', 'visible');        // アバター
455                                 
456                                 // コメント総数取得
457                                 $totalCount = self::$_mainDb->getCommentCount($this->contentType, $this->_langId, $contentsId, true/*公開コメントのみ*/);
458                                 $pageLink = $this->createPageLink($pageNo, $totalCount, $viewCount, $this->currentPageUrl);
459                 
460                                 // コメント取得
461                                 self::$_mainDb->getComment($this->contentType, $this->_langId, $contentsId, $viewCount, $pageNo, $viewDirection, array($this, 'itemsLoop'));
462                         
463                                 // コメントがない場合はリストを非表示
464                                 //if (!$this->isExistsComment) $this->tmpl->setAttribute('comment_list', 'visibility', 'hidden');
465                                 if ($this->isExistsComment) $this->tmpl->setAttribute('show_comment_list', 'visibility', 'visible');            // 既存コメントを表示
466                         } else {
467                                 // 初期表示以外の場合はリプライ先のコメントのみ表示
468                         }
469                         
470                         $showComment = true;            // コメント表示するかどうか
471                 } else {
472                         $this->message = self::MESSAGE_NOT_PERMITTED_REFER;             // 閲覧不可メッセージ
473                 }
474
475                 // コメントを受け付けるときは、コメント入力欄を表示
476                 if ($showComment && $this->commentAccept){
477                         $this->tmpl->setAttribute('add_comment', 'visibility', 'visible');              // コメント投稿欄を表示
478                         
479                         // 入力値を戻す
480                         if ($this->useTitle){
481                                 $this->tmpl->setAttribute('input_title', 'visibility', 'visible');              // タイトルあり
482                                 $this->tmpl->addVar("input_title", "title", $this->convertToDispString($title));
483                         }
484                         if ($this->useAuthor){
485                                 $this->tmpl->setAttribute('input_author', 'visibility', 'visible');             // 投稿者名あり
486                                 $this->tmpl->addVar("input_author", "author", $this->convertToDispString($author));
487                                 
488                                 // ログイン中はログインユーザ名を表示
489                                 if ($this->gEnv->isCurrentUserLogined()){
490                                         $this->tmpl->addVar("input_author", "author_disabled", 'readonly');
491                                         $this->tmpl->setAttribute('update_cookie_author', 'visibility', 'hidden');
492                                         $this->tmpl->setAttribute('init_cookie_author', 'visibility', 'hidden');
493                                 }
494                         }
495                         if ($this->useEmail){
496                                 $this->tmpl->setAttribute('input_email', 'visibility', 'visible');              // Eメールあり
497                                 $this->tmpl->addVar("input_email", "email", $this->convertToDispString($email));
498                                 
499                                 // ログイン中はログインユーザのEメールを表示
500                                 $userEmail = $this->gEnv->getCurrentUserEmail();
501                                 if ($this->gEnv->isCurrentUserLogined() && !empty($userEmail)){         // Eメールが設定されている場合のみ
502                                         $this->tmpl->addVar("input_email", "email_disabled", 'readonly');
503                                         $this->tmpl->setAttribute('update_cookie_email', 'visibility', 'hidden');
504                                         $this->tmpl->setAttribute('init_cookie_email', 'visibility', 'hidden');
505                                 }
506                         }
507                         if ($this->useUrl){
508                                 $this->tmpl->setAttribute('input_url', 'visibility', 'visible');                // URLあり
509                                 $this->tmpl->addVar("input_url", "url", $this->convertToDispString($url));
510                         }
511
512                         //if ($this->useAvatar) $this->tmpl->setAttribute('input_title', 'visibility', 'visible');              // アバターあり
513                         
514                         if ($sendStatus == 1){          // 確認画面のとき
515                                 $this->tmpl->addVar("input_title", "title_disabled", 'readonly');
516                                 $this->tmpl->addVar("input_author", "author_disabled", 'readonly');
517                                 $this->tmpl->addVar("input_email", "email_disabled", 'readonly');
518                                 $this->tmpl->addVar("input_url", "url_disabled", 'readonly');
519                                 $this->tmpl->addVar("input_comment", "comment_disabled", 'readonly');
520                                 $this->tmpl->setAttribute('cancel_button', 'visibility', 'visible');            // キャンセルボタン表示
521                                 
522                                 // クッキー停止
523                                 $this->tmpl->setAttribute('update_cookie_author', 'visibility', 'hidden');
524                                 $this->tmpl->setAttribute('init_cookie_author', 'visibility', 'hidden');
525                                 $this->tmpl->setAttribute('update_cookie_email', 'visibility', 'hidden');
526                                 $this->tmpl->setAttribute('init_cookie_email', 'visibility', 'hidden');
527                         }
528
529                         // HTML入力用の設定
530                         if ($this->permitHtml){
531                                 if ($sendStatus == 0){          // 初期状態のとき
532                                         $this->tmpl->setAttribute('input_comment', 'visibility', 'visible');            // コメント入力フィールド表示
533                                         
534                                         $this->tmpl->setAttribute('show_wysiwyg', 'visibility', 'visible');             // wysiwygエディター表示
535                                         $this->addLib[] = ScriptLibInfo::LIB_CKEDITOR;          // CKEditorライブラリを追加
536                                 } else {
537                                         $this->tmpl->setAttribute('show_comment', 'visibility', 'visible');
538                                         $this->tmpl->addVar('show_comment', 'comment_html', $commentHtml);                      // HTMLコメント
539                                         $this->tmpl->addVar('show_comment', 'comment', $this->convertToDispString($comment));                   // コメントソース
540                                 }
541                         } else {
542                                 $this->tmpl->setAttribute('input_comment', 'visibility', 'visible');    // コメント入力フィールド表示
543                                 $this->tmpl->addVar('input_comment', 'comment', $this->convertToDispString($comment));                  // コメントソース
544                         }
545
546                         // コメントID作成
547                         $commentId = md5($this->contentType . self::COMMENT_ID_SEPARATOR . $contentsId);                        // コメントID作成用セパレータ
548                         
549                         $this->tmpl->addVar("add_comment", "send_button_label", $sendButtonLabel);// 送信ボタンラベル
550                         $this->tmpl->addVar("add_comment", "send_status", $sendStatus);// 送信状況
551                         $this->tmpl->addVar("add_comment", "status",    $sendStatus);                   // 送信ステータス
552                         $this->tmpl->addVar("add_comment", "ticket", $postTicket);                              // 画面確認用
553                         $this->tmpl->addVar("add_comment", "comment_id", $commentId);                           // コメントID
554                         
555                         //if ($sendStatus != 0 || $this->getMsgCount() > 0){            // 初期状態以外または入力エラーがあるとき
556                         if (!$isInit){          // 初期表示以外のとき
557                                 // コメント入力エリアをトップに表示
558                                 $this->tmpl->setAttribute('scrollup_comment', 'visibility', 'visible');
559                         }
560                                 
561                         // 名前保存用のスクリプトライブラリ追加
562                         $this->tmpl->setAttribute('init_form', 'visibility', 'visible');
563                         $this->tmpl->setAttribute('update_cookie', 'visibility', 'visible');
564                         $this->addLib[] = self::COOKIE_LIB;
565                         
566                 } else {                // コメント投稿不可の場合
567                         if (empty($this->message)) $this->message = self::MESSAGE_NOT_PERMITTED_POST;           // 投稿不可メッセージ
568                 }
569                 
570                 // コメントがないときは投稿なしメッセージを表示
571                 if (empty($this->message) && !$this->isExistsComment){
572                         $this->message = self::MESSAGE_NO_COMMENT;
573                 }
574                 
575                 // 初期表示の場合で入力エラーメッセージがない場合はメッセージを表示
576                 //if ($sendStatus == 0 && $this->getMsgCount() == 0 && !empty($this->message)){
577                 if ($isInit && !empty($this->message)){
578                         $this->tmpl->setAttribute('message', 'visibility', 'visible');
579                         $this->tmpl->addVar("message", "message", $this->convertToDispString($this->message));
580                 }
581         }
582         /**
583          * ウィジェットのタイトルを設定
584          *
585          * @param RequestManager $request               HTTPリクエスト処理クラス
586          * @param object         $param                 任意使用パラメータ。そのまま_assign()に渡る
587          * @return string                                               ウィジェットのタイトル名
588          */
589         function _setTitle($request, &$param)
590         {
591                 return $this->widgetTitle;
592         }
593         /**
594          * ウィジェット終了処理
595          *
596          * @param RequestManager $request               HTTPリクエスト処理クラス
597          * @return                                                              なし
598          */
599         function exitWidget()
600         {
601                 if ($this->cmd == M3_REQUEST_CMD_DO_WIDGET){    // ウィジェット単体実行
602                         // ダウンロード不可のときはエラーログを残す
603                         $msgDetail = '';
604                         if (!empty($this->commentSerialNo)) $msgDetail .= 'コメントシリアル番号=' . $this->commentSerialNo;
605                         $this->writeUserError(__METHOD__, '画像ファイルへの不正なアクセスを検出しました。画像ID=' . $imageId, 2200, $msgDetail);
606                         
607                         // ページ作成処理中断
608                         $this->gPage->abortPage();
609                                                 
610                         // レスポンスヘッダ設定
611                         $this->gPage->setResponse(403/*アクセス禁止*/);
612
613                         // システム強制終了
614                         $this->gPage->exitSystem();
615                 } else {
616                         $this->cancelParse();           // テンプレート変換処理中断
617                 }
618         }
619         /**
620          * 画像ダウンロード処理
621          *
622          * @param RequestManager $request               HTTPリクエスト処理クラス
623          * @return                                                              なし
624          */
625         function downloadImage($request)
626         {
627                 $imageId = $request->trimValueOf(commentCommonDef::REQUEST_PARAM_IMAGE_ID);     // 画像ID
628
629                 // ページ作成処理中断
630                 $this->gPage->abortPage();
631                 
632                 // 添付ファイル情報を取得
633                 $downloadCompleted = false;                             // ダウンロード処理完了かどうか
634                 
635                 if (empty($this->commentSerialNo)){             // まだ画像が登録されていない場合
636                         $downloadFile = $this->imageDir . DIRECTORY_SEPARATOR . $imageId;
637                         $downloadFilename = $attachFileRow['af_filename'];
638                         if (empty($downloadFilename)) $downloadFilename = $attachFileRow['af_original_filename'];
639                 
640                         // ダウンロード処理
641                         $ret = $this->gPage->downloadFile($downloadFile, $downloadFilename);
642                         $downloadCompleted = true;                              // ダウンロード処理完了かどうか
643                 } else {
644                         $ret = $this->gInstance->getFileManager()->getAttachFileInfo(commentCommonDef::$_viewContentType, $this->commentSerialNo, $attachFileRows);
645                         if ($ret){
646                                 for ($i = 0; $i < count($attachFileRows); $i++){
647                                         $fileRow = $attachFileRows[$i];
648                                         if ($imageId == $fileRow['af_file_id']){
649                                                 //$downloadFile = commentCommonDef::getAttachFileDir() . DIRECTORY_SEPARATOR . $imageId;
650                                                 $downloadFile = $this->imageDir . DIRECTORY_SEPARATOR . $imageId;
651                                                 $downloadFilename = $fileRow['af_filename'];
652                                                 if (empty($downloadFilename)) $downloadFilename = $fileRow['af_original_filename'];
653                                         
654                                                 // ダウンロード処理
655                                                 $ret = $this->gPage->downloadFile($downloadFile, $downloadFilename);
656                                                 $downloadCompleted = true;                              // ダウンロード処理完了かどうか
657                                                 break;
658                                         }
659                                 }
660                         }
661                 }
662                 if ($downloadCompleted){                // ダウンロード処理完了のとき
663                         // ダウンロードログを残す
664                         $this->gInstance->getAnalyzeManager()->logContentDownload(commentCommonDef::$_viewContentType . commentCommonDef::DOWNLOAD_TYPE_IMAGE, $imageId);
665                 } else {
666                         $msgDetail = '';
667                         if (!empty($this->commentSerialNo)) $msgDetail .= 'コメントシリアル番号=' . $this->commentSerialNo;
668                         $this->writeError(__METHOD__, '画像ファイルのダウンロードに失敗しました。画像ファイルが見つかりません。画像ID=' . $imageId, 2200, $msgDetail);
669                 }
670
671                 // システム強制終了
672                 $this->gPage->exitSystem();
673         }
674         /**
675          * タイトル作成
676          *
677          * @param string $contentType           コンテンツタイプ
678          * @param string $contentsId            共通コンテンツID
679          * @param string                                        タイトル
680          */
681         function createTitle($contentType, $contentsId)
682         {
683                 $title = self::DEFAULT_TITLE;
684                 
685                 switch ($contentType){
686                         case M3_VIEW_TYPE_CONTENT:                              // 汎用コンテンツ
687                                 $ret = self::$_mainDb->getContentById(''/*PC用コンテンツ*/, $this->_langId, $contentsId, $row);
688                                 if ($ret) $contentName = $row['cn_name'];
689                                 break;
690                         case M3_VIEW_TYPE_PRODUCT:                              // 商品情報(Eコマース)
691                                 $ret = self::$_mainDb->getProductById($contentsId, $this->_langId, $row);
692                                 if ($ret) $contentName = $row['pt_name'];
693                                 break;
694                         case M3_VIEW_TYPE_BBS:                                  // BBS
695                                 // 未使用
696                                 break;
697                         case M3_VIEW_TYPE_BLOG:                         // ブログ
698                                 $ret = self::$_mainDb->getEntryById($contentsId, $this->_langId, $row);
699                                 if ($ret) $contentName = $row['be_name'];
700                                 break;
701                         case M3_VIEW_TYPE_WIKI:                         // wiki
702                                 $contentName = $contentsId;
703                                 break;
704                         case M3_VIEW_TYPE_USER:                         // ユーザ作成コンテンツ
705                                 $ret = self::$_mainDb->getRoomById($contentsId, $this->_langId, $row);
706                                 if ($ret) $contentName = $row['ur_name'];
707                                 break;
708                         case M3_VIEW_TYPE_EVENT:                                // イベント情報
709                                 $ret = self::$_mainDb->getEventById($contentsId, $this->_langId, $row);
710                                 if ($ret) $contentName = $row['ee_name'];
711                                 break;
712                         case M3_VIEW_TYPE_PHOTO:                                // フォトギャラリー
713                                 $ret = self::$_mainDb->getPhotoById($contentsId, $this->_langId, $row);
714                                 if ($ret) $contentName = $row['ht_name'];
715                                 break;
716                 }
717                 if (!empty($contentName)){
718                         $title = sprintf(self::COMMENT_TITLE_FORMAT, $contentName);
719                 }
720                 return $title;
721         }
722         /**
723          * JavascriptライブラリをHTMLヘッダ部に設定
724          *
725          * JavascriptライブラリをHTMLのheadタグ内に追加出力する。
726          * _assign()よりも後に実行される。
727          *
728          * @param RequestManager $request               HTTPリクエスト処理クラス
729          * @param object         $param                 任意使用パラメータ。
730          * @return string,array                                 Javascriptライブラリ。出力しない場合は空文字列を設定。
731          */
732         function _addScriptLibToHead($request, &$param)
733         {
734                 return $this->addLib;
735         }
736         /**
737          * 取得したコンテンツ項目をテンプレートに設定する
738          *
739          * @param int           $index                  行番号
740          * @param array         $fetchedRow             取得行
741          * @param object        $param                  任意使用パラメータ
742          * @return bool                                         trueを返すとループ続行。falseを返すとその時点で終了。
743          */
744         function itemsLoop($index, $fetchedRow)
745         {
746                 $contentsId = $fetchedRow['cm_contents_id'];            // 共通コンテンツID
747                 $permaLink = commentCommonDef::COMMENT_PERMA_HEAD . $fetchedRow['cm_no'];               // コメントパーマリンク
748                 $titleTag = '';
749                 if ($this->useTitle){
750                         $commentTitle = $fetchedRow['cm_title'];                        // コメントタイトル
751                         if (empty($commentTitle)) $commentTitle = self::NO_COMMENT_TITLE;
752                         $titleTag = '<a name="' . $permaLink . '" href="#' . $permaLink . '">' . $this->convertToDispString($commentTitle) . '</a>';
753                 }
754                 $permaUrl = commentCommonDef::createCommentUrl($this->contentType, $contentsId, $fetchedRow['cm_no']);
755                 $permaTag = '<a href="' . $this->convertUrlToHtmlEntity($permaUrl) . '"><img src="' . $this->getUrl($this->gEnv->getRootUrl() . self::PERMALINK_ICON_FILE) . 
756                                                 '" width="' . self::PERMA_BUTTON_ICON_SIZE . '" height="' . self::PERMA_BUTTON_ICON_SIZE . '" title="パーマリンク" alt="パーマリンク" style="border:none;margin:0;padding:0;" /></a>';
757                 $commentTag = $fetchedRow['cm_message'];
758                 
759                 // コメント投稿ユーザ名
760                 if ($this->useAuthor){
761                         $userName = $fetchedRow['lu_name'];
762                         if (empty($userName)) $userName = $fetchedRow['cm_author'];
763                 }
764                 if (empty($userName)) $userName = self::NO_COMMENT_AUTHOR;
765         
766                 // アバター
767                 $avatarTag = '';
768                 if ($this->useAvatar){
769                         $avatarUrl = $this->gInstance->getImageManager()->getAvatarUrl($fetchedRow['lu_avatar']);
770                         $avatarTitle = $this->convertToDispString($userName) . self::AVATAR_TITLE_TAIL;
771                         $avatarTag = '<img src="' . $this->getUrl($avatarUrl) . '" width="' . $this->avatarSize . '" height="' . $this->avatarSize . 
772                                                         '" border="0" alt="' . $avatarTitle . '" title="' . $avatarTitle . '" />';
773                 }
774
775                 // URL
776                 $urlTag = '';
777                 if ($this->useUrl){
778                         $url = $fetchedRow['cm_url'];
779                         if (!empty($url)) $urlTag = '<br />URL: <a href="' . $this->convertUrlToHtmlEntity($url) . '" target="_blank">' . $this->convertToDispString($url) . '</a>';
780                 }
781
782                 // コメント内容
783                 if ($this->permitHtml){                 // HTMLコメントの場合
784                         // BBコード変換で自動リンクは作成される
785                         // Magic3マクロの変換
786                         //$commentTag = $this->convertM3ToHtml($fetchedRow['cm_message'], true/*改行コーをbrタグに変換*/);
787                         $commentTag = str_replace(M3_TAG_START . M3_TAG_MACRO_ROOT_URL . M3_TAG_END, $this->currentPageRootUrl, $commentTag);// アプリケーションルートを変換
788                 } else {
789                         if ($this->autolink){           // 自動リンクの場合
790                                 $commentTag = preg_replace("/(https?):\/\/([\w;\/\?:\@&=\+\$,\-\.!~\*'\(\)%#]+)/", "<a href=\"$1://$2\" target=\"_blank\">$1://$2</a>", $commentTag);
791                         }
792                         $commentTag = $this->convertToPreviewText($this->convertToDispString($commentTag));// 改行コードをbrタグに変換
793                 }
794
795                 $row = array(
796                         'title' => $titleTag,           // タイトル
797                         'avatar' => $avatarTag,                 // アバター
798                         'author' => $this->convertToDispString($userName),      // 投稿者名
799                         'date' => $this->timestampToDate($fetchedRow['cm_create_dt']),          // コメント投稿日(日付)
800                         'time' => $this->timestampToTime($fetchedRow['cm_create_dt']),          // コメント投稿日(時間)
801                         'permalink' => $permaTag,                       // パーマリンクアイコン
802                         'url'   => $urlTag,             // URL
803                         'comment' => $commentTag        // コメント内容
804                 );
805                 $this->tmpl->addVars('comment_list', $row);
806                 $this->tmpl->parseTemplate('comment_list', 'a');
807                 $this->isExistsComment = true;                          // 表示データがあるかどうか
808                 return true;
809         }
810         /**
811          * ページリンク作成
812          *
813          * @param int $pageNo                   ページ番号(1~)。ページ番号が範囲外にある場合は自動的に調整
814          * @param int $totalCount               総項目数
815          * @param int $viewItemCount    1ページあたりの項目数
816          * @param string $baseUrl               リンク用のベースURL
817          * @return string                               リンクHTML
818          */
819         function createPageLink(&$pageNo, $totalCount, $viewItemCount, $baseUrl)
820         {
821                 // 表示するページ番号の修正
822                 $pageCount = (int)(($totalCount -1) / $viewItemCount) + 1;              // 総ページ数
823                 if ($pageNo < 1) $pageNo = 1;
824                 if ($pageNo > $pageCount) $pageNo = $pageCount;
825
826                 // ページング用リンク作成
827                 $pageLink = '';
828                 if ($pageCount > 1){    // ページが2ページ以上のときリンクを作成
829                         // ページ数1から「LINK_PAGE_COUNT」までのリンクを作成
830                         $maxPageCount = $pageCount < self::LINK_PAGE_COUNT ? $pageCount : self::LINK_PAGE_COUNT;
831                         for ($i = 1; $i <= $maxPageCount; $i++){
832                                 if ($i == $pageNo){
833                                         $link = '&nbsp;' . $i;
834                                 } else {
835                                         $linkUrl = $this->getUrl($baseUrl . '&page=' . $i, true/*リンク用*/);
836                                         $link = '&nbsp;<a href="' . $this->convertUrlToHtmlEntity($linkUrl) . '" >' . $i . '</a>';
837                                 }
838                                 $pageLink .= $link;
839                         }
840                         // 残りは「...」表示
841                         if ($pageCount > self::LINK_PAGE_COUNT) $pageLink .= '&nbsp;...';
842                 }
843                 if ($pageNo > 1){               // 前ページがあるとき
844                         $linkUrl = $this->getUrl($baseUrl . '&page=' . ($pageNo -1), true/*リンク用*/);
845                         $link = '<a href="' . $this->convertUrlToHtmlEntity($linkUrl) . '" >前へ</a>';
846                         $pageLink = $link . $pageLink;
847                 }
848                 if ($pageNo < $pageCount){              // 次ページがあるとき
849                         $linkUrl = $this->getUrl($baseUrl . '&page=' . ($pageNo +1), true/*リンク用*/);
850                         $link = '&nbsp;<a href="' . $this->convertUrlToHtmlEntity($linkUrl) . '" >次へ</a>';
851                         $pageLink .= $link;
852                 }
853                 return $pageLink;
854         }
855         /**
856          * BBCodeのコメントを解析し、HTMLに変換
857          *
858          * @param string $src           コメント入力
859          * @return string                       HTML変換したコメント
860          */
861         function parseComment($src)
862         {
863                 $commentHtml = $this->gInstance->getTextConvManager()->convBBCodeToHtml($src, true);
864                 return $commentHtml;
865         }
866         /**
867          * コンテンツの画像URLを変換
868          *
869          * @param string $html          変換元コンテンツ
870          * @return string                       変換後コンテンツ
871          */
872         function convertImageUrl($html)
873         {
874                 $str = '/<img[^<]*?src\s*=\s*[\'"]+(.+?)[\'"]+[^>]*?>/si';
875                 $dest = preg_replace_callback($str, array($this, "_convert_image_url_callback"), $html);
876                 return $dest;
877         }
878         /**
879          * IMGタグURL変換コールバック関数
880          *
881          * @param array $matchData              検索マッチデータ
882          * @return string                               変換後データ
883          */
884     function _convert_image_url_callback($matchData)
885         {
886                 static $index = 0;
887                 
888                 // エラーチェック
889                 if ($this->isErrorInReadImage) return '';               // 画像読み込みエラー発生
890                 
891                 //$destTag = $matchData[0];     // マッチしたタグ全体
892                 $destTag = '';
893                 $imageUrl = $matchData[1];
894                 if ($this->gEnv->isSystemUrlAccess($imageUrl)){         // システム内のファイルのとき
895                 } else {
896                         if ($this->isReadImageCheck){
897                                 if ($index < count($this->imageFileInfoArray)){
898                                         $fileInfo = $this->imageFileInfoArray[$index];
899                                         if (!empty($imageUrl) && $imageUrl == $fileInfo->originalUrl){  // 取得先URL比較
900                                                 $imageId = $fileInfo->fileId;           // 画像ID
901                                                 $imagePath = $this->imageDir . DIRECTORY_SEPARATOR . $imageId;
902                                                 
903                                                 // 画像サイズ取得
904                                                 $this->gInstance->getImageManager()->getImageInfo($imagePath, $width, $height);
905                                                 
906                                                 $param = commentCommonDef::REQUEST_PARAM_IMAGE_ID . '=' . $imageId;
907                                                 $newUrl = $this->createCmdUrlToCurrentWidget($param, true/*マクロ形式で取得*/);
908                                                 $destTag = '<img src="' . $newUrl . '" width="' . $width . '" height="' . $height . '" />';
909                                         } else {
910                                                 $this->isErrorInReadImage = true;               // 画像読み込みエラー発生
911                                         }
912                                 } else {
913                                         $this->isErrorInReadImage = true;               // 画像読み込みエラー発生
914                                 }
915                         } else {
916                                 if (empty($imageUrl)){
917                                         $this->isErrorInReadImage = true;               // 画像読み込みエラー発生
918                                 } else {
919                                         // 画像ファイル名作成
920                                         $imageId = $this->gInstance->getFileManager()->createRandFileId();
921                                         $originalFilename = basename($imageUrl);                // 元のファイル名
922                                         $imagePath = $this->imageDir . DIRECTORY_SEPARATOR . $imageId;
923                                                 
924                                         // 画像を取り込む
925                                         $tmpFile = tempnam($this->gEnv->getWorkDirPath(), M3_SYSTEM_WORK_UPLOAD_FILENAME_HEAD);
926                                         if ($tmpFile !== false){
927                                                 // 一時ファイルに読み込む
928                                                 $ret = true;
929                                                 if ($input = @fopen($imageUrl, 'r')){
930                                                         $ret = file_put_contents($tmpFile, $input);
931                                                         if ($ret !== false) $ret = true;
932                                                         @fclose($input);
933                                                 } else {
934                                                         $ret = false;
935                                                 }
936
937                                                 // 画像作成
938                                                 if ($ret){
939                                                         $ret = $this->gInstance->getImageManager()->createImage($tmpFile, $imagePath, $this->maxImageSize, commentCommonDef::OUTPUT_IMAGE_TYPE, $destSize);
940                                                 }
941
942                                                 // 画像登録
943                                                 if ($ret){
944                                                         $ret = $this->gInstance->getFileManager()->addAttachFileInfo(commentCommonDef::$_viewContentType, $imageId, $imagePath, $originalFilename, $imageUrl);
945                                                 }
946
947                                                 // 画像が作成できない場合は画像を表示しない
948                                                 if ($ret){
949                                                         $param = commentCommonDef::REQUEST_PARAM_IMAGE_ID . '=' . $imageId;
950                                                         $newUrl = $this->createCmdUrlToCurrentWidget($param);
951                                                         $destTag = '<img src="' . $this->getUrl($newUrl) . '" width="' . $destSize['width'] . '" height="' . $destSize['height'] . '" />';
952                                                 }
953                                                 if (!$ret) $this->isErrorInReadImage = true;            // 画像読み込みエラー発生
954
955                                                 // 一時ファイル削除
956                                                 unlink($tmpFile);
957                                         } else {
958                                                 $this->isErrorInReadImage = true;               // 画像読み込みエラー発生
959                                         }
960                                 }
961                         }
962                 }
963                 $index++;
964                 return $destTag;
965     }
966 }
967 ?>