PageRenderTime 35ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/cnhk-slideshow/ajax.php

https://bitbucket.org/antonyravel/cape-resorts
PHP | 398 lines | 353 code | 42 blank | 3 comment | 39 complexity | 5da3a36251493a7fc17aa37812900d3b MD5 | raw file
  1. <?php
  2. if ( is_admin() ) {
  3. add_action( 'wp_ajax_cnhkx_initial', 'cnhk_ajax_initial' );
  4. function cnhk_ajax_initial() {
  5. if ( ! current_user_can( 'manage_options' ) ) {
  6. wp_die( 'Not enough permissions.' );
  7. }
  8. if ( isset( $_POST['nonce'] ) && $_POST['nonce'] == $_SESSION['cnhk']['ajaxnonce'] ) {
  9. $settings = get_option( 'cnhk_options' );
  10. $slides = get_option( 'cnhk_options_slides' );
  11. $slideshows = get_option( 'cnhk_options_ss' );
  12. switch( $_POST['what'] ) {
  13. case 'use' :
  14. if ( intval( $_POST['num_id'] ) < count( $slides[0] ) ) {
  15. $slide_id = intval( $_POST['num_id'] );
  16. $current_usage = $slides[1][$slide_id];
  17. $response = array( 'status' => true, 'target' => array( 'use', $slide_id ), 'value' => array() );
  18. if ( -1 == $current_usage ) {
  19. array_push( $response['value'], array( 'index' => -1, 'use' => 'Unused', 'current' => true ) );
  20. } else {
  21. array_push( $response['value'], array( 'index' => -1, 'use' => 'Unused', 'current' => false ) );
  22. }
  23. foreach ( $settings[0] as $key => $value ) {
  24. if ( $current_usage == $key ) {
  25. array_push( $response['value'], array( 'index' => $key, 'use' => $value, 'current' => true ) );
  26. } else {
  27. array_push( $response['value'], array( 'index' => $key, 'use' => $value, 'current' => false ) );
  28. }
  29. }
  30. $output = json_encode( $response, JSON_FORCE_OBJECT );
  31. header( "Content-Type: application/json" );
  32. echo $output;
  33. exit;
  34. } else {
  35. $error = '<h3>Data error</h3><p>The plugin may need to be reinstalled</p>';
  36. $output = json_encode( array( 'status' => false, 'error' => $error ) );
  37. header( "Content-Type: application/json" );
  38. echo $output;
  39. exit;
  40. }
  41. break;
  42. case 'title' :
  43. if ( intval( $_POST['num_id'] ) < count( $slides[0] ) ) {
  44. $slide_id = $_POST['num_id'];
  45. $title = $slides[2][$slide_id];
  46. $response = array( 'status' => true, 'title' => $title, 'subject' => 'title', 'target' => $slide_id );
  47. $output = json_encode( $response, JSON_FORCE_OBJECT );
  48. header( "Content-Type: application/json" );
  49. echo $output;
  50. exit;
  51. } else {
  52. $error = '<h3>Data error</h3><p>The plugin may need to be re-installed</p>';
  53. $output = json_encode( array( 'status' => false, 'error' => $error ) );
  54. header( "Content-Type: application/json" );
  55. echo $output;
  56. exit;
  57. }
  58. break;
  59. case 'link' :
  60. if ( intval( $_POST['num_id'] ) < count( $slides[0] ) ) {
  61. $target = intval( $_POST['num_id'] );
  62. $link = urldecode( $slides[3][$target] );
  63. $response = array(
  64. 'status' => true,
  65. 'link' => $link,
  66. 'subject' => 'link',
  67. 'target' => $target
  68. );
  69. $output = json_encode( $response, JSON_FORCE_OBJECT );
  70. header( "Content-type: application/json" );
  71. echo $output;
  72. exit;
  73. }
  74. break;
  75. default:
  76. }
  77. } else {
  78. $error = '<h3>Security error</h3><p>Try to reload this page and make sure to use the cancel button for canceling an action.</p>';
  79. $output = json_encode( array( 'status' => false, 'error' => $error, 'modal' => true ) );
  80. header( "Content-Type: application/json" );
  81. echo $output;
  82. exit;
  83. }
  84. exit;
  85. /*
  86. * End of cnhk_ajax_initial()
  87. */
  88. }
  89. add_action( 'wp_ajax_cnhkx_resp', 'cnhk_ajax_resp' );
  90. function cnhk_ajax_resp() {
  91. if ( ! current_user_can( 'manage_options' ) ) {
  92. wp_die( 'Not enough permissions.' );
  93. }
  94. if ( isset( $_POST['nonce'] ) && $_POST['nonce'] == $_SESSION['cnhk']['ajaxnonce'] ) {
  95. $settings = get_option( 'cnhk_options' );
  96. $slides = get_option( 'cnhk_options_slides' );
  97. $slideshows = get_option( 'cnhk_options_ss' );
  98. switch ( $_POST['what'] ) {
  99. case 'use' :
  100. if ( intval( $_POST['target'] ) < count( $slides[0] ) && intval( $_POST['value'] ) < count( $settings[0] ) ) {
  101. $target = intval( $_POST['target'] );
  102. $value = intval( $_POST['value'] );
  103. $old_value = $slides[1][$target];
  104. $size = cnhk_get_snapsize( $target );
  105. $old_class = $size['class'];
  106. if ( -1 != $old_value ) {
  107. $offset = array_search( $target, $slideshows['elem'][$old_value] );
  108. if ( false !== $offset ) {
  109. array_splice( $slideshows['elem'][$old_value], $offset, 1 );
  110. update_option( 'cnhk_options_ss', $slideshows );
  111. }
  112. }
  113. $slides[1][$target] = $value;
  114. update_option( 'cnhk_options_slides', $slides );
  115. $value_name = ( -1 == $value ) ? 'Unused' : $settings[0][$value];
  116. $snap_size = cnhk_get_snapsize( $target );
  117. $response = array(
  118. 'status' => true,
  119. 'target' => $target,
  120. 'value_name' => $value_name,
  121. 'width' => $snap_size['width'],
  122. 'height' => $snap_size['height'],
  123. 'old_class' => $old_class,
  124. 'new_class' => $snap_size['class'],
  125. 'title' => $slides[2][$target]
  126. );
  127. $output = json_encode( $response, JSON_FORCE_OBJECT );
  128. header( "Content-Type: application/json" );
  129. echo $output;
  130. exit;
  131. } else {
  132. $error = '<h3>Data error</h3><p>The plugin may need to be reinstalled</p>';
  133. $output = json_encode( array( 'status' => false, 'error' => $error ) );
  134. header( "Content-Type: application/json" );
  135. echo $output;
  136. exit;
  137. }
  138. break;
  139. case 'textfield_submited' :
  140. if ( intval( $_POST['target'] ) < count( $slides[0] ) && intval( $_POST['value'] ) < count( $settings[0] ) ) {
  141. $target = intval( $_POST['target'] );
  142. $new_value = trim( $_POST['value'] );
  143. $subject = $_POST['subject'];
  144. switch ( $subject ) {
  145. case 'title' :
  146. $new_title = $new_value;
  147. if ( cnhk_is_valid_title( $new_title ) ) {
  148. $ext = strtolower( strrchr( $slides[0][$target], '.' ) );
  149. $new_name = cnhk_make_slug( $new_title ) . $ext;
  150. $name_jpg = CNHK_DIR . DIRECTORY_SEPARATOR . "uploads" . DIRECTORY_SEPARATOR . cnhk_make_slug( $new_title ) . '.jpg';
  151. $name_png = CNHK_DIR . DIRECTORY_SEPARATOR . "uploads" . DIRECTORY_SEPARATOR . cnhk_make_slug( $new_title ) . '.png';
  152. $old_name = $slides[0][$target];
  153. $old_title = $slides[2][$target];
  154. if ( ! ( ( file_exists( $name_jpg ) || file_exists( $name_png ) ) && $new_title != $old_title ) ) {
  155. try {
  156. $basedir = CNHK_DIR .DIRECTORY_SEPARATOR. "uploads". DIRECTORY_SEPARATOR ;
  157. rename( $basedir.$old_name, $basedir.$new_name );
  158. $slides[0][$target] = $new_name;
  159. $slides[2][$target] = $new_title;
  160. update_option( 'cnhk_options_slides', $slides );
  161. $outofdate_links = array(
  162. array(
  163. 'id' => "linkedit_$target",
  164. 'href' => CNHK_ADMIN_SLIDE . "&action=edit&obj=" . rawurlencode($slides[2][$target]) . "&key=" . $_SESSION ['cnhk']['token_edit'],
  165. 'title' => 'Edit "' . $slides[2][$target] . '"'
  166. ),
  167. array(
  168. 'id' => "linkdelete_$target",
  169. 'href' => CNHK_ADMIN_SLIDE . "&action=delete&obj=" . rawurlencode($slides[2][$target]) . "&key=" . $_SESSION ['cnhk']['token_edit'],
  170. 'title' => 'Delete "' . $slides[2][$target] . '"'
  171. ),
  172. array(
  173. 'id' => "slidetitle_$target",
  174. 'href' => false,
  175. 'title' => "Edit {$slides[2][$target]}'s title"
  176. )
  177. );
  178. $response = array(
  179. 'status' => true,
  180. 'new_value' => $new_title,
  181. 'subject' => $subject,
  182. 'target' => $target,
  183. 'outofdate_links' => $outofdate_links
  184. );
  185. $output = json_encode( $response, JSON_FORCE_OBJECT );
  186. header( "Content-Type: application/json" );
  187. echo $output;
  188. exit;
  189. } catch ( Exception $e ) {
  190. $response = array(
  191. 'status' => false,
  192. 'error' => "<h3>No data updated</h3></p>The file <strong>\"" . $old_name . "\"</strong> can not be renamed.</p>",
  193. 'target' => $target,
  194. 'subject' => $subject
  195. );
  196. $output = json_encode( $response, JSON_FORCE_OBJECT );
  197. header( "Content-Type: application/json" );
  198. echo $output;
  199. exit;
  200. }
  201. } else {
  202. $response = array(
  203. 'status' => false,
  204. 'error' => "<h3>No data updated</h3><p>This title matches an existing file. Please choose another one.</p>",
  205. 'target' => $target,
  206. 'subject' => $subject
  207. );
  208. $output = json_encode( $response, JSON_FORCE_OBJECT );
  209. header( "Content-Type: application/json" );
  210. echo $output;
  211. exit;
  212. }
  213. } else {
  214. $response = array(
  215. 'status' => false,
  216. 'error' => "<h3>No data updated</h3><p>The title should not be empty and can not contain any special characters. <strong>\"" . $new_value . "\"</strong> is invalid.</p>",
  217. 'target' => $target,
  218. 'subject' => $subject
  219. );
  220. $output = json_encode( $response, JSON_FORCE_OBJECT );
  221. header( "Content-Type: application/json" );
  222. echo $output;
  223. exit;
  224. }
  225. break;
  226. case 'link' :
  227. $new_link = $new_value;
  228. if ( 0 == strcasecmp( 'no link', $new_link ) || empty( $new_link ) ) {
  229. $new_link = 'No link';
  230. }
  231. $slides[3][$target] = rawurlencode( $new_link );
  232. update_option( 'cnhk_options_slides', $slides );
  233. $response = array(
  234. 'status' => true,
  235. 'subject' => $subject,
  236. 'target' => $target,
  237. 'new_value' => $new_link
  238. );
  239. $output = json_encode( $response, JSON_FORCE_OBJECT );
  240. header( "Content-Type: application/json" );
  241. echo $output;
  242. exit;
  243. break;
  244. default:
  245. $response = array(
  246. 'status' => false,
  247. 'error' => "<h3>No data updated</h3><p>No matching subject.</p>"
  248. );
  249. $output = json_encode( $response, JSON_FORCE_OBJECT );
  250. header( "Content-Type: application/json" );
  251. echo $output;
  252. exit;
  253. break;
  254. } /* End switch ( $subject ) */
  255. } else {
  256. $error = '<h3>Data error</h3><p>The plugin may need to be re-installed</p>';
  257. $output = json_encode( array( 'status' => false, 'error' => $error ) );
  258. header( "Content-Type: application/json" );
  259. echo $output;
  260. exit;
  261. }
  262. break;
  263. default :
  264. }
  265. } else {
  266. $error = '<h3>Security error</h3><p>Try to reload this page and make sure to use the cancel button for canceling an action.</p>';
  267. $output = json_encode( array( 'status' => false, 'error' => $error, 'modal' => true ) );
  268. header( "Content-Type: application/json" );
  269. echo $output;
  270. exit;
  271. }
  272. /*
  273. * End of cnhk_ajax_resp()
  274. */
  275. }
  276. add_action( 'wp_ajax_afterupload', 'cnhk_ajax_afterupload' );
  277. function cnhk_ajax_afterupload() {
  278. if ( ! current_user_can( 'manage_options' ) ) {
  279. wp_die( 'Not enough permissions.' );
  280. }
  281. if ( isset( $_POST['nonce'] ) && $_POST['nonce'] == $_SESSION['cnhk']['ajaxnonce'] ) {
  282. $slides = get_option( 'cnhk_options_slides' );
  283. switch( $_POST['what'] ) {
  284. case 'slide' :
  285. $file = $_POST['file'];
  286. preg_match( "#slide(\d+)\.(jpg|png)#i", $file, $elem );
  287. $nb = $elem[1];
  288. $ext = '.' . $elem[2];
  289. array_push( $slides[0], "slide{$nb}{$ext}" );
  290. array_push( $slides[1], -1 );
  291. array_push( $slides[2], "slide{$nb}" );
  292. array_push( $slides[3], rawurlencode("No link") );
  293. array_push( $slides[4], time() );
  294. update_option( 'cnhk_options_slides', $slides );
  295. $new_index = count( $slides[0] ) -1;
  296. $snap_size = cnhk_get_snapsize( $new_index );
  297. $title = "slide$nb";
  298. $src = CNHK_URL . "/uploads/{$file}";
  299. $DT = gmdate( "d/m/Y - h:i A", $slides[4][$new_index] ) . ' GMT';
  300. $response = json_encode( array(
  301. 'status' => true,
  302. 'subject' => 'slide',
  303. 'index' => $new_index,
  304. 'src' => $src,
  305. 'title' => $title,
  306. 'udate' => esc_attr( $DT ),
  307. 'width' => $snap_size['width'],
  308. 'height' => $snap_size['height'],
  309. 'wideclass' => $snap_size['class'],
  310. 'key' => $_SESSION ['cnhk']['token_edit']
  311. ), JSON_FORCE_OBJECT );
  312. header( "Content-Type: application/json" );
  313. echo $response;
  314. exit;
  315. break;
  316. default :
  317. }
  318. } else {
  319. $error = '<h3>Security error</h3><p>Try to reload this page and make sure to use the cancel button for canceling an action.</p>';
  320. $output = json_encode( array( 'status' => false, 'error' => $error, 'modal' => true ) );
  321. header( "Content-Type: application/json" );
  322. echo $output;
  323. exit;
  324. }
  325. }
  326. }