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

/wp-content/plugins/nextgen-gallery/admin/ajax.php

https://gitlab.com/blueprintmrk/bladencountyrecords
PHP | 446 lines | 275 code | 85 blank | 86 comment | 59 complexity | 9e63cdcdd92e3b9795373c020988240f MD5 | raw file
  1. <?php
  2. add_action('wp_ajax_ngg_ajax_operation', 'ngg_ajax_operation' );
  3. /**
  4. * Image edit functions via AJAX
  5. *
  6. * @author Alex Rabe
  7. * @copyright 2008 - 2010
  8. *
  9. * @return void
  10. */
  11. function ngg_ajax_operation() {
  12. global $wpdb;
  13. // if nonce is not correct it returns -1
  14. check_ajax_referer( "ngg-ajax" );
  15. // check for correct capability
  16. if ( !is_user_logged_in() )
  17. die('-1');
  18. // check for correct NextGEN capability
  19. if ( !current_user_can('NextGEN Upload images') && !current_user_can('NextGEN Manage gallery') )
  20. die('-1');
  21. // include the ngg function
  22. include_once (dirname (__FILE__) . '/functions.php');
  23. // Get the image id
  24. if ( isset($_POST['image'])) {
  25. $id = (int) $_POST['image'];
  26. // let's get the image data
  27. $picture = nggdb::find_image( $id );
  28. // what do you want to do ?
  29. switch ( $_POST['operation'] ) {
  30. case 'create_thumbnail' :
  31. $result = nggAdmin::create_thumbnail($picture);
  32. break;
  33. case 'resize_image' :
  34. $result = nggAdmin::resize_image($picture);
  35. break;
  36. case 'rotate_cw' :
  37. $result = nggAdmin::rotate_image($picture, 'CW');
  38. nggAdmin::create_thumbnail($picture);
  39. break;
  40. case 'rotate_ccw' :
  41. $result = nggAdmin::rotate_image($picture, 'CCW');
  42. nggAdmin::create_thumbnail($picture);
  43. break;
  44. case 'set_watermark' :
  45. $result = nggAdmin::set_watermark($picture);
  46. break;
  47. case 'recover_image' :
  48. $result = nggAdmin::recover_image($picture);
  49. break;
  50. case 'import_metadata' :
  51. $result = nggAdmin::import_MetaData( $id );
  52. break;
  53. case 'get_image_ids' :
  54. $result = nggAdmin::get_image_ids( $id );
  55. break;
  56. default :
  57. do_action( 'ngg_ajax_' . $_POST['operation'] );
  58. die('-1');
  59. break;
  60. }
  61. // A success should return a '1'
  62. die ($result);
  63. }
  64. // The script should never stop here
  65. die('0');
  66. }
  67. add_action('wp_ajax_createNewThumb', 'createNewThumb');
  68. function createNewThumb() {
  69. global $ngg;
  70. // check for correct capability
  71. if ( !is_user_logged_in() )
  72. die('-1');
  73. // check for correct NextGEN capability
  74. if ( !current_user_can('NextGEN Manage gallery') )
  75. die('-1');
  76. include_once( nggGallery::graphic_library() );
  77. $id = (int) $_POST['id'];
  78. $picture = nggdb::find_image( $id );
  79. $x = round( $_POST['x'] * $_POST['rr'], 0);
  80. $y = round( $_POST['y'] * $_POST['rr'], 0);
  81. $w = round( $_POST['w'] * $_POST['rr'], 0);
  82. $h = round( $_POST['h'] * $_POST['rr'], 0);
  83. $thumb = new ngg_Thumbnail($picture->imagePath, TRUE);
  84. $thumb->crop($x, $y, $w, $h);
  85. // Note : the routine is a bit different to create_thumbnail(), due to rounding it's resized in the other way
  86. if ($ngg->options['thumbfix']) {
  87. // check for portrait format
  88. if ($thumb->currentDimensions['height'] > $thumb->currentDimensions['width']) {
  89. // first resize to the wanted height, here changed to create_thumbnail()
  90. $thumb->resize(0, $ngg->options['thumbheight']);
  91. // get optimal y startpos
  92. $ypos = ($thumb->currentDimensions['height'] - $ngg->options['thumbheight']) / 2;
  93. $thumb->crop(0, $ypos, $ngg->options['thumbwidth'],$ngg->options['thumbheight']);
  94. } else {
  95. // first resize to the wanted width, here changed to create_thumbnail()
  96. $thumb->resize($ngg->options['thumbwidth'], 0);
  97. //
  98. // get optimal x startpos
  99. $xpos = ($thumb->currentDimensions['width'] - $ngg->options['thumbwidth']) / 2;
  100. $thumb->crop($xpos, 0, $ngg->options['thumbwidth'],$ngg->options['thumbheight']);
  101. }
  102. //this create a thumbnail but keep ratio settings
  103. } else {
  104. $thumb->resize($ngg->options['thumbwidth'],$ngg->options['thumbheight']);
  105. }
  106. if ( $thumb->save($picture->thumbPath, 100)) {
  107. //read the new sizes
  108. $new_size = @getimagesize ( $picture->thumbPath );
  109. $size['width'] = $new_size[0];
  110. $size['height'] = $new_size[1];
  111. // add them to the database
  112. nggdb::update_image_meta($picture->pid, array( 'thumbnail' => $size) );
  113. echo "OK";
  114. } else {
  115. header('HTTP/1.1 500 Internal Server Error');
  116. echo "KO";
  117. }
  118. exit();
  119. }
  120. add_action('wp_ajax_rotateImage', 'ngg_rotateImage');
  121. function ngg_rotateImage() {
  122. // check for correct capability
  123. if ( !is_user_logged_in() )
  124. die('-1');
  125. // check for correct NextGEN capability
  126. if ( !current_user_can('NextGEN Manage gallery') )
  127. die('-1');
  128. require_once( dirname( dirname(__FILE__) ) . '/ngg-config.php');
  129. // include the ngg function
  130. include_once (dirname (__FILE__). '/functions.php');
  131. $ngg_options = get_option('ngg_options');
  132. $id = (int) $_POST['id'];
  133. $result = '-1';
  134. switch ( $_POST['ra'] ) {
  135. case 'cw' :
  136. $result = nggAdmin::rotate_image($id, 'CW');
  137. break;
  138. case 'ccw' :
  139. $result = nggAdmin::rotate_image($id, 'CCW');
  140. break;
  141. case 'fv' :
  142. $result = nggAdmin::rotate_image($id, 0, 'V');
  143. break;
  144. case 'fh' :
  145. $result = nggAdmin::rotate_image($id, 0, 'H');
  146. break;
  147. }
  148. // recreate the thumbnail
  149. nggAdmin::create_thumbnail($id);
  150. if ( $result == 1 )
  151. die('1');
  152. header('HTTP/1.1 500 Internal Server Error');
  153. die( $result );
  154. }
  155. add_action('wp_ajax_ngg_dashboard', 'ngg_ajax_dashboard');
  156. function ngg_ajax_dashboard() {
  157. require_once( dirname( dirname(__FILE__) ) . '/admin/admin.php');
  158. require_once( dirname( dirname(__FILE__) ) . '/admin/overview.php');
  159. if ( !current_user_can('NextGEN Gallery overview') )
  160. die('-1');
  161. @header( 'Content-Type: ' . get_option( 'html_type' ) . '; charset=' . get_option( 'blog_charset' ) );
  162. @header( 'X-Content-Type-Options: nosniff' );
  163. switch ( $_GET['jax'] ) {
  164. case 'ngg_lastdonators' :
  165. ngg_overview_donators();
  166. break;
  167. case 'dashboard_primary' :
  168. ngg_overview_news();
  169. break;
  170. case 'ngg_locale' :
  171. ngg_locale();
  172. break;
  173. case 'dashboard_plugins' :
  174. ngg_related_plugins();
  175. break;
  176. }
  177. die();
  178. }
  179. add_action('wp_ajax_ngg_file_browser', 'ngg_ajax_file_browser');
  180. /**
  181. * jQuery File Tree PHP Connector
  182. * @author Cory S.N. LaViska - A Beautiful Site (http://abeautifulsite.net/)
  183. * @version 1.0.1
  184. *
  185. * @return string folder content
  186. */
  187. function ngg_ajax_file_browser() {
  188. global $ngg;
  189. // check for correct NextGEN capability
  190. if ( !current_user_can('NextGEN Upload images') && !current_user_can('NextGEN Manage gallery') )
  191. die('No access');
  192. if ( !defined('ABSPATH') )
  193. die('No access');
  194. // if nonce is not correct it returns -1
  195. check_ajax_referer( 'ngg-ajax', 'nonce' );
  196. //PHP4 compat script
  197. if (!function_exists('scandir')) {
  198. function scandir($dir, $listDirectories = false, $skipDots = true ) {
  199. $dirArray = array();
  200. if ($handle = opendir($dir) ) {
  201. while (false !== ($file = readdir($handle))) {
  202. if (($file != '.' && $file != '..' ) || $skipDots == true) {
  203. if($listDirectories == false) { if(is_dir($file)) { continue; } }
  204. array_push($dirArray, basename($file) );
  205. }
  206. }
  207. closedir($handle);
  208. }
  209. return $dirArray;
  210. }
  211. }
  212. // start from the default path
  213. $root = trailingslashit ( WINABSPATH );
  214. // get the current directory
  215. $dir = trailingslashit ( urldecode($_POST['dir']) );
  216. if( file_exists($root . $dir) ) {
  217. $files = scandir($root . $dir);
  218. natcasesort($files);
  219. // The 2 counts for . and ..
  220. if( count($files) > 2 ) {
  221. echo "<ul class=\"jqueryDirTree\" style=\"display: none;\">";
  222. // return only directories
  223. foreach( $files as $file ) {
  224. //reserved name for the thumnbnails, don't use it as folder name
  225. if ( $file == 'thumbs')
  226. continue;
  227. if ( file_exists($root . $dir . $file) && $file != '.' && $file != '..' && is_dir($root . $dir . $file) ) {
  228. echo "<li class=\"directory collapsed\"><a href=\"#\" rel=\"" . esc_html($dir . $file) . "/\">" . esc_html($file) . "</a></li>";
  229. }
  230. }
  231. echo "</ul>";
  232. }
  233. }
  234. die();
  235. }
  236. add_action('wp_ajax_ngg_tinymce', 'ngg_ajax_tinymce');
  237. /**
  238. * Call TinyMCE window content via admin-ajax
  239. *
  240. * @since 1.7.0
  241. * @return html content
  242. */
  243. function ngg_ajax_tinymce() {
  244. // check for rights
  245. if ( !current_user_can('edit_pages') && !current_user_can('edit_posts') )
  246. die(__("You are not allowed to be here"));
  247. include_once( dirname( dirname(__FILE__) ) . '/admin/tinymce/window.php');
  248. die();
  249. }
  250. add_action( 'wp_ajax_ngg_rebuild_unique_slugs', 'ngg_ajax_rebuild_unique_slugs' );
  251. /**
  252. * This rebuild the slugs for albums, galleries and images as ajax routine, max 50 elements per request
  253. *
  254. * @since 1.7.0
  255. * @return string '1'
  256. */
  257. function ngg_ajax_rebuild_unique_slugs() {
  258. global $wpdb;
  259. $action = $_POST['_action'];
  260. $offset = (int) $_POST['offset'];
  261. switch ($action) {
  262. case 'images':
  263. $images = $wpdb->get_results("SELECT * FROM $wpdb->nggpictures ORDER BY pid ASC LIMIT $offset, 50", OBJECT_K);
  264. if ( is_array($images) ) {
  265. foreach ($images as $image) {
  266. //slug must be unique, we use the alttext for that
  267. $image->slug = nggdb::get_unique_slug( sanitize_title( $image->alttext ), 'image' );
  268. $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->nggpictures SET image_slug= '%s' WHERE pid = '%d'" , $image->slug, $image->pid ) );
  269. }
  270. }
  271. break;
  272. case 'gallery':
  273. $galleries = $wpdb->get_results("SELECT * FROM $wpdb->nggallery ORDER BY gid ASC LIMIT $offset, 50", OBJECT_K);
  274. if ( is_array($galleries) ) {
  275. foreach ($galleries as $gallery) {
  276. //slug must be unique, we use the title for that
  277. $gallery->slug = nggdb::get_unique_slug( sanitize_title( $gallery->title ), 'gallery' );
  278. $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->nggallery SET slug= '%s' WHERE gid = '%d'" , $gallery->slug, $gallery->gid ) );
  279. }
  280. }
  281. break;
  282. case 'album':
  283. $albumlist = $wpdb->get_results("SELECT * FROM $wpdb->nggalbum ORDER BY id ASC LIMIT $offset, 50", OBJECT_K);
  284. if ( is_array($albumlist) ) {
  285. foreach ($albumlist as $album) {
  286. //slug must be unique, we use the name for that
  287. $album->slug = nggdb::get_unique_slug( sanitize_title( $album->name ), 'album' );
  288. $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->nggalbum SET slug= '%s' WHERE id = '%d'" , $album->slug, $album->id ) );
  289. }
  290. }
  291. break;
  292. }
  293. die(1);
  294. }
  295. add_action('wp_ajax_ngg_image_check', 'ngg_ajax_image_check');
  296. /**
  297. * Test for various image resolution
  298. *
  299. * @since 1.7.3
  300. * @return result
  301. */
  302. function ngg_ajax_image_check() {
  303. // check for correct NextGEN capability
  304. if ( !current_user_can('NextGEN Upload images') )
  305. die('No access');
  306. if ( !defined('ABSPATH') )
  307. die('No access');
  308. $step = (int) $_POST['step'];
  309. // build the test sizes
  310. $sizes = array();
  311. $sizes[1] = array ( 'width' => 800, 'height' => 600);
  312. $sizes[2] = array ( 'width' => 1024, 'height' => 768);
  313. $sizes[3] = array ( 'width' => 1280, 'height' => 960); // 1MP
  314. $sizes[4] = array ( 'width' => 1600, 'height' => 1200); // 2MP
  315. $sizes[5] = array ( 'width' => 2016, 'height' => 1512); // 3MP
  316. $sizes[6] = array ( 'width' => 2272, 'height' => 1704); // 4MP
  317. $sizes[7] = array ( 'width' => 2560, 'height' => 1920); // 5MP
  318. $sizes[8] = array ( 'width' => 2848, 'height' => 2136); // 6MP
  319. $sizes[9] = array ( 'width' => 3072, 'height' => 2304); // 7MP
  320. $sizes[10] = array ( 'width' => 3264, 'height' => 2448); // 8MP
  321. $sizes[11] = array ( 'width' => 4048, 'height' => 3040); // 12MP
  322. if ( $step < 1 || $step > 11 )
  323. die('No vaild value');
  324. // let's test each image size
  325. $temp = imagecreatetruecolor ($sizes[$step]['width'], $sizes[$step]['height'] );
  326. imagedestroy ($temp);
  327. $result = array ('stat' => 'ok', 'message' => sprintf(__('Could create image with %s x %s pixel', 'nggallery'), $sizes[$step]['width'], $sizes[$step]['height'] ) );
  328. header('Content-Type: application/json; charset=' . get_option('blog_charset'), true);
  329. echo json_encode($result);
  330. die();
  331. }
  332. add_action('wp_ajax_ngg_test_head_footer', 'ngg_ajax_test_head_footer');
  333. /**
  334. * Check for the header / footer, parts taken from Matt Martz (http://sivel.net/)
  335. *
  336. * @see https://gist.github.com/378450
  337. * @since 1.7.3
  338. * @return result
  339. */
  340. function ngg_ajax_test_head_footer() {
  341. // Build the url to call, NOTE: uses home_url and thus requires WordPress 3.0
  342. $url = add_query_arg( array( 'test-head' => '', 'test-footer' => '' ), home_url() );
  343. // Perform the HTTP GET ignoring SSL errors
  344. $response = wp_remote_get( $url, array( 'sslverify' => false ) );
  345. // Grab the response code and make sure the request was sucessful
  346. $code = (int) wp_remote_retrieve_response_code( $response );
  347. if ( $code == 200 ) {
  348. global $head_footer_errors;
  349. $head_footer_errors = array();
  350. // Strip all tabs, line feeds, carriage returns and spaces
  351. $html = preg_replace( '/[\t\r\n\s]/', '', wp_remote_retrieve_body( $response ) );
  352. // Check to see if we found the existence of wp_head
  353. if ( ! strstr( $html, '<!--wp_head-->' ) )
  354. die('Missing the call to <?php wp_head(); ?> in your theme');
  355. // Check to see if we found the existence of wp_footer
  356. if ( ! strstr( $html, '<!--wp_footer-->' ) )
  357. die('Missing the call to <?php wp_footer(); ?> in your theme');
  358. }
  359. die('success');
  360. }
  361. ?>