1 | <?php |
---|
2 | /* |
---|
3 | Plugin Name: Menu Random Photo |
---|
4 | Version: 2.8.6 |
---|
5 | Description: Adds a random picture block into menu |
---|
6 | Plugin URI: http://piwigo.org/ext/extension_view.php?eid=778 |
---|
7 | Authors: JanisV, romanf |
---|
8 | Has Settings: true |
---|
9 | */ |
---|
10 | |
---|
11 | global $conf; |
---|
12 | |
---|
13 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
---|
14 | |
---|
15 | define('MRP_ID', basename(dirname(__FILE__))); |
---|
16 | define('MRP_PATH' , PHPWG_PLUGINS_PATH . MRP_ID . '/'); |
---|
17 | define('MRP_ADMIN', get_root_url() . 'admin.php?page=plugin-' . MRP_ID); |
---|
18 | |
---|
19 | add_event_handler('init', 'MRP_init'); |
---|
20 | add_event_handler('blockmanager_register_blocks', 'MRP_register_menubar_blocks', EVENT_HANDLER_PRIORITY_NEUTRAL-1); |
---|
21 | |
---|
22 | function MRP_init() |
---|
23 | { |
---|
24 | global $conf, $user; |
---|
25 | |
---|
26 | $conf['MRP'] = safe_unserialize($conf['MRP']); |
---|
27 | |
---|
28 | if (defined('IN_ADMIN')) |
---|
29 | { |
---|
30 | add_event_handler('get_admin_plugin_menu_links', 'MRP_admin_menu'); |
---|
31 | } |
---|
32 | else |
---|
33 | { |
---|
34 | if ($user['theme'] != 'smartpocket') |
---|
35 | { |
---|
36 | add_event_handler('blockmanager_apply', 'MRP_blockmanager_apply'); |
---|
37 | add_event_handler('loc_end_page_header', 'MRP_end_page_header'); |
---|
38 | } |
---|
39 | } |
---|
40 | |
---|
41 | load_language('plugin.lang', MRP_PATH); |
---|
42 | } |
---|
43 | |
---|
44 | function MRP_register_menubar_blocks($menu_ref_arr) |
---|
45 | { |
---|
46 | $menu = & $menu_ref_arr[0]; |
---|
47 | if ($menu->get_id() != 'menubar') |
---|
48 | return; |
---|
49 | $menu->register_block( new RegisteredBlock('mbRandomPhoto', 'Menu Random Photo', 'MRP')); |
---|
50 | } |
---|
51 | |
---|
52 | function MRP_blockmanager_apply($menu_ref_arr) |
---|
53 | { |
---|
54 | global $user, $conf; |
---|
55 | |
---|
56 | $menu = & $menu_ref_arr[0]; |
---|
57 | |
---|
58 | if(( |
---|
59 | ($block=$menu->get_block('mbRandomPhoto'))!=null) and |
---|
60 | ($user['nb_total_images']>0) |
---|
61 | ) |
---|
62 | { |
---|
63 | $block->set_title($conf['MRP']['title']); |
---|
64 | $block->template=realpath(MRP_PATH.'template/menu_random_photo.tpl'); |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | function MRP_end_page_header() |
---|
69 | { |
---|
70 | global $user, $template, $page, $conf; |
---|
71 | |
---|
72 | if(!array_key_exists('body_id', $page)) |
---|
73 | { |
---|
74 | /* |
---|
75 | * it seems the error message reported on mantis:1476 is displayed because |
---|
76 | * the 'body_id' doesn't exist in the $page |
---|
77 | * |
---|
78 | * not abble to reproduce the error, but initializing the key to an empty |
---|
79 | * value if it doesn't exist may be a sufficient solution |
---|
80 | */ |
---|
81 | $page['body_id']=""; |
---|
82 | } |
---|
83 | |
---|
84 | if($page['body_id'] == 'theCategoryPage' or ($conf['picture_menu'] and $page['body_id'] == 'thePicturePage')) |
---|
85 | { |
---|
86 | $randomPictProp = array( |
---|
87 | 'delay' => $conf['MRP']['delay'], |
---|
88 | 'blockHeight' => $conf['MRP']['height'], |
---|
89 | // 'showname' => $this->config['amm_randompicture_showname'], |
---|
90 | // 'showcomment' => $this->config['amm_randompicture_showcomment'], |
---|
91 | 'pictures' => getRandomPictures($conf['MRP']['randompicture_preload']), |
---|
92 | ); |
---|
93 | |
---|
94 | if (count($randomPictProp['pictures']) > 0) |
---|
95 | { |
---|
96 | $local_tpl = new Template(MRP_PATH.'/template', ""); |
---|
97 | $local_tpl->set_filename('body_page', realpath(MRP_PATH.'/template/menu_random_photo.js.tpl')); |
---|
98 | |
---|
99 | $local_tpl->assign('data', $randomPictProp); |
---|
100 | |
---|
101 | $template->append('head_elements', $local_tpl->parse('body_page', true)); |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * return a list of thumbnails |
---|
108 | * each array items is an array |
---|
109 | * 'imageId' => (integer) |
---|
110 | * 'imageFile' => (String) |
---|
111 | * 'comment' => (String) |
---|
112 | * 'path' => (String) |
---|
113 | * 'catId' => (String) |
---|
114 | * 'name' => (String) |
---|
115 | * 'permalink' => (String) |
---|
116 | * 'imageName' => (String) |
---|
117 | * |
---|
118 | * @param Integer $number : number of returned images |
---|
119 | * @return Array |
---|
120 | */ |
---|
121 | function getRandomPictures($num=25) |
---|
122 | { |
---|
123 | global $user, $conf, $page, $header_msgs; |
---|
124 | |
---|
125 | $returned=array(); |
---|
126 | |
---|
127 | if (!isset($_SERVER["HTTP_USER_AGENT"]) || |
---|
128 | preg_match('/(Googlebot|bingbot|Baiduspider|yandex|AhrefsBot|msnbot|NCollector)/', $_SERVER["HTTP_USER_AGENT"])) |
---|
129 | { |
---|
130 | return($returned); |
---|
131 | } |
---|
132 | |
---|
133 | if (!isset($conf['MRP']['apply_to_albums'])) |
---|
134 | { |
---|
135 | $page['errors'][] = l10n('MenuRandomPhoto seems not to be installed/configured properly. Please remove and reinstall.'); |
---|
136 | return ($returned); |
---|
137 | } |
---|
138 | |
---|
139 | $sql=array(); |
---|
140 | |
---|
141 | // If we use all pics for MRP, then we use the pre-fetch, if we only show |
---|
142 | // some, then we can't use the pre-fetch but will limit to (hopefully) a few albums. |
---|
143 | |
---|
144 | // pre-fetch: |
---|
145 | // because ORDER BY RAND() can be very slow on a big database, let's |
---|
146 | // make a first query with no join and by security take 5 times |
---|
147 | // $num. We keep the result in session for 5 minutes. |
---|
148 | if ($conf['MRP']['apply_to_albums'] == "all" && |
---|
149 | ( |
---|
150 | !isset($_SESSION['mrp_random_pics']) |
---|
151 | or !isset($_SESSION['mrp_random_pics_generated_on']) |
---|
152 | or $_SESSION['mrp_random_pics_generated_on'] < time() - 5*60 // 5 minutes ago |
---|
153 | ) |
---|
154 | ) |
---|
155 | { |
---|
156 | $query = 'SELECT id FROM '.IMAGES_TABLE.' WHERE level <= '.$user['level'].' ORDER BY RAND() LIMIT '.($num*5).';'; |
---|
157 | |
---|
158 | $mrp = query2array($query, null, 'id'); |
---|
159 | if (!is_array($mrp) || count($mrp)<1) |
---|
160 | { |
---|
161 | $page['errors'][] = l10n('No pictures for MenuRandomPhoto found.'); |
---|
162 | return ($returned); |
---|
163 | } |
---|
164 | |
---|
165 | $_SESSION['mrp_random_pics'] = $mrp; |
---|
166 | $_SESSION['mrp_random_pics_generated_on'] = time(); |
---|
167 | } |
---|
168 | |
---|
169 | $sql['select'] = ' |
---|
170 | SELECT |
---|
171 | i.id as image_id, |
---|
172 | i.file as image_file, |
---|
173 | i.comment, |
---|
174 | i.path, |
---|
175 | c.id as catid, |
---|
176 | c.name, |
---|
177 | c.permalink, |
---|
178 | i.name as imgname |
---|
179 | '; |
---|
180 | |
---|
181 | $sql['from'] = ' |
---|
182 | FROM '.CATEGORIES_TABLE.' c |
---|
183 | JOIN '.IMAGE_CATEGORY_TABLE.' ic ON ic.category_id = c.id |
---|
184 | JOIN '.IMAGES_TABLE.' i ON i.id = ic.image_id |
---|
185 | '; |
---|
186 | |
---|
187 | $sql['where'] = 'WHERE i.level<='.$user['level']; |
---|
188 | |
---|
189 | if($user['forbidden_categories']!="") |
---|
190 | $sql['where'].=" AND c.id NOT IN (".$user['forbidden_categories'].") "; |
---|
191 | |
---|
192 | if ($conf['MRP']['apply_to_albums'] == "all") |
---|
193 | $sql['where'].= " AND i.id IN (".implode(',', $_SESSION['mrp_random_pics']).")"; |
---|
194 | else |
---|
195 | $sql['where'] .= " AND c.menurandomphoto_active='true' "; |
---|
196 | |
---|
197 | /* |
---|
198 | switch($this->config['amm_randompicture_selectMode']) |
---|
199 | { |
---|
200 | case 'f': |
---|
201 | $sql['from'].=", ".USER_INFOS_TABLE." ui |
---|
202 | LEFT JOIN ".FAVORITES_TABLE." f ON ui.user_id=f.user_id "; |
---|
203 | $sql['where'].=" AND ui.status='webmaster' |
---|
204 | AND f.image_id = i.id "; |
---|
205 | break; |
---|
206 | case 'c': |
---|
207 | $sql['where'].="AND ("; |
---|
208 | foreach($this->config['amm_randompicture_selectCat'] as $key => $val) |
---|
209 | { |
---|
210 | $sql['where'].=($key==0?'':' OR ')." FIND_IN_SET($val, c.uppercats) "; |
---|
211 | } |
---|
212 | $sql['where'].=") "; |
---|
213 | break; |
---|
214 | } |
---|
215 | */ |
---|
216 | $sql = $sql['select'].$sql['from'].$sql['where']." ORDER BY RAND() LIMIT $num;"; |
---|
217 | |
---|
218 | $result = pwg_query($sql); |
---|
219 | if($result) |
---|
220 | { |
---|
221 | if ($conf['MRP']['square']) |
---|
222 | { |
---|
223 | $height = $conf['MRP']['height']; |
---|
224 | if($height == 0) |
---|
225 | $derivative_params = ImageStdParams::get_by_type(IMG_SQUARE); |
---|
226 | else |
---|
227 | $derivative_params = ImageStdParams::get_custom($height, $height, 1, $height, $height); |
---|
228 | } |
---|
229 | else |
---|
230 | { |
---|
231 | $derivative_params = IMG_THUMB; |
---|
232 | } |
---|
233 | |
---|
234 | while($row=pwg_db_fetch_assoc($result)) |
---|
235 | { |
---|
236 | $row['section']='categories'; |
---|
237 | $row['category']=array( |
---|
238 | 'id' => $row['catid'], |
---|
239 | 'name' => $row['name'], |
---|
240 | 'permalink' => $row['permalink'] |
---|
241 | ); |
---|
242 | |
---|
243 | $row['link']=make_picture_url($row); |
---|
244 | $infos = array('id'=>$row['image_id'], 'path'=>$row['path']); |
---|
245 | $row['thumb']=DerivativeImage::url($derivative_params, $infos); |
---|
246 | |
---|
247 | $returned[]=$row; |
---|
248 | } |
---|
249 | } |
---|
250 | |
---|
251 | if (count($returned)<1) |
---|
252 | $page['errors'][] = l10n('No pictures for MenuRandomPhoto found.'); |
---|
253 | |
---|
254 | return($returned); |
---|
255 | } |
---|
256 | |
---|
257 | function MRP_admin_menu($menu) |
---|
258 | { |
---|
259 | $menu[] = array( |
---|
260 | 'NAME' => 'Menu Random Photo', |
---|
261 | 'URL' => MRP_ADMIN, |
---|
262 | ); |
---|
263 | |
---|
264 | return $menu; |
---|
265 | } |
---|