link.lib.php 41 KB

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