| 831 | florian | 1 | <?php
 | 
        
           |  |  | 2 |   | 
        
           |  |  | 3 | ////////////////////////////////////////////////////////////////////////////////
 | 
        
           |  |  | 4 | //                                                                            //
 | 
        
           |  |  | 5 | //   Copyright (C) 2006  Phorum Development Team                              //
 | 
        
           |  |  | 6 | //   http://www.phorum.org                                                    //
 | 
        
           |  |  | 7 | //                                                                            //
 | 
        
           |  |  | 8 | //   This program is free software. You can redistribute it and/or modify     //
 | 
        
           |  |  | 9 | //   it under the terms of either the current Phorum License (viewable at     //
 | 
        
           |  |  | 10 | //   phorum.org) or the Phorum License that was distributed with this file    //
 | 
        
           |  |  | 11 | //                                                                            //
 | 
        
           |  |  | 12 | //   This program is distributed in the hope that it will be useful,          //
 | 
        
           |  |  | 13 | //   but WITHOUT ANY WARRANTY, without even the implied warranty of           //
 | 
        
           |  |  | 14 | //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     //
 | 
        
           |  |  | 15 | //                                                                            //
 | 
        
           |  |  | 16 | //   You should have received a copy of the Phorum License                    //
 | 
        
           |  |  | 17 | //   along with this program.                                                 //
 | 
        
           |  |  | 18 | ////////////////////////////////////////////////////////////////////////////////
 | 
        
           |  |  | 19 |   | 
        
           |  |  | 20 |     // Check if the Phorum file uploading settings match the
 | 
        
           |  |  | 21 |     // limits that are imposed by the system.
 | 
        
           |  |  | 22 |   | 
        
           |  |  | 23 |     // TODO (document in faq / documentation)
 | 
        
           |  |  | 24 |     // The upload size can be limited by Apache's LimitRequestBody directive,
 | 
        
           |  |  | 25 |     // but we cannot check that one from PHP.
 | 
        
           |  |  | 26 |   | 
        
           |  |  | 27 |     require_once('./include/format_functions.php'); // For phorum_filesize()
 | 
        
           |  |  | 28 |   | 
        
           |  |  | 29 |     $phorum_check = "File uploading (personal files and attachments)";
 | 
        
           |  |  | 30 |   | 
        
           |  |  | 31 |     function phorum_check_upload_limits() {
 | 
        
           |  |  | 32 |         $PHORUM = $GLOBALS["PHORUM"];
 | 
        
           |  |  | 33 |   | 
        
           |  |  | 34 |         // Keep track if uploads are used.
 | 
        
           |  |  | 35 |         $upload_used = false;
 | 
        
           |  |  | 36 |   | 
        
           |  |  | 37 |         // Get the maximum file upload size for PHP.
 | 
        
           |  |  | 38 |         $php_max_upload = phorum_php_max_upload();
 | 
        
           |  |  | 39 |   | 
        
           |  |  | 40 |         // Get the maximum packet size for the database.
 | 
        
           |  |  | 41 |         // For determining the maximum allowed upload size,
 | 
        
           |  |  | 42 |         // we have to take packet overhead into account.
 | 
        
           |  |  | 43 |         $max_packetsize = phorum_db_maxpacketsize();
 | 
        
           |  |  | 44 |         if ($max_packetsize == NULL) {
 | 
        
           |  |  | 45 |             $db_max_upload = $php_max_upload;
 | 
        
           |  |  | 46 |         } else {
 | 
        
           |  |  | 47 |             $db_max_upload = phorum_db_maxpacketsize() * 0.6;
 | 
        
           |  |  | 48 |         }
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 50 |         // Check limits for file uploading in personal profile.
 | 
        
           |  |  | 51 |         if ($PHORUM["file_uploads"] && $PHORUM["max_file_size"]) {
 | 
        
           |  |  | 52 |             $upload_used = true;
 | 
        
           |  |  | 53 |             $res = phorum_single_check_upload_limits(
 | 
        
           |  |  | 54 |                 $PHORUM["max_file_size"]*1024,
 | 
        
           |  |  | 55 |                 "the Max File Size option for user file uploads " .
 | 
        
           |  |  | 56 |                 "(in their profile)",
 | 
        
           |  |  | 57 |                 $php_max_upload, $db_max_upload
 | 
        
           |  |  | 58 |             );
 | 
        
           |  |  | 59 |             if ($res != NULL) return $res;
 | 
        
           |  |  | 60 |         }
 | 
        
           |  |  | 61 |   | 
        
           |  |  | 62 |         // Check limits for attachment uploading in forums.
 | 
        
           |  |  | 63 |         $forums = phorum_db_get_forums();
 | 
        
           |  |  | 64 |         foreach ($forums as $id => $forum) {
 | 
        
           |  |  | 65 |             if ($forum["max_attachments"] > 0 && $forum["max_attachment_size"]) {
 | 
        
           |  |  | 66 |                 $upload_used = true;
 | 
        
           |  |  | 67 |                 $res = phorum_single_check_upload_limits(
 | 
        
           |  |  | 68 |                     $forum["max_attachment_size"]*1024,
 | 
        
           |  |  | 69 |                     "the Max File Size option for uploading attachments
 | 
        
           |  |  | 70 |                      in the forum \"{$forum['name']}\"",
 | 
        
           |  |  | 71 |                     $php_max_upload, $db_max_upload
 | 
        
           |  |  | 72 |                 );
 | 
        
           |  |  | 73 |             }
 | 
        
           |  |  | 74 |         }
 | 
        
           |  |  | 75 |   | 
        
           |  |  | 76 |         // No upload functionality found so far? Then we're done.
 | 
        
           |  |  | 77 |         if (! $upload_used) return array(PHORUM_SANITY_OK, NULL);
 | 
        
           |  |  | 78 |   | 
        
           |  |  | 79 |         // Check if the upload temp directory can be written.
 | 
        
           |  |  | 80 |         $tmpdir = get_cfg_var('upload_tmp_dir');
 | 
        
           |  |  | 81 |         if (!empty($tmpdir)) {
 | 
        
           |  |  | 82 |             $fp = @fopen("$tmpdir/sanity_checks_dummy_uploadtmpfile", "w");
 | 
        
           |  |  | 83 |             if (! $fp) return array(
 | 
        
           |  |  | 84 |                 PHORUM_SANITY_CRIT,
 | 
        
           |  |  | 85 |                 "The system is unable to write files
 | 
        
           |  |  | 86 |                  to PHP's upload tmpdir \"".htmlspecialchars($tmpdir)."\".
 | 
        
           |  |  | 87 |                  The system error was:<br/><br/>".
 | 
        
           |  |  | 88 |                  htmlspecialchars($php_errormsg).".",
 | 
        
           |  |  | 89 |                 "Change the upload_tmp_dir setting in your php.ini file
 | 
        
           |  |  | 90 |                  or give your webserver more permissions for the current
 | 
        
           |  |  | 91 |                  upload directory."
 | 
        
           |  |  | 92 |             );
 | 
        
           |  |  | 93 |         }
 | 
        
           |  |  | 94 |         fclose($fp);
 | 
        
           |  |  | 95 |         unlink("$tmpdir/sanity_checks_dummy_uploadtmpfile");
 | 
        
           |  |  | 96 |   | 
        
           |  |  | 97 |         return array(PHORUM_SANITY_OK, NULL);
 | 
        
           |  |  | 98 |     }
 | 
        
           |  |  | 99 |   | 
        
           |  |  | 100 |     // ========================================================================
 | 
        
           |  |  | 101 |     // Helper functions
 | 
        
           |  |  | 102 |     // ========================================================================
 | 
        
           |  |  | 103 |   | 
        
           |  |  | 104 |     // We have to check multiple upload limits. Using this function,
 | 
        
           |  |  | 105 |     // we do not have to rebuild all error messages over and over
 | 
        
           |  |  | 106 |     // again.
 | 
        
           |  |  | 107 |     function phorum_single_check_upload_limits ($howmuch, $what, $maxphp, $maxdb)
 | 
        
           |  |  | 108 |     {
 | 
        
           |  |  | 109 |         // Check PHP limits.
 | 
        
           |  |  | 110 |         if (!empty($maxphp) && $howmuch > $maxphp) return array(
 | 
        
           |  |  | 111 |             PHORUM_SANITY_WARN,
 | 
        
           |  |  | 112 |             "You have configured ".htmlspecialchars($what)." to ".
 | 
        
           |  |  | 113 |              phorum_filesize($howmuch).". Your PHP installation only
 | 
        
           |  |  | 114 |              supports ".phorum_filesize($maxphp).". Your users might
 | 
        
           |  |  | 115 |              have problems with uploading their files because of this.",
 | 
        
           |  |  | 116 |             "Raise the options post_max_size and upload_max_filesize in your
 | 
        
           |  |  | 117 |              php.ini file to match the Max File Size option or lower this
 | 
        
           |  |  | 118 |              configuration option for your forums."
 | 
        
           |  |  | 119 |         );
 | 
        
           |  |  | 120 |   | 
        
           |  |  | 121 |         // Check database limits.
 | 
        
           |  |  | 122 |         if (!empty($maxdb) && $howmuch > $maxdb) return array(
 | 
        
           |  |  | 123 |             PHORUM_SANITY_WARN,
 | 
        
           |  |  | 124 |             "You have configured ".htmlspecialchars($what)." to ".
 | 
        
           |  |  | 125 |              phorum_filesize($howmuch).". Your database only supports ".
 | 
        
           |  |  | 126 |              phorum_filesize($maxdb).". Your users might have problems with
 | 
        
           |  |  | 127 |              uploading their files because of this.",
 | 
        
           |  |  | 128 |             "Configure your database to allow larger packets or lower the
 | 
        
           |  |  | 129 |              Max File Size configuration option for your forums."
 | 
        
           |  |  | 130 |         );
 | 
        
           |  |  | 131 |   | 
        
           |  |  | 132 |         return NULL;
 | 
        
           |  |  | 133 |     }
 | 
        
           |  |  | 134 |   | 
        
           |  |  | 135 |     function phorum_php_max_upload()
 | 
        
           |  |  | 136 |     {
 | 
        
           |  |  | 137 |         // Determine the PHP system upload limit. The limit for
 | 
        
           |  |  | 138 |         // maximum upload filesize is not the only thing we
 | 
        
           |  |  | 139 |         // have to look at. We should also take the maximum
 | 
        
           |  |  | 140 |         // POST size in account.
 | 
        
           |  |  | 141 |         $pms = phorum_phpcfgsize2bytes(get_cfg_var('post_max_size'));
 | 
        
           |  |  | 142 |         $umf = phorum_phpcfgsize2bytes(get_cfg_var('upload_max_filesize'));
 | 
        
           |  |  | 143 |         $limit = ($umf > $pms ? $pms : $umf);
 | 
        
           |  |  | 144 |   | 
        
           |  |  | 145 |         return $limit;
 | 
        
           |  |  | 146 |     }
 | 
        
           |  |  | 147 |   | 
        
           |  |  | 148 |     // Convert the size parameters that can be used in the
 | 
        
           |  |  | 149 |     // PHP ini-file (e.g. 1024, 10k, 8M) to a number of bytes.
 | 
        
           |  |  | 150 |     function phorum_phpcfgsize2bytes($val) {
 | 
        
           |  |  | 151 |         $val = trim($val);
 | 
        
           |  |  | 152 |         $last = strtolower($val{strlen($val)-1});
 | 
        
           |  |  | 153 |         switch($last) {
 | 
        
           |  |  | 154 |            // The 'G' modifier is available since PHP 5.1.0
 | 
        
           |  |  | 155 |            case 'g':
 | 
        
           |  |  | 156 |                $val *= 1024;
 | 
        
           |  |  | 157 |            case 'm':
 | 
        
           |  |  | 158 |                $val *= 1024;
 | 
        
           |  |  | 159 |            case 'k':
 | 
        
           |  |  | 160 |                $val *= 1024;
 | 
        
           |  |  | 161 |         }
 | 
        
           |  |  | 162 |         return $val;
 | 
        
           |  |  | 163 |     }
 | 
        
           |  |  | 164 | ?>
 |