LocalServer.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. class Duoshuo_LocalServer{
  3. protected $response = array();
  4. /**
  5. *
  6. * @var Duoshuo_WordPress
  7. */
  8. protected $plugin;
  9. public function __construct($plugin){
  10. $this->plugin = $plugin;
  11. }
  12. /**
  13. * 从服务器pull评论到本地
  14. *
  15. * @param array $input
  16. */
  17. public function sync_log($input = array()){
  18. $syncLock = $this->plugin->getOption('sync_lock');//检查是否正在同步评论 同步完成后该值会置0
  19. if($syncLock && $syncLock > time()- 300){//正在或5分钟内发生过写回但没置0
  20. $this->response = array(
  21. 'code' => Duoshuo_Exception::SUCCESS,
  22. 'response'=> '同步中,请稍候',
  23. );
  24. return;
  25. }
  26. try{
  27. $this->response['affected'] = $this->plugin->syncLog();
  28. $this->response['last_log_id'] = $this->plugin->getOption('last_log_id');
  29. }
  30. catch(Exception $ex){
  31. //$this->plugin->updateOption('sync_lock', $ex->getLine());
  32. }
  33. $this->response['code'] = Duoshuo_Exception::SUCCESS;
  34. }
  35. public function update_option($input = array()){
  36. //duoshuo_short_name
  37. //duoshuo_secret
  38. //duoshuo_notice
  39. foreach($input as $optionName => $optionValue)
  40. if (substr($optionName, 0, 8) === 'duoshuo_'){
  41. update_option($_POST['option'], $_POST['value']);
  42. }
  43. $this->response['code'] = 0;
  44. }
  45. public function dispatch($input){
  46. if (!isset($input['signature']))
  47. throw new Duoshuo_Exception('Invalid signature.', Duoshuo_Exception::INVALID_SIGNATURE);
  48. $signature = $input['signature'];
  49. unset($input['signature']);
  50. ksort($input);
  51. $baseString = http_build_query($input, null, '&');
  52. $expectSignature = base64_encode(Duoshuo_Abstract::hmacsha1($baseString, $this->plugin->getOption('secret')));
  53. if ($signature !== $expectSignature)
  54. throw new Duoshuo_Exception('Invalid signature, expect: ' . $expectSignature . '. (' . $baseString . ')', Duoshuo_Exception::INVALID_SIGNATURE);
  55. $method = $input['action'];
  56. if (!method_exists($this, $method))
  57. throw new Duoshuo_Exception('Unknown action.', Duoshuo_Exception::OPERATION_NOT_SUPPORTED);
  58. $this->response = array();
  59. $this->$method($input);
  60. $this->sendResponse();
  61. }
  62. public function sendResponse(){
  63. echo json_encode($this->response);
  64. }
  65. }