Esempio n. 1
0
 /** ====================================================================================================================================================
  * Ensure that the needed folders are writable by the webserver. 
  * Will check usual folders and files.
  * You may add this in your configuration page <code>$this->check_folder_rights( array(array($theFolderToCheck, "rw")) ) ;</code>
  * If not a error msg is printed
  * 
  * @param array $folders list of array with a first element (the complete path of the folder to check) and a second element (the needed rights "r", "w" [or a combination of those])
  * @return void
  */
 public function check_folder_rights($folders)
 {
     $f = array(array(WP_CONTENT_DIR . '/sedlex/', "rw"), array(WP_PLUGIN_DIR . '/' . str_replace(basename(__FILE__), "", plugin_basename(__FILE__)) . 'readme.txt', "rw"), array(WP_PLUGIN_DIR . '/' . str_replace(basename(__FILE__), "", plugin_basename(__FILE__)) . 'css/', "r"), array(WP_PLUGIN_DIR . '/' . str_replace(basename(__FILE__), "", plugin_basename(__FILE__)) . 'js/', "r"), array(WP_PLUGIN_DIR . '/' . str_replace(basename(__FILE__), "", plugin_basename(__FILE__)) . 'lang/', "rw"), array(WP_PLUGIN_DIR . '/' . str_replace(basename(__FILE__), "", plugin_basename(__FILE__)) . 'core/', "r"), array(WP_PLUGIN_DIR . '/' . str_replace(basename(__FILE__), "", plugin_basename(__FILE__)) . 'core/img/', "r"), array(WP_PLUGIN_DIR . '/' . str_replace(basename(__FILE__), "", plugin_basename(__FILE__)) . 'core/templates/', "r"), array(WP_PLUGIN_DIR . '/' . str_replace(basename(__FILE__), "", plugin_basename(__FILE__)) . 'core/lang/', "rw"), array(WP_PLUGIN_DIR . '/' . str_replace(basename(__FILE__), "", plugin_basename(__FILE__)) . 'core/js/', "r"), array(WP_PLUGIN_DIR . '/' . str_replace(basename(__FILE__), "", plugin_basename(__FILE__)) . 'core/css/', "r"));
     $folders = array_merge($folders, $f);
     $result = "";
     foreach ($folders as $f) {
         if (is_dir($f[0]) || @is_file($f[0])) {
             $readable = SLFramework_Utils::is_readable($f[0]);
             $writable = SLFramework_Utils::is_writable($f[0]);
             @chmod($f[0], 0755);
             $pb = false;
             if (strpos($f[1], "r") !== false && !$readable) {
                 $pb = true;
             }
             if (strpos($f[1], "w") !== false && !$writable) {
                 $pb = true;
             }
             if ($pb) {
                 if (is_dir($f[0])) {
                     $result .= "<p>" . sprintf(__('The folder %s is not %s !', 'SL_framework'), "<code>" . $f[0] . "</code>", "<code>" . $f[1] . "</code>") . "</p>";
                 }
                 if (@is_file($f[0])) {
                     $result .= "<p>" . sprintf(__('The file %s is not %s !', 'SL_framework'), "<code>" . $f[0] . "</code>", "<code>" . $f[1] . "</code>") . "</p>";
                 }
             }
         } else {
             // We check if the last have an extension
             if (strpos(basename($f[0]), ".") === false) {
                 // It is a folder
                 if (!@mkdir($f[0], 0755, true)) {
                     $result .= "<p>" . sprintf(__('The folder %s does not exists and cannot be created !', 'SL_framework'), "<code>" . $f[0] . "</code>") . "</p>";
                 }
             } else {
                 $foldtemp = str_replace(basename($f[0]), "", str_replace(basename($f[0]) . "/", "", $f[0]));
                 // We create the sub folders
                 if (!is_dir($foldtemp) && !@mkdir($foldtemp, 0755, true)) {
                     $result .= "<p>" . sprintf(__('The folder %s does not exists and cannot be created !', 'SL_framework'), "<code>" . $foldtemp . "</code>") . "</p>";
                 } else {
                     // We touch the file
                     @chmod($foldtemp, 0755);
                     if (@file_put_contents($f[0], '') === false) {
                         $result .= "<p>" . sprintf(__('The file %s does not exists and cannot be created !', 'SL_framework'), "<code>" . $f[0] . "</code>") . "</p>";
                     }
                 }
             }
         }
     }
     if ($result != "") {
         echo "<div class='error fade'><p>" . __('There are some issues with folders rights. Please corret them as soon as possible as they could induce bugs and instabilities.', 'SL_framework') . "</p><p>" . __('Please see below:', 'SL_framework') . "</p>" . $result . "</div>";
     }
 }
 /** ====================================================================================================================================================
  * Check if a folder or a file is writable
  * 
  * @param string $path the path to the folder or the file
  * @return boolean true if the folder or the file is writable
  */
 static function is_writable($path)
 {
     if ($path[strlen($path) - 1] == '/') {
         // recursively return a temporary file path
         return SLFramework_Utils::is_writable($path . uniqid(mt_rand()) . '.tmp');
     } else {
         if (is_dir($path)) {
             return SLFramework_Utils::is_writable($path . '/' . uniqid(mt_rand()) . '.tmp');
         }
     }
     // check tmp file for read/write capabilities
     $rm = file_exists($path);
     $f = @fopen($path, 'a');
     if ($f === false) {
         return false;
     }
     @fclose($f);
     if (!$rm) {
         @unlink($path);
     }
     return true;
 }