made a copy
[atutor.git] / editor / edit_content.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto                  */
7 /* http://atutor.ca                                                                                                             */
8 /*                                                                                                                                              */
9 /* This program is free software. You can redistribute it and/or                */
10 /* modify it under the terms of the GNU General Public License                  */
11 /* as published by the Free Software Foundation.                                                */
12 /************************************************************************/
13 // $Id$
14
15 define('AT_INCLUDE_PATH', '../include/');
16
17 global $db, $associated_forum;
18
19 $get_related_glossary = true;
20 require(AT_INCLUDE_PATH.'vitals.inc.php');
21 require(AT_INCLUDE_PATH.'lib/tinymce.inc.php');
22 require(AT_INCLUDE_PATH.'lib/filemanager.inc.php');
23
24 $cid = intval($_REQUEST['cid']);
25
26 if ($_POST) {
27         $do_check = TRUE;
28 } else {
29         $do_check = FALSE;
30 }
31
32 require(AT_INCLUDE_PATH.'lib/editor_tab_functions.inc.php');
33
34 if ($_POST['close'] || $_GET['close']) {
35         if ($_GET['close']) {
36                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
37         } else {
38                 $msg->addFeedback('CLOSED');
39                 if ($cid == 0) {
40                         header('Location: '.AT_BASE_HREF.'tools/content/index.php');
41                         exit;
42                 }
43         }
44         
45         if ($_REQUEST['cid'] == 0) {
46                 header('Location: '.AT_BASE_HREF.'tools/content/index.php');
47                 exit;
48         }
49         header('Location: '.$_base_path.url_rewrite('content.php?cid='.intval($_REQUEST['cid'])));
50         exit;
51 }
52         
53 $tabs = get_tabs();     
54 $num_tabs = count($tabs);
55 for ($i=0; $i < $num_tabs; $i++) {
56         if (isset($_POST['button_'.$i]) && ($_POST['button_'.$i] != -1)) { 
57                 $current_tab = $i;
58                 $_POST['current_tab'] = $i;
59                 break;
60         }
61 }
62
63 if (isset($_GET['tab'])) {
64         $current_tab = intval($_GET['tab']);
65 }
66 if (isset($_POST['current_tab'])) {
67         $current_tab = intval($_POST['current_tab']);
68 }
69
70 if (isset($_POST['submit_file'])) {
71         paste_from_file(body_text);
72 } else if (isset($_POST['submit']) && ($_POST['submit'] != 'submit1')) {
73         /* we're saving. redirects if successful. */
74         save_changes(true, $current_tab);
75 }
76
77 if (isset($_POST['submit_file_alt'])) {
78         paste_from_file(body_text_alt);
79 } else if (isset($_POST['submit']) && ($_POST['submit'] != 'submit1')) {
80         /* we're saving. redirects if successful. */
81         save_changes(true, $current_tab);
82 }
83
84 if (isset($_POST['submit'])) {
85         /* we're saving. redirects if successful. */
86         save_changes(true, $current_tab);
87 }
88
89 if (!isset($current_tab) && isset($_POST['button_1']) && ($_POST['button_1'] == -1) && !isset($_POST['submit'])) {
90         $current_tab = 1;
91 } else if (!isset($current_tab) && (($_POST['desc_submit'] != '') || ($_POST['reverse'] != ''))) {
92         $current_tab = 4;  /* after clicking 'make decisions' on accessibility tab */
93 } else if (!isset($current_tab)) {
94         $current_tab = 0;
95 }
96
97 if ($cid) {
98         $_section[0][0] = _AT('edit_content');
99 } else {
100         $_section[0][0] = _AT('add_content');
101 }
102
103 if ($cid) {
104         $result = $contentManager->getContentPage($cid);
105
106         if (!($content_row = @mysql_fetch_assoc($result))) {
107                 require(AT_INCLUDE_PATH.'header.inc.php');
108                 $msg->printErrors('PAGE_NOT_FOUND');
109                 require (AT_INCLUDE_PATH.'footer.inc.php');
110                 exit;
111         }
112
113         $path   = $contentManager->getContentPath($cid);
114         $content_test = $contentManager->getContentTestsAssoc($cid);
115
116         if (defined('AT_FORCE_GET_FILE') && AT_FORCE_GET_FILE) {
117                 $course_base_href = 'get.php/';
118         } else {
119                 $course_base_href = 'content/' . $_SESSION['course_id'] . '/';
120         }
121
122         if ($content_row['content_path']) {
123                 $content_base_href .= $content_row['content_path'].'/';
124         }
125 } else {
126         if (defined('AT_FORCE_GET_FILE') && AT_FORCE_GET_FILE) {
127                 $content_base_href = 'get.php/';
128         } else {
129                 $content_base_href = 'content/' . $_SESSION['course_id'] . '/';
130         }
131 }
132
133 if ($current_tab == 4) {
134         /* kludge for issue #1626: */
135         /* fixes the base href for the AChecker tab. */
136         $course_base_href = '';
137         $content_base_href = '';
138 }
139
140 //Added by Silvia
141
142 // tools/filemanager/top.php
143
144 if (!$_GET['f']) {
145         $_SESSION['done'] = 0;
146 }
147 if (!authenticate(AT_PRIV_FILES,AT_PRIV_RETURN)) {
148         authenticate(AT_PRIV_CONTENT);
149 }
150
151 $current_path = AT_CONTENT_DIR.$_SESSION['course_id'].'/';
152
153 $MakeDirOn = true;
154
155 /* get this courses MaxQuota and MaxFileSize: */
156 $sql    = "SELECT max_quota, max_file_size FROM ".TABLE_PREFIX."courses WHERE course_id=$_SESSION[course_id]";
157 $result = mysql_query($sql, $db);
158 $row    = mysql_fetch_array($result);
159 $my_MaxCourseSize       = $row['max_quota'];
160 $my_MaxFileSize         = $row['max_file_size'];
161
162 if ($my_MaxCourseSize == AT_COURSESIZE_DEFAULT) {
163         $my_MaxCourseSize = $MaxCourseSize;
164 }
165 if ($my_MaxFileSize == AT_FILESIZE_DEFAULT) {
166         $my_MaxFileSize = $MaxFileSize;
167 } else if ($my_MaxFileSize == AT_FILESIZE_SYSTEM_MAX) {
168         $my_MaxFileSize = megabytes_to_bytes(substr(ini_get('upload_max_filesize'), 0, -1));
169 }
170
171 $MaxSubDirs  = 5;
172 $MaxDirDepth = 10;
173
174 if ($_GET['pathext'] != '') {
175         $pathext = urldecode($_GET['pathext']);
176 } else if ($_POST['pathext'] != '') {
177         $pathext = $_POST['pathext'];
178 }
179
180 if (strpos($pathext, '..') !== false) {
181         require(AT_INCLUDE_PATH.'header.inc.php');
182         $msg->printErrors('UNKNOWN');   
183         require(AT_INCLUDE_PATH.'footer.inc.php');
184         exit;
185 }
186 if($_GET['back'] == 1) {
187         $pathext  = substr($pathext, 0, -1);
188         $slashpos = strrpos($pathext, '/');
189         if($slashpos == 0) {
190                 $pathext = '';
191         } else {
192                 $pathext = substr($pathext, 0, ($slashpos+1));
193         }
194
195 }
196
197 $start_at = 2;
198 /* remove the forward or backwards slash from the path */
199 $newpath = $current_path;
200 $depth = substr_count($pathext, '/');
201
202 if ($pathext != '') {
203         $bits = explode('/', $pathext);
204         foreach ($bits as $bit) {
205                 if ($bit != '') {
206                         $bit_path .= $bit;
207
208                         $_section[$start_at][0] = $bit;
209                         $_section[$start_at][1] = '../tools/filemanager/index.php?pathext=' . urlencode($bit_path) . SEP . 'popup=' . $popup . SEP . 'framed=' . $framed;
210
211                         $start_at++;
212                 }
213         }
214         $bit_path = "";
215         $bit = "";
216 }
217
218 /* if upload successful, close the window */
219 if ($f) {
220         $onload = 'closeWindow(\'progWin\');';
221 }
222
223 /* make new directory */
224 if (isset($_POST['mkdir'])) {
225 if ($_POST['mkdir_value'] && ($depth < $MaxDirDepth) ) {
226         $_POST['dirname'] = trim($_POST['dirname']);
227
228         /* anything else should be okay, since we're on *nix..hopefully */
229         $_POST['dirname'] = preg_replace('/[^a-zA-Z0-9._]/', '', $_POST['dirname']);
230
231         if ($_POST['dirname'] == '') {
232                 $msg->addError(array('FOLDER_NOT_CREATED', $_POST['dirname'] ));
233         } 
234         else if (strpos($_POST['dirname'], '..') !== false) {
235                 $msg->addError('BAD_FOLDER_NAME');
236         }       
237         else {
238                 $result = @mkdir($current_path.$pathext.$_POST['dirname'], 0700);
239                 if($result == 0) {
240                         $msg->addError(array('FOLDER_NOT_CREATED', $_POST['dirname'] ));
241                 }
242                 else {
243                         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
244                 }
245         }
246 }
247 }
248 $newpath = substr($current_path.$pathext, 0, -1);
249
250
251 /* open the directory */
252 if (!($dir = @opendir($newpath))) {
253         if (isset($_GET['create']) && ($newpath.'/' == $current_path)) {
254                 @mkdir($newpath);
255                 if (!($dir = @opendir($newpath))) {
256                         require(AT_INCLUDE_PATH.'header.inc.php');
257                         $msg->printErrors('CANNOT_CREATE_DIR');                 
258                         require(AT_INCLUDE_PATH.'footer.inc.php');
259                         exit;
260                 } else {
261                         $msg->addFeedback('CONTENT_DIR_CREATED');
262                 }
263         } else {
264                 require(AT_INCLUDE_PATH.'header.inc.php');
265
266                 $msg->printErrors('CANNOT_OPEN_DIR');
267                 require(AT_INCLUDE_PATH.'footer.inc.php');
268                 exit;
269         }
270 }
271 /*
272 if (isset($_POST['cancel'])) {
273         $msg->addFeedback('CANCELLED');
274 }*/
275 //end top.inc.php
276
277 // upload.php
278 $_SESSION['done'] = 1;
279 $popup = $_REQUEST['popup'];
280 $framed = $_REQUEST['framed'];
281
282 /* get this courses MaxQuota and MaxFileSize: */
283 $sql    = "SELECT max_quota, max_file_size FROM ".TABLE_PREFIX."courses WHERE course_id=$_SESSION[course_id]";
284 $result = mysql_query($sql, $db);
285 $row    = mysql_fetch_array($result);
286 $my_MaxCourseSize       = $row['max_quota'];
287 $my_MaxFileSize = $row['max_file_size'];
288
289         if ($my_MaxCourseSize == AT_COURSESIZE_DEFAULT) {
290                 $my_MaxCourseSize = $MaxCourseSize;
291         }
292         if ($my_MaxFileSize == AT_FILESIZE_DEFAULT) {
293                 $my_MaxFileSize = $MaxFileSize;
294         } else if ($my_MaxFileSize == AT_FILESIZE_SYSTEM_MAX) {
295                 $my_MaxFileSize = megabytes_to_bytes(substr(ini_get('upload_max_filesize'), 0, -1));
296         }
297
298 $path = AT_CONTENT_DIR . $_SESSION['course_id'].'/'.$_POST['pathext'];
299
300 if (isset($_POST['upload'])) {
301
302         if($_FILES['uploadedfile']['name'])     {
303 //              echo 'il file esiste';
304                 $_FILES['uploadedfile']['name'] = trim($_FILES['uploadedfile']['name']);
305                 $_FILES['uploadedfile']['name'] = str_replace(' ', '_', $_FILES['uploadedfile']['name']);
306
307                 $path_parts = pathinfo($_FILES['uploadedfile']['name']);
308                 $ext = $path_parts['extension'];
309
310                 /* check if this file extension is allowed: */
311                 /* $IllegalExtentions is defined in ./include/config.inc.php */
312                 if (in_array($ext, $IllegalExtentions)) {
313                         $errors = array('FILE_ILLEGAL', $ext);
314                         $msg->addError($errors);
315                         header('Location: index.php?pathext='.$_POST['pathext']);
316                         exit;
317                 }
318
319                 /* also have to handle the 'application/x-zip-compressed'  case */
320                 if (   ($_FILES['uploadedfile']['type'] == 'application/x-zip-compressed')
321                         || ($_FILES['uploadedfile']['type'] == 'application/zip')
322                         || ($_FILES['uploadedfile']['type'] == 'application/x-zip')){
323                         $is_zip = true;                                         
324                 }
325
326         
327                 /* anything else should be okay, since we're on *nix.. hopefully */
328                 $_FILES['uploadedfile']['name'] = str_replace(array(' ', '/', '\\', ':', '*', '?', '"', '<', '>', '|', '\''), '', $_FILES['uploadedfile']['name']);
329
330
331                 /* if the file size is within allowed limits */
332                 if( ($_FILES['uploadedfile']['size'] > 0) && ($_FILES['uploadedfile']['size'] <= $my_MaxFileSize) ) {
333
334                         /* if adding the file will not exceed the maximum allowed total */
335                         $course_total = dirsize($path);
336
337                         if ((($course_total + $_FILES['uploadedfile']['size']) <= ($my_MaxCourseSize + $MaxCourseFloat)) || ($my_MaxCourseSize == AT_COURSESIZE_UNLIMITED)) {
338
339                                 /* check if this file exists first */
340                                 if (file_exists($path.$_FILES['uploadedfile']['name'])) {
341                                         /* this file already exists, so we want to prompt for override */
342
343                                         /* save it somewhere else, temporarily first                    */
344                                         /* file_name.time ? */
345                                         $_FILES['uploadedfile']['name'] = substr(time(), -4).'.'.$_FILES['uploadedfile']['name'];
346
347                                         $f = array('FILE_EXISTS',
348                                                                         substr($_FILES['uploadedfile']['name'], 5), 
349                                                                         $_FILES['uploadedfile']['name'],
350                                                                         $_POST['pathext'],
351                                                                         $_GET['popup'],
352                                                                         SEP);
353                                         $msg->addFeedback($f);
354                                 }
355
356                                 /* copy the file in the directory */
357                                 $result = move_uploaded_file( $_FILES['uploadedfile']['tmp_name'], $path.$_FILES['uploadedfile']['name'] );
358
359                                 if (!$result) {
360                                         require(AT_INCLUDE_PATH.'header.inc.php');
361                                         $msg->printErrors('FILE_NOT_SAVED');
362                                         echo '<a href="tools/filemanager/index.php?pathext=' . $_POST['pathext'] . SEP . 'popup=' . $_GET['popup'] . '">' . _AT('back') . '</a>';
363                                         require(AT_INCLUDE_PATH.'footer.inc.php');
364                                         exit;
365                                 } else {
366                                         if ($is_zip) {
367                                                 $f = array('FILE_UPLOADED_ZIP',
368                                                                                 urlencode($_POST['pathext']), 
369                                                                                 urlencode($_FILES['uploadedfile']['name']), 
370                                                                                 $_GET['popup'],
371                                                                                 SEP);
372                                                 //$msg->addFeedback($f);
373                 
374                                         } /* else */
375
376                                         $msg->addFeedback('FILE_UPLOADED');
377                                 }
378                         } else {
379                                 $msg->addError(array('MAX_STORAGE_EXCEEDED', get_human_size($my_MaxCourseSize)));
380                         }
381                 } else {
382                         $msg->addError(array('FILE_TOO_BIG', get_human_size($my_MaxFileSize)));
383                 }
384         } else {
385                 $msg->addError('FILE_NOT_SELECTED');
386         }
387 }
388
389 // end upload.php       
390
391
392 if ($current_tab == 5){
393         if ($_POST['alternatives']==1){
394                 if (isset($_POST['add'])){
395                         if (isset($_POST['resources'])){
396                                 if (isset($_POST['radio_alt'])){
397                                 
398                                         $sql    = "SELECT * FROM ".TABLE_PREFIX."primary_resources WHERE content_id='$cid' and primary_resource_id='$_POST[resources]'";
399                                 $result = mysql_query($sql, $db);
400                         
401                                 if (mysql_num_rows($result) > 0) {
402                                         while ($row = mysql_fetch_assoc($result)) {
403                                                 $sql_contr      = "SELECT * FROM ".TABLE_PREFIX."secondary_resources WHERE primary_resource_id='$row[primary_resource_id]' and secondary_resource='$_POST[radio_alt]'";
404                                                 $contr          = mysql_query($sql_contr, $db);
405                                                 if (mysql_num_rows($contr) > 0) {
406                                                         $msg->addError('ALTERNATIVE_ALREADY_DECLARED');
407                                                 }
408                                                 else {
409                                                         $sql_ins = "INSERT INTO ".TABLE_PREFIX."secondary_resources VALUES (NULL, '$row[primary_resource_id]', '$_POST[radio_alt]', 'en')";
410                                                                 $ins     = mysql_query($sql_ins, $db);
411                                                                 $msg->addFeedback('ALTERNATIVE_ADDED');
412                                                         }
413                                                 }
414                                         }
415                         }
416                                 else 
417                                         $msg->addError('ALTERNATIVE_NOT_DEFINED');
418                         }
419                         else 
420                                 $msg->addError('RESOURCE_NOT_DEFINED');
421                         }
422                 }
423
424                 
425         if ($_REQUEST[act]=='delete')   {
426                 $sql = "DELETE FROM ".TABLE_PREFIX."secondary_resources WHERE secondary_resource_id='$_REQUEST[id_alt]'";
427                 $result = mysql_query($sql,$db);
428                 $sql = "DELETE FROM ".TABLE_PREFIX."secondary_resources_types WHERE secondary_resource_id='$_REQUEST[id_alt]'";
429                 $result = mysql_query($sql,$db);
430                 
431                 $msg->addFeedback('ALTERNATIVE_DELETED');
432         }
433
434 }
435
436
437 if ($current_tab == 5) {
438         if (isset($_POST['save_types_and_language'])){
439                 if(($_POST['alternatives']==1) || ($_GET['alternatives']==1)){
440                         $sql    = "SELECT primary_resource_id FROM ".TABLE_PREFIX."primary_resources WHERE content_id='$cid'";
441                 $result = mysql_query($sql, $db);
442
443                 if (mysql_num_rows($result) > 0) {
444                                 while ($row = mysql_fetch_assoc($result)) {
445                                         $sql_type        = "SELECT * FROM ".TABLE_PREFIX."resource_types";
446                                 $result_type = mysql_query($sql_type, $db);
447                                 
448                                 if (mysql_num_rows($result_type) > 0) {
449                                                 while ($type = mysql_fetch_assoc($result_type)) {
450                                                         $sql_contr  = "SELECT * FROM ".TABLE_PREFIX."primary_resources_types WHERE primary_resource_id='$row[primary_resource_id]' and type_id='$type[type_id]'";
451                                                         $contr          = mysql_query($sql_contr, $db);    
452                                                         if (mysql_num_rows($contr) > 0) {
453                                                                 while ($control = mysql_fetch_assoc($contr)) {
454                                                                         if (isset($_POST['checkbox_'.$type[type].'_'.$row[primary_resource_id].'_primary']))
455                                                                                 continue;
456                                                                         else {
457                                                                                 $sql_del = "DELETE FROM ".TABLE_PREFIX."primary_resources_types WHERE primary_resource_id='$control[primary_resource_id]' and type_id='$control[type_id]'";
458                                                                                 $result_del = mysql_query($sql_del, $db);
459                                                                         }
460                                                                 }       
461                                                         }
462                                                         else {
463                                                                 if (isset($_POST['checkbox_'.$type[type].'_'.$row[primary_resource_id].'_primary'])){
464                                                                         $sql_ins        = "INSERT INTO ".TABLE_PREFIX."primary_resources_types VALUES ($row[primary_resource_id], $type[type_id])";
465                                                                         $ins            = mysql_query($sql_ins, $db);
466                                                                         }       
467                                                                 
468                                                                 $sql_alt        = "SELECT * FROM ".TABLE_PREFIX."secondary_resources WHERE primary_resource_id='$row[primary_resource_id]'";
469                                                         $result_alt = mysql_query($sql_alt, $db);
470                                                 
471                                                                 if (mysql_num_rows($result_alt) > 0) {
472                                                                 while ($alt = mysql_fetch_assoc($result_alt)) {
473                                                                                 $sql_contr  = "SELECT * FROM ".TABLE_PREFIX."secondary_resources_types WHERE secondary_resource_id='$alt[secondary_resource_id]' and type_id='$type[type_id]'";
474                                                                                 $contr  = mysql_query($sql_contr, $db);    
475                                                                                 if (mysql_num_rows($contr) > 0) {
476                                                                                         while ($control = mysql_fetch_assoc($contr)) {
477                                                                                                 if (isset($_POST['checkbox_'.$type[type].'_'.$alt[secondary_resource_id].'_secondary']))
478                                                                                                         continue;
479                                                                                                 else {
480                                                                                                         $sql_del = "DELETE FROM ".TABLE_PREFIX."secondary_resources_types WHERE secondary_resource_id='$control[secondary_resource_id]' and type_id='$control[type_id]'";
481                                                                                                         $result_del = mysql_query($sql_del, $db);
482                                                                                                         }
483                                                                                                 }               
484                                                                                         }
485                                                                                 else {
486                                                                                         if (isset($_POST['checkbox_'.$type[type].'_'.$alt[secondary_resource_id].'_secondary'])){
487                                                                                                 $sql_ins        = "INSERT INTO ".TABLE_PREFIX."secondary_resources_types VALUES ($alt[secondary_resource_id], $type[type_id])";
488                                                                                                 $ins            = mysql_query($sql_ins, $db);
489                                                                                                 }
490                                                                                         $lang=$_POST['lang_'.$alt[secondary_resource_id].'_secondary'];
491                                                                                         $sql_up = "UPDATE ".TABLE_PREFIX."secondary_resources SET language_code='$lang' WHERE secondary_resource_id=$alt[secondary_resource_id]";
492                                                                                         $up     = mysql_query($sql_up, $db);
493
494       
495                                                                                 }       
496                                                                         }                       
497                                                                 }
498                                                 
499                                                                 $lang=$_POST['lang_'.$row[primary_resource_id].'_primary'];
500                                                                 $sql_up = "UPDATE ".TABLE_PREFIX."primary_resources SET language_code='$lang' WHERE primary_resource_id=$row[primary_resource_id]";
501                                                                 $up     = mysql_query($sql_up, $db);
502                                                                 /* Added feedback message after types and language are updated Greg Oct 19/08  
503
504                                                                   */
505                                                                  $msg->addFeedback('RESOURCE_PROPERTIES_UPDATED');
506                                                         }
507                                                 }
508                                         }
509                                 }
510                 }
511                 }
512                 else {
513                         if (isset($_POST['body_text_alt'])){
514                                 $cid_wholepage = $cid.'_wholepage';
515                                 $sql    = "SELECT * FROM ".TABLE_PREFIX."primary_resources WHERE content_id='$cid' and resource='$cid_wholepage'";
516                         $result = mysql_query($sql, $db);
517                                 if (mysql_num_rows($result) > 0){
518                                 while ($row = mysql_fetch_assoc($result)) {
519                                         $sql_type        = "SELECT * FROM ".TABLE_PREFIX."resource_types";
520                                                 $result_type = mysql_query($sql_type, $db);
521                                         
522                                         if (mysql_num_rows($result_type) > 0) {
523                                                         while ($type = mysql_fetch_assoc($result_type)) {
524                                                                 $sql_contr  = "SELECT * FROM ".TABLE_PREFIX."primary_resources_types WHERE primary_resource_id='$row[primary_resource_id]' and type_id='$type[type_id]'";
525                                                                 $contr          = mysql_query($sql_contr, $db);    
526                                                                 if (mysql_num_rows($contr) > 0) {
527                                                                         while ($control = mysql_fetch_assoc($contr)) {
528                                                                                 if (isset($_POST['checkbox_'.$type[type].'_'.$row[primary_resource_id].'_primary']))
529                                                                                         continue;
530                                                                                 else {
531                                                                                         $sql_del = "DELETE FROM ".TABLE_PREFIX."primary_resources_types WHERE primary_resource_id='$control[primary_resource_id]' and type_id='$control[type_id]'";
532                                                                                         $result_del = mysql_query($sql_del, $db);
533                                                                                 }
534                                                                         }       
535                                                                 } else {
536                                                                         if (isset($_POST['checkbox_'.$type[type].'_'.$row[primary_resource_id].'_primary'])){
537                                                                                 $sql_ins        = "INSERT INTO ".TABLE_PREFIX."primary_resources_types VALUES ($row[primary_resource_id], $type[type_id])";
538                                                                                 $ins            = mysql_query($sql_ins, $db);
539                                                                         }
540                                                                 }       
541                                                                 if ($_POST['body_text_alt'] != $_POST['body_text']){
542                                                                 $sql_check_secondary    = "SELECT * FROM ".TABLE_PREFIX."secondary_resources WHERE primary_resource_id='$row[primary_resource_id]'";
543                                                                 $check_secondary                = mysql_query($sql_check_secondary, $db);
544                                                                 if (mysql_num_rows($check_secondary) > 0) {
545                                                                         while ($secondary = mysql_fetch_assoc($check_secondary)) {
546                                                                                 $sql_up = "UPDATE ".TABLE_PREFIX."secondary_resources SET secondary_resource='$_POST[body_text_alt]' WHERE secondary_resource_id=$secondary[secondary_resource_id]";
547                                                                                         $up     = mysql_query($sql_up, $db);
548                                                                                         $msg->addFeedback('ALTERNATIVE_UPDATED');
549                                                                         }       
550                                                                 }else {
551                                                                         $sql_ins = "INSERT INTO ".TABLE_PREFIX."secondary_resources VALUES (NULL, '$row[primary_resource_id]', '$_POST[body_text_alt]', 'en')";
552                                                                                 $ins     = mysql_query($sql_ins, $db);
553                                                                                 $msg->addFeedback('ALTERNATIVE_ADDED');
554                                                                         }
555                                                                         $sql_alt        = "SELECT * FROM ".TABLE_PREFIX."secondary_resources WHERE primary_resource_id='$row[primary_resource_id]'";
556                                                                 $result_alt = mysql_query($sql_alt, $db);
557                                                 
558                                                                         if (mysql_num_rows($result_alt) > 0) {
559                                                                         while ($alt = mysql_fetch_assoc($result_alt)) {
560                                                                                         $sql_contr  = "SELECT * FROM ".TABLE_PREFIX."secondary_resources_types WHERE secondary_resource_id='$alt[secondary_resource_id]' and type_id='$type[type_id]'";
561                                                                                         $contr  = mysql_query($sql_contr, $db);    
562                                                                                         if (mysql_num_rows($contr) > 0) {
563                                                                                                 while ($control = mysql_fetch_assoc($contr)) {
564                                                                                                         if (isset($_POST['checkbox_'.$type[type].'_'.$alt[secondary_resource_id].'_secondary']))
565                                                                                                                 continue;
566                                                                                                         else {
567                                                                                                                 $sql_del = "DELETE FROM ".TABLE_PREFIX."secondary_resources_types WHERE secondary_resource_id='$control[secondary_resource_id]' and type_id='$control[type_id]'";
568                                                                                                                 $result_del = mysql_query($sql_del, $db);
569                                                                                                         }
570                                                                                                 }               
571                                                                                         } else {
572                                                                                                 if (isset($_POST['checkbox_'.$type[type].'_'.$alt[secondary_resource_id].'_secondary'])){
573                                                                                                         $sql_ins        = "INSERT INTO ".TABLE_PREFIX."secondary_resources_types VALUES ($alt[secondary_resource_id], $type[type_id])";
574                                                                                                         $ins            = mysql_query($sql_ins, $db);
575                                                                                                 }
576                                                                                                 $lang   = $_POST['lang_'.$alt[secondary_resource_id].'_secondary'];
577                                                                                                 $sql_up = "UPDATE ".TABLE_PREFIX."secondary_resources SET language_code='$lang' WHERE secondary_resource_id='$alt[secondary_resource_id]'";
578                                                                                                 $up     = mysql_query($sql_up, $db);
579
580                                                                                         }
581                                                                                 $lang=$_POST['lang_'.$row[primary_resource_id].'_primary'];
582                                                                                         $sql_up = "UPDATE ".TABLE_PREFIX."primary_resources SET language_code='$lang' WHERE primary_resource_id='$row[primary_resource_id]'";
583                                                                                         $up     = mysql_query($sql_up, $db);
584
585                                                                                 }
586                                                                         }
587                                                                 }
588                                                         }
589                                                 }
590                                         }
591                                 }
592                         }
593             }
594         }
595 }               
596
597 //End Added by Silvia 
598
599 //Extended Test within content functionality - Harris @Sep 9, 2008
600 //if ($current_tab == 6){
601 //}
602
603 if (($current_tab == 0) || ($_current_tab == 5)) {
604         if (!isset($_REQUEST['setvisual']) && !isset($_REQUEST['settext'])) {
605                 if ($_SESSION['prefs']['PREF_CONTENT_EDITOR'] == 1) {
606                         $_POST['formatting'] = 1;
607                         $_REQUEST['settext'] = 0;
608                         $_REQUEST['setvisual'] = 0;
609
610                 } else if ($_SESSION['prefs']['PREF_CONTENT_EDITOR'] == 2) {
611                         $_POST['formatting'] = 1;
612                         $_POST['settext'] = 0;
613                         $_POST['setvisual'] = 1;
614
615                 } else { // else if == 0
616                         $_POST['formatting'] = 0;
617                         $_REQUEST['settext'] = 0;
618                         $_REQUEST['setvisual'] = 0;
619                 }
620         }
621         if ((!$_POST['setvisual'] && $_POST['settext']) || !$_GET['setvisual']){
622                 $onload = "document.form.ctitle.focus(); ";
623         }
624 }
625
626 // initialize buttons, texts, radio buttons for editor
627 if ($current_tab == 0) 
628 {
629         $onload.="on_load();";
630 }
631
632 if ($current_tab == 5) 
633 {
634         $onload.="on_load();";
635 }
636
637 require(AT_INCLUDE_PATH.'header.inc.php');
638
639 if ($current_tab == 0) 
640 {
641         load_editor(false, "none");
642 }
643
644 if ($current_tab == 5) 
645 {
646         load_editor(false, "none");
647 }
648
649
650 //TODO*************BOLOGNA****************REMOVE ME**************/
651 //loading toolbar for insert discussion topic or web link into the content
652 if ($current_tab == 0){
653     if(authenticate(AT_PRIV_CONTENT,AT_PRIV_RETURN)){
654         $home_links = get_home_navigation();                        //vengono lette le caratteristiche di ogni modulo attivato nella home page.
655         $main_links = get_main_navigation($current_page);           //vengono lette le caratteristiche di ogni modulo attivo nel main navigation
656
657         $num = count($main_links);                                  //necessario elminare il primo e l'utlimo elemento poichè sono rispettivamente "Home" e "Manage"
658         unset($main_links[0]);                                      //"Home" label
659         unset($main_links[$num-1]);                                 //"Manage" label
660
661         $all_tools = $home_links;                                   //$all_tools represent a merge between $home_links and main_links without repetitions.
662         $check=false;
663         foreach($main_links as $main) {
664             foreach($home_links as $home) {
665                 if($home['title'] == $main['title']) {
666                     $check=true;
667                     break;
668                 }
669             }
670             if(!$check)
671                 $all_tools[]=$main;
672             else
673                 $check=false;
674         }
675     }
676 }
677
678
679 $cid = intval($_REQUEST['cid']);
680 $pid = intval($_REQUEST['pid']);
681
682 ?>
683 <form action="<?php echo $_SERVER['PHP_SELF']; ?>?cid=<?php echo $cid; ?>" method="post" name="form" enctype="multipart/form-data">
684 <?php
685
686         if ($cid) {
687                 //$content_row = sql_quote($content_row);
688                 if (isset($_POST['current_tab'])) {
689                         //$changes_made = check_for_changes($content_row);
690                 } else {
691                         $changes_made = array();
692
693                         $_POST['formatting'] = $content_row['formatting'];
694                         $_POST['head'] = $content_row['head'];
695                         $_POST['use_customized_head'] = $content_row['use_customized_head'];
696                         $_POST['title']      = $content_row['title'];
697                         $_POST['body_text']  = $content_row['text'];
698                         $_POST['keywords']   = $content_row['keywords'];
699                         $_POST['test_message'] = $content_row['test_message'];
700                         $_POST['allow_test_export'] = $content_row['allow_test_export'];
701
702                         $_POST['day']   = substr($content_row['release_date'], 8, 2);
703                         $_POST['month'] = substr($content_row['release_date'], 5, 2);
704                         $_POST['year']  = substr($content_row['release_date'], 0, 4);
705                         $_POST['hour']  = substr($content_row['release_date'], 11, 2);
706                         $_POST['min']= substr($content_row['release_date'], 14, 2);
707
708                         $_POST['ordering'] = $content_row['ordering'];
709                         $_POST['related'] = $contentManager->getRelatedContent($cid);
710                         
711                         $_POST['pid'] = $pid = $content_row['content_parent_id'];
712
713                         $_POST['related_term'] = $glossary_ids_related;
714                 }
715
716         } else {
717                 $cid = 0;
718                 if (!isset($_POST['current_tab'])) {
719                         $_POST['day']  = date('d');
720                         $_POST['month']  = date('m');
721                         $_POST['year'] = date('Y');
722                         $_POST['hour'] = date('H');
723                         $_POST['min']  = 0;
724
725                         if (isset($_GET['pid'])) {
726                                 $pid = $_POST['pid'] = intval($_GET['pid']);
727                                 $_POST['ordering'] = count($contentManager->getContent($pid))+1;
728                         } else {
729                                 $_POST['pid'] = 0;
730                                 $_POST['ordering'] = count($contentManager->getContent(0))+1;
731                         }
732                 }
733         }
734
735         echo '<input type="hidden" name="cid" value="'.$cid.'" />';
736         echo '<input type="hidden" name="title" value="'.htmlspecialchars($stripslashes($_POST['title'])).'" />';
737         if ($_REQUEST['sub'] == 1)
738         {
739                 echo '<input type="hidden" name="sub" value="1" />';
740                 echo '<input type="hidden" name="folder_title" value="'.htmlspecialchars($stripslashes($_POST['folder_title'])).'" />';
741         }
742         echo '<input type="submit" name="submit" style="display:none;"/>';
743         if (($current_tab != 0) && (($_current_tab != 5))) {
744                 echo '<input type="hidden" name="body_text" value="'.htmlspecialchars($stripslashes($_POST['body_text'])).'" />';
745                 echo '<input type="hidden" name="head" value="'.htmlspecialchars($stripslashes($_POST['head'])).'" />';
746                 echo '<input type="hidden" name="use_customized_head" value="'.(($_POST['use_customized_head']=="") ? 0 : $_POST['use_customized_head']).'" />';
747                 echo '<input type="hidden" name="displayhead" value="'.$_POST['displayhead'].'" />';
748                 echo '<input type="hidden" name="setvisual" value="'.$_POST['setvisual'].'" />';
749                 echo '<input type="hidden" name="settext" value="'.$_POST['settext'].'" />';            
750                 echo '<input type="hidden" name="formatting" value="'.$_POST['formatting'].'" />';
751         }
752
753         echo '<input type="hidden" name="ordering" value="'.$_POST['ordering'].'" />';
754         echo '<input type="hidden" name="pid" value="'.$pid.'" />';
755
756         echo '<input type="hidden" name="day" value="'.$_POST['day'].'" />';
757         echo '<input type="hidden" name="month" value="'.$_POST['month'].'" />';
758         echo '<input type="hidden" name="year" value="'.$_POST['year'].'" />';
759         echo '<input type="hidden" name="hour" value="'.$_POST['hour'].'" />';
760         echo '<input type="hidden" name="minute" value="'.$_POST['minute'].'" />';
761         echo '<input type="hidden" name="min" value="'.$_POST['min'].'" />';
762         
763         echo '<input type="hidden" name="alternatives" value="'.$_POST['alternatives'].'" />';
764         
765         echo '<input type="hidden" name="current_tab" value="'.$current_tab.'" />';
766
767         if (is_array($_POST['related']) && ($current_tab != 1)) {
768                 foreach($_POST['related'] as $r_id) {
769                         echo '<input type="hidden" name="related[]" value="'.$r_id.'" />';
770                 }
771         }
772         echo '<input type="hidden" name="keywords" value="'.htmlspecialchars(stripslashes($_POST['keywords'])).'" />';
773
774         //content test association
775         echo '<input type="hidden" name="test_message" value="'.$_POST['test_message'].'" />';
776         
777         /* get glossary terms */
778         $matches = find_terms(stripslashes($_POST['body_text']));
779         $num_terms = count($matches[0]);
780         $matches = $matches[0];
781         $word = str_replace(array('[?]', '[/?]'), '', $matches);
782
783         if (is_array($word)) {
784                 /* update $_POST['glossary_defs'] with any new/changed terms */
785                 for($i=0; $i<$num_terms; $i++) {
786                         $word[$i] = urlencode($word[$i]);
787                         if (!isset($_POST['glossary_defs'][$word[$i]])) {
788                                 $_POST['glossary_defs'][$word[$i]] = $glossary[$word[$i]];
789                         }
790                 }
791         }
792
793         if (is_array($_POST['glossary_defs']) && ($current_tab != 2)) {
794                 foreach($_POST['glossary_defs'] as $w => $d) {
795                         /* this term still exists in the content */
796                         if (!in_array($w, $word)) {
797                                 unset($_POST['glossary_defs'][$w]);
798                                 continue;
799                         }
800                         echo '<input type="hidden" name="glossary_defs['.$w.']" value="'.htmlspecialchars(stripslashes($d)).'" />';
801                 }
802                 if (isset($_POST['related_term'])) {
803                         foreach($_POST['related_term'] as $w => $d) {
804                                 echo '<input type="hidden" name="related_term['.$w.']" value="'.$d.'" />';
805                         }
806                 }
807         }
808
809         //tests
810         if (is_array($_POST['tid']) && $current_tab != 6){
811                 /* Test & Survey --> Other tabs triggers this condition */
812                 foreach ($_POST['tid'] as $i=>$tid){
813                         echo '<input type="hidden" name="tid['.$i.']" value="'.$tid.'" />';
814                 }
815         } elseif ($current_tab != 6){
816                 /* Edit Content (On Edit content tab), without clicking Test & Survey */
817                 $i = 0;
818                 if ($content_test){
819                         while ($content_test_row = mysql_fetch_assoc($content_test)){
820                                 echo '<input type="hidden" name="tid['.$i++.']" value="'.$content_test_row['test_id'].'" />';
821                         }
822                 }
823         }
824         if (!isset($_POST['allow_test_export']) && $current_tab != 6) {
825                 //export flag handling.
826                 $sql = "SELECT `allow_test_export` FROM ".TABLE_PREFIX."content WHERE content_id=$_REQUEST[cid]";
827                 $result2 = mysql_query($sql, $db);
828                 if ($result2){
829                         $c_row = mysql_fetch_assoc($result2);
830                 }
831                 if (intval($c_row['allow_test_export'])==1){
832                         echo '<input type="hidden" name="allow_test_export" value="1" />';
833                 } else {
834                         echo '<input type="hidden" name="allow_test_export" value="0" />';
835                 }
836         } else {
837                 echo '<input type="hidden" name="allow_test_export" value="'.intval($_POST['allow_test_export']).'" />';
838         }
839
840         if ($do_check) {
841                 $changes_made = check_for_changes($content_row);
842         }
843
844 ?>
845 <div align="center">
846         <?php output_tabs($current_tab, $changes_made); ?>
847 </div>
848 <div class="input-form" style="width: 95%">
849
850         <?php if ($changes_made): ?>
851                 <div class="unsaved">
852                         <span style="color:red;"><?php echo _AT('save_changes_unsaved'); ?></span> 
853                         <input type="submit" name="submit" value="<?php echo _AT('save'); ?>" title="<?php echo _AT('save_changes'); ?> alt-s" accesskey="s" style="border: 1px solid red;" /> 
854                         <input type="submit" name="close" class="button green" value="<?php echo _AT('close'); ?>" />  <input type="checkbox" id="close" name="save_n_close" value="1" <?php if ($_SESSION['save_n_close']) { echo 'checked="checked"'; } ?> />
855                         <label for="close"><?php echo _AT('close_after_saving'); ?></label>
856                 </div>
857
858         <?php else: ?>
859                 <div class="saved">
860                         <?php //if ($cid) { echo _AT('save_changes_saved'); } ?> <input type="submit" name="submit" value="<?php echo _AT('save'); ?>" title="<?php echo _AT('save_changes'); ?> alt-s" accesskey="s" /> <input type="submit" name="close" value="<?php echo _AT('close'); ?>" /> <input type="checkbox" style="border:0px;" id="close" name="save_n_close" value="1" <?php if ($_SESSION['save_n_close']) { echo 'checked="checked"'; } ?> /><label for="close"><?php echo _AT('close_after_saving'); ?></label>
861                 </div>
862         <?php endif; ?>
863         <?php 
864         //      if ($current_tab != 5){
865                                 include(AT_INCLUDE_PATH.'html/editor_tabs/'.$tabs[$current_tab][1]);
866                                 echo '</div></form>';
867         //      }
868         //      else 
869         //      {
870         //              echo '</div></form>';
871         //              include(AT_INCLUDE_PATH.'html/editor_tabs/'.$tabs[$current_tab][1]);
872                         
873         //      }
874                         
875 ?>
876
877 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>