hotpotatoes.lib.php 11 KB

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