1<?php 2 3/** 4 * Plugin BatchEdit: AJAX server 5 * 6 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 7 * @author Mykola Ostrovskyy <dwpforge@gmail.com> 8 */ 9 10require_once(DOKU_PLUGIN . 'batchedit/engine.php'); 11 12class BatcheditServer { 13 14 const AJAX_COOKIE = '{7b4e584c-bf85-4f7b-953b-15e327df08ff}'; 15 16 private $plugin; 17 /** 18 * 19 */ 20 public function __construct($plugin) { 21 $this->plugin = $plugin; 22 } 23 24 /** 25 * 26 */ 27 public function handle() { 28 try { 29 $this->verifyAdminRights(); 30 31 switch ($_REQUEST['command']) { 32 case 'cancel': 33 $this->handleCancel(); 34 break; 35 36 case 'progress': 37 $this->handleProgress(); 38 break; 39 } 40 } 41 catch (Exception $error) { 42 $this->sendResponse(array('error' => 'server_error', 'message' => $error->getMessage())); 43 } 44 } 45 46 /** 47 * 48 */ 49 private function verifyAdminRights() { 50 global $conf; 51 52 if (auth_quickaclcheck($conf['start']) < AUTH_ADMIN) { 53 throw new Exception('Access denied'); 54 } 55 } 56 57 /** 58 * 59 */ 60 private function verifySession() { 61 if (!isset($_REQUEST['session']) || preg_match('/^[0-9a-f]+$/', $_REQUEST['session']) != 1) { 62 throw new Exception('Invalid session identifier'); 63 } 64 } 65 66 /** 67 * 68 */ 69 private function handleCancel() { 70 $this->verifySession(); 71 72 BatcheditEngine::cancelOperation($_REQUEST['session']); 73 } 74 75 /** 76 * 77 */ 78 private function handleProgress() { 79 $this->verifySession(); 80 81 $progress = new BatcheditProgress($_REQUEST['session']); 82 83 list($operation, $progress) = $progress->get(); 84 85 if ($operation == BatcheditProgress::UNKNOWN) { 86 throw new Exception('Progress unknown'); 87 } 88 89 $this->sendResponse(array('operation' => $this->getProgressLabel($operation), 'progress' => $progress)); 90 } 91 92 /** 93 * 94 */ 95 private function getProgressLabel($operation) { 96 switch ($operation) { 97 case BatcheditProgress::SEARCH: 98 return $this->plugin->getLang('lbl_searching'); 99 100 case BatcheditProgress::APPLY: 101 return $this->plugin->getLang('lbl_applying'); 102 } 103 104 return ''; 105 } 106 107 /** 108 * 109 */ 110 private function sendResponse($data) { 111 header('Content-Type: application/json'); 112 print(self::AJAX_COOKIE . json_encode($data) . self::AJAX_COOKIE); 113 } 114} 115