b944fe1bdd09ac98f6e50f2565a6e2ae9c12a3fb
[atutor.git] / mods / atalker / atalkerlib.inc.php
1 <?php
2 /****************************************************************/
3 /* ATalker                                                                                                      */
4 /****************************************************************/
5 /* Copyright (c) 2002-2005 by Greg Gay                                  */
6 /* http://atutor.ca                                                                                             */
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.                                */
11 /****************************************************************/
12 // $Id: atalkerlib.inc.php 5123 2005-07-12 14:59:03Z greg
13
14 // Common functions etc used thoughout ATalker
15
16
17 // Setup the speech directories if they don't yet exist
18 if($_SESSION['privileges'] == AT_ADMIN_PRIV_ADMIN){
19
20         // where admin audio files are saved
21         define('AT_SPEECH_TEMPLATE_ROOT', AT_CONTENT_DIR.'template/');
22         define('AT_SPEECH_TEMPLATE_DIR', AT_CONTENT_DIR.'template/'.$_SESSION['lang'].'/');
23         define('AT_SPEECH_TEMPLATE_URL', $_base_href.'content/template/'.$_SESSION['lang'].'/');
24         define('AT_SPEECH_FILES_DIR', AT_CONTENT_DIR.'template/temp/'); 
25         define('AT_SPEECH_URL', $_base_href.'content/template/temp/');
26         define('AT_SPEECH_DIR', AT_CONTENT_DIR.'template/temp/');
27
28         // See if the speech directories exists yet, and create them if they don't      
29         if(!opendir(AT_SPEECH_TEMPLATE_ROOT)){
30                         mkdir(AT_SPEECH_TEMPLATE_ROOT, 0755);
31         }
32         if(!opendir(AT_SPEECH_DIR)){
33                         mkdir(AT_SPEECH_DIR, 0755);
34         }
35         
36         if(!opendir(AT_SPEECH_FILES_DIR)){
37                                 mkdir(AT_SPEECH_FILES_DIR, 0755);
38         }
39         
40         if(!opendir(AT_SPEECH_TEMPLATE_DIR)){
41                         mkdir(AT_SPEECH_TEMPLATE_DIR, 0755);
42         }        
43
44 }else{
45         define('AT_SPEECH_DIR', AT_CONTENT_DIR.'speech/');
46         define('AT_SPEECH_FILES_DIR', AT_CONTENT_DIR.$_SESSION['course_id'].'/speech/'); 
47         define('AT_SPEECH_URL', $_base_href.'content/speech/');
48         // See if the speech directories exists yet, and create them if they don't      
49         if(@!opendir(AT_SPEECH_DIR)){
50                         mkdir(AT_SPEECH_DIR, 0755);
51         }
52         if($_SESSION['course_id'] != "0"){
53                 if(@!opendir(AT_SPEECH_FILES_DIR)){
54                                 mkdir(AT_SPEECH_FILES_DIR, 0755);
55                         }
56         }
57
58 }
59
60 //Validate filename form field
61 if($_POST['filename']){
62         if (!(eregi("^[a-zA-Z0-9_]([a-zA-Z0-9_\.])*$", $_POST['filename']))) {
63                                 $error = "TTS_FILE_CHARS";
64                                 $msg->addError($error);
65         }
66 }
67
68 if($_GET['postdata']){
69         $postdata = stripslashes($_GET['postdata']);
70         $_POST = unserialize($postdata);
71         
72  }else{
73                         
74         $postdata  = serialize($_POST);
75 }
76
77 // Garbage collector: delete tempfiles after $filelife seconds
78  function clean_tts_files(){
79         //global AT_SPEECH_DIR;
80         $filelife = "1200"; //1200 seconds = 20 minutes
81         if ($handle = opendir(AT_SPEECH_DIR)){
82                 while (false !== ($file = readdir($handle))) {
83                         $rawfile = split("\.", $file);
84                         if($rawfile[0] != ''){
85                                 $dir_files[$i] .= "$rawfile[0]\n";
86                                 $this_now = time();
87                                 if($this_now - $rawfile[0] > $filelife){
88                                         unlink(AT_SPEECH_DIR.$file);
89                                 }
90                         $i++;
91                         }
92                 }
93         }
94  }
95 clean_tts_files();
96
97 // Manage ATalker tabs and popup window
98         if ((isset($_REQUEST['popup']))  &&  ($_REQUEST['popup'] == TRUE)) {
99                 $popup = TRUE;
100                 $popup_win = "popup=1";
101         } 
102
103
104 // Read feedback messages out load
105
106 /**
107 *       Create scm file to pass to text2wave to define the voice, the volumn and the speed of the wave to build
108         @access private
109         @author Eura Ercolani
110         @return The complete path of the generated file
111 **/
112
113 function build_scm_file()
114 {
115         global $db;
116         
117         //get voice info from the table AT_languages and AT_voices
118         $sql_command = "SELECT voice_name, voice_volumn, voice_speed FROM ".TABLE_PREFIX."languages, ".TABLE_PREFIX."voices WHERE language_code='";
119         $sql_command .= $_SESSION[lang]."' AND ".TABLE_PREFIX."languages.voice_id = ".TABLE_PREFIX."voices.voice_id";
120         $result = mysql_query($sql_command, $db);
121         $row = mysql_fetch_row($result);
122         $voice_name = $row[0]; //name of the voice to use
123         $volumn = $row[1]; //range value between 1(low) and 10 (high)
124         $speed = $row[2]; //range value between 0.6 (very fast) and 2.0 (very slow)
125         $scheme_file_txt .= "(";
126         $scheme_file_txt .= $voice_name;
127         $scheme_file_txt .= ")\n";
128         $scheme_file_txt .= "(";
129         $scheme_file_txt .= "Parameter.set 'Duration_Stretch ".$speed;
130         $scheme_file_txt .= ")";
131         
132         //Define the scm file name
133         $now = time();
134         $scheme_file_name = AT_MSGS_DIR.$_SESSION[lang].DIRECTORY_SEPARATOR.$now.'.scm';
135         //Open the file for output
136         $fp = fopen($scheme_file_name,'w');
137         if (!$fp) 
138         {
139                 echo _AT(AT_ERROR_TTS__NOT_CREATE_SCHEME);
140                 exit;
141         }
142         //Write into the file
143         fputs($fp, $scheme_file_txt);
144         fclose($fp);
145         
146         $voice_info[0] = $scheme_file_name;
147         $voice_info[1] = $volumn;
148         
149         return $voice_info;
150          
151 }
152
153 /**
154 *  Reads aloud  error and feedback messages
155 *  @ access  public
156 *  @param array $messages      a list of messages sent to the  $msg->printAll() function; 
157 *  @param  array $vals                a list of subistute (i.e.for %s within language) values for dynamic messages
158 *  @author  Greg Gay
159 */
160
161
162 function read_messages($messages, $vals){
163         global $_base_href, $course_base_href, $msg, $play, $val, $db;
164         /* Modified by Eura Ercolani: mimetype support - BEGIN */
165         
166         /* Modified by Eura Ercolani: mimetype support - END */
167         foreach ($messages as $item){
168                 $sql = "SELECT * from ".TABLE_PREFIX."language_text WHERE language_code = '$_SESSION[lang]' AND term = '$item'";
169                 $result = mysql_query($sql, $db);
170                 
171                 while($row = mysql_fetch_row($result)){
172
173                         /* Modified by Eura Ercolani: messages localization - BEGIN */
174                         //check to see if the folder exists....
175                         if(!is_dir(AT_MSGS_DIR.DIRECTORY_SEPARATOR.$_SESSION[lang]))//folder does not exists, I make it
176                                 mkdir(AT_MSGS_DIR.$_SESSION[lang]);
177                         //$file_in =  AT_MSGS_DIR.$row[2].'.txt';
178                         $file_in =  AT_MSGS_DIR.$_SESSION[lang].DIRECTORY_SEPARATOR.$row[2].'.txt';
179                         //$file_out =  AT_MSGS_DIR.$row[2].'.wav';
180                         $file_out =  AT_MSGS_DIR.$_SESSION[lang].DIRECTORY_SEPARATOR.$row[2].'.wav';
181                         //$file_out_mp3 = AT_MSGS_DIR.$row[2].'.mp3';
182                         $file_out_mp3 = AT_MSGS_DIR.$_SESSION[lang].DIRECTORY_SEPARATOR.$row[2].'.mp3';
183                         //$file_recieve = AT_MSGS_URL.$row[2].'.mp3';
184                         $file_recieve = AT_MSGS_URL.$_SESSION[lang].DIRECTORY_SEPARATOR.$row[2].'.mp3';
185                         /* Modified by Eura Ercolani: messages localization - END */
186
187                         if(file_exists($file_out_mp3)){
188
189                                 /* Modified by Eura Ercolani: mime type support - BEGIN */
190                                 //echo  '<embed src="'.$file_recieve.'" autostart="true" hidden="true" volumn="10" ></embed>';                  
191                                 echo  '<embed src="'.$file_recieve.'" autostart="true" height="0" width="0" volumn="10" type="'.$_SESSION['mp3HiddenMimeType'].'"></embed>';
192                                 /* Modified by Eura Ercolani: mime type support - END */
193                                 
194
195                         }else{
196                                 $fp = fopen($file_in,'w');
197
198                                 if (!$fp) {
199                                         echo AT_ERROR_TTS_NOT_CREATE_TEXT;
200                                         exit;
201                                 }
202
203                                 $message = strip_tags($row[3]);
204                                 $message = str_replace("%s", $vals[$row[2]], $message);
205                                 fputs($fp, $message);
206                                 fclose($fp);
207                                 /* Modified by Eura Ercolani: voice setting - BEGIN */
208                                 $voice_info = build_scm_file();
209                                 $command = "text2wave ".$file_in." -o ".$file_out;
210                                 $command .= " -F 48000 -scale ".$voice_info[1]." -eval ".$voice_info[0]; 
211                                 //$command = "text2wave $file_in -o $file_out -F 48000";
212                                 /* Modified by Eura Ercolani: voice setting - BEGIN */
213         
214                                 if(shell_exec('lame --longhelp')){
215
216                                         $command2 .= ' lame --quiet '.$file_out.' '. $file_out_mp3;
217
218                                 }else if (shell_exec('bladeenc -h')) {
219
220                                         $command2 .= ' bladeenc -quiet '.$file_out.' '. $file_out_mp3;  
221                                 }
222                                 
223                                 escapeshellcmd($command);
224                                 escapeshellcmd($command2);
225                                 passthru($command);
226
227                                 passthru($command2);
228                                 /* Modified by Eura Ercolani: mimetype support - BEGIN */
229                                 //echo '<embed src="'.$file_recieve.'" autostart="true" hidden="true"  volumn="10" ></embed>';  
230                                 echo '<embed src="'.$file_recieve.'" autostart="true" height="0" width="0"  volumn="10" type="'.$_SESSION['mp3HiddenMimeType'].'"></embed>';    
231                                 /* Modified by Eura Ercolani: mimetype support - END */
232                                 unlink($file_in);       
233                                 unlink($file_out);
234                                 /* Modified by Eura Ercolani: delete scm file - BEGIN */
235                                 unlink($voice_info[0]);
236                                 /* Modified by Eura Ercolani: delete scm file - END */
237                         }
238                 }
239         }
240 }
241
242
243 // List tabs for the ATalker 
244 function get_atalker_tabs() {
245         //these are the _AT(x) variable names and their include file
246         $tabs[0] = array('text_reader',   'index.php', '');
247         $tabs[1] = array('sable_reader', 'index.php', '');
248         if($_SESSION['privileges'] == AT_ADMIN_PRIV_ADMIN){
249                 $tabs[2] = array('voice_files', 'index.php', '');
250         }
251         
252 return $tabs;
253 }
254         
255
256 // Check to see what encoders are available: currently supported encoders include: lame. bladeenc, oggenc
257
258 function get_encoders(){
259         global $select, $_POST;
260
261         $command2 = 'bladeenc -h';
262         $command2 = escapeshellcmd($command2);
263         $command3 = 'lame --version';
264         $command3 = escapeshellcmd($command3);
265         if(shell_exec($command2) != '' || shell_exec($command3) != ''){
266                 echo '<option value="mp3"';
267                 if($_POST['file_type'] == 'mp3'){ 
268                         echo $select; 
269                 }
270                 echo '>MP3</option>';   
271         }
272
273         $command = 'oggenc --version';
274         $command = escapeshellcmd($command);
275         if(shell_exec($command) != ''){
276                 echo '<option value="ogg"';
277                 if($_POST['file_type'] == 'ogg'){ 
278                         echo $select; 
279                 }
280                 echo '>OGG</option>';   
281                 }
282
283 }
284
285 // Send the user to the right place after each TTS action
286 function gen_tts(){
287         global $_POST, $now, $file_recieve, $error, $postdata, $msg, $file_save, $filename, $tab, $voice_file, $lang_var, $messages;
288         //echo $voice_file;
289         //exit;
290         if(!$error && !$_GET['page']){
291                 if($_POST['download']){
292                         //unset($_POST['export'])
293                         header('Content-type: audio/x-'.$_POST['file_type']);
294                         header('Content-Disposition: attachment; filename="'.$now.'.'.$_POST['file_type']);
295                         readfile($file_recieve);
296                         
297                 }else if($_POST['save']){
298                         $file_save = str_replace(" ", "_", $file_save);
299
300
301                         if(@fopen($file_save, r)){
302                                 $error=  array(TTS_FILE_EXISTS, $filename);
303                                 $msg->addError($error);
304                         }else{
305                                 if(copy($file_recieve, $file_save)){
306                                         $feedback =  array(TTS_FILE_SAVED, $filename);
307                                         $msg->addFeedback($feedback);
308                                 }else{
309                                         $error =  array(TTS_FILE_SAVE_FAILED, $filename);
310                                         $msg->addError($error);
311                                 }
312                         }
313                         unset($_POST['save']);
314                 }else if($_POST['export']){
315                         unset($_POST['export']);
316                         header('Content-type: text/plain');
317                         header("Location:".AT_SPEECH_URL.$now.".sable");
318                 
319                 }else{
320                 
321                         //header('Content-type: audio/x-'.$_POST['file_type']);
322                         header('Content-type: audio/x-'.$_POST['file_type']);
323                         header("Location:".$file_recieve);
324                 }
325         }
326 }
327
328 ?>