link.lib.php 40 KB

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