Is there a way to determine, given an id, if it's for a category or image?
I'm working on my Physical Photo Move plugin, adding support for physical directories. As it stands (for images), the plugin uses the $_GET['tab'] as the id to retrieve the information from the images and image_categories tables.
Now that I'm also working with categories, I've found that there is overlap in the ids for images and categories. So if, for example, I'm trying to edit a category with the same id as a photo (as returned by $_GET['tab']) the plugin gets confused.
I've been perusing the source code in GitHub trying to find a function or something that will assist with this but have come up empty. Could someone please point to a way that I can determine, in admin.php of my plugin, if the item being worked on is a 'photo' or 'album'? I'm so close to having this done and then I ran into this bug while testing.
Thanks!
Last edited by windracer (2018-01-24 03:57:07)
Offline
Hi windracer,
images.id and categories.id are independant. There is a category (=album) with id=1 and an image (=photo) with id=1 too.
Can you give me an example of $_GET['tab'] where you can get confused between categories.id and images.id?
Offline
Hi Patrick.
My plugin adds a tab to the admin area of a physical photo or album. When I use $_GET['tab'] (in admin.php) on an album, I get '35' but I also have an image with an id that returns '35' to $_GET['tab'] to the plugin. I need to be able to figure out if my plugin tab is being accessed from a photo or album and I can't rely on just the id.
Offline
Show me more of your code, I'll tell you :-)
Offline
Appreciate it! :)
https://github.com/jradwan/Piwigo-physi … /admin.php
I wrote what I thought would be a good function to determine the item type based on id. In admin.php:
// retrieve the item id and type $item_id = ($_GET['image_id']); $item_type = ppm_check_item_type($item_id);
ppm_check_item_type is in include/functions.inc.php:
/ determine if an item is a photo or album function ppm_check_item_type($item_id) { $item_type = 'none'; $item_info = get_image_infos($item_id); if (!is_null($item_info)) { // this is a photo $item_type = 'photo'; } else { // this is a physical album $item_type = 'album'; } return $item_type; }
My mistake there is assuming that if get_image_infos returns no information, the item is an album. But if an album and a photo have the same id, that code mistakenly identifies the item as a photo and then my later code fails because it expects to be working on an album and not a photo. Hope I'm explaining that enough ...
Last edited by windracer (2018-01-24 18:16:26)
Offline
Any ideas? I'm going to try and poke around with this more this weekend ...
Offline
I think plg wanted the actual code to know what you want to do then
i don't see on which page you have an url with a $_GET['tab'] existing and being potentially an image or an album...
Offline
I linked to my Github repo above where the full code is.
Here's my test album with an id of 35:
https://photos.radwans.net/index.php?/category/35
(elsewhere in the gallery, there is a photo that has an id of 35 as well) When I go to my plugin's Move tab in the admin area for album 35, I get this:
Warning: [mysql error 1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3 SELECT * FROM categories WHERE id = ; in /usr/local/piwigo-beta/include/dblayer/functions_mysqli.inc.php on line 845 Fatal error: Uncaught Error: Call to a member function fetch_assoc() on boolean in /usr/local/piwigo-beta/include/dblayer/functions_mysqli.inc.php:226 Stack trace: #0 /usr/local/piwigo-beta/include/functions_category.inc.php(176): pwg_db_fetch_assoc(false) #1 /usr/local/piwigo-beta/plugins/physical_photo_move/admin.php(112): get_cat_info(NULL) #2 /usr/local/piwigo-beta/admin/plugin.php(68): include_once('/usr/local/piwi...') #3 /usr/local/piwigo-beta/admin.php(311): include('/usr/local/piwi...') #4 {main} thrown in /usr/local/piwigo-beta/include/dblayer/functions_mysqli.inc.php on line 226
So admin.php gets an image_id of 35 and passes it to my ppm_check_item_type function. ppm_check_item_type, as I have it written now (see post above), makes a call to get_image_infos with the id and if it gets a null return, knows this is an album. But since id 35 exists as a photo, it returns an item_type of 'photo' instead. And that causes code issues later on.
So I'm trying to figure out how better to write the ppm_check_item_type function to better determine if the id I am working with is a category or image.
Offline
windracer wrote:
I linked to my Github repo above where the full code is.
oops sorry
so you're adding a link depending to the source https://github.com/jradwan/Piwigo-physi … nc.php#L37 so why you're not adding a second parameter in order to add the photo/album info?
Offline
Oh, so something like this?
$sheets['ppm'] = array( 'caption' => l10n('MOVE_BUTTON'), 'url' => get_root_url().'admin.php?page=plugin-physical_photo_move-'.$_GET['image_id'], 'item' => 'photo' );
And then use that 'item' array element in my check function?
Offline
1- don't use a GET/POST or anything that the suer can manipulate, directly in an url, for safety reasons ( check XSS)
Use our function check_input_parameter() [Github] Piwigo file admin/photo.php@L35
2- why not just : 'url' => get_root_url().'admin.php?page=plugin-physical_photo_move-'.$image_id_checked'.'&ppm_type=image' There you're sending a second $_GET, the $_GET['ppm_type']
Offline
Thanks for the suggestions, I'll give 'em a shot ...
Offline
That worked, flop25, thanks for the suggestion.
In main.inc.php I set
'url' => get_root_url().'admin.php?page=plugin-physical_photo_move-'.$_GET['image_id'].'&ppm_type='.$id,
Where $id will be 'photo' or 'album'. Then in admin.php I added the parameter check:
check_input_parameter('ppm_type', $_GET, false, '/^(photo|album)$/');
And used the new parameter in my if statements directly, eliminating the need for my separate ppm_check_item_type function:
if ($_GET['ppm_type'] == 'photo') { ... } elseif ($_GET['ppm_type'] == 'album') { ... }
Seems to work! Still need to do some testing, but I think that'll do it. Thanks again.
Offline
Great news! And thanks for your plug-in! It will be quite important for users sticking with physical categories. You're a quick learner doing a very good job. I learnt like you, but creating a plug-in was easier at that time
Offline
Thanks for the kind words and your continued assistance!
I've released v2.00 of the plugin now.
Offline