hotpotatoes.lib.php 11 KB

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