7 * LICENSE: This source file is licensed under the terms of the GNU General Public License.
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
16 require_once(M3_SYSTEM_INCLUDE_PATH . '/common/core.php');
18 class ConnectManager extends Core
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 = 'サイト名未設定'; // サイト名未設定の場合のデフォルトサイト名
31 function __construct()
34 parent::__construct();
37 $this->db = $this->gInstance->getSytemDbObject();
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=失敗
55 public function registUpdateInfo($type, $url, $toWidgetId, $fromWidgetId, $message, $contentTitle, $contentLink, $contentDt, $registDt='')
57 $now = date("Y/m/d H:i:s"); // 現在日時
59 // 自サーバが非公開状態のときは送信しない
60 if (!$this->gSystem->siteInPublic()) return false;
63 if (empty($url)) $url = $this->gSystem->getSystemConfig(self::CF_CONNECT_SERVER_URL);// ポータル接続先URL
66 if (empty($url)) return false;
68 // サーバIDが設定されていないときは終了
69 $serverId = $this->db->getSystemConfig(self::CF_SERVER_ID);
70 if (empty($serverId)) return false;
73 if (empty($type)) $type = M3_VIEW_TYPE_CONTENT; // デフォルトは汎用コンテンツ
76 if (empty($toWidgetId)) $toWidgetId = self::DEFAULT_TO_WIDGET_ID;
79 list($url, $query) = explode('?', $url);
80 if (!strEndsWith($url, '.php')){
81 if (!strEndsWith($url, '/')) $url .= '/';
82 $url .= M3_FILENAME_SERVER_CONNECTOR; // サーバ接続用コネクター
86 $query = M3_REQUEST_PARAM_WIDGET_ID . '=' . urlencode($toWidgetId);
91 if (empty($registDt)) $registDt = $now;
94 $siteName =$this->gEnv->getSiteName();
95 // if (empty($siteName)) $siteName = self::DEFAULT_SITE_NAME;
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()); // 送信元サイトへのリンク
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;
124 $xml = new SimpleXMLElement($output);
125 $status = M3_RESPONSE_PARAM_STATUS;
126 if ($xml->status == 1){
129 $errMsg = 'サーバからの戻りコードが不正(' . $xml->status . ')';
130 $msgDetail = 'コンテンツタイトル=' . $contentTitle;
132 } catch (Exception $e){
133 $errMsg = 'サーバからの戻りデータが不正';
134 $msgDetail = 'コンテンツタイトル=' . $contentTitle . ',エラーデータ=' . $output;
141 $msgDetail = 'サーバURL=' . $url;
142 $this->gOpeLog->writeInfo(__METHOD__, 'コンテンツ更新情報をサーバへアップしました。(コンテンツタイトル=' . $contentTitle . ')', 1000, $msgDetail);
144 $this->gOpeLog->writeError(__METHOD__, 'コンテンツ更新情報送信エラー(要因=' . $errMsg . ')', 1100, $msgDetail);
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=失敗
157 public function sendToPortal($postParam, &$xmlObj, $url = '', $toWidgetId = '')
159 $now = date("Y/m/d H:i:s"); // 現在日時
161 // ##### パラメータチェック #####
162 // 自サーバが非公開状態のときは送信しない
163 if (!$this->gSystem->siteInPublic()) return false;
165 // 接続先が未指定のときは、接続先を取得
166 if (empty($url)) $url = $this->gSystem->getSystemConfig(self::CF_PORTAL_SERVER_URL);// ポータルサーバURL
169 if (empty($url)) return false;
171 // サーバIDが設定されていないときは終了
172 $serverId = $this->db->getSystemConfig(self::CF_SERVER_ID);
173 if (empty($serverId)) return false;
175 // ##### 送信データ設定 #####
177 if (empty($toWidgetId)) $toWidgetId = self::DEFAULT_PORTAL_TO_WIDGET_ID;
180 list($url, $query) = explode('?', $url);
181 if (!strEndsWith($url, '.php')){
182 if (!strEndsWith($url, '/')) $url .= '/';
183 $url .= M3_FILENAME_SERVER_CONNECTOR; // サーバ接続用コネクター
187 $query = M3_REQUEST_PARAM_WIDGET_ID . '=' . urlencode($toWidgetId);
189 $url .= '?' . $query;
192 $params = array_merge(array('server_id' => $serverId), $postParam); // サーバID
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);
209 $xmlObj = @new SimpleXMLElement($output);
210 $status = M3_RESPONSE_PARAM_STATUS;
211 if ($xmlObj->status == 1){
214 $errMsg = 'サーバからの戻りコードが不正(' . $xmlObj->status . ')';
216 } catch (Exception $e){
217 $errMsg = 'サーバからの戻りデータが不正';
223 $msgDetail = 'サーバURL=' . $url . ', 送信データ(';
224 $keys = array_keys($params);// キーを取得
225 for ($i = 0; $i < count($keys); $i++){
226 if ($i > 0) $msgDetail .= ', ';
228 $value = $params[$key];
229 $msgDetail .= $key . '=' . $value;
234 $this->gOpeLog->writeInfo(__METHOD__, 'ポータルサーバと通信しました。', 1000, $msgDetail);
236 $this->gOpeLog->writeError(__METHOD__, 'ポータルサーバ間通信エラー(要因=' . $errMsg . ')', 1100, $msgDetail);