| | 1571 | /** |
| | 1572 | * Function introduced for Piwigo 2.4 and the new "multiple size" |
| | 1573 | * (derivatives) feature. As we only need the biggest sent photo as |
| | 1574 | * "original", we remove chunks for smaller sizes. We can't make it earlier |
| | 1575 | * in ws_images_add_chunk because at this moment we don't know which $type |
| | 1576 | * will be the biggest (we could remove the thumb, but let's use the same |
| | 1577 | * algorithm) |
| | 1578 | */ |
| | 1579 | function remove_chunks($original_sum, $type) |
| | 1580 | { |
| | 1581 | global $conf; |
| | 1582 | |
| | 1583 | $upload_dir = $conf['upload_dir'].'/buffer'; |
| | 1584 | $pattern = '/'.$original_sum.'-'.$type.'/'; |
| | 1585 | $chunks = array(); |
| | 1586 | |
| | 1587 | if ($handle = opendir($upload_dir)) |
| | 1588 | { |
| | 1589 | while (false !== ($file = readdir($handle))) |
| | 1590 | { |
| | 1591 | if (preg_match($pattern, $file)) |
| | 1592 | { |
| | 1593 | array_push($chunks, $upload_dir.'/'.$file); |
| | 1594 | } |
| | 1595 | } |
| | 1596 | closedir($handle); |
| | 1597 | } |
| | 1598 | |
| | 1599 | foreach ($chunks as $chunk) |
| | 1600 | { |
| | 1601 | unlink($chunk); |
| | 1602 | } |
| | 1603 | } |
| | 1604 | |
| 1662 | | list($file_path, $original_sum) = pwg_db_fetch_row(pwg_query($query)); |
| 1663 | | |
| 1664 | | // TODO only files added with web API can be updated with web API |
| 1665 | | |
| 1666 | | // |
| 1667 | | // makes sure directories are there and call the merge_chunks |
| 1668 | | // |
| 1669 | | $infos = add_file($file_path, $params['type'], $original_sum, $params['sum']); |
| 1670 | | |
| 1671 | | // |
| 1672 | | // update basic metadata from file |
| 1673 | | // |
| 1674 | | $update = array(); |
| 1675 | | |
| | 1698 | $image = pwg_db_fetch_assoc(pwg_query($query)); |
| | 1699 | |
| | 1700 | if ($image == null) |
| | 1701 | { |
| | 1702 | return new PwgError(404, "image_id not found"); |
| | 1703 | } |
| | 1704 | |
| | 1705 | // since Piwigo 2.4 and derivatives, we do not take the imported "thumb" |
| | 1706 | // into account |
| | 1707 | if ('thumb' == $params['type']) |
| | 1708 | { |
| | 1709 | remove_chunks($image['md5sum'], $type); |
| | 1710 | return true; |
| | 1711 | } |
| | 1712 | |
| | 1713 | // since Piwigo 2.4 and derivatives, we only care about the "original" |
| | 1714 | $original_type = 'file'; |
| 1678 | | $update['high_filesize'] = $infos['filesize']; |
| 1679 | | $update['high_width'] = $infos['width']; |
| 1680 | | $update['high_height'] = $infos['height']; |
| 1681 | | $update['has_high'] = 'true'; |
| 1682 | | } |
| 1683 | | |
| | 1717 | $original_type = 'high'; |
| | 1718 | } |
| | 1719 | |
| | 1720 | $file_path = $conf['upload_dir'].'/buffer/'.$image['md5sum'].'-original'; |
| | 1721 | |
| | 1722 | merge_chunks($file_path, $image['md5sum'], $original_type); |
| | 1723 | chmod($file_path, 0644); |
| | 1724 | |
| | 1725 | include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); |
| | 1726 | |
| | 1727 | // if we receive the "file", we only update the original if the "file" is |
| | 1728 | // bigger than current original |
| 1686 | | $update['filesize'] = $infos['filesize']; |
| 1687 | | $update['width'] = $infos['width']; |
| 1688 | | $update['height'] = $infos['height']; |
| 1689 | | } |
| 1690 | | |
| 1691 | | // we may have nothing to update at database level, for example with a |
| 1692 | | // thumbnail update |
| 1693 | | if (count($update) > 0) |
| 1694 | | { |
| 1695 | | $update['id'] = $params['image_id']; |
| 1696 | | |
| 1697 | | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
| 1698 | | mass_updates( |
| 1699 | | IMAGES_TABLE, |
| 1700 | | array( |
| 1701 | | 'primary' => array('id'), |
| 1702 | | 'update' => array_diff(array_keys($update), array('id')) |
| 1703 | | ), |
| 1704 | | array($update) |
| 1705 | | ); |
| 1706 | | } |
| | 1731 | $do_update = false; |
| | 1732 | |
| | 1733 | $infos = pwg_image_infos($file_path); |
| | 1734 | |
| | 1735 | foreach (array('width', 'height', 'filesize') as $image_info) |
| | 1736 | { |
| | 1737 | if ($infos[$image_info] > $image[$image_info]) |
| | 1738 | { |
| | 1739 | $do_update = true; |
| | 1740 | } |
| | 1741 | } |
| | 1742 | |
| | 1743 | if (!$do_update) |
| | 1744 | { |
| | 1745 | unlink($file_path); |
| | 1746 | return true; |
| | 1747 | } |
| | 1748 | } |
| | 1749 | |
| | 1750 | $image_id = add_uploaded_file( |
| | 1751 | $file_path, |
| | 1752 | $image['file'], |
| | 1753 | null, |
| | 1754 | null, |
| | 1755 | $params['image_id'], |
| | 1756 | $image['md5sum'] // we force the md5sum to remain the same |
| | 1757 | ); |
| | 1758 | |
| | 1759 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
| | 1760 | delete_element_derivatives($params['image_id']); |
| 1751 | | if ($params['resize']) |
| 1752 | | { |
| 1753 | | ws_logfile('[pwg.images.add] resize activated'); |
| 1754 | | |
| 1755 | | // temporary file path |
| 1756 | | $type = 'file'; |
| 1757 | | $file_path = $conf['upload_dir'].'/buffer/'.$params['original_sum'].'-'.$type; |
| 1758 | | |
| 1759 | | merge_chunks($file_path, $params['original_sum'], $type); |
| 1760 | | chmod($file_path, 0644); |
| 1761 | | |
| 1762 | | include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); |
| 1763 | | |
| 1764 | | $image_id = add_uploaded_file( |
| 1765 | | $file_path, |
| 1766 | | $params['original_filename'] |
| 1767 | | ); |
| 1768 | | |
| 1769 | | // add_uploaded_file doesn't remove the original file in the buffer |
| 1770 | | // directory if it was not uploaded as $_FILES |
| 1771 | | unlink($file_path); |
| | 1805 | // due to the new feature "derivatives" (multiple sizes) introduced for |
| | 1806 | // Piwigo 2.4, we only take the biggest photos sent on |
| | 1807 | // pwg.images.addChunk. If "high" is available we use it as "original" |
| | 1808 | // else we use "file". |
| | 1809 | remove_chunks($params['original_sum'], 'thumb'); |
| | 1810 | |
| | 1811 | if (isset($params['high_sum'])) |
| | 1812 | { |
| | 1813 | $original_type = 'high'; |
| | 1814 | remove_chunks($params['original_sum'], 'file'); |
| 1775 | | // current date |
| 1776 | | list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();')); |
| 1777 | | list($year, $month, $day) = preg_split('/[^\d]/', $dbnow, 4); |
| 1778 | | |
| 1779 | | // upload directory hierarchy |
| 1780 | | $upload_dir = sprintf( |
| 1781 | | $conf['upload_dir'].'/%s/%s/%s', |
| 1782 | | $year, |
| 1783 | | $month, |
| 1784 | | $day |
| 1785 | | ); |
| 1786 | | |
| 1787 | | // compute file path |
| 1788 | | $date_string = preg_replace('/[^\d]/', '', $dbnow); |
| 1789 | | $random_string = substr($params['file_sum'], 0, 8); |
| 1790 | | $filename_wo_ext = $date_string.'-'.$random_string; |
| 1791 | | $file_path = $upload_dir.'/'.$filename_wo_ext.'.jpg'; |
| 1792 | | |
| 1793 | | // add files |
| 1794 | | $file_infos = add_file($file_path, 'file', $params['original_sum'], $params['file_sum']); |
| 1795 | | $thumb_infos = add_file($file_path, 'thumb', $params['original_sum'], $params['thumbnail_sum']); |
| 1796 | | |
| 1797 | | if (isset($params['high_sum'])) |
| 1798 | | { |
| 1799 | | $high_infos = add_file($file_path, 'high', $params['original_sum'], $params['high_sum']); |
| 1800 | | } |
| 1801 | | |
| 1802 | | // database registration |
| 1803 | | $insert = array( |
| 1804 | | 'file' => !empty($params['original_filename']) ? $params['original_filename'] : $filename_wo_ext.'.jpg', |
| 1805 | | 'date_available' => $dbnow, |
| 1806 | | 'tn_ext' => 'jpg', |
| 1807 | | 'name' => $params['name'], |
| 1808 | | 'path' => $file_path, |
| 1809 | | 'filesize' => $file_infos['filesize'], |
| 1810 | | 'width' => $file_infos['width'], |
| 1811 | | 'height' => $file_infos['height'], |
| 1812 | | 'md5sum' => $params['original_sum'], |
| 1813 | | 'added_by' => $user['id'], |
| 1814 | | ); |
| 1815 | | |
| 1816 | | if (isset($params['high_sum'])) |
| 1817 | | { |
| 1818 | | $insert['has_high'] = 'true'; |
| 1819 | | $insert['high_filesize'] = $high_infos['filesize']; |
| 1820 | | $insert['high_width'] = $high_infos['width']; |
| 1821 | | $insert['high_height'] = $high_infos['height']; |
| 1822 | | } |
| 1823 | | |
| 1824 | | single_insert( |
| 1825 | | IMAGES_TABLE, |
| 1826 | | $insert |
| 1827 | | ); |
| 1828 | | |
| 1829 | | $image_id = pwg_db_insert_id(IMAGES_TABLE); |
| 1830 | | |
| 1831 | | // update metadata from the uploaded file (exif/iptc) |
| 1832 | | require_once(PHPWG_ROOT_PATH.'admin/include/functions_metadata.php'); |
| 1833 | | sync_metadata(array($image_id)); |
| 1834 | | } |
| | 1818 | $original_type = 'file'; |
| | 1819 | } |
| | 1820 | |
| | 1821 | $file_path = $conf['upload_dir'].'/buffer/'.$params['original_sum'].'-original'; |
| | 1822 | |
| | 1823 | merge_chunks($file_path, $params['original_sum'], $original_type); |
| | 1824 | chmod($file_path, 0644); |
| | 1825 | |
| | 1826 | include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); |
| | 1827 | |
| | 1828 | $image_id = add_uploaded_file( |
| | 1829 | $file_path, |
| | 1830 | $params['original_filename'], |
| | 1831 | null, // categories |
| | 1832 | isset($params['level']) ? $params['level'] : null, |
| | 1833 | null, // image_id |
| | 1834 | $params['original_sum'] |
| | 1835 | ); |
| 2451 | | foreach (array('thumb', 'file', 'high') as $type) { |
| 2452 | | $param_name = $type; |
| 2453 | | if ('thumb' == $type) { |
| 2454 | | $param_name = 'thumbnail'; |
| 2455 | | } |
| 2456 | | |
| 2457 | | if (isset($params[$param_name.'_sum'])) { |
| 2458 | | include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); |
| 2459 | | $type_path = file_path_for_type($path, $type); |
| 2460 | | if (!is_file($type_path)) { |
| 2461 | | $ret[$param_name] = 'missing'; |
| 2462 | | } |
| 2463 | | else { |
| 2464 | | if (md5_file($type_path) != $params[$param_name.'_sum']) { |
| 2465 | | $ret[$param_name] = 'differs'; |
| 2466 | | } |
| 2467 | | else { |
| 2468 | | $ret[$param_name] = 'equals'; |
| 2469 | | } |
| 2470 | | } |
| 2471 | | } |
| 2472 | | } |
| | 2458 | if (isset($params['thumbnail_sum'])) |
| | 2459 | { |
| | 2460 | // We always say the thumbnail is equal to create no reaction on the |
| | 2461 | // other side. Since Piwigo 2.4 and derivatives, the thumbnails and web |
| | 2462 | // sizes are always generated by Piwigo |
| | 2463 | $ret['thumbnail'] = 'equals'; |
| | 2464 | } |
| | 2465 | |
| | 2466 | if (isset($params['high_sum'])) |
| | 2467 | { |
| | 2468 | $ret['file'] = 'equals'; |
| | 2469 | $compare_type = 'high'; |
| | 2470 | } |
| | 2471 | elseif (isset($params['file_sum'])) |
| | 2472 | { |
| | 2473 | $compare_type = 'file'; |
| | 2474 | } |
| | 2475 | |
| | 2476 | if (isset($compare_type)) |
| | 2477 | { |
| | 2478 | ws_logfile(__FUNCTION__.', md5_file($path) = '.md5_file($path)); |
| | 2479 | if (md5_file($path) != $params[$compare_type.'_sum']) |
| | 2480 | { |
| | 2481 | $ret[$compare_type] = 'differs'; |
| | 2482 | } |
| | 2483 | else |
| | 2484 | { |
| | 2485 | $ret[$compare_type] = 'equals'; |
| | 2486 | } |
| | 2487 | } |
| | 2488 | |
| | 2489 | ws_logfile(__FUNCTION__.', output : '.var_export($ret, true)); |