link.lib.php 40 KB

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