nggGallery

Main PHP class for the WordPress plugin NextGEN Gallery.

Defined (1)

The class is defined in the following location(s).

/lib/core.php  
  1. class nggGallery { 
  2.  
  3. /** 
  4. * Show a error messages 
  5. */ 
  6. static function show_error($message) { 
  7. echo '<div class="error" id="error"><p>' . $message . '</p></div>'; 
  8.  
  9. /** 
  10. * Show a system messages 
  11. */ 
  12. static function show_message($message) { 
  13. echo '<div class="updated fade" id="message"><p>' . $message . '</p></div>'; 
  14.  
  15. /** 
  16. * get the thumbnail url to the image 
  17. */ 
  18. static function get_thumbnail_url($imageID, $picturepath = '', $fileName = '') { 
  19.  
  20. // get the complete url to the thumbnail 
  21. global $wpdb; 
  22.  
  23. // safety first 
  24. $imageID = (int) $imageID; 
  25.  
  26. // get gallery values 
  27. if ( empty($fileName) ) { 
  28. list($fileName, $picturepath ) = $wpdb->get_row("SELECT p.filename, g.path FROM $wpdb->nggpictures AS p INNER JOIN $wpdb->nggallery AS g ON (p.galleryid = g.gid) WHERE p.pid = '$imageID' ", ARRAY_N); 
  29.  
  30. if ( empty($picturepath) ) { 
  31. $picturepath = $wpdb->get_var("SELECT g.path FROM $wpdb->nggpictures AS p INNER JOIN $wpdb->nggallery AS g ON (p.galleryid = g.gid) WHERE p.pid = '$imageID' "); 
  32.  
  33. // set gallery url 
  34. $folder_url = site_url() . '/' . $picturepath.nggGallery::get_thumbnail_folder($picturepath, FALSE); 
  35. $thumbnailURL = $folder_url . 'thumbs_' . $fileName; 
  36.  
  37. return $thumbnailURL; 
  38.  
  39. /** 
  40. * get the complete url to the image 
  41. */ 
  42. static function get_image_url($imageID, $picturepath = '', $fileName = '') { 
  43. global $wpdb; 
  44.  
  45. // safety first 
  46. $imageID = (int) $imageID; 
  47.  
  48. // get gallery values 
  49. if (empty($fileName)) { 
  50. list($fileName, $picturepath ) = $wpdb->get_row("SELECT p.filename, g.path FROM $wpdb->nggpictures AS p INNER JOIN $wpdb->nggallery AS g ON (p.galleryid = g.gid) WHERE p.pid = '$imageID' ", ARRAY_N); 
  51.  
  52. if (empty($picturepath)) { 
  53. $picturepath = $wpdb->get_var("SELECT g.path FROM $wpdb->nggpictures AS p INNER JOIN $wpdb->nggallery AS g ON (p.galleryid = g.gid) WHERE p.pid = '$imageID' "); 
  54.  
  55. // set gallery url 
  56. $imageURL = site_url() . '/' . $picturepath . '/' . $fileName; 
  57.  
  58. return $imageURL; 
  59.  
  60. /** 
  61. * nggGallery::get_thumbnail_folder() 
  62. * @param mixed $gallerypath 
  63. * @param bool $include_Abspath 
  64. * @return string $foldername 
  65. */ 
  66. static function create_thumbnail_folder($gallerypath, $include_Abspath = TRUE) { 
  67. if (!$include_Abspath) { 
  68. $gallerypath = WINABSPATH . $gallerypath; 
  69.  
  70. if (!file_exists($gallerypath)) { 
  71. return FALSE; 
  72.  
  73. if (is_dir($gallerypath . '/thumbs/')) { 
  74. return '/thumbs/'; 
  75.  
  76. if (is_admin()) { 
  77. if (!is_dir($gallerypath . '/thumbs/')) { 
  78. if ( !wp_mkdir_p($gallerypath . '/thumbs/') ) { 
  79. if (SAFE_MODE) { 
  80. nggAdmin::check_safemode($gallerypath . '/thumbs/'); 
  81. } else { 
  82. nggGallery::show_error(__('Unable to create directory ', 'nggallery') . $gallerypath . '/thumbs !'); 
  83. return FALSE; 
  84. return '/thumbs/'; 
  85.  
  86. return FALSE; 
  87.  
  88.  
  89. /** 
  90. * nggGallery::get_thumbnail_folder() 
  91. * @param mixed $gallerypath 
  92. * @param bool $include_Abspath 
  93. * @deprecated use create_thumbnail_folder() if needed; 
  94. * @return string $foldername 
  95. */ 
  96. static function get_thumbnail_folder($gallerypath, $include_Abspath = TRUE) { 
  97. return nggGallery::create_thumbnail_folder($gallerypath, $include_Abspath); 
  98.  
  99. /** 
  100. * nggGallery::get_thumbnail_prefix() - obsolete 
  101. * @param string $gallerypath 
  102. * @param bool $include_Abspath 
  103. * @deprecated prefix is now fixed to "thumbs_"; 
  104. * @return string "thumbs_"; 
  105. */ 
  106. static function get_thumbnail_prefix($gallerypath, $include_Abspath = TRUE) { 
  107. return 'thumbs_'; 
  108.  
  109. /** 
  110. * nggGallery::get_option() - get the options and overwrite them with custom meta settings 
  111. * @param string $key 
  112. * @return array $options 
  113. */ 
  114. static function get_option($key) { 
  115. global $post; 
  116.  
  117. // get first the options from the database 
  118. $options = get_option($key); 
  119.  
  120. if ( $post == null ) 
  121. return $options; 
  122.  
  123. // Get all key/value data for the current post. 
  124. $meta_array = get_post_custom(); 
  125.  
  126. // Ensure that this is a array 
  127. if ( !is_array($meta_array) ) 
  128. $meta_array = array($meta_array); 
  129.  
  130. // assign meta key to db setting key 
  131. $meta_tags = array( 
  132. 'string' => array( 
  133. 'ngg_gal_ShowOrder' => 'galShowOrder',  
  134. 'ngg_gal_Sort' => 'galSort',  
  135. 'ngg_gal_SortDirection' => 'galSortDir',  
  136. 'ngg_gal_ShowDescription' => 'galShowDesc',  
  137. 'ngg_ir_Audio' => 'irAudio',  
  138. 'ngg_ir_Overstretch' => 'irOverstretch',  
  139. 'ngg_ir_Transition' => 'irTransition',  
  140. 'ngg_ir_Backcolor' => 'irBackcolor',  
  141. 'ngg_ir_Frontcolor' => 'irFrontcolor',  
  142. 'ngg_ir_Lightcolor' => 'irLightcolor',  
  143. 'ngg_slideshowFX' => 'slideFx',  
  144. ),  
  145.  
  146. 'int' => array( 
  147. 'ngg_gal_Images' => 'galImages',  
  148. 'ngg_gal_Columns' => 'galColumns',  
  149. 'ngg_paged_Galleries' => 'galPagedGalleries',  
  150. 'ngg_ir_Width' => 'irWidth',  
  151. 'ngg_ir_Height' => 'irHeight',  
  152. 'ngg_ir_Rotatetime' => 'irRotatetime' 
  153. ),  
  154.  
  155. 'bool' => array( 
  156. 'ngg_gal_ShowSlide' => 'galShowSlide',  
  157. 'ngg_gal_ShowPiclense' => 'usePicLens',  
  158. 'ngg_gal_ImageBrowser' => 'galImgBrowser',  
  159. 'ngg_gal_HideImages' => 'galHiddenImg',  
  160. 'ngg_ir_Shuffle' => 'irShuffle',  
  161. 'ngg_ir_LinkFromDisplay' => 'irLinkfromdisplay',  
  162. 'ngg_ir_ShowNavigation' => 'irShownavigation',  
  163. 'ngg_ir_ShowWatermark' => 'irWatermark',  
  164. 'ngg_ir_Kenburns' => 'irKenburns' 
  165. ); 
  166.  
  167. foreach ($meta_tags as $typ => $meta_keys) { 
  168. foreach ($meta_keys as $key => $db_value) { 
  169. // if the kex exist overwrite it with the custom field 
  170. if (array_key_exists($key, $meta_array)) { 
  171. switch ($typ) { 
  172. case 'string': 
  173. $options[$db_value] = (string) esc_attr($meta_array[$key][0]); 
  174. break; 
  175. case 'int': 
  176. $options[$db_value] = (int) $meta_array[$key][0]; 
  177. break; 
  178. case 'bool': 
  179. $options[$db_value] = (bool) $meta_array[$key][0]; 
  180. break; 
  181.  
  182. return $options; 
  183.  
  184. /** 
  185. * nggGallery::scale_image() - Scale down a image 
  186. * @param mixed $location (filename) 
  187. * @param int $maxw - max width 
  188. * @param int $maxh - max height 
  189. * @return array (width, heigth) 
  190. */ 
  191. static function scale_image($location, $maxw = 0, $maxh = 0) { 
  192. $img = @getimagesize($location); 
  193. if ($img) { 
  194. $w = $img[0]; 
  195. $h = $img[1]; 
  196.  
  197. $dim = array('w', 'h'); 
  198. foreach($dim AS $val) { 
  199. $max = "max{$val}"; 
  200. if(${$val} > ${$max} && ${$max}) { 
  201. $alt = ($val == 'w') ? 'h' : 'w'; 
  202. $ratio = ${$alt} / ${$val}; 
  203. ${$val} = ${$max}; 
  204. ${$alt} = ${$val} * $ratio; 
  205.  
  206. return array( $w, $h ); 
  207. return false; 
  208.  
  209. /** 
  210. * Renders a section of user display code. The code is first checked for in the current theme display directory 
  211. * before defaulting to the plugin 
  212. * Call the function : nggGallery::render ('template_name', array ('var1' => $var1, 'var2' => $var2)); 
  213. * @autor John Godley 
  214. * @param string $template_name Name of the template file (without extension) 
  215. * @param string $vars Array of variable name=>value that is available to the display code (optional) 
  216. * @param bool $callback In case we check we didn't find template we tested it one time more (optional) 
  217. * @return void 
  218. **/ 
  219. static function render($template_name, $vars = array (), $callback = false) { 
  220. foreach ($vars AS $key => $val) { 
  221. $$key = $val; 
  222.  
  223. // hook into the render feature to allow other plugins to include templates 
  224. $custom_template = apply_filters( 'ngg_render_template', false, $template_name ); 
  225.  
  226. if ( ( $custom_template != false ) && file_exists ($custom_template) ) { 
  227. include ( $custom_template ); 
  228. //search in theme folder 
  229. } elseif (file_exists (get_stylesheet_directory() . "/nggallery/$template_name.php")) { 
  230. include (get_stylesheet_directory() . "/nggallery/$template_name.php"); 
  231. //search in WP_CONTENT_DIR 
  232. } elseif (file_exists (WP_CONTENT_DIR . "/ngg_styles/$template_name.php")) { 
  233. include (WP_CONTENT_DIR . "/ngg_styles/$template_name.php"); 
  234. //use defaults 
  235. } elseif (file_exists (NGGALLERY_ABSPATH . "/view/$template_name.php")) { 
  236. include (NGGALLERY_ABSPATH . "/view/$template_name.php"); 
  237. } elseif ( $callback === true ) { 
  238. echo "<p>Rendering of template $template_name.php failed</p>"; 
  239. } else { 
  240. //test without the "-template" name one time more 
  241.  
  242. $explode = explode('-', $template_name, 2); 
  243. $template_name = array_shift($explode); 
  244. nggGallery::render ($template_name, $vars , true); 
  245.  
  246. /** 
  247. * Captures an section of user display code. 
  248. * @autor John Godley 
  249. * @param string $template_name Name of the template file (without extension) 
  250. * @param string $vars Array of variable name=>value that is available to the display code (optional) 
  251. * @return string 
  252. **/ 
  253. static function capture ($template_name, $vars = array ()) { 
  254. ob_start (); 
  255. nggGallery::render ($template_name, $vars); 
  256. $output = ob_get_contents (); 
  257. ob_end_clean (); 
  258.  
  259. return $output; 
  260.  
  261. /** 
  262. * nggGallery::graphic_library() - switch between GD and ImageMagick 
  263. * @return path to the selected library 
  264. */ 
  265. static function graphic_library() { 
  266.  
  267. $ngg_options = get_option('ngg_options'); 
  268.  
  269. if ( $ngg_options['graphicLibrary'] == 'im') 
  270. return NGGALLERY_ABSPATH . '/lib/imagemagick.inc.php'; 
  271. else 
  272. return NGGALLERY_ABSPATH . '/lib/gd.thumbnail.inc.php'; 
  273.  
  274.  
  275.  
  276. /** 
  277. * Find where is the gallery stylesheet located. 
  278. * Combines several techniques to find out where is the current 
  279. * gallery stylesheet located. 
  280. * 20140924: Simplification & Documentation 
  281. * 20140924: included 
  282. * @since 1.9.13 
  283. * @access static 
  284. * @see (none) 
  285. * @link Filter List http://wpgetready.com/wiki/nextcellent-plugin/nextcellent-filter-list/ 
  286. * @global type $varname Short description. 
  287. * @param (none) 
  288. * @return bool|mixed|string|void 
  289. */ 
  290.  
  291. static function get_theme_css_file() { 
  292. //Allow to include a custom stylesheet, return stylesheet uri 
  293. $stylesheet = apply_filters( 'ngg_load_stylesheet', false ); 
  294. //Is there a stylsheet customization? 
  295. if ( $stylesheet !== false ) { 
  296. return ($stylesheet); //Yes, return it 
  297. //No filter customization. Is the user putting a custom stylesheet on his/her theme? 
  298. if ( file_exists (get_stylesheet_directory() . '/nggallery.css') ) { 
  299. return get_stylesheet_directory_uri() . '/nggallery.css'; //Yes, return uri to the custom style 
  300. //No filter and no custom style. Bye bye 
  301. return false; 
  302.  
  303. /** 
  304. * Support for i18n with wpml, polyglot or qtrans 
  305. * @param string $in 
  306. * @param string $name (optional) required for wpml to determine the type of translation 
  307. * @return string $in localized 
  308. */ 
  309. static function i18n($in, $name = null) { 
  310.  
  311. if ( function_exists( 'langswitch_filter_langs_with_message' ) ) 
  312. $in = langswitch_filter_langs_with_message($in); 
  313.  
  314. if ( function_exists( 'polyglot_filter' )) 
  315. $in = polyglot_filter($in); 
  316.  
  317. if ( function_exists( 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage' )) 
  318. $in = qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($in); 
  319.  
  320. if (is_string($name) && !empty($name) && function_exists('icl_translate')) 
  321. $in = icl_translate('plugin_ngg', $name, $in, true); 
  322.  
  323. $in = apply_filters('localization', $in); 
  324.  
  325. return $in; 
  326.  
  327. /** 
  328. * This function register strings for the use with WPML plugin (see http://wpml.org/ ) 
  329. * @param object $image 
  330. * @return void 
  331. */ 
  332. static function RegisterString($image) { 
  333. if (function_exists('icl_register_string')) { 
  334. global $wpdb; 
  335. icl_register_string('plugin_ngg', 'pic_' . $image->pid . '_description', $image->description, TRUE); 
  336. icl_register_string('plugin_ngg', 'pic_' . $image->pid . '_alttext', $image->alttext, TRUE); 
  337.  
  338. /** 
  339. * Check the memory_limit and calculate a recommended memory size 
  340. * @since V1.2.0 
  341. * @return string message about recommended image size 
  342. */ 
  343. static function check_memory_limit() { 
  344.  
  345. if ( (function_exists('memory_get_usage')) && (ini_get('memory_limit')) ) { 
  346.  
  347. // get memory limit 
  348. $memory_limit = ini_get('memory_limit'); 
  349. if ($memory_limit != '') 
  350. $memory_limit = substr($memory_limit, 0, -1) * 1024 * 1024; 
  351.  
  352. // calculate the free memory 
  353. $freeMemory = $memory_limit - memory_get_usage(); 
  354.  
  355. // build the test sizes 
  356. $sizes = array(); 
  357. $sizes[] = array ( 'width' => 800, 'height' => 600); 
  358. $sizes[] = array ( 'width' => 1024, 'height' => 768); 
  359. $sizes[] = array ( 'width' => 1280, 'height' => 960); // 1MP 
  360. $sizes[] = array ( 'width' => 1600, 'height' => 1200); // 2MP 
  361. $sizes[] = array ( 'width' => 2016, 'height' => 1512); // 3MP 
  362. $sizes[] = array ( 'width' => 2272, 'height' => 1704); // 4MP 
  363. $sizes[] = array ( 'width' => 2560, 'height' => 1920); // 5MP 
  364.  
  365. // test the classic sizes 
  366. foreach ($sizes as $size) { 
  367. // very, very rough estimation 
  368. if ($freeMemory < round( $size['width'] * $size['height'] * 5.09 )) { 
  369. $result = sprintf( __( 'Note : Based on your server memory limit you should not upload larger images then <strong>%d x %d</strong> pixel', 'nggallery' ), $size['width'], $size['height']); 
  370. return $result; 
  371. return; 
  372.  
  373. /** 
  374. * Slightly modfifed version of pathinfo(), clean up filename & rename jpeg to jpg 
  375. * @param string $name The name being checked. 
  376. * @return array containing information about file 
  377. */ 
  378. static function fileinfo( $name ) { 
  379.  
  380. //Sanitizes a filename replacing whitespace with dashes 
  381. $name = sanitize_file_name($name); 
  382.  
  383. //get the parts of the name 
  384. $filepart = pathinfo ( strtolower($name) ); 
  385.  
  386. if ( empty($filepart) ) 
  387. return false; 
  388.  
  389. // required until PHP 5.2.0 
  390. if ( empty($filepart['filename']) ) 
  391. $filepart['filename'] = substr($filepart['basename'], 0 , strlen($filepart['basename']) - (strlen($filepart['extension']) + 1) ); 
  392.  
  393. $filepart['filename'] = sanitize_title_with_dashes( $filepart['filename'] ); 
  394.  
  395. //extension jpeg will not be recognized by the slideshow, so we rename it 
  396. $filepart['extension'] = ($filepart['extension'] == 'jpeg') ? 'jpg' : $filepart['extension']; 
  397.  
  398. //combine the new file name 
  399. $filepart['basename'] = $filepart['filename'] . '.' . $filepart['extension']; 
  400.  
  401. return $filepart; 
  402.  
  403. /** 
  404. * Check for extended capabilites. Must previously registers with add_ngg_capabilites() 
  405. * @since 1.5.0 
  406. * @param string $capability 
  407. * @return bool $result of capability check 
  408. */ 
  409. static function current_user_can( $capability ) { 
  410.  
  411. global $_ngg_capabilites; 
  412.  
  413. if ( is_array($_ngg_capabilites) ) 
  414. if ( in_array($capability , $_ngg_capabilites) ) 
  415. return current_user_can( $capability ); 
  416.  
  417. return true; 
  418.  
  419. /** 
  420. * Check for extended capabilites and echo disabled="disabled" for input form 
  421. * @since 1.5.0 
  422. * @param string $capability 
  423. * @return void 
  424. */ 
  425. static function current_user_can_form( $capability ) { 
  426.  
  427. if ( !nggGallery::current_user_can( $capability )) 
  428. echo 'disabled="disabled"'; 
  429.  
  430. /** 
  431. * Register more capabilities for custom use and add it to the administrator 
  432. * @since 1.5.0 
  433. * @param string $capability 
  434. * @param bool $register the new capability automatic to the admin role 
  435. * @return void 
  436. */ 
  437. static function add_capabilites( $capability , $register = true ) { 
  438. global $_ngg_capabilites; 
  439.  
  440. if ( !is_array($_ngg_capabilites) ) 
  441. $_ngg_capabilites = array(); 
  442.  
  443. $_ngg_capabilites[] = $capability; 
  444.  
  445. if ( $register ) { 
  446. $role = get_role('administrator'); 
  447. if ( !empty($role) ) 
  448. $role->add_cap( $capability ); 
  449.  
  450.  
  451. /** 
  452. * Check for mobile user agent 
  453. * @since 1.6.0 
  454. * @author Part taken from WPtouch plugin (http://www.bravenewcode.com) 
  455. * @return bool $result of check 
  456. */ 
  457. static function detect_mobile_phone() { 
  458.  
  459. $useragents = array(); 
  460.  
  461. // Check if WPtouch is running 
  462. if ( function_exists('bnc_wptouch_get_user_agents') ) 
  463. $useragents = bnc_wptouch_get_user_agents(); 
  464. else { 
  465. $useragents = array( 
  466. "iPhone", // Apple iPhone 
  467. "iPod", // Apple iPod touch 
  468. "Android", // 1.5+ Android 
  469. "dream", // Pre 1.5 Android 
  470. "CUPCAKE", // 1.5+ Android 
  471. "blackberry9500", // Storm 
  472. "blackberry9530", // Storm 
  473. "blackberry9520", // Storm v2 
  474. "blackberry9550", // Storm v2 
  475. "blackberry9800", // Torch 
  476. "webOS", // Palm Pre Experimental 
  477. "incognito", // Other iPhone browser 
  478. "webmate" // Other iPhone browser 
  479. ); 
  480.  
  481. asort( $useragents ); 
  482.  
  483. // Godfather Steve says no to flash 
  484. if ( is_array($useragents) ) 
  485. $useragents[] = "iPad"; // Apple iPad; 
  486.  
  487. // WPtouch User Agent Filter 
  488. $useragents = apply_filters( 'wptouch_user_agents', $useragents ); 
  489.  
  490. foreach ( $useragents as $useragent ) { 
  491. if ( preg_match( "#$useragent#i", $_SERVER['HTTP_USER_AGENT'] ) ) 
  492. return true; 
  493.  
  494. return false; 
  495.  
  496. /** 
  497. * get_memory_usage 
  498. * @access only for debug purpose 
  499. * @since 1.8.3 
  500. * @param string $text 
  501. * @return void 
  502. */ 
  503. static function get_memory( $text = '' ) { 
  504. global $memory; 
  505.  
  506. $memory_peak = memory_get_usage(); 
  507. $diff = 0; 
  508.  
  509. if ( isset($memory) ) 
  510. $diff = $memory_peak - $memory; 
  511.  
  512. $exp = ($diff < 0) ? '-' : ''; 
  513. $diff = ($exp == '-') ? 0 - $diff : $diff; 
  514.  
  515. $memory = $memory_peak; 
  516.  
  517. $unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb'); 
  518. $rounded = @round($diff/pow(1024, ($i=floor(log($diff, 1024)))), 2).' '.$unit[$i]; 
  519.  
  520. echo $text . ': ' . $exp . $rounded .'<br />'; 
  521.  
  522.  
  523. /** 
  524. * Show NextGEN Version in header 
  525. * @since 1.9.0 
  526. * @return void 
  527. */ 
  528. static function nextgen_version() { 
  529. global $ngg; 
  530. echo apply_filters('show_nextgen_version', '<!-- <meta name="NextGEN" version="'. $ngg->version . '" /> -->' . "\n"); 
  531.  
  532. /** 
  533. * Prevents injection filtering HTML Code 
  534. * 20140604: Improved based on suggestions of jayque9 
  535. * http://wordpress.org/support/topic/prevent-removal-of-html-code-from-image-descriptions 
  536. */ 
  537. static function suppress_injection 
  538. ($html_text) { 
  539. global $allowedposttags; 
  540. return wp_kses(stripslashes($html_text), $allowedposttags);