kannelsms.lib.php 24 KB

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