1<?php
2/*
3    +-----------------------------------------------------------------------------+
4    | ILIAS open source                                                           |
5    +-----------------------------------------------------------------------------+
6    | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
7    |                                                                             |
8    | This program is free software; you can redistribute it and/or               |
9    | modify it under the terms of the GNU General Public License                 |
10    | as published by the Free Software Foundation; either version 2              |
11    | of the License, or (at your option) any later version.                      |
12    |                                                                             |
13    | This program is distributed in the hope that it will be useful,             |
14    | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
15    | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
16    | GNU General Public License for more details.                                |
17    |                                                                             |
18    | You should have received a copy of the GNU General Public License           |
19    | along with this program; if not, write to the Free Software                 |
20    | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
21    +-----------------------------------------------------------------------------+
22*/
23
24chdir('../../../../../');
25
26require_once 'Services/Init/classes/class.ilInitialisation.php';
27ilInitialisation::initILIAS();
28
29/**
30 * @var $ilIliasIniFile ilIniFile
31 */
32global $DIC;
33
34$ilIliasIniFile = $DIC['ilIliasIniFile'];
35
36$htdocs = $ilIliasIniFile->readVariable('server', 'absolute_path') . '/';
37$weburl = $ilIliasIniFile->readVariable('server', 'http_path') . '/';
38$installpath = $htdocs;
39
40
41// directory where tinymce files are located
42$iliasMobPath = 'data/' . CLIENT_ID . '/mobs/';
43$iliasAbsolutePath = $htdocs;
44$iliasHttpPath = $weburl;
45// base url for images
46$tinyMCE_base_url = $weburl;
47$tinyMCE_DOC_url = $installpath;
48
49if ($iliasHttpPath) {
50    /**
51     * @var $https ilHttps
52     */
53    global $DIC;
54
55    $https = $DIC['https'];
56
57    if (strpos($iliasHttpPath, 'https://') === false && $https->isDetected()) {
58        $iliasHttpPath = str_replace('http://', 'https://', $iliasHttpPath);
59    }
60}
61
62// allowed extentions for uploaded image files
63$tinyMCE_valid_imgs = array('gif', 'jpg', 'jpeg', 'png');
64
65// allow upload in image library
66$tinyMCE_upload_allowed = true;
67
68include_once 'webservice/soap/include/inc.soap_functions.php';
69$mobs = ilSoapFunctions::getMobsOfObject(session_id() . '::' . CLIENT_ID, $_GET['obj_type'] . ':html', (int) $_GET['obj_id']);
70$preview = '';
71
72
73$img = isset($_POST['imglist']) ? $_POST['imglist'] : '';
74$_root = $installpath;
75$errors = array();
76
77// upload images
78if (isset($_FILES['img_file']['size']) && $_FILES['img_file']['size'] > 0) {
79    include_once 'webservice/soap/include/inc.soap_functions.php';
80    $safefilename = preg_replace('/[^a-zA-z0-9_\.]/', '', $_FILES['img_file']['name']);
81    $media_object = ilSoapFunctions::saveTempFileAsMediaObject(session_id() . '::' . CLIENT_ID, $safefilename, $_FILES['img_file']['tmp_name']);
82    if (file_exists($iliasAbsolutePath . $iliasMobPath . 'mm_' . $media_object->getId() . '/' . $media_object->getTitle())) {
83        // only save usage if the file was uploaded
84        $media_object->_saveUsage($media_object->getId(), $_GET['obj_type'] . ':html', (int) $_GET['obj_id']);
85    }
86    $preview = $iliasHttpPath . $iliasMobPath . 'mm_' . $media_object->getId() . '/' . $media_object->getTitle();
87    $mobs[$media_object->getId()] = $media_object->getId();
88}
89
90$tpl = new ilGlobalTemplate(dirname(__FILE__) . '/tpl.imagemanager.html', true, true);
91
92// delete image
93if ($tinyMCE_img_delete_allowed && isset($_POST['lib_action'])
94    && ($_POST['lib_action'] == 'delete') && !empty($img)) {
95    deleteImg();
96}
97
98if ($tinyMCE_img_delete_allowed) {
99    $tpl->touchBlock("delete_allowed");
100}
101outMobImages();
102outMobImageParams();
103$tpl->setVariable('OBJ_ID', (int) $_GET['obj_id']);
104$tpl->setVariable('OBJ_TYPE', $_GET['obj_type']);
105$tpl->setVariable('VALUE_UPDATE', (int) $_GET['update']);
106$tpl->setVariable('ILIAS_INST_PATH', $iliasHttpPath);
107if ($_GET['update'] == 1) {
108    $tpl->setVariable('INSERT_COMMAND', '{#update}');
109} else {
110    $tpl->setVariable('INSERT_COMMAND', '{#insert}');
111}
112$tpl->setVariable('URL_PREVIEW', $preview);
113$error_messages = '';
114if (!empty($errors)) {
115    $error_messages .= '<span class="error">';
116    foreach ($errors as $err) {
117        $error_messages .= $err . '<br />';
118    }
119    $error_messages .= '</span>';
120}
121$tpl->setVariable('ERROR_MESSAGES', $error_messages);
122$tpl->printToStdout("DEFAULT", false, true);
123
124function outMobImages()
125{
126    /**
127     * @var $tpl ilTemplate
128     */
129    global $DIC, $mobs, $iliasMobPath, $iliasAbsolutePath, $iliasHttpPath, $tinyMCE_valid_imgs, $errors, $img, $arr_tinyMCE_image_files;
130
131    $tpl = $DIC['tpl'];
132
133    $arr_tinyMCE_image_files = array();
134
135    $i = 0;
136    // read image directory
137    foreach ($mobs as $mob) {
138        $mobdir = $iliasAbsolutePath . $iliasMobPath . 'mm_' . $mob . '/';
139        $d = @dir($mobdir);
140        if ($d) {
141            while (false !== ($entry = $d->read())) {
142                $ext = strtolower(substr(strrchr($entry, '.'), 1));
143                if (is_file($mobdir . $entry) && in_array($ext, $tinyMCE_valid_imgs)) {
144                    $arr_tinyMCE_image_files[$i]['file_name'] = $entry;
145                    $arr_tinyMCE_image_files[$i]['file_dir'] = $mobdir;
146                    $arr_tinyMCE_image_files[$i]['http_dir'] = $iliasHttpPath . $iliasMobPath . 'mm_' . $mob . '/';
147                    $i++;
148                }
149            }
150            $d->close();
151        } else {
152            $errors[] = '{#ibrowser.errornodir}';
153        }
154    }
155    // sort the list of image filenames alphabetically.
156    sort($arr_tinyMCE_image_files);
157
158    for ($k = 0; $k < count($arr_tinyMCE_image_files); $k++) {
159        $entry = $arr_tinyMCE_image_files[$k]['file_name'];
160        $size = getimagesize($arr_tinyMCE_image_files[$k]['file_dir'] . $entry);
161        $fsize = filesize($arr_tinyMCE_image_files[$k]['file_dir'] . $entry);
162        $tpl->setCurrentBlock('imagefile');
163        $tpl->setVariable('IMAGEFILE_VALUE', $arr_tinyMCE_image_files[$k]['http_dir']);
164        $tpl->setVariable('IMAGEFILE_TEXT', $entry);
165        if ($entry == $img) {
166            $tpl->setVariable('IMAGEFILE_SELECTED', ' selected=\'selected\'');
167        }
168        $tpl->parseCurrentBlock();
169    }
170}
171
172function deleteImg()
173{
174}
175
176function outMobImageParams()
177{
178    global $DIC, $arr_tinyMCE_image_files;
179
180    $tpl = $DIC['tpl'];
181    for ($k = 0; $k < count($arr_tinyMCE_image_files); $k++) {
182        $tpl->setCurrentBlock('imageparams');
183        $entry = $arr_tinyMCE_image_files[$k]['file_name'];
184        $size = getimagesize($arr_tinyMCE_image_files[$k]['file_dir'] . $entry);
185        $fsize = filesize($arr_tinyMCE_image_files[$k]['file_dir'] . $entry);
186        $tpl->setVariable('IMG_WIDTH', $size[0]);
187        $tpl->setVariable('IMG_HEIGHT', $size[1]);
188        $tpl->setVariable('IMG_PATH', $arr_tinyMCE_image_files[$k]['http_dir']);
189        $tpl->setVariable('F_SIZE', ilUtil::formatSize($fsize));
190        $tpl->parseCurrentBlock();
191    }
192}
193
194function liboptions($arr, $prefix = '', $sel = '')
195{
196    $buf = '';
197    foreach ($arr as $lib) {
198        $buf .= '<option value="' . $lib['value'] . '"' . (($lib['value'] == $sel) ? ' selected' : '') . '>' . $prefix . $lib['text'] . '</option>' . "\n";
199    }
200    return $buf;
201}
202