get_id() != 'menubar') return; $menu->register_block( new RegisteredBlock('mbRandomPhoto', 'Menu Random Photo', 'MRP')); } function MRP_blockmanager_apply($menu_ref_arr) { global $user, $conf; $menu = & $menu_ref_arr[0]; if(( ($block=$menu->get_block('mbRandomPhoto'))!=null) and ($user['nb_total_images']>0) ) { $block->set_title($conf['MRP']['title']); $block->template=realpath(MRP_PATH.'template/menu_random_photo.tpl'); } } function MRP_end_page_header() { global $user, $template, $page, $conf; if(!array_key_exists('body_id', $page)) { /* * it seems the error message reported on mantis:1476 is displayed because * the 'body_id' doesn't exist in the $page * * not abble to reproduce the error, but initializing the key to an empty * value if it doesn't exist may be a sufficient solution */ $page['body_id']=""; } if($page['body_id'] == 'theCategoryPage' or ($conf['picture_menu'] and $page['body_id'] == 'thePicturePage')) { $randomPictProp = array( 'delay' => $conf['MRP']['delay'], 'blockHeight' => $conf['MRP']['height'], // 'showname' => $this->config['amm_randompicture_showname'], // 'showcomment' => $this->config['amm_randompicture_showcomment'], 'pictures' => getRandomPictures($conf['MRP']['randompicture_preload']), ); if (count($randomPictProp['pictures']) > 0) { $local_tpl = new Template(MRP_PATH.'/template', ""); $local_tpl->set_filename('body_page', realpath(MRP_PATH.'/template/menu_random_photo.js.tpl')); $local_tpl->assign('data', $randomPictProp); $template->append('head_elements', $local_tpl->parse('body_page', true)); } } } /** * return a list of thumbnails * each array items is an array * 'imageId' => (integer) * 'imageFile' => (String) * 'comment' => (String) * 'path' => (String) * 'catId' => (String) * 'name' => (String) * 'permalink' => (String) * 'imageName' => (String) * * @param Integer $number : number of returned images * @return Array */ function getRandomPictures($num=25) { global $user, $conf, $page, $header_msgs; $returned=array(); if (!isset($_SERVER["HTTP_USER_AGENT"]) || preg_match('/(Googlebot|bingbot|Baiduspider|yandex|AhrefsBot|msnbot|NCollector)/', $_SERVER["HTTP_USER_AGENT"])) { return($returned); } if (!isset($conf['MRP']['apply_to_albums'])) { $page['errors'][] = l10n('MenuRandomPhoto seems not to be installed/configured properly. Please remove and reinstall.'); return ($returned); } $sql=array(); // If we use all pics for MRP, then we use the pre-fetch, if we only show // some, then we can't use the pre-fetch but will limit to (hopefully) a few albums. // pre-fetch: // because ORDER BY RAND() can be very slow on a big database, let's // make a first query with no join and by security take 5 times // $num. We keep the result in session for 5 minutes. if ($conf['MRP']['apply_to_albums'] == "all" && ( !isset($_SESSION['mrp_random_pics']) or !isset($_SESSION['mrp_random_pics_generated_on']) or $_SESSION['mrp_random_pics_generated_on'] < time() - 5*60 // 5 minutes ago ) ) { $query = 'SELECT id FROM '.IMAGES_TABLE.' WHERE level <= '.$user['level'].' ORDER BY RAND() LIMIT '.($num*5).';'; $mrp = query2array($query, null, 'id'); if (!is_array($mrp) || count($mrp)<1) { $page['errors'][] = l10n('No pictures for MenuRandomPhoto found.'); return ($returned); } $_SESSION['mrp_random_pics'] = $mrp; $_SESSION['mrp_random_pics_generated_on'] = time(); } $sql['select'] = ' SELECT i.id as image_id, i.file as image_file, i.comment, i.path, c.id as catid, c.name, c.permalink, i.name as imgname '; $sql['from'] = ' FROM '.CATEGORIES_TABLE.' c JOIN '.IMAGE_CATEGORY_TABLE.' ic ON ic.category_id = c.id JOIN '.IMAGES_TABLE.' i ON i.id = ic.image_id '; $sql['where'] = 'WHERE i.level<='.$user['level']; if($user['forbidden_categories']!="") $sql['where'].=" AND c.id NOT IN (".$user['forbidden_categories'].") "; if ($conf['MRP']['apply_to_albums'] == "all") $sql['where'].= " AND i.id IN (".implode(',', $_SESSION['mrp_random_pics']).")"; else $sql['where'] .= " AND c.menurandomphoto_active='true' "; /* switch($this->config['amm_randompicture_selectMode']) { case 'f': $sql['from'].=", ".USER_INFOS_TABLE." ui LEFT JOIN ".FAVORITES_TABLE." f ON ui.user_id=f.user_id "; $sql['where'].=" AND ui.status='webmaster' AND f.image_id = i.id "; break; case 'c': $sql['where'].="AND ("; foreach($this->config['amm_randompicture_selectCat'] as $key => $val) { $sql['where'].=($key==0?'':' OR ')." FIND_IN_SET($val, c.uppercats) "; } $sql['where'].=") "; break; } */ $sql = $sql['select'].$sql['from'].$sql['where']." ORDER BY RAND() LIMIT $num;"; $result = pwg_query($sql); if($result) { if ($conf['MRP']['square']) { $height = $conf['MRP']['height']; if($height == 0) $derivative_params = ImageStdParams::get_by_type(IMG_SQUARE); else $derivative_params = ImageStdParams::get_custom($height, $height, 1, $height, $height); } else { $derivative_params = IMG_THUMB; } while($row=pwg_db_fetch_assoc($result)) { $row['section']='categories'; $row['category']=array( 'id' => $row['catid'], 'name' => $row['name'], 'permalink' => $row['permalink'] ); $row['link']=make_picture_url($row); $infos = array('id'=>$row['image_id'], 'path'=>$row['path']); $row['thumb']=DerivativeImage::url($derivative_params, $infos); $returned[]=$row; } } if (count($returned)<1) $page['errors'][] = l10n('No pictures for MenuRandomPhoto found.'); return($returned); } function MRP_admin_menu($menu) { $menu[] = array( 'NAME' => 'Menu Random Photo', 'URL' => MRP_ADMIN, ); return $menu; }