hotpotatoes.lib.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <?php
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) Istvan Mandak
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  17. ==============================================================================
  18. */
  19. /**
  20. ==============================================================================
  21. * Code library for HotPotatoes integration.
  22. *
  23. * @author Istvan Mandak
  24. * @package dokeos.exercise
  25. ==============================================================================
  26. */
  27. $dbTable = Database::get_course_table(DOCUMENT_TABLE);
  28. /**
  29. * Creates a hotpotato directory
  30. *
  31. * If a directory of that name already exists, don't create any. If a file of that name exists, remove it and create a directory
  32. * @param string Wanted path
  33. * @return boolean Always true so far
  34. */
  35. function hotpotatoes_init($baseWorkDir)
  36. {
  37. //global $_course, $_uid;
  38. $documentPath=$baseWorkDir.'/';
  39. if (!is_dir($documentPath))
  40. {
  41. if (is_file($documentPath))
  42. {
  43. @unlink($documentPath);
  44. }
  45. @mkdir($documentPath);
  46. return true;
  47. }else{
  48. //if this directory already exists, return false
  49. return false;
  50. }
  51. //why create a .htaccess here?
  52. //if (!is_file($documentPath.".htacces"))
  53. //{
  54. // if (!($fp = fopen($documentPath.".htaccess", "w"))) {
  55. // }
  56. // $str = "order deny,allow\nallow from all";
  57. // if (!fwrite($fp,$str)) { }
  58. //}
  59. }
  60. /**
  61. * Gets the title of the quizz file given as parameter
  62. * @param string File name
  63. * @param string File path
  64. * @return string The exercise title
  65. */
  66. function GetQuizName($fname,$fpath)
  67. {
  68. $title = "";
  69. $title = GetComment($fname);
  70. if($title=="")
  71. {
  72. if (!($fp = fopen($fpath.$fname, "r"))) {
  73. //die("could not open Quiz input");
  74. return GetFileName($fname);
  75. }
  76. $contents = fread($fp, filesize($fpath.$fname));
  77. fclose($fp);
  78. $contents = strtolower($contents);
  79. $pattern = array ( 1 => "title>", 2 => "/title>");
  80. $s_contents = substr($contents,0,strpos($contents,$pattern["2"])-1);
  81. $e_contents = substr($s_contents,strpos($contents,$pattern["1"])+strlen($pattern["1"]),strlen($s_contents));
  82. $title = $e_contents;
  83. }
  84. return $title;
  85. }
  86. /**
  87. * Gets the comment about a file from the corresponding database record
  88. * @param string File path
  89. * @return string Comment from the database record
  90. */
  91. function GetComment($path)
  92. {
  93. global $dbTable;
  94. $query = "select comment from $dbTable where path='$path'";
  95. $result = api_sql_query($query,__FILE__,__LINE__);
  96. while($row = mysql_fetch_array($result))
  97. {
  98. return $row[0];
  99. }
  100. return "";
  101. }
  102. /**
  103. * Sets the comment in the database for a particular path
  104. * @param string File path
  105. * @param string Comment to set
  106. * @return string Result of the database operation (api_sql_query will output some message directly on error anyway)
  107. */
  108. function SetComment($path,$comment)
  109. {
  110. global $dbTable;
  111. $query = "update $dbTable set comment='$comment' where path='$path'";
  112. $result = api_sql_query($query,__FILE__,__LINE__);
  113. return "$result";
  114. }
  115. /**
  116. * Get the name of the file from a path (without the extension)
  117. *
  118. * This assumes the path is made of elements split by '/', not '\' or '\\'
  119. * @param string Path
  120. * @return string File name
  121. */
  122. function GetFileName($fname)
  123. {
  124. $name = explode('/',$fname);
  125. $name = $name[sizeof($name)-1];
  126. return $name;
  127. }
  128. /**
  129. * Reads the file contents into a string
  130. * @param string Urlencoded path
  131. * @return string The file contents
  132. */
  133. function ReadFileCont($full_file_path)
  134. {
  135. if (!($fp = fopen(urldecode($full_file_path), "r"))) {
  136. // if (!($fp = fopen($full_file_path, "r"))) {
  137. return "";
  138. }
  139. $contents = fread($fp, filesize($full_file_path));
  140. fclose($fp);
  141. return $contents;
  142. }
  143. /**
  144. * Writes the file contents into the file given
  145. * @param string Urlencoded path
  146. * @param string The file contents
  147. */
  148. function WriteFileCont($full_file_path,$content)
  149. {
  150. if (!($fp = fopen(urldecode($full_file_path), "w"))) {
  151. //die("could not open Quiz input");
  152. }
  153. fwrite($fp,$content);
  154. fclose($fp);
  155. }
  156. /**
  157. * Gets the name of an img whose path is given (without directories or extensions)
  158. * @param string An image tag (<img src="...." ...>)
  159. * @return string The image file name or an empty string
  160. * @uses GetFileName No comment
  161. */
  162. function GetImgName($imgtag)
  163. { // select src tag from img tag
  164. $match = array();
  165. //preg_match('/(src=(["\'])1.*(["\'])1)/i',$imgtag,$match); //src
  166. preg_match('/src(\s)*=(\s)*[\'"]([^\'"]*)[\'"]/i',$imgtag,$match); //get the img src as contained between " or '
  167. //list($key,$srctag)=each($match);
  168. $src=$match[3];
  169. //$src = substr($srctag,5,(strlen($srctag)-7));
  170. if (stristr($src,"http")===false) // valid or invalid image name
  171. {
  172. if($src=="")
  173. { return ""; }
  174. else
  175. {
  176. $tmp_src = GetFileName($src) ;
  177. if ($tmp_src == "")
  178. {
  179. return $src;
  180. } else {
  181. return $tmp_src;
  182. }
  183. }
  184. }
  185. else //the img tag contained "http", which means it is probably external. Ignore it.
  186. {
  187. return "";
  188. }
  189. }
  190. /**
  191. * Gets the source path of an image tag
  192. * @param string An image tag
  193. * @return string The image source or ""
  194. */
  195. function GetSrcName($imgtag)
  196. { // select src tag from img tag
  197. $match = array();
  198. preg_match("|(src=\".*\" )|U",$imgtag,$match); //src
  199. list($key,$srctag)=each($match);
  200. $src = substr($srctag,5,(strlen($srctag)-7));
  201. if (stristr($src,"http")==false) // valid or invalid image name
  202. {
  203. return $src;
  204. }
  205. else
  206. { return ""; }
  207. }
  208. /**
  209. * Gets the image parameters from an image path
  210. * @param string File name
  211. * @param string File path
  212. * @param reference Reference to a list of image parameters (emptied, then used to return results)
  213. * @param reference Reference to a counter of images (emptied, then used to return results)
  214. */
  215. function GetImgParams($fname,$fpath,&$imgparams,&$imgcount)
  216. { //select img tags from context
  217. $imgparams = array();
  218. //phpinfo();
  219. $contents = ReadFileCont("$fpath"."$fname");
  220. $matches = array();
  221. preg_match_all("(<img .*>)",$contents,$matches);
  222. $imgcount = 0;
  223. while (list($int,$match)=each($matches))
  224. {
  225. //each match consists of a key and a value
  226. while(list($key,$imgtag)=each($match))
  227. {
  228. $imgname = GetImgName($imgtag);
  229. if ($imgname!="" && !in_array($imgname,$imgparams)){
  230. array_push($imgparams,$imgname); // name (+ type) of the images in the html test
  231. $imgcount = $imgcount + 1; // number of images in the html test
  232. }
  233. }
  234. }
  235. }
  236. /**
  237. * Generates a list of hidden fields with the image params given as parameter to this function
  238. * @param array List of image parameters
  239. * @return string String containing the hidden parameters built from the list given
  240. */
  241. function GenerateHiddenList($imgparams)
  242. {
  243. $list = "";
  244. if(is_array($imgparams)){
  245. while (list($int,$string)=each($imgparams))
  246. {
  247. $list .= "<input type=\"hidden\" name=\"imgparams[]\" value=\"$string\" />\n";
  248. }
  249. }
  250. return $list;
  251. }
  252. /**
  253. * Searches for a node in the given array
  254. * @param reference Reference to the array to search
  255. * @param string Node we are looking for in the array
  256. * @return mixed Node name or false if not found
  257. */
  258. function myarraysearch(&$array,$node)
  259. {
  260. $match = FALSE;
  261. $tmp_array = array();
  262. for($i=0;$i<count($array);$i++)
  263. {
  264. if (!strcmp($array[$i],$node))
  265. {
  266. $match = $node;
  267. }
  268. else
  269. { array_push($tmp_array,$array[$i]); }
  270. }
  271. $array = $tmp_array;
  272. return $match;
  273. }
  274. /**
  275. * Look for the image name into an array
  276. * @param reference Reference to an array to search
  277. * @param string String to look for
  278. * @return mixed String given if found, false otherwise
  279. * @uses myarraysearch This function is just an additional layer on the myarraysearch() function
  280. */
  281. function CheckImageName(&$imgparams,$string)
  282. {
  283. $checked = myarraysearch($imgparams,$string);
  284. return $checked;
  285. }
  286. /**
  287. * Replaces an image tag by ???
  288. * @param string The content to replace
  289. * @return string The modified content
  290. */
  291. function ReplaceImgTag($content)
  292. {
  293. $newcontent = $content;
  294. $matches = array();
  295. preg_match_all("(<img .*>)",$content,$matches);
  296. $imgcount = 0;
  297. while (list($int,$match)=each($matches))
  298. {
  299. while(list($key,$imgtag)=each($match))
  300. {
  301. $imgname = GetSrcName($imgtag);
  302. if ($imgname=="") {} // valid or invalid image name
  303. else {
  304. $prehref = $imgname;
  305. $posthref = GetFileName($imgname);
  306. $newcontent = str_replace($prehref,$posthref,$newcontent);
  307. }
  308. }
  309. }
  310. return $newcontent;
  311. }
  312. /**
  313. * Fills the folder name up to a certain length with "0"
  314. * @param string Original folder name
  315. * @param integer Length to reach
  316. * @return string Modified folder name
  317. */
  318. function FillFolderName($name,$nsize)
  319. {
  320. $str = "";
  321. for($i=0;$i < $nsize-strlen($name);$i++)
  322. {
  323. $str .= "0";
  324. }
  325. $str .= $name;
  326. return $str;
  327. }
  328. /**
  329. * Generates the HotPotato folder tree
  330. * @param string Folder path
  331. * @return string Folder name (modified)
  332. */
  333. function GenerateHpFolder($folder)
  334. {
  335. $filelist = array();
  336. if ($dir = @opendir($folder)) {
  337. while (($file = readdir($dir)) !== false) {
  338. if ( $file != ".") {
  339. if ($file != "..")
  340. {
  341. $full_name = $folder."/".$file;
  342. if (is_dir($full_name))
  343. {
  344. $filelist[] = $file;
  345. }
  346. }
  347. }
  348. }
  349. }
  350. $w = 0;
  351. do {
  352. $name = FillFolderName(mt_rand(1,99999),6);
  353. $checked = myarraysearch($filelist,$name);
  354. //as long as we find the name in the array, continue looping. As soon as we have a new element, quit
  355. if ($checked) { $w = 1; }
  356. else { $w = 0; }
  357. } while ($w==1);
  358. return $name;
  359. }
  360. /**
  361. * Gets the folder name (strip down path)
  362. * @param string Path
  363. * @return string Folder name stripped down
  364. */
  365. function GetFolderName($fname)
  366. {
  367. $name = explode('/',$fname);
  368. $name = $name[sizeof($name)-2];
  369. return $name;
  370. }
  371. /**
  372. * Gets the folder path (withouth the name of the folder itself) ?
  373. * @param string Path
  374. * @return string Path stripped down
  375. */
  376. function GetFolderPath($fname)
  377. {
  378. $str = "";
  379. $name = explode('/',$fname);
  380. for($i=0;$i < sizeof($name)-1; $i++)
  381. { $str = $str.$name[$i]."/"; }
  382. return $str;
  383. }
  384. /**
  385. * Checks if there are subfolders
  386. * @param string Path
  387. * @return integer 1 if a subfolder was found, 0 otherwise
  388. */
  389. function CheckSubFolder($path)
  390. {
  391. $folder = GetFolderPath($path);
  392. $dflag = 0;
  393. if ($dir = @opendir($folder)) {
  394. while (($file = readdir($dir)) !== false) {
  395. if ( $file != ".") {
  396. if ($file != "..") {
  397. $full_name = $folder."/".$file;
  398. if (is_dir($full_name)) {
  399. $dflag = 1; // first directory
  400. }
  401. }
  402. }
  403. }
  404. }
  405. return $dflag;
  406. }
  407. /**
  408. * Hotpotato Garbage Collector
  409. * @param string Path
  410. * @param integer Flag
  411. * @param integer User id
  412. * @return void No return value, but echoes results
  413. */
  414. function HotPotGCt($folder,$flag,$userID)
  415. { // Garbage Collector
  416. $filelist = array();
  417. if ($dir = @opendir($folder)) {
  418. while (($file = readdir($dir)) !== false) {
  419. if ( $file != ".") {
  420. if ($file != "..")
  421. {
  422. $full_name = $folder."/".$file;
  423. if (is_dir($full_name))
  424. {
  425. HotPotGCt($folder."/".$file,$flag,$userID);
  426. }
  427. else
  428. {
  429. $filelist[] = $file;
  430. }
  431. }
  432. }
  433. }
  434. closedir($dir);
  435. }
  436. while (list ($key, $val) = each ($filelist))
  437. {
  438. if (stristr($val,$userID.".t.html"))
  439. {
  440. if ($flag == 1)
  441. {
  442. my_delete($folder."/".$val);
  443. }
  444. else
  445. {
  446. echo $folder."/".$val."<br />";
  447. }
  448. }
  449. }
  450. }
  451. ?>