OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / include / manager / connectManager.php
1 <?php
2 /**
3  * サーバ接続マネージャー
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-2011 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: connectManager.php 5114 2012-08-16 01:04:11Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 require_once(M3_SYSTEM_INCLUDE_PATH . '/common/core.php');
17
18 class ConnectManager extends Core
19 {
20         private $db;                                            // DBオブジェクト
21         const CF_CONNECT_SERVER_URL = 'default_connect_server_url';                     // ポータル接続先URL
22         const CF_PORTAL_SERVER_URL = 'portal_server_url';               // ポータルサーバURL
23         const CF_SERVER_ID = 'server_id';               // サーバID
24         const DEFAULT_TO_WIDGET_ID = 'c/updateinfo';            // デフォルトの送信先ウィジェット
25         const DEFAULT_PORTAL_TO_WIDGET_ID = 'c/portal';         // デフォルトのポータルの送信先ウィジェット
26 //      const DEFAULT_SITE_NAME = 'サイト名未設定';              // サイト名未設定の場合のデフォルトサイト名
27         
28         /**
29          * コンストラクタ
30          */
31         function __construct()
32         {
33                 // 親クラスを呼び出す
34                 parent::__construct();
35                         
36                 // システムDBオブジェクト取得
37                 $this->db = $this->gInstance->getSytemDbObject();
38         }
39         /**
40          * コンテンツ更新情報をサーバに登録
41          *
42          * 登録した更新内容はログテーブルに残す
43          *
44          * @param int $type                             コンテンツタイプ(なし=汎用コンテンツ)
45          * @param string $url                   送信先URL(空のときはデフォルトの登録用URL)
46          * @param string $toWidgetId    送信先ウィジェット(空のときはデフォルトの登録用ウィジェット)
47          * @param string $fromWidgetId  送信元ウィジェットID
48          * @param string $message               送信メッセージ
49          * @param string $contentTitle  コンテンツタイトル
50          * @param string $contentLink   コンテンツへのリンク
51          * @param timestamp $contentDt  コンテンツの更新日時
52          * @param timestamp $registDt   登録情報を有効にする日時
53          * @return bool                                 true=成功、false=失敗
54          */
55         public function registUpdateInfo($type, $url, $toWidgetId, $fromWidgetId, $message, $contentTitle, $contentLink, $contentDt, $registDt='')
56         {
57                 $now = date("Y/m/d H:i:s");     // 現在日時
58                 
59                 // 自サーバが非公開状態のときは送信しない
60                 if (!$this->gSystem->siteInPublic()) return false;
61                 
62                 // 接続先が未指定のときは、接続先を取得
63                 if (empty($url)) $url = $this->gSystem->getSystemConfig(self::CF_CONNECT_SERVER_URL);// ポータル接続先URL
64                 
65                 // 接続先が指定されていないときは終了
66                 if (empty($url)) return false;
67                 
68                 // サーバIDが設定されていないときは終了
69                 $serverId = $this->db->getSystemConfig(self::CF_SERVER_ID);
70                 if (empty($serverId)) return false;
71                 
72                 // コンテンツタイプ設定
73                 if (empty($type)) $type = M3_VIEW_TYPE_CONTENT;         // デフォルトは汎用コンテンツ
74                 
75                 // 送信先ウィジェット設定
76                 if (empty($toWidgetId)) $toWidgetId = self::DEFAULT_TO_WIDGET_ID;
77                 
78                 // 送信URL作成
79                 list($url, $query) = explode('?', $url);
80                 if (!strEndsWith($url, '.php')){
81                         if (!strEndsWith($url, '/')) $url .= '/';
82                         $url .= M3_FILENAME_SERVER_CONNECTOR;           // サーバ接続用コネクター
83                 }
84                 // 送信先ウィジェットを指定
85                 if (empty($query)){
86                         $query = M3_REQUEST_PARAM_WIDGET_ID . '=' . urlencode($toWidgetId);
87                 }
88                 $url .= '?' . $query;
89
90                 // 登録情報を有効にする日時
91                 if (empty($registDt)) $registDt = $now;
92                 
93                 // サイト名
94                 $siteName =$this->gEnv->getSiteName();
95 //              if (empty($siteName)) $siteName = self::DEFAULT_SITE_NAME;
96                 
97                 // 送信データ作成
98                 $params = array('server_id'             => $serverId,           // サーバID
99                                                 'act'           => 'regist',            // データ登録
100                                                 'regist_dt'     => $registDt,           // 情報を登録する日時
101                                                 'content_type'  => $type,               // コンテンツタイプ
102                                                 'content_name'  => $contentTitle,               // コンテンツ名
103                                                 'content_link'  => $contentLink,                // コンテンツリンク先
104                                                 'content_dt'    => $contentDt,          // コンテンツ更新日時
105                                                 'message'       => $message,            // 送信メッセージ
106                                                 'site_name'     => $siteName,           // 送信元サイト名
107                                                 'site_link'     => $this->gEnv->getRootUrl());          // 送信元サイトへのリンク
108                 
109                 // データ送信
110                 $ret = false;           // 送信状況
111                 $ch = curl_init($url);
112                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
113                 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
114                 curl_setopt($ch, CURLOPT_USERAGENT, M3_SYSTEM_NAME . '/' . M3_SYSTEM_VERSION);          // ユーザエージェント
115                 curl_setopt($ch, CURLOPT_POST, TRUE);
116                 curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
117                 $output = curl_exec($ch);
118                 if ($output === false){
119                         $errMsg = curl_error($ch);
120                         $msgDetail = 'コンテンツタイトル=' . $contentTitle;
121                 } else {
122                         // 取得したデータを解析
123                         try {
124                                 $xml = new SimpleXMLElement($output);
125                                 $status = M3_RESPONSE_PARAM_STATUS;
126                                 if ($xml->status == 1){
127                                         $ret = true;            // 送信状況
128                                 } else {
129                                         $errMsg = 'サーバからの戻りコードが不正(' . $xml->status . ')';
130                                         $msgDetail = 'コンテンツタイトル=' . $contentTitle;
131                                 }
132                         } catch (Exception $e){
133                                 $errMsg = 'サーバからの戻りデータが不正';
134                                 $msgDetail = 'コンテンツタイトル=' . $contentTitle . ',エラーデータ=' . $output;
135                         }
136                 }
137                 curl_close($ch);
138
139                 // 送信成功したときは、ログに残す
140                 if ($ret){
141                         $msgDetail = 'サーバURL=' . $url;
142                         $this->gOpeLog->writeInfo(__METHOD__, 'コンテンツ更新情報をサーバへアップしました。(コンテンツタイトル=' . $contentTitle . ')', 1000, $msgDetail);
143                 } else {                // エラーの場合
144                         $this->gOpeLog->writeError(__METHOD__, 'コンテンツ更新情報送信エラー(要因=' . $errMsg . ')', 1100, $msgDetail);
145                 }
146                 return $ret;
147         }
148         /**
149          * ポータルサーバと通信
150          *
151          * @param array $postParam              Post送信データ
152          * @param string $url                   送信先URL(空のときはデフォルトのポータルサーバURL)
153          * @param string $toWidgetId    送信先ウィジェット(空のときはデフォルトの登録用ウィジェット)
154          * @param object $xmlObj                受信データSimpleXMLElementオブジェクト
155          * @return bool                                 true=成功、false=失敗
156          */
157         public function sendToPortal($postParam, &$xmlObj, $url = '', $toWidgetId = '')
158         {
159                 $now = date("Y/m/d H:i:s");     // 現在日時
160                 
161                 // ##### パラメータチェック #####
162                 // 自サーバが非公開状態のときは送信しない
163                 if (!$this->gSystem->siteInPublic()) return false;
164                 
165                 // 接続先が未指定のときは、接続先を取得
166                 if (empty($url)) $url = $this->gSystem->getSystemConfig(self::CF_PORTAL_SERVER_URL);// ポータルサーバURL
167                 
168                 // 接続先が指定されていないときは終了
169                 if (empty($url)) return false;
170                 
171                 // サーバIDが設定されていないときは終了
172                 $serverId = $this->db->getSystemConfig(self::CF_SERVER_ID);
173                 if (empty($serverId)) return false;
174                 
175                 // ##### 送信データ設定 #####
176                 // 送信先ウィジェット設定
177                 if (empty($toWidgetId)) $toWidgetId = self::DEFAULT_PORTAL_TO_WIDGET_ID;
178                 
179                 // 送信URL作成
180                 list($url, $query) = explode('?', $url);
181                 if (!strEndsWith($url, '.php')){
182                         if (!strEndsWith($url, '/')) $url .= '/';
183                         $url .= M3_FILENAME_SERVER_CONNECTOR;           // サーバ接続用コネクター
184                 }
185                 // 送信先ウィジェットを指定
186                 if (empty($query)){
187                         $query = M3_REQUEST_PARAM_WIDGET_ID . '=' . urlencode($toWidgetId);
188                 }
189                 $url .= '?' . $query;
190
191                 // 送信データ作成
192                 $params = array_merge(array('server_id'         => $serverId), $postParam);             // サーバID
193                 
194                 // データ送信
195                 $ret = false;           // 送信状況
196                 $msgDetail = '';        // 詳細メッセージ
197                 $ch = curl_init($url);
198                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
199                 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
200                 curl_setopt($ch, CURLOPT_USERAGENT, M3_SYSTEM_NAME . '/' . M3_SYSTEM_VERSION);          // ユーザエージェント
201                 curl_setopt($ch, CURLOPT_POST, TRUE);
202                 curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
203                 $output = curl_exec($ch);
204                 if ($output === false){
205                         $errMsg = curl_error($ch);
206                 } else {
207                         // 取得したデータを解析
208                         try {
209                                 $xmlObj = @new SimpleXMLElement($output);
210                                 $status = M3_RESPONSE_PARAM_STATUS;
211                                 if ($xmlObj->status == 1){
212                                         $ret = true;            // 送信状況
213                                 } else {
214                                         $errMsg = 'サーバからの戻りコードが不正(' . $xmlObj->status . ')';
215                                 }
216                         } catch (Exception $e){
217                                 $errMsg = 'サーバからの戻りデータが不正';
218                         }
219                 }
220                 curl_close($ch);
221
222                 // 通信状況をログに残す
223                 $msgDetail = 'サーバURL=' . $url . ', 送信データ(';
224                 $keys = array_keys($params);// キーを取得
225                 for ($i = 0; $i < count($keys); $i++){
226                         if ($i > 0) $msgDetail .= ', ';
227                         $key = $keys[$i];
228                         $value = $params[$key];
229                         $msgDetail .= $key . '=' . $value;
230                 }
231                 $msgDetail .= ')';
232                 
233                 if ($ret){
234                         $this->gOpeLog->writeInfo(__METHOD__, 'ポータルサーバと通信しました。', 1000, $msgDetail);
235                 } else {                // エラーの場合
236                         $this->gOpeLog->writeError(__METHOD__, 'ポータルサーバ間通信エラー(要因=' . $errMsg . ')', 1100, $msgDetail);
237                 }
238                 return $ret;
239         }
240 }
241 ?>