hotpotatoes.lib.php 12 KB

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