link.lib.php 41 KB

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