hotpotatoes.lib.php 13 KB

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