PageRenderTime 27ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/auth-1.0/libraries/drivers/Auth/Tiki.php

https://github.com/robertleeplummerjr/bluebox
PHP | 565 lines | 361 code | 75 blank | 129 comment | 18 complexity | eee84822b38f81f21a8d69fdd4e274b8 MD5 | raw file
  1. <?php
  2. ini_set('error_reporting', E_ALL);
  3. ini_set('display_errors', 1);
  4. class Http_Exception extends Exception{
  5. const NOT_MODIFIED = 304;
  6. const BAD_REQUEST = 400;
  7. const NOT_FOUND = 404;
  8. const NOT_ALOWED = 405;
  9. const CONFLICT = 409;
  10. const PRECONDITION_FAILED = 412;
  11. const INTERNAL_ERROR = 500;
  12. }
  13. class Http_Stat
  14. {
  15. private $_status = null;
  16. private $_type = null;
  17. private $_url = null;
  18. private $_params = null;
  19. private $_success = null;
  20. private $_data = null;
  21. function __construct($status, $type, $url, $params, $success = false, $data = null)
  22. {
  23. $this->_status = $status;
  24. $this->_type = $type;
  25. $this->_url = $url;
  26. $this->_params = $params;
  27. $this->_success = $success;
  28. $this->_data = $data;
  29. }
  30. function getStatus()
  31. {
  32. return $this->_status;
  33. }
  34. function getType()
  35. {
  36. return $this->_type;
  37. }
  38. function getUrl()
  39. {
  40. return $this->_url;
  41. }
  42. function getParams()
  43. {
  44. return $this->_params;
  45. }
  46. function getSuccess()
  47. {
  48. return $this->_success;
  49. }
  50. function getData()
  51. {
  52. return $this->_data;
  53. }
  54. }
  55. class Http
  56. {
  57. private $_host = null;
  58. private $_port = null;
  59. private $_user = null;
  60. private $_pass = null;
  61. private $_protocol = null;
  62. const HTTP = 'http';
  63. const HTTPS = 'https';
  64. private $_connMultiple = false;
  65. /**
  66. * Factory of the class. Lazy connect
  67. *
  68. * @param string $host
  69. * @param integer $port
  70. * @param string $user
  71. * @param string $pass
  72. * @return Http
  73. */
  74. static public function connect($host, $port = 80, $protocol = self::HTTP)
  75. {
  76. return new self($host, $port, $protocol, false);
  77. }
  78. /**
  79. *
  80. * @return Http
  81. */
  82. static public function multiConnect()
  83. {
  84. return new self(null, null, null, true);
  85. }
  86. private $_append = array();
  87. public function add($http)
  88. {
  89. $this->_append[] = $http;
  90. return $this;
  91. }
  92. private $_silentMode = false;
  93. /**
  94. *
  95. * @param bool $mode
  96. * @return Http
  97. */
  98. public function silentMode($mode=true)
  99. {
  100. $this->_silentMode = $mode;
  101. return $this;
  102. }
  103. protected function __construct($host, $port, $protocol, $connMultiple)
  104. {
  105. $this->_connMultiple = $connMultiple;
  106. $this->_host = $host;
  107. $this->_port = $port;
  108. $this->_protocol = $protocol;
  109. }
  110. public function setCredentials($user, $pass)
  111. {
  112. $this->_user = $user;
  113. $this->_pass = $pass;
  114. }
  115. const POST = 'POST';
  116. const GET = 'GET';
  117. const DELETE = 'DELETE';
  118. private $_requests = array();
  119. /**
  120. * @param string $url
  121. * @param array $params
  122. * @return Http
  123. */
  124. public function post($url, $params=array())
  125. {
  126. $this->_requests[] = array(self::POST, $this->_url($url), $params);
  127. return $this;
  128. }
  129. /**
  130. * @param string $url
  131. * @param array $params
  132. * @return Http
  133. */
  134. public function get($url, $params=array())
  135. {
  136. $this->_requests[] = array(self::GET, $this->_url($url), $params);
  137. return $this;
  138. }
  139. /**
  140. * @param string $url
  141. * @param array $params
  142. * @return Http
  143. */
  144. public function delete($url, $params=array())
  145. {
  146. $this->_requests[] = array(self::DELETE, $this->_url($url), $params);
  147. return $this;
  148. }
  149. public function _getRequests()
  150. {
  151. return $this->_requests;
  152. }
  153. /**
  154. * POST request
  155. *
  156. * @param string $url
  157. * @param array $params
  158. * @return string
  159. */
  160. public function doPost($url, $params=array())
  161. {
  162. return $this->_exec(self::POST, $this->_url($url), $params);
  163. }
  164. /**
  165. * GET Request
  166. *
  167. * @param string $url
  168. * @param array $params
  169. * @return string
  170. */
  171. public function doGet($url, $params=array())
  172. {
  173. return $this->_exec(self::GET, $this->_url($url), $params);
  174. }
  175. /**
  176. * DELETE Request
  177. *
  178. * @param string $url
  179. * @param array $params
  180. * @return string
  181. */
  182. public function doDelete($url, $params=array())
  183. {
  184. return $this->_exec(self::DELETE, $this->_url($url), $params);
  185. }
  186. private $_headers = array();
  187. /**
  188. * setHeaders
  189. *
  190. * @param array $headers
  191. * @return Http
  192. */
  193. public function setHeaders($headers)
  194. {
  195. $this->_headers = $headers;
  196. return $this;
  197. }
  198. /**
  199. * Builds absolute url
  200. *
  201. * @param unknown_type $url
  202. * @return unknown
  203. */
  204. private function _url($url=null)
  205. {
  206. return "{$this->_protocol}://{$this->_host}:{$this->_port}/{$url}";
  207. }
  208. const HTTP_OK = 200;
  209. const HTTP_CREATED = 201;
  210. const HTTP_ACEPTED = 202;
  211. /**
  212. * Performing the real request
  213. *
  214. * @param string $type
  215. * @param string $url
  216. * @param array $params
  217. * @return string
  218. */
  219. private function _exec($type, $url, $params = array())
  220. {
  221. $headers = $this->_headers;
  222. $s = curl_init();
  223. if(!is_null($this->_user)){
  224. curl_setopt($s, CURLOPT_USERPWD, $this->_user.':'.$this->_pass);
  225. }
  226. switch ($type) {
  227. case self::DELETE:
  228. curl_setopt($s, CURLOPT_URL, $url . '?' . http_build_query($params));
  229. curl_setopt($s, CURLOPT_CUSTOMREQUEST, self::DELETE);
  230. break;
  231. case self::POST:
  232. curl_setopt($s, CURLOPT_URL, $url);
  233. curl_setopt($s, CURLOPT_POST, true);
  234. curl_setopt($s, CURLOPT_POSTFIELDS, $params);
  235. break;
  236. case self::GET:
  237. curl_setopt($s, CURLOPT_URL, $url . '?' . http_build_query($params));
  238. break;
  239. }
  240. curl_setopt($s, CURLOPT_RETURNTRANSFER, true);
  241. curl_setopt($s, CURLOPT_HTTPHEADER, $headers);
  242. $_out = curl_exec($s);
  243. $status = curl_getinfo($s, CURLINFO_HTTP_CODE);
  244. curl_close($s);
  245. switch ($status) {
  246. case self::HTTP_OK:
  247. case self::HTTP_CREATED:
  248. case self::HTTP_ACEPTED:
  249. $out = $_out;
  250. break;
  251. default:
  252. if (!$this->_silentMode) {
  253. throw new Http_Exception("http error: {$status}", $status);
  254. }
  255. }
  256. return $out;
  257. }
  258. public function run()
  259. {
  260. if ($this->_connMultiple) {
  261. return $this->_runMultiple();
  262. } else {
  263. return $this->_run();
  264. }
  265. }
  266. private function _runMultiple()
  267. {
  268. $out= null;
  269. if (count($this->_append) > 0) {
  270. $arr = array();
  271. foreach ($this->_append as $_append) {
  272. $arr = array_merge($arr, $_append->_getRequests());
  273. }
  274. $this->_requests = $arr;
  275. $out = $this->_run();
  276. }
  277. return $out;
  278. }
  279. private function _run()
  280. {
  281. $headers = $this->_headers;
  282. $curly = $result = array();
  283. $mh = curl_multi_init();
  284. foreach ($this->_requests as $id => $reg) {
  285. $curly[$id] = curl_init();
  286. $type = $reg[0];
  287. $url = $reg[1];
  288. $params = $reg[2];
  289. if(!is_null($this->_user)){
  290. curl_setopt($curly[$id], CURLOPT_USERPWD, $this->_user.':'.$this->_pass);
  291. }
  292. switch ($type) {
  293. case self::DELETE:
  294. curl_setopt($curly[$id], CURLOPT_URL, $url . '?' . http_build_query($params));
  295. curl_setopt($curly[$id], CURLOPT_CUSTOMREQUEST, self::DELETE);
  296. break;
  297. case self::POST:
  298. curl_setopt($curly[$id], CURLOPT_URL, $url);
  299. curl_setopt($curly[$id], CURLOPT_POST, true);
  300. curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $params);
  301. break;
  302. case self::GET:
  303. curl_setopt($curly[$id], CURLOPT_URL, $url . '?' . http_build_query($params));
  304. break;
  305. }
  306. curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, true);
  307. curl_setopt($curly[$id], CURLOPT_HTTPHEADER, $headers);
  308. curl_multi_add_handle($mh, $curly[$id]);
  309. }
  310. $running = null;
  311. do {
  312. curl_multi_exec($mh, $running);
  313. sleep(0.2);
  314. } while($running > 0);
  315. foreach($curly as $id => $c) {
  316. $status = curl_getinfo($c, CURLINFO_HTTP_CODE);
  317. switch ($status) {
  318. case self::HTTP_OK:
  319. case self::HTTP_CREATED:
  320. case self::HTTP_ACEPTED:
  321. $result[$id] = new Http_Stat($status, $type, $url, $params, true, curl_multi_getcontent($c));
  322. break;
  323. default:
  324. if (!$this->_silentMode) {
  325. $result[$id] = new Http_Stat($status, $type, $url, $params, false);
  326. }
  327. }
  328. curl_multi_remove_handle($mh, $c);
  329. }
  330. curl_multi_close($mh);
  331. return $result;
  332. }
  333. }
  334. defined('SYSPATH') OR die('No direct access allowed.');
  335. /**
  336. * Tiki Auth driver, relies on Doctrine driver
  337. * Note: this Auth driver does not support roles nor auto-login.
  338. *
  339. * $Id: File.php 3917 2009-04-02 03:06:22Z zombor $
  340. *
  341. * @package Auth
  342. * @author Robert Plummer
  343. * @copyright (c) 2009 Darren Schreiber
  344. * @license Mozilla Public License (MPL) v1.1
  345. */
  346. class Auth_Tiki_Driver extends Auth_Driver
  347. {
  348. /**
  349. * VERSION
  350. */
  351. const VERSION = '0.1';
  352. /**
  353. * Constructor loads the user list into the class.
  354. */
  355. public function __construct(array $config, $passwordOrig = '')
  356. {
  357. parent::__construct($config);
  358. include_once("Doctrine.php");
  359. $this->doctrine = new Auth_Doctrine_Driver($config);
  360. if (empty($passwordOrig) && !empty($_REQUEST['login']['password'])) {
  361. $passwordOrig = $_REQUEST['login']['password'];
  362. }
  363. $this->passwordOrig = $passwordOrig;
  364. }
  365. /**
  366. * Logs a user in.
  367. *
  368. * @param string username
  369. * @param string password
  370. * @param boolean enable auto-login (not supported)
  371. * @return boolean
  372. */
  373. public function login($username, $password, $remember, $requirePassword = TRUE)
  374. {
  375. $client = Http::connect('bluebox.tikisuite.org');
  376. $client->setCredentials($username, $this->passwordOrig);
  377. $status = $client->get('/tiki/tiki-index.php')->run();
  378. $tikiLogin = FALSE;
  379. if (isset($status[0])) {
  380. $tikiLogin = $status[0]->getSuccess();
  381. }
  382. if ($tikiLogin != TRUE) return $tikiLogin;
  383. //at this point the user exists in tiki
  384. $userExists = (strlen($this->password($username)) > 0 ? TRUE : FALSE);
  385. if (empty($userExists) && !empty($password)) {
  386. $userId = $this->createUser($username, $password);
  387. $deviceId = $this->createDevice(1, $username, $this->passwordOrig);
  388. $numberId = $this->createNumber($deviceId, $username);
  389. }
  390. return $this->complete_login($username);
  391. //we don't need to verify password here, it was done above from tiki server
  392. //return $this->doctrine->login($username, $password, $remember, $requirePassword);
  393. }
  394. /**
  395. * Forces a user to be logged in, without specifying a password.
  396. *
  397. * @param mixed username
  398. * @return boolean
  399. */
  400. public function force_login($username)
  401. {
  402. return $this->doctrine->force_login($username);
  403. }
  404. /**
  405. * Get the stored password for a username.
  406. *
  407. * @param mixed username
  408. * @return string
  409. */
  410. public function password($username)
  411. {
  412. return $this->doctrine->password($username);
  413. }
  414. /**
  415. * Generate and return a token that allows for resetting of a user's password
  416. * @param string $username Username of the user to reset
  417. * @return string Returns a token/hash that should be sent to a user via a confirmed method to allow for reset
  418. */
  419. public function resetToken($username)
  420. {
  421. return $this->doctrine->resetToken($username);
  422. }
  423. /**
  424. * Force a new password to be recorded, if a password reset token matches the stored token previously generated.
  425. * @param string $username
  426. * @param string $token Password token, generated and recorded by resetToken()
  427. * @param string $newPassword
  428. * @return boolean True if reset is successful
  429. */
  430. public function resetPassword($username, $token, $newPassword)
  431. {
  432. return $this->doctrine->resetPassword($username, $token, $newPassword);
  433. }
  434. public function getRealIpAddr()
  435. {
  436. return $this->doctrine->getRealIpAddr();
  437. }
  438. private function createUser($username, $password) {
  439. $emailParts = explode('@', $username);
  440. $user = new User();
  441. $user['first_name'] = $emailParts[0];
  442. $user['last_name'] = 'Tiki';
  443. $user['username'] = $username;
  444. $user['email_address'] = $username;
  445. $user['password'] = $password;
  446. $user['user_type'] = User::TYPE_NORMAL_USER;
  447. $user['location_id'] = 1;
  448. $user['account_id'] = 1;
  449. //save user
  450. Doctrine::getTable('User')->getRecordListener()->get('MultiTenant')->setOption('disabled', TRUE);
  451. $userId = $user->save();
  452. Doctrine::getTable('User')->getRecordListener()->get('MultiTenant')->setOption('disabled', FALSE);
  453. return $userId;
  454. }
  455. private function createDevice($userId, $username, $password) {
  456. $emailParts = explode('@', $username);
  457. $device = new Device();
  458. $device['name'] = $emailParts[0];
  459. $device['user_id'] = $userId;
  460. $device['context_id'] = 1;
  461. $device['account_id'] = 1;
  462. $device['type'] = "SipDevice";
  463. $device['plugins'] = array(
  464. 'sip'=>array(
  465. 'username'=> $username,
  466. 'password'=> $password,
  467. ),
  468. );
  469. //save device
  470. Doctrine::getTable('Device')->getRecordListener()->get('MultiTenant')->setOption('disabled', TRUE);
  471. $deviceId = $device->save();
  472. Doctrine::getTable('Device')->getRecordListener()->get('MultiTenant')->setOption('disabled', FALSE);
  473. return $deviceId;
  474. }
  475. private function createNumber($deviceId, $username) {
  476. $emailParts = explode('@', $username);
  477. $number = new Number();
  478. $number['number'] = $emailParts[0];
  479. $number['type'] = Number::TYPE_INTERNAL;
  480. $number['status'] = Number::STATUS_NORMAL;
  481. $number['location_id'] = 1;
  482. $number['account_id'] = 1;
  483. $number['foreign_id'] = $deviceId;
  484. //save number
  485. Doctrine::getTable('Number')->getRecordListener()->get('MultiTenant')->setOption('disabled', TRUE);
  486. $numberId = $number->save();
  487. Doctrine::getTable('Number')->getRecordListener()->get('MultiTenant')->setOption('disabled', FALSE);
  488. return $numberId;
  489. }
  490. } // End Auth_File_Driver