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