kannelsms.lib.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. <?php
  2. /* For licensing terms, see /vendor/license.txt */
  3. /**
  4. * Class Kannelsms
  5. * This script handles incoming SMS information, process it and sends an SMS if everything is right
  6. *
  7. * @package chamilo.plugin.kannelsms.lib
  8. * @author Imanol Losada <imanol.losada@beeznest.com>
  9. *
  10. * Kannelsms-Chamilo connector class
  11. */
  12. class Kannelsms implements SmsPluginLibraryInterface
  13. {
  14. public $api;
  15. public $hostAddress;
  16. public $username;
  17. public $password;
  18. public $from;
  19. public $plugin_enabled = false;
  20. /**
  21. * Constructor (generates a connection to the API)
  22. * @param string Kannelsms API key required to use the plugin
  23. * @return void
  24. */
  25. public function __construct($apiKey = null)
  26. {
  27. $plugin = KannelsmsPlugin::create();
  28. $kannelSMSPlugin = $plugin->get('tool_enable');
  29. $this->table = Database::get_main_table('user_field_values');
  30. if ($kannelSMSPlugin == true) {
  31. $this->api = new Kannel(' ');
  32. $this->hostAddress = $plugin->get('hostAddress');
  33. $this->username = $plugin->get('username');
  34. $this->password = $plugin->get('password');
  35. $this->from = $plugin->get('from');
  36. $this->plugin_enabled = true;
  37. }
  38. }
  39. /**
  40. * getMobilePhoneNumberById (retrieves a user mobile phone number by user id)
  41. * @param int $userId User id
  42. * @return int User's mobile phone number
  43. */
  44. public function getMobilePhoneNumberById($userId)
  45. {
  46. $mobilePhoneNumberExtraField = new ExtraField('user');
  47. $mobilePhoneNumberExtraField = $mobilePhoneNumberExtraField->get_handler_field_info_by_field_variable('mobile_phone_number');
  48. $mobilePhoneNumberExtraFieldValue = new ExtraFieldValue('user');
  49. $mobilePhoneNumberExtraFieldValue = $mobilePhoneNumberExtraFieldValue->get_values_by_handler_and_field_id(
  50. $userId,
  51. $mobilePhoneNumberExtraField['id']
  52. );
  53. return $mobilePhoneNumberExtraFieldValue['value'];
  54. }
  55. /**
  56. * send (sends an SMS to the user)
  57. * @param array Data needed to send the SMS. It is mandatory to include the
  58. * 'smsType' and 'userId' (or 'mobilePhoneNumber') fields at least.
  59. * More data may be neccesary depending on the message type
  60. * Example: $additional_parameters = array(
  61. * 'smsType' => EXAMPLE_SMS_TYPE,
  62. * 'userId' => $userId,
  63. * 'moreData' => $moreData
  64. * );
  65. * @return void
  66. */
  67. public function send($additionalParameters)
  68. {
  69. $trimmedKey = trim(CONFIG_SECURITY_API_KEY);
  70. if (!empty($trimmedKey)) {
  71. $message = array(
  72. "to" => array_key_exists("mobilePhoneNumber", $additionalParameters) ?
  73. $additionalParameters['mobilePhoneNumber'] : $this->getMobilePhoneNumberById($additionalParameters['userId']),
  74. "message" => $this->getSms($additionalParameters)
  75. );
  76. if (!empty($message['message'])) {
  77. if (extension_loaded('curl')) {
  78. $url = $this->hostAddress.'?username='.
  79. $this->username.'&password='.$this->password.'&from='.
  80. $this->from.'&to='.$message['to'].'&msg='.urlencode($message['message']);
  81. $ch = curl_init($url);
  82. curl_exec($ch);
  83. curl_close($ch);
  84. }
  85. // Commented for future message logging / tracking purposes
  86. /*if( $result["success"] ) {
  87. echo "Message sent - ID: " . $result["id"];
  88. } else {
  89. echo "Message failed - Error: " . $result["error_message"];
  90. }*/
  91. }
  92. }
  93. }
  94. /**
  95. * buildSms (builds an SMS from a template and data)
  96. * @param object KannelsmsPlugin object
  97. * @param object Template object
  98. * @param string Template file name
  99. * @param string Text key from lang file
  100. * @param array Data to fill message variables (if any)
  101. * @return object Template object with message property updated
  102. */
  103. public function buildSms($plugin, $tpl, $templateName, $messageKey, $parameters = null)
  104. {
  105. $result = Database::select(
  106. 'selected_value',
  107. 'settings_current',
  108. array(
  109. 'where'=> array('variable = ?' => array('kannelsms_message'.$messageKey))
  110. )
  111. );
  112. if (empty($result)) {
  113. $tpl->assign('message', '');
  114. } else {
  115. $templatePath = 'kannelsms/sms_templates/';
  116. $content = $tpl->fetch($templatePath.$templateName);
  117. $message = $plugin->get_lang($messageKey);
  118. if ($parameters !== null) {
  119. $message = vsprintf($message, $parameters);
  120. }
  121. $tpl->assign('message', $message);
  122. }
  123. return $tpl->params['message'];
  124. }
  125. /**
  126. * getSms (returns an SMS message depending of its type)
  127. * @param array Data needed to send the SMS. It is mandatory to include the
  128. * 'smsType' and 'userId' (or 'mobilePhoneNumber') fields at least.
  129. * More data may be neccesary depending on the message type
  130. * Example: $additional_parameters = array(
  131. * 'smsType' => EXAMPLE_SMS_TYPE,
  132. * 'userId' => $userId,
  133. * 'moreData' => $moreData
  134. * );
  135. * @return string A ready to be sent SMS
  136. */
  137. public function getSms($additionalParameters)
  138. {
  139. $plugin = KannelsmsPlugin::create();
  140. $tool_name = $plugin->get_lang('plugin_title');
  141. $tpl = new Template($tool_name);
  142. switch ($additionalParameters['smsType']) {
  143. case SmsPlugin::WELCOME_LOGIN_PASSWORD:
  144. $userInfo = api_get_user_info($additionalParameters['userId']);
  145. return $this->buildSms(
  146. $plugin,
  147. $tpl,
  148. 'welcome_login_password.tpl',
  149. 'WelcomeXLoginXPasswordX',
  150. array(
  151. api_get_setting('siteName'),
  152. $userInfo['username'],
  153. $additionalParameters['password']
  154. )
  155. );
  156. break;
  157. case SmsPlugin::NEW_FILE_SHARED_COURSE_BY:
  158. return $this->buildSms(
  159. $plugin,
  160. $tpl,
  161. 'new_file_shared_course_by.tpl',
  162. 'XNewFileSharedCourseXByX',
  163. array(
  164. api_get_setting('siteName'),
  165. $additionalParameters['courseTitle'],
  166. $additionalParameters['userUsername']
  167. )
  168. );
  169. break;
  170. case SmsPlugin::ACCOUNT_APPROVED_CONNECT:
  171. return $this->buildSms(
  172. $plugin,
  173. $tpl,
  174. 'account_approved_connect.tpl',
  175. 'XAccountApprovedConnectX',
  176. array(
  177. api_get_setting('siteName'),
  178. $tpl->params['_p']['web']
  179. )
  180. );
  181. break;
  182. case SmsPlugin::NEW_COURSE_BEEN_CREATED:
  183. return $this->buildSms(
  184. $plugin,
  185. $tpl,
  186. 'new_course_been_created.tpl',
  187. 'XNewCourseXBeenCreatedX',
  188. array(
  189. api_get_setting('siteName'),
  190. $additionalParameters['courseName'],
  191. $additionalParameters['creatorUsername']
  192. )
  193. );
  194. break;
  195. case SmsPlugin::NEW_USER_SUBSCRIBED_COURSE:
  196. return $this->buildSms(
  197. $plugin,
  198. $tpl,
  199. 'new_user_subscribed_course.tpl',
  200. 'XNewUserXSubscribedCourseX',
  201. array(
  202. api_get_setting('siteName'),
  203. $additionalParameters['userUsername'],
  204. $additionalParameters['courseCode']
  205. )
  206. );
  207. break;
  208. case SmsPlugin::NEW_COURSE_SUGGESTED_TEACHER:
  209. return $this->buildSms(
  210. $plugin,
  211. $tpl,
  212. 'new_course_suggested_teacher.tpl',
  213. 'XNewCourseSuggestedTeacherX',
  214. array(
  215. api_get_setting('siteName'),
  216. $additionalParameters['userUsername']
  217. )
  218. );
  219. break;
  220. case SmsPlugin::COURSE_OPENING_REQUEST_CODE_REGISTERED:
  221. return $this->buildSms(
  222. $plugin,
  223. $tpl,
  224. 'course_opening_request_code_registered.tpl',
  225. 'XCourseOpeningRequestCodeXRegistered',
  226. array(
  227. api_get_setting('siteName'),
  228. $additionalParameters['courseCode']
  229. )
  230. );
  231. break;
  232. case SmsPlugin::COURSE_OPENING_REQUEST_CODE_APPROVED:
  233. return $this->buildSms(
  234. $plugin,
  235. $tpl,
  236. 'course_opening_request_course_code_approved.tpl',
  237. 'XCourseOpeningRequestCourseCodeXApproved',
  238. array(
  239. api_get_setting('siteName'),
  240. $additionalParameters['courseCode']
  241. )
  242. );
  243. break;
  244. case SmsPlugin::COURSE_OPENING_REQUEST_CODE_REJECTED:
  245. return $this->buildSms(
  246. $plugin,
  247. $tpl,
  248. 'request_open_course_code_rejected.tpl',
  249. 'XRequestOpenCourseCodeXReject',
  250. array(
  251. api_get_setting('siteName'),
  252. $additionalParameters['courseCode']
  253. )
  254. );
  255. break;
  256. case SmsPlugin::COURSE_OPENING_REQUEST_CODE:
  257. return $this->buildSms(
  258. $plugin,
  259. $tpl,
  260. 'course_opening_request_course_code.tpl',
  261. 'XCourseOpeningRequestCourseCodeX',
  262. array(
  263. api_get_setting('siteName'),
  264. $additionalParameters['courseCode']
  265. )
  266. );
  267. break;
  268. case SmsPlugin::BEEN_SUBSCRIBED_COURSE:
  269. return $this->buildSms(
  270. $plugin,
  271. $tpl,
  272. 'been_subscribed_course.tpl',
  273. 'XBeenSubscribedCourseX',
  274. array(
  275. api_get_setting('siteName'),
  276. $additionalParameters['courseTitle']
  277. )
  278. );
  279. break;
  280. case SmsPlugin::ASSIGNMENT_BEEN_CREATED_COURSE:
  281. return $this->buildSms(
  282. $plugin,
  283. $tpl,
  284. 'assignment_been_created_course.tpl',
  285. 'XAssignmentBeenCreatedCourseX',
  286. array(
  287. api_get_setting('siteName'),
  288. $additionalParameters['courseTitle']
  289. )
  290. );
  291. break;
  292. // Message types to be implemented. Fill the array parameter with arguments.
  293. /*case SmsPlugin::ACCOUNT_CREATED_UPDATED_LOGIN_PASSWORD:
  294. return $this->buildSms(
  295. $plugin,
  296. $tpl,
  297. 'account_created_updated_login_password.tpl',
  298. 'XAccountCreatedUpdatedLoginXPasswordX',
  299. array(
  300. api_get_setting('siteName')
  301. )
  302. );
  303. break;*/
  304. /*case SmsPlugin::PASSWORD_UPDATED_LOGIN_PASSWORD:
  305. return $this->buildSms(
  306. $plugin,
  307. $tpl,
  308. 'password_updated_login_password.tpl',
  309. 'XPasswordUpdatedLoginXPasswordX',
  310. array(
  311. api_get_setting('siteName')
  312. )
  313. );
  314. break;*/
  315. /*case SmsPlugin::REQUESTED_PASSWORD_CHANGE:
  316. return $this->buildSms(
  317. $plugin,
  318. $tpl,
  319. 'requested_password_change.tpl',
  320. 'XPasswordUpdatedLoginXPasswordX',
  321. array(
  322. api_get_setting('siteName')
  323. )
  324. );
  325. break;*/
  326. /*case SmsPlugin::RECEIVED_NEW_PERSONAL_MESSAGES:
  327. return $this->buildSms(
  328. $plugin,
  329. $tpl,
  330. 'received_new_personal_messages.tpl',
  331. 'XReceivedNewPersonalMessages',
  332. array(
  333. api_get_setting('siteName')
  334. )
  335. );
  336. break;*/
  337. /*case SmsPlugin::NEW_USER_PENDING_APPROVAL:
  338. return $this->buildSms(
  339. $plugin,
  340. $tpl,
  341. 'new_user_pending_approval.tpl',
  342. 'XNewUserXPendingApproval',
  343. array(
  344. api_get_setting('siteName')
  345. )
  346. );
  347. break;*/
  348. /*case SmsPlugin::POSTED_FORUM_COURSE:
  349. return $this->buildSms(
  350. $plugin,
  351. $tpl,
  352. 'posted_forum_course.tpl',
  353. 'XXPostedForumXCourseX',
  354. array(
  355. api_get_setting('siteName')
  356. )
  357. );
  358. break;*/
  359. /*case SmsPlugin::CHECK_EMAIL_CONNECT_MORE_INFO:
  360. return $this->buildSms(
  361. $plugin,
  362. $tpl,
  363. 'check_email_connect_more_info.tpl',
  364. 'XXXCheckEmailConnectMoreInfo',
  365. array(
  366. api_get_setting('siteName')
  367. )
  368. );
  369. break;*/
  370. /*case SmsPlugin::STUDENT_ANSWERED_TEST:
  371. return $this->buildSms(
  372. $plugin,
  373. $tpl,
  374. 'student_answered_test.tpl',
  375. 'XXStudentXAnsweredTestX',
  376. array(
  377. api_get_setting('siteName')
  378. )
  379. );
  380. break;*/
  381. /*case SmsPlugin::STUDENT_ANSWERED_TEST_OPEN_QUESTION:
  382. return $this->buildSms(
  383. $plugin,
  384. $tpl,
  385. 'student_answered_test_open_question.tpl',
  386. 'XXStudentXAnsweredTestXOpenQuestion',
  387. array(
  388. api_get_setting('siteName')
  389. )
  390. );
  391. break;*/
  392. /*case SmsPlugin::STUDENT_ANSWERED_TEST_VOICE_QUESTION:
  393. return $this->buildSms(
  394. $plugin,
  395. $tpl,
  396. 'student_answered_test_voice_question.tpl',
  397. 'XXStudentXAnsweredTestXVoiceQuestion',
  398. array(
  399. api_get_setting('siteName')
  400. )
  401. );
  402. break;*/
  403. /*case SmsPlugin::ANSWER_OPEN_QUESTION_TEST_REVIEWED:
  404. return $this->buildSms(
  405. $plugin,
  406. $tpl,
  407. 'answer_open_question_test_reviewed.tpl',
  408. 'XXAnswerOpenQuestionTestXReviewed',
  409. array(
  410. api_get_setting('siteName')
  411. )
  412. );
  413. break;*/
  414. /*case SmsPlugin::NEW_THREAD_STARTED_FORUM:
  415. return $this->buildSms(
  416. $plugin,
  417. $tpl,
  418. 'new_thread_started_forum.tpl',
  419. 'XXNewThreadXStartedForumX',
  420. array(
  421. api_get_setting('siteName')
  422. )
  423. );
  424. break;*/
  425. /*case SmsPlugin::NEW_ANSWER_POSTED_FORUM:
  426. return $this->buildSms(
  427. $plugin,
  428. $tpl,
  429. 'new_answer_posted_forum.tpl',
  430. 'XXNewAnswerPostedXForumX',
  431. array(
  432. api_get_setting('siteName')
  433. )
  434. );
  435. break;*/
  436. /*case SmsPlugin::NEW_SYSTEM_ANNOUNCEMENT_ADDED:
  437. return $this->buildSms(
  438. $plugin,
  439. $tpl,
  440. 'new_system_announcement_added.tpl',
  441. 'XXNewSystemAnnouncementAdded',
  442. array(
  443. api_get_setting('siteName')
  444. )
  445. );
  446. break;*/
  447. /*case SmsPlugin::TEST_NEW_SYSTEM_ANNOUNCEMENT_ADDED:
  448. return $this->buildSms(
  449. $plugin,
  450. $tpl,
  451. 'test_new_system_announcement_added.tpl',
  452. 'XTestXNewSystemAnnouncementAdded',
  453. array(
  454. api_get_setting('siteName')
  455. )
  456. );
  457. break;*/
  458. /*case SmsPlugin::SYSTEM_ANNOUNCEMENT_UPDATE:
  459. return $this->buildSms(
  460. $plugin,
  461. $tpl,
  462. 'system_announcement_update.tpl',
  463. 'XXSystemAnnouncementUpdate',
  464. array(
  465. api_get_setting('siteName')
  466. )
  467. );
  468. break;*/
  469. /*case SmsPlugin::TEST_SYSTEM_ANNOUNCEMENT_UPDATE:
  470. return $this->buildSms(
  471. $plugin,
  472. $tpl,
  473. 'test_system_announcement_update.tpl',
  474. 'XXSystemAnnouncementUpdate',
  475. array(
  476. api_get_setting('siteName')
  477. )
  478. );
  479. break;*/
  480. /*case SmsPlugin::USER_UPLOADED_ASSIGNMENT_COURSE_STUDENT_SUBMITS_PAPER:
  481. return $this->buildSms(
  482. $plugin,
  483. $tpl,
  484. 'user_uploaded_assignment_course_student_submits_paper.tpl',
  485. 'XUserXUploadedAssignmentXCourseXStudentSubmitsPaper',
  486. array(
  487. api_get_setting('siteName')
  488. )
  489. );
  490. break;*/
  491. /*case SmsPlugin::USER_UPLOADED_ASSIGNMENT_CHECK_STUDENT_SUBMITS_PAPER:
  492. return $this->buildSms(
  493. $plugin,
  494. $tpl,
  495. 'user_uploaded_assignment_check_student_submits_paper.tpl',
  496. 'XUserXUploadedAssignmentXCheckXStudentSubmitsPaper',
  497. array(
  498. api_get_setting('siteName')
  499. )
  500. );
  501. break;*/
  502. /*case SmsPlugin::USER_UPLOADED_ASSIGNMENT_COURSE:
  503. return $this->buildSms(
  504. $plugin,
  505. $tpl,
  506. 'user_uploaded_assignment_course.tpl',
  507. 'XUserXUploadedAssignmentXCourseX',
  508. array(
  509. api_get_setting('siteName')
  510. )
  511. );
  512. break;*/
  513. /*case SmsPlugin::USER_UPLOADED_ASSIGNMENT_CHECK:
  514. return $this->buildSms(
  515. $plugin,
  516. $tpl,
  517. 'user_uploaded_assignment_check.tpl',
  518. 'XUserXUploadedAssignmentXCheckX',
  519. array(
  520. api_get_setting('siteName')
  521. )
  522. );
  523. break;*/
  524. /*case SmsPlugin::SUBSCRIBED_SESSION:
  525. return $this->buildSms(
  526. $plugin,
  527. $tpl,
  528. 'subscribed_session.tpl',
  529. 'XSubscribedSessionX',
  530. array(
  531. api_get_setting('siteName')
  532. )
  533. );
  534. break;*/
  535. /*case SmsPlugin::SUBSCRIBED_SESSION_CSV:
  536. return $this->buildSms(
  537. $plugin,
  538. $tpl,
  539. 'subscribed_session_csv.tpl',
  540. 'XSubscribedSessionXCSV',
  541. array(
  542. api_get_setting('siteName')
  543. )
  544. );
  545. break;*/
  546. /*case SmsPlugin::USER_SUGGESTED_BE_FRIENDS:
  547. return $this->buildSms(
  548. $plugin,
  549. $tpl,
  550. 'user_suggested_be_friends.tpl',
  551. 'XUserXSuggestedBeFriends',
  552. array(
  553. api_get_setting('siteName')
  554. )
  555. );
  556. break;*/
  557. /*case SmsPlugin::USER_ANSWERED_INBOX_MESSAGE:
  558. return $this->buildSms(
  559. $plugin,
  560. $tpl,
  561. 'user_answered_inbox_message.tpl',
  562. 'XUserXAnsweredInboxMessage',
  563. array(
  564. api_get_setting('siteName')
  565. )
  566. );
  567. break;*/
  568. /*case SmsPlugin::BEEN_INVITED_JOIN_GROUP:
  569. return $this->buildSms(
  570. $plugin,
  571. $tpl,
  572. 'been_invited_join_group.tpl',
  573. 'XBeenInvitedJoinGroupX',
  574. array(
  575. api_get_setting('siteName')
  576. )
  577. );
  578. break;*/
  579. /*case SmsPlugin::MESSAGES_SENT_EDITED_GROUP_EDITED:
  580. return $this->buildSms(
  581. $plugin,
  582. $tpl,
  583. 'messages_sent_edited_group_edited.tpl',
  584. 'XMessagesSentEditedGroupXEdited',
  585. array(
  586. api_get_setting('siteName')
  587. )
  588. );
  589. break;*/
  590. /*case SmsPlugin::MESSAGES_SENT_EDITED_GROUP_ADDED:
  591. return $this->buildSms(
  592. $plugin,
  593. $tpl,
  594. 'messages_sent_edited_group_added.tpl',
  595. 'XMessagesSentEditedGroupXAdded',
  596. array(
  597. api_get_setting('siteName')
  598. )
  599. );
  600. break;*/
  601. /*case SmsPlugin::BEEN_INVITED_COMPLETE_SURVEY_COURSE:
  602. return $this->buildSms(
  603. $plugin,
  604. $tpl,
  605. 'been_invited_complete_survey_course.tpl',
  606. 'XBeenInvitedCompleteSurveyXCourseX',
  607. array(
  608. api_get_setting('siteName')
  609. )
  610. );
  611. break;*/
  612. /*case SmsPlugin::REMINDER_ASSIGNMENT_COURSE_DUE:
  613. return $this->buildSms(
  614. $plugin,
  615. $tpl,
  616. 'reminder_assignment_course_due.tpl',
  617. 'XReminderAssignmentXCourseXDue',
  618. array(
  619. api_get_setting('siteName')
  620. )
  621. );
  622. break;*/
  623. /*case SmsPlugin::USER_DETAILS_MODIFIED:
  624. return $this->buildSms(
  625. $plugin,
  626. $tpl,
  627. 'user_details_modified.tpl',
  628. 'XUserDetailsModified',
  629. array(
  630. api_get_setting('siteName')
  631. )
  632. );
  633. break;*/
  634. default:
  635. return '';
  636. }
  637. }
  638. }