Exemplo n.º 1
0
 /**
  * Uploads an author image to the upload/learning_path/images directory
  * @param	array	The image array, coming from the $_FILES superglobal
  * @return	boolean	True on success, false on error
  */
 function upload_image($image_array)
 {
     $image_moved = false;
     if (!empty($image_array['name'])) {
         $upload_ok = process_uploaded_file($image_array);
         $has_attachment = true;
     } else {
         $image_moved = true;
     }
     if ($upload_ok) {
         if ($has_attachment) {
             $courseDir = api_get_course_path() . '/upload/learning_path/images';
             $sys_course_path = api_get_path(SYS_COURSE_PATH);
             $updir = $sys_course_path . $courseDir;
             // Try to add an extension to the file if it hasn't one
             $new_file_name = add_ext_on_mime(stripslashes($image_array['name']), $image_array['type']);
             if (!filter_extension($new_file_name)) {
                 //Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
                 $image_moved = false;
             } else {
                 $file_extension = explode('.', $image_array['name']);
                 $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]);
                 $new_file_name = uniqid('') . '.' . $file_extension;
                 $new_path = $updir . '/' . $new_file_name;
                 //$result= @move_uploaded_file($image_array['tmp_name'], $new_path);
                 // resize the image
                 include_once api_get_path(LIBRARY_PATH) . 'image.lib.php';
                 $temp = new image($image_array['tmp_name']);
                 $picture_infos = @getimagesize($image_array['tmp_name']);
                 // $picture_infos[0]-> width
                 if ($picture_infos[0] > 104) {
                     $thumbwidth = 104;
                 } else {
                     $thumbwidth = $picture_infos[0];
                 }
                 if ($picture_infos[1] > 96) {
                     $new_height = 96;
                 } else {
                     $new_height = $picture_infos[1];
                 }
                 //$new_height = round(($thumbwidth/$picture_infos[0])*$picture_infos[1]);
                 $temp->resize($thumbwidth, $new_height, 0);
                 $type = $picture_infos[2];
                 $result = false;
                 switch ($type) {
                     case 2:
                         $result = $temp->send_image('JPG', $new_path);
                         break;
                     case 3:
                         $result = $temp->send_image('PNG', $new_path);
                         break;
                     case 1:
                         $result = $temp->send_image('GIF', $new_path);
                         break;
                 }
                 $temp->resize($thumbwidth, $new_height, 0);
                 $type = $picture_infos[2];
                 $result = false;
                 switch ($type) {
                     case 2:
                         $result = $temp->send_image('JPG', $new_path);
                         break;
                     case 3:
                         $result = $temp->send_image('PNG', $new_path);
                         break;
                     case 1:
                         $result = $temp->send_image('GIF', $new_path);
                         break;
                 }
                 // Storing the image filename
                 if ($result) {
                     $image_moved = true;
                     $this->set_preview_image($new_file_name);
                     return true;
                 }
             }
         }
     }
     return false;
 }
/**
 * Add (or edit) a template. This function displays the form and also takes care of uploading the image and storing the information in the database
 *
 * @author Patrick Cool <*****@*****.**>, Ghent University, Belgium
 * @version August 2008
 * @since Dokeos 1.8.6
 */
