Example #1
0
 /**
  * Moves uploaded files from P4A_UPLOADS_TMP_DIR to P4A_UPLOADS_DIR
  * @throws P4A_Exception
  */
 public function saveUploads()
 {
     while ($field = $this->fields->nextItem()) {
         $field_type = $field->getType();
         if ($field_type == 'file') {
             $new_value = $field->getNewValue();
             $old_value = $field->getValue();
             $target_dir = P4A_UPLOADS_DIR . '/' . $field->getUploadSubpath();
             if (!is_dir($target_dir)) {
                 if (!P4A_Mkdir_Recursive($target_dir)) {
                     throw new P4A_Exception("Cannot create directory \"{$target_dir}\"", P4A_FILESYSTEM_ERROR);
                 }
             }
             $a_new_value = explode(',', substr($new_value, 1, -1));
             $a_old_value = explode(',', substr($old_value, 1, -1));
             if ($old_value === null) {
                 if ($new_value !== null) {
                     $a_new_value[0] = P4A_Get_Unique_File_Name($a_new_value[6], $target_dir);
                     unset($a_new_value[6]);
                     $new_path = $target_dir . '/' . $a_new_value[0];
                     $old_path = P4A_UPLOADS_DIR . '/' . $a_new_value[1];
                     if (!rename($old_path, $new_path)) {
                         throw new P4A_Exception("Cannot rename file \"{$old_path}\" to \"{$new_path}\"", P4A_FILESYSTEM_ERROR);
                     }
                     $a_new_value[1] = P4A_Strip_Double_Slashes(str_replace(P4A_UPLOADS_DIR, '', $new_path));
                     $field->setNewValue('{' . join($a_new_value, ',') . '}');
                 } else {
                     $field->setNewValue(null);
                 }
             } else {
                 if ($new_value === null) {
                     $path = P4A_UPLOADS_DIR . $a_old_value[1];
                     if (!@unlink($path) and @file_exists($path)) {
                         throw new P4A_Exception("Cannot delete file \"{$path}\"", P4A_FILESYSTEM_ERROR);
                     }
                     $field->setNewValue(null);
                 } elseif ($new_value != $old_value) {
                     $path = P4A_UPLOADS_DIR . $a_old_value[1];
                     if (!@unlink($path) and @file_exists($path)) {
                         throw new P4A_Exception("Cannot delete file \"{$path}\"", P4A_FILESYSTEM_ERROR);
                     }
                     $a_new_value[0] = P4A_Get_Unique_File_Name($a_new_value[6], $target_dir);
                     unset($a_new_value[6]);
                     $new_path = $target_dir . '/' . $a_new_value[0];
                     $old_path = P4A_UPLOADS_DIR . '/' . $a_new_value[1];
                     if (!@rename($old_path, $new_path)) {
                         throw new P4A_Exception("Cannot rename file \"{$old_path}\" to \"{$new_path}\"", P4A_FILESYSTEM_ERROR);
                     }
                     $a_new_value[1] = str_replace(P4A_UPLOADS_DIR, '', $new_path);
                     $field->setNewValue('{' . join($a_new_value, ',') . '}');
                 }
             }
         }
     }
 }
Example #2
0
function p4a_check_configuration($additionalDir = null)
{
    $correct = true;
    $title = "Configuration checks for \"" . P4A_APPLICATION_NAME . "\"";
    $error = "<center><h2>{$title}</h2></center>\n";
    // OPERATING SYSTEM
    $error .= "<div class='box'>Checking SERVER OPERATING SYSTEM:<br />";
    if (_DS_ == '/') {
        $error .= "P4A is configured as running on <b>Linux</b>, if your server operating system is different, than correct P4A_OS and _DS_ definition.";
    } else {
        $error .= "P4A is configured as running on <b>Windows</b>, if your server operating system is different, than correct P4A_OS and _DS_ definition.";
    }
    $error .= "</div>\n";
    // PHP VERSION
    $error .= "<div class='box'>Checking PHP VERSION: ";
    $phpversion = explode('-', PHP_VERSION);
    $phpversion = explode('.', $phpversion[0]);
    if ($phpversion[0] < 5 or $phpversion[0] == 5 and $phpversion[1] < 2) {
        $error .= "<span class='red'>{$phpversion[0]}.{$phpversion[1]}.{$phpversion[2]}</span><br />PHP 5.2.0 (or higher) is required in order to run P4A";
        $correct = false;
    } else {
        $error .= "<span class='green'>{$phpversion[0]}.{$phpversion[1]}.{$phpversion[2]}</span>";
    }
    $error .= "</div>\n";
    // DOCUMENT ROOT
    $error .= "<div class='box'>Checking DOCUMENT_ROOT: ";
    if (strlen(P4A_SERVER_DIR) == 0) {
        $error .= "<span class='red'>FAILED</span><br />Define P4A_SERVER_DIR as your DOCUMENT_ROOT.";
        $correct = false;
    } else {
        $error .= "<span class='green'>OK</span>";
    }
    $error .= "</div>";
    // UPLOADS DIRECTORY
    $error .= "<div class='box'>Checking UPLOADS DIRECTORY: ";
    if (is_dir(P4A_UPLOADS_DIR) and is_writable(P4A_UPLOADS_DIR)) {
        $ok = true;
    } elseif (!is_dir(P4A_UPLOADS_DIR)) {
        if (P4A_Mkdir_Recursive(P4A_UPLOADS_DIR)) {
            $ok = true;
        } else {
            $ok = false;
        }
    } else {
        $ok = false;
    }
    if ($ok) {
        $error .= "<span class='green'>OK</span>";
    } else {
        $error .= "<span class='red'>FAILED</span><br />Create \"" . P4A_UPLOADS_DIR . "\" and set it writable.";
        $correct = false;
    }
    $error .= "</div>";
    // UPLOADS TEMPORARY DIRECTORY
    $error .= "<div class='box'>Checking UPLOADS TEMPORARY DIRECTORY: ";
    if (is_dir(P4A_UPLOADS_TMP_DIR) and is_writable(P4A_UPLOADS_TMP_DIR)) {
        $ok = true;
    } elseif (!is_dir(P4A_UPLOADS_TMP_DIR)) {
        if (P4A_Mkdir_Recursive(P4A_UPLOADS_TMP_DIR)) {
            $ok = true;
        } else {
            $ok = false;
        }
    } else {
        $ok = false;
    }
    if ($ok) {
        $error .= "<span class='green'>OK</span>";
    } else {
        $error .= "<span class='red'>FAILED</span><br />Create \"" . P4A_UPLOADS_TMP_DIR . "\" and set it writable.";
        $correct = false;
    }
    $error .= "</div>";
    // ADDITIONAL DIRECTORY
    if ($additionalDir) {
        $error .= "<div class='box'>Checking ADDITIONAL DIRECTORY: ";
        if (is_dir($additionalDir) and is_writable($additionalDir)) {
            $ok = true;
        } elseif (!is_dir($additionalDir)) {
            if (P4A_Mkdir_Recursive($additionalDir)) {
                $ok = true;
            } else {
                $ok = false;
            }
        } else {
            $ok = false;
        }
        if ($ok) {
            $error .= "<span class='green'>OK</span>";
        } else {
            $error .= "<span class='red'>FAILED</span><br />Create \"{$additionalDir}\" and set it writable.";
            $correct = false;
        }
        $error .= "</div>";
    }
    // DATABASE CONNECTION
    $error .= "<div class='box'>Checking DATABASE CONNECTION: ";
    if (defined('P4A_DSN')) {
        try {
            P4A_DB::singleton(P4A_DSN)->adapter->getConnection();
            $error .= "<span class='green'>OK</span>";
        } catch (Exception $e) {
            $error .= "<span class='red'>FAILED</span><br />Error: " . $e->getMessage() . "<br />Check P4A_DSN definition.";
            $correct = false;
        }
    } else {
        $error .= "P4A_DSN is not defined, no database connection.";
    }
    $error .= "</div>";
    // FINAL STRINGS
    $style = "<style>body {font-family:sans-serif; font-size:90%; color:#111} h1,h2,h3,h4{text-align:center} .box{padding:10px; border:1px solid #111; background-color:#fafafa; margin-bottom:10px;} .red{color:red;font-weight:bold} .green{color:green;font-weight:bold}</style>";
    $error = "<html><head><title>{$title}</title></head><body>{$style}{$error}</body></html>";
    if ($correct) {
        return true;
    } else {
        return $error;
    }
}