Adds check if delivered package matches required package
[GitHub/WoltLab/WCF.git] / wcfsetup / install.php
index 8e27f505f163923a9b319a928e8852b59af89800..abc233dfcaa05c6b1ba57730fca596a283ced31f 100644 (file)
@@ -3,10 +3,11 @@
  * This script tries to find the temp folder and unzip all setup files into.
  *
  * @author     Marcel Werk
- * @copyright  2001-2011 WoltLab GmbH
+ * @copyright  2001-2013 WoltLab GmbH
  * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  */
 // define constants
+define('INSTALL_SCRIPT', __FILE__);
 define('INSTALL_SCRIPT_DIR', dirname(__FILE__).'/');
 define('SETUP_FILE', INSTALL_SCRIPT_DIR . 'WCFSetup.tar.gz');
 define('NO_IMPORTS', 1);
@@ -33,7 +34,7 @@ $neededFilesPattern = array(
 /**
  * WCF::handleException() calls the show method on exceptions that implement this interface.
  *
- * @package    com.woltlab.wcf.system.exception
+ * @package    com.woltlab.wcf
  * @author     Marcel Werk
  */
 interface IPrintableException {
@@ -46,7 +47,7 @@ interface IPrintableException {
 /**
  * A SystemException is thrown when an unexpected error occurs.
  *
- * @package    com.woltlab.wcf.system.exception
+ * @package    com.woltlab.wcf
  * @author     Marcel Werk
  */
 class SystemException extends \Exception implements IPrintableException {
@@ -217,10 +218,16 @@ function handleError($errorNo, $message, $filename, $lineNo) {
 /**
  * BasicFileUtil contains file-related functions.
  *
- * @package    com.woltlab.wcf.util
+ * @package    com.woltlab.wcf
  * @author     Marcel Werk
  */
 class BasicFileUtil {
+       /**
+        * chmod mode
+        * @var integer
+        */
+       protected static $mode = null;
+       
        /**
         * Tries to find the temp folder.
         *
@@ -265,7 +272,7 @@ class BasicFileUtil {
                        return $path . '/';
                }
        
-               $path = WCF_DIR.'tmp/';
+               $path = INSTALL_SCRIPT_DIR.'tmp/';
                if (@file_exists($path) && @is_writable($path)) {
                        return $path;
                }
@@ -289,33 +296,61 @@ class BasicFileUtil {
        
        /**
         * Tries to make a file or directory writable. It starts of with the least
-        * permissions and goes up until 0777.
+        * permissions and goes up until 0666 for files and 0777 for directories.
         *
         * @param       string          $filename
         */
        public static function makeWritable($filename) {
-               if (is_writable($filename)) {
+               if (!file_exists($filename)) {
                        return;
                }
                
-               $chmods = array('0644', '0755', '0775', '0777');
+               // determine mode
+               if (self::$mode === null) {
+                       // do not use PHP_OS here, as this represents the system it was built on != running on
+                       if (strpos(php_uname(), 'Windows') !== false) {
+                               self::$mode = 0777;
+                       }
+                       else {
+                               clearstatcache();
+                               
+                               self::$mode = 0666;
+                               
+                               $tmpFilename = '__permissions_'.sha1(time()).'.txt';
+                               @touch($tmpFilename);
+                               
+                               // create a new file and check the file owner, if it is the same
+                               // as this file (uploaded through FTP), we can safely grant write
+                               // permissions exclusively to the owner rather than everyone
+                               if (file_exists($tmpFilename)) {
+                                       $scriptOwner = fileowner(__FILE__);
+                                       $fileOwner = fileowner($tmpFilename);
+                                       
+                                       if ($scriptOwner === $fileOwner) {
+                                               self::$mode = 0644;
+                                       }
+                                       
+                                       @unlink($tmpFilename);
+                               }
+                       }
+               }
                
                $startIndex = 0;
                if (is_dir($filename)) {
-                       $startIndex = 1;
-               }
-               
-               for ($i = $startIndex; $i < 4; $i++) {
-                       @chmod($filename, octdec($chmods[$i]));
-                       
-                       if (is_writable($filename)) {
-                               break;
+                       if (self::$mode == 0644) {
+                               @chmod($filename, 0755);
                        }
-                       else if ($i == 3) {
-                               // does not work with 0777
-                               throw new SystemException("Unable to make '".$filename."' writable. This is a misconfiguration of your server, please contact your system administrator or hosting provider.");
+                       else {
+                               @chmod($filename, 0777);
                        }
                }
+               else {
+                       @chmod($filename, self::$mode);
+               }
+               
+               if (!is_writable($filename)) {
+                       throw new SystemException("Unable to make '".$filename."' writable. This is a misconfiguration of your server, please contact your system administrator or hosting provider.");
+               }
        }
 }
 
@@ -582,7 +617,14 @@ class Tar {
 
                // Extract the values
                //$data = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor", $binaryData);
-               $data = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor/a155prefix", $binaryData);
+               if (version_compare(PHP_VERSION, '5.5.0-dev', '>=')) {
+                       $format = 'Z100filename/Z8mode/Z8uid/Z8gid/Z12size/Z12mtime/Z8checksum/Z1typeflag/Z100link/Z6magic/Z2version/Z32uname/Z32gname/Z8devmajor/Z8devminor/Z155prefix';
+               }
+               else {
+                       $format = 'a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor/a155prefix';
+               }
+               
+               $data = unpack($format, $binaryData);
                
                // Extract the properties
                $header['checksum'] = octDec(trim($data['checksum']));