link.lib.php 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Function library for the links tool.
  5. *
  6. * This is a complete remake of the original link tool.
  7. * New features:
  8. * - Organize links into categories;
  9. * - favorites/bookmarks interface;
  10. * - move links up/down within a category;
  11. * - move categories up/down;
  12. * - expand/collapse all categories;
  13. * - add link to 'root' category => category-less link is always visible.
  14. *
  15. * @author Patrick Cool, complete remake (December 2003 - January 2004)
  16. * @author René Haentjens, CSV file import (October 2004)
  17. * @package chamilo.link
  18. */
  19. /* FUNCTIONS */
  20. class Link extends Model
  21. {
  22. var $table;
  23. var $is_course_model = true;
  24. var $columns = array('id', 'c_id','url','title','description','category_id', 'display_order', 'on_homepage', 'target', 'session_id');
  25. var $required = array('url', 'title');
  26. public function __construct() {
  27. $this->table = Database::get_course_table(TABLE_LINK);
  28. }
  29. public function save($params, $show_query = null) {
  30. $course_info = api_get_course_info();
  31. $params['session_id'] = api_get_session_id();
  32. $params['category_id'] = isset($params['category_id']) ? $params['category_id'] : 0;
  33. $id = parent::save($params, $show_query);
  34. if (!empty($id)) {
  35. api_item_property_update($course_info, TOOL_LINK, $id, 'LinkAdded', api_get_user_id());
  36. }
  37. return $id;
  38. }
  39. }
  40. /**
  41. * Used to add a link or a category
  42. * @param string $type, "link" or "category"
  43. * @todo replace strings by constants
  44. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  45. */
  46. function addlinkcategory($type) {
  47. global $catlinkstatus;
  48. global $msgErr;
  49. $ok = true;
  50. $course_id = api_get_course_int_id();
  51. $courseInfo = api_get_course_info();
  52. if ($type == 'link') {
  53. $tbl_link = Database :: get_course_table(TABLE_LINK);
  54. $title = Security :: remove_XSS(stripslashes($_POST['title']));
  55. $urllink = Security :: remove_XSS($_POST['urllink']);
  56. $description = Security :: remove_XSS($_POST['description']);
  57. $selectcategory = Security :: remove_XSS($_POST['selectcategory']);
  58. if ($_POST['onhomepage'] == '') {
  59. $onhomepage = 0;
  60. } else {
  61. $onhomepage = Security :: remove_XSS($_POST['onhomepage']);
  62. }
  63. if (empty($_POST['target_link'])) {
  64. $target = '_self'; // Default target.
  65. } else {
  66. $target = Security :: remove_XSS($_POST['target_link']);
  67. }
  68. $urllink = trim($urllink);
  69. $title = trim($title);
  70. $description = trim($description);
  71. // We ensure URL to be absolute.
  72. if (strpos($urllink, '://') === false) {
  73. $urllink = 'http://' . $urllink;
  74. }
  75. // If the title is empty, we use the URL as title.
  76. if ($title == '') {
  77. $title = $urllink;
  78. }
  79. // If the URL is invalid, an error occurs.
  80. // Ivan, 13-OCT-2010, Chamilo 1.8.8: Let us still tolerate PHP 5.1.x and avoid a specific bug in filter_var(), see http://bugs.php.net/51192
  81. //if (!filter_var($urllink, FILTER_VALIDATE_URL)) {
  82. if (!api_valid_url($urllink, true)) { // A check against an absolute URL
  83. $msgErr = get_lang('GiveURL');
  84. Display :: display_error_message(get_lang('GiveURL'));
  85. $ok = false;
  86. } else {
  87. // Looking for the largest order number for this category.
  88. $result = Database :: query("SELECT MAX(display_order) FROM " . $tbl_link . " WHERE c_id = $course_id AND category_id = '" . intval($_POST['selectcategory']) . "'");
  89. list ($orderMax) = Database :: fetch_row($result);
  90. $order = $orderMax +1;
  91. $session_id = api_get_session_id();
  92. $sql = "INSERT INTO " . $tbl_link . " (c_id, url, title, description, category_id, display_order, on_homepage, target, session_id)
  93. VALUES (".$course_id.", '".Database :: escape_string($urllink) . "','" . Database :: escape_string($title) . "','" . Database :: escape_string($description) . "','" .
  94. Database :: escape_string($selectcategory) . "','" . Database :: escape_string($order) . "', '" . Database :: escape_string($onhomepage) . "','" .
  95. Database :: escape_string($target) . "','" . Database :: escape_string($session_id) . "')";
  96. $catlinkstatus = get_lang('LinkAdded');
  97. Database :: query($sql);
  98. $link_id = Database :: insert_id();
  99. if ($link_id) {
  100. api_set_default_visibility($courseInfo, $link_id, TOOL_LINK);
  101. }
  102. if ((api_get_setting('search_enabled') == 'true') && $link_id && extension_loaded('xapian')) {
  103. require_once api_get_path(LIBRARY_PATH) . 'search/ChamiloIndexer.class.php';
  104. require_once api_get_path(LIBRARY_PATH) . 'search/IndexableChunk.class.php';
  105. require_once api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php';
  106. $course_int_id = api_get_course_int_id();
  107. $courseid = api_get_course_id();
  108. $specific_fields = get_specific_field_list();
  109. $ic_slide = new IndexableChunk();
  110. // Add all terms to db.
  111. $all_specific_terms = '';
  112. foreach ($specific_fields as $specific_field) {
  113. if (isset ($_REQUEST[$specific_field['code']])) {
  114. $sterms = trim($_REQUEST[$specific_field['code']]);
  115. if (!empty ($sterms)) {
  116. $all_specific_terms .= ' ' . $sterms;
  117. $sterms = explode(',', $sterms);
  118. foreach ($sterms as $sterm) {
  119. $ic_slide->addTerm(trim($sterm), $specific_field['code']);
  120. add_specific_field_value($specific_field['id'], $courseid, TOOL_LINK, $link_id, $sterm);
  121. }
  122. }
  123. }
  124. }
  125. // Build the chunk to index.
  126. $ic_slide->addValue('title', $title);
  127. $ic_slide->addCourseId($courseid);
  128. $ic_slide->addToolId(TOOL_LINK);
  129. $xapian_data = array (
  130. SE_COURSE_ID => $courseid,
  131. SE_TOOL_ID => TOOL_LINK,
  132. SE_DATA => array (
  133. 'link_id' => (int) $link_id
  134. ),
  135. SE_USER => (int) api_get_user_id(),
  136. );
  137. $ic_slide->xapian_data = serialize($xapian_data);
  138. $description = $all_specific_terms . ' ' . $description;
  139. $ic_slide->addValue('content', $description);
  140. // Add category name if set.
  141. if (isset ($_POST['selectcategory']) && $selectcategory > 0) {
  142. $table_link_category = Database :: get_course_table(TABLE_LINK_CATEGORY);
  143. $sql_cat = 'SELECT * FROM %s WHERE id=%d AND c_id = %d LIMIT 1';
  144. $sql_cat = sprintf($sql_cat, $table_link_category, (int) $selectcategory, $course_int_id);
  145. $result = Database :: query($sql_cat);
  146. if (Database :: num_rows($result) == 1) {
  147. $row = Database :: fetch_array($result);
  148. $ic_slide->addValue('category', $row['category_title']);
  149. }
  150. }
  151. $di = new ChamiloIndexer();
  152. isset ($_POST['language']) ? $lang = Database :: escape_string($_POST['language']) : $lang = 'english';
  153. $di->connectDb(NULL, NULL, $lang);
  154. $di->addChunk($ic_slide);
  155. // Index and return search engine document id.
  156. $did = $di->index();
  157. if ($did) {
  158. // Save it to db.
  159. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  160. $sql = 'INSERT INTO %s (c_id, id, course_code, tool_id, ref_id_high_level, search_did)
  161. VALUES (NULL , \'%s\', \'%s\', %s, %s)';
  162. $sql = sprintf($sql, $tbl_se_ref, $course_int_id, $courseid, TOOL_LINK, $link_id, $did);
  163. Database :: query($sql);
  164. }
  165. }
  166. unset ($urllink, $title, $description, $selectcategory);
  167. Display :: display_confirmation_message(get_lang('LinkAdded'));
  168. }
  169. } elseif ($type == 'category') {
  170. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  171. $category_title = trim($_POST['category_title']);
  172. $description = trim($_POST['description']);
  173. if (empty($category_title)) {
  174. $msgErr = get_lang('GiveCategoryName');
  175. Display :: display_error_message(get_lang('GiveCategoryName'));
  176. $ok = false;
  177. } else {
  178. // Looking for the largest order number for this category.
  179. $result = Database :: query("SELECT MAX(display_order) FROM " . $tbl_categories." WHERE c_id = $course_id ");
  180. list ($orderMax) = Database :: fetch_row($result);
  181. $order = $orderMax +1;
  182. $order = intval($order);
  183. $session_id = api_get_session_id();
  184. $sql = "INSERT INTO ".$tbl_categories." (c_id, category_title, description, display_order, session_id)
  185. VALUES (".$course_id.", '" .Database::escape_string($category_title) . "', '" . Database::escape_string($description) . "', '$order', '$session_id')";
  186. Database :: query($sql);
  187. $catlinkstatus = get_lang('CategoryAdded');
  188. unset ($category_title, $description);
  189. Display :: display_confirmation_message(get_lang('CategoryAdded'));
  190. }
  191. }
  192. // "WHAT'S NEW" notification : update last tool Edit.
  193. if ($type == 'link') {
  194. global $_user;
  195. $_course = api_get_course_info();
  196. global $nameTools;
  197. api_item_property_update($_course, TOOL_LINK, $link_id, 'LinkAdded', $_user['user_id']);
  198. }
  199. return $ok;
  200. }
  201. /**
  202. * Used to delete a link or a category
  203. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  204. */
  205. function deletelinkcategory($type) {
  206. global $catlinkstatus;
  207. $_course = api_get_course_info();
  208. $tbl_link = Database :: get_course_table(TABLE_LINK);
  209. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  210. $TABLE_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  211. $course_id = api_get_course_int_id();
  212. if ($type == 'link') {
  213. global $id;
  214. // -> Items are no longer fysically deleted, but the visibility is set to 2 (in item_property).
  215. // This will make a restore function possible for the platform administrator.
  216. if (isset ($_GET['id']) && $_GET['id'] == strval(intval($_GET['id']))) {
  217. $sql = "UPDATE $tbl_link SET on_homepage='0' WHERE c_id = $course_id AND id='" . intval($_GET['id']) . "'";
  218. Database :: query($sql);
  219. }
  220. api_item_property_update($_course, TOOL_LINK, $id, 'delete', api_get_user_id());
  221. delete_link_from_search_engine(api_get_course_id(), $id);
  222. $catlinkstatus = get_lang('LinkDeleted');
  223. unset ($id);
  224. Display :: display_confirmation_message(get_lang('LinkDeleted'));
  225. }
  226. if ($type == 'category') {
  227. global $id;
  228. if (isset ($_GET['id']) && !empty ($_GET['id'])) {
  229. // First we delete the category itself and afterwards all the links of this category.
  230. $sql = "DELETE FROM " . $tbl_categories . " WHERE c_id = $course_id AND id='" . intval($_GET['id']) . "'";
  231. Database :: query($sql);
  232. $sql = "DELETE FROM " . $tbl_link . " WHERE c_id = $course_id AND category_id='" . intval($_GET['id']) . "'";
  233. $catlinkstatus = get_lang('CategoryDeleted');
  234. unset ($id);
  235. Database :: query($sql);
  236. Display :: display_confirmation_message(get_lang('CategoryDeleted'));
  237. }
  238. }
  239. }
  240. /**
  241. * Removes a link from search engine database
  242. *
  243. * @param string $course_id Course code
  244. * @param int $document_id Document id to delete
  245. */
  246. function delete_link_from_search_engine($course_id, $link_id) {
  247. // Remove from search engine if enabled.
  248. if (api_get_setting('search_enabled') == 'true') {
  249. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  250. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  251. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  252. $res = Database :: query($sql);
  253. if (Database :: num_rows($res) > 0) {
  254. $row = Database :: fetch_array($res);
  255. require_once api_get_path(LIBRARY_PATH) . 'search/ChamiloIndexer.class.php';
  256. $di = new ChamiloIndexer();
  257. $di->remove_document((int) $row['search_did']);
  258. }
  259. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  260. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  261. Database :: query($sql);
  262. // Remove terms from db.
  263. require_once (api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php');
  264. delete_all_values_for_item($course_id, TOOL_DOCUMENT, $link_id);
  265. }
  266. }
  267. /**
  268. *
  269. * Get link info
  270. * @param int link id
  271. * @return array link info
  272. *
  273. * */
  274. function get_link_info($id) {
  275. $tbl_link = Database :: get_course_table(TABLE_LINK);
  276. $course_id = api_get_course_int_id();
  277. $sql = "SELECT * FROM " . $tbl_link . " WHERE c_id = $course_id AND id='" . intval($id) . "' ";
  278. $result = Database::query($sql);
  279. if (Database::num_rows($result)) {
  280. $data = Database::fetch_array($result);
  281. }
  282. return $data;
  283. }
  284. /**
  285. * Used to edit a link or a category
  286. * @todo Rewrite the whole links tool because it is becoming completely cluttered,
  287. * code does not follow the coding conventions, does not use html_quickform, ...
  288. * Some features were patched in.
  289. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  290. * @todo replace the globals with the appropriate $_POST or $_GET values
  291. */
  292. function editlinkcategory($type) {
  293. global $catlinkstatus;
  294. global $id;
  295. global $submit_link;
  296. global $submit_category;
  297. global $_user;
  298. $_course = api_get_course_info();
  299. global $nameTools;
  300. global $urllink;
  301. global $title;
  302. global $description;
  303. global $category;
  304. global $selectcategory;
  305. global $description;
  306. global $category_title;
  307. global $onhomepage;
  308. global $target_link;
  309. $tbl_link = Database :: get_course_table(TABLE_LINK);
  310. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  311. $course_id = api_get_course_int_id();
  312. if ($type == 'link') {
  313. // This is used to populate the link-form with the info found in the database.
  314. if (!empty ($_GET['id'])) {
  315. $sql = "SELECT * FROM " . $tbl_link . " WHERE c_id = $course_id AND id='" . intval($_GET['id']) . "'";
  316. $result = Database :: query($sql);
  317. if ($myrow = Database :: fetch_array($result)) {
  318. $urllink = $myrow['url'];
  319. $title = $myrow['title'];
  320. $description = $myrow['description'];
  321. $category = $myrow['category_id'];
  322. if ($myrow['on_homepage'] != 0) {
  323. $onhomepage = 'checked';
  324. }
  325. $target_link = $myrow['target'];
  326. }
  327. }
  328. // This is used to put the modified info of the link-form into the database.
  329. if ($_POST['submitLink']) {
  330. // Ivan, 13-OCT-2010: It is a litle bit messy code below, just in case I added some extra-security checks here.
  331. $_POST['urllink'] = trim($_POST['urllink']);
  332. $_POST['title'] = trim(Security :: remove_XSS($_POST['title']));
  333. $_POST['description'] = trim(Security :: remove_XSS($_POST['description']));
  334. $_POST['selectcategory'] = intval($_POST['selectcategory']);
  335. $_POST['id'] = intval($_POST['id']);
  336. // We ensure URL to be absolute.
  337. if (strpos($_POST['urllink'], '://') === false) {
  338. $_POST['urllink'] = 'http://' . $_POST['urllink'];
  339. }
  340. // If the title is empty, we use the URL as title.
  341. if ($_POST['title'] == '') {
  342. $_POST['title'] = $_POST['urllink'];
  343. }
  344. // If the URL is invalid, an error occurs.
  345. // Ivan, 13-OCT-2010, Chamilo 1.8.8: Let us still tolerate PHP 5.1.x and avoid a specific bug in filter_var(), see http://bugs.php.net/51192
  346. //if (!filter_var($urllink, FILTER_VALIDATE_URL)) {
  347. if (!api_valid_url($urllink, true)) { // A check against an absolute URL.
  348. $msgErr = get_lang('GiveURL');
  349. Display :: display_error_message(get_lang('GiveURL'));
  350. return false;
  351. }
  352. $onhomepage = Security :: remove_XSS($_POST['onhomepage']);
  353. $target = Database::escape_string($_POST['target_link']);
  354. if (empty ($mytarget)) {
  355. $mytarget = '_self';
  356. }
  357. $mytarget = ",target='" . $target . "'";
  358. // Finding the old category_id.
  359. $sql = "SELECT * FROM " . $tbl_link . " WHERE c_id = $course_id AND id='" . intval($_POST['id']) . "'";
  360. $result = Database :: query($sql);
  361. $row = Database :: fetch_array($result);
  362. $category_id = $row['category_id'];
  363. if ($category_id != $_POST['selectcategory']) {
  364. $sql = "SELECT MAX(display_order) FROM " . $tbl_link . " WHERE c_id = $course_id AND category_id='" . intval($_POST['selectcategory']) . "'";
  365. $result = Database :: query($sql);
  366. list ($max_display_order) = Database :: fetch_row($result);
  367. $max_display_order++;
  368. } else {
  369. $max_display_order = $row['display_order'];
  370. }
  371. $sql = "UPDATE " . $tbl_link . " SET " .
  372. "url='" . Database :: escape_string($_POST['urllink']) . "', " .
  373. "title='" . Database :: escape_string($_POST['title']) . "', " .
  374. "description='" . Database :: escape_string($_POST['description']) . "', " .
  375. "category_id='" . Database :: escape_string($_POST['selectcategory']) . "', " .
  376. "display_order='" . $max_display_order . "', " .
  377. "on_homepage='" . Database :: escape_string($onhomepage) . " ' $mytarget " .
  378. " WHERE c_id = $course_id AND id='" . intval($_POST['id']) . "'";
  379. Database :: query($sql);
  380. // Update search enchine and its values table if enabled.
  381. if (api_get_setting('search_enabled') == 'true') {
  382. $link_id = intval($_POST['id']);
  383. $course_int_id = api_get_course_int_id();
  384. $course_id = api_get_course_id();
  385. $link_url = Database :: escape_string($_POST['urllink']);
  386. $link_title = Database :: escape_string($_POST['title']);
  387. $link_description = Database :: escape_string($_POST['description']);
  388. // Actually, it consists on delete terms from db, insert new ones, create a new search engine document, and remove the old one.
  389. // Get search_did.
  390. $tbl_se_ref = Database :: get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  391. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  392. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  393. $res = Database :: query($sql);
  394. if (Database :: num_rows($res) > 0) {
  395. require_once api_get_path(LIBRARY_PATH) . 'search/ChamiloIndexer.class.php';
  396. require_once api_get_path(LIBRARY_PATH) . 'search/IndexableChunk.class.php';
  397. require_once api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php';
  398. $se_ref = Database :: fetch_array($res);
  399. $specific_fields = get_specific_field_list();
  400. $ic_slide = new IndexableChunk();
  401. $all_specific_terms = '';
  402. foreach ($specific_fields as $specific_field) {
  403. delete_all_specific_field_value($course_id, $specific_field['id'], TOOL_LINK, $link_id);
  404. if (isset ($_REQUEST[$specific_field['code']])) {
  405. $sterms = trim($_REQUEST[$specific_field['code']]);
  406. if (!empty ($sterms)) {
  407. $all_specific_terms .= ' ' . $sterms;
  408. $sterms = explode(',', $sterms);
  409. foreach ($sterms as $sterm) {
  410. $ic_slide->addTerm(trim($sterm), $specific_field['code']);
  411. add_specific_field_value($specific_field['id'], $course_id, TOOL_LINK, $link_id, $sterm);
  412. }
  413. }
  414. }
  415. }
  416. // Build the chunk to index.
  417. $ic_slide->addValue("title", $link_title);
  418. $ic_slide->addCourseId($course_id);
  419. $ic_slide->addToolId(TOOL_LINK);
  420. $xapian_data = array (
  421. SE_COURSE_ID => $course_id,
  422. SE_TOOL_ID => TOOL_LINK,
  423. SE_DATA => array (
  424. 'link_id' => (int) $link_id
  425. ),
  426. SE_USER => (int) api_get_user_id(),
  427. );
  428. $ic_slide->xapian_data = serialize($xapian_data);
  429. $link_description = $all_specific_terms . ' ' . $link_description;
  430. $ic_slide->addValue('content', $link_description);
  431. // Add category name if set.
  432. if (isset ($_POST['selectcategory']) && $selectcategory > 0) {
  433. $table_link_category = Database :: get_course_table(TABLE_LINK_CATEGORY);
  434. $sql_cat = 'SELECT * FROM %s WHERE id=%d and c_id = %d LIMIT 1';
  435. $sql_cat = sprintf($sql_cat, $table_link_category, (int) $selectcategory, $course_int_id);
  436. $result = Database :: query($sql_cat);
  437. if (Database :: num_rows($result) == 1) {
  438. $row = Database :: fetch_array($result);
  439. $ic_slide->addValue('category', $row['category_title']);
  440. }
  441. }
  442. $di = new ChamiloIndexer();
  443. isset ($_POST['language']) ? $lang = Database :: escape_string($_POST['language']) : $lang = 'english';
  444. $di->connectDb(NULL, NULL, $lang);
  445. $di->remove_document((int) $se_ref['search_did']);
  446. $di->addChunk($ic_slide);
  447. // Index and return search engine document id.
  448. $did = $di->index();
  449. if ($did) {
  450. // Save it to db.
  451. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=\'%s\'';
  452. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_LINK, $link_id);
  453. Database :: query($sql);
  454. $sql = 'INSERT INTO %s (c_id, id, course_code, tool_id, ref_id_high_level, search_did)
  455. VALUES (NULL , \'%s\', \'%s\', %s, %s)';
  456. $sql = sprintf($sql, $tbl_se_ref, $course_int_id, $course_id, TOOL_LINK, $link_id, $did);
  457. Database :: query($sql);
  458. }
  459. }
  460. }
  461. // "WHAT'S NEW" notification: update table last_toolEdit.
  462. api_item_property_update($_course, TOOL_LINK, $_POST['id'], 'LinkUpdated', $_user['user_id']);
  463. Display :: display_confirmation_message(get_lang('LinkModded'));
  464. }
  465. }
  466. if ($type == 'category') {
  467. // This is used to populate the category-form with the info found in the database.
  468. if (!$submit_category) {
  469. $sql = "SELECT * FROM " . $tbl_categories . " WHERE c_id = $course_id AND id='" . intval($_GET['id']) . "'";
  470. $result = Database :: query($sql);
  471. if ($myrow = Database :: fetch_array($result)) {
  472. $category_title = $myrow['category_title'];
  473. $description = $myrow['description'];
  474. }
  475. }
  476. // This is used to put the modified info of the category-form into the database.
  477. if ($submit_category) {
  478. $sql = "UPDATE " . $tbl_categories . "
  479. SET category_title='" . Database :: escape_string($_POST['category_title']) . "', description='" . Database :: escape_string($_POST['description']) . "'
  480. WHERE c_id = $course_id AND id='" . Database :: escape_string($_POST['id']) . "'";
  481. Database :: query($sql);
  482. Display :: display_confirmation_message(get_lang('CategoryModded'));
  483. }
  484. }
  485. return true; // On errors before this statement, exit from this function by returning false value.
  486. }
  487. /**
  488. * Creates a correct $view for in the URL
  489. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  490. */
  491. function makedefaultviewcode($locatie) {
  492. global $aantalcategories, $view;
  493. for ($j = 0; $j <= $aantalcategories -1; $j++) {
  494. $view[$j] = 0;
  495. }
  496. $view[intval($locatie)] = '1';
  497. }
  498. /**
  499. * Changes the visibility of a link
  500. * @todo add the changing of the visibility of a course
  501. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  502. */
  503. function change_visibility($id, $scope) {
  504. global $_user;
  505. $_course = api_get_course_info();
  506. if ($scope == 'link') {
  507. api_item_property_update($_course, TOOL_LINK, $id, $_GET['action'], $_user['user_id']);
  508. Display :: display_confirmation_message(get_lang('VisibilityChanged'));
  509. }
  510. }
  511. /**
  512. * Displays all the links of a given category.
  513. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  514. */
  515. function showlinksofcategory($catid) {
  516. global $is_allowed, $charset, $urlview, $up, $down, $_user, $token;
  517. $tbl_link = Database :: get_course_table(TABLE_LINK);
  518. $TABLE_ITEM_PROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  519. // Condition for the session.
  520. $session_id = api_get_session_id();
  521. $condition_session = api_get_session_condition($session_id, true, true);
  522. $catid = intval($catid);
  523. $course_id = api_get_course_int_id();
  524. $sqlLinks = "SELECT *, link.id FROM " . $tbl_link . " link, " . $TABLE_ITEM_PROPERTY . " itemproperties
  525. WHERE itemproperties.tool='" . TOOL_LINK . "' AND
  526. link.id=itemproperties.ref AND
  527. link.category_id='" . $catid . "' AND
  528. (itemproperties.visibility='0' OR itemproperties.visibility='1')
  529. $condition_session AND
  530. link.c_id = ".$course_id." AND
  531. itemproperties.c_id = ".$course_id."
  532. ORDER BY link.display_order DESC";
  533. $result = Database :: query($sqlLinks);
  534. $numberoflinks = Database :: num_rows($result);
  535. if ($numberoflinks > 0) {
  536. echo '<table class="data_table" width="100%">';
  537. $i = 1;
  538. while ($myrow = Database :: fetch_array($result)) {
  539. // Validacion when belongs to a session.
  540. $session_img = api_get_session_image($myrow['session_id'], $_user['status']);
  541. $css_class = $i % 2 == 0 ? $css_class = 'row_odd' : $css_class = 'row_even';
  542. $link_validator = '';
  543. if (api_is_allowed_to_edit(null, true)) {
  544. $link_validator = ''.Display::url(Display::return_icon('preview_view.png', get_lang('CheckURL'), array(), 16), '#', array('onclick'=>"check_url('".$myrow['id']."', '".addslashes($myrow['url'])."');"));
  545. $link_validator .= Display::span('', array('id'=>'url_id_'.$myrow['id']));
  546. }
  547. if ($myrow['visibility'] == '1') {
  548. echo '<tr class="'.$css_class.'">';
  549. echo '<td align="center" valign="middle" width="15">';
  550. echo '<a href="link_goto.php?'.api_get_cidreq().'&amp;link_id='.$myrow['id'].'&amp;link_url='.urlencode($myrow['url']).'" target="_blank">
  551. <img src="../../main/img/link.gif" border="0" alt="'.get_lang('Link').'"/></a></td>
  552. <td width="80%" valign="top"><a href="link_goto.php?'.api_get_cidreq().'&amp;link_id='.$myrow['id'].'&amp;link_url='.urlencode($myrow['url']).'" target="'.$myrow['target'].'">';
  553. echo Security :: remove_XSS($myrow['title']);
  554. echo '</a>';
  555. echo $link_validator;
  556. echo $session_img;
  557. echo '<br />'.$myrow['description'];
  558. } else {
  559. if (api_is_allowed_to_edit(null, true)) {
  560. echo '<tr class="'.$css_class.'">';
  561. echo '<td align="center" valign="middle" width="15"><a href="link_goto.php?'.api_get_cidreq().'&amp;link_id='.$myrow['id']."&amp;link_url=".urlencode($myrow['url']).'" target="_blank" class="invisible">';
  562. echo Display :: return_icon('link_na.gif', get_lang('Link')), '</a>';
  563. echo '</td><td width="80%" valign="top"><a href="link_goto.php?', api_get_cidreq(), '&amp;link_id=', $myrow['id'], '&amp;link_url=', urlencode($myrow['url']),'" target="', $myrow['target'], '" class="invisible">';
  564. echo Security :: remove_XSS($myrow['url']);
  565. echo "</a>";
  566. echo $link_validator;
  567. echo $session_img, '<br />', $myrow['title'];
  568. }
  569. }
  570. echo '<td style="text-align:center;">';
  571. if (api_is_allowed_to_edit(null, true)) {
  572. if ($session_id == $myrow['session_id']) {
  573. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&amp;sec_token='.$token.'&amp;action=editlink&amp;category=' . (!empty ($category) ? $category : '') . '&amp;id=' . $myrow['id'] . '&amp;urlview=' . $urlview . '" title="' . get_lang('Modify') . '">' .
  574. Display :: return_icon('edit.png', get_lang('Modify'), array (), ICON_SIZE_SMALL) . '</a>';
  575. // DISPLAY MOVE UP COMMAND only if it is not the top link.
  576. /*
  577. if ($i != 1) {
  578. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&amp;sec_token='.$token.'&amp;urlview=' . $urlview . '&amp;up=', $myrow[0], '" title="' . get_lang('Up') . '">' . Display :: return_icon('up.png', get_lang('Up'), array (), ICON_SIZE_SMALL) . '', "</a>\n";
  579. } else {
  580. echo Display :: return_icon('up_na.png', get_lang('Up'), array (), ICON_SIZE_SMALL) . '</a>';
  581. }
  582. // DISPLAY MOVE DOWN COMMAND only if it is not the bottom link.
  583. if ($i < $numberoflinks) {
  584. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&amp;sec_token='.$token.'&amp;urlview=' . $urlview . '&amp;down=' . $myrow[0] . '" title="' . get_lang('Down') . '">' . Display :: return_icon('down.png', get_lang('Down'), array (), ICON_SIZE_SMALL) . '', "</a>\n";
  585. } else {
  586. echo Display :: return_icon('down_na.png', get_lang('Down'), array (), ICON_SIZE_SMALL) . '', "</a>\n";
  587. }*/
  588. if ($myrow['visibility'] == '1') {
  589. echo '<a href="link.php?' . api_get_cidreq() . '&amp;sec_token='.$token.'&amp;action=invisible&amp;id=' . $myrow['id'] . '&amp;scope=link&amp;urlview=' . $urlview . '" title="' . get_lang('Hide') . '">' .
  590. Display :: return_icon('visible.png', get_lang('Hide'), array (), ICON_SIZE_SMALL) . '</a>';
  591. }
  592. if ($myrow['visibility'] == '0') {
  593. echo ' <a href="link.php?' . api_get_cidreq() . '&amp;sec_token='.$token.'&amp;action=visible&amp;id=' . $myrow['id'] . '&amp;scope=link&amp;urlview=' . $urlview . '" title="' . get_lang('Show') . '">' .
  594. Display :: return_icon('invisible.png', get_lang('Show'), array (), ICON_SIZE_SMALL) . '</a>';
  595. }
  596. echo ' <a href="' . api_get_self() . '?' . api_get_cidreq() . '&amp;sec_token='.$token.'&amp;action=deletelink&amp;id=', $myrow['id'], '&amp;urlview=', $urlview, "\" onclick=\"javascript: if(!confirm('" . get_lang('LinkDelconfirm') . "')) return false;\" title=\"" . get_lang('Delete') . '">' .
  597. Display :: return_icon('delete.png', get_lang('Delete'), array (), ICON_SIZE_SMALL) . '</a>';
  598. } else {
  599. echo Display :: return_icon('edit_na.png', get_lang('EditionNotAvailableFromSession'), array (), ICON_SIZE_SMALL); //get_lang('EditionNotAvailableFromSession');
  600. }
  601. }
  602. echo '</td></tr>';
  603. $i++;
  604. }
  605. echo '</table>';
  606. }
  607. }
  608. /**
  609. * Displays the edit, delete and move icons
  610. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  611. */
  612. function showcategoryadmintools($categoryid) {
  613. global $urlview, $aantalcategories, $catcounter, $token;
  614. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&amp;sec_token='.$token.'&amp;action=editcategory&amp;id=' . $categoryid . '&amp;urlview=' . $urlview . '" title=' . get_lang('Modify') . '">' . Display :: return_icon('edit.png', get_lang('Modify'), array (), ICON_SIZE_SMALL) . '</a>';
  615. // DISPLAY MOVE UP COMMAND only if it is not the top link.
  616. if ($catcounter != 1) {
  617. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&amp;sec_token='.$token.'&amp;catmove=true&amp;up=', $categoryid, '&amp;urlview=' . $urlview . '" title="' . get_lang('Up') . '">' . Display :: return_icon('up.png', get_lang('Up'), array (), ICON_SIZE_SMALL) . '</a>';
  618. } else {
  619. echo Display :: return_icon('up_na.png', get_lang('Up'), array (), ICON_SIZE_SMALL) . '</a>';
  620. }
  621. // DISPLAY MOVE DOWN COMMAND only if it is not the bottom link.
  622. if ($catcounter < $aantalcategories) {
  623. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() .'&amp;sec_token='.$token.'&amp;catmove=true&amp;down=' . $categoryid . '&amp;urlview=' . $urlview . '">
  624. ' . Display :: return_icon('down.png', get_lang('Down'), array (), ICON_SIZE_SMALL) . '</a>';
  625. } else {
  626. echo Display :: return_icon('down_na.png', get_lang('Down'), array (), ICON_SIZE_SMALL) . '</a>';
  627. }
  628. echo '<a href="' . api_get_self() . '?' . api_get_cidreq() . '&amp;sec_token='.$token.'&amp;action=deletecategory&amp;id=', $categoryid, "&amp;urlview=$urlview\" onclick=\"javascript: if(!confirm('" . get_lang('CategoryDelconfirm') . "')) return false;\">", Display :: return_icon('delete.png', get_lang('Delete'), array (), ICON_SIZE_SMALL) . '</a>';
  629. $catcounter++;
  630. }
  631. /**
  632. * move a link or a linkcategory up or down
  633. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  634. */
  635. function movecatlink($catlinkid) {
  636. global $catmove;
  637. global $up;
  638. global $down;
  639. $tbl_link = Database :: get_course_table(TABLE_LINK);
  640. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  641. $course_id = api_get_course_int_id();
  642. if (!empty ($down)) {
  643. $thiscatlinkId = intval($down);
  644. $sortDirection = 'DESC';
  645. }
  646. if (!empty ($up)) {
  647. $thiscatlinkId = intval($up);
  648. $sortDirection = 'ASC';
  649. }
  650. // We check if it is a category we are moving or a link. If it is a category, a querystring catmove = true is present in the url.
  651. if ($catmove == 'true') {
  652. $movetable = $tbl_categories;
  653. $catid = $catlinkid;
  654. } else {
  655. $movetable = $tbl_link;
  656. // Getting the category of the link.
  657. if (!empty ($thiscatlinkId)) {
  658. $sql = "SELECT category_id FROM " . $movetable . " WHERE c_id = $course_id AND id='$thiscatlinkId'";
  659. $result = Database :: query($sql);
  660. $catid = Database :: fetch_array($result);
  661. }
  662. }
  663. // This code is copied and modified from announcements.php.
  664. if (!empty($sortDirection)) {
  665. if (!in_array(trim(strtoupper($sortDirection)), array ('ASC', 'DESC'))){
  666. $sortDirection = 'ASC';
  667. }
  668. if ($catmove == 'true') {
  669. $sqlcatlinks = "SELECT id, display_order FROM " . $movetable . " WHERE c_id = $course_id ORDER BY display_order $sortDirection";
  670. } else {
  671. $sqlcatlinks = "SELECT id, display_order FROM " . $movetable . "
  672. WHERE c_id = $course_id AND category_id='" . $catid[0] . "'
  673. ORDER BY display_order $sortDirection";
  674. }
  675. $linkresult = Database :: query($sqlcatlinks);
  676. while ($sortrow = Database :: fetch_array($linkresult)) {
  677. // STEP 2 : FOUND THE NEXT ANNOUNCEMENT ID AND ORDER, COMMIT SWAP
  678. // This part seems unlogic, but it isn't . We first look for the current link with the querystring ID
  679. // and we know the next iteration of the while loop is the next one. These should be swapped.
  680. if (isset ($thislinkFound) && $thislinkFound) {
  681. $nextlinkId = $sortrow['id'];
  682. $nextlinkOrdre = $sortrow['display_order'];
  683. Database :: query("UPDATE " . $movetable . "
  684. SET display_order = '$nextlinkOrdre'
  685. WHERE c_id = $course_id AND id = '$thiscatlinkId'");
  686. Database :: query("UPDATE " . $movetable . "
  687. SET display_order = '$thislinkOrdre'
  688. WHERE c_id = $course_id AND id = '$nextlinkId'");
  689. break;
  690. }
  691. if ($sortrow['id'] == $thiscatlinkId) {
  692. $thislinkOrdre = $sortrow['display_order'];
  693. $thislinkFound = true;
  694. }
  695. }
  696. }
  697. Display :: display_confirmation_message(get_lang('LinkMoved'));
  698. }
  699. /**
  700. * CSV file import functions
  701. * @author René Haentjens , Ghent University
  702. */
  703. function get_cat($catname) {
  704. // Get category id (existing or make new).
  705. $tbl_categories = Database :: get_course_table(TABLE_LINK_CATEGORY);
  706. $course_id = api_get_course_int_id();
  707. $result = Database :: query("SELECT id FROM " . $tbl_categories . " WHERE c_id = $course_id AND category_title='" . Database::escape_string($catname) . "'");
  708. if (Database :: num_rows($result) >= 1 && ($row = Database :: fetch_array($result))) {
  709. return $row['id']; // Several categories with same name: take the first.
  710. }
  711. $result = Database :: query("SELECT MAX(display_order) FROM " . $tbl_categories." WHERE c_id = $course_id ");
  712. list ($max_order) = Database :: fetch_row($result);
  713. Database :: query("INSERT INTO " . $tbl_categories . " (c_id, category_title, description, display_order)
  714. VALUES (".$course_id.", '" . Database::escape_string($catname) . "','','" . ($max_order +1) . "')");
  715. return Database :: insert_id();
  716. }
  717. /**
  718. * CSV file import functions
  719. * @author René Haentjens , Ghent University
  720. */
  721. function put_link($url, $cat, $title, $description, $on_homepage, $hidden) {
  722. $tbl_link = Database :: get_course_table(TABLE_LINK);
  723. $course_id = api_get_course_int_id();
  724. $urleq = "url='" . Database :: escape_string($url) . "'";
  725. $cateq = "category_id=" . intval($cat);
  726. $result = Database :: query("SELECT id FROM $tbl_link WHERE c_id = $course_id AND " . $urleq . ' AND ' . $cateq);
  727. if (Database :: num_rows($result) >= 1 && ($row = Database :: fetch_array($result))) {
  728. Database :: query("UPDATE $tbl_link set title='" . Database :: escape_string($title) . "', description='" . Database :: escape_string($description) . "'
  729. WHERE c_id = $course_id AND id='" . Database :: escape_string($row['id']) . "'");
  730. $ipu = 'LinkUpdated';
  731. $rv = 1; // 1 = upd
  732. } else {
  733. // Add new link
  734. $result = Database :: query("SELECT MAX(display_order) FROM $tbl_link WHERE c_id = $course_id AND category_id='" . intval($cat) . "'");
  735. list ($max_order) = Database :: fetch_row($result);
  736. Database :: query("INSERT INTO $tbl_link (c_id, url, title, description, category_id, display_order, on_homepage)
  737. VALUES (".api_get_course_int_id().", '" . Database :: escape_string($url) . "','" . Database :: escape_string($title) . "','" . Database :: escape_string($description) . "','" . intval($cat) . "','" . (intval($max_order) + 1) . "','" . intval($on_homepage) . "')");
  738. $id = Database :: insert_id();
  739. $ipu = 'LinkAdded';
  740. $rv = 2; // 2 = new
  741. }
  742. global $nameTools, $_user;
  743. $_course = api_get_course_info();
  744. api_item_property_update($_course, TOOL_LINK, $id, $ipu, $_user['user_id']);
  745. if ($hidden && $ipu == 'LinkAdded') {
  746. api_item_property_update($_course, TOOL_LINK, $id, 'invisible', $_user['user_id']);
  747. }
  748. return $rv;
  749. }
  750. /**
  751. * CSV file import functions
  752. * @author René Haentjens , Ghent University
  753. */
  754. function import_link($linkdata) {
  755. // url, category_id, title, description, ...
  756. // Field names used in the uploaded file
  757. $known_fields = array (
  758. 'url',
  759. 'category',
  760. 'title',
  761. 'description',
  762. 'on_homepage',
  763. 'hidden'
  764. );
  765. $hide_fields = array (
  766. 'kw',
  767. 'kwd',
  768. 'kwds',
  769. 'keyword',
  770. 'keywords'
  771. );
  772. // All other fields are added to description, as "name:value".
  773. // Only one hide_field is assumed to be present, <> is removed from value.
  774. if (!($url = trim($linkdata['url'])) || !($title = trim($linkdata['title']))) {
  775. return 0; // 0 = fail
  776. }
  777. $cat = ($catname = trim($linkdata['category'])) ? get_cat($catname) : 0;
  778. $regs = array (); // Will be passed to ereg()
  779. foreach ($linkdata as $key => $value)
  780. if (!in_array($key, $known_fields))
  781. if (in_array($key, $hide_fields) && ereg('^<?([^>]*)>?$', $value, $regs)) // possibly in <...>
  782. if (($kwlist = trim($regs[1])) != '')
  783. $kw = '<i kw="' . htmlspecialchars($kwlist) . '">';
  784. else
  785. $kw = '';
  786. // i.e. assume only one of the $hide_fields will be present
  787. // and if found, hide the value as expando property of an <i> tag
  788. elseif (trim($value)) {
  789. $d .= ', ' . $key . ':' . $value;
  790. }
  791. if ($d) {
  792. $d = substr($d, 2) . ' - ';
  793. }
  794. return put_link($url, $cat, $title, $kw . ereg_replace('\[((/?(b|big|i|small|sub|sup|u))|br/)\]', '<\\1>', htmlspecialchars($d . $linkdata['description'])) . ($kw ? '</i>' : ''), $linkdata['on_homepage'] ? '1' : '0', $linkdata['hidden'] ? '1' : '0');
  795. // i.e. allow some BBcode tags, e.g. [b]...[/b]
  796. }
  797. /**
  798. * CSV file import functions
  799. * @author René Haentjens , Ghent University
  800. */
  801. function import_csvfile() {
  802. global $catlinkstatus; // Feedback message to user.
  803. if (is_uploaded_file($filespec = $_FILES['import_file']['tmp_name']) && filesize($filespec) && ($myFile = @ fopen($filespec, 'r'))) {
  804. // read first line of file (column names) and find ',' or ';'
  805. $listsep = strpos($colnames = trim(fgets($myFile)), ',') !== false ? ',' : (strpos($colnames, ';') !== false ? ';' : '');
  806. if ($listsep) {
  807. $columns = array_map('strtolower', explode($listsep, $colnames));
  808. if (in_array('url', $columns) && in_array('title', $columns)) {
  809. $stats = array (
  810. 0,
  811. 0,
  812. 0
  813. ); // fails, updates, inserts
  814. // Modified by Ivan Tcholakov, 01-FEB-2010.
  815. //while (($data = fgetcsv($myFile, 32768, $listsep))) {
  816. while (($data = Text::api_fgetcsv($myFile, null, $listsep))) {
  817. //
  818. foreach ($data as $i => & $text) {
  819. $linkdata[$columns[$i]] = $text;
  820. }
  821. $stats[import_link($linkdata)]++;
  822. unset ($linkdata);
  823. }
  824. $catlinkstatus = '';
  825. if ($stats[0]) {
  826. $catlinkstatus .= $stats[0] . ' ' . get_lang('CsvLinesFailed');
  827. }
  828. if ($stats[1]) {
  829. $catlinkstatus .= $stats[1] . ' ' . get_lang('CsvLinesOld');
  830. }
  831. if ($stats[2]) {
  832. $catlinkstatus .= $stats[2] . ' ' . get_lang('CsvLinesNew');
  833. }
  834. } else {
  835. $catlinkstatus = get_lang('CsvFileNoURL') . ($colnames ? get_lang('CsvFileLine1') . htmlspecialchars(substr($colnames, 0, 200)) . '...' : '');
  836. }
  837. } else {
  838. $catlinkstatus = get_lang('CsvFileNoSeps') . ($colnames ? get_lang('CsvFileLine1') . htmlspecialchars(substr($colnames, 0, 200)) . '...' : '');
  839. }
  840. fclose($myFile);
  841. } else {
  842. $catlinkstatus = get_lang('CsvFileNotFound');
  843. }
  844. }
  845. /**
  846. * This function checks if the url is a youtube link
  847. * @author Jorge Frisancho
  848. * @author Julio Montoya - Fixing code
  849. * @version 1.0
  850. */
  851. function is_youtube_link($url) {
  852. $is_youtube_link = strrpos($url, "youtube") || strrpos($url, "youtu.be");
  853. return $is_youtube_link;
  854. }
  855. function get_youtube_video_id($url) {
  856. // This is the length of YouTube's video IDs
  857. $len = 11;
  858. // The ID string starts after "v=", which is usually right after
  859. // "youtube.com/watch?" in the URL
  860. $pos = strpos($url, "v=");
  861. $id = '';
  862. //If false try other options
  863. if ($pos === false) {
  864. $url_parsed = parse_url($url);
  865. //Youtube shortener
  866. //http://youtu.be/ID
  867. $pos = strpos($url, "youtu.be");
  868. if ($pos == false) {
  869. $id = '';
  870. } else {
  871. return substr($url_parsed['path'], 1);
  872. }
  873. //if empty try the youtube.com/embed/ID
  874. if (empty($id)) {
  875. $pos = strpos($url, "embed");
  876. if ($pos === false) {
  877. return '';
  878. } else {
  879. return substr($url_parsed['path'], 7);
  880. }
  881. }
  882. } else {
  883. // Offset the start location to match the beginning of the ID string
  884. $pos += 2;
  885. // Get the ID string and return it
  886. $id = substr($url, $pos, $len);
  887. return $id;
  888. }
  889. }