function add_edit_template()
{
    // initiate the object
    $form = new FormValidator('template', 'post', 'settings.php?category=Templates&action=' . $_GET['action'] . '&id=' . $_GET['id']);
    // settting the form elements: the header
    if ($_GET['action'] == 'add') {
        $title = get_lang('AddTemplate');
    } else {
        $title = get_lang('EditTemplate');
    }
    $form->addElement('header', '', $title);
    // settting the form elements: the title of the template
    $form->add_textfield('title', get_lang('Title'), false);
    // settting the form elements: the content of the template (wysiwyg editor)
    $form->addElement('html_editor', 'template_text', get_lang('Text'));
    // settting the form elements: the form to upload an image to be used with the template
    $form->addElement('file', 'template_image', get_lang('Image'), '');
    // settting the form elements: a little bit information about the template image
    $form->addElement('static', 'file_comment', '', get_lang('TemplateImageComment100x70'));
    // getting all the information of the template when editing a template
    if ($_GET['action'] == 'edit') {
        // Database table definition
        $table_system_template = Database::get_main_table('system_template');
        $sql = "SELECT * FROM {$table_system_template} WHERE id = '" . Database::escape_string($_GET['id']) . "'";
        $result = api_sql_query($sql, __FILE__, __LINE__);
        $row = Database::fetch_array($result);
        $defaults['template_id'] = $_GET['id'];
        $defaults['template_text'] = $row['content'];
        $defaults['title'] = $row['title'];
        // adding an extra field: a hidden field with the id of the template we are editing
        $form->addElement('hidden', 'template_id');
        // adding an extrra field: a preview of the image that is currently used
        if (!empty($row['image'])) {
            $form->addElement('static', 'template_image_preview', '', '<img src="' . api_get_path(WEB_PATH) . 'home/default_platform_document/' . $row['image'] . '" alt="' . get_lang('TemplatePreview') . '"/>');
        } else {
            $form->addElement('static', 'template_image_preview', '', '<img src="' . api_get_path(WEB_PATH) . 'home/default_platform_document/noimage.gif" alt="' . get_lang('NoTemplatePreview') . '"/>');
        }
        // setting the information of the template that we are editing
        $form->setDefaults($defaults);
    }
    // settting the form elements: the submit button
    $form->addElement('style_submit_button', 'submit', get_lang('Ok'), 'class="save"');
    // setting the rules: the required fields
    $form->addRule('title', '<div class="required">' . get_lang('ThisFieldIsRequired'), 'required');
    $form->addRule('template_text', '<div class="required">' . get_lang('ThisFieldIsRequired'), 'required');
    // if the form validates (complies to all rules) we save the information, else we display the form again (with error message if needed)
    if ($form->validate()) {
        // exporting the values
        $values = $form->exportValues();
        // upload the file
        if (!empty($_FILES['template_image']['name'])) {
            include_once api_get_path(LIBRARY_PATH) . 'fileUpload.lib.php';
            $upload_ok = process_uploaded_file($_FILES['template_image']);
            if ($upload_ok) {
                // Try to add an extension to the file if it hasn't one
                $new_file_name = add_ext_on_mime(stripslashes($_FILES['template_image']['name']), $_FILES['template_image']['type']);
                // upload dir
                $upload_dir = api_get_path(SYS_PATH) . 'home/default_platform_document/';
                // create dir if not exists
                if (!is_dir($upload_dir)) {
                    $perm = api_get_setting('permissions_for_new_directories');
                    $perm = octdec(!empty($perm) ? $perm : '0770');
                    $res = @mkdir($upload_dir, $perm);
                }
                // resize image to max default and upload
                require_once api_get_path(LIBRARY_PATH) . 'image.lib.php';
                $temp = new image($_FILES['template_image']['tmp_name']);
                $picture_infos = @getimagesize($_FILES['template_image']['tmp_name']);
                $max_width_for_picture = 100;
                if ($picture_infos[0] > $max_width_for_picture) {
                    $thumbwidth = $max_width_for_picture;
                    if (empty($thumbwidth) or $thumbwidth == 0) {
                        $thumbwidth = $max_width_for_picture;
                    }
                    $new_height = round($thumbwidth / $picture_infos[0] * $picture_infos[1]);
                    $temp->resize($thumbwidth, $new_height, 0);
                }
                $type = $picture_infos[2];
                switch (!empty($type)) {
                    case 2:
                        $temp->send_image('JPG', $upload_dir . $new_file_name);
                        break;
                    case 3:
                        $temp->send_image('PNG', $upload_dir . $new_file_name);
                        break;
                    case 1:
                        $temp->send_image('GIF', $upload_dir . $new_file_name);
                        break;
                }
            }
        }
        // store the information in the database (as insert or as update)
        $table_system_template = Database::get_main_table('system_template');
        if ($_GET['action'] == 'add') {
            $sql = "INSERT INTO {$table_system_template} (title, content, image) VALUES ('" . Database::escape_string($values['title']) . "','" . Database::escape_string($values['template_text']) . "','" . Database::escape_string($new_file_name) . "')";
            $result = api_sql_query($sql, __FILE__, __LINE__);
            // display a feedback message
            Display::display_confirmation_message('TemplateAdded');
            echo '<a href="settings.php?category=Templates&amp;action=add">' . Display::return_icon('add_template.gif', get_lang('AddTemplate')) . get_lang('AddTemplate') . '</a>';
        } else {
            $sql = "UPDATE {$table_system_template} set title = '" . Database::escape_string($values['title']) . "',\n\t\t\t\t\t\t\t\t\t\t\t   \t\t  content = '" . Database::escape_string($values['template_text']) . "'";
            if (!empty($new_file_name)) {
                $sql .= ", image = '" . Database::escape_string($new_file_name) . "'";
            }
            $sql .= " WHERE id='" . Database::escape_string($_GET['id']) . "'";
            $result = api_sql_query($sql, __FILE__, __LINE__);
            // display a feedback message
            Display::display_confirmation_message('TemplateEdited');
        }
        display_templates();
    } else {
        // display the form
        $form->display();
    }
}