Merge "Fixes to the "reset password" mode of Special:ChangeEmail"
[lhc/web/wiklou.git] / includes / media / IPTC.php
index 3db845b..3ae59ad 100644 (file)
@@ -1,8 +1,31 @@
 <?php
 /**
-*Class for some IPTC functions.
+ * Class for some IPTC functions.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup Media
+ */
 
-*/
+/**
+ * Class for some IPTC functions.
+ *
+ * @ingroup Media
+ */
 class IPTC {
 
        /**
@@ -12,21 +35,21 @@ class IPTC {
        *
        * @see http://www.iptc.org/std/IIM/4.1/specification/IIMV4.1.pdf
        *
-       * @param String $data app13 block from jpeg containing iptc/iim data
+       * @param $rawData String app13 block from jpeg containing iptc/iim data
        * @return Array iptc metadata array
        */
        static function parse( $rawData ) {
                $parsed = iptcparse( $rawData );
                $data = Array();
-               if (!is_array($parsed)) {
+               if ( !is_array( $parsed ) ) {
                                return $data;
                }
 
                $c = '';
                //charset info contained in tag 1:90.
-               if (isset($parsed['1#090']) && isset($parsed['1#090'][0])) {
-                       $c = self::getCharset($parsed['1#090'][0]);
-                       if ($c === false) {
+               if ( isset( $parsed['1#090'] ) && isset( $parsed['1#090'][0] ) ) {
+                       $c = self::getCharset( $parsed['1#090'][0] );
+                       if ( $c === false ) {
                                //Unknown charset. refuse to parse.
                                //note: There is a different between
                                //unknown and no charset specified.
@@ -36,8 +59,8 @@ class IPTC {
                }
 
                foreach ( $parsed as $tag => $val ) {
-                       if ( isset( $val[0] ) && trim($val[0]) == '' ) {
-                               wfDebugLog('iptc', "IPTC tag $tag had only whitespace as its value.");
+                       if ( isset( $val[0] ) && trim( $val[0] ) == '' ) {
+                               wfDebugLog( 'iptc', "IPTC tag $tag had only whitespace as its value." );
                                continue;
                        }
                        switch( $tag ) {
@@ -102,7 +125,7 @@ class IPTC {
                                        break;
                                case '2#115': /* source */
                                        /* "Identifies the original owner of the intellectual content of the
-                                        *objectdata. This could be an agency, a member of an agency or 
+                                        *objectdata. This could be an agency, a member of an agency or
                                         *an individual." */
                                        $data['Source'] = self::convIPTC( $val, $c );
                                        break;
@@ -152,13 +175,11 @@ class IPTC {
                                        if ( isset( $parsed['2#070'] ) ) {
                                                //if a version is set for the software.
                                                $softwareVersion = self::convIPTC( $parsed['2#070'], $c );
-                                               unset($parsed['2#070']);
+                                               unset( $parsed['2#070'] );
                                                $data['Software'] = array( array( $software[0], $softwareVersion[0] ) );
                                        } else {
                                                $data['Software'] = $software;
                                        }
-
-
                                        break;
                                case '2#075':
                                        /* Object cycle.
@@ -206,8 +227,8 @@ class IPTC {
                                        } else {
                                                $time = Array();
                                        }
-                                       $timestamp =  self::timeHelper( $val, $time, $c );
-                                       if ($timestamp) {
+                                       $timestamp = self::timeHelper( $val, $time, $c );
+                                       if ( $timestamp ) {
                                                $data['DateTimeOriginal'] = $timestamp;
                                        }
                                        break;
@@ -220,8 +241,8 @@ class IPTC {
                                        } else {
                                                $time = Array();
                                        }
-                                       $timestamp =  self::timeHelper( $val, $time, $c );
-                                       if ($timestamp) {
+                                       $timestamp = self::timeHelper( $val, $time, $c );
+                                       if ( $timestamp ) {
                                                $data['DateTimeDigitized'] = $timestamp;
                                        }
                                        break;
@@ -233,8 +254,8 @@ class IPTC {
                                        } else {
                                                $time = Array();
                                        }
-                                       $timestamp =  self::timeHelper( $val, $time, $c );
-                                       if ($timestamp) {
+                                       $timestamp = self::timeHelper( $val, $time, $c );
+                                       if ( $timestamp ) {
                                                $data['DateTimeReleased'] = $timestamp;
                                        }
                                        break;
@@ -246,8 +267,8 @@ class IPTC {
                                        } else {
                                                $time = Array();
                                        }
-                                       $timestamp =  self::timeHelper( $val, $time, $c );
-                                       if ($timestamp) {
+                                       $timestamp = self::timeHelper( $val, $time, $c );
+                                       if ( $timestamp ) {
                                                $data['DateTimeExpires'] = $timestamp;
                                        }
                                        break;
@@ -292,7 +313,7 @@ class IPTC {
                                        // describing the subject matter of the content.
                                        $codes = self::convIPTC( $val, $c );
                                        foreach ( $codes as $ic ) {
-                                               $fields = explode(':', $ic, 3 );
+                                               $fields = explode( ':', $ic, 3 );
 
                                                if ( count( $fields ) < 2 ||
                                                        $fields[0] !== 'IPTC' )
@@ -331,34 +352,35 @@ class IPTC {
        /**
        * Convert an iptc date and time tags into the exif format
        *
-       * @todo Potentially this should also capture the timezone offset. 
+       * @todo Potentially this should also capture the timezone offset.
        * @param Array $date The date tag
        * @param Array $time The time tag
+       * @param $c
        * @return String Date in exif format.
        */
        private static function timeHelper( $date, $time, $c ) {
                if ( count( $date ) === 1 ) {
                        //the standard says this should always be 1
                        //just double checking.
-                       list($date) = self::convIPTC( $date, $c );
+                       list( $date ) = self::convIPTC( $date, $c );
                } else {
                        return null;
                }
 
                if ( count( $time ) === 1 ) {
-                       list($time) = self::convIPTC( $time, $c );
+                       list( $time ) = self::convIPTC( $time, $c );
                        $dateOnly = false;
                } else {
                        $time = '000000+0000'; //placeholder
                        $dateOnly = true;
                }
 
-               if ( ! ( preg_match('/\d\d\d\d\d\d[-+]\d\d\d\d/', $time) 
+               if ( !( preg_match('/\d\d\d\d\d\d[-+]\d\d\d\d/', $time)
                        && preg_match('/\d\d\d\d\d\d\d\d/', $date)
-                       && substr($date, 0, 4) !== '0000'
-                       && substr($date, 4, 2) !== '00'
-                       && substr($date, 6, 2) !== '00'
-                ) ) {
+                       && substr( $date, 0, 4 ) !== '0000'
+                       && substr( $date, 4, 2 ) !== '00'
+                       && substr( $date, 6, 2 ) !== '00'
+               ) ) {
                        //something wrong.
                        // Note, this rejects some valid dates according to iptc spec
                        // for example: the date 00000400 means the photo was taken in
@@ -392,22 +414,22 @@ class IPTC {
                } else {
                        return $finalTimestamp;
                }
-
        }
 
        /**
        * Helper function to convert charset for iptc values.
-       * @param $data Mixed String or Array: The iptc string
+       * @param $data string|array The iptc string
        * @param $charset String: The charset
+        *
+        * @return string|array
        */
        private static function convIPTC ( $data, $charset ) {
-               global $wgLang;
                if ( is_array( $data ) ) {
-                       foreach ($data as &$val) {
+                       foreach ( $data as &$val ) {
                                $val = self::convIPTCHelper( $val, $charset );
                        }
                } else {
-                       $data = self::convIPTCHelper ( $data, $charset );
+                       $data = self::convIPTCHelper( $data, $charset );
                }
 
                return $data;
@@ -416,34 +438,41 @@ class IPTC {
        * Helper function of a helper function to convert charset for iptc values.
        * @param $data Mixed String or Array: The iptc string
        * @param $charset String: The charset
+       *
+       * @return string
        */
        private static function convIPTCHelper ( $data, $charset ) {
                if ( $charset ) {
-                       $data = iconv($charset, "UTF-8//IGNORE", $data);
-                       if ($data === false) {
+                       wfSuppressWarnings();
+                       $data = iconv( $charset, "UTF-8//IGNORE", $data );
+                       wfRestoreWarnings();
+                       if ( $data === false ) {
                                $data = "";
-                               wfDebugLog('iptc', __METHOD__ . " Error converting iptc data charset $charset to utf-8");
+                               wfDebugLog( 'iptc', __METHOD__ . " Error converting iptc data charset $charset to utf-8" );
                        }
                } else {
                        //treat as utf-8 if is valid utf-8. otherwise pretend its windows-1252
                        // most of the time if there is no 1:90 tag, it is either ascii, latin1, or utf-8
                        $oldData = $data;
                        UtfNormal::quickIsNFCVerify( $data ); //make $data valid utf-8
-                       if ($data === $oldData) return $data; //if validation didn't change $data
-                       else return self::convIPTCHelper ( $oldData, 'Windows-1252' );
+                       if ( $data === $oldData ) {
+                               return $data; //if validation didn't change $data
+                       } else {
+                               return self::convIPTCHelper( $oldData, 'Windows-1252' );
+                       }
                }
                return trim( $data );
        }
 
        /**
        * take the value of 1:90 tag and returns a charset
-       * @param String $tag 1:90 tag. 
-       * @return charset name or "?"
+       * @param String $tag 1:90 tag.
+       * @return string charset name or "?"
        * Warning, this function does not (and is not intended to) detect
        * all iso 2022 escape codes. In practise, the code for utf-8 is the
        * only code that seems to have wide use. It does detect that code.
        */
-       static function getCharset($tag) {
+       static function getCharset( $tag ) {
 
                //According to iim standard, charset is defined by the tag 1:90.
                //in which there are iso 2022 escape sequences to specify the character set.
@@ -508,7 +537,7 @@ class IPTC {
                                $c = "NS_4551-1";
                                break;
                        case "\x1b(f": //iso646-FR
-                               $c = "NF_Z_62-010"; 
+                               $c = "NF_Z_62-010";
                                break;
                        case "\x1b(g":
                                $c = "PT2"; //iso646-PT2
@@ -561,7 +590,7 @@ class IPTC {
                                $c = 'CSN_369103';
                                break;
                        default:
-                               wfDebugLog('iptc', __METHOD__ . 'Unknown charset in iptc 1:90: ' . bin2hex( $tag ) ); 
+                               wfDebugLog( 'iptc', __METHOD__ . 'Unknown charset in iptc 1:90: ' . bin2hex( $tag ) );
                                //at this point just give up and refuse to parse iptc?
                                $c = false;
                }