PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/system/application/controllers/gadget_ajax.php

https://bitbucket.org/seezoo/seezoo/
PHP | 461 lines | 228 code | 62 blank | 171 comment | 20 complexity | 38886584e699644d5de545ccab627a49 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * ===============================================================================
  4. *
  5. * Seezoo Gadget_Ajaxコントローラ
  6. *
  7. * ガジェットの応答系コントローラ
  8. * @package Seezoo Core
  9. * @author Yoshiaki Sugimoto <neo.yoshiaki.sugimoto@gmail.com>
  10. *
  11. * ===============================================================================
  12. */
  13. class Gadget_ajax extends SZ_Controller
  14. {
  15. /**
  16. * コンストラクタ
  17. */
  18. function __construct()
  19. {
  20. parent::SZ_Controller(FALSE);
  21. // this class works ajax request only.
  22. // check Request Header by Flint.js posted X-Requsted-With
  23. if (!$this->_is_ajax_request())
  24. {
  25. exit('access_denied.');
  26. }
  27. $this->load->model(array('ajax_model'));
  28. $this->output->set_header('Content-Type: text/html; charset=UTF-8');
  29. }
  30. /**
  31. * Ajaxリクエストチェック
  32. * @access private
  33. */
  34. function _is_ajax_request()
  35. {
  36. // if User Agent is IE6, also, same value of XMLHttpRequest created by Flint.js.
  37. return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest') ? TRUE : FALSE;
  38. }
  39. /**
  40. * ガジェット操作用Ajaxトークンチェック
  41. * @access private
  42. * @param string $name
  43. * @param string $val
  44. */
  45. function _token_check($name, $val)
  46. {
  47. if (!$this->session->userdata($name) || $this->session->userdata($name) !== $val)
  48. {
  49. exit('access denied.');
  50. }
  51. }
  52. /**
  53. * 使用できるガジェットリスト取得
  54. * @access public
  55. * @param $token
  56. */
  57. function get_gadget_list($token = FALSE)
  58. {
  59. $this->_token_check('sz_token', $token);
  60. $gadget = $this->ajax_model->get_gadget_list();
  61. echo json_encode($gadget);
  62. exit;
  63. }
  64. /**
  65. * ガジェット追加
  66. * @access public
  67. * @param $gid
  68. * @param $token
  69. */
  70. function add_gadget($gid, $token = FALSE)
  71. {
  72. $this->_token_check('sz_token', $token);
  73. $ret = $this->ajax_model->add_new_gadget((int)$gid);
  74. if (!$ret || !is_array($ret))
  75. {
  76. echo 'error';
  77. }
  78. else
  79. {
  80. echo json_encode($ret);
  81. }
  82. }
  83. /**
  84. * ガジェットのロード
  85. * @access public
  86. * @param string $token
  87. */
  88. function load_gadgets($token = FALSE)
  89. {
  90. $this->_token_check('sz_token', $token);
  91. $gadgets = $this->ajax_model->get_user_gadgets((int)$this->session->userdata('user_id'));
  92. if ($gadgets === FALSE)
  93. {
  94. echo 'none';
  95. }
  96. else
  97. {
  98. echo json_encode($gadgets);
  99. }
  100. }
  101. /**
  102. * ガジェット並び順変更
  103. * @access public
  104. * @param $token
  105. */
  106. function sort_gadget($token = FALSE)
  107. {
  108. $this->_token_check('sz_token', $token);
  109. $data = array();
  110. foreach ($_POST as $key => $val)
  111. {
  112. if (intval($key) > 0)
  113. {
  114. $data[$val] = (int)$key;
  115. }
  116. }
  117. $this->ajax_model->do_sort_gadget($data);
  118. }
  119. /**
  120. * ガジェット削除
  121. * @access public
  122. * @param $gmid
  123. * @param $token
  124. */
  125. function delete_gadget($gmid, $token = FALSE)
  126. {
  127. $this->_token_check('sz_token', $token);
  128. if (!ctype_digit($gmid))
  129. {
  130. echo 'error';
  131. }
  132. $ret = $this->ajax_model->delete_gadget($gmid);
  133. echo ($ret) ? 'complete' : 'error';
  134. exit;
  135. }
  136. /**
  137. * memoガジェットデータ取得
  138. * @access public
  139. * @param $key
  140. * @param $token
  141. */
  142. function get_gadget_memo($key, $token = FALSE)
  143. {
  144. $this->_token_check('sz_token', $token);
  145. $data = $this->ajax_model->get_gadget_data_memo($key);
  146. if ($data)
  147. {
  148. $out = array(
  149. 'update_time' => $data->update_time,
  150. 'data' => form_prep($data->data)
  151. );
  152. echo json_encode($out);
  153. }
  154. else
  155. {
  156. echo 'error';
  157. }
  158. }
  159. /**
  160. * memoガジェットデータ保存
  161. * @access public
  162. * @param $key
  163. * @param $token
  164. */
  165. function save_memo($key, $token = FALSE)
  166. {
  167. $this->_token_check('sz_token', $token);
  168. $post = array('data' => $this->input->post('data'));
  169. $this->ajax_model->save_memo_data($key, $post);
  170. }
  171. /**
  172. * weatherガジェットデータ取得
  173. * @access public
  174. * @param string $key
  175. * @param string $token
  176. */
  177. function get_gadget_weather($key, $token = FALSE)
  178. {
  179. $this->_token_check('sz_token', $token);
  180. $uid = (int)$this->session->userdata('user_id');
  181. $data = $this->ajax_model->get_gadget_data_weather($key);
  182. if ($data)
  183. {
  184. echo json_encode($data);
  185. }
  186. else
  187. {
  188. echo 'error';
  189. }
  190. }
  191. /**
  192. * weatherガジェット:表示地方変更
  193. * @param $key
  194. * @param $token
  195. */
  196. function gadget_weather_change_area($key, $token = FALSE)
  197. {
  198. $this->_token_check('sz_token', $token);
  199. $city = $this->input->post('city_id');
  200. if (!ctype_digit($city))
  201. {
  202. exit('error');
  203. }
  204. $ret = $this->ajax_model->gadget_weather_update($key, (int)$city);
  205. echo ($ret) ? 'complete' : 'error';
  206. exit;
  207. }
  208. // function get_gmail($key, $token)
  209. // {
  210. //// $this->_token_check('sz_token', $token);
  211. //
  212. // $d = $this->input->post('last_date');
  213. //
  214. // if (!$this->session->userdata('gmail_hash'))
  215. // {
  216. // exit('need_login');
  217. // }
  218. //
  219. // //$this->session->keep_flashdata('gmail_logged_in');
  220. // $ac = $this->session->userdata('gmail_hash');
  221. //
  222. // $data = $this->ajax_model->get_gmail_data($ac, $d);
  223. //
  224. // if ($data === FALSE)
  225. // {
  226. // echo 'error';
  227. // }
  228. // else if ($data == 'no_account')
  229. // {
  230. // echo 'need_login';
  231. // }
  232. // else
  233. // {
  234. // echo json_encode($data);
  235. // }
  236. // }
  237. //
  238. // function gadget_gmail_login($token = FALSE)
  239. // {
  240. // $this->_token_check('sz_token', $token);
  241. //
  242. // $email = $this->input->post('email');
  243. // $pass = $this->input->post('password');
  244. //
  245. // $ret = $this->ajax_model->do_gmail_login($email, $pass);
  246. //
  247. // if ($ret)
  248. // {
  249. // $hash = md5(uniqid(mt_rand(), TRUE));
  250. // $this->session->set_userdata('gmail_hash', $ret);
  251. // echo 'complete';
  252. // }
  253. // else
  254. // {
  255. // echo 'error';
  256. // }
  257. // //$this->session->set_userdata('gmail_logged_in', base64_encode($email .':' . $pass));
  258. // }
  259. /**
  260. * twitterガジェットデータ取得
  261. * @access public
  262. * @param $key
  263. * @param $token
  264. */
  265. function get_gadget_twitter($key, $token = FALSE)
  266. {
  267. $this->_token_check('sz_token', $token);
  268. $ret = $this->ajax_model->get_twitter_gadget_data($key);
  269. if (!$ret)
  270. {
  271. echo 'error';
  272. }
  273. else
  274. {
  275. echo json_encode($ret);
  276. }
  277. }
  278. /**
  279. * twitterガジェット設定変更
  280. * @param $key
  281. * @param $token
  282. */
  283. function gadget_twitter_config($key, $token = FALSE)
  284. {
  285. $this->_token_check('sz_token', $token);
  286. $n = $this->input->post('account_name', TRUE);
  287. $u = $this->input->post('update_time', TRUE);
  288. $c = $this->input->post('show_count', TRUE);
  289. if ($n == '' || $n == '最新')
  290. {
  291. $n = '';
  292. }
  293. // pre_validate
  294. if (!ctype_digit($u) || !ctype_digit($c))
  295. {
  296. echo 'error';
  297. exit;
  298. }
  299. $data = array(
  300. 'account_name' => $n,
  301. 'update_time' => $u,
  302. 'show_count' => $c
  303. );
  304. $ret = $this->ajax_model->update_twitter_config($key, $data);
  305. if ($ret)
  306. {
  307. echo json_encode($data);
  308. }
  309. else
  310. {
  311. echo 'error';
  312. }
  313. }
  314. /**
  315. * RSSガジェットデー取得
  316. * @access public
  317. * @param $key
  318. * @param $token
  319. */
  320. function get_gadget_rss($key, $token = FALSE)
  321. {
  322. $this->_token_check('sz_token', $token);
  323. $data = $this->ajax_model->get_gadget_rss_data($key);
  324. $this->load->library('rss');
  325. $ret = $this->rss->load($data, 'desc'); // return array
  326. $ret['rss_url'] = $data;
  327. echo json_encode($ret);
  328. }
  329. /**
  330. * RSSガジェット設定変更
  331. * @access public
  332. * @param $key
  333. * @param $token
  334. */
  335. function gadget_rss_config($key, $token = FALSE)
  336. {
  337. $this->_token_check('sz_token', $token);
  338. $data = array(
  339. 'rss_url' => $this->input->post('rss_url', TRUE)
  340. );
  341. $ret = $this->ajax_model->update_gadget_rss_config($key, $data);
  342. echo ($ret) ? 'complete' : 'error';
  343. exit;
  344. }
  345. /**
  346. * BBSガジェットデータ取得
  347. * @access public
  348. * @param $token
  349. */
  350. function gadget_bbs_get_data($token = FALSE)
  351. {
  352. $this->_token_check('sz_token', $token);
  353. $data['users'] = $this->ajax_model->get_user_name_list();
  354. $data['data'] = $this->ajax_model->get_recent_bbs();
  355. echo json_encode($data);
  356. }
  357. /**
  358. * BBSガジェット投稿
  359. * @access public
  360. * @param $token
  361. */
  362. function gadget_bbs_submit($token = FALSE)
  363. {
  364. $this->_token_check('sz_token', $token);
  365. $data = array(
  366. 'body' => nl2br($this->input->post('body', TRUE)),
  367. 'posted_user_id' => (int)$this->session->userdata('user_id'),
  368. 'post_date' => date('Y-m-d H:i:s', time())
  369. );
  370. $ret = $this->ajax_model->insert_bbs_data($data);
  371. echo ($ret) ? 'complete' : 'error';
  372. exit;
  373. }
  374. /**
  375. * BBSガジェット再読み込み
  376. * @access public
  377. * @param $token
  378. */
  379. function gadget_bbs_update($token = FALSE)
  380. {
  381. $this->_token_check('sz_token', $token);
  382. $last = $this->input->post('last_update', TRUE);
  383. $data['users'] = $this->ajax_model->get_user_name_list();
  384. $data['data'] = $this->ajax_model->get_since_bbs($last);
  385. echo json_encode($data);
  386. }
  387. /**
  388. * 翻訳ガジェット翻訳実行
  389. * @param $token
  390. */
  391. function gadget_do_translate($token = FALSE)
  392. {
  393. $this->_token_check('sz_token', $token);
  394. $query = $this->input->post('translate_value', TRUE);
  395. $type = (int)$this->input->post('translate_to');
  396. $ret = $this->ajax_model->get_translated_data($query, $type);
  397. echo ($ret) ? $ret : 'error';
  398. }
  399. }