hotpotatoes.lib.php 12 KB

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