function uploadPhoto($user, $file)
{
    if ($user->getRole()->getRoleID() == "4") {
        return "This user was forbidden to do this!";
    }
    if (gettype($file["error"]) == "array") {
        return "Only accept one file!";
    }
    $res = isValidUploadFile($file["error"]);
    if ($res !== true) {
        return $res;
    }
    $res = isValidImage($file["name"]);
    if ($res !== true) {
        return $res;
    }
    $fileDir = "photo/";
    $filePath = $fileDir . $user->getUserID() . "." . pathinfo($file["name"], PATHINFO_EXTENSION);
    if (file_exists($filePath)) {
        unlink($filePath);
    }
    if (!move_uploaded_file($file['tmp_name'], $filePath)) {
        return "Fail to move file, please contact administrator!";
    }
    $user->setPhotoURL($filePath);
    return true;
}
function uploadFile($userID, $groupID, $file)
{
    $userDAO = new UserDAO();
    $user = $userDAO->getUserByID($userID);
    if ($user->getRole()->getRoleID() == "4") {
        return "This user was forbidden to upload file!";
    }
    if (!isValidID($groupID)) {
        return "Group id is not valid!";
    }
    $groupDAO = new GroupDAO();
    $group = $groupDAO->getGroupByID($groupID);
    if ($group === null) {
        return "Can not find this group!";
    }
    if ($group->getActivateStatus() === "2") {
        return "Group is not activated!";
    }
    $groupMemberDAO = new GroupMemberDAO();
    $groupMember = $groupMemberDAO->getGroupMember($group, $user);
    if ($groupMember === null) {
        return "User didn't belong to this group!";
    }
    if (gettype($file["error"]) == "array") {
        return "Only accept one file!";
    }
    $res = isValidUploadFile($file["error"]);
    if ($res !== true) {
        return $res;
    }
    $fileType = -1;
    $res = isValidImage($file["name"]);
    if ($res === true) {
        $fileType = "2";
    }
    $res = isValidFile($file["name"]);
    if ($res === true) {
        $fileType = "3";
    }
    if ($fileType === -1) {
        return "Only accepts jpeg/jpg/gif/png/zip file!";
    }
    $record = new Record($group, $user, $fileType, "temp", "1");
    $recordDAO = new RecordDAO();
    $recordDAO->insertRecord($record);
    $fileDir = "upload/";
    $filePath = $fileDir . $record->getRecordID() . "_" . $file["name"];
    $record->setContent($filePath);
    $recordDAO->updateRecord($record);
    if (file_exists($filePath)) {
        unlink($filePath);
    }
    if (!move_uploaded_file($file['tmp_name'], $filePath)) {
        return "Fail to move file, please contact administrator!";
    }
    return true;
}
Exemple #3
0
function submitForm() {
    global $course_id, $course_code, $webDir, $langModifDone;

    // handle uploaded files
    $fileData = array();
    foreach (CourseXMLConfig::$binaryFields as $bkey) {
        if (in_array($bkey, CourseXMLConfig::$multipleFields) || in_array($bkey, CourseXMLConfig::$arrayFields)) {
            if (isset($_FILES[$bkey]) && isset($_FILES[$bkey]['tmp_name']) && isset($_FILES[$bkey]['type'])
                    && is_array($_FILES[$bkey]['tmp_name'])) {
                for ($i = 0; $i < count($_FILES[$bkey]['tmp_name']); $i++) {
                    if (is_uploaded_file($_FILES[$bkey]['tmp_name'][$i])
                            && isValidImage($_FILES[$bkey]['type'][$i])) {
                        // convert to resized jpg if possible
                        $uploaded = $_FILES[$bkey]['tmp_name'][$i];
                        $copied = $_FILES[$bkey]['tmp_name'][$i] . '.new';
                        $type = $_FILES[$bkey]['type'][$i];

                        if (copy_resized_image($uploaded, $type, IMAGESIZE_LARGE, IMAGESIZE_LARGE, $copied)) {
                            $fileData[$bkey][$i] = base64_encode(file_get_contents($copied));
                            $fileData[$bkey . '_mime'][$i] = 'image/jpeg'; // copy_resized_image always outputs jpg
                        } else { // erase possible previous image or failed conversion
                            $fileData[$bkey][$i] = '';
                            $fileData[$bkey . '_mime'][$i] = '';
                        }
                    } else {
                        // add to array as empty, in order to keep correspondence
                        $fileData[$bkey][$i] = '';
                        $fileData[$bkey . '_mime'][$i] = '';
                    }
                }
            }
        } else {
            if (isset($_FILES[$bkey])
                    && is_uploaded_file($_FILES[$bkey]['tmp_name'])
                    && isValidImage($_FILES[$bkey]['type'])) {
                // convert to resized jpg if possible
                $uploaded = $_FILES[$bkey]['tmp_name'];
                $copied = $_FILES[$bkey]['tmp_name'] . '.new';
                $type = $_FILES[$bkey]['type'];

                if (copy_resized_image($uploaded, $type, IMAGESIZE_LARGE, IMAGESIZE_LARGE, $copied)) {
                    $fileData[$bkey] = base64_encode(file_get_contents($copied));
                    $fileData[$bkey . '_mime'] = 'image/jpeg'; // copy_resized_image always outputs jpg
                    // unset old photo because array_merge_recursive below will keep the old one
                    unset($_POST[$bkey]);
                    unset($_POST[$bkey . '_mime']);
                } else { // erase possible previous image or failed conversion
                    $fileData[$bkey] = '';
                    $fileData[$bkey . '_mime'] = '';
                }
            }
        }
    }

    $skeleton = $webDir . '/modules/course_metadata/skeleton.xml';
    $extraData = CourseXMLElement::getAutogenData($course_id);
    // manually merge instructor photo, to achieve multiplicity sync
    foreach ($fileData['course_instructor_photo'] as $key => $value) {
        if (!empty($value)) {
            $_POST['course_instructor_photo'][$key] = $value;
        }
    }
    unset($fileData['course_instructor_photo']);
    foreach ($fileData['course_instructor_photo_mime'] as $key => $value) {
        if (!empty($value)) {
            $_POST['course_instructor_photo_mime'][$key] = $value;
        }
    }
    unset($fileData['course_instructor_photo_mime']);
    $data = array_merge($_POST, $extraData, $fileData);
    // course-based adaptation
    $dnum = Database::get()->querySingle("select count(id) as count from document where course_id = ?d", $course_id)->count;
    $vnum = Database::get()->querySingle("select count(id) as count from video where course_id = ?d", $course_id)->count;
    $vlnum = Database::get()->querySingle("select count(id) as count from videolink where course_id = ?d", $course_id)->count;
    if ($dnum + $vnum + $vlnum < 1) {
        $data['course_confirmVideolectures'] = 'false';
    }

    $xml = simplexml_load_file($skeleton, 'CourseXMLElement');
    $xml->adapt($data);
    $xml->populate($data);

    CourseXMLElement::save($course_id, $course_code, $xml);

    return "<div class='alert alert-success'>$langModifDone</div>";
}