link.lib.php 40 KB

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