OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / chacha_main / include / container / chacha_mainBaseWidgetContainer.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    マイクロブログ
10  * @author     株式会社 毎日メディアサービス
11  * @copyright  Copyright 2010 株式会社 毎日メディアサービス.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: chacha_mainBaseWidgetContainer.php 3352 2010-07-08 02:59:39Z fishbone $
14  * @link       http://www.m-media.co.jp
15  */
16 require_once($gEnvManager->getContainerPath() . '/baseWidgetContainer.php');
17 require_once($gEnvManager->getCurrentWidgetDbPath() . '/chacha_mainDb.php');
18
19 class chacha_mainBaseWidgetContainer extends BaseWidgetContainer
20 {
21         protected $_db;                 // DB接続オブジェクト
22         protected $_configArray;                // BBS定義値
23         protected $_headCss;    // CSS定義
24         protected $_boardId;    // 掲示板ID
25         protected $_currentPageUrl;     // 現在のページのURL
26         protected $_autolink;           // リンクを自動作成
27         const CREATE_CODE_RETRY_COUNT = 10;                             // コード生成のリトライ数
28         const DEFAULT_BBS_ID_HEAD = 'board';            // デフォルトの掲示板ID
29         const DEFAULT_LAST_MESSAGE_COUNT = 50;  // メッセージ取得用デフォルトの最新メッセージ数
30         const DEFAULT_MESSAGE_COUNT_PER_PAGE = 100;     // メッセージ取得用デフォルトの1ページあたりメッセージ数
31         const AVATAR_DIR = '/widgets/chacha/avatar';                    // アバター格納ディレクトリ
32         const AVATAR32_DIR = '/widgets/chacha/avatar32';                        // アバター(小)格納ディレクトリ
33         const DEFAULT_AVATAR_ICON_FILE = '/images/default_avatar.gif';          // デフォルトのアバターアイコン
34         const DEFAULT_AVATAR_FILE_EXT = 'gif';                  // アバターファイルのデフォルト拡張子
35         const DEFAULT_SMALL_AVATAR_FILE_EXT = 'gif';                    // アバター(小)ファイルのデフォルト拡張子
36         const AVATAR_TAG_ID = 'chacha_avatar_image';                    // アバタータグID
37         const AVATAR_SIZE = 64;                 // アバター画像のサイズ
38         const SMALL_AVATAR_SIZE = 32;                   // アバター画像(小)のサイズ
39         const TOP_MESSAGE_LENGTH = 100;         // トップページのメッセージ長
40         // DB定義値
41         const CF_BBS_TITLE = 'title';                   // 掲示板タイトル
42         const CF_TITLE_COLOR = 'title_color';   // タイトルカラー
43         const CF_TOP_LINK = 'top_link';         // トップ画像のリンク先
44         const CF_TOP_IMAGE = 'top_image';               // トップ画像
45         const CF_BBS_GUIDE = 'bbs_guide';               // 掲示板規則
46         const CF_BG_IMAGE = 'bg_image';         // 背景画像
47         
48         const CF_BG_COLOR = 'bg_color';         // 背景色
49         const CF_INNER_BG_COLOR = 'inner_bg_color';             // 内枠のデフォルト背景色
50         const CF_PROFILE_COLOR = 'profile_color';               // プロフィール背景色
51         
52         const CF_MENU_COLOR = 'menu_color';             // メニュー背景色
53         const CF_MAKE_THREAD_COLOR = 'makethread_color';                // スレッド作成部背景色
54         const CF_THREAD_COLOR = 'thread_color';         // スレッド表示部背景色
55         const CF_TEXT_COLOR = 'text_color';             // 文字色
56                 
57         const CF_SUBJECT_LENGTH = 'subject_length';             // 件名最大長
58         const CF_NAME_LENGTH = 'name_length';           // 投稿者名最大長
59         const CF_EMAIL_LENGTH = 'email_length';         // emailアドレス最大長
60         const CF_MESSAGE_LENGTH = 'message_length';     // 最大メッセージ長
61         const CF_ERR_MESSAGE_COLOR = 'err_message_color';               // エラーメッセージ文字色
62         const CF_LINE_LENGTH = 'line_length';           // 投稿文行長
63         const CF_LINE_COUNT = 'line_count';                     // 投稿文行数
64         const CF_RES_ANCHOR_LINK_COUNT = 'res_anchor_link_count';               // レスアンカーリンク数
65         const CF_THREAD_COUNT = 'thread_count';         // トップ画面に表示するスレッド最大数
66         const CF_RES_COUNT = 'res_count';               // トップ画面に表示するレス最大数
67         const CF_THREAD_RES = 'thread_res';             // 1スレッドに投稿できるレス数の上限
68         const CF_MENU_THREAD_COUNT = 'menu_thread_count';       // メニューに表示するスレッド最大数
69         const CF_SHOW_EMAIL = 'show_email';             // Eメールアドレスを表示
70         const CF_AUTOLINK = 'autolink';                 // 自動的にリンクを作成
71         const CF_NONAME_NAME = 'noname_name';                           // 名前未設定時の表示名
72         const CF_MESSAGE_COUNT_TOP = 'message_count_top';       // トップページのメッセージ表示項目数
73         const CF_MESSAGE_COUNT_MYPAGE = 'message_count_mypage'; // マイページのメッセージ表示項目数
74         const CF_TOP_CONTENTS = 'top_contents';         // トップコンテンツ
75         // 画面
76         const TASK_TOP = 'top';                 // トップ画面
77         const TASK_SUBJECT = 'subject';                 // スレッド件名
78         const TASK_THREAD = 'thread';                   // スレッド処理
79         const TASK_NEW_THREAD = 'newthread';            // スレッド新規作成
80         const TASK_READ = 'read';               // スレッド表示
81         const TASK_PROFILE = 'profile';                 // プロフィール表示
82         const TASK_MYPAGE = 'mypage';                   // マイページ表示
83         // URL用パラメータ
84         const URL_PARAM_MEMBER_ID = 'memberid';         // 会員ID
85         const URL_PARAM_MESSAGE_ID = 'messageid';               // メッセージID
86         
87         // CSS
88         const CSS_BLOG_STYLE = 'width:100%;text-align:left;padding:10px 0 10px 0;';             // ベースのスタイル
89         const CSS_BLOG_TOP_STYLE = 'width:95%;text-align:left;padding:0 0 5px 0;';                                              // トップコンテンツ枠のスタイル
90         const CSS_BLOG_INNER_STYLE = 'width:95%;text-align:left;padding:5px 0 5px 0;';                                  // 内枠のスタイル
91         const CSS_LINK_STYLE_TOP = 'margin:0 10px;text-align:right;';   // 上のリンク部のスタイル
92         const CSS_LINK_STYLE_BOTTOM = 'margin:0 10px;';                                 // 下のリンク部のスタイル
93         const CSS_LINK_STYLE_INNER_BOTTOM = 'margin:0 10px;text-align:right;';  // 内枠の下のリンク部のスタイル
94         
95         /**
96          * コンストラクタ
97          */
98         function __construct()
99         {
100                 // 親クラスを呼び出す
101                 parent::__construct();
102                 
103                 // サブウィジェット起動のときだけ初期処理実行
104                 if ($this->gEnv->getIsSubWidget()){
105                         // DBオブジェクト作成
106                         $this->_db = new chacha_mainDb();
107                 
108                         // 定義ID取得
109                         // 定義IDから掲示板IDを作成
110                         $configId = $this->gEnv->getCurrentWidgetConfigId();
111                         $this->_boardId = self::DEFAULT_BBS_ID_HEAD . intval($configId);
112                 
113                         // BBS定義を読み込む
114                         $this->_loadConfig($this->_boardId);
115                 
116                         $this->_currentPageUrl = $this->gEnv->createCurrentPageUrl();   // 現在のページのURL
117                         $this->_autolink = $this->_configArray[self::CF_AUTOLINK];              // リンクを自動作成
118                 }
119         }
120         /**
121          * テンプレートにデータ埋め込む
122          *
123          * _setTemplate()で指定したテンプレートファイルにデータを埋め込む。
124          *
125          * @param RequestManager $request               HTTPリクエスト処理クラス
126          * @param object         $param                 任意使用パラメータ。_setTemplate()と共有。
127          * @return                                                              なし
128          */
129         function _postAssign($request, &$param)
130         {
131                 // Ajax処理のときは終了
132                 if ($param == 'ajax') return;
133                 
134                 // トップ画像
135 /*              $titleColor = $this->_configArray[self::CF_TITLE_COLOR];        // タイトルカラー
136                 $topImage = $this->_configArray[self::CF_TOP_IMAGE];
137                 if (!empty($topImage)){
138                         $topLink = $this->convertUrlToHtmlEntity($this->_configArray[self::CF_TOP_LINK]);               // トップ画像のリンク先
139                         $topImage = $this->gEnv->getCurrentWidgetImagesUrl() . '/' . $topImage;
140                         $topImage = '<img src="' . $topImage . '" border="0" />';
141                         if (!empty($topLink)) $topImage = '<a href="' . $topLink . '">' . $topImage . '</a>';
142                 }
143                 $this->tmpl->addVar("_widget", "top_image", $topImage);         // トップ画像
144                 
145                 // 掲示板タイトル、説明
146                 $title = $this->_configArray[self::CF_BBS_TITLE];                       // 掲示板タイトル
147                 $this->tmpl->addVar("_widget", "bbs_title", $this->convertToDispString($title));
148                 $this->tmpl->addVar("_widget", "guide", $this->_configArray[self::CF_BBS_GUIDE]);*/
149                 
150                 // 共通のスタイル
151                 $bbsStyle = self::CSS_BLOG_STYLE;
152                 $bgImage = $this->gEnv->getCurrentWidgetImagesUrl() . '/' . $this->_configArray[self::CF_BG_IMAGE];
153                 $bgColor = $this->_configArray[self::CF_BG_COLOR];
154                 $textColor = $this->_configArray[self::CF_TEXT_COLOR];          // 文字色
155 //              $bbsStyle .= 'background-image:url(' . $bgImage . ');';
156                 $bbsStyle .= 'background-color:' . $bgColor . ';';
157 //              $bbsStyle .= 'color:' . $textColor . ';';
158                 $this->tmpl->addVar("_widget", "blog_style", $bbsStyle);
159                 $this->tmpl->addVar("top_link_area", "link_style_top", self::CSS_LINK_STYLE_TOP);// 上下のリンク部のスタイル
160                 $this->tmpl->addVar("_widget", "link_style_bottom", self::CSS_LINK_STYLE_BOTTOM);// 下のリンク部のスタイル
161                 $this->tmpl->addVar("_widget", "link_style_inner_bottom", self::CSS_LINK_STYLE_INNER_BOTTOM);// 内枠下のリンク部のスタイル
162                 
163                 $menuColor = $this->_configArray[self::CF_MENU_COLOR];          // メニュー背景色
164                 $menuStyle .= 'background-color:' . $menuColor . ';';
165                 $this->tmpl->addVar("_widget", "menu_style", $menuStyle);
166                 
167                 // メッセージカラーを設定
168                 if ($this->getMsgCount(1) > 0 || $this->getMsgCount(2) > 0){            // エラーメッセージが出力されているとき
169                         $errMessageColor = $this->_configArray[self::CF_ERR_MESSAGE_COLOR];             // エラーメッセージ色
170                         if (!empty($errMessageColor)){
171                                 $errMessageStyle .= 'color:' . $errMessageColor . ';';
172                                 $attr = 'style="' . $errMessageStyle . '"';
173                                 $this->setMessageAttr($attr);
174                         }
175                 }
176                 
177                 // 追加CSSを作成
178                 $cssId = $this->gEnv->getCurrentWidgetId() . '_0';
179                 $this->tmpl->addVar("_widget", "css_id",        $cssId);        // CSS用ID
180                 
181                 // 遷移先を設定
182                 $this->tmpl->addVar("_widget", "home_url", $this->convertUrlToHtmlEntity($this->getUrl($this->gEnv->createCurrentPageUrl(), true)));
183 //              $this->tmpl->addVar("_widget", "subject_url",   $this->convertUrlToHtmlEntity($this->getUrl($this->gEnv->createCurrentPageUrl() . '&task=' . self::TASK_SUBJECT, true)));       // スレッド件名
184 //              $this->tmpl->addVar("_widget", "newthread_url", $this->convertUrlToHtmlEntity($this->getUrl($this->gEnv->createCurrentPageUrl() . '&task=' . self::TASK_NEW_THREAD, true)));    // 新規スレッド作成
185         }
186         /**
187          * CSSデータをHTMLヘッダ部に設定
188          *
189          * CSSデータをHTMLのheadタグ内に追加出力する。
190          * _assign()よりも後に実行される。
191          *
192          * @param RequestManager $request               HTTPリクエスト処理クラス
193          * @param object         $param                 任意使用パラメータ。
194          * @return string                                               CSS文字列。出力しない場合は空文字列を設定。
195          */
196         function _addCssToHead($request, &$param)
197         {
198                 return $this->_headCss;
199         }
200         /**
201          * ブログ定義値をDBから取得
202          *
203          * @param string $boardId       掲示板ID
204          * @return bool                         true=取得成功、false=取得失敗
205          */
206         function _loadConfig($boardId)
207         {
208                 $this->_configArray = array();
209
210                 // BBS定義を読み込み
211                 $ret = $this->_db->getAllConfig($rows, $boardId);
212                 if ($ret){
213                         // 取得データを連想配列にする
214                         $configCount = count($rows);
215                         for ($i = 0; $i < $configCount; $i++){
216                                 $key = $rows[$i]['mc_id'];
217                                 $value = $rows[$i]['mc_value'];
218                                 $this->_configArray[$key] = $value;
219                         }
220                 }
221                 return $ret;
222         }
223         /**
224          * 投稿文入力データのエラーチェック
225          *
226          * @param string $boardId               掲示板ID
227          * @param string $threadId              スレッドID
228          * @param string $name                  ユーザ名
229          * @param string $email                 Eメールアドレス
230          * @param string $message               投稿文
231          * @return bool                                 true=正常終了、false=異常終了
232          */
233         function checkMessageInput($boardId, $threadId, $name, $email, $message)
234         {
235                 // 入力チェック
236                 $this->checkInput($threadId, 'スレッドID');
237                 $this->checkInput($message, 'メッセージ');
238                 
239                 if ($this->getMsgCount() == 0){
240                         // スレッドIDが正しいかチェック
241                         $ret = $this->_db->getThreadInfo($boardId, $threadId, $row);
242                         if ($ret){
243                                 // レス可能かチェック
244                                 $resCount = $this->_configArray[self::CF_THREAD_RES];
245                                 if ($row['th_message_count'] > $resCount){
246                                         $this->setUserErrorMsg('このスレッドは' . $resCount . 'を超えました。<br /> もう書けないので、新しいスレッドを立ててくださいです。。。');
247                                 }
248                         } else {
249                                 $this->setUserErrorMsg('ERROR:スレッドIDが不正です!');
250                         }
251                         
252                         // 本文
253                         if (strlen($message) > $this->_configArray[self::CF_MESSAGE_LENGTH]){
254                                 $this->setUserErrorMsg('ERROR:本文が長すぎます!');
255                         }
256
257                         // 名前
258                         if (strlen($name) > $this->_configArray[self::CF_NAME_LENGTH]) $this->setUserErrorMsg('ERROR:名前が長すぎます!');
259                         
260                         // メールアドレス
261                         if (strlen($email) > $this->_configArray[self::CF_EMAIL_LENGTH]) $this->setUserErrorMsg('ERROR:メールアドレスが長すぎます!');
262                 }
263                 if ($this->getMsgCount() == 0){
264                         return true;
265                 } else {
266                         return false;
267                 }
268         }
269         /**
270          * アバターファイルのURLを取得
271          *
272          * @param string $memberId              会員ID
273          * @return string                               URL
274          */
275         function getAvatarUrl($memberId)
276         {
277                 $avatarImagePath = $this->gEnv->getResourcePath() . self::AVATAR_DIR . '/' . $memberId . '.' . self::DEFAULT_AVATAR_FILE_EXT;
278                 if (file_exists($avatarImagePath)){             // アバターファイルが見つからないときはデフォルトを使用
279                         $avatarImageUrl = $this->gEnv->getResourceUrl() . self::AVATAR_DIR . '/' . $memberId . '.' . self::DEFAULT_AVATAR_FILE_EXT;
280                 } else {
281                         $avatarImageUrl = $this->gEnv->getCurrentWidgetRootUrl() . self::DEFAULT_AVATAR_ICON_FILE;
282                 }
283                 return $avatarImageUrl;
284         }
285         /**
286          * メッセージを表示用に変換
287          *
288          * @param string $message               変換元メッセージ
289          * @param string $threadId              スレッドID
290          * @return string                               変換後メッセージ
291          */
292         function convDispMessage($message, $threadId)
293         {
294                 // リンク変換
295                 if (!empty($this->_autolink)){          // 自動リンク作成のとき
296                         $message = preg_replace("/(https?):\/\/([\w;\/\?:\@&=\+\$,\-\.!~\*'\(\)%#]+)/", "<a href=\"$1://$2\" target=\"_blank\">$1://$2</a>", $message);
297                 
298                         // メッセージへのリンク
299                 //      $baseUrl = $this->convertUrlToHtmlEntity($this->getUrl($this->_currentPageUrl . '&' . M3_REQUEST_PARAM_BBS_THREAD_ID . '=' . $threadId, true));
300                         $messageUrl = $baseUrl . $this->convertUrlToHtmlEntity('&' . M3_REQUEST_PARAM_ITEM_NO . '=');
301                         $messageListUrl = $baseUrl . $this->convertUrlToHtmlEntity('&' . M3_REQUEST_PARAM_LIST_NO . '=');
302                         $message = preg_replace("/&gt;&gt;([0-9]+)(?![-\d])/", '<a href="' . $messageUrl . '$1" target="_blank">&gt;&gt;$1</a>', $message);
303                         $message = preg_replace("/&gt;&gt;([0-9]+)\-([0-9]+)/", '<a href="' . $messageListUrl . '$1-$2" target="_blank">&gt;&gt;$1-$2</a>', $message);
304                 }
305                 return $message;
306         }
307         /**
308          * ランダム文字列を作成
309          *
310          * @param string $baseChars             使用する文字
311          * @param int $length                   作成する文字列の長さ
312          * @return string                               ランダム文字列。作成できなかった場合は空文字列。
313          */
314         function _createRandString($baseChars, $length)
315         {
316         // 文字列の初期化
317         $destStr = '';
318
319                 if (!(is_numeric($length) && $length > 0)) return $destStr;
320     
321                 for ($i = 0; $i < $length; $i++){
322                         $pos = rand(0, strlen($baseChars) -1);
323                         $destStr .= $baseChars[$pos];
324                 }
325                 return $destStr;
326         }
327         /**
328          * テキストデータを表示用のテキストに変換
329          *
330          * 変換内容 ・改行コードをスペース「&nbsp;」に変換
331          *
332          * @param string $src                   変換するデータ
333          * @return string                               変換後データ
334          */
335         function _convertToPreviewTextWithSpace($src)
336         {
337                 return preg_replace("/(\015\012)|(\015)|(\012)/", "&nbsp;&nbsp;", $src);
338         }
339 }
340 ?>