includes/functions.php
changeset 667 72818d2bf336
parent 662 fcab604da9a7
child 685 17ebe24cdf85
equal deleted inserted replaced
666:402adf8ea719 667:72818d2bf336
    74  * @param string The GET query string to append
    74  * @param string The GET query string to append
    75  * @param bool   If true, perform htmlspecialchars() on the return value to make it HTML-safe
    75  * @param bool   If true, perform htmlspecialchars() on the return value to make it HTML-safe
    76  * @return string
    76  * @return string
    77  */
    77  */
    78 
    78 
    79 function makeUrl($t, $query = false, $escape = false)
    79 if ( !function_exists('makeUrl') )
    80 {
    80 {
    81   global $db, $session, $paths, $template, $plugins; // Common objects
    81   function makeUrl($t, $query = false, $escape = false)
    82   $flags = '';
    82   {
    83   $sep = urlSeparator;
    83     global $db, $session, $paths, $template, $plugins; // Common objects
    84   $t = sanitize_page_id($t);
    84     $flags = '';
    85   if ( isset($_GET['printable'] ) )
    85     $sep = urlSeparator;
    86   {
    86     $t = sanitize_page_id($t);
    87     $flags .= $sep . 'printable=yes';
    87     if ( isset($_GET['printable'] ) )
    88     $sep = '&';
    88     {
    89   }
    89       $flags .= $sep . 'printable=yes';
    90   if ( isset($_GET['theme'] ) )
    90       $sep = '&';
    91   {
    91     }
    92     $flags .= $sep . 'theme='.$session->theme;
    92     if ( isset($_GET['theme'] ) )
    93     $sep = '&';
    93     {
    94   }
    94       $flags .= $sep . 'theme='.$session->theme;
    95   if ( isset($_GET['style'] ) )
    95       $sep = '&';
    96   {
    96     }
    97     $flags .= $sep . 'style='.$session->style;
    97     if ( isset($_GET['style'] ) )
    98     $sep = '&';
    98     {
    99   }
    99       $flags .= $sep . 'style='.$session->style;
   100   if ( isset($_GET['lang']) && preg_match('/^[a-z0-9_]+$/', @$_GET['lang']) )
   100       $sep = '&';
   101   {
   101     }
   102     $flags .= $sep . 'lang=' . urlencode($_GET['lang']);
   102     if ( isset($_GET['lang']) && preg_match('/^[a-z0-9_]+$/', @$_GET['lang']) )
   103     $sep = '&';
   103     {
   104   }
   104       $flags .= $sep . 'lang=' . urlencode($_GET['lang']);
   105 
   105       $sep = '&';
   106   $url = $session->append_sid(contentPath.$t.$flags);
   106     }
   107   if($query)
   107   
   108   {
   108     $url = $session->append_sid(contentPath.$t.$flags);
   109     $sep = strstr($url, '?') ? '&' : '?';
   109     if($query)
   110     $url = $url . $sep . $query;
   110     {
   111   }
   111       $sep = strstr($url, '?') ? '&' : '?';
   112 
   112       $url = $url . $sep . $query;
   113   return ($escape) ? htmlspecialchars($url) : $url;
   113     }
       
   114   
       
   115     return ($escape) ? htmlspecialchars($url) : $url;
       
   116   }
   114 }
   117 }
   115 
   118 
   116 /**
   119 /**
   117  * Create a URI for an internal link, and be namespace-friendly. Watch out for this one because it's different from most other Enano functions, in that the namespace is the first parameter.
   120  * Create a URI for an internal link, and be namespace-friendly. Watch out for this one because it's different from most other Enano functions, in that the namespace is the first parameter.
   118  * @param string The namespace ID
   121  * @param string The namespace ID
   120  * @param string The GET query string to append
   123  * @param string The GET query string to append
   121  * @param bool   If true, perform htmlspecialchars() on the return value to make it HTML-safe
   124  * @param bool   If true, perform htmlspecialchars() on the return value to make it HTML-safe
   122  * @return string
   125  * @return string
   123  */
   126  */
   124 
   127 
   125 function makeUrlNS($n, $t, $query = false, $escape = false)
   128 if ( !function_exists('makeUrlNS') )
   126 {
   129 {
   127   global $db, $session, $paths, $template, $plugins; // Common objects
   130   function makeUrlNS($n, $t, $query = false, $escape = false)
   128   $flags = '';
   131   {
   129 
   132     global $db, $session, $paths, $template, $plugins; // Common objects
   130   if(defined('ENANO_BASE_CLASSES_INITIALIZED'))
   133     $flags = '';
   131   {
   134   
   132     $sep = urlSeparator;
   135     if(defined('ENANO_BASE_CLASSES_INITIALIZED'))
   133   }
   136     {
   134   else
   137       $sep = urlSeparator;
   135   {
       
   136     $sep = (strstr($_SERVER['REQUEST_URI'], '?')) ? '&' : '?';
       
   137   }
       
   138   if ( isset( $_GET['printable'] ) ) {
       
   139     $flags .= $sep . 'printable';
       
   140     $sep = '&';
       
   141   }
       
   142   if ( isset( $_GET['theme'] ) )
       
   143   {
       
   144     $flags .= $sep . 'theme='.$session->theme;
       
   145     $sep = '&';
       
   146   }
       
   147   if ( isset( $_GET['style'] ) )
       
   148   {
       
   149     $flags .= $sep . 'style='.$session->style;
       
   150     $sep = '&';
       
   151   }
       
   152   if ( isset($_GET['lang']) && preg_match('/^[a-z0-9_]+$/', @$_GET['lang']) )
       
   153   {
       
   154     $flags .= $sep . 'lang=' . urlencode($_GET['lang']);
       
   155     $sep = '&';
       
   156   }
       
   157   
       
   158   $ns_prefix = "$n:";
       
   159 
       
   160   if(defined('ENANO_BASE_CLASSES_INITIALIZED'))
       
   161   {
       
   162     $ns_prefix = ( isset($paths->nslist[$n]) ) ? $paths->nslist[$n] : $n . substr($paths->nslist['Special'], -1);
       
   163     $url = contentPath . $ns_prefix . $t . $flags;
       
   164   }
       
   165   else
       
   166   {
       
   167     // If the path manager hasn't been initted yet, take an educated guess at what the URI should be
       
   168     $url = contentPath . $n . ':' . $t . $flags;
       
   169   }
       
   170 
       
   171   if($query)
       
   172   {
       
   173     if(strstr($url, '?'))
       
   174     {
       
   175       $sep =  '&';
       
   176     }
   138     }
   177     else
   139     else
   178     {
   140     {
   179       $sep = '?';
   141       $sep = (strstr($_SERVER['REQUEST_URI'], '?')) ? '&' : '?';
   180     }
   142     }
   181     $url = $url . $sep . $query . $flags;
   143     if ( isset( $_GET['printable'] ) ) {
   182   }
   144       $flags .= $sep . 'printable';
   183 
   145       $sep = '&';
   184   if(defined('ENANO_BASE_CLASSES_INITIALIZED'))
   146     }
   185   {
   147     if ( isset( $_GET['theme'] ) )
   186     $url = $session->append_sid($url);
   148     {
   187   }
   149       $flags .= $sep . 'theme='.$session->theme;
   188 
   150       $sep = '&';
   189   return ($escape) ? htmlspecialchars($url) : $url;
   151     }
       
   152     if ( isset( $_GET['style'] ) )
       
   153     {
       
   154       $flags .= $sep . 'style='.$session->style;
       
   155       $sep = '&';
       
   156     }
       
   157     if ( isset($_GET['lang']) && preg_match('/^[a-z0-9_]+$/', @$_GET['lang']) )
       
   158     {
       
   159       $flags .= $sep . 'lang=' . urlencode($_GET['lang']);
       
   160       $sep = '&';
       
   161     }
       
   162     
       
   163     $ns_prefix = "$n:";
       
   164   
       
   165     if(defined('ENANO_BASE_CLASSES_INITIALIZED'))
       
   166     {
       
   167       $ns_prefix = ( isset($paths->nslist[$n]) ) ? $paths->nslist[$n] : $n . substr($paths->nslist['Special'], -1);
       
   168       $url = contentPath . $ns_prefix . $t . $flags;
       
   169     }
       
   170     else
       
   171     {
       
   172       // If the path manager hasn't been initted yet, take an educated guess at what the URI should be
       
   173       $url = contentPath . $n . ':' . $t . $flags;
       
   174     }
       
   175   
       
   176     if($query)
       
   177     {
       
   178       if(strstr($url, '?'))
       
   179       {
       
   180         $sep =  '&';
       
   181       }
       
   182       else
       
   183       {
       
   184         $sep = '?';
       
   185       }
       
   186       $url = $url . $sep . $query . $flags;
       
   187     }
       
   188   
       
   189     if(defined('ENANO_BASE_CLASSES_INITIALIZED'))
       
   190     {
       
   191       $url = $session->append_sid($url);
       
   192     }
       
   193   
       
   194     return ($escape) ? htmlspecialchars($url) : $url;
       
   195   }
   190 }
   196 }
   191 
   197 
   192 /**
   198 /**
   193  * Create a URI for an internal link, be namespace-friendly, and add http://hostname/scriptpath to the beginning if possible. Watch out for this one because it's different from most other Enano functions, in that the namespace is the first parameter.
   199  * Create a URI for an internal link, be namespace-friendly, and add http://hostname/scriptpath to the beginning if possible. Watch out for this one because it's different from most other Enano functions, in that the namespace is the first parameter.
   194  * @param string The namespace ID
   200  * @param string The namespace ID
   947 
   953 
   948 /**
   954 /**
   949  * Prints out the file information box seen on File: pages. Doesn't take or return anything, but assumes that the page information is already set in $paths, and expects $paths->namespace to be File.
   955  * Prints out the file information box seen on File: pages. Doesn't take or return anything, but assumes that the page information is already set in $paths, and expects $paths->namespace to be File.
   950  */
   956  */
   951 
   957 
   952 function show_file_info()
   958 function show_file_info($page = false)
   953 {
   959 {
   954   global $db, $session, $paths, $template, $plugins; // Common objects
   960   global $db, $session, $paths, $template, $plugins; // Common objects
   955   global $lang;
   961   global $lang;
   956   
   962   
       
   963   $local_page_id = $paths->page_id;
       
   964   $local_namespace = $paths->namespace;
       
   965   
       
   966   if ( is_object($page) )
       
   967   {
       
   968     $local_page = $page->page_id;
       
   969     $local_namespace = $page->namespace;
       
   970   }
       
   971   
   957   // Prevent unnecessary work
   972   // Prevent unnecessary work
   958   if ( $paths->namespace != 'File' )
   973   if ( $local_namespace != 'File' )
   959     return null;
   974     return null;
   960   
   975   
   961   $selfn = $paths->page_id;
   976   $selfn = $local_page_id;
   962   if ( substr($paths->cpage['name'], 0, strlen($paths->nslist['File'])) == $paths->nslist['File'])
   977   if ( substr($paths->cpage['name'], 0, strlen($paths->nslist['File'])) == $paths->nslist['File'])
   963   {
   978   {
   964     $selfn = substr($paths->page_id, strlen($paths->nslist['File']), strlen($paths->page_id));
   979     $selfn = substr($local_page_id, strlen($paths->nslist['File']), strlen($local_page_id));
   965   }
   980   }
   966   $selfn = $db->escape($selfn);
   981   $selfn = $db->escape($selfn);
   967   $q = $db->sql_query('SELECT f.mimetype,f.time_id,f.size,l.log_id FROM ' . table_prefix . "files AS f\n"
   982   $q = $db->sql_query('SELECT f.mimetype,f.time_id,f.size,l.log_id FROM ' . table_prefix . "files AS f\n"
   968                     . "  LEFT JOIN " . table_prefix . "logs AS l\n"
   983                     . "  LEFT JOIN " . table_prefix . "logs AS l\n"
   969                     . "    ON ( l.time_id = f.time_id AND ( l.action = 'reupload' OR l.action IS NULL ) )\n"
   984                     . "    ON ( l.time_id = f.time_id AND ( l.action = 'reupload' OR l.action IS NULL ) )\n"
   976   
   991   
   977   if ( $db->numrows() < 1 )
   992   if ( $db->numrows() < 1 )
   978   {
   993   {
   979     echo '<div class="mdg-comment" style="margin-left: 0;">
   994     echo '<div class="mdg-comment" style="margin-left: 0;">
   980             <h3>' . $lang->get('onpage_filebox_heading') . '</h3>
   995             <h3>' . $lang->get('onpage_filebox_heading') . '</h3>
   981             <p>' . $lang->get('onpage_filebox_msg_not_found', array('upload_link' => makeUrlNS('Special', 'UploadFile/'.$paths->page_id))) . '</p>
   996             <p>' . $lang->get('onpage_filebox_msg_not_found', array('upload_link' => makeUrlNS('Special', 'UploadFile/'.$local_page_id))) . '</p>
   982           </div>
   997           </div>
   983           <br />';
   998           <br />';
   984     return;
   999     return;
   985   }
  1000   }
   986   $r = $db->fetchrow();
  1001   $r = $db->fetchrow();
  3013  * @param string Filename to encode into the compressed data - defaults to blank
  3028  * @param string Filename to encode into the compressed data - defaults to blank
  3014  * @param string Comment for archive - defaults to blank
  3029  * @param string Comment for archive - defaults to blank
  3015  * @return string Compressed data
  3030  * @return string Compressed data
  3016  */
  3031  */
  3017 
  3032 
  3018 if ( !function_exists('gzencode') )
  3033 function enano_gzencode($data = "", $level = 6, $filename = "", $comments = "")
  3019 {
  3034 {
  3020   function gzencode($data = "", $level = 6, $filename = "", $comments = "")
  3035   $flags = (empty($comment)? 0 : 16) + (empty($filename)? 0 : 8);
  3021   {
  3036   $mtime = time();
  3022     $flags = (empty($comment)? 0 : 16) + (empty($filename)? 0 : 8);
  3037   
  3023     $mtime = time();
  3038   if ( !function_exists('gzdeflate') )
  3024     
  3039     return false;
  3025     if ( !function_exists('gzdeflate') )
  3040  
  3026       return false;
  3041   return (pack("C1C1C1C1VC1C1", 0x1f, 0x8b, 8, $flags, $mtime, 2, 0xFF) .
  3027    
  3042           (empty($filename) ? "" : $filename . "\0") .
  3028     return (pack("C1C1C1C1VC1C1", 0x1f, 0x8b, 8, $flags, $mtime, 2, 0xFF) .
  3043           (empty($comment) ? "" : $comment . "\0") .
  3029             (empty($filename) ? "" : $filename . "\0") .
  3044           gzdeflate($data, $level) .
  3030             (empty($comment) ? "" : $comment . "\0") .
  3045           pack("VV", crc32($data), strlen($data)));
  3031             gzdeflate($data, $level) .
       
  3032             pack("VV", crc32($data), strlen($data)));
       
  3033   }
       
  3034 }
  3046 }
  3035 
  3047 
  3036 /**
  3048 /**
  3037  * Gzips the output buffer.
  3049  * Gzips the output buffer.
  3038  */
  3050  */
  3041 {
  3053 {
  3042   global $do_gzip;
  3054   global $do_gzip;
  3043   
  3055   
  3044   //
  3056   //
  3045   // Compress buffered output if required and send to browser
  3057   // Compress buffered output if required and send to browser
       
  3058   // Sorry, doesn't work in IE. What else is new?
  3046   //
  3059   //
  3047   if ( $do_gzip && function_exists('gzdeflate') )
  3060   if ( $do_gzip && function_exists('gzdeflate') && !strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE') )
  3048   {
  3061   {
  3049     $gzip_contents = ob_get_contents();
  3062     $gzip_contents = ob_get_contents();
  3050     ob_end_clean();
  3063     ob_end_clean();
  3051     
  3064     
  3052     $return = @gzencode($gzip_contents);
  3065     $return = @enano_gzencode($gzip_contents);
  3053     if ( $return )
  3066     if ( $return )
  3054     {
  3067     {
  3055       header('Content-encoding: gzip');
  3068       header('Content-encoding: gzip');
  3056       echo $return;
  3069       echo $return;
  3057     }
  3070     }
  3646     return false;
  3659     return false;
  3647   
  3660   
  3648   if ( !file_exists($in_file) )
  3661   if ( !file_exists($in_file) )
  3649     return false;
  3662     return false;
  3650   
  3663   
  3651   if ( preg_match('/["\'\/\\]/', $in_file) || preg_match('/["\'\/\\]/', $out_file) )
  3664   if ( preg_match('/["\'\/\\\\]/', $in_file) || preg_match('/["\'\/\\\\]/', $out_file) )
  3652     die('SECURITY: scale_image(): infile or outfile path is screwy');
  3665     die('SECURITY: scale_image(): infile or outfile path is screwy');
  3653   
  3666   
  3654   if ( file_exists($out_file) && !$unlink )
  3667   if ( file_exists($out_file) && !$unlink )
  3655     return false;
  3668     return false;
  3656   else if ( file_exists($out_file) && $unlink )
  3669   else if ( file_exists($out_file) && $unlink